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"
30 * This is only the first step towards a full-features scrub. It reads all
31 * extent and super block and verifies the checksums. In case a bad checksum
32 * is found or the extent cannot be read, good data will be written back if
35 * Future enhancements:
36 * - In case an unrepairable extent is encountered, track which files are
37 * affected and report them
38 * - In case of a read error on files with nodatasum, map the file and read
39 * the extent to trigger a writeback of the good copy
40 * - track and record media errors, throw out bad devices
41 * - add a mode to also read unallocated space
47 static void scrub_bio_end_io(struct bio
*bio
, int err
);
48 static void scrub_checksum(struct btrfs_work
*work
);
49 static int scrub_checksum_data(struct scrub_dev
*sdev
,
50 struct scrub_page
*spag
, void *buffer
);
51 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
52 struct scrub_page
*spag
, u64 logical
,
54 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
);
55 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
);
56 static void scrub_fixup_end_io(struct bio
*bio
, int err
);
57 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
59 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
);
61 #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
62 #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
65 u64 flags
; /* extent flags */
69 u8 csum
[BTRFS_CSUM_SIZE
];
74 struct scrub_dev
*sdev
;
79 struct scrub_page spag
[SCRUB_PAGES_PER_BIO
];
82 struct btrfs_work work
;
86 struct scrub_bio
*bios
[SCRUB_BIOS_PER_DEV
];
87 struct btrfs_device
*dev
;
93 wait_queue_head_t list_wait
;
95 struct list_head csum_list
;
101 struct btrfs_scrub_progress stat
;
102 spinlock_t stat_lock
;
105 struct scrub_fixup_nodatasum
{
106 struct scrub_dev
*sdev
;
108 struct btrfs_root
*root
;
109 struct btrfs_work work
;
113 struct scrub_warning
{
114 struct btrfs_path
*path
;
115 u64 extent_item_size
;
121 struct btrfs_device
*dev
;
126 static void scrub_free_csums(struct scrub_dev
*sdev
)
128 while (!list_empty(&sdev
->csum_list
)) {
129 struct btrfs_ordered_sum
*sum
;
130 sum
= list_first_entry(&sdev
->csum_list
,
131 struct btrfs_ordered_sum
, list
);
132 list_del(&sum
->list
);
137 static void scrub_free_bio(struct bio
*bio
)
140 struct page
*last_page
= NULL
;
145 for (i
= 0; i
< bio
->bi_vcnt
; ++i
) {
146 if (bio
->bi_io_vec
[i
].bv_page
== last_page
)
148 last_page
= bio
->bi_io_vec
[i
].bv_page
;
149 __free_page(last_page
);
154 static noinline_for_stack
void scrub_free_dev(struct scrub_dev
*sdev
)
161 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
162 struct scrub_bio
*sbio
= sdev
->bios
[i
];
167 scrub_free_bio(sbio
->bio
);
171 scrub_free_csums(sdev
);
175 static noinline_for_stack
176 struct scrub_dev
*scrub_setup_dev(struct btrfs_device
*dev
)
178 struct scrub_dev
*sdev
;
180 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
182 sdev
= kzalloc(sizeof(*sdev
), GFP_NOFS
);
186 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
187 struct scrub_bio
*sbio
;
189 sbio
= kzalloc(sizeof(*sbio
), GFP_NOFS
);
192 sdev
->bios
[i
] = sbio
;
197 sbio
->work
.func
= scrub_checksum
;
199 if (i
!= SCRUB_BIOS_PER_DEV
-1)
200 sdev
->bios
[i
]->next_free
= i
+ 1;
202 sdev
->bios
[i
]->next_free
= -1;
204 sdev
->first_free
= 0;
206 atomic_set(&sdev
->in_flight
, 0);
207 atomic_set(&sdev
->fixup_cnt
, 0);
208 atomic_set(&sdev
->cancel_req
, 0);
209 sdev
->csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
210 INIT_LIST_HEAD(&sdev
->csum_list
);
212 spin_lock_init(&sdev
->list_lock
);
213 spin_lock_init(&sdev
->stat_lock
);
214 init_waitqueue_head(&sdev
->list_wait
);
218 scrub_free_dev(sdev
);
219 return ERR_PTR(-ENOMEM
);
222 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
, void *ctx
)
228 struct extent_buffer
*eb
;
229 struct btrfs_inode_item
*inode_item
;
230 struct scrub_warning
*swarn
= ctx
;
231 struct btrfs_fs_info
*fs_info
= swarn
->dev
->dev_root
->fs_info
;
232 struct inode_fs_paths
*ipath
= NULL
;
233 struct btrfs_root
*local_root
;
234 struct btrfs_key root_key
;
236 root_key
.objectid
= root
;
237 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
238 root_key
.offset
= (u64
)-1;
239 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
240 if (IS_ERR(local_root
)) {
241 ret
= PTR_ERR(local_root
);
245 ret
= inode_item_info(inum
, 0, local_root
, swarn
->path
);
247 btrfs_release_path(swarn
->path
);
251 eb
= swarn
->path
->nodes
[0];
252 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
253 struct btrfs_inode_item
);
254 isize
= btrfs_inode_size(eb
, inode_item
);
255 nlink
= btrfs_inode_nlink(eb
, inode_item
);
256 btrfs_release_path(swarn
->path
);
258 ipath
= init_ipath(4096, local_root
, swarn
->path
);
259 ret
= paths_from_inode(inum
, ipath
);
265 * we deliberately ignore the bit ipath might have been too small to
266 * hold all of the paths here
268 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
269 printk(KERN_WARNING
"btrfs: %s at logical %llu on dev "
270 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
271 "length %llu, links %u (path: %s)\n", swarn
->errstr
,
272 swarn
->logical
, swarn
->dev
->name
,
273 (unsigned long long)swarn
->sector
, root
, inum
, offset
,
274 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
275 (char *)(unsigned long)ipath
->fspath
->val
[i
]);
281 printk(KERN_WARNING
"btrfs: %s at logical %llu on dev "
282 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
283 "resolving failed with ret=%d\n", swarn
->errstr
,
284 swarn
->logical
, swarn
->dev
->name
,
285 (unsigned long long)swarn
->sector
, root
, inum
, offset
, ret
);
291 static void scrub_print_warning(const char *errstr
, struct scrub_bio
*sbio
,
294 struct btrfs_device
*dev
= sbio
->sdev
->dev
;
295 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
296 struct btrfs_path
*path
;
297 struct btrfs_key found_key
;
298 struct extent_buffer
*eb
;
299 struct btrfs_extent_item
*ei
;
300 struct scrub_warning swarn
;
305 unsigned long ptr
= 0;
306 const int bufsize
= 4096;
309 path
= btrfs_alloc_path();
311 swarn
.scratch_buf
= kmalloc(bufsize
, GFP_NOFS
);
312 swarn
.msg_buf
= kmalloc(bufsize
, GFP_NOFS
);
313 swarn
.sector
= (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9;
314 swarn
.logical
= sbio
->logical
+ ix
* PAGE_SIZE
;
315 swarn
.errstr
= errstr
;
317 swarn
.msg_bufsize
= bufsize
;
318 swarn
.scratch_bufsize
= bufsize
;
320 if (!path
|| !swarn
.scratch_buf
|| !swarn
.msg_buf
)
323 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
);
327 extent_offset
= swarn
.logical
- found_key
.objectid
;
328 swarn
.extent_item_size
= found_key
.offset
;
331 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
332 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
334 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
336 ret
= tree_backref_for_extent(&ptr
, eb
, ei
, item_size
,
337 &ref_root
, &ref_level
);
338 printk(KERN_WARNING
"%s at logical %llu on dev %s, "
339 "sector %llu: metadata %s (level %d) in tree "
340 "%llu\n", errstr
, swarn
.logical
, dev
->name
,
341 (unsigned long long)swarn
.sector
,
342 ref_level
? "node" : "leaf",
343 ret
< 0 ? -1 : ref_level
,
344 ret
< 0 ? -1 : ref_root
);
348 iterate_extent_inodes(fs_info
, path
, found_key
.objectid
,
350 scrub_print_warning_inode
, &swarn
);
354 btrfs_free_path(path
);
355 kfree(swarn
.scratch_buf
);
356 kfree(swarn
.msg_buf
);
359 static int scrub_fixup_readpage(u64 inum
, u64 offset
, u64 root
, void *ctx
)
361 struct page
*page
= NULL
;
363 struct scrub_fixup_nodatasum
*fixup
= ctx
;
366 struct btrfs_key key
;
367 struct inode
*inode
= NULL
;
368 u64 end
= offset
+ PAGE_SIZE
- 1;
369 struct btrfs_root
*local_root
;
372 key
.type
= BTRFS_ROOT_ITEM_KEY
;
373 key
.offset
= (u64
)-1;
374 local_root
= btrfs_read_fs_root_no_name(fixup
->root
->fs_info
, &key
);
375 if (IS_ERR(local_root
))
376 return PTR_ERR(local_root
);
378 key
.type
= BTRFS_INODE_ITEM_KEY
;
381 inode
= btrfs_iget(fixup
->root
->fs_info
->sb
, &key
, local_root
, NULL
);
383 return PTR_ERR(inode
);
385 index
= offset
>> PAGE_CACHE_SHIFT
;
387 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
393 if (PageUptodate(page
)) {
394 struct btrfs_mapping_tree
*map_tree
;
395 if (PageDirty(page
)) {
397 * we need to write the data to the defect sector. the
398 * data that was in that sector is not in memory,
399 * because the page was modified. we must not write the
400 * modified page to that sector.
402 * TODO: what could be done here: wait for the delalloc
403 * runner to write out that page (might involve
404 * COW) and see whether the sector is still
405 * referenced afterwards.
407 * For the meantime, we'll treat this error
408 * incorrectable, although there is a chance that a
409 * later scrub will find the bad sector again and that
410 * there's no dirty page in memory, then.
415 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
416 ret
= repair_io_failure(map_tree
, offset
, PAGE_SIZE
,
417 fixup
->logical
, page
,
423 * we need to get good data first. the general readpage path
424 * will call repair_io_failure for us, we just have to make
425 * sure we read the bad mirror.
427 ret
= set_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
428 EXTENT_DAMAGED
, GFP_NOFS
);
430 /* set_extent_bits should give proper error */
437 ret
= extent_read_full_page(&BTRFS_I(inode
)->io_tree
, page
,
440 wait_on_page_locked(page
);
442 corrected
= !test_range_bit(&BTRFS_I(inode
)->io_tree
, offset
,
443 end
, EXTENT_DAMAGED
, 0, NULL
);
445 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
446 EXTENT_DAMAGED
, GFP_NOFS
);
458 if (ret
== 0 && corrected
) {
460 * we only need to call readpage for one of the inodes belonging
461 * to this extent. so make iterate_extent_inodes stop
469 static void scrub_fixup_nodatasum(struct btrfs_work
*work
)
472 struct scrub_fixup_nodatasum
*fixup
;
473 struct scrub_dev
*sdev
;
474 struct btrfs_trans_handle
*trans
= NULL
;
475 struct btrfs_fs_info
*fs_info
;
476 struct btrfs_path
*path
;
477 int uncorrectable
= 0;
479 fixup
= container_of(work
, struct scrub_fixup_nodatasum
, work
);
481 fs_info
= fixup
->root
->fs_info
;
483 path
= btrfs_alloc_path();
485 spin_lock(&sdev
->stat_lock
);
486 ++sdev
->stat
.malloc_errors
;
487 spin_unlock(&sdev
->stat_lock
);
492 trans
= btrfs_join_transaction(fixup
->root
);
499 * the idea is to trigger a regular read through the standard path. we
500 * read a page from the (failed) logical address by specifying the
501 * corresponding copynum of the failed sector. thus, that readpage is
503 * that is the point where on-the-fly error correction will kick in
504 * (once it's finished) and rewrite the failed sector if a good copy
507 ret
= iterate_inodes_from_logical(fixup
->logical
, fixup
->root
->fs_info
,
508 path
, scrub_fixup_readpage
,
516 spin_lock(&sdev
->stat_lock
);
517 ++sdev
->stat
.corrected_errors
;
518 spin_unlock(&sdev
->stat_lock
);
521 if (trans
&& !IS_ERR(trans
))
522 btrfs_end_transaction(trans
, fixup
->root
);
524 spin_lock(&sdev
->stat_lock
);
525 ++sdev
->stat
.uncorrectable_errors
;
526 spin_unlock(&sdev
->stat_lock
);
527 printk_ratelimited(KERN_ERR
"btrfs: unable to fixup "
528 "(nodatasum) error at logical %llu\n",
532 btrfs_free_path(path
);
535 /* see caller why we're pretending to be paused in the scrub counters */
536 mutex_lock(&fs_info
->scrub_lock
);
537 atomic_dec(&fs_info
->scrubs_running
);
538 atomic_dec(&fs_info
->scrubs_paused
);
539 mutex_unlock(&fs_info
->scrub_lock
);
540 atomic_dec(&sdev
->fixup_cnt
);
541 wake_up(&fs_info
->scrub_pause_wait
);
542 wake_up(&sdev
->list_wait
);
546 * scrub_recheck_error gets called when either verification of the page
547 * failed or the bio failed to read, e.g. with EIO. In the latter case,
548 * recheck_error gets called for every page in the bio, even though only
551 static int scrub_recheck_error(struct scrub_bio
*sbio
, int ix
)
553 struct scrub_dev
*sdev
= sbio
->sdev
;
554 u64 sector
= (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9;
555 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
556 DEFAULT_RATELIMIT_BURST
);
559 if (scrub_fixup_io(READ
, sbio
->sdev
->dev
->bdev
, sector
,
560 sbio
->bio
->bi_io_vec
[ix
].bv_page
) == 0) {
561 if (scrub_fixup_check(sbio
, ix
) == 0)
564 if (__ratelimit(&_rs
))
565 scrub_print_warning("i/o error", sbio
, ix
);
567 if (__ratelimit(&_rs
))
568 scrub_print_warning("checksum error", sbio
, ix
);
571 spin_lock(&sdev
->stat_lock
);
572 ++sdev
->stat
.read_errors
;
573 spin_unlock(&sdev
->stat_lock
);
575 scrub_fixup(sbio
, ix
);
579 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
)
584 u64 flags
= sbio
->spag
[ix
].flags
;
586 page
= sbio
->bio
->bi_io_vec
[ix
].bv_page
;
587 buffer
= kmap_atomic(page
, KM_USER0
);
588 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
589 ret
= scrub_checksum_data(sbio
->sdev
,
590 sbio
->spag
+ ix
, buffer
);
591 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
592 ret
= scrub_checksum_tree_block(sbio
->sdev
,
594 sbio
->logical
+ ix
* PAGE_SIZE
,
599 kunmap_atomic(buffer
, KM_USER0
);
604 static void scrub_fixup_end_io(struct bio
*bio
, int err
)
606 complete((struct completion
*)bio
->bi_private
);
609 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
)
611 struct scrub_dev
*sdev
= sbio
->sdev
;
612 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
613 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
614 struct btrfs_bio
*bbio
= NULL
;
615 struct scrub_fixup_nodatasum
*fixup
;
616 u64 logical
= sbio
->logical
+ ix
* PAGE_SIZE
;
620 DECLARE_COMPLETION_ONSTACK(complete
);
622 if ((sbio
->spag
[ix
].flags
& BTRFS_EXTENT_FLAG_DATA
) &&
623 (sbio
->spag
[ix
].have_csum
== 0)) {
624 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
628 fixup
->logical
= logical
;
629 fixup
->root
= fs_info
->extent_root
;
630 fixup
->mirror_num
= sbio
->spag
[ix
].mirror_num
;
632 * increment scrubs_running to prevent cancel requests from
633 * completing as long as a fixup worker is running. we must also
634 * increment scrubs_paused to prevent deadlocking on pause
635 * requests used for transactions commits (as the worker uses a
636 * transaction context). it is safe to regard the fixup worker
637 * as paused for all matters practical. effectively, we only
638 * avoid cancellation requests from completing.
640 mutex_lock(&fs_info
->scrub_lock
);
641 atomic_inc(&fs_info
->scrubs_running
);
642 atomic_inc(&fs_info
->scrubs_paused
);
643 mutex_unlock(&fs_info
->scrub_lock
);
644 atomic_inc(&sdev
->fixup_cnt
);
645 fixup
->work
.func
= scrub_fixup_nodatasum
;
646 btrfs_queue_worker(&fs_info
->scrub_workers
, &fixup
->work
);
651 ret
= btrfs_map_block(map_tree
, REQ_WRITE
, logical
, &length
,
653 if (ret
|| !bbio
|| length
< PAGE_SIZE
) {
655 "scrub_fixup: btrfs_map_block failed us for %llu\n",
656 (unsigned long long)logical
);
662 if (bbio
->num_stripes
== 1)
663 /* there aren't any replicas */
667 * first find a good copy
669 for (i
= 0; i
< bbio
->num_stripes
; ++i
) {
670 if (i
+ 1 == sbio
->spag
[ix
].mirror_num
)
673 if (scrub_fixup_io(READ
, bbio
->stripes
[i
].dev
->bdev
,
674 bbio
->stripes
[i
].physical
>> 9,
675 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
676 /* I/O-error, this is not a good copy */
680 if (scrub_fixup_check(sbio
, ix
) == 0)
683 if (i
== bbio
->num_stripes
)
686 if (!sdev
->readonly
) {
688 * bi_io_vec[ix].bv_page now contains good data, write it back
690 if (scrub_fixup_io(WRITE
, sdev
->dev
->bdev
,
691 (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9,
692 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
693 /* I/O-error, writeback failed, give up */
699 spin_lock(&sdev
->stat_lock
);
700 ++sdev
->stat
.corrected_errors
;
701 spin_unlock(&sdev
->stat_lock
);
703 printk_ratelimited(KERN_ERR
"btrfs: fixed up error at logical %llu\n",
704 (unsigned long long)logical
);
709 spin_lock(&sdev
->stat_lock
);
710 ++sdev
->stat
.uncorrectable_errors
;
711 spin_unlock(&sdev
->stat_lock
);
713 printk_ratelimited(KERN_ERR
"btrfs: unable to fixup (regular) error at "
714 "logical %llu\n", (unsigned long long)logical
);
717 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
720 struct bio
*bio
= NULL
;
722 DECLARE_COMPLETION_ONSTACK(complete
);
724 bio
= bio_alloc(GFP_NOFS
, 1);
726 bio
->bi_sector
= sector
;
727 bio_add_page(bio
, page
, PAGE_SIZE
, 0);
728 bio
->bi_end_io
= scrub_fixup_end_io
;
729 bio
->bi_private
= &complete
;
732 /* this will also unplug the queue */
733 wait_for_completion(&complete
);
735 ret
= !test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
740 static void scrub_bio_end_io(struct bio
*bio
, int err
)
742 struct scrub_bio
*sbio
= bio
->bi_private
;
743 struct scrub_dev
*sdev
= sbio
->sdev
;
744 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
749 btrfs_queue_worker(&fs_info
->scrub_workers
, &sbio
->work
);
752 static void scrub_checksum(struct btrfs_work
*work
)
754 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
755 struct scrub_dev
*sdev
= sbio
->sdev
;
765 for (i
= 0; i
< sbio
->count
; ++i
)
766 ret
|= scrub_recheck_error(sbio
, i
);
768 spin_lock(&sdev
->stat_lock
);
769 ++sdev
->stat
.unverified_errors
;
770 spin_unlock(&sdev
->stat_lock
);
773 sbio
->bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
774 sbio
->bio
->bi_flags
|= 1 << BIO_UPTODATE
;
775 sbio
->bio
->bi_phys_segments
= 0;
776 sbio
->bio
->bi_idx
= 0;
778 for (i
= 0; i
< sbio
->count
; i
++) {
780 bi
= &sbio
->bio
->bi_io_vec
[i
];
782 bi
->bv_len
= PAGE_SIZE
;
786 for (i
= 0; i
< sbio
->count
; ++i
) {
787 page
= sbio
->bio
->bi_io_vec
[i
].bv_page
;
788 buffer
= kmap_atomic(page
, KM_USER0
);
789 flags
= sbio
->spag
[i
].flags
;
790 logical
= sbio
->logical
+ i
* PAGE_SIZE
;
792 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
793 ret
= scrub_checksum_data(sdev
, sbio
->spag
+ i
, buffer
);
794 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
795 ret
= scrub_checksum_tree_block(sdev
, sbio
->spag
+ i
,
797 } else if (flags
& BTRFS_EXTENT_FLAG_SUPER
) {
799 (void)scrub_checksum_super(sbio
, buffer
);
803 kunmap_atomic(buffer
, KM_USER0
);
805 ret
= scrub_recheck_error(sbio
, i
);
807 spin_lock(&sdev
->stat_lock
);
808 ++sdev
->stat
.unverified_errors
;
809 spin_unlock(&sdev
->stat_lock
);
815 scrub_free_bio(sbio
->bio
);
817 spin_lock(&sdev
->list_lock
);
818 sbio
->next_free
= sdev
->first_free
;
819 sdev
->first_free
= sbio
->index
;
820 spin_unlock(&sdev
->list_lock
);
821 atomic_dec(&sdev
->in_flight
);
822 wake_up(&sdev
->list_wait
);
825 static int scrub_checksum_data(struct scrub_dev
*sdev
,
826 struct scrub_page
*spag
, void *buffer
)
828 u8 csum
[BTRFS_CSUM_SIZE
];
831 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
833 if (!spag
->have_csum
)
836 crc
= btrfs_csum_data(root
, buffer
, crc
, PAGE_SIZE
);
837 btrfs_csum_final(crc
, csum
);
838 if (memcmp(csum
, spag
->csum
, sdev
->csum_size
))
841 spin_lock(&sdev
->stat_lock
);
842 ++sdev
->stat
.data_extents_scrubbed
;
843 sdev
->stat
.data_bytes_scrubbed
+= PAGE_SIZE
;
845 ++sdev
->stat
.csum_errors
;
846 spin_unlock(&sdev
->stat_lock
);
851 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
852 struct scrub_page
*spag
, u64 logical
,
855 struct btrfs_header
*h
;
856 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
857 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
858 u8 csum
[BTRFS_CSUM_SIZE
];
864 * we don't use the getter functions here, as we
865 * a) don't have an extent buffer and
866 * b) the page is already kmapped
868 h
= (struct btrfs_header
*)buffer
;
870 if (logical
!= le64_to_cpu(h
->bytenr
))
873 if (spag
->generation
!= le64_to_cpu(h
->generation
))
876 if (memcmp(h
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
879 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
883 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
884 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
885 btrfs_csum_final(crc
, csum
);
886 if (memcmp(csum
, h
->csum
, sdev
->csum_size
))
889 spin_lock(&sdev
->stat_lock
);
890 ++sdev
->stat
.tree_extents_scrubbed
;
891 sdev
->stat
.tree_bytes_scrubbed
+= PAGE_SIZE
;
893 ++sdev
->stat
.csum_errors
;
895 ++sdev
->stat
.verify_errors
;
896 spin_unlock(&sdev
->stat_lock
);
898 return fail
|| crc_fail
;
901 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
)
903 struct btrfs_super_block
*s
;
905 struct scrub_dev
*sdev
= sbio
->sdev
;
906 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
907 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
908 u8 csum
[BTRFS_CSUM_SIZE
];
912 s
= (struct btrfs_super_block
*)buffer
;
913 logical
= sbio
->logical
;
915 if (logical
!= le64_to_cpu(s
->bytenr
))
918 if (sbio
->spag
[0].generation
!= le64_to_cpu(s
->generation
))
921 if (memcmp(s
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
924 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
925 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
926 btrfs_csum_final(crc
, csum
);
927 if (memcmp(csum
, s
->csum
, sbio
->sdev
->csum_size
))
932 * if we find an error in a super block, we just report it.
933 * They will get written with the next transaction commit
936 spin_lock(&sdev
->stat_lock
);
937 ++sdev
->stat
.super_errors
;
938 spin_unlock(&sdev
->stat_lock
);
944 static int scrub_submit(struct scrub_dev
*sdev
)
946 struct scrub_bio
*sbio
;
948 if (sdev
->curr
== -1)
951 sbio
= sdev
->bios
[sdev
->curr
];
954 atomic_inc(&sdev
->in_flight
);
956 submit_bio(READ
, sbio
->bio
);
961 static int scrub_page(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
962 u64 physical
, u64 flags
, u64 gen
, int mirror_num
,
965 struct scrub_bio
*sbio
;
971 * grab a fresh bio or wait for one to become available
973 while (sdev
->curr
== -1) {
974 spin_lock(&sdev
->list_lock
);
975 sdev
->curr
= sdev
->first_free
;
976 if (sdev
->curr
!= -1) {
977 sdev
->first_free
= sdev
->bios
[sdev
->curr
]->next_free
;
978 sdev
->bios
[sdev
->curr
]->next_free
= -1;
979 sdev
->bios
[sdev
->curr
]->count
= 0;
980 spin_unlock(&sdev
->list_lock
);
982 spin_unlock(&sdev
->list_lock
);
983 wait_event(sdev
->list_wait
, sdev
->first_free
!= -1);
986 sbio
= sdev
->bios
[sdev
->curr
];
987 if (sbio
->count
== 0) {
990 sbio
->physical
= physical
;
991 sbio
->logical
= logical
;
992 bio
= bio_alloc(GFP_NOFS
, SCRUB_PAGES_PER_BIO
);
996 bio
->bi_private
= sbio
;
997 bio
->bi_end_io
= scrub_bio_end_io
;
998 bio
->bi_bdev
= sdev
->dev
->bdev
;
999 bio
->bi_sector
= sbio
->physical
>> 9;
1002 } else if (sbio
->physical
+ sbio
->count
* PAGE_SIZE
!= physical
||
1003 sbio
->logical
+ sbio
->count
* PAGE_SIZE
!= logical
) {
1004 ret
= scrub_submit(sdev
);
1009 sbio
->spag
[sbio
->count
].flags
= flags
;
1010 sbio
->spag
[sbio
->count
].generation
= gen
;
1011 sbio
->spag
[sbio
->count
].have_csum
= 0;
1012 sbio
->spag
[sbio
->count
].mirror_num
= mirror_num
;
1014 page
= alloc_page(GFP_NOFS
);
1018 ret
= bio_add_page(sbio
->bio
, page
, PAGE_SIZE
, 0);
1021 ret
= scrub_submit(sdev
);
1028 sbio
->spag
[sbio
->count
].have_csum
= 1;
1029 memcpy(sbio
->spag
[sbio
->count
].csum
, csum
, sdev
->csum_size
);
1032 if (sbio
->count
== SCRUB_PAGES_PER_BIO
|| force
) {
1035 ret
= scrub_submit(sdev
);
1043 static int scrub_find_csum(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1046 struct btrfs_ordered_sum
*sum
= NULL
;
1049 unsigned long num_sectors
;
1050 u32 sectorsize
= sdev
->dev
->dev_root
->sectorsize
;
1052 while (!list_empty(&sdev
->csum_list
)) {
1053 sum
= list_first_entry(&sdev
->csum_list
,
1054 struct btrfs_ordered_sum
, list
);
1055 if (sum
->bytenr
> logical
)
1057 if (sum
->bytenr
+ sum
->len
> logical
)
1060 ++sdev
->stat
.csum_discards
;
1061 list_del(&sum
->list
);
1068 num_sectors
= sum
->len
/ sectorsize
;
1069 for (i
= 0; i
< num_sectors
; ++i
) {
1070 if (sum
->sums
[i
].bytenr
== logical
) {
1071 memcpy(csum
, &sum
->sums
[i
].sum
, sdev
->csum_size
);
1076 if (ret
&& i
== num_sectors
- 1) {
1077 list_del(&sum
->list
);
1083 /* scrub extent tries to collect up to 64 kB for each bio */
1084 static int scrub_extent(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1085 u64 physical
, u64 flags
, u64 gen
, int mirror_num
)
1088 u8 csum
[BTRFS_CSUM_SIZE
];
1091 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1094 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
1095 /* push csums to sbio */
1096 have_csum
= scrub_find_csum(sdev
, logical
, l
, csum
);
1098 ++sdev
->stat
.no_csum
;
1100 ret
= scrub_page(sdev
, logical
, l
, physical
, flags
, gen
,
1101 mirror_num
, have_csum
? csum
: NULL
, 0);
1111 static noinline_for_stack
int scrub_stripe(struct scrub_dev
*sdev
,
1112 struct map_lookup
*map
, int num
, u64 base
, u64 length
)
1114 struct btrfs_path
*path
;
1115 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
1116 struct btrfs_root
*root
= fs_info
->extent_root
;
1117 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
1118 struct btrfs_extent_item
*extent
;
1119 struct blk_plug plug
;
1125 struct extent_buffer
*l
;
1126 struct btrfs_key key
;
1131 struct reada_control
*reada1
;
1132 struct reada_control
*reada2
;
1133 struct btrfs_key key_start
;
1134 struct btrfs_key key_end
;
1136 u64 increment
= map
->stripe_len
;
1141 do_div(nstripes
, map
->stripe_len
);
1142 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1143 offset
= map
->stripe_len
* num
;
1144 increment
= map
->stripe_len
* map
->num_stripes
;
1146 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1147 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1148 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
1149 increment
= map
->stripe_len
* factor
;
1150 mirror_num
= num
% map
->sub_stripes
+ 1;
1151 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1152 increment
= map
->stripe_len
;
1153 mirror_num
= num
% map
->num_stripes
+ 1;
1154 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1155 increment
= map
->stripe_len
;
1156 mirror_num
= num
% map
->num_stripes
+ 1;
1158 increment
= map
->stripe_len
;
1162 path
= btrfs_alloc_path();
1166 path
->search_commit_root
= 1;
1167 path
->skip_locking
= 1;
1170 * trigger the readahead for extent tree csum tree and wait for
1171 * completion. During readahead, the scrub is officially paused
1172 * to not hold off transaction commits
1174 logical
= base
+ offset
;
1176 wait_event(sdev
->list_wait
,
1177 atomic_read(&sdev
->in_flight
) == 0);
1178 atomic_inc(&fs_info
->scrubs_paused
);
1179 wake_up(&fs_info
->scrub_pause_wait
);
1181 /* FIXME it might be better to start readahead at commit root */
1182 key_start
.objectid
= logical
;
1183 key_start
.type
= BTRFS_EXTENT_ITEM_KEY
;
1184 key_start
.offset
= (u64
)0;
1185 key_end
.objectid
= base
+ offset
+ nstripes
* increment
;
1186 key_end
.type
= BTRFS_EXTENT_ITEM_KEY
;
1187 key_end
.offset
= (u64
)0;
1188 reada1
= btrfs_reada_add(root
, &key_start
, &key_end
);
1190 key_start
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
1191 key_start
.type
= BTRFS_EXTENT_CSUM_KEY
;
1192 key_start
.offset
= logical
;
1193 key_end
.objectid
= BTRFS_EXTENT_CSUM_OBJECTID
;
1194 key_end
.type
= BTRFS_EXTENT_CSUM_KEY
;
1195 key_end
.offset
= base
+ offset
+ nstripes
* increment
;
1196 reada2
= btrfs_reada_add(csum_root
, &key_start
, &key_end
);
1198 if (!IS_ERR(reada1
))
1199 btrfs_reada_wait(reada1
);
1200 if (!IS_ERR(reada2
))
1201 btrfs_reada_wait(reada2
);
1203 mutex_lock(&fs_info
->scrub_lock
);
1204 while (atomic_read(&fs_info
->scrub_pause_req
)) {
1205 mutex_unlock(&fs_info
->scrub_lock
);
1206 wait_event(fs_info
->scrub_pause_wait
,
1207 atomic_read(&fs_info
->scrub_pause_req
) == 0);
1208 mutex_lock(&fs_info
->scrub_lock
);
1210 atomic_dec(&fs_info
->scrubs_paused
);
1211 mutex_unlock(&fs_info
->scrub_lock
);
1212 wake_up(&fs_info
->scrub_pause_wait
);
1215 * collect all data csums for the stripe to avoid seeking during
1216 * the scrub. This might currently (crc32) end up to be about 1MB
1218 blk_start_plug(&plug
);
1221 * now find all extents for each stripe and scrub them
1223 logical
= base
+ offset
;
1224 physical
= map
->stripes
[num
].physical
;
1226 for (i
= 0; i
< nstripes
; ++i
) {
1230 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
1231 atomic_read(&sdev
->cancel_req
)) {
1236 * check to see if we have to pause
1238 if (atomic_read(&fs_info
->scrub_pause_req
)) {
1239 /* push queued extents */
1241 wait_event(sdev
->list_wait
,
1242 atomic_read(&sdev
->in_flight
) == 0);
1243 atomic_inc(&fs_info
->scrubs_paused
);
1244 wake_up(&fs_info
->scrub_pause_wait
);
1245 mutex_lock(&fs_info
->scrub_lock
);
1246 while (atomic_read(&fs_info
->scrub_pause_req
)) {
1247 mutex_unlock(&fs_info
->scrub_lock
);
1248 wait_event(fs_info
->scrub_pause_wait
,
1249 atomic_read(&fs_info
->scrub_pause_req
) == 0);
1250 mutex_lock(&fs_info
->scrub_lock
);
1252 atomic_dec(&fs_info
->scrubs_paused
);
1253 mutex_unlock(&fs_info
->scrub_lock
);
1254 wake_up(&fs_info
->scrub_pause_wait
);
1257 ret
= btrfs_lookup_csums_range(csum_root
, logical
,
1258 logical
+ map
->stripe_len
- 1,
1259 &sdev
->csum_list
, 1);
1263 key
.objectid
= logical
;
1264 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1265 key
.offset
= (u64
)0;
1267 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1271 ret
= btrfs_previous_item(root
, path
, 0,
1272 BTRFS_EXTENT_ITEM_KEY
);
1276 /* there's no smaller item, so stick with the
1278 btrfs_release_path(path
);
1279 ret
= btrfs_search_slot(NULL
, root
, &key
,
1288 slot
= path
->slots
[0];
1289 if (slot
>= btrfs_header_nritems(l
)) {
1290 ret
= btrfs_next_leaf(root
, path
);
1298 btrfs_item_key_to_cpu(l
, &key
, slot
);
1300 if (key
.objectid
+ key
.offset
<= logical
)
1303 if (key
.objectid
>= logical
+ map
->stripe_len
)
1306 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
)
1309 extent
= btrfs_item_ptr(l
, slot
,
1310 struct btrfs_extent_item
);
1311 flags
= btrfs_extent_flags(l
, extent
);
1312 generation
= btrfs_extent_generation(l
, extent
);
1314 if (key
.objectid
< logical
&&
1315 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
1317 "btrfs scrub: tree block %llu spanning "
1318 "stripes, ignored. logical=%llu\n",
1319 (unsigned long long)key
.objectid
,
1320 (unsigned long long)logical
);
1325 * trim extent to this stripe
1327 if (key
.objectid
< logical
) {
1328 key
.offset
-= logical
- key
.objectid
;
1329 key
.objectid
= logical
;
1331 if (key
.objectid
+ key
.offset
>
1332 logical
+ map
->stripe_len
) {
1333 key
.offset
= logical
+ map
->stripe_len
-
1337 ret
= scrub_extent(sdev
, key
.objectid
, key
.offset
,
1338 key
.objectid
- logical
+ physical
,
1339 flags
, generation
, mirror_num
);
1346 btrfs_release_path(path
);
1347 logical
+= increment
;
1348 physical
+= map
->stripe_len
;
1349 spin_lock(&sdev
->stat_lock
);
1350 sdev
->stat
.last_physical
= physical
;
1351 spin_unlock(&sdev
->stat_lock
);
1353 /* push queued extents */
1357 blk_finish_plug(&plug
);
1358 btrfs_free_path(path
);
1359 return ret
< 0 ? ret
: 0;
1362 static noinline_for_stack
int scrub_chunk(struct scrub_dev
*sdev
,
1363 u64 chunk_tree
, u64 chunk_objectid
, u64 chunk_offset
, u64 length
)
1365 struct btrfs_mapping_tree
*map_tree
=
1366 &sdev
->dev
->dev_root
->fs_info
->mapping_tree
;
1367 struct map_lookup
*map
;
1368 struct extent_map
*em
;
1372 read_lock(&map_tree
->map_tree
.lock
);
1373 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
1374 read_unlock(&map_tree
->map_tree
.lock
);
1379 map
= (struct map_lookup
*)em
->bdev
;
1380 if (em
->start
!= chunk_offset
)
1383 if (em
->len
< length
)
1386 for (i
= 0; i
< map
->num_stripes
; ++i
) {
1387 if (map
->stripes
[i
].dev
== sdev
->dev
) {
1388 ret
= scrub_stripe(sdev
, map
, i
, chunk_offset
, length
);
1394 free_extent_map(em
);
1399 static noinline_for_stack
1400 int scrub_enumerate_chunks(struct scrub_dev
*sdev
, u64 start
, u64 end
)
1402 struct btrfs_dev_extent
*dev_extent
= NULL
;
1403 struct btrfs_path
*path
;
1404 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1405 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1412 struct extent_buffer
*l
;
1413 struct btrfs_key key
;
1414 struct btrfs_key found_key
;
1415 struct btrfs_block_group_cache
*cache
;
1417 path
= btrfs_alloc_path();
1422 path
->search_commit_root
= 1;
1423 path
->skip_locking
= 1;
1425 key
.objectid
= sdev
->dev
->devid
;
1427 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1431 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1435 if (path
->slots
[0] >=
1436 btrfs_header_nritems(path
->nodes
[0])) {
1437 ret
= btrfs_next_leaf(root
, path
);
1444 slot
= path
->slots
[0];
1446 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
1448 if (found_key
.objectid
!= sdev
->dev
->devid
)
1451 if (btrfs_key_type(&found_key
) != BTRFS_DEV_EXTENT_KEY
)
1454 if (found_key
.offset
>= end
)
1457 if (found_key
.offset
< key
.offset
)
1460 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1461 length
= btrfs_dev_extent_length(l
, dev_extent
);
1463 if (found_key
.offset
+ length
<= start
) {
1464 key
.offset
= found_key
.offset
+ length
;
1465 btrfs_release_path(path
);
1469 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1470 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1471 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1474 * get a reference on the corresponding block group to prevent
1475 * the chunk from going away while we scrub it
1477 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
1482 ret
= scrub_chunk(sdev
, chunk_tree
, chunk_objectid
,
1483 chunk_offset
, length
);
1484 btrfs_put_block_group(cache
);
1488 key
.offset
= found_key
.offset
+ length
;
1489 btrfs_release_path(path
);
1492 btrfs_free_path(path
);
1495 * ret can still be 1 from search_slot or next_leaf,
1496 * that's not an error
1498 return ret
< 0 ? ret
: 0;
1501 static noinline_for_stack
int scrub_supers(struct scrub_dev
*sdev
)
1507 struct btrfs_device
*device
= sdev
->dev
;
1508 struct btrfs_root
*root
= device
->dev_root
;
1510 gen
= root
->fs_info
->last_trans_committed
;
1512 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1513 bytenr
= btrfs_sb_offset(i
);
1514 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>= device
->total_bytes
)
1517 ret
= scrub_page(sdev
, bytenr
, PAGE_SIZE
, bytenr
,
1518 BTRFS_EXTENT_FLAG_SUPER
, gen
, i
, NULL
, 1);
1522 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1528 * get a reference count on fs_info->scrub_workers. start worker if necessary
1530 static noinline_for_stack
int scrub_workers_get(struct btrfs_root
*root
)
1532 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1534 mutex_lock(&fs_info
->scrub_lock
);
1535 if (fs_info
->scrub_workers_refcnt
== 0) {
1536 btrfs_init_workers(&fs_info
->scrub_workers
, "scrub",
1537 fs_info
->thread_pool_size
, &fs_info
->generic_worker
);
1538 fs_info
->scrub_workers
.idle_thresh
= 4;
1539 btrfs_start_workers(&fs_info
->scrub_workers
, 1);
1541 ++fs_info
->scrub_workers_refcnt
;
1542 mutex_unlock(&fs_info
->scrub_lock
);
1547 static noinline_for_stack
void scrub_workers_put(struct btrfs_root
*root
)
1549 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1551 mutex_lock(&fs_info
->scrub_lock
);
1552 if (--fs_info
->scrub_workers_refcnt
== 0)
1553 btrfs_stop_workers(&fs_info
->scrub_workers
);
1554 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
1555 mutex_unlock(&fs_info
->scrub_lock
);
1559 int btrfs_scrub_dev(struct btrfs_root
*root
, u64 devid
, u64 start
, u64 end
,
1560 struct btrfs_scrub_progress
*progress
, int readonly
)
1562 struct scrub_dev
*sdev
;
1563 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1565 struct btrfs_device
*dev
;
1567 if (btrfs_fs_closing(root
->fs_info
))
1571 * check some assumptions
1573 if (root
->sectorsize
!= PAGE_SIZE
||
1574 root
->sectorsize
!= root
->leafsize
||
1575 root
->sectorsize
!= root
->nodesize
) {
1576 printk(KERN_ERR
"btrfs_scrub: size assumptions fail\n");
1580 ret
= scrub_workers_get(root
);
1584 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1585 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1586 if (!dev
|| dev
->missing
) {
1587 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1588 scrub_workers_put(root
);
1591 mutex_lock(&fs_info
->scrub_lock
);
1593 if (!dev
->in_fs_metadata
) {
1594 mutex_unlock(&fs_info
->scrub_lock
);
1595 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1596 scrub_workers_put(root
);
1600 if (dev
->scrub_device
) {
1601 mutex_unlock(&fs_info
->scrub_lock
);
1602 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1603 scrub_workers_put(root
);
1604 return -EINPROGRESS
;
1606 sdev
= scrub_setup_dev(dev
);
1608 mutex_unlock(&fs_info
->scrub_lock
);
1609 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1610 scrub_workers_put(root
);
1611 return PTR_ERR(sdev
);
1613 sdev
->readonly
= readonly
;
1614 dev
->scrub_device
= sdev
;
1616 atomic_inc(&fs_info
->scrubs_running
);
1617 mutex_unlock(&fs_info
->scrub_lock
);
1618 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1620 down_read(&fs_info
->scrub_super_lock
);
1621 ret
= scrub_supers(sdev
);
1622 up_read(&fs_info
->scrub_super_lock
);
1625 ret
= scrub_enumerate_chunks(sdev
, start
, end
);
1627 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1628 atomic_dec(&fs_info
->scrubs_running
);
1629 wake_up(&fs_info
->scrub_pause_wait
);
1631 wait_event(sdev
->list_wait
, atomic_read(&sdev
->fixup_cnt
) == 0);
1634 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1636 mutex_lock(&fs_info
->scrub_lock
);
1637 dev
->scrub_device
= NULL
;
1638 mutex_unlock(&fs_info
->scrub_lock
);
1640 scrub_free_dev(sdev
);
1641 scrub_workers_put(root
);
1646 int btrfs_scrub_pause(struct btrfs_root
*root
)
1648 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1650 mutex_lock(&fs_info
->scrub_lock
);
1651 atomic_inc(&fs_info
->scrub_pause_req
);
1652 while (atomic_read(&fs_info
->scrubs_paused
) !=
1653 atomic_read(&fs_info
->scrubs_running
)) {
1654 mutex_unlock(&fs_info
->scrub_lock
);
1655 wait_event(fs_info
->scrub_pause_wait
,
1656 atomic_read(&fs_info
->scrubs_paused
) ==
1657 atomic_read(&fs_info
->scrubs_running
));
1658 mutex_lock(&fs_info
->scrub_lock
);
1660 mutex_unlock(&fs_info
->scrub_lock
);
1665 int btrfs_scrub_continue(struct btrfs_root
*root
)
1667 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1669 atomic_dec(&fs_info
->scrub_pause_req
);
1670 wake_up(&fs_info
->scrub_pause_wait
);
1674 int btrfs_scrub_pause_super(struct btrfs_root
*root
)
1676 down_write(&root
->fs_info
->scrub_super_lock
);
1680 int btrfs_scrub_continue_super(struct btrfs_root
*root
)
1682 up_write(&root
->fs_info
->scrub_super_lock
);
1686 int btrfs_scrub_cancel(struct btrfs_root
*root
)
1688 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1690 mutex_lock(&fs_info
->scrub_lock
);
1691 if (!atomic_read(&fs_info
->scrubs_running
)) {
1692 mutex_unlock(&fs_info
->scrub_lock
);
1696 atomic_inc(&fs_info
->scrub_cancel_req
);
1697 while (atomic_read(&fs_info
->scrubs_running
)) {
1698 mutex_unlock(&fs_info
->scrub_lock
);
1699 wait_event(fs_info
->scrub_pause_wait
,
1700 atomic_read(&fs_info
->scrubs_running
) == 0);
1701 mutex_lock(&fs_info
->scrub_lock
);
1703 atomic_dec(&fs_info
->scrub_cancel_req
);
1704 mutex_unlock(&fs_info
->scrub_lock
);
1709 int btrfs_scrub_cancel_dev(struct btrfs_root
*root
, struct btrfs_device
*dev
)
1711 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1712 struct scrub_dev
*sdev
;
1714 mutex_lock(&fs_info
->scrub_lock
);
1715 sdev
= dev
->scrub_device
;
1717 mutex_unlock(&fs_info
->scrub_lock
);
1720 atomic_inc(&sdev
->cancel_req
);
1721 while (dev
->scrub_device
) {
1722 mutex_unlock(&fs_info
->scrub_lock
);
1723 wait_event(fs_info
->scrub_pause_wait
,
1724 dev
->scrub_device
== NULL
);
1725 mutex_lock(&fs_info
->scrub_lock
);
1727 mutex_unlock(&fs_info
->scrub_lock
);
1731 int btrfs_scrub_cancel_devid(struct btrfs_root
*root
, u64 devid
)
1733 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1734 struct btrfs_device
*dev
;
1738 * we have to hold the device_list_mutex here so the device
1739 * does not go away in cancel_dev. FIXME: find a better solution
1741 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1742 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1744 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1747 ret
= btrfs_scrub_cancel_dev(root
, dev
);
1748 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1753 int btrfs_scrub_progress(struct btrfs_root
*root
, u64 devid
,
1754 struct btrfs_scrub_progress
*progress
)
1756 struct btrfs_device
*dev
;
1757 struct scrub_dev
*sdev
= NULL
;
1759 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1760 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1762 sdev
= dev
->scrub_device
;
1764 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1765 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1767 return dev
? (sdev
? 0 : -ENOTCONN
) : -ENODEV
;