xref: /linux/fs/btrfs/scrub.c (revision 8341374f67f6e6350de98baaf5b05bca88f4ad81)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2011, 2012 STRATO.  All rights reserved.
4  */
5 
6 #include <linux/blkdev.h>
7 #include <linux/ratelimit.h>
8 #include <linux/sched/mm.h>
9 #include <crypto/hash.h>
10 #include "ctree.h"
11 #include "discard.h"
12 #include "volumes.h"
13 #include "disk-io.h"
14 #include "ordered-data.h"
15 #include "transaction.h"
16 #include "backref.h"
17 #include "extent_io.h"
18 #include "dev-replace.h"
19 #include "raid56.h"
20 #include "block-group.h"
21 #include "zoned.h"
22 #include "fs.h"
23 #include "accessors.h"
24 #include "file-item.h"
25 #include "scrub.h"
26 #include "raid-stripe-tree.h"
27 
28 /*
29  * This is only the first step towards a full-features scrub. It reads all
30  * extent and super block and verifies the checksums. In case a bad checksum
31  * is found or the extent cannot be read, good data will be written back if
32  * any can be found.
33  *
34  * Future enhancements:
35  *  - In case an unrepairable extent is encountered, track which files are
36  *    affected and report them
37  *  - track and record media errors, throw out bad devices
38  *  - add a mode to also read unallocated space
39  */
40 
41 struct scrub_ctx;
42 
43 /*
44  * The following value only influences the performance.
45  *
46  * This determines how many stripes would be submitted in one go,
47  * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).
48  */
49 #define SCRUB_STRIPES_PER_GROUP		8
50 
51 /*
52  * How many groups we have for each sctx.
53  *
54  * This would be 8M per device, the same value as the old scrub in-flight bios
55  * size limit.
56  */
57 #define SCRUB_GROUPS_PER_SCTX		16
58 
59 #define SCRUB_TOTAL_STRIPES		(SCRUB_GROUPS_PER_SCTX * SCRUB_STRIPES_PER_GROUP)
60 
61 /*
62  * The following value times PAGE_SIZE needs to be large enough to match the
63  * largest node/leaf/sector size that shall be supported.
64  */
65 #define SCRUB_MAX_SECTORS_PER_BLOCK	(BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K)
66 
67 /* Represent one sector and its needed info to verify the content. */
68 struct scrub_sector_verification {
69 	union {
70 		/*
71 		 * Csum pointer for data csum verification.  Should point to a
72 		 * sector csum inside scrub_stripe::csums.
73 		 *
74 		 * NULL if this data sector has no csum.
75 		 */
76 		u8 *csum;
77 
78 		/*
79 		 * Extra info for metadata verification.  All sectors inside a
80 		 * tree block share the same generation.
81 		 */
82 		u64 generation;
83 	};
84 };
85 
86 enum scrub_stripe_flags {
87 	/* Set when @mirror_num, @dev, @physical and @logical are set. */
88 	SCRUB_STRIPE_FLAG_INITIALIZED,
89 
90 	/* Set when the read-repair is finished. */
91 	SCRUB_STRIPE_FLAG_REPAIR_DONE,
92 
93 	/*
94 	 * Set for data stripes if it's triggered from P/Q stripe.
95 	 * During such scrub, we should not report errors in data stripes, nor
96 	 * update the accounting.
97 	 */
98 	SCRUB_STRIPE_FLAG_NO_REPORT,
99 };
100 
101 /*
102  * We have multiple bitmaps for one scrub_stripe.
103  * However each bitmap has at most (BTRFS_STRIPE_LEN / blocksize) bits,
104  * which is normally 16, and much smaller than BITS_PER_LONG (32 or 64).
105  *
106  * So to reduce memory usage for each scrub_stripe, we pack those bitmaps
107  * into a larger one.
108  *
109  * These enum records where the sub-bitmap are inside the larger one.
110  * Each subbitmap starts at scrub_bitmap_nr_##name * nr_sectors bit.
111  */
112 enum {
113 	/* Which blocks are covered by extent items. */
114 	scrub_bitmap_nr_has_extent = 0,
115 
116 	/* Which blocks are metadata. */
117 	scrub_bitmap_nr_is_metadata,
118 
119 	/*
120 	 * Which blocks have errors, including IO, csum, and metadata
121 	 * errors.
122 	 * This sub-bitmap is the OR results of the next few error related
123 	 * sub-bitmaps.
124 	 */
125 	scrub_bitmap_nr_error,
126 	scrub_bitmap_nr_io_error,
127 	scrub_bitmap_nr_csum_error,
128 	scrub_bitmap_nr_meta_error,
129 	scrub_bitmap_nr_meta_gen_error,
130 	scrub_bitmap_nr_last,
131 };
132 
133 #define SCRUB_STRIPE_MAX_FOLIOS		(BTRFS_STRIPE_LEN / PAGE_SIZE)
134 
135 /*
136  * Represent one contiguous range with a length of BTRFS_STRIPE_LEN.
137  */
138 struct scrub_stripe {
139 	struct scrub_ctx *sctx;
140 	struct btrfs_block_group *bg;
141 
142 	struct folio *folios[SCRUB_STRIPE_MAX_FOLIOS];
143 	struct scrub_sector_verification *sectors;
144 
145 	struct btrfs_device *dev;
146 	u64 logical;
147 	u64 physical;
148 
149 	u16 mirror_num;
150 
151 	/* Should be BTRFS_STRIPE_LEN / sectorsize. */
152 	u16 nr_sectors;
153 
154 	/*
155 	 * How many data/meta extents are in this stripe.  Only for scrub status
156 	 * reporting purposes.
157 	 */
158 	u16 nr_data_extents;
159 	u16 nr_meta_extents;
160 
161 	atomic_t pending_io;
162 	wait_queue_head_t io_wait;
163 	wait_queue_head_t repair_wait;
164 
165 	/*
166 	 * Indicate the states of the stripe.  Bits are defined in
167 	 * scrub_stripe_flags enum.
168 	 */
169 	unsigned long state;
170 
171 	/* The large bitmap contains all the sub-bitmaps. */
172 	unsigned long bitmaps[BITS_TO_LONGS(scrub_bitmap_nr_last *
173 					    (BTRFS_STRIPE_LEN / BTRFS_MIN_BLOCKSIZE))];
174 
175 	/*
176 	 * For writeback (repair or replace) error reporting.
177 	 * This one is protected by a spinlock, thus can not be packed into
178 	 * the larger bitmap.
179 	 */
180 	unsigned long write_error_bitmap;
181 
182 	/* Writeback can be concurrent, thus we need to protect the bitmap. */
183 	spinlock_t write_error_lock;
184 
185 	/*
186 	 * Checksum for the whole stripe if this stripe is inside a data block
187 	 * group.
188 	 */
189 	u8 *csums;
190 
191 	struct work_struct work;
192 };
193 
194 struct scrub_ctx {
195 	struct scrub_stripe	stripes[SCRUB_TOTAL_STRIPES];
196 	struct scrub_stripe	*raid56_data_stripes;
197 	struct btrfs_fs_info	*fs_info;
198 	struct btrfs_path	extent_path;
199 	struct btrfs_path	csum_path;
200 	int			first_free;
201 	int			cur_stripe;
202 	atomic_t		cancel_req;
203 	int			readonly;
204 
205 	/* State of IO submission throttling affecting the associated device */
206 	ktime_t			throttle_deadline;
207 	u64			throttle_sent;
208 
209 	bool			is_dev_replace;
210 	u64			write_pointer;
211 
212 	struct mutex            wr_lock;
213 	struct btrfs_device     *wr_tgtdev;
214 
215 	/*
216 	 * statistics
217 	 */
218 	struct btrfs_scrub_progress stat;
219 	spinlock_t		stat_lock;
220 
221 	/*
222 	 * Use a ref counter to avoid use-after-free issues. Scrub workers
223 	 * decrement bios_in_flight and workers_pending and then do a wakeup
224 	 * on the list_wait wait queue. We must ensure the main scrub task
225 	 * doesn't free the scrub context before or while the workers are
226 	 * doing the wakeup() call.
227 	 */
228 	refcount_t              refs;
229 };
230 
231 #define scrub_calc_start_bit(stripe, name, block_nr)			\
232 ({									\
233 	unsigned int __start_bit;					\
234 									\
235 	ASSERT(block_nr < stripe->nr_sectors,				\
236 		"nr_sectors=%u block_nr=%u", stripe->nr_sectors, block_nr); \
237 	__start_bit = scrub_bitmap_nr_##name * stripe->nr_sectors + block_nr; \
238 	__start_bit;							\
239 })
240 
241 #define IMPLEMENT_SCRUB_BITMAP_OPS(name)				\
242 static inline void scrub_bitmap_set_##name(struct scrub_stripe *stripe,	\
243 				    unsigned int block_nr,		\
244 				    unsigned int nr_blocks)		\
245 {									\
246 	const unsigned int start_bit = scrub_calc_start_bit(stripe,	\
247 							    name, block_nr); \
248 									\
249 	bitmap_set(stripe->bitmaps, start_bit, nr_blocks);		\
250 }									\
251 static inline void scrub_bitmap_clear_##name(struct scrub_stripe *stripe, \
252 				      unsigned int block_nr,		\
253 				      unsigned int nr_blocks)		\
254 {									\
255 	const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
256 							    block_nr);	\
257 									\
258 	bitmap_clear(stripe->bitmaps, start_bit, nr_blocks);		\
259 }									\
260 static inline bool scrub_bitmap_test_bit_##name(struct scrub_stripe *stripe, \
261 				     unsigned int block_nr)		\
262 {									\
263 	const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
264 							    block_nr);	\
265 									\
266 	return test_bit(start_bit, stripe->bitmaps);			\
267 }									\
268 static inline void scrub_bitmap_set_bit_##name(struct scrub_stripe *stripe, \
269 				     unsigned int block_nr)		\
270 {									\
271 	const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
272 							    block_nr);	\
273 									\
274 	set_bit(start_bit, stripe->bitmaps);				\
275 }									\
276 static inline void scrub_bitmap_clear_bit_##name(struct scrub_stripe *stripe, \
277 				     unsigned int block_nr)		\
278 {									\
279 	const unsigned int start_bit = scrub_calc_start_bit(stripe, name, \
280 							    block_nr);	\
281 									\
282 	clear_bit(start_bit, stripe->bitmaps);				\
283 }									\
284 static inline unsigned long scrub_bitmap_read_##name(struct scrub_stripe *stripe) \
285 {									\
286 	const unsigned int nr_blocks = stripe->nr_sectors;		\
287 									\
288 	ASSERT(nr_blocks > 0 && nr_blocks <= BITS_PER_LONG,		\
289 	       "nr_blocks=%u BITS_PER_LONG=%u",				\
290 	       nr_blocks, BITS_PER_LONG);				\
291 									\
292 	return bitmap_read(stripe->bitmaps, nr_blocks * scrub_bitmap_nr_##name, \
293 			   stripe->nr_sectors);				\
294 }									\
295 static inline bool scrub_bitmap_empty_##name(struct scrub_stripe *stripe) \
296 {									\
297 	unsigned long bitmap = scrub_bitmap_read_##name(stripe);	\
298 									\
299 	return bitmap_empty(&bitmap, stripe->nr_sectors);		\
300 }									\
301 static inline unsigned int scrub_bitmap_weight_##name(struct scrub_stripe *stripe) \
302 {									\
303 	unsigned long bitmap = scrub_bitmap_read_##name(stripe);	\
304 									\
305 	return bitmap_weight(&bitmap, stripe->nr_sectors);		\
306 }
307 IMPLEMENT_SCRUB_BITMAP_OPS(has_extent);
308 IMPLEMENT_SCRUB_BITMAP_OPS(is_metadata);
309 IMPLEMENT_SCRUB_BITMAP_OPS(error);
310 IMPLEMENT_SCRUB_BITMAP_OPS(io_error);
311 IMPLEMENT_SCRUB_BITMAP_OPS(csum_error);
312 IMPLEMENT_SCRUB_BITMAP_OPS(meta_error);
313 IMPLEMENT_SCRUB_BITMAP_OPS(meta_gen_error);
314 
315 struct scrub_warning {
316 	struct btrfs_path	*path;
317 	u64			extent_item_size;
318 	const char		*errstr;
319 	u64			physical;
320 	u64			logical;
321 	struct btrfs_device	*dev;
322 };
323 
324 struct scrub_error_records {
325 	/*
326 	 * Bitmap recording which blocks hit errors (IO/csum/...) during the
327 	 * initial read.
328 	 */
329 	unsigned long init_error_bitmap;
330 
331 	unsigned int nr_io_errors;
332 	unsigned int nr_csum_errors;
333 	unsigned int nr_meta_errors;
334 	unsigned int nr_meta_gen_errors;
335 };
336 
release_scrub_stripe(struct scrub_stripe * stripe)337 static void release_scrub_stripe(struct scrub_stripe *stripe)
338 {
339 	if (!stripe)
340 		return;
341 
342 	for (int i = 0; i < SCRUB_STRIPE_MAX_FOLIOS; i++) {
343 		if (stripe->folios[i])
344 			folio_put(stripe->folios[i]);
345 		stripe->folios[i] = NULL;
346 	}
347 	kfree(stripe->sectors);
348 	kfree(stripe->csums);
349 	stripe->sectors = NULL;
350 	stripe->csums = NULL;
351 	stripe->sctx = NULL;
352 	stripe->state = 0;
353 }
354 
init_scrub_stripe(struct btrfs_fs_info * fs_info,struct scrub_stripe * stripe)355 static int init_scrub_stripe(struct btrfs_fs_info *fs_info,
356 			     struct scrub_stripe *stripe)
357 {
358 	const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
359 	int ret;
360 
361 	memset(stripe, 0, sizeof(*stripe));
362 
363 	stripe->nr_sectors = BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
364 	stripe->state = 0;
365 
366 	init_waitqueue_head(&stripe->io_wait);
367 	init_waitqueue_head(&stripe->repair_wait);
368 	atomic_set(&stripe->pending_io, 0);
369 	spin_lock_init(&stripe->write_error_lock);
370 
371 	ASSERT(BTRFS_STRIPE_LEN >> min_folio_shift <= SCRUB_STRIPE_MAX_FOLIOS);
372 	ret = btrfs_alloc_folio_array(BTRFS_STRIPE_LEN >> min_folio_shift,
373 				      fs_info->block_min_order, stripe->folios);
374 	if (ret < 0)
375 		goto error;
376 
377 	stripe->sectors = kcalloc(stripe->nr_sectors,
378 				  sizeof(struct scrub_sector_verification),
379 				  GFP_KERNEL);
380 	if (!stripe->sectors)
381 		goto error;
382 
383 	stripe->csums = kcalloc(BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
384 				fs_info->csum_size, GFP_KERNEL);
385 	if (!stripe->csums)
386 		goto error;
387 	return 0;
388 error:
389 	release_scrub_stripe(stripe);
390 	return -ENOMEM;
391 }
392 
wait_scrub_stripe_io(struct scrub_stripe * stripe)393 static void wait_scrub_stripe_io(struct scrub_stripe *stripe)
394 {
395 	wait_event(stripe->io_wait, atomic_read(&stripe->pending_io) == 0);
396 }
397 
398 static void scrub_put_ctx(struct scrub_ctx *sctx);
399 
__scrub_blocked_if_needed(struct btrfs_fs_info * fs_info)400 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
401 {
402 	while (atomic_read(&fs_info->scrub_pause_req)) {
403 		mutex_unlock(&fs_info->scrub_lock);
404 		wait_event(fs_info->scrub_pause_wait,
405 		   atomic_read(&fs_info->scrub_pause_req) == 0);
406 		mutex_lock(&fs_info->scrub_lock);
407 	}
408 }
409 
scrub_pause_on(struct btrfs_fs_info * fs_info)410 static void scrub_pause_on(struct btrfs_fs_info *fs_info)
411 {
412 	atomic_inc(&fs_info->scrubs_paused);
413 	wake_up(&fs_info->scrub_pause_wait);
414 }
415 
scrub_pause_off(struct btrfs_fs_info * fs_info)416 static void scrub_pause_off(struct btrfs_fs_info *fs_info)
417 {
418 	mutex_lock(&fs_info->scrub_lock);
419 	__scrub_blocked_if_needed(fs_info);
420 	atomic_dec(&fs_info->scrubs_paused);
421 	mutex_unlock(&fs_info->scrub_lock);
422 
423 	wake_up(&fs_info->scrub_pause_wait);
424 }
425 
scrub_blocked_if_needed(struct btrfs_fs_info * fs_info)426 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
427 {
428 	scrub_pause_on(fs_info);
429 	scrub_pause_off(fs_info);
430 }
431 
scrub_free_ctx(struct scrub_ctx * sctx)432 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
433 {
434 	int i;
435 
436 	if (!sctx)
437 		return;
438 
439 	for (i = 0; i < SCRUB_TOTAL_STRIPES; i++)
440 		release_scrub_stripe(&sctx->stripes[i]);
441 
442 	kvfree(sctx);
443 }
444 
scrub_put_ctx(struct scrub_ctx * sctx)445 static void scrub_put_ctx(struct scrub_ctx *sctx)
446 {
447 	if (refcount_dec_and_test(&sctx->refs))
448 		scrub_free_ctx(sctx);
449 }
450 
scrub_setup_ctx(struct btrfs_fs_info * fs_info,bool is_dev_replace)451 static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
452 		struct btrfs_fs_info *fs_info, bool is_dev_replace)
453 {
454 	struct scrub_ctx *sctx;
455 	int		i;
456 
457 	/* Since sctx has inline 128 stripes, it can go beyond 64K easily.  Use
458 	 * kvzalloc().
459 	 */
460 	sctx = kvzalloc(sizeof(*sctx), GFP_KERNEL);
461 	if (!sctx)
462 		goto nomem;
463 	refcount_set(&sctx->refs, 1);
464 	sctx->is_dev_replace = is_dev_replace;
465 	sctx->fs_info = fs_info;
466 	sctx->extent_path.search_commit_root = 1;
467 	sctx->extent_path.skip_locking = 1;
468 	sctx->csum_path.search_commit_root = 1;
469 	sctx->csum_path.skip_locking = 1;
470 	for (i = 0; i < SCRUB_TOTAL_STRIPES; i++) {
471 		int ret;
472 
473 		ret = init_scrub_stripe(fs_info, &sctx->stripes[i]);
474 		if (ret < 0)
475 			goto nomem;
476 		sctx->stripes[i].sctx = sctx;
477 	}
478 	sctx->first_free = 0;
479 	atomic_set(&sctx->cancel_req, 0);
480 
481 	spin_lock_init(&sctx->stat_lock);
482 	sctx->throttle_deadline = 0;
483 
484 	mutex_init(&sctx->wr_lock);
485 	if (is_dev_replace) {
486 		WARN_ON(!fs_info->dev_replace.tgtdev);
487 		sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
488 	}
489 
490 	return sctx;
491 
492 nomem:
493 	scrub_free_ctx(sctx);
494 	return ERR_PTR(-ENOMEM);
495 }
496 
scrub_print_warning_inode(u64 inum,u64 offset,u64 num_bytes,u64 root,void * warn_ctx)497 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,
498 				     u64 root, void *warn_ctx)
499 {
500 	u32 nlink;
501 	int ret;
502 	int i;
503 	unsigned nofs_flag;
504 	struct extent_buffer *eb;
505 	struct btrfs_inode_item *inode_item;
506 	struct scrub_warning *swarn = warn_ctx;
507 	struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
508 	struct inode_fs_paths *ipath = NULL;
509 	struct btrfs_root *local_root;
510 	struct btrfs_key key;
511 
512 	local_root = btrfs_get_fs_root(fs_info, root, true);
513 	if (IS_ERR(local_root)) {
514 		ret = PTR_ERR(local_root);
515 		goto err;
516 	}
517 
518 	/*
519 	 * this makes the path point to (inum INODE_ITEM ioff)
520 	 */
521 	key.objectid = inum;
522 	key.type = BTRFS_INODE_ITEM_KEY;
523 	key.offset = 0;
524 
525 	ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
526 	if (ret) {
527 		btrfs_put_root(local_root);
528 		btrfs_release_path(swarn->path);
529 		goto err;
530 	}
531 
532 	eb = swarn->path->nodes[0];
533 	inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
534 					struct btrfs_inode_item);
535 	nlink = btrfs_inode_nlink(eb, inode_item);
536 	btrfs_release_path(swarn->path);
537 
538 	/*
539 	 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
540 	 * uses GFP_NOFS in this context, so we keep it consistent but it does
541 	 * not seem to be strictly necessary.
542 	 */
543 	nofs_flag = memalloc_nofs_save();
544 	ipath = init_ipath(4096, local_root, swarn->path);
545 	memalloc_nofs_restore(nofs_flag);
546 	if (IS_ERR(ipath)) {
547 		btrfs_put_root(local_root);
548 		ret = PTR_ERR(ipath);
549 		ipath = NULL;
550 		goto err;
551 	}
552 	ret = paths_from_inode(inum, ipath);
553 
554 	if (ret < 0)
555 		goto err;
556 
557 	/*
558 	 * we deliberately ignore the bit ipath might have been too small to
559 	 * hold all of the paths here
560 	 */
561 	for (i = 0; i < ipath->fspath->elem_cnt; ++i)
562 		btrfs_warn(fs_info,
563 "scrub: %s at logical %llu on dev %s, physical %llu root %llu inode %llu offset %llu length %u links %u (path: %s)",
564 				  swarn->errstr, swarn->logical,
565 				  btrfs_dev_name(swarn->dev),
566 				  swarn->physical,
567 				  root, inum, offset,
568 				  fs_info->sectorsize, nlink,
569 				  (char *)(unsigned long)ipath->fspath->val[i]);
570 
571 	btrfs_put_root(local_root);
572 	free_ipath(ipath);
573 	return 0;
574 
575 err:
576 	btrfs_warn(fs_info,
577 			  "scrub: %s at logical %llu on dev %s, physical %llu root %llu inode %llu offset %llu: path resolving failed with ret=%d",
578 			  swarn->errstr, swarn->logical,
579 			  btrfs_dev_name(swarn->dev),
580 			  swarn->physical,
581 			  root, inum, offset, ret);
582 
583 	free_ipath(ipath);
584 	return 0;
585 }
586 
scrub_print_common_warning(const char * errstr,struct btrfs_device * dev,bool is_super,u64 logical,u64 physical)587 static void scrub_print_common_warning(const char *errstr, struct btrfs_device *dev,
588 				       bool is_super, u64 logical, u64 physical)
589 {
590 	struct btrfs_fs_info *fs_info = dev->fs_info;
591 	BTRFS_PATH_AUTO_FREE(path);
592 	struct btrfs_key found_key;
593 	struct extent_buffer *eb;
594 	struct btrfs_extent_item *ei;
595 	struct scrub_warning swarn;
596 	u64 flags = 0;
597 	u32 item_size;
598 	int ret;
599 
600 	/* Super block error, no need to search extent tree. */
601 	if (is_super) {
602 		btrfs_warn(fs_info, "scrub: %s on device %s, physical %llu",
603 				  errstr, btrfs_dev_name(dev), physical);
604 		return;
605 	}
606 	path = btrfs_alloc_path();
607 	if (!path)
608 		return;
609 
610 	swarn.physical = physical;
611 	swarn.logical = logical;
612 	swarn.errstr = errstr;
613 	swarn.dev = NULL;
614 
615 	ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
616 				  &flags);
617 	if (ret < 0)
618 		return;
619 
620 	swarn.extent_item_size = found_key.offset;
621 
622 	eb = path->nodes[0];
623 	ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
624 	item_size = btrfs_item_size(eb, path->slots[0]);
625 
626 	if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
627 		unsigned long ptr = 0;
628 		u8 ref_level;
629 		u64 ref_root;
630 
631 		while (true) {
632 			ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
633 						      item_size, &ref_root,
634 						      &ref_level);
635 			if (ret < 0) {
636 				btrfs_warn(fs_info,
637 		   "scrub: failed to resolve tree backref for logical %llu: %d",
638 					   swarn.logical, ret);
639 				break;
640 			}
641 			if (ret > 0)
642 				break;
643 			btrfs_warn(fs_info,
644 "scrub: %s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
645 				errstr, swarn.logical, btrfs_dev_name(dev),
646 				swarn.physical, (ref_level ? "node" : "leaf"),
647 				ref_level, ref_root);
648 		}
649 		btrfs_release_path(path);
650 	} else {
651 		struct btrfs_backref_walk_ctx ctx = { 0 };
652 
653 		btrfs_release_path(path);
654 
655 		ctx.bytenr = found_key.objectid;
656 		ctx.extent_item_pos = swarn.logical - found_key.objectid;
657 		ctx.fs_info = fs_info;
658 
659 		swarn.path = path;
660 		swarn.dev = dev;
661 
662 		iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn);
663 	}
664 }
665 
fill_writer_pointer_gap(struct scrub_ctx * sctx,u64 physical)666 static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
667 {
668 	int ret = 0;
669 	u64 length;
670 
671 	if (!btrfs_is_zoned(sctx->fs_info))
672 		return 0;
673 
674 	if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
675 		return 0;
676 
677 	if (sctx->write_pointer < physical) {
678 		length = physical - sctx->write_pointer;
679 
680 		ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
681 						sctx->write_pointer, length);
682 		if (!ret)
683 			sctx->write_pointer = physical;
684 	}
685 	return ret;
686 }
687 
scrub_stripe_get_kaddr(struct scrub_stripe * stripe,int sector_nr)688 static void *scrub_stripe_get_kaddr(struct scrub_stripe *stripe, int sector_nr)
689 {
690 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
691 	const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
692 	u32 offset = (sector_nr << fs_info->sectorsize_bits);
693 	const struct folio *folio = stripe->folios[offset >> min_folio_shift];
694 
695 	/* stripe->folios[] is allocated by us and no highmem is allowed. */
696 	ASSERT(folio);
697 	ASSERT(!folio_test_highmem(folio));
698 	return folio_address(folio) + offset_in_folio(folio, offset);
699 }
700 
scrub_stripe_get_paddr(struct scrub_stripe * stripe,int sector_nr)701 static phys_addr_t scrub_stripe_get_paddr(struct scrub_stripe *stripe, int sector_nr)
702 {
703 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
704 	const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
705 	u32 offset = (sector_nr << fs_info->sectorsize_bits);
706 	const struct folio *folio = stripe->folios[offset >> min_folio_shift];
707 
708 	/* stripe->folios[] is allocated by us and no highmem is allowed. */
709 	ASSERT(folio);
710 	ASSERT(!folio_test_highmem(folio));
711 	/* And the range must be contained inside the folio. */
712 	ASSERT(offset_in_folio(folio, offset) + fs_info->sectorsize <= folio_size(folio));
713 	return page_to_phys(folio_page(folio, 0)) + offset_in_folio(folio, offset);
714 }
715 
scrub_verify_one_metadata(struct scrub_stripe * stripe,int sector_nr)716 static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr)
717 {
718 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
719 	const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
720 	const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits);
721 	void *first_kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
722 	struct btrfs_header *header = first_kaddr;
723 	SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
724 	u8 on_disk_csum[BTRFS_CSUM_SIZE];
725 	u8 calculated_csum[BTRFS_CSUM_SIZE];
726 
727 	/*
728 	 * Here we don't have a good way to attach the pages (and subpages)
729 	 * to a dummy extent buffer, thus we have to directly grab the members
730 	 * from pages.
731 	 */
732 	memcpy(on_disk_csum, header->csum, fs_info->csum_size);
733 
734 	if (logical != btrfs_stack_header_bytenr(header)) {
735 		scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
736 		scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
737 		btrfs_warn_rl(fs_info,
738 	  "scrub: tree block %llu mirror %u has bad bytenr, has %llu want %llu",
739 			      logical, stripe->mirror_num,
740 			      btrfs_stack_header_bytenr(header), logical);
741 		return;
742 	}
743 	if (memcmp(header->fsid, fs_info->fs_devices->metadata_uuid,
744 		   BTRFS_FSID_SIZE) != 0) {
745 		scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
746 		scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
747 		btrfs_warn_rl(fs_info,
748 	      "scrub: tree block %llu mirror %u has bad fsid, has %pU want %pU",
749 			      logical, stripe->mirror_num,
750 			      header->fsid, fs_info->fs_devices->fsid);
751 		return;
752 	}
753 	if (memcmp(header->chunk_tree_uuid, fs_info->chunk_tree_uuid,
754 		   BTRFS_UUID_SIZE) != 0) {
755 		scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
756 		scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
757 		btrfs_warn_rl(fs_info,
758    "scrub: tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU",
759 			      logical, stripe->mirror_num,
760 			      header->chunk_tree_uuid, fs_info->chunk_tree_uuid);
761 		return;
762 	}
763 
764 	/* Now check tree block csum. */
765 	shash->tfm = fs_info->csum_shash;
766 	crypto_shash_init(shash);
767 	crypto_shash_update(shash, first_kaddr + BTRFS_CSUM_SIZE,
768 			    fs_info->sectorsize - BTRFS_CSUM_SIZE);
769 
770 	for (int i = sector_nr + 1; i < sector_nr + sectors_per_tree; i++) {
771 		crypto_shash_update(shash, scrub_stripe_get_kaddr(stripe, i),
772 				    fs_info->sectorsize);
773 	}
774 
775 	crypto_shash_final(shash, calculated_csum);
776 	if (memcmp(calculated_csum, on_disk_csum, fs_info->csum_size) != 0) {
777 		scrub_bitmap_set_meta_error(stripe, sector_nr, sectors_per_tree);
778 		scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
779 		btrfs_warn_rl(fs_info,
780 "scrub: tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT,
781 			      logical, stripe->mirror_num,
782 			      CSUM_FMT_VALUE(fs_info->csum_size, on_disk_csum),
783 			      CSUM_FMT_VALUE(fs_info->csum_size, calculated_csum));
784 		return;
785 	}
786 	if (stripe->sectors[sector_nr].generation !=
787 	    btrfs_stack_header_generation(header)) {
788 		scrub_bitmap_set_meta_gen_error(stripe, sector_nr, sectors_per_tree);
789 		scrub_bitmap_set_error(stripe, sector_nr, sectors_per_tree);
790 		btrfs_warn_rl(fs_info,
791       "scrub: tree block %llu mirror %u has bad generation, has %llu want %llu",
792 			      logical, stripe->mirror_num,
793 			      btrfs_stack_header_generation(header),
794 			      stripe->sectors[sector_nr].generation);
795 		return;
796 	}
797 	scrub_bitmap_clear_error(stripe, sector_nr, sectors_per_tree);
798 	scrub_bitmap_clear_csum_error(stripe, sector_nr, sectors_per_tree);
799 	scrub_bitmap_clear_meta_error(stripe, sector_nr, sectors_per_tree);
800 	scrub_bitmap_clear_meta_gen_error(stripe, sector_nr, sectors_per_tree);
801 }
802 
scrub_verify_one_sector(struct scrub_stripe * stripe,int sector_nr)803 static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr)
804 {
805 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
806 	struct scrub_sector_verification *sector = &stripe->sectors[sector_nr];
807 	const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
808 	phys_addr_t paddr = scrub_stripe_get_paddr(stripe, sector_nr);
809 	u8 csum_buf[BTRFS_CSUM_SIZE];
810 	int ret;
811 
812 	ASSERT(sector_nr >= 0 && sector_nr < stripe->nr_sectors);
813 
814 	/* Sector not utilized, skip it. */
815 	if (!scrub_bitmap_test_bit_has_extent(stripe, sector_nr))
816 		return;
817 
818 	/* IO error, no need to check. */
819 	if (scrub_bitmap_test_bit_io_error(stripe, sector_nr))
820 		return;
821 
822 	/* Metadata, verify the full tree block. */
823 	if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr)) {
824 		/*
825 		 * Check if the tree block crosses the stripe boundary.  If
826 		 * crossed the boundary, we cannot verify it but only give a
827 		 * warning.
828 		 *
829 		 * This can only happen on a very old filesystem where chunks
830 		 * are not ensured to be stripe aligned.
831 		 */
832 		if (unlikely(sector_nr + sectors_per_tree > stripe->nr_sectors)) {
833 			btrfs_warn_rl(fs_info,
834 			"scrub: tree block at %llu crosses stripe boundary %llu",
835 				      stripe->logical +
836 				      (sector_nr << fs_info->sectorsize_bits),
837 				      stripe->logical);
838 			return;
839 		}
840 		scrub_verify_one_metadata(stripe, sector_nr);
841 		return;
842 	}
843 
844 	/*
845 	 * Data is easier, we just verify the data csum (if we have it).  For
846 	 * cases without csum, we have no other choice but to trust it.
847 	 */
848 	if (!sector->csum) {
849 		scrub_bitmap_clear_bit_error(stripe, sector_nr);
850 		return;
851 	}
852 
853 	ret = btrfs_check_block_csum(fs_info, paddr, csum_buf, sector->csum);
854 	if (ret < 0) {
855 		scrub_bitmap_set_bit_csum_error(stripe, sector_nr);
856 		scrub_bitmap_set_bit_error(stripe, sector_nr);
857 	} else {
858 		scrub_bitmap_clear_bit_csum_error(stripe, sector_nr);
859 		scrub_bitmap_clear_bit_error(stripe, sector_nr);
860 	}
861 }
862 
863 /* Verify specified sectors of a stripe. */
scrub_verify_one_stripe(struct scrub_stripe * stripe,unsigned long bitmap)864 static void scrub_verify_one_stripe(struct scrub_stripe *stripe, unsigned long bitmap)
865 {
866 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
867 	const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
868 	int sector_nr;
869 
870 	for_each_set_bit(sector_nr, &bitmap, stripe->nr_sectors) {
871 		scrub_verify_one_sector(stripe, sector_nr);
872 		if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr))
873 			sector_nr += sectors_per_tree - 1;
874 	}
875 }
876 
calc_sector_number(struct scrub_stripe * stripe,struct bio_vec * first_bvec)877 static int calc_sector_number(struct scrub_stripe *stripe, struct bio_vec *first_bvec)
878 {
879 	int i;
880 
881 	for (i = 0; i < stripe->nr_sectors; i++) {
882 		if (scrub_stripe_get_kaddr(stripe, i) == bvec_virt(first_bvec))
883 			break;
884 	}
885 	ASSERT(i < stripe->nr_sectors);
886 	return i;
887 }
888 
889 /*
890  * Repair read is different to the regular read:
891  *
892  * - Only reads the failed sectors
893  * - May have extra blocksize limits
894  */
scrub_repair_read_endio(struct btrfs_bio * bbio)895 static void scrub_repair_read_endio(struct btrfs_bio *bbio)
896 {
897 	struct scrub_stripe *stripe = bbio->private;
898 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
899 	struct bio_vec *bvec;
900 	int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
901 	u32 bio_size = 0;
902 	int i;
903 
904 	ASSERT(sector_nr < stripe->nr_sectors);
905 
906 	bio_for_each_bvec_all(bvec, &bbio->bio, i)
907 		bio_size += bvec->bv_len;
908 
909 	if (bbio->bio.bi_status) {
910 		scrub_bitmap_set_io_error(stripe, sector_nr,
911 					  bio_size >> fs_info->sectorsize_bits);
912 		scrub_bitmap_set_error(stripe, sector_nr,
913 				       bio_size >> fs_info->sectorsize_bits);
914 	} else {
915 		scrub_bitmap_clear_io_error(stripe, sector_nr,
916 					  bio_size >> fs_info->sectorsize_bits);
917 	}
918 	bio_put(&bbio->bio);
919 	if (atomic_dec_and_test(&stripe->pending_io))
920 		wake_up(&stripe->io_wait);
921 }
922 
calc_next_mirror(int mirror,int num_copies)923 static int calc_next_mirror(int mirror, int num_copies)
924 {
925 	ASSERT(mirror <= num_copies);
926 	return (mirror + 1 > num_copies) ? 1 : mirror + 1;
927 }
928 
scrub_bio_add_sector(struct btrfs_bio * bbio,struct scrub_stripe * stripe,int sector_nr)929 static void scrub_bio_add_sector(struct btrfs_bio *bbio, struct scrub_stripe *stripe,
930 				 int sector_nr)
931 {
932 	void *kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
933 	int ret;
934 
935 	ret = bio_add_page(&bbio->bio, virt_to_page(kaddr), bbio->fs_info->sectorsize,
936 			   offset_in_page(kaddr));
937 	/*
938 	 * Caller should ensure the bbio has enough size.
939 	 * And we cannot use __bio_add_page(), which doesn't do any merge.
940 	 *
941 	 * Meanwhile for scrub_submit_initial_read() we fully rely on the merge
942 	 * to create the minimal amount of bio vectors, for fs block size < page
943 	 * size cases.
944 	 */
945 	ASSERT(ret == bbio->fs_info->sectorsize);
946 }
947 
scrub_stripe_submit_repair_read(struct scrub_stripe * stripe,int mirror,int blocksize,bool wait)948 static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
949 					    int mirror, int blocksize, bool wait)
950 {
951 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
952 	struct btrfs_bio *bbio = NULL;
953 	const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
954 	int i;
955 
956 	ASSERT(stripe->mirror_num >= 1);
957 	ASSERT(atomic_read(&stripe->pending_io) == 0);
958 
959 	for_each_set_bit(i, &old_error_bitmap, stripe->nr_sectors) {
960 		/* The current sector cannot be merged, submit the bio. */
961 		if (bbio && ((i > 0 && !test_bit(i - 1, &old_error_bitmap)) ||
962 			     bbio->bio.bi_iter.bi_size >= blocksize)) {
963 			ASSERT(bbio->bio.bi_iter.bi_size);
964 			atomic_inc(&stripe->pending_io);
965 			btrfs_submit_bbio(bbio, mirror);
966 			if (wait)
967 				wait_scrub_stripe_io(stripe);
968 			bbio = NULL;
969 		}
970 
971 		if (!bbio) {
972 			bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_READ,
973 				fs_info, scrub_repair_read_endio, stripe);
974 			bbio->bio.bi_iter.bi_sector = (stripe->logical +
975 				(i << fs_info->sectorsize_bits)) >> SECTOR_SHIFT;
976 		}
977 
978 		scrub_bio_add_sector(bbio, stripe, i);
979 	}
980 	if (bbio) {
981 		ASSERT(bbio->bio.bi_iter.bi_size);
982 		atomic_inc(&stripe->pending_io);
983 		btrfs_submit_bbio(bbio, mirror);
984 		if (wait)
985 			wait_scrub_stripe_io(stripe);
986 	}
987 }
988 
scrub_stripe_report_errors(struct scrub_ctx * sctx,struct scrub_stripe * stripe,const struct scrub_error_records * errors)989 static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
990 				       struct scrub_stripe *stripe,
991 				       const struct scrub_error_records *errors)
992 {
993 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
994 				      DEFAULT_RATELIMIT_BURST);
995 	struct btrfs_fs_info *fs_info = sctx->fs_info;
996 	struct btrfs_device *dev = NULL;
997 	const unsigned long extent_bitmap = scrub_bitmap_read_has_extent(stripe);
998 	const unsigned long error_bitmap = scrub_bitmap_read_error(stripe);
999 	u64 physical = 0;
1000 	int nr_data_sectors = 0;
1001 	int nr_meta_sectors = 0;
1002 	int nr_nodatacsum_sectors = 0;
1003 	int nr_repaired_sectors = 0;
1004 	int sector_nr;
1005 
1006 	if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state))
1007 		return;
1008 
1009 	/*
1010 	 * Init needed infos for error reporting.
1011 	 *
1012 	 * Although our scrub_stripe infrastructure is mostly based on btrfs_submit_bio()
1013 	 * thus no need for dev/physical, error reporting still needs dev and physical.
1014 	 */
1015 	if (!bitmap_empty(&errors->init_error_bitmap, stripe->nr_sectors)) {
1016 		u64 mapped_len = fs_info->sectorsize;
1017 		struct btrfs_io_context *bioc = NULL;
1018 		int stripe_index = stripe->mirror_num - 1;
1019 		int ret;
1020 
1021 		/* For scrub, our mirror_num should always start at 1. */
1022 		ASSERT(stripe->mirror_num >= 1);
1023 		ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
1024 				      stripe->logical, &mapped_len, &bioc,
1025 				      NULL, NULL);
1026 		/*
1027 		 * If we failed, dev will be NULL, and later detailed reports
1028 		 * will just be skipped.
1029 		 */
1030 		if (ret < 0)
1031 			goto skip;
1032 		physical = bioc->stripes[stripe_index].physical;
1033 		dev = bioc->stripes[stripe_index].dev;
1034 		btrfs_put_bioc(bioc);
1035 	}
1036 
1037 skip:
1038 	for_each_set_bit(sector_nr, &extent_bitmap, stripe->nr_sectors) {
1039 		bool repaired = false;
1040 
1041 		if (scrub_bitmap_test_bit_is_metadata(stripe, sector_nr)) {
1042 			nr_meta_sectors++;
1043 		} else {
1044 			nr_data_sectors++;
1045 			if (!stripe->sectors[sector_nr].csum)
1046 				nr_nodatacsum_sectors++;
1047 		}
1048 
1049 		if (test_bit(sector_nr, &errors->init_error_bitmap) &&
1050 		    !test_bit(sector_nr, &error_bitmap)) {
1051 			nr_repaired_sectors++;
1052 			repaired = true;
1053 		}
1054 
1055 		/* Good sector from the beginning, nothing need to be done. */
1056 		if (!test_bit(sector_nr, &errors->init_error_bitmap))
1057 			continue;
1058 
1059 		/*
1060 		 * Report error for the corrupted sectors.  If repaired, just
1061 		 * output the message of repaired message.
1062 		 */
1063 		if (repaired) {
1064 			if (dev) {
1065 				btrfs_err_rl(fs_info,
1066 		"scrub: fixed up error at logical %llu on dev %s physical %llu",
1067 					    stripe->logical, btrfs_dev_name(dev),
1068 					    physical);
1069 			} else {
1070 				btrfs_err_rl(fs_info,
1071 			   "scrub: fixed up error at logical %llu on mirror %u",
1072 					    stripe->logical, stripe->mirror_num);
1073 			}
1074 			continue;
1075 		}
1076 
1077 		/* The remaining are all for unrepaired. */
1078 		if (dev) {
1079 			btrfs_err_rl(fs_info,
1080 "scrub: unable to fixup (regular) error at logical %llu on dev %s physical %llu",
1081 					    stripe->logical, btrfs_dev_name(dev),
1082 					    physical);
1083 		} else {
1084 			btrfs_err_rl(fs_info,
1085 	  "scrub: unable to fixup (regular) error at logical %llu on mirror %u",
1086 					    stripe->logical, stripe->mirror_num);
1087 		}
1088 
1089 		if (scrub_bitmap_test_bit_io_error(stripe, sector_nr))
1090 			if (__ratelimit(&rs) && dev)
1091 				scrub_print_common_warning("i/o error", dev, false,
1092 						     stripe->logical, physical);
1093 		if (scrub_bitmap_test_bit_csum_error(stripe, sector_nr))
1094 			if (__ratelimit(&rs) && dev)
1095 				scrub_print_common_warning("checksum error", dev, false,
1096 						     stripe->logical, physical);
1097 		if (scrub_bitmap_test_bit_meta_error(stripe, sector_nr))
1098 			if (__ratelimit(&rs) && dev)
1099 				scrub_print_common_warning("header error", dev, false,
1100 						     stripe->logical, physical);
1101 		if (scrub_bitmap_test_bit_meta_gen_error(stripe, sector_nr))
1102 			if (__ratelimit(&rs) && dev)
1103 				scrub_print_common_warning("generation error", dev, false,
1104 						     stripe->logical, physical);
1105 	}
1106 
1107 	/* Update the device stats. */
1108 	for (int i = 0; i < errors->nr_io_errors; i++)
1109 		btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_READ_ERRS);
1110 	for (int i = 0; i < errors->nr_csum_errors; i++)
1111 		btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_CORRUPTION_ERRS);
1112 	/* Generation mismatch error is based on each metadata, not each block. */
1113 	for (int i = 0; i < errors->nr_meta_gen_errors;
1114 	     i += (fs_info->nodesize >> fs_info->sectorsize_bits))
1115 		btrfs_dev_stat_inc_and_print(stripe->dev, BTRFS_DEV_STAT_GENERATION_ERRS);
1116 
1117 	spin_lock(&sctx->stat_lock);
1118 	sctx->stat.data_extents_scrubbed += stripe->nr_data_extents;
1119 	sctx->stat.tree_extents_scrubbed += stripe->nr_meta_extents;
1120 	sctx->stat.data_bytes_scrubbed += nr_data_sectors << fs_info->sectorsize_bits;
1121 	sctx->stat.tree_bytes_scrubbed += nr_meta_sectors << fs_info->sectorsize_bits;
1122 	sctx->stat.no_csum += nr_nodatacsum_sectors;
1123 	sctx->stat.read_errors += errors->nr_io_errors;
1124 	sctx->stat.csum_errors += errors->nr_csum_errors;
1125 	sctx->stat.verify_errors += errors->nr_meta_errors +
1126 				    errors->nr_meta_gen_errors;
1127 	sctx->stat.uncorrectable_errors +=
1128 		bitmap_weight(&error_bitmap, stripe->nr_sectors);
1129 	sctx->stat.corrected_errors += nr_repaired_sectors;
1130 	spin_unlock(&sctx->stat_lock);
1131 }
1132 
1133 static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
1134 				unsigned long write_bitmap, bool dev_replace);
1135 
1136 /*
1137  * The main entrance for all read related scrub work, including:
1138  *
1139  * - Wait for the initial read to finish
1140  * - Verify and locate any bad sectors
1141  * - Go through the remaining mirrors and try to read as large blocksize as
1142  *   possible
1143  * - Go through all mirrors (including the failed mirror) sector-by-sector
1144  * - Submit writeback for repaired sectors
1145  *
1146  * Writeback for dev-replace does not happen here, it needs extra
1147  * synchronization for zoned devices.
1148  */
scrub_stripe_read_repair_worker(struct work_struct * work)1149 static void scrub_stripe_read_repair_worker(struct work_struct *work)
1150 {
1151 	struct scrub_stripe *stripe = container_of(work, struct scrub_stripe, work);
1152 	struct scrub_ctx *sctx = stripe->sctx;
1153 	struct btrfs_fs_info *fs_info = sctx->fs_info;
1154 	struct scrub_error_records errors = { 0 };
1155 	int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
1156 					  stripe->bg->length);
1157 	unsigned long repaired;
1158 	unsigned long error;
1159 	int mirror;
1160 	int i;
1161 
1162 	ASSERT(stripe->mirror_num > 0);
1163 
1164 	wait_scrub_stripe_io(stripe);
1165 	scrub_verify_one_stripe(stripe, scrub_bitmap_read_has_extent(stripe));
1166 	/* Save the initial failed bitmap for later repair and report usage. */
1167 	errors.init_error_bitmap = scrub_bitmap_read_error(stripe);
1168 	errors.nr_io_errors = scrub_bitmap_weight_io_error(stripe);
1169 	errors.nr_csum_errors = scrub_bitmap_weight_csum_error(stripe);
1170 	errors.nr_meta_errors = scrub_bitmap_weight_meta_error(stripe);
1171 	errors.nr_meta_gen_errors = scrub_bitmap_weight_meta_gen_error(stripe);
1172 
1173 	if (bitmap_empty(&errors.init_error_bitmap, stripe->nr_sectors))
1174 		goto out;
1175 
1176 	/*
1177 	 * Try all remaining mirrors.
1178 	 *
1179 	 * Here we still try to read as large block as possible, as this is
1180 	 * faster and we have extra safety nets to rely on.
1181 	 */
1182 	for (mirror = calc_next_mirror(stripe->mirror_num, num_copies);
1183 	     mirror != stripe->mirror_num;
1184 	     mirror = calc_next_mirror(mirror, num_copies)) {
1185 		const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
1186 
1187 		scrub_stripe_submit_repair_read(stripe, mirror,
1188 						BTRFS_STRIPE_LEN, false);
1189 		wait_scrub_stripe_io(stripe);
1190 		scrub_verify_one_stripe(stripe, old_error_bitmap);
1191 		if (scrub_bitmap_empty_error(stripe))
1192 			goto out;
1193 	}
1194 
1195 	/*
1196 	 * Last safety net, try re-checking all mirrors, including the failed
1197 	 * one, sector-by-sector.
1198 	 *
1199 	 * As if one sector failed the drive's internal csum, the whole read
1200 	 * containing the offending sector would be marked as error.
1201 	 * Thus here we do sector-by-sector read.
1202 	 *
1203 	 * This can be slow, thus we only try it as the last resort.
1204 	 */
1205 
1206 	for (i = 0, mirror = stripe->mirror_num;
1207 	     i < num_copies;
1208 	     i++, mirror = calc_next_mirror(mirror, num_copies)) {
1209 		const unsigned long old_error_bitmap = scrub_bitmap_read_error(stripe);
1210 
1211 		scrub_stripe_submit_repair_read(stripe, mirror,
1212 						fs_info->sectorsize, true);
1213 		wait_scrub_stripe_io(stripe);
1214 		scrub_verify_one_stripe(stripe, old_error_bitmap);
1215 		if (scrub_bitmap_empty_error(stripe))
1216 			goto out;
1217 	}
1218 out:
1219 	error = scrub_bitmap_read_error(stripe);
1220 	/*
1221 	 * Submit the repaired sectors.  For zoned case, we cannot do repair
1222 	 * in-place, but queue the bg to be relocated.
1223 	 */
1224 	bitmap_andnot(&repaired, &errors.init_error_bitmap, &error,
1225 		      stripe->nr_sectors);
1226 	if (!sctx->readonly && !bitmap_empty(&repaired, stripe->nr_sectors)) {
1227 		if (btrfs_is_zoned(fs_info)) {
1228 			btrfs_repair_one_zone(fs_info, sctx->stripes[0].bg->start);
1229 		} else {
1230 			scrub_write_sectors(sctx, stripe, repaired, false);
1231 			wait_scrub_stripe_io(stripe);
1232 		}
1233 	}
1234 
1235 	scrub_stripe_report_errors(sctx, stripe, &errors);
1236 	set_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state);
1237 	wake_up(&stripe->repair_wait);
1238 }
1239 
scrub_read_endio(struct btrfs_bio * bbio)1240 static void scrub_read_endio(struct btrfs_bio *bbio)
1241 {
1242 	struct scrub_stripe *stripe = bbio->private;
1243 	struct bio_vec *bvec;
1244 	int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
1245 	int num_sectors;
1246 	u32 bio_size = 0;
1247 	int i;
1248 
1249 	ASSERT(sector_nr < stripe->nr_sectors);
1250 	bio_for_each_bvec_all(bvec, &bbio->bio, i)
1251 		bio_size += bvec->bv_len;
1252 	num_sectors = bio_size >> stripe->bg->fs_info->sectorsize_bits;
1253 
1254 	if (bbio->bio.bi_status) {
1255 		scrub_bitmap_set_io_error(stripe, sector_nr, num_sectors);
1256 		scrub_bitmap_set_error(stripe, sector_nr, num_sectors);
1257 	} else {
1258 		scrub_bitmap_clear_io_error(stripe, sector_nr, num_sectors);
1259 	}
1260 	bio_put(&bbio->bio);
1261 	if (atomic_dec_and_test(&stripe->pending_io)) {
1262 		wake_up(&stripe->io_wait);
1263 		INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
1264 		queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
1265 	}
1266 }
1267 
scrub_write_endio(struct btrfs_bio * bbio)1268 static void scrub_write_endio(struct btrfs_bio *bbio)
1269 {
1270 	struct scrub_stripe *stripe = bbio->private;
1271 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1272 	struct bio_vec *bvec;
1273 	int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
1274 	u32 bio_size = 0;
1275 	int i;
1276 
1277 	bio_for_each_bvec_all(bvec, &bbio->bio, i)
1278 		bio_size += bvec->bv_len;
1279 
1280 	if (bbio->bio.bi_status) {
1281 		unsigned long flags;
1282 
1283 		spin_lock_irqsave(&stripe->write_error_lock, flags);
1284 		bitmap_set(&stripe->write_error_bitmap, sector_nr,
1285 			   bio_size >> fs_info->sectorsize_bits);
1286 		spin_unlock_irqrestore(&stripe->write_error_lock, flags);
1287 		for (int i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
1288 			btrfs_dev_stat_inc_and_print(stripe->dev,
1289 						     BTRFS_DEV_STAT_WRITE_ERRS);
1290 	}
1291 	bio_put(&bbio->bio);
1292 
1293 	if (atomic_dec_and_test(&stripe->pending_io))
1294 		wake_up(&stripe->io_wait);
1295 }
1296 
scrub_submit_write_bio(struct scrub_ctx * sctx,struct scrub_stripe * stripe,struct btrfs_bio * bbio,bool dev_replace)1297 static void scrub_submit_write_bio(struct scrub_ctx *sctx,
1298 				   struct scrub_stripe *stripe,
1299 				   struct btrfs_bio *bbio, bool dev_replace)
1300 {
1301 	struct btrfs_fs_info *fs_info = sctx->fs_info;
1302 	u32 bio_len = bbio->bio.bi_iter.bi_size;
1303 	u32 bio_off = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT) -
1304 		      stripe->logical;
1305 
1306 	fill_writer_pointer_gap(sctx, stripe->physical + bio_off);
1307 	atomic_inc(&stripe->pending_io);
1308 	btrfs_submit_repair_write(bbio, stripe->mirror_num, dev_replace);
1309 	if (!btrfs_is_zoned(fs_info))
1310 		return;
1311 	/*
1312 	 * For zoned writeback, queue depth must be 1, thus we must wait for
1313 	 * the write to finish before the next write.
1314 	 */
1315 	wait_scrub_stripe_io(stripe);
1316 
1317 	/*
1318 	 * And also need to update the write pointer if write finished
1319 	 * successfully.
1320 	 */
1321 	if (!test_bit(bio_off >> fs_info->sectorsize_bits,
1322 		      &stripe->write_error_bitmap))
1323 		sctx->write_pointer += bio_len;
1324 }
1325 
1326 /*
1327  * Submit the write bio(s) for the sectors specified by @write_bitmap.
1328  *
1329  * Here we utilize btrfs_submit_repair_write(), which has some extra benefits:
1330  *
1331  * - Only needs logical bytenr and mirror_num
1332  *   Just like the scrub read path
1333  *
1334  * - Would only result in writes to the specified mirror
1335  *   Unlike the regular writeback path, which would write back to all stripes
1336  *
1337  * - Handle dev-replace and read-repair writeback differently
1338  */
scrub_write_sectors(struct scrub_ctx * sctx,struct scrub_stripe * stripe,unsigned long write_bitmap,bool dev_replace)1339 static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
1340 				unsigned long write_bitmap, bool dev_replace)
1341 {
1342 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1343 	struct btrfs_bio *bbio = NULL;
1344 	int sector_nr;
1345 
1346 	for_each_set_bit(sector_nr, &write_bitmap, stripe->nr_sectors) {
1347 		/* We should only writeback sectors covered by an extent. */
1348 		ASSERT(scrub_bitmap_test_bit_has_extent(stripe, sector_nr));
1349 
1350 		/* Cannot merge with previous sector, submit the current one. */
1351 		if (bbio && sector_nr && !test_bit(sector_nr - 1, &write_bitmap)) {
1352 			scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
1353 			bbio = NULL;
1354 		}
1355 		if (!bbio) {
1356 			bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_WRITE,
1357 					       fs_info, scrub_write_endio, stripe);
1358 			bbio->bio.bi_iter.bi_sector = (stripe->logical +
1359 				(sector_nr << fs_info->sectorsize_bits)) >>
1360 				SECTOR_SHIFT;
1361 		}
1362 		scrub_bio_add_sector(bbio, stripe, sector_nr);
1363 	}
1364 	if (bbio)
1365 		scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
1366 }
1367 
1368 /*
1369  * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
1370  * second.  Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
1371  */
scrub_throttle_dev_io(struct scrub_ctx * sctx,struct btrfs_device * device,unsigned int bio_size)1372 static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *device,
1373 				  unsigned int bio_size)
1374 {
1375 	const int time_slice = 1000;
1376 	s64 delta;
1377 	ktime_t now;
1378 	u32 div;
1379 	u64 bwlimit;
1380 
1381 	bwlimit = READ_ONCE(device->scrub_speed_max);
1382 	if (bwlimit == 0)
1383 		return;
1384 
1385 	/*
1386 	 * Slice is divided into intervals when the IO is submitted, adjust by
1387 	 * bwlimit and maximum of 64 intervals.
1388 	 */
1389 	div = clamp(bwlimit / (16 * 1024 * 1024), 1, 64);
1390 
1391 	/* Start new epoch, set deadline */
1392 	now = ktime_get();
1393 	if (sctx->throttle_deadline == 0) {
1394 		sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
1395 		sctx->throttle_sent = 0;
1396 	}
1397 
1398 	/* Still in the time to send? */
1399 	if (ktime_before(now, sctx->throttle_deadline)) {
1400 		/* If current bio is within the limit, send it */
1401 		sctx->throttle_sent += bio_size;
1402 		if (sctx->throttle_sent <= div_u64(bwlimit, div))
1403 			return;
1404 
1405 		/* We're over the limit, sleep until the rest of the slice */
1406 		delta = ktime_ms_delta(sctx->throttle_deadline, now);
1407 	} else {
1408 		/* New request after deadline, start new epoch */
1409 		delta = 0;
1410 	}
1411 
1412 	if (delta) {
1413 		long timeout;
1414 
1415 		timeout = div_u64(delta * HZ, 1000);
1416 		schedule_timeout_interruptible(timeout);
1417 	}
1418 
1419 	/* Next call will start the deadline period */
1420 	sctx->throttle_deadline = 0;
1421 }
1422 
1423 /*
1424  * Given a physical address, this will calculate it's
1425  * logical offset. if this is a parity stripe, it will return
1426  * the most left data stripe's logical offset.
1427  *
1428  * return 0 if it is a data stripe, 1 means parity stripe.
1429  */
get_raid56_logic_offset(u64 physical,int num,struct btrfs_chunk_map * map,u64 * offset,u64 * stripe_start)1430 static int get_raid56_logic_offset(u64 physical, int num,
1431 				   struct btrfs_chunk_map *map, u64 *offset,
1432 				   u64 *stripe_start)
1433 {
1434 	int i;
1435 	int j = 0;
1436 	u64 last_offset;
1437 	const int data_stripes = nr_data_stripes(map);
1438 
1439 	last_offset = (physical - map->stripes[num].physical) * data_stripes;
1440 	if (stripe_start)
1441 		*stripe_start = last_offset;
1442 
1443 	*offset = last_offset;
1444 	for (i = 0; i < data_stripes; i++) {
1445 		u32 stripe_nr;
1446 		u32 stripe_index;
1447 		u32 rot;
1448 
1449 		*offset = last_offset + btrfs_stripe_nr_to_offset(i);
1450 
1451 		stripe_nr = (u32)(*offset >> BTRFS_STRIPE_LEN_SHIFT) / data_stripes;
1452 
1453 		/* Work out the disk rotation on this stripe-set */
1454 		rot = stripe_nr % map->num_stripes;
1455 		/* calculate which stripe this data locates */
1456 		rot += i;
1457 		stripe_index = rot % map->num_stripes;
1458 		if (stripe_index == num)
1459 			return 0;
1460 		if (stripe_index < num)
1461 			j++;
1462 	}
1463 	*offset = last_offset + btrfs_stripe_nr_to_offset(j);
1464 	return 1;
1465 }
1466 
1467 /*
1468  * Return 0 if the extent item range covers any byte of the range.
1469  * Return <0 if the extent item is before @search_start.
1470  * Return >0 if the extent item is after @start_start + @search_len.
1471  */
compare_extent_item_range(struct btrfs_path * path,u64 search_start,u64 search_len)1472 static int compare_extent_item_range(struct btrfs_path *path,
1473 				     u64 search_start, u64 search_len)
1474 {
1475 	struct btrfs_fs_info *fs_info = path->nodes[0]->fs_info;
1476 	u64 len;
1477 	struct btrfs_key key;
1478 
1479 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1480 	ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY ||
1481 	       key.type == BTRFS_METADATA_ITEM_KEY);
1482 	if (key.type == BTRFS_METADATA_ITEM_KEY)
1483 		len = fs_info->nodesize;
1484 	else
1485 		len = key.offset;
1486 
1487 	if (key.objectid + len <= search_start)
1488 		return -1;
1489 	if (key.objectid >= search_start + search_len)
1490 		return 1;
1491 	return 0;
1492 }
1493 
1494 /*
1495  * Locate one extent item which covers any byte in range
1496  * [@search_start, @search_start + @search_length)
1497  *
1498  * If the path is not initialized, we will initialize the search by doing
1499  * a btrfs_search_slot().
1500  * If the path is already initialized, we will use the path as the initial
1501  * slot, to avoid duplicated btrfs_search_slot() calls.
1502  *
1503  * NOTE: If an extent item starts before @search_start, we will still
1504  * return the extent item. This is for data extent crossing stripe boundary.
1505  *
1506  * Return 0 if we found such extent item, and @path will point to the extent item.
1507  * Return >0 if no such extent item can be found, and @path will be released.
1508  * Return <0 if hit fatal error, and @path will be released.
1509  */
find_first_extent_item(struct btrfs_root * extent_root,struct btrfs_path * path,u64 search_start,u64 search_len)1510 static int find_first_extent_item(struct btrfs_root *extent_root,
1511 				  struct btrfs_path *path,
1512 				  u64 search_start, u64 search_len)
1513 {
1514 	struct btrfs_fs_info *fs_info = extent_root->fs_info;
1515 	struct btrfs_key key;
1516 	int ret;
1517 
1518 	/* Continue using the existing path */
1519 	if (path->nodes[0])
1520 		goto search_forward;
1521 
1522 	key.objectid = search_start;
1523 	if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1524 		key.type = BTRFS_METADATA_ITEM_KEY;
1525 	else
1526 		key.type = BTRFS_EXTENT_ITEM_KEY;
1527 	key.offset = (u64)-1;
1528 
1529 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1530 	if (ret < 0)
1531 		return ret;
1532 	if (unlikely(ret == 0)) {
1533 		/*
1534 		 * Key with offset -1 found, there would have to exist an extent
1535 		 * item with such offset, but this is out of the valid range.
1536 		 */
1537 		btrfs_release_path(path);
1538 		return -EUCLEAN;
1539 	}
1540 
1541 	/*
1542 	 * Here we intentionally pass 0 as @min_objectid, as there could be
1543 	 * an extent item starting before @search_start.
1544 	 */
1545 	ret = btrfs_previous_extent_item(extent_root, path, 0);
1546 	if (ret < 0)
1547 		return ret;
1548 	/*
1549 	 * No matter whether we have found an extent item, the next loop will
1550 	 * properly do every check on the key.
1551 	 */
1552 search_forward:
1553 	while (true) {
1554 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1555 		if (key.objectid >= search_start + search_len)
1556 			break;
1557 		if (key.type != BTRFS_METADATA_ITEM_KEY &&
1558 		    key.type != BTRFS_EXTENT_ITEM_KEY)
1559 			goto next;
1560 
1561 		ret = compare_extent_item_range(path, search_start, search_len);
1562 		if (ret == 0)
1563 			return ret;
1564 		if (ret > 0)
1565 			break;
1566 next:
1567 		ret = btrfs_next_item(extent_root, path);
1568 		if (ret) {
1569 			/* Either no more items or a fatal error. */
1570 			btrfs_release_path(path);
1571 			return ret;
1572 		}
1573 	}
1574 	btrfs_release_path(path);
1575 	return 1;
1576 }
1577 
get_extent_info(struct btrfs_path * path,u64 * extent_start_ret,u64 * size_ret,u64 * flags_ret,u64 * generation_ret)1578 static void get_extent_info(struct btrfs_path *path, u64 *extent_start_ret,
1579 			    u64 *size_ret, u64 *flags_ret, u64 *generation_ret)
1580 {
1581 	struct btrfs_key key;
1582 	struct btrfs_extent_item *ei;
1583 
1584 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1585 	ASSERT(key.type == BTRFS_METADATA_ITEM_KEY ||
1586 	       key.type == BTRFS_EXTENT_ITEM_KEY);
1587 	*extent_start_ret = key.objectid;
1588 	if (key.type == BTRFS_METADATA_ITEM_KEY)
1589 		*size_ret = path->nodes[0]->fs_info->nodesize;
1590 	else
1591 		*size_ret = key.offset;
1592 	ei = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_item);
1593 	*flags_ret = btrfs_extent_flags(path->nodes[0], ei);
1594 	*generation_ret = btrfs_extent_generation(path->nodes[0], ei);
1595 }
1596 
sync_write_pointer_for_zoned(struct scrub_ctx * sctx,u64 logical,u64 physical,u64 physical_end)1597 static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
1598 					u64 physical, u64 physical_end)
1599 {
1600 	struct btrfs_fs_info *fs_info = sctx->fs_info;
1601 	int ret = 0;
1602 
1603 	if (!btrfs_is_zoned(fs_info))
1604 		return 0;
1605 
1606 	mutex_lock(&sctx->wr_lock);
1607 	if (sctx->write_pointer < physical_end) {
1608 		ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
1609 						    physical,
1610 						    sctx->write_pointer);
1611 		if (ret)
1612 			btrfs_err(fs_info, "scrub: zoned: failed to recover write pointer");
1613 	}
1614 	mutex_unlock(&sctx->wr_lock);
1615 	btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
1616 
1617 	return ret;
1618 }
1619 
fill_one_extent_info(struct btrfs_fs_info * fs_info,struct scrub_stripe * stripe,u64 extent_start,u64 extent_len,u64 extent_flags,u64 extent_gen)1620 static void fill_one_extent_info(struct btrfs_fs_info *fs_info,
1621 				 struct scrub_stripe *stripe,
1622 				 u64 extent_start, u64 extent_len,
1623 				 u64 extent_flags, u64 extent_gen)
1624 {
1625 	for (u64 cur_logical = max(stripe->logical, extent_start);
1626 	     cur_logical < min(stripe->logical + BTRFS_STRIPE_LEN,
1627 			       extent_start + extent_len);
1628 	     cur_logical += fs_info->sectorsize) {
1629 		const int nr_sector = (cur_logical - stripe->logical) >>
1630 				      fs_info->sectorsize_bits;
1631 		struct scrub_sector_verification *sector =
1632 						&stripe->sectors[nr_sector];
1633 
1634 		scrub_bitmap_set_bit_has_extent(stripe, nr_sector);
1635 		if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1636 			scrub_bitmap_set_bit_is_metadata(stripe, nr_sector);
1637 			sector->generation = extent_gen;
1638 		}
1639 	}
1640 }
1641 
scrub_stripe_reset_bitmaps(struct scrub_stripe * stripe)1642 static void scrub_stripe_reset_bitmaps(struct scrub_stripe *stripe)
1643 {
1644 	ASSERT(stripe->nr_sectors);
1645 	bitmap_zero(stripe->bitmaps, scrub_bitmap_nr_last * stripe->nr_sectors);
1646 }
1647 
1648 /*
1649  * Locate one stripe which has at least one extent in its range.
1650  *
1651  * Return 0 if found such stripe, and store its info into @stripe.
1652  * Return >0 if there is no such stripe in the specified range.
1653  * Return <0 for error.
1654  */
scrub_find_fill_first_stripe(struct btrfs_block_group * bg,struct btrfs_path * extent_path,struct btrfs_path * csum_path,struct btrfs_device * dev,u64 physical,int mirror_num,u64 logical_start,u32 logical_len,struct scrub_stripe * stripe)1655 static int scrub_find_fill_first_stripe(struct btrfs_block_group *bg,
1656 					struct btrfs_path *extent_path,
1657 					struct btrfs_path *csum_path,
1658 					struct btrfs_device *dev, u64 physical,
1659 					int mirror_num, u64 logical_start,
1660 					u32 logical_len,
1661 					struct scrub_stripe *stripe)
1662 {
1663 	struct btrfs_fs_info *fs_info = bg->fs_info;
1664 	struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bg->start);
1665 	struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bg->start);
1666 	const u64 logical_end = logical_start + logical_len;
1667 	u64 cur_logical = logical_start;
1668 	u64 stripe_end;
1669 	u64 extent_start;
1670 	u64 extent_len;
1671 	u64 extent_flags;
1672 	u64 extent_gen;
1673 	int ret;
1674 
1675 	if (unlikely(!extent_root || !csum_root)) {
1676 		btrfs_err(fs_info, "scrub: no valid extent or csum root found");
1677 		return -EUCLEAN;
1678 	}
1679 	memset(stripe->sectors, 0, sizeof(struct scrub_sector_verification) *
1680 				   stripe->nr_sectors);
1681 	scrub_stripe_reset_bitmaps(stripe);
1682 
1683 	/* The range must be inside the bg. */
1684 	ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length);
1685 
1686 	ret = find_first_extent_item(extent_root, extent_path, logical_start,
1687 				     logical_len);
1688 	/* Either error or not found. */
1689 	if (ret)
1690 		goto out;
1691 	get_extent_info(extent_path, &extent_start, &extent_len, &extent_flags,
1692 			&extent_gen);
1693 	if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1694 		stripe->nr_meta_extents++;
1695 	if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
1696 		stripe->nr_data_extents++;
1697 	cur_logical = max(extent_start, cur_logical);
1698 
1699 	/*
1700 	 * Round down to stripe boundary.
1701 	 *
1702 	 * The extra calculation against bg->start is to handle block groups
1703 	 * whose logical bytenr is not BTRFS_STRIPE_LEN aligned.
1704 	 */
1705 	stripe->logical = round_down(cur_logical - bg->start, BTRFS_STRIPE_LEN) +
1706 			  bg->start;
1707 	stripe->physical = physical + stripe->logical - logical_start;
1708 	stripe->dev = dev;
1709 	stripe->bg = bg;
1710 	stripe->mirror_num = mirror_num;
1711 	stripe_end = stripe->logical + BTRFS_STRIPE_LEN - 1;
1712 
1713 	/* Fill the first extent info into stripe->sectors[] array. */
1714 	fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
1715 			     extent_flags, extent_gen);
1716 	cur_logical = extent_start + extent_len;
1717 
1718 	/* Fill the extent info for the remaining sectors. */
1719 	while (cur_logical <= stripe_end) {
1720 		ret = find_first_extent_item(extent_root, extent_path, cur_logical,
1721 					     stripe_end - cur_logical + 1);
1722 		if (ret < 0)
1723 			goto out;
1724 		if (ret > 0) {
1725 			ret = 0;
1726 			break;
1727 		}
1728 		get_extent_info(extent_path, &extent_start, &extent_len,
1729 				&extent_flags, &extent_gen);
1730 		if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1731 			stripe->nr_meta_extents++;
1732 		if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
1733 			stripe->nr_data_extents++;
1734 		fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
1735 				     extent_flags, extent_gen);
1736 		cur_logical = extent_start + extent_len;
1737 	}
1738 
1739 	/* Now fill the data csum. */
1740 	if (bg->flags & BTRFS_BLOCK_GROUP_DATA) {
1741 		int sector_nr;
1742 		unsigned long csum_bitmap = 0;
1743 
1744 		/* Csum space should have already been allocated. */
1745 		ASSERT(stripe->csums);
1746 
1747 		/*
1748 		 * Our csum bitmap should be large enough, as BTRFS_STRIPE_LEN
1749 		 * should contain at most 16 sectors.
1750 		 */
1751 		ASSERT(BITS_PER_LONG >= BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
1752 
1753 		ret = btrfs_lookup_csums_bitmap(csum_root, csum_path,
1754 						stripe->logical, stripe_end,
1755 						stripe->csums, &csum_bitmap);
1756 		if (ret < 0)
1757 			goto out;
1758 		if (ret > 0)
1759 			ret = 0;
1760 
1761 		for_each_set_bit(sector_nr, &csum_bitmap, stripe->nr_sectors) {
1762 			stripe->sectors[sector_nr].csum = stripe->csums +
1763 				sector_nr * fs_info->csum_size;
1764 		}
1765 	}
1766 	set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
1767 out:
1768 	return ret;
1769 }
1770 
scrub_reset_stripe(struct scrub_stripe * stripe)1771 static void scrub_reset_stripe(struct scrub_stripe *stripe)
1772 {
1773 	scrub_stripe_reset_bitmaps(stripe);
1774 
1775 	stripe->nr_meta_extents = 0;
1776 	stripe->nr_data_extents = 0;
1777 	stripe->state = 0;
1778 
1779 	for (int i = 0; i < stripe->nr_sectors; i++) {
1780 		stripe->sectors[i].csum = NULL;
1781 		stripe->sectors[i].generation = 0;
1782 	}
1783 }
1784 
stripe_length(const struct scrub_stripe * stripe)1785 static u32 stripe_length(const struct scrub_stripe *stripe)
1786 {
1787 	ASSERT(stripe->bg);
1788 
1789 	return min(BTRFS_STRIPE_LEN,
1790 		   stripe->bg->start + stripe->bg->length - stripe->logical);
1791 }
1792 
scrub_submit_extent_sector_read(struct scrub_stripe * stripe)1793 static void scrub_submit_extent_sector_read(struct scrub_stripe *stripe)
1794 {
1795 	struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1796 	struct btrfs_bio *bbio = NULL;
1797 	unsigned int nr_sectors = stripe_length(stripe) >> fs_info->sectorsize_bits;
1798 	const unsigned long has_extent = scrub_bitmap_read_has_extent(stripe);
1799 	u64 stripe_len = BTRFS_STRIPE_LEN;
1800 	int mirror = stripe->mirror_num;
1801 	int i;
1802 
1803 	atomic_inc(&stripe->pending_io);
1804 
1805 	for_each_set_bit(i, &has_extent, stripe->nr_sectors) {
1806 		/* We're beyond the chunk boundary, no need to read anymore. */
1807 		if (i >= nr_sectors)
1808 			break;
1809 
1810 		/* The current sector cannot be merged, submit the bio. */
1811 		if (bbio &&
1812 		    ((i > 0 && !test_bit(i - 1, &has_extent)) ||
1813 		     bbio->bio.bi_iter.bi_size >= stripe_len)) {
1814 			ASSERT(bbio->bio.bi_iter.bi_size);
1815 			atomic_inc(&stripe->pending_io);
1816 			btrfs_submit_bbio(bbio, mirror);
1817 			bbio = NULL;
1818 		}
1819 
1820 		if (!bbio) {
1821 			struct btrfs_io_stripe io_stripe = {};
1822 			struct btrfs_io_context *bioc = NULL;
1823 			const u64 logical = stripe->logical +
1824 					    (i << fs_info->sectorsize_bits);
1825 			int ret;
1826 
1827 			io_stripe.rst_search_commit_root = true;
1828 			stripe_len = (nr_sectors - i) << fs_info->sectorsize_bits;
1829 			/*
1830 			 * For RST cases, we need to manually split the bbio to
1831 			 * follow the RST boundary.
1832 			 */
1833 			ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
1834 					      &stripe_len, &bioc, &io_stripe, &mirror);
1835 			btrfs_put_bioc(bioc);
1836 			if (ret < 0) {
1837 				if (ret != -ENODATA) {
1838 					/*
1839 					 * Earlier btrfs_get_raid_extent_offset()
1840 					 * returned -ENODATA, which means there's
1841 					 * no entry for the corresponding range
1842 					 * in the stripe tree.  But if it's in
1843 					 * the extent tree, then it's a preallocated
1844 					 * extent and not an error.
1845 					 */
1846 					scrub_bitmap_set_bit_io_error(stripe, i);
1847 					scrub_bitmap_set_bit_error(stripe, i);
1848 				}
1849 				continue;
1850 			}
1851 
1852 			bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_READ,
1853 					       fs_info, scrub_read_endio, stripe);
1854 			bbio->bio.bi_iter.bi_sector = logical >> SECTOR_SHIFT;
1855 		}
1856 
1857 		scrub_bio_add_sector(bbio, stripe, i);
1858 	}
1859 
1860 	if (bbio) {
1861 		ASSERT(bbio->bio.bi_iter.bi_size);
1862 		atomic_inc(&stripe->pending_io);
1863 		btrfs_submit_bbio(bbio, mirror);
1864 	}
1865 
1866 	if (atomic_dec_and_test(&stripe->pending_io)) {
1867 		wake_up(&stripe->io_wait);
1868 		INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
1869 		queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
1870 	}
1871 }
1872 
scrub_submit_initial_read(struct scrub_ctx * sctx,struct scrub_stripe * stripe)1873 static void scrub_submit_initial_read(struct scrub_ctx *sctx,
1874 				      struct scrub_stripe *stripe)
1875 {
1876 	struct btrfs_fs_info *fs_info = sctx->fs_info;
1877 	struct btrfs_bio *bbio;
1878 	const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order;
1879 	unsigned int nr_sectors = stripe_length(stripe) >> fs_info->sectorsize_bits;
1880 	int mirror = stripe->mirror_num;
1881 
1882 	ASSERT(stripe->bg);
1883 	ASSERT(stripe->mirror_num > 0);
1884 	ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
1885 
1886 	if (btrfs_need_stripe_tree_update(fs_info, stripe->bg->flags)) {
1887 		scrub_submit_extent_sector_read(stripe);
1888 		return;
1889 	}
1890 
1891 	bbio = btrfs_bio_alloc(BTRFS_STRIPE_LEN >> min_folio_shift, REQ_OP_READ, fs_info,
1892 			       scrub_read_endio, stripe);
1893 
1894 	bbio->bio.bi_iter.bi_sector = stripe->logical >> SECTOR_SHIFT;
1895 	/* Read the whole range inside the chunk boundary. */
1896 	for (unsigned int cur = 0; cur < nr_sectors; cur++)
1897 		scrub_bio_add_sector(bbio, stripe, cur);
1898 	atomic_inc(&stripe->pending_io);
1899 
1900 	/*
1901 	 * For dev-replace, either user asks to avoid the source dev, or
1902 	 * the device is missing, we try the next mirror instead.
1903 	 */
1904 	if (sctx->is_dev_replace &&
1905 	    (fs_info->dev_replace.cont_reading_from_srcdev_mode ==
1906 	     BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID ||
1907 	     !stripe->dev->bdev)) {
1908 		int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
1909 						  stripe->bg->length);
1910 
1911 		mirror = calc_next_mirror(mirror, num_copies);
1912 	}
1913 	btrfs_submit_bbio(bbio, mirror);
1914 }
1915 
stripe_has_metadata_error(struct scrub_stripe * stripe)1916 static bool stripe_has_metadata_error(struct scrub_stripe *stripe)
1917 {
1918 	const unsigned long error = scrub_bitmap_read_error(stripe);
1919 	int i;
1920 
1921 	for_each_set_bit(i, &error, stripe->nr_sectors) {
1922 		if (scrub_bitmap_test_bit_is_metadata(stripe, i)) {
1923 			struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1924 
1925 			btrfs_err(fs_info,
1926 		    "scrub: stripe %llu has unrepaired metadata sector at logical %llu",
1927 				  stripe->logical,
1928 				  stripe->logical + (i << fs_info->sectorsize_bits));
1929 			return true;
1930 		}
1931 	}
1932 	return false;
1933 }
1934 
submit_initial_group_read(struct scrub_ctx * sctx,unsigned int first_slot,unsigned int nr_stripes)1935 static void submit_initial_group_read(struct scrub_ctx *sctx,
1936 				      unsigned int first_slot,
1937 				      unsigned int nr_stripes)
1938 {
1939 	struct blk_plug plug;
1940 
1941 	ASSERT(first_slot < SCRUB_TOTAL_STRIPES);
1942 	ASSERT(first_slot + nr_stripes <= SCRUB_TOTAL_STRIPES);
1943 
1944 	scrub_throttle_dev_io(sctx, sctx->stripes[0].dev,
1945 			      btrfs_stripe_nr_to_offset(nr_stripes));
1946 	blk_start_plug(&plug);
1947 	for (int i = 0; i < nr_stripes; i++) {
1948 		struct scrub_stripe *stripe = &sctx->stripes[first_slot + i];
1949 
1950 		/* Those stripes should be initialized. */
1951 		ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
1952 		scrub_submit_initial_read(sctx, stripe);
1953 	}
1954 	blk_finish_plug(&plug);
1955 }
1956 
flush_scrub_stripes(struct scrub_ctx * sctx)1957 static int flush_scrub_stripes(struct scrub_ctx *sctx)
1958 {
1959 	struct btrfs_fs_info *fs_info = sctx->fs_info;
1960 	struct scrub_stripe *stripe;
1961 	const int nr_stripes = sctx->cur_stripe;
1962 	int ret = 0;
1963 
1964 	if (!nr_stripes)
1965 		return 0;
1966 
1967 	ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &sctx->stripes[0].state));
1968 
1969 	/* Submit the stripes which are populated but not submitted. */
1970 	if (nr_stripes % SCRUB_STRIPES_PER_GROUP) {
1971 		const int first_slot = round_down(nr_stripes, SCRUB_STRIPES_PER_GROUP);
1972 
1973 		submit_initial_group_read(sctx, first_slot, nr_stripes - first_slot);
1974 	}
1975 
1976 	for (int i = 0; i < nr_stripes; i++) {
1977 		stripe = &sctx->stripes[i];
1978 
1979 		wait_event(stripe->repair_wait,
1980 			   test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
1981 	}
1982 
1983 	/* Submit for dev-replace. */
1984 	if (sctx->is_dev_replace) {
1985 		/*
1986 		 * For dev-replace, if we know there is something wrong with
1987 		 * metadata, we should immediately abort.
1988 		 */
1989 		for (int i = 0; i < nr_stripes; i++) {
1990 			if (unlikely(stripe_has_metadata_error(&sctx->stripes[i]))) {
1991 				ret = -EIO;
1992 				goto out;
1993 			}
1994 		}
1995 		for (int i = 0; i < nr_stripes; i++) {
1996 			unsigned long good;
1997 			unsigned long has_extent;
1998 			unsigned long error;
1999 
2000 			stripe = &sctx->stripes[i];
2001 
2002 			ASSERT(stripe->dev == fs_info->dev_replace.srcdev);
2003 
2004 			has_extent = scrub_bitmap_read_has_extent(stripe);
2005 			error = scrub_bitmap_read_error(stripe);
2006 			bitmap_andnot(&good, &has_extent, &error, stripe->nr_sectors);
2007 			scrub_write_sectors(sctx, stripe, good, true);
2008 		}
2009 	}
2010 
2011 	/* Wait for the above writebacks to finish. */
2012 	for (int i = 0; i < nr_stripes; i++) {
2013 		stripe = &sctx->stripes[i];
2014 
2015 		wait_scrub_stripe_io(stripe);
2016 		spin_lock(&sctx->stat_lock);
2017 		sctx->stat.last_physical = stripe->physical + stripe_length(stripe);
2018 		spin_unlock(&sctx->stat_lock);
2019 		scrub_reset_stripe(stripe);
2020 	}
2021 out:
2022 	sctx->cur_stripe = 0;
2023 	return ret;
2024 }
2025 
raid56_scrub_wait_endio(struct bio * bio)2026 static void raid56_scrub_wait_endio(struct bio *bio)
2027 {
2028 	complete(bio->bi_private);
2029 }
2030 
queue_scrub_stripe(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct btrfs_device * dev,int mirror_num,u64 logical,u32 length,u64 physical,u64 * found_logical_ret)2031 static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *bg,
2032 			      struct btrfs_device *dev, int mirror_num,
2033 			      u64 logical, u32 length, u64 physical,
2034 			      u64 *found_logical_ret)
2035 {
2036 	struct scrub_stripe *stripe;
2037 	int ret;
2038 
2039 	/*
2040 	 * There should always be one slot left, as caller filling the last
2041 	 * slot should flush them all.
2042 	 */
2043 	ASSERT(sctx->cur_stripe < SCRUB_TOTAL_STRIPES);
2044 
2045 	/* @found_logical_ret must be specified. */
2046 	ASSERT(found_logical_ret);
2047 
2048 	stripe = &sctx->stripes[sctx->cur_stripe];
2049 	scrub_reset_stripe(stripe);
2050 	ret = scrub_find_fill_first_stripe(bg, &sctx->extent_path,
2051 					   &sctx->csum_path, dev, physical,
2052 					   mirror_num, logical, length, stripe);
2053 	/* Either >0 as no more extents or <0 for error. */
2054 	if (ret)
2055 		return ret;
2056 	*found_logical_ret = stripe->logical;
2057 	sctx->cur_stripe++;
2058 
2059 	/* We filled one group, submit it. */
2060 	if (sctx->cur_stripe % SCRUB_STRIPES_PER_GROUP == 0) {
2061 		const int first_slot = sctx->cur_stripe - SCRUB_STRIPES_PER_GROUP;
2062 
2063 		submit_initial_group_read(sctx, first_slot, SCRUB_STRIPES_PER_GROUP);
2064 	}
2065 
2066 	/* Last slot used, flush them all. */
2067 	if (sctx->cur_stripe == SCRUB_TOTAL_STRIPES)
2068 		return flush_scrub_stripes(sctx);
2069 	return 0;
2070 }
2071 
scrub_raid56_parity_stripe(struct scrub_ctx * sctx,struct btrfs_device * scrub_dev,struct btrfs_block_group * bg,struct btrfs_chunk_map * map,u64 full_stripe_start)2072 static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
2073 				      struct btrfs_device *scrub_dev,
2074 				      struct btrfs_block_group *bg,
2075 				      struct btrfs_chunk_map *map,
2076 				      u64 full_stripe_start)
2077 {
2078 	DECLARE_COMPLETION_ONSTACK(io_done);
2079 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2080 	struct btrfs_raid_bio *rbio;
2081 	struct btrfs_io_context *bioc = NULL;
2082 	struct btrfs_path extent_path = { 0 };
2083 	struct btrfs_path csum_path = { 0 };
2084 	struct bio *bio;
2085 	struct scrub_stripe *stripe;
2086 	bool all_empty = true;
2087 	const int data_stripes = nr_data_stripes(map);
2088 	unsigned long extent_bitmap = 0;
2089 	u64 length = btrfs_stripe_nr_to_offset(data_stripes);
2090 	int ret;
2091 
2092 	ASSERT(sctx->raid56_data_stripes);
2093 
2094 	/*
2095 	 * For data stripe search, we cannot reuse the same extent/csum paths,
2096 	 * as the data stripe bytenr may be smaller than previous extent.  Thus
2097 	 * we have to use our own extent/csum paths.
2098 	 */
2099 	extent_path.search_commit_root = 1;
2100 	extent_path.skip_locking = 1;
2101 	csum_path.search_commit_root = 1;
2102 	csum_path.skip_locking = 1;
2103 
2104 	for (int i = 0; i < data_stripes; i++) {
2105 		int stripe_index;
2106 		int rot;
2107 		u64 physical;
2108 
2109 		stripe = &sctx->raid56_data_stripes[i];
2110 		rot = div_u64(full_stripe_start - bg->start,
2111 			      data_stripes) >> BTRFS_STRIPE_LEN_SHIFT;
2112 		stripe_index = (i + rot) % map->num_stripes;
2113 		physical = map->stripes[stripe_index].physical +
2114 			   btrfs_stripe_nr_to_offset(rot);
2115 
2116 		scrub_reset_stripe(stripe);
2117 		set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state);
2118 		ret = scrub_find_fill_first_stripe(bg, &extent_path, &csum_path,
2119 				map->stripes[stripe_index].dev, physical, 1,
2120 				full_stripe_start + btrfs_stripe_nr_to_offset(i),
2121 				BTRFS_STRIPE_LEN, stripe);
2122 		if (ret < 0)
2123 			goto out;
2124 		/*
2125 		 * No extent in this data stripe, need to manually mark them
2126 		 * initialized to make later read submission happy.
2127 		 */
2128 		if (ret > 0) {
2129 			stripe->logical = full_stripe_start +
2130 					  btrfs_stripe_nr_to_offset(i);
2131 			stripe->dev = map->stripes[stripe_index].dev;
2132 			stripe->mirror_num = 1;
2133 			set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
2134 		}
2135 	}
2136 
2137 	/* Check if all data stripes are empty. */
2138 	for (int i = 0; i < data_stripes; i++) {
2139 		stripe = &sctx->raid56_data_stripes[i];
2140 		if (!scrub_bitmap_empty_has_extent(stripe)) {
2141 			all_empty = false;
2142 			break;
2143 		}
2144 	}
2145 	if (all_empty) {
2146 		ret = 0;
2147 		goto out;
2148 	}
2149 
2150 	for (int i = 0; i < data_stripes; i++) {
2151 		stripe = &sctx->raid56_data_stripes[i];
2152 		scrub_submit_initial_read(sctx, stripe);
2153 	}
2154 	for (int i = 0; i < data_stripes; i++) {
2155 		stripe = &sctx->raid56_data_stripes[i];
2156 
2157 		wait_event(stripe->repair_wait,
2158 			   test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
2159 	}
2160 	/* For now, no zoned support for RAID56. */
2161 	ASSERT(!btrfs_is_zoned(sctx->fs_info));
2162 
2163 	/*
2164 	 * Now all data stripes are properly verified. Check if we have any
2165 	 * unrepaired, if so abort immediately or we could further corrupt the
2166 	 * P/Q stripes.
2167 	 *
2168 	 * During the loop, also populate extent_bitmap.
2169 	 */
2170 	for (int i = 0; i < data_stripes; i++) {
2171 		unsigned long error;
2172 		unsigned long has_extent;
2173 
2174 		stripe = &sctx->raid56_data_stripes[i];
2175 
2176 		error = scrub_bitmap_read_error(stripe);
2177 		has_extent = scrub_bitmap_read_has_extent(stripe);
2178 
2179 		/*
2180 		 * We should only check the errors where there is an extent.
2181 		 * As we may hit an empty data stripe while it's missing.
2182 		 */
2183 		bitmap_and(&error, &error, &has_extent, stripe->nr_sectors);
2184 		if (unlikely(!bitmap_empty(&error, stripe->nr_sectors))) {
2185 			btrfs_err(fs_info,
2186 "scrub: unrepaired sectors detected, full stripe %llu data stripe %u errors %*pbl",
2187 				  full_stripe_start, i, stripe->nr_sectors,
2188 				  &error);
2189 			ret = -EIO;
2190 			goto out;
2191 		}
2192 		bitmap_or(&extent_bitmap, &extent_bitmap, &has_extent,
2193 			  stripe->nr_sectors);
2194 	}
2195 
2196 	/* Now we can check and regenerate the P/Q stripe. */
2197 	bio = bio_alloc(NULL, 1, REQ_OP_READ, GFP_NOFS);
2198 	bio->bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT;
2199 	bio->bi_private = &io_done;
2200 	bio->bi_end_io = raid56_scrub_wait_endio;
2201 
2202 	btrfs_bio_counter_inc_blocked(fs_info);
2203 	ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
2204 			      &length, &bioc, NULL, NULL);
2205 	if (ret < 0) {
2206 		bio_put(bio);
2207 		btrfs_put_bioc(bioc);
2208 		btrfs_bio_counter_dec(fs_info);
2209 		goto out;
2210 	}
2211 	rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, scrub_dev, &extent_bitmap,
2212 				BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
2213 	btrfs_put_bioc(bioc);
2214 	if (!rbio) {
2215 		ret = -ENOMEM;
2216 		bio_put(bio);
2217 		btrfs_bio_counter_dec(fs_info);
2218 		goto out;
2219 	}
2220 	/* Use the recovered stripes as cache to avoid read them from disk again. */
2221 	for (int i = 0; i < data_stripes; i++) {
2222 		stripe = &sctx->raid56_data_stripes[i];
2223 
2224 		raid56_parity_cache_data_folios(rbio, stripe->folios,
2225 				full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT));
2226 	}
2227 	raid56_parity_submit_scrub_rbio(rbio);
2228 	wait_for_completion_io(&io_done);
2229 	ret = blk_status_to_errno(bio->bi_status);
2230 	bio_put(bio);
2231 	btrfs_bio_counter_dec(fs_info);
2232 
2233 	btrfs_release_path(&extent_path);
2234 	btrfs_release_path(&csum_path);
2235 out:
2236 	return ret;
2237 }
2238 
2239 /*
2240  * Scrub one range which can only has simple mirror based profile.
2241  * (Including all range in SINGLE/DUP/RAID1/RAID1C*, and each stripe in
2242  *  RAID0/RAID10).
2243  *
2244  * Since we may need to handle a subset of block group, we need @logical_start
2245  * and @logical_length parameter.
2246  */
scrub_simple_mirror(struct scrub_ctx * sctx,struct btrfs_block_group * bg,u64 logical_start,u64 logical_length,struct btrfs_device * device,u64 physical,int mirror_num)2247 static int scrub_simple_mirror(struct scrub_ctx *sctx,
2248 			       struct btrfs_block_group *bg,
2249 			       u64 logical_start, u64 logical_length,
2250 			       struct btrfs_device *device,
2251 			       u64 physical, int mirror_num)
2252 {
2253 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2254 	const u64 logical_end = logical_start + logical_length;
2255 	u64 cur_logical = logical_start;
2256 	int ret = 0;
2257 
2258 	/* The range must be inside the bg */
2259 	ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length);
2260 
2261 	/* Go through each extent items inside the logical range */
2262 	while (cur_logical < logical_end) {
2263 		u64 found_logical = U64_MAX;
2264 		u64 cur_physical = physical + cur_logical - logical_start;
2265 
2266 		/* Canceled? */
2267 		if (atomic_read(&fs_info->scrub_cancel_req) ||
2268 		    atomic_read(&sctx->cancel_req)) {
2269 			ret = -ECANCELED;
2270 			break;
2271 		}
2272 		/* Paused? */
2273 		if (atomic_read(&fs_info->scrub_pause_req)) {
2274 			/* Push queued extents */
2275 			scrub_blocked_if_needed(fs_info);
2276 		}
2277 		/* Block group removed? */
2278 		spin_lock(&bg->lock);
2279 		if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
2280 			spin_unlock(&bg->lock);
2281 			ret = 0;
2282 			break;
2283 		}
2284 		spin_unlock(&bg->lock);
2285 
2286 		ret = queue_scrub_stripe(sctx, bg, device, mirror_num,
2287 					 cur_logical, logical_end - cur_logical,
2288 					 cur_physical, &found_logical);
2289 		if (ret > 0) {
2290 			/* No more extent, just update the accounting */
2291 			spin_lock(&sctx->stat_lock);
2292 			sctx->stat.last_physical = physical + logical_length;
2293 			spin_unlock(&sctx->stat_lock);
2294 			ret = 0;
2295 			break;
2296 		}
2297 		if (ret < 0)
2298 			break;
2299 
2300 		/* queue_scrub_stripe() returned 0, @found_logical must be updated. */
2301 		ASSERT(found_logical != U64_MAX);
2302 		cur_logical = found_logical + BTRFS_STRIPE_LEN;
2303 
2304 		/* Don't hold CPU for too long time */
2305 		cond_resched();
2306 	}
2307 	return ret;
2308 }
2309 
2310 /* Calculate the full stripe length for simple stripe based profiles */
simple_stripe_full_stripe_len(const struct btrfs_chunk_map * map)2311 static u64 simple_stripe_full_stripe_len(const struct btrfs_chunk_map *map)
2312 {
2313 	ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2314 			    BTRFS_BLOCK_GROUP_RAID10));
2315 
2316 	return btrfs_stripe_nr_to_offset(map->num_stripes / map->sub_stripes);
2317 }
2318 
2319 /* Get the logical bytenr for the stripe */
simple_stripe_get_logical(struct btrfs_chunk_map * map,struct btrfs_block_group * bg,int stripe_index)2320 static u64 simple_stripe_get_logical(struct btrfs_chunk_map *map,
2321 				     struct btrfs_block_group *bg,
2322 				     int stripe_index)
2323 {
2324 	ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2325 			    BTRFS_BLOCK_GROUP_RAID10));
2326 	ASSERT(stripe_index < map->num_stripes);
2327 
2328 	/*
2329 	 * (stripe_index / sub_stripes) gives how many data stripes we need to
2330 	 * skip.
2331 	 */
2332 	return btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes) +
2333 	       bg->start;
2334 }
2335 
2336 /* Get the mirror number for the stripe */
simple_stripe_mirror_num(struct btrfs_chunk_map * map,int stripe_index)2337 static int simple_stripe_mirror_num(struct btrfs_chunk_map *map, int stripe_index)
2338 {
2339 	ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2340 			    BTRFS_BLOCK_GROUP_RAID10));
2341 	ASSERT(stripe_index < map->num_stripes);
2342 
2343 	/* For RAID0, it's fixed to 1, for RAID10 it's 0,1,0,1... */
2344 	return stripe_index % map->sub_stripes + 1;
2345 }
2346 
scrub_simple_stripe(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct btrfs_chunk_map * map,struct btrfs_device * device,int stripe_index)2347 static int scrub_simple_stripe(struct scrub_ctx *sctx,
2348 			       struct btrfs_block_group *bg,
2349 			       struct btrfs_chunk_map *map,
2350 			       struct btrfs_device *device,
2351 			       int stripe_index)
2352 {
2353 	const u64 logical_increment = simple_stripe_full_stripe_len(map);
2354 	const u64 orig_logical = simple_stripe_get_logical(map, bg, stripe_index);
2355 	const u64 orig_physical = map->stripes[stripe_index].physical;
2356 	const int mirror_num = simple_stripe_mirror_num(map, stripe_index);
2357 	u64 cur_logical = orig_logical;
2358 	u64 cur_physical = orig_physical;
2359 	int ret = 0;
2360 
2361 	while (cur_logical < bg->start + bg->length) {
2362 		/*
2363 		 * Inside each stripe, RAID0 is just SINGLE, and RAID10 is
2364 		 * just RAID1, so we can reuse scrub_simple_mirror() to scrub
2365 		 * this stripe.
2366 		 */
2367 		ret = scrub_simple_mirror(sctx, bg, cur_logical,
2368 					  BTRFS_STRIPE_LEN, device, cur_physical,
2369 					  mirror_num);
2370 		if (ret)
2371 			return ret;
2372 		/* Skip to next stripe which belongs to the target device */
2373 		cur_logical += logical_increment;
2374 		/* For physical offset, we just go to next stripe */
2375 		cur_physical += BTRFS_STRIPE_LEN;
2376 	}
2377 	return ret;
2378 }
2379 
scrub_stripe(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct btrfs_chunk_map * map,struct btrfs_device * scrub_dev,int stripe_index)2380 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
2381 					   struct btrfs_block_group *bg,
2382 					   struct btrfs_chunk_map *map,
2383 					   struct btrfs_device *scrub_dev,
2384 					   int stripe_index)
2385 {
2386 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2387 	const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
2388 	const u64 chunk_logical = bg->start;
2389 	int ret;
2390 	int ret2;
2391 	u64 physical = map->stripes[stripe_index].physical;
2392 	const u64 dev_stripe_len = btrfs_calc_stripe_length(map);
2393 	const u64 physical_end = physical + dev_stripe_len;
2394 	u64 logical;
2395 	u64 logic_end;
2396 	/* The logical increment after finishing one stripe */
2397 	u64 increment;
2398 	/* Offset inside the chunk */
2399 	u64 offset;
2400 	u64 stripe_logical;
2401 
2402 	/* Extent_path should be released by now. */
2403 	ASSERT(sctx->extent_path.nodes[0] == NULL);
2404 
2405 	scrub_blocked_if_needed(fs_info);
2406 
2407 	if (sctx->is_dev_replace &&
2408 	    btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
2409 		mutex_lock(&sctx->wr_lock);
2410 		sctx->write_pointer = physical;
2411 		mutex_unlock(&sctx->wr_lock);
2412 	}
2413 
2414 	/* Prepare the extra data stripes used by RAID56. */
2415 	if (profile & BTRFS_BLOCK_GROUP_RAID56_MASK) {
2416 		ASSERT(sctx->raid56_data_stripes == NULL);
2417 
2418 		sctx->raid56_data_stripes = kcalloc(nr_data_stripes(map),
2419 						    sizeof(struct scrub_stripe),
2420 						    GFP_KERNEL);
2421 		if (!sctx->raid56_data_stripes) {
2422 			ret = -ENOMEM;
2423 			goto out;
2424 		}
2425 		for (int i = 0; i < nr_data_stripes(map); i++) {
2426 			ret = init_scrub_stripe(fs_info,
2427 						&sctx->raid56_data_stripes[i]);
2428 			if (ret < 0)
2429 				goto out;
2430 			sctx->raid56_data_stripes[i].bg = bg;
2431 			sctx->raid56_data_stripes[i].sctx = sctx;
2432 		}
2433 	}
2434 	/*
2435 	 * There used to be a big double loop to handle all profiles using the
2436 	 * same routine, which grows larger and more gross over time.
2437 	 *
2438 	 * So here we handle each profile differently, so simpler profiles
2439 	 * have simpler scrubbing function.
2440 	 */
2441 	if (!(profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10 |
2442 			 BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2443 		/*
2444 		 * Above check rules out all complex profile, the remaining
2445 		 * profiles are SINGLE|DUP|RAID1|RAID1C*, which is simple
2446 		 * mirrored duplication without stripe.
2447 		 *
2448 		 * Only @physical and @mirror_num needs to calculated using
2449 		 * @stripe_index.
2450 		 */
2451 		ret = scrub_simple_mirror(sctx, bg, bg->start, bg->length,
2452 				scrub_dev, map->stripes[stripe_index].physical,
2453 				stripe_index + 1);
2454 		offset = 0;
2455 		goto out;
2456 	}
2457 	if (profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
2458 		ret = scrub_simple_stripe(sctx, bg, map, scrub_dev, stripe_index);
2459 		offset = btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes);
2460 		goto out;
2461 	}
2462 
2463 	/* Only RAID56 goes through the old code */
2464 	ASSERT(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK);
2465 	ret = 0;
2466 
2467 	/* Calculate the logical end of the stripe */
2468 	get_raid56_logic_offset(physical_end, stripe_index,
2469 				map, &logic_end, NULL);
2470 	logic_end += chunk_logical;
2471 
2472 	/* Initialize @offset in case we need to go to out: label */
2473 	get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
2474 	increment = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
2475 
2476 	/*
2477 	 * Due to the rotation, for RAID56 it's better to iterate each stripe
2478 	 * using their physical offset.
2479 	 */
2480 	while (physical < physical_end) {
2481 		ret = get_raid56_logic_offset(physical, stripe_index, map,
2482 					      &logical, &stripe_logical);
2483 		logical += chunk_logical;
2484 		if (ret) {
2485 			/* it is parity strip */
2486 			stripe_logical += chunk_logical;
2487 			ret = scrub_raid56_parity_stripe(sctx, scrub_dev, bg,
2488 							 map, stripe_logical);
2489 			spin_lock(&sctx->stat_lock);
2490 			sctx->stat.last_physical = min(physical + BTRFS_STRIPE_LEN,
2491 						       physical_end);
2492 			spin_unlock(&sctx->stat_lock);
2493 			if (ret)
2494 				goto out;
2495 			goto next;
2496 		}
2497 
2498 		/*
2499 		 * Now we're at a data stripe, scrub each extents in the range.
2500 		 *
2501 		 * At this stage, if we ignore the repair part, inside each data
2502 		 * stripe it is no different than SINGLE profile.
2503 		 * We can reuse scrub_simple_mirror() here, as the repair part
2504 		 * is still based on @mirror_num.
2505 		 */
2506 		ret = scrub_simple_mirror(sctx, bg, logical, BTRFS_STRIPE_LEN,
2507 					  scrub_dev, physical, 1);
2508 		if (ret < 0)
2509 			goto out;
2510 next:
2511 		logical += increment;
2512 		physical += BTRFS_STRIPE_LEN;
2513 		spin_lock(&sctx->stat_lock);
2514 		sctx->stat.last_physical = physical;
2515 		spin_unlock(&sctx->stat_lock);
2516 	}
2517 out:
2518 	ret2 = flush_scrub_stripes(sctx);
2519 	if (!ret)
2520 		ret = ret2;
2521 	btrfs_release_path(&sctx->extent_path);
2522 	btrfs_release_path(&sctx->csum_path);
2523 
2524 	if (sctx->raid56_data_stripes) {
2525 		for (int i = 0; i < nr_data_stripes(map); i++)
2526 			release_scrub_stripe(&sctx->raid56_data_stripes[i]);
2527 		kfree(sctx->raid56_data_stripes);
2528 		sctx->raid56_data_stripes = NULL;
2529 	}
2530 
2531 	if (sctx->is_dev_replace && ret >= 0) {
2532 		int ret2;
2533 
2534 		ret2 = sync_write_pointer_for_zoned(sctx,
2535 				chunk_logical + offset,
2536 				map->stripes[stripe_index].physical,
2537 				physical_end);
2538 		if (ret2)
2539 			ret = ret2;
2540 	}
2541 
2542 	return ret < 0 ? ret : 0;
2543 }
2544 
scrub_chunk(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct btrfs_device * scrub_dev,u64 dev_offset,u64 dev_extent_len)2545 static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
2546 					  struct btrfs_block_group *bg,
2547 					  struct btrfs_device *scrub_dev,
2548 					  u64 dev_offset,
2549 					  u64 dev_extent_len)
2550 {
2551 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2552 	struct btrfs_chunk_map *map;
2553 	int i;
2554 	int ret = 0;
2555 
2556 	map = btrfs_find_chunk_map(fs_info, bg->start, bg->length);
2557 	if (!map) {
2558 		/*
2559 		 * Might have been an unused block group deleted by the cleaner
2560 		 * kthread or relocation.
2561 		 */
2562 		spin_lock(&bg->lock);
2563 		if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags))
2564 			ret = -EINVAL;
2565 		spin_unlock(&bg->lock);
2566 
2567 		return ret;
2568 	}
2569 	if (map->start != bg->start)
2570 		goto out;
2571 	if (map->chunk_len < dev_extent_len)
2572 		goto out;
2573 
2574 	for (i = 0; i < map->num_stripes; ++i) {
2575 		if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
2576 		    map->stripes[i].physical == dev_offset) {
2577 			ret = scrub_stripe(sctx, bg, map, scrub_dev, i);
2578 			if (ret)
2579 				goto out;
2580 		}
2581 	}
2582 out:
2583 	btrfs_free_chunk_map(map);
2584 
2585 	return ret;
2586 }
2587 
finish_extent_writes_for_zoned(struct btrfs_root * root,struct btrfs_block_group * cache)2588 static int finish_extent_writes_for_zoned(struct btrfs_root *root,
2589 					  struct btrfs_block_group *cache)
2590 {
2591 	struct btrfs_fs_info *fs_info = cache->fs_info;
2592 
2593 	if (!btrfs_is_zoned(fs_info))
2594 		return 0;
2595 
2596 	btrfs_wait_block_group_reservations(cache);
2597 	btrfs_wait_nocow_writers(cache);
2598 	btrfs_wait_ordered_roots(fs_info, U64_MAX, cache);
2599 
2600 	return btrfs_commit_current_transaction(root);
2601 }
2602 
2603 static noinline_for_stack
scrub_enumerate_chunks(struct scrub_ctx * sctx,struct btrfs_device * scrub_dev,u64 start,u64 end)2604 int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2605 			   struct btrfs_device *scrub_dev, u64 start, u64 end)
2606 {
2607 	struct btrfs_dev_extent *dev_extent = NULL;
2608 	BTRFS_PATH_AUTO_FREE(path);
2609 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2610 	struct btrfs_root *root = fs_info->dev_root;
2611 	u64 chunk_offset;
2612 	int ret = 0;
2613 	int ro_set;
2614 	int slot;
2615 	struct extent_buffer *l;
2616 	struct btrfs_key key;
2617 	struct btrfs_key found_key;
2618 	struct btrfs_block_group *cache;
2619 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
2620 
2621 	path = btrfs_alloc_path();
2622 	if (!path)
2623 		return -ENOMEM;
2624 
2625 	path->reada = READA_FORWARD;
2626 	path->search_commit_root = 1;
2627 	path->skip_locking = 1;
2628 
2629 	key.objectid = scrub_dev->devid;
2630 	key.type = BTRFS_DEV_EXTENT_KEY;
2631 	key.offset = 0ull;
2632 
2633 	while (1) {
2634 		u64 dev_extent_len;
2635 
2636 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2637 		if (ret < 0)
2638 			break;
2639 		if (ret > 0) {
2640 			if (path->slots[0] >=
2641 			    btrfs_header_nritems(path->nodes[0])) {
2642 				ret = btrfs_next_leaf(root, path);
2643 				if (ret < 0)
2644 					break;
2645 				if (ret > 0) {
2646 					ret = 0;
2647 					break;
2648 				}
2649 			} else {
2650 				ret = 0;
2651 			}
2652 		}
2653 
2654 		l = path->nodes[0];
2655 		slot = path->slots[0];
2656 
2657 		btrfs_item_key_to_cpu(l, &found_key, slot);
2658 
2659 		if (found_key.objectid != scrub_dev->devid)
2660 			break;
2661 
2662 		if (found_key.type != BTRFS_DEV_EXTENT_KEY)
2663 			break;
2664 
2665 		if (found_key.offset >= end)
2666 			break;
2667 
2668 		if (found_key.offset < key.offset)
2669 			break;
2670 
2671 		dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2672 		dev_extent_len = btrfs_dev_extent_length(l, dev_extent);
2673 
2674 		if (found_key.offset + dev_extent_len <= start)
2675 			goto skip;
2676 
2677 		chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2678 
2679 		/*
2680 		 * get a reference on the corresponding block group to prevent
2681 		 * the chunk from going away while we scrub it
2682 		 */
2683 		cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2684 
2685 		/* some chunks are removed but not committed to disk yet,
2686 		 * continue scrubbing */
2687 		if (!cache)
2688 			goto skip;
2689 
2690 		ASSERT(cache->start <= chunk_offset);
2691 		/*
2692 		 * We are using the commit root to search for device extents, so
2693 		 * that means we could have found a device extent item from a
2694 		 * block group that was deleted in the current transaction. The
2695 		 * logical start offset of the deleted block group, stored at
2696 		 * @chunk_offset, might be part of the logical address range of
2697 		 * a new block group (which uses different physical extents).
2698 		 * In this case btrfs_lookup_block_group() has returned the new
2699 		 * block group, and its start address is less than @chunk_offset.
2700 		 *
2701 		 * We skip such new block groups, because it's pointless to
2702 		 * process them, as we won't find their extents because we search
2703 		 * for them using the commit root of the extent tree. For a device
2704 		 * replace it's also fine to skip it, we won't miss copying them
2705 		 * to the target device because we have the write duplication
2706 		 * setup through the regular write path (by btrfs_map_block()),
2707 		 * and we have committed a transaction when we started the device
2708 		 * replace, right after setting up the device replace state.
2709 		 */
2710 		if (cache->start < chunk_offset) {
2711 			btrfs_put_block_group(cache);
2712 			goto skip;
2713 		}
2714 
2715 		if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
2716 			if (!test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags)) {
2717 				btrfs_put_block_group(cache);
2718 				goto skip;
2719 			}
2720 		}
2721 
2722 		/*
2723 		 * Make sure that while we are scrubbing the corresponding block
2724 		 * group doesn't get its logical address and its device extents
2725 		 * reused for another block group, which can possibly be of a
2726 		 * different type and different profile. We do this to prevent
2727 		 * false error detections and crashes due to bogus attempts to
2728 		 * repair extents.
2729 		 */
2730 		spin_lock(&cache->lock);
2731 		if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
2732 			spin_unlock(&cache->lock);
2733 			btrfs_put_block_group(cache);
2734 			goto skip;
2735 		}
2736 		btrfs_freeze_block_group(cache);
2737 		spin_unlock(&cache->lock);
2738 
2739 		/*
2740 		 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
2741 		 * to avoid deadlock caused by:
2742 		 * btrfs_inc_block_group_ro()
2743 		 * -> btrfs_wait_for_commit()
2744 		 * -> btrfs_commit_transaction()
2745 		 * -> btrfs_scrub_pause()
2746 		 */
2747 		scrub_pause_on(fs_info);
2748 
2749 		/*
2750 		 * Don't do chunk preallocation for scrub.
2751 		 *
2752 		 * This is especially important for SYSTEM bgs, or we can hit
2753 		 * -EFBIG from btrfs_finish_chunk_alloc() like:
2754 		 * 1. The only SYSTEM bg is marked RO.
2755 		 *    Since SYSTEM bg is small, that's pretty common.
2756 		 * 2. New SYSTEM bg will be allocated
2757 		 *    Due to regular version will allocate new chunk.
2758 		 * 3. New SYSTEM bg is empty and will get cleaned up
2759 		 *    Before cleanup really happens, it's marked RO again.
2760 		 * 4. Empty SYSTEM bg get scrubbed
2761 		 *    We go back to 2.
2762 		 *
2763 		 * This can easily boost the amount of SYSTEM chunks if cleaner
2764 		 * thread can't be triggered fast enough, and use up all space
2765 		 * of btrfs_super_block::sys_chunk_array
2766 		 *
2767 		 * While for dev replace, we need to try our best to mark block
2768 		 * group RO, to prevent race between:
2769 		 * - Write duplication
2770 		 *   Contains latest data
2771 		 * - Scrub copy
2772 		 *   Contains data from commit tree
2773 		 *
2774 		 * If target block group is not marked RO, nocow writes can
2775 		 * be overwritten by scrub copy, causing data corruption.
2776 		 * So for dev-replace, it's not allowed to continue if a block
2777 		 * group is not RO.
2778 		 */
2779 		ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
2780 		if (!ret && sctx->is_dev_replace) {
2781 			ret = finish_extent_writes_for_zoned(root, cache);
2782 			if (ret) {
2783 				btrfs_dec_block_group_ro(cache);
2784 				scrub_pause_off(fs_info);
2785 				btrfs_put_block_group(cache);
2786 				break;
2787 			}
2788 		}
2789 
2790 		if (ret == 0) {
2791 			ro_set = 1;
2792 		} else if (ret == -ENOSPC && !sctx->is_dev_replace &&
2793 			   !(cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) {
2794 			/*
2795 			 * btrfs_inc_block_group_ro return -ENOSPC when it
2796 			 * failed in creating new chunk for metadata.
2797 			 * It is not a problem for scrub, because
2798 			 * metadata are always cowed, and our scrub paused
2799 			 * commit_transactions.
2800 			 *
2801 			 * For RAID56 chunks, we have to mark them read-only
2802 			 * for scrub, as later we would use our own cache
2803 			 * out of RAID56 realm.
2804 			 * Thus we want the RAID56 bg to be marked RO to
2805 			 * prevent RMW from screwing up out cache.
2806 			 */
2807 			ro_set = 0;
2808 		} else if (ret == -ETXTBSY) {
2809 			btrfs_warn(fs_info,
2810 	     "scrub: skipping scrub of block group %llu due to active swapfile",
2811 				   cache->start);
2812 			scrub_pause_off(fs_info);
2813 			ret = 0;
2814 			goto skip_unfreeze;
2815 		} else {
2816 			btrfs_warn(fs_info, "scrub: failed setting block group ro: %d",
2817 				   ret);
2818 			btrfs_unfreeze_block_group(cache);
2819 			btrfs_put_block_group(cache);
2820 			scrub_pause_off(fs_info);
2821 			break;
2822 		}
2823 
2824 		/*
2825 		 * Now the target block is marked RO, wait for nocow writes to
2826 		 * finish before dev-replace.
2827 		 * COW is fine, as COW never overwrites extents in commit tree.
2828 		 */
2829 		if (sctx->is_dev_replace) {
2830 			btrfs_wait_nocow_writers(cache);
2831 			btrfs_wait_ordered_roots(fs_info, U64_MAX, cache);
2832 		}
2833 
2834 		scrub_pause_off(fs_info);
2835 		down_write(&dev_replace->rwsem);
2836 		dev_replace->cursor_right = found_key.offset + dev_extent_len;
2837 		dev_replace->cursor_left = found_key.offset;
2838 		dev_replace->item_needs_writeback = 1;
2839 		up_write(&dev_replace->rwsem);
2840 
2841 		ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset,
2842 				  dev_extent_len);
2843 		if (sctx->is_dev_replace &&
2844 		    !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
2845 						      cache, found_key.offset))
2846 			ro_set = 0;
2847 
2848 		down_write(&dev_replace->rwsem);
2849 		dev_replace->cursor_left = dev_replace->cursor_right;
2850 		dev_replace->item_needs_writeback = 1;
2851 		up_write(&dev_replace->rwsem);
2852 
2853 		if (ro_set)
2854 			btrfs_dec_block_group_ro(cache);
2855 
2856 		/*
2857 		 * We might have prevented the cleaner kthread from deleting
2858 		 * this block group if it was already unused because we raced
2859 		 * and set it to RO mode first. So add it back to the unused
2860 		 * list, otherwise it might not ever be deleted unless a manual
2861 		 * balance is triggered or it becomes used and unused again.
2862 		 */
2863 		spin_lock(&cache->lock);
2864 		if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags) &&
2865 		    !cache->ro && cache->reserved == 0 && cache->used == 0) {
2866 			spin_unlock(&cache->lock);
2867 			if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
2868 				btrfs_discard_queue_work(&fs_info->discard_ctl,
2869 							 cache);
2870 			else
2871 				btrfs_mark_bg_unused(cache);
2872 		} else {
2873 			spin_unlock(&cache->lock);
2874 		}
2875 skip_unfreeze:
2876 		btrfs_unfreeze_block_group(cache);
2877 		btrfs_put_block_group(cache);
2878 		if (ret)
2879 			break;
2880 		if (unlikely(sctx->is_dev_replace &&
2881 			     atomic64_read(&dev_replace->num_write_errors) > 0)) {
2882 			ret = -EIO;
2883 			break;
2884 		}
2885 		if (sctx->stat.malloc_errors > 0) {
2886 			ret = -ENOMEM;
2887 			break;
2888 		}
2889 skip:
2890 		key.offset = found_key.offset + dev_extent_len;
2891 		btrfs_release_path(path);
2892 	}
2893 
2894 	return ret;
2895 }
2896 
scrub_one_super(struct scrub_ctx * sctx,struct btrfs_device * dev,struct page * page,u64 physical,u64 generation)2897 static int scrub_one_super(struct scrub_ctx *sctx, struct btrfs_device *dev,
2898 			   struct page *page, u64 physical, u64 generation)
2899 {
2900 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2901 	struct btrfs_super_block *sb = page_address(page);
2902 	int ret;
2903 
2904 	ret = bdev_rw_virt(dev->bdev, physical >> SECTOR_SHIFT, sb,
2905 			BTRFS_SUPER_INFO_SIZE, REQ_OP_READ);
2906 	if (ret < 0)
2907 		return ret;
2908 	ret = btrfs_check_super_csum(fs_info, sb);
2909 	if (unlikely(ret != 0)) {
2910 		btrfs_err_rl(fs_info,
2911 		  "scrub: super block at physical %llu devid %llu has bad csum",
2912 			physical, dev->devid);
2913 		return -EIO;
2914 	}
2915 	if (unlikely(btrfs_super_generation(sb) != generation)) {
2916 		btrfs_err_rl(fs_info,
2917 "scrub: super block at physical %llu devid %llu has bad generation %llu expect %llu",
2918 			     physical, dev->devid,
2919 			     btrfs_super_generation(sb), generation);
2920 		return -EUCLEAN;
2921 	}
2922 
2923 	return btrfs_validate_super(fs_info, sb, -1);
2924 }
2925 
scrub_supers(struct scrub_ctx * sctx,struct btrfs_device * scrub_dev)2926 static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2927 					   struct btrfs_device *scrub_dev)
2928 {
2929 	int	i;
2930 	u64	bytenr;
2931 	u64	gen;
2932 	int ret = 0;
2933 	struct page *page;
2934 	struct btrfs_fs_info *fs_info = sctx->fs_info;
2935 
2936 	if (BTRFS_FS_ERROR(fs_info))
2937 		return -EROFS;
2938 
2939 	page = alloc_page(GFP_KERNEL);
2940 	if (!page) {
2941 		spin_lock(&sctx->stat_lock);
2942 		sctx->stat.malloc_errors++;
2943 		spin_unlock(&sctx->stat_lock);
2944 		return -ENOMEM;
2945 	}
2946 
2947 	/* Seed devices of a new filesystem has their own generation. */
2948 	if (scrub_dev->fs_devices != fs_info->fs_devices)
2949 		gen = scrub_dev->generation;
2950 	else
2951 		gen = btrfs_get_last_trans_committed(fs_info);
2952 
2953 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2954 		ret = btrfs_sb_log_location(scrub_dev, i, 0, &bytenr);
2955 		if (ret == -ENOENT)
2956 			break;
2957 
2958 		if (ret) {
2959 			spin_lock(&sctx->stat_lock);
2960 			sctx->stat.super_errors++;
2961 			spin_unlock(&sctx->stat_lock);
2962 			continue;
2963 		}
2964 
2965 		if (bytenr + BTRFS_SUPER_INFO_SIZE >
2966 		    scrub_dev->commit_total_bytes)
2967 			break;
2968 		if (!btrfs_check_super_location(scrub_dev, bytenr))
2969 			continue;
2970 
2971 		ret = scrub_one_super(sctx, scrub_dev, page, bytenr, gen);
2972 		if (ret) {
2973 			spin_lock(&sctx->stat_lock);
2974 			sctx->stat.super_errors++;
2975 			spin_unlock(&sctx->stat_lock);
2976 		}
2977 	}
2978 	__free_page(page);
2979 	return 0;
2980 }
2981 
scrub_workers_put(struct btrfs_fs_info * fs_info)2982 static void scrub_workers_put(struct btrfs_fs_info *fs_info)
2983 {
2984 	if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
2985 					&fs_info->scrub_lock)) {
2986 		struct workqueue_struct *scrub_workers = fs_info->scrub_workers;
2987 
2988 		fs_info->scrub_workers = NULL;
2989 		mutex_unlock(&fs_info->scrub_lock);
2990 
2991 		if (scrub_workers)
2992 			destroy_workqueue(scrub_workers);
2993 	}
2994 }
2995 
2996 /*
2997  * get a reference count on fs_info->scrub_workers. start worker if necessary
2998  */
scrub_workers_get(struct btrfs_fs_info * fs_info)2999 static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info)
3000 {
3001 	struct workqueue_struct *scrub_workers = NULL;
3002 	unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
3003 	int max_active = fs_info->thread_pool_size;
3004 	int ret = -ENOMEM;
3005 
3006 	if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
3007 		return 0;
3008 
3009 	scrub_workers = alloc_workqueue("btrfs-scrub", flags, max_active);
3010 	if (!scrub_workers)
3011 		return -ENOMEM;
3012 
3013 	mutex_lock(&fs_info->scrub_lock);
3014 	if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
3015 		ASSERT(fs_info->scrub_workers == NULL);
3016 		fs_info->scrub_workers = scrub_workers;
3017 		refcount_set(&fs_info->scrub_workers_refcnt, 1);
3018 		mutex_unlock(&fs_info->scrub_lock);
3019 		return 0;
3020 	}
3021 	/* Other thread raced in and created the workers for us */
3022 	refcount_inc(&fs_info->scrub_workers_refcnt);
3023 	mutex_unlock(&fs_info->scrub_lock);
3024 
3025 	ret = 0;
3026 
3027 	destroy_workqueue(scrub_workers);
3028 	return ret;
3029 }
3030 
btrfs_scrub_dev(struct btrfs_fs_info * fs_info,u64 devid,u64 start,u64 end,struct btrfs_scrub_progress * progress,bool readonly,bool is_dev_replace)3031 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3032 		    u64 end, struct btrfs_scrub_progress *progress,
3033 		    bool readonly, bool is_dev_replace)
3034 {
3035 	struct btrfs_dev_lookup_args args = { .devid = devid };
3036 	struct scrub_ctx *sctx;
3037 	int ret;
3038 	struct btrfs_device *dev;
3039 	unsigned int nofs_flag;
3040 	bool need_commit = false;
3041 
3042 	if (btrfs_fs_closing(fs_info))
3043 		return -EAGAIN;
3044 
3045 	/* At mount time we have ensured nodesize is in the range of [4K, 64K]. */
3046 	ASSERT(fs_info->nodesize <= BTRFS_STRIPE_LEN);
3047 
3048 	/*
3049 	 * SCRUB_MAX_SECTORS_PER_BLOCK is calculated using the largest possible
3050 	 * value (max nodesize / min sectorsize), thus nodesize should always
3051 	 * be fine.
3052 	 */
3053 	ASSERT(fs_info->nodesize <=
3054 	       SCRUB_MAX_SECTORS_PER_BLOCK << fs_info->sectorsize_bits);
3055 
3056 	/* Allocate outside of device_list_mutex */
3057 	sctx = scrub_setup_ctx(fs_info, is_dev_replace);
3058 	if (IS_ERR(sctx))
3059 		return PTR_ERR(sctx);
3060 
3061 	ret = scrub_workers_get(fs_info);
3062 	if (ret)
3063 		goto out_free_ctx;
3064 
3065 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
3066 	dev = btrfs_find_device(fs_info->fs_devices, &args);
3067 	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
3068 		     !is_dev_replace)) {
3069 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3070 		ret = -ENODEV;
3071 		goto out;
3072 	}
3073 
3074 	if (!is_dev_replace && !readonly &&
3075 	    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
3076 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3077 		btrfs_err(fs_info,
3078 			"scrub: devid %llu: filesystem on %s is not writable",
3079 				 devid, btrfs_dev_name(dev));
3080 		ret = -EROFS;
3081 		goto out;
3082 	}
3083 
3084 	mutex_lock(&fs_info->scrub_lock);
3085 	if (unlikely(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
3086 		     test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state))) {
3087 		mutex_unlock(&fs_info->scrub_lock);
3088 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3089 		ret = -EIO;
3090 		goto out;
3091 	}
3092 
3093 	down_read(&fs_info->dev_replace.rwsem);
3094 	if (dev->scrub_ctx ||
3095 	    (!is_dev_replace &&
3096 	     btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3097 		up_read(&fs_info->dev_replace.rwsem);
3098 		mutex_unlock(&fs_info->scrub_lock);
3099 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3100 		ret = -EINPROGRESS;
3101 		goto out;
3102 	}
3103 	up_read(&fs_info->dev_replace.rwsem);
3104 
3105 	sctx->readonly = readonly;
3106 	dev->scrub_ctx = sctx;
3107 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3108 
3109 	/*
3110 	 * checking @scrub_pause_req here, we can avoid
3111 	 * race between committing transaction and scrubbing.
3112 	 */
3113 	__scrub_blocked_if_needed(fs_info);
3114 	atomic_inc(&fs_info->scrubs_running);
3115 	mutex_unlock(&fs_info->scrub_lock);
3116 
3117 	/*
3118 	 * In order to avoid deadlock with reclaim when there is a transaction
3119 	 * trying to pause scrub, make sure we use GFP_NOFS for all the
3120 	 * allocations done at btrfs_scrub_sectors() and scrub_sectors_for_parity()
3121 	 * invoked by our callees. The pausing request is done when the
3122 	 * transaction commit starts, and it blocks the transaction until scrub
3123 	 * is paused (done at specific points at scrub_stripe() or right above
3124 	 * before incrementing fs_info->scrubs_running).
3125 	 */
3126 	nofs_flag = memalloc_nofs_save();
3127 	if (!is_dev_replace) {
3128 		u64 old_super_errors;
3129 
3130 		spin_lock(&sctx->stat_lock);
3131 		old_super_errors = sctx->stat.super_errors;
3132 		spin_unlock(&sctx->stat_lock);
3133 
3134 		btrfs_info(fs_info, "scrub: started on devid %llu", devid);
3135 		/*
3136 		 * by holding device list mutex, we can
3137 		 * kick off writing super in log tree sync.
3138 		 */
3139 		mutex_lock(&fs_info->fs_devices->device_list_mutex);
3140 		ret = scrub_supers(sctx, dev);
3141 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3142 
3143 		spin_lock(&sctx->stat_lock);
3144 		/*
3145 		 * Super block errors found, but we can not commit transaction
3146 		 * at current context, since btrfs_commit_transaction() needs
3147 		 * to pause the current running scrub (hold by ourselves).
3148 		 */
3149 		if (sctx->stat.super_errors > old_super_errors && !sctx->readonly)
3150 			need_commit = true;
3151 		spin_unlock(&sctx->stat_lock);
3152 	}
3153 
3154 	if (!ret)
3155 		ret = scrub_enumerate_chunks(sctx, dev, start, end);
3156 	memalloc_nofs_restore(nofs_flag);
3157 
3158 	atomic_dec(&fs_info->scrubs_running);
3159 	wake_up(&fs_info->scrub_pause_wait);
3160 
3161 	if (progress)
3162 		memcpy(progress, &sctx->stat, sizeof(*progress));
3163 
3164 	if (!is_dev_replace)
3165 		btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
3166 			ret ? "not finished" : "finished", devid, ret);
3167 
3168 	mutex_lock(&fs_info->scrub_lock);
3169 	dev->scrub_ctx = NULL;
3170 	mutex_unlock(&fs_info->scrub_lock);
3171 
3172 	scrub_workers_put(fs_info);
3173 	scrub_put_ctx(sctx);
3174 
3175 	/*
3176 	 * We found some super block errors before, now try to force a
3177 	 * transaction commit, as scrub has finished.
3178 	 */
3179 	if (need_commit) {
3180 		struct btrfs_trans_handle *trans;
3181 
3182 		trans = btrfs_start_transaction(fs_info->tree_root, 0);
3183 		if (IS_ERR(trans)) {
3184 			ret = PTR_ERR(trans);
3185 			btrfs_err(fs_info,
3186 	"scrub: failed to start transaction to fix super block errors: %d", ret);
3187 			return ret;
3188 		}
3189 		ret = btrfs_commit_transaction(trans);
3190 		if (ret < 0)
3191 			btrfs_err(fs_info,
3192 	"scrub: failed to commit transaction to fix super block errors: %d", ret);
3193 	}
3194 	return ret;
3195 out:
3196 	scrub_workers_put(fs_info);
3197 out_free_ctx:
3198 	scrub_free_ctx(sctx);
3199 
3200 	return ret;
3201 }
3202 
btrfs_scrub_pause(struct btrfs_fs_info * fs_info)3203 void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
3204 {
3205 	mutex_lock(&fs_info->scrub_lock);
3206 	atomic_inc(&fs_info->scrub_pause_req);
3207 	while (atomic_read(&fs_info->scrubs_paused) !=
3208 	       atomic_read(&fs_info->scrubs_running)) {
3209 		mutex_unlock(&fs_info->scrub_lock);
3210 		wait_event(fs_info->scrub_pause_wait,
3211 			   atomic_read(&fs_info->scrubs_paused) ==
3212 			   atomic_read(&fs_info->scrubs_running));
3213 		mutex_lock(&fs_info->scrub_lock);
3214 	}
3215 	mutex_unlock(&fs_info->scrub_lock);
3216 }
3217 
btrfs_scrub_continue(struct btrfs_fs_info * fs_info)3218 void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
3219 {
3220 	atomic_dec(&fs_info->scrub_pause_req);
3221 	wake_up(&fs_info->scrub_pause_wait);
3222 }
3223 
btrfs_scrub_cancel(struct btrfs_fs_info * fs_info)3224 int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
3225 {
3226 	mutex_lock(&fs_info->scrub_lock);
3227 	if (!atomic_read(&fs_info->scrubs_running)) {
3228 		mutex_unlock(&fs_info->scrub_lock);
3229 		return -ENOTCONN;
3230 	}
3231 
3232 	atomic_inc(&fs_info->scrub_cancel_req);
3233 	while (atomic_read(&fs_info->scrubs_running)) {
3234 		mutex_unlock(&fs_info->scrub_lock);
3235 		wait_event(fs_info->scrub_pause_wait,
3236 			   atomic_read(&fs_info->scrubs_running) == 0);
3237 		mutex_lock(&fs_info->scrub_lock);
3238 	}
3239 	atomic_dec(&fs_info->scrub_cancel_req);
3240 	mutex_unlock(&fs_info->scrub_lock);
3241 
3242 	return 0;
3243 }
3244 
btrfs_scrub_cancel_dev(struct btrfs_device * dev)3245 int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
3246 {
3247 	struct btrfs_fs_info *fs_info = dev->fs_info;
3248 	struct scrub_ctx *sctx;
3249 
3250 	mutex_lock(&fs_info->scrub_lock);
3251 	sctx = dev->scrub_ctx;
3252 	if (!sctx) {
3253 		mutex_unlock(&fs_info->scrub_lock);
3254 		return -ENOTCONN;
3255 	}
3256 	atomic_inc(&sctx->cancel_req);
3257 	while (dev->scrub_ctx) {
3258 		mutex_unlock(&fs_info->scrub_lock);
3259 		wait_event(fs_info->scrub_pause_wait,
3260 			   dev->scrub_ctx == NULL);
3261 		mutex_lock(&fs_info->scrub_lock);
3262 	}
3263 	mutex_unlock(&fs_info->scrub_lock);
3264 
3265 	return 0;
3266 }
3267 
btrfs_scrub_progress(struct btrfs_fs_info * fs_info,u64 devid,struct btrfs_scrub_progress * progress)3268 int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3269 			 struct btrfs_scrub_progress *progress)
3270 {
3271 	struct btrfs_dev_lookup_args args = { .devid = devid };
3272 	struct btrfs_device *dev;
3273 	struct scrub_ctx *sctx = NULL;
3274 
3275 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
3276 	dev = btrfs_find_device(fs_info->fs_devices, &args);
3277 	if (dev)
3278 		sctx = dev->scrub_ctx;
3279 	if (sctx)
3280 		memcpy(progress, &sctx->stat, sizeof(*progress));
3281 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3282 
3283 	return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
3284 }
3285