1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
6 #include <linux/blkdev.h>
7 #include <linux/ratelimit.h>
8 #include <linux/sched/mm.h>
9 #include <crypto/hash.h>
14 #include "ordered-data.h"
15 #include "transaction.h"
17 #include "extent_io.h"
18 #include "dev-replace.h"
19 #include "check-integrity.h"
20 #include "rcu-string.h"
22 #include "block-group.h"
26 * This is only the first step towards a full-features scrub. It reads all
27 * extent and super block and verifies the checksums. In case a bad checksum
28 * is found or the extent cannot be read, good data will be written back if
31 * Future enhancements:
32 * - In case an unrepairable extent is encountered, track which files are
33 * affected and report them
34 * - track and record media errors, throw out bad devices
35 * - add a mode to also read unallocated space
42 * the following three values only influence the performance.
43 * The last one configures the number of parallel and outstanding I/O
44 * operations. The first two values configure an upper limit for the number
45 * of (dynamically allocated) pages that are added to a bio.
47 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
48 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
49 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
52 * the following value times PAGE_SIZE needs to be large enough to match the
53 * largest node/leaf/sector size that shall be supported.
54 * Values larger than BTRFS_STRIPE_LEN are not supported.
56 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
58 struct scrub_recover {
60 struct btrfs_bio *bbio;
65 struct scrub_block *sblock;
67 struct btrfs_device *dev;
68 struct list_head list;
69 u64 flags; /* extent flags */
73 u64 physical_for_dev_replace;
78 u8 csum[BTRFS_CSUM_SIZE];
80 struct scrub_recover *recover;
85 struct scrub_ctx *sctx;
86 struct btrfs_device *dev;
91 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
92 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
94 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
98 struct btrfs_work work;
102 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
104 atomic_t outstanding_pages;
105 refcount_t refs; /* free mem on transition to zero */
106 struct scrub_ctx *sctx;
107 struct scrub_parity *sparity;
109 unsigned int header_error:1;
110 unsigned int checksum_error:1;
111 unsigned int no_io_error_seen:1;
112 unsigned int generation_error:1; /* also sets header_error */
114 /* The following is for the data used to check parity */
115 /* It is for the data with checksum */
116 unsigned int data_corrected:1;
118 struct btrfs_work work;
121 /* Used for the chunks with parity stripe such RAID5/6 */
122 struct scrub_parity {
123 struct scrub_ctx *sctx;
125 struct btrfs_device *scrub_dev;
137 struct list_head spages;
139 /* Work of parity check and repair */
140 struct btrfs_work work;
142 /* Mark the parity blocks which have data */
143 unsigned long *dbitmap;
146 * Mark the parity blocks which have data, but errors happen when
147 * read data or check data
149 unsigned long *ebitmap;
151 unsigned long bitmap[];
155 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
156 struct btrfs_fs_info *fs_info;
159 atomic_t bios_in_flight;
160 atomic_t workers_pending;
161 spinlock_t list_lock;
162 wait_queue_head_t list_wait;
163 struct list_head csum_list;
166 int pages_per_rd_bio;
171 struct scrub_bio *wr_curr_bio;
172 struct mutex wr_lock;
173 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
174 struct btrfs_device *wr_tgtdev;
175 bool flush_all_writes;
180 struct btrfs_scrub_progress stat;
181 spinlock_t stat_lock;
184 * Use a ref counter to avoid use-after-free issues. Scrub workers
185 * decrement bios_in_flight and workers_pending and then do a wakeup
186 * on the list_wait wait queue. We must ensure the main scrub task
187 * doesn't free the scrub context before or while the workers are
188 * doing the wakeup() call.
193 struct scrub_warning {
194 struct btrfs_path *path;
195 u64 extent_item_size;
199 struct btrfs_device *dev;
202 struct full_stripe_lock {
209 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
210 struct scrub_block *sblocks_for_recheck);
211 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
212 struct scrub_block *sblock,
213 int retry_failed_mirror);
214 static void scrub_recheck_block_checksum(struct scrub_block *sblock);
215 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
216 struct scrub_block *sblock_good);
217 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
218 struct scrub_block *sblock_good,
219 int page_num, int force_write);
220 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
221 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
223 static int scrub_checksum_data(struct scrub_block *sblock);
224 static int scrub_checksum_tree_block(struct scrub_block *sblock);
225 static int scrub_checksum_super(struct scrub_block *sblock);
226 static void scrub_block_put(struct scrub_block *sblock);
227 static void scrub_page_get(struct scrub_page *spage);
228 static void scrub_page_put(struct scrub_page *spage);
229 static void scrub_parity_get(struct scrub_parity *sparity);
230 static void scrub_parity_put(struct scrub_parity *sparity);
231 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
232 u64 physical, struct btrfs_device *dev, u64 flags,
233 u64 gen, int mirror_num, u8 *csum,
234 u64 physical_for_dev_replace);
235 static void scrub_bio_end_io(struct bio *bio);
236 static void scrub_bio_end_io_worker(struct btrfs_work *work);
237 static void scrub_block_complete(struct scrub_block *sblock);
238 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
239 u64 extent_logical, u32 extent_len,
240 u64 *extent_physical,
241 struct btrfs_device **extent_dev,
242 int *extent_mirror_num);
243 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
244 struct scrub_page *spage);
245 static void scrub_wr_submit(struct scrub_ctx *sctx);
246 static void scrub_wr_bio_end_io(struct bio *bio);
247 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
248 static void scrub_put_ctx(struct scrub_ctx *sctx);
250 static inline int scrub_is_page_on_raid56(struct scrub_page *spage)
252 return spage->recover &&
253 (spage->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
256 static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
258 refcount_inc(&sctx->refs);
259 atomic_inc(&sctx->bios_in_flight);
262 static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
264 atomic_dec(&sctx->bios_in_flight);
265 wake_up(&sctx->list_wait);
269 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
271 while (atomic_read(&fs_info->scrub_pause_req)) {
272 mutex_unlock(&fs_info->scrub_lock);
273 wait_event(fs_info->scrub_pause_wait,
274 atomic_read(&fs_info->scrub_pause_req) == 0);
275 mutex_lock(&fs_info->scrub_lock);
279 static void scrub_pause_on(struct btrfs_fs_info *fs_info)
281 atomic_inc(&fs_info->scrubs_paused);
282 wake_up(&fs_info->scrub_pause_wait);
285 static void scrub_pause_off(struct btrfs_fs_info *fs_info)
287 mutex_lock(&fs_info->scrub_lock);
288 __scrub_blocked_if_needed(fs_info);
289 atomic_dec(&fs_info->scrubs_paused);
290 mutex_unlock(&fs_info->scrub_lock);
292 wake_up(&fs_info->scrub_pause_wait);
295 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
297 scrub_pause_on(fs_info);
298 scrub_pause_off(fs_info);
302 * Insert new full stripe lock into full stripe locks tree
304 * Return pointer to existing or newly inserted full_stripe_lock structure if
305 * everything works well.
306 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
308 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
311 static struct full_stripe_lock *insert_full_stripe_lock(
312 struct btrfs_full_stripe_locks_tree *locks_root,
316 struct rb_node *parent = NULL;
317 struct full_stripe_lock *entry;
318 struct full_stripe_lock *ret;
320 lockdep_assert_held(&locks_root->lock);
322 p = &locks_root->root.rb_node;
325 entry = rb_entry(parent, struct full_stripe_lock, node);
326 if (fstripe_logical < entry->logical) {
328 } else if (fstripe_logical > entry->logical) {
339 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
341 return ERR_PTR(-ENOMEM);
342 ret->logical = fstripe_logical;
344 mutex_init(&ret->mutex);
346 rb_link_node(&ret->node, parent, p);
347 rb_insert_color(&ret->node, &locks_root->root);
352 * Search for a full stripe lock of a block group
354 * Return pointer to existing full stripe lock if found
355 * Return NULL if not found
357 static struct full_stripe_lock *search_full_stripe_lock(
358 struct btrfs_full_stripe_locks_tree *locks_root,
361 struct rb_node *node;
362 struct full_stripe_lock *entry;
364 lockdep_assert_held(&locks_root->lock);
366 node = locks_root->root.rb_node;
368 entry = rb_entry(node, struct full_stripe_lock, node);
369 if (fstripe_logical < entry->logical)
370 node = node->rb_left;
371 else if (fstripe_logical > entry->logical)
372 node = node->rb_right;
380 * Helper to get full stripe logical from a normal bytenr.
382 * Caller must ensure @cache is a RAID56 block group.
384 static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr)
389 * Due to chunk item size limit, full stripe length should not be
390 * larger than U32_MAX. Just a sanity check here.
392 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
395 * round_down() can only handle power of 2, while RAID56 full
396 * stripe length can be 64KiB * n, so we need to manually round down.
398 ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) *
399 cache->full_stripe_len + cache->start;
404 * Lock a full stripe to avoid concurrency of recovery and read
406 * It's only used for profiles with parities (RAID5/6), for other profiles it
409 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
410 * So caller must call unlock_full_stripe() at the same context.
412 * Return <0 if encounters error.
414 static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
417 struct btrfs_block_group *bg_cache;
418 struct btrfs_full_stripe_locks_tree *locks_root;
419 struct full_stripe_lock *existing;
424 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
430 /* Profiles not based on parity don't need full stripe lock */
431 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
433 locks_root = &bg_cache->full_stripe_locks_root;
435 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
437 /* Now insert the full stripe lock */
438 mutex_lock(&locks_root->lock);
439 existing = insert_full_stripe_lock(locks_root, fstripe_start);
440 mutex_unlock(&locks_root->lock);
441 if (IS_ERR(existing)) {
442 ret = PTR_ERR(existing);
445 mutex_lock(&existing->mutex);
448 btrfs_put_block_group(bg_cache);
453 * Unlock a full stripe.
455 * NOTE: Caller must ensure it's the same context calling corresponding
456 * lock_full_stripe().
458 * Return 0 if we unlock full stripe without problem.
459 * Return <0 for error
461 static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
464 struct btrfs_block_group *bg_cache;
465 struct btrfs_full_stripe_locks_tree *locks_root;
466 struct full_stripe_lock *fstripe_lock;
471 /* If we didn't acquire full stripe lock, no need to continue */
475 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
480 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
483 locks_root = &bg_cache->full_stripe_locks_root;
484 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
486 mutex_lock(&locks_root->lock);
487 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
488 /* Unpaired unlock_full_stripe() detected */
492 mutex_unlock(&locks_root->lock);
496 if (fstripe_lock->refs == 0) {
498 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
499 fstripe_lock->logical);
501 fstripe_lock->refs--;
504 if (fstripe_lock->refs == 0) {
505 rb_erase(&fstripe_lock->node, &locks_root->root);
508 mutex_unlock(&locks_root->lock);
510 mutex_unlock(&fstripe_lock->mutex);
514 btrfs_put_block_group(bg_cache);
518 static void scrub_free_csums(struct scrub_ctx *sctx)
520 while (!list_empty(&sctx->csum_list)) {
521 struct btrfs_ordered_sum *sum;
522 sum = list_first_entry(&sctx->csum_list,
523 struct btrfs_ordered_sum, list);
524 list_del(&sum->list);
529 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
536 /* this can happen when scrub is cancelled */
537 if (sctx->curr != -1) {
538 struct scrub_bio *sbio = sctx->bios[sctx->curr];
540 for (i = 0; i < sbio->page_count; i++) {
541 WARN_ON(!sbio->pagev[i]->page);
542 scrub_block_put(sbio->pagev[i]->sblock);
547 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
548 struct scrub_bio *sbio = sctx->bios[i];
555 kfree(sctx->wr_curr_bio);
556 scrub_free_csums(sctx);
560 static void scrub_put_ctx(struct scrub_ctx *sctx)
562 if (refcount_dec_and_test(&sctx->refs))
563 scrub_free_ctx(sctx);
566 static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
567 struct btrfs_fs_info *fs_info, int is_dev_replace)
569 struct scrub_ctx *sctx;
572 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
575 refcount_set(&sctx->refs, 1);
576 sctx->is_dev_replace = is_dev_replace;
577 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
579 sctx->fs_info = fs_info;
580 INIT_LIST_HEAD(&sctx->csum_list);
581 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
582 struct scrub_bio *sbio;
584 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
587 sctx->bios[i] = sbio;
591 sbio->page_count = 0;
592 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL,
595 if (i != SCRUB_BIOS_PER_SCTX - 1)
596 sctx->bios[i]->next_free = i + 1;
598 sctx->bios[i]->next_free = -1;
600 sctx->first_free = 0;
601 atomic_set(&sctx->bios_in_flight, 0);
602 atomic_set(&sctx->workers_pending, 0);
603 atomic_set(&sctx->cancel_req, 0);
605 spin_lock_init(&sctx->list_lock);
606 spin_lock_init(&sctx->stat_lock);
607 init_waitqueue_head(&sctx->list_wait);
609 WARN_ON(sctx->wr_curr_bio != NULL);
610 mutex_init(&sctx->wr_lock);
611 sctx->wr_curr_bio = NULL;
612 if (is_dev_replace) {
613 WARN_ON(!fs_info->dev_replace.tgtdev);
614 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
615 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
616 sctx->flush_all_writes = false;
622 scrub_free_ctx(sctx);
623 return ERR_PTR(-ENOMEM);
626 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
634 struct extent_buffer *eb;
635 struct btrfs_inode_item *inode_item;
636 struct scrub_warning *swarn = warn_ctx;
637 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
638 struct inode_fs_paths *ipath = NULL;
639 struct btrfs_root *local_root;
640 struct btrfs_key key;
642 local_root = btrfs_get_fs_root(fs_info, root, true);
643 if (IS_ERR(local_root)) {
644 ret = PTR_ERR(local_root);
649 * this makes the path point to (inum INODE_ITEM ioff)
652 key.type = BTRFS_INODE_ITEM_KEY;
655 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
657 btrfs_put_root(local_root);
658 btrfs_release_path(swarn->path);
662 eb = swarn->path->nodes[0];
663 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
664 struct btrfs_inode_item);
665 isize = btrfs_inode_size(eb, inode_item);
666 nlink = btrfs_inode_nlink(eb, inode_item);
667 btrfs_release_path(swarn->path);
670 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
671 * uses GFP_NOFS in this context, so we keep it consistent but it does
672 * not seem to be strictly necessary.
674 nofs_flag = memalloc_nofs_save();
675 ipath = init_ipath(4096, local_root, swarn->path);
676 memalloc_nofs_restore(nofs_flag);
678 btrfs_put_root(local_root);
679 ret = PTR_ERR(ipath);
683 ret = paths_from_inode(inum, ipath);
689 * we deliberately ignore the bit ipath might have been too small to
690 * hold all of the paths here
692 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
693 btrfs_warn_in_rcu(fs_info,
694 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
695 swarn->errstr, swarn->logical,
696 rcu_str_deref(swarn->dev->name),
699 min(isize - offset, (u64)PAGE_SIZE), nlink,
700 (char *)(unsigned long)ipath->fspath->val[i]);
702 btrfs_put_root(local_root);
707 btrfs_warn_in_rcu(fs_info,
708 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
709 swarn->errstr, swarn->logical,
710 rcu_str_deref(swarn->dev->name),
712 root, inum, offset, ret);
718 static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
720 struct btrfs_device *dev;
721 struct btrfs_fs_info *fs_info;
722 struct btrfs_path *path;
723 struct btrfs_key found_key;
724 struct extent_buffer *eb;
725 struct btrfs_extent_item *ei;
726 struct scrub_warning swarn;
727 unsigned long ptr = 0;
735 WARN_ON(sblock->page_count < 1);
736 dev = sblock->pagev[0]->dev;
737 fs_info = sblock->sctx->fs_info;
739 path = btrfs_alloc_path();
743 swarn.physical = sblock->pagev[0]->physical;
744 swarn.logical = sblock->pagev[0]->logical;
745 swarn.errstr = errstr;
748 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
753 extent_item_pos = swarn.logical - found_key.objectid;
754 swarn.extent_item_size = found_key.offset;
757 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
758 item_size = btrfs_item_size_nr(eb, path->slots[0]);
760 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
762 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
763 item_size, &ref_root,
765 btrfs_warn_in_rcu(fs_info,
766 "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
767 errstr, swarn.logical,
768 rcu_str_deref(dev->name),
770 ref_level ? "node" : "leaf",
771 ret < 0 ? -1 : ref_level,
772 ret < 0 ? -1 : ref_root);
774 btrfs_release_path(path);
776 btrfs_release_path(path);
779 iterate_extent_inodes(fs_info, found_key.objectid,
781 scrub_print_warning_inode, &swarn, false);
785 btrfs_free_path(path);
788 static inline void scrub_get_recover(struct scrub_recover *recover)
790 refcount_inc(&recover->refs);
793 static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
794 struct scrub_recover *recover)
796 if (refcount_dec_and_test(&recover->refs)) {
797 btrfs_bio_counter_dec(fs_info);
798 btrfs_put_bbio(recover->bbio);
804 * scrub_handle_errored_block gets called when either verification of the
805 * pages failed or the bio failed to read, e.g. with EIO. In the latter
806 * case, this function handles all pages in the bio, even though only one
808 * The goal of this function is to repair the errored block by using the
809 * contents of one of the mirrors.
811 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
813 struct scrub_ctx *sctx = sblock_to_check->sctx;
814 struct btrfs_device *dev;
815 struct btrfs_fs_info *fs_info;
817 unsigned int failed_mirror_index;
818 unsigned int is_metadata;
819 unsigned int have_csum;
820 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
821 struct scrub_block *sblock_bad;
826 bool full_stripe_locked;
827 unsigned int nofs_flag;
828 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
829 DEFAULT_RATELIMIT_BURST);
831 BUG_ON(sblock_to_check->page_count < 1);
832 fs_info = sctx->fs_info;
833 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
835 * if we find an error in a super block, we just report it.
836 * They will get written with the next transaction commit
839 spin_lock(&sctx->stat_lock);
840 ++sctx->stat.super_errors;
841 spin_unlock(&sctx->stat_lock);
844 logical = sblock_to_check->pagev[0]->logical;
845 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
846 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
847 is_metadata = !(sblock_to_check->pagev[0]->flags &
848 BTRFS_EXTENT_FLAG_DATA);
849 have_csum = sblock_to_check->pagev[0]->have_csum;
850 dev = sblock_to_check->pagev[0]->dev;
852 if (btrfs_is_zoned(fs_info) && !sctx->is_dev_replace)
853 return btrfs_repair_one_zone(fs_info, logical);
856 * We must use GFP_NOFS because the scrub task might be waiting for a
857 * worker task executing this function and in turn a transaction commit
858 * might be waiting the scrub task to pause (which needs to wait for all
859 * the worker tasks to complete before pausing).
860 * We do allocations in the workers through insert_full_stripe_lock()
861 * and scrub_add_page_to_wr_bio(), which happens down the call chain of
864 nofs_flag = memalloc_nofs_save();
866 * For RAID5/6, race can happen for a different device scrub thread.
867 * For data corruption, Parity and Data threads will both try
868 * to recovery the data.
869 * Race can lead to doubly added csum error, or even unrecoverable
872 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
874 memalloc_nofs_restore(nofs_flag);
875 spin_lock(&sctx->stat_lock);
877 sctx->stat.malloc_errors++;
878 sctx->stat.read_errors++;
879 sctx->stat.uncorrectable_errors++;
880 spin_unlock(&sctx->stat_lock);
885 * read all mirrors one after the other. This includes to
886 * re-read the extent or metadata block that failed (that was
887 * the cause that this fixup code is called) another time,
888 * page by page this time in order to know which pages
889 * caused I/O errors and which ones are good (for all mirrors).
890 * It is the goal to handle the situation when more than one
891 * mirror contains I/O errors, but the errors do not
892 * overlap, i.e. the data can be repaired by selecting the
893 * pages from those mirrors without I/O error on the
894 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
895 * would be that mirror #1 has an I/O error on the first page,
896 * the second page is good, and mirror #2 has an I/O error on
897 * the second page, but the first page is good.
898 * Then the first page of the first mirror can be repaired by
899 * taking the first page of the second mirror, and the
900 * second page of the second mirror can be repaired by
901 * copying the contents of the 2nd page of the 1st mirror.
902 * One more note: if the pages of one mirror contain I/O
903 * errors, the checksum cannot be verified. In order to get
904 * the best data for repairing, the first attempt is to find
905 * a mirror without I/O errors and with a validated checksum.
906 * Only if this is not possible, the pages are picked from
907 * mirrors with I/O errors without considering the checksum.
908 * If the latter is the case, at the end, the checksum of the
909 * repaired area is verified in order to correctly maintain
913 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
914 sizeof(*sblocks_for_recheck), GFP_KERNEL);
915 if (!sblocks_for_recheck) {
916 spin_lock(&sctx->stat_lock);
917 sctx->stat.malloc_errors++;
918 sctx->stat.read_errors++;
919 sctx->stat.uncorrectable_errors++;
920 spin_unlock(&sctx->stat_lock);
921 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
925 /* setup the context, map the logical blocks and alloc the pages */
926 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
928 spin_lock(&sctx->stat_lock);
929 sctx->stat.read_errors++;
930 sctx->stat.uncorrectable_errors++;
931 spin_unlock(&sctx->stat_lock);
932 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
935 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
936 sblock_bad = sblocks_for_recheck + failed_mirror_index;
938 /* build and submit the bios for the failed mirror, check checksums */
939 scrub_recheck_block(fs_info, sblock_bad, 1);
941 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
942 sblock_bad->no_io_error_seen) {
944 * the error disappeared after reading page by page, or
945 * the area was part of a huge bio and other parts of the
946 * bio caused I/O errors, or the block layer merged several
947 * read requests into one and the error is caused by a
948 * different bio (usually one of the two latter cases is
951 spin_lock(&sctx->stat_lock);
952 sctx->stat.unverified_errors++;
953 sblock_to_check->data_corrected = 1;
954 spin_unlock(&sctx->stat_lock);
956 if (sctx->is_dev_replace)
957 scrub_write_block_to_dev_replace(sblock_bad);
961 if (!sblock_bad->no_io_error_seen) {
962 spin_lock(&sctx->stat_lock);
963 sctx->stat.read_errors++;
964 spin_unlock(&sctx->stat_lock);
965 if (__ratelimit(&rs))
966 scrub_print_warning("i/o error", sblock_to_check);
967 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
968 } else if (sblock_bad->checksum_error) {
969 spin_lock(&sctx->stat_lock);
970 sctx->stat.csum_errors++;
971 spin_unlock(&sctx->stat_lock);
972 if (__ratelimit(&rs))
973 scrub_print_warning("checksum error", sblock_to_check);
974 btrfs_dev_stat_inc_and_print(dev,
975 BTRFS_DEV_STAT_CORRUPTION_ERRS);
976 } else if (sblock_bad->header_error) {
977 spin_lock(&sctx->stat_lock);
978 sctx->stat.verify_errors++;
979 spin_unlock(&sctx->stat_lock);
980 if (__ratelimit(&rs))
981 scrub_print_warning("checksum/header error",
983 if (sblock_bad->generation_error)
984 btrfs_dev_stat_inc_and_print(dev,
985 BTRFS_DEV_STAT_GENERATION_ERRS);
987 btrfs_dev_stat_inc_and_print(dev,
988 BTRFS_DEV_STAT_CORRUPTION_ERRS);
991 if (sctx->readonly) {
992 ASSERT(!sctx->is_dev_replace);
997 * now build and submit the bios for the other mirrors, check
999 * First try to pick the mirror which is completely without I/O
1000 * errors and also does not have a checksum error.
1001 * If one is found, and if a checksum is present, the full block
1002 * that is known to contain an error is rewritten. Afterwards
1003 * the block is known to be corrected.
1004 * If a mirror is found which is completely correct, and no
1005 * checksum is present, only those pages are rewritten that had
1006 * an I/O error in the block to be repaired, since it cannot be
1007 * determined, which copy of the other pages is better (and it
1008 * could happen otherwise that a correct page would be
1009 * overwritten by a bad one).
1011 for (mirror_index = 0; ;mirror_index++) {
1012 struct scrub_block *sblock_other;
1014 if (mirror_index == failed_mirror_index)
1017 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1018 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1019 if (mirror_index >= BTRFS_MAX_MIRRORS)
1021 if (!sblocks_for_recheck[mirror_index].page_count)
1024 sblock_other = sblocks_for_recheck + mirror_index;
1026 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1027 int max_allowed = r->bbio->num_stripes -
1028 r->bbio->num_tgtdevs;
1030 if (mirror_index >= max_allowed)
1032 if (!sblocks_for_recheck[1].page_count)
1035 ASSERT(failed_mirror_index == 0);
1036 sblock_other = sblocks_for_recheck + 1;
1037 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1040 /* build and submit the bios, check checksums */
1041 scrub_recheck_block(fs_info, sblock_other, 0);
1043 if (!sblock_other->header_error &&
1044 !sblock_other->checksum_error &&
1045 sblock_other->no_io_error_seen) {
1046 if (sctx->is_dev_replace) {
1047 scrub_write_block_to_dev_replace(sblock_other);
1048 goto corrected_error;
1050 ret = scrub_repair_block_from_good_copy(
1051 sblock_bad, sblock_other);
1053 goto corrected_error;
1058 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1059 goto did_not_correct_error;
1062 * In case of I/O errors in the area that is supposed to be
1063 * repaired, continue by picking good copies of those pages.
1064 * Select the good pages from mirrors to rewrite bad pages from
1065 * the area to fix. Afterwards verify the checksum of the block
1066 * that is supposed to be repaired. This verification step is
1067 * only done for the purpose of statistic counting and for the
1068 * final scrub report, whether errors remain.
1069 * A perfect algorithm could make use of the checksum and try
1070 * all possible combinations of pages from the different mirrors
1071 * until the checksum verification succeeds. For example, when
1072 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1073 * of mirror #2 is readable but the final checksum test fails,
1074 * then the 2nd page of mirror #3 could be tried, whether now
1075 * the final checksum succeeds. But this would be a rare
1076 * exception and is therefore not implemented. At least it is
1077 * avoided that the good copy is overwritten.
1078 * A more useful improvement would be to pick the sectors
1079 * without I/O error based on sector sizes (512 bytes on legacy
1080 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1081 * mirror could be repaired by taking 512 byte of a different
1082 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1083 * area are unreadable.
1086 for (page_num = 0; page_num < sblock_bad->page_count;
1088 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
1089 struct scrub_block *sblock_other = NULL;
1091 /* skip no-io-error page in scrub */
1092 if (!spage_bad->io_error && !sctx->is_dev_replace)
1095 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1097 * In case of dev replace, if raid56 rebuild process
1098 * didn't work out correct data, then copy the content
1099 * in sblock_bad to make sure target device is identical
1100 * to source device, instead of writing garbage data in
1101 * sblock_for_recheck array to target device.
1103 sblock_other = NULL;
1104 } else if (spage_bad->io_error) {
1105 /* try to find no-io-error page in mirrors */
1106 for (mirror_index = 0;
1107 mirror_index < BTRFS_MAX_MIRRORS &&
1108 sblocks_for_recheck[mirror_index].page_count > 0;
1110 if (!sblocks_for_recheck[mirror_index].
1111 pagev[page_num]->io_error) {
1112 sblock_other = sblocks_for_recheck +
1121 if (sctx->is_dev_replace) {
1123 * did not find a mirror to fetch the page
1124 * from. scrub_write_page_to_dev_replace()
1125 * handles this case (page->io_error), by
1126 * filling the block with zeros before
1127 * submitting the write request
1130 sblock_other = sblock_bad;
1132 if (scrub_write_page_to_dev_replace(sblock_other,
1135 &fs_info->dev_replace.num_write_errors);
1138 } else if (sblock_other) {
1139 ret = scrub_repair_page_from_good_copy(sblock_bad,
1143 spage_bad->io_error = 0;
1149 if (success && !sctx->is_dev_replace) {
1150 if (is_metadata || have_csum) {
1152 * need to verify the checksum now that all
1153 * sectors on disk are repaired (the write
1154 * request for data to be repaired is on its way).
1155 * Just be lazy and use scrub_recheck_block()
1156 * which re-reads the data before the checksum
1157 * is verified, but most likely the data comes out
1158 * of the page cache.
1160 scrub_recheck_block(fs_info, sblock_bad, 1);
1161 if (!sblock_bad->header_error &&
1162 !sblock_bad->checksum_error &&
1163 sblock_bad->no_io_error_seen)
1164 goto corrected_error;
1166 goto did_not_correct_error;
1169 spin_lock(&sctx->stat_lock);
1170 sctx->stat.corrected_errors++;
1171 sblock_to_check->data_corrected = 1;
1172 spin_unlock(&sctx->stat_lock);
1173 btrfs_err_rl_in_rcu(fs_info,
1174 "fixed up error at logical %llu on dev %s",
1175 logical, rcu_str_deref(dev->name));
1178 did_not_correct_error:
1179 spin_lock(&sctx->stat_lock);
1180 sctx->stat.uncorrectable_errors++;
1181 spin_unlock(&sctx->stat_lock);
1182 btrfs_err_rl_in_rcu(fs_info,
1183 "unable to fixup (regular) error at logical %llu on dev %s",
1184 logical, rcu_str_deref(dev->name));
1188 if (sblocks_for_recheck) {
1189 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1191 struct scrub_block *sblock = sblocks_for_recheck +
1193 struct scrub_recover *recover;
1196 for (page_index = 0; page_index < sblock->page_count;
1198 sblock->pagev[page_index]->sblock = NULL;
1199 recover = sblock->pagev[page_index]->recover;
1201 scrub_put_recover(fs_info, recover);
1202 sblock->pagev[page_index]->recover =
1205 scrub_page_put(sblock->pagev[page_index]);
1208 kfree(sblocks_for_recheck);
1211 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1212 memalloc_nofs_restore(nofs_flag);
1218 static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
1220 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1222 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1225 return (int)bbio->num_stripes;
1228 static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1231 int nstripes, int mirror,
1237 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1239 for (i = 0; i < nstripes; i++) {
1240 if (raid_map[i] == RAID6_Q_STRIPE ||
1241 raid_map[i] == RAID5_P_STRIPE)
1244 if (logical >= raid_map[i] &&
1245 logical < raid_map[i] + mapped_length)
1250 *stripe_offset = logical - raid_map[i];
1252 /* The other RAID type */
1253 *stripe_index = mirror;
1258 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
1259 struct scrub_block *sblocks_for_recheck)
1261 struct scrub_ctx *sctx = original_sblock->sctx;
1262 struct btrfs_fs_info *fs_info = sctx->fs_info;
1263 u64 length = original_sblock->page_count * PAGE_SIZE;
1264 u64 logical = original_sblock->pagev[0]->logical;
1265 u64 generation = original_sblock->pagev[0]->generation;
1266 u64 flags = original_sblock->pagev[0]->flags;
1267 u64 have_csum = original_sblock->pagev[0]->have_csum;
1268 struct scrub_recover *recover;
1269 struct btrfs_bio *bbio;
1280 * note: the two members refs and outstanding_pages
1281 * are not used (and not set) in the blocks that are used for
1282 * the recheck procedure
1285 while (length > 0) {
1286 sublen = min_t(u64, length, PAGE_SIZE);
1287 mapped_length = sublen;
1291 * with a length of PAGE_SIZE, each returned stripe
1292 * represents one mirror
1294 btrfs_bio_counter_inc_blocked(fs_info);
1295 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
1296 logical, &mapped_length, &bbio);
1297 if (ret || !bbio || mapped_length < sublen) {
1298 btrfs_put_bbio(bbio);
1299 btrfs_bio_counter_dec(fs_info);
1303 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1305 btrfs_put_bbio(bbio);
1306 btrfs_bio_counter_dec(fs_info);
1310 refcount_set(&recover->refs, 1);
1311 recover->bbio = bbio;
1312 recover->map_length = mapped_length;
1314 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
1316 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
1318 for (mirror_index = 0; mirror_index < nmirrors;
1320 struct scrub_block *sblock;
1321 struct scrub_page *spage;
1323 sblock = sblocks_for_recheck + mirror_index;
1324 sblock->sctx = sctx;
1326 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1329 spin_lock(&sctx->stat_lock);
1330 sctx->stat.malloc_errors++;
1331 spin_unlock(&sctx->stat_lock);
1332 scrub_put_recover(fs_info, recover);
1335 scrub_page_get(spage);
1336 sblock->pagev[page_index] = spage;
1337 spage->sblock = sblock;
1338 spage->flags = flags;
1339 spage->generation = generation;
1340 spage->logical = logical;
1341 spage->have_csum = have_csum;
1344 original_sblock->pagev[0]->csum,
1345 sctx->fs_info->csum_size);
1347 scrub_stripe_index_and_offset(logical,
1356 spage->physical = bbio->stripes[stripe_index].physical +
1358 spage->dev = bbio->stripes[stripe_index].dev;
1360 BUG_ON(page_index >= original_sblock->page_count);
1361 spage->physical_for_dev_replace =
1362 original_sblock->pagev[page_index]->
1363 physical_for_dev_replace;
1364 /* for missing devices, dev->bdev is NULL */
1365 spage->mirror_num = mirror_index + 1;
1366 sblock->page_count++;
1367 spage->page = alloc_page(GFP_NOFS);
1371 scrub_get_recover(recover);
1372 spage->recover = recover;
1374 scrub_put_recover(fs_info, recover);
1383 static void scrub_bio_wait_endio(struct bio *bio)
1385 complete(bio->bi_private);
1388 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1390 struct scrub_page *spage)
1392 DECLARE_COMPLETION_ONSTACK(done);
1396 bio->bi_iter.bi_sector = spage->logical >> 9;
1397 bio->bi_private = &done;
1398 bio->bi_end_io = scrub_bio_wait_endio;
1400 mirror_num = spage->sblock->pagev[0]->mirror_num;
1401 ret = raid56_parity_recover(fs_info, bio, spage->recover->bbio,
1402 spage->recover->map_length,
1407 wait_for_completion_io(&done);
1408 return blk_status_to_errno(bio->bi_status);
1411 static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1412 struct scrub_block *sblock)
1414 struct scrub_page *first_page = sblock->pagev[0];
1418 /* All pages in sblock belong to the same stripe on the same device. */
1419 ASSERT(first_page->dev);
1420 if (!first_page->dev->bdev)
1423 bio = btrfs_io_bio_alloc(BIO_MAX_VECS);
1424 bio_set_dev(bio, first_page->dev->bdev);
1426 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1427 struct scrub_page *spage = sblock->pagev[page_num];
1429 WARN_ON(!spage->page);
1430 bio_add_page(bio, spage->page, PAGE_SIZE, 0);
1433 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1440 scrub_recheck_block_checksum(sblock);
1444 for (page_num = 0; page_num < sblock->page_count; page_num++)
1445 sblock->pagev[page_num]->io_error = 1;
1447 sblock->no_io_error_seen = 0;
1451 * this function will check the on disk data for checksum errors, header
1452 * errors and read I/O errors. If any I/O errors happen, the exact pages
1453 * which are errored are marked as being bad. The goal is to enable scrub
1454 * to take those pages that are not errored from all the mirrors so that
1455 * the pages that are errored in the just handled mirror can be repaired.
1457 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1458 struct scrub_block *sblock,
1459 int retry_failed_mirror)
1463 sblock->no_io_error_seen = 1;
1465 /* short cut for raid56 */
1466 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1467 return scrub_recheck_block_on_raid56(fs_info, sblock);
1469 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1471 struct scrub_page *spage = sblock->pagev[page_num];
1473 if (spage->dev->bdev == NULL) {
1474 spage->io_error = 1;
1475 sblock->no_io_error_seen = 0;
1479 WARN_ON(!spage->page);
1480 bio = btrfs_io_bio_alloc(1);
1481 bio_set_dev(bio, spage->dev->bdev);
1483 bio_add_page(bio, spage->page, PAGE_SIZE, 0);
1484 bio->bi_iter.bi_sector = spage->physical >> 9;
1485 bio->bi_opf = REQ_OP_READ;
1487 if (btrfsic_submit_bio_wait(bio)) {
1488 spage->io_error = 1;
1489 sblock->no_io_error_seen = 0;
1495 if (sblock->no_io_error_seen)
1496 scrub_recheck_block_checksum(sblock);
1499 static inline int scrub_check_fsid(u8 fsid[],
1500 struct scrub_page *spage)
1502 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1505 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1509 static void scrub_recheck_block_checksum(struct scrub_block *sblock)
1511 sblock->header_error = 0;
1512 sblock->checksum_error = 0;
1513 sblock->generation_error = 0;
1515 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1516 scrub_checksum_data(sblock);
1518 scrub_checksum_tree_block(sblock);
1521 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1522 struct scrub_block *sblock_good)
1527 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1530 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1540 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1541 struct scrub_block *sblock_good,
1542 int page_num, int force_write)
1544 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
1545 struct scrub_page *spage_good = sblock_good->pagev[page_num];
1546 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
1548 BUG_ON(spage_bad->page == NULL);
1549 BUG_ON(spage_good->page == NULL);
1550 if (force_write || sblock_bad->header_error ||
1551 sblock_bad->checksum_error || spage_bad->io_error) {
1555 if (!spage_bad->dev->bdev) {
1556 btrfs_warn_rl(fs_info,
1557 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
1561 bio = btrfs_io_bio_alloc(1);
1562 bio_set_dev(bio, spage_bad->dev->bdev);
1563 bio->bi_iter.bi_sector = spage_bad->physical >> 9;
1564 bio->bi_opf = REQ_OP_WRITE;
1566 ret = bio_add_page(bio, spage_good->page, PAGE_SIZE, 0);
1567 if (PAGE_SIZE != ret) {
1572 if (btrfsic_submit_bio_wait(bio)) {
1573 btrfs_dev_stat_inc_and_print(spage_bad->dev,
1574 BTRFS_DEV_STAT_WRITE_ERRS);
1575 atomic64_inc(&fs_info->dev_replace.num_write_errors);
1585 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1587 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
1591 * This block is used for the check of the parity on the source device,
1592 * so the data needn't be written into the destination device.
1594 if (sblock->sparity)
1597 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1600 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1602 atomic64_inc(&fs_info->dev_replace.num_write_errors);
1606 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1609 struct scrub_page *spage = sblock->pagev[page_num];
1611 BUG_ON(spage->page == NULL);
1612 if (spage->io_error)
1613 clear_page(page_address(spage->page));
1615 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1618 static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
1623 if (!btrfs_is_zoned(sctx->fs_info))
1626 if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
1629 if (sctx->write_pointer < physical) {
1630 length = physical - sctx->write_pointer;
1632 ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
1633 sctx->write_pointer, length);
1635 sctx->write_pointer = physical;
1640 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1641 struct scrub_page *spage)
1643 struct scrub_bio *sbio;
1646 mutex_lock(&sctx->wr_lock);
1648 if (!sctx->wr_curr_bio) {
1649 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
1651 if (!sctx->wr_curr_bio) {
1652 mutex_unlock(&sctx->wr_lock);
1655 sctx->wr_curr_bio->sctx = sctx;
1656 sctx->wr_curr_bio->page_count = 0;
1658 sbio = sctx->wr_curr_bio;
1659 if (sbio->page_count == 0) {
1662 ret = fill_writer_pointer_gap(sctx,
1663 spage->physical_for_dev_replace);
1665 mutex_unlock(&sctx->wr_lock);
1669 sbio->physical = spage->physical_for_dev_replace;
1670 sbio->logical = spage->logical;
1671 sbio->dev = sctx->wr_tgtdev;
1674 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
1678 bio->bi_private = sbio;
1679 bio->bi_end_io = scrub_wr_bio_end_io;
1680 bio_set_dev(bio, sbio->dev->bdev);
1681 bio->bi_iter.bi_sector = sbio->physical >> 9;
1682 bio->bi_opf = REQ_OP_WRITE;
1684 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1685 spage->physical_for_dev_replace ||
1686 sbio->logical + sbio->page_count * PAGE_SIZE !=
1688 scrub_wr_submit(sctx);
1692 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1693 if (ret != PAGE_SIZE) {
1694 if (sbio->page_count < 1) {
1697 mutex_unlock(&sctx->wr_lock);
1700 scrub_wr_submit(sctx);
1704 sbio->pagev[sbio->page_count] = spage;
1705 scrub_page_get(spage);
1707 if (sbio->page_count == sctx->pages_per_wr_bio)
1708 scrub_wr_submit(sctx);
1709 mutex_unlock(&sctx->wr_lock);
1714 static void scrub_wr_submit(struct scrub_ctx *sctx)
1716 struct scrub_bio *sbio;
1718 if (!sctx->wr_curr_bio)
1721 sbio = sctx->wr_curr_bio;
1722 sctx->wr_curr_bio = NULL;
1723 WARN_ON(!sbio->bio->bi_bdev);
1724 scrub_pending_bio_inc(sctx);
1725 /* process all writes in a single worker thread. Then the block layer
1726 * orders the requests before sending them to the driver which
1727 * doubled the write performance on spinning disks when measured
1729 btrfsic_submit_bio(sbio->bio);
1731 if (btrfs_is_zoned(sctx->fs_info))
1732 sctx->write_pointer = sbio->physical + sbio->page_count * PAGE_SIZE;
1735 static void scrub_wr_bio_end_io(struct bio *bio)
1737 struct scrub_bio *sbio = bio->bi_private;
1738 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
1740 sbio->status = bio->bi_status;
1743 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
1744 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
1747 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1749 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1750 struct scrub_ctx *sctx = sbio->sctx;
1753 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1755 struct btrfs_dev_replace *dev_replace =
1756 &sbio->sctx->fs_info->dev_replace;
1758 for (i = 0; i < sbio->page_count; i++) {
1759 struct scrub_page *spage = sbio->pagev[i];
1761 spage->io_error = 1;
1762 atomic64_inc(&dev_replace->num_write_errors);
1766 for (i = 0; i < sbio->page_count; i++)
1767 scrub_page_put(sbio->pagev[i]);
1771 scrub_pending_bio_dec(sctx);
1774 static int scrub_checksum(struct scrub_block *sblock)
1780 * No need to initialize these stats currently,
1781 * because this function only use return value
1782 * instead of these stats value.
1787 sblock->header_error = 0;
1788 sblock->generation_error = 0;
1789 sblock->checksum_error = 0;
1791 WARN_ON(sblock->page_count < 1);
1792 flags = sblock->pagev[0]->flags;
1794 if (flags & BTRFS_EXTENT_FLAG_DATA)
1795 ret = scrub_checksum_data(sblock);
1796 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1797 ret = scrub_checksum_tree_block(sblock);
1798 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1799 (void)scrub_checksum_super(sblock);
1803 scrub_handle_errored_block(sblock);
1808 static int scrub_checksum_data(struct scrub_block *sblock)
1810 struct scrub_ctx *sctx = sblock->sctx;
1811 struct btrfs_fs_info *fs_info = sctx->fs_info;
1812 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
1813 u8 csum[BTRFS_CSUM_SIZE];
1814 struct scrub_page *spage;
1817 BUG_ON(sblock->page_count < 1);
1818 spage = sblock->pagev[0];
1819 if (!spage->have_csum)
1822 kaddr = page_address(spage->page);
1824 shash->tfm = fs_info->csum_shash;
1825 crypto_shash_init(shash);
1828 * In scrub_pages() and scrub_pages_for_parity() we ensure each spage
1829 * only contains one sector of data.
1831 crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
1833 if (memcmp(csum, spage->csum, fs_info->csum_size))
1834 sblock->checksum_error = 1;
1835 return sblock->checksum_error;
1838 static int scrub_checksum_tree_block(struct scrub_block *sblock)
1840 struct scrub_ctx *sctx = sblock->sctx;
1841 struct btrfs_header *h;
1842 struct btrfs_fs_info *fs_info = sctx->fs_info;
1843 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
1844 u8 calculated_csum[BTRFS_CSUM_SIZE];
1845 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1847 * This is done in sectorsize steps even for metadata as there's a
1848 * constraint for nodesize to be aligned to sectorsize. This will need
1849 * to change so we don't misuse data and metadata units like that.
1851 const u32 sectorsize = sctx->fs_info->sectorsize;
1852 const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits;
1854 struct scrub_page *spage;
1857 BUG_ON(sblock->page_count < 1);
1859 /* Each member in pagev is just one block, not a full page */
1860 ASSERT(sblock->page_count == num_sectors);
1862 spage = sblock->pagev[0];
1863 kaddr = page_address(spage->page);
1864 h = (struct btrfs_header *)kaddr;
1865 memcpy(on_disk_csum, h->csum, sctx->fs_info->csum_size);
1868 * we don't use the getter functions here, as we
1869 * a) don't have an extent buffer and
1870 * b) the page is already kmapped
1872 if (spage->logical != btrfs_stack_header_bytenr(h))
1873 sblock->header_error = 1;
1875 if (spage->generation != btrfs_stack_header_generation(h)) {
1876 sblock->header_error = 1;
1877 sblock->generation_error = 1;
1880 if (!scrub_check_fsid(h->fsid, spage))
1881 sblock->header_error = 1;
1883 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1885 sblock->header_error = 1;
1887 shash->tfm = fs_info->csum_shash;
1888 crypto_shash_init(shash);
1889 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
1890 sectorsize - BTRFS_CSUM_SIZE);
1892 for (i = 1; i < num_sectors; i++) {
1893 kaddr = page_address(sblock->pagev[i]->page);
1894 crypto_shash_update(shash, kaddr, sectorsize);
1897 crypto_shash_final(shash, calculated_csum);
1898 if (memcmp(calculated_csum, on_disk_csum, sctx->fs_info->csum_size))
1899 sblock->checksum_error = 1;
1901 return sblock->header_error || sblock->checksum_error;
1904 static int scrub_checksum_super(struct scrub_block *sblock)
1906 struct btrfs_super_block *s;
1907 struct scrub_ctx *sctx = sblock->sctx;
1908 struct btrfs_fs_info *fs_info = sctx->fs_info;
1909 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
1910 u8 calculated_csum[BTRFS_CSUM_SIZE];
1911 struct scrub_page *spage;
1916 BUG_ON(sblock->page_count < 1);
1917 spage = sblock->pagev[0];
1918 kaddr = page_address(spage->page);
1919 s = (struct btrfs_super_block *)kaddr;
1921 if (spage->logical != btrfs_super_bytenr(s))
1924 if (spage->generation != btrfs_super_generation(s))
1927 if (!scrub_check_fsid(s->fsid, spage))
1930 shash->tfm = fs_info->csum_shash;
1931 crypto_shash_init(shash);
1932 crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE,
1933 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum);
1935 if (memcmp(calculated_csum, s->csum, sctx->fs_info->csum_size))
1938 if (fail_cor + fail_gen) {
1940 * if we find an error in a super block, we just report it.
1941 * They will get written with the next transaction commit
1944 spin_lock(&sctx->stat_lock);
1945 ++sctx->stat.super_errors;
1946 spin_unlock(&sctx->stat_lock);
1948 btrfs_dev_stat_inc_and_print(spage->dev,
1949 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1951 btrfs_dev_stat_inc_and_print(spage->dev,
1952 BTRFS_DEV_STAT_GENERATION_ERRS);
1955 return fail_cor + fail_gen;
1958 static void scrub_block_get(struct scrub_block *sblock)
1960 refcount_inc(&sblock->refs);
1963 static void scrub_block_put(struct scrub_block *sblock)
1965 if (refcount_dec_and_test(&sblock->refs)) {
1968 if (sblock->sparity)
1969 scrub_parity_put(sblock->sparity);
1971 for (i = 0; i < sblock->page_count; i++)
1972 scrub_page_put(sblock->pagev[i]);
1977 static void scrub_page_get(struct scrub_page *spage)
1979 atomic_inc(&spage->refs);
1982 static void scrub_page_put(struct scrub_page *spage)
1984 if (atomic_dec_and_test(&spage->refs)) {
1986 __free_page(spage->page);
1991 static void scrub_submit(struct scrub_ctx *sctx)
1993 struct scrub_bio *sbio;
1995 if (sctx->curr == -1)
1998 sbio = sctx->bios[sctx->curr];
2000 scrub_pending_bio_inc(sctx);
2001 btrfsic_submit_bio(sbio->bio);
2004 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2005 struct scrub_page *spage)
2007 struct scrub_block *sblock = spage->sblock;
2008 struct scrub_bio *sbio;
2013 * grab a fresh bio or wait for one to become available
2015 while (sctx->curr == -1) {
2016 spin_lock(&sctx->list_lock);
2017 sctx->curr = sctx->first_free;
2018 if (sctx->curr != -1) {
2019 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2020 sctx->bios[sctx->curr]->next_free = -1;
2021 sctx->bios[sctx->curr]->page_count = 0;
2022 spin_unlock(&sctx->list_lock);
2024 spin_unlock(&sctx->list_lock);
2025 wait_event(sctx->list_wait, sctx->first_free != -1);
2028 sbio = sctx->bios[sctx->curr];
2029 if (sbio->page_count == 0) {
2032 sbio->physical = spage->physical;
2033 sbio->logical = spage->logical;
2034 sbio->dev = spage->dev;
2037 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
2041 bio->bi_private = sbio;
2042 bio->bi_end_io = scrub_bio_end_io;
2043 bio_set_dev(bio, sbio->dev->bdev);
2044 bio->bi_iter.bi_sector = sbio->physical >> 9;
2045 bio->bi_opf = REQ_OP_READ;
2047 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2049 sbio->logical + sbio->page_count * PAGE_SIZE !=
2051 sbio->dev != spage->dev) {
2056 sbio->pagev[sbio->page_count] = spage;
2057 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2058 if (ret != PAGE_SIZE) {
2059 if (sbio->page_count < 1) {
2068 scrub_block_get(sblock); /* one for the page added to the bio */
2069 atomic_inc(&sblock->outstanding_pages);
2071 if (sbio->page_count == sctx->pages_per_rd_bio)
2077 static void scrub_missing_raid56_end_io(struct bio *bio)
2079 struct scrub_block *sblock = bio->bi_private;
2080 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
2083 sblock->no_io_error_seen = 0;
2087 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2090 static void scrub_missing_raid56_worker(struct btrfs_work *work)
2092 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2093 struct scrub_ctx *sctx = sblock->sctx;
2094 struct btrfs_fs_info *fs_info = sctx->fs_info;
2096 struct btrfs_device *dev;
2098 logical = sblock->pagev[0]->logical;
2099 dev = sblock->pagev[0]->dev;
2101 if (sblock->no_io_error_seen)
2102 scrub_recheck_block_checksum(sblock);
2104 if (!sblock->no_io_error_seen) {
2105 spin_lock(&sctx->stat_lock);
2106 sctx->stat.read_errors++;
2107 spin_unlock(&sctx->stat_lock);
2108 btrfs_err_rl_in_rcu(fs_info,
2109 "IO error rebuilding logical %llu for dev %s",
2110 logical, rcu_str_deref(dev->name));
2111 } else if (sblock->header_error || sblock->checksum_error) {
2112 spin_lock(&sctx->stat_lock);
2113 sctx->stat.uncorrectable_errors++;
2114 spin_unlock(&sctx->stat_lock);
2115 btrfs_err_rl_in_rcu(fs_info,
2116 "failed to rebuild valid logical %llu for dev %s",
2117 logical, rcu_str_deref(dev->name));
2119 scrub_write_block_to_dev_replace(sblock);
2122 if (sctx->is_dev_replace && sctx->flush_all_writes) {
2123 mutex_lock(&sctx->wr_lock);
2124 scrub_wr_submit(sctx);
2125 mutex_unlock(&sctx->wr_lock);
2128 scrub_block_put(sblock);
2129 scrub_pending_bio_dec(sctx);
2132 static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2134 struct scrub_ctx *sctx = sblock->sctx;
2135 struct btrfs_fs_info *fs_info = sctx->fs_info;
2136 u64 length = sblock->page_count * PAGE_SIZE;
2137 u64 logical = sblock->pagev[0]->logical;
2138 struct btrfs_bio *bbio = NULL;
2140 struct btrfs_raid_bio *rbio;
2144 btrfs_bio_counter_inc_blocked(fs_info);
2145 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
2147 if (ret || !bbio || !bbio->raid_map)
2150 if (WARN_ON(!sctx->is_dev_replace ||
2151 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2153 * We shouldn't be scrubbing a missing device. Even for dev
2154 * replace, we should only get here for RAID 5/6. We either
2155 * managed to mount something with no mirrors remaining or
2156 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2161 bio = btrfs_io_bio_alloc(0);
2162 bio->bi_iter.bi_sector = logical >> 9;
2163 bio->bi_private = sblock;
2164 bio->bi_end_io = scrub_missing_raid56_end_io;
2166 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
2170 for (i = 0; i < sblock->page_count; i++) {
2171 struct scrub_page *spage = sblock->pagev[i];
2173 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2176 btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL);
2177 scrub_block_get(sblock);
2178 scrub_pending_bio_inc(sctx);
2179 raid56_submit_missing_rbio(rbio);
2185 btrfs_bio_counter_dec(fs_info);
2186 btrfs_put_bbio(bbio);
2187 spin_lock(&sctx->stat_lock);
2188 sctx->stat.malloc_errors++;
2189 spin_unlock(&sctx->stat_lock);
2192 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
2193 u64 physical, struct btrfs_device *dev, u64 flags,
2194 u64 gen, int mirror_num, u8 *csum,
2195 u64 physical_for_dev_replace)
2197 struct scrub_block *sblock;
2198 const u32 sectorsize = sctx->fs_info->sectorsize;
2201 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2203 spin_lock(&sctx->stat_lock);
2204 sctx->stat.malloc_errors++;
2205 spin_unlock(&sctx->stat_lock);
2209 /* one ref inside this function, plus one for each page added to
2211 refcount_set(&sblock->refs, 1);
2212 sblock->sctx = sctx;
2213 sblock->no_io_error_seen = 1;
2215 for (index = 0; len > 0; index++) {
2216 struct scrub_page *spage;
2218 * Here we will allocate one page for one sector to scrub.
2219 * This is fine if PAGE_SIZE == sectorsize, but will cost
2220 * more memory for PAGE_SIZE > sectorsize case.
2222 u32 l = min(sectorsize, len);
2224 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2227 spin_lock(&sctx->stat_lock);
2228 sctx->stat.malloc_errors++;
2229 spin_unlock(&sctx->stat_lock);
2230 scrub_block_put(sblock);
2233 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2234 scrub_page_get(spage);
2235 sblock->pagev[index] = spage;
2236 spage->sblock = sblock;
2238 spage->flags = flags;
2239 spage->generation = gen;
2240 spage->logical = logical;
2241 spage->physical = physical;
2242 spage->physical_for_dev_replace = physical_for_dev_replace;
2243 spage->mirror_num = mirror_num;
2245 spage->have_csum = 1;
2246 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
2248 spage->have_csum = 0;
2250 sblock->page_count++;
2251 spage->page = alloc_page(GFP_KERNEL);
2257 physical_for_dev_replace += l;
2260 WARN_ON(sblock->page_count == 0);
2261 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
2263 * This case should only be hit for RAID 5/6 device replace. See
2264 * the comment in scrub_missing_raid56_pages() for details.
2266 scrub_missing_raid56_pages(sblock);
2268 for (index = 0; index < sblock->page_count; index++) {
2269 struct scrub_page *spage = sblock->pagev[index];
2272 ret = scrub_add_page_to_rd_bio(sctx, spage);
2274 scrub_block_put(sblock);
2279 if (flags & BTRFS_EXTENT_FLAG_SUPER)
2283 /* last one frees, either here or in bio completion for last page */
2284 scrub_block_put(sblock);
2288 static void scrub_bio_end_io(struct bio *bio)
2290 struct scrub_bio *sbio = bio->bi_private;
2291 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
2293 sbio->status = bio->bi_status;
2296 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
2299 static void scrub_bio_end_io_worker(struct btrfs_work *work)
2301 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2302 struct scrub_ctx *sctx = sbio->sctx;
2305 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
2307 for (i = 0; i < sbio->page_count; i++) {
2308 struct scrub_page *spage = sbio->pagev[i];
2310 spage->io_error = 1;
2311 spage->sblock->no_io_error_seen = 0;
2315 /* now complete the scrub_block items that have all pages completed */
2316 for (i = 0; i < sbio->page_count; i++) {
2317 struct scrub_page *spage = sbio->pagev[i];
2318 struct scrub_block *sblock = spage->sblock;
2320 if (atomic_dec_and_test(&sblock->outstanding_pages))
2321 scrub_block_complete(sblock);
2322 scrub_block_put(sblock);
2327 spin_lock(&sctx->list_lock);
2328 sbio->next_free = sctx->first_free;
2329 sctx->first_free = sbio->index;
2330 spin_unlock(&sctx->list_lock);
2332 if (sctx->is_dev_replace && sctx->flush_all_writes) {
2333 mutex_lock(&sctx->wr_lock);
2334 scrub_wr_submit(sctx);
2335 mutex_unlock(&sctx->wr_lock);
2338 scrub_pending_bio_dec(sctx);
2341 static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2342 unsigned long *bitmap,
2347 u32 sectorsize_bits = sparity->sctx->fs_info->sectorsize_bits;
2349 if (len >= sparity->stripe_len) {
2350 bitmap_set(bitmap, 0, sparity->nsectors);
2354 start -= sparity->logic_start;
2355 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2356 offset = offset >> sectorsize_bits;
2357 nsectors = len >> sectorsize_bits;
2359 if (offset + nsectors <= sparity->nsectors) {
2360 bitmap_set(bitmap, offset, nsectors);
2364 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2365 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2368 static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2371 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2374 static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2377 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2380 static void scrub_block_complete(struct scrub_block *sblock)
2384 if (!sblock->no_io_error_seen) {
2386 scrub_handle_errored_block(sblock);
2389 * if has checksum error, write via repair mechanism in
2390 * dev replace case, otherwise write here in dev replace
2393 corrupted = scrub_checksum(sblock);
2394 if (!corrupted && sblock->sctx->is_dev_replace)
2395 scrub_write_block_to_dev_replace(sblock);
2398 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2399 u64 start = sblock->pagev[0]->logical;
2400 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2403 ASSERT(end - start <= U32_MAX);
2404 scrub_parity_mark_sectors_error(sblock->sparity,
2405 start, end - start);
2409 static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum)
2411 sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits;
2412 list_del(&sum->list);
2417 * Find the desired csum for range [logical, logical + sectorsize), and store
2418 * the csum into @csum.
2420 * The search source is sctx->csum_list, which is a pre-populated list
2421 * storing bytenr ordered csum ranges. We're reponsible to cleanup any range
2422 * that is before @logical.
2424 * Return 0 if there is no csum for the range.
2425 * Return 1 if there is csum for the range and copied to @csum.
2427 static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
2431 while (!list_empty(&sctx->csum_list)) {
2432 struct btrfs_ordered_sum *sum = NULL;
2433 unsigned long index;
2434 unsigned long num_sectors;
2436 sum = list_first_entry(&sctx->csum_list,
2437 struct btrfs_ordered_sum, list);
2438 /* The current csum range is beyond our range, no csum found */
2439 if (sum->bytenr > logical)
2443 * The current sum is before our bytenr, since scrub is always
2444 * done in bytenr order, the csum will never be used anymore,
2445 * clean it up so that later calls won't bother with the range,
2446 * and continue search the next range.
2448 if (sum->bytenr + sum->len <= logical) {
2449 drop_csum_range(sctx, sum);
2453 /* Now the csum range covers our bytenr, copy the csum */
2455 index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits;
2456 num_sectors = sum->len >> sctx->fs_info->sectorsize_bits;
2458 memcpy(csum, sum->sums + index * sctx->fs_info->csum_size,
2459 sctx->fs_info->csum_size);
2461 /* Cleanup the range if we're at the end of the csum range */
2462 if (index == num_sectors - 1)
2463 drop_csum_range(sctx, sum);
2471 /* scrub extent tries to collect up to 64 kB for each bio */
2472 static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
2473 u64 logical, u32 len,
2474 u64 physical, struct btrfs_device *dev, u64 flags,
2475 u64 gen, int mirror_num, u64 physical_for_dev_replace)
2478 u8 csum[BTRFS_CSUM_SIZE];
2481 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2482 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2483 blocksize = map->stripe_len;
2485 blocksize = sctx->fs_info->sectorsize;
2486 spin_lock(&sctx->stat_lock);
2487 sctx->stat.data_extents_scrubbed++;
2488 sctx->stat.data_bytes_scrubbed += len;
2489 spin_unlock(&sctx->stat_lock);
2490 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2491 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2492 blocksize = map->stripe_len;
2494 blocksize = sctx->fs_info->nodesize;
2495 spin_lock(&sctx->stat_lock);
2496 sctx->stat.tree_extents_scrubbed++;
2497 sctx->stat.tree_bytes_scrubbed += len;
2498 spin_unlock(&sctx->stat_lock);
2500 blocksize = sctx->fs_info->sectorsize;
2505 u32 l = min(len, blocksize);
2508 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2509 /* push csums to sbio */
2510 have_csum = scrub_find_csum(sctx, logical, csum);
2512 ++sctx->stat.no_csum;
2514 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
2515 mirror_num, have_csum ? csum : NULL,
2516 physical_for_dev_replace);
2522 physical_for_dev_replace += l;
2527 static int scrub_pages_for_parity(struct scrub_parity *sparity,
2528 u64 logical, u32 len,
2529 u64 physical, struct btrfs_device *dev,
2530 u64 flags, u64 gen, int mirror_num, u8 *csum)
2532 struct scrub_ctx *sctx = sparity->sctx;
2533 struct scrub_block *sblock;
2534 const u32 sectorsize = sctx->fs_info->sectorsize;
2537 ASSERT(IS_ALIGNED(len, sectorsize));
2539 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2541 spin_lock(&sctx->stat_lock);
2542 sctx->stat.malloc_errors++;
2543 spin_unlock(&sctx->stat_lock);
2547 /* one ref inside this function, plus one for each page added to
2549 refcount_set(&sblock->refs, 1);
2550 sblock->sctx = sctx;
2551 sblock->no_io_error_seen = 1;
2552 sblock->sparity = sparity;
2553 scrub_parity_get(sparity);
2555 for (index = 0; len > 0; index++) {
2556 struct scrub_page *spage;
2558 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2561 spin_lock(&sctx->stat_lock);
2562 sctx->stat.malloc_errors++;
2563 spin_unlock(&sctx->stat_lock);
2564 scrub_block_put(sblock);
2567 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2568 /* For scrub block */
2569 scrub_page_get(spage);
2570 sblock->pagev[index] = spage;
2571 /* For scrub parity */
2572 scrub_page_get(spage);
2573 list_add_tail(&spage->list, &sparity->spages);
2574 spage->sblock = sblock;
2576 spage->flags = flags;
2577 spage->generation = gen;
2578 spage->logical = logical;
2579 spage->physical = physical;
2580 spage->mirror_num = mirror_num;
2582 spage->have_csum = 1;
2583 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
2585 spage->have_csum = 0;
2587 sblock->page_count++;
2588 spage->page = alloc_page(GFP_KERNEL);
2593 /* Iterate over the stripe range in sectorsize steps */
2595 logical += sectorsize;
2596 physical += sectorsize;
2599 WARN_ON(sblock->page_count == 0);
2600 for (index = 0; index < sblock->page_count; index++) {
2601 struct scrub_page *spage = sblock->pagev[index];
2604 ret = scrub_add_page_to_rd_bio(sctx, spage);
2606 scrub_block_put(sblock);
2611 /* last one frees, either here or in bio completion for last page */
2612 scrub_block_put(sblock);
2616 static int scrub_extent_for_parity(struct scrub_parity *sparity,
2617 u64 logical, u32 len,
2618 u64 physical, struct btrfs_device *dev,
2619 u64 flags, u64 gen, int mirror_num)
2621 struct scrub_ctx *sctx = sparity->sctx;
2623 u8 csum[BTRFS_CSUM_SIZE];
2626 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
2627 scrub_parity_mark_sectors_error(sparity, logical, len);
2631 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2632 blocksize = sparity->stripe_len;
2633 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2634 blocksize = sparity->stripe_len;
2636 blocksize = sctx->fs_info->sectorsize;
2641 u32 l = min(len, blocksize);
2644 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2645 /* push csums to sbio */
2646 have_csum = scrub_find_csum(sctx, logical, csum);
2650 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2651 flags, gen, mirror_num,
2652 have_csum ? csum : NULL);
2664 * Given a physical address, this will calculate it's
2665 * logical offset. if this is a parity stripe, it will return
2666 * the most left data stripe's logical offset.
2668 * return 0 if it is a data stripe, 1 means parity stripe.
2670 static int get_raid56_logic_offset(u64 physical, int num,
2671 struct map_lookup *map, u64 *offset,
2680 const int data_stripes = nr_data_stripes(map);
2682 last_offset = (physical - map->stripes[num].physical) * data_stripes;
2684 *stripe_start = last_offset;
2686 *offset = last_offset;
2687 for (i = 0; i < data_stripes; i++) {
2688 *offset = last_offset + i * map->stripe_len;
2690 stripe_nr = div64_u64(*offset, map->stripe_len);
2691 stripe_nr = div_u64(stripe_nr, data_stripes);
2693 /* Work out the disk rotation on this stripe-set */
2694 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
2695 /* calculate which stripe this data locates */
2697 stripe_index = rot % map->num_stripes;
2698 if (stripe_index == num)
2700 if (stripe_index < num)
2703 *offset = last_offset + j * map->stripe_len;
2707 static void scrub_free_parity(struct scrub_parity *sparity)
2709 struct scrub_ctx *sctx = sparity->sctx;
2710 struct scrub_page *curr, *next;
2713 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2715 spin_lock(&sctx->stat_lock);
2716 sctx->stat.read_errors += nbits;
2717 sctx->stat.uncorrectable_errors += nbits;
2718 spin_unlock(&sctx->stat_lock);
2721 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2722 list_del_init(&curr->list);
2723 scrub_page_put(curr);
2729 static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2731 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2733 struct scrub_ctx *sctx = sparity->sctx;
2735 scrub_free_parity(sparity);
2736 scrub_pending_bio_dec(sctx);
2739 static void scrub_parity_bio_endio(struct bio *bio)
2741 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2742 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
2745 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2750 btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL,
2752 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
2755 static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2757 struct scrub_ctx *sctx = sparity->sctx;
2758 struct btrfs_fs_info *fs_info = sctx->fs_info;
2760 struct btrfs_raid_bio *rbio;
2761 struct btrfs_bio *bbio = NULL;
2765 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2769 length = sparity->logic_end - sparity->logic_start;
2771 btrfs_bio_counter_inc_blocked(fs_info);
2772 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
2774 if (ret || !bbio || !bbio->raid_map)
2777 bio = btrfs_io_bio_alloc(0);
2778 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2779 bio->bi_private = sparity;
2780 bio->bi_end_io = scrub_parity_bio_endio;
2782 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
2783 length, sparity->scrub_dev,
2789 scrub_pending_bio_inc(sctx);
2790 raid56_parity_submit_scrub_rbio(rbio);
2796 btrfs_bio_counter_dec(fs_info);
2797 btrfs_put_bbio(bbio);
2798 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2800 spin_lock(&sctx->stat_lock);
2801 sctx->stat.malloc_errors++;
2802 spin_unlock(&sctx->stat_lock);
2804 scrub_free_parity(sparity);
2807 static inline int scrub_calc_parity_bitmap_len(int nsectors)
2809 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
2812 static void scrub_parity_get(struct scrub_parity *sparity)
2814 refcount_inc(&sparity->refs);
2817 static void scrub_parity_put(struct scrub_parity *sparity)
2819 if (!refcount_dec_and_test(&sparity->refs))
2822 scrub_parity_check_and_repair(sparity);
2825 static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2826 struct map_lookup *map,
2827 struct btrfs_device *sdev,
2828 struct btrfs_path *path,
2832 struct btrfs_fs_info *fs_info = sctx->fs_info;
2833 struct btrfs_root *root = fs_info->extent_root;
2834 struct btrfs_root *csum_root = fs_info->csum_root;
2835 struct btrfs_extent_item *extent;
2836 struct btrfs_bio *bbio = NULL;
2840 struct extent_buffer *l;
2841 struct btrfs_key key;
2844 u64 extent_physical;
2845 /* Check the comment in scrub_stripe() for why u32 is enough here */
2848 struct btrfs_device *extent_dev;
2849 struct scrub_parity *sparity;
2852 int extent_mirror_num;
2855 ASSERT(map->stripe_len <= U32_MAX);
2856 nsectors = map->stripe_len >> fs_info->sectorsize_bits;
2857 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2858 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2861 spin_lock(&sctx->stat_lock);
2862 sctx->stat.malloc_errors++;
2863 spin_unlock(&sctx->stat_lock);
2867 ASSERT(map->stripe_len <= U32_MAX);
2868 sparity->stripe_len = map->stripe_len;
2869 sparity->nsectors = nsectors;
2870 sparity->sctx = sctx;
2871 sparity->scrub_dev = sdev;
2872 sparity->logic_start = logic_start;
2873 sparity->logic_end = logic_end;
2874 refcount_set(&sparity->refs, 1);
2875 INIT_LIST_HEAD(&sparity->spages);
2876 sparity->dbitmap = sparity->bitmap;
2877 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2880 while (logic_start < logic_end) {
2881 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2882 key.type = BTRFS_METADATA_ITEM_KEY;
2884 key.type = BTRFS_EXTENT_ITEM_KEY;
2885 key.objectid = logic_start;
2886 key.offset = (u64)-1;
2888 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2893 ret = btrfs_previous_extent_item(root, path, 0);
2897 btrfs_release_path(path);
2898 ret = btrfs_search_slot(NULL, root, &key,
2910 slot = path->slots[0];
2911 if (slot >= btrfs_header_nritems(l)) {
2912 ret = btrfs_next_leaf(root, path);
2921 btrfs_item_key_to_cpu(l, &key, slot);
2923 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2924 key.type != BTRFS_METADATA_ITEM_KEY)
2927 if (key.type == BTRFS_METADATA_ITEM_KEY)
2928 bytes = fs_info->nodesize;
2932 if (key.objectid + bytes <= logic_start)
2935 if (key.objectid >= logic_end) {
2940 while (key.objectid >= logic_start + map->stripe_len)
2941 logic_start += map->stripe_len;
2943 extent = btrfs_item_ptr(l, slot,
2944 struct btrfs_extent_item);
2945 flags = btrfs_extent_flags(l, extent);
2946 generation = btrfs_extent_generation(l, extent);
2948 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2949 (key.objectid < logic_start ||
2950 key.objectid + bytes >
2951 logic_start + map->stripe_len)) {
2953 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2954 key.objectid, logic_start);
2955 spin_lock(&sctx->stat_lock);
2956 sctx->stat.uncorrectable_errors++;
2957 spin_unlock(&sctx->stat_lock);
2961 extent_logical = key.objectid;
2962 ASSERT(bytes <= U32_MAX);
2965 if (extent_logical < logic_start) {
2966 extent_len -= logic_start - extent_logical;
2967 extent_logical = logic_start;
2970 if (extent_logical + extent_len >
2971 logic_start + map->stripe_len)
2972 extent_len = logic_start + map->stripe_len -
2975 scrub_parity_mark_sectors_data(sparity, extent_logical,
2978 mapped_length = extent_len;
2980 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
2981 extent_logical, &mapped_length, &bbio,
2984 if (!bbio || mapped_length < extent_len)
2988 btrfs_put_bbio(bbio);
2991 extent_physical = bbio->stripes[0].physical;
2992 extent_mirror_num = bbio->mirror_num;
2993 extent_dev = bbio->stripes[0].dev;
2994 btrfs_put_bbio(bbio);
2996 ret = btrfs_lookup_csums_range(csum_root,
2998 extent_logical + extent_len - 1,
2999 &sctx->csum_list, 1);
3003 ret = scrub_extent_for_parity(sparity, extent_logical,
3010 scrub_free_csums(sctx);
3015 if (extent_logical + extent_len <
3016 key.objectid + bytes) {
3017 logic_start += map->stripe_len;
3019 if (logic_start >= logic_end) {
3024 if (logic_start < key.objectid + bytes) {
3033 btrfs_release_path(path);
3038 logic_start += map->stripe_len;
3042 ASSERT(logic_end - logic_start <= U32_MAX);
3043 scrub_parity_mark_sectors_error(sparity, logic_start,
3044 logic_end - logic_start);
3046 scrub_parity_put(sparity);
3048 mutex_lock(&sctx->wr_lock);
3049 scrub_wr_submit(sctx);
3050 mutex_unlock(&sctx->wr_lock);
3052 btrfs_release_path(path);
3053 return ret < 0 ? ret : 0;
3056 static void sync_replace_for_zoned(struct scrub_ctx *sctx)
3058 if (!btrfs_is_zoned(sctx->fs_info))
3061 sctx->flush_all_writes = true;
3063 mutex_lock(&sctx->wr_lock);
3064 scrub_wr_submit(sctx);
3065 mutex_unlock(&sctx->wr_lock);
3067 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3070 static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
3071 u64 physical, u64 physical_end)
3073 struct btrfs_fs_info *fs_info = sctx->fs_info;
3076 if (!btrfs_is_zoned(fs_info))
3079 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3081 mutex_lock(&sctx->wr_lock);
3082 if (sctx->write_pointer < physical_end) {
3083 ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
3085 sctx->write_pointer);
3088 "zoned: failed to recover write pointer");
3090 mutex_unlock(&sctx->wr_lock);
3091 btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
3096 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
3097 struct map_lookup *map,
3098 struct btrfs_device *scrub_dev,
3099 int num, u64 base, u64 length,
3100 struct btrfs_block_group *cache)
3102 struct btrfs_path *path, *ppath;
3103 struct btrfs_fs_info *fs_info = sctx->fs_info;
3104 struct btrfs_root *root = fs_info->extent_root;
3105 struct btrfs_root *csum_root = fs_info->csum_root;
3106 struct btrfs_extent_item *extent;
3107 struct blk_plug plug;
3112 struct extent_buffer *l;
3119 struct reada_control *reada1;
3120 struct reada_control *reada2;
3121 struct btrfs_key key;
3122 struct btrfs_key key_end;
3123 u64 increment = map->stripe_len;
3126 u64 extent_physical;
3128 * Unlike chunk length, extent length should never go beyond
3129 * BTRFS_MAX_EXTENT_SIZE, thus u32 is enough here.
3134 struct btrfs_device *extent_dev;
3135 int extent_mirror_num;
3138 physical = map->stripes[num].physical;
3140 nstripes = div64_u64(length, map->stripe_len);
3141 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3142 offset = map->stripe_len * num;
3143 increment = map->stripe_len * map->num_stripes;
3145 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3146 int factor = map->num_stripes / map->sub_stripes;
3147 offset = map->stripe_len * (num / map->sub_stripes);
3148 increment = map->stripe_len * factor;
3149 mirror_num = num % map->sub_stripes + 1;
3150 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
3151 increment = map->stripe_len;
3152 mirror_num = num % map->num_stripes + 1;
3153 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3154 increment = map->stripe_len;
3155 mirror_num = num % map->num_stripes + 1;
3156 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3157 get_raid56_logic_offset(physical, num, map, &offset, NULL);
3158 increment = map->stripe_len * nr_data_stripes(map);
3161 increment = map->stripe_len;
3165 path = btrfs_alloc_path();
3169 ppath = btrfs_alloc_path();
3171 btrfs_free_path(path);
3176 * work on commit root. The related disk blocks are static as
3177 * long as COW is applied. This means, it is save to rewrite
3178 * them to repair disk errors without any race conditions
3180 path->search_commit_root = 1;
3181 path->skip_locking = 1;
3183 ppath->search_commit_root = 1;
3184 ppath->skip_locking = 1;
3186 * trigger the readahead for extent tree csum tree and wait for
3187 * completion. During readahead, the scrub is officially paused
3188 * to not hold off transaction commits
3190 logical = base + offset;
3191 physical_end = physical + nstripes * map->stripe_len;
3192 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3193 get_raid56_logic_offset(physical_end, num,
3194 map, &logic_end, NULL);
3197 logic_end = logical + increment * nstripes;
3199 wait_event(sctx->list_wait,
3200 atomic_read(&sctx->bios_in_flight) == 0);
3201 scrub_blocked_if_needed(fs_info);
3203 /* FIXME it might be better to start readahead at commit root */
3204 key.objectid = logical;
3205 key.type = BTRFS_EXTENT_ITEM_KEY;
3206 key.offset = (u64)0;
3207 key_end.objectid = logic_end;
3208 key_end.type = BTRFS_METADATA_ITEM_KEY;
3209 key_end.offset = (u64)-1;
3210 reada1 = btrfs_reada_add(root, &key, &key_end);
3212 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3213 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3214 key.type = BTRFS_EXTENT_CSUM_KEY;
3215 key.offset = logical;
3216 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3217 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3218 key_end.offset = logic_end;
3219 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
3224 if (!IS_ERR(reada1))
3225 btrfs_reada_wait(reada1);
3226 if (!IS_ERR_OR_NULL(reada2))
3227 btrfs_reada_wait(reada2);
3231 * collect all data csums for the stripe to avoid seeking during
3232 * the scrub. This might currently (crc32) end up to be about 1MB
3234 blk_start_plug(&plug);
3236 if (sctx->is_dev_replace &&
3237 btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
3238 mutex_lock(&sctx->wr_lock);
3239 sctx->write_pointer = physical;
3240 mutex_unlock(&sctx->wr_lock);
3241 sctx->flush_all_writes = true;
3245 * now find all extents for each stripe and scrub them
3248 while (physical < physical_end) {
3252 if (atomic_read(&fs_info->scrub_cancel_req) ||
3253 atomic_read(&sctx->cancel_req)) {
3258 * check to see if we have to pause
3260 if (atomic_read(&fs_info->scrub_pause_req)) {
3261 /* push queued extents */
3262 sctx->flush_all_writes = true;
3264 mutex_lock(&sctx->wr_lock);
3265 scrub_wr_submit(sctx);
3266 mutex_unlock(&sctx->wr_lock);
3267 wait_event(sctx->list_wait,
3268 atomic_read(&sctx->bios_in_flight) == 0);
3269 sctx->flush_all_writes = false;
3270 scrub_blocked_if_needed(fs_info);
3273 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3274 ret = get_raid56_logic_offset(physical, num, map,
3279 /* it is parity strip */
3280 stripe_logical += base;
3281 stripe_end = stripe_logical + increment;
3282 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3283 ppath, stripe_logical,
3291 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3292 key.type = BTRFS_METADATA_ITEM_KEY;
3294 key.type = BTRFS_EXTENT_ITEM_KEY;
3295 key.objectid = logical;
3296 key.offset = (u64)-1;
3298 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3303 ret = btrfs_previous_extent_item(root, path, 0);
3307 /* there's no smaller item, so stick with the
3309 btrfs_release_path(path);
3310 ret = btrfs_search_slot(NULL, root, &key,
3322 slot = path->slots[0];
3323 if (slot >= btrfs_header_nritems(l)) {
3324 ret = btrfs_next_leaf(root, path);
3333 btrfs_item_key_to_cpu(l, &key, slot);
3335 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3336 key.type != BTRFS_METADATA_ITEM_KEY)
3339 if (key.type == BTRFS_METADATA_ITEM_KEY)
3340 bytes = fs_info->nodesize;
3344 if (key.objectid + bytes <= logical)
3347 if (key.objectid >= logical + map->stripe_len) {
3348 /* out of this device extent */
3349 if (key.objectid >= logic_end)
3355 * If our block group was removed in the meanwhile, just
3356 * stop scrubbing since there is no point in continuing.
3357 * Continuing would prevent reusing its device extents
3358 * for new block groups for a long time.
3360 spin_lock(&cache->lock);
3361 if (cache->removed) {
3362 spin_unlock(&cache->lock);
3366 spin_unlock(&cache->lock);
3368 extent = btrfs_item_ptr(l, slot,
3369 struct btrfs_extent_item);
3370 flags = btrfs_extent_flags(l, extent);
3371 generation = btrfs_extent_generation(l, extent);
3373 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3374 (key.objectid < logical ||
3375 key.objectid + bytes >
3376 logical + map->stripe_len)) {
3378 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3379 key.objectid, logical);
3380 spin_lock(&sctx->stat_lock);
3381 sctx->stat.uncorrectable_errors++;
3382 spin_unlock(&sctx->stat_lock);
3387 extent_logical = key.objectid;
3388 ASSERT(bytes <= U32_MAX);
3392 * trim extent to this stripe
3394 if (extent_logical < logical) {
3395 extent_len -= logical - extent_logical;
3396 extent_logical = logical;
3398 if (extent_logical + extent_len >
3399 logical + map->stripe_len) {
3400 extent_len = logical + map->stripe_len -
3404 extent_physical = extent_logical - logical + physical;
3405 extent_dev = scrub_dev;
3406 extent_mirror_num = mirror_num;
3407 if (sctx->is_dev_replace)
3408 scrub_remap_extent(fs_info, extent_logical,
3409 extent_len, &extent_physical,
3411 &extent_mirror_num);
3413 if (flags & BTRFS_EXTENT_FLAG_DATA) {
3414 ret = btrfs_lookup_csums_range(csum_root,
3416 extent_logical + extent_len - 1,
3417 &sctx->csum_list, 1);
3422 ret = scrub_extent(sctx, map, extent_logical, extent_len,
3423 extent_physical, extent_dev, flags,
3424 generation, extent_mirror_num,
3425 extent_logical - logical + physical);
3427 scrub_free_csums(sctx);
3432 if (sctx->is_dev_replace)
3433 sync_replace_for_zoned(sctx);
3435 if (extent_logical + extent_len <
3436 key.objectid + bytes) {
3437 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3439 * loop until we find next data stripe
3440 * or we have finished all stripes.
3443 physical += map->stripe_len;
3444 ret = get_raid56_logic_offset(physical,
3449 if (ret && physical < physical_end) {
3450 stripe_logical += base;
3451 stripe_end = stripe_logical +
3453 ret = scrub_raid56_parity(sctx,
3454 map, scrub_dev, ppath,
3462 physical += map->stripe_len;
3463 logical += increment;
3465 if (logical < key.objectid + bytes) {
3470 if (physical >= physical_end) {
3478 btrfs_release_path(path);
3480 logical += increment;
3481 physical += map->stripe_len;
3482 spin_lock(&sctx->stat_lock);
3484 sctx->stat.last_physical = map->stripes[num].physical +
3487 sctx->stat.last_physical = physical;
3488 spin_unlock(&sctx->stat_lock);
3493 /* push queued extents */
3495 mutex_lock(&sctx->wr_lock);
3496 scrub_wr_submit(sctx);
3497 mutex_unlock(&sctx->wr_lock);
3499 blk_finish_plug(&plug);
3500 btrfs_free_path(path);
3501 btrfs_free_path(ppath);
3503 if (sctx->is_dev_replace && ret >= 0) {
3506 ret2 = sync_write_pointer_for_zoned(sctx, base + offset,
3507 map->stripes[num].physical,
3513 return ret < 0 ? ret : 0;
3516 static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
3517 struct btrfs_device *scrub_dev,
3518 u64 chunk_offset, u64 length,
3520 struct btrfs_block_group *cache)
3522 struct btrfs_fs_info *fs_info = sctx->fs_info;
3523 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
3524 struct map_lookup *map;
3525 struct extent_map *em;
3529 read_lock(&map_tree->lock);
3530 em = lookup_extent_mapping(map_tree, chunk_offset, 1);
3531 read_unlock(&map_tree->lock);
3535 * Might have been an unused block group deleted by the cleaner
3536 * kthread or relocation.
3538 spin_lock(&cache->lock);
3539 if (!cache->removed)
3541 spin_unlock(&cache->lock);
3546 map = em->map_lookup;
3547 if (em->start != chunk_offset)
3550 if (em->len < length)
3553 for (i = 0; i < map->num_stripes; ++i) {
3554 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
3555 map->stripes[i].physical == dev_offset) {
3556 ret = scrub_stripe(sctx, map, scrub_dev, i,
3557 chunk_offset, length, cache);
3563 free_extent_map(em);
3568 static int finish_extent_writes_for_zoned(struct btrfs_root *root,
3569 struct btrfs_block_group *cache)
3571 struct btrfs_fs_info *fs_info = cache->fs_info;
3572 struct btrfs_trans_handle *trans;
3574 if (!btrfs_is_zoned(fs_info))
3577 btrfs_wait_block_group_reservations(cache);
3578 btrfs_wait_nocow_writers(cache);
3579 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
3581 trans = btrfs_join_transaction(root);
3583 return PTR_ERR(trans);
3584 return btrfs_commit_transaction(trans);
3587 static noinline_for_stack
3588 int scrub_enumerate_chunks(struct scrub_ctx *sctx,
3589 struct btrfs_device *scrub_dev, u64 start, u64 end)
3591 struct btrfs_dev_extent *dev_extent = NULL;
3592 struct btrfs_path *path;
3593 struct btrfs_fs_info *fs_info = sctx->fs_info;
3594 struct btrfs_root *root = fs_info->dev_root;
3600 struct extent_buffer *l;
3601 struct btrfs_key key;
3602 struct btrfs_key found_key;
3603 struct btrfs_block_group *cache;
3604 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
3606 path = btrfs_alloc_path();
3610 path->reada = READA_FORWARD;
3611 path->search_commit_root = 1;
3612 path->skip_locking = 1;
3614 key.objectid = scrub_dev->devid;
3616 key.type = BTRFS_DEV_EXTENT_KEY;
3619 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3623 if (path->slots[0] >=
3624 btrfs_header_nritems(path->nodes[0])) {
3625 ret = btrfs_next_leaf(root, path);
3638 slot = path->slots[0];
3640 btrfs_item_key_to_cpu(l, &found_key, slot);
3642 if (found_key.objectid != scrub_dev->devid)
3645 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
3648 if (found_key.offset >= end)
3651 if (found_key.offset < key.offset)
3654 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3655 length = btrfs_dev_extent_length(l, dev_extent);
3657 if (found_key.offset + length <= start)
3660 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3663 * get a reference on the corresponding block group to prevent
3664 * the chunk from going away while we scrub it
3666 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3668 /* some chunks are removed but not committed to disk yet,
3669 * continue scrubbing */
3673 if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
3674 spin_lock(&cache->lock);
3675 if (!cache->to_copy) {
3676 spin_unlock(&cache->lock);
3677 btrfs_put_block_group(cache);
3680 spin_unlock(&cache->lock);
3684 * Make sure that while we are scrubbing the corresponding block
3685 * group doesn't get its logical address and its device extents
3686 * reused for another block group, which can possibly be of a
3687 * different type and different profile. We do this to prevent
3688 * false error detections and crashes due to bogus attempts to
3691 spin_lock(&cache->lock);
3692 if (cache->removed) {
3693 spin_unlock(&cache->lock);
3694 btrfs_put_block_group(cache);
3697 btrfs_freeze_block_group(cache);
3698 spin_unlock(&cache->lock);
3701 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3702 * to avoid deadlock caused by:
3703 * btrfs_inc_block_group_ro()
3704 * -> btrfs_wait_for_commit()
3705 * -> btrfs_commit_transaction()
3706 * -> btrfs_scrub_pause()
3708 scrub_pause_on(fs_info);
3711 * Don't do chunk preallocation for scrub.
3713 * This is especially important for SYSTEM bgs, or we can hit
3714 * -EFBIG from btrfs_finish_chunk_alloc() like:
3715 * 1. The only SYSTEM bg is marked RO.
3716 * Since SYSTEM bg is small, that's pretty common.
3717 * 2. New SYSTEM bg will be allocated
3718 * Due to regular version will allocate new chunk.
3719 * 3. New SYSTEM bg is empty and will get cleaned up
3720 * Before cleanup really happens, it's marked RO again.
3721 * 4. Empty SYSTEM bg get scrubbed
3724 * This can easily boost the amount of SYSTEM chunks if cleaner
3725 * thread can't be triggered fast enough, and use up all space
3726 * of btrfs_super_block::sys_chunk_array
3728 * While for dev replace, we need to try our best to mark block
3729 * group RO, to prevent race between:
3730 * - Write duplication
3731 * Contains latest data
3733 * Contains data from commit tree
3735 * If target block group is not marked RO, nocow writes can
3736 * be overwritten by scrub copy, causing data corruption.
3737 * So for dev-replace, it's not allowed to continue if a block
3740 ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
3741 if (!ret && sctx->is_dev_replace) {
3742 ret = finish_extent_writes_for_zoned(root, cache);
3744 btrfs_dec_block_group_ro(cache);
3745 scrub_pause_off(fs_info);
3746 btrfs_put_block_group(cache);
3753 } else if (ret == -ENOSPC && !sctx->is_dev_replace) {
3755 * btrfs_inc_block_group_ro return -ENOSPC when it
3756 * failed in creating new chunk for metadata.
3757 * It is not a problem for scrub, because
3758 * metadata are always cowed, and our scrub paused
3759 * commit_transactions.
3762 } else if (ret == -ETXTBSY) {
3764 "skipping scrub of block group %llu due to active swapfile",
3766 scrub_pause_off(fs_info);
3771 "failed setting block group ro: %d", ret);
3772 btrfs_unfreeze_block_group(cache);
3773 btrfs_put_block_group(cache);
3774 scrub_pause_off(fs_info);
3779 * Now the target block is marked RO, wait for nocow writes to
3780 * finish before dev-replace.
3781 * COW is fine, as COW never overwrites extents in commit tree.
3783 if (sctx->is_dev_replace) {
3784 btrfs_wait_nocow_writers(cache);
3785 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
3789 scrub_pause_off(fs_info);
3790 down_write(&dev_replace->rwsem);
3791 dev_replace->cursor_right = found_key.offset + length;
3792 dev_replace->cursor_left = found_key.offset;
3793 dev_replace->item_needs_writeback = 1;
3794 up_write(&dev_replace->rwsem);
3796 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
3797 found_key.offset, cache);
3800 * flush, submit all pending read and write bios, afterwards
3802 * Note that in the dev replace case, a read request causes
3803 * write requests that are submitted in the read completion
3804 * worker. Therefore in the current situation, it is required
3805 * that all write requests are flushed, so that all read and
3806 * write requests are really completed when bios_in_flight
3809 sctx->flush_all_writes = true;
3811 mutex_lock(&sctx->wr_lock);
3812 scrub_wr_submit(sctx);
3813 mutex_unlock(&sctx->wr_lock);
3815 wait_event(sctx->list_wait,
3816 atomic_read(&sctx->bios_in_flight) == 0);
3818 scrub_pause_on(fs_info);
3821 * must be called before we decrease @scrub_paused.
3822 * make sure we don't block transaction commit while
3823 * we are waiting pending workers finished.
3825 wait_event(sctx->list_wait,
3826 atomic_read(&sctx->workers_pending) == 0);
3827 sctx->flush_all_writes = false;
3829 scrub_pause_off(fs_info);
3831 if (sctx->is_dev_replace &&
3832 !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
3833 cache, found_key.offset))
3836 down_write(&dev_replace->rwsem);
3837 dev_replace->cursor_left = dev_replace->cursor_right;
3838 dev_replace->item_needs_writeback = 1;
3839 up_write(&dev_replace->rwsem);
3842 btrfs_dec_block_group_ro(cache);
3845 * We might have prevented the cleaner kthread from deleting
3846 * this block group if it was already unused because we raced
3847 * and set it to RO mode first. So add it back to the unused
3848 * list, otherwise it might not ever be deleted unless a manual
3849 * balance is triggered or it becomes used and unused again.
3851 spin_lock(&cache->lock);
3852 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3854 spin_unlock(&cache->lock);
3855 if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
3856 btrfs_discard_queue_work(&fs_info->discard_ctl,
3859 btrfs_mark_bg_unused(cache);
3861 spin_unlock(&cache->lock);
3864 btrfs_unfreeze_block_group(cache);
3865 btrfs_put_block_group(cache);
3868 if (sctx->is_dev_replace &&
3869 atomic64_read(&dev_replace->num_write_errors) > 0) {
3873 if (sctx->stat.malloc_errors > 0) {
3878 key.offset = found_key.offset + length;
3879 btrfs_release_path(path);
3882 btrfs_free_path(path);
3887 static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3888 struct btrfs_device *scrub_dev)
3894 struct btrfs_fs_info *fs_info = sctx->fs_info;
3896 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3899 /* Seed devices of a new filesystem has their own generation. */
3900 if (scrub_dev->fs_devices != fs_info->fs_devices)
3901 gen = scrub_dev->generation;
3903 gen = fs_info->last_trans_committed;
3905 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3906 bytenr = btrfs_sb_offset(i);
3907 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3908 scrub_dev->commit_total_bytes)
3910 if (!btrfs_check_super_location(scrub_dev, bytenr))
3913 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
3914 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
3919 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3924 static void scrub_workers_put(struct btrfs_fs_info *fs_info)
3926 if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
3927 &fs_info->scrub_lock)) {
3928 struct btrfs_workqueue *scrub_workers = NULL;
3929 struct btrfs_workqueue *scrub_wr_comp = NULL;
3930 struct btrfs_workqueue *scrub_parity = NULL;
3932 scrub_workers = fs_info->scrub_workers;
3933 scrub_wr_comp = fs_info->scrub_wr_completion_workers;
3934 scrub_parity = fs_info->scrub_parity_workers;
3936 fs_info->scrub_workers = NULL;
3937 fs_info->scrub_wr_completion_workers = NULL;
3938 fs_info->scrub_parity_workers = NULL;
3939 mutex_unlock(&fs_info->scrub_lock);
3941 btrfs_destroy_workqueue(scrub_workers);
3942 btrfs_destroy_workqueue(scrub_wr_comp);
3943 btrfs_destroy_workqueue(scrub_parity);
3948 * get a reference count on fs_info->scrub_workers. start worker if necessary
3950 static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3953 struct btrfs_workqueue *scrub_workers = NULL;
3954 struct btrfs_workqueue *scrub_wr_comp = NULL;
3955 struct btrfs_workqueue *scrub_parity = NULL;
3956 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
3957 int max_active = fs_info->thread_pool_size;
3960 if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
3963 scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", flags,
3964 is_dev_replace ? 1 : max_active, 4);
3966 goto fail_scrub_workers;
3968 scrub_wr_comp = btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
3971 goto fail_scrub_wr_completion_workers;
3973 scrub_parity = btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
3976 goto fail_scrub_parity_workers;
3978 mutex_lock(&fs_info->scrub_lock);
3979 if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
3980 ASSERT(fs_info->scrub_workers == NULL &&
3981 fs_info->scrub_wr_completion_workers == NULL &&
3982 fs_info->scrub_parity_workers == NULL);
3983 fs_info->scrub_workers = scrub_workers;
3984 fs_info->scrub_wr_completion_workers = scrub_wr_comp;
3985 fs_info->scrub_parity_workers = scrub_parity;
3986 refcount_set(&fs_info->scrub_workers_refcnt, 1);
3987 mutex_unlock(&fs_info->scrub_lock);
3990 /* Other thread raced in and created the workers for us */
3991 refcount_inc(&fs_info->scrub_workers_refcnt);
3992 mutex_unlock(&fs_info->scrub_lock);
3995 btrfs_destroy_workqueue(scrub_parity);
3996 fail_scrub_parity_workers:
3997 btrfs_destroy_workqueue(scrub_wr_comp);
3998 fail_scrub_wr_completion_workers:
3999 btrfs_destroy_workqueue(scrub_workers);
4004 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4005 u64 end, struct btrfs_scrub_progress *progress,
4006 int readonly, int is_dev_replace)
4008 struct scrub_ctx *sctx;
4010 struct btrfs_device *dev;
4011 unsigned int nofs_flag;
4013 if (btrfs_fs_closing(fs_info))
4016 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
4018 * in this case scrub is unable to calculate the checksum
4019 * the way scrub is implemented. Do not handle this
4020 * situation at all because it won't ever happen.
4023 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
4029 if (fs_info->nodesize >
4030 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
4031 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
4033 * would exhaust the array bounds of pagev member in
4034 * struct scrub_block
4037 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
4039 SCRUB_MAX_PAGES_PER_BLOCK,
4040 fs_info->sectorsize,
4041 SCRUB_MAX_PAGES_PER_BLOCK);
4045 /* Allocate outside of device_list_mutex */
4046 sctx = scrub_setup_ctx(fs_info, is_dev_replace);
4048 return PTR_ERR(sctx);
4050 ret = scrub_workers_get(fs_info, is_dev_replace);
4054 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4055 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
4056 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4058 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4063 if (!is_dev_replace && !readonly &&
4064 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
4065 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4066 btrfs_err_in_rcu(fs_info,
4067 "scrub on devid %llu: filesystem on %s is not writable",
4068 devid, rcu_str_deref(dev->name));
4073 mutex_lock(&fs_info->scrub_lock);
4074 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
4075 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
4076 mutex_unlock(&fs_info->scrub_lock);
4077 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4082 down_read(&fs_info->dev_replace.rwsem);
4083 if (dev->scrub_ctx ||
4085 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
4086 up_read(&fs_info->dev_replace.rwsem);
4087 mutex_unlock(&fs_info->scrub_lock);
4088 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4092 up_read(&fs_info->dev_replace.rwsem);
4094 sctx->readonly = readonly;
4095 dev->scrub_ctx = sctx;
4096 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4099 * checking @scrub_pause_req here, we can avoid
4100 * race between committing transaction and scrubbing.
4102 __scrub_blocked_if_needed(fs_info);
4103 atomic_inc(&fs_info->scrubs_running);
4104 mutex_unlock(&fs_info->scrub_lock);
4107 * In order to avoid deadlock with reclaim when there is a transaction
4108 * trying to pause scrub, make sure we use GFP_NOFS for all the
4109 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
4110 * invoked by our callees. The pausing request is done when the
4111 * transaction commit starts, and it blocks the transaction until scrub
4112 * is paused (done at specific points at scrub_stripe() or right above
4113 * before incrementing fs_info->scrubs_running).
4115 nofs_flag = memalloc_nofs_save();
4116 if (!is_dev_replace) {
4117 btrfs_info(fs_info, "scrub: started on devid %llu", devid);
4119 * by holding device list mutex, we can
4120 * kick off writing super in log tree sync.
4122 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4123 ret = scrub_supers(sctx, dev);
4124 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4128 ret = scrub_enumerate_chunks(sctx, dev, start, end);
4129 memalloc_nofs_restore(nofs_flag);
4131 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
4132 atomic_dec(&fs_info->scrubs_running);
4133 wake_up(&fs_info->scrub_pause_wait);
4135 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
4138 memcpy(progress, &sctx->stat, sizeof(*progress));
4140 if (!is_dev_replace)
4141 btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
4142 ret ? "not finished" : "finished", devid, ret);
4144 mutex_lock(&fs_info->scrub_lock);
4145 dev->scrub_ctx = NULL;
4146 mutex_unlock(&fs_info->scrub_lock);
4148 scrub_workers_put(fs_info);
4149 scrub_put_ctx(sctx);
4153 scrub_workers_put(fs_info);
4155 scrub_free_ctx(sctx);
4160 void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
4162 mutex_lock(&fs_info->scrub_lock);
4163 atomic_inc(&fs_info->scrub_pause_req);
4164 while (atomic_read(&fs_info->scrubs_paused) !=
4165 atomic_read(&fs_info->scrubs_running)) {
4166 mutex_unlock(&fs_info->scrub_lock);
4167 wait_event(fs_info->scrub_pause_wait,
4168 atomic_read(&fs_info->scrubs_paused) ==
4169 atomic_read(&fs_info->scrubs_running));
4170 mutex_lock(&fs_info->scrub_lock);
4172 mutex_unlock(&fs_info->scrub_lock);
4175 void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
4177 atomic_dec(&fs_info->scrub_pause_req);
4178 wake_up(&fs_info->scrub_pause_wait);
4181 int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
4183 mutex_lock(&fs_info->scrub_lock);
4184 if (!atomic_read(&fs_info->scrubs_running)) {
4185 mutex_unlock(&fs_info->scrub_lock);
4189 atomic_inc(&fs_info->scrub_cancel_req);
4190 while (atomic_read(&fs_info->scrubs_running)) {
4191 mutex_unlock(&fs_info->scrub_lock);
4192 wait_event(fs_info->scrub_pause_wait,
4193 atomic_read(&fs_info->scrubs_running) == 0);
4194 mutex_lock(&fs_info->scrub_lock);
4196 atomic_dec(&fs_info->scrub_cancel_req);
4197 mutex_unlock(&fs_info->scrub_lock);
4202 int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
4204 struct btrfs_fs_info *fs_info = dev->fs_info;
4205 struct scrub_ctx *sctx;
4207 mutex_lock(&fs_info->scrub_lock);
4208 sctx = dev->scrub_ctx;
4210 mutex_unlock(&fs_info->scrub_lock);
4213 atomic_inc(&sctx->cancel_req);
4214 while (dev->scrub_ctx) {
4215 mutex_unlock(&fs_info->scrub_lock);
4216 wait_event(fs_info->scrub_pause_wait,
4217 dev->scrub_ctx == NULL);
4218 mutex_lock(&fs_info->scrub_lock);
4220 mutex_unlock(&fs_info->scrub_lock);
4225 int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
4226 struct btrfs_scrub_progress *progress)
4228 struct btrfs_device *dev;
4229 struct scrub_ctx *sctx = NULL;
4231 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4232 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
4234 sctx = dev->scrub_ctx;
4236 memcpy(progress, &sctx->stat, sizeof(*progress));
4237 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4239 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
4242 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4243 u64 extent_logical, u32 extent_len,
4244 u64 *extent_physical,
4245 struct btrfs_device **extent_dev,
4246 int *extent_mirror_num)
4249 struct btrfs_bio *bbio = NULL;
4252 mapped_length = extent_len;
4253 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
4254 &mapped_length, &bbio, 0);
4255 if (ret || !bbio || mapped_length < extent_len ||
4256 !bbio->stripes[0].dev->bdev) {
4257 btrfs_put_bbio(bbio);
4261 *extent_physical = bbio->stripes[0].physical;
4262 *extent_mirror_num = bbio->mirror_num;
4263 *extent_dev = bbio->stripes[0].dev;
4264 btrfs_put_bbio(bbio);