2 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/blkdev.h>
20 #include <linux/ratelimit.h>
21 #include <linux/sched/mm.h>
25 #include "ordered-data.h"
26 #include "transaction.h"
28 #include "extent_io.h"
29 #include "dev-replace.h"
30 #include "check-integrity.h"
31 #include "rcu-string.h"
35 * This is only the first step towards a full-features scrub. It reads all
36 * extent and super block and verifies the checksums. In case a bad checksum
37 * is found or the extent cannot be read, good data will be written back if
40 * Future enhancements:
41 * - In case an unrepairable extent is encountered, track which files are
42 * affected and report them
43 * - track and record media errors, throw out bad devices
44 * - add a mode to also read unallocated space
51 * the following three values only influence the performance.
52 * The last one configures the number of parallel and outstanding I/O
53 * operations. The first two values configure an upper limit for the number
54 * of (dynamically allocated) pages that are added to a bio.
56 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
57 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
58 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
61 * the following value times PAGE_SIZE needs to be large enough to match the
62 * largest node/leaf/sector size that shall be supported.
63 * Values larger than BTRFS_STRIPE_LEN are not supported.
65 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
67 struct scrub_recover
{
69 struct btrfs_bio
*bbio
;
74 struct scrub_block
*sblock
;
76 struct btrfs_device
*dev
;
77 struct list_head list
;
78 u64 flags
; /* extent flags */
82 u64 physical_for_dev_replace
;
85 unsigned int mirror_num
:8;
86 unsigned int have_csum
:1;
87 unsigned int io_error
:1;
89 u8 csum
[BTRFS_CSUM_SIZE
];
91 struct scrub_recover
*recover
;
96 struct scrub_ctx
*sctx
;
97 struct btrfs_device
*dev
;
102 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
103 struct scrub_page
*pagev
[SCRUB_PAGES_PER_WR_BIO
];
105 struct scrub_page
*pagev
[SCRUB_PAGES_PER_RD_BIO
];
109 struct btrfs_work work
;
113 struct scrub_page
*pagev
[SCRUB_MAX_PAGES_PER_BLOCK
];
115 atomic_t outstanding_pages
;
116 refcount_t refs
; /* free mem on transition to zero */
117 struct scrub_ctx
*sctx
;
118 struct scrub_parity
*sparity
;
120 unsigned int header_error
:1;
121 unsigned int checksum_error
:1;
122 unsigned int no_io_error_seen
:1;
123 unsigned int generation_error
:1; /* also sets header_error */
125 /* The following is for the data used to check parity */
126 /* It is for the data with checksum */
127 unsigned int data_corrected
:1;
129 struct btrfs_work work
;
132 /* Used for the chunks with parity stripe such RAID5/6 */
133 struct scrub_parity
{
134 struct scrub_ctx
*sctx
;
136 struct btrfs_device
*scrub_dev
;
148 struct list_head spages
;
150 /* Work of parity check and repair */
151 struct btrfs_work work
;
153 /* Mark the parity blocks which have data */
154 unsigned long *dbitmap
;
157 * Mark the parity blocks which have data, but errors happen when
158 * read data or check data
160 unsigned long *ebitmap
;
162 unsigned long bitmap
[0];
166 struct scrub_bio
*bios
[SCRUB_BIOS_PER_SCTX
];
167 struct btrfs_fs_info
*fs_info
;
170 atomic_t bios_in_flight
;
171 atomic_t workers_pending
;
172 spinlock_t list_lock
;
173 wait_queue_head_t list_wait
;
175 struct list_head csum_list
;
178 int pages_per_rd_bio
;
182 struct scrub_bio
*wr_curr_bio
;
183 struct mutex wr_lock
;
184 int pages_per_wr_bio
; /* <= SCRUB_PAGES_PER_WR_BIO */
185 struct btrfs_device
*wr_tgtdev
;
186 bool flush_all_writes
;
191 struct btrfs_scrub_progress stat
;
192 spinlock_t stat_lock
;
195 * Use a ref counter to avoid use-after-free issues. Scrub workers
196 * decrement bios_in_flight and workers_pending and then do a wakeup
197 * on the list_wait wait queue. We must ensure the main scrub task
198 * doesn't free the scrub context before or while the workers are
199 * doing the wakeup() call.
204 struct scrub_fixup_nodatasum
{
205 struct scrub_ctx
*sctx
;
206 struct btrfs_device
*dev
;
208 struct btrfs_root
*root
;
209 struct btrfs_work work
;
213 struct scrub_nocow_inode
{
217 struct list_head list
;
220 struct scrub_copy_nocow_ctx
{
221 struct scrub_ctx
*sctx
;
225 u64 physical_for_dev_replace
;
226 struct list_head inodes
;
227 struct btrfs_work work
;
230 struct scrub_warning
{
231 struct btrfs_path
*path
;
232 u64 extent_item_size
;
236 struct btrfs_device
*dev
;
239 struct full_stripe_lock
{
246 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
);
247 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
);
248 static void scrub_pending_trans_workers_inc(struct scrub_ctx
*sctx
);
249 static void scrub_pending_trans_workers_dec(struct scrub_ctx
*sctx
);
250 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
);
251 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
252 struct scrub_block
*sblocks_for_recheck
);
253 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
254 struct scrub_block
*sblock
,
255 int retry_failed_mirror
);
256 static void scrub_recheck_block_checksum(struct scrub_block
*sblock
);
257 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
258 struct scrub_block
*sblock_good
);
259 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
260 struct scrub_block
*sblock_good
,
261 int page_num
, int force_write
);
262 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
);
263 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
265 static int scrub_checksum_data(struct scrub_block
*sblock
);
266 static int scrub_checksum_tree_block(struct scrub_block
*sblock
);
267 static int scrub_checksum_super(struct scrub_block
*sblock
);
268 static void scrub_block_get(struct scrub_block
*sblock
);
269 static void scrub_block_put(struct scrub_block
*sblock
);
270 static void scrub_page_get(struct scrub_page
*spage
);
271 static void scrub_page_put(struct scrub_page
*spage
);
272 static void scrub_parity_get(struct scrub_parity
*sparity
);
273 static void scrub_parity_put(struct scrub_parity
*sparity
);
274 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
275 struct scrub_page
*spage
);
276 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
277 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
278 u64 gen
, int mirror_num
, u8
*csum
, int force
,
279 u64 physical_for_dev_replace
);
280 static void scrub_bio_end_io(struct bio
*bio
);
281 static void scrub_bio_end_io_worker(struct btrfs_work
*work
);
282 static void scrub_block_complete(struct scrub_block
*sblock
);
283 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
284 u64 extent_logical
, u64 extent_len
,
285 u64
*extent_physical
,
286 struct btrfs_device
**extent_dev
,
287 int *extent_mirror_num
);
288 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
289 struct scrub_page
*spage
);
290 static void scrub_wr_submit(struct scrub_ctx
*sctx
);
291 static void scrub_wr_bio_end_io(struct bio
*bio
);
292 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
);
293 static int write_page_nocow(struct scrub_ctx
*sctx
,
294 u64 physical_for_dev_replace
, struct page
*page
);
295 static int copy_nocow_pages_for_inode(u64 inum
, u64 offset
, u64 root
,
296 struct scrub_copy_nocow_ctx
*ctx
);
297 static int copy_nocow_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
298 int mirror_num
, u64 physical_for_dev_replace
);
299 static void copy_nocow_pages_worker(struct btrfs_work
*work
);
300 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
301 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
302 static void scrub_put_ctx(struct scrub_ctx
*sctx
);
305 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
)
307 refcount_inc(&sctx
->refs
);
308 atomic_inc(&sctx
->bios_in_flight
);
311 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
)
313 atomic_dec(&sctx
->bios_in_flight
);
314 wake_up(&sctx
->list_wait
);
318 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
320 while (atomic_read(&fs_info
->scrub_pause_req
)) {
321 mutex_unlock(&fs_info
->scrub_lock
);
322 wait_event(fs_info
->scrub_pause_wait
,
323 atomic_read(&fs_info
->scrub_pause_req
) == 0);
324 mutex_lock(&fs_info
->scrub_lock
);
328 static void scrub_pause_on(struct btrfs_fs_info
*fs_info
)
330 atomic_inc(&fs_info
->scrubs_paused
);
331 wake_up(&fs_info
->scrub_pause_wait
);
334 static void scrub_pause_off(struct btrfs_fs_info
*fs_info
)
336 mutex_lock(&fs_info
->scrub_lock
);
337 __scrub_blocked_if_needed(fs_info
);
338 atomic_dec(&fs_info
->scrubs_paused
);
339 mutex_unlock(&fs_info
->scrub_lock
);
341 wake_up(&fs_info
->scrub_pause_wait
);
344 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
346 scrub_pause_on(fs_info
);
347 scrub_pause_off(fs_info
);
351 * Insert new full stripe lock into full stripe locks tree
353 * Return pointer to existing or newly inserted full_stripe_lock structure if
354 * everything works well.
355 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
357 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
360 static struct full_stripe_lock
*insert_full_stripe_lock(
361 struct btrfs_full_stripe_locks_tree
*locks_root
,
365 struct rb_node
*parent
= NULL
;
366 struct full_stripe_lock
*entry
;
367 struct full_stripe_lock
*ret
;
369 WARN_ON(!mutex_is_locked(&locks_root
->lock
));
371 p
= &locks_root
->root
.rb_node
;
374 entry
= rb_entry(parent
, struct full_stripe_lock
, node
);
375 if (fstripe_logical
< entry
->logical
) {
377 } else if (fstripe_logical
> entry
->logical
) {
385 /* Insert new lock */
386 ret
= kmalloc(sizeof(*ret
), GFP_KERNEL
);
388 return ERR_PTR(-ENOMEM
);
389 ret
->logical
= fstripe_logical
;
391 mutex_init(&ret
->mutex
);
393 rb_link_node(&ret
->node
, parent
, p
);
394 rb_insert_color(&ret
->node
, &locks_root
->root
);
399 * Search for a full stripe lock of a block group
401 * Return pointer to existing full stripe lock if found
402 * Return NULL if not found
404 static struct full_stripe_lock
*search_full_stripe_lock(
405 struct btrfs_full_stripe_locks_tree
*locks_root
,
408 struct rb_node
*node
;
409 struct full_stripe_lock
*entry
;
411 WARN_ON(!mutex_is_locked(&locks_root
->lock
));
413 node
= locks_root
->root
.rb_node
;
415 entry
= rb_entry(node
, struct full_stripe_lock
, node
);
416 if (fstripe_logical
< entry
->logical
)
417 node
= node
->rb_left
;
418 else if (fstripe_logical
> entry
->logical
)
419 node
= node
->rb_right
;
427 * Helper to get full stripe logical from a normal bytenr.
429 * Caller must ensure @cache is a RAID56 block group.
431 static u64
get_full_stripe_logical(struct btrfs_block_group_cache
*cache
,
437 * Due to chunk item size limit, full stripe length should not be
438 * larger than U32_MAX. Just a sanity check here.
440 WARN_ON_ONCE(cache
->full_stripe_len
>= U32_MAX
);
443 * round_down() can only handle power of 2, while RAID56 full
444 * stripe length can be 64KiB * n, so we need to manually round down.
446 ret
= div64_u64(bytenr
- cache
->key
.objectid
, cache
->full_stripe_len
) *
447 cache
->full_stripe_len
+ cache
->key
.objectid
;
452 * Lock a full stripe to avoid concurrency of recovery and read
454 * It's only used for profiles with parities (RAID5/6), for other profiles it
457 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
458 * So caller must call unlock_full_stripe() at the same context.
460 * Return <0 if encounters error.
462 static int lock_full_stripe(struct btrfs_fs_info
*fs_info
, u64 bytenr
,
465 struct btrfs_block_group_cache
*bg_cache
;
466 struct btrfs_full_stripe_locks_tree
*locks_root
;
467 struct full_stripe_lock
*existing
;
472 bg_cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
478 /* Profiles not based on parity don't need full stripe lock */
479 if (!(bg_cache
->flags
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
481 locks_root
= &bg_cache
->full_stripe_locks_root
;
483 fstripe_start
= get_full_stripe_logical(bg_cache
, bytenr
);
485 /* Now insert the full stripe lock */
486 mutex_lock(&locks_root
->lock
);
487 existing
= insert_full_stripe_lock(locks_root
, fstripe_start
);
488 mutex_unlock(&locks_root
->lock
);
489 if (IS_ERR(existing
)) {
490 ret
= PTR_ERR(existing
);
493 mutex_lock(&existing
->mutex
);
496 btrfs_put_block_group(bg_cache
);
501 * Unlock a full stripe.
503 * NOTE: Caller must ensure it's the same context calling corresponding
504 * lock_full_stripe().
506 * Return 0 if we unlock full stripe without problem.
507 * Return <0 for error
509 static int unlock_full_stripe(struct btrfs_fs_info
*fs_info
, u64 bytenr
,
512 struct btrfs_block_group_cache
*bg_cache
;
513 struct btrfs_full_stripe_locks_tree
*locks_root
;
514 struct full_stripe_lock
*fstripe_lock
;
519 /* If we didn't acquire full stripe lock, no need to continue */
523 bg_cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
528 if (!(bg_cache
->flags
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
531 locks_root
= &bg_cache
->full_stripe_locks_root
;
532 fstripe_start
= get_full_stripe_logical(bg_cache
, bytenr
);
534 mutex_lock(&locks_root
->lock
);
535 fstripe_lock
= search_full_stripe_lock(locks_root
, fstripe_start
);
536 /* Unpaired unlock_full_stripe() detected */
540 mutex_unlock(&locks_root
->lock
);
544 if (fstripe_lock
->refs
== 0) {
546 btrfs_warn(fs_info
, "full stripe lock at %llu refcount underflow",
547 fstripe_lock
->logical
);
549 fstripe_lock
->refs
--;
552 if (fstripe_lock
->refs
== 0) {
553 rb_erase(&fstripe_lock
->node
, &locks_root
->root
);
556 mutex_unlock(&locks_root
->lock
);
558 mutex_unlock(&fstripe_lock
->mutex
);
562 btrfs_put_block_group(bg_cache
);
567 * used for workers that require transaction commits (i.e., for the
570 static void scrub_pending_trans_workers_inc(struct scrub_ctx
*sctx
)
572 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
574 refcount_inc(&sctx
->refs
);
576 * increment scrubs_running to prevent cancel requests from
577 * completing as long as a worker is running. we must also
578 * increment scrubs_paused to prevent deadlocking on pause
579 * requests used for transactions commits (as the worker uses a
580 * transaction context). it is safe to regard the worker
581 * as paused for all matters practical. effectively, we only
582 * avoid cancellation requests from completing.
584 mutex_lock(&fs_info
->scrub_lock
);
585 atomic_inc(&fs_info
->scrubs_running
);
586 atomic_inc(&fs_info
->scrubs_paused
);
587 mutex_unlock(&fs_info
->scrub_lock
);
590 * check if @scrubs_running=@scrubs_paused condition
591 * inside wait_event() is not an atomic operation.
592 * which means we may inc/dec @scrub_running/paused
593 * at any time. Let's wake up @scrub_pause_wait as
594 * much as we can to let commit transaction blocked less.
596 wake_up(&fs_info
->scrub_pause_wait
);
598 atomic_inc(&sctx
->workers_pending
);
601 /* used for workers that require transaction commits */
602 static void scrub_pending_trans_workers_dec(struct scrub_ctx
*sctx
)
604 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
607 * see scrub_pending_trans_workers_inc() why we're pretending
608 * to be paused in the scrub counters
610 mutex_lock(&fs_info
->scrub_lock
);
611 atomic_dec(&fs_info
->scrubs_running
);
612 atomic_dec(&fs_info
->scrubs_paused
);
613 mutex_unlock(&fs_info
->scrub_lock
);
614 atomic_dec(&sctx
->workers_pending
);
615 wake_up(&fs_info
->scrub_pause_wait
);
616 wake_up(&sctx
->list_wait
);
620 static void scrub_free_csums(struct scrub_ctx
*sctx
)
622 while (!list_empty(&sctx
->csum_list
)) {
623 struct btrfs_ordered_sum
*sum
;
624 sum
= list_first_entry(&sctx
->csum_list
,
625 struct btrfs_ordered_sum
, list
);
626 list_del(&sum
->list
);
631 static noinline_for_stack
void scrub_free_ctx(struct scrub_ctx
*sctx
)
638 /* this can happen when scrub is cancelled */
639 if (sctx
->curr
!= -1) {
640 struct scrub_bio
*sbio
= sctx
->bios
[sctx
->curr
];
642 for (i
= 0; i
< sbio
->page_count
; i
++) {
643 WARN_ON(!sbio
->pagev
[i
]->page
);
644 scrub_block_put(sbio
->pagev
[i
]->sblock
);
649 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
650 struct scrub_bio
*sbio
= sctx
->bios
[i
];
657 kfree(sctx
->wr_curr_bio
);
658 scrub_free_csums(sctx
);
662 static void scrub_put_ctx(struct scrub_ctx
*sctx
)
664 if (refcount_dec_and_test(&sctx
->refs
))
665 scrub_free_ctx(sctx
);
668 static noinline_for_stack
669 struct scrub_ctx
*scrub_setup_ctx(struct btrfs_device
*dev
, int is_dev_replace
)
671 struct scrub_ctx
*sctx
;
673 struct btrfs_fs_info
*fs_info
= dev
->fs_info
;
675 sctx
= kzalloc(sizeof(*sctx
), GFP_KERNEL
);
678 refcount_set(&sctx
->refs
, 1);
679 sctx
->is_dev_replace
= is_dev_replace
;
680 sctx
->pages_per_rd_bio
= SCRUB_PAGES_PER_RD_BIO
;
682 sctx
->fs_info
= dev
->fs_info
;
683 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
684 struct scrub_bio
*sbio
;
686 sbio
= kzalloc(sizeof(*sbio
), GFP_KERNEL
);
689 sctx
->bios
[i
] = sbio
;
693 sbio
->page_count
= 0;
694 btrfs_init_work(&sbio
->work
, btrfs_scrub_helper
,
695 scrub_bio_end_io_worker
, NULL
, NULL
);
697 if (i
!= SCRUB_BIOS_PER_SCTX
- 1)
698 sctx
->bios
[i
]->next_free
= i
+ 1;
700 sctx
->bios
[i
]->next_free
= -1;
702 sctx
->first_free
= 0;
703 atomic_set(&sctx
->bios_in_flight
, 0);
704 atomic_set(&sctx
->workers_pending
, 0);
705 atomic_set(&sctx
->cancel_req
, 0);
706 sctx
->csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
707 INIT_LIST_HEAD(&sctx
->csum_list
);
709 spin_lock_init(&sctx
->list_lock
);
710 spin_lock_init(&sctx
->stat_lock
);
711 init_waitqueue_head(&sctx
->list_wait
);
713 WARN_ON(sctx
->wr_curr_bio
!= NULL
);
714 mutex_init(&sctx
->wr_lock
);
715 sctx
->wr_curr_bio
= NULL
;
716 if (is_dev_replace
) {
717 WARN_ON(!fs_info
->dev_replace
.tgtdev
);
718 sctx
->pages_per_wr_bio
= SCRUB_PAGES_PER_WR_BIO
;
719 sctx
->wr_tgtdev
= fs_info
->dev_replace
.tgtdev
;
720 sctx
->flush_all_writes
= false;
726 scrub_free_ctx(sctx
);
727 return ERR_PTR(-ENOMEM
);
730 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
,
738 struct extent_buffer
*eb
;
739 struct btrfs_inode_item
*inode_item
;
740 struct scrub_warning
*swarn
= warn_ctx
;
741 struct btrfs_fs_info
*fs_info
= swarn
->dev
->fs_info
;
742 struct inode_fs_paths
*ipath
= NULL
;
743 struct btrfs_root
*local_root
;
744 struct btrfs_key root_key
;
745 struct btrfs_key key
;
747 root_key
.objectid
= root
;
748 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
749 root_key
.offset
= (u64
)-1;
750 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
751 if (IS_ERR(local_root
)) {
752 ret
= PTR_ERR(local_root
);
757 * this makes the path point to (inum INODE_ITEM ioff)
760 key
.type
= BTRFS_INODE_ITEM_KEY
;
763 ret
= btrfs_search_slot(NULL
, local_root
, &key
, swarn
->path
, 0, 0);
765 btrfs_release_path(swarn
->path
);
769 eb
= swarn
->path
->nodes
[0];
770 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
771 struct btrfs_inode_item
);
772 isize
= btrfs_inode_size(eb
, inode_item
);
773 nlink
= btrfs_inode_nlink(eb
, inode_item
);
774 btrfs_release_path(swarn
->path
);
777 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
778 * uses GFP_NOFS in this context, so we keep it consistent but it does
779 * not seem to be strictly necessary.
781 nofs_flag
= memalloc_nofs_save();
782 ipath
= init_ipath(4096, local_root
, swarn
->path
);
783 memalloc_nofs_restore(nofs_flag
);
785 ret
= PTR_ERR(ipath
);
789 ret
= paths_from_inode(inum
, ipath
);
795 * we deliberately ignore the bit ipath might have been too small to
796 * hold all of the paths here
798 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
799 btrfs_warn_in_rcu(fs_info
,
800 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
801 swarn
->errstr
, swarn
->logical
,
802 rcu_str_deref(swarn
->dev
->name
),
803 (unsigned long long)swarn
->sector
,
805 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
806 (char *)(unsigned long)ipath
->fspath
->val
[i
]);
812 btrfs_warn_in_rcu(fs_info
,
813 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
814 swarn
->errstr
, swarn
->logical
,
815 rcu_str_deref(swarn
->dev
->name
),
816 (unsigned long long)swarn
->sector
,
817 root
, inum
, offset
, ret
);
823 static void scrub_print_warning(const char *errstr
, struct scrub_block
*sblock
)
825 struct btrfs_device
*dev
;
826 struct btrfs_fs_info
*fs_info
;
827 struct btrfs_path
*path
;
828 struct btrfs_key found_key
;
829 struct extent_buffer
*eb
;
830 struct btrfs_extent_item
*ei
;
831 struct scrub_warning swarn
;
832 unsigned long ptr
= 0;
840 WARN_ON(sblock
->page_count
< 1);
841 dev
= sblock
->pagev
[0]->dev
;
842 fs_info
= sblock
->sctx
->fs_info
;
844 path
= btrfs_alloc_path();
848 swarn
.sector
= (sblock
->pagev
[0]->physical
) >> 9;
849 swarn
.logical
= sblock
->pagev
[0]->logical
;
850 swarn
.errstr
= errstr
;
853 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
,
858 extent_item_pos
= swarn
.logical
- found_key
.objectid
;
859 swarn
.extent_item_size
= found_key
.offset
;
862 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
863 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
865 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
867 ret
= tree_backref_for_extent(&ptr
, eb
, &found_key
, ei
,
868 item_size
, &ref_root
,
870 btrfs_warn_in_rcu(fs_info
,
871 "%s at logical %llu on dev %s, sector %llu: metadata %s (level %d) in tree %llu",
872 errstr
, swarn
.logical
,
873 rcu_str_deref(dev
->name
),
874 (unsigned long long)swarn
.sector
,
875 ref_level
? "node" : "leaf",
876 ret
< 0 ? -1 : ref_level
,
877 ret
< 0 ? -1 : ref_root
);
879 btrfs_release_path(path
);
881 btrfs_release_path(path
);
884 iterate_extent_inodes(fs_info
, found_key
.objectid
,
886 scrub_print_warning_inode
, &swarn
);
890 btrfs_free_path(path
);
893 static int scrub_fixup_readpage(u64 inum
, u64 offset
, u64 root
, void *fixup_ctx
)
895 struct page
*page
= NULL
;
897 struct scrub_fixup_nodatasum
*fixup
= fixup_ctx
;
900 struct btrfs_key key
;
901 struct inode
*inode
= NULL
;
902 struct btrfs_fs_info
*fs_info
;
903 u64 end
= offset
+ PAGE_SIZE
- 1;
904 struct btrfs_root
*local_root
;
908 key
.type
= BTRFS_ROOT_ITEM_KEY
;
909 key
.offset
= (u64
)-1;
911 fs_info
= fixup
->root
->fs_info
;
912 srcu_index
= srcu_read_lock(&fs_info
->subvol_srcu
);
914 local_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
915 if (IS_ERR(local_root
)) {
916 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
917 return PTR_ERR(local_root
);
920 key
.type
= BTRFS_INODE_ITEM_KEY
;
923 inode
= btrfs_iget(fs_info
->sb
, &key
, local_root
, NULL
);
924 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
926 return PTR_ERR(inode
);
928 index
= offset
>> PAGE_SHIFT
;
930 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
936 if (PageUptodate(page
)) {
937 if (PageDirty(page
)) {
939 * we need to write the data to the defect sector. the
940 * data that was in that sector is not in memory,
941 * because the page was modified. we must not write the
942 * modified page to that sector.
944 * TODO: what could be done here: wait for the delalloc
945 * runner to write out that page (might involve
946 * COW) and see whether the sector is still
947 * referenced afterwards.
949 * For the meantime, we'll treat this error
950 * incorrectable, although there is a chance that a
951 * later scrub will find the bad sector again and that
952 * there's no dirty page in memory, then.
957 ret
= repair_io_failure(fs_info
, inum
, offset
, PAGE_SIZE
,
958 fixup
->logical
, page
,
959 offset
- page_offset(page
),
965 * we need to get good data first. the general readpage path
966 * will call repair_io_failure for us, we just have to make
967 * sure we read the bad mirror.
969 ret
= set_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
972 /* set_extent_bits should give proper error */
979 ret
= extent_read_full_page(&BTRFS_I(inode
)->io_tree
, page
,
982 wait_on_page_locked(page
);
984 corrected
= !test_range_bit(&BTRFS_I(inode
)->io_tree
, offset
,
985 end
, EXTENT_DAMAGED
, 0, NULL
);
987 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
1000 if (ret
== 0 && corrected
) {
1002 * we only need to call readpage for one of the inodes belonging
1003 * to this extent. so make iterate_extent_inodes stop
1011 static void scrub_fixup_nodatasum(struct btrfs_work
*work
)
1013 struct btrfs_fs_info
*fs_info
;
1015 struct scrub_fixup_nodatasum
*fixup
;
1016 struct scrub_ctx
*sctx
;
1017 struct btrfs_trans_handle
*trans
= NULL
;
1018 struct btrfs_path
*path
;
1019 int uncorrectable
= 0;
1021 fixup
= container_of(work
, struct scrub_fixup_nodatasum
, work
);
1023 fs_info
= fixup
->root
->fs_info
;
1025 path
= btrfs_alloc_path();
1027 spin_lock(&sctx
->stat_lock
);
1028 ++sctx
->stat
.malloc_errors
;
1029 spin_unlock(&sctx
->stat_lock
);
1034 trans
= btrfs_join_transaction(fixup
->root
);
1035 if (IS_ERR(trans
)) {
1041 * the idea is to trigger a regular read through the standard path. we
1042 * read a page from the (failed) logical address by specifying the
1043 * corresponding copynum of the failed sector. thus, that readpage is
1045 * that is the point where on-the-fly error correction will kick in
1046 * (once it's finished) and rewrite the failed sector if a good copy
1049 ret
= iterate_inodes_from_logical(fixup
->logical
, fs_info
, path
,
1050 scrub_fixup_readpage
, fixup
);
1057 spin_lock(&sctx
->stat_lock
);
1058 ++sctx
->stat
.corrected_errors
;
1059 spin_unlock(&sctx
->stat_lock
);
1062 if (trans
&& !IS_ERR(trans
))
1063 btrfs_end_transaction(trans
);
1064 if (uncorrectable
) {
1065 spin_lock(&sctx
->stat_lock
);
1066 ++sctx
->stat
.uncorrectable_errors
;
1067 spin_unlock(&sctx
->stat_lock
);
1068 btrfs_dev_replace_stats_inc(
1069 &fs_info
->dev_replace
.num_uncorrectable_read_errors
);
1070 btrfs_err_rl_in_rcu(fs_info
,
1071 "unable to fixup (nodatasum) error at logical %llu on dev %s",
1072 fixup
->logical
, rcu_str_deref(fixup
->dev
->name
));
1075 btrfs_free_path(path
);
1078 scrub_pending_trans_workers_dec(sctx
);
1081 static inline void scrub_get_recover(struct scrub_recover
*recover
)
1083 refcount_inc(&recover
->refs
);
1086 static inline void scrub_put_recover(struct btrfs_fs_info
*fs_info
,
1087 struct scrub_recover
*recover
)
1089 if (refcount_dec_and_test(&recover
->refs
)) {
1090 btrfs_bio_counter_dec(fs_info
);
1091 btrfs_put_bbio(recover
->bbio
);
1097 * scrub_handle_errored_block gets called when either verification of the
1098 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1099 * case, this function handles all pages in the bio, even though only one
1101 * The goal of this function is to repair the errored block by using the
1102 * contents of one of the mirrors.
1104 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
)
1106 struct scrub_ctx
*sctx
= sblock_to_check
->sctx
;
1107 struct btrfs_device
*dev
;
1108 struct btrfs_fs_info
*fs_info
;
1111 unsigned int failed_mirror_index
;
1112 unsigned int is_metadata
;
1113 unsigned int have_csum
;
1114 struct scrub_block
*sblocks_for_recheck
; /* holds one for each mirror */
1115 struct scrub_block
*sblock_bad
;
1120 bool full_stripe_locked
;
1121 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
1122 DEFAULT_RATELIMIT_BURST
);
1124 BUG_ON(sblock_to_check
->page_count
< 1);
1125 fs_info
= sctx
->fs_info
;
1126 if (sblock_to_check
->pagev
[0]->flags
& BTRFS_EXTENT_FLAG_SUPER
) {
1128 * if we find an error in a super block, we just report it.
1129 * They will get written with the next transaction commit
1132 spin_lock(&sctx
->stat_lock
);
1133 ++sctx
->stat
.super_errors
;
1134 spin_unlock(&sctx
->stat_lock
);
1137 length
= sblock_to_check
->page_count
* PAGE_SIZE
;
1138 logical
= sblock_to_check
->pagev
[0]->logical
;
1139 BUG_ON(sblock_to_check
->pagev
[0]->mirror_num
< 1);
1140 failed_mirror_index
= sblock_to_check
->pagev
[0]->mirror_num
- 1;
1141 is_metadata
= !(sblock_to_check
->pagev
[0]->flags
&
1142 BTRFS_EXTENT_FLAG_DATA
);
1143 have_csum
= sblock_to_check
->pagev
[0]->have_csum
;
1144 dev
= sblock_to_check
->pagev
[0]->dev
;
1147 * For RAID5/6, race can happen for a different device scrub thread.
1148 * For data corruption, Parity and Data threads will both try
1149 * to recovery the data.
1150 * Race can lead to doubly added csum error, or even unrecoverable
1153 ret
= lock_full_stripe(fs_info
, logical
, &full_stripe_locked
);
1155 spin_lock(&sctx
->stat_lock
);
1157 sctx
->stat
.malloc_errors
++;
1158 sctx
->stat
.read_errors
++;
1159 sctx
->stat
.uncorrectable_errors
++;
1160 spin_unlock(&sctx
->stat_lock
);
1164 if (sctx
->is_dev_replace
&& !is_metadata
&& !have_csum
) {
1165 sblocks_for_recheck
= NULL
;
1166 goto nodatasum_case
;
1170 * read all mirrors one after the other. This includes to
1171 * re-read the extent or metadata block that failed (that was
1172 * the cause that this fixup code is called) another time,
1173 * page by page this time in order to know which pages
1174 * caused I/O errors and which ones are good (for all mirrors).
1175 * It is the goal to handle the situation when more than one
1176 * mirror contains I/O errors, but the errors do not
1177 * overlap, i.e. the data can be repaired by selecting the
1178 * pages from those mirrors without I/O error on the
1179 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1180 * would be that mirror #1 has an I/O error on the first page,
1181 * the second page is good, and mirror #2 has an I/O error on
1182 * the second page, but the first page is good.
1183 * Then the first page of the first mirror can be repaired by
1184 * taking the first page of the second mirror, and the
1185 * second page of the second mirror can be repaired by
1186 * copying the contents of the 2nd page of the 1st mirror.
1187 * One more note: if the pages of one mirror contain I/O
1188 * errors, the checksum cannot be verified. In order to get
1189 * the best data for repairing, the first attempt is to find
1190 * a mirror without I/O errors and with a validated checksum.
1191 * Only if this is not possible, the pages are picked from
1192 * mirrors with I/O errors without considering the checksum.
1193 * If the latter is the case, at the end, the checksum of the
1194 * repaired area is verified in order to correctly maintain
1198 sblocks_for_recheck
= kcalloc(BTRFS_MAX_MIRRORS
,
1199 sizeof(*sblocks_for_recheck
), GFP_NOFS
);
1200 if (!sblocks_for_recheck
) {
1201 spin_lock(&sctx
->stat_lock
);
1202 sctx
->stat
.malloc_errors
++;
1203 sctx
->stat
.read_errors
++;
1204 sctx
->stat
.uncorrectable_errors
++;
1205 spin_unlock(&sctx
->stat_lock
);
1206 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
1210 /* setup the context, map the logical blocks and alloc the pages */
1211 ret
= scrub_setup_recheck_block(sblock_to_check
, sblocks_for_recheck
);
1213 spin_lock(&sctx
->stat_lock
);
1214 sctx
->stat
.read_errors
++;
1215 sctx
->stat
.uncorrectable_errors
++;
1216 spin_unlock(&sctx
->stat_lock
);
1217 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
1220 BUG_ON(failed_mirror_index
>= BTRFS_MAX_MIRRORS
);
1221 sblock_bad
= sblocks_for_recheck
+ failed_mirror_index
;
1223 /* build and submit the bios for the failed mirror, check checksums */
1224 scrub_recheck_block(fs_info
, sblock_bad
, 1);
1226 if (!sblock_bad
->header_error
&& !sblock_bad
->checksum_error
&&
1227 sblock_bad
->no_io_error_seen
) {
1229 * the error disappeared after reading page by page, or
1230 * the area was part of a huge bio and other parts of the
1231 * bio caused I/O errors, or the block layer merged several
1232 * read requests into one and the error is caused by a
1233 * different bio (usually one of the two latter cases is
1236 spin_lock(&sctx
->stat_lock
);
1237 sctx
->stat
.unverified_errors
++;
1238 sblock_to_check
->data_corrected
= 1;
1239 spin_unlock(&sctx
->stat_lock
);
1241 if (sctx
->is_dev_replace
)
1242 scrub_write_block_to_dev_replace(sblock_bad
);
1246 if (!sblock_bad
->no_io_error_seen
) {
1247 spin_lock(&sctx
->stat_lock
);
1248 sctx
->stat
.read_errors
++;
1249 spin_unlock(&sctx
->stat_lock
);
1250 if (__ratelimit(&_rs
))
1251 scrub_print_warning("i/o error", sblock_to_check
);
1252 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
1253 } else if (sblock_bad
->checksum_error
) {
1254 spin_lock(&sctx
->stat_lock
);
1255 sctx
->stat
.csum_errors
++;
1256 spin_unlock(&sctx
->stat_lock
);
1257 if (__ratelimit(&_rs
))
1258 scrub_print_warning("checksum error", sblock_to_check
);
1259 btrfs_dev_stat_inc_and_print(dev
,
1260 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1261 } else if (sblock_bad
->header_error
) {
1262 spin_lock(&sctx
->stat_lock
);
1263 sctx
->stat
.verify_errors
++;
1264 spin_unlock(&sctx
->stat_lock
);
1265 if (__ratelimit(&_rs
))
1266 scrub_print_warning("checksum/header error",
1268 if (sblock_bad
->generation_error
)
1269 btrfs_dev_stat_inc_and_print(dev
,
1270 BTRFS_DEV_STAT_GENERATION_ERRS
);
1272 btrfs_dev_stat_inc_and_print(dev
,
1273 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1276 if (sctx
->readonly
) {
1277 ASSERT(!sctx
->is_dev_replace
);
1281 if (!is_metadata
&& !have_csum
) {
1282 struct scrub_fixup_nodatasum
*fixup_nodatasum
;
1284 WARN_ON(sctx
->is_dev_replace
);
1289 * !is_metadata and !have_csum, this means that the data
1290 * might not be COWed, that it might be modified
1291 * concurrently. The general strategy to work on the
1292 * commit root does not help in the case when COW is not
1295 fixup_nodatasum
= kzalloc(sizeof(*fixup_nodatasum
), GFP_NOFS
);
1296 if (!fixup_nodatasum
)
1297 goto did_not_correct_error
;
1298 fixup_nodatasum
->sctx
= sctx
;
1299 fixup_nodatasum
->dev
= dev
;
1300 fixup_nodatasum
->logical
= logical
;
1301 fixup_nodatasum
->root
= fs_info
->extent_root
;
1302 fixup_nodatasum
->mirror_num
= failed_mirror_index
+ 1;
1303 scrub_pending_trans_workers_inc(sctx
);
1304 btrfs_init_work(&fixup_nodatasum
->work
, btrfs_scrub_helper
,
1305 scrub_fixup_nodatasum
, NULL
, NULL
);
1306 btrfs_queue_work(fs_info
->scrub_workers
,
1307 &fixup_nodatasum
->work
);
1312 * now build and submit the bios for the other mirrors, check
1314 * First try to pick the mirror which is completely without I/O
1315 * errors and also does not have a checksum error.
1316 * If one is found, and if a checksum is present, the full block
1317 * that is known to contain an error is rewritten. Afterwards
1318 * the block is known to be corrected.
1319 * If a mirror is found which is completely correct, and no
1320 * checksum is present, only those pages are rewritten that had
1321 * an I/O error in the block to be repaired, since it cannot be
1322 * determined, which copy of the other pages is better (and it
1323 * could happen otherwise that a correct page would be
1324 * overwritten by a bad one).
1326 for (mirror_index
= 0;
1327 mirror_index
< BTRFS_MAX_MIRRORS
&&
1328 sblocks_for_recheck
[mirror_index
].page_count
> 0;
1330 struct scrub_block
*sblock_other
;
1332 if (mirror_index
== failed_mirror_index
)
1334 sblock_other
= sblocks_for_recheck
+ mirror_index
;
1336 /* build and submit the bios, check checksums */
1337 scrub_recheck_block(fs_info
, sblock_other
, 0);
1339 if (!sblock_other
->header_error
&&
1340 !sblock_other
->checksum_error
&&
1341 sblock_other
->no_io_error_seen
) {
1342 if (sctx
->is_dev_replace
) {
1343 scrub_write_block_to_dev_replace(sblock_other
);
1344 goto corrected_error
;
1346 ret
= scrub_repair_block_from_good_copy(
1347 sblock_bad
, sblock_other
);
1349 goto corrected_error
;
1354 if (sblock_bad
->no_io_error_seen
&& !sctx
->is_dev_replace
)
1355 goto did_not_correct_error
;
1358 * In case of I/O errors in the area that is supposed to be
1359 * repaired, continue by picking good copies of those pages.
1360 * Select the good pages from mirrors to rewrite bad pages from
1361 * the area to fix. Afterwards verify the checksum of the block
1362 * that is supposed to be repaired. This verification step is
1363 * only done for the purpose of statistic counting and for the
1364 * final scrub report, whether errors remain.
1365 * A perfect algorithm could make use of the checksum and try
1366 * all possible combinations of pages from the different mirrors
1367 * until the checksum verification succeeds. For example, when
1368 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1369 * of mirror #2 is readable but the final checksum test fails,
1370 * then the 2nd page of mirror #3 could be tried, whether now
1371 * the final checksum succeeds. But this would be a rare
1372 * exception and is therefore not implemented. At least it is
1373 * avoided that the good copy is overwritten.
1374 * A more useful improvement would be to pick the sectors
1375 * without I/O error based on sector sizes (512 bytes on legacy
1376 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1377 * mirror could be repaired by taking 512 byte of a different
1378 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1379 * area are unreadable.
1382 for (page_num
= 0; page_num
< sblock_bad
->page_count
;
1384 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1385 struct scrub_block
*sblock_other
= NULL
;
1387 /* skip no-io-error page in scrub */
1388 if (!page_bad
->io_error
&& !sctx
->is_dev_replace
)
1391 /* try to find no-io-error page in mirrors */
1392 if (page_bad
->io_error
) {
1393 for (mirror_index
= 0;
1394 mirror_index
< BTRFS_MAX_MIRRORS
&&
1395 sblocks_for_recheck
[mirror_index
].page_count
> 0;
1397 if (!sblocks_for_recheck
[mirror_index
].
1398 pagev
[page_num
]->io_error
) {
1399 sblock_other
= sblocks_for_recheck
+
1408 if (sctx
->is_dev_replace
) {
1410 * did not find a mirror to fetch the page
1411 * from. scrub_write_page_to_dev_replace()
1412 * handles this case (page->io_error), by
1413 * filling the block with zeros before
1414 * submitting the write request
1417 sblock_other
= sblock_bad
;
1419 if (scrub_write_page_to_dev_replace(sblock_other
,
1421 btrfs_dev_replace_stats_inc(
1422 &fs_info
->dev_replace
.num_write_errors
);
1425 } else if (sblock_other
) {
1426 ret
= scrub_repair_page_from_good_copy(sblock_bad
,
1430 page_bad
->io_error
= 0;
1436 if (success
&& !sctx
->is_dev_replace
) {
1437 if (is_metadata
|| have_csum
) {
1439 * need to verify the checksum now that all
1440 * sectors on disk are repaired (the write
1441 * request for data to be repaired is on its way).
1442 * Just be lazy and use scrub_recheck_block()
1443 * which re-reads the data before the checksum
1444 * is verified, but most likely the data comes out
1445 * of the page cache.
1447 scrub_recheck_block(fs_info
, sblock_bad
, 1);
1448 if (!sblock_bad
->header_error
&&
1449 !sblock_bad
->checksum_error
&&
1450 sblock_bad
->no_io_error_seen
)
1451 goto corrected_error
;
1453 goto did_not_correct_error
;
1456 spin_lock(&sctx
->stat_lock
);
1457 sctx
->stat
.corrected_errors
++;
1458 sblock_to_check
->data_corrected
= 1;
1459 spin_unlock(&sctx
->stat_lock
);
1460 btrfs_err_rl_in_rcu(fs_info
,
1461 "fixed up error at logical %llu on dev %s",
1462 logical
, rcu_str_deref(dev
->name
));
1465 did_not_correct_error
:
1466 spin_lock(&sctx
->stat_lock
);
1467 sctx
->stat
.uncorrectable_errors
++;
1468 spin_unlock(&sctx
->stat_lock
);
1469 btrfs_err_rl_in_rcu(fs_info
,
1470 "unable to fixup (regular) error at logical %llu on dev %s",
1471 logical
, rcu_str_deref(dev
->name
));
1475 if (sblocks_for_recheck
) {
1476 for (mirror_index
= 0; mirror_index
< BTRFS_MAX_MIRRORS
;
1478 struct scrub_block
*sblock
= sblocks_for_recheck
+
1480 struct scrub_recover
*recover
;
1483 for (page_index
= 0; page_index
< sblock
->page_count
;
1485 sblock
->pagev
[page_index
]->sblock
= NULL
;
1486 recover
= sblock
->pagev
[page_index
]->recover
;
1488 scrub_put_recover(fs_info
, recover
);
1489 sblock
->pagev
[page_index
]->recover
=
1492 scrub_page_put(sblock
->pagev
[page_index
]);
1495 kfree(sblocks_for_recheck
);
1498 ret
= unlock_full_stripe(fs_info
, logical
, full_stripe_locked
);
1504 static inline int scrub_nr_raid_mirrors(struct btrfs_bio
*bbio
)
1506 if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID5
)
1508 else if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID6
)
1511 return (int)bbio
->num_stripes
;
1514 static inline void scrub_stripe_index_and_offset(u64 logical
, u64 map_type
,
1517 int nstripes
, int mirror
,
1523 if (map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
1525 for (i
= 0; i
< nstripes
; i
++) {
1526 if (raid_map
[i
] == RAID6_Q_STRIPE
||
1527 raid_map
[i
] == RAID5_P_STRIPE
)
1530 if (logical
>= raid_map
[i
] &&
1531 logical
< raid_map
[i
] + mapped_length
)
1536 *stripe_offset
= logical
- raid_map
[i
];
1538 /* The other RAID type */
1539 *stripe_index
= mirror
;
1544 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
1545 struct scrub_block
*sblocks_for_recheck
)
1547 struct scrub_ctx
*sctx
= original_sblock
->sctx
;
1548 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
1549 u64 length
= original_sblock
->page_count
* PAGE_SIZE
;
1550 u64 logical
= original_sblock
->pagev
[0]->logical
;
1551 u64 generation
= original_sblock
->pagev
[0]->generation
;
1552 u64 flags
= original_sblock
->pagev
[0]->flags
;
1553 u64 have_csum
= original_sblock
->pagev
[0]->have_csum
;
1554 struct scrub_recover
*recover
;
1555 struct btrfs_bio
*bbio
;
1566 * note: the two members refs and outstanding_pages
1567 * are not used (and not set) in the blocks that are used for
1568 * the recheck procedure
1571 while (length
> 0) {
1572 sublen
= min_t(u64
, length
, PAGE_SIZE
);
1573 mapped_length
= sublen
;
1577 * with a length of PAGE_SIZE, each returned stripe
1578 * represents one mirror
1580 btrfs_bio_counter_inc_blocked(fs_info
);
1581 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
1582 logical
, &mapped_length
, &bbio
);
1583 if (ret
|| !bbio
|| mapped_length
< sublen
) {
1584 btrfs_put_bbio(bbio
);
1585 btrfs_bio_counter_dec(fs_info
);
1589 recover
= kzalloc(sizeof(struct scrub_recover
), GFP_NOFS
);
1591 btrfs_put_bbio(bbio
);
1592 btrfs_bio_counter_dec(fs_info
);
1596 refcount_set(&recover
->refs
, 1);
1597 recover
->bbio
= bbio
;
1598 recover
->map_length
= mapped_length
;
1600 BUG_ON(page_index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
1602 nmirrors
= min(scrub_nr_raid_mirrors(bbio
), BTRFS_MAX_MIRRORS
);
1604 for (mirror_index
= 0; mirror_index
< nmirrors
;
1606 struct scrub_block
*sblock
;
1607 struct scrub_page
*page
;
1609 sblock
= sblocks_for_recheck
+ mirror_index
;
1610 sblock
->sctx
= sctx
;
1612 page
= kzalloc(sizeof(*page
), GFP_NOFS
);
1615 spin_lock(&sctx
->stat_lock
);
1616 sctx
->stat
.malloc_errors
++;
1617 spin_unlock(&sctx
->stat_lock
);
1618 scrub_put_recover(fs_info
, recover
);
1621 scrub_page_get(page
);
1622 sblock
->pagev
[page_index
] = page
;
1623 page
->sblock
= sblock
;
1624 page
->flags
= flags
;
1625 page
->generation
= generation
;
1626 page
->logical
= logical
;
1627 page
->have_csum
= have_csum
;
1630 original_sblock
->pagev
[0]->csum
,
1633 scrub_stripe_index_and_offset(logical
,
1642 page
->physical
= bbio
->stripes
[stripe_index
].physical
+
1644 page
->dev
= bbio
->stripes
[stripe_index
].dev
;
1646 BUG_ON(page_index
>= original_sblock
->page_count
);
1647 page
->physical_for_dev_replace
=
1648 original_sblock
->pagev
[page_index
]->
1649 physical_for_dev_replace
;
1650 /* for missing devices, dev->bdev is NULL */
1651 page
->mirror_num
= mirror_index
+ 1;
1652 sblock
->page_count
++;
1653 page
->page
= alloc_page(GFP_NOFS
);
1657 scrub_get_recover(recover
);
1658 page
->recover
= recover
;
1660 scrub_put_recover(fs_info
, recover
);
1669 struct scrub_bio_ret
{
1670 struct completion event
;
1671 blk_status_t status
;
1674 static void scrub_bio_wait_endio(struct bio
*bio
)
1676 struct scrub_bio_ret
*ret
= bio
->bi_private
;
1678 ret
->status
= bio
->bi_status
;
1679 complete(&ret
->event
);
1682 static inline int scrub_is_page_on_raid56(struct scrub_page
*page
)
1684 return page
->recover
&&
1685 (page
->recover
->bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
);
1688 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info
*fs_info
,
1690 struct scrub_page
*page
)
1692 struct scrub_bio_ret done
;
1695 init_completion(&done
.event
);
1697 bio
->bi_iter
.bi_sector
= page
->logical
>> 9;
1698 bio
->bi_private
= &done
;
1699 bio
->bi_end_io
= scrub_bio_wait_endio
;
1701 ret
= raid56_parity_recover(fs_info
, bio
, page
->recover
->bbio
,
1702 page
->recover
->map_length
,
1703 page
->mirror_num
, 0);
1707 wait_for_completion_io(&done
.event
);
1715 * this function will check the on disk data for checksum errors, header
1716 * errors and read I/O errors. If any I/O errors happen, the exact pages
1717 * which are errored are marked as being bad. The goal is to enable scrub
1718 * to take those pages that are not errored from all the mirrors so that
1719 * the pages that are errored in the just handled mirror can be repaired.
1721 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
1722 struct scrub_block
*sblock
,
1723 int retry_failed_mirror
)
1727 sblock
->no_io_error_seen
= 1;
1729 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1731 struct scrub_page
*page
= sblock
->pagev
[page_num
];
1733 if (page
->dev
->bdev
== NULL
) {
1735 sblock
->no_io_error_seen
= 0;
1739 WARN_ON(!page
->page
);
1740 bio
= btrfs_io_bio_alloc(1);
1741 bio_set_dev(bio
, page
->dev
->bdev
);
1743 bio_add_page(bio
, page
->page
, PAGE_SIZE
, 0);
1744 if (!retry_failed_mirror
&& scrub_is_page_on_raid56(page
)) {
1745 if (scrub_submit_raid56_bio_wait(fs_info
, bio
, page
)) {
1747 sblock
->no_io_error_seen
= 0;
1750 bio
->bi_iter
.bi_sector
= page
->physical
>> 9;
1751 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
1753 if (btrfsic_submit_bio_wait(bio
)) {
1755 sblock
->no_io_error_seen
= 0;
1762 if (sblock
->no_io_error_seen
)
1763 scrub_recheck_block_checksum(sblock
);
1766 static inline int scrub_check_fsid(u8 fsid
[],
1767 struct scrub_page
*spage
)
1769 struct btrfs_fs_devices
*fs_devices
= spage
->dev
->fs_devices
;
1772 ret
= memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1776 static void scrub_recheck_block_checksum(struct scrub_block
*sblock
)
1778 sblock
->header_error
= 0;
1779 sblock
->checksum_error
= 0;
1780 sblock
->generation_error
= 0;
1782 if (sblock
->pagev
[0]->flags
& BTRFS_EXTENT_FLAG_DATA
)
1783 scrub_checksum_data(sblock
);
1785 scrub_checksum_tree_block(sblock
);
1788 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
1789 struct scrub_block
*sblock_good
)
1794 for (page_num
= 0; page_num
< sblock_bad
->page_count
; page_num
++) {
1797 ret_sub
= scrub_repair_page_from_good_copy(sblock_bad
,
1807 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
1808 struct scrub_block
*sblock_good
,
1809 int page_num
, int force_write
)
1811 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1812 struct scrub_page
*page_good
= sblock_good
->pagev
[page_num
];
1813 struct btrfs_fs_info
*fs_info
= sblock_bad
->sctx
->fs_info
;
1815 BUG_ON(page_bad
->page
== NULL
);
1816 BUG_ON(page_good
->page
== NULL
);
1817 if (force_write
|| sblock_bad
->header_error
||
1818 sblock_bad
->checksum_error
|| page_bad
->io_error
) {
1822 if (!page_bad
->dev
->bdev
) {
1823 btrfs_warn_rl(fs_info
,
1824 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
1828 bio
= btrfs_io_bio_alloc(1);
1829 bio_set_dev(bio
, page_bad
->dev
->bdev
);
1830 bio
->bi_iter
.bi_sector
= page_bad
->physical
>> 9;
1831 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
1833 ret
= bio_add_page(bio
, page_good
->page
, PAGE_SIZE
, 0);
1834 if (PAGE_SIZE
!= ret
) {
1839 if (btrfsic_submit_bio_wait(bio
)) {
1840 btrfs_dev_stat_inc_and_print(page_bad
->dev
,
1841 BTRFS_DEV_STAT_WRITE_ERRS
);
1842 btrfs_dev_replace_stats_inc(
1843 &fs_info
->dev_replace
.num_write_errors
);
1853 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
)
1855 struct btrfs_fs_info
*fs_info
= sblock
->sctx
->fs_info
;
1859 * This block is used for the check of the parity on the source device,
1860 * so the data needn't be written into the destination device.
1862 if (sblock
->sparity
)
1865 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1868 ret
= scrub_write_page_to_dev_replace(sblock
, page_num
);
1870 btrfs_dev_replace_stats_inc(
1871 &fs_info
->dev_replace
.num_write_errors
);
1875 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
1878 struct scrub_page
*spage
= sblock
->pagev
[page_num
];
1880 BUG_ON(spage
->page
== NULL
);
1881 if (spage
->io_error
) {
1882 void *mapped_buffer
= kmap_atomic(spage
->page
);
1884 clear_page(mapped_buffer
);
1885 flush_dcache_page(spage
->page
);
1886 kunmap_atomic(mapped_buffer
);
1888 return scrub_add_page_to_wr_bio(sblock
->sctx
, spage
);
1891 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
1892 struct scrub_page
*spage
)
1894 struct scrub_bio
*sbio
;
1897 mutex_lock(&sctx
->wr_lock
);
1899 if (!sctx
->wr_curr_bio
) {
1900 sctx
->wr_curr_bio
= kzalloc(sizeof(*sctx
->wr_curr_bio
),
1902 if (!sctx
->wr_curr_bio
) {
1903 mutex_unlock(&sctx
->wr_lock
);
1906 sctx
->wr_curr_bio
->sctx
= sctx
;
1907 sctx
->wr_curr_bio
->page_count
= 0;
1909 sbio
= sctx
->wr_curr_bio
;
1910 if (sbio
->page_count
== 0) {
1913 sbio
->physical
= spage
->physical_for_dev_replace
;
1914 sbio
->logical
= spage
->logical
;
1915 sbio
->dev
= sctx
->wr_tgtdev
;
1918 bio
= btrfs_io_bio_alloc(sctx
->pages_per_wr_bio
);
1922 bio
->bi_private
= sbio
;
1923 bio
->bi_end_io
= scrub_wr_bio_end_io
;
1924 bio_set_dev(bio
, sbio
->dev
->bdev
);
1925 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
1926 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
1928 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
1929 spage
->physical_for_dev_replace
||
1930 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
1932 scrub_wr_submit(sctx
);
1936 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
1937 if (ret
!= PAGE_SIZE
) {
1938 if (sbio
->page_count
< 1) {
1941 mutex_unlock(&sctx
->wr_lock
);
1944 scrub_wr_submit(sctx
);
1948 sbio
->pagev
[sbio
->page_count
] = spage
;
1949 scrub_page_get(spage
);
1951 if (sbio
->page_count
== sctx
->pages_per_wr_bio
)
1952 scrub_wr_submit(sctx
);
1953 mutex_unlock(&sctx
->wr_lock
);
1958 static void scrub_wr_submit(struct scrub_ctx
*sctx
)
1960 struct scrub_bio
*sbio
;
1962 if (!sctx
->wr_curr_bio
)
1965 sbio
= sctx
->wr_curr_bio
;
1966 sctx
->wr_curr_bio
= NULL
;
1967 WARN_ON(!sbio
->bio
->bi_disk
);
1968 scrub_pending_bio_inc(sctx
);
1969 /* process all writes in a single worker thread. Then the block layer
1970 * orders the requests before sending them to the driver which
1971 * doubled the write performance on spinning disks when measured
1973 btrfsic_submit_bio(sbio
->bio
);
1976 static void scrub_wr_bio_end_io(struct bio
*bio
)
1978 struct scrub_bio
*sbio
= bio
->bi_private
;
1979 struct btrfs_fs_info
*fs_info
= sbio
->dev
->fs_info
;
1981 sbio
->status
= bio
->bi_status
;
1984 btrfs_init_work(&sbio
->work
, btrfs_scrubwrc_helper
,
1985 scrub_wr_bio_end_io_worker
, NULL
, NULL
);
1986 btrfs_queue_work(fs_info
->scrub_wr_completion_workers
, &sbio
->work
);
1989 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
)
1991 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
1992 struct scrub_ctx
*sctx
= sbio
->sctx
;
1995 WARN_ON(sbio
->page_count
> SCRUB_PAGES_PER_WR_BIO
);
1997 struct btrfs_dev_replace
*dev_replace
=
1998 &sbio
->sctx
->fs_info
->dev_replace
;
2000 for (i
= 0; i
< sbio
->page_count
; i
++) {
2001 struct scrub_page
*spage
= sbio
->pagev
[i
];
2003 spage
->io_error
= 1;
2004 btrfs_dev_replace_stats_inc(&dev_replace
->
2009 for (i
= 0; i
< sbio
->page_count
; i
++)
2010 scrub_page_put(sbio
->pagev
[i
]);
2014 scrub_pending_bio_dec(sctx
);
2017 static int scrub_checksum(struct scrub_block
*sblock
)
2023 * No need to initialize these stats currently,
2024 * because this function only use return value
2025 * instead of these stats value.
2030 sblock
->header_error
= 0;
2031 sblock
->generation_error
= 0;
2032 sblock
->checksum_error
= 0;
2034 WARN_ON(sblock
->page_count
< 1);
2035 flags
= sblock
->pagev
[0]->flags
;
2037 if (flags
& BTRFS_EXTENT_FLAG_DATA
)
2038 ret
= scrub_checksum_data(sblock
);
2039 else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
2040 ret
= scrub_checksum_tree_block(sblock
);
2041 else if (flags
& BTRFS_EXTENT_FLAG_SUPER
)
2042 (void)scrub_checksum_super(sblock
);
2046 scrub_handle_errored_block(sblock
);
2051 static int scrub_checksum_data(struct scrub_block
*sblock
)
2053 struct scrub_ctx
*sctx
= sblock
->sctx
;
2054 u8 csum
[BTRFS_CSUM_SIZE
];
2062 BUG_ON(sblock
->page_count
< 1);
2063 if (!sblock
->pagev
[0]->have_csum
)
2066 on_disk_csum
= sblock
->pagev
[0]->csum
;
2067 page
= sblock
->pagev
[0]->page
;
2068 buffer
= kmap_atomic(page
);
2070 len
= sctx
->fs_info
->sectorsize
;
2073 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2075 crc
= btrfs_csum_data(buffer
, crc
, l
);
2076 kunmap_atomic(buffer
);
2081 BUG_ON(index
>= sblock
->page_count
);
2082 BUG_ON(!sblock
->pagev
[index
]->page
);
2083 page
= sblock
->pagev
[index
]->page
;
2084 buffer
= kmap_atomic(page
);
2087 btrfs_csum_final(crc
, csum
);
2088 if (memcmp(csum
, on_disk_csum
, sctx
->csum_size
))
2089 sblock
->checksum_error
= 1;
2091 return sblock
->checksum_error
;
2094 static int scrub_checksum_tree_block(struct scrub_block
*sblock
)
2096 struct scrub_ctx
*sctx
= sblock
->sctx
;
2097 struct btrfs_header
*h
;
2098 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2099 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
2100 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
2102 void *mapped_buffer
;
2109 BUG_ON(sblock
->page_count
< 1);
2110 page
= sblock
->pagev
[0]->page
;
2111 mapped_buffer
= kmap_atomic(page
);
2112 h
= (struct btrfs_header
*)mapped_buffer
;
2113 memcpy(on_disk_csum
, h
->csum
, sctx
->csum_size
);
2116 * we don't use the getter functions here, as we
2117 * a) don't have an extent buffer and
2118 * b) the page is already kmapped
2120 if (sblock
->pagev
[0]->logical
!= btrfs_stack_header_bytenr(h
))
2121 sblock
->header_error
= 1;
2123 if (sblock
->pagev
[0]->generation
!= btrfs_stack_header_generation(h
)) {
2124 sblock
->header_error
= 1;
2125 sblock
->generation_error
= 1;
2128 if (!scrub_check_fsid(h
->fsid
, sblock
->pagev
[0]))
2129 sblock
->header_error
= 1;
2131 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
2133 sblock
->header_error
= 1;
2135 len
= sctx
->fs_info
->nodesize
- BTRFS_CSUM_SIZE
;
2136 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
2137 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
2140 u64 l
= min_t(u64
, len
, mapped_size
);
2142 crc
= btrfs_csum_data(p
, crc
, l
);
2143 kunmap_atomic(mapped_buffer
);
2148 BUG_ON(index
>= sblock
->page_count
);
2149 BUG_ON(!sblock
->pagev
[index
]->page
);
2150 page
= sblock
->pagev
[index
]->page
;
2151 mapped_buffer
= kmap_atomic(page
);
2152 mapped_size
= PAGE_SIZE
;
2156 btrfs_csum_final(crc
, calculated_csum
);
2157 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
2158 sblock
->checksum_error
= 1;
2160 return sblock
->header_error
|| sblock
->checksum_error
;
2163 static int scrub_checksum_super(struct scrub_block
*sblock
)
2165 struct btrfs_super_block
*s
;
2166 struct scrub_ctx
*sctx
= sblock
->sctx
;
2167 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
2168 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
2170 void *mapped_buffer
;
2179 BUG_ON(sblock
->page_count
< 1);
2180 page
= sblock
->pagev
[0]->page
;
2181 mapped_buffer
= kmap_atomic(page
);
2182 s
= (struct btrfs_super_block
*)mapped_buffer
;
2183 memcpy(on_disk_csum
, s
->csum
, sctx
->csum_size
);
2185 if (sblock
->pagev
[0]->logical
!= btrfs_super_bytenr(s
))
2188 if (sblock
->pagev
[0]->generation
!= btrfs_super_generation(s
))
2191 if (!scrub_check_fsid(s
->fsid
, sblock
->pagev
[0]))
2194 len
= BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
;
2195 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
2196 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
2199 u64 l
= min_t(u64
, len
, mapped_size
);
2201 crc
= btrfs_csum_data(p
, crc
, l
);
2202 kunmap_atomic(mapped_buffer
);
2207 BUG_ON(index
>= sblock
->page_count
);
2208 BUG_ON(!sblock
->pagev
[index
]->page
);
2209 page
= sblock
->pagev
[index
]->page
;
2210 mapped_buffer
= kmap_atomic(page
);
2211 mapped_size
= PAGE_SIZE
;
2215 btrfs_csum_final(crc
, calculated_csum
);
2216 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
2219 if (fail_cor
+ fail_gen
) {
2221 * if we find an error in a super block, we just report it.
2222 * They will get written with the next transaction commit
2225 spin_lock(&sctx
->stat_lock
);
2226 ++sctx
->stat
.super_errors
;
2227 spin_unlock(&sctx
->stat_lock
);
2229 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
2230 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
2232 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
2233 BTRFS_DEV_STAT_GENERATION_ERRS
);
2236 return fail_cor
+ fail_gen
;
2239 static void scrub_block_get(struct scrub_block
*sblock
)
2241 refcount_inc(&sblock
->refs
);
2244 static void scrub_block_put(struct scrub_block
*sblock
)
2246 if (refcount_dec_and_test(&sblock
->refs
)) {
2249 if (sblock
->sparity
)
2250 scrub_parity_put(sblock
->sparity
);
2252 for (i
= 0; i
< sblock
->page_count
; i
++)
2253 scrub_page_put(sblock
->pagev
[i
]);
2258 static void scrub_page_get(struct scrub_page
*spage
)
2260 atomic_inc(&spage
->refs
);
2263 static void scrub_page_put(struct scrub_page
*spage
)
2265 if (atomic_dec_and_test(&spage
->refs
)) {
2267 __free_page(spage
->page
);
2272 static void scrub_submit(struct scrub_ctx
*sctx
)
2274 struct scrub_bio
*sbio
;
2276 if (sctx
->curr
== -1)
2279 sbio
= sctx
->bios
[sctx
->curr
];
2281 scrub_pending_bio_inc(sctx
);
2282 btrfsic_submit_bio(sbio
->bio
);
2285 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
2286 struct scrub_page
*spage
)
2288 struct scrub_block
*sblock
= spage
->sblock
;
2289 struct scrub_bio
*sbio
;
2294 * grab a fresh bio or wait for one to become available
2296 while (sctx
->curr
== -1) {
2297 spin_lock(&sctx
->list_lock
);
2298 sctx
->curr
= sctx
->first_free
;
2299 if (sctx
->curr
!= -1) {
2300 sctx
->first_free
= sctx
->bios
[sctx
->curr
]->next_free
;
2301 sctx
->bios
[sctx
->curr
]->next_free
= -1;
2302 sctx
->bios
[sctx
->curr
]->page_count
= 0;
2303 spin_unlock(&sctx
->list_lock
);
2305 spin_unlock(&sctx
->list_lock
);
2306 wait_event(sctx
->list_wait
, sctx
->first_free
!= -1);
2309 sbio
= sctx
->bios
[sctx
->curr
];
2310 if (sbio
->page_count
== 0) {
2313 sbio
->physical
= spage
->physical
;
2314 sbio
->logical
= spage
->logical
;
2315 sbio
->dev
= spage
->dev
;
2318 bio
= btrfs_io_bio_alloc(sctx
->pages_per_rd_bio
);
2322 bio
->bi_private
= sbio
;
2323 bio
->bi_end_io
= scrub_bio_end_io
;
2324 bio_set_dev(bio
, sbio
->dev
->bdev
);
2325 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
2326 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
2328 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
2330 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
2332 sbio
->dev
!= spage
->dev
) {
2337 sbio
->pagev
[sbio
->page_count
] = spage
;
2338 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
2339 if (ret
!= PAGE_SIZE
) {
2340 if (sbio
->page_count
< 1) {
2349 scrub_block_get(sblock
); /* one for the page added to the bio */
2350 atomic_inc(&sblock
->outstanding_pages
);
2352 if (sbio
->page_count
== sctx
->pages_per_rd_bio
)
2358 static void scrub_missing_raid56_end_io(struct bio
*bio
)
2360 struct scrub_block
*sblock
= bio
->bi_private
;
2361 struct btrfs_fs_info
*fs_info
= sblock
->sctx
->fs_info
;
2364 sblock
->no_io_error_seen
= 0;
2368 btrfs_queue_work(fs_info
->scrub_workers
, &sblock
->work
);
2371 static void scrub_missing_raid56_worker(struct btrfs_work
*work
)
2373 struct scrub_block
*sblock
= container_of(work
, struct scrub_block
, work
);
2374 struct scrub_ctx
*sctx
= sblock
->sctx
;
2375 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2377 struct btrfs_device
*dev
;
2379 logical
= sblock
->pagev
[0]->logical
;
2380 dev
= sblock
->pagev
[0]->dev
;
2382 if (sblock
->no_io_error_seen
)
2383 scrub_recheck_block_checksum(sblock
);
2385 if (!sblock
->no_io_error_seen
) {
2386 spin_lock(&sctx
->stat_lock
);
2387 sctx
->stat
.read_errors
++;
2388 spin_unlock(&sctx
->stat_lock
);
2389 btrfs_err_rl_in_rcu(fs_info
,
2390 "IO error rebuilding logical %llu for dev %s",
2391 logical
, rcu_str_deref(dev
->name
));
2392 } else if (sblock
->header_error
|| sblock
->checksum_error
) {
2393 spin_lock(&sctx
->stat_lock
);
2394 sctx
->stat
.uncorrectable_errors
++;
2395 spin_unlock(&sctx
->stat_lock
);
2396 btrfs_err_rl_in_rcu(fs_info
,
2397 "failed to rebuild valid logical %llu for dev %s",
2398 logical
, rcu_str_deref(dev
->name
));
2400 scrub_write_block_to_dev_replace(sblock
);
2403 scrub_block_put(sblock
);
2405 if (sctx
->is_dev_replace
&& sctx
->flush_all_writes
) {
2406 mutex_lock(&sctx
->wr_lock
);
2407 scrub_wr_submit(sctx
);
2408 mutex_unlock(&sctx
->wr_lock
);
2411 scrub_pending_bio_dec(sctx
);
2414 static void scrub_missing_raid56_pages(struct scrub_block
*sblock
)
2416 struct scrub_ctx
*sctx
= sblock
->sctx
;
2417 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
2418 u64 length
= sblock
->page_count
* PAGE_SIZE
;
2419 u64 logical
= sblock
->pagev
[0]->logical
;
2420 struct btrfs_bio
*bbio
= NULL
;
2422 struct btrfs_raid_bio
*rbio
;
2426 btrfs_bio_counter_inc_blocked(fs_info
);
2427 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
, logical
,
2429 if (ret
|| !bbio
|| !bbio
->raid_map
)
2432 if (WARN_ON(!sctx
->is_dev_replace
||
2433 !(bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))) {
2435 * We shouldn't be scrubbing a missing device. Even for dev
2436 * replace, we should only get here for RAID 5/6. We either
2437 * managed to mount something with no mirrors remaining or
2438 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2443 bio
= btrfs_io_bio_alloc(0);
2444 bio
->bi_iter
.bi_sector
= logical
>> 9;
2445 bio
->bi_private
= sblock
;
2446 bio
->bi_end_io
= scrub_missing_raid56_end_io
;
2448 rbio
= raid56_alloc_missing_rbio(fs_info
, bio
, bbio
, length
);
2452 for (i
= 0; i
< sblock
->page_count
; i
++) {
2453 struct scrub_page
*spage
= sblock
->pagev
[i
];
2455 raid56_add_scrub_pages(rbio
, spage
->page
, spage
->logical
);
2458 btrfs_init_work(&sblock
->work
, btrfs_scrub_helper
,
2459 scrub_missing_raid56_worker
, NULL
, NULL
);
2460 scrub_block_get(sblock
);
2461 scrub_pending_bio_inc(sctx
);
2462 raid56_submit_missing_rbio(rbio
);
2468 btrfs_bio_counter_dec(fs_info
);
2469 btrfs_put_bbio(bbio
);
2470 spin_lock(&sctx
->stat_lock
);
2471 sctx
->stat
.malloc_errors
++;
2472 spin_unlock(&sctx
->stat_lock
);
2475 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2476 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2477 u64 gen
, int mirror_num
, u8
*csum
, int force
,
2478 u64 physical_for_dev_replace
)
2480 struct scrub_block
*sblock
;
2483 sblock
= kzalloc(sizeof(*sblock
), GFP_KERNEL
);
2485 spin_lock(&sctx
->stat_lock
);
2486 sctx
->stat
.malloc_errors
++;
2487 spin_unlock(&sctx
->stat_lock
);
2491 /* one ref inside this function, plus one for each page added to
2493 refcount_set(&sblock
->refs
, 1);
2494 sblock
->sctx
= sctx
;
2495 sblock
->no_io_error_seen
= 1;
2497 for (index
= 0; len
> 0; index
++) {
2498 struct scrub_page
*spage
;
2499 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2501 spage
= kzalloc(sizeof(*spage
), GFP_KERNEL
);
2504 spin_lock(&sctx
->stat_lock
);
2505 sctx
->stat
.malloc_errors
++;
2506 spin_unlock(&sctx
->stat_lock
);
2507 scrub_block_put(sblock
);
2510 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2511 scrub_page_get(spage
);
2512 sblock
->pagev
[index
] = spage
;
2513 spage
->sblock
= sblock
;
2515 spage
->flags
= flags
;
2516 spage
->generation
= gen
;
2517 spage
->logical
= logical
;
2518 spage
->physical
= physical
;
2519 spage
->physical_for_dev_replace
= physical_for_dev_replace
;
2520 spage
->mirror_num
= mirror_num
;
2522 spage
->have_csum
= 1;
2523 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2525 spage
->have_csum
= 0;
2527 sblock
->page_count
++;
2528 spage
->page
= alloc_page(GFP_KERNEL
);
2534 physical_for_dev_replace
+= l
;
2537 WARN_ON(sblock
->page_count
== 0);
2540 * This case should only be hit for RAID 5/6 device replace. See
2541 * the comment in scrub_missing_raid56_pages() for details.
2543 scrub_missing_raid56_pages(sblock
);
2545 for (index
= 0; index
< sblock
->page_count
; index
++) {
2546 struct scrub_page
*spage
= sblock
->pagev
[index
];
2549 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2551 scrub_block_put(sblock
);
2560 /* last one frees, either here or in bio completion for last page */
2561 scrub_block_put(sblock
);
2565 static void scrub_bio_end_io(struct bio
*bio
)
2567 struct scrub_bio
*sbio
= bio
->bi_private
;
2568 struct btrfs_fs_info
*fs_info
= sbio
->dev
->fs_info
;
2570 sbio
->status
= bio
->bi_status
;
2573 btrfs_queue_work(fs_info
->scrub_workers
, &sbio
->work
);
2576 static void scrub_bio_end_io_worker(struct btrfs_work
*work
)
2578 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
2579 struct scrub_ctx
*sctx
= sbio
->sctx
;
2582 BUG_ON(sbio
->page_count
> SCRUB_PAGES_PER_RD_BIO
);
2584 for (i
= 0; i
< sbio
->page_count
; i
++) {
2585 struct scrub_page
*spage
= sbio
->pagev
[i
];
2587 spage
->io_error
= 1;
2588 spage
->sblock
->no_io_error_seen
= 0;
2592 /* now complete the scrub_block items that have all pages completed */
2593 for (i
= 0; i
< sbio
->page_count
; i
++) {
2594 struct scrub_page
*spage
= sbio
->pagev
[i
];
2595 struct scrub_block
*sblock
= spage
->sblock
;
2597 if (atomic_dec_and_test(&sblock
->outstanding_pages
))
2598 scrub_block_complete(sblock
);
2599 scrub_block_put(sblock
);
2604 spin_lock(&sctx
->list_lock
);
2605 sbio
->next_free
= sctx
->first_free
;
2606 sctx
->first_free
= sbio
->index
;
2607 spin_unlock(&sctx
->list_lock
);
2609 if (sctx
->is_dev_replace
&& sctx
->flush_all_writes
) {
2610 mutex_lock(&sctx
->wr_lock
);
2611 scrub_wr_submit(sctx
);
2612 mutex_unlock(&sctx
->wr_lock
);
2615 scrub_pending_bio_dec(sctx
);
2618 static inline void __scrub_mark_bitmap(struct scrub_parity
*sparity
,
2619 unsigned long *bitmap
,
2625 int sectorsize
= sparity
->sctx
->fs_info
->sectorsize
;
2627 if (len
>= sparity
->stripe_len
) {
2628 bitmap_set(bitmap
, 0, sparity
->nsectors
);
2632 start
-= sparity
->logic_start
;
2633 start
= div64_u64_rem(start
, sparity
->stripe_len
, &offset
);
2634 offset
= div_u64(offset
, sectorsize
);
2635 nsectors64
= div_u64(len
, sectorsize
);
2637 ASSERT(nsectors64
< UINT_MAX
);
2638 nsectors
= (u32
)nsectors64
;
2640 if (offset
+ nsectors
<= sparity
->nsectors
) {
2641 bitmap_set(bitmap
, offset
, nsectors
);
2645 bitmap_set(bitmap
, offset
, sparity
->nsectors
- offset
);
2646 bitmap_set(bitmap
, 0, nsectors
- (sparity
->nsectors
- offset
));
2649 static inline void scrub_parity_mark_sectors_error(struct scrub_parity
*sparity
,
2652 __scrub_mark_bitmap(sparity
, sparity
->ebitmap
, start
, len
);
2655 static inline void scrub_parity_mark_sectors_data(struct scrub_parity
*sparity
,
2658 __scrub_mark_bitmap(sparity
, sparity
->dbitmap
, start
, len
);
2661 static void scrub_block_complete(struct scrub_block
*sblock
)
2665 if (!sblock
->no_io_error_seen
) {
2667 scrub_handle_errored_block(sblock
);
2670 * if has checksum error, write via repair mechanism in
2671 * dev replace case, otherwise write here in dev replace
2674 corrupted
= scrub_checksum(sblock
);
2675 if (!corrupted
&& sblock
->sctx
->is_dev_replace
)
2676 scrub_write_block_to_dev_replace(sblock
);
2679 if (sblock
->sparity
&& corrupted
&& !sblock
->data_corrected
) {
2680 u64 start
= sblock
->pagev
[0]->logical
;
2681 u64 end
= sblock
->pagev
[sblock
->page_count
- 1]->logical
+
2684 scrub_parity_mark_sectors_error(sblock
->sparity
,
2685 start
, end
- start
);
2689 static int scrub_find_csum(struct scrub_ctx
*sctx
, u64 logical
, u8
*csum
)
2691 struct btrfs_ordered_sum
*sum
= NULL
;
2692 unsigned long index
;
2693 unsigned long num_sectors
;
2695 while (!list_empty(&sctx
->csum_list
)) {
2696 sum
= list_first_entry(&sctx
->csum_list
,
2697 struct btrfs_ordered_sum
, list
);
2698 if (sum
->bytenr
> logical
)
2700 if (sum
->bytenr
+ sum
->len
> logical
)
2703 ++sctx
->stat
.csum_discards
;
2704 list_del(&sum
->list
);
2711 index
= div_u64(logical
- sum
->bytenr
, sctx
->fs_info
->sectorsize
);
2712 ASSERT(index
< UINT_MAX
);
2714 num_sectors
= sum
->len
/ sctx
->fs_info
->sectorsize
;
2715 memcpy(csum
, sum
->sums
+ index
, sctx
->csum_size
);
2716 if (index
== num_sectors
- 1) {
2717 list_del(&sum
->list
);
2723 /* scrub extent tries to collect up to 64 kB for each bio */
2724 static int scrub_extent(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2725 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2726 u64 gen
, int mirror_num
, u64 physical_for_dev_replace
)
2729 u8 csum
[BTRFS_CSUM_SIZE
];
2732 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2733 blocksize
= sctx
->fs_info
->sectorsize
;
2734 spin_lock(&sctx
->stat_lock
);
2735 sctx
->stat
.data_extents_scrubbed
++;
2736 sctx
->stat
.data_bytes_scrubbed
+= len
;
2737 spin_unlock(&sctx
->stat_lock
);
2738 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2739 blocksize
= sctx
->fs_info
->nodesize
;
2740 spin_lock(&sctx
->stat_lock
);
2741 sctx
->stat
.tree_extents_scrubbed
++;
2742 sctx
->stat
.tree_bytes_scrubbed
+= len
;
2743 spin_unlock(&sctx
->stat_lock
);
2745 blocksize
= sctx
->fs_info
->sectorsize
;
2750 u64 l
= min_t(u64
, len
, blocksize
);
2753 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2754 /* push csums to sbio */
2755 have_csum
= scrub_find_csum(sctx
, logical
, csum
);
2757 ++sctx
->stat
.no_csum
;
2758 if (sctx
->is_dev_replace
&& !have_csum
) {
2759 ret
= copy_nocow_pages(sctx
, logical
, l
,
2761 physical_for_dev_replace
);
2762 goto behind_scrub_pages
;
2765 ret
= scrub_pages(sctx
, logical
, l
, physical
, dev
, flags
, gen
,
2766 mirror_num
, have_csum
? csum
: NULL
, 0,
2767 physical_for_dev_replace
);
2774 physical_for_dev_replace
+= l
;
2779 static int scrub_pages_for_parity(struct scrub_parity
*sparity
,
2780 u64 logical
, u64 len
,
2781 u64 physical
, struct btrfs_device
*dev
,
2782 u64 flags
, u64 gen
, int mirror_num
, u8
*csum
)
2784 struct scrub_ctx
*sctx
= sparity
->sctx
;
2785 struct scrub_block
*sblock
;
2788 sblock
= kzalloc(sizeof(*sblock
), GFP_KERNEL
);
2790 spin_lock(&sctx
->stat_lock
);
2791 sctx
->stat
.malloc_errors
++;
2792 spin_unlock(&sctx
->stat_lock
);
2796 /* one ref inside this function, plus one for each page added to
2798 refcount_set(&sblock
->refs
, 1);
2799 sblock
->sctx
= sctx
;
2800 sblock
->no_io_error_seen
= 1;
2801 sblock
->sparity
= sparity
;
2802 scrub_parity_get(sparity
);
2804 for (index
= 0; len
> 0; index
++) {
2805 struct scrub_page
*spage
;
2806 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2808 spage
= kzalloc(sizeof(*spage
), GFP_KERNEL
);
2811 spin_lock(&sctx
->stat_lock
);
2812 sctx
->stat
.malloc_errors
++;
2813 spin_unlock(&sctx
->stat_lock
);
2814 scrub_block_put(sblock
);
2817 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2818 /* For scrub block */
2819 scrub_page_get(spage
);
2820 sblock
->pagev
[index
] = spage
;
2821 /* For scrub parity */
2822 scrub_page_get(spage
);
2823 list_add_tail(&spage
->list
, &sparity
->spages
);
2824 spage
->sblock
= sblock
;
2826 spage
->flags
= flags
;
2827 spage
->generation
= gen
;
2828 spage
->logical
= logical
;
2829 spage
->physical
= physical
;
2830 spage
->mirror_num
= mirror_num
;
2832 spage
->have_csum
= 1;
2833 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2835 spage
->have_csum
= 0;
2837 sblock
->page_count
++;
2838 spage
->page
= alloc_page(GFP_KERNEL
);
2846 WARN_ON(sblock
->page_count
== 0);
2847 for (index
= 0; index
< sblock
->page_count
; index
++) {
2848 struct scrub_page
*spage
= sblock
->pagev
[index
];
2851 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2853 scrub_block_put(sblock
);
2858 /* last one frees, either here or in bio completion for last page */
2859 scrub_block_put(sblock
);
2863 static int scrub_extent_for_parity(struct scrub_parity
*sparity
,
2864 u64 logical
, u64 len
,
2865 u64 physical
, struct btrfs_device
*dev
,
2866 u64 flags
, u64 gen
, int mirror_num
)
2868 struct scrub_ctx
*sctx
= sparity
->sctx
;
2870 u8 csum
[BTRFS_CSUM_SIZE
];
2874 scrub_parity_mark_sectors_error(sparity
, logical
, len
);
2878 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2879 blocksize
= sctx
->fs_info
->sectorsize
;
2880 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2881 blocksize
= sctx
->fs_info
->nodesize
;
2883 blocksize
= sctx
->fs_info
->sectorsize
;
2888 u64 l
= min_t(u64
, len
, blocksize
);
2891 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2892 /* push csums to sbio */
2893 have_csum
= scrub_find_csum(sctx
, logical
, csum
);
2897 ret
= scrub_pages_for_parity(sparity
, logical
, l
, physical
, dev
,
2898 flags
, gen
, mirror_num
,
2899 have_csum
? csum
: NULL
);
2911 * Given a physical address, this will calculate it's
2912 * logical offset. if this is a parity stripe, it will return
2913 * the most left data stripe's logical offset.
2915 * return 0 if it is a data stripe, 1 means parity stripe.
2917 static int get_raid56_logic_offset(u64 physical
, int num
,
2918 struct map_lookup
*map
, u64
*offset
,
2928 last_offset
= (physical
- map
->stripes
[num
].physical
) *
2929 nr_data_stripes(map
);
2931 *stripe_start
= last_offset
;
2933 *offset
= last_offset
;
2934 for (i
= 0; i
< nr_data_stripes(map
); i
++) {
2935 *offset
= last_offset
+ i
* map
->stripe_len
;
2937 stripe_nr
= div64_u64(*offset
, map
->stripe_len
);
2938 stripe_nr
= div_u64(stripe_nr
, nr_data_stripes(map
));
2940 /* Work out the disk rotation on this stripe-set */
2941 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
, &rot
);
2942 /* calculate which stripe this data locates */
2944 stripe_index
= rot
% map
->num_stripes
;
2945 if (stripe_index
== num
)
2947 if (stripe_index
< num
)
2950 *offset
= last_offset
+ j
* map
->stripe_len
;
2954 static void scrub_free_parity(struct scrub_parity
*sparity
)
2956 struct scrub_ctx
*sctx
= sparity
->sctx
;
2957 struct scrub_page
*curr
, *next
;
2960 nbits
= bitmap_weight(sparity
->ebitmap
, sparity
->nsectors
);
2962 spin_lock(&sctx
->stat_lock
);
2963 sctx
->stat
.read_errors
+= nbits
;
2964 sctx
->stat
.uncorrectable_errors
+= nbits
;
2965 spin_unlock(&sctx
->stat_lock
);
2968 list_for_each_entry_safe(curr
, next
, &sparity
->spages
, list
) {
2969 list_del_init(&curr
->list
);
2970 scrub_page_put(curr
);
2976 static void scrub_parity_bio_endio_worker(struct btrfs_work
*work
)
2978 struct scrub_parity
*sparity
= container_of(work
, struct scrub_parity
,
2980 struct scrub_ctx
*sctx
= sparity
->sctx
;
2982 scrub_free_parity(sparity
);
2983 scrub_pending_bio_dec(sctx
);
2986 static void scrub_parity_bio_endio(struct bio
*bio
)
2988 struct scrub_parity
*sparity
= (struct scrub_parity
*)bio
->bi_private
;
2989 struct btrfs_fs_info
*fs_info
= sparity
->sctx
->fs_info
;
2992 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
2997 btrfs_init_work(&sparity
->work
, btrfs_scrubparity_helper
,
2998 scrub_parity_bio_endio_worker
, NULL
, NULL
);
2999 btrfs_queue_work(fs_info
->scrub_parity_workers
, &sparity
->work
);
3002 static void scrub_parity_check_and_repair(struct scrub_parity
*sparity
)
3004 struct scrub_ctx
*sctx
= sparity
->sctx
;
3005 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3007 struct btrfs_raid_bio
*rbio
;
3008 struct btrfs_bio
*bbio
= NULL
;
3012 if (!bitmap_andnot(sparity
->dbitmap
, sparity
->dbitmap
, sparity
->ebitmap
,
3016 length
= sparity
->logic_end
- sparity
->logic_start
;
3018 btrfs_bio_counter_inc_blocked(fs_info
);
3019 ret
= btrfs_map_sblock(fs_info
, BTRFS_MAP_WRITE
, sparity
->logic_start
,
3021 if (ret
|| !bbio
|| !bbio
->raid_map
)
3024 bio
= btrfs_io_bio_alloc(0);
3025 bio
->bi_iter
.bi_sector
= sparity
->logic_start
>> 9;
3026 bio
->bi_private
= sparity
;
3027 bio
->bi_end_io
= scrub_parity_bio_endio
;
3029 rbio
= raid56_parity_alloc_scrub_rbio(fs_info
, bio
, bbio
,
3030 length
, sparity
->scrub_dev
,
3036 scrub_pending_bio_inc(sctx
);
3037 raid56_parity_submit_scrub_rbio(rbio
);
3043 btrfs_bio_counter_dec(fs_info
);
3044 btrfs_put_bbio(bbio
);
3045 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
3047 spin_lock(&sctx
->stat_lock
);
3048 sctx
->stat
.malloc_errors
++;
3049 spin_unlock(&sctx
->stat_lock
);
3051 scrub_free_parity(sparity
);
3054 static inline int scrub_calc_parity_bitmap_len(int nsectors
)
3056 return DIV_ROUND_UP(nsectors
, BITS_PER_LONG
) * sizeof(long);
3059 static void scrub_parity_get(struct scrub_parity
*sparity
)
3061 refcount_inc(&sparity
->refs
);
3064 static void scrub_parity_put(struct scrub_parity
*sparity
)
3066 if (!refcount_dec_and_test(&sparity
->refs
))
3069 scrub_parity_check_and_repair(sparity
);
3072 static noinline_for_stack
int scrub_raid56_parity(struct scrub_ctx
*sctx
,
3073 struct map_lookup
*map
,
3074 struct btrfs_device
*sdev
,
3075 struct btrfs_path
*path
,
3079 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3080 struct btrfs_root
*root
= fs_info
->extent_root
;
3081 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
3082 struct btrfs_extent_item
*extent
;
3083 struct btrfs_bio
*bbio
= NULL
;
3087 struct extent_buffer
*l
;
3088 struct btrfs_key key
;
3091 u64 extent_physical
;
3094 struct btrfs_device
*extent_dev
;
3095 struct scrub_parity
*sparity
;
3098 int extent_mirror_num
;
3101 nsectors
= div_u64(map
->stripe_len
, fs_info
->sectorsize
);
3102 bitmap_len
= scrub_calc_parity_bitmap_len(nsectors
);
3103 sparity
= kzalloc(sizeof(struct scrub_parity
) + 2 * bitmap_len
,
3106 spin_lock(&sctx
->stat_lock
);
3107 sctx
->stat
.malloc_errors
++;
3108 spin_unlock(&sctx
->stat_lock
);
3112 sparity
->stripe_len
= map
->stripe_len
;
3113 sparity
->nsectors
= nsectors
;
3114 sparity
->sctx
= sctx
;
3115 sparity
->scrub_dev
= sdev
;
3116 sparity
->logic_start
= logic_start
;
3117 sparity
->logic_end
= logic_end
;
3118 refcount_set(&sparity
->refs
, 1);
3119 INIT_LIST_HEAD(&sparity
->spages
);
3120 sparity
->dbitmap
= sparity
->bitmap
;
3121 sparity
->ebitmap
= (void *)sparity
->bitmap
+ bitmap_len
;
3124 while (logic_start
< logic_end
) {
3125 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
3126 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3128 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3129 key
.objectid
= logic_start
;
3130 key
.offset
= (u64
)-1;
3132 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3137 ret
= btrfs_previous_extent_item(root
, path
, 0);
3141 btrfs_release_path(path
);
3142 ret
= btrfs_search_slot(NULL
, root
, &key
,
3154 slot
= path
->slots
[0];
3155 if (slot
>= btrfs_header_nritems(l
)) {
3156 ret
= btrfs_next_leaf(root
, path
);
3165 btrfs_item_key_to_cpu(l
, &key
, slot
);
3167 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3168 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
3171 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
3172 bytes
= fs_info
->nodesize
;
3176 if (key
.objectid
+ bytes
<= logic_start
)
3179 if (key
.objectid
>= logic_end
) {
3184 while (key
.objectid
>= logic_start
+ map
->stripe_len
)
3185 logic_start
+= map
->stripe_len
;
3187 extent
= btrfs_item_ptr(l
, slot
,
3188 struct btrfs_extent_item
);
3189 flags
= btrfs_extent_flags(l
, extent
);
3190 generation
= btrfs_extent_generation(l
, extent
);
3192 if ((flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) &&
3193 (key
.objectid
< logic_start
||
3194 key
.objectid
+ bytes
>
3195 logic_start
+ map
->stripe_len
)) {
3197 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3198 key
.objectid
, logic_start
);
3199 spin_lock(&sctx
->stat_lock
);
3200 sctx
->stat
.uncorrectable_errors
++;
3201 spin_unlock(&sctx
->stat_lock
);
3205 extent_logical
= key
.objectid
;
3208 if (extent_logical
< logic_start
) {
3209 extent_len
-= logic_start
- extent_logical
;
3210 extent_logical
= logic_start
;
3213 if (extent_logical
+ extent_len
>
3214 logic_start
+ map
->stripe_len
)
3215 extent_len
= logic_start
+ map
->stripe_len
-
3218 scrub_parity_mark_sectors_data(sparity
, extent_logical
,
3221 mapped_length
= extent_len
;
3223 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_READ
,
3224 extent_logical
, &mapped_length
, &bbio
,
3227 if (!bbio
|| mapped_length
< extent_len
)
3231 btrfs_put_bbio(bbio
);
3234 extent_physical
= bbio
->stripes
[0].physical
;
3235 extent_mirror_num
= bbio
->mirror_num
;
3236 extent_dev
= bbio
->stripes
[0].dev
;
3237 btrfs_put_bbio(bbio
);
3239 ret
= btrfs_lookup_csums_range(csum_root
,
3241 extent_logical
+ extent_len
- 1,
3242 &sctx
->csum_list
, 1);
3246 ret
= scrub_extent_for_parity(sparity
, extent_logical
,
3253 scrub_free_csums(sctx
);
3258 if (extent_logical
+ extent_len
<
3259 key
.objectid
+ bytes
) {
3260 logic_start
+= map
->stripe_len
;
3262 if (logic_start
>= logic_end
) {
3267 if (logic_start
< key
.objectid
+ bytes
) {
3276 btrfs_release_path(path
);
3281 logic_start
+= map
->stripe_len
;
3285 scrub_parity_mark_sectors_error(sparity
, logic_start
,
3286 logic_end
- logic_start
);
3287 scrub_parity_put(sparity
);
3289 mutex_lock(&sctx
->wr_lock
);
3290 scrub_wr_submit(sctx
);
3291 mutex_unlock(&sctx
->wr_lock
);
3293 btrfs_release_path(path
);
3294 return ret
< 0 ? ret
: 0;
3297 static noinline_for_stack
int scrub_stripe(struct scrub_ctx
*sctx
,
3298 struct map_lookup
*map
,
3299 struct btrfs_device
*scrub_dev
,
3300 int num
, u64 base
, u64 length
,
3303 struct btrfs_path
*path
, *ppath
;
3304 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3305 struct btrfs_root
*root
= fs_info
->extent_root
;
3306 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
3307 struct btrfs_extent_item
*extent
;
3308 struct blk_plug plug
;
3313 struct extent_buffer
*l
;
3320 struct reada_control
*reada1
;
3321 struct reada_control
*reada2
;
3322 struct btrfs_key key
;
3323 struct btrfs_key key_end
;
3324 u64 increment
= map
->stripe_len
;
3327 u64 extent_physical
;
3331 struct btrfs_device
*extent_dev
;
3332 int extent_mirror_num
;
3335 physical
= map
->stripes
[num
].physical
;
3337 nstripes
= div64_u64(length
, map
->stripe_len
);
3338 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3339 offset
= map
->stripe_len
* num
;
3340 increment
= map
->stripe_len
* map
->num_stripes
;
3342 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3343 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3344 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
3345 increment
= map
->stripe_len
* factor
;
3346 mirror_num
= num
% map
->sub_stripes
+ 1;
3347 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3348 increment
= map
->stripe_len
;
3349 mirror_num
= num
% map
->num_stripes
+ 1;
3350 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3351 increment
= map
->stripe_len
;
3352 mirror_num
= num
% map
->num_stripes
+ 1;
3353 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3354 get_raid56_logic_offset(physical
, num
, map
, &offset
, NULL
);
3355 increment
= map
->stripe_len
* nr_data_stripes(map
);
3358 increment
= map
->stripe_len
;
3362 path
= btrfs_alloc_path();
3366 ppath
= btrfs_alloc_path();
3368 btrfs_free_path(path
);
3373 * work on commit root. The related disk blocks are static as
3374 * long as COW is applied. This means, it is save to rewrite
3375 * them to repair disk errors without any race conditions
3377 path
->search_commit_root
= 1;
3378 path
->skip_locking
= 1;
3380 ppath
->search_commit_root
= 1;
3381 ppath
->skip_locking
= 1;
3383 * trigger the readahead for extent tree csum tree and wait for
3384 * completion. During readahead, the scrub is officially paused
3385 * to not hold off transaction commits
3387 logical
= base
+ offset
;
3388 physical_end
= physical
+ nstripes
* map
->stripe_len
;
3389 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3390 get_raid56_logic_offset(physical_end
, num
,
3391 map
, &logic_end
, NULL
);
3394 logic_end
= logical
+ increment
* nstripes
;
3396 wait_event(sctx
->list_wait
,
3397 atomic_read(&sctx
->bios_in_flight
) == 0);
3398 scrub_blocked_if_needed(fs_info
);
3400 /* FIXME it might be better to start readahead at commit root */
3401 key
.objectid
= logical
;
3402 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3403 key
.offset
= (u64
)0;
3404 key_end
.objectid
= logic_end
;
3405 key_end
.type
= BTRFS_METADATA_ITEM_KEY
;
3406 key_end
.offset
= (u64
)-1;
3407 reada1
= btrfs_reada_add(root
, &key
, &key_end
);
3409 key
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3410 key
.type
= BTRFS_EXTENT_CSUM_KEY
;
3411 key
.offset
= logical
;
3412 key_end
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3413 key_end
.type
= BTRFS_EXTENT_CSUM_KEY
;
3414 key_end
.offset
= logic_end
;
3415 reada2
= btrfs_reada_add(csum_root
, &key
, &key_end
);
3417 if (!IS_ERR(reada1
))
3418 btrfs_reada_wait(reada1
);
3419 if (!IS_ERR(reada2
))
3420 btrfs_reada_wait(reada2
);
3424 * collect all data csums for the stripe to avoid seeking during
3425 * the scrub. This might currently (crc32) end up to be about 1MB
3427 blk_start_plug(&plug
);
3430 * now find all extents for each stripe and scrub them
3433 while (physical
< physical_end
) {
3437 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
3438 atomic_read(&sctx
->cancel_req
)) {
3443 * check to see if we have to pause
3445 if (atomic_read(&fs_info
->scrub_pause_req
)) {
3446 /* push queued extents */
3447 sctx
->flush_all_writes
= true;
3449 mutex_lock(&sctx
->wr_lock
);
3450 scrub_wr_submit(sctx
);
3451 mutex_unlock(&sctx
->wr_lock
);
3452 wait_event(sctx
->list_wait
,
3453 atomic_read(&sctx
->bios_in_flight
) == 0);
3454 sctx
->flush_all_writes
= false;
3455 scrub_blocked_if_needed(fs_info
);
3458 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3459 ret
= get_raid56_logic_offset(physical
, num
, map
,
3464 /* it is parity strip */
3465 stripe_logical
+= base
;
3466 stripe_end
= stripe_logical
+ increment
;
3467 ret
= scrub_raid56_parity(sctx
, map
, scrub_dev
,
3468 ppath
, stripe_logical
,
3476 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
3477 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3479 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3480 key
.objectid
= logical
;
3481 key
.offset
= (u64
)-1;
3483 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3488 ret
= btrfs_previous_extent_item(root
, path
, 0);
3492 /* there's no smaller item, so stick with the
3494 btrfs_release_path(path
);
3495 ret
= btrfs_search_slot(NULL
, root
, &key
,
3507 slot
= path
->slots
[0];
3508 if (slot
>= btrfs_header_nritems(l
)) {
3509 ret
= btrfs_next_leaf(root
, path
);
3518 btrfs_item_key_to_cpu(l
, &key
, slot
);
3520 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3521 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
3524 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
3525 bytes
= fs_info
->nodesize
;
3529 if (key
.objectid
+ bytes
<= logical
)
3532 if (key
.objectid
>= logical
+ map
->stripe_len
) {
3533 /* out of this device extent */
3534 if (key
.objectid
>= logic_end
)
3539 extent
= btrfs_item_ptr(l
, slot
,
3540 struct btrfs_extent_item
);
3541 flags
= btrfs_extent_flags(l
, extent
);
3542 generation
= btrfs_extent_generation(l
, extent
);
3544 if ((flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) &&
3545 (key
.objectid
< logical
||
3546 key
.objectid
+ bytes
>
3547 logical
+ map
->stripe_len
)) {
3549 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3550 key
.objectid
, logical
);
3551 spin_lock(&sctx
->stat_lock
);
3552 sctx
->stat
.uncorrectable_errors
++;
3553 spin_unlock(&sctx
->stat_lock
);
3558 extent_logical
= key
.objectid
;
3562 * trim extent to this stripe
3564 if (extent_logical
< logical
) {
3565 extent_len
-= logical
- extent_logical
;
3566 extent_logical
= logical
;
3568 if (extent_logical
+ extent_len
>
3569 logical
+ map
->stripe_len
) {
3570 extent_len
= logical
+ map
->stripe_len
-
3574 extent_physical
= extent_logical
- logical
+ physical
;
3575 extent_dev
= scrub_dev
;
3576 extent_mirror_num
= mirror_num
;
3578 scrub_remap_extent(fs_info
, extent_logical
,
3579 extent_len
, &extent_physical
,
3581 &extent_mirror_num
);
3583 ret
= btrfs_lookup_csums_range(csum_root
,
3587 &sctx
->csum_list
, 1);
3591 ret
= scrub_extent(sctx
, extent_logical
, extent_len
,
3592 extent_physical
, extent_dev
, flags
,
3593 generation
, extent_mirror_num
,
3594 extent_logical
- logical
+ physical
);
3596 scrub_free_csums(sctx
);
3601 if (extent_logical
+ extent_len
<
3602 key
.objectid
+ bytes
) {
3603 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3605 * loop until we find next data stripe
3606 * or we have finished all stripes.
3609 physical
+= map
->stripe_len
;
3610 ret
= get_raid56_logic_offset(physical
,
3615 if (ret
&& physical
< physical_end
) {
3616 stripe_logical
+= base
;
3617 stripe_end
= stripe_logical
+
3619 ret
= scrub_raid56_parity(sctx
,
3620 map
, scrub_dev
, ppath
,
3628 physical
+= map
->stripe_len
;
3629 logical
+= increment
;
3631 if (logical
< key
.objectid
+ bytes
) {
3636 if (physical
>= physical_end
) {
3644 btrfs_release_path(path
);
3646 logical
+= increment
;
3647 physical
+= map
->stripe_len
;
3648 spin_lock(&sctx
->stat_lock
);
3650 sctx
->stat
.last_physical
= map
->stripes
[num
].physical
+
3653 sctx
->stat
.last_physical
= physical
;
3654 spin_unlock(&sctx
->stat_lock
);
3659 /* push queued extents */
3661 mutex_lock(&sctx
->wr_lock
);
3662 scrub_wr_submit(sctx
);
3663 mutex_unlock(&sctx
->wr_lock
);
3665 blk_finish_plug(&plug
);
3666 btrfs_free_path(path
);
3667 btrfs_free_path(ppath
);
3668 return ret
< 0 ? ret
: 0;
3671 static noinline_for_stack
int scrub_chunk(struct scrub_ctx
*sctx
,
3672 struct btrfs_device
*scrub_dev
,
3673 u64 chunk_offset
, u64 length
,
3675 struct btrfs_block_group_cache
*cache
,
3678 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3679 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3680 struct map_lookup
*map
;
3681 struct extent_map
*em
;
3685 read_lock(&map_tree
->map_tree
.lock
);
3686 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3687 read_unlock(&map_tree
->map_tree
.lock
);
3691 * Might have been an unused block group deleted by the cleaner
3692 * kthread or relocation.
3694 spin_lock(&cache
->lock
);
3695 if (!cache
->removed
)
3697 spin_unlock(&cache
->lock
);
3702 map
= em
->map_lookup
;
3703 if (em
->start
!= chunk_offset
)
3706 if (em
->len
< length
)
3709 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3710 if (map
->stripes
[i
].dev
->bdev
== scrub_dev
->bdev
&&
3711 map
->stripes
[i
].physical
== dev_offset
) {
3712 ret
= scrub_stripe(sctx
, map
, scrub_dev
, i
,
3713 chunk_offset
, length
,
3720 free_extent_map(em
);
3725 static noinline_for_stack
3726 int scrub_enumerate_chunks(struct scrub_ctx
*sctx
,
3727 struct btrfs_device
*scrub_dev
, u64 start
, u64 end
,
3730 struct btrfs_dev_extent
*dev_extent
= NULL
;
3731 struct btrfs_path
*path
;
3732 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3733 struct btrfs_root
*root
= fs_info
->dev_root
;
3739 struct extent_buffer
*l
;
3740 struct btrfs_key key
;
3741 struct btrfs_key found_key
;
3742 struct btrfs_block_group_cache
*cache
;
3743 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
3745 path
= btrfs_alloc_path();
3749 path
->reada
= READA_FORWARD
;
3750 path
->search_commit_root
= 1;
3751 path
->skip_locking
= 1;
3753 key
.objectid
= scrub_dev
->devid
;
3755 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3758 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3762 if (path
->slots
[0] >=
3763 btrfs_header_nritems(path
->nodes
[0])) {
3764 ret
= btrfs_next_leaf(root
, path
);
3777 slot
= path
->slots
[0];
3779 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
3781 if (found_key
.objectid
!= scrub_dev
->devid
)
3784 if (found_key
.type
!= BTRFS_DEV_EXTENT_KEY
)
3787 if (found_key
.offset
>= end
)
3790 if (found_key
.offset
< key
.offset
)
3793 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3794 length
= btrfs_dev_extent_length(l
, dev_extent
);
3796 if (found_key
.offset
+ length
<= start
)
3799 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3802 * get a reference on the corresponding block group to prevent
3803 * the chunk from going away while we scrub it
3805 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3807 /* some chunks are removed but not committed to disk yet,
3808 * continue scrubbing */
3813 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3814 * to avoid deadlock caused by:
3815 * btrfs_inc_block_group_ro()
3816 * -> btrfs_wait_for_commit()
3817 * -> btrfs_commit_transaction()
3818 * -> btrfs_scrub_pause()
3820 scrub_pause_on(fs_info
);
3821 ret
= btrfs_inc_block_group_ro(fs_info
, cache
);
3822 if (!ret
&& is_dev_replace
) {
3824 * If we are doing a device replace wait for any tasks
3825 * that started dellaloc right before we set the block
3826 * group to RO mode, as they might have just allocated
3827 * an extent from it or decided they could do a nocow
3828 * write. And if any such tasks did that, wait for their
3829 * ordered extents to complete and then commit the
3830 * current transaction, so that we can later see the new
3831 * extent items in the extent tree - the ordered extents
3832 * create delayed data references (for cow writes) when
3833 * they complete, which will be run and insert the
3834 * corresponding extent items into the extent tree when
3835 * we commit the transaction they used when running
3836 * inode.c:btrfs_finish_ordered_io(). We later use
3837 * the commit root of the extent tree to find extents
3838 * to copy from the srcdev into the tgtdev, and we don't
3839 * want to miss any new extents.
3841 btrfs_wait_block_group_reservations(cache
);
3842 btrfs_wait_nocow_writers(cache
);
3843 ret
= btrfs_wait_ordered_roots(fs_info
, U64_MAX
,
3844 cache
->key
.objectid
,
3847 struct btrfs_trans_handle
*trans
;
3849 trans
= btrfs_join_transaction(root
);
3851 ret
= PTR_ERR(trans
);
3853 ret
= btrfs_commit_transaction(trans
);
3855 scrub_pause_off(fs_info
);
3856 btrfs_put_block_group(cache
);
3861 scrub_pause_off(fs_info
);
3865 } else if (ret
== -ENOSPC
) {
3867 * btrfs_inc_block_group_ro return -ENOSPC when it
3868 * failed in creating new chunk for metadata.
3869 * It is not a problem for scrub/replace, because
3870 * metadata are always cowed, and our scrub paused
3871 * commit_transactions.
3876 "failed setting block group ro: %d", ret
);
3877 btrfs_put_block_group(cache
);
3881 btrfs_dev_replace_lock(&fs_info
->dev_replace
, 1);
3882 dev_replace
->cursor_right
= found_key
.offset
+ length
;
3883 dev_replace
->cursor_left
= found_key
.offset
;
3884 dev_replace
->item_needs_writeback
= 1;
3885 btrfs_dev_replace_unlock(&fs_info
->dev_replace
, 1);
3886 ret
= scrub_chunk(sctx
, scrub_dev
, chunk_offset
, length
,
3887 found_key
.offset
, cache
, is_dev_replace
);
3890 * flush, submit all pending read and write bios, afterwards
3892 * Note that in the dev replace case, a read request causes
3893 * write requests that are submitted in the read completion
3894 * worker. Therefore in the current situation, it is required
3895 * that all write requests are flushed, so that all read and
3896 * write requests are really completed when bios_in_flight
3899 sctx
->flush_all_writes
= true;
3901 mutex_lock(&sctx
->wr_lock
);
3902 scrub_wr_submit(sctx
);
3903 mutex_unlock(&sctx
->wr_lock
);
3905 wait_event(sctx
->list_wait
,
3906 atomic_read(&sctx
->bios_in_flight
) == 0);
3908 scrub_pause_on(fs_info
);
3911 * must be called before we decrease @scrub_paused.
3912 * make sure we don't block transaction commit while
3913 * we are waiting pending workers finished.
3915 wait_event(sctx
->list_wait
,
3916 atomic_read(&sctx
->workers_pending
) == 0);
3917 sctx
->flush_all_writes
= false;
3919 scrub_pause_off(fs_info
);
3921 btrfs_dev_replace_lock(&fs_info
->dev_replace
, 1);
3922 dev_replace
->cursor_left
= dev_replace
->cursor_right
;
3923 dev_replace
->item_needs_writeback
= 1;
3924 btrfs_dev_replace_unlock(&fs_info
->dev_replace
, 1);
3927 btrfs_dec_block_group_ro(cache
);
3930 * We might have prevented the cleaner kthread from deleting
3931 * this block group if it was already unused because we raced
3932 * and set it to RO mode first. So add it back to the unused
3933 * list, otherwise it might not ever be deleted unless a manual
3934 * balance is triggered or it becomes used and unused again.
3936 spin_lock(&cache
->lock
);
3937 if (!cache
->removed
&& !cache
->ro
&& cache
->reserved
== 0 &&
3938 btrfs_block_group_used(&cache
->item
) == 0) {
3939 spin_unlock(&cache
->lock
);
3940 spin_lock(&fs_info
->unused_bgs_lock
);
3941 if (list_empty(&cache
->bg_list
)) {
3942 btrfs_get_block_group(cache
);
3943 list_add_tail(&cache
->bg_list
,
3944 &fs_info
->unused_bgs
);
3946 spin_unlock(&fs_info
->unused_bgs_lock
);
3948 spin_unlock(&cache
->lock
);
3951 btrfs_put_block_group(cache
);
3954 if (is_dev_replace
&&
3955 atomic64_read(&dev_replace
->num_write_errors
) > 0) {
3959 if (sctx
->stat
.malloc_errors
> 0) {
3964 key
.offset
= found_key
.offset
+ length
;
3965 btrfs_release_path(path
);
3968 btrfs_free_path(path
);
3973 static noinline_for_stack
int scrub_supers(struct scrub_ctx
*sctx
,
3974 struct btrfs_device
*scrub_dev
)
3980 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
3982 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
3985 /* Seed devices of a new filesystem has their own generation. */
3986 if (scrub_dev
->fs_devices
!= fs_info
->fs_devices
)
3987 gen
= scrub_dev
->generation
;
3989 gen
= fs_info
->last_trans_committed
;
3991 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
3992 bytenr
= btrfs_sb_offset(i
);
3993 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>
3994 scrub_dev
->commit_total_bytes
)
3997 ret
= scrub_pages(sctx
, bytenr
, BTRFS_SUPER_INFO_SIZE
, bytenr
,
3998 scrub_dev
, BTRFS_EXTENT_FLAG_SUPER
, gen
, i
,
4003 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
4009 * get a reference count on fs_info->scrub_workers. start worker if necessary
4011 static noinline_for_stack
int scrub_workers_get(struct btrfs_fs_info
*fs_info
,
4014 unsigned int flags
= WQ_FREEZABLE
| WQ_UNBOUND
;
4015 int max_active
= fs_info
->thread_pool_size
;
4017 if (fs_info
->scrub_workers_refcnt
== 0) {
4018 fs_info
->scrub_workers
= btrfs_alloc_workqueue(fs_info
, "scrub",
4019 flags
, is_dev_replace
? 1 : max_active
, 4);
4020 if (!fs_info
->scrub_workers
)
4021 goto fail_scrub_workers
;
4023 fs_info
->scrub_wr_completion_workers
=
4024 btrfs_alloc_workqueue(fs_info
, "scrubwrc", flags
,
4026 if (!fs_info
->scrub_wr_completion_workers
)
4027 goto fail_scrub_wr_completion_workers
;
4029 fs_info
->scrub_nocow_workers
=
4030 btrfs_alloc_workqueue(fs_info
, "scrubnc", flags
, 1, 0);
4031 if (!fs_info
->scrub_nocow_workers
)
4032 goto fail_scrub_nocow_workers
;
4033 fs_info
->scrub_parity_workers
=
4034 btrfs_alloc_workqueue(fs_info
, "scrubparity", flags
,
4036 if (!fs_info
->scrub_parity_workers
)
4037 goto fail_scrub_parity_workers
;
4039 ++fs_info
->scrub_workers_refcnt
;
4042 fail_scrub_parity_workers
:
4043 btrfs_destroy_workqueue(fs_info
->scrub_nocow_workers
);
4044 fail_scrub_nocow_workers
:
4045 btrfs_destroy_workqueue(fs_info
->scrub_wr_completion_workers
);
4046 fail_scrub_wr_completion_workers
:
4047 btrfs_destroy_workqueue(fs_info
->scrub_workers
);
4052 static noinline_for_stack
void scrub_workers_put(struct btrfs_fs_info
*fs_info
)
4054 if (--fs_info
->scrub_workers_refcnt
== 0) {
4055 btrfs_destroy_workqueue(fs_info
->scrub_workers
);
4056 btrfs_destroy_workqueue(fs_info
->scrub_wr_completion_workers
);
4057 btrfs_destroy_workqueue(fs_info
->scrub_nocow_workers
);
4058 btrfs_destroy_workqueue(fs_info
->scrub_parity_workers
);
4060 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
4063 int btrfs_scrub_dev(struct btrfs_fs_info
*fs_info
, u64 devid
, u64 start
,
4064 u64 end
, struct btrfs_scrub_progress
*progress
,
4065 int readonly
, int is_dev_replace
)
4067 struct scrub_ctx
*sctx
;
4069 struct btrfs_device
*dev
;
4070 struct rcu_string
*name
;
4072 if (btrfs_fs_closing(fs_info
))
4075 if (fs_info
->nodesize
> BTRFS_STRIPE_LEN
) {
4077 * in this case scrub is unable to calculate the checksum
4078 * the way scrub is implemented. Do not handle this
4079 * situation at all because it won't ever happen.
4082 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
4088 if (fs_info
->sectorsize
!= PAGE_SIZE
) {
4089 /* not supported for data w/o checksums */
4090 btrfs_err_rl(fs_info
,
4091 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
4092 fs_info
->sectorsize
, PAGE_SIZE
);
4096 if (fs_info
->nodesize
>
4097 PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
||
4098 fs_info
->sectorsize
> PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
) {
4100 * would exhaust the array bounds of pagev member in
4101 * struct scrub_block
4104 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
4106 SCRUB_MAX_PAGES_PER_BLOCK
,
4107 fs_info
->sectorsize
,
4108 SCRUB_MAX_PAGES_PER_BLOCK
);
4113 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4114 dev
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
4115 if (!dev
|| (dev
->missing
&& !is_dev_replace
)) {
4116 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4120 if (!is_dev_replace
&& !readonly
&& !dev
->writeable
) {
4121 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4123 name
= rcu_dereference(dev
->name
);
4124 btrfs_err(fs_info
, "scrub: device %s is not writable",
4130 mutex_lock(&fs_info
->scrub_lock
);
4131 if (!dev
->in_fs_metadata
|| dev
->is_tgtdev_for_dev_replace
) {
4132 mutex_unlock(&fs_info
->scrub_lock
);
4133 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4137 btrfs_dev_replace_lock(&fs_info
->dev_replace
, 0);
4138 if (dev
->scrub_device
||
4140 btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))) {
4141 btrfs_dev_replace_unlock(&fs_info
->dev_replace
, 0);
4142 mutex_unlock(&fs_info
->scrub_lock
);
4143 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4144 return -EINPROGRESS
;
4146 btrfs_dev_replace_unlock(&fs_info
->dev_replace
, 0);
4148 ret
= scrub_workers_get(fs_info
, is_dev_replace
);
4150 mutex_unlock(&fs_info
->scrub_lock
);
4151 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4155 sctx
= scrub_setup_ctx(dev
, is_dev_replace
);
4157 mutex_unlock(&fs_info
->scrub_lock
);
4158 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4159 scrub_workers_put(fs_info
);
4160 return PTR_ERR(sctx
);
4162 sctx
->readonly
= readonly
;
4163 dev
->scrub_device
= sctx
;
4164 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4167 * checking @scrub_pause_req here, we can avoid
4168 * race between committing transaction and scrubbing.
4170 __scrub_blocked_if_needed(fs_info
);
4171 atomic_inc(&fs_info
->scrubs_running
);
4172 mutex_unlock(&fs_info
->scrub_lock
);
4174 if (!is_dev_replace
) {
4176 * by holding device list mutex, we can
4177 * kick off writing super in log tree sync.
4179 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4180 ret
= scrub_supers(sctx
, dev
);
4181 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4185 ret
= scrub_enumerate_chunks(sctx
, dev
, start
, end
,
4188 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
4189 atomic_dec(&fs_info
->scrubs_running
);
4190 wake_up(&fs_info
->scrub_pause_wait
);
4192 wait_event(sctx
->list_wait
, atomic_read(&sctx
->workers_pending
) == 0);
4195 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
4197 mutex_lock(&fs_info
->scrub_lock
);
4198 dev
->scrub_device
= NULL
;
4199 scrub_workers_put(fs_info
);
4200 mutex_unlock(&fs_info
->scrub_lock
);
4202 scrub_put_ctx(sctx
);
4207 void btrfs_scrub_pause(struct btrfs_fs_info
*fs_info
)
4209 mutex_lock(&fs_info
->scrub_lock
);
4210 atomic_inc(&fs_info
->scrub_pause_req
);
4211 while (atomic_read(&fs_info
->scrubs_paused
) !=
4212 atomic_read(&fs_info
->scrubs_running
)) {
4213 mutex_unlock(&fs_info
->scrub_lock
);
4214 wait_event(fs_info
->scrub_pause_wait
,
4215 atomic_read(&fs_info
->scrubs_paused
) ==
4216 atomic_read(&fs_info
->scrubs_running
));
4217 mutex_lock(&fs_info
->scrub_lock
);
4219 mutex_unlock(&fs_info
->scrub_lock
);
4222 void btrfs_scrub_continue(struct btrfs_fs_info
*fs_info
)
4224 atomic_dec(&fs_info
->scrub_pause_req
);
4225 wake_up(&fs_info
->scrub_pause_wait
);
4228 int btrfs_scrub_cancel(struct btrfs_fs_info
*fs_info
)
4230 mutex_lock(&fs_info
->scrub_lock
);
4231 if (!atomic_read(&fs_info
->scrubs_running
)) {
4232 mutex_unlock(&fs_info
->scrub_lock
);
4236 atomic_inc(&fs_info
->scrub_cancel_req
);
4237 while (atomic_read(&fs_info
->scrubs_running
)) {
4238 mutex_unlock(&fs_info
->scrub_lock
);
4239 wait_event(fs_info
->scrub_pause_wait
,
4240 atomic_read(&fs_info
->scrubs_running
) == 0);
4241 mutex_lock(&fs_info
->scrub_lock
);
4243 atomic_dec(&fs_info
->scrub_cancel_req
);
4244 mutex_unlock(&fs_info
->scrub_lock
);
4249 int btrfs_scrub_cancel_dev(struct btrfs_fs_info
*fs_info
,
4250 struct btrfs_device
*dev
)
4252 struct scrub_ctx
*sctx
;
4254 mutex_lock(&fs_info
->scrub_lock
);
4255 sctx
= dev
->scrub_device
;
4257 mutex_unlock(&fs_info
->scrub_lock
);
4260 atomic_inc(&sctx
->cancel_req
);
4261 while (dev
->scrub_device
) {
4262 mutex_unlock(&fs_info
->scrub_lock
);
4263 wait_event(fs_info
->scrub_pause_wait
,
4264 dev
->scrub_device
== NULL
);
4265 mutex_lock(&fs_info
->scrub_lock
);
4267 mutex_unlock(&fs_info
->scrub_lock
);
4272 int btrfs_scrub_progress(struct btrfs_fs_info
*fs_info
, u64 devid
,
4273 struct btrfs_scrub_progress
*progress
)
4275 struct btrfs_device
*dev
;
4276 struct scrub_ctx
*sctx
= NULL
;
4278 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
4279 dev
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
4281 sctx
= dev
->scrub_device
;
4283 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
4284 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
4286 return dev
? (sctx
? 0 : -ENOTCONN
) : -ENODEV
;
4289 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
4290 u64 extent_logical
, u64 extent_len
,
4291 u64
*extent_physical
,
4292 struct btrfs_device
**extent_dev
,
4293 int *extent_mirror_num
)
4296 struct btrfs_bio
*bbio
= NULL
;
4299 mapped_length
= extent_len
;
4300 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_READ
, extent_logical
,
4301 &mapped_length
, &bbio
, 0);
4302 if (ret
|| !bbio
|| mapped_length
< extent_len
||
4303 !bbio
->stripes
[0].dev
->bdev
) {
4304 btrfs_put_bbio(bbio
);
4308 *extent_physical
= bbio
->stripes
[0].physical
;
4309 *extent_mirror_num
= bbio
->mirror_num
;
4310 *extent_dev
= bbio
->stripes
[0].dev
;
4311 btrfs_put_bbio(bbio
);
4314 static int copy_nocow_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
4315 int mirror_num
, u64 physical_for_dev_replace
)
4317 struct scrub_copy_nocow_ctx
*nocow_ctx
;
4318 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
4320 nocow_ctx
= kzalloc(sizeof(*nocow_ctx
), GFP_NOFS
);
4322 spin_lock(&sctx
->stat_lock
);
4323 sctx
->stat
.malloc_errors
++;
4324 spin_unlock(&sctx
->stat_lock
);
4328 scrub_pending_trans_workers_inc(sctx
);
4330 nocow_ctx
->sctx
= sctx
;
4331 nocow_ctx
->logical
= logical
;
4332 nocow_ctx
->len
= len
;
4333 nocow_ctx
->mirror_num
= mirror_num
;
4334 nocow_ctx
->physical_for_dev_replace
= physical_for_dev_replace
;
4335 btrfs_init_work(&nocow_ctx
->work
, btrfs_scrubnc_helper
,
4336 copy_nocow_pages_worker
, NULL
, NULL
);
4337 INIT_LIST_HEAD(&nocow_ctx
->inodes
);
4338 btrfs_queue_work(fs_info
->scrub_nocow_workers
,
4344 static int record_inode_for_nocow(u64 inum
, u64 offset
, u64 root
, void *ctx
)
4346 struct scrub_copy_nocow_ctx
*nocow_ctx
= ctx
;
4347 struct scrub_nocow_inode
*nocow_inode
;
4349 nocow_inode
= kzalloc(sizeof(*nocow_inode
), GFP_NOFS
);
4352 nocow_inode
->inum
= inum
;
4353 nocow_inode
->offset
= offset
;
4354 nocow_inode
->root
= root
;
4355 list_add_tail(&nocow_inode
->list
, &nocow_ctx
->inodes
);
4359 #define COPY_COMPLETE 1
4361 static void copy_nocow_pages_worker(struct btrfs_work
*work
)
4363 struct scrub_copy_nocow_ctx
*nocow_ctx
=
4364 container_of(work
, struct scrub_copy_nocow_ctx
, work
);
4365 struct scrub_ctx
*sctx
= nocow_ctx
->sctx
;
4366 struct btrfs_fs_info
*fs_info
= sctx
->fs_info
;
4367 struct btrfs_root
*root
= fs_info
->extent_root
;
4368 u64 logical
= nocow_ctx
->logical
;
4369 u64 len
= nocow_ctx
->len
;
4370 int mirror_num
= nocow_ctx
->mirror_num
;
4371 u64 physical_for_dev_replace
= nocow_ctx
->physical_for_dev_replace
;
4373 struct btrfs_trans_handle
*trans
= NULL
;
4374 struct btrfs_path
*path
;
4375 int not_written
= 0;
4377 path
= btrfs_alloc_path();
4379 spin_lock(&sctx
->stat_lock
);
4380 sctx
->stat
.malloc_errors
++;
4381 spin_unlock(&sctx
->stat_lock
);
4386 trans
= btrfs_join_transaction(root
);
4387 if (IS_ERR(trans
)) {
4392 ret
= iterate_inodes_from_logical(logical
, fs_info
, path
,
4393 record_inode_for_nocow
, nocow_ctx
);
4394 if (ret
!= 0 && ret
!= -ENOENT
) {
4396 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4397 logical
, physical_for_dev_replace
, len
, mirror_num
,
4403 btrfs_end_transaction(trans
);
4405 while (!list_empty(&nocow_ctx
->inodes
)) {
4406 struct scrub_nocow_inode
*entry
;
4407 entry
= list_first_entry(&nocow_ctx
->inodes
,
4408 struct scrub_nocow_inode
,
4410 list_del_init(&entry
->list
);
4411 ret
= copy_nocow_pages_for_inode(entry
->inum
, entry
->offset
,
4412 entry
->root
, nocow_ctx
);
4414 if (ret
== COPY_COMPLETE
) {
4422 while (!list_empty(&nocow_ctx
->inodes
)) {
4423 struct scrub_nocow_inode
*entry
;
4424 entry
= list_first_entry(&nocow_ctx
->inodes
,
4425 struct scrub_nocow_inode
,
4427 list_del_init(&entry
->list
);
4430 if (trans
&& !IS_ERR(trans
))
4431 btrfs_end_transaction(trans
);
4433 btrfs_dev_replace_stats_inc(&fs_info
->dev_replace
.
4434 num_uncorrectable_read_errors
);
4436 btrfs_free_path(path
);
4439 scrub_pending_trans_workers_dec(sctx
);
4442 static int check_extent_to_block(struct btrfs_inode
*inode
, u64 start
, u64 len
,
4445 struct extent_state
*cached_state
= NULL
;
4446 struct btrfs_ordered_extent
*ordered
;
4447 struct extent_io_tree
*io_tree
;
4448 struct extent_map
*em
;
4449 u64 lockstart
= start
, lockend
= start
+ len
- 1;
4452 io_tree
= &inode
->io_tree
;
4454 lock_extent_bits(io_tree
, lockstart
, lockend
, &cached_state
);
4455 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
, len
);
4457 btrfs_put_ordered_extent(ordered
);
4462 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
4469 * This extent does not actually cover the logical extent anymore,
4470 * move on to the next inode.
4472 if (em
->block_start
> logical
||
4473 em
->block_start
+ em
->block_len
< logical
+ len
) {
4474 free_extent_map(em
);
4478 free_extent_map(em
);
4481 unlock_extent_cached(io_tree
, lockstart
, lockend
, &cached_state
,
4486 static int copy_nocow_pages_for_inode(u64 inum
, u64 offset
, u64 root
,
4487 struct scrub_copy_nocow_ctx
*nocow_ctx
)
4489 struct btrfs_fs_info
*fs_info
= nocow_ctx
->sctx
->fs_info
;
4490 struct btrfs_key key
;
4491 struct inode
*inode
;
4493 struct btrfs_root
*local_root
;
4494 struct extent_io_tree
*io_tree
;
4495 u64 physical_for_dev_replace
;
4496 u64 nocow_ctx_logical
;
4497 u64 len
= nocow_ctx
->len
;
4498 unsigned long index
;
4503 key
.objectid
= root
;
4504 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4505 key
.offset
= (u64
)-1;
4507 srcu_index
= srcu_read_lock(&fs_info
->subvol_srcu
);
4509 local_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4510 if (IS_ERR(local_root
)) {
4511 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
4512 return PTR_ERR(local_root
);
4515 key
.type
= BTRFS_INODE_ITEM_KEY
;
4516 key
.objectid
= inum
;
4518 inode
= btrfs_iget(fs_info
->sb
, &key
, local_root
, NULL
);
4519 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
4521 return PTR_ERR(inode
);
4523 /* Avoid truncate/dio/punch hole.. */
4525 inode_dio_wait(inode
);
4527 physical_for_dev_replace
= nocow_ctx
->physical_for_dev_replace
;
4528 io_tree
= &BTRFS_I(inode
)->io_tree
;
4529 nocow_ctx_logical
= nocow_ctx
->logical
;
4531 ret
= check_extent_to_block(BTRFS_I(inode
), offset
, len
,
4534 ret
= ret
> 0 ? 0 : ret
;
4538 while (len
>= PAGE_SIZE
) {
4539 index
= offset
>> PAGE_SHIFT
;
4541 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
4543 btrfs_err(fs_info
, "find_or_create_page() failed");
4548 if (PageUptodate(page
)) {
4549 if (PageDirty(page
))
4552 ClearPageError(page
);
4553 err
= extent_read_full_page(io_tree
, page
,
4555 nocow_ctx
->mirror_num
);
4563 * If the page has been remove from the page cache,
4564 * the data on it is meaningless, because it may be
4565 * old one, the new data may be written into the new
4566 * page in the page cache.
4568 if (page
->mapping
!= inode
->i_mapping
) {
4573 if (!PageUptodate(page
)) {
4579 ret
= check_extent_to_block(BTRFS_I(inode
), offset
, len
,
4582 ret
= ret
> 0 ? 0 : ret
;
4586 err
= write_page_nocow(nocow_ctx
->sctx
,
4587 physical_for_dev_replace
, page
);
4597 offset
+= PAGE_SIZE
;
4598 physical_for_dev_replace
+= PAGE_SIZE
;
4599 nocow_ctx_logical
+= PAGE_SIZE
;
4602 ret
= COPY_COMPLETE
;
4604 inode_unlock(inode
);
4609 static int write_page_nocow(struct scrub_ctx
*sctx
,
4610 u64 physical_for_dev_replace
, struct page
*page
)
4613 struct btrfs_device
*dev
;
4616 dev
= sctx
->wr_tgtdev
;
4620 btrfs_warn_rl(dev
->fs_info
,
4621 "scrub write_page_nocow(bdev == NULL) is unexpected");
4624 bio
= btrfs_io_bio_alloc(1);
4625 bio
->bi_iter
.bi_size
= 0;
4626 bio
->bi_iter
.bi_sector
= physical_for_dev_replace
>> 9;
4627 bio_set_dev(bio
, dev
->bdev
);
4628 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
;
4629 ret
= bio_add_page(bio
, page
, PAGE_SIZE
, 0);
4630 if (ret
!= PAGE_SIZE
) {
4633 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
4637 if (btrfsic_submit_bio_wait(bio
))
4638 goto leave_with_eio
;