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>
12 #include "ordered-data.h"
13 #include "transaction.h"
15 #include "extent_io.h"
16 #include "dev-replace.h"
17 #include "check-integrity.h"
18 #include "rcu-string.h"
22 * This is only the first step towards a full-features scrub. It reads all
23 * extent and super block and verifies the checksums. In case a bad checksum
24 * is found or the extent cannot be read, good data will be written back if
27 * Future enhancements:
28 * - In case an unrepairable extent is encountered, track which files are
29 * affected and report them
30 * - track and record media errors, throw out bad devices
31 * - add a mode to also read unallocated space
38 * the following three values only influence the performance.
39 * The last one configures the number of parallel and outstanding I/O
40 * operations. The first two values configure an upper limit for the number
41 * of (dynamically allocated) pages that are added to a bio.
43 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
44 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
45 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
48 * the following value times PAGE_SIZE needs to be large enough to match the
49 * largest node/leaf/sector size that shall be supported.
50 * Values larger than BTRFS_STRIPE_LEN are not supported.
52 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
54 struct scrub_recover
{
56 struct btrfs_bio
*bbio
;
61 struct scrub_block
*sblock
;
63 struct btrfs_device
*dev
;
64 struct list_head list
;
65 u64 flags
; /* extent flags */
69 u64 physical_for_dev_replace
;
72 unsigned int mirror_num
:8;
73 unsigned int have_csum
:1;
74 unsigned int io_error
:1;
76 u8 csum
[BTRFS_CSUM_SIZE
];
78 struct scrub_recover
*recover
;
83 struct scrub_ctx
*sctx
;
84 struct btrfs_device
*dev
;
89 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
90 struct scrub_page
*pagev
[SCRUB_PAGES_PER_WR_BIO
];
92 struct scrub_page
*pagev
[SCRUB_PAGES_PER_RD_BIO
];
96 struct btrfs_work work
;
100 struct scrub_page
*pagev
[SCRUB_MAX_PAGES_PER_BLOCK
];
102 atomic_t outstanding_pages
;
103 refcount_t refs
; /* free mem on transition to zero */
104 struct scrub_ctx
*sctx
;
105 struct scrub_parity
*sparity
;
107 unsigned int header_error
:1;
108 unsigned int checksum_error
:1;
109 unsigned int no_io_error_seen
:1;
110 unsigned int generation_error
:1; /* also sets header_error */
112 /* The following is for the data used to check parity */
113 /* It is for the data with checksum */
114 unsigned int data_corrected
:1;
116 struct btrfs_work work
;
119 /* Used for the chunks with parity stripe such RAID5/6 */
120 struct scrub_parity
{
121 struct scrub_ctx
*sctx
;
123 struct btrfs_device
*scrub_dev
;
135 struct list_head spages
;
137 /* Work of parity check and repair */
138 struct btrfs_work work
;
140 /* Mark the parity blocks which have data */
141 unsigned long *dbitmap
;
144 * Mark the parity blocks which have data, but errors happen when
145 * read data or check data
147 unsigned long *ebitmap
;
149 unsigned long bitmap
[0];
153 struct scrub_bio
*bios
[SCRUB_BIOS_PER_SCTX
];
154 struct btrfs_fs_info
*fs_info
;
157 atomic_t bios_in_flight
;
158 atomic_t workers_pending
;
159 spinlock_t list_lock
;
160 wait_queue_head_t list_wait
;
162 struct list_head csum_list
;
165 int pages_per_rd_bio
;
169 struct scrub_bio
*wr_curr_bio
;
170 struct mutex wr_lock
;
171 int pages_per_wr_bio
; /* <= SCRUB_PAGES_PER_WR_BIO */
172 struct btrfs_device
*wr_tgtdev
;
173 bool flush_all_writes
;
178 struct btrfs_scrub_progress stat
;
179 spinlock_t stat_lock
;
182 * Use a ref counter to avoid use-after-free issues. Scrub workers
183 * decrement bios_in_flight and workers_pending and then do a wakeup
184 * on the list_wait wait queue. We must ensure the main scrub task
185 * doesn't free the scrub context before or while the workers are
186 * doing the wakeup() call.
191 struct scrub_warning
{
192 struct btrfs_path
*path
;
193 u64 extent_item_size
;
197 struct btrfs_device
*dev
;
200 struct full_stripe_lock
{
207 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
);
208 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
);
209 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
);
210 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
211 struct scrub_block
*sblocks_for_recheck
);
212 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
213 struct scrub_block
*sblock
,
214 int retry_failed_mirror
);
215 static void scrub_recheck_block_checksum(struct scrub_block
*sblock
);
216 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
217 struct scrub_block
*sblock_good
);
218 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
219 struct scrub_block
*sblock_good
,
220 int page_num
, int force_write
);
221 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
);
222 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
224 static int scrub_checksum_data(struct scrub_block
*sblock
);
225 static int scrub_checksum_tree_block(struct scrub_block
*sblock
);
226 static int scrub_checksum_super(struct scrub_block
*sblock
);
227 static void scrub_block_get(struct scrub_block
*sblock
);
228 static void scrub_block_put(struct scrub_block
*sblock
);
229 static void scrub_page_get(struct scrub_page
*spage
);
230 static void scrub_page_put(struct scrub_page
*spage
);
231 static void scrub_parity_get(struct scrub_parity
*sparity
);
232 static void scrub_parity_put(struct scrub_parity
*sparity
);
233 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
234 struct scrub_page
*spage
);
235 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
236 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
237 u64 gen
, int mirror_num
, u8
*csum
, int force
,
238 u64 physical_for_dev_replace
);
239 static void scrub_bio_end_io(struct bio
*bio
);
240 static void scrub_bio_end_io_worker(struct btrfs_work
*work
);
241 static void scrub_block_complete(struct scrub_block
*sblock
);
242 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
243 u64 extent_logical
, u64 extent_len
,
244 u64
*extent_physical
,
245 struct btrfs_device
**extent_dev
,
246 int *extent_mirror_num
);
247 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
248 struct scrub_page
*spage
);
249 static void scrub_wr_submit(struct scrub_ctx
*sctx
);
250 static void scrub_wr_bio_end_io(struct bio
*bio
);
251 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
);
252 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
253 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
254 static void scrub_put_ctx(struct scrub_ctx
*sctx
);
256 static inline int scrub_is_page_on_raid56(struct scrub_page
*page
)
258 return page
->recover
&&
259 (page
->recover
->bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
);
262 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
)
264 refcount_inc(&sctx
->refs
);
265 atomic_inc(&sctx
->bios_in_flight
);
268 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
)
270 atomic_dec(&sctx
->bios_in_flight
);
271 wake_up(&sctx
->list_wait
);
275 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
277 while (atomic_read(&fs_info
->scrub_pause_req
)) {
278 mutex_unlock(&fs_info
->scrub_lock
);
279 wait_event(fs_info
->scrub_pause_wait
,
280 atomic_read(&fs_info
->scrub_pause_req
) == 0);
281 mutex_lock(&fs_info
->scrub_lock
);
285 static void scrub_pause_on(struct btrfs_fs_info
*fs_info
)
287 atomic_inc(&fs_info
->scrubs_paused
);
288 wake_up(&fs_info
->scrub_pause_wait
);
291 static void scrub_pause_off(struct btrfs_fs_info
*fs_info
)
293 mutex_lock(&fs_info
->scrub_lock
);
294 __scrub_blocked_if_needed(fs_info
);
295 atomic_dec(&fs_info
->scrubs_paused
);
296 mutex_unlock(&fs_info
->scrub_lock
);
298 wake_up(&fs_info
->scrub_pause_wait
);
301 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
303 scrub_pause_on(fs_info
);
304 scrub_pause_off(fs_info
);
308 * Insert new full stripe lock into full stripe locks tree
310 * Return pointer to existing or newly inserted full_stripe_lock structure if
311 * everything works well.
312 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
314 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
317 static struct full_stripe_lock
*insert_full_stripe_lock(
318 struct btrfs_full_stripe_locks_tree
*locks_root
,
322 struct rb_node
*parent
= NULL
;
323 struct full_stripe_lock
*entry
;
324 struct full_stripe_lock
*ret
;
325 unsigned int nofs_flag
;
327 lockdep_assert_held(&locks_root
->lock
);
329 p
= &locks_root
->root
.rb_node
;
332 entry
= rb_entry(parent
, struct full_stripe_lock
, node
);
333 if (fstripe_logical
< entry
->logical
) {
335 } else if (fstripe_logical
> entry
->logical
) {
346 * We must use GFP_NOFS because the scrub task might be waiting for a
347 * worker task executing this function and in turn a transaction commit
348 * might be waiting the scrub task to pause (which needs to wait for all
349 * the worker tasks to complete before pausing).
351 nofs_flag
= memalloc_nofs_save();
352 ret
= kmalloc(sizeof(*ret
), GFP_KERNEL
);
353 memalloc_nofs_restore(nofs_flag
);
355 return ERR_PTR(-ENOMEM
);
356 ret
->logical
= fstripe_logical
;
358 mutex_init(&ret
->mutex
);
360 rb_link_node(&ret
->node
, parent
, p
);
361 rb_insert_color(&ret
->node
, &locks_root
->root
);
366 * Search for a full stripe lock of a block group
368 * Return pointer to existing full stripe lock if found
369 * Return NULL if not found
371 static struct full_stripe_lock
*search_full_stripe_lock(
372 struct btrfs_full_stripe_locks_tree
*locks_root
,
375 struct rb_node
*node
;
376 struct full_stripe_lock
*entry
;
378 lockdep_assert_held(&locks_root
->lock
);
380 node
= locks_root
->root
.rb_node
;
382 entry
= rb_entry(node
, struct full_stripe_lock
, node
);
383 if (fstripe_logical
< entry
->logical
)
384 node
= node
->rb_left
;
385 else if (fstripe_logical
> entry
->logical
)
386 node
= node
->rb_right
;
394 * Helper to get full stripe logical from a normal bytenr.
396 * Caller must ensure @cache is a RAID56 block group.
398 static u64
get_full_stripe_logical(struct btrfs_block_group_cache
*cache
,
404 * Due to chunk item size limit, full stripe length should not be
405 * larger than U32_MAX. Just a sanity check here.
407 WARN_ON_ONCE(cache
->full_stripe_len
>= U32_MAX
);
410 * round_down() can only handle power of 2, while RAID56 full
411 * stripe length can be 64KiB * n, so we need to manually round down.
413 ret
= div64_u64(bytenr
- cache
->key
.objectid
, cache
->full_stripe_len
) *
414 cache
->full_stripe_len
+ cache
->key
.objectid
;
419 * Lock a full stripe to avoid concurrency of recovery and read
421 * It's only used for profiles with parities (RAID5/6), for other profiles it
424 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
425 * So caller must call unlock_full_stripe() at the same context.
427 * Return <0 if encounters error.
429 static int lock_full_stripe(struct btrfs_fs_info
*fs_info
, u64 bytenr
,
432 struct btrfs_block_group_cache
*bg_cache
;
433 struct btrfs_full_stripe_locks_tree
*locks_root
;
434 struct full_stripe_lock
*existing
;
439 bg_cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
445 /* Profiles not based on parity don't need full stripe lock */
446 if (!(bg_cache
->flags
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
448 locks_root
= &bg_cache
->full_stripe_locks_root
;
450 fstripe_start
= get_full_stripe_logical(bg_cache
, bytenr
);
452 /* Now insert the full stripe lock */
453 mutex_lock(&locks_root
->lock
);
454 existing
= insert_full_stripe_lock(locks_root
, fstripe_start
);
455 mutex_unlock(&locks_root
->lock
);
456 if (IS_ERR(existing
)) {
457 ret
= PTR_ERR(existing
);
460 mutex_lock(&existing
->mutex
);
463 btrfs_put_block_group(bg_cache
);
468 * Unlock a full stripe.
470 * NOTE: Caller must ensure it's the same context calling corresponding
471 * lock_full_stripe().
473 * Return 0 if we unlock full stripe without problem.
474 * Return <0 for error
476 static int unlock_full_stripe(struct btrfs_fs_info
*fs_info
, u64 bytenr
,
479 struct btrfs_block_group_cache
*bg_cache
;
480 struct btrfs_full_stripe_locks_tree
*locks_root
;
481 struct full_stripe_lock
*fstripe_lock
;
486 /* If we didn't acquire full stripe lock, no need to continue */
490 bg_cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
495 if (!(bg_cache
->flags
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
498 locks_root
= &bg_cache
->full_stripe_locks_root
;
499 fstripe_start
= get_full_stripe_logical(bg_cache
, bytenr
);
501 mutex_lock(&locks_root
->lock
);
502 fstripe_lock
= search_full_stripe_lock(locks_root
, fstripe_start
);
503 /* Unpaired unlock_full_stripe() detected */
507 mutex_unlock(&locks_root
->lock
);
511 if (fstripe_lock
->refs
== 0) {
513 btrfs_warn(fs_info
, "full stripe lock at %llu refcount underflow",
514 fstripe_lock
->logical
);
516 fstripe_lock
->refs
--;
519 if (fstripe_lock
->refs
== 0) {
520 rb_erase(&fstripe_lock
->node
, &locks_root
->root
);
523 mutex_unlock(&locks_root
->lock
);
525 mutex_unlock(&fstripe_lock
->mutex
);
529 btrfs_put_block_group(bg_cache
);
533 static void scrub_free_csums(struct scrub_ctx
*sctx
)
535 while (!list_empty(&sctx
->csum_list
)) {
536 struct btrfs_ordered_sum
*sum
;
537 sum
= list_first_entry(&sctx
->csum_list
,
538 struct btrfs_ordered_sum
, list
);
539 list_del(&sum
->list
);
544 static noinline_for_stack
void scrub_free_ctx(struct scrub_ctx
*sctx
)
551 /* this can happen when scrub is cancelled */
552 if (sctx
->curr
!= -1) {
553 struct scrub_bio
*sbio
= sctx
->bios
[sctx
->curr
];
555 for (i
= 0; i
< sbio
->page_count
; i
++) {
556 WARN_ON(!sbio
->pagev
[i
]->page
);
557 scrub_block_put(sbio
->pagev
[i
]->sblock
);
562 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
563 struct scrub_bio
*sbio
= sctx
->bios
[i
];
570 kfree(sctx
->wr_curr_bio
);
571 scrub_free_csums(sctx
);
575 static void scrub_put_ctx(struct scrub_ctx
*sctx
)
577 if (refcount_dec_and_test(&sctx
->refs
))
578 scrub_free_ctx(sctx
);
581 static noinline_for_stack
struct scrub_ctx
*scrub_setup_ctx(
582 struct btrfs_fs_info
*fs_info
, int is_dev_replace
)
584 struct scrub_ctx
*sctx
;
587 sctx
= kzalloc(sizeof(*sctx
), GFP_KERNEL
);
590 refcount_set(&sctx
->refs
, 1);
591 sctx
->is_dev_replace
= is_dev_replace
;
592 sctx
->pages_per_rd_bio
= SCRUB_PAGES_PER_RD_BIO
;
594 sctx
->fs_info
= fs_info
;
595 INIT_LIST_HEAD(&sctx
->csum_list
);
596 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
597 struct scrub_bio
*sbio
;
599 sbio
= kzalloc(sizeof(*sbio
), GFP_KERNEL
);
602 sctx
->bios
[i
] = sbio
;
606 sbio
->page_count
= 0;
607 btrfs_init_work(&sbio
->work
, btrfs_scrub_helper
,
608 scrub_bio_end_io_worker
, NULL
, NULL
);
610 if (i
!= SCRUB_BIOS_PER_SCTX
- 1)
611 sctx
->bios
[i
]->next_free
= i
+ 1;
613 sctx
->bios
[i
]->next_free
= -1;
615 sctx
->first_free
= 0;
616 atomic_set(&sctx
->bios_in_flight
, 0);
617 atomic_set(&sctx
->workers_pending
, 0);
618 atomic_set(&sctx
->cancel_req
, 0);
619 sctx
->csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
621 spin_lock_init(&sctx
->list_lock
);
622 spin_lock_init(&sctx
->stat_lock
);
623 init_waitqueue_head(&sctx
->list_wait
);
625 WARN_ON(sctx
->wr_curr_bio
!= NULL
);
626 mutex_init(&sctx
->wr_lock
);
627 sctx
->wr_curr_bio
= NULL
;
628 if (is_dev_replace
) {
629 WARN_ON(!fs_info
->dev_replace
.tgtdev
);
630 sctx
->pages_per_wr_bio
= SCRUB_PAGES_PER_WR_BIO
;
631 sctx
->wr_tgtdev
= fs_info
->dev_replace
.tgtdev
;
632 sctx
->flush_all_writes
= false;
638 scrub_free_ctx(sctx
);
639 return ERR_PTR(-ENOMEM
);
642 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
,
650 struct extent_buffer
*eb
;
651 struct btrfs_inode_item
*inode_item
;
652 struct scrub_warning
*swarn
= warn_ctx
;
653 struct btrfs_fs_info
*fs_info
= swarn
->dev
->fs_info
;
654 struct inode_fs_paths
*ipath
= NULL
;
655 struct btrfs_root
*local_root
;
656 struct btrfs_key root_key
;
657 struct btrfs_key key
;
659 root_key
.objectid
= root
;
660 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
661 root_key
.offset
= (u64
)-1;
662 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
663 if (IS_ERR(local_root
)) {
664 ret
= PTR_ERR(local_root
);
669 * this makes the path point to (inum INODE_ITEM ioff)
672 key
.type
= BTRFS_INODE_ITEM_KEY
;
675 ret
= btrfs_search_slot(NULL
, local_root
, &key
, swarn
->path
, 0, 0);
677 btrfs_release_path(swarn
->path
);
681 eb
= swarn
->path
->nodes
[0];
682 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
683 struct btrfs_inode_item
);
684 isize
= btrfs_inode_size(eb
, inode_item
);
685 nlink
= btrfs_inode_nlink(eb
, inode_item
);
686 btrfs_release_path(swarn
->path
);
689 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
690 * uses GFP_NOFS in this context, so we keep it consistent but it does
691 * not seem to be strictly necessary.
693 nofs_flag
= memalloc_nofs_save();
694 ipath
= init_ipath(4096, local_root
, swarn
->path
);
695 memalloc_nofs_restore(nofs_flag
);
697 ret
= PTR_ERR(ipath
);
701 ret
= paths_from_inode(inum
, ipath
);
707 * we deliberately ignore the bit ipath might have been too small to
708 * hold all of the paths here
710 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
711 btrfs_warn_in_rcu(fs_info
,
712 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
713 swarn
->errstr
, swarn
->logical
,
714 rcu_str_deref(swarn
->dev
->name
),
717 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
718 (char *)(unsigned long)ipath
->fspath
->val
[i
]);
724 btrfs_warn_in_rcu(fs_info
,
725 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
726 swarn
->errstr
, swarn
->logical
,
727 rcu_str_deref(swarn
->dev
->name
),
729 root
, inum
, offset
, ret
);
735 static void scrub_print_warning(const char *errstr
, struct scrub_block
*sblock
)
737 struct btrfs_device
*dev
;
738 struct btrfs_fs_info
*fs_info
;
739 struct btrfs_path
*path
;
740 struct btrfs_key found_key
;
741 struct extent_buffer
*eb
;
742 struct btrfs_extent_item
*ei
;
743 struct scrub_warning swarn
;
744 unsigned long ptr
= 0;
752 WARN_ON(sblock
->page_count
< 1);
753 dev
= sblock
->pagev
[0]->dev
;
754 fs_info
= sblock
->sctx
->fs_info
;
756 path
= btrfs_alloc_path();
760 swarn
.physical
= sblock
->pagev
[0]->physical
;
761 swarn
.logical
= sblock
->pagev
[0]->logical
;
762 swarn
.errstr
= errstr
;
765 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
,
770 extent_item_pos
= swarn
.logical
- found_key
.objectid
;
771 swarn
.extent_item_size
= found_key
.offset
;
774 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
775 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
777 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
779 ret
= tree_backref_for_extent(&ptr
, eb
, &found_key
, ei
,
780 item_size
, &ref_root
,
782 btrfs_warn_in_rcu(fs_info
,
783 "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
784 errstr
, swarn
.logical
,
785 rcu_str_deref(dev
->name
),
787 ref_level
? "node" : "leaf",
788 ret
< 0 ? -1 : ref_level
,
789 ret
< 0 ? -1 : ref_root
);
791 btrfs_release_path(path
);
793 btrfs_release_path(path
);
796 iterate_extent_inodes(fs_info
, found_key
.objectid
,
798 scrub_print_warning_inode
, &swarn
, false);
802 btrfs_free_path(path
);
805 static inline void scrub_get_recover(struct scrub_recover
*recover
)
807 refcount_inc(&recover
->refs
);
810 static inline void scrub_put_recover(struct btrfs_fs_info
*fs_info
,
811 struct scrub_recover
*recover
)
813 if (refcount_dec_and_test(&recover
->refs
)) {
814 btrfs_bio_counter_dec(fs_info
);
815 btrfs_put_bbio(recover
->bbio
);
821 * scrub_handle_errored_block gets called when either verification of the
822 * pages failed or the bio failed to read, e.g. with EIO. In the latter
823 * case, this function handles all pages in the bio, even though only one
825 * The goal of this function is to repair the errored block by using the
826 * contents of one of the mirrors.
828 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
)
830 struct scrub_ctx
*sctx
= sblock_to_check
->sctx
;
831 struct btrfs_device
*dev
;
832 struct btrfs_fs_info
*fs_info
;
834 unsigned int failed_mirror_index
;
835 unsigned int is_metadata
;
836 unsigned int have_csum
;
837 struct scrub_block
*sblocks_for_recheck
; /* holds one for each mirror */
838 struct scrub_block
*sblock_bad
;
843 bool full_stripe_locked
;
844 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
845 DEFAULT_RATELIMIT_BURST
);
847 BUG_ON(sblock_to_check
->page_count
< 1);
848 fs_info
= sctx
->fs_info
;
849 if (sblock_to_check
->pagev
[0]->flags
& BTRFS_EXTENT_FLAG_SUPER
) {
851 * if we find an error in a super block, we just report it.
852 * They will get written with the next transaction commit
855 spin_lock(&sctx
->stat_lock
);
856 ++sctx
->stat
.super_errors
;
857 spin_unlock(&sctx
->stat_lock
);
860 logical
= sblock_to_check
->pagev
[0]->logical
;
861 BUG_ON(sblock_to_check
->pagev
[0]->mirror_num
< 1);
862 failed_mirror_index
= sblock_to_check
->pagev
[0]->mirror_num
- 1;
863 is_metadata
= !(sblock_to_check
->pagev
[0]->flags
&
864 BTRFS_EXTENT_FLAG_DATA
);
865 have_csum
= sblock_to_check
->pagev
[0]->have_csum
;
866 dev
= sblock_to_check
->pagev
[0]->dev
;
869 * For RAID5/6, race can happen for a different device scrub thread.
870 * For data corruption, Parity and Data threads will both try
871 * to recovery the data.
872 * Race can lead to doubly added csum error, or even unrecoverable
875 ret
= lock_full_stripe(fs_info
, logical
, &full_stripe_locked
);
877 spin_lock(&sctx
->stat_lock
);
879 sctx
->stat
.malloc_errors
++;
880 sctx
->stat
.read_errors
++;
881 sctx
->stat
.uncorrectable_errors
++;
882 spin_unlock(&sctx
->stat_lock
);
887 * read all mirrors one after the other. This includes to
888 * re-read the extent or metadata block that failed (that was
889 * the cause that this fixup code is called) another time,
890 * page by page this time in order to know which pages
891 * caused I/O errors and which ones are good (for all mirrors).
892 * It is the goal to handle the situation when more than one
893 * mirror contains I/O errors, but the errors do not
894 * overlap, i.e. the data can be repaired by selecting the
895 * pages from those mirrors without I/O error on the
896 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
897 * would be that mirror #1 has an I/O error on the first page,
898 * the second page is good, and mirror #2 has an I/O error on
899 * the second page, but the first page is good.
900 * Then the first page of the first mirror can be repaired by
901 * taking the first page of the second mirror, and the
902 * second page of the second mirror can be repaired by
903 * copying the contents of the 2nd page of the 1st mirror.
904 * One more note: if the pages of one mirror contain I/O
905 * errors, the checksum cannot be verified. In order to get
906 * the best data for repairing, the first attempt is to find
907 * a mirror without I/O errors and with a validated checksum.
908 * Only if this is not possible, the pages are picked from
909 * mirrors with I/O errors without considering the checksum.
910 * If the latter is the case, at the end, the checksum of the
911 * repaired area is verified in order to correctly maintain
915 sblocks_for_recheck
= kcalloc(BTRFS_MAX_MIRRORS
,
916 sizeof(*sblocks_for_recheck
), GFP_NOFS
);
917 if (!sblocks_for_recheck
) {
918 spin_lock(&sctx
->stat_lock
);
919 sctx
->stat
.malloc_errors
++;
920 sctx
->stat
.read_errors
++;
921 sctx
->stat
.uncorrectable_errors
++;
922 spin_unlock(&sctx
->stat_lock
);
923 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
927 /* setup the context, map the logical blocks and alloc the pages */
928 ret
= scrub_setup_recheck_block(sblock_to_check
, sblocks_for_recheck
);
930 spin_lock(&sctx
->stat_lock
);
931 sctx
->stat
.read_errors
++;
932 sctx
->stat
.uncorrectable_errors
++;
933 spin_unlock(&sctx
->stat_lock
);
934 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
937 BUG_ON(failed_mirror_index
>= BTRFS_MAX_MIRRORS
);
938 sblock_bad
= sblocks_for_recheck
+ failed_mirror_index
;
940 /* build and submit the bios for the failed mirror, check checksums */
941 scrub_recheck_block(fs_info
, sblock_bad
, 1);
943 if (!sblock_bad
->header_error
&& !sblock_bad
->checksum_error
&&
944 sblock_bad
->no_io_error_seen
) {
946 * the error disappeared after reading page by page, or
947 * the area was part of a huge bio and other parts of the
948 * bio caused I/O errors, or the block layer merged several
949 * read requests into one and the error is caused by a
950 * different bio (usually one of the two latter cases is
953 spin_lock(&sctx
->stat_lock
);
954 sctx
->stat
.unverified_errors
++;
955 sblock_to_check
->data_corrected
= 1;
956 spin_unlock(&sctx
->stat_lock
);
958 if (sctx
->is_dev_replace
)
959 scrub_write_block_to_dev_replace(sblock_bad
);
963 if (!sblock_bad
->no_io_error_seen
) {
964 spin_lock(&sctx
->stat_lock
);
965 sctx
->stat
.read_errors
++;
966 spin_unlock(&sctx
->stat_lock
);
967 if (__ratelimit(&_rs
))
968 scrub_print_warning("i/o error", sblock_to_check
);
969 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
970 } else if (sblock_bad
->checksum_error
) {
971 spin_lock(&sctx
->stat_lock
);
972 sctx
->stat
.csum_errors
++;
973 spin_unlock(&sctx
->stat_lock
);
974 if (__ratelimit(&_rs
))
975 scrub_print_warning("checksum error", sblock_to_check
);
976 btrfs_dev_stat_inc_and_print(dev
,
977 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
978 } else if (sblock_bad
->header_error
) {
979 spin_lock(&sctx
->stat_lock
);
980 sctx
->stat
.verify_errors
++;
981 spin_unlock(&sctx
->stat_lock
);
982 if (__ratelimit(&_rs
))
983 scrub_print_warning("checksum/header error",
985 if (sblock_bad
->generation_error
)
986 btrfs_dev_stat_inc_and_print(dev
,
987 BTRFS_DEV_STAT_GENERATION_ERRS
);
989 btrfs_dev_stat_inc_and_print(dev
,
990 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
993 if (sctx
->readonly
) {
994 ASSERT(!sctx
->is_dev_replace
);
999 * now build and submit the bios for the other mirrors, check
1001 * First try to pick the mirror which is completely without I/O
1002 * errors and also does not have a checksum error.
1003 * If one is found, and if a checksum is present, the full block
1004 * that is known to contain an error is rewritten. Afterwards
1005 * the block is known to be corrected.
1006 * If a mirror is found which is completely correct, and no
1007 * checksum is present, only those pages are rewritten that had
1008 * an I/O error in the block to be repaired, since it cannot be
1009 * determined, which copy of the other pages is better (and it
1010 * could happen otherwise that a correct page would be
1011 * overwritten by a bad one).
1013 for (mirror_index
= 0; ;mirror_index
++) {
1014 struct scrub_block
*sblock_other
;
1016 if (mirror_index
== failed_mirror_index
)
1019 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1020 if (!scrub_is_page_on_raid56(sblock_bad
->pagev
[0])) {
1021 if (mirror_index
>= BTRFS_MAX_MIRRORS
)
1023 if (!sblocks_for_recheck
[mirror_index
].page_count
)
1026 sblock_other
= sblocks_for_recheck
+ mirror_index
;
1028 struct scrub_recover
*r
= sblock_bad
->pagev
[0]->recover
;
1029 int max_allowed
= r
->bbio
->num_stripes
-
1030 r
->bbio
->num_tgtdevs
;
1032 if (mirror_index
>= max_allowed
)
1034 if (!sblocks_for_recheck
[1].page_count
)
1037 ASSERT(failed_mirror_index
== 0);
1038 sblock_other
= sblocks_for_recheck
+ 1;
1039 sblock_other
->pagev
[0]->mirror_num
= 1 + mirror_index
;
1042 /* build and submit the bios, check checksums */
1043 scrub_recheck_block(fs_info
, sblock_other
, 0);
1045 if (!sblock_other
->header_error
&&
1046 !sblock_other
->checksum_error
&&
1047 sblock_other
->no_io_error_seen
) {
1048 if (sctx
->is_dev_replace
) {
1049 scrub_write_block_to_dev_replace(sblock_other
);
1050 goto corrected_error
;
1052 ret
= scrub_repair_block_from_good_copy(
1053 sblock_bad
, sblock_other
);
1055 goto corrected_error
;
1060 if (sblock_bad
->no_io_error_seen
&& !sctx
->is_dev_replace
)
1061 goto did_not_correct_error
;
1064 * In case of I/O errors in the area that is supposed to be
1065 * repaired, continue by picking good copies of those pages.
1066 * Select the good pages from mirrors to rewrite bad pages from
1067 * the area to fix. Afterwards verify the checksum of the block
1068 * that is supposed to be repaired. This verification step is
1069 * only done for the purpose of statistic counting and for the
1070 * final scrub report, whether errors remain.
1071 * A perfect algorithm could make use of the checksum and try
1072 * all possible combinations of pages from the different mirrors
1073 * until the checksum verification succeeds. For example, when
1074 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1075 * of mirror #2 is readable but the final checksum test fails,
1076 * then the 2nd page of mirror #3 could be tried, whether now
1077 * the final checksum succeeds. But this would be a rare
1078 * exception and is therefore not implemented. At least it is
1079 * avoided that the good copy is overwritten.
1080 * A more useful improvement would be to pick the sectors
1081 * without I/O error based on sector sizes (512 bytes on legacy
1082 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1083 * mirror could be repaired by taking 512 byte of a different
1084 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1085 * area are unreadable.
1088 for (page_num
= 0; page_num
< sblock_bad
->page_count
;
1090 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1091 struct scrub_block
*sblock_other
= NULL
;
1093 /* skip no-io-error page in scrub */
1094 if (!page_bad
->io_error
&& !sctx
->is_dev_replace
)
1097 if (scrub_is_page_on_raid56(sblock_bad
->pagev
[0])) {
1099 * In case of dev replace, if raid56 rebuild process
1100 * didn't work out correct data, then copy the content
1101 * in sblock_bad to make sure target device is identical
1102 * to source device, instead of writing garbage data in
1103 * sblock_for_recheck array to target device.
1105 sblock_other
= NULL
;
1106 } else if (page_bad
->io_error
) {
1107 /* try to find no-io-error page in mirrors */
1108 for (mirror_index
= 0;
1109 mirror_index
< BTRFS_MAX_MIRRORS
&&
1110 sblocks_for_recheck
[mirror_index
].page_count
> 0;
1112 if (!sblocks_for_recheck
[mirror_index
].
1113 pagev
[page_num
]->io_error
) {
1114 sblock_other
= sblocks_for_recheck
+
1123 if (sctx
->is_dev_replace
) {
1125 * did not find a mirror to fetch the page
1126 * from. scrub_write_page_to_dev_replace()
1127 * handles this case (page->io_error), by
1128 * filling the block with zeros before
1129 * submitting the write request
1132 sblock_other
= sblock_bad
;
1134 if (scrub_write_page_to_dev_replace(sblock_other
,
1136 btrfs_dev_replace_stats_inc(
1137 &fs_info
->dev_replace
.num_write_errors
);
1140 } else if (sblock_other
) {
1141 ret
= scrub_repair_page_from_good_copy(sblock_bad
,
1145 page_bad
->io_error
= 0;
1151 if (success
&& !sctx
->is_dev_replace
) {
1152 if (is_metadata
|| have_csum
) {
1154 * need to verify the checksum now that all
1155 * sectors on disk are repaired (the write
1156 * request for data to be repaired is on its way).
1157 * Just be lazy and use scrub_recheck_block()
1158 * which re-reads the data before the checksum
1159 * is verified, but most likely the data comes out
1160 * of the page cache.
1162 scrub_recheck_block(fs_info
, sblock_bad
, 1);
1163 if (!sblock_bad
->header_error
&&
1164 !sblock_bad
->checksum_error
&&
1165 sblock_bad
->no_io_error_seen
)
1166 goto corrected_error
;
1168 goto did_not_correct_error
;
1171 spin_lock(&sctx
->stat_lock
);
1172 sctx
->stat
.corrected_errors
++;
1173 sblock_to_check
->data_corrected
= 1;
1174 spin_unlock(&sctx
->stat_lock
);
1175 btrfs_err_rl_in_rcu(fs_info
,
1176 "fixed up error at logical %llu on dev %s",
1177 logical
, rcu_str_deref(dev
->name
));
1180 did_not_correct_error
:
1181 spin_lock(&sctx
->stat_lock
);
1182 sctx
->stat
.uncorrectable_errors
++;
1183 spin_unlock(&sctx
->stat_lock
);
1184 btrfs_err_rl_in_rcu(fs_info
,
1185 "unable to fixup (regular) error at logical %llu on dev %s",
1186 logical
, rcu_str_deref(dev
->name
));
1190 if (sblocks_for_recheck
) {
1191 for (mirror_index
= 0; mirror_index
< BTRFS_MAX_MIRRORS
;
1193 struct scrub_block
*sblock
= sblocks_for_recheck
+
1195 struct scrub_recover
*recover
;
1198 for (page_index
= 0; page_index
< sblock
->page_count
;
1200 sblock
->pagev
[page_index
]->sblock
= NULL
;
1201 recover
= sblock
->pagev
[page_index
]->recover
;
1203 scrub_put_recover(fs_info
, recover
);
1204 sblock
->pagev
[page_index
]->recover
=
1207 scrub_page_put(sblock
->pagev
[page_index
]);
1210 kfree(sblocks_for_recheck
);
1213 ret
= unlock_full_stripe(fs_info
, logical
, full_stripe_locked
);
1219 static inline int scrub_nr_raid_mirrors(struct btrfs_bio
*bbio
)
1221 if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID5
)
1223 else if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID6
)
1226 return (int)bbio
->num_stripes
;
1229 static inline void scrub_stripe_index_and_offset(u64 logical
, u64 map_type
,
1232 int nstripes
, int mirror
,
1238 if (map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
1240 for (i
= 0; i
< nstripes
; i
++) {
1241 if (raid_map
[i
] == RAID6_Q_STRIPE
||
1242 raid_map
[i
] == RAID5_P_STRIPE
)
1245 if (logical
>= raid_map
[i
] &&
1246 logical
< raid_map
[i
] + mapped_length
)
1251 *stripe_offset
= logical
- raid_map
[i
];
1253 /* The other RAID type */
1254 *stripe_index
= mirror
;
1259 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
1260 struct scrub_block
*sblocks_for_recheck
)
1262 struct scrub_ctx
*sctx
= original_sblock
->sctx
;
1263 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
1264 u64 length
= original_sblock
->page_count
* PAGE_SIZE
;
1265 u64 logical
= original_sblock
->pagev
[0]->logical
;
1266 u64 generation
= original_sblock
->pagev
[0]->generation
;
1267 u64 flags
= original_sblock
->pagev
[0]->flags
;
1268 u64 have_csum
= original_sblock
->pagev
[0]->have_csum
;
1269 struct scrub_recover
*recover
;
1270 struct btrfs_bio
*bbio
;
1281 * note: the two members refs and outstanding_pages
1282 * are not used (and not set) in the blocks that are used for
1283 * the recheck procedure
1286 while (length
> 0) {
1287 sublen
= min_t(u64
, length
, PAGE_SIZE
);
1288 mapped_length
= sublen
;
1292 * with a length of PAGE_SIZE, each returned stripe
1293 * represents one mirror
1295 btrfs_bio_counter_inc_blocked(fs_info
);
1296 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
1297 logical
, &mapped_length
, &bbio
);
1298 if (ret
|| !bbio
|| mapped_length
< sublen
) {
1299 btrfs_put_bbio(bbio
);
1300 btrfs_bio_counter_dec(fs_info
);
1304 recover
= kzalloc(sizeof(struct scrub_recover
), GFP_NOFS
);
1306 btrfs_put_bbio(bbio
);
1307 btrfs_bio_counter_dec(fs_info
);
1311 refcount_set(&recover
->refs
, 1);
1312 recover
->bbio
= bbio
;
1313 recover
->map_length
= mapped_length
;
1315 BUG_ON(page_index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
1317 nmirrors
= min(scrub_nr_raid_mirrors(bbio
), BTRFS_MAX_MIRRORS
);
1319 for (mirror_index
= 0; mirror_index
< nmirrors
;
1321 struct scrub_block
*sblock
;
1322 struct scrub_page
*page
;
1324 sblock
= sblocks_for_recheck
+ mirror_index
;
1325 sblock
->sctx
= sctx
;
1327 page
= kzalloc(sizeof(*page
), GFP_NOFS
);
1330 spin_lock(&sctx
->stat_lock
);
1331 sctx
->stat
.malloc_errors
++;
1332 spin_unlock(&sctx
->stat_lock
);
1333 scrub_put_recover(fs_info
, recover
);
1336 scrub_page_get(page
);
1337 sblock
->pagev
[page_index
] = page
;
1338 page
->sblock
= sblock
;
1339 page
->flags
= flags
;
1340 page
->generation
= generation
;
1341 page
->logical
= logical
;
1342 page
->have_csum
= have_csum
;
1345 original_sblock
->pagev
[0]->csum
,
1348 scrub_stripe_index_and_offset(logical
,
1357 page
->physical
= bbio
->stripes
[stripe_index
].physical
+
1359 page
->dev
= bbio
->stripes
[stripe_index
].dev
;
1361 BUG_ON(page_index
>= original_sblock
->page_count
);
1362 page
->physical_for_dev_replace
=
1363 original_sblock
->pagev
[page_index
]->
1364 physical_for_dev_replace
;
1365 /* for missing devices, dev->bdev is NULL */
1366 page
->mirror_num
= mirror_index
+ 1;
1367 sblock
->page_count
++;
1368 page
->page
= alloc_page(GFP_NOFS
);
1372 scrub_get_recover(recover
);
1373 page
->recover
= recover
;
1375 scrub_put_recover(fs_info
, recover
);
1384 static void scrub_bio_wait_endio(struct bio
*bio
)
1386 complete(bio
->bi_private
);
1389 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info
*fs_info
,
1391 struct scrub_page
*page
)
1393 DECLARE_COMPLETION_ONSTACK(done
);
1397 bio
->bi_iter
.bi_sector
= page
->logical
>> 9;
1398 bio
->bi_private
= &done
;
1399 bio
->bi_end_io
= scrub_bio_wait_endio
;
1401 mirror_num
= page
->sblock
->pagev
[0]->mirror_num
;
1402 ret
= raid56_parity_recover(fs_info
, bio
, page
->recover
->bbio
,
1403 page
->recover
->map_length
,
1408 wait_for_completion_io(&done
);
1409 return blk_status_to_errno(bio
->bi_status
);
1412 static void scrub_recheck_block_on_raid56(struct btrfs_fs_info
*fs_info
,
1413 struct scrub_block
*sblock
)
1415 struct scrub_page
*first_page
= sblock
->pagev
[0];
1419 /* All pages in sblock belong to the same stripe on the same device. */
1420 ASSERT(first_page
->dev
);
1421 if (!first_page
->dev
->bdev
)
1424 bio
= btrfs_io_bio_alloc(BIO_MAX_PAGES
);
1425 bio_set_dev(bio
, first_page
->dev
->bdev
);
1427 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1428 struct scrub_page
*page
= sblock
->pagev
[page_num
];
1430 WARN_ON(!page
->page
);
1431 bio_add_page(bio
, page
->page
, PAGE_SIZE
, 0);
1434 if (scrub_submit_raid56_bio_wait(fs_info
, bio
, first_page
)) {
1441 scrub_recheck_block_checksum(sblock
);
1445 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++)
1446 sblock
->pagev
[page_num
]->io_error
= 1;
1448 sblock
->no_io_error_seen
= 0;
1452 * this function will check the on disk data for checksum errors, header
1453 * errors and read I/O errors. If any I/O errors happen, the exact pages
1454 * which are errored are marked as being bad. The goal is to enable scrub
1455 * to take those pages that are not errored from all the mirrors so that
1456 * the pages that are errored in the just handled mirror can be repaired.
1458 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
1459 struct scrub_block
*sblock
,
1460 int retry_failed_mirror
)
1464 sblock
->no_io_error_seen
= 1;
1466 /* short cut for raid56 */
1467 if (!retry_failed_mirror
&& scrub_is_page_on_raid56(sblock
->pagev
[0]))
1468 return scrub_recheck_block_on_raid56(fs_info
, sblock
);
1470 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1472 struct scrub_page
*page
= sblock
->pagev
[page_num
];
1474 if (page
->dev
->bdev
== NULL
) {
1476 sblock
->no_io_error_seen
= 0;
1480 WARN_ON(!page
->page
);
1481 bio
= btrfs_io_bio_alloc(1);
1482 bio_set_dev(bio
, page
->dev
->bdev
);
1484 bio_add_page(bio
, page
->page
, PAGE_SIZE
, 0);
1485 bio
->bi_iter
.bi_sector
= page
->physical
>> 9;
1486 bio
->bi_opf
= REQ_OP_READ
;
1488 if (btrfsic_submit_bio_wait(bio
)) {
1490 sblock
->no_io_error_seen
= 0;
1496 if (sblock
->no_io_error_seen
)
1497 scrub_recheck_block_checksum(sblock
);
1500 static inline int scrub_check_fsid(u8 fsid
[],
1501 struct scrub_page
*spage
)
1503 struct btrfs_fs_devices
*fs_devices
= spage
->dev
->fs_devices
;
1506 ret
= memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1510 static void scrub_recheck_block_checksum(struct scrub_block
*sblock
)
1512 sblock
->header_error
= 0;
1513 sblock
->checksum_error
= 0;
1514 sblock
->generation_error
= 0;
1516 if (sblock
->pagev
[0]->flags
& BTRFS_EXTENT_FLAG_DATA
)
1517 scrub_checksum_data(sblock
);
1519 scrub_checksum_tree_block(sblock
);
1522 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
1523 struct scrub_block
*sblock_good
)
1528 for (page_num
= 0; page_num
< sblock_bad
->page_count
; page_num
++) {
1531 ret_sub
= scrub_repair_page_from_good_copy(sblock_bad
,
1541 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
1542 struct scrub_block
*sblock_good
,
1543 int page_num
, int force_write
)
1545 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1546 struct scrub_page
*page_good
= sblock_good
->pagev
[page_num
];
1547 struct btrfs_fs_info
*fs_info
= sblock_bad
->sctx
->fs_info
;
1549 BUG_ON(page_bad
->page
== NULL
);
1550 BUG_ON(page_good
->page
== NULL
);
1551 if (force_write
|| sblock_bad
->header_error
||
1552 sblock_bad
->checksum_error
|| page_bad
->io_error
) {
1556 if (!page_bad
->dev
->bdev
) {
1557 btrfs_warn_rl(fs_info
,
1558 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
1562 bio
= btrfs_io_bio_alloc(1);
1563 bio_set_dev(bio
, page_bad
->dev
->bdev
);
1564 bio
->bi_iter
.bi_sector
= page_bad
->physical
>> 9;
1565 bio
->bi_opf
= REQ_OP_WRITE
;
1567 ret
= bio_add_page(bio
, page_good
->page
, PAGE_SIZE
, 0);
1568 if (PAGE_SIZE
!= ret
) {
1573 if (btrfsic_submit_bio_wait(bio
)) {
1574 btrfs_dev_stat_inc_and_print(page_bad
->dev
,
1575 BTRFS_DEV_STAT_WRITE_ERRS
);
1576 btrfs_dev_replace_stats_inc(
1577 &fs_info
->dev_replace
.num_write_errors
);
1587 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
)
1589 struct btrfs_fs_info
*fs_info
= sblock
->sctx
->fs_info
;
1593 * This block is used for the check of the parity on the source device,
1594 * so the data needn't be written into the destination device.
1596 if (sblock
->sparity
)
1599 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1602 ret
= scrub_write_page_to_dev_replace(sblock
, page_num
);
1604 btrfs_dev_replace_stats_inc(
1605 &fs_info
->dev_replace
.num_write_errors
);
1609 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
1612 struct scrub_page
*spage
= sblock
->pagev
[page_num
];
1614 BUG_ON(spage
->page
== NULL
);
1615 if (spage
->io_error
) {
1616 void *mapped_buffer
= kmap_atomic(spage
->page
);
1618 clear_page(mapped_buffer
);
1619 flush_dcache_page(spage
->page
);
1620 kunmap_atomic(mapped_buffer
);
1622 return scrub_add_page_to_wr_bio(sblock
->sctx
, spage
);
1625 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
1626 struct scrub_page
*spage
)
1628 struct scrub_bio
*sbio
;
1631 mutex_lock(&sctx
->wr_lock
);
1633 if (!sctx
->wr_curr_bio
) {
1634 unsigned int nofs_flag
;
1637 * We must use GFP_NOFS because the scrub task might be waiting
1638 * for a worker task executing this function and in turn a
1639 * transaction commit might be waiting the scrub task to pause
1640 * (which needs to wait for all the worker tasks to complete
1643 nofs_flag
= memalloc_nofs_save();
1644 sctx
->wr_curr_bio
= kzalloc(sizeof(*sctx
->wr_curr_bio
),
1646 memalloc_nofs_restore(nofs_flag
);
1647 if (!sctx
->wr_curr_bio
) {
1648 mutex_unlock(&sctx
->wr_lock
);
1651 sctx
->wr_curr_bio
->sctx
= sctx
;
1652 sctx
->wr_curr_bio
->page_count
= 0;
1654 sbio
= sctx
->wr_curr_bio
;
1655 if (sbio
->page_count
== 0) {
1658 sbio
->physical
= spage
->physical_for_dev_replace
;
1659 sbio
->logical
= spage
->logical
;
1660 sbio
->dev
= sctx
->wr_tgtdev
;
1663 bio
= btrfs_io_bio_alloc(sctx
->pages_per_wr_bio
);
1667 bio
->bi_private
= sbio
;
1668 bio
->bi_end_io
= scrub_wr_bio_end_io
;
1669 bio_set_dev(bio
, sbio
->dev
->bdev
);
1670 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
1671 bio
->bi_opf
= REQ_OP_WRITE
;
1673 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
1674 spage
->physical_for_dev_replace
||
1675 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
1677 scrub_wr_submit(sctx
);
1681 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
1682 if (ret
!= PAGE_SIZE
) {
1683 if (sbio
->page_count
< 1) {
1686 mutex_unlock(&sctx
->wr_lock
);
1689 scrub_wr_submit(sctx
);
1693 sbio
->pagev
[sbio
->page_count
] = spage
;
1694 scrub_page_get(spage
);
1696 if (sbio
->page_count
== sctx
->pages_per_wr_bio
)
1697 scrub_wr_submit(sctx
);
1698 mutex_unlock(&sctx
->wr_lock
);
1703 static void scrub_wr_submit(struct scrub_ctx
*sctx
)
1705 struct scrub_bio
*sbio
;
1707 if (!sctx
->wr_curr_bio
)
1710 sbio
= sctx
->wr_curr_bio
;
1711 sctx
->wr_curr_bio
= NULL
;
1712 WARN_ON(!sbio
->bio
->bi_disk
);
1713 scrub_pending_bio_inc(sctx
);
1714 /* process all writes in a single worker thread. Then the block layer
1715 * orders the requests before sending them to the driver which
1716 * doubled the write performance on spinning disks when measured
1718 btrfsic_submit_bio(sbio
->bio
);
1721 static void scrub_wr_bio_end_io(struct bio
*bio
)
1723 struct scrub_bio
*sbio
= bio
->bi_private
;
1724 struct btrfs_fs_info
*fs_info
= sbio
->dev
->fs_info
;
1726 sbio
->status
= bio
->bi_status
;
1729 btrfs_init_work(&sbio
->work
, btrfs_scrubwrc_helper
,
1730 scrub_wr_bio_end_io_worker
, NULL
, NULL
);
1731 btrfs_queue_work(fs_info
->scrub_wr_completion_workers
, &sbio
->work
);
1734 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
)
1736 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
1737 struct scrub_ctx
*sctx
= sbio
->sctx
;
1740 WARN_ON(sbio
->page_count
> SCRUB_PAGES_PER_WR_BIO
);
1742 struct btrfs_dev_replace
*dev_replace
=
1743 &sbio
->sctx
->fs_info
->dev_replace
;
1745 for (i
= 0; i
< sbio
->page_count
; i
++) {
1746 struct scrub_page
*spage
= sbio
->pagev
[i
];
1748 spage
->io_error
= 1;
1749 btrfs_dev_replace_stats_inc(&dev_replace
->
1754 for (i
= 0; i
< sbio
->page_count
; i
++)
1755 scrub_page_put(sbio
->pagev
[i
]);
1759 scrub_pending_bio_dec(sctx
);
1762 static int scrub_checksum(struct scrub_block
*sblock
)
1768 * No need to initialize these stats currently,
1769 * because this function only use return value
1770 * instead of these stats value.
1775 sblock
->header_error
= 0;
1776 sblock
->generation_error
= 0;
1777 sblock
->checksum_error
= 0;
1779 WARN_ON(sblock
->page_count
< 1);
1780 flags
= sblock
->pagev
[0]->flags
;
1782 if (flags
& BTRFS_EXTENT_FLAG_DATA
)
1783 ret
= scrub_checksum_data(sblock
);
1784 else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
1785 ret
= scrub_checksum_tree_block(sblock
);
1786 else if (flags
& BTRFS_EXTENT_FLAG_SUPER
)
1787 (void)scrub_checksum_super(sblock
);
1791 scrub_handle_errored_block(sblock
);
1796 static int scrub_checksum_data(struct scrub_block
*sblock
)
1798 struct scrub_ctx
*sctx
= sblock
->sctx
;
1799 u8 csum
[BTRFS_CSUM_SIZE
];
1807 BUG_ON(sblock
->page_count
< 1);
1808 if (!sblock
->pagev
[0]->have_csum
)
1811 on_disk_csum
= sblock
->pagev
[0]->csum
;
1812 page
= sblock
->pagev
[0]->page
;
1813 buffer
= kmap_atomic(page
);
1815 len
= sctx
->fs_info
->sectorsize
;
1818 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1820 crc
= btrfs_csum_data(buffer
, crc
, l
);
1821 kunmap_atomic(buffer
);
1826 BUG_ON(index
>= sblock
->page_count
);
1827 BUG_ON(!sblock
->pagev
[index
]->page
);
1828 page
= sblock
->pagev
[index
]->page
;
1829 buffer
= kmap_atomic(page
);
1832 btrfs_csum_final(crc
, csum
);
1833 if (memcmp(csum
, on_disk_csum
, sctx
->csum_size
))
1834 sblock
->checksum_error
= 1;
1836 return sblock
->checksum_error
;
1839 static int scrub_checksum_tree_block(struct scrub_block
*sblock
)
1841 struct scrub_ctx
*sctx
= sblock
->sctx
;
1842 struct btrfs_header
*h
;
1843 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
1844 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1845 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1847 void *mapped_buffer
;
1854 BUG_ON(sblock
->page_count
< 1);
1855 page
= sblock
->pagev
[0]->page
;
1856 mapped_buffer
= kmap_atomic(page
);
1857 h
= (struct btrfs_header
*)mapped_buffer
;
1858 memcpy(on_disk_csum
, h
->csum
, sctx
->csum_size
);
1861 * we don't use the getter functions here, as we
1862 * a) don't have an extent buffer and
1863 * b) the page is already kmapped
1865 if (sblock
->pagev
[0]->logical
!= btrfs_stack_header_bytenr(h
))
1866 sblock
->header_error
= 1;
1868 if (sblock
->pagev
[0]->generation
!= btrfs_stack_header_generation(h
)) {
1869 sblock
->header_error
= 1;
1870 sblock
->generation_error
= 1;
1873 if (!scrub_check_fsid(h
->fsid
, sblock
->pagev
[0]))
1874 sblock
->header_error
= 1;
1876 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
1878 sblock
->header_error
= 1;
1880 len
= sctx
->fs_info
->nodesize
- BTRFS_CSUM_SIZE
;
1881 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
1882 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
1885 u64 l
= min_t(u64
, len
, mapped_size
);
1887 crc
= btrfs_csum_data(p
, crc
, l
);
1888 kunmap_atomic(mapped_buffer
);
1893 BUG_ON(index
>= sblock
->page_count
);
1894 BUG_ON(!sblock
->pagev
[index
]->page
);
1895 page
= sblock
->pagev
[index
]->page
;
1896 mapped_buffer
= kmap_atomic(page
);
1897 mapped_size
= PAGE_SIZE
;
1901 btrfs_csum_final(crc
, calculated_csum
);
1902 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
1903 sblock
->checksum_error
= 1;
1905 return sblock
->header_error
|| sblock
->checksum_error
;
1908 static int scrub_checksum_super(struct scrub_block
*sblock
)
1910 struct btrfs_super_block
*s
;
1911 struct scrub_ctx
*sctx
= sblock
->sctx
;
1912 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1913 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1915 void *mapped_buffer
;
1924 BUG_ON(sblock
->page_count
< 1);
1925 page
= sblock
->pagev
[0]->page
;
1926 mapped_buffer
= kmap_atomic(page
);
1927 s
= (struct btrfs_super_block
*)mapped_buffer
;
1928 memcpy(on_disk_csum
, s
->csum
, sctx
->csum_size
);
1930 if (sblock
->pagev
[0]->logical
!= btrfs_super_bytenr(s
))
1933 if (sblock
->pagev
[0]->generation
!= btrfs_super_generation(s
))
1936 if (!scrub_check_fsid(s
->fsid
, sblock
->pagev
[0]))
1939 len
= BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
;
1940 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
1941 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
1944 u64 l
= min_t(u64
, len
, mapped_size
);
1946 crc
= btrfs_csum_data(p
, crc
, l
);
1947 kunmap_atomic(mapped_buffer
);
1952 BUG_ON(index
>= sblock
->page_count
);
1953 BUG_ON(!sblock
->pagev
[index
]->page
);
1954 page
= sblock
->pagev
[index
]->page
;
1955 mapped_buffer
= kmap_atomic(page
);
1956 mapped_size
= PAGE_SIZE
;
1960 btrfs_csum_final(crc
, calculated_csum
);
1961 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
1964 if (fail_cor
+ fail_gen
) {
1966 * if we find an error in a super block, we just report it.
1967 * They will get written with the next transaction commit
1970 spin_lock(&sctx
->stat_lock
);
1971 ++sctx
->stat
.super_errors
;
1972 spin_unlock(&sctx
->stat_lock
);
1974 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
1975 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1977 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
1978 BTRFS_DEV_STAT_GENERATION_ERRS
);
1981 return fail_cor
+ fail_gen
;
1984 static void scrub_block_get(struct scrub_block
*sblock
)
1986 refcount_inc(&sblock
->refs
);
1989 static void scrub_block_put(struct scrub_block
*sblock
)
1991 if (refcount_dec_and_test(&sblock
->refs
)) {
1994 if (sblock
->sparity
)
1995 scrub_parity_put(sblock
->sparity
);
1997 for (i
= 0; i
< sblock
->page_count
; i
++)
1998 scrub_page_put(sblock
->pagev
[i
]);
2003 static void scrub_page_get(struct scrub_page
*spage
)
2005 atomic_inc(&spage
->refs
);
2008 static void scrub_page_put(struct scrub_page
*spage
)
2010 if (atomic_dec_and_test(&spage
->refs
)) {
2012 __free_page(spage
->page
);
2017 static void scrub_submit(struct scrub_ctx
*sctx
)
2019 struct scrub_bio
*sbio
;
2021 if (sctx
->curr
== -1)
2024 sbio
= sctx
->bios
[sctx
->curr
];
2026 scrub_pending_bio_inc(sctx
);
2027 btrfsic_submit_bio(sbio
->bio
);
2030 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
2031 struct scrub_page
*spage
)
2033 struct scrub_block
*sblock
= spage
->sblock
;
2034 struct scrub_bio
*sbio
;
2039 * grab a fresh bio or wait for one to become available
2041 while (sctx
->curr
== -1) {
2042 spin_lock(&sctx
->list_lock
);
2043 sctx
->curr
= sctx
->first_free
;
2044 if (sctx
->curr
!= -1) {
2045 sctx
->first_free
= sctx
->bios
[sctx
->curr
]->next_free
;
2046 sctx
->bios
[sctx
->curr
]->next_free
= -1;
2047 sctx
->bios
[sctx
->curr
]->page_count
= 0;
2048 spin_unlock(&sctx
->list_lock
);
2050 spin_unlock(&sctx
->list_lock
);
2051 wait_event(sctx
->list_wait
, sctx
->first_free
!= -1);
2054 sbio
= sctx
->bios
[sctx
->curr
];
2055 if (sbio
->page_count
== 0) {
2058 sbio
->physical
= spage
->physical
;
2059 sbio
->logical
= spage
->logical
;
2060 sbio
->dev
= spage
->dev
;
2063 bio
= btrfs_io_bio_alloc(sctx
->pages_per_rd_bio
);
2067 bio
->bi_private
= sbio
;
2068 bio
->bi_end_io
= scrub_bio_end_io
;
2069 bio_set_dev(bio
, sbio
->dev
->bdev
);
2070 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
2071 bio
->bi_opf
= REQ_OP_READ
;
2073 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
2075 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
2077 sbio
->dev
!= spage
->dev
) {
2082 sbio
->pagev
[sbio
->page_count
] = spage
;
2083 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
2084 if (ret
!= PAGE_SIZE
) {
2085 if (sbio
->page_count
< 1) {
2094 scrub_block_get(sblock
); /* one for the page added to the bio */
2095 atomic_inc(&sblock
->outstanding_pages
);
2097 if (sbio
->page_count
== sctx
->pages_per_rd_bio
)
2103 static void scrub_missing_raid56_end_io(struct bio
*bio
)
2105 struct scrub_block
*sblock
= bio
->bi_private
;
2106 struct btrfs_fs_info
*fs_info
= sblock
->sctx
->fs_info
;
2109 sblock
->no_io_error_seen
= 0;
2113 btrfs_queue_work(fs_info
->scrub_workers
, &sblock
->work
);
2116 static void scrub_missing_raid56_worker(struct btrfs_work
*work
)
2118 struct scrub_block
*sblock
= container_of(work
, struct scrub_block
, work
);
2119 struct scrub_ctx
*sctx
= sblock
->sctx
;
2120 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2122 struct btrfs_device
*dev
;
2124 logical
= sblock
->pagev
[0]->logical
;
2125 dev
= sblock
->pagev
[0]->dev
;
2127 if (sblock
->no_io_error_seen
)
2128 scrub_recheck_block_checksum(sblock
);
2130 if (!sblock
->no_io_error_seen
) {
2131 spin_lock(&sctx
->stat_lock
);
2132 sctx
->stat
.read_errors
++;
2133 spin_unlock(&sctx
->stat_lock
);
2134 btrfs_err_rl_in_rcu(fs_info
,
2135 "IO error rebuilding logical %llu for dev %s",
2136 logical
, rcu_str_deref(dev
->name
));
2137 } else if (sblock
->header_error
|| sblock
->checksum_error
) {
2138 spin_lock(&sctx
->stat_lock
);
2139 sctx
->stat
.uncorrectable_errors
++;
2140 spin_unlock(&sctx
->stat_lock
);
2141 btrfs_err_rl_in_rcu(fs_info
,
2142 "failed to rebuild valid logical %llu for dev %s",
2143 logical
, rcu_str_deref(dev
->name
));
2145 scrub_write_block_to_dev_replace(sblock
);
2148 if (sctx
->is_dev_replace
&& sctx
->flush_all_writes
) {
2149 mutex_lock(&sctx
->wr_lock
);
2150 scrub_wr_submit(sctx
);
2151 mutex_unlock(&sctx
->wr_lock
);
2154 scrub_block_put(sblock
);
2155 scrub_pending_bio_dec(sctx
);
2158 static void scrub_missing_raid56_pages(struct scrub_block
*sblock
)
2160 struct scrub_ctx
*sctx
= sblock
->sctx
;
2161 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2162 u64 length
= sblock
->page_count
* PAGE_SIZE
;
2163 u64 logical
= sblock
->pagev
[0]->logical
;
2164 struct btrfs_bio
*bbio
= NULL
;
2166 struct btrfs_raid_bio
*rbio
;
2170 btrfs_bio_counter_inc_blocked(fs_info
);
2171 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
, logical
,
2173 if (ret
|| !bbio
|| !bbio
->raid_map
)
2176 if (WARN_ON(!sctx
->is_dev_replace
||
2177 !(bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))) {
2179 * We shouldn't be scrubbing a missing device. Even for dev
2180 * replace, we should only get here for RAID 5/6. We either
2181 * managed to mount something with no mirrors remaining or
2182 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2187 bio
= btrfs_io_bio_alloc(0);
2188 bio
->bi_iter
.bi_sector
= logical
>> 9;
2189 bio
->bi_private
= sblock
;
2190 bio
->bi_end_io
= scrub_missing_raid56_end_io
;
2192 rbio
= raid56_alloc_missing_rbio(fs_info
, bio
, bbio
, length
);
2196 for (i
= 0; i
< sblock
->page_count
; i
++) {
2197 struct scrub_page
*spage
= sblock
->pagev
[i
];
2199 raid56_add_scrub_pages(rbio
, spage
->page
, spage
->logical
);
2202 btrfs_init_work(&sblock
->work
, btrfs_scrub_helper
,
2203 scrub_missing_raid56_worker
, NULL
, NULL
);
2204 scrub_block_get(sblock
);
2205 scrub_pending_bio_inc(sctx
);
2206 raid56_submit_missing_rbio(rbio
);
2212 btrfs_bio_counter_dec(fs_info
);
2213 btrfs_put_bbio(bbio
);
2214 spin_lock(&sctx
->stat_lock
);
2215 sctx
->stat
.malloc_errors
++;
2216 spin_unlock(&sctx
->stat_lock
);
2219 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2220 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2221 u64 gen
, int mirror_num
, u8
*csum
, int force
,
2222 u64 physical_for_dev_replace
)
2224 struct scrub_block
*sblock
;
2227 sblock
= kzalloc(sizeof(*sblock
), GFP_KERNEL
);
2229 spin_lock(&sctx
->stat_lock
);
2230 sctx
->stat
.malloc_errors
++;
2231 spin_unlock(&sctx
->stat_lock
);
2235 /* one ref inside this function, plus one for each page added to
2237 refcount_set(&sblock
->refs
, 1);
2238 sblock
->sctx
= sctx
;
2239 sblock
->no_io_error_seen
= 1;
2241 for (index
= 0; len
> 0; index
++) {
2242 struct scrub_page
*spage
;
2243 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2245 spage
= kzalloc(sizeof(*spage
), GFP_KERNEL
);
2248 spin_lock(&sctx
->stat_lock
);
2249 sctx
->stat
.malloc_errors
++;
2250 spin_unlock(&sctx
->stat_lock
);
2251 scrub_block_put(sblock
);
2254 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2255 scrub_page_get(spage
);
2256 sblock
->pagev
[index
] = spage
;
2257 spage
->sblock
= sblock
;
2259 spage
->flags
= flags
;
2260 spage
->generation
= gen
;
2261 spage
->logical
= logical
;
2262 spage
->physical
= physical
;
2263 spage
->physical_for_dev_replace
= physical_for_dev_replace
;
2264 spage
->mirror_num
= mirror_num
;
2266 spage
->have_csum
= 1;
2267 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2269 spage
->have_csum
= 0;
2271 sblock
->page_count
++;
2272 spage
->page
= alloc_page(GFP_KERNEL
);
2278 physical_for_dev_replace
+= l
;
2281 WARN_ON(sblock
->page_count
== 0);
2282 if (test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
)) {
2284 * This case should only be hit for RAID 5/6 device replace. See
2285 * the comment in scrub_missing_raid56_pages() for details.
2287 scrub_missing_raid56_pages(sblock
);
2289 for (index
= 0; index
< sblock
->page_count
; index
++) {
2290 struct scrub_page
*spage
= sblock
->pagev
[index
];
2293 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2295 scrub_block_put(sblock
);
2304 /* last one frees, either here or in bio completion for last page */
2305 scrub_block_put(sblock
);
2309 static void scrub_bio_end_io(struct bio
*bio
)
2311 struct scrub_bio
*sbio
= bio
->bi_private
;
2312 struct btrfs_fs_info
*fs_info
= sbio
->dev
->fs_info
;
2314 sbio
->status
= bio
->bi_status
;
2317 btrfs_queue_work(fs_info
->scrub_workers
, &sbio
->work
);
2320 static void scrub_bio_end_io_worker(struct btrfs_work
*work
)
2322 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
2323 struct scrub_ctx
*sctx
= sbio
->sctx
;
2326 BUG_ON(sbio
->page_count
> SCRUB_PAGES_PER_RD_BIO
);
2328 for (i
= 0; i
< sbio
->page_count
; i
++) {
2329 struct scrub_page
*spage
= sbio
->pagev
[i
];
2331 spage
->io_error
= 1;
2332 spage
->sblock
->no_io_error_seen
= 0;
2336 /* now complete the scrub_block items that have all pages completed */
2337 for (i
= 0; i
< sbio
->page_count
; i
++) {
2338 struct scrub_page
*spage
= sbio
->pagev
[i
];
2339 struct scrub_block
*sblock
= spage
->sblock
;
2341 if (atomic_dec_and_test(&sblock
->outstanding_pages
))
2342 scrub_block_complete(sblock
);
2343 scrub_block_put(sblock
);
2348 spin_lock(&sctx
->list_lock
);
2349 sbio
->next_free
= sctx
->first_free
;
2350 sctx
->first_free
= sbio
->index
;
2351 spin_unlock(&sctx
->list_lock
);
2353 if (sctx
->is_dev_replace
&& sctx
->flush_all_writes
) {
2354 mutex_lock(&sctx
->wr_lock
);
2355 scrub_wr_submit(sctx
);
2356 mutex_unlock(&sctx
->wr_lock
);
2359 scrub_pending_bio_dec(sctx
);
2362 static inline void __scrub_mark_bitmap(struct scrub_parity
*sparity
,
2363 unsigned long *bitmap
,
2369 int sectorsize
= sparity
->sctx
->fs_info
->sectorsize
;
2371 if (len
>= sparity
->stripe_len
) {
2372 bitmap_set(bitmap
, 0, sparity
->nsectors
);
2376 start
-= sparity
->logic_start
;
2377 start
= div64_u64_rem(start
, sparity
->stripe_len
, &offset
);
2378 offset
= div_u64(offset
, sectorsize
);
2379 nsectors64
= div_u64(len
, sectorsize
);
2381 ASSERT(nsectors64
< UINT_MAX
);
2382 nsectors
= (u32
)nsectors64
;
2384 if (offset
+ nsectors
<= sparity
->nsectors
) {
2385 bitmap_set(bitmap
, offset
, nsectors
);
2389 bitmap_set(bitmap
, offset
, sparity
->nsectors
- offset
);
2390 bitmap_set(bitmap
, 0, nsectors
- (sparity
->nsectors
- offset
));
2393 static inline void scrub_parity_mark_sectors_error(struct scrub_parity
*sparity
,
2396 __scrub_mark_bitmap(sparity
, sparity
->ebitmap
, start
, len
);
2399 static inline void scrub_parity_mark_sectors_data(struct scrub_parity
*sparity
,
2402 __scrub_mark_bitmap(sparity
, sparity
->dbitmap
, start
, len
);
2405 static void scrub_block_complete(struct scrub_block
*sblock
)
2409 if (!sblock
->no_io_error_seen
) {
2411 scrub_handle_errored_block(sblock
);
2414 * if has checksum error, write via repair mechanism in
2415 * dev replace case, otherwise write here in dev replace
2418 corrupted
= scrub_checksum(sblock
);
2419 if (!corrupted
&& sblock
->sctx
->is_dev_replace
)
2420 scrub_write_block_to_dev_replace(sblock
);
2423 if (sblock
->sparity
&& corrupted
&& !sblock
->data_corrected
) {
2424 u64 start
= sblock
->pagev
[0]->logical
;
2425 u64 end
= sblock
->pagev
[sblock
->page_count
- 1]->logical
+
2428 scrub_parity_mark_sectors_error(sblock
->sparity
,
2429 start
, end
- start
);
2433 static int scrub_find_csum(struct scrub_ctx
*sctx
, u64 logical
, u8
*csum
)
2435 struct btrfs_ordered_sum
*sum
= NULL
;
2436 unsigned long index
;
2437 unsigned long num_sectors
;
2439 while (!list_empty(&sctx
->csum_list
)) {
2440 sum
= list_first_entry(&sctx
->csum_list
,
2441 struct btrfs_ordered_sum
, list
);
2442 if (sum
->bytenr
> logical
)
2444 if (sum
->bytenr
+ sum
->len
> logical
)
2447 ++sctx
->stat
.csum_discards
;
2448 list_del(&sum
->list
);
2455 index
= div_u64(logical
- sum
->bytenr
, sctx
->fs_info
->sectorsize
);
2456 ASSERT(index
< UINT_MAX
);
2458 num_sectors
= sum
->len
/ sctx
->fs_info
->sectorsize
;
2459 memcpy(csum
, sum
->sums
+ index
, sctx
->csum_size
);
2460 if (index
== num_sectors
- 1) {
2461 list_del(&sum
->list
);
2467 /* scrub extent tries to collect up to 64 kB for each bio */
2468 static int scrub_extent(struct scrub_ctx
*sctx
, struct map_lookup
*map
,
2469 u64 logical
, u64 len
,
2470 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2471 u64 gen
, int mirror_num
, u64 physical_for_dev_replace
)
2474 u8 csum
[BTRFS_CSUM_SIZE
];
2477 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2478 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
2479 blocksize
= map
->stripe_len
;
2481 blocksize
= sctx
->fs_info
->sectorsize
;
2482 spin_lock(&sctx
->stat_lock
);
2483 sctx
->stat
.data_extents_scrubbed
++;
2484 sctx
->stat
.data_bytes_scrubbed
+= len
;
2485 spin_unlock(&sctx
->stat_lock
);
2486 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2487 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
2488 blocksize
= map
->stripe_len
;
2490 blocksize
= sctx
->fs_info
->nodesize
;
2491 spin_lock(&sctx
->stat_lock
);
2492 sctx
->stat
.tree_extents_scrubbed
++;
2493 sctx
->stat
.tree_bytes_scrubbed
+= len
;
2494 spin_unlock(&sctx
->stat_lock
);
2496 blocksize
= sctx
->fs_info
->sectorsize
;
2501 u64 l
= min_t(u64
, len
, blocksize
);
2504 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2505 /* push csums to sbio */
2506 have_csum
= scrub_find_csum(sctx
, logical
, csum
);
2508 ++sctx
->stat
.no_csum
;
2510 ret
= scrub_pages(sctx
, logical
, l
, physical
, dev
, flags
, gen
,
2511 mirror_num
, have_csum
? csum
: NULL
, 0,
2512 physical_for_dev_replace
);
2518 physical_for_dev_replace
+= l
;
2523 static int scrub_pages_for_parity(struct scrub_parity
*sparity
,
2524 u64 logical
, u64 len
,
2525 u64 physical
, struct btrfs_device
*dev
,
2526 u64 flags
, u64 gen
, int mirror_num
, u8
*csum
)
2528 struct scrub_ctx
*sctx
= sparity
->sctx
;
2529 struct scrub_block
*sblock
;
2532 sblock
= kzalloc(sizeof(*sblock
), GFP_KERNEL
);
2534 spin_lock(&sctx
->stat_lock
);
2535 sctx
->stat
.malloc_errors
++;
2536 spin_unlock(&sctx
->stat_lock
);
2540 /* one ref inside this function, plus one for each page added to
2542 refcount_set(&sblock
->refs
, 1);
2543 sblock
->sctx
= sctx
;
2544 sblock
->no_io_error_seen
= 1;
2545 sblock
->sparity
= sparity
;
2546 scrub_parity_get(sparity
);
2548 for (index
= 0; len
> 0; index
++) {
2549 struct scrub_page
*spage
;
2550 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2552 spage
= kzalloc(sizeof(*spage
), GFP_KERNEL
);
2555 spin_lock(&sctx
->stat_lock
);
2556 sctx
->stat
.malloc_errors
++;
2557 spin_unlock(&sctx
->stat_lock
);
2558 scrub_block_put(sblock
);
2561 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2562 /* For scrub block */
2563 scrub_page_get(spage
);
2564 sblock
->pagev
[index
] = spage
;
2565 /* For scrub parity */
2566 scrub_page_get(spage
);
2567 list_add_tail(&spage
->list
, &sparity
->spages
);
2568 spage
->sblock
= sblock
;
2570 spage
->flags
= flags
;
2571 spage
->generation
= gen
;
2572 spage
->logical
= logical
;
2573 spage
->physical
= physical
;
2574 spage
->mirror_num
= mirror_num
;
2576 spage
->have_csum
= 1;
2577 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2579 spage
->have_csum
= 0;
2581 sblock
->page_count
++;
2582 spage
->page
= alloc_page(GFP_KERNEL
);
2590 WARN_ON(sblock
->page_count
== 0);
2591 for (index
= 0; index
< sblock
->page_count
; index
++) {
2592 struct scrub_page
*spage
= sblock
->pagev
[index
];
2595 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2597 scrub_block_put(sblock
);
2602 /* last one frees, either here or in bio completion for last page */
2603 scrub_block_put(sblock
);
2607 static int scrub_extent_for_parity(struct scrub_parity
*sparity
,
2608 u64 logical
, u64 len
,
2609 u64 physical
, struct btrfs_device
*dev
,
2610 u64 flags
, u64 gen
, int mirror_num
)
2612 struct scrub_ctx
*sctx
= sparity
->sctx
;
2614 u8 csum
[BTRFS_CSUM_SIZE
];
2617 if (test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
)) {
2618 scrub_parity_mark_sectors_error(sparity
, logical
, len
);
2622 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2623 blocksize
= sparity
->stripe_len
;
2624 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2625 blocksize
= sparity
->stripe_len
;
2627 blocksize
= sctx
->fs_info
->sectorsize
;
2632 u64 l
= min_t(u64
, len
, blocksize
);
2635 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2636 /* push csums to sbio */
2637 have_csum
= scrub_find_csum(sctx
, logical
, csum
);
2641 ret
= scrub_pages_for_parity(sparity
, logical
, l
, physical
, dev
,
2642 flags
, gen
, mirror_num
,
2643 have_csum
? csum
: NULL
);
2655 * Given a physical address, this will calculate it's
2656 * logical offset. if this is a parity stripe, it will return
2657 * the most left data stripe's logical offset.
2659 * return 0 if it is a data stripe, 1 means parity stripe.
2661 static int get_raid56_logic_offset(u64 physical
, int num
,
2662 struct map_lookup
*map
, u64
*offset
,
2672 last_offset
= (physical
- map
->stripes
[num
].physical
) *
2673 nr_data_stripes(map
);
2675 *stripe_start
= last_offset
;
2677 *offset
= last_offset
;
2678 for (i
= 0; i
< nr_data_stripes(map
); i
++) {
2679 *offset
= last_offset
+ i
* map
->stripe_len
;
2681 stripe_nr
= div64_u64(*offset
, map
->stripe_len
);
2682 stripe_nr
= div_u64(stripe_nr
, nr_data_stripes(map
));
2684 /* Work out the disk rotation on this stripe-set */
2685 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
, &rot
);
2686 /* calculate which stripe this data locates */
2688 stripe_index
= rot
% map
->num_stripes
;
2689 if (stripe_index
== num
)
2691 if (stripe_index
< num
)
2694 *offset
= last_offset
+ j
* map
->stripe_len
;
2698 static void scrub_free_parity(struct scrub_parity
*sparity
)
2700 struct scrub_ctx
*sctx
= sparity
->sctx
;
2701 struct scrub_page
*curr
, *next
;
2704 nbits
= bitmap_weight(sparity
->ebitmap
, sparity
->nsectors
);
2706 spin_lock(&sctx
->stat_lock
);
2707 sctx
->stat
.read_errors
+= nbits
;
2708 sctx
->stat
.uncorrectable_errors
+= nbits
;
2709 spin_unlock(&sctx
->stat_lock
);
2712 list_for_each_entry_safe(curr
, next
, &sparity
->spages
, list
) {
2713 list_del_init(&curr
->list
);
2714 scrub_page_put(curr
);
2720 static void scrub_parity_bio_endio_worker(struct btrfs_work
*work
)
2722 struct scrub_parity
*sparity
= container_of(work
, struct scrub_parity
,
2724 struct scrub_ctx
*sctx
= sparity
->sctx
;
2726 scrub_free_parity(sparity
);
2727 scrub_pending_bio_dec(sctx
);
2730 static void scrub_parity_bio_endio(struct bio
*bio
)
2732 struct scrub_parity
*sparity
= (struct scrub_parity
*)bio
->bi_private
;
2733 struct btrfs_fs_info
*fs_info
= sparity
->sctx
->fs_info
;
2736 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
2741 btrfs_init_work(&sparity
->work
, btrfs_scrubparity_helper
,
2742 scrub_parity_bio_endio_worker
, NULL
, NULL
);
2743 btrfs_queue_work(fs_info
->scrub_parity_workers
, &sparity
->work
);
2746 static void scrub_parity_check_and_repair(struct scrub_parity
*sparity
)
2748 struct scrub_ctx
*sctx
= sparity
->sctx
;
2749 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2751 struct btrfs_raid_bio
*rbio
;
2752 struct btrfs_bio
*bbio
= NULL
;
2756 if (!bitmap_andnot(sparity
->dbitmap
, sparity
->dbitmap
, sparity
->ebitmap
,
2760 length
= sparity
->logic_end
- sparity
->logic_start
;
2762 btrfs_bio_counter_inc_blocked(fs_info
);
2763 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_WRITE
, sparity
->logic_start
,
2765 if (ret
|| !bbio
|| !bbio
->raid_map
)
2768 bio
= btrfs_io_bio_alloc(0);
2769 bio
->bi_iter
.bi_sector
= sparity
->logic_start
>> 9;
2770 bio
->bi_private
= sparity
;
2771 bio
->bi_end_io
= scrub_parity_bio_endio
;
2773 rbio
= raid56_parity_alloc_scrub_rbio(fs_info
, bio
, bbio
,
2774 length
, sparity
->scrub_dev
,
2780 scrub_pending_bio_inc(sctx
);
2781 raid56_parity_submit_scrub_rbio(rbio
);
2787 btrfs_bio_counter_dec(fs_info
);
2788 btrfs_put_bbio(bbio
);
2789 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
2791 spin_lock(&sctx
->stat_lock
);
2792 sctx
->stat
.malloc_errors
++;
2793 spin_unlock(&sctx
->stat_lock
);
2795 scrub_free_parity(sparity
);
2798 static inline int scrub_calc_parity_bitmap_len(int nsectors
)
2800 return DIV_ROUND_UP(nsectors
, BITS_PER_LONG
) * sizeof(long);
2803 static void scrub_parity_get(struct scrub_parity
*sparity
)
2805 refcount_inc(&sparity
->refs
);
2808 static void scrub_parity_put(struct scrub_parity
*sparity
)
2810 if (!refcount_dec_and_test(&sparity
->refs
))
2813 scrub_parity_check_and_repair(sparity
);
2816 static noinline_for_stack
int scrub_raid56_parity(struct scrub_ctx
*sctx
,
2817 struct map_lookup
*map
,
2818 struct btrfs_device
*sdev
,
2819 struct btrfs_path
*path
,
2823 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2824 struct btrfs_root
*root
= fs_info
->extent_root
;
2825 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
2826 struct btrfs_extent_item
*extent
;
2827 struct btrfs_bio
*bbio
= NULL
;
2831 struct extent_buffer
*l
;
2832 struct btrfs_key key
;
2835 u64 extent_physical
;
2838 struct btrfs_device
*extent_dev
;
2839 struct scrub_parity
*sparity
;
2842 int extent_mirror_num
;
2845 nsectors
= div_u64(map
->stripe_len
, fs_info
->sectorsize
);
2846 bitmap_len
= scrub_calc_parity_bitmap_len(nsectors
);
2847 sparity
= kzalloc(sizeof(struct scrub_parity
) + 2 * bitmap_len
,
2850 spin_lock(&sctx
->stat_lock
);
2851 sctx
->stat
.malloc_errors
++;
2852 spin_unlock(&sctx
->stat_lock
);
2856 sparity
->stripe_len
= map
->stripe_len
;
2857 sparity
->nsectors
= nsectors
;
2858 sparity
->sctx
= sctx
;
2859 sparity
->scrub_dev
= sdev
;
2860 sparity
->logic_start
= logic_start
;
2861 sparity
->logic_end
= logic_end
;
2862 refcount_set(&sparity
->refs
, 1);
2863 INIT_LIST_HEAD(&sparity
->spages
);
2864 sparity
->dbitmap
= sparity
->bitmap
;
2865 sparity
->ebitmap
= (void *)sparity
->bitmap
+ bitmap_len
;
2868 while (logic_start
< logic_end
) {
2869 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
2870 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2872 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2873 key
.objectid
= logic_start
;
2874 key
.offset
= (u64
)-1;
2876 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2881 ret
= btrfs_previous_extent_item(root
, path
, 0);
2885 btrfs_release_path(path
);
2886 ret
= btrfs_search_slot(NULL
, root
, &key
,
2898 slot
= path
->slots
[0];
2899 if (slot
>= btrfs_header_nritems(l
)) {
2900 ret
= btrfs_next_leaf(root
, path
);
2909 btrfs_item_key_to_cpu(l
, &key
, slot
);
2911 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
2912 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
2915 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
2916 bytes
= fs_info
->nodesize
;
2920 if (key
.objectid
+ bytes
<= logic_start
)
2923 if (key
.objectid
>= logic_end
) {
2928 while (key
.objectid
>= logic_start
+ map
->stripe_len
)
2929 logic_start
+= map
->stripe_len
;
2931 extent
= btrfs_item_ptr(l
, slot
,
2932 struct btrfs_extent_item
);
2933 flags
= btrfs_extent_flags(l
, extent
);
2934 generation
= btrfs_extent_generation(l
, extent
);
2936 if ((flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) &&
2937 (key
.objectid
< logic_start
||
2938 key
.objectid
+ bytes
>
2939 logic_start
+ map
->stripe_len
)) {
2941 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2942 key
.objectid
, logic_start
);
2943 spin_lock(&sctx
->stat_lock
);
2944 sctx
->stat
.uncorrectable_errors
++;
2945 spin_unlock(&sctx
->stat_lock
);
2949 extent_logical
= key
.objectid
;
2952 if (extent_logical
< logic_start
) {
2953 extent_len
-= logic_start
- extent_logical
;
2954 extent_logical
= logic_start
;
2957 if (extent_logical
+ extent_len
>
2958 logic_start
+ map
->stripe_len
)
2959 extent_len
= logic_start
+ map
->stripe_len
-
2962 scrub_parity_mark_sectors_data(sparity
, extent_logical
,
2965 mapped_length
= extent_len
;
2967 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_READ
,
2968 extent_logical
, &mapped_length
, &bbio
,
2971 if (!bbio
|| mapped_length
< extent_len
)
2975 btrfs_put_bbio(bbio
);
2978 extent_physical
= bbio
->stripes
[0].physical
;
2979 extent_mirror_num
= bbio
->mirror_num
;
2980 extent_dev
= bbio
->stripes
[0].dev
;
2981 btrfs_put_bbio(bbio
);
2983 ret
= btrfs_lookup_csums_range(csum_root
,
2985 extent_logical
+ extent_len
- 1,
2986 &sctx
->csum_list
, 1);
2990 ret
= scrub_extent_for_parity(sparity
, extent_logical
,
2997 scrub_free_csums(sctx
);
3002 if (extent_logical
+ extent_len
<
3003 key
.objectid
+ bytes
) {
3004 logic_start
+= map
->stripe_len
;
3006 if (logic_start
>= logic_end
) {
3011 if (logic_start
< key
.objectid
+ bytes
) {
3020 btrfs_release_path(path
);
3025 logic_start
+= map
->stripe_len
;
3029 scrub_parity_mark_sectors_error(sparity
, logic_start
,
3030 logic_end
- logic_start
);
3031 scrub_parity_put(sparity
);
3033 mutex_lock(&sctx
->wr_lock
);
3034 scrub_wr_submit(sctx
);
3035 mutex_unlock(&sctx
->wr_lock
);
3037 btrfs_release_path(path
);
3038 return ret
< 0 ? ret
: 0;
3041 static noinline_for_stack
int scrub_stripe(struct scrub_ctx
*sctx
,
3042 struct map_lookup
*map
,
3043 struct btrfs_device
*scrub_dev
,
3044 int num
, u64 base
, u64 length
)
3046 struct btrfs_path
*path
, *ppath
;
3047 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3048 struct btrfs_root
*root
= fs_info
->extent_root
;
3049 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
3050 struct btrfs_extent_item
*extent
;
3051 struct blk_plug plug
;
3056 struct extent_buffer
*l
;
3063 struct reada_control
*reada1
;
3064 struct reada_control
*reada2
;
3065 struct btrfs_key key
;
3066 struct btrfs_key key_end
;
3067 u64 increment
= map
->stripe_len
;
3070 u64 extent_physical
;
3074 struct btrfs_device
*extent_dev
;
3075 int extent_mirror_num
;
3078 physical
= map
->stripes
[num
].physical
;
3080 nstripes
= div64_u64(length
, map
->stripe_len
);
3081 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3082 offset
= map
->stripe_len
* num
;
3083 increment
= map
->stripe_len
* map
->num_stripes
;
3085 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3086 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3087 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
3088 increment
= map
->stripe_len
* factor
;
3089 mirror_num
= num
% map
->sub_stripes
+ 1;
3090 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3091 increment
= map
->stripe_len
;
3092 mirror_num
= num
% map
->num_stripes
+ 1;
3093 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3094 increment
= map
->stripe_len
;
3095 mirror_num
= num
% map
->num_stripes
+ 1;
3096 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3097 get_raid56_logic_offset(physical
, num
, map
, &offset
, NULL
);
3098 increment
= map
->stripe_len
* nr_data_stripes(map
);
3101 increment
= map
->stripe_len
;
3105 path
= btrfs_alloc_path();
3109 ppath
= btrfs_alloc_path();
3111 btrfs_free_path(path
);
3116 * work on commit root. The related disk blocks are static as
3117 * long as COW is applied. This means, it is save to rewrite
3118 * them to repair disk errors without any race conditions
3120 path
->search_commit_root
= 1;
3121 path
->skip_locking
= 1;
3123 ppath
->search_commit_root
= 1;
3124 ppath
->skip_locking
= 1;
3126 * trigger the readahead for extent tree csum tree and wait for
3127 * completion. During readahead, the scrub is officially paused
3128 * to not hold off transaction commits
3130 logical
= base
+ offset
;
3131 physical_end
= physical
+ nstripes
* map
->stripe_len
;
3132 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3133 get_raid56_logic_offset(physical_end
, num
,
3134 map
, &logic_end
, NULL
);
3137 logic_end
= logical
+ increment
* nstripes
;
3139 wait_event(sctx
->list_wait
,
3140 atomic_read(&sctx
->bios_in_flight
) == 0);
3141 scrub_blocked_if_needed(fs_info
);
3143 /* FIXME it might be better to start readahead at commit root */
3144 key
.objectid
= logical
;
3145 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3146 key
.offset
= (u64
)0;
3147 key_end
.objectid
= logic_end
;
3148 key_end
.type
= BTRFS_METADATA_ITEM_KEY
;
3149 key_end
.offset
= (u64
)-1;
3150 reada1
= btrfs_reada_add(root
, &key
, &key_end
);
3152 key
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3153 key
.type
= BTRFS_EXTENT_CSUM_KEY
;
3154 key
.offset
= logical
;
3155 key_end
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3156 key_end
.type
= BTRFS_EXTENT_CSUM_KEY
;
3157 key_end
.offset
= logic_end
;
3158 reada2
= btrfs_reada_add(csum_root
, &key
, &key_end
);
3160 if (!IS_ERR(reada1
))
3161 btrfs_reada_wait(reada1
);
3162 if (!IS_ERR(reada2
))
3163 btrfs_reada_wait(reada2
);
3167 * collect all data csums for the stripe to avoid seeking during
3168 * the scrub. This might currently (crc32) end up to be about 1MB
3170 blk_start_plug(&plug
);
3173 * now find all extents for each stripe and scrub them
3176 while (physical
< physical_end
) {
3180 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
3181 atomic_read(&sctx
->cancel_req
)) {
3186 * check to see if we have to pause
3188 if (atomic_read(&fs_info
->scrub_pause_req
)) {
3189 /* push queued extents */
3190 sctx
->flush_all_writes
= true;
3192 mutex_lock(&sctx
->wr_lock
);
3193 scrub_wr_submit(sctx
);
3194 mutex_unlock(&sctx
->wr_lock
);
3195 wait_event(sctx
->list_wait
,
3196 atomic_read(&sctx
->bios_in_flight
) == 0);
3197 sctx
->flush_all_writes
= false;
3198 scrub_blocked_if_needed(fs_info
);
3201 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3202 ret
= get_raid56_logic_offset(physical
, num
, map
,
3207 /* it is parity strip */
3208 stripe_logical
+= base
;
3209 stripe_end
= stripe_logical
+ increment
;
3210 ret
= scrub_raid56_parity(sctx
, map
, scrub_dev
,
3211 ppath
, stripe_logical
,
3219 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
3220 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3222 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3223 key
.objectid
= logical
;
3224 key
.offset
= (u64
)-1;
3226 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3231 ret
= btrfs_previous_extent_item(root
, path
, 0);
3235 /* there's no smaller item, so stick with the
3237 btrfs_release_path(path
);
3238 ret
= btrfs_search_slot(NULL
, root
, &key
,
3250 slot
= path
->slots
[0];
3251 if (slot
>= btrfs_header_nritems(l
)) {
3252 ret
= btrfs_next_leaf(root
, path
);
3261 btrfs_item_key_to_cpu(l
, &key
, slot
);
3263 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3264 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
3267 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
3268 bytes
= fs_info
->nodesize
;
3272 if (key
.objectid
+ bytes
<= logical
)
3275 if (key
.objectid
>= logical
+ map
->stripe_len
) {
3276 /* out of this device extent */
3277 if (key
.objectid
>= logic_end
)
3282 extent
= btrfs_item_ptr(l
, slot
,
3283 struct btrfs_extent_item
);
3284 flags
= btrfs_extent_flags(l
, extent
);
3285 generation
= btrfs_extent_generation(l
, extent
);
3287 if ((flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) &&
3288 (key
.objectid
< logical
||
3289 key
.objectid
+ bytes
>
3290 logical
+ map
->stripe_len
)) {
3292 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3293 key
.objectid
, logical
);
3294 spin_lock(&sctx
->stat_lock
);
3295 sctx
->stat
.uncorrectable_errors
++;
3296 spin_unlock(&sctx
->stat_lock
);
3301 extent_logical
= key
.objectid
;
3305 * trim extent to this stripe
3307 if (extent_logical
< logical
) {
3308 extent_len
-= logical
- extent_logical
;
3309 extent_logical
= logical
;
3311 if (extent_logical
+ extent_len
>
3312 logical
+ map
->stripe_len
) {
3313 extent_len
= logical
+ map
->stripe_len
-
3317 extent_physical
= extent_logical
- logical
+ physical
;
3318 extent_dev
= scrub_dev
;
3319 extent_mirror_num
= mirror_num
;
3320 if (sctx
->is_dev_replace
)
3321 scrub_remap_extent(fs_info
, extent_logical
,
3322 extent_len
, &extent_physical
,
3324 &extent_mirror_num
);
3326 ret
= btrfs_lookup_csums_range(csum_root
,
3330 &sctx
->csum_list
, 1);
3334 ret
= scrub_extent(sctx
, map
, extent_logical
, extent_len
,
3335 extent_physical
, extent_dev
, flags
,
3336 generation
, extent_mirror_num
,
3337 extent_logical
- logical
+ physical
);
3339 scrub_free_csums(sctx
);
3344 if (extent_logical
+ extent_len
<
3345 key
.objectid
+ bytes
) {
3346 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3348 * loop until we find next data stripe
3349 * or we have finished all stripes.
3352 physical
+= map
->stripe_len
;
3353 ret
= get_raid56_logic_offset(physical
,
3358 if (ret
&& physical
< physical_end
) {
3359 stripe_logical
+= base
;
3360 stripe_end
= stripe_logical
+
3362 ret
= scrub_raid56_parity(sctx
,
3363 map
, scrub_dev
, ppath
,
3371 physical
+= map
->stripe_len
;
3372 logical
+= increment
;
3374 if (logical
< key
.objectid
+ bytes
) {
3379 if (physical
>= physical_end
) {
3387 btrfs_release_path(path
);
3389 logical
+= increment
;
3390 physical
+= map
->stripe_len
;
3391 spin_lock(&sctx
->stat_lock
);
3393 sctx
->stat
.last_physical
= map
->stripes
[num
].physical
+
3396 sctx
->stat
.last_physical
= physical
;
3397 spin_unlock(&sctx
->stat_lock
);
3402 /* push queued extents */
3404 mutex_lock(&sctx
->wr_lock
);
3405 scrub_wr_submit(sctx
);
3406 mutex_unlock(&sctx
->wr_lock
);
3408 blk_finish_plug(&plug
);
3409 btrfs_free_path(path
);
3410 btrfs_free_path(ppath
);
3411 return ret
< 0 ? ret
: 0;
3414 static noinline_for_stack
int scrub_chunk(struct scrub_ctx
*sctx
,
3415 struct btrfs_device
*scrub_dev
,
3416 u64 chunk_offset
, u64 length
,
3418 struct btrfs_block_group_cache
*cache
)
3420 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3421 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3422 struct map_lookup
*map
;
3423 struct extent_map
*em
;
3427 read_lock(&map_tree
->map_tree
.lock
);
3428 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3429 read_unlock(&map_tree
->map_tree
.lock
);
3433 * Might have been an unused block group deleted by the cleaner
3434 * kthread or relocation.
3436 spin_lock(&cache
->lock
);
3437 if (!cache
->removed
)
3439 spin_unlock(&cache
->lock
);
3444 map
= em
->map_lookup
;
3445 if (em
->start
!= chunk_offset
)
3448 if (em
->len
< length
)
3451 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3452 if (map
->stripes
[i
].dev
->bdev
== scrub_dev
->bdev
&&
3453 map
->stripes
[i
].physical
== dev_offset
) {
3454 ret
= scrub_stripe(sctx
, map
, scrub_dev
, i
,
3455 chunk_offset
, length
);
3461 free_extent_map(em
);
3466 static noinline_for_stack
3467 int scrub_enumerate_chunks(struct scrub_ctx
*sctx
,
3468 struct btrfs_device
*scrub_dev
, u64 start
, u64 end
)
3470 struct btrfs_dev_extent
*dev_extent
= NULL
;
3471 struct btrfs_path
*path
;
3472 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3473 struct btrfs_root
*root
= fs_info
->dev_root
;
3479 struct extent_buffer
*l
;
3480 struct btrfs_key key
;
3481 struct btrfs_key found_key
;
3482 struct btrfs_block_group_cache
*cache
;
3483 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
3485 path
= btrfs_alloc_path();
3489 path
->reada
= READA_FORWARD
;
3490 path
->search_commit_root
= 1;
3491 path
->skip_locking
= 1;
3493 key
.objectid
= scrub_dev
->devid
;
3495 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3498 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3502 if (path
->slots
[0] >=
3503 btrfs_header_nritems(path
->nodes
[0])) {
3504 ret
= btrfs_next_leaf(root
, path
);
3517 slot
= path
->slots
[0];
3519 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
3521 if (found_key
.objectid
!= scrub_dev
->devid
)
3524 if (found_key
.type
!= BTRFS_DEV_EXTENT_KEY
)
3527 if (found_key
.offset
>= end
)
3530 if (found_key
.offset
< key
.offset
)
3533 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3534 length
= btrfs_dev_extent_length(l
, dev_extent
);
3536 if (found_key
.offset
+ length
<= start
)
3539 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3542 * get a reference on the corresponding block group to prevent
3543 * the chunk from going away while we scrub it
3545 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3547 /* some chunks are removed but not committed to disk yet,
3548 * continue scrubbing */
3553 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3554 * to avoid deadlock caused by:
3555 * btrfs_inc_block_group_ro()
3556 * -> btrfs_wait_for_commit()
3557 * -> btrfs_commit_transaction()
3558 * -> btrfs_scrub_pause()
3560 scrub_pause_on(fs_info
);
3561 ret
= btrfs_inc_block_group_ro(cache
);
3562 if (!ret
&& sctx
->is_dev_replace
) {
3564 * If we are doing a device replace wait for any tasks
3565 * that started dellaloc right before we set the block
3566 * group to RO mode, as they might have just allocated
3567 * an extent from it or decided they could do a nocow
3568 * write. And if any such tasks did that, wait for their
3569 * ordered extents to complete and then commit the
3570 * current transaction, so that we can later see the new
3571 * extent items in the extent tree - the ordered extents
3572 * create delayed data references (for cow writes) when
3573 * they complete, which will be run and insert the
3574 * corresponding extent items into the extent tree when
3575 * we commit the transaction they used when running
3576 * inode.c:btrfs_finish_ordered_io(). We later use
3577 * the commit root of the extent tree to find extents
3578 * to copy from the srcdev into the tgtdev, and we don't
3579 * want to miss any new extents.
3581 btrfs_wait_block_group_reservations(cache
);
3582 btrfs_wait_nocow_writers(cache
);
3583 ret
= btrfs_wait_ordered_roots(fs_info
, U64_MAX
,
3584 cache
->key
.objectid
,
3587 struct btrfs_trans_handle
*trans
;
3589 trans
= btrfs_join_transaction(root
);
3591 ret
= PTR_ERR(trans
);
3593 ret
= btrfs_commit_transaction(trans
);
3595 scrub_pause_off(fs_info
);
3596 btrfs_put_block_group(cache
);
3601 scrub_pause_off(fs_info
);
3605 } else if (ret
== -ENOSPC
) {
3607 * btrfs_inc_block_group_ro return -ENOSPC when it
3608 * failed in creating new chunk for metadata.
3609 * It is not a problem for scrub/replace, because
3610 * metadata are always cowed, and our scrub paused
3611 * commit_transactions.
3616 "failed setting block group ro: %d", ret
);
3617 btrfs_put_block_group(cache
);
3621 btrfs_dev_replace_write_lock(&fs_info
->dev_replace
);
3622 dev_replace
->cursor_right
= found_key
.offset
+ length
;
3623 dev_replace
->cursor_left
= found_key
.offset
;
3624 dev_replace
->item_needs_writeback
= 1;
3625 btrfs_dev_replace_write_unlock(&fs_info
->dev_replace
);
3626 ret
= scrub_chunk(sctx
, scrub_dev
, chunk_offset
, length
,
3627 found_key
.offset
, cache
);
3630 * flush, submit all pending read and write bios, afterwards
3632 * Note that in the dev replace case, a read request causes
3633 * write requests that are submitted in the read completion
3634 * worker. Therefore in the current situation, it is required
3635 * that all write requests are flushed, so that all read and
3636 * write requests are really completed when bios_in_flight
3639 sctx
->flush_all_writes
= true;
3641 mutex_lock(&sctx
->wr_lock
);
3642 scrub_wr_submit(sctx
);
3643 mutex_unlock(&sctx
->wr_lock
);
3645 wait_event(sctx
->list_wait
,
3646 atomic_read(&sctx
->bios_in_flight
) == 0);
3648 scrub_pause_on(fs_info
);
3651 * must be called before we decrease @scrub_paused.
3652 * make sure we don't block transaction commit while
3653 * we are waiting pending workers finished.
3655 wait_event(sctx
->list_wait
,
3656 atomic_read(&sctx
->workers_pending
) == 0);
3657 sctx
->flush_all_writes
= false;
3659 scrub_pause_off(fs_info
);
3661 btrfs_dev_replace_write_lock(&fs_info
->dev_replace
);
3662 dev_replace
->cursor_left
= dev_replace
->cursor_right
;
3663 dev_replace
->item_needs_writeback
= 1;
3664 btrfs_dev_replace_write_unlock(&fs_info
->dev_replace
);
3667 btrfs_dec_block_group_ro(cache
);
3670 * We might have prevented the cleaner kthread from deleting
3671 * this block group if it was already unused because we raced
3672 * and set it to RO mode first. So add it back to the unused
3673 * list, otherwise it might not ever be deleted unless a manual
3674 * balance is triggered or it becomes used and unused again.
3676 spin_lock(&cache
->lock
);
3677 if (!cache
->removed
&& !cache
->ro
&& cache
->reserved
== 0 &&
3678 btrfs_block_group_used(&cache
->item
) == 0) {
3679 spin_unlock(&cache
->lock
);
3680 btrfs_mark_bg_unused(cache
);
3682 spin_unlock(&cache
->lock
);
3685 btrfs_put_block_group(cache
);
3688 if (sctx
->is_dev_replace
&&
3689 atomic64_read(&dev_replace
->num_write_errors
) > 0) {
3693 if (sctx
->stat
.malloc_errors
> 0) {
3698 key
.offset
= found_key
.offset
+ length
;
3699 btrfs_release_path(path
);
3702 btrfs_free_path(path
);
3707 static noinline_for_stack
int scrub_supers(struct scrub_ctx
*sctx
,
3708 struct btrfs_device
*scrub_dev
)
3714 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3716 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
3719 /* Seed devices of a new filesystem has their own generation. */
3720 if (scrub_dev
->fs_devices
!= fs_info
->fs_devices
)
3721 gen
= scrub_dev
->generation
;
3723 gen
= fs_info
->last_trans_committed
;
3725 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
3726 bytenr
= btrfs_sb_offset(i
);
3727 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>
3728 scrub_dev
->commit_total_bytes
)
3731 ret
= scrub_pages(sctx
, bytenr
, BTRFS_SUPER_INFO_SIZE
, bytenr
,
3732 scrub_dev
, BTRFS_EXTENT_FLAG_SUPER
, gen
, i
,
3737 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
3743 * get a reference count on fs_info->scrub_workers. start worker if necessary
3745 static noinline_for_stack
int scrub_workers_get(struct btrfs_fs_info
*fs_info
,
3748 unsigned int flags
= WQ_FREEZABLE
| WQ_UNBOUND
;
3749 int max_active
= fs_info
->thread_pool_size
;
3751 if (fs_info
->scrub_workers_refcnt
== 0) {
3752 fs_info
->scrub_workers
= btrfs_alloc_workqueue(fs_info
, "scrub",
3753 flags
, is_dev_replace
? 1 : max_active
, 4);
3754 if (!fs_info
->scrub_workers
)
3755 goto fail_scrub_workers
;
3757 fs_info
->scrub_wr_completion_workers
=
3758 btrfs_alloc_workqueue(fs_info
, "scrubwrc", flags
,
3760 if (!fs_info
->scrub_wr_completion_workers
)
3761 goto fail_scrub_wr_completion_workers
;
3763 fs_info
->scrub_parity_workers
=
3764 btrfs_alloc_workqueue(fs_info
, "scrubparity", flags
,
3766 if (!fs_info
->scrub_parity_workers
)
3767 goto fail_scrub_parity_workers
;
3769 ++fs_info
->scrub_workers_refcnt
;
3772 fail_scrub_parity_workers
:
3773 btrfs_destroy_workqueue(fs_info
->scrub_wr_completion_workers
);
3774 fail_scrub_wr_completion_workers
:
3775 btrfs_destroy_workqueue(fs_info
->scrub_workers
);
3780 int btrfs_scrub_dev(struct btrfs_fs_info
*fs_info
, u64 devid
, u64 start
,
3781 u64 end
, struct btrfs_scrub_progress
*progress
,
3782 int readonly
, int is_dev_replace
)
3784 struct scrub_ctx
*sctx
;
3786 struct btrfs_device
*dev
;
3787 unsigned int nofs_flag
;
3788 struct btrfs_workqueue
*scrub_workers
= NULL
;
3789 struct btrfs_workqueue
*scrub_wr_comp
= NULL
;
3790 struct btrfs_workqueue
*scrub_parity
= NULL
;
3792 if (btrfs_fs_closing(fs_info
))
3795 if (fs_info
->nodesize
> BTRFS_STRIPE_LEN
) {
3797 * in this case scrub is unable to calculate the checksum
3798 * the way scrub is implemented. Do not handle this
3799 * situation at all because it won't ever happen.
3802 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
3808 if (fs_info
->sectorsize
!= PAGE_SIZE
) {
3809 /* not supported for data w/o checksums */
3810 btrfs_err_rl(fs_info
,
3811 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
3812 fs_info
->sectorsize
, PAGE_SIZE
);
3816 if (fs_info
->nodesize
>
3817 PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
||
3818 fs_info
->sectorsize
> PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
) {
3820 * would exhaust the array bounds of pagev member in
3821 * struct scrub_block
3824 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
3826 SCRUB_MAX_PAGES_PER_BLOCK
,
3827 fs_info
->sectorsize
,
3828 SCRUB_MAX_PAGES_PER_BLOCK
);
3832 /* Allocate outside of device_list_mutex */
3833 sctx
= scrub_setup_ctx(fs_info
, is_dev_replace
);
3835 return PTR_ERR(sctx
);
3837 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
3838 dev
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
3839 if (!dev
|| (test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
) &&
3841 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3846 if (!is_dev_replace
&& !readonly
&&
3847 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
)) {
3848 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3849 btrfs_err_in_rcu(fs_info
, "scrub: device %s is not writable",
3850 rcu_str_deref(dev
->name
));
3855 mutex_lock(&fs_info
->scrub_lock
);
3856 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &dev
->dev_state
) ||
3857 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &dev
->dev_state
)) {
3858 mutex_unlock(&fs_info
->scrub_lock
);
3859 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3864 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
3865 if (dev
->scrub_ctx
||
3867 btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))) {
3868 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
3869 mutex_unlock(&fs_info
->scrub_lock
);
3870 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3874 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
3876 ret
= scrub_workers_get(fs_info
, is_dev_replace
);
3878 mutex_unlock(&fs_info
->scrub_lock
);
3879 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3883 sctx
->readonly
= readonly
;
3884 dev
->scrub_ctx
= sctx
;
3885 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3888 * checking @scrub_pause_req here, we can avoid
3889 * race between committing transaction and scrubbing.
3891 __scrub_blocked_if_needed(fs_info
);
3892 atomic_inc(&fs_info
->scrubs_running
);
3893 mutex_unlock(&fs_info
->scrub_lock
);
3896 * In order to avoid deadlock with reclaim when there is a transaction
3897 * trying to pause scrub, make sure we use GFP_NOFS for all the
3898 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
3899 * invoked by our callees. The pausing request is done when the
3900 * transaction commit starts, and it blocks the transaction until scrub
3901 * is paused (done at specific points at scrub_stripe() or right above
3902 * before incrementing fs_info->scrubs_running).
3904 nofs_flag
= memalloc_nofs_save();
3905 if (!is_dev_replace
) {
3907 * by holding device list mutex, we can
3908 * kick off writing super in log tree sync.
3910 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
3911 ret
= scrub_supers(sctx
, dev
);
3912 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3916 ret
= scrub_enumerate_chunks(sctx
, dev
, start
, end
);
3917 memalloc_nofs_restore(nofs_flag
);
3919 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
3920 atomic_dec(&fs_info
->scrubs_running
);
3921 wake_up(&fs_info
->scrub_pause_wait
);
3923 wait_event(sctx
->list_wait
, atomic_read(&sctx
->workers_pending
) == 0);
3926 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
3928 mutex_lock(&fs_info
->scrub_lock
);
3929 dev
->scrub_ctx
= NULL
;
3930 if (--fs_info
->scrub_workers_refcnt
== 0) {
3931 scrub_workers
= fs_info
->scrub_workers
;
3932 scrub_wr_comp
= fs_info
->scrub_wr_completion_workers
;
3933 scrub_parity
= fs_info
->scrub_parity_workers
;
3935 mutex_unlock(&fs_info
->scrub_lock
);
3937 btrfs_destroy_workqueue(scrub_workers
);
3938 btrfs_destroy_workqueue(scrub_wr_comp
);
3939 btrfs_destroy_workqueue(scrub_parity
);
3940 scrub_put_ctx(sctx
);
3945 scrub_free_ctx(sctx
);
3950 void btrfs_scrub_pause(struct btrfs_fs_info
*fs_info
)
3952 mutex_lock(&fs_info
->scrub_lock
);
3953 atomic_inc(&fs_info
->scrub_pause_req
);
3954 while (atomic_read(&fs_info
->scrubs_paused
) !=
3955 atomic_read(&fs_info
->scrubs_running
)) {
3956 mutex_unlock(&fs_info
->scrub_lock
);
3957 wait_event(fs_info
->scrub_pause_wait
,
3958 atomic_read(&fs_info
->scrubs_paused
) ==
3959 atomic_read(&fs_info
->scrubs_running
));
3960 mutex_lock(&fs_info
->scrub_lock
);
3962 mutex_unlock(&fs_info
->scrub_lock
);
3965 void btrfs_scrub_continue(struct btrfs_fs_info
*fs_info
)
3967 atomic_dec(&fs_info
->scrub_pause_req
);
3968 wake_up(&fs_info
->scrub_pause_wait
);
3971 int btrfs_scrub_cancel(struct btrfs_fs_info
*fs_info
)
3973 mutex_lock(&fs_info
->scrub_lock
);
3974 if (!atomic_read(&fs_info
->scrubs_running
)) {
3975 mutex_unlock(&fs_info
->scrub_lock
);
3979 atomic_inc(&fs_info
->scrub_cancel_req
);
3980 while (atomic_read(&fs_info
->scrubs_running
)) {
3981 mutex_unlock(&fs_info
->scrub_lock
);
3982 wait_event(fs_info
->scrub_pause_wait
,
3983 atomic_read(&fs_info
->scrubs_running
) == 0);
3984 mutex_lock(&fs_info
->scrub_lock
);
3986 atomic_dec(&fs_info
->scrub_cancel_req
);
3987 mutex_unlock(&fs_info
->scrub_lock
);
3992 int btrfs_scrub_cancel_dev(struct btrfs_fs_info
*fs_info
,
3993 struct btrfs_device
*dev
)
3995 struct scrub_ctx
*sctx
;
3997 mutex_lock(&fs_info
->scrub_lock
);
3998 sctx
= dev
->scrub_ctx
;
4000 mutex_unlock(&fs_info
->scrub_lock
);
4003 atomic_inc(&sctx
->cancel_req
);
4004 while (dev
->scrub_ctx
) {
4005 mutex_unlock(&fs_info
->scrub_lock
);
4006 wait_event(fs_info
->scrub_pause_wait
,
4007 dev
->scrub_ctx
== NULL
);
4008 mutex_lock(&fs_info
->scrub_lock
);
4010 mutex_unlock(&fs_info
->scrub_lock
);
4015 int btrfs_scrub_progress(struct btrfs_fs_info
*fs_info
, u64 devid
,
4016 struct btrfs_scrub_progress
*progress
)
4018 struct btrfs_device
*dev
;
4019 struct scrub_ctx
*sctx
= NULL
;
4021 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4022 dev
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
4024 sctx
= dev
->scrub_ctx
;
4026 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
4027 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4029 return dev
? (sctx
? 0 : -ENOTCONN
) : -ENODEV
;
4032 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
4033 u64 extent_logical
, u64 extent_len
,
4034 u64
*extent_physical
,
4035 struct btrfs_device
**extent_dev
,
4036 int *extent_mirror_num
)
4039 struct btrfs_bio
*bbio
= NULL
;
4042 mapped_length
= extent_len
;
4043 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_READ
, extent_logical
,
4044 &mapped_length
, &bbio
, 0);
4045 if (ret
|| !bbio
|| mapped_length
< extent_len
||
4046 !bbio
->stripes
[0].dev
->bdev
) {
4047 btrfs_put_bbio(bbio
);
4051 *extent_physical
= bbio
->stripes
[0].physical
;
4052 *extent_mirror_num
= bbio
->mirror_num
;
4053 *extent_dev
= bbio
->stripes
[0].dev
;
4054 btrfs_put_bbio(bbio
);