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