blob: 088641ba7a8e6d19ad604d669905697814d1f719 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Arne Jansena2de7332011-03-08 14:14:00 +01002/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01004 */
5
Arne Jansena2de7332011-03-08 14:14:00 +01006#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +02007#include <linux/ratelimit.h>
David Sterbade2491f2017-05-31 19:21:38 +02008#include <linux/sched/mm.h>
Johannes Thumshirnd5178572019-06-03 16:58:57 +02009#include <crypto/hash.h>
Arne Jansena2de7332011-03-08 14:14:00 +010010#include "ctree.h"
Dennis Zhou6e80d4f2019-12-13 16:22:15 -080011#include "discard.h"
Arne Jansena2de7332011-03-08 14:14:00 +010012#include "volumes.h"
13#include "disk-io.h"
14#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020015#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020016#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020017#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010018#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040020#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050021#include "raid56.h"
Josef Bacikaac00232019-06-20 15:37:44 -040022#include "block-group.h"
Naohiro Aota12659252020-11-10 20:26:14 +090023#include "zoned.h"
Arne Jansena2de7332011-03-08 14:14:00 +010024
25/*
26 * This is only the first step towards a full-features scrub. It reads all
27 * extent and super block and verifies the checksums. In case a bad checksum
28 * is found or the extent cannot be read, good data will be written back if
29 * any can be found.
30 *
31 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010032 * - In case an unrepairable extent is encountered, track which files are
33 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010034 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010035 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010036 */
37
Stefan Behrensb5d67f62012-03-27 14:21:27 -040038struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010039struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010040
Stefan Behrensff023aa2012-11-06 11:43:11 +010041/*
42 * the following three values only influence the performance.
43 * The last one configures the number of parallel and outstanding I/O
44 * operations. The first two values configure an upper limit for the number
45 * of (dynamically allocated) pages that are added to a bio.
46 */
47#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
48#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
49#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010050
51/*
52 * the following value times PAGE_SIZE needs to be large enough to match the
53 * largest node/leaf/sector size that shall be supported.
54 * Values larger than BTRFS_STRIPE_LEN are not supported.
55 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040056#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010057
Miao Xieaf8e2d12014-10-23 14:42:50 +080058struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020059 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080060 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080061 u64 map_length;
62};
63
Arne Jansena2de7332011-03-08 14:14:00 +010064struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040065 struct scrub_block *sblock;
66 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020067 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080068 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010069 u64 flags; /* extent flags */
70 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040071 u64 logical;
72 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010073 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080074 atomic_t refs;
Qu Wenruo2c363952020-11-13 20:51:44 +080075 u8 mirror_num;
76 int have_csum:1;
77 int io_error:1;
Arne Jansena2de7332011-03-08 14:14:00 +010078 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080079
80 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010081};
82
83struct scrub_bio {
84 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010085 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010086 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010087 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020088 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010089 u64 logical;
90 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010091#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
92 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
93#else
94 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
95#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040096 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010097 int next_free;
98 struct btrfs_work work;
99};
100
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400101struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100102 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400103 int page_count;
104 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200105 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100106 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800107 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400108 struct {
109 unsigned int header_error:1;
110 unsigned int checksum_error:1;
111 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200112 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800113
114 /* The following is for the data used to check parity */
115 /* It is for the data with checksum */
116 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400117 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700118 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400119};
120
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800121/* Used for the chunks with parity stripe such RAID5/6 */
122struct scrub_parity {
123 struct scrub_ctx *sctx;
124
125 struct btrfs_device *scrub_dev;
126
127 u64 logic_start;
128
129 u64 logic_end;
130
131 int nsectors;
132
Qu Wenruofa485d22020-12-02 14:48:07 +0800133 u32 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800134
Elena Reshetova78a76452017-03-03 10:55:24 +0200135 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800136
137 struct list_head spages;
138
139 /* Work of parity check and repair */
140 struct btrfs_work work;
141
142 /* Mark the parity blocks which have data */
143 unsigned long *dbitmap;
144
145 /*
146 * Mark the parity blocks which have data, but errors happen when
147 * read data or check data
148 */
149 unsigned long *ebitmap;
150
Gustavo A. R. Silvaa8753ee2020-03-06 16:13:33 -0600151 unsigned long bitmap[];
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800152};
153
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100154struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100155 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400156 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100157 int first_free;
158 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100159 atomic_t bios_in_flight;
160 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100161 spinlock_t list_lock;
162 wait_queue_head_t list_wait;
Arne Jansena2de7332011-03-08 14:14:00 +0100163 struct list_head csum_list;
164 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100165 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100166 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100167
David Sterbaeb3b5052019-10-09 13:58:13 +0200168 /* State of IO submission throttling affecting the associated device */
169 ktime_t throttle_deadline;
170 u64 throttle_sent;
171
Stefan Behrens63a212a2012-11-05 18:29:28 +0100172 int is_dev_replace;
Naohiro Aotade17add2021-02-04 19:22:13 +0900173 u64 write_pointer;
David Sterba3fb99302017-05-16 19:10:32 +0200174
175 struct scrub_bio *wr_curr_bio;
176 struct mutex wr_lock;
177 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
David Sterba3fb99302017-05-16 19:10:32 +0200178 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200179 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100180
Arne Jansena2de7332011-03-08 14:14:00 +0100181 /*
182 * statistics
183 */
184 struct btrfs_scrub_progress stat;
185 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000186
187 /*
188 * Use a ref counter to avoid use-after-free issues. Scrub workers
189 * decrement bios_in_flight and workers_pending and then do a wakeup
190 * on the list_wait wait queue. We must ensure the main scrub task
191 * doesn't free the scrub context before or while the workers are
192 * doing the wakeup() call.
193 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200194 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100195};
196
Jan Schmidt558540c2011-06-13 19:59:12 +0200197struct scrub_warning {
198 struct btrfs_path *path;
199 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200200 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200201 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200202 u64 logical;
203 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200204};
205
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800206struct full_stripe_lock {
207 struct rb_node node;
208 u64 logical;
209 u64 refs;
210 struct mutex mutex;
211};
212
Zhao Leibe50a8d2015-01-20 15:11:42 +0800213static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100215static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800216 struct scrub_block *sblock,
217 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800218static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400219static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800220 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400221static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
222 struct scrub_block *sblock_good,
223 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100224static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
225static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
226 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400227static int scrub_checksum_data(struct scrub_block *sblock);
228static int scrub_checksum_tree_block(struct scrub_block *sblock);
229static int scrub_checksum_super(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400230static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100231static void scrub_page_get(struct scrub_page *spage);
232static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800233static void scrub_parity_get(struct scrub_parity *sparity);
234static void scrub_parity_put(struct scrub_parity *sparity);
Qu Wenruofa485d22020-12-02 14:48:07 +0800235static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100236 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +0800237 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100238 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200239static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400240static void scrub_bio_end_io_worker(struct btrfs_work *work);
241static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100242static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +0800243 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100244 u64 *extent_physical,
245 struct btrfs_device **extent_dev,
246 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100247static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
248 struct scrub_page *spage);
249static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200250static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100251static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000252static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400253
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800254static inline int scrub_is_page_on_raid56(struct scrub_page *spage)
Liu Bo762221f2018-01-02 13:36:42 -0700255{
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800256 return spage->recover &&
257 (spage->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Liu Bo762221f2018-01-02 13:36:42 -0700258}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400259
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100260static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
261{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200262 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100263 atomic_inc(&sctx->bios_in_flight);
264}
265
266static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
267{
268 atomic_dec(&sctx->bios_in_flight);
269 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000270 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100271}
272
Wang Shilongcb7ab022013-12-04 21:16:53 +0800273static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800274{
275 while (atomic_read(&fs_info->scrub_pause_req)) {
276 mutex_unlock(&fs_info->scrub_lock);
277 wait_event(fs_info->scrub_pause_wait,
278 atomic_read(&fs_info->scrub_pause_req) == 0);
279 mutex_lock(&fs_info->scrub_lock);
280 }
281}
282
Zhaolei0e22be82015-08-05 16:43:28 +0800283static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800284{
285 atomic_inc(&fs_info->scrubs_paused);
286 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800287}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800288
Zhaolei0e22be82015-08-05 16:43:28 +0800289static void scrub_pause_off(struct btrfs_fs_info *fs_info)
290{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800291 mutex_lock(&fs_info->scrub_lock);
292 __scrub_blocked_if_needed(fs_info);
293 atomic_dec(&fs_info->scrubs_paused);
294 mutex_unlock(&fs_info->scrub_lock);
295
296 wake_up(&fs_info->scrub_pause_wait);
297}
298
Zhaolei0e22be82015-08-05 16:43:28 +0800299static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
300{
301 scrub_pause_on(fs_info);
302 scrub_pause_off(fs_info);
303}
304
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100305/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800306 * Insert new full stripe lock into full stripe locks tree
307 *
308 * Return pointer to existing or newly inserted full_stripe_lock structure if
309 * everything works well.
310 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
311 *
312 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
313 * function
314 */
315static struct full_stripe_lock *insert_full_stripe_lock(
316 struct btrfs_full_stripe_locks_tree *locks_root,
317 u64 fstripe_logical)
318{
319 struct rb_node **p;
320 struct rb_node *parent = NULL;
321 struct full_stripe_lock *entry;
322 struct full_stripe_lock *ret;
323
David Sterbaa32bf9a2018-03-16 02:21:22 +0100324 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800325
326 p = &locks_root->root.rb_node;
327 while (*p) {
328 parent = *p;
329 entry = rb_entry(parent, struct full_stripe_lock, node);
330 if (fstripe_logical < entry->logical) {
331 p = &(*p)->rb_left;
332 } else if (fstripe_logical > entry->logical) {
333 p = &(*p)->rb_right;
334 } else {
335 entry->refs++;
336 return entry;
337 }
338 }
339
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000340 /*
341 * Insert new lock.
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000342 */
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800343 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
344 if (!ret)
345 return ERR_PTR(-ENOMEM);
346 ret->logical = fstripe_logical;
347 ret->refs = 1;
348 mutex_init(&ret->mutex);
349
350 rb_link_node(&ret->node, parent, p);
351 rb_insert_color(&ret->node, &locks_root->root);
352 return ret;
353}
354
355/*
356 * Search for a full stripe lock of a block group
357 *
358 * Return pointer to existing full stripe lock if found
359 * Return NULL if not found
360 */
361static struct full_stripe_lock *search_full_stripe_lock(
362 struct btrfs_full_stripe_locks_tree *locks_root,
363 u64 fstripe_logical)
364{
365 struct rb_node *node;
366 struct full_stripe_lock *entry;
367
David Sterbaa32bf9a2018-03-16 02:21:22 +0100368 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800369
370 node = locks_root->root.rb_node;
371 while (node) {
372 entry = rb_entry(node, struct full_stripe_lock, node);
373 if (fstripe_logical < entry->logical)
374 node = node->rb_left;
375 else if (fstripe_logical > entry->logical)
376 node = node->rb_right;
377 else
378 return entry;
379 }
380 return NULL;
381}
382
383/*
384 * Helper to get full stripe logical from a normal bytenr.
385 *
386 * Caller must ensure @cache is a RAID56 block group.
387 */
David Sterba32da53862019-10-29 19:20:18 +0100388static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr)
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800389{
390 u64 ret;
391
392 /*
393 * Due to chunk item size limit, full stripe length should not be
394 * larger than U32_MAX. Just a sanity check here.
395 */
396 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
397
398 /*
399 * round_down() can only handle power of 2, while RAID56 full
400 * stripe length can be 64KiB * n, so we need to manually round down.
401 */
David Sterbab3470b52019-10-23 18:48:22 +0200402 ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) *
403 cache->full_stripe_len + cache->start;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800404 return ret;
405}
406
407/*
408 * Lock a full stripe to avoid concurrency of recovery and read
409 *
410 * It's only used for profiles with parities (RAID5/6), for other profiles it
411 * does nothing.
412 *
413 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
414 * So caller must call unlock_full_stripe() at the same context.
415 *
416 * Return <0 if encounters error.
417 */
418static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
419 bool *locked_ret)
420{
David Sterba32da53862019-10-29 19:20:18 +0100421 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800422 struct btrfs_full_stripe_locks_tree *locks_root;
423 struct full_stripe_lock *existing;
424 u64 fstripe_start;
425 int ret = 0;
426
427 *locked_ret = false;
428 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
429 if (!bg_cache) {
430 ASSERT(0);
431 return -ENOENT;
432 }
433
434 /* Profiles not based on parity don't need full stripe lock */
435 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
436 goto out;
437 locks_root = &bg_cache->full_stripe_locks_root;
438
439 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
440
441 /* Now insert the full stripe lock */
442 mutex_lock(&locks_root->lock);
443 existing = insert_full_stripe_lock(locks_root, fstripe_start);
444 mutex_unlock(&locks_root->lock);
445 if (IS_ERR(existing)) {
446 ret = PTR_ERR(existing);
447 goto out;
448 }
449 mutex_lock(&existing->mutex);
450 *locked_ret = true;
451out:
452 btrfs_put_block_group(bg_cache);
453 return ret;
454}
455
456/*
457 * Unlock a full stripe.
458 *
459 * NOTE: Caller must ensure it's the same context calling corresponding
460 * lock_full_stripe().
461 *
462 * Return 0 if we unlock full stripe without problem.
463 * Return <0 for error
464 */
465static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
466 bool locked)
467{
David Sterba32da53862019-10-29 19:20:18 +0100468 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800469 struct btrfs_full_stripe_locks_tree *locks_root;
470 struct full_stripe_lock *fstripe_lock;
471 u64 fstripe_start;
472 bool freeit = false;
473 int ret = 0;
474
475 /* If we didn't acquire full stripe lock, no need to continue */
476 if (!locked)
477 return 0;
478
479 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
480 if (!bg_cache) {
481 ASSERT(0);
482 return -ENOENT;
483 }
484 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
485 goto out;
486
487 locks_root = &bg_cache->full_stripe_locks_root;
488 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
489
490 mutex_lock(&locks_root->lock);
491 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
492 /* Unpaired unlock_full_stripe() detected */
493 if (!fstripe_lock) {
494 WARN_ON(1);
495 ret = -ENOENT;
496 mutex_unlock(&locks_root->lock);
497 goto out;
498 }
499
500 if (fstripe_lock->refs == 0) {
501 WARN_ON(1);
502 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
503 fstripe_lock->logical);
504 } else {
505 fstripe_lock->refs--;
506 }
507
508 if (fstripe_lock->refs == 0) {
509 rb_erase(&fstripe_lock->node, &locks_root->root);
510 freeit = true;
511 }
512 mutex_unlock(&locks_root->lock);
513
514 mutex_unlock(&fstripe_lock->mutex);
515 if (freeit)
516 kfree(fstripe_lock);
517out:
518 btrfs_put_block_group(bg_cache);
519 return ret;
520}
521
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100522static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100523{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100524 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100525 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100526 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100527 struct btrfs_ordered_sum, list);
528 list_del(&sum->list);
529 kfree(sum);
530 }
531}
532
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100533static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100534{
535 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100536
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100537 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100538 return;
539
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400540 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100541 if (sctx->curr != -1) {
542 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400543
544 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100545 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400546 scrub_block_put(sbio->pagev[i]->sblock);
547 }
548 bio_put(sbio->bio);
549 }
550
Stefan Behrensff023aa2012-11-06 11:43:11 +0100551 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100552 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100553
554 if (!sbio)
555 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100556 kfree(sbio);
557 }
558
David Sterba3fb99302017-05-16 19:10:32 +0200559 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100560 scrub_free_csums(sctx);
561 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100562}
563
Filipe Mananaf55985f2015-02-09 21:14:24 +0000564static void scrub_put_ctx(struct scrub_ctx *sctx)
565{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200566 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000567 scrub_free_ctx(sctx);
568}
569
David Sterba92f7ba42018-12-04 16:11:55 +0100570static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
571 struct btrfs_fs_info *fs_info, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100572{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100573 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100574 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100575
David Sterba58c4e172016-02-11 10:49:42 +0100576 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100577 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100578 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200579 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100580 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200581 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100582 sctx->curr = -1;
David Sterba92f7ba42018-12-04 16:11:55 +0100583 sctx->fs_info = fs_info;
Dan Robertsone49be142019-02-19 02:56:43 +0000584 INIT_LIST_HEAD(&sctx->csum_list);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100585 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100586 struct scrub_bio *sbio;
587
David Sterba58c4e172016-02-11 10:49:42 +0100588 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100589 if (!sbio)
590 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100591 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100592
Arne Jansena2de7332011-03-08 14:14:00 +0100593 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100594 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400595 sbio->page_count = 0;
Omar Sandovala0cac0e2019-09-16 11:30:57 -0700596 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL,
597 NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100598
Stefan Behrensff023aa2012-11-06 11:43:11 +0100599 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100600 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200601 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100602 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100603 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100604 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100605 atomic_set(&sctx->bios_in_flight, 0);
606 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100607 atomic_set(&sctx->cancel_req, 0);
Arne Jansena2de7332011-03-08 14:14:00 +0100608
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100609 spin_lock_init(&sctx->list_lock);
610 spin_lock_init(&sctx->stat_lock);
611 init_waitqueue_head(&sctx->list_wait);
David Sterbaeb3b5052019-10-09 13:58:13 +0200612 sctx->throttle_deadline = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100613
David Sterba3fb99302017-05-16 19:10:32 +0200614 WARN_ON(sctx->wr_curr_bio != NULL);
615 mutex_init(&sctx->wr_lock);
616 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200617 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200618 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200619 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200620 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200621 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100622 }
David Sterba8fcdac32017-05-16 19:10:23 +0200623
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100624 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100625
626nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100627 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100628 return ERR_PTR(-ENOMEM);
629}
630
Stefan Behrensff023aa2012-11-06 11:43:11 +0100631static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
632 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200633{
Jan Schmidt558540c2011-06-13 19:59:12 +0200634 u32 nlink;
635 int ret;
636 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200637 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200638 struct extent_buffer *eb;
639 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100640 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400641 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200642 struct inode_fs_paths *ipath = NULL;
643 struct btrfs_root *local_root;
David Sterba1d4c08e2015-01-02 19:36:14 +0100644 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200645
David Sterba56e93572020-05-15 19:35:55 +0200646 local_root = btrfs_get_fs_root(fs_info, root, true);
Jan Schmidt558540c2011-06-13 19:59:12 +0200647 if (IS_ERR(local_root)) {
648 ret = PTR_ERR(local_root);
649 goto err;
650 }
651
David Sterba14692cc2015-01-02 18:55:46 +0100652 /*
653 * this makes the path point to (inum INODE_ITEM ioff)
654 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100655 key.objectid = inum;
656 key.type = BTRFS_INODE_ITEM_KEY;
657 key.offset = 0;
658
659 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200660 if (ret) {
Josef Bacik00246522020-01-24 09:33:01 -0500661 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200662 btrfs_release_path(swarn->path);
663 goto err;
664 }
665
666 eb = swarn->path->nodes[0];
667 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
668 struct btrfs_inode_item);
Jan Schmidt558540c2011-06-13 19:59:12 +0200669 nlink = btrfs_inode_nlink(eb, inode_item);
670 btrfs_release_path(swarn->path);
671
David Sterbade2491f2017-05-31 19:21:38 +0200672 /*
673 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
674 * uses GFP_NOFS in this context, so we keep it consistent but it does
675 * not seem to be strictly necessary.
676 */
677 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200678 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200679 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300680 if (IS_ERR(ipath)) {
Josef Bacik00246522020-01-24 09:33:01 -0500681 btrfs_put_root(local_root);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300682 ret = PTR_ERR(ipath);
683 ipath = NULL;
684 goto err;
685 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200686 ret = paths_from_inode(inum, ipath);
687
688 if (ret < 0)
689 goto err;
690
691 /*
692 * we deliberately ignore the bit ipath might have been too small to
693 * hold all of the paths here
694 */
695 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400696 btrfs_warn_in_rcu(fs_info,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800697"%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %u, links %u (path: %s)",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400698 swarn->errstr, swarn->logical,
699 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200700 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400701 root, inum, offset,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800702 fs_info->sectorsize, nlink,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400703 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200704
Josef Bacik00246522020-01-24 09:33:01 -0500705 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200706 free_ipath(ipath);
707 return 0;
708
709err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400710 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200711 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400712 swarn->errstr, swarn->logical,
713 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200714 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400715 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200716
717 free_ipath(ipath);
718 return 0;
719}
720
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400721static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200722{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100723 struct btrfs_device *dev;
724 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200725 struct btrfs_path *path;
726 struct btrfs_key found_key;
727 struct extent_buffer *eb;
728 struct btrfs_extent_item *ei;
729 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200730 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100731 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600732 u64 flags = 0;
733 u64 ref_root;
734 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300735 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600736 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200737
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100738 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100739 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400740 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100741
Jan Schmidt558540c2011-06-13 19:59:12 +0200742 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200743 if (!path)
744 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200745
David Sterba6aa21262017-10-04 17:07:07 +0200746 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100747 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200748 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100749 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200750
Liu Bo69917e42012-09-07 20:01:28 -0600751 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
752 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200753 if (ret < 0)
754 goto out;
755
Jan Schmidt4692cf52011-12-02 14:56:41 +0100756 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200757 swarn.extent_item_size = found_key.offset;
758
759 eb = path->nodes[0];
760 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
761 item_size = btrfs_item_size_nr(eb, path->slots[0]);
762
Liu Bo69917e42012-09-07 20:01:28 -0600763 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200764 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800765 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
766 item_size, &ref_root,
767 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200768 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200769"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400770 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400771 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200772 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200773 ref_level ? "node" : "leaf",
774 ret < 0 ? -1 : ref_level,
775 ret < 0 ? -1 : ref_root);
776 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600777 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200778 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600779 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200780 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100781 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100782 iterate_extent_inodes(fs_info, found_key.objectid,
783 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400784 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200785 }
786
787out:
788 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200789}
790
Miao Xieaf8e2d12014-10-23 14:42:50 +0800791static inline void scrub_get_recover(struct scrub_recover *recover)
792{
Elena Reshetova6f615012017-03-03 10:55:21 +0200793 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800794}
795
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800796static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
797 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +0800798{
Elena Reshetova6f615012017-03-03 10:55:21 +0200799 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800800 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +0800801 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800802 kfree(recover);
803 }
804}
805
Arne Jansena2de7332011-03-08 14:14:00 +0100806/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400807 * scrub_handle_errored_block gets called when either verification of the
808 * pages failed or the bio failed to read, e.g. with EIO. In the latter
809 * case, this function handles all pages in the bio, even though only one
810 * may be bad.
811 * The goal of this function is to repair the errored block by using the
812 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100813 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400814static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100815{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100816 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100817 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400818 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400820 unsigned int failed_mirror_index;
821 unsigned int is_metadata;
822 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400823 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
824 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100825 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400826 int mirror_index;
827 int page_num;
828 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +0800829 bool full_stripe_locked;
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000830 unsigned int nofs_flag;
David Sterba8bb1cf12020-08-17 12:12:38 +0200831 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400832 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100833
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400834 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400835 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000836 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
837 /*
838 * if we find an error in a super block, we just report it.
839 * They will get written with the next transaction commit
840 * anyway
841 */
842 spin_lock(&sctx->stat_lock);
843 ++sctx->stat.super_errors;
844 spin_unlock(&sctx->stat_lock);
845 return 0;
846 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100847 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100848 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
849 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
850 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400851 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100852 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100853 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400854
Naohiro Aotaf7ef5282021-02-04 19:22:16 +0900855 if (btrfs_is_zoned(fs_info) && !sctx->is_dev_replace)
856 return btrfs_repair_one_zone(fs_info, logical);
857
Qu Wenruo28d70e22017-04-14 08:35:55 +0800858 /*
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000859 * We must use GFP_NOFS because the scrub task might be waiting for a
860 * worker task executing this function and in turn a transaction commit
861 * might be waiting the scrub task to pause (which needs to wait for all
862 * the worker tasks to complete before pausing).
863 * We do allocations in the workers through insert_full_stripe_lock()
864 * and scrub_add_page_to_wr_bio(), which happens down the call chain of
865 * this function.
866 */
867 nofs_flag = memalloc_nofs_save();
868 /*
Qu Wenruo28d70e22017-04-14 08:35:55 +0800869 * For RAID5/6, race can happen for a different device scrub thread.
870 * For data corruption, Parity and Data threads will both try
871 * to recovery the data.
872 * Race can lead to doubly added csum error, or even unrecoverable
873 * error.
874 */
875 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
876 if (ret < 0) {
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000877 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +0800878 spin_lock(&sctx->stat_lock);
879 if (ret == -ENOMEM)
880 sctx->stat.malloc_errors++;
881 sctx->stat.read_errors++;
882 sctx->stat.uncorrectable_errors++;
883 spin_unlock(&sctx->stat_lock);
884 return ret;
885 }
886
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400887 /*
888 * read all mirrors one after the other. This includes to
889 * re-read the extent or metadata block that failed (that was
890 * the cause that this fixup code is called) another time,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800891 * sector by sector this time in order to know which sectors
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400892 * caused I/O errors and which ones are good (for all mirrors).
893 * It is the goal to handle the situation when more than one
894 * mirror contains I/O errors, but the errors do not
895 * overlap, i.e. the data can be repaired by selecting the
Qu Wenruo8df507c2021-04-22 19:02:46 +0800896 * sectors from those mirrors without I/O error on the
897 * particular sectors. One example (with blocks >= 2 * sectorsize)
898 * would be that mirror #1 has an I/O error on the first sector,
899 * the second sector is good, and mirror #2 has an I/O error on
900 * the second sector, but the first sector is good.
901 * Then the first sector of the first mirror can be repaired by
902 * taking the first sector of the second mirror, and the
903 * second sector of the second mirror can be repaired by
904 * copying the contents of the 2nd sector of the 1st mirror.
905 * One more note: if the sectors of one mirror contain I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400906 * errors, the checksum cannot be verified. In order to get
907 * the best data for repairing, the first attempt is to find
908 * a mirror without I/O errors and with a validated checksum.
Qu Wenruo8df507c2021-04-22 19:02:46 +0800909 * Only if this is not possible, the sectors are picked from
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400910 * mirrors with I/O errors without considering the checksum.
911 * If the latter is the case, at the end, the checksum of the
912 * repaired area is verified in order to correctly maintain
913 * the statistics.
914 */
915
David Sterba31e818f2015-02-20 18:00:26 +0100916 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000917 sizeof(*sblocks_for_recheck), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400918 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100919 spin_lock(&sctx->stat_lock);
920 sctx->stat.malloc_errors++;
921 sctx->stat.read_errors++;
922 sctx->stat.uncorrectable_errors++;
923 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100924 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400925 goto out;
926 }
927
928 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800929 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400930 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100931 spin_lock(&sctx->stat_lock);
932 sctx->stat.read_errors++;
933 sctx->stat.uncorrectable_errors++;
934 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100935 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400936 goto out;
937 }
938 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
939 sblock_bad = sblocks_for_recheck + failed_mirror_index;
940
941 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +0800942 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400943
944 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
945 sblock_bad->no_io_error_seen) {
946 /*
947 * the error disappeared after reading page by page, or
948 * the area was part of a huge bio and other parts of the
949 * bio caused I/O errors, or the block layer merged several
950 * read requests into one and the error is caused by a
951 * different bio (usually one of the two latter cases is
952 * the cause)
953 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100954 spin_lock(&sctx->stat_lock);
955 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800956 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100957 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958
Stefan Behrensff023aa2012-11-06 11:43:11 +0100959 if (sctx->is_dev_replace)
960 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 goto out;
962 }
963
964 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100965 spin_lock(&sctx->stat_lock);
966 sctx->stat.read_errors++;
967 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200968 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100970 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400971 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100972 spin_lock(&sctx->stat_lock);
973 sctx->stat.csum_errors++;
974 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200975 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100977 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200978 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400979 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100980 spin_lock(&sctx->stat_lock);
981 sctx->stat.verify_errors++;
982 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200983 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400984 scrub_print_warning("checksum/header error",
985 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200986 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100987 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200988 BTRFS_DEV_STAT_GENERATION_ERRS);
989 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100990 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200991 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400992 }
993
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200994 if (sctx->readonly) {
995 ASSERT(!sctx->is_dev_replace);
996 goto out;
997 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400998
Qu Wenruo665d4952018-07-11 13:41:21 +0800999 /*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001001 * checksums.
1002 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001003 * errors and also does not have a checksum error.
1004 * If one is found, and if a checksum is present, the full block
1005 * that is known to contain an error is rewritten. Afterwards
1006 * the block is known to be corrected.
1007 * If a mirror is found which is completely correct, and no
1008 * checksum is present, only those pages are rewritten that had
1009 * an I/O error in the block to be repaired, since it cannot be
1010 * determined, which copy of the other pages is better (and it
1011 * could happen otherwise that a correct page would be
1012 * overwritten by a bad one).
1013 */
Liu Bo762221f2018-01-02 13:36:42 -07001014 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001015 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001016
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001017 if (mirror_index == failed_mirror_index)
1018 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001019
1020 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1021 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1022 if (mirror_index >= BTRFS_MAX_MIRRORS)
1023 break;
1024 if (!sblocks_for_recheck[mirror_index].page_count)
1025 break;
1026
1027 sblock_other = sblocks_for_recheck + mirror_index;
1028 } else {
1029 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1030 int max_allowed = r->bbio->num_stripes -
1031 r->bbio->num_tgtdevs;
1032
1033 if (mirror_index >= max_allowed)
1034 break;
1035 if (!sblocks_for_recheck[1].page_count)
1036 break;
1037
1038 ASSERT(failed_mirror_index == 0);
1039 sblock_other = sblocks_for_recheck + 1;
1040 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1041 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001042
1043 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001044 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001045
1046 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001047 !sblock_other->checksum_error &&
1048 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001049 if (sctx->is_dev_replace) {
1050 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001051 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001052 } else {
1053 ret = scrub_repair_block_from_good_copy(
1054 sblock_bad, sblock_other);
1055 if (!ret)
1056 goto corrected_error;
1057 }
Arne Jansena2de7332011-03-08 14:14:00 +01001058 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001059 }
1060
Zhao Leib968fed2015-01-20 15:11:41 +08001061 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1062 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001063
1064 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001065 * In case of I/O errors in the area that is supposed to be
Qu Wenruo8df507c2021-04-22 19:02:46 +08001066 * repaired, continue by picking good copies of those sectors.
1067 * Select the good sectors from mirrors to rewrite bad sectors from
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001068 * the area to fix. Afterwards verify the checksum of the block
1069 * that is supposed to be repaired. This verification step is
1070 * only done for the purpose of statistic counting and for the
1071 * final scrub report, whether errors remain.
1072 * A perfect algorithm could make use of the checksum and try
Qu Wenruo8df507c2021-04-22 19:02:46 +08001073 * all possible combinations of sectors from the different mirrors
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001074 * until the checksum verification succeeds. For example, when
Qu Wenruo8df507c2021-04-22 19:02:46 +08001075 * the 2nd sector of mirror #1 faces I/O errors, and the 2nd sector
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001076 * of mirror #2 is readable but the final checksum test fails,
Qu Wenruo8df507c2021-04-22 19:02:46 +08001077 * then the 2nd sector of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001078 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001079 * exception and is therefore not implemented. At least it is
1080 * avoided that the good copy is overwritten.
1081 * A more useful improvement would be to pick the sectors
1082 * without I/O error based on sector sizes (512 bytes on legacy
Qu Wenruo8df507c2021-04-22 19:02:46 +08001083 * disks) instead of on sectorsize. Then maybe 512 byte of one
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001084 * mirror could be repaired by taking 512 byte of a different
Qu Wenruo8df507c2021-04-22 19:02:46 +08001085 * mirror, even if other 512 byte sectors in the same sectorsize
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001086 * area are unreadable.
1087 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001089 for (page_num = 0; page_num < sblock_bad->page_count;
1090 page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001091 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001092 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001093
Zhao Leib968fed2015-01-20 15:11:41 +08001094 /* skip no-io-error page in scrub */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001095 if (!spage_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001096 continue;
1097
Liu Bo47597002018-03-02 16:10:41 -07001098 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1099 /*
1100 * In case of dev replace, if raid56 rebuild process
1101 * didn't work out correct data, then copy the content
1102 * in sblock_bad to make sure target device is identical
1103 * to source device, instead of writing garbage data in
1104 * sblock_for_recheck array to target device.
1105 */
1106 sblock_other = NULL;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001107 } else if (spage_bad->io_error) {
Liu Bo47597002018-03-02 16:10:41 -07001108 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001109 for (mirror_index = 0;
1110 mirror_index < BTRFS_MAX_MIRRORS &&
1111 sblocks_for_recheck[mirror_index].page_count > 0;
1112 mirror_index++) {
1113 if (!sblocks_for_recheck[mirror_index].
1114 pagev[page_num]->io_error) {
1115 sblock_other = sblocks_for_recheck +
1116 mirror_index;
1117 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001118 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001119 }
Zhao Leib968fed2015-01-20 15:11:41 +08001120 if (!sblock_other)
1121 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001122 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001123
Zhao Leib968fed2015-01-20 15:11:41 +08001124 if (sctx->is_dev_replace) {
1125 /*
1126 * did not find a mirror to fetch the page
1127 * from. scrub_write_page_to_dev_replace()
1128 * handles this case (page->io_error), by
1129 * filling the block with zeros before
1130 * submitting the write request
1131 */
1132 if (!sblock_other)
1133 sblock_other = sblock_bad;
1134
1135 if (scrub_write_page_to_dev_replace(sblock_other,
1136 page_num) != 0) {
David Sterbae37abe92018-04-04 17:20:52 +02001137 atomic64_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001138 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001139 success = 0;
1140 }
1141 } else if (sblock_other) {
1142 ret = scrub_repair_page_from_good_copy(sblock_bad,
1143 sblock_other,
1144 page_num, 0);
1145 if (0 == ret)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001146 spage_bad->io_error = 0;
Zhao Leib968fed2015-01-20 15:11:41 +08001147 else
1148 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001149 }
1150 }
1151
Zhao Leib968fed2015-01-20 15:11:41 +08001152 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001153 if (is_metadata || have_csum) {
1154 /*
1155 * need to verify the checksum now that all
1156 * sectors on disk are repaired (the write
1157 * request for data to be repaired is on its way).
1158 * Just be lazy and use scrub_recheck_block()
1159 * which re-reads the data before the checksum
1160 * is verified, but most likely the data comes out
1161 * of the page cache.
1162 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001163 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001164 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001165 !sblock_bad->checksum_error &&
1166 sblock_bad->no_io_error_seen)
1167 goto corrected_error;
1168 else
1169 goto did_not_correct_error;
1170 } else {
1171corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001172 spin_lock(&sctx->stat_lock);
1173 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001174 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001175 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001176 btrfs_err_rl_in_rcu(fs_info,
1177 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001178 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001179 }
1180 } else {
1181did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001182 spin_lock(&sctx->stat_lock);
1183 sctx->stat.uncorrectable_errors++;
1184 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001185 btrfs_err_rl_in_rcu(fs_info,
1186 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001187 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001188 }
1189
1190out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001191 if (sblocks_for_recheck) {
1192 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1193 mirror_index++) {
1194 struct scrub_block *sblock = sblocks_for_recheck +
1195 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001196 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001197 int page_index;
1198
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001199 for (page_index = 0; page_index < sblock->page_count;
1200 page_index++) {
1201 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001202 recover = sblock->pagev[page_index]->recover;
1203 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001204 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001205 sblock->pagev[page_index]->recover =
1206 NULL;
1207 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001208 scrub_page_put(sblock->pagev[page_index]);
1209 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001210 }
1211 kfree(sblocks_for_recheck);
1212 }
1213
Qu Wenruo28d70e22017-04-14 08:35:55 +08001214 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
Filipe Manana7c3c7cb2018-12-07 13:23:32 +00001215 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +08001216 if (ret < 0)
1217 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001219}
1220
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001221static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001222{
Zhao Lei10f11902015-01-20 15:11:43 +08001223 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1224 return 2;
1225 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1226 return 3;
1227 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001228 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001229}
1230
Zhao Lei10f11902015-01-20 15:11:43 +08001231static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1232 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001233 u64 mapped_length,
1234 int nstripes, int mirror,
1235 int *stripe_index,
1236 u64 *stripe_offset)
1237{
1238 int i;
1239
Zhao Leiffe2d202015-01-20 15:11:44 +08001240 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001241 /* RAID5/6 */
1242 for (i = 0; i < nstripes; i++) {
1243 if (raid_map[i] == RAID6_Q_STRIPE ||
1244 raid_map[i] == RAID5_P_STRIPE)
1245 continue;
1246
1247 if (logical >= raid_map[i] &&
1248 logical < raid_map[i] + mapped_length)
1249 break;
1250 }
1251
1252 *stripe_index = i;
1253 *stripe_offset = logical - raid_map[i];
1254 } else {
1255 /* The other RAID type */
1256 *stripe_index = mirror;
1257 *stripe_offset = 0;
1258 }
1259}
1260
Zhao Leibe50a8d2015-01-20 15:11:42 +08001261static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001262 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001263{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001264 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001265 struct btrfs_fs_info *fs_info = sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001266 u64 length = original_sblock->page_count * fs_info->sectorsize;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001267 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001268 u64 generation = original_sblock->pagev[0]->generation;
1269 u64 flags = original_sblock->pagev[0]->flags;
1270 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001271 struct scrub_recover *recover;
1272 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001273 u64 sublen;
1274 u64 mapped_length;
1275 u64 stripe_offset;
1276 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001277 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001278 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001279 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280 int ret;
1281
1282 /*
Zhao Lei57019342015-01-20 15:11:45 +08001283 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001284 * are not used (and not set) in the blocks that are used for
1285 * the recheck procedure
1286 */
1287
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 while (length > 0) {
Qu Wenruo8df507c2021-04-22 19:02:46 +08001289 sublen = min_t(u64, length, fs_info->sectorsize);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001290 mapped_length = sublen;
1291 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001292
1293 /*
Qu Wenruo8df507c2021-04-22 19:02:46 +08001294 * With a length of sectorsize, each returned stripe represents
1295 * one mirror
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001296 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001297 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001298 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001299 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001300 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001301 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001302 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001303 return -EIO;
1304 }
1305
Miao Xieaf8e2d12014-10-23 14:42:50 +08001306 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1307 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001308 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001309 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001310 return -ENOMEM;
1311 }
1312
Elena Reshetova6f615012017-03-03 10:55:21 +02001313 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001314 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001315 recover->map_length = mapped_length;
1316
Ashish Samant24731142016-04-29 18:33:59 -07001317 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001318
Zhao Leibe50a8d2015-01-20 15:11:42 +08001319 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001320
Miao Xieaf8e2d12014-10-23 14:42:50 +08001321 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001322 mirror_index++) {
1323 struct scrub_block *sblock;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001324 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001326 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001327 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001328
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001329 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1330 if (!spage) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001331leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001332 spin_lock(&sctx->stat_lock);
1333 sctx->stat.malloc_errors++;
1334 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001335 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001336 return -ENOMEM;
1337 }
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001338 scrub_page_get(spage);
1339 sblock->pagev[page_index] = spage;
1340 spage->sblock = sblock;
1341 spage->flags = flags;
1342 spage->generation = generation;
1343 spage->logical = logical;
1344 spage->have_csum = have_csum;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001345 if (have_csum)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001346 memcpy(spage->csum,
Zhao Lei4734b7e2015-08-19 22:39:18 +08001347 original_sblock->pagev[0]->csum,
David Sterba2ae0c2d2020-06-30 17:44:49 +02001348 sctx->fs_info->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001349
Zhao Lei10f11902015-01-20 15:11:43 +08001350 scrub_stripe_index_and_offset(logical,
1351 bbio->map_type,
1352 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001353 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001354 bbio->num_stripes -
1355 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001356 mirror_index,
1357 &stripe_index,
1358 &stripe_offset);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001359 spage->physical = bbio->stripes[stripe_index].physical +
Miao Xieaf8e2d12014-10-23 14:42:50 +08001360 stripe_offset;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001361 spage->dev = bbio->stripes[stripe_index].dev;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001362
Stefan Behrensff023aa2012-11-06 11:43:11 +01001363 BUG_ON(page_index >= original_sblock->page_count);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001364 spage->physical_for_dev_replace =
Stefan Behrensff023aa2012-11-06 11:43:11 +01001365 original_sblock->pagev[page_index]->
1366 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001367 /* for missing devices, dev->bdev is NULL */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001368 spage->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001369 sblock->page_count++;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001370 spage->page = alloc_page(GFP_NOFS);
1371 if (!spage->page)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001372 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001373
1374 scrub_get_recover(recover);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001375 spage->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001376 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001377 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001378 length -= sublen;
1379 logical += sublen;
1380 page_index++;
1381 }
1382
1383 return 0;
1384}
1385
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001386static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001387{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001388 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001389}
1390
Miao Xieaf8e2d12014-10-23 14:42:50 +08001391static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1392 struct bio *bio,
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001393 struct scrub_page *spage)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001394{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001395 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001396 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001397 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001398
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001399 bio->bi_iter.bi_sector = spage->logical >> 9;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001400 bio->bi_private = &done;
1401 bio->bi_end_io = scrub_bio_wait_endio;
1402
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001403 mirror_num = spage->sblock->pagev[0]->mirror_num;
1404 ret = raid56_parity_recover(fs_info, bio, spage->recover->bbio,
1405 spage->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001406 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001407 if (ret)
1408 return ret;
1409
Liu Bob4ff5ad2017-11-30 17:26:39 -07001410 wait_for_completion_io(&done);
1411 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001412}
1413
Liu Bo6ca17652018-03-07 12:08:09 -07001414static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1415 struct scrub_block *sblock)
1416{
1417 struct scrub_page *first_page = sblock->pagev[0];
1418 struct bio *bio;
1419 int page_num;
1420
1421 /* All pages in sblock belong to the same stripe on the same device. */
1422 ASSERT(first_page->dev);
1423 if (!first_page->dev->bdev)
1424 goto out;
1425
Christoph Hellwiga8affc02021-03-11 12:01:37 +01001426 bio = btrfs_io_bio_alloc(BIO_MAX_VECS);
Liu Bo6ca17652018-03-07 12:08:09 -07001427 bio_set_dev(bio, first_page->dev->bdev);
1428
1429 for (page_num = 0; page_num < sblock->page_count; page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001430 struct scrub_page *spage = sblock->pagev[page_num];
Liu Bo6ca17652018-03-07 12:08:09 -07001431
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001432 WARN_ON(!spage->page);
1433 bio_add_page(bio, spage->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001434 }
1435
1436 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1437 bio_put(bio);
1438 goto out;
1439 }
1440
1441 bio_put(bio);
1442
1443 scrub_recheck_block_checksum(sblock);
1444
1445 return;
1446out:
1447 for (page_num = 0; page_num < sblock->page_count; page_num++)
1448 sblock->pagev[page_num]->io_error = 1;
1449
1450 sblock->no_io_error_seen = 0;
1451}
1452
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001453/*
1454 * this function will check the on disk data for checksum errors, header
1455 * errors and read I/O errors. If any I/O errors happen, the exact pages
1456 * which are errored are marked as being bad. The goal is to enable scrub
1457 * to take those pages that are not errored from all the mirrors so that
1458 * the pages that are errored in the just handled mirror can be repaired.
1459 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001460static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001461 struct scrub_block *sblock,
1462 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001463{
1464 int page_num;
1465
1466 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001467
Liu Bo6ca17652018-03-07 12:08:09 -07001468 /* short cut for raid56 */
1469 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1470 return scrub_recheck_block_on_raid56(fs_info, sblock);
1471
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001472 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1473 struct bio *bio;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001474 struct scrub_page *spage = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001475
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001476 if (spage->dev->bdev == NULL) {
1477 spage->io_error = 1;
Stefan Behrensea9947b2012-05-04 15:16:07 -04001478 sblock->no_io_error_seen = 0;
1479 continue;
1480 }
1481
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001482 WARN_ON(!spage->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001483 bio = btrfs_io_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001484 bio_set_dev(bio, spage->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001485
Qu Wenruo8df507c2021-04-22 19:02:46 +08001486 bio_add_page(bio, spage->page, fs_info->sectorsize, 0);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001487 bio->bi_iter.bi_sector = spage->physical >> 9;
Liu Bo6ca17652018-03-07 12:08:09 -07001488 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001489
Liu Bo6ca17652018-03-07 12:08:09 -07001490 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001491 spage->io_error = 1;
Liu Bo6ca17652018-03-07 12:08:09 -07001492 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001493 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001494
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001495 bio_put(bio);
1496 }
1497
1498 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001499 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001500}
1501
Miao Xie17a9be22014-07-24 11:37:08 +08001502static inline int scrub_check_fsid(u8 fsid[],
1503 struct scrub_page *spage)
1504{
1505 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1506 int ret;
1507
Anand Jain44880fd2017-07-29 17:50:09 +08001508 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001509 return !ret;
1510}
1511
Zhao Leiba7cf982015-08-24 21:18:02 +08001512static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001513{
Zhao Leiba7cf982015-08-24 21:18:02 +08001514 sblock->header_error = 0;
1515 sblock->checksum_error = 0;
1516 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001517
Zhao Leiba7cf982015-08-24 21:18:02 +08001518 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1519 scrub_checksum_data(sblock);
1520 else
1521 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001522}
1523
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001524static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001525 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001526{
1527 int page_num;
1528 int ret = 0;
1529
1530 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1531 int ret_sub;
1532
1533 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1534 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001535 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001536 if (ret_sub)
1537 ret = ret_sub;
1538 }
1539
1540 return ret;
1541}
1542
1543static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1544 struct scrub_block *sblock_good,
1545 int page_num, int force_write)
1546{
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001547 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
1548 struct scrub_page *spage_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001549 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001550 const u32 sectorsize = fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001551
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001552 BUG_ON(spage_bad->page == NULL);
1553 BUG_ON(spage_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001554 if (force_write || sblock_bad->header_error ||
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001555 sblock_bad->checksum_error || spage_bad->io_error) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001556 struct bio *bio;
1557 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001558
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001559 if (!spage_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001560 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001561 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001562 return -EIO;
1563 }
1564
David Sterbac5e4c3d2017-06-12 17:29:41 +02001565 bio = btrfs_io_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001566 bio_set_dev(bio, spage_bad->dev->bdev);
1567 bio->bi_iter.bi_sector = spage_bad->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001568 bio->bi_opf = REQ_OP_WRITE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001569
Qu Wenruo8df507c2021-04-22 19:02:46 +08001570 ret = bio_add_page(bio, spage_good->page, sectorsize, 0);
1571 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 bio_put(bio);
1573 return -EIO;
1574 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001575
Mike Christie4e49ea42016-06-05 14:31:41 -05001576 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001577 btrfs_dev_stat_inc_and_print(spage_bad->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001578 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterbae37abe92018-04-04 17:20:52 +02001579 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001580 bio_put(bio);
1581 return -EIO;
1582 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 bio_put(bio);
1584 }
1585
1586 return 0;
1587}
1588
Stefan Behrensff023aa2012-11-06 11:43:11 +01001589static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1590{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001591 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001592 int page_num;
1593
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001594 /*
1595 * This block is used for the check of the parity on the source device,
1596 * so the data needn't be written into the destination device.
1597 */
1598 if (sblock->sparity)
1599 return;
1600
Stefan Behrensff023aa2012-11-06 11:43:11 +01001601 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1602 int ret;
1603
1604 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1605 if (ret)
David Sterbae37abe92018-04-04 17:20:52 +02001606 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001607 }
1608}
1609
1610static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1611 int page_num)
1612{
1613 struct scrub_page *spage = sblock->pagev[page_num];
1614
1615 BUG_ON(spage->page == NULL);
David Sterbaa8b3a892020-05-29 15:26:07 +02001616 if (spage->io_error)
1617 clear_page(page_address(spage->page));
Stefan Behrensff023aa2012-11-06 11:43:11 +01001618
Stefan Behrensff023aa2012-11-06 11:43:11 +01001619 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1620}
1621
Naohiro Aotade17add2021-02-04 19:22:13 +09001622static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
1623{
1624 int ret = 0;
1625 u64 length;
1626
1627 if (!btrfs_is_zoned(sctx->fs_info))
1628 return 0;
1629
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09001630 if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
1631 return 0;
1632
Naohiro Aotade17add2021-02-04 19:22:13 +09001633 if (sctx->write_pointer < physical) {
1634 length = physical - sctx->write_pointer;
1635
1636 ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
1637 sctx->write_pointer, length);
1638 if (!ret)
1639 sctx->write_pointer = physical;
1640 }
1641 return ret;
1642}
1643
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1645 struct scrub_page *spage)
1646{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001647 struct scrub_bio *sbio;
1648 int ret;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001649 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001650
David Sterba3fb99302017-05-16 19:10:32 +02001651 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001652again:
David Sterba3fb99302017-05-16 19:10:32 +02001653 if (!sctx->wr_curr_bio) {
1654 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001655 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001656 if (!sctx->wr_curr_bio) {
1657 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001658 return -ENOMEM;
1659 }
David Sterba3fb99302017-05-16 19:10:32 +02001660 sctx->wr_curr_bio->sctx = sctx;
1661 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001662 }
David Sterba3fb99302017-05-16 19:10:32 +02001663 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001664 if (sbio->page_count == 0) {
1665 struct bio *bio;
1666
Naohiro Aotade17add2021-02-04 19:22:13 +09001667 ret = fill_writer_pointer_gap(sctx,
1668 spage->physical_for_dev_replace);
1669 if (ret) {
1670 mutex_unlock(&sctx->wr_lock);
1671 return ret;
1672 }
1673
Stefan Behrensff023aa2012-11-06 11:43:11 +01001674 sbio->physical = spage->physical_for_dev_replace;
1675 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001676 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001677 bio = sbio->bio;
1678 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001679 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001680 sbio->bio = bio;
1681 }
1682
1683 bio->bi_private = sbio;
1684 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001685 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001686 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001687 bio->bi_opf = REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001688 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001689 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001690 spage->physical_for_dev_replace ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08001691 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001692 spage->logical) {
1693 scrub_wr_submit(sctx);
1694 goto again;
1695 }
1696
Qu Wenruo8df507c2021-04-22 19:02:46 +08001697 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
1698 if (ret != sectorsize) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001699 if (sbio->page_count < 1) {
1700 bio_put(sbio->bio);
1701 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001702 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001703 return -EIO;
1704 }
1705 scrub_wr_submit(sctx);
1706 goto again;
1707 }
1708
1709 sbio->pagev[sbio->page_count] = spage;
1710 scrub_page_get(spage);
1711 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001712 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001713 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001714 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001715
1716 return 0;
1717}
1718
1719static void scrub_wr_submit(struct scrub_ctx *sctx)
1720{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001721 struct scrub_bio *sbio;
1722
David Sterba3fb99302017-05-16 19:10:32 +02001723 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001724 return;
1725
David Sterba3fb99302017-05-16 19:10:32 +02001726 sbio = sctx->wr_curr_bio;
1727 sctx->wr_curr_bio = NULL;
Christoph Hellwig309dca302021-01-24 11:02:34 +01001728 WARN_ON(!sbio->bio->bi_bdev);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001729 scrub_pending_bio_inc(sctx);
1730 /* process all writes in a single worker thread. Then the block layer
1731 * orders the requests before sending them to the driver which
1732 * doubled the write performance on spinning disks when measured
1733 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001734 btrfsic_submit_bio(sbio->bio);
Naohiro Aotade17add2021-02-04 19:22:13 +09001735
1736 if (btrfs_is_zoned(sctx->fs_info))
Qu Wenruo8df507c2021-04-22 19:02:46 +08001737 sctx->write_pointer = sbio->physical + sbio->page_count *
1738 sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001739}
1740
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001741static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001742{
1743 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001744 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001745
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001746 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001747 sbio->bio = bio;
1748
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001749 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001750 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001751}
1752
1753static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1754{
1755 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1756 struct scrub_ctx *sctx = sbio->sctx;
1757 int i;
1758
1759 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001760 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001761 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001762 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001763
1764 for (i = 0; i < sbio->page_count; i++) {
1765 struct scrub_page *spage = sbio->pagev[i];
1766
1767 spage->io_error = 1;
David Sterbae37abe92018-04-04 17:20:52 +02001768 atomic64_inc(&dev_replace->num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001769 }
1770 }
1771
1772 for (i = 0; i < sbio->page_count; i++)
1773 scrub_page_put(sbio->pagev[i]);
1774
1775 bio_put(sbio->bio);
1776 kfree(sbio);
1777 scrub_pending_bio_dec(sctx);
1778}
1779
1780static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001781{
1782 u64 flags;
1783 int ret;
1784
Zhao Leiba7cf982015-08-24 21:18:02 +08001785 /*
1786 * No need to initialize these stats currently,
1787 * because this function only use return value
1788 * instead of these stats value.
1789 *
1790 * Todo:
1791 * always use stats
1792 */
1793 sblock->header_error = 0;
1794 sblock->generation_error = 0;
1795 sblock->checksum_error = 0;
1796
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001797 WARN_ON(sblock->page_count < 1);
1798 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001799 ret = 0;
1800 if (flags & BTRFS_EXTENT_FLAG_DATA)
1801 ret = scrub_checksum_data(sblock);
1802 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1803 ret = scrub_checksum_tree_block(sblock);
1804 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1805 (void)scrub_checksum_super(sblock);
1806 else
1807 WARN_ON(1);
1808 if (ret)
1809 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001810
1811 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001812}
1813
1814static int scrub_checksum_data(struct scrub_block *sblock)
1815{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001816 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001817 struct btrfs_fs_info *fs_info = sctx->fs_info;
1818 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Arne Jansena2de7332011-03-08 14:14:00 +01001819 u8 csum[BTRFS_CSUM_SIZE];
David Sterbad41ebef2020-05-29 16:20:35 +02001820 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001821 char *kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001822
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001823 BUG_ON(sblock->page_count < 1);
David Sterbad41ebef2020-05-29 16:20:35 +02001824 spage = sblock->pagev[0];
1825 if (!spage->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001826 return 0;
1827
David Sterbad41ebef2020-05-29 16:20:35 +02001828 kaddr = page_address(spage->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001829
David Sterba771aba02020-05-29 15:54:41 +02001830 shash->tfm = fs_info->csum_shash;
1831 crypto_shash_init(shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001832
Qu Wenruob29dca42020-12-02 14:48:10 +08001833 /*
1834 * In scrub_pages() and scrub_pages_for_parity() we ensure each spage
1835 * only contains one sector of data.
1836 */
1837 crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
1838
1839 if (memcmp(csum, spage->csum, fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001840 sblock->checksum_error = 1;
Zhao Leiba7cf982015-08-24 21:18:02 +08001841 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001842}
1843
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001845{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001846 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001847 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 struct btrfs_fs_info *fs_info = sctx->fs_info;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001849 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 u8 calculated_csum[BTRFS_CSUM_SIZE];
1851 u8 on_disk_csum[BTRFS_CSUM_SIZE];
Qu Wenruo53f32512020-12-02 14:48:09 +08001852 /*
1853 * This is done in sectorsize steps even for metadata as there's a
1854 * constraint for nodesize to be aligned to sectorsize. This will need
1855 * to change so we don't misuse data and metadata units like that.
1856 */
1857 const u32 sectorsize = sctx->fs_info->sectorsize;
1858 const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits;
David Sterba521e1022020-05-29 15:54:41 +02001859 int i;
David Sterba100aa5d2020-05-29 16:20:35 +02001860 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001861 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001862
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001863 BUG_ON(sblock->page_count < 1);
Qu Wenruo53f32512020-12-02 14:48:09 +08001864
1865 /* Each member in pagev is just one block, not a full page */
1866 ASSERT(sblock->page_count == num_sectors);
1867
David Sterba100aa5d2020-05-29 16:20:35 +02001868 spage = sblock->pagev[0];
1869 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001870 h = (struct btrfs_header *)kaddr;
David Sterba2ae0c2d2020-06-30 17:44:49 +02001871 memcpy(on_disk_csum, h->csum, sctx->fs_info->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001872
1873 /*
1874 * we don't use the getter functions here, as we
1875 * a) don't have an extent buffer and
1876 * b) the page is already kmapped
1877 */
David Sterba100aa5d2020-05-29 16:20:35 +02001878 if (spage->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001879 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001880
David Sterba100aa5d2020-05-29 16:20:35 +02001881 if (spage->generation != btrfs_stack_header_generation(h)) {
Zhao Leiba7cf982015-08-24 21:18:02 +08001882 sblock->header_error = 1;
1883 sblock->generation_error = 1;
1884 }
Arne Jansena2de7332011-03-08 14:14:00 +01001885
David Sterba100aa5d2020-05-29 16:20:35 +02001886 if (!scrub_check_fsid(h->fsid, spage))
Zhao Leiba7cf982015-08-24 21:18:02 +08001887 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001888
1889 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1890 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001891 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001892
David Sterba521e1022020-05-29 15:54:41 +02001893 shash->tfm = fs_info->csum_shash;
1894 crypto_shash_init(shash);
1895 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
Qu Wenruo53f32512020-12-02 14:48:09 +08001896 sectorsize - BTRFS_CSUM_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001897
Qu Wenruo53f32512020-12-02 14:48:09 +08001898 for (i = 1; i < num_sectors; i++) {
David Sterba521e1022020-05-29 15:54:41 +02001899 kaddr = page_address(sblock->pagev[i]->page);
Qu Wenruo53f32512020-12-02 14:48:09 +08001900 crypto_shash_update(shash, kaddr, sectorsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001901 }
1902
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001903 crypto_shash_final(shash, calculated_csum);
David Sterba2ae0c2d2020-06-30 17:44:49 +02001904 if (memcmp(calculated_csum, on_disk_csum, sctx->fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001905 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001906
Zhao Leiba7cf982015-08-24 21:18:02 +08001907 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001908}
1909
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001910static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001911{
1912 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001913 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001914 struct btrfs_fs_info *fs_info = sctx->fs_info;
1915 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001916 u8 calculated_csum[BTRFS_CSUM_SIZE];
David Sterbac7460542020-05-29 15:47:05 +02001917 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001918 char *kaddr;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001919 int fail_gen = 0;
1920 int fail_cor = 0;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001921
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001922 BUG_ON(sblock->page_count < 1);
David Sterbac7460542020-05-29 15:47:05 +02001923 spage = sblock->pagev[0];
1924 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001925 s = (struct btrfs_super_block *)kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001926
David Sterbac7460542020-05-29 15:47:05 +02001927 if (spage->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001928 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001929
David Sterbac7460542020-05-29 15:47:05 +02001930 if (spage->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001931 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001932
David Sterbac7460542020-05-29 15:47:05 +02001933 if (!scrub_check_fsid(s->fsid, spage))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001934 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001935
David Sterba83cf6d52020-05-29 15:40:36 +02001936 shash->tfm = fs_info->csum_shash;
1937 crypto_shash_init(shash);
1938 crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE,
1939 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001940
David Sterba2ae0c2d2020-06-30 17:44:49 +02001941 if (memcmp(calculated_csum, s->csum, sctx->fs_info->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001942 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001943
Stefan Behrens442a4f62012-05-25 16:06:08 +02001944 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001945 /*
1946 * if we find an error in a super block, we just report it.
1947 * They will get written with the next transaction commit
1948 * anyway
1949 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001950 spin_lock(&sctx->stat_lock);
1951 ++sctx->stat.super_errors;
1952 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001953 if (fail_cor)
David Sterbac7460542020-05-29 15:47:05 +02001954 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001955 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1956 else
David Sterbac7460542020-05-29 15:47:05 +02001957 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001958 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001959 }
1960
Stefan Behrens442a4f62012-05-25 16:06:08 +02001961 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001962}
1963
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964static void scrub_block_get(struct scrub_block *sblock)
1965{
Elena Reshetova186debd2017-03-03 10:55:23 +02001966 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001967}
1968
1969static void scrub_block_put(struct scrub_block *sblock)
1970{
Elena Reshetova186debd2017-03-03 10:55:23 +02001971 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001972 int i;
1973
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001974 if (sblock->sparity)
1975 scrub_parity_put(sblock->sparity);
1976
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001977 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001978 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001979 kfree(sblock);
1980 }
1981}
1982
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001983static void scrub_page_get(struct scrub_page *spage)
1984{
Zhao Lei57019342015-01-20 15:11:45 +08001985 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001986}
1987
1988static void scrub_page_put(struct scrub_page *spage)
1989{
Zhao Lei57019342015-01-20 15:11:45 +08001990 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001991 if (spage->page)
1992 __free_page(spage->page);
1993 kfree(spage);
1994 }
1995}
1996
David Sterbaeb3b5052019-10-09 13:58:13 +02001997/*
1998 * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
1999 * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
2000 */
2001static void scrub_throttle(struct scrub_ctx *sctx)
2002{
2003 const int time_slice = 1000;
2004 struct scrub_bio *sbio;
2005 struct btrfs_device *device;
2006 s64 delta;
2007 ktime_t now;
2008 u32 div;
2009 u64 bwlimit;
2010
2011 sbio = sctx->bios[sctx->curr];
2012 device = sbio->dev;
2013 bwlimit = READ_ONCE(device->scrub_speed_max);
2014 if (bwlimit == 0)
2015 return;
2016
2017 /*
2018 * Slice is divided into intervals when the IO is submitted, adjust by
2019 * bwlimit and maximum of 64 intervals.
2020 */
2021 div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
2022 div = min_t(u32, 64, div);
2023
2024 /* Start new epoch, set deadline */
2025 now = ktime_get();
2026 if (sctx->throttle_deadline == 0) {
2027 sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
2028 sctx->throttle_sent = 0;
2029 }
2030
2031 /* Still in the time to send? */
2032 if (ktime_before(now, sctx->throttle_deadline)) {
2033 /* If current bio is within the limit, send it */
2034 sctx->throttle_sent += sbio->bio->bi_iter.bi_size;
2035 if (sctx->throttle_sent <= div_u64(bwlimit, div))
2036 return;
2037
2038 /* We're over the limit, sleep until the rest of the slice */
2039 delta = ktime_ms_delta(sctx->throttle_deadline, now);
2040 } else {
2041 /* New request after deadline, start new epoch */
2042 delta = 0;
2043 }
2044
2045 if (delta) {
2046 long timeout;
2047
2048 timeout = div_u64(delta * HZ, 1000);
2049 schedule_timeout_interruptible(timeout);
2050 }
2051
2052 /* Next call will start the deadline period */
2053 sctx->throttle_deadline = 0;
2054}
2055
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002056static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002057{
2058 struct scrub_bio *sbio;
2059
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002060 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002061 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002062
David Sterbaeb3b5052019-10-09 13:58:13 +02002063 scrub_throttle(sctx);
2064
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002065 sbio = sctx->bios[sctx->curr];
2066 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002067 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002068 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002069}
2070
Stefan Behrensff023aa2012-11-06 11:43:11 +01002071static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2072 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002073{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002074 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002075 struct scrub_bio *sbio;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002076 const u32 sectorsize = sctx->fs_info->sectorsize;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002077 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002078
2079again:
2080 /*
2081 * grab a fresh bio or wait for one to become available
2082 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002083 while (sctx->curr == -1) {
2084 spin_lock(&sctx->list_lock);
2085 sctx->curr = sctx->first_free;
2086 if (sctx->curr != -1) {
2087 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2088 sctx->bios[sctx->curr]->next_free = -1;
2089 sctx->bios[sctx->curr]->page_count = 0;
2090 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002091 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002092 spin_unlock(&sctx->list_lock);
2093 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002094 }
2095 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002096 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002097 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002098 struct bio *bio;
2099
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002100 sbio->physical = spage->physical;
2101 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002102 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002103 bio = sbio->bio;
2104 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002105 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002106 sbio->bio = bio;
2107 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002108
2109 bio->bi_private = sbio;
2110 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002111 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002112 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02002113 bio->bi_opf = REQ_OP_READ;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002114 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002115 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002116 spage->physical ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08002117 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002118 spage->logical ||
2119 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002120 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002121 goto again;
2122 }
2123
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002124 sbio->pagev[sbio->page_count] = spage;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002125 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
2126 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002127 if (sbio->page_count < 1) {
2128 bio_put(sbio->bio);
2129 sbio->bio = NULL;
2130 return -EIO;
2131 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002132 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002133 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002134 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002135
Stefan Behrensff023aa2012-11-06 11:43:11 +01002136 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002137 atomic_inc(&sblock->outstanding_pages);
2138 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002139 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002140 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002141
2142 return 0;
2143}
2144
Linus Torvalds22365972015-09-05 15:14:43 -07002145static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002146{
2147 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002148 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002149
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002150 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002151 sblock->no_io_error_seen = 0;
2152
Scott Talbert46732722016-05-09 09:14:28 -04002153 bio_put(bio);
2154
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002155 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2156}
2157
2158static void scrub_missing_raid56_worker(struct btrfs_work *work)
2159{
2160 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2161 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002162 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002163 u64 logical;
2164 struct btrfs_device *dev;
2165
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002166 logical = sblock->pagev[0]->logical;
2167 dev = sblock->pagev[0]->dev;
2168
Zhao Leiaffe4a52015-08-24 21:32:06 +08002169 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002170 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002171
2172 if (!sblock->no_io_error_seen) {
2173 spin_lock(&sctx->stat_lock);
2174 sctx->stat.read_errors++;
2175 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002177 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002178 logical, rcu_str_deref(dev->name));
2179 } else if (sblock->header_error || sblock->checksum_error) {
2180 spin_lock(&sctx->stat_lock);
2181 sctx->stat.uncorrectable_errors++;
2182 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002183 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002184 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002185 logical, rcu_str_deref(dev->name));
2186 } else {
2187 scrub_write_block_to_dev_replace(sblock);
2188 }
2189
David Sterba2073c4c2017-03-31 17:12:51 +02002190 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002191 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002192 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002193 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002194 }
2195
Omar Sandoval57d4f0b2019-09-16 11:30:56 -07002196 scrub_block_put(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002197 scrub_pending_bio_dec(sctx);
2198}
2199
2200static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2201{
2202 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002203 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002204 u64 length = sblock->page_count * PAGE_SIZE;
2205 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002206 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002207 struct bio *bio;
2208 struct btrfs_raid_bio *rbio;
2209 int ret;
2210 int i;
2211
Qu Wenruoae6529c2017-03-29 09:33:21 +08002212 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002213 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002214 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002215 if (ret || !bbio || !bbio->raid_map)
2216 goto bbio_out;
2217
2218 if (WARN_ON(!sctx->is_dev_replace ||
2219 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2220 /*
2221 * We shouldn't be scrubbing a missing device. Even for dev
2222 * replace, we should only get here for RAID 5/6. We either
2223 * managed to mount something with no mirrors remaining or
2224 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2225 */
2226 goto bbio_out;
2227 }
2228
David Sterbac5e4c3d2017-06-12 17:29:41 +02002229 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002230 bio->bi_iter.bi_sector = logical >> 9;
2231 bio->bi_private = sblock;
2232 bio->bi_end_io = scrub_missing_raid56_end_io;
2233
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002234 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002235 if (!rbio)
2236 goto rbio_out;
2237
2238 for (i = 0; i < sblock->page_count; i++) {
2239 struct scrub_page *spage = sblock->pagev[i];
2240
2241 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2242 }
2243
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002244 btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002245 scrub_block_get(sblock);
2246 scrub_pending_bio_inc(sctx);
2247 raid56_submit_missing_rbio(rbio);
2248 return;
2249
2250rbio_out:
2251 bio_put(bio);
2252bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002253 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002254 btrfs_put_bbio(bbio);
2255 spin_lock(&sctx->stat_lock);
2256 sctx->stat.malloc_errors++;
2257 spin_unlock(&sctx->stat_lock);
2258}
2259
Qu Wenruofa485d22020-12-02 14:48:07 +08002260static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002261 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002262 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002263 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002264{
2265 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002266 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002267 int index;
2268
David Sterba58c4e172016-02-11 10:49:42 +01002269 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002270 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002271 spin_lock(&sctx->stat_lock);
2272 sctx->stat.malloc_errors++;
2273 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002274 return -ENOMEM;
2275 }
2276
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002277 /* one ref inside this function, plus one for each page added to
2278 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002279 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002280 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002281 sblock->no_io_error_seen = 1;
2282
2283 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002284 struct scrub_page *spage;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002285 /*
2286 * Here we will allocate one page for one sector to scrub.
2287 * This is fine if PAGE_SIZE == sectorsize, but will cost
2288 * more memory for PAGE_SIZE > sectorsize case.
2289 */
2290 u32 l = min(sectorsize, len);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002291
David Sterba58c4e172016-02-11 10:49:42 +01002292 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002293 if (!spage) {
2294leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002295 spin_lock(&sctx->stat_lock);
2296 sctx->stat.malloc_errors++;
2297 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002298 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002299 return -ENOMEM;
2300 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002301 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2302 scrub_page_get(spage);
2303 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002304 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002305 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002306 spage->flags = flags;
2307 spage->generation = gen;
2308 spage->logical = logical;
2309 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002310 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002311 spage->mirror_num = mirror_num;
2312 if (csum) {
2313 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002314 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002315 } else {
2316 spage->have_csum = 0;
2317 }
2318 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002319 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002320 if (!spage->page)
2321 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002322 len -= l;
2323 logical += l;
2324 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002325 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002326 }
2327
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002328 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002329 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002330 /*
2331 * This case should only be hit for RAID 5/6 device replace. See
2332 * the comment in scrub_missing_raid56_pages() for details.
2333 */
2334 scrub_missing_raid56_pages(sblock);
2335 } else {
2336 for (index = 0; index < sblock->page_count; index++) {
2337 struct scrub_page *spage = sblock->pagev[index];
2338 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002339
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002340 ret = scrub_add_page_to_rd_bio(sctx, spage);
2341 if (ret) {
2342 scrub_block_put(sblock);
2343 return ret;
2344 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002345 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002346
Qu Wenruo96e63a42020-11-03 21:31:02 +08002347 if (flags & BTRFS_EXTENT_FLAG_SUPER)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002348 scrub_submit(sctx);
2349 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002350
2351 /* last one frees, either here or in bio completion for last page */
2352 scrub_block_put(sblock);
2353 return 0;
2354}
2355
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002356static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002357{
2358 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002359 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002360
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002361 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002362 sbio->bio = bio;
2363
Qu Wenruo0339ef22014-02-28 10:46:17 +08002364 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002365}
2366
2367static void scrub_bio_end_io_worker(struct btrfs_work *work)
2368{
2369 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002371 int i;
2372
Stefan Behrensff023aa2012-11-06 11:43:11 +01002373 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002374 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002375 for (i = 0; i < sbio->page_count; i++) {
2376 struct scrub_page *spage = sbio->pagev[i];
2377
2378 spage->io_error = 1;
2379 spage->sblock->no_io_error_seen = 0;
2380 }
2381 }
2382
2383 /* now complete the scrub_block items that have all pages completed */
2384 for (i = 0; i < sbio->page_count; i++) {
2385 struct scrub_page *spage = sbio->pagev[i];
2386 struct scrub_block *sblock = spage->sblock;
2387
2388 if (atomic_dec_and_test(&sblock->outstanding_pages))
2389 scrub_block_complete(sblock);
2390 scrub_block_put(sblock);
2391 }
2392
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002393 bio_put(sbio->bio);
2394 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002395 spin_lock(&sctx->list_lock);
2396 sbio->next_free = sctx->first_free;
2397 sctx->first_free = sbio->index;
2398 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002399
David Sterba2073c4c2017-03-31 17:12:51 +02002400 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002401 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002402 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002403 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002404 }
2405
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002406 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002407}
2408
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002409static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2410 unsigned long *bitmap,
Qu Wenruofa485d22020-12-02 14:48:07 +08002411 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002412{
Liu Bo972d7212017-04-03 13:45:33 -07002413 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002414 u32 nsectors;
David Sterbaab108d92020-07-01 20:45:04 +02002415 u32 sectorsize_bits = sparity->sctx->fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002416
2417 if (len >= sparity->stripe_len) {
2418 bitmap_set(bitmap, 0, sparity->nsectors);
2419 return;
2420 }
2421
2422 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002423 start = div64_u64_rem(start, sparity->stripe_len, &offset);
David Sterbaab108d92020-07-01 20:45:04 +02002424 offset = offset >> sectorsize_bits;
Qu Wenruofa485d22020-12-02 14:48:07 +08002425 nsectors = len >> sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002426
2427 if (offset + nsectors <= sparity->nsectors) {
2428 bitmap_set(bitmap, offset, nsectors);
2429 return;
2430 }
2431
2432 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2433 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2434}
2435
2436static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002437 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002438{
2439 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2440}
2441
2442static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002443 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002444{
2445 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2446}
2447
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002448static void scrub_block_complete(struct scrub_block *sblock)
2449{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002450 int corrupted = 0;
2451
Stefan Behrensff023aa2012-11-06 11:43:11 +01002452 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002453 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002454 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002455 } else {
2456 /*
2457 * if has checksum error, write via repair mechanism in
2458 * dev replace case, otherwise write here in dev replace
2459 * case.
2460 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002461 corrupted = scrub_checksum(sblock);
2462 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002463 scrub_write_block_to_dev_replace(sblock);
2464 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002465
2466 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2467 u64 start = sblock->pagev[0]->logical;
2468 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
Qu Wenruo8df507c2021-04-22 19:02:46 +08002469 sblock->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002470
Qu Wenruofa485d22020-12-02 14:48:07 +08002471 ASSERT(end - start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002472 scrub_parity_mark_sectors_error(sblock->sparity,
2473 start, end - start);
2474 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002475}
2476
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002477static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum)
2478{
2479 sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits;
2480 list_del(&sum->list);
2481 kfree(sum);
2482}
2483
2484/*
2485 * Find the desired csum for range [logical, logical + sectorsize), and store
2486 * the csum into @csum.
2487 *
2488 * The search source is sctx->csum_list, which is a pre-populated list
David Sterba1a9fd412021-05-21 17:42:23 +02002489 * storing bytenr ordered csum ranges. We're responsible to cleanup any range
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002490 * that is before @logical.
2491 *
2492 * Return 0 if there is no csum for the range.
2493 * Return 1 if there is csum for the range and copied to @csum.
2494 */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002495static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002496{
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002497 bool found = false;
Arne Jansena2de7332011-03-08 14:14:00 +01002498
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002499 while (!list_empty(&sctx->csum_list)) {
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002500 struct btrfs_ordered_sum *sum = NULL;
2501 unsigned long index;
2502 unsigned long num_sectors;
2503
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002504 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002505 struct btrfs_ordered_sum, list);
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002506 /* The current csum range is beyond our range, no csum found */
Arne Jansena2de7332011-03-08 14:14:00 +01002507 if (sum->bytenr > logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002508 break;
2509
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002510 /*
2511 * The current sum is before our bytenr, since scrub is always
2512 * done in bytenr order, the csum will never be used anymore,
2513 * clean it up so that later calls won't bother with the range,
2514 * and continue search the next range.
2515 */
2516 if (sum->bytenr + sum->len <= logical) {
2517 drop_csum_range(sctx, sum);
2518 continue;
2519 }
2520
2521 /* Now the csum range covers our bytenr, copy the csum */
2522 found = true;
2523 index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits;
2524 num_sectors = sum->len >> sctx->fs_info->sectorsize_bits;
2525
2526 memcpy(csum, sum->sums + index * sctx->fs_info->csum_size,
2527 sctx->fs_info->csum_size);
2528
2529 /* Cleanup the range if we're at the end of the csum range */
2530 if (index == num_sectors - 1)
2531 drop_csum_range(sctx, sum);
2532 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002533 }
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002534 if (!found)
Arne Jansena2de7332011-03-08 14:14:00 +01002535 return 0;
Miao Xief51a4a12013-06-19 10:36:09 +08002536 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002537}
2538
2539/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002540static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
Qu Wenruofa485d22020-12-02 14:48:07 +08002541 u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002542 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002543 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002544{
2545 int ret;
2546 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002547 u32 blocksize;
2548
2549 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002550 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2551 blocksize = map->stripe_len;
2552 else
2553 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002554 spin_lock(&sctx->stat_lock);
2555 sctx->stat.data_extents_scrubbed++;
2556 sctx->stat.data_bytes_scrubbed += len;
2557 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002558 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002559 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2560 blocksize = map->stripe_len;
2561 else
2562 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002563 spin_lock(&sctx->stat_lock);
2564 sctx->stat.tree_extents_scrubbed++;
2565 sctx->stat.tree_bytes_scrubbed += len;
2566 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002567 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002568 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002569 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002570 }
Arne Jansena2de7332011-03-08 14:14:00 +01002571
2572 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002573 u32 l = min(len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002574 int have_csum = 0;
2575
2576 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2577 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002578 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002579 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002580 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002581 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002582 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002583 mirror_num, have_csum ? csum : NULL,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002584 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002585 if (ret)
2586 return ret;
2587 len -= l;
2588 logical += l;
2589 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002590 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002591 }
2592 return 0;
2593}
2594
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002595static int scrub_pages_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002596 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002597 u64 physical, struct btrfs_device *dev,
2598 u64 flags, u64 gen, int mirror_num, u8 *csum)
2599{
2600 struct scrub_ctx *sctx = sparity->sctx;
2601 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002602 const u32 sectorsize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002603 int index;
2604
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002605 ASSERT(IS_ALIGNED(len, sectorsize));
2606
David Sterba58c4e172016-02-11 10:49:42 +01002607 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002608 if (!sblock) {
2609 spin_lock(&sctx->stat_lock);
2610 sctx->stat.malloc_errors++;
2611 spin_unlock(&sctx->stat_lock);
2612 return -ENOMEM;
2613 }
2614
2615 /* one ref inside this function, plus one for each page added to
2616 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002617 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002618 sblock->sctx = sctx;
2619 sblock->no_io_error_seen = 1;
2620 sblock->sparity = sparity;
2621 scrub_parity_get(sparity);
2622
2623 for (index = 0; len > 0; index++) {
2624 struct scrub_page *spage;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002625
David Sterba58c4e172016-02-11 10:49:42 +01002626 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002627 if (!spage) {
2628leave_nomem:
2629 spin_lock(&sctx->stat_lock);
2630 sctx->stat.malloc_errors++;
2631 spin_unlock(&sctx->stat_lock);
2632 scrub_block_put(sblock);
2633 return -ENOMEM;
2634 }
2635 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2636 /* For scrub block */
2637 scrub_page_get(spage);
2638 sblock->pagev[index] = spage;
2639 /* For scrub parity */
2640 scrub_page_get(spage);
2641 list_add_tail(&spage->list, &sparity->spages);
2642 spage->sblock = sblock;
2643 spage->dev = dev;
2644 spage->flags = flags;
2645 spage->generation = gen;
2646 spage->logical = logical;
2647 spage->physical = physical;
2648 spage->mirror_num = mirror_num;
2649 if (csum) {
2650 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002651 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002652 } else {
2653 spage->have_csum = 0;
2654 }
2655 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002656 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002657 if (!spage->page)
2658 goto leave_nomem;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002659
2660
2661 /* Iterate over the stripe range in sectorsize steps */
2662 len -= sectorsize;
2663 logical += sectorsize;
2664 physical += sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002665 }
2666
2667 WARN_ON(sblock->page_count == 0);
2668 for (index = 0; index < sblock->page_count; index++) {
2669 struct scrub_page *spage = sblock->pagev[index];
2670 int ret;
2671
2672 ret = scrub_add_page_to_rd_bio(sctx, spage);
2673 if (ret) {
2674 scrub_block_put(sblock);
2675 return ret;
2676 }
2677 }
2678
2679 /* last one frees, either here or in bio completion for last page */
2680 scrub_block_put(sblock);
2681 return 0;
2682}
2683
2684static int scrub_extent_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002685 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002686 u64 physical, struct btrfs_device *dev,
2687 u64 flags, u64 gen, int mirror_num)
2688{
2689 struct scrub_ctx *sctx = sparity->sctx;
2690 int ret;
2691 u8 csum[BTRFS_CSUM_SIZE];
2692 u32 blocksize;
2693
Anand Jaine6e674b2017-12-04 12:54:54 +08002694 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002695 scrub_parity_mark_sectors_error(sparity, logical, len);
2696 return 0;
2697 }
2698
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002699 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002700 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002701 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002702 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002703 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002704 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002705 WARN_ON(1);
2706 }
2707
2708 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002709 u32 l = min(len, blocksize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002710 int have_csum = 0;
2711
2712 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2713 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002714 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002715 if (have_csum == 0)
2716 goto skip;
2717 }
2718 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2719 flags, gen, mirror_num,
2720 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002721 if (ret)
2722 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002723skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002724 len -= l;
2725 logical += l;
2726 physical += l;
2727 }
2728 return 0;
2729}
2730
Wang Shilong3b080b22014-04-01 18:01:43 +08002731/*
2732 * Given a physical address, this will calculate it's
2733 * logical offset. if this is a parity stripe, it will return
2734 * the most left data stripe's logical offset.
2735 *
2736 * return 0 if it is a data stripe, 1 means parity stripe.
2737 */
2738static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002739 struct map_lookup *map, u64 *offset,
2740 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002741{
2742 int i;
2743 int j = 0;
2744 u64 stripe_nr;
2745 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002746 u32 stripe_index;
2747 u32 rot;
David Sterbacff82672019-05-17 11:43:45 +02002748 const int data_stripes = nr_data_stripes(map);
Wang Shilong3b080b22014-04-01 18:01:43 +08002749
David Sterbacff82672019-05-17 11:43:45 +02002750 last_offset = (physical - map->stripes[num].physical) * data_stripes;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002751 if (stripe_start)
2752 *stripe_start = last_offset;
2753
Wang Shilong3b080b22014-04-01 18:01:43 +08002754 *offset = last_offset;
David Sterbacff82672019-05-17 11:43:45 +02002755 for (i = 0; i < data_stripes; i++) {
Wang Shilong3b080b22014-04-01 18:01:43 +08002756 *offset = last_offset + i * map->stripe_len;
2757
Liu Bo42c61ab2017-04-03 13:45:24 -07002758 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbacff82672019-05-17 11:43:45 +02002759 stripe_nr = div_u64(stripe_nr, data_stripes);
Wang Shilong3b080b22014-04-01 18:01:43 +08002760
2761 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002762 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002763 /* calculate which stripe this data locates */
2764 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002765 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002766 if (stripe_index == num)
2767 return 0;
2768 if (stripe_index < num)
2769 j++;
2770 }
2771 *offset = last_offset + j * map->stripe_len;
2772 return 1;
2773}
2774
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002775static void scrub_free_parity(struct scrub_parity *sparity)
2776{
2777 struct scrub_ctx *sctx = sparity->sctx;
2778 struct scrub_page *curr, *next;
2779 int nbits;
2780
2781 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2782 if (nbits) {
2783 spin_lock(&sctx->stat_lock);
2784 sctx->stat.read_errors += nbits;
2785 sctx->stat.uncorrectable_errors += nbits;
2786 spin_unlock(&sctx->stat_lock);
2787 }
2788
2789 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2790 list_del_init(&curr->list);
2791 scrub_page_put(curr);
2792 }
2793
2794 kfree(sparity);
2795}
2796
Zhao Lei20b2e302015-06-04 20:09:15 +08002797static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2798{
2799 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2800 work);
2801 struct scrub_ctx *sctx = sparity->sctx;
2802
2803 scrub_free_parity(sparity);
2804 scrub_pending_bio_dec(sctx);
2805}
2806
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002807static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002808{
2809 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002810 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002811
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002812 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002813 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2814 sparity->nsectors);
2815
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002816 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002817
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002818 btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL,
2819 NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002821}
2822
2823static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2824{
2825 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002826 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002827 struct bio *bio;
2828 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002829 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002830 u64 length;
2831 int ret;
2832
2833 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2834 sparity->nsectors))
2835 goto out;
2836
Zhao Leia0dd59d2015-07-21 15:42:26 +08002837 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08002838
2839 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002840 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02002841 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002842 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002843 goto bbio_out;
2844
David Sterbac5e4c3d2017-06-12 17:29:41 +02002845 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002846 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2847 bio->bi_private = sparity;
2848 bio->bi_end_io = scrub_parity_bio_endio;
2849
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002850 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002851 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002852 sparity->dbitmap,
2853 sparity->nsectors);
2854 if (!rbio)
2855 goto rbio_out;
2856
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002857 scrub_pending_bio_inc(sctx);
2858 raid56_parity_submit_scrub_rbio(rbio);
2859 return;
2860
2861rbio_out:
2862 bio_put(bio);
2863bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002864 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08002865 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002866 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2867 sparity->nsectors);
2868 spin_lock(&sctx->stat_lock);
2869 sctx->stat.malloc_errors++;
2870 spin_unlock(&sctx->stat_lock);
2871out:
2872 scrub_free_parity(sparity);
2873}
2874
2875static inline int scrub_calc_parity_bitmap_len(int nsectors)
2876{
Zhao Leibfca9a62014-12-08 19:55:57 +08002877 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002878}
2879
2880static void scrub_parity_get(struct scrub_parity *sparity)
2881{
Elena Reshetova78a76452017-03-03 10:55:24 +02002882 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002883}
2884
2885static void scrub_parity_put(struct scrub_parity *sparity)
2886{
Elena Reshetova78a76452017-03-03 10:55:24 +02002887 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002888 return;
2889
2890 scrub_parity_check_and_repair(sparity);
2891}
2892
2893static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2894 struct map_lookup *map,
2895 struct btrfs_device *sdev,
2896 struct btrfs_path *path,
2897 u64 logic_start,
2898 u64 logic_end)
2899{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002900 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002901 struct btrfs_root *root = fs_info->extent_root;
2902 struct btrfs_root *csum_root = fs_info->csum_root;
2903 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07002904 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002905 u64 flags;
2906 int ret;
2907 int slot;
2908 struct extent_buffer *l;
2909 struct btrfs_key key;
2910 u64 generation;
2911 u64 extent_logical;
2912 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08002913 /* Check the comment in scrub_stripe() for why u32 is enough here */
2914 u32 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002915 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002916 struct btrfs_device *extent_dev;
2917 struct scrub_parity *sparity;
2918 int nsectors;
2919 int bitmap_len;
2920 int extent_mirror_num;
2921 int stop_loop = 0;
2922
Qu Wenruofa485d22020-12-02 14:48:07 +08002923 ASSERT(map->stripe_len <= U32_MAX);
David Sterbaab108d92020-07-01 20:45:04 +02002924 nsectors = map->stripe_len >> fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002925 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2926 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2927 GFP_NOFS);
2928 if (!sparity) {
2929 spin_lock(&sctx->stat_lock);
2930 sctx->stat.malloc_errors++;
2931 spin_unlock(&sctx->stat_lock);
2932 return -ENOMEM;
2933 }
2934
Qu Wenruofa485d22020-12-02 14:48:07 +08002935 ASSERT(map->stripe_len <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002936 sparity->stripe_len = map->stripe_len;
2937 sparity->nsectors = nsectors;
2938 sparity->sctx = sctx;
2939 sparity->scrub_dev = sdev;
2940 sparity->logic_start = logic_start;
2941 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02002942 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002943 INIT_LIST_HEAD(&sparity->spages);
2944 sparity->dbitmap = sparity->bitmap;
2945 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2946
2947 ret = 0;
2948 while (logic_start < logic_end) {
2949 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2950 key.type = BTRFS_METADATA_ITEM_KEY;
2951 else
2952 key.type = BTRFS_EXTENT_ITEM_KEY;
2953 key.objectid = logic_start;
2954 key.offset = (u64)-1;
2955
2956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2957 if (ret < 0)
2958 goto out;
2959
2960 if (ret > 0) {
2961 ret = btrfs_previous_extent_item(root, path, 0);
2962 if (ret < 0)
2963 goto out;
2964 if (ret > 0) {
2965 btrfs_release_path(path);
2966 ret = btrfs_search_slot(NULL, root, &key,
2967 path, 0, 0);
2968 if (ret < 0)
2969 goto out;
2970 }
2971 }
2972
2973 stop_loop = 0;
2974 while (1) {
2975 u64 bytes;
2976
2977 l = path->nodes[0];
2978 slot = path->slots[0];
2979 if (slot >= btrfs_header_nritems(l)) {
2980 ret = btrfs_next_leaf(root, path);
2981 if (ret == 0)
2982 continue;
2983 if (ret < 0)
2984 goto out;
2985
2986 stop_loop = 1;
2987 break;
2988 }
2989 btrfs_item_key_to_cpu(l, &key, slot);
2990
Zhao Leid7cad232015-07-22 13:14:48 +08002991 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2992 key.type != BTRFS_METADATA_ITEM_KEY)
2993 goto next;
2994
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002995 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002996 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002997 else
2998 bytes = key.offset;
2999
3000 if (key.objectid + bytes <= logic_start)
3001 goto next;
3002
Zhao Leia0dd59d2015-07-21 15:42:26 +08003003 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003004 stop_loop = 1;
3005 break;
3006 }
3007
3008 while (key.objectid >= logic_start + map->stripe_len)
3009 logic_start += map->stripe_len;
3010
3011 extent = btrfs_item_ptr(l, slot,
3012 struct btrfs_extent_item);
3013 flags = btrfs_extent_flags(l, extent);
3014 generation = btrfs_extent_generation(l, extent);
3015
Zhao Leia323e812015-07-23 12:29:49 +08003016 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3017 (key.objectid < logic_start ||
3018 key.objectid + bytes >
3019 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003020 btrfs_err(fs_info,
3021 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003022 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003023 spin_lock(&sctx->stat_lock);
3024 sctx->stat.uncorrectable_errors++;
3025 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003026 goto next;
3027 }
3028again:
3029 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003030 ASSERT(bytes <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003031 extent_len = bytes;
3032
3033 if (extent_logical < logic_start) {
3034 extent_len -= logic_start - extent_logical;
3035 extent_logical = logic_start;
3036 }
3037
3038 if (extent_logical + extent_len >
3039 logic_start + map->stripe_len)
3040 extent_len = logic_start + map->stripe_len -
3041 extent_logical;
3042
3043 scrub_parity_mark_sectors_data(sparity, extent_logical,
3044 extent_len);
3045
Omar Sandoval4a770892015-06-19 11:52:52 -07003046 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003047 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003048 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3049 extent_logical, &mapped_length, &bbio,
3050 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003051 if (!ret) {
3052 if (!bbio || mapped_length < extent_len)
3053 ret = -EIO;
3054 }
3055 if (ret) {
3056 btrfs_put_bbio(bbio);
3057 goto out;
3058 }
3059 extent_physical = bbio->stripes[0].physical;
3060 extent_mirror_num = bbio->mirror_num;
3061 extent_dev = bbio->stripes[0].dev;
3062 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003063
3064 ret = btrfs_lookup_csums_range(csum_root,
3065 extent_logical,
3066 extent_logical + extent_len - 1,
3067 &sctx->csum_list, 1);
3068 if (ret)
3069 goto out;
3070
3071 ret = scrub_extent_for_parity(sparity, extent_logical,
3072 extent_len,
3073 extent_physical,
3074 extent_dev, flags,
3075 generation,
3076 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003077
3078 scrub_free_csums(sctx);
3079
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003080 if (ret)
3081 goto out;
3082
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003083 if (extent_logical + extent_len <
3084 key.objectid + bytes) {
3085 logic_start += map->stripe_len;
3086
3087 if (logic_start >= logic_end) {
3088 stop_loop = 1;
3089 break;
3090 }
3091
3092 if (logic_start < key.objectid + bytes) {
3093 cond_resched();
3094 goto again;
3095 }
3096 }
3097next:
3098 path->slots[0]++;
3099 }
3100
3101 btrfs_release_path(path);
3102
3103 if (stop_loop)
3104 break;
3105
3106 logic_start += map->stripe_len;
3107 }
3108out:
Qu Wenruofa485d22020-12-02 14:48:07 +08003109 if (ret < 0) {
3110 ASSERT(logic_end - logic_start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003111 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003112 logic_end - logic_start);
Qu Wenruofa485d22020-12-02 14:48:07 +08003113 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003114 scrub_parity_put(sparity);
3115 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003116 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003117 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003118 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003119
3120 btrfs_release_path(path);
3121 return ret < 0 ? ret : 0;
3122}
3123
Naohiro Aotade17add2021-02-04 19:22:13 +09003124static void sync_replace_for_zoned(struct scrub_ctx *sctx)
3125{
3126 if (!btrfs_is_zoned(sctx->fs_info))
3127 return;
3128
3129 sctx->flush_all_writes = true;
3130 scrub_submit(sctx);
3131 mutex_lock(&sctx->wr_lock);
3132 scrub_wr_submit(sctx);
3133 mutex_unlock(&sctx->wr_lock);
3134
3135 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3136}
3137
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003138static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
3139 u64 physical, u64 physical_end)
3140{
3141 struct btrfs_fs_info *fs_info = sctx->fs_info;
3142 int ret = 0;
3143
3144 if (!btrfs_is_zoned(fs_info))
3145 return 0;
3146
3147 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3148
3149 mutex_lock(&sctx->wr_lock);
3150 if (sctx->write_pointer < physical_end) {
3151 ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
3152 physical,
3153 sctx->write_pointer);
3154 if (ret)
3155 btrfs_err(fs_info,
3156 "zoned: failed to recover write pointer");
3157 }
3158 mutex_unlock(&sctx->wr_lock);
3159 btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
3160
3161 return ret;
3162}
3163
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003164static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003165 struct map_lookup *map,
3166 struct btrfs_device *scrub_dev,
Filipe Manana2473d242020-05-08 11:01:10 +01003167 int num, u64 base, u64 length,
3168 struct btrfs_block_group *cache)
Arne Jansena2de7332011-03-08 14:14:00 +01003169{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003170 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003171 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003172 struct btrfs_root *root = fs_info->extent_root;
3173 struct btrfs_root *csum_root = fs_info->csum_root;
3174 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003175 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003176 u64 flags;
3177 int ret;
3178 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003179 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003180 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003181 u64 physical;
3182 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003183 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003184 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003185 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003186 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003187 struct reada_control *reada1;
3188 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003189 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003190 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003191 u64 increment = map->stripe_len;
3192 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003193 u64 extent_logical;
3194 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08003195 /*
3196 * Unlike chunk length, extent length should never go beyond
3197 * BTRFS_MAX_EXTENT_SIZE, thus u32 is enough here.
3198 */
3199 u32 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003200 u64 stripe_logical;
3201 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003202 struct btrfs_device *extent_dev;
3203 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003204 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003205
Wang Shilong3b080b22014-04-01 18:01:43 +08003206 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003207 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003208 nstripes = div64_u64(length, map->stripe_len);
David Sterba7735cd72019-11-28 15:37:46 +01003209 mirror_num = 1;
3210 increment = map->stripe_len;
Arne Jansena2de7332011-03-08 14:14:00 +01003211 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3212 offset = map->stripe_len * num;
3213 increment = map->stripe_len * map->num_stripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003214 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3215 int factor = map->num_stripes / map->sub_stripes;
3216 offset = map->stripe_len * (num / map->sub_stripes);
3217 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003218 mirror_num = num % map->sub_stripes + 1;
David Sterbac7369b32019-05-31 15:39:31 +02003219 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Jan Schmidt193ea742011-06-13 19:56:54 +02003220 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003221 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidt193ea742011-06-13 19:56:54 +02003222 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003223 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003224 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003225 increment = map->stripe_len * nr_data_stripes(map);
Arne Jansena2de7332011-03-08 14:14:00 +01003226 }
3227
3228 path = btrfs_alloc_path();
3229 if (!path)
3230 return -ENOMEM;
3231
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003232 ppath = btrfs_alloc_path();
3233 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003234 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003235 return -ENOMEM;
3236 }
3237
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003238 /*
3239 * work on commit root. The related disk blocks are static as
3240 * long as COW is applied. This means, it is save to rewrite
3241 * them to repair disk errors without any race conditions
3242 */
Arne Jansena2de7332011-03-08 14:14:00 +01003243 path->search_commit_root = 1;
3244 path->skip_locking = 1;
3245
Gui Hecheng063c54d2015-01-09 09:39:40 +08003246 ppath->search_commit_root = 1;
3247 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003248 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003249 * trigger the readahead for extent tree csum tree and wait for
3250 * completion. During readahead, the scrub is officially paused
3251 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003252 */
3253 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003254 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003255 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003256 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003257 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003258 logic_end += base;
3259 } else {
3260 logic_end = logical + increment * nstripes;
3261 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003262 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003263 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003264 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003265
Arne Jansen7a262852011-06-10 12:39:23 +02003266 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003267 key.objectid = logical;
3268 key.type = BTRFS_EXTENT_ITEM_KEY;
3269 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003270 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003271 key_end.type = BTRFS_METADATA_ITEM_KEY;
3272 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003273 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003274
Filipe Mananaa6889ca2020-10-12 11:55:26 +01003275 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3276 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3277 key.type = BTRFS_EXTENT_CSUM_KEY;
3278 key.offset = logical;
3279 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3280 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3281 key_end.offset = logic_end;
3282 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
3283 } else {
3284 reada2 = NULL;
3285 }
Arne Jansena2de7332011-03-08 14:14:00 +01003286
Arne Jansen7a262852011-06-10 12:39:23 +02003287 if (!IS_ERR(reada1))
3288 btrfs_reada_wait(reada1);
Filipe Mananaa6889ca2020-10-12 11:55:26 +01003289 if (!IS_ERR_OR_NULL(reada2))
Arne Jansen7a262852011-06-10 12:39:23 +02003290 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003291
Arne Jansena2de7332011-03-08 14:14:00 +01003292
3293 /*
3294 * collect all data csums for the stripe to avoid seeking during
3295 * the scrub. This might currently (crc32) end up to be about 1MB
3296 */
Arne Jansene7786c32011-05-28 20:58:38 +00003297 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003298
Naohiro Aotade17add2021-02-04 19:22:13 +09003299 if (sctx->is_dev_replace &&
3300 btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
3301 mutex_lock(&sctx->wr_lock);
3302 sctx->write_pointer = physical;
3303 mutex_unlock(&sctx->wr_lock);
3304 sctx->flush_all_writes = true;
3305 }
3306
Arne Jansena2de7332011-03-08 14:14:00 +01003307 /*
3308 * now find all extents for each stripe and scrub them
3309 */
Arne Jansena2de7332011-03-08 14:14:00 +01003310 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003311 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003312 /*
3313 * canceled?
3314 */
3315 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003316 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003317 ret = -ECANCELED;
3318 goto out;
3319 }
3320 /*
3321 * check to see if we have to pause
3322 */
3323 if (atomic_read(&fs_info->scrub_pause_req)) {
3324 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003325 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003326 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003327 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003328 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003329 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003330 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003331 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003332 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003333 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003334 }
3335
Zhao Leif2f66a22015-07-21 12:22:29 +08003336 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3337 ret = get_raid56_logic_offset(physical, num, map,
3338 &logical,
3339 &stripe_logical);
3340 logical += base;
3341 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003342 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003343 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003344 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003345 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3346 ppath, stripe_logical,
3347 stripe_end);
3348 if (ret)
3349 goto out;
3350 goto skip;
3351 }
3352 }
3353
Wang Shilong7c76edb2014-01-12 21:38:32 +08003354 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3355 key.type = BTRFS_METADATA_ITEM_KEY;
3356 else
3357 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003358 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003359 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003360
3361 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3362 if (ret < 0)
3363 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003364
Arne Jansen8c510322011-06-03 10:09:26 +02003365 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003366 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003367 if (ret < 0)
3368 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003369 if (ret > 0) {
3370 /* there's no smaller item, so stick with the
3371 * larger one */
3372 btrfs_release_path(path);
3373 ret = btrfs_search_slot(NULL, root, &key,
3374 path, 0, 0);
3375 if (ret < 0)
3376 goto out;
3377 }
Arne Jansena2de7332011-03-08 14:14:00 +01003378 }
3379
Liu Bo625f1c8d2013-04-27 02:56:57 +00003380 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003381 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003382 u64 bytes;
3383
Arne Jansena2de7332011-03-08 14:14:00 +01003384 l = path->nodes[0];
3385 slot = path->slots[0];
3386 if (slot >= btrfs_header_nritems(l)) {
3387 ret = btrfs_next_leaf(root, path);
3388 if (ret == 0)
3389 continue;
3390 if (ret < 0)
3391 goto out;
3392
Liu Bo625f1c8d2013-04-27 02:56:57 +00003393 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003394 break;
3395 }
3396 btrfs_item_key_to_cpu(l, &key, slot);
3397
Zhao Leid7cad232015-07-22 13:14:48 +08003398 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3399 key.type != BTRFS_METADATA_ITEM_KEY)
3400 goto next;
3401
Josef Bacik3173a182013-03-07 14:22:04 -05003402 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003403 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003404 else
3405 bytes = key.offset;
3406
3407 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003408 goto next;
3409
Liu Bo625f1c8d2013-04-27 02:56:57 +00003410 if (key.objectid >= logical + map->stripe_len) {
3411 /* out of this device extent */
3412 if (key.objectid >= logic_end)
3413 stop_loop = 1;
3414 break;
3415 }
Arne Jansena2de7332011-03-08 14:14:00 +01003416
Filipe Manana2473d242020-05-08 11:01:10 +01003417 /*
3418 * If our block group was removed in the meanwhile, just
3419 * stop scrubbing since there is no point in continuing.
3420 * Continuing would prevent reusing its device extents
3421 * for new block groups for a long time.
3422 */
3423 spin_lock(&cache->lock);
3424 if (cache->removed) {
3425 spin_unlock(&cache->lock);
3426 ret = 0;
3427 goto out;
3428 }
3429 spin_unlock(&cache->lock);
3430
Arne Jansena2de7332011-03-08 14:14:00 +01003431 extent = btrfs_item_ptr(l, slot,
3432 struct btrfs_extent_item);
3433 flags = btrfs_extent_flags(l, extent);
3434 generation = btrfs_extent_generation(l, extent);
3435
Zhao Leia323e812015-07-23 12:29:49 +08003436 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3437 (key.objectid < logical ||
3438 key.objectid + bytes >
3439 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003440 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003441 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003442 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003443 spin_lock(&sctx->stat_lock);
3444 sctx->stat.uncorrectable_errors++;
3445 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003446 goto next;
3447 }
3448
Liu Bo625f1c8d2013-04-27 02:56:57 +00003449again:
3450 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003451 ASSERT(bytes <= U32_MAX);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003452 extent_len = bytes;
3453
Arne Jansena2de7332011-03-08 14:14:00 +01003454 /*
3455 * trim extent to this stripe
3456 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003457 if (extent_logical < logical) {
3458 extent_len -= logical - extent_logical;
3459 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003460 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003461 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003462 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003463 extent_len = logical + map->stripe_len -
3464 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003465 }
3466
Liu Bo625f1c8d2013-04-27 02:56:57 +00003467 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003468 extent_dev = scrub_dev;
3469 extent_mirror_num = mirror_num;
Omar Sandoval32934282018-08-14 11:09:52 -07003470 if (sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003471 scrub_remap_extent(fs_info, extent_logical,
3472 extent_len, &extent_physical,
3473 &extent_dev,
3474 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003475
Filipe Manana89490302020-05-08 11:02:07 +01003476 if (flags & BTRFS_EXTENT_FLAG_DATA) {
3477 ret = btrfs_lookup_csums_range(csum_root,
3478 extent_logical,
3479 extent_logical + extent_len - 1,
3480 &sctx->csum_list, 1);
3481 if (ret)
3482 goto out;
3483 }
Arne Jansena2de7332011-03-08 14:14:00 +01003484
Liu Bo6ca17652018-03-07 12:08:09 -07003485 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003486 extent_physical, extent_dev, flags,
3487 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003488 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003489
3490 scrub_free_csums(sctx);
3491
Liu Bo625f1c8d2013-04-27 02:56:57 +00003492 if (ret)
3493 goto out;
3494
Naohiro Aotade17add2021-02-04 19:22:13 +09003495 if (sctx->is_dev_replace)
3496 sync_replace_for_zoned(sctx);
3497
Liu Bo625f1c8d2013-04-27 02:56:57 +00003498 if (extent_logical + extent_len <
3499 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003500 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003501 /*
3502 * loop until we find next data stripe
3503 * or we have finished all stripes.
3504 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003505loop:
3506 physical += map->stripe_len;
3507 ret = get_raid56_logic_offset(physical,
3508 num, map, &logical,
3509 &stripe_logical);
3510 logical += base;
3511
3512 if (ret && physical < physical_end) {
3513 stripe_logical += base;
3514 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003515 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003516 ret = scrub_raid56_parity(sctx,
3517 map, scrub_dev, ppath,
3518 stripe_logical,
3519 stripe_end);
3520 if (ret)
3521 goto out;
3522 goto loop;
3523 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003524 } else {
3525 physical += map->stripe_len;
3526 logical += increment;
3527 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003528 if (logical < key.objectid + bytes) {
3529 cond_resched();
3530 goto again;
3531 }
3532
Wang Shilong3b080b22014-04-01 18:01:43 +08003533 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003534 stop_loop = 1;
3535 break;
3536 }
3537 }
Arne Jansena2de7332011-03-08 14:14:00 +01003538next:
3539 path->slots[0]++;
3540 }
Chris Mason71267332011-05-23 06:30:52 -04003541 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003542skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003543 logical += increment;
3544 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003545 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003546 if (stop_loop)
3547 sctx->stat.last_physical = map->stripes[num].physical +
3548 length;
3549 else
3550 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003551 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003552 if (stop_loop)
3553 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003554 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003555out:
Arne Jansena2de7332011-03-08 14:14:00 +01003556 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003557 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003558 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003559 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003560 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003561
Arne Jansene7786c32011-05-28 20:58:38 +00003562 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003563 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003564 btrfs_free_path(ppath);
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003565
3566 if (sctx->is_dev_replace && ret >= 0) {
3567 int ret2;
3568
3569 ret2 = sync_write_pointer_for_zoned(sctx, base + offset,
3570 map->stripes[num].physical,
3571 physical_end);
3572 if (ret2)
3573 ret = ret2;
3574 }
3575
Arne Jansena2de7332011-03-08 14:14:00 +01003576 return ret < 0 ? ret : 0;
3577}
3578
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003579static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003580 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003581 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003582 u64 dev_offset,
David Sterba32da53862019-10-29 19:20:18 +01003583 struct btrfs_block_group *cache)
Arne Jansena2de7332011-03-08 14:14:00 +01003584{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003585 struct btrfs_fs_info *fs_info = sctx->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02003586 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003587 struct map_lookup *map;
3588 struct extent_map *em;
3589 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003590 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003591
David Sterbac8bf1b62019-05-17 11:43:17 +02003592 read_lock(&map_tree->lock);
3593 em = lookup_extent_mapping(map_tree, chunk_offset, 1);
3594 read_unlock(&map_tree->lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003595
Filipe Manana020d5b72015-11-19 10:57:20 +00003596 if (!em) {
3597 /*
3598 * Might have been an unused block group deleted by the cleaner
3599 * kthread or relocation.
3600 */
3601 spin_lock(&cache->lock);
3602 if (!cache->removed)
3603 ret = -EINVAL;
3604 spin_unlock(&cache->lock);
3605
3606 return ret;
3607 }
Arne Jansena2de7332011-03-08 14:14:00 +01003608
Jeff Mahoney95617d62015-06-03 10:55:48 -04003609 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003610 if (em->start != chunk_offset)
3611 goto out;
3612
3613 if (em->len < length)
3614 goto out;
3615
3616 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003617 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003618 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003619 ret = scrub_stripe(sctx, map, scrub_dev, i,
Filipe Manana2473d242020-05-08 11:01:10 +01003620 chunk_offset, length, cache);
Arne Jansena2de7332011-03-08 14:14:00 +01003621 if (ret)
3622 goto out;
3623 }
3624 }
3625out:
3626 free_extent_map(em);
3627
3628 return ret;
3629}
3630
Naohiro Aotade17add2021-02-04 19:22:13 +09003631static int finish_extent_writes_for_zoned(struct btrfs_root *root,
3632 struct btrfs_block_group *cache)
3633{
3634 struct btrfs_fs_info *fs_info = cache->fs_info;
3635 struct btrfs_trans_handle *trans;
3636
3637 if (!btrfs_is_zoned(fs_info))
3638 return 0;
3639
3640 btrfs_wait_block_group_reservations(cache);
3641 btrfs_wait_nocow_writers(cache);
3642 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
3643
3644 trans = btrfs_join_transaction(root);
3645 if (IS_ERR(trans))
3646 return PTR_ERR(trans);
3647 return btrfs_commit_transaction(trans);
3648}
3649
Arne Jansena2de7332011-03-08 14:14:00 +01003650static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003651int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Omar Sandoval32934282018-08-14 11:09:52 -07003652 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01003653{
3654 struct btrfs_dev_extent *dev_extent = NULL;
3655 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003656 struct btrfs_fs_info *fs_info = sctx->fs_info;
3657 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003658 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003659 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003660 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003661 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003662 int slot;
3663 struct extent_buffer *l;
3664 struct btrfs_key key;
3665 struct btrfs_key found_key;
David Sterba32da53862019-10-29 19:20:18 +01003666 struct btrfs_block_group *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003667 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003668
3669 path = btrfs_alloc_path();
3670 if (!path)
3671 return -ENOMEM;
3672
David Sterbae4058b52015-11-27 16:31:35 +01003673 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003674 path->search_commit_root = 1;
3675 path->skip_locking = 1;
3676
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003677 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003678 key.offset = 0ull;
3679 key.type = BTRFS_DEV_EXTENT_KEY;
3680
Arne Jansena2de7332011-03-08 14:14:00 +01003681 while (1) {
3682 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3683 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003684 break;
3685 if (ret > 0) {
3686 if (path->slots[0] >=
3687 btrfs_header_nritems(path->nodes[0])) {
3688 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003689 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003690 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003691 if (ret > 0) {
3692 ret = 0;
3693 break;
3694 }
3695 } else {
3696 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003697 }
3698 }
Arne Jansena2de7332011-03-08 14:14:00 +01003699
3700 l = path->nodes[0];
3701 slot = path->slots[0];
3702
3703 btrfs_item_key_to_cpu(l, &found_key, slot);
3704
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003705 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003706 break;
3707
David Sterba962a2982014-06-04 18:41:45 +02003708 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003709 break;
3710
3711 if (found_key.offset >= end)
3712 break;
3713
3714 if (found_key.offset < key.offset)
3715 break;
3716
3717 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3718 length = btrfs_dev_extent_length(l, dev_extent);
3719
Qu Wenruoced96ed2014-06-19 10:42:51 +08003720 if (found_key.offset + length <= start)
3721 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003722
Arne Jansena2de7332011-03-08 14:14:00 +01003723 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3724
3725 /*
3726 * get a reference on the corresponding block group to prevent
3727 * the chunk from going away while we scrub it
3728 */
3729 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003730
3731 /* some chunks are removed but not committed to disk yet,
3732 * continue scrubbing */
3733 if (!cache)
3734 goto skip;
3735
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003736 if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
3737 spin_lock(&cache->lock);
3738 if (!cache->to_copy) {
3739 spin_unlock(&cache->lock);
Filipe Manana0dc16ef2021-04-14 14:05:26 +01003740 btrfs_put_block_group(cache);
3741 goto skip;
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003742 }
3743 spin_unlock(&cache->lock);
3744 }
3745
Zhaolei55e3a602015-08-05 16:43:30 +08003746 /*
Filipe Manana2473d242020-05-08 11:01:10 +01003747 * Make sure that while we are scrubbing the corresponding block
3748 * group doesn't get its logical address and its device extents
3749 * reused for another block group, which can possibly be of a
3750 * different type and different profile. We do this to prevent
3751 * false error detections and crashes due to bogus attempts to
3752 * repair extents.
3753 */
3754 spin_lock(&cache->lock);
3755 if (cache->removed) {
3756 spin_unlock(&cache->lock);
3757 btrfs_put_block_group(cache);
3758 goto skip;
3759 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003760 btrfs_freeze_block_group(cache);
Filipe Manana2473d242020-05-08 11:01:10 +01003761 spin_unlock(&cache->lock);
3762
3763 /*
Zhaolei55e3a602015-08-05 16:43:30 +08003764 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3765 * to avoid deadlock caused by:
3766 * btrfs_inc_block_group_ro()
3767 * -> btrfs_wait_for_commit()
3768 * -> btrfs_commit_transaction()
3769 * -> btrfs_scrub_pause()
3770 */
3771 scrub_pause_on(fs_info);
Qu Wenruob12de522019-11-15 10:09:00 +08003772
3773 /*
3774 * Don't do chunk preallocation for scrub.
3775 *
3776 * This is especially important for SYSTEM bgs, or we can hit
3777 * -EFBIG from btrfs_finish_chunk_alloc() like:
3778 * 1. The only SYSTEM bg is marked RO.
3779 * Since SYSTEM bg is small, that's pretty common.
3780 * 2. New SYSTEM bg will be allocated
3781 * Due to regular version will allocate new chunk.
3782 * 3. New SYSTEM bg is empty and will get cleaned up
3783 * Before cleanup really happens, it's marked RO again.
3784 * 4. Empty SYSTEM bg get scrubbed
3785 * We go back to 2.
3786 *
3787 * This can easily boost the amount of SYSTEM chunks if cleaner
3788 * thread can't be triggered fast enough, and use up all space
3789 * of btrfs_super_block::sys_chunk_array
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003790 *
3791 * While for dev replace, we need to try our best to mark block
3792 * group RO, to prevent race between:
3793 * - Write duplication
3794 * Contains latest data
3795 * - Scrub copy
3796 * Contains data from commit tree
3797 *
3798 * If target block group is not marked RO, nocow writes can
3799 * be overwritten by scrub copy, causing data corruption.
3800 * So for dev-replace, it's not allowed to continue if a block
3801 * group is not RO.
Qu Wenruob12de522019-11-15 10:09:00 +08003802 */
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003803 ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
Naohiro Aotade17add2021-02-04 19:22:13 +09003804 if (!ret && sctx->is_dev_replace) {
3805 ret = finish_extent_writes_for_zoned(root, cache);
3806 if (ret) {
3807 btrfs_dec_block_group_ro(cache);
3808 scrub_pause_off(fs_info);
3809 btrfs_put_block_group(cache);
3810 break;
3811 }
3812 }
3813
Zhaolei76a8efa2015-11-17 18:46:17 +08003814 if (ret == 0) {
3815 ro_set = 1;
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003816 } else if (ret == -ENOSPC && !sctx->is_dev_replace) {
Zhaolei76a8efa2015-11-17 18:46:17 +08003817 /*
3818 * btrfs_inc_block_group_ro return -ENOSPC when it
3819 * failed in creating new chunk for metadata.
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003820 * It is not a problem for scrub, because
Zhaolei76a8efa2015-11-17 18:46:17 +08003821 * metadata are always cowed, and our scrub paused
3822 * commit_transactions.
3823 */
3824 ro_set = 0;
Filipe Manana195a49e2021-02-05 12:55:37 +00003825 } else if (ret == -ETXTBSY) {
3826 btrfs_warn(fs_info,
3827 "skipping scrub of block group %llu due to active swapfile",
3828 cache->start);
3829 scrub_pause_off(fs_info);
3830 ret = 0;
3831 goto skip_unfreeze;
Zhaolei76a8efa2015-11-17 18:46:17 +08003832 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003833 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003834 "failed setting block group ro: %d", ret);
Filipe Manana6b7304a2020-05-08 11:01:47 +01003835 btrfs_unfreeze_block_group(cache);
Zhaolei55e3a602015-08-05 16:43:30 +08003836 btrfs_put_block_group(cache);
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003837 scrub_pause_off(fs_info);
Zhaolei55e3a602015-08-05 16:43:30 +08003838 break;
3839 }
3840
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003841 /*
3842 * Now the target block is marked RO, wait for nocow writes to
3843 * finish before dev-replace.
3844 * COW is fine, as COW never overwrites extents in commit tree.
3845 */
3846 if (sctx->is_dev_replace) {
3847 btrfs_wait_nocow_writers(cache);
3848 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
3849 cache->length);
3850 }
3851
3852 scrub_pause_off(fs_info);
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003853 down_write(&dev_replace->rwsem);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003854 dev_replace->cursor_right = found_key.offset + length;
3855 dev_replace->cursor_left = found_key.offset;
3856 dev_replace->item_needs_writeback = 1;
David Sterbacb5583d2018-09-07 16:11:23 +02003857 up_write(&dev_replace->rwsem);
3858
Zhao Lei8c204c92015-08-19 15:02:40 +08003859 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Omar Sandoval32934282018-08-14 11:09:52 -07003860 found_key.offset, cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003861
3862 /*
3863 * flush, submit all pending read and write bios, afterwards
3864 * wait for them.
3865 * Note that in the dev replace case, a read request causes
3866 * write requests that are submitted in the read completion
3867 * worker. Therefore in the current situation, it is required
3868 * that all write requests are flushed, so that all read and
3869 * write requests are really completed when bios_in_flight
3870 * changes to 0.
3871 */
David Sterba2073c4c2017-03-31 17:12:51 +02003872 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003873 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003874 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003875 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003876 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003877
3878 wait_event(sctx->list_wait,
3879 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003880
3881 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003882
3883 /*
3884 * must be called before we decrease @scrub_paused.
3885 * make sure we don't block transaction commit while
3886 * we are waiting pending workers finished.
3887 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003888 wait_event(sctx->list_wait,
3889 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003890 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003891
Zhaoleib708ce92015-08-05 16:43:29 +08003892 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003893
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003894 if (sctx->is_dev_replace &&
3895 !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
3896 cache, found_key.offset))
3897 ro_set = 0;
3898
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003899 down_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003900 dev_replace->cursor_left = dev_replace->cursor_right;
3901 dev_replace->item_needs_writeback = 1;
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003902 up_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003903
Zhaolei76a8efa2015-11-17 18:46:17 +08003904 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003905 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003906
Filipe Manana758f2df2015-11-19 11:45:48 +00003907 /*
3908 * We might have prevented the cleaner kthread from deleting
3909 * this block group if it was already unused because we raced
3910 * and set it to RO mode first. So add it back to the unused
3911 * list, otherwise it might not ever be deleted unless a manual
3912 * balance is triggered or it becomes used and unused again.
3913 */
3914 spin_lock(&cache->lock);
3915 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
David Sterbabf38be62019-10-23 18:48:11 +02003916 cache->used == 0) {
Filipe Manana758f2df2015-11-19 11:45:48 +00003917 spin_unlock(&cache->lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003918 if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
3919 btrfs_discard_queue_work(&fs_info->discard_ctl,
3920 cache);
3921 else
3922 btrfs_mark_bg_unused(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003923 } else {
3924 spin_unlock(&cache->lock);
3925 }
Filipe Manana195a49e2021-02-05 12:55:37 +00003926skip_unfreeze:
Filipe Manana6b7304a2020-05-08 11:01:47 +01003927 btrfs_unfreeze_block_group(cache);
Arne Jansena2de7332011-03-08 14:14:00 +01003928 btrfs_put_block_group(cache);
3929 if (ret)
3930 break;
Omar Sandoval32934282018-08-14 11:09:52 -07003931 if (sctx->is_dev_replace &&
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003932 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003933 ret = -EIO;
3934 break;
3935 }
3936 if (sctx->stat.malloc_errors > 0) {
3937 ret = -ENOMEM;
3938 break;
3939 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003940skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003941 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003942 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003943 }
3944
Arne Jansena2de7332011-03-08 14:14:00 +01003945 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003946
Zhaolei55e3a602015-08-05 16:43:30 +08003947 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003948}
3949
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003950static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3951 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003952{
3953 int i;
3954 u64 bytenr;
3955 u64 gen;
3956 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003957 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003958
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003959 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Josef Bacikfbabd4a2020-07-21 10:38:37 -04003960 return -EROFS;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003961
Miao Xie5f546062014-07-24 11:37:09 +08003962 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003964 gen = scrub_dev->generation;
3965 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003966 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003967
3968 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3969 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003970 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3971 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003972 break;
Naohiro Aota12659252020-11-10 20:26:14 +09003973 if (!btrfs_check_super_location(scrub_dev, bytenr))
3974 continue;
Arne Jansena2de7332011-03-08 14:14:00 +01003975
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003976 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003977 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Qu Wenruo96e63a42020-11-03 21:31:02 +08003978 NULL, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003979 if (ret)
3980 return ret;
3981 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003982 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003983
3984 return 0;
3985}
3986
Josef Bacike89c4a92020-08-10 11:42:29 -04003987static void scrub_workers_put(struct btrfs_fs_info *fs_info)
3988{
3989 if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
3990 &fs_info->scrub_lock)) {
3991 struct btrfs_workqueue *scrub_workers = NULL;
3992 struct btrfs_workqueue *scrub_wr_comp = NULL;
3993 struct btrfs_workqueue *scrub_parity = NULL;
3994
3995 scrub_workers = fs_info->scrub_workers;
3996 scrub_wr_comp = fs_info->scrub_wr_completion_workers;
3997 scrub_parity = fs_info->scrub_parity_workers;
3998
3999 fs_info->scrub_workers = NULL;
4000 fs_info->scrub_wr_completion_workers = NULL;
4001 fs_info->scrub_parity_workers = NULL;
4002 mutex_unlock(&fs_info->scrub_lock);
4003
4004 btrfs_destroy_workqueue(scrub_workers);
4005 btrfs_destroy_workqueue(scrub_wr_comp);
4006 btrfs_destroy_workqueue(scrub_parity);
4007 }
4008}
4009
Arne Jansena2de7332011-03-08 14:14:00 +01004010/*
4011 * get a reference count on fs_info->scrub_workers. start worker if necessary
4012 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004013static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4014 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004015{
Josef Bacike89c4a92020-08-10 11:42:29 -04004016 struct btrfs_workqueue *scrub_workers = NULL;
4017 struct btrfs_workqueue *scrub_wr_comp = NULL;
4018 struct btrfs_workqueue *scrub_parity = NULL;
David Sterba6f011052015-02-16 18:34:01 +01004019 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004020 int max_active = fs_info->thread_pool_size;
Josef Bacike89c4a92020-08-10 11:42:29 -04004021 int ret = -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004022
Josef Bacike89c4a92020-08-10 11:42:29 -04004023 if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
4024 return 0;
Anand Jaineb4318e2019-01-30 14:45:01 +08004025
Josef Bacike89c4a92020-08-10 11:42:29 -04004026 scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", flags,
4027 is_dev_replace ? 1 : max_active, 4);
4028 if (!scrub_workers)
4029 goto fail_scrub_workers;
4030
4031 scrub_wr_comp = btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
4032 max_active, 2);
4033 if (!scrub_wr_comp)
4034 goto fail_scrub_wr_completion_workers;
4035
4036 scrub_parity = btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
4037 max_active, 2);
4038 if (!scrub_parity)
4039 goto fail_scrub_parity_workers;
4040
4041 mutex_lock(&fs_info->scrub_lock);
Anand Jainff09c4c2019-01-30 14:45:02 +08004042 if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
Josef Bacike89c4a92020-08-10 11:42:29 -04004043 ASSERT(fs_info->scrub_workers == NULL &&
4044 fs_info->scrub_wr_completion_workers == NULL &&
4045 fs_info->scrub_parity_workers == NULL);
4046 fs_info->scrub_workers = scrub_workers;
4047 fs_info->scrub_wr_completion_workers = scrub_wr_comp;
4048 fs_info->scrub_parity_workers = scrub_parity;
Anand Jainff09c4c2019-01-30 14:45:02 +08004049 refcount_set(&fs_info->scrub_workers_refcnt, 1);
Josef Bacike89c4a92020-08-10 11:42:29 -04004050 mutex_unlock(&fs_info->scrub_lock);
4051 return 0;
Arne Jansen632dd772011-06-10 12:07:07 +02004052 }
Josef Bacike89c4a92020-08-10 11:42:29 -04004053 /* Other thread raced in and created the workers for us */
4054 refcount_inc(&fs_info->scrub_workers_refcnt);
4055 mutex_unlock(&fs_info->scrub_lock);
Zhao Leie82afc52015-06-12 20:36:58 +08004056
Josef Bacike89c4a92020-08-10 11:42:29 -04004057 ret = 0;
4058 btrfs_destroy_workqueue(scrub_parity);
Zhao Leie82afc52015-06-12 20:36:58 +08004059fail_scrub_parity_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004060 btrfs_destroy_workqueue(scrub_wr_comp);
Zhao Leie82afc52015-06-12 20:36:58 +08004061fail_scrub_wr_completion_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004062 btrfs_destroy_workqueue(scrub_workers);
Zhao Leie82afc52015-06-12 20:36:58 +08004063fail_scrub_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004064 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004065}
4066
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004067int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4068 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004069 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004070{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004071 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004072 int ret;
4073 struct btrfs_device *dev;
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004074 unsigned int nofs_flag;
Arne Jansena2de7332011-03-08 14:14:00 +01004075
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004076 if (btrfs_fs_closing(fs_info))
David Sterba6c3abed2019-02-25 19:57:41 +01004077 return -EAGAIN;
Arne Jansena2de7332011-03-08 14:14:00 +01004078
Jeff Mahoneyda170662016-06-15 09:22:56 -04004079 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004080 /*
4081 * in this case scrub is unable to calculate the checksum
4082 * the way scrub is implemented. Do not handle this
4083 * situation at all because it won't ever happen.
4084 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004085 btrfs_err(fs_info,
4086 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004087 fs_info->nodesize,
4088 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004089 return -EINVAL;
4090 }
4091
Jeff Mahoneyda170662016-06-15 09:22:56 -04004092 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004093 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004094 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004095 /*
4096 * would exhaust the array bounds of pagev member in
4097 * struct scrub_block
4098 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004099 btrfs_err(fs_info,
4100 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004101 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004102 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004103 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004104 SCRUB_MAX_PAGES_PER_BLOCK);
4105 return -EINVAL;
4106 }
4107
David Sterba0e94c4f42018-12-04 16:11:56 +01004108 /* Allocate outside of device_list_mutex */
4109 sctx = scrub_setup_ctx(fs_info, is_dev_replace);
4110 if (IS_ERR(sctx))
4111 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004112
Josef Bacike89c4a92020-08-10 11:42:29 -04004113 ret = scrub_workers_get(fs_info, is_dev_replace);
4114 if (ret)
4115 goto out_free_ctx;
4116
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004117 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jainb2598ed2020-11-03 13:49:43 +08004118 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
Anand Jaine6e674b2017-12-04 12:54:54 +08004119 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4120 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004121 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004122 ret = -ENODEV;
Josef Bacike89c4a92020-08-10 11:42:29 -04004123 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004124 }
Arne Jansena2de7332011-03-08 14:14:00 +01004125
Anand Jainebbede42017-12-04 12:54:52 +08004126 if (!is_dev_replace && !readonly &&
4127 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004128 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterbaa4852cf2020-07-09 11:25:40 +02004129 btrfs_err_in_rcu(fs_info,
4130 "scrub on devid %llu: filesystem on %s is not writable",
4131 devid, rcu_str_deref(dev->name));
David Sterba0e94c4f42018-12-04 16:11:56 +01004132 ret = -EROFS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004133 goto out;
Miao Xie5d68da32014-07-24 11:37:07 +08004134 }
4135
Wang Shilong3b7a0162013-10-12 02:11:12 +08004136 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004137 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004138 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004139 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004140 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004141 ret = -EIO;
Josef Bacike89c4a92020-08-10 11:42:29 -04004142 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004143 }
4144
David Sterbacb5583d2018-09-07 16:11:23 +02004145 down_read(&fs_info->dev_replace.rwsem);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004146 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01004147 (!is_dev_replace &&
4148 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterbacb5583d2018-09-07 16:11:23 +02004149 up_read(&fs_info->dev_replace.rwsem);
Arne Jansena2de7332011-03-08 14:14:00 +01004150 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004151 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004152 ret = -EINPROGRESS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004153 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004154 }
David Sterbacb5583d2018-09-07 16:11:23 +02004155 up_read(&fs_info->dev_replace.rwsem);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004156
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004157 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08004158 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004159 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004160
Wang Shilong3cb09292013-12-04 21:15:19 +08004161 /*
4162 * checking @scrub_pause_req here, we can avoid
4163 * race between committing transaction and scrubbing.
4164 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004165 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004166 atomic_inc(&fs_info->scrubs_running);
4167 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004168
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004169 /*
4170 * In order to avoid deadlock with reclaim when there is a transaction
4171 * trying to pause scrub, make sure we use GFP_NOFS for all the
4172 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
4173 * invoked by our callees. The pausing request is done when the
4174 * transaction commit starts, and it blocks the transaction until scrub
4175 * is paused (done at specific points at scrub_stripe() or right above
4176 * before incrementing fs_info->scrubs_running).
4177 */
4178 nofs_flag = memalloc_nofs_save();
Stefan Behrensff023aa2012-11-06 11:43:11 +01004179 if (!is_dev_replace) {
Anand Jaind1e14422019-01-03 16:17:40 +08004180 btrfs_info(fs_info, "scrub: started on devid %llu", devid);
Wang Shilong9b011ad2013-10-25 19:12:02 +08004181 /*
4182 * by holding device list mutex, we can
4183 * kick off writing super in log tree sync.
4184 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004185 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004186 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004187 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004188 }
Arne Jansena2de7332011-03-08 14:14:00 +01004189
4190 if (!ret)
Omar Sandoval32934282018-08-14 11:09:52 -07004191 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004192 memalloc_nofs_restore(nofs_flag);
Arne Jansena2de7332011-03-08 14:14:00 +01004193
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004194 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004195 atomic_dec(&fs_info->scrubs_running);
4196 wake_up(&fs_info->scrub_pause_wait);
4197
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004198 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004199
Arne Jansena2de7332011-03-08 14:14:00 +01004200 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004201 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004202
Anand Jaind1e14422019-01-03 16:17:40 +08004203 if (!is_dev_replace)
4204 btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
4205 ret ? "not finished" : "finished", devid, ret);
4206
Arne Jansena2de7332011-03-08 14:14:00 +01004207 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004208 dev->scrub_ctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004209 mutex_unlock(&fs_info->scrub_lock);
4210
Josef Bacike89c4a92020-08-10 11:42:29 -04004211 scrub_workers_put(fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +00004212 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004213
4214 return ret;
Josef Bacike89c4a92020-08-10 11:42:29 -04004215out:
4216 scrub_workers_put(fs_info);
David Sterba0e94c4f42018-12-04 16:11:56 +01004217out_free_ctx:
4218 scrub_free_ctx(sctx);
4219
4220 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004221}
4222
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004223void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004224{
Arne Jansena2de7332011-03-08 14:14:00 +01004225 mutex_lock(&fs_info->scrub_lock);
4226 atomic_inc(&fs_info->scrub_pause_req);
4227 while (atomic_read(&fs_info->scrubs_paused) !=
4228 atomic_read(&fs_info->scrubs_running)) {
4229 mutex_unlock(&fs_info->scrub_lock);
4230 wait_event(fs_info->scrub_pause_wait,
4231 atomic_read(&fs_info->scrubs_paused) ==
4232 atomic_read(&fs_info->scrubs_running));
4233 mutex_lock(&fs_info->scrub_lock);
4234 }
4235 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004236}
4237
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004238void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004239{
Arne Jansena2de7332011-03-08 14:14:00 +01004240 atomic_dec(&fs_info->scrub_pause_req);
4241 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004242}
4243
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004244int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004245{
Arne Jansena2de7332011-03-08 14:14:00 +01004246 mutex_lock(&fs_info->scrub_lock);
4247 if (!atomic_read(&fs_info->scrubs_running)) {
4248 mutex_unlock(&fs_info->scrub_lock);
4249 return -ENOTCONN;
4250 }
4251
4252 atomic_inc(&fs_info->scrub_cancel_req);
4253 while (atomic_read(&fs_info->scrubs_running)) {
4254 mutex_unlock(&fs_info->scrub_lock);
4255 wait_event(fs_info->scrub_pause_wait,
4256 atomic_read(&fs_info->scrubs_running) == 0);
4257 mutex_lock(&fs_info->scrub_lock);
4258 }
4259 atomic_dec(&fs_info->scrub_cancel_req);
4260 mutex_unlock(&fs_info->scrub_lock);
4261
4262 return 0;
4263}
4264
David Sterba163e97e2019-03-20 16:32:55 +01004265int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004266{
David Sterba163e97e2019-03-20 16:32:55 +01004267 struct btrfs_fs_info *fs_info = dev->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004268 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004269
4270 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004271 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004272 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004273 mutex_unlock(&fs_info->scrub_lock);
4274 return -ENOTCONN;
4275 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004276 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004277 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004278 mutex_unlock(&fs_info->scrub_lock);
4279 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004280 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004281 mutex_lock(&fs_info->scrub_lock);
4282 }
4283 mutex_unlock(&fs_info->scrub_lock);
4284
4285 return 0;
4286}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004287
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004288int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004289 struct btrfs_scrub_progress *progress)
4290{
4291 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004292 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004293
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004294 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jainb2598ed2020-11-03 13:49:43 +08004295 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004296 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004297 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004298 if (sctx)
4299 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004300 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004301
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004302 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004303}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004304
4305static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +08004306 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004307 u64 *extent_physical,
4308 struct btrfs_device **extent_dev,
4309 int *extent_mirror_num)
4310{
4311 u64 mapped_length;
4312 struct btrfs_bio *bbio = NULL;
4313 int ret;
4314
4315 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004316 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004317 &mapped_length, &bbio, 0);
4318 if (ret || !bbio || mapped_length < extent_len ||
4319 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004320 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004321 return;
4322 }
4323
4324 *extent_physical = bbio->stripes[0].physical;
4325 *extent_mirror_num = bbio->mirror_num;
4326 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004327 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004328}