2 * Copyright (C) 2011 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 "check-integrity.h"
29 #include "rcu-string.h"
32 * This is only the first step towards a full-features scrub. It reads all
33 * extent and super block and verifies the checksums. In case a bad checksum
34 * is found or the extent cannot be read, good data will be written back if
37 * Future enhancements:
38 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
40 * - track and record media errors, throw out bad devices
41 * - add a mode to also read unallocated space
47 #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
48 #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
49 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
52 struct scrub_block
*sblock
;
54 struct btrfs_device
*dev
;
55 u64 flags
; /* extent flags */
60 unsigned int mirror_num
:8;
61 unsigned int have_csum
:1;
62 unsigned int io_error
:1;
64 u8 csum
[BTRFS_CSUM_SIZE
];
69 struct scrub_dev
*sdev
;
74 struct scrub_page
*pagev
[SCRUB_PAGES_PER_BIO
];
77 struct btrfs_work work
;
81 struct scrub_page pagev
[SCRUB_MAX_PAGES_PER_BLOCK
];
83 atomic_t outstanding_pages
;
84 atomic_t ref_count
; /* free mem on transition to zero */
85 struct scrub_dev
*sdev
;
87 unsigned int header_error
:1;
88 unsigned int checksum_error
:1;
89 unsigned int no_io_error_seen
:1;
90 unsigned int generation_error
:1; /* also sets header_error */
95 struct scrub_bio
*bios
[SCRUB_BIOS_PER_DEV
];
96 struct btrfs_device
*dev
;
101 spinlock_t list_lock
;
102 wait_queue_head_t list_wait
;
104 struct list_head csum_list
;
107 int pages_per_bio
; /* <= SCRUB_PAGES_PER_BIO */
114 struct btrfs_scrub_progress stat
;
115 spinlock_t stat_lock
;
118 struct scrub_fixup_nodatasum
{
119 struct scrub_dev
*sdev
;
121 struct btrfs_root
*root
;
122 struct btrfs_work work
;
126 struct scrub_warning
{
127 struct btrfs_path
*path
;
128 u64 extent_item_size
;
134 struct btrfs_device
*dev
;
140 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
);
141 static int scrub_setup_recheck_block(struct scrub_dev
*sdev
,
142 struct btrfs_mapping_tree
*map_tree
,
143 u64 length
, u64 logical
,
144 struct scrub_block
*sblock
);
145 static int scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
146 struct scrub_block
*sblock
, int is_metadata
,
147 int have_csum
, u8
*csum
, u64 generation
,
149 static void scrub_recheck_block_checksum(struct btrfs_fs_info
*fs_info
,
150 struct scrub_block
*sblock
,
151 int is_metadata
, int have_csum
,
152 const u8
*csum
, u64 generation
,
154 static void scrub_complete_bio_end_io(struct bio
*bio
, int err
);
155 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
156 struct scrub_block
*sblock_good
,
158 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
159 struct scrub_block
*sblock_good
,
160 int page_num
, int force_write
);
161 static int scrub_checksum_data(struct scrub_block
*sblock
);
162 static int scrub_checksum_tree_block(struct scrub_block
*sblock
);
163 static int scrub_checksum_super(struct scrub_block
*sblock
);
164 static void scrub_block_get(struct scrub_block
*sblock
);
165 static void scrub_block_put(struct scrub_block
*sblock
);
166 static int scrub_add_page_to_bio(struct scrub_dev
*sdev
,
167 struct scrub_page
*spage
);
168 static int scrub_pages(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
169 u64 physical
, u64 flags
, u64 gen
, int mirror_num
,
170 u8
*csum
, int force
);
171 static void scrub_bio_end_io(struct bio
*bio
, int err
);
172 static void scrub_bio_end_io_worker(struct btrfs_work
*work
);
173 static void scrub_block_complete(struct scrub_block
*sblock
);
176 static void scrub_free_csums(struct scrub_dev
*sdev
)
178 while (!list_empty(&sdev
->csum_list
)) {
179 struct btrfs_ordered_sum
*sum
;
180 sum
= list_first_entry(&sdev
->csum_list
,
181 struct btrfs_ordered_sum
, list
);
182 list_del(&sum
->list
);
187 static noinline_for_stack
void scrub_free_dev(struct scrub_dev
*sdev
)
194 /* this can happen when scrub is cancelled */
195 if (sdev
->curr
!= -1) {
196 struct scrub_bio
*sbio
= sdev
->bios
[sdev
->curr
];
198 for (i
= 0; i
< sbio
->page_count
; i
++) {
199 BUG_ON(!sbio
->pagev
[i
]);
200 BUG_ON(!sbio
->pagev
[i
]->page
);
201 scrub_block_put(sbio
->pagev
[i
]->sblock
);
206 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
207 struct scrub_bio
*sbio
= sdev
->bios
[i
];
214 scrub_free_csums(sdev
);
218 static noinline_for_stack
219 struct scrub_dev
*scrub_setup_dev(struct btrfs_device
*dev
)
221 struct scrub_dev
*sdev
;
223 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
226 pages_per_bio
= min_t(int, SCRUB_PAGES_PER_BIO
,
227 bio_get_nr_vecs(dev
->bdev
));
228 sdev
= kzalloc(sizeof(*sdev
), GFP_NOFS
);
232 sdev
->pages_per_bio
= pages_per_bio
;
234 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
235 struct scrub_bio
*sbio
;
237 sbio
= kzalloc(sizeof(*sbio
), GFP_NOFS
);
240 sdev
->bios
[i
] = sbio
;
244 sbio
->page_count
= 0;
245 sbio
->work
.func
= scrub_bio_end_io_worker
;
247 if (i
!= SCRUB_BIOS_PER_DEV
-1)
248 sdev
->bios
[i
]->next_free
= i
+ 1;
250 sdev
->bios
[i
]->next_free
= -1;
252 sdev
->first_free
= 0;
253 sdev
->nodesize
= dev
->dev_root
->nodesize
;
254 sdev
->leafsize
= dev
->dev_root
->leafsize
;
255 sdev
->sectorsize
= dev
->dev_root
->sectorsize
;
256 atomic_set(&sdev
->in_flight
, 0);
257 atomic_set(&sdev
->fixup_cnt
, 0);
258 atomic_set(&sdev
->cancel_req
, 0);
259 sdev
->csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
260 INIT_LIST_HEAD(&sdev
->csum_list
);
262 spin_lock_init(&sdev
->list_lock
);
263 spin_lock_init(&sdev
->stat_lock
);
264 init_waitqueue_head(&sdev
->list_wait
);
268 scrub_free_dev(sdev
);
269 return ERR_PTR(-ENOMEM
);
272 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
, void *ctx
)
278 struct extent_buffer
*eb
;
279 struct btrfs_inode_item
*inode_item
;
280 struct scrub_warning
*swarn
= ctx
;
281 struct btrfs_fs_info
*fs_info
= swarn
->dev
->dev_root
->fs_info
;
282 struct inode_fs_paths
*ipath
= NULL
;
283 struct btrfs_root
*local_root
;
284 struct btrfs_key root_key
;
286 root_key
.objectid
= root
;
287 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
288 root_key
.offset
= (u64
)-1;
289 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
290 if (IS_ERR(local_root
)) {
291 ret
= PTR_ERR(local_root
);
295 ret
= inode_item_info(inum
, 0, local_root
, swarn
->path
);
297 btrfs_release_path(swarn
->path
);
301 eb
= swarn
->path
->nodes
[0];
302 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
303 struct btrfs_inode_item
);
304 isize
= btrfs_inode_size(eb
, inode_item
);
305 nlink
= btrfs_inode_nlink(eb
, inode_item
);
306 btrfs_release_path(swarn
->path
);
308 ipath
= init_ipath(4096, local_root
, swarn
->path
);
310 ret
= PTR_ERR(ipath
);
314 ret
= paths_from_inode(inum
, ipath
);
320 * we deliberately ignore the bit ipath might have been too small to
321 * hold all of the paths here
323 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
324 printk_in_rcu(KERN_WARNING
"btrfs: %s at logical %llu on dev "
325 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
326 "length %llu, links %u (path: %s)\n", swarn
->errstr
,
327 swarn
->logical
, rcu_str_deref(swarn
->dev
->name
),
328 (unsigned long long)swarn
->sector
, root
, inum
, offset
,
329 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
330 (char *)(unsigned long)ipath
->fspath
->val
[i
]);
336 printk_in_rcu(KERN_WARNING
"btrfs: %s at logical %llu on dev "
337 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
338 "resolving failed with ret=%d\n", swarn
->errstr
,
339 swarn
->logical
, rcu_str_deref(swarn
->dev
->name
),
340 (unsigned long long)swarn
->sector
, root
, inum
, offset
, ret
);
346 static void scrub_print_warning(const char *errstr
, struct scrub_block
*sblock
)
348 struct btrfs_device
*dev
= sblock
->sdev
->dev
;
349 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
350 struct btrfs_path
*path
;
351 struct btrfs_key found_key
;
352 struct extent_buffer
*eb
;
353 struct btrfs_extent_item
*ei
;
354 struct scrub_warning swarn
;
355 unsigned long ptr
= 0;
361 const int bufsize
= 4096;
364 path
= btrfs_alloc_path();
366 swarn
.scratch_buf
= kmalloc(bufsize
, GFP_NOFS
);
367 swarn
.msg_buf
= kmalloc(bufsize
, GFP_NOFS
);
368 BUG_ON(sblock
->page_count
< 1);
369 swarn
.sector
= (sblock
->pagev
[0].physical
) >> 9;
370 swarn
.logical
= sblock
->pagev
[0].logical
;
371 swarn
.errstr
= errstr
;
373 swarn
.msg_bufsize
= bufsize
;
374 swarn
.scratch_bufsize
= bufsize
;
376 if (!path
|| !swarn
.scratch_buf
|| !swarn
.msg_buf
)
379 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
,
384 extent_item_pos
= swarn
.logical
- found_key
.objectid
;
385 swarn
.extent_item_size
= found_key
.offset
;
388 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
389 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
390 btrfs_release_path(path
);
392 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
394 ret
= tree_backref_for_extent(&ptr
, eb
, ei
, item_size
,
395 &ref_root
, &ref_level
);
396 printk_in_rcu(KERN_WARNING
397 "btrfs: %s at logical %llu on dev %s, "
398 "sector %llu: metadata %s (level %d) in tree "
399 "%llu\n", errstr
, swarn
.logical
,
400 rcu_str_deref(dev
->name
),
401 (unsigned long long)swarn
.sector
,
402 ref_level
? "node" : "leaf",
403 ret
< 0 ? -1 : ref_level
,
404 ret
< 0 ? -1 : ref_root
);
408 iterate_extent_inodes(fs_info
, found_key
.objectid
,
410 scrub_print_warning_inode
, &swarn
);
414 btrfs_free_path(path
);
415 kfree(swarn
.scratch_buf
);
416 kfree(swarn
.msg_buf
);
419 static int scrub_fixup_readpage(u64 inum
, u64 offset
, u64 root
, void *ctx
)
421 struct page
*page
= NULL
;
423 struct scrub_fixup_nodatasum
*fixup
= ctx
;
426 struct btrfs_key key
;
427 struct inode
*inode
= NULL
;
428 u64 end
= offset
+ PAGE_SIZE
- 1;
429 struct btrfs_root
*local_root
;
432 key
.type
= BTRFS_ROOT_ITEM_KEY
;
433 key
.offset
= (u64
)-1;
434 local_root
= btrfs_read_fs_root_no_name(fixup
->root
->fs_info
, &key
);
435 if (IS_ERR(local_root
))
436 return PTR_ERR(local_root
);
438 key
.type
= BTRFS_INODE_ITEM_KEY
;
441 inode
= btrfs_iget(fixup
->root
->fs_info
->sb
, &key
, local_root
, NULL
);
443 return PTR_ERR(inode
);
445 index
= offset
>> PAGE_CACHE_SHIFT
;
447 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
453 if (PageUptodate(page
)) {
454 struct btrfs_mapping_tree
*map_tree
;
455 if (PageDirty(page
)) {
457 * we need to write the data to the defect sector. the
458 * data that was in that sector is not in memory,
459 * because the page was modified. we must not write the
460 * modified page to that sector.
462 * TODO: what could be done here: wait for the delalloc
463 * runner to write out that page (might involve
464 * COW) and see whether the sector is still
465 * referenced afterwards.
467 * For the meantime, we'll treat this error
468 * incorrectable, although there is a chance that a
469 * later scrub will find the bad sector again and that
470 * there's no dirty page in memory, then.
475 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
476 ret
= repair_io_failure(map_tree
, offset
, PAGE_SIZE
,
477 fixup
->logical
, page
,
483 * we need to get good data first. the general readpage path
484 * will call repair_io_failure for us, we just have to make
485 * sure we read the bad mirror.
487 ret
= set_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
488 EXTENT_DAMAGED
, GFP_NOFS
);
490 /* set_extent_bits should give proper error */
497 ret
= extent_read_full_page(&BTRFS_I(inode
)->io_tree
, page
,
500 wait_on_page_locked(page
);
502 corrected
= !test_range_bit(&BTRFS_I(inode
)->io_tree
, offset
,
503 end
, EXTENT_DAMAGED
, 0, NULL
);
505 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
506 EXTENT_DAMAGED
, GFP_NOFS
);
518 if (ret
== 0 && corrected
) {
520 * we only need to call readpage for one of the inodes belonging
521 * to this extent. so make iterate_extent_inodes stop
529 static void scrub_fixup_nodatasum(struct btrfs_work
*work
)
532 struct scrub_fixup_nodatasum
*fixup
;
533 struct scrub_dev
*sdev
;
534 struct btrfs_trans_handle
*trans
= NULL
;
535 struct btrfs_fs_info
*fs_info
;
536 struct btrfs_path
*path
;
537 int uncorrectable
= 0;
539 fixup
= container_of(work
, struct scrub_fixup_nodatasum
, work
);
541 fs_info
= fixup
->root
->fs_info
;
543 path
= btrfs_alloc_path();
545 spin_lock(&sdev
->stat_lock
);
546 ++sdev
->stat
.malloc_errors
;
547 spin_unlock(&sdev
->stat_lock
);
552 trans
= btrfs_join_transaction(fixup
->root
);
559 * the idea is to trigger a regular read through the standard path. we
560 * read a page from the (failed) logical address by specifying the
561 * corresponding copynum of the failed sector. thus, that readpage is
563 * that is the point where on-the-fly error correction will kick in
564 * (once it's finished) and rewrite the failed sector if a good copy
567 ret
= iterate_inodes_from_logical(fixup
->logical
, fixup
->root
->fs_info
,
568 path
, scrub_fixup_readpage
,
576 spin_lock(&sdev
->stat_lock
);
577 ++sdev
->stat
.corrected_errors
;
578 spin_unlock(&sdev
->stat_lock
);
581 if (trans
&& !IS_ERR(trans
))
582 btrfs_end_transaction(trans
, fixup
->root
);
584 spin_lock(&sdev
->stat_lock
);
585 ++sdev
->stat
.uncorrectable_errors
;
586 spin_unlock(&sdev
->stat_lock
);
588 printk_ratelimited_in_rcu(KERN_ERR
589 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
590 (unsigned long long)fixup
->logical
,
591 rcu_str_deref(sdev
->dev
->name
));
594 btrfs_free_path(path
);
597 /* see caller why we're pretending to be paused in the scrub counters */
598 mutex_lock(&fs_info
->scrub_lock
);
599 atomic_dec(&fs_info
->scrubs_running
);
600 atomic_dec(&fs_info
->scrubs_paused
);
601 mutex_unlock(&fs_info
->scrub_lock
);
602 atomic_dec(&sdev
->fixup_cnt
);
603 wake_up(&fs_info
->scrub_pause_wait
);
604 wake_up(&sdev
->list_wait
);
608 * scrub_handle_errored_block gets called when either verification of the
609 * pages failed or the bio failed to read, e.g. with EIO. In the latter
610 * case, this function handles all pages in the bio, even though only one
612 * The goal of this function is to repair the errored block by using the
613 * contents of one of the mirrors.
615 static int scrub_handle_errored_block(struct scrub_block
*sblock_to_check
)
617 struct scrub_dev
*sdev
= sblock_to_check
->sdev
;
618 struct btrfs_fs_info
*fs_info
;
622 unsigned int failed_mirror_index
;
623 unsigned int is_metadata
;
624 unsigned int have_csum
;
626 struct scrub_block
*sblocks_for_recheck
; /* holds one for each mirror */
627 struct scrub_block
*sblock_bad
;
632 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
633 DEFAULT_RATELIMIT_BURST
);
635 BUG_ON(sblock_to_check
->page_count
< 1);
636 fs_info
= sdev
->dev
->dev_root
->fs_info
;
637 length
= sblock_to_check
->page_count
* PAGE_SIZE
;
638 logical
= sblock_to_check
->pagev
[0].logical
;
639 generation
= sblock_to_check
->pagev
[0].generation
;
640 BUG_ON(sblock_to_check
->pagev
[0].mirror_num
< 1);
641 failed_mirror_index
= sblock_to_check
->pagev
[0].mirror_num
- 1;
642 is_metadata
= !(sblock_to_check
->pagev
[0].flags
&
643 BTRFS_EXTENT_FLAG_DATA
);
644 have_csum
= sblock_to_check
->pagev
[0].have_csum
;
645 csum
= sblock_to_check
->pagev
[0].csum
;
648 * read all mirrors one after the other. This includes to
649 * re-read the extent or metadata block that failed (that was
650 * the cause that this fixup code is called) another time,
651 * page by page this time in order to know which pages
652 * caused I/O errors and which ones are good (for all mirrors).
653 * It is the goal to handle the situation when more than one
654 * mirror contains I/O errors, but the errors do not
655 * overlap, i.e. the data can be repaired by selecting the
656 * pages from those mirrors without I/O error on the
657 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
658 * would be that mirror #1 has an I/O error on the first page,
659 * the second page is good, and mirror #2 has an I/O error on
660 * the second page, but the first page is good.
661 * Then the first page of the first mirror can be repaired by
662 * taking the first page of the second mirror, and the
663 * second page of the second mirror can be repaired by
664 * copying the contents of the 2nd page of the 1st mirror.
665 * One more note: if the pages of one mirror contain I/O
666 * errors, the checksum cannot be verified. In order to get
667 * the best data for repairing, the first attempt is to find
668 * a mirror without I/O errors and with a validated checksum.
669 * Only if this is not possible, the pages are picked from
670 * mirrors with I/O errors without considering the checksum.
671 * If the latter is the case, at the end, the checksum of the
672 * repaired area is verified in order to correctly maintain
676 sblocks_for_recheck
= kzalloc(BTRFS_MAX_MIRRORS
*
677 sizeof(*sblocks_for_recheck
),
679 if (!sblocks_for_recheck
) {
680 spin_lock(&sdev
->stat_lock
);
681 sdev
->stat
.malloc_errors
++;
682 sdev
->stat
.read_errors
++;
683 sdev
->stat
.uncorrectable_errors
++;
684 spin_unlock(&sdev
->stat_lock
);
685 btrfs_dev_stat_inc_and_print(sdev
->dev
,
686 BTRFS_DEV_STAT_READ_ERRS
);
690 /* setup the context, map the logical blocks and alloc the pages */
691 ret
= scrub_setup_recheck_block(sdev
, &fs_info
->mapping_tree
, length
,
692 logical
, sblocks_for_recheck
);
694 spin_lock(&sdev
->stat_lock
);
695 sdev
->stat
.read_errors
++;
696 sdev
->stat
.uncorrectable_errors
++;
697 spin_unlock(&sdev
->stat_lock
);
698 btrfs_dev_stat_inc_and_print(sdev
->dev
,
699 BTRFS_DEV_STAT_READ_ERRS
);
702 BUG_ON(failed_mirror_index
>= BTRFS_MAX_MIRRORS
);
703 sblock_bad
= sblocks_for_recheck
+ failed_mirror_index
;
705 /* build and submit the bios for the failed mirror, check checksums */
706 ret
= scrub_recheck_block(fs_info
, sblock_bad
, is_metadata
, have_csum
,
707 csum
, generation
, sdev
->csum_size
);
709 spin_lock(&sdev
->stat_lock
);
710 sdev
->stat
.read_errors
++;
711 sdev
->stat
.uncorrectable_errors
++;
712 spin_unlock(&sdev
->stat_lock
);
713 btrfs_dev_stat_inc_and_print(sdev
->dev
,
714 BTRFS_DEV_STAT_READ_ERRS
);
718 if (!sblock_bad
->header_error
&& !sblock_bad
->checksum_error
&&
719 sblock_bad
->no_io_error_seen
) {
721 * the error disappeared after reading page by page, or
722 * the area was part of a huge bio and other parts of the
723 * bio caused I/O errors, or the block layer merged several
724 * read requests into one and the error is caused by a
725 * different bio (usually one of the two latter cases is
728 spin_lock(&sdev
->stat_lock
);
729 sdev
->stat
.unverified_errors
++;
730 spin_unlock(&sdev
->stat_lock
);
735 if (!sblock_bad
->no_io_error_seen
) {
736 spin_lock(&sdev
->stat_lock
);
737 sdev
->stat
.read_errors
++;
738 spin_unlock(&sdev
->stat_lock
);
739 if (__ratelimit(&_rs
))
740 scrub_print_warning("i/o error", sblock_to_check
);
741 btrfs_dev_stat_inc_and_print(sdev
->dev
,
742 BTRFS_DEV_STAT_READ_ERRS
);
743 } else if (sblock_bad
->checksum_error
) {
744 spin_lock(&sdev
->stat_lock
);
745 sdev
->stat
.csum_errors
++;
746 spin_unlock(&sdev
->stat_lock
);
747 if (__ratelimit(&_rs
))
748 scrub_print_warning("checksum error", sblock_to_check
);
749 btrfs_dev_stat_inc_and_print(sdev
->dev
,
750 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
751 } else if (sblock_bad
->header_error
) {
752 spin_lock(&sdev
->stat_lock
);
753 sdev
->stat
.verify_errors
++;
754 spin_unlock(&sdev
->stat_lock
);
755 if (__ratelimit(&_rs
))
756 scrub_print_warning("checksum/header error",
758 if (sblock_bad
->generation_error
)
759 btrfs_dev_stat_inc_and_print(sdev
->dev
,
760 BTRFS_DEV_STAT_GENERATION_ERRS
);
762 btrfs_dev_stat_inc_and_print(sdev
->dev
,
763 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
767 goto did_not_correct_error
;
769 if (!is_metadata
&& !have_csum
) {
770 struct scrub_fixup_nodatasum
*fixup_nodatasum
;
773 * !is_metadata and !have_csum, this means that the data
774 * might not be COW'ed, that it might be modified
775 * concurrently. The general strategy to work on the
776 * commit root does not help in the case when COW is not
779 fixup_nodatasum
= kzalloc(sizeof(*fixup_nodatasum
), GFP_NOFS
);
780 if (!fixup_nodatasum
)
781 goto did_not_correct_error
;
782 fixup_nodatasum
->sdev
= sdev
;
783 fixup_nodatasum
->logical
= logical
;
784 fixup_nodatasum
->root
= fs_info
->extent_root
;
785 fixup_nodatasum
->mirror_num
= failed_mirror_index
+ 1;
787 * increment scrubs_running to prevent cancel requests from
788 * completing as long as a fixup worker is running. we must also
789 * increment scrubs_paused to prevent deadlocking on pause
790 * requests used for transactions commits (as the worker uses a
791 * transaction context). it is safe to regard the fixup worker
792 * as paused for all matters practical. effectively, we only
793 * avoid cancellation requests from completing.
795 mutex_lock(&fs_info
->scrub_lock
);
796 atomic_inc(&fs_info
->scrubs_running
);
797 atomic_inc(&fs_info
->scrubs_paused
);
798 mutex_unlock(&fs_info
->scrub_lock
);
799 atomic_inc(&sdev
->fixup_cnt
);
800 fixup_nodatasum
->work
.func
= scrub_fixup_nodatasum
;
801 btrfs_queue_worker(&fs_info
->scrub_workers
,
802 &fixup_nodatasum
->work
);
807 * now build and submit the bios for the other mirrors, check
810 for (mirror_index
= 0;
811 mirror_index
< BTRFS_MAX_MIRRORS
&&
812 sblocks_for_recheck
[mirror_index
].page_count
> 0;
814 if (mirror_index
== failed_mirror_index
)
817 /* build and submit the bios, check checksums */
818 ret
= scrub_recheck_block(fs_info
,
819 sblocks_for_recheck
+ mirror_index
,
820 is_metadata
, have_csum
, csum
,
821 generation
, sdev
->csum_size
);
823 goto did_not_correct_error
;
827 * first try to pick the mirror which is completely without I/O
828 * errors and also does not have a checksum error.
829 * If one is found, and if a checksum is present, the full block
830 * that is known to contain an error is rewritten. Afterwards
831 * the block is known to be corrected.
832 * If a mirror is found which is completely correct, and no
833 * checksum is present, only those pages are rewritten that had
834 * an I/O error in the block to be repaired, since it cannot be
835 * determined, which copy of the other pages is better (and it
836 * could happen otherwise that a correct page would be
837 * overwritten by a bad one).
839 for (mirror_index
= 0;
840 mirror_index
< BTRFS_MAX_MIRRORS
&&
841 sblocks_for_recheck
[mirror_index
].page_count
> 0;
843 struct scrub_block
*sblock_other
= sblocks_for_recheck
+
846 if (!sblock_other
->header_error
&&
847 !sblock_other
->checksum_error
&&
848 sblock_other
->no_io_error_seen
) {
849 int force_write
= is_metadata
|| have_csum
;
851 ret
= scrub_repair_block_from_good_copy(sblock_bad
,
855 goto corrected_error
;
860 * in case of I/O errors in the area that is supposed to be
861 * repaired, continue by picking good copies of those pages.
862 * Select the good pages from mirrors to rewrite bad pages from
863 * the area to fix. Afterwards verify the checksum of the block
864 * that is supposed to be repaired. This verification step is
865 * only done for the purpose of statistic counting and for the
866 * final scrub report, whether errors remain.
867 * A perfect algorithm could make use of the checksum and try
868 * all possible combinations of pages from the different mirrors
869 * until the checksum verification succeeds. For example, when
870 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
871 * of mirror #2 is readable but the final checksum test fails,
872 * then the 2nd page of mirror #3 could be tried, whether now
873 * the final checksum succeedes. But this would be a rare
874 * exception and is therefore not implemented. At least it is
875 * avoided that the good copy is overwritten.
876 * A more useful improvement would be to pick the sectors
877 * without I/O error based on sector sizes (512 bytes on legacy
878 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
879 * mirror could be repaired by taking 512 byte of a different
880 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
881 * area are unreadable.
884 /* can only fix I/O errors from here on */
885 if (sblock_bad
->no_io_error_seen
)
886 goto did_not_correct_error
;
889 for (page_num
= 0; page_num
< sblock_bad
->page_count
; page_num
++) {
890 struct scrub_page
*page_bad
= sblock_bad
->pagev
+ page_num
;
892 if (!page_bad
->io_error
)
895 for (mirror_index
= 0;
896 mirror_index
< BTRFS_MAX_MIRRORS
&&
897 sblocks_for_recheck
[mirror_index
].page_count
> 0;
899 struct scrub_block
*sblock_other
= sblocks_for_recheck
+
901 struct scrub_page
*page_other
= sblock_other
->pagev
+
904 if (!page_other
->io_error
) {
905 ret
= scrub_repair_page_from_good_copy(
906 sblock_bad
, sblock_other
, page_num
, 0);
908 page_bad
->io_error
= 0;
909 break; /* succeeded for this page */
914 if (page_bad
->io_error
) {
915 /* did not find a mirror to copy the page from */
921 if (is_metadata
|| have_csum
) {
923 * need to verify the checksum now that all
924 * sectors on disk are repaired (the write
925 * request for data to be repaired is on its way).
926 * Just be lazy and use scrub_recheck_block()
927 * which re-reads the data before the checksum
928 * is verified, but most likely the data comes out
931 ret
= scrub_recheck_block(fs_info
, sblock_bad
,
932 is_metadata
, have_csum
, csum
,
933 generation
, sdev
->csum_size
);
934 if (!ret
&& !sblock_bad
->header_error
&&
935 !sblock_bad
->checksum_error
&&
936 sblock_bad
->no_io_error_seen
)
937 goto corrected_error
;
939 goto did_not_correct_error
;
942 spin_lock(&sdev
->stat_lock
);
943 sdev
->stat
.corrected_errors
++;
944 spin_unlock(&sdev
->stat_lock
);
945 printk_ratelimited_in_rcu(KERN_ERR
946 "btrfs: fixed up error at logical %llu on dev %s\n",
947 (unsigned long long)logical
,
948 rcu_str_deref(sdev
->dev
->name
));
951 did_not_correct_error
:
952 spin_lock(&sdev
->stat_lock
);
953 sdev
->stat
.uncorrectable_errors
++;
954 spin_unlock(&sdev
->stat_lock
);
955 printk_ratelimited_in_rcu(KERN_ERR
956 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
957 (unsigned long long)logical
,
958 rcu_str_deref(sdev
->dev
->name
));
962 if (sblocks_for_recheck
) {
963 for (mirror_index
= 0; mirror_index
< BTRFS_MAX_MIRRORS
;
965 struct scrub_block
*sblock
= sblocks_for_recheck
+
969 for (page_index
= 0; page_index
< SCRUB_PAGES_PER_BIO
;
971 if (sblock
->pagev
[page_index
].page
)
973 sblock
->pagev
[page_index
].page
);
975 kfree(sblocks_for_recheck
);
981 static int scrub_setup_recheck_block(struct scrub_dev
*sdev
,
982 struct btrfs_mapping_tree
*map_tree
,
983 u64 length
, u64 logical
,
984 struct scrub_block
*sblocks_for_recheck
)
991 * note: the three members sdev, ref_count and outstanding_pages
992 * are not used (and not set) in the blocks that are used for
993 * the recheck procedure
998 u64 sublen
= min_t(u64
, length
, PAGE_SIZE
);
999 u64 mapped_length
= sublen
;
1000 struct btrfs_bio
*bbio
= NULL
;
1003 * with a length of PAGE_SIZE, each returned stripe
1004 * represents one mirror
1006 ret
= btrfs_map_block(map_tree
, WRITE
, logical
, &mapped_length
,
1008 if (ret
|| !bbio
|| mapped_length
< sublen
) {
1013 BUG_ON(page_index
>= SCRUB_PAGES_PER_BIO
);
1014 for (mirror_index
= 0; mirror_index
< (int)bbio
->num_stripes
;
1016 struct scrub_block
*sblock
;
1017 struct scrub_page
*page
;
1019 if (mirror_index
>= BTRFS_MAX_MIRRORS
)
1022 sblock
= sblocks_for_recheck
+ mirror_index
;
1023 page
= sblock
->pagev
+ page_index
;
1024 page
->logical
= logical
;
1025 page
->physical
= bbio
->stripes
[mirror_index
].physical
;
1026 /* for missing devices, dev->bdev is NULL */
1027 page
->dev
= bbio
->stripes
[mirror_index
].dev
;
1028 page
->mirror_num
= mirror_index
+ 1;
1029 page
->page
= alloc_page(GFP_NOFS
);
1031 spin_lock(&sdev
->stat_lock
);
1032 sdev
->stat
.malloc_errors
++;
1033 spin_unlock(&sdev
->stat_lock
);
1037 sblock
->page_count
++;
1049 * this function will check the on disk data for checksum errors, header
1050 * errors and read I/O errors. If any I/O errors happen, the exact pages
1051 * which are errored are marked as being bad. The goal is to enable scrub
1052 * to take those pages that are not errored from all the mirrors so that
1053 * the pages that are errored in the just handled mirror can be repaired.
1055 static int scrub_recheck_block(struct btrfs_fs_info
*fs_info
,
1056 struct scrub_block
*sblock
, int is_metadata
,
1057 int have_csum
, u8
*csum
, u64 generation
,
1062 sblock
->no_io_error_seen
= 1;
1063 sblock
->header_error
= 0;
1064 sblock
->checksum_error
= 0;
1066 for (page_num
= 0; page_num
< sblock
->page_count
; page_num
++) {
1069 struct scrub_page
*page
= sblock
->pagev
+ page_num
;
1070 DECLARE_COMPLETION_ONSTACK(complete
);
1072 if (page
->dev
->bdev
== NULL
) {
1074 sblock
->no_io_error_seen
= 0;
1078 BUG_ON(!page
->page
);
1079 bio
= bio_alloc(GFP_NOFS
, 1);
1082 bio
->bi_bdev
= page
->dev
->bdev
;
1083 bio
->bi_sector
= page
->physical
>> 9;
1084 bio
->bi_end_io
= scrub_complete_bio_end_io
;
1085 bio
->bi_private
= &complete
;
1087 ret
= bio_add_page(bio
, page
->page
, PAGE_SIZE
, 0);
1088 if (PAGE_SIZE
!= ret
) {
1092 btrfsic_submit_bio(READ
, bio
);
1094 /* this will also unplug the queue */
1095 wait_for_completion(&complete
);
1097 page
->io_error
= !test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1098 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1099 sblock
->no_io_error_seen
= 0;
1103 if (sblock
->no_io_error_seen
)
1104 scrub_recheck_block_checksum(fs_info
, sblock
, is_metadata
,
1105 have_csum
, csum
, generation
,
1111 static void scrub_recheck_block_checksum(struct btrfs_fs_info
*fs_info
,
1112 struct scrub_block
*sblock
,
1113 int is_metadata
, int have_csum
,
1114 const u8
*csum
, u64 generation
,
1118 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1120 struct btrfs_root
*root
= fs_info
->extent_root
;
1121 void *mapped_buffer
;
1123 BUG_ON(!sblock
->pagev
[0].page
);
1125 struct btrfs_header
*h
;
1127 mapped_buffer
= kmap_atomic(sblock
->pagev
[0].page
);
1128 h
= (struct btrfs_header
*)mapped_buffer
;
1130 if (sblock
->pagev
[0].logical
!= le64_to_cpu(h
->bytenr
) ||
1131 memcmp(h
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
) ||
1132 memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
1134 sblock
->header_error
= 1;
1135 } else if (generation
!= le64_to_cpu(h
->generation
)) {
1136 sblock
->header_error
= 1;
1137 sblock
->generation_error
= 1;
1144 mapped_buffer
= kmap_atomic(sblock
->pagev
[0].page
);
1147 for (page_num
= 0;;) {
1148 if (page_num
== 0 && is_metadata
)
1149 crc
= btrfs_csum_data(root
,
1150 ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
,
1151 crc
, PAGE_SIZE
- BTRFS_CSUM_SIZE
);
1153 crc
= btrfs_csum_data(root
, mapped_buffer
, crc
,
1156 kunmap_atomic(mapped_buffer
);
1158 if (page_num
>= sblock
->page_count
)
1160 BUG_ON(!sblock
->pagev
[page_num
].page
);
1162 mapped_buffer
= kmap_atomic(sblock
->pagev
[page_num
].page
);
1165 btrfs_csum_final(crc
, calculated_csum
);
1166 if (memcmp(calculated_csum
, csum
, csum_size
))
1167 sblock
->checksum_error
= 1;
1170 static void scrub_complete_bio_end_io(struct bio
*bio
, int err
)
1172 complete((struct completion
*)bio
->bi_private
);
1175 static int scrub_repair_block_from_good_copy(struct scrub_block
*sblock_bad
,
1176 struct scrub_block
*sblock_good
,
1182 for (page_num
= 0; page_num
< sblock_bad
->page_count
; page_num
++) {
1185 ret_sub
= scrub_repair_page_from_good_copy(sblock_bad
,
1196 static int scrub_repair_page_from_good_copy(struct scrub_block
*sblock_bad
,
1197 struct scrub_block
*sblock_good
,
1198 int page_num
, int force_write
)
1200 struct scrub_page
*page_bad
= sblock_bad
->pagev
+ page_num
;
1201 struct scrub_page
*page_good
= sblock_good
->pagev
+ page_num
;
1203 BUG_ON(sblock_bad
->pagev
[page_num
].page
== NULL
);
1204 BUG_ON(sblock_good
->pagev
[page_num
].page
== NULL
);
1205 if (force_write
|| sblock_bad
->header_error
||
1206 sblock_bad
->checksum_error
|| page_bad
->io_error
) {
1209 DECLARE_COMPLETION_ONSTACK(complete
);
1211 bio
= bio_alloc(GFP_NOFS
, 1);
1214 bio
->bi_bdev
= page_bad
->dev
->bdev
;
1215 bio
->bi_sector
= page_bad
->physical
>> 9;
1216 bio
->bi_end_io
= scrub_complete_bio_end_io
;
1217 bio
->bi_private
= &complete
;
1219 ret
= bio_add_page(bio
, page_good
->page
, PAGE_SIZE
, 0);
1220 if (PAGE_SIZE
!= ret
) {
1224 btrfsic_submit_bio(WRITE
, bio
);
1226 /* this will also unplug the queue */
1227 wait_for_completion(&complete
);
1228 if (!bio_flagged(bio
, BIO_UPTODATE
)) {
1229 btrfs_dev_stat_inc_and_print(page_bad
->dev
,
1230 BTRFS_DEV_STAT_WRITE_ERRS
);
1240 static void scrub_checksum(struct scrub_block
*sblock
)
1245 BUG_ON(sblock
->page_count
< 1);
1246 flags
= sblock
->pagev
[0].flags
;
1248 if (flags
& BTRFS_EXTENT_FLAG_DATA
)
1249 ret
= scrub_checksum_data(sblock
);
1250 else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
1251 ret
= scrub_checksum_tree_block(sblock
);
1252 else if (flags
& BTRFS_EXTENT_FLAG_SUPER
)
1253 (void)scrub_checksum_super(sblock
);
1257 scrub_handle_errored_block(sblock
);
1260 static int scrub_checksum_data(struct scrub_block
*sblock
)
1262 struct scrub_dev
*sdev
= sblock
->sdev
;
1263 u8 csum
[BTRFS_CSUM_SIZE
];
1269 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1273 BUG_ON(sblock
->page_count
< 1);
1274 if (!sblock
->pagev
[0].have_csum
)
1277 on_disk_csum
= sblock
->pagev
[0].csum
;
1278 page
= sblock
->pagev
[0].page
;
1279 buffer
= kmap_atomic(page
);
1281 len
= sdev
->sectorsize
;
1284 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1286 crc
= btrfs_csum_data(root
, buffer
, crc
, l
);
1287 kunmap_atomic(buffer
);
1292 BUG_ON(index
>= sblock
->page_count
);
1293 BUG_ON(!sblock
->pagev
[index
].page
);
1294 page
= sblock
->pagev
[index
].page
;
1295 buffer
= kmap_atomic(page
);
1298 btrfs_csum_final(crc
, csum
);
1299 if (memcmp(csum
, on_disk_csum
, sdev
->csum_size
))
1305 static int scrub_checksum_tree_block(struct scrub_block
*sblock
)
1307 struct scrub_dev
*sdev
= sblock
->sdev
;
1308 struct btrfs_header
*h
;
1309 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1310 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1311 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1312 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1314 void *mapped_buffer
;
1323 BUG_ON(sblock
->page_count
< 1);
1324 page
= sblock
->pagev
[0].page
;
1325 mapped_buffer
= kmap_atomic(page
);
1326 h
= (struct btrfs_header
*)mapped_buffer
;
1327 memcpy(on_disk_csum
, h
->csum
, sdev
->csum_size
);
1330 * we don't use the getter functions here, as we
1331 * a) don't have an extent buffer and
1332 * b) the page is already kmapped
1335 if (sblock
->pagev
[0].logical
!= le64_to_cpu(h
->bytenr
))
1338 if (sblock
->pagev
[0].generation
!= le64_to_cpu(h
->generation
))
1341 if (memcmp(h
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
1344 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
1348 BUG_ON(sdev
->nodesize
!= sdev
->leafsize
);
1349 len
= sdev
->nodesize
- BTRFS_CSUM_SIZE
;
1350 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
1351 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
1354 u64 l
= min_t(u64
, len
, mapped_size
);
1356 crc
= btrfs_csum_data(root
, p
, crc
, l
);
1357 kunmap_atomic(mapped_buffer
);
1362 BUG_ON(index
>= sblock
->page_count
);
1363 BUG_ON(!sblock
->pagev
[index
].page
);
1364 page
= sblock
->pagev
[index
].page
;
1365 mapped_buffer
= kmap_atomic(page
);
1366 mapped_size
= PAGE_SIZE
;
1370 btrfs_csum_final(crc
, calculated_csum
);
1371 if (memcmp(calculated_csum
, on_disk_csum
, sdev
->csum_size
))
1374 return fail
|| crc_fail
;
1377 static int scrub_checksum_super(struct scrub_block
*sblock
)
1379 struct btrfs_super_block
*s
;
1380 struct scrub_dev
*sdev
= sblock
->sdev
;
1381 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1382 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1383 u8 calculated_csum
[BTRFS_CSUM_SIZE
];
1384 u8 on_disk_csum
[BTRFS_CSUM_SIZE
];
1386 void *mapped_buffer
;
1395 BUG_ON(sblock
->page_count
< 1);
1396 page
= sblock
->pagev
[0].page
;
1397 mapped_buffer
= kmap_atomic(page
);
1398 s
= (struct btrfs_super_block
*)mapped_buffer
;
1399 memcpy(on_disk_csum
, s
->csum
, sdev
->csum_size
);
1401 if (sblock
->pagev
[0].logical
!= le64_to_cpu(s
->bytenr
))
1404 if (sblock
->pagev
[0].generation
!= le64_to_cpu(s
->generation
))
1407 if (memcmp(s
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
1410 len
= BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
;
1411 mapped_size
= PAGE_SIZE
- BTRFS_CSUM_SIZE
;
1412 p
= ((u8
*)mapped_buffer
) + BTRFS_CSUM_SIZE
;
1415 u64 l
= min_t(u64
, len
, mapped_size
);
1417 crc
= btrfs_csum_data(root
, p
, crc
, l
);
1418 kunmap_atomic(mapped_buffer
);
1423 BUG_ON(index
>= sblock
->page_count
);
1424 BUG_ON(!sblock
->pagev
[index
].page
);
1425 page
= sblock
->pagev
[index
].page
;
1426 mapped_buffer
= kmap_atomic(page
);
1427 mapped_size
= PAGE_SIZE
;
1431 btrfs_csum_final(crc
, calculated_csum
);
1432 if (memcmp(calculated_csum
, on_disk_csum
, sdev
->csum_size
))
1435 if (fail_cor
+ fail_gen
) {
1437 * if we find an error in a super block, we just report it.
1438 * They will get written with the next transaction commit
1441 spin_lock(&sdev
->stat_lock
);
1442 ++sdev
->stat
.super_errors
;
1443 spin_unlock(&sdev
->stat_lock
);
1445 btrfs_dev_stat_inc_and_print(sdev
->dev
,
1446 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
1448 btrfs_dev_stat_inc_and_print(sdev
->dev
,
1449 BTRFS_DEV_STAT_GENERATION_ERRS
);
1452 return fail_cor
+ fail_gen
;
1455 static void scrub_block_get(struct scrub_block
*sblock
)
1457 atomic_inc(&sblock
->ref_count
);
1460 static void scrub_block_put(struct scrub_block
*sblock
)
1462 if (atomic_dec_and_test(&sblock
->ref_count
)) {
1465 for (i
= 0; i
< sblock
->page_count
; i
++)
1466 if (sblock
->pagev
[i
].page
)
1467 __free_page(sblock
->pagev
[i
].page
);
1472 static void scrub_submit(struct scrub_dev
*sdev
)
1474 struct scrub_bio
*sbio
;
1476 if (sdev
->curr
== -1)
1479 sbio
= sdev
->bios
[sdev
->curr
];
1481 atomic_inc(&sdev
->in_flight
);
1483 btrfsic_submit_bio(READ
, sbio
->bio
);
1486 static int scrub_add_page_to_bio(struct scrub_dev
*sdev
,
1487 struct scrub_page
*spage
)
1489 struct scrub_block
*sblock
= spage
->sblock
;
1490 struct scrub_bio
*sbio
;
1495 * grab a fresh bio or wait for one to become available
1497 while (sdev
->curr
== -1) {
1498 spin_lock(&sdev
->list_lock
);
1499 sdev
->curr
= sdev
->first_free
;
1500 if (sdev
->curr
!= -1) {
1501 sdev
->first_free
= sdev
->bios
[sdev
->curr
]->next_free
;
1502 sdev
->bios
[sdev
->curr
]->next_free
= -1;
1503 sdev
->bios
[sdev
->curr
]->page_count
= 0;
1504 spin_unlock(&sdev
->list_lock
);
1506 spin_unlock(&sdev
->list_lock
);
1507 wait_event(sdev
->list_wait
, sdev
->first_free
!= -1);
1510 sbio
= sdev
->bios
[sdev
->curr
];
1511 if (sbio
->page_count
== 0) {
1514 sbio
->physical
= spage
->physical
;
1515 sbio
->logical
= spage
->logical
;
1518 bio
= bio_alloc(GFP_NOFS
, sdev
->pages_per_bio
);
1524 bio
->bi_private
= sbio
;
1525 bio
->bi_end_io
= scrub_bio_end_io
;
1526 bio
->bi_bdev
= sdev
->dev
->bdev
;
1527 bio
->bi_sector
= spage
->physical
>> 9;
1529 } else if (sbio
->physical
+ sbio
->page_count
* PAGE_SIZE
!=
1531 sbio
->logical
+ sbio
->page_count
* PAGE_SIZE
!=
1537 sbio
->pagev
[sbio
->page_count
] = spage
;
1538 ret
= bio_add_page(sbio
->bio
, spage
->page
, PAGE_SIZE
, 0);
1539 if (ret
!= PAGE_SIZE
) {
1540 if (sbio
->page_count
< 1) {
1549 scrub_block_get(sblock
); /* one for the added page */
1550 atomic_inc(&sblock
->outstanding_pages
);
1552 if (sbio
->page_count
== sdev
->pages_per_bio
)
1558 static int scrub_pages(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1559 u64 physical
, u64 flags
, u64 gen
, int mirror_num
,
1560 u8
*csum
, int force
)
1562 struct scrub_block
*sblock
;
1565 sblock
= kzalloc(sizeof(*sblock
), GFP_NOFS
);
1567 spin_lock(&sdev
->stat_lock
);
1568 sdev
->stat
.malloc_errors
++;
1569 spin_unlock(&sdev
->stat_lock
);
1573 /* one ref inside this function, plus one for each page later on */
1574 atomic_set(&sblock
->ref_count
, 1);
1575 sblock
->sdev
= sdev
;
1576 sblock
->no_io_error_seen
= 1;
1578 for (index
= 0; len
> 0; index
++) {
1579 struct scrub_page
*spage
= sblock
->pagev
+ index
;
1580 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1582 BUG_ON(index
>= SCRUB_MAX_PAGES_PER_BLOCK
);
1583 spage
->page
= alloc_page(GFP_NOFS
);
1585 spin_lock(&sdev
->stat_lock
);
1586 sdev
->stat
.malloc_errors
++;
1587 spin_unlock(&sdev
->stat_lock
);
1590 __free_page(sblock
->pagev
[index
].page
);
1595 spage
->sblock
= sblock
;
1596 spage
->dev
= sdev
->dev
;
1597 spage
->flags
= flags
;
1598 spage
->generation
= gen
;
1599 spage
->logical
= logical
;
1600 spage
->physical
= physical
;
1601 spage
->mirror_num
= mirror_num
;
1603 spage
->have_csum
= 1;
1604 memcpy(spage
->csum
, csum
, sdev
->csum_size
);
1606 spage
->have_csum
= 0;
1608 sblock
->page_count
++;
1614 BUG_ON(sblock
->page_count
== 0);
1615 for (index
= 0; index
< sblock
->page_count
; index
++) {
1616 struct scrub_page
*spage
= sblock
->pagev
+ index
;
1619 ret
= scrub_add_page_to_bio(sdev
, spage
);
1621 scrub_block_put(sblock
);
1629 /* last one frees, either here or in bio completion for last page */
1630 scrub_block_put(sblock
);
1634 static void scrub_bio_end_io(struct bio
*bio
, int err
)
1636 struct scrub_bio
*sbio
= bio
->bi_private
;
1637 struct scrub_dev
*sdev
= sbio
->sdev
;
1638 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
1643 btrfs_queue_worker(&fs_info
->scrub_workers
, &sbio
->work
);
1646 static void scrub_bio_end_io_worker(struct btrfs_work
*work
)
1648 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
1649 struct scrub_dev
*sdev
= sbio
->sdev
;
1652 BUG_ON(sbio
->page_count
> SCRUB_PAGES_PER_BIO
);
1654 for (i
= 0; i
< sbio
->page_count
; i
++) {
1655 struct scrub_page
*spage
= sbio
->pagev
[i
];
1657 spage
->io_error
= 1;
1658 spage
->sblock
->no_io_error_seen
= 0;
1662 /* now complete the scrub_block items that have all pages completed */
1663 for (i
= 0; i
< sbio
->page_count
; i
++) {
1664 struct scrub_page
*spage
= sbio
->pagev
[i
];
1665 struct scrub_block
*sblock
= spage
->sblock
;
1667 if (atomic_dec_and_test(&sblock
->outstanding_pages
))
1668 scrub_block_complete(sblock
);
1669 scrub_block_put(sblock
);
1674 spin_lock(&sdev
->list_lock
);
1675 sbio
->next_free
= sdev
->first_free
;
1676 sdev
->first_free
= sbio
->index
;
1677 spin_unlock(&sdev
->list_lock
);
1678 atomic_dec(&sdev
->in_flight
);
1679 wake_up(&sdev
->list_wait
);
1682 static void scrub_block_complete(struct scrub_block
*sblock
)
1684 if (!sblock
->no_io_error_seen
)
1685 scrub_handle_errored_block(sblock
);
1687 scrub_checksum(sblock
);
1690 static int scrub_find_csum(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1693 struct btrfs_ordered_sum
*sum
= NULL
;
1696 unsigned long num_sectors
;
1698 while (!list_empty(&sdev
->csum_list
)) {
1699 sum
= list_first_entry(&sdev
->csum_list
,
1700 struct btrfs_ordered_sum
, list
);
1701 if (sum
->bytenr
> logical
)
1703 if (sum
->bytenr
+ sum
->len
> logical
)
1706 ++sdev
->stat
.csum_discards
;
1707 list_del(&sum
->list
);
1714 num_sectors
= sum
->len
/ sdev
->sectorsize
;
1715 for (i
= 0; i
< num_sectors
; ++i
) {
1716 if (sum
->sums
[i
].bytenr
== logical
) {
1717 memcpy(csum
, &sum
->sums
[i
].sum
, sdev
->csum_size
);
1722 if (ret
&& i
== num_sectors
- 1) {
1723 list_del(&sum
->list
);
1729 /* scrub extent tries to collect up to 64 kB for each bio */
1730 static int scrub_extent(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1731 u64 physical
, u64 flags
, u64 gen
, int mirror_num
)
1734 u8 csum
[BTRFS_CSUM_SIZE
];
1737 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
1738 blocksize
= sdev
->sectorsize
;
1739 spin_lock(&sdev
->stat_lock
);
1740 sdev
->stat
.data_extents_scrubbed
++;
1741 sdev
->stat
.data_bytes_scrubbed
+= len
;
1742 spin_unlock(&sdev
->stat_lock
);
1743 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1744 BUG_ON(sdev
->nodesize
!= sdev
->leafsize
);
1745 blocksize
= sdev
->nodesize
;
1746 spin_lock(&sdev
->stat_lock
);
1747 sdev
->stat
.tree_extents_scrubbed
++;
1748 sdev
->stat
.tree_bytes_scrubbed
+= len
;
1749 spin_unlock(&sdev
->stat_lock
);
1751 blocksize
= sdev
->sectorsize
;
1756 u64 l
= min_t(u64
, len
, blocksize
);
1759 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
1760 /* push csums to sbio */
1761 have_csum
= scrub_find_csum(sdev
, logical
, l
, csum
);
1763 ++sdev
->stat
.no_csum
;
1765 ret
= scrub_pages(sdev
, logical
, l
, physical
, flags
, gen
,
1766 mirror_num
, have_csum
? csum
: NULL
, 0);
1776 static noinline_for_stack
int scrub_stripe(struct scrub_dev
*sdev
,
1777 struct map_lookup
*map
, int num
, u64 base
, u64 length
)
1779 struct btrfs_path
*path
;
1780 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
1781 struct btrfs_root
*root
= fs_info
->extent_root
;
1782 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
1783 struct btrfs_extent_item
*extent
;
1784 struct blk_plug plug
;
1790 struct extent_buffer
*l
;
1791 struct btrfs_key key
;
1796 struct reada_control
*reada1
;
1797 struct reada_control
*reada2
;
1798 struct btrfs_key key_start
;
1799 struct btrfs_key key_end
;
1801 u64 increment
= map
->stripe_len
;
1806 do_div(nstripes
, map
->stripe_len
);
1807 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1808 offset
= map
->stripe_len
* num
;
1809 increment
= map
->stripe_len
* map
->num_stripes
;
1811 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1812 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1813 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
1814 increment
= map
->stripe_len
* factor
;
1815 mirror_num
= num
% map
->sub_stripes
+ 1;
1816 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1817 increment
= map
->stripe_len
;
1818 mirror_num
= num
% map
->num_stripes
+ 1;
1819 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1820 increment
= map
->stripe_len
;
1821 mirror_num
= num
% map
->num_stripes
+ 1;
1823 increment
= map
->stripe_len
;
1827 path
= btrfs_alloc_path();
1832 * work on commit root. The related disk blocks are static as
1833 * long as COW is applied. This means, it is save to rewrite
1834 * them to repair disk errors without any race conditions
1836 path
->search_commit_root
= 1;
1837 path
->skip_locking
= 1;
1840 * trigger the readahead for extent tree csum tree and wait for
1841 * completion. During readahead, the scrub is officially paused
1842 * to not hold off transaction commits
1844 logical
= base
+ offset
;
1846 wait_event(sdev
->list_wait
,
1847 atomic_read(&sdev
->in_flight
) == 0);
1848 atomic_inc(&fs_info
->scrubs_paused
);
1849 wake_up(&fs_info
->scrub_pause_wait
);
1851 /* FIXME it might be better to start readahead at commit root */
1852 key_start
.objectid
= logical
;
1853 key_start
.type
= BTRFS_EXTENT_ITEM_KEY
;
1854 key_start
.offset
= (u64
)0;
1855 key_end
.objectid
= base
+ offset
+ nstripes
* increment
;
1856 key_end
.type
= BTRFS_EXTENT_ITEM_KEY
;
1857 key_end
.offset
= (u64
)0;
1858 reada1
= btrfs_reada_add(root
, &key_start
, &key_end
);
1860 key_start
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
1861 key_start
.type
= BTRFS_EXTENT_CSUM_KEY
;
1862 key_start
.offset
= logical
;
1863 key_end
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
1864 key_end
.type
= BTRFS_EXTENT_CSUM_KEY
;
1865 key_end
.offset
= base
+ offset
+ nstripes
* increment
;
1866 reada2
= btrfs_reada_add(csum_root
, &key_start
, &key_end
);
1868 if (!IS_ERR(reada1
))
1869 btrfs_reada_wait(reada1
);
1870 if (!IS_ERR(reada2
))
1871 btrfs_reada_wait(reada2
);
1873 mutex_lock(&fs_info
->scrub_lock
);
1874 while (atomic_read(&fs_info
->scrub_pause_req
)) {
1875 mutex_unlock(&fs_info
->scrub_lock
);
1876 wait_event(fs_info
->scrub_pause_wait
,
1877 atomic_read(&fs_info
->scrub_pause_req
) == 0);
1878 mutex_lock(&fs_info
->scrub_lock
);
1880 atomic_dec(&fs_info
->scrubs_paused
);
1881 mutex_unlock(&fs_info
->scrub_lock
);
1882 wake_up(&fs_info
->scrub_pause_wait
);
1885 * collect all data csums for the stripe to avoid seeking during
1886 * the scrub. This might currently (crc32) end up to be about 1MB
1888 blk_start_plug(&plug
);
1891 * now find all extents for each stripe and scrub them
1893 logical
= base
+ offset
;
1894 physical
= map
->stripes
[num
].physical
;
1896 for (i
= 0; i
< nstripes
; ++i
) {
1900 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
1901 atomic_read(&sdev
->cancel_req
)) {
1906 * check to see if we have to pause
1908 if (atomic_read(&fs_info
->scrub_pause_req
)) {
1909 /* push queued extents */
1911 wait_event(sdev
->list_wait
,
1912 atomic_read(&sdev
->in_flight
) == 0);
1913 atomic_inc(&fs_info
->scrubs_paused
);
1914 wake_up(&fs_info
->scrub_pause_wait
);
1915 mutex_lock(&fs_info
->scrub_lock
);
1916 while (atomic_read(&fs_info
->scrub_pause_req
)) {
1917 mutex_unlock(&fs_info
->scrub_lock
);
1918 wait_event(fs_info
->scrub_pause_wait
,
1919 atomic_read(&fs_info
->scrub_pause_req
) == 0);
1920 mutex_lock(&fs_info
->scrub_lock
);
1922 atomic_dec(&fs_info
->scrubs_paused
);
1923 mutex_unlock(&fs_info
->scrub_lock
);
1924 wake_up(&fs_info
->scrub_pause_wait
);
1927 ret
= btrfs_lookup_csums_range(csum_root
, logical
,
1928 logical
+ map
->stripe_len
- 1,
1929 &sdev
->csum_list
, 1);
1933 key
.objectid
= logical
;
1934 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1935 key
.offset
= (u64
)0;
1937 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1941 ret
= btrfs_previous_item(root
, path
, 0,
1942 BTRFS_EXTENT_ITEM_KEY
);
1946 /* there's no smaller item, so stick with the
1948 btrfs_release_path(path
);
1949 ret
= btrfs_search_slot(NULL
, root
, &key
,
1958 slot
= path
->slots
[0];
1959 if (slot
>= btrfs_header_nritems(l
)) {
1960 ret
= btrfs_next_leaf(root
, path
);
1968 btrfs_item_key_to_cpu(l
, &key
, slot
);
1970 if (key
.objectid
+ key
.offset
<= logical
)
1973 if (key
.objectid
>= logical
+ map
->stripe_len
)
1976 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
)
1979 extent
= btrfs_item_ptr(l
, slot
,
1980 struct btrfs_extent_item
);
1981 flags
= btrfs_extent_flags(l
, extent
);
1982 generation
= btrfs_extent_generation(l
, extent
);
1984 if (key
.objectid
< logical
&&
1985 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
1987 "btrfs scrub: tree block %llu spanning "
1988 "stripes, ignored. logical=%llu\n",
1989 (unsigned long long)key
.objectid
,
1990 (unsigned long long)logical
);
1995 * trim extent to this stripe
1997 if (key
.objectid
< logical
) {
1998 key
.offset
-= logical
- key
.objectid
;
1999 key
.objectid
= logical
;
2001 if (key
.objectid
+ key
.offset
>
2002 logical
+ map
->stripe_len
) {
2003 key
.offset
= logical
+ map
->stripe_len
-
2007 ret
= scrub_extent(sdev
, key
.objectid
, key
.offset
,
2008 key
.objectid
- logical
+ physical
,
2009 flags
, generation
, mirror_num
);
2016 btrfs_release_path(path
);
2017 logical
+= increment
;
2018 physical
+= map
->stripe_len
;
2019 spin_lock(&sdev
->stat_lock
);
2020 sdev
->stat
.last_physical
= physical
;
2021 spin_unlock(&sdev
->stat_lock
);
2023 /* push queued extents */
2027 blk_finish_plug(&plug
);
2028 btrfs_free_path(path
);
2029 return ret
< 0 ? ret
: 0;
2032 static noinline_for_stack
int scrub_chunk(struct scrub_dev
*sdev
,
2033 u64 chunk_tree
, u64 chunk_objectid
, u64 chunk_offset
, u64 length
,
2036 struct btrfs_mapping_tree
*map_tree
=
2037 &sdev
->dev
->dev_root
->fs_info
->mapping_tree
;
2038 struct map_lookup
*map
;
2039 struct extent_map
*em
;
2043 read_lock(&map_tree
->map_tree
.lock
);
2044 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
2045 read_unlock(&map_tree
->map_tree
.lock
);
2050 map
= (struct map_lookup
*)em
->bdev
;
2051 if (em
->start
!= chunk_offset
)
2054 if (em
->len
< length
)
2057 for (i
= 0; i
< map
->num_stripes
; ++i
) {
2058 if (map
->stripes
[i
].dev
== sdev
->dev
&&
2059 map
->stripes
[i
].physical
== dev_offset
) {
2060 ret
= scrub_stripe(sdev
, map
, i
, chunk_offset
, length
);
2066 free_extent_map(em
);
2071 static noinline_for_stack
2072 int scrub_enumerate_chunks(struct scrub_dev
*sdev
, u64 start
, u64 end
)
2074 struct btrfs_dev_extent
*dev_extent
= NULL
;
2075 struct btrfs_path
*path
;
2076 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
2077 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2084 struct extent_buffer
*l
;
2085 struct btrfs_key key
;
2086 struct btrfs_key found_key
;
2087 struct btrfs_block_group_cache
*cache
;
2089 path
= btrfs_alloc_path();
2094 path
->search_commit_root
= 1;
2095 path
->skip_locking
= 1;
2097 key
.objectid
= sdev
->dev
->devid
;
2099 key
.type
= BTRFS_DEV_EXTENT_KEY
;
2103 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2107 if (path
->slots
[0] >=
2108 btrfs_header_nritems(path
->nodes
[0])) {
2109 ret
= btrfs_next_leaf(root
, path
);
2116 slot
= path
->slots
[0];
2118 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
2120 if (found_key
.objectid
!= sdev
->dev
->devid
)
2123 if (btrfs_key_type(&found_key
) != BTRFS_DEV_EXTENT_KEY
)
2126 if (found_key
.offset
>= end
)
2129 if (found_key
.offset
< key
.offset
)
2132 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
2133 length
= btrfs_dev_extent_length(l
, dev_extent
);
2135 if (found_key
.offset
+ length
<= start
) {
2136 key
.offset
= found_key
.offset
+ length
;
2137 btrfs_release_path(path
);
2141 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
2142 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
2143 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
2146 * get a reference on the corresponding block group to prevent
2147 * the chunk from going away while we scrub it
2149 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2154 ret
= scrub_chunk(sdev
, chunk_tree
, chunk_objectid
,
2155 chunk_offset
, length
, found_key
.offset
);
2156 btrfs_put_block_group(cache
);
2160 key
.offset
= found_key
.offset
+ length
;
2161 btrfs_release_path(path
);
2164 btrfs_free_path(path
);
2167 * ret can still be 1 from search_slot or next_leaf,
2168 * that's not an error
2170 return ret
< 0 ? ret
: 0;
2173 static noinline_for_stack
int scrub_supers(struct scrub_dev
*sdev
)
2179 struct btrfs_device
*device
= sdev
->dev
;
2180 struct btrfs_root
*root
= device
->dev_root
;
2182 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
)
2185 gen
= root
->fs_info
->last_trans_committed
;
2187 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
2188 bytenr
= btrfs_sb_offset(i
);
2189 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
> device
->total_bytes
)
2192 ret
= scrub_pages(sdev
, bytenr
, BTRFS_SUPER_INFO_SIZE
, bytenr
,
2193 BTRFS_EXTENT_FLAG_SUPER
, gen
, i
, NULL
, 1);
2197 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
2203 * get a reference count on fs_info->scrub_workers. start worker if necessary
2205 static noinline_for_stack
int scrub_workers_get(struct btrfs_root
*root
)
2207 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2210 mutex_lock(&fs_info
->scrub_lock
);
2211 if (fs_info
->scrub_workers_refcnt
== 0) {
2212 btrfs_init_workers(&fs_info
->scrub_workers
, "scrub",
2213 fs_info
->thread_pool_size
, &fs_info
->generic_worker
);
2214 fs_info
->scrub_workers
.idle_thresh
= 4;
2215 ret
= btrfs_start_workers(&fs_info
->scrub_workers
);
2219 ++fs_info
->scrub_workers_refcnt
;
2221 mutex_unlock(&fs_info
->scrub_lock
);
2226 static noinline_for_stack
void scrub_workers_put(struct btrfs_root
*root
)
2228 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2230 mutex_lock(&fs_info
->scrub_lock
);
2231 if (--fs_info
->scrub_workers_refcnt
== 0)
2232 btrfs_stop_workers(&fs_info
->scrub_workers
);
2233 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
2234 mutex_unlock(&fs_info
->scrub_lock
);
2238 int btrfs_scrub_dev(struct btrfs_root
*root
, u64 devid
, u64 start
, u64 end
,
2239 struct btrfs_scrub_progress
*progress
, int readonly
)
2241 struct scrub_dev
*sdev
;
2242 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2244 struct btrfs_device
*dev
;
2246 if (btrfs_fs_closing(root
->fs_info
))
2250 * check some assumptions
2252 if (root
->nodesize
!= root
->leafsize
) {
2254 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2255 root
->nodesize
, root
->leafsize
);
2259 if (root
->nodesize
> BTRFS_STRIPE_LEN
) {
2261 * in this case scrub is unable to calculate the checksum
2262 * the way scrub is implemented. Do not handle this
2263 * situation at all because it won't ever happen.
2266 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2267 root
->nodesize
, BTRFS_STRIPE_LEN
);
2271 if (root
->sectorsize
!= PAGE_SIZE
) {
2272 /* not supported for data w/o checksums */
2274 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
2275 root
->sectorsize
, (unsigned long long)PAGE_SIZE
);
2279 ret
= scrub_workers_get(root
);
2283 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2284 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
2285 if (!dev
|| dev
->missing
) {
2286 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2287 scrub_workers_put(root
);
2290 mutex_lock(&fs_info
->scrub_lock
);
2292 if (!dev
->in_fs_metadata
) {
2293 mutex_unlock(&fs_info
->scrub_lock
);
2294 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2295 scrub_workers_put(root
);
2299 if (dev
->scrub_device
) {
2300 mutex_unlock(&fs_info
->scrub_lock
);
2301 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2302 scrub_workers_put(root
);
2303 return -EINPROGRESS
;
2305 sdev
= scrub_setup_dev(dev
);
2307 mutex_unlock(&fs_info
->scrub_lock
);
2308 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2309 scrub_workers_put(root
);
2310 return PTR_ERR(sdev
);
2312 sdev
->readonly
= readonly
;
2313 dev
->scrub_device
= sdev
;
2315 atomic_inc(&fs_info
->scrubs_running
);
2316 mutex_unlock(&fs_info
->scrub_lock
);
2317 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2319 down_read(&fs_info
->scrub_super_lock
);
2320 ret
= scrub_supers(sdev
);
2321 up_read(&fs_info
->scrub_super_lock
);
2324 ret
= scrub_enumerate_chunks(sdev
, start
, end
);
2326 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
2327 atomic_dec(&fs_info
->scrubs_running
);
2328 wake_up(&fs_info
->scrub_pause_wait
);
2330 wait_event(sdev
->list_wait
, atomic_read(&sdev
->fixup_cnt
) == 0);
2333 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
2335 mutex_lock(&fs_info
->scrub_lock
);
2336 dev
->scrub_device
= NULL
;
2337 mutex_unlock(&fs_info
->scrub_lock
);
2339 scrub_free_dev(sdev
);
2340 scrub_workers_put(root
);
2345 void btrfs_scrub_pause(struct btrfs_root
*root
)
2347 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2349 mutex_lock(&fs_info
->scrub_lock
);
2350 atomic_inc(&fs_info
->scrub_pause_req
);
2351 while (atomic_read(&fs_info
->scrubs_paused
) !=
2352 atomic_read(&fs_info
->scrubs_running
)) {
2353 mutex_unlock(&fs_info
->scrub_lock
);
2354 wait_event(fs_info
->scrub_pause_wait
,
2355 atomic_read(&fs_info
->scrubs_paused
) ==
2356 atomic_read(&fs_info
->scrubs_running
));
2357 mutex_lock(&fs_info
->scrub_lock
);
2359 mutex_unlock(&fs_info
->scrub_lock
);
2362 void btrfs_scrub_continue(struct btrfs_root
*root
)
2364 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2366 atomic_dec(&fs_info
->scrub_pause_req
);
2367 wake_up(&fs_info
->scrub_pause_wait
);
2370 void btrfs_scrub_pause_super(struct btrfs_root
*root
)
2372 down_write(&root
->fs_info
->scrub_super_lock
);
2375 void btrfs_scrub_continue_super(struct btrfs_root
*root
)
2377 up_write(&root
->fs_info
->scrub_super_lock
);
2380 int __btrfs_scrub_cancel(struct btrfs_fs_info
*fs_info
)
2383 mutex_lock(&fs_info
->scrub_lock
);
2384 if (!atomic_read(&fs_info
->scrubs_running
)) {
2385 mutex_unlock(&fs_info
->scrub_lock
);
2389 atomic_inc(&fs_info
->scrub_cancel_req
);
2390 while (atomic_read(&fs_info
->scrubs_running
)) {
2391 mutex_unlock(&fs_info
->scrub_lock
);
2392 wait_event(fs_info
->scrub_pause_wait
,
2393 atomic_read(&fs_info
->scrubs_running
) == 0);
2394 mutex_lock(&fs_info
->scrub_lock
);
2396 atomic_dec(&fs_info
->scrub_cancel_req
);
2397 mutex_unlock(&fs_info
->scrub_lock
);
2402 int btrfs_scrub_cancel(struct btrfs_root
*root
)
2404 return __btrfs_scrub_cancel(root
->fs_info
);
2407 int btrfs_scrub_cancel_dev(struct btrfs_root
*root
, struct btrfs_device
*dev
)
2409 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2410 struct scrub_dev
*sdev
;
2412 mutex_lock(&fs_info
->scrub_lock
);
2413 sdev
= dev
->scrub_device
;
2415 mutex_unlock(&fs_info
->scrub_lock
);
2418 atomic_inc(&sdev
->cancel_req
);
2419 while (dev
->scrub_device
) {
2420 mutex_unlock(&fs_info
->scrub_lock
);
2421 wait_event(fs_info
->scrub_pause_wait
,
2422 dev
->scrub_device
== NULL
);
2423 mutex_lock(&fs_info
->scrub_lock
);
2425 mutex_unlock(&fs_info
->scrub_lock
);
2430 int btrfs_scrub_cancel_devid(struct btrfs_root
*root
, u64 devid
)
2432 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2433 struct btrfs_device
*dev
;
2437 * we have to hold the device_list_mutex here so the device
2438 * does not go away in cancel_dev. FIXME: find a better solution
2440 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
2441 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
2443 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2446 ret
= btrfs_scrub_cancel_dev(root
, dev
);
2447 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2452 int btrfs_scrub_progress(struct btrfs_root
*root
, u64 devid
,
2453 struct btrfs_scrub_progress
*progress
)
2455 struct btrfs_device
*dev
;
2456 struct scrub_dev
*sdev
= NULL
;
2458 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2459 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
2461 sdev
= dev
->scrub_device
;
2463 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
2464 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2466 return dev
? (sdev
? 0 : -ENOTCONN
) : -ENODEV
;