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>
24 #include "ordered-data.h"
25 #include "transaction.h"
27 #include "extent_io.h"
28 #include "dev-replace.h"
29 #include "check-integrity.h"
30 #include "rcu-string.h"
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
39 * Future enhancements:
40 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
42 * - track and record media errors, throw out bad devices
43 * - add a mode to also read unallocated space
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
55 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
64 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
66 struct scrub_recover
{
68 struct btrfs_bio
*bbio
;
73 struct scrub_block
*sblock
;
75 struct btrfs_device
*dev
;
76 struct list_head list
;
77 u64 flags
; /* extent flags */
81 u64 physical_for_dev_replace
;
84 unsigned int mirror_num
:8;
85 unsigned int have_csum
:1;
86 unsigned int io_error
:1;
88 u8 csum
[BTRFS_CSUM_SIZE
];
90 struct scrub_recover
*recover
;
95 struct scrub_ctx
*sctx
;
96 struct btrfs_device
*dev
;
101 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page
*pagev
[SCRUB_PAGES_PER_WR_BIO
];
104 struct scrub_page
*pagev
[SCRUB_PAGES_PER_RD_BIO
];
108 struct btrfs_work work
;
112 struct scrub_page
*pagev
[SCRUB_MAX_PAGES_PER_BLOCK
];
114 atomic_t outstanding_pages
;
115 atomic_t refs
; /* free mem on transition to zero */
116 struct scrub_ctx
*sctx
;
117 struct scrub_parity
*sparity
;
119 unsigned int header_error
:1;
120 unsigned int checksum_error
:1;
121 unsigned int no_io_error_seen
:1;
122 unsigned int generation_error
:1; /* also sets header_error */
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected
:1;
130 /* Used for the chunks with parity stripe such RAID5/6 */
131 struct scrub_parity
{
132 struct scrub_ctx
*sctx
;
134 struct btrfs_device
*scrub_dev
;
146 struct list_head spages
;
148 /* Work of parity check and repair */
149 struct btrfs_work work
;
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap
;
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
158 unsigned long *ebitmap
;
160 unsigned long bitmap
[0];
163 struct scrub_wr_ctx
{
164 struct scrub_bio
*wr_curr_bio
;
165 struct btrfs_device
*tgtdev
;
166 int pages_per_wr_bio
; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes
;
168 struct mutex wr_lock
;
172 struct scrub_bio
*bios
[SCRUB_BIOS_PER_SCTX
];
173 struct btrfs_root
*dev_root
;
176 atomic_t bios_in_flight
;
177 atomic_t workers_pending
;
178 spinlock_t list_lock
;
179 wait_queue_head_t list_wait
;
181 struct list_head csum_list
;
184 int pages_per_rd_bio
;
189 struct scrub_wr_ctx wr_ctx
;
194 struct btrfs_scrub_progress stat
;
195 spinlock_t stat_lock
;
198 * Use a ref counter to avoid use-after-free issues. Scrub workers
199 * decrement bios_in_flight and workers_pending and then do a wakeup
200 * on the list_wait wait queue. We must ensure the main scrub task
201 * doesn't free the scrub context before or while the workers are
202 * doing the wakeup() call.
207 struct scrub_fixup_nodatasum
{
208 struct scrub_ctx
*sctx
;
209 struct btrfs_device
*dev
;
211 struct btrfs_root
*root
;
212 struct btrfs_work work
;
216 struct scrub_nocow_inode
{
220 struct list_head list
;
223 struct scrub_copy_nocow_ctx
{
224 struct scrub_ctx
*sctx
;
228 u64 physical_for_dev_replace
;
229 struct list_head inodes
;
230 struct btrfs_work work
;
233 struct scrub_warning
{
234 struct btrfs_path
*path
;
235 u64 extent_item_size
;
239 struct btrfs_device
*dev
;
242 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
);
243 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
);
244 static void scrub_pending_trans_workers_inc(struct scrub_ctx
*sctx
);
245 static void scrub_pending_trans_workers_dec(struct scrub_ctx
*sctx
);
246 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
);
247 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
248 struct scrub_block
*sblocks_for_recheck
);
249 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
250 struct scrub_block
*sblock
, int is_metadata
,
251 int have_csum
, u8
*csum
, u64 generation
,
252 u16 csum_size
, int retry_failed_mirror
);
253 static void scrub_recheck_block_checksum(struct btrfs_fs_info
*fs_info
,
254 struct scrub_block
*sblock
,
255 int is_metadata
, int have_csum
,
256 const u8
*csum
, u64 generation
,
258 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
259 struct scrub_block
*sblock_good
);
260 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
261 struct scrub_block
*sblock_good
,
262 int page_num
, int force_write
);
263 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
);
264 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
266 static int scrub_checksum_data(struct scrub_block
*sblock
);
267 static int scrub_checksum_tree_block(struct scrub_block
*sblock
);
268 static int scrub_checksum_super(struct scrub_block
*sblock
);
269 static void scrub_block_get(struct scrub_block
*sblock
);
270 static void scrub_block_put(struct scrub_block
*sblock
);
271 static void scrub_page_get(struct scrub_page
*spage
);
272 static void scrub_page_put(struct scrub_page
*spage
);
273 static void scrub_parity_get(struct scrub_parity
*sparity
);
274 static void scrub_parity_put(struct scrub_parity
*sparity
);
275 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
276 struct scrub_page
*spage
);
277 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
278 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
279 u64 gen
, int mirror_num
, u8
*csum
, int force
,
280 u64 physical_for_dev_replace
);
281 static void scrub_bio_end_io(struct bio
*bio
, int err
);
282 static void scrub_bio_end_io_worker(struct btrfs_work
*work
);
283 static void scrub_block_complete(struct scrub_block
*sblock
);
284 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
285 u64 extent_logical
, u64 extent_len
,
286 u64
*extent_physical
,
287 struct btrfs_device
**extent_dev
,
288 int *extent_mirror_num
);
289 static int scrub_setup_wr_ctx(struct scrub_ctx
*sctx
,
290 struct scrub_wr_ctx
*wr_ctx
,
291 struct btrfs_fs_info
*fs_info
,
292 struct btrfs_device
*dev
,
294 static void scrub_free_wr_ctx(struct scrub_wr_ctx
*wr_ctx
);
295 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
296 struct scrub_page
*spage
);
297 static void scrub_wr_submit(struct scrub_ctx
*sctx
);
298 static void scrub_wr_bio_end_io(struct bio
*bio
, int err
);
299 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
);
300 static int write_page_nocow(struct scrub_ctx
*sctx
,
301 u64 physical_for_dev_replace
, struct page
*page
);
302 static int copy_nocow_pages_for_inode(u64 inum
, u64 offset
, u64 root
,
303 struct scrub_copy_nocow_ctx
*ctx
);
304 static int copy_nocow_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
305 int mirror_num
, u64 physical_for_dev_replace
);
306 static void copy_nocow_pages_worker(struct btrfs_work
*work
);
307 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
308 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
);
309 static void scrub_put_ctx(struct scrub_ctx
*sctx
);
312 static void scrub_pending_bio_inc(struct scrub_ctx
*sctx
)
314 atomic_inc(&sctx
->refs
);
315 atomic_inc(&sctx
->bios_in_flight
);
318 static void scrub_pending_bio_dec(struct scrub_ctx
*sctx
)
320 atomic_dec(&sctx
->bios_in_flight
);
321 wake_up(&sctx
->list_wait
);
325 static void __scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
327 while (atomic_read(&fs_info
->scrub_pause_req
)) {
328 mutex_unlock(&fs_info
->scrub_lock
);
329 wait_event(fs_info
->scrub_pause_wait
,
330 atomic_read(&fs_info
->scrub_pause_req
) == 0);
331 mutex_lock(&fs_info
->scrub_lock
);
335 static void scrub_blocked_if_needed(struct btrfs_fs_info
*fs_info
)
337 atomic_inc(&fs_info
->scrubs_paused
);
338 wake_up(&fs_info
->scrub_pause_wait
);
340 mutex_lock(&fs_info
->scrub_lock
);
341 __scrub_blocked_if_needed(fs_info
);
342 atomic_dec(&fs_info
->scrubs_paused
);
343 mutex_unlock(&fs_info
->scrub_lock
);
345 wake_up(&fs_info
->scrub_pause_wait
);
349 * used for workers that require transaction commits (i.e., for the
352 static void scrub_pending_trans_workers_inc(struct scrub_ctx
*sctx
)
354 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
356 atomic_inc(&sctx
->refs
);
358 * increment scrubs_running to prevent cancel requests from
359 * completing as long as a worker is running. we must also
360 * increment scrubs_paused to prevent deadlocking on pause
361 * requests used for transactions commits (as the worker uses a
362 * transaction context). it is safe to regard the worker
363 * as paused for all matters practical. effectively, we only
364 * avoid cancellation requests from completing.
366 mutex_lock(&fs_info
->scrub_lock
);
367 atomic_inc(&fs_info
->scrubs_running
);
368 atomic_inc(&fs_info
->scrubs_paused
);
369 mutex_unlock(&fs_info
->scrub_lock
);
372 * check if @scrubs_running=@scrubs_paused condition
373 * inside wait_event() is not an atomic operation.
374 * which means we may inc/dec @scrub_running/paused
375 * at any time. Let's wake up @scrub_pause_wait as
376 * much as we can to let commit transaction blocked less.
378 wake_up(&fs_info
->scrub_pause_wait
);
380 atomic_inc(&sctx
->workers_pending
);
383 /* used for workers that require transaction commits */
384 static void scrub_pending_trans_workers_dec(struct scrub_ctx
*sctx
)
386 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
389 * see scrub_pending_trans_workers_inc() why we're pretending
390 * to be paused in the scrub counters
392 mutex_lock(&fs_info
->scrub_lock
);
393 atomic_dec(&fs_info
->scrubs_running
);
394 atomic_dec(&fs_info
->scrubs_paused
);
395 mutex_unlock(&fs_info
->scrub_lock
);
396 atomic_dec(&sctx
->workers_pending
);
397 wake_up(&fs_info
->scrub_pause_wait
);
398 wake_up(&sctx
->list_wait
);
402 static void scrub_free_csums(struct scrub_ctx
*sctx
)
404 while (!list_empty(&sctx
->csum_list
)) {
405 struct btrfs_ordered_sum
*sum
;
406 sum
= list_first_entry(&sctx
->csum_list
,
407 struct btrfs_ordered_sum
, list
);
408 list_del(&sum
->list
);
413 static noinline_for_stack
void scrub_free_ctx(struct scrub_ctx
*sctx
)
420 scrub_free_wr_ctx(&sctx
->wr_ctx
);
422 /* this can happen when scrub is cancelled */
423 if (sctx
->curr
!= -1) {
424 struct scrub_bio
*sbio
= sctx
->bios
[sctx
->curr
];
426 for (i
= 0; i
< sbio
->page_count
; i
++) {
427 WARN_ON(!sbio
->pagev
[i
]->page
);
428 scrub_block_put(sbio
->pagev
[i
]->sblock
);
433 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
434 struct scrub_bio
*sbio
= sctx
->bios
[i
];
441 scrub_free_csums(sctx
);
445 static void scrub_put_ctx(struct scrub_ctx
*sctx
)
447 if (atomic_dec_and_test(&sctx
->refs
))
448 scrub_free_ctx(sctx
);
451 static noinline_for_stack
452 struct scrub_ctx
*scrub_setup_ctx(struct btrfs_device
*dev
, int is_dev_replace
)
454 struct scrub_ctx
*sctx
;
456 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
457 int pages_per_rd_bio
;
461 * the setting of pages_per_rd_bio is correct for scrub but might
462 * be wrong for the dev_replace code where we might read from
463 * different devices in the initial huge bios. However, that
464 * code is able to correctly handle the case when adding a page
468 pages_per_rd_bio
= min_t(int, SCRUB_PAGES_PER_RD_BIO
,
469 bio_get_nr_vecs(dev
->bdev
));
471 pages_per_rd_bio
= SCRUB_PAGES_PER_RD_BIO
;
472 sctx
= kzalloc(sizeof(*sctx
), GFP_NOFS
);
475 atomic_set(&sctx
->refs
, 1);
476 sctx
->is_dev_replace
= is_dev_replace
;
477 sctx
->pages_per_rd_bio
= pages_per_rd_bio
;
479 sctx
->dev_root
= dev
->dev_root
;
480 for (i
= 0; i
< SCRUB_BIOS_PER_SCTX
; ++i
) {
481 struct scrub_bio
*sbio
;
483 sbio
= kzalloc(sizeof(*sbio
), GFP_NOFS
);
486 sctx
->bios
[i
] = sbio
;
490 sbio
->page_count
= 0;
491 btrfs_init_work(&sbio
->work
, btrfs_scrub_helper
,
492 scrub_bio_end_io_worker
, NULL
, NULL
);
494 if (i
!= SCRUB_BIOS_PER_SCTX
- 1)
495 sctx
->bios
[i
]->next_free
= i
+ 1;
497 sctx
->bios
[i
]->next_free
= -1;
499 sctx
->first_free
= 0;
500 sctx
->nodesize
= dev
->dev_root
->nodesize
;
501 sctx
->sectorsize
= dev
->dev_root
->sectorsize
;
502 atomic_set(&sctx
->bios_in_flight
, 0);
503 atomic_set(&sctx
->workers_pending
, 0);
504 atomic_set(&sctx
->cancel_req
, 0);
505 sctx
->csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
506 INIT_LIST_HEAD(&sctx
->csum_list
);
508 spin_lock_init(&sctx
->list_lock
);
509 spin_lock_init(&sctx
->stat_lock
);
510 init_waitqueue_head(&sctx
->list_wait
);
512 ret
= scrub_setup_wr_ctx(sctx
, &sctx
->wr_ctx
, fs_info
,
513 fs_info
->dev_replace
.tgtdev
, is_dev_replace
);
515 scrub_free_ctx(sctx
);
521 scrub_free_ctx(sctx
);
522 return ERR_PTR(-ENOMEM
);
525 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
,
532 struct extent_buffer
*eb
;
533 struct btrfs_inode_item
*inode_item
;
534 struct scrub_warning
*swarn
= warn_ctx
;
535 struct btrfs_fs_info
*fs_info
= swarn
->dev
->dev_root
->fs_info
;
536 struct inode_fs_paths
*ipath
= NULL
;
537 struct btrfs_root
*local_root
;
538 struct btrfs_key root_key
;
539 struct btrfs_key key
;
541 root_key
.objectid
= root
;
542 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
543 root_key
.offset
= (u64
)-1;
544 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
545 if (IS_ERR(local_root
)) {
546 ret
= PTR_ERR(local_root
);
551 * this makes the path point to (inum INODE_ITEM ioff)
554 key
.type
= BTRFS_INODE_ITEM_KEY
;
557 ret
= btrfs_search_slot(NULL
, local_root
, &key
, swarn
->path
, 0, 0);
559 btrfs_release_path(swarn
->path
);
563 eb
= swarn
->path
->nodes
[0];
564 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
565 struct btrfs_inode_item
);
566 isize
= btrfs_inode_size(eb
, inode_item
);
567 nlink
= btrfs_inode_nlink(eb
, inode_item
);
568 btrfs_release_path(swarn
->path
);
570 ipath
= init_ipath(4096, local_root
, swarn
->path
);
572 ret
= PTR_ERR(ipath
);
576 ret
= paths_from_inode(inum
, ipath
);
582 * we deliberately ignore the bit ipath might have been too small to
583 * hold all of the paths here
585 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
586 printk_in_rcu(KERN_WARNING
"BTRFS: %s at logical %llu on dev "
587 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
588 "length %llu, links %u (path: %s)\n", swarn
->errstr
,
589 swarn
->logical
, rcu_str_deref(swarn
->dev
->name
),
590 (unsigned long long)swarn
->sector
, root
, inum
, offset
,
591 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
592 (char *)(unsigned long)ipath
->fspath
->val
[i
]);
598 printk_in_rcu(KERN_WARNING
"BTRFS: %s at logical %llu on dev "
599 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
600 "resolving failed with ret=%d\n", swarn
->errstr
,
601 swarn
->logical
, rcu_str_deref(swarn
->dev
->name
),
602 (unsigned long long)swarn
->sector
, root
, inum
, offset
, ret
);
608 static void scrub_print_warning(const char *errstr
, struct scrub_block
*sblock
)
610 struct btrfs_device
*dev
;
611 struct btrfs_fs_info
*fs_info
;
612 struct btrfs_path
*path
;
613 struct btrfs_key found_key
;
614 struct extent_buffer
*eb
;
615 struct btrfs_extent_item
*ei
;
616 struct scrub_warning swarn
;
617 unsigned long ptr
= 0;
625 WARN_ON(sblock
->page_count
< 1);
626 dev
= sblock
->pagev
[0]->dev
;
627 fs_info
= sblock
->sctx
->dev_root
->fs_info
;
629 path
= btrfs_alloc_path();
633 swarn
.sector
= (sblock
->pagev
[0]->physical
) >> 9;
634 swarn
.logical
= sblock
->pagev
[0]->logical
;
635 swarn
.errstr
= errstr
;
638 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
,
643 extent_item_pos
= swarn
.logical
- found_key
.objectid
;
644 swarn
.extent_item_size
= found_key
.offset
;
647 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
648 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
650 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
652 ret
= tree_backref_for_extent(&ptr
, eb
, &found_key
, ei
,
653 item_size
, &ref_root
,
655 printk_in_rcu(KERN_WARNING
656 "BTRFS: %s at logical %llu on dev %s, "
657 "sector %llu: metadata %s (level %d) in tree "
658 "%llu\n", errstr
, swarn
.logical
,
659 rcu_str_deref(dev
->name
),
660 (unsigned long long)swarn
.sector
,
661 ref_level
? "node" : "leaf",
662 ret
< 0 ? -1 : ref_level
,
663 ret
< 0 ? -1 : ref_root
);
665 btrfs_release_path(path
);
667 btrfs_release_path(path
);
670 iterate_extent_inodes(fs_info
, found_key
.objectid
,
672 scrub_print_warning_inode
, &swarn
);
676 btrfs_free_path(path
);
679 static int scrub_fixup_readpage(u64 inum
, u64 offset
, u64 root
, void *fixup_ctx
)
681 struct page
*page
= NULL
;
683 struct scrub_fixup_nodatasum
*fixup
= fixup_ctx
;
686 struct btrfs_key key
;
687 struct inode
*inode
= NULL
;
688 struct btrfs_fs_info
*fs_info
;
689 u64 end
= offset
+ PAGE_SIZE
- 1;
690 struct btrfs_root
*local_root
;
694 key
.type
= BTRFS_ROOT_ITEM_KEY
;
695 key
.offset
= (u64
)-1;
697 fs_info
= fixup
->root
->fs_info
;
698 srcu_index
= srcu_read_lock(&fs_info
->subvol_srcu
);
700 local_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
701 if (IS_ERR(local_root
)) {
702 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
703 return PTR_ERR(local_root
);
706 key
.type
= BTRFS_INODE_ITEM_KEY
;
709 inode
= btrfs_iget(fs_info
->sb
, &key
, local_root
, NULL
);
710 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
712 return PTR_ERR(inode
);
714 index
= offset
>> PAGE_CACHE_SHIFT
;
716 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
722 if (PageUptodate(page
)) {
723 if (PageDirty(page
)) {
725 * we need to write the data to the defect sector. the
726 * data that was in that sector is not in memory,
727 * because the page was modified. we must not write the
728 * modified page to that sector.
730 * TODO: what could be done here: wait for the delalloc
731 * runner to write out that page (might involve
732 * COW) and see whether the sector is still
733 * referenced afterwards.
735 * For the meantime, we'll treat this error
736 * incorrectable, although there is a chance that a
737 * later scrub will find the bad sector again and that
738 * there's no dirty page in memory, then.
743 ret
= repair_io_failure(inode
, offset
, PAGE_SIZE
,
744 fixup
->logical
, page
,
745 offset
- page_offset(page
),
751 * we need to get good data first. the general readpage path
752 * will call repair_io_failure for us, we just have to make
753 * sure we read the bad mirror.
755 ret
= set_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
756 EXTENT_DAMAGED
, GFP_NOFS
);
758 /* set_extent_bits should give proper error */
765 ret
= extent_read_full_page(&BTRFS_I(inode
)->io_tree
, page
,
768 wait_on_page_locked(page
);
770 corrected
= !test_range_bit(&BTRFS_I(inode
)->io_tree
, offset
,
771 end
, EXTENT_DAMAGED
, 0, NULL
);
773 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
774 EXTENT_DAMAGED
, GFP_NOFS
);
786 if (ret
== 0 && corrected
) {
788 * we only need to call readpage for one of the inodes belonging
789 * to this extent. so make iterate_extent_inodes stop
797 static void scrub_fixup_nodatasum(struct btrfs_work
*work
)
800 struct scrub_fixup_nodatasum
*fixup
;
801 struct scrub_ctx
*sctx
;
802 struct btrfs_trans_handle
*trans
= NULL
;
803 struct btrfs_path
*path
;
804 int uncorrectable
= 0;
806 fixup
= container_of(work
, struct scrub_fixup_nodatasum
, work
);
809 path
= btrfs_alloc_path();
811 spin_lock(&sctx
->stat_lock
);
812 ++sctx
->stat
.malloc_errors
;
813 spin_unlock(&sctx
->stat_lock
);
818 trans
= btrfs_join_transaction(fixup
->root
);
825 * the idea is to trigger a regular read through the standard path. we
826 * read a page from the (failed) logical address by specifying the
827 * corresponding copynum of the failed sector. thus, that readpage is
829 * that is the point where on-the-fly error correction will kick in
830 * (once it's finished) and rewrite the failed sector if a good copy
833 ret
= iterate_inodes_from_logical(fixup
->logical
, fixup
->root
->fs_info
,
834 path
, scrub_fixup_readpage
,
842 spin_lock(&sctx
->stat_lock
);
843 ++sctx
->stat
.corrected_errors
;
844 spin_unlock(&sctx
->stat_lock
);
847 if (trans
&& !IS_ERR(trans
))
848 btrfs_end_transaction(trans
, fixup
->root
);
850 spin_lock(&sctx
->stat_lock
);
851 ++sctx
->stat
.uncorrectable_errors
;
852 spin_unlock(&sctx
->stat_lock
);
853 btrfs_dev_replace_stats_inc(
854 &sctx
->dev_root
->fs_info
->dev_replace
.
855 num_uncorrectable_read_errors
);
856 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
857 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
858 fixup
->logical
, rcu_str_deref(fixup
->dev
->name
));
861 btrfs_free_path(path
);
864 scrub_pending_trans_workers_dec(sctx
);
867 static inline void scrub_get_recover(struct scrub_recover
*recover
)
869 atomic_inc(&recover
->refs
);
872 static inline void scrub_put_recover(struct scrub_recover
*recover
)
874 if (atomic_dec_and_test(&recover
->refs
)) {
875 btrfs_put_bbio(recover
->bbio
);
881 * scrub_handle_errored_block gets called when either verification of the
882 * pages failed or the bio failed to read, e.g. with EIO. In the latter
883 * case, this function handles all pages in the bio, even though only one
885 * The goal of this function is to repair the errored block by using the
886 * contents of one of the mirrors.
888 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
)
890 struct scrub_ctx
*sctx
= sblock_to_check
->sctx
;
891 struct btrfs_device
*dev
;
892 struct btrfs_fs_info
*fs_info
;
896 unsigned int failed_mirror_index
;
897 unsigned int is_metadata
;
898 unsigned int have_csum
;
900 struct scrub_block
*sblocks_for_recheck
; /* holds one for each mirror */
901 struct scrub_block
*sblock_bad
;
906 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
907 DEFAULT_RATELIMIT_BURST
);
909 BUG_ON(sblock_to_check
->page_count
< 1);
910 fs_info
= sctx
->dev_root
->fs_info
;
911 if (sblock_to_check
->pagev
[0]->flags
& BTRFS_EXTENT_FLAG_SUPER
) {
913 * if we find an error in a super block, we just report it.
914 * They will get written with the next transaction commit
917 spin_lock(&sctx
->stat_lock
);
918 ++sctx
->stat
.super_errors
;
919 spin_unlock(&sctx
->stat_lock
);
922 length
= sblock_to_check
->page_count
* PAGE_SIZE
;
923 logical
= sblock_to_check
->pagev
[0]->logical
;
924 generation
= sblock_to_check
->pagev
[0]->generation
;
925 BUG_ON(sblock_to_check
->pagev
[0]->mirror_num
< 1);
926 failed_mirror_index
= sblock_to_check
->pagev
[0]->mirror_num
- 1;
927 is_metadata
= !(sblock_to_check
->pagev
[0]->flags
&
928 BTRFS_EXTENT_FLAG_DATA
);
929 have_csum
= sblock_to_check
->pagev
[0]->have_csum
;
930 csum
= sblock_to_check
->pagev
[0]->csum
;
931 dev
= sblock_to_check
->pagev
[0]->dev
;
933 if (sctx
->is_dev_replace
&& !is_metadata
&& !have_csum
) {
934 sblocks_for_recheck
= NULL
;
939 * read all mirrors one after the other. This includes to
940 * re-read the extent or metadata block that failed (that was
941 * the cause that this fixup code is called) another time,
942 * page by page this time in order to know which pages
943 * caused I/O errors and which ones are good (for all mirrors).
944 * It is the goal to handle the situation when more than one
945 * mirror contains I/O errors, but the errors do not
946 * overlap, i.e. the data can be repaired by selecting the
947 * pages from those mirrors without I/O error on the
948 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
949 * would be that mirror #1 has an I/O error on the first page,
950 * the second page is good, and mirror #2 has an I/O error on
951 * the second page, but the first page is good.
952 * Then the first page of the first mirror can be repaired by
953 * taking the first page of the second mirror, and the
954 * second page of the second mirror can be repaired by
955 * copying the contents of the 2nd page of the 1st mirror.
956 * One more note: if the pages of one mirror contain I/O
957 * errors, the checksum cannot be verified. In order to get
958 * the best data for repairing, the first attempt is to find
959 * a mirror without I/O errors and with a validated checksum.
960 * Only if this is not possible, the pages are picked from
961 * mirrors with I/O errors without considering the checksum.
962 * If the latter is the case, at the end, the checksum of the
963 * repaired area is verified in order to correctly maintain
967 sblocks_for_recheck
= kcalloc(BTRFS_MAX_MIRRORS
,
968 sizeof(*sblocks_for_recheck
), GFP_NOFS
);
969 if (!sblocks_for_recheck
) {
970 spin_lock(&sctx
->stat_lock
);
971 sctx
->stat
.malloc_errors
++;
972 sctx
->stat
.read_errors
++;
973 sctx
->stat
.uncorrectable_errors
++;
974 spin_unlock(&sctx
->stat_lock
);
975 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
979 /* setup the context, map the logical blocks and alloc the pages */
980 ret
= scrub_setup_recheck_block(sblock_to_check
, sblocks_for_recheck
);
982 spin_lock(&sctx
->stat_lock
);
983 sctx
->stat
.read_errors
++;
984 sctx
->stat
.uncorrectable_errors
++;
985 spin_unlock(&sctx
->stat_lock
);
986 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
989 BUG_ON(failed_mirror_index
>= BTRFS_MAX_MIRRORS
);
990 sblock_bad
= sblocks_for_recheck
+ failed_mirror_index
;
992 /* build and submit the bios for the failed mirror, check checksums */
993 scrub_recheck_block(fs_info
, sblock_bad
, is_metadata
, have_csum
,
994 csum
, generation
, sctx
->csum_size
, 1);
996 if (!sblock_bad
->header_error
&& !sblock_bad
->checksum_error
&&
997 sblock_bad
->no_io_error_seen
) {
999 * the error disappeared after reading page by page, or
1000 * the area was part of a huge bio and other parts of the
1001 * bio caused I/O errors, or the block layer merged several
1002 * read requests into one and the error is caused by a
1003 * different bio (usually one of the two latter cases is
1006 spin_lock(&sctx
->stat_lock
);
1007 sctx
->stat
.unverified_errors
++;
1008 sblock_to_check
->data_corrected
= 1;
1009 spin_unlock(&sctx
->stat_lock
);
1011 if (sctx
->is_dev_replace
)
1012 scrub_write_block_to_dev_replace(sblock_bad
);
1016 if (!sblock_bad
->no_io_error_seen
) {
1017 spin_lock(&sctx
->stat_lock
);
1018 sctx
->stat
.read_errors
++;
1019 spin_unlock(&sctx
->stat_lock
);
1020 if (__ratelimit(&_rs
))
1021 scrub_print_warning("i/o error", sblock_to_check
);
1022 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_READ_ERRS
);
1023 } else if (sblock_bad
->checksum_error
) {
1024 spin_lock(&sctx
->stat_lock
);
1025 sctx
->stat
.csum_errors
++;
1026 spin_unlock(&sctx
->stat_lock
);
1027 if (__ratelimit(&_rs
))
1028 scrub_print_warning("checksum error", sblock_to_check
);
1029 btrfs_dev_stat_inc_and_print(dev
,
1030 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1031 } else if (sblock_bad
->header_error
) {
1032 spin_lock(&sctx
->stat_lock
);
1033 sctx
->stat
.verify_errors
++;
1034 spin_unlock(&sctx
->stat_lock
);
1035 if (__ratelimit(&_rs
))
1036 scrub_print_warning("checksum/header error",
1038 if (sblock_bad
->generation_error
)
1039 btrfs_dev_stat_inc_and_print(dev
,
1040 BTRFS_DEV_STAT_GENERATION_ERRS
);
1042 btrfs_dev_stat_inc_and_print(dev
,
1043 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1046 if (sctx
->readonly
) {
1047 ASSERT(!sctx
->is_dev_replace
);
1051 if (!is_metadata
&& !have_csum
) {
1052 struct scrub_fixup_nodatasum
*fixup_nodatasum
;
1054 WARN_ON(sctx
->is_dev_replace
);
1059 * !is_metadata and !have_csum, this means that the data
1060 * might not be COW'ed, that it might be modified
1061 * concurrently. The general strategy to work on the
1062 * commit root does not help in the case when COW is not
1065 fixup_nodatasum
= kzalloc(sizeof(*fixup_nodatasum
), GFP_NOFS
);
1066 if (!fixup_nodatasum
)
1067 goto did_not_correct_error
;
1068 fixup_nodatasum
->sctx
= sctx
;
1069 fixup_nodatasum
->dev
= dev
;
1070 fixup_nodatasum
->logical
= logical
;
1071 fixup_nodatasum
->root
= fs_info
->extent_root
;
1072 fixup_nodatasum
->mirror_num
= failed_mirror_index
+ 1;
1073 scrub_pending_trans_workers_inc(sctx
);
1074 btrfs_init_work(&fixup_nodatasum
->work
, btrfs_scrub_helper
,
1075 scrub_fixup_nodatasum
, NULL
, NULL
);
1076 btrfs_queue_work(fs_info
->scrub_workers
,
1077 &fixup_nodatasum
->work
);
1082 * now build and submit the bios for the other mirrors, check
1084 * First try to pick the mirror which is completely without I/O
1085 * errors and also does not have a checksum error.
1086 * If one is found, and if a checksum is present, the full block
1087 * that is known to contain an error is rewritten. Afterwards
1088 * the block is known to be corrected.
1089 * If a mirror is found which is completely correct, and no
1090 * checksum is present, only those pages are rewritten that had
1091 * an I/O error in the block to be repaired, since it cannot be
1092 * determined, which copy of the other pages is better (and it
1093 * could happen otherwise that a correct page would be
1094 * overwritten by a bad one).
1096 for (mirror_index
= 0;
1097 mirror_index
< BTRFS_MAX_MIRRORS
&&
1098 sblocks_for_recheck
[mirror_index
].page_count
> 0;
1100 struct scrub_block
*sblock_other
;
1102 if (mirror_index
== failed_mirror_index
)
1104 sblock_other
= sblocks_for_recheck
+ mirror_index
;
1106 /* build and submit the bios, check checksums */
1107 scrub_recheck_block(fs_info
, sblock_other
, is_metadata
,
1108 have_csum
, csum
, generation
,
1109 sctx
->csum_size
, 0);
1111 if (!sblock_other
->header_error
&&
1112 !sblock_other
->checksum_error
&&
1113 sblock_other
->no_io_error_seen
) {
1114 if (sctx
->is_dev_replace
) {
1115 scrub_write_block_to_dev_replace(sblock_other
);
1116 goto corrected_error
;
1118 ret
= scrub_repair_block_from_good_copy(
1119 sblock_bad
, sblock_other
);
1121 goto corrected_error
;
1126 if (sblock_bad
->no_io_error_seen
&& !sctx
->is_dev_replace
)
1127 goto did_not_correct_error
;
1130 * In case of I/O errors in the area that is supposed to be
1131 * repaired, continue by picking good copies of those pages.
1132 * Select the good pages from mirrors to rewrite bad pages from
1133 * the area to fix. Afterwards verify the checksum of the block
1134 * that is supposed to be repaired. This verification step is
1135 * only done for the purpose of statistic counting and for the
1136 * final scrub report, whether errors remain.
1137 * A perfect algorithm could make use of the checksum and try
1138 * all possible combinations of pages from the different mirrors
1139 * until the checksum verification succeeds. For example, when
1140 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1141 * of mirror #2 is readable but the final checksum test fails,
1142 * then the 2nd page of mirror #3 could be tried, whether now
1143 * the final checksum succeedes. But this would be a rare
1144 * exception and is therefore not implemented. At least it is
1145 * avoided that the good copy is overwritten.
1146 * A more useful improvement would be to pick the sectors
1147 * without I/O error based on sector sizes (512 bytes on legacy
1148 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1149 * mirror could be repaired by taking 512 byte of a different
1150 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1151 * area are unreadable.
1154 for (page_num
= 0; page_num
< sblock_bad
->page_count
;
1156 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1157 struct scrub_block
*sblock_other
= NULL
;
1159 /* skip no-io-error page in scrub */
1160 if (!page_bad
->io_error
&& !sctx
->is_dev_replace
)
1163 /* try to find no-io-error page in mirrors */
1164 if (page_bad
->io_error
) {
1165 for (mirror_index
= 0;
1166 mirror_index
< BTRFS_MAX_MIRRORS
&&
1167 sblocks_for_recheck
[mirror_index
].page_count
> 0;
1169 if (!sblocks_for_recheck
[mirror_index
].
1170 pagev
[page_num
]->io_error
) {
1171 sblock_other
= sblocks_for_recheck
+
1180 if (sctx
->is_dev_replace
) {
1182 * did not find a mirror to fetch the page
1183 * from. scrub_write_page_to_dev_replace()
1184 * handles this case (page->io_error), by
1185 * filling the block with zeros before
1186 * submitting the write request
1189 sblock_other
= sblock_bad
;
1191 if (scrub_write_page_to_dev_replace(sblock_other
,
1193 btrfs_dev_replace_stats_inc(
1195 fs_info
->dev_replace
.
1199 } else if (sblock_other
) {
1200 ret
= scrub_repair_page_from_good_copy(sblock_bad
,
1204 page_bad
->io_error
= 0;
1210 if (success
&& !sctx
->is_dev_replace
) {
1211 if (is_metadata
|| have_csum
) {
1213 * need to verify the checksum now that all
1214 * sectors on disk are repaired (the write
1215 * request for data to be repaired is on its way).
1216 * Just be lazy and use scrub_recheck_block()
1217 * which re-reads the data before the checksum
1218 * is verified, but most likely the data comes out
1219 * of the page cache.
1221 scrub_recheck_block(fs_info
, sblock_bad
,
1222 is_metadata
, have_csum
, csum
,
1223 generation
, sctx
->csum_size
, 1);
1224 if (!sblock_bad
->header_error
&&
1225 !sblock_bad
->checksum_error
&&
1226 sblock_bad
->no_io_error_seen
)
1227 goto corrected_error
;
1229 goto did_not_correct_error
;
1232 spin_lock(&sctx
->stat_lock
);
1233 sctx
->stat
.corrected_errors
++;
1234 sblock_to_check
->data_corrected
= 1;
1235 spin_unlock(&sctx
->stat_lock
);
1236 printk_ratelimited_in_rcu(KERN_ERR
1237 "BTRFS: fixed up error at logical %llu on dev %s\n",
1238 logical
, rcu_str_deref(dev
->name
));
1241 did_not_correct_error
:
1242 spin_lock(&sctx
->stat_lock
);
1243 sctx
->stat
.uncorrectable_errors
++;
1244 spin_unlock(&sctx
->stat_lock
);
1245 printk_ratelimited_in_rcu(KERN_ERR
1246 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
1247 logical
, rcu_str_deref(dev
->name
));
1251 if (sblocks_for_recheck
) {
1252 for (mirror_index
= 0; mirror_index
< BTRFS_MAX_MIRRORS
;
1254 struct scrub_block
*sblock
= sblocks_for_recheck
+
1256 struct scrub_recover
*recover
;
1259 for (page_index
= 0; page_index
< sblock
->page_count
;
1261 sblock
->pagev
[page_index
]->sblock
= NULL
;
1262 recover
= sblock
->pagev
[page_index
]->recover
;
1264 scrub_put_recover(recover
);
1265 sblock
->pagev
[page_index
]->recover
=
1268 scrub_page_put(sblock
->pagev
[page_index
]);
1271 kfree(sblocks_for_recheck
);
1277 static inline int scrub_nr_raid_mirrors(struct btrfs_bio
*bbio
)
1279 if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID5
)
1281 else if (bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID6
)
1284 return (int)bbio
->num_stripes
;
1287 static inline void scrub_stripe_index_and_offset(u64 logical
, u64 map_type
,
1290 int nstripes
, int mirror
,
1296 if (map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
1298 for (i
= 0; i
< nstripes
; i
++) {
1299 if (raid_map
[i
] == RAID6_Q_STRIPE
||
1300 raid_map
[i
] == RAID5_P_STRIPE
)
1303 if (logical
>= raid_map
[i
] &&
1304 logical
< raid_map
[i
] + mapped_length
)
1309 *stripe_offset
= logical
- raid_map
[i
];
1311 /* The other RAID type */
1312 *stripe_index
= mirror
;
1317 static int scrub_setup_recheck_block(struct scrub_block
*original_sblock
,
1318 struct scrub_block
*sblocks_for_recheck
)
1320 struct scrub_ctx
*sctx
= original_sblock
->sctx
;
1321 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
1322 u64 length
= original_sblock
->page_count
* PAGE_SIZE
;
1323 u64 logical
= original_sblock
->pagev
[0]->logical
;
1324 struct scrub_recover
*recover
;
1325 struct btrfs_bio
*bbio
;
1336 * note: the two members refs and outstanding_pages
1337 * are not used (and not set) in the blocks that are used for
1338 * the recheck procedure
1341 while (length
> 0) {
1342 sublen
= min_t(u64
, length
, PAGE_SIZE
);
1343 mapped_length
= sublen
;
1347 * with a length of PAGE_SIZE, each returned stripe
1348 * represents one mirror
1350 ret
= btrfs_map_sblock(fs_info
, REQ_GET_READ_MIRRORS
, logical
,
1351 &mapped_length
, &bbio
, 0, 1);
1352 if (ret
|| !bbio
|| mapped_length
< sublen
) {
1353 btrfs_put_bbio(bbio
);
1357 recover
= kzalloc(sizeof(struct scrub_recover
), GFP_NOFS
);
1359 btrfs_put_bbio(bbio
);
1363 atomic_set(&recover
->refs
, 1);
1364 recover
->bbio
= bbio
;
1365 recover
->map_length
= mapped_length
;
1367 BUG_ON(page_index
>= SCRUB_PAGES_PER_RD_BIO
);
1369 nmirrors
= min(scrub_nr_raid_mirrors(bbio
), BTRFS_MAX_MIRRORS
);
1371 for (mirror_index
= 0; mirror_index
< nmirrors
;
1373 struct scrub_block
*sblock
;
1374 struct scrub_page
*page
;
1376 sblock
= sblocks_for_recheck
+ mirror_index
;
1377 sblock
->sctx
= sctx
;
1378 page
= kzalloc(sizeof(*page
), GFP_NOFS
);
1381 spin_lock(&sctx
->stat_lock
);
1382 sctx
->stat
.malloc_errors
++;
1383 spin_unlock(&sctx
->stat_lock
);
1384 scrub_put_recover(recover
);
1387 scrub_page_get(page
);
1388 sblock
->pagev
[page_index
] = page
;
1389 page
->logical
= logical
;
1391 scrub_stripe_index_and_offset(logical
,
1400 page
->physical
= bbio
->stripes
[stripe_index
].physical
+
1402 page
->dev
= bbio
->stripes
[stripe_index
].dev
;
1404 BUG_ON(page_index
>= original_sblock
->page_count
);
1405 page
->physical_for_dev_replace
=
1406 original_sblock
->pagev
[page_index
]->
1407 physical_for_dev_replace
;
1408 /* for missing devices, dev->bdev is NULL */
1409 page
->mirror_num
= mirror_index
+ 1;
1410 sblock
->page_count
++;
1411 page
->page
= alloc_page(GFP_NOFS
);
1415 scrub_get_recover(recover
);
1416 page
->recover
= recover
;
1418 scrub_put_recover(recover
);
1427 struct scrub_bio_ret
{
1428 struct completion event
;
1432 static void scrub_bio_wait_endio(struct bio
*bio
, int error
)
1434 struct scrub_bio_ret
*ret
= bio
->bi_private
;
1437 complete(&ret
->event
);
1440 static inline int scrub_is_page_on_raid56(struct scrub_page
*page
)
1442 return page
->recover
&&
1443 (page
->recover
->bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
);
1446 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info
*fs_info
,
1448 struct scrub_page
*page
)
1450 struct scrub_bio_ret done
;
1453 init_completion(&done
.event
);
1455 bio
->bi_iter
.bi_sector
= page
->logical
>> 9;
1456 bio
->bi_private
= &done
;
1457 bio
->bi_end_io
= scrub_bio_wait_endio
;
1459 ret
= raid56_parity_recover(fs_info
->fs_root
, bio
, page
->recover
->bbio
,
1460 page
->recover
->map_length
,
1461 page
->mirror_num
, 0);
1465 wait_for_completion(&done
.event
);
1473 * this function will check the on disk data for checksum errors, header
1474 * errors and read I/O errors. If any I/O errors happen, the exact pages
1475 * which are errored are marked as being bad. The goal is to enable scrub
1476 * to take those pages that are not errored from all the mirrors so that
1477 * the pages that are errored in the just handled mirror can be repaired.
1479 static void scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
1480 struct scrub_block
*sblock
, int is_metadata
,
1481 int have_csum
, u8
*csum
, u64 generation
,
1482 u16 csum_size
, int retry_failed_mirror
)
1486 sblock
->no_io_error_seen
= 1;
1487 sblock
->header_error
= 0;
1488 sblock
->checksum_error
= 0;
1490 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1492 struct scrub_page
*page
= sblock
->pagev
[page_num
];
1494 if (page
->dev
->bdev
== NULL
) {
1496 sblock
->no_io_error_seen
= 0;
1500 WARN_ON(!page
->page
);
1501 bio
= btrfs_io_bio_alloc(GFP_NOFS
, 1);
1504 sblock
->no_io_error_seen
= 0;
1507 bio
->bi_bdev
= page
->dev
->bdev
;
1509 bio_add_page(bio
, page
->page
, PAGE_SIZE
, 0);
1510 if (!retry_failed_mirror
&& scrub_is_page_on_raid56(page
)) {
1511 if (scrub_submit_raid56_bio_wait(fs_info
, bio
, page
))
1512 sblock
->no_io_error_seen
= 0;
1514 bio
->bi_iter
.bi_sector
= page
->physical
>> 9;
1516 if (btrfsic_submit_bio_wait(READ
, bio
))
1517 sblock
->no_io_error_seen
= 0;
1523 if (sblock
->no_io_error_seen
)
1524 scrub_recheck_block_checksum(fs_info
, sblock
, is_metadata
,
1525 have_csum
, csum
, generation
,
1531 static inline int scrub_check_fsid(u8 fsid
[],
1532 struct scrub_page
*spage
)
1534 struct btrfs_fs_devices
*fs_devices
= spage
->dev
->fs_devices
;
1537 ret
= memcmp(fsid
, fs_devices
->fsid
, BTRFS_UUID_SIZE
);
1541 static void scrub_recheck_block_checksum(struct btrfs_fs_info
*fs_info
,
1542 struct scrub_block
*sblock
,
1543 int is_metadata
, int have_csum
,
1544 const u8
*csum
, u64 generation
,
1548 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1550 void *mapped_buffer
;
1552 WARN_ON(!sblock
->pagev
[0]->page
);
1554 struct btrfs_header
*h
;
1556 mapped_buffer
= kmap_atomic(sblock
->pagev
[0]->page
);
1557 h
= (struct btrfs_header
*)mapped_buffer
;
1559 if (sblock
->pagev
[0]->logical
!= btrfs_stack_header_bytenr(h
) ||
1560 !scrub_check_fsid(h
->fsid
, sblock
->pagev
[0]) ||
1561 memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
1563 sblock
->header_error
= 1;
1564 } else if (generation
!= btrfs_stack_header_generation(h
)) {
1565 sblock
->header_error
= 1;
1566 sblock
->generation_error
= 1;
1573 mapped_buffer
= kmap_atomic(sblock
->pagev
[0]->page
);
1576 for (page_num
= 0;;) {
1577 if (page_num
== 0 && is_metadata
)
1578 crc
= btrfs_csum_data(
1579 ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
,
1580 crc
, PAGE_SIZE
- BTRFS_CSUM_SIZE
);
1582 crc
= btrfs_csum_data(mapped_buffer
, crc
, PAGE_SIZE
);
1584 kunmap_atomic(mapped_buffer
);
1586 if (page_num
>= sblock
->page_count
)
1588 WARN_ON(!sblock
->pagev
[page_num
]->page
);
1590 mapped_buffer
= kmap_atomic(sblock
->pagev
[page_num
]->page
);
1593 btrfs_csum_final(crc
, calculated_csum
);
1594 if (memcmp(calculated_csum
, csum
, csum_size
))
1595 sblock
->checksum_error
= 1;
1598 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
1599 struct scrub_block
*sblock_good
)
1604 for (page_num
= 0; page_num
< sblock_bad
->page_count
; page_num
++) {
1607 ret_sub
= scrub_repair_page_from_good_copy(sblock_bad
,
1617 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
1618 struct scrub_block
*sblock_good
,
1619 int page_num
, int force_write
)
1621 struct scrub_page
*page_bad
= sblock_bad
->pagev
[page_num
];
1622 struct scrub_page
*page_good
= sblock_good
->pagev
[page_num
];
1624 BUG_ON(page_bad
->page
== NULL
);
1625 BUG_ON(page_good
->page
== NULL
);
1626 if (force_write
|| sblock_bad
->header_error
||
1627 sblock_bad
->checksum_error
|| page_bad
->io_error
) {
1631 if (!page_bad
->dev
->bdev
) {
1632 printk_ratelimited(KERN_WARNING
"BTRFS: "
1633 "scrub_repair_page_from_good_copy(bdev == NULL) "
1634 "is unexpected!\n");
1638 bio
= btrfs_io_bio_alloc(GFP_NOFS
, 1);
1641 bio
->bi_bdev
= page_bad
->dev
->bdev
;
1642 bio
->bi_iter
.bi_sector
= page_bad
->physical
>> 9;
1644 ret
= bio_add_page(bio
, page_good
->page
, PAGE_SIZE
, 0);
1645 if (PAGE_SIZE
!= ret
) {
1650 if (btrfsic_submit_bio_wait(WRITE
, bio
)) {
1651 btrfs_dev_stat_inc_and_print(page_bad
->dev
,
1652 BTRFS_DEV_STAT_WRITE_ERRS
);
1653 btrfs_dev_replace_stats_inc(
1654 &sblock_bad
->sctx
->dev_root
->fs_info
->
1655 dev_replace
.num_write_errors
);
1665 static void scrub_write_block_to_dev_replace(struct scrub_block
*sblock
)
1670 * This block is used for the check of the parity on the source device,
1671 * so the data needn't be written into the destination device.
1673 if (sblock
->sparity
)
1676 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1679 ret
= scrub_write_page_to_dev_replace(sblock
, page_num
);
1681 btrfs_dev_replace_stats_inc(
1682 &sblock
->sctx
->dev_root
->fs_info
->dev_replace
.
1687 static int scrub_write_page_to_dev_replace(struct scrub_block
*sblock
,
1690 struct scrub_page
*spage
= sblock
->pagev
[page_num
];
1692 BUG_ON(spage
->page
== NULL
);
1693 if (spage
->io_error
) {
1694 void *mapped_buffer
= kmap_atomic(spage
->page
);
1696 memset(mapped_buffer
, 0, PAGE_CACHE_SIZE
);
1697 flush_dcache_page(spage
->page
);
1698 kunmap_atomic(mapped_buffer
);
1700 return scrub_add_page_to_wr_bio(sblock
->sctx
, spage
);
1703 static int scrub_add_page_to_wr_bio(struct scrub_ctx
*sctx
,
1704 struct scrub_page
*spage
)
1706 struct scrub_wr_ctx
*wr_ctx
= &sctx
->wr_ctx
;
1707 struct scrub_bio
*sbio
;
1710 mutex_lock(&wr_ctx
->wr_lock
);
1712 if (!wr_ctx
->wr_curr_bio
) {
1713 wr_ctx
->wr_curr_bio
= kzalloc(sizeof(*wr_ctx
->wr_curr_bio
),
1715 if (!wr_ctx
->wr_curr_bio
) {
1716 mutex_unlock(&wr_ctx
->wr_lock
);
1719 wr_ctx
->wr_curr_bio
->sctx
= sctx
;
1720 wr_ctx
->wr_curr_bio
->page_count
= 0;
1722 sbio
= wr_ctx
->wr_curr_bio
;
1723 if (sbio
->page_count
== 0) {
1726 sbio
->physical
= spage
->physical_for_dev_replace
;
1727 sbio
->logical
= spage
->logical
;
1728 sbio
->dev
= wr_ctx
->tgtdev
;
1731 bio
= btrfs_io_bio_alloc(GFP_NOFS
, wr_ctx
->pages_per_wr_bio
);
1733 mutex_unlock(&wr_ctx
->wr_lock
);
1739 bio
->bi_private
= sbio
;
1740 bio
->bi_end_io
= scrub_wr_bio_end_io
;
1741 bio
->bi_bdev
= sbio
->dev
->bdev
;
1742 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
1744 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
1745 spage
->physical_for_dev_replace
||
1746 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
1748 scrub_wr_submit(sctx
);
1752 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
1753 if (ret
!= PAGE_SIZE
) {
1754 if (sbio
->page_count
< 1) {
1757 mutex_unlock(&wr_ctx
->wr_lock
);
1760 scrub_wr_submit(sctx
);
1764 sbio
->pagev
[sbio
->page_count
] = spage
;
1765 scrub_page_get(spage
);
1767 if (sbio
->page_count
== wr_ctx
->pages_per_wr_bio
)
1768 scrub_wr_submit(sctx
);
1769 mutex_unlock(&wr_ctx
->wr_lock
);
1774 static void scrub_wr_submit(struct scrub_ctx
*sctx
)
1776 struct scrub_wr_ctx
*wr_ctx
= &sctx
->wr_ctx
;
1777 struct scrub_bio
*sbio
;
1779 if (!wr_ctx
->wr_curr_bio
)
1782 sbio
= wr_ctx
->wr_curr_bio
;
1783 wr_ctx
->wr_curr_bio
= NULL
;
1784 WARN_ON(!sbio
->bio
->bi_bdev
);
1785 scrub_pending_bio_inc(sctx
);
1786 /* process all writes in a single worker thread. Then the block layer
1787 * orders the requests before sending them to the driver which
1788 * doubled the write performance on spinning disks when measured
1790 btrfsic_submit_bio(WRITE
, sbio
->bio
);
1793 static void scrub_wr_bio_end_io(struct bio
*bio
, int err
)
1795 struct scrub_bio
*sbio
= bio
->bi_private
;
1796 struct btrfs_fs_info
*fs_info
= sbio
->dev
->dev_root
->fs_info
;
1801 btrfs_init_work(&sbio
->work
, btrfs_scrubwrc_helper
,
1802 scrub_wr_bio_end_io_worker
, NULL
, NULL
);
1803 btrfs_queue_work(fs_info
->scrub_wr_completion_workers
, &sbio
->work
);
1806 static void scrub_wr_bio_end_io_worker(struct btrfs_work
*work
)
1808 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
1809 struct scrub_ctx
*sctx
= sbio
->sctx
;
1812 WARN_ON(sbio
->page_count
> SCRUB_PAGES_PER_WR_BIO
);
1814 struct btrfs_dev_replace
*dev_replace
=
1815 &sbio
->sctx
->dev_root
->fs_info
->dev_replace
;
1817 for (i
= 0; i
< sbio
->page_count
; i
++) {
1818 struct scrub_page
*spage
= sbio
->pagev
[i
];
1820 spage
->io_error
= 1;
1821 btrfs_dev_replace_stats_inc(&dev_replace
->
1826 for (i
= 0; i
< sbio
->page_count
; i
++)
1827 scrub_page_put(sbio
->pagev
[i
]);
1831 scrub_pending_bio_dec(sctx
);
1834 static int scrub_checksum(struct scrub_block
*sblock
)
1839 WARN_ON(sblock
->page_count
< 1);
1840 flags
= sblock
->pagev
[0]->flags
;
1842 if (flags
& BTRFS_EXTENT_FLAG_DATA
)
1843 ret
= scrub_checksum_data(sblock
);
1844 else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
1845 ret
= scrub_checksum_tree_block(sblock
);
1846 else if (flags
& BTRFS_EXTENT_FLAG_SUPER
)
1847 (void)scrub_checksum_super(sblock
);
1851 scrub_handle_errored_block(sblock
);
1856 static int scrub_checksum_data(struct scrub_block
*sblock
)
1858 struct scrub_ctx
*sctx
= sblock
->sctx
;
1859 u8 csum
[BTRFS_CSUM_SIZE
];
1868 BUG_ON(sblock
->page_count
< 1);
1869 if (!sblock
->pagev
[0]->have_csum
)
1872 on_disk_csum
= sblock
->pagev
[0]->csum
;
1873 page
= sblock
->pagev
[0]->page
;
1874 buffer
= kmap_atomic(page
);
1876 len
= sctx
->sectorsize
;
1879 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1881 crc
= btrfs_csum_data(buffer
, crc
, l
);
1882 kunmap_atomic(buffer
);
1887 BUG_ON(index
>= sblock
->page_count
);
1888 BUG_ON(!sblock
->pagev
[index
]->page
);
1889 page
= sblock
->pagev
[index
]->page
;
1890 buffer
= kmap_atomic(page
);
1893 btrfs_csum_final(crc
, csum
);
1894 if (memcmp(csum
, on_disk_csum
, sctx
->csum_size
))
1900 static int scrub_checksum_tree_block(struct scrub_block
*sblock
)
1902 struct scrub_ctx
*sctx
= sblock
->sctx
;
1903 struct btrfs_header
*h
;
1904 struct btrfs_root
*root
= sctx
->dev_root
;
1905 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1906 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1907 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1909 void *mapped_buffer
;
1918 BUG_ON(sblock
->page_count
< 1);
1919 page
= sblock
->pagev
[0]->page
;
1920 mapped_buffer
= kmap_atomic(page
);
1921 h
= (struct btrfs_header
*)mapped_buffer
;
1922 memcpy(on_disk_csum
, h
->csum
, sctx
->csum_size
);
1925 * we don't use the getter functions here, as we
1926 * a) don't have an extent buffer and
1927 * b) the page is already kmapped
1930 if (sblock
->pagev
[0]->logical
!= btrfs_stack_header_bytenr(h
))
1933 if (sblock
->pagev
[0]->generation
!= btrfs_stack_header_generation(h
))
1936 if (!scrub_check_fsid(h
->fsid
, sblock
->pagev
[0]))
1939 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
1943 len
= sctx
->nodesize
- BTRFS_CSUM_SIZE
;
1944 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
1945 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
1948 u64 l
= min_t(u64
, len
, mapped_size
);
1950 crc
= btrfs_csum_data(p
, crc
, l
);
1951 kunmap_atomic(mapped_buffer
);
1956 BUG_ON(index
>= sblock
->page_count
);
1957 BUG_ON(!sblock
->pagev
[index
]->page
);
1958 page
= sblock
->pagev
[index
]->page
;
1959 mapped_buffer
= kmap_atomic(page
);
1960 mapped_size
= PAGE_SIZE
;
1964 btrfs_csum_final(crc
, calculated_csum
);
1965 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
1968 return fail
|| crc_fail
;
1971 static int scrub_checksum_super(struct scrub_block
*sblock
)
1973 struct btrfs_super_block
*s
;
1974 struct scrub_ctx
*sctx
= sblock
->sctx
;
1975 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1976 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1978 void *mapped_buffer
;
1987 BUG_ON(sblock
->page_count
< 1);
1988 page
= sblock
->pagev
[0]->page
;
1989 mapped_buffer
= kmap_atomic(page
);
1990 s
= (struct btrfs_super_block
*)mapped_buffer
;
1991 memcpy(on_disk_csum
, s
->csum
, sctx
->csum_size
);
1993 if (sblock
->pagev
[0]->logical
!= btrfs_super_bytenr(s
))
1996 if (sblock
->pagev
[0]->generation
!= btrfs_super_generation(s
))
1999 if (!scrub_check_fsid(s
->fsid
, sblock
->pagev
[0]))
2002 len
= BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
;
2003 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
2004 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
2007 u64 l
= min_t(u64
, len
, mapped_size
);
2009 crc
= btrfs_csum_data(p
, crc
, l
);
2010 kunmap_atomic(mapped_buffer
);
2015 BUG_ON(index
>= sblock
->page_count
);
2016 BUG_ON(!sblock
->pagev
[index
]->page
);
2017 page
= sblock
->pagev
[index
]->page
;
2018 mapped_buffer
= kmap_atomic(page
);
2019 mapped_size
= PAGE_SIZE
;
2023 btrfs_csum_final(crc
, calculated_csum
);
2024 if (memcmp(calculated_csum
, on_disk_csum
, sctx
->csum_size
))
2027 if (fail_cor
+ fail_gen
) {
2029 * if we find an error in a super block, we just report it.
2030 * They will get written with the next transaction commit
2033 spin_lock(&sctx
->stat_lock
);
2034 ++sctx
->stat
.super_errors
;
2035 spin_unlock(&sctx
->stat_lock
);
2037 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
2038 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
2040 btrfs_dev_stat_inc_and_print(sblock
->pagev
[0]->dev
,
2041 BTRFS_DEV_STAT_GENERATION_ERRS
);
2044 return fail_cor
+ fail_gen
;
2047 static void scrub_block_get(struct scrub_block
*sblock
)
2049 atomic_inc(&sblock
->refs
);
2052 static void scrub_block_put(struct scrub_block
*sblock
)
2054 if (atomic_dec_and_test(&sblock
->refs
)) {
2057 if (sblock
->sparity
)
2058 scrub_parity_put(sblock
->sparity
);
2060 for (i
= 0; i
< sblock
->page_count
; i
++)
2061 scrub_page_put(sblock
->pagev
[i
]);
2066 static void scrub_page_get(struct scrub_page
*spage
)
2068 atomic_inc(&spage
->refs
);
2071 static void scrub_page_put(struct scrub_page
*spage
)
2073 if (atomic_dec_and_test(&spage
->refs
)) {
2075 __free_page(spage
->page
);
2080 static void scrub_submit(struct scrub_ctx
*sctx
)
2082 struct scrub_bio
*sbio
;
2084 if (sctx
->curr
== -1)
2087 sbio
= sctx
->bios
[sctx
->curr
];
2089 scrub_pending_bio_inc(sctx
);
2091 if (!sbio
->bio
->bi_bdev
) {
2093 * this case should not happen. If btrfs_map_block() is
2094 * wrong, it could happen for dev-replace operations on
2095 * missing devices when no mirrors are available, but in
2096 * this case it should already fail the mount.
2097 * This case is handled correctly (but _very_ slowly).
2099 printk_ratelimited(KERN_WARNING
2100 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
2101 bio_endio(sbio
->bio
, -EIO
);
2103 btrfsic_submit_bio(READ
, sbio
->bio
);
2107 static int scrub_add_page_to_rd_bio(struct scrub_ctx
*sctx
,
2108 struct scrub_page
*spage
)
2110 struct scrub_block
*sblock
= spage
->sblock
;
2111 struct scrub_bio
*sbio
;
2116 * grab a fresh bio or wait for one to become available
2118 while (sctx
->curr
== -1) {
2119 spin_lock(&sctx
->list_lock
);
2120 sctx
->curr
= sctx
->first_free
;
2121 if (sctx
->curr
!= -1) {
2122 sctx
->first_free
= sctx
->bios
[sctx
->curr
]->next_free
;
2123 sctx
->bios
[sctx
->curr
]->next_free
= -1;
2124 sctx
->bios
[sctx
->curr
]->page_count
= 0;
2125 spin_unlock(&sctx
->list_lock
);
2127 spin_unlock(&sctx
->list_lock
);
2128 wait_event(sctx
->list_wait
, sctx
->first_free
!= -1);
2131 sbio
= sctx
->bios
[sctx
->curr
];
2132 if (sbio
->page_count
== 0) {
2135 sbio
->physical
= spage
->physical
;
2136 sbio
->logical
= spage
->logical
;
2137 sbio
->dev
= spage
->dev
;
2140 bio
= btrfs_io_bio_alloc(GFP_NOFS
, sctx
->pages_per_rd_bio
);
2146 bio
->bi_private
= sbio
;
2147 bio
->bi_end_io
= scrub_bio_end_io
;
2148 bio
->bi_bdev
= sbio
->dev
->bdev
;
2149 bio
->bi_iter
.bi_sector
= sbio
->physical
>> 9;
2151 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
2153 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
2155 sbio
->dev
!= spage
->dev
) {
2160 sbio
->pagev
[sbio
->page_count
] = spage
;
2161 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
2162 if (ret
!= PAGE_SIZE
) {
2163 if (sbio
->page_count
< 1) {
2172 scrub_block_get(sblock
); /* one for the page added to the bio */
2173 atomic_inc(&sblock
->outstanding_pages
);
2175 if (sbio
->page_count
== sctx
->pages_per_rd_bio
)
2181 static int scrub_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2182 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2183 u64 gen
, int mirror_num
, u8
*csum
, int force
,
2184 u64 physical_for_dev_replace
)
2186 struct scrub_block
*sblock
;
2189 sblock
= kzalloc(sizeof(*sblock
), GFP_NOFS
);
2191 spin_lock(&sctx
->stat_lock
);
2192 sctx
->stat
.malloc_errors
++;
2193 spin_unlock(&sctx
->stat_lock
);
2197 /* one ref inside this function, plus one for each page added to
2199 atomic_set(&sblock
->refs
, 1);
2200 sblock
->sctx
= sctx
;
2201 sblock
->no_io_error_seen
= 1;
2203 for (index
= 0; len
> 0; index
++) {
2204 struct scrub_page
*spage
;
2205 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2207 spage
= kzalloc(sizeof(*spage
), GFP_NOFS
);
2210 spin_lock(&sctx
->stat_lock
);
2211 sctx
->stat
.malloc_errors
++;
2212 spin_unlock(&sctx
->stat_lock
);
2213 scrub_block_put(sblock
);
2216 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2217 scrub_page_get(spage
);
2218 sblock
->pagev
[index
] = spage
;
2219 spage
->sblock
= sblock
;
2221 spage
->flags
= flags
;
2222 spage
->generation
= gen
;
2223 spage
->logical
= logical
;
2224 spage
->physical
= physical
;
2225 spage
->physical_for_dev_replace
= physical_for_dev_replace
;
2226 spage
->mirror_num
= mirror_num
;
2228 spage
->have_csum
= 1;
2229 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2231 spage
->have_csum
= 0;
2233 sblock
->page_count
++;
2234 spage
->page
= alloc_page(GFP_NOFS
);
2240 physical_for_dev_replace
+= l
;
2243 WARN_ON(sblock
->page_count
== 0);
2244 for (index
= 0; index
< sblock
->page_count
; index
++) {
2245 struct scrub_page
*spage
= sblock
->pagev
[index
];
2248 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2250 scrub_block_put(sblock
);
2258 /* last one frees, either here or in bio completion for last page */
2259 scrub_block_put(sblock
);
2263 static void scrub_bio_end_io(struct bio
*bio
, int err
)
2265 struct scrub_bio
*sbio
= bio
->bi_private
;
2266 struct btrfs_fs_info
*fs_info
= sbio
->dev
->dev_root
->fs_info
;
2271 btrfs_queue_work(fs_info
->scrub_workers
, &sbio
->work
);
2274 static void scrub_bio_end_io_worker(struct btrfs_work
*work
)
2276 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
2277 struct scrub_ctx
*sctx
= sbio
->sctx
;
2280 BUG_ON(sbio
->page_count
> SCRUB_PAGES_PER_RD_BIO
);
2282 for (i
= 0; i
< sbio
->page_count
; i
++) {
2283 struct scrub_page
*spage
= sbio
->pagev
[i
];
2285 spage
->io_error
= 1;
2286 spage
->sblock
->no_io_error_seen
= 0;
2290 /* now complete the scrub_block items that have all pages completed */
2291 for (i
= 0; i
< sbio
->page_count
; i
++) {
2292 struct scrub_page
*spage
= sbio
->pagev
[i
];
2293 struct scrub_block
*sblock
= spage
->sblock
;
2295 if (atomic_dec_and_test(&sblock
->outstanding_pages
))
2296 scrub_block_complete(sblock
);
2297 scrub_block_put(sblock
);
2302 spin_lock(&sctx
->list_lock
);
2303 sbio
->next_free
= sctx
->first_free
;
2304 sctx
->first_free
= sbio
->index
;
2305 spin_unlock(&sctx
->list_lock
);
2307 if (sctx
->is_dev_replace
&&
2308 atomic_read(&sctx
->wr_ctx
.flush_all_writes
)) {
2309 mutex_lock(&sctx
->wr_ctx
.wr_lock
);
2310 scrub_wr_submit(sctx
);
2311 mutex_unlock(&sctx
->wr_ctx
.wr_lock
);
2314 scrub_pending_bio_dec(sctx
);
2317 static inline void __scrub_mark_bitmap(struct scrub_parity
*sparity
,
2318 unsigned long *bitmap
,
2323 int sectorsize
= sparity
->sctx
->dev_root
->sectorsize
;
2325 if (len
>= sparity
->stripe_len
) {
2326 bitmap_set(bitmap
, 0, sparity
->nsectors
);
2330 start
-= sparity
->logic_start
;
2331 start
= div_u64_rem(start
, sparity
->stripe_len
, &offset
);
2332 offset
/= sectorsize
;
2333 nsectors
= (int)len
/ sectorsize
;
2335 if (offset
+ nsectors
<= sparity
->nsectors
) {
2336 bitmap_set(bitmap
, offset
, nsectors
);
2340 bitmap_set(bitmap
, offset
, sparity
->nsectors
- offset
);
2341 bitmap_set(bitmap
, 0, nsectors
- (sparity
->nsectors
- offset
));
2344 static inline void scrub_parity_mark_sectors_error(struct scrub_parity
*sparity
,
2347 __scrub_mark_bitmap(sparity
, sparity
->ebitmap
, start
, len
);
2350 static inline void scrub_parity_mark_sectors_data(struct scrub_parity
*sparity
,
2353 __scrub_mark_bitmap(sparity
, sparity
->dbitmap
, start
, len
);
2356 static void scrub_block_complete(struct scrub_block
*sblock
)
2360 if (!sblock
->no_io_error_seen
) {
2362 scrub_handle_errored_block(sblock
);
2365 * if has checksum error, write via repair mechanism in
2366 * dev replace case, otherwise write here in dev replace
2369 corrupted
= scrub_checksum(sblock
);
2370 if (!corrupted
&& sblock
->sctx
->is_dev_replace
)
2371 scrub_write_block_to_dev_replace(sblock
);
2374 if (sblock
->sparity
&& corrupted
&& !sblock
->data_corrected
) {
2375 u64 start
= sblock
->pagev
[0]->logical
;
2376 u64 end
= sblock
->pagev
[sblock
->page_count
- 1]->logical
+
2379 scrub_parity_mark_sectors_error(sblock
->sparity
,
2380 start
, end
- start
);
2384 static int scrub_find_csum(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2387 struct btrfs_ordered_sum
*sum
= NULL
;
2388 unsigned long index
;
2389 unsigned long num_sectors
;
2391 while (!list_empty(&sctx
->csum_list
)) {
2392 sum
= list_first_entry(&sctx
->csum_list
,
2393 struct btrfs_ordered_sum
, list
);
2394 if (sum
->bytenr
> logical
)
2396 if (sum
->bytenr
+ sum
->len
> logical
)
2399 ++sctx
->stat
.csum_discards
;
2400 list_del(&sum
->list
);
2407 index
= ((u32
)(logical
- sum
->bytenr
)) / sctx
->sectorsize
;
2408 num_sectors
= sum
->len
/ sctx
->sectorsize
;
2409 memcpy(csum
, sum
->sums
+ index
, sctx
->csum_size
);
2410 if (index
== num_sectors
- 1) {
2411 list_del(&sum
->list
);
2417 /* scrub extent tries to collect up to 64 kB for each bio */
2418 static int scrub_extent(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
2419 u64 physical
, struct btrfs_device
*dev
, u64 flags
,
2420 u64 gen
, int mirror_num
, u64 physical_for_dev_replace
)
2423 u8 csum
[BTRFS_CSUM_SIZE
];
2426 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2427 blocksize
= sctx
->sectorsize
;
2428 spin_lock(&sctx
->stat_lock
);
2429 sctx
->stat
.data_extents_scrubbed
++;
2430 sctx
->stat
.data_bytes_scrubbed
+= len
;
2431 spin_unlock(&sctx
->stat_lock
);
2432 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2433 blocksize
= sctx
->nodesize
;
2434 spin_lock(&sctx
->stat_lock
);
2435 sctx
->stat
.tree_extents_scrubbed
++;
2436 sctx
->stat
.tree_bytes_scrubbed
+= len
;
2437 spin_unlock(&sctx
->stat_lock
);
2439 blocksize
= sctx
->sectorsize
;
2444 u64 l
= min_t(u64
, len
, blocksize
);
2447 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2448 /* push csums to sbio */
2449 have_csum
= scrub_find_csum(sctx
, logical
, l
, csum
);
2451 ++sctx
->stat
.no_csum
;
2452 if (sctx
->is_dev_replace
&& !have_csum
) {
2453 ret
= copy_nocow_pages(sctx
, logical
, l
,
2455 physical_for_dev_replace
);
2456 goto behind_scrub_pages
;
2459 ret
= scrub_pages(sctx
, logical
, l
, physical
, dev
, flags
, gen
,
2460 mirror_num
, have_csum
? csum
: NULL
, 0,
2461 physical_for_dev_replace
);
2468 physical_for_dev_replace
+= l
;
2473 static int scrub_pages_for_parity(struct scrub_parity
*sparity
,
2474 u64 logical
, u64 len
,
2475 u64 physical
, struct btrfs_device
*dev
,
2476 u64 flags
, u64 gen
, int mirror_num
, u8
*csum
)
2478 struct scrub_ctx
*sctx
= sparity
->sctx
;
2479 struct scrub_block
*sblock
;
2482 sblock
= kzalloc(sizeof(*sblock
), GFP_NOFS
);
2484 spin_lock(&sctx
->stat_lock
);
2485 sctx
->stat
.malloc_errors
++;
2486 spin_unlock(&sctx
->stat_lock
);
2490 /* one ref inside this function, plus one for each page added to
2492 atomic_set(&sblock
->refs
, 1);
2493 sblock
->sctx
= sctx
;
2494 sblock
->no_io_error_seen
= 1;
2495 sblock
->sparity
= sparity
;
2496 scrub_parity_get(sparity
);
2498 for (index
= 0; len
> 0; index
++) {
2499 struct scrub_page
*spage
;
2500 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
2502 spage
= kzalloc(sizeof(*spage
), GFP_NOFS
);
2505 spin_lock(&sctx
->stat_lock
);
2506 sctx
->stat
.malloc_errors
++;
2507 spin_unlock(&sctx
->stat_lock
);
2508 scrub_block_put(sblock
);
2511 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
2512 /* For scrub block */
2513 scrub_page_get(spage
);
2514 sblock
->pagev
[index
] = spage
;
2515 /* For scrub parity */
2516 scrub_page_get(spage
);
2517 list_add_tail(&spage
->list
, &sparity
->spages
);
2518 spage
->sblock
= sblock
;
2520 spage
->flags
= flags
;
2521 spage
->generation
= gen
;
2522 spage
->logical
= logical
;
2523 spage
->physical
= physical
;
2524 spage
->mirror_num
= mirror_num
;
2526 spage
->have_csum
= 1;
2527 memcpy(spage
->csum
, csum
, sctx
->csum_size
);
2529 spage
->have_csum
= 0;
2531 sblock
->page_count
++;
2532 spage
->page
= alloc_page(GFP_NOFS
);
2540 WARN_ON(sblock
->page_count
== 0);
2541 for (index
= 0; index
< sblock
->page_count
; index
++) {
2542 struct scrub_page
*spage
= sblock
->pagev
[index
];
2545 ret
= scrub_add_page_to_rd_bio(sctx
, spage
);
2547 scrub_block_put(sblock
);
2552 /* last one frees, either here or in bio completion for last page */
2553 scrub_block_put(sblock
);
2557 static int scrub_extent_for_parity(struct scrub_parity
*sparity
,
2558 u64 logical
, u64 len
,
2559 u64 physical
, struct btrfs_device
*dev
,
2560 u64 flags
, u64 gen
, int mirror_num
)
2562 struct scrub_ctx
*sctx
= sparity
->sctx
;
2564 u8 csum
[BTRFS_CSUM_SIZE
];
2567 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2568 blocksize
= sctx
->sectorsize
;
2569 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
2570 blocksize
= sctx
->nodesize
;
2572 blocksize
= sctx
->sectorsize
;
2577 u64 l
= min_t(u64
, len
, blocksize
);
2580 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
2581 /* push csums to sbio */
2582 have_csum
= scrub_find_csum(sctx
, logical
, l
, csum
);
2586 ret
= scrub_pages_for_parity(sparity
, logical
, l
, physical
, dev
,
2587 flags
, gen
, mirror_num
,
2588 have_csum
? csum
: NULL
);
2600 * Given a physical address, this will calculate it's
2601 * logical offset. if this is a parity stripe, it will return
2602 * the most left data stripe's logical offset.
2604 * return 0 if it is a data stripe, 1 means parity stripe.
2606 static int get_raid56_logic_offset(u64 physical
, int num
,
2607 struct map_lookup
*map
, u64
*offset
,
2617 last_offset
= (physical
- map
->stripes
[num
].physical
) *
2618 nr_data_stripes(map
);
2620 *stripe_start
= last_offset
;
2622 *offset
= last_offset
;
2623 for (i
= 0; i
< nr_data_stripes(map
); i
++) {
2624 *offset
= last_offset
+ i
* map
->stripe_len
;
2626 stripe_nr
= div_u64(*offset
, map
->stripe_len
);
2627 stripe_nr
= div_u64(stripe_nr
, nr_data_stripes(map
));
2629 /* Work out the disk rotation on this stripe-set */
2630 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
, &rot
);
2631 /* calculate which stripe this data locates */
2633 stripe_index
= rot
% map
->num_stripes
;
2634 if (stripe_index
== num
)
2636 if (stripe_index
< num
)
2639 *offset
= last_offset
+ j
* map
->stripe_len
;
2643 static void scrub_free_parity(struct scrub_parity
*sparity
)
2645 struct scrub_ctx
*sctx
= sparity
->sctx
;
2646 struct scrub_page
*curr
, *next
;
2649 nbits
= bitmap_weight(sparity
->ebitmap
, sparity
->nsectors
);
2651 spin_lock(&sctx
->stat_lock
);
2652 sctx
->stat
.read_errors
+= nbits
;
2653 sctx
->stat
.uncorrectable_errors
+= nbits
;
2654 spin_unlock(&sctx
->stat_lock
);
2657 list_for_each_entry_safe(curr
, next
, &sparity
->spages
, list
) {
2658 list_del_init(&curr
->list
);
2659 scrub_page_put(curr
);
2665 static void scrub_parity_bio_endio(struct bio
*bio
, int error
)
2667 struct scrub_parity
*sparity
= (struct scrub_parity
*)bio
->bi_private
;
2668 struct scrub_ctx
*sctx
= sparity
->sctx
;
2671 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
2674 scrub_free_parity(sparity
);
2675 scrub_pending_bio_dec(sctx
);
2679 static void scrub_parity_check_and_repair(struct scrub_parity
*sparity
)
2681 struct scrub_ctx
*sctx
= sparity
->sctx
;
2683 struct btrfs_raid_bio
*rbio
;
2684 struct scrub_page
*spage
;
2685 struct btrfs_bio
*bbio
= NULL
;
2689 if (!bitmap_andnot(sparity
->dbitmap
, sparity
->dbitmap
, sparity
->ebitmap
,
2693 length
= sparity
->logic_end
- sparity
->logic_start
+ 1;
2694 ret
= btrfs_map_sblock(sctx
->dev_root
->fs_info
, WRITE
,
2695 sparity
->logic_start
,
2696 &length
, &bbio
, 0, 1);
2697 if (ret
|| !bbio
|| !bbio
->raid_map
)
2700 bio
= btrfs_io_bio_alloc(GFP_NOFS
, 0);
2704 bio
->bi_iter
.bi_sector
= sparity
->logic_start
>> 9;
2705 bio
->bi_private
= sparity
;
2706 bio
->bi_end_io
= scrub_parity_bio_endio
;
2708 rbio
= raid56_parity_alloc_scrub_rbio(sctx
->dev_root
, bio
, bbio
,
2709 length
, sparity
->scrub_dev
,
2715 list_for_each_entry(spage
, &sparity
->spages
, list
)
2716 raid56_parity_add_scrub_pages(rbio
, spage
->page
,
2719 scrub_pending_bio_inc(sctx
);
2720 raid56_parity_submit_scrub_rbio(rbio
);
2726 btrfs_put_bbio(bbio
);
2727 bitmap_or(sparity
->ebitmap
, sparity
->ebitmap
, sparity
->dbitmap
,
2729 spin_lock(&sctx
->stat_lock
);
2730 sctx
->stat
.malloc_errors
++;
2731 spin_unlock(&sctx
->stat_lock
);
2733 scrub_free_parity(sparity
);
2736 static inline int scrub_calc_parity_bitmap_len(int nsectors
)
2738 return DIV_ROUND_UP(nsectors
, BITS_PER_LONG
) * (BITS_PER_LONG
/ 8);
2741 static void scrub_parity_get(struct scrub_parity
*sparity
)
2743 atomic_inc(&sparity
->refs
);
2746 static void scrub_parity_put(struct scrub_parity
*sparity
)
2748 if (!atomic_dec_and_test(&sparity
->refs
))
2751 scrub_parity_check_and_repair(sparity
);
2754 static noinline_for_stack
int scrub_raid56_parity(struct scrub_ctx
*sctx
,
2755 struct map_lookup
*map
,
2756 struct btrfs_device
*sdev
,
2757 struct btrfs_path
*path
,
2761 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
2762 struct btrfs_root
*root
= fs_info
->extent_root
;
2763 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
2764 struct btrfs_extent_item
*extent
;
2768 struct extent_buffer
*l
;
2769 struct btrfs_key key
;
2772 u64 extent_physical
;
2774 struct btrfs_device
*extent_dev
;
2775 struct scrub_parity
*sparity
;
2778 int extent_mirror_num
;
2781 nsectors
= map
->stripe_len
/ root
->sectorsize
;
2782 bitmap_len
= scrub_calc_parity_bitmap_len(nsectors
);
2783 sparity
= kzalloc(sizeof(struct scrub_parity
) + 2 * bitmap_len
,
2786 spin_lock(&sctx
->stat_lock
);
2787 sctx
->stat
.malloc_errors
++;
2788 spin_unlock(&sctx
->stat_lock
);
2792 sparity
->stripe_len
= map
->stripe_len
;
2793 sparity
->nsectors
= nsectors
;
2794 sparity
->sctx
= sctx
;
2795 sparity
->scrub_dev
= sdev
;
2796 sparity
->logic_start
= logic_start
;
2797 sparity
->logic_end
= logic_end
;
2798 atomic_set(&sparity
->refs
, 1);
2799 INIT_LIST_HEAD(&sparity
->spages
);
2800 sparity
->dbitmap
= sparity
->bitmap
;
2801 sparity
->ebitmap
= (void *)sparity
->bitmap
+ bitmap_len
;
2804 while (logic_start
< logic_end
) {
2805 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
2806 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2808 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2809 key
.objectid
= logic_start
;
2810 key
.offset
= (u64
)-1;
2812 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2817 ret
= btrfs_previous_extent_item(root
, path
, 0);
2821 btrfs_release_path(path
);
2822 ret
= btrfs_search_slot(NULL
, root
, &key
,
2834 slot
= path
->slots
[0];
2835 if (slot
>= btrfs_header_nritems(l
)) {
2836 ret
= btrfs_next_leaf(root
, path
);
2845 btrfs_item_key_to_cpu(l
, &key
, slot
);
2847 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
2848 bytes
= root
->nodesize
;
2852 if (key
.objectid
+ bytes
<= logic_start
)
2855 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
2856 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
2859 if (key
.objectid
> logic_end
) {
2864 while (key
.objectid
>= logic_start
+ map
->stripe_len
)
2865 logic_start
+= map
->stripe_len
;
2867 extent
= btrfs_item_ptr(l
, slot
,
2868 struct btrfs_extent_item
);
2869 flags
= btrfs_extent_flags(l
, extent
);
2870 generation
= btrfs_extent_generation(l
, extent
);
2872 if (key
.objectid
< logic_start
&&
2873 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
2875 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2876 key
.objectid
, logic_start
);
2880 extent_logical
= key
.objectid
;
2883 if (extent_logical
< logic_start
) {
2884 extent_len
-= logic_start
- extent_logical
;
2885 extent_logical
= logic_start
;
2888 if (extent_logical
+ extent_len
>
2889 logic_start
+ map
->stripe_len
)
2890 extent_len
= logic_start
+ map
->stripe_len
-
2893 scrub_parity_mark_sectors_data(sparity
, extent_logical
,
2896 scrub_remap_extent(fs_info
, extent_logical
,
2897 extent_len
, &extent_physical
,
2899 &extent_mirror_num
);
2901 ret
= btrfs_lookup_csums_range(csum_root
,
2903 extent_logical
+ extent_len
- 1,
2904 &sctx
->csum_list
, 1);
2908 ret
= scrub_extent_for_parity(sparity
, extent_logical
,
2917 scrub_free_csums(sctx
);
2918 if (extent_logical
+ extent_len
<
2919 key
.objectid
+ bytes
) {
2920 logic_start
+= map
->stripe_len
;
2922 if (logic_start
>= logic_end
) {
2927 if (logic_start
< key
.objectid
+ bytes
) {
2936 btrfs_release_path(path
);
2941 logic_start
+= map
->stripe_len
;
2945 scrub_parity_mark_sectors_error(sparity
, logic_start
,
2946 logic_end
- logic_start
+ 1);
2947 scrub_parity_put(sparity
);
2949 mutex_lock(&sctx
->wr_ctx
.wr_lock
);
2950 scrub_wr_submit(sctx
);
2951 mutex_unlock(&sctx
->wr_ctx
.wr_lock
);
2953 btrfs_release_path(path
);
2954 return ret
< 0 ? ret
: 0;
2957 static noinline_for_stack
int scrub_stripe(struct scrub_ctx
*sctx
,
2958 struct map_lookup
*map
,
2959 struct btrfs_device
*scrub_dev
,
2960 int num
, u64 base
, u64 length
,
2963 struct btrfs_path
*path
, *ppath
;
2964 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
2965 struct btrfs_root
*root
= fs_info
->extent_root
;
2966 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
2967 struct btrfs_extent_item
*extent
;
2968 struct blk_plug plug
;
2973 struct extent_buffer
*l
;
2974 struct btrfs_key key
;
2981 struct reada_control
*reada1
;
2982 struct reada_control
*reada2
;
2983 struct btrfs_key key_start
;
2984 struct btrfs_key key_end
;
2985 u64 increment
= map
->stripe_len
;
2988 u64 extent_physical
;
2992 struct btrfs_device
*extent_dev
;
2993 int extent_mirror_num
;
2996 physical
= map
->stripes
[num
].physical
;
2998 nstripes
= div_u64(length
, map
->stripe_len
);
2999 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3000 offset
= map
->stripe_len
* num
;
3001 increment
= map
->stripe_len
* map
->num_stripes
;
3003 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3004 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3005 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
3006 increment
= map
->stripe_len
* factor
;
3007 mirror_num
= num
% map
->sub_stripes
+ 1;
3008 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3009 increment
= map
->stripe_len
;
3010 mirror_num
= num
% map
->num_stripes
+ 1;
3011 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3012 increment
= map
->stripe_len
;
3013 mirror_num
= num
% map
->num_stripes
+ 1;
3014 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3015 get_raid56_logic_offset(physical
, num
, map
, &offset
, NULL
);
3016 increment
= map
->stripe_len
* nr_data_stripes(map
);
3019 increment
= map
->stripe_len
;
3023 path
= btrfs_alloc_path();
3027 ppath
= btrfs_alloc_path();
3029 btrfs_free_path(path
);
3034 * work on commit root. The related disk blocks are static as
3035 * long as COW is applied. This means, it is save to rewrite
3036 * them to repair disk errors without any race conditions
3038 path
->search_commit_root
= 1;
3039 path
->skip_locking
= 1;
3041 ppath
->search_commit_root
= 1;
3042 ppath
->skip_locking
= 1;
3044 * trigger the readahead for extent tree csum tree and wait for
3045 * completion. During readahead, the scrub is officially paused
3046 * to not hold off transaction commits
3048 logical
= base
+ offset
;
3049 physical_end
= physical
+ nstripes
* map
->stripe_len
;
3050 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3051 get_raid56_logic_offset(physical_end
, num
,
3052 map
, &logic_end
, NULL
);
3055 logic_end
= logical
+ increment
* nstripes
;
3057 wait_event(sctx
->list_wait
,
3058 atomic_read(&sctx
->bios_in_flight
) == 0);
3059 scrub_blocked_if_needed(fs_info
);
3061 /* FIXME it might be better to start readahead at commit root */
3062 key_start
.objectid
= logical
;
3063 key_start
.type
= BTRFS_EXTENT_ITEM_KEY
;
3064 key_start
.offset
= (u64
)0;
3065 key_end
.objectid
= logic_end
;
3066 key_end
.type
= BTRFS_METADATA_ITEM_KEY
;
3067 key_end
.offset
= (u64
)-1;
3068 reada1
= btrfs_reada_add(root
, &key_start
, &key_end
);
3070 key_start
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3071 key_start
.type
= BTRFS_EXTENT_CSUM_KEY
;
3072 key_start
.offset
= logical
;
3073 key_end
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
3074 key_end
.type
= BTRFS_EXTENT_CSUM_KEY
;
3075 key_end
.offset
= logic_end
;
3076 reada2
= btrfs_reada_add(csum_root
, &key_start
, &key_end
);
3078 if (!IS_ERR(reada1
))
3079 btrfs_reada_wait(reada1
);
3080 if (!IS_ERR(reada2
))
3081 btrfs_reada_wait(reada2
);
3085 * collect all data csums for the stripe to avoid seeking during
3086 * the scrub. This might currently (crc32) end up to be about 1MB
3088 blk_start_plug(&plug
);
3091 * now find all extents for each stripe and scrub them
3094 while (physical
< physical_end
) {
3095 /* for raid56, we skip parity stripe */
3096 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3097 ret
= get_raid56_logic_offset(physical
, num
,
3098 map
, &logical
, &stripe_logical
);
3101 stripe_logical
+= base
;
3102 stripe_end
= stripe_logical
+ increment
- 1;
3103 ret
= scrub_raid56_parity(sctx
, map
, scrub_dev
,
3104 ppath
, stripe_logical
,
3114 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
3115 atomic_read(&sctx
->cancel_req
)) {
3120 * check to see if we have to pause
3122 if (atomic_read(&fs_info
->scrub_pause_req
)) {
3123 /* push queued extents */
3124 atomic_set(&sctx
->wr_ctx
.flush_all_writes
, 1);
3126 mutex_lock(&sctx
->wr_ctx
.wr_lock
);
3127 scrub_wr_submit(sctx
);
3128 mutex_unlock(&sctx
->wr_ctx
.wr_lock
);
3129 wait_event(sctx
->list_wait
,
3130 atomic_read(&sctx
->bios_in_flight
) == 0);
3131 atomic_set(&sctx
->wr_ctx
.flush_all_writes
, 0);
3132 scrub_blocked_if_needed(fs_info
);
3135 if (btrfs_fs_incompat(fs_info
, SKINNY_METADATA
))
3136 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3138 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3139 key
.objectid
= logical
;
3140 key
.offset
= (u64
)-1;
3142 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3147 ret
= btrfs_previous_extent_item(root
, path
, 0);
3151 /* there's no smaller item, so stick with the
3153 btrfs_release_path(path
);
3154 ret
= btrfs_search_slot(NULL
, root
, &key
,
3166 slot
= path
->slots
[0];
3167 if (slot
>= btrfs_header_nritems(l
)) {
3168 ret
= btrfs_next_leaf(root
, path
);
3177 btrfs_item_key_to_cpu(l
, &key
, slot
);
3179 if (key
.type
== BTRFS_METADATA_ITEM_KEY
)
3180 bytes
= root
->nodesize
;
3184 if (key
.objectid
+ bytes
<= logical
)
3187 if (key
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
3188 key
.type
!= BTRFS_METADATA_ITEM_KEY
)
3191 if (key
.objectid
>= logical
+ map
->stripe_len
) {
3192 /* out of this device extent */
3193 if (key
.objectid
>= logic_end
)
3198 extent
= btrfs_item_ptr(l
, slot
,
3199 struct btrfs_extent_item
);
3200 flags
= btrfs_extent_flags(l
, extent
);
3201 generation
= btrfs_extent_generation(l
, extent
);
3203 if (key
.objectid
< logical
&&
3204 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
3206 "scrub: tree block %llu spanning "
3207 "stripes, ignored. logical=%llu",
3208 key
.objectid
, logical
);
3213 extent_logical
= key
.objectid
;
3217 * trim extent to this stripe
3219 if (extent_logical
< logical
) {
3220 extent_len
-= logical
- extent_logical
;
3221 extent_logical
= logical
;
3223 if (extent_logical
+ extent_len
>
3224 logical
+ map
->stripe_len
) {
3225 extent_len
= logical
+ map
->stripe_len
-
3229 extent_physical
= extent_logical
- logical
+ physical
;
3230 extent_dev
= scrub_dev
;
3231 extent_mirror_num
= mirror_num
;
3233 scrub_remap_extent(fs_info
, extent_logical
,
3234 extent_len
, &extent_physical
,
3236 &extent_mirror_num
);
3238 ret
= btrfs_lookup_csums_range(csum_root
, logical
,
3239 logical
+ map
->stripe_len
- 1,
3240 &sctx
->csum_list
, 1);
3244 ret
= scrub_extent(sctx
, extent_logical
, extent_len
,
3245 extent_physical
, extent_dev
, flags
,
3246 generation
, extent_mirror_num
,
3247 extent_logical
- logical
+ physical
);
3251 scrub_free_csums(sctx
);
3252 if (extent_logical
+ extent_len
<
3253 key
.objectid
+ bytes
) {
3254 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
3256 * loop until we find next data stripe
3257 * or we have finished all stripes.
3260 physical
+= map
->stripe_len
;
3261 ret
= get_raid56_logic_offset(physical
,
3266 if (ret
&& physical
< physical_end
) {
3267 stripe_logical
+= base
;
3268 stripe_end
= stripe_logical
+
3270 ret
= scrub_raid56_parity(sctx
,
3271 map
, scrub_dev
, ppath
,
3279 physical
+= map
->stripe_len
;
3280 logical
+= increment
;
3282 if (logical
< key
.objectid
+ bytes
) {
3287 if (physical
>= physical_end
) {
3295 btrfs_release_path(path
);
3297 logical
+= increment
;
3298 physical
+= map
->stripe_len
;
3299 spin_lock(&sctx
->stat_lock
);
3301 sctx
->stat
.last_physical
= map
->stripes
[num
].physical
+
3304 sctx
->stat
.last_physical
= physical
;
3305 spin_unlock(&sctx
->stat_lock
);
3310 /* push queued extents */
3312 mutex_lock(&sctx
->wr_ctx
.wr_lock
);
3313 scrub_wr_submit(sctx
);
3314 mutex_unlock(&sctx
->wr_ctx
.wr_lock
);
3316 blk_finish_plug(&plug
);
3317 btrfs_free_path(path
);
3318 btrfs_free_path(ppath
);
3319 return ret
< 0 ? ret
: 0;
3322 static noinline_for_stack
int scrub_chunk(struct scrub_ctx
*sctx
,
3323 struct btrfs_device
*scrub_dev
,
3324 u64 chunk_tree
, u64 chunk_objectid
,
3325 u64 chunk_offset
, u64 length
,
3326 u64 dev_offset
, int is_dev_replace
)
3328 struct btrfs_mapping_tree
*map_tree
=
3329 &sctx
->dev_root
->fs_info
->mapping_tree
;
3330 struct map_lookup
*map
;
3331 struct extent_map
*em
;
3335 read_lock(&map_tree
->map_tree
.lock
);
3336 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3337 read_unlock(&map_tree
->map_tree
.lock
);
3342 map
= (struct map_lookup
*)em
->bdev
;
3343 if (em
->start
!= chunk_offset
)
3346 if (em
->len
< length
)
3349 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3350 if (map
->stripes
[i
].dev
->bdev
== scrub_dev
->bdev
&&
3351 map
->stripes
[i
].physical
== dev_offset
) {
3352 ret
= scrub_stripe(sctx
, map
, scrub_dev
, i
,
3353 chunk_offset
, length
,
3360 free_extent_map(em
);
3365 static noinline_for_stack
3366 int scrub_enumerate_chunks(struct scrub_ctx
*sctx
,
3367 struct btrfs_device
*scrub_dev
, u64 start
, u64 end
,
3370 struct btrfs_dev_extent
*dev_extent
= NULL
;
3371 struct btrfs_path
*path
;
3372 struct btrfs_root
*root
= sctx
->dev_root
;
3373 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3380 struct extent_buffer
*l
;
3381 struct btrfs_key key
;
3382 struct btrfs_key found_key
;
3383 struct btrfs_block_group_cache
*cache
;
3384 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
3386 path
= btrfs_alloc_path();
3391 path
->search_commit_root
= 1;
3392 path
->skip_locking
= 1;
3394 key
.objectid
= scrub_dev
->devid
;
3396 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3399 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3403 if (path
->slots
[0] >=
3404 btrfs_header_nritems(path
->nodes
[0])) {
3405 ret
= btrfs_next_leaf(root
, path
);
3412 slot
= path
->slots
[0];
3414 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
3416 if (found_key
.objectid
!= scrub_dev
->devid
)
3419 if (found_key
.type
!= BTRFS_DEV_EXTENT_KEY
)
3422 if (found_key
.offset
>= end
)
3425 if (found_key
.offset
< key
.offset
)
3428 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3429 length
= btrfs_dev_extent_length(l
, dev_extent
);
3431 if (found_key
.offset
+ length
<= start
)
3434 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3435 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3436 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3439 * get a reference on the corresponding block group to prevent
3440 * the chunk from going away while we scrub it
3442 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3444 /* some chunks are removed but not committed to disk yet,
3445 * continue scrubbing */
3449 dev_replace
->cursor_right
= found_key
.offset
+ length
;
3450 dev_replace
->cursor_left
= found_key
.offset
;
3451 dev_replace
->item_needs_writeback
= 1;
3452 ret
= scrub_chunk(sctx
, scrub_dev
, chunk_tree
, chunk_objectid
,
3453 chunk_offset
, length
, found_key
.offset
,
3457 * flush, submit all pending read and write bios, afterwards
3459 * Note that in the dev replace case, a read request causes
3460 * write requests that are submitted in the read completion
3461 * worker. Therefore in the current situation, it is required
3462 * that all write requests are flushed, so that all read and
3463 * write requests are really completed when bios_in_flight
3466 atomic_set(&sctx
->wr_ctx
.flush_all_writes
, 1);
3468 mutex_lock(&sctx
->wr_ctx
.wr_lock
);
3469 scrub_wr_submit(sctx
);
3470 mutex_unlock(&sctx
->wr_ctx
.wr_lock
);
3472 wait_event(sctx
->list_wait
,
3473 atomic_read(&sctx
->bios_in_flight
) == 0);
3474 atomic_inc(&fs_info
->scrubs_paused
);
3475 wake_up(&fs_info
->scrub_pause_wait
);
3478 * must be called before we decrease @scrub_paused.
3479 * make sure we don't block transaction commit while
3480 * we are waiting pending workers finished.
3482 wait_event(sctx
->list_wait
,
3483 atomic_read(&sctx
->workers_pending
) == 0);
3484 atomic_set(&sctx
->wr_ctx
.flush_all_writes
, 0);
3486 mutex_lock(&fs_info
->scrub_lock
);
3487 __scrub_blocked_if_needed(fs_info
);
3488 atomic_dec(&fs_info
->scrubs_paused
);
3489 mutex_unlock(&fs_info
->scrub_lock
);
3490 wake_up(&fs_info
->scrub_pause_wait
);
3492 btrfs_put_block_group(cache
);
3495 if (is_dev_replace
&&
3496 atomic64_read(&dev_replace
->num_write_errors
) > 0) {
3500 if (sctx
->stat
.malloc_errors
> 0) {
3505 dev_replace
->cursor_left
= dev_replace
->cursor_right
;
3506 dev_replace
->item_needs_writeback
= 1;
3508 key
.offset
= found_key
.offset
+ length
;
3509 btrfs_release_path(path
);
3512 btrfs_free_path(path
);
3515 * ret can still be 1 from search_slot or next_leaf,
3516 * that's not an error
3518 return ret
< 0 ? ret
: 0;
3521 static noinline_for_stack
int scrub_supers(struct scrub_ctx
*sctx
,
3522 struct btrfs_device
*scrub_dev
)
3528 struct btrfs_root
*root
= sctx
->dev_root
;
3530 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
3533 /* Seed devices of a new filesystem has their own generation. */
3534 if (scrub_dev
->fs_devices
!= root
->fs_info
->fs_devices
)
3535 gen
= scrub_dev
->generation
;
3537 gen
= root
->fs_info
->last_trans_committed
;
3539 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
3540 bytenr
= btrfs_sb_offset(i
);
3541 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>
3542 scrub_dev
->commit_total_bytes
)
3545 ret
= scrub_pages(sctx
, bytenr
, BTRFS_SUPER_INFO_SIZE
, bytenr
,
3546 scrub_dev
, BTRFS_EXTENT_FLAG_SUPER
, gen
, i
,
3551 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
3557 * get a reference count on fs_info->scrub_workers. start worker if necessary
3559 static noinline_for_stack
int scrub_workers_get(struct btrfs_fs_info
*fs_info
,
3563 unsigned int flags
= WQ_FREEZABLE
| WQ_UNBOUND
;
3564 int max_active
= fs_info
->thread_pool_size
;
3566 if (fs_info
->scrub_workers_refcnt
== 0) {
3568 fs_info
->scrub_workers
=
3569 btrfs_alloc_workqueue("btrfs-scrub", flags
,
3572 fs_info
->scrub_workers
=
3573 btrfs_alloc_workqueue("btrfs-scrub", flags
,
3575 if (!fs_info
->scrub_workers
) {
3579 fs_info
->scrub_wr_completion_workers
=
3580 btrfs_alloc_workqueue("btrfs-scrubwrc", flags
,
3582 if (!fs_info
->scrub_wr_completion_workers
) {
3586 fs_info
->scrub_nocow_workers
=
3587 btrfs_alloc_workqueue("btrfs-scrubnc", flags
, 1, 0);
3588 if (!fs_info
->scrub_nocow_workers
) {
3593 ++fs_info
->scrub_workers_refcnt
;
3598 static noinline_for_stack
void scrub_workers_put(struct btrfs_fs_info
*fs_info
)
3600 if (--fs_info
->scrub_workers_refcnt
== 0) {
3601 btrfs_destroy_workqueue(fs_info
->scrub_workers
);
3602 btrfs_destroy_workqueue(fs_info
->scrub_wr_completion_workers
);
3603 btrfs_destroy_workqueue(fs_info
->scrub_nocow_workers
);
3605 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
3608 int btrfs_scrub_dev(struct btrfs_fs_info
*fs_info
, u64 devid
, u64 start
,
3609 u64 end
, struct btrfs_scrub_progress
*progress
,
3610 int readonly
, int is_dev_replace
)
3612 struct scrub_ctx
*sctx
;
3614 struct btrfs_device
*dev
;
3615 struct rcu_string
*name
;
3617 if (btrfs_fs_closing(fs_info
))
3620 if (fs_info
->chunk_root
->nodesize
> BTRFS_STRIPE_LEN
) {
3622 * in this case scrub is unable to calculate the checksum
3623 * the way scrub is implemented. Do not handle this
3624 * situation at all because it won't ever happen.
3627 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
3628 fs_info
->chunk_root
->nodesize
, BTRFS_STRIPE_LEN
);
3632 if (fs_info
->chunk_root
->sectorsize
!= PAGE_SIZE
) {
3633 /* not supported for data w/o checksums */
3635 "scrub: size assumption sectorsize != PAGE_SIZE "
3636 "(%d != %lu) fails",
3637 fs_info
->chunk_root
->sectorsize
, PAGE_SIZE
);
3641 if (fs_info
->chunk_root
->nodesize
>
3642 PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
||
3643 fs_info
->chunk_root
->sectorsize
>
3644 PAGE_SIZE
* SCRUB_MAX_PAGES_PER_BLOCK
) {
3646 * would exhaust the array bounds of pagev member in
3647 * struct scrub_block
3649 btrfs_err(fs_info
, "scrub: size assumption nodesize and sectorsize "
3650 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
3651 fs_info
->chunk_root
->nodesize
,
3652 SCRUB_MAX_PAGES_PER_BLOCK
,
3653 fs_info
->chunk_root
->sectorsize
,
3654 SCRUB_MAX_PAGES_PER_BLOCK
);
3659 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
3660 dev
= btrfs_find_device(fs_info
, devid
, NULL
, NULL
);
3661 if (!dev
|| (dev
->missing
&& !is_dev_replace
)) {
3662 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3666 if (!is_dev_replace
&& !readonly
&& !dev
->writeable
) {
3667 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3669 name
= rcu_dereference(dev
->name
);
3670 btrfs_err(fs_info
, "scrub: device %s is not writable",
3676 mutex_lock(&fs_info
->scrub_lock
);
3677 if (!dev
->in_fs_metadata
|| dev
->is_tgtdev_for_dev_replace
) {
3678 mutex_unlock(&fs_info
->scrub_lock
);
3679 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3683 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3684 if (dev
->scrub_device
||
3686 btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))) {
3687 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3688 mutex_unlock(&fs_info
->scrub_lock
);
3689 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3690 return -EINPROGRESS
;
3692 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3694 ret
= scrub_workers_get(fs_info
, is_dev_replace
);
3696 mutex_unlock(&fs_info
->scrub_lock
);
3697 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3701 sctx
= scrub_setup_ctx(dev
, is_dev_replace
);
3703 mutex_unlock(&fs_info
->scrub_lock
);
3704 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3705 scrub_workers_put(fs_info
);
3706 return PTR_ERR(sctx
);
3708 sctx
->readonly
= readonly
;
3709 dev
->scrub_device
= sctx
;
3710 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3713 * checking @scrub_pause_req here, we can avoid
3714 * race between committing transaction and scrubbing.
3716 __scrub_blocked_if_needed(fs_info
);
3717 atomic_inc(&fs_info
->scrubs_running
);
3718 mutex_unlock(&fs_info
->scrub_lock
);
3720 if (!is_dev_replace
) {
3722 * by holding device list mutex, we can
3723 * kick off writing super in log tree sync.
3725 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
3726 ret
= scrub_supers(sctx
, dev
);
3727 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
3731 ret
= scrub_enumerate_chunks(sctx
, dev
, start
, end
,
3734 wait_event(sctx
->list_wait
, atomic_read(&sctx
->bios_in_flight
) == 0);
3735 atomic_dec(&fs_info
->scrubs_running
);
3736 wake_up(&fs_info
->scrub_pause_wait
);
3738 wait_event(sctx
->list_wait
, atomic_read(&sctx
->workers_pending
) == 0);
3741 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
3743 mutex_lock(&fs_info
->scrub_lock
);
3744 dev
->scrub_device
= NULL
;
3745 scrub_workers_put(fs_info
);
3746 mutex_unlock(&fs_info
->scrub_lock
);
3748 scrub_put_ctx(sctx
);
3753 void btrfs_scrub_pause(struct btrfs_root
*root
)
3755 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3757 mutex_lock(&fs_info
->scrub_lock
);
3758 atomic_inc(&fs_info
->scrub_pause_req
);
3759 while (atomic_read(&fs_info
->scrubs_paused
) !=
3760 atomic_read(&fs_info
->scrubs_running
)) {
3761 mutex_unlock(&fs_info
->scrub_lock
);
3762 wait_event(fs_info
->scrub_pause_wait
,
3763 atomic_read(&fs_info
->scrubs_paused
) ==
3764 atomic_read(&fs_info
->scrubs_running
));
3765 mutex_lock(&fs_info
->scrub_lock
);
3767 mutex_unlock(&fs_info
->scrub_lock
);
3770 void btrfs_scrub_continue(struct btrfs_root
*root
)
3772 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3774 atomic_dec(&fs_info
->scrub_pause_req
);
3775 wake_up(&fs_info
->scrub_pause_wait
);
3778 int btrfs_scrub_cancel(struct btrfs_fs_info
*fs_info
)
3780 mutex_lock(&fs_info
->scrub_lock
);
3781 if (!atomic_read(&fs_info
->scrubs_running
)) {
3782 mutex_unlock(&fs_info
->scrub_lock
);
3786 atomic_inc(&fs_info
->scrub_cancel_req
);
3787 while (atomic_read(&fs_info
->scrubs_running
)) {
3788 mutex_unlock(&fs_info
->scrub_lock
);
3789 wait_event(fs_info
->scrub_pause_wait
,
3790 atomic_read(&fs_info
->scrubs_running
) == 0);
3791 mutex_lock(&fs_info
->scrub_lock
);
3793 atomic_dec(&fs_info
->scrub_cancel_req
);
3794 mutex_unlock(&fs_info
->scrub_lock
);
3799 int btrfs_scrub_cancel_dev(struct btrfs_fs_info
*fs_info
,
3800 struct btrfs_device
*dev
)
3802 struct scrub_ctx
*sctx
;
3804 mutex_lock(&fs_info
->scrub_lock
);
3805 sctx
= dev
->scrub_device
;
3807 mutex_unlock(&fs_info
->scrub_lock
);
3810 atomic_inc(&sctx
->cancel_req
);
3811 while (dev
->scrub_device
) {
3812 mutex_unlock(&fs_info
->scrub_lock
);
3813 wait_event(fs_info
->scrub_pause_wait
,
3814 dev
->scrub_device
== NULL
);
3815 mutex_lock(&fs_info
->scrub_lock
);
3817 mutex_unlock(&fs_info
->scrub_lock
);
3822 int btrfs_scrub_progress(struct btrfs_root
*root
, u64 devid
,
3823 struct btrfs_scrub_progress
*progress
)
3825 struct btrfs_device
*dev
;
3826 struct scrub_ctx
*sctx
= NULL
;
3828 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
3829 dev
= btrfs_find_device(root
->fs_info
, devid
, NULL
, NULL
);
3831 sctx
= dev
->scrub_device
;
3833 memcpy(progress
, &sctx
->stat
, sizeof(*progress
));
3834 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
3836 return dev
? (sctx
? 0 : -ENOTCONN
) : -ENODEV
;
3839 static void scrub_remap_extent(struct btrfs_fs_info
*fs_info
,
3840 u64 extent_logical
, u64 extent_len
,
3841 u64
*extent_physical
,
3842 struct btrfs_device
**extent_dev
,
3843 int *extent_mirror_num
)
3846 struct btrfs_bio
*bbio
= NULL
;
3849 mapped_length
= extent_len
;
3850 ret
= btrfs_map_block(fs_info
, READ
, extent_logical
,
3851 &mapped_length
, &bbio
, 0);
3852 if (ret
|| !bbio
|| mapped_length
< extent_len
||
3853 !bbio
->stripes
[0].dev
->bdev
) {
3854 btrfs_put_bbio(bbio
);
3858 *extent_physical
= bbio
->stripes
[0].physical
;
3859 *extent_mirror_num
= bbio
->mirror_num
;
3860 *extent_dev
= bbio
->stripes
[0].dev
;
3861 btrfs_put_bbio(bbio
);
3864 static int scrub_setup_wr_ctx(struct scrub_ctx
*sctx
,
3865 struct scrub_wr_ctx
*wr_ctx
,
3866 struct btrfs_fs_info
*fs_info
,
3867 struct btrfs_device
*dev
,
3870 WARN_ON(wr_ctx
->wr_curr_bio
!= NULL
);
3872 mutex_init(&wr_ctx
->wr_lock
);
3873 wr_ctx
->wr_curr_bio
= NULL
;
3874 if (!is_dev_replace
)
3877 WARN_ON(!dev
->bdev
);
3878 wr_ctx
->pages_per_wr_bio
= min_t(int, SCRUB_PAGES_PER_WR_BIO
,
3879 bio_get_nr_vecs(dev
->bdev
));
3880 wr_ctx
->tgtdev
= dev
;
3881 atomic_set(&wr_ctx
->flush_all_writes
, 0);
3885 static void scrub_free_wr_ctx(struct scrub_wr_ctx
*wr_ctx
)
3887 mutex_lock(&wr_ctx
->wr_lock
);
3888 kfree(wr_ctx
->wr_curr_bio
);
3889 wr_ctx
->wr_curr_bio
= NULL
;
3890 mutex_unlock(&wr_ctx
->wr_lock
);
3893 static int copy_nocow_pages(struct scrub_ctx
*sctx
, u64 logical
, u64 len
,
3894 int mirror_num
, u64 physical_for_dev_replace
)
3896 struct scrub_copy_nocow_ctx
*nocow_ctx
;
3897 struct btrfs_fs_info
*fs_info
= sctx
->dev_root
->fs_info
;
3899 nocow_ctx
= kzalloc(sizeof(*nocow_ctx
), GFP_NOFS
);
3901 spin_lock(&sctx
->stat_lock
);
3902 sctx
->stat
.malloc_errors
++;
3903 spin_unlock(&sctx
->stat_lock
);
3907 scrub_pending_trans_workers_inc(sctx
);
3909 nocow_ctx
->sctx
= sctx
;
3910 nocow_ctx
->logical
= logical
;
3911 nocow_ctx
->len
= len
;
3912 nocow_ctx
->mirror_num
= mirror_num
;
3913 nocow_ctx
->physical_for_dev_replace
= physical_for_dev_replace
;
3914 btrfs_init_work(&nocow_ctx
->work
, btrfs_scrubnc_helper
,
3915 copy_nocow_pages_worker
, NULL
, NULL
);
3916 INIT_LIST_HEAD(&nocow_ctx
->inodes
);
3917 btrfs_queue_work(fs_info
->scrub_nocow_workers
,
3923 static int record_inode_for_nocow(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3925 struct scrub_copy_nocow_ctx
*nocow_ctx
= ctx
;
3926 struct scrub_nocow_inode
*nocow_inode
;
3928 nocow_inode
= kzalloc(sizeof(*nocow_inode
), GFP_NOFS
);
3931 nocow_inode
->inum
= inum
;
3932 nocow_inode
->offset
= offset
;
3933 nocow_inode
->root
= root
;
3934 list_add_tail(&nocow_inode
->list
, &nocow_ctx
->inodes
);
3938 #define COPY_COMPLETE 1
3940 static void copy_nocow_pages_worker(struct btrfs_work
*work
)
3942 struct scrub_copy_nocow_ctx
*nocow_ctx
=
3943 container_of(work
, struct scrub_copy_nocow_ctx
, work
);
3944 struct scrub_ctx
*sctx
= nocow_ctx
->sctx
;
3945 u64 logical
= nocow_ctx
->logical
;
3946 u64 len
= nocow_ctx
->len
;
3947 int mirror_num
= nocow_ctx
->mirror_num
;
3948 u64 physical_for_dev_replace
= nocow_ctx
->physical_for_dev_replace
;
3950 struct btrfs_trans_handle
*trans
= NULL
;
3951 struct btrfs_fs_info
*fs_info
;
3952 struct btrfs_path
*path
;
3953 struct btrfs_root
*root
;
3954 int not_written
= 0;
3956 fs_info
= sctx
->dev_root
->fs_info
;
3957 root
= fs_info
->extent_root
;
3959 path
= btrfs_alloc_path();
3961 spin_lock(&sctx
->stat_lock
);
3962 sctx
->stat
.malloc_errors
++;
3963 spin_unlock(&sctx
->stat_lock
);
3968 trans
= btrfs_join_transaction(root
);
3969 if (IS_ERR(trans
)) {
3974 ret
= iterate_inodes_from_logical(logical
, fs_info
, path
,
3975 record_inode_for_nocow
, nocow_ctx
);
3976 if (ret
!= 0 && ret
!= -ENOENT
) {
3977 btrfs_warn(fs_info
, "iterate_inodes_from_logical() failed: log %llu, "
3978 "phys %llu, len %llu, mir %u, ret %d",
3979 logical
, physical_for_dev_replace
, len
, mirror_num
,
3985 btrfs_end_transaction(trans
, root
);
3987 while (!list_empty(&nocow_ctx
->inodes
)) {
3988 struct scrub_nocow_inode
*entry
;
3989 entry
= list_first_entry(&nocow_ctx
->inodes
,
3990 struct scrub_nocow_inode
,
3992 list_del_init(&entry
->list
);
3993 ret
= copy_nocow_pages_for_inode(entry
->inum
, entry
->offset
,
3994 entry
->root
, nocow_ctx
);
3996 if (ret
== COPY_COMPLETE
) {
4004 while (!list_empty(&nocow_ctx
->inodes
)) {
4005 struct scrub_nocow_inode
*entry
;
4006 entry
= list_first_entry(&nocow_ctx
->inodes
,
4007 struct scrub_nocow_inode
,
4009 list_del_init(&entry
->list
);
4012 if (trans
&& !IS_ERR(trans
))
4013 btrfs_end_transaction(trans
, root
);
4015 btrfs_dev_replace_stats_inc(&fs_info
->dev_replace
.
4016 num_uncorrectable_read_errors
);
4018 btrfs_free_path(path
);
4021 scrub_pending_trans_workers_dec(sctx
);
4024 static int check_extent_to_block(struct inode
*inode
, u64 start
, u64 len
,
4027 struct extent_state
*cached_state
= NULL
;
4028 struct btrfs_ordered_extent
*ordered
;
4029 struct extent_io_tree
*io_tree
;
4030 struct extent_map
*em
;
4031 u64 lockstart
= start
, lockend
= start
+ len
- 1;
4034 io_tree
= &BTRFS_I(inode
)->io_tree
;
4036 lock_extent_bits(io_tree
, lockstart
, lockend
, 0, &cached_state
);
4037 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
, len
);
4039 btrfs_put_ordered_extent(ordered
);
4044 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
4051 * This extent does not actually cover the logical extent anymore,
4052 * move on to the next inode.
4054 if (em
->block_start
> logical
||
4055 em
->block_start
+ em
->block_len
< logical
+ len
) {
4056 free_extent_map(em
);
4060 free_extent_map(em
);
4063 unlock_extent_cached(io_tree
, lockstart
, lockend
, &cached_state
,
4068 static int copy_nocow_pages_for_inode(u64 inum
, u64 offset
, u64 root
,
4069 struct scrub_copy_nocow_ctx
*nocow_ctx
)
4071 struct btrfs_fs_info
*fs_info
= nocow_ctx
->sctx
->dev_root
->fs_info
;
4072 struct btrfs_key key
;
4073 struct inode
*inode
;
4075 struct btrfs_root
*local_root
;
4076 struct extent_io_tree
*io_tree
;
4077 u64 physical_for_dev_replace
;
4078 u64 nocow_ctx_logical
;
4079 u64 len
= nocow_ctx
->len
;
4080 unsigned long index
;
4085 key
.objectid
= root
;
4086 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4087 key
.offset
= (u64
)-1;
4089 srcu_index
= srcu_read_lock(&fs_info
->subvol_srcu
);
4091 local_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4092 if (IS_ERR(local_root
)) {
4093 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
4094 return PTR_ERR(local_root
);
4097 key
.type
= BTRFS_INODE_ITEM_KEY
;
4098 key
.objectid
= inum
;
4100 inode
= btrfs_iget(fs_info
->sb
, &key
, local_root
, NULL
);
4101 srcu_read_unlock(&fs_info
->subvol_srcu
, srcu_index
);
4103 return PTR_ERR(inode
);
4105 /* Avoid truncate/dio/punch hole.. */
4106 mutex_lock(&inode
->i_mutex
);
4107 inode_dio_wait(inode
);
4109 physical_for_dev_replace
= nocow_ctx
->physical_for_dev_replace
;
4110 io_tree
= &BTRFS_I(inode
)->io_tree
;
4111 nocow_ctx_logical
= nocow_ctx
->logical
;
4113 ret
= check_extent_to_block(inode
, offset
, len
, nocow_ctx_logical
);
4115 ret
= ret
> 0 ? 0 : ret
;
4119 while (len
>= PAGE_CACHE_SIZE
) {
4120 index
= offset
>> PAGE_CACHE_SHIFT
;
4122 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
4124 btrfs_err(fs_info
, "find_or_create_page() failed");
4129 if (PageUptodate(page
)) {
4130 if (PageDirty(page
))
4133 ClearPageError(page
);
4134 err
= extent_read_full_page(io_tree
, page
,
4136 nocow_ctx
->mirror_num
);
4144 * If the page has been remove from the page cache,
4145 * the data on it is meaningless, because it may be
4146 * old one, the new data may be written into the new
4147 * page in the page cache.
4149 if (page
->mapping
!= inode
->i_mapping
) {
4151 page_cache_release(page
);
4154 if (!PageUptodate(page
)) {
4160 ret
= check_extent_to_block(inode
, offset
, len
,
4163 ret
= ret
> 0 ? 0 : ret
;
4167 err
= write_page_nocow(nocow_ctx
->sctx
,
4168 physical_for_dev_replace
, page
);
4173 page_cache_release(page
);
4178 offset
+= PAGE_CACHE_SIZE
;
4179 physical_for_dev_replace
+= PAGE_CACHE_SIZE
;
4180 nocow_ctx_logical
+= PAGE_CACHE_SIZE
;
4181 len
-= PAGE_CACHE_SIZE
;
4183 ret
= COPY_COMPLETE
;
4185 mutex_unlock(&inode
->i_mutex
);
4190 static int write_page_nocow(struct scrub_ctx
*sctx
,
4191 u64 physical_for_dev_replace
, struct page
*page
)
4194 struct btrfs_device
*dev
;
4197 dev
= sctx
->wr_ctx
.tgtdev
;
4201 printk_ratelimited(KERN_WARNING
4202 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
4205 bio
= btrfs_io_bio_alloc(GFP_NOFS
, 1);
4207 spin_lock(&sctx
->stat_lock
);
4208 sctx
->stat
.malloc_errors
++;
4209 spin_unlock(&sctx
->stat_lock
);
4212 bio
->bi_iter
.bi_size
= 0;
4213 bio
->bi_iter
.bi_sector
= physical_for_dev_replace
>> 9;
4214 bio
->bi_bdev
= dev
->bdev
;
4215 ret
= bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0);
4216 if (ret
!= PAGE_CACHE_SIZE
) {
4219 btrfs_dev_stat_inc_and_print(dev
, BTRFS_DEV_STAT_WRITE_ERRS
);
4223 if (btrfsic_submit_bio_wait(WRITE_SYNC
, bio
))
4224 goto leave_with_eio
;