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 * - To enhance the performance, better read-ahead strategies for the
37 * extent-tree can be employed.
38 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
40 * - In case of a read error on files with nodatasum, map the file and read
41 * the extent to trigger a writeback of the good copy
42 * - track and record media errors, throw out bad devices
43 * - add a mode to also read unallocated space
44 * - make the prefetch cancellable
50 static void scrub_bio_end_io(struct bio
*bio
, int err
);
51 static void scrub_checksum(struct btrfs_work
*work
);
52 static int scrub_checksum_data(struct scrub_dev
*sdev
,
53 struct scrub_page
*spag
, void *buffer
);
54 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
55 struct scrub_page
*spag
, u64 logical
,
57 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
);
58 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
);
59 static void scrub_fixup_end_io(struct bio
*bio
, int err
);
60 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
62 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
);
64 #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
65 #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
68 u64 flags
; /* extent flags */
72 u8 csum
[BTRFS_CSUM_SIZE
];
77 struct scrub_dev
*sdev
;
82 struct scrub_page spag
[SCRUB_PAGES_PER_BIO
];
85 struct btrfs_work work
;
89 struct scrub_bio
*bios
[SCRUB_BIOS_PER_DEV
];
90 struct btrfs_device
*dev
;
96 wait_queue_head_t list_wait
;
98 struct list_head csum_list
;
104 struct btrfs_scrub_progress stat
;
105 spinlock_t stat_lock
;
108 struct scrub_fixup_nodatasum
{
109 struct scrub_dev
*sdev
;
111 struct btrfs_root
*root
;
112 struct btrfs_work work
;
116 struct scrub_warning
{
117 struct btrfs_path
*path
;
118 u64 extent_item_size
;
124 struct btrfs_device
*dev
;
129 static void scrub_free_csums(struct scrub_dev
*sdev
)
131 while (!list_empty(&sdev
->csum_list
)) {
132 struct btrfs_ordered_sum
*sum
;
133 sum
= list_first_entry(&sdev
->csum_list
,
134 struct btrfs_ordered_sum
, list
);
135 list_del(&sum
->list
);
140 static void scrub_free_bio(struct bio
*bio
)
143 struct page
*last_page
= NULL
;
148 for (i
= 0; i
< bio
->bi_vcnt
; ++i
) {
149 if (bio
->bi_io_vec
[i
].bv_page
== last_page
)
151 last_page
= bio
->bi_io_vec
[i
].bv_page
;
152 __free_page(last_page
);
157 static noinline_for_stack
void scrub_free_dev(struct scrub_dev
*sdev
)
164 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
165 struct scrub_bio
*sbio
= sdev
->bios
[i
];
170 scrub_free_bio(sbio
->bio
);
174 scrub_free_csums(sdev
);
178 static noinline_for_stack
179 struct scrub_dev
*scrub_setup_dev(struct btrfs_device
*dev
)
181 struct scrub_dev
*sdev
;
183 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
185 sdev
= kzalloc(sizeof(*sdev
), GFP_NOFS
);
189 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
190 struct scrub_bio
*sbio
;
192 sbio
= kzalloc(sizeof(*sbio
), GFP_NOFS
);
195 sdev
->bios
[i
] = sbio
;
200 sbio
->work
.func
= scrub_checksum
;
202 if (i
!= SCRUB_BIOS_PER_DEV
-1)
203 sdev
->bios
[i
]->next_free
= i
+ 1;
205 sdev
->bios
[i
]->next_free
= -1;
207 sdev
->first_free
= 0;
209 atomic_set(&sdev
->in_flight
, 0);
210 atomic_set(&sdev
->fixup_cnt
, 0);
211 atomic_set(&sdev
->cancel_req
, 0);
212 sdev
->csum_size
= btrfs_super_csum_size(&fs_info
->super_copy
);
213 INIT_LIST_HEAD(&sdev
->csum_list
);
215 spin_lock_init(&sdev
->list_lock
);
216 spin_lock_init(&sdev
->stat_lock
);
217 init_waitqueue_head(&sdev
->list_wait
);
221 scrub_free_dev(sdev
);
222 return ERR_PTR(-ENOMEM
);
225 static int scrub_print_warning_inode(u64 inum
, u64 offset
, u64 root
, void *ctx
)
231 struct extent_buffer
*eb
;
232 struct btrfs_inode_item
*inode_item
;
233 struct scrub_warning
*swarn
= ctx
;
234 struct btrfs_fs_info
*fs_info
= swarn
->dev
->dev_root
->fs_info
;
235 struct inode_fs_paths
*ipath
= NULL
;
236 struct btrfs_root
*local_root
;
237 struct btrfs_key root_key
;
239 root_key
.objectid
= root
;
240 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
241 root_key
.offset
= (u64
)-1;
242 local_root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
243 if (IS_ERR(local_root
)) {
244 ret
= PTR_ERR(local_root
);
248 ret
= inode_item_info(inum
, 0, local_root
, swarn
->path
);
250 btrfs_release_path(swarn
->path
);
254 eb
= swarn
->path
->nodes
[0];
255 inode_item
= btrfs_item_ptr(eb
, swarn
->path
->slots
[0],
256 struct btrfs_inode_item
);
257 isize
= btrfs_inode_size(eb
, inode_item
);
258 nlink
= btrfs_inode_nlink(eb
, inode_item
);
259 btrfs_release_path(swarn
->path
);
261 ipath
= init_ipath(4096, local_root
, swarn
->path
);
262 ret
= paths_from_inode(inum
, ipath
);
268 * we deliberately ignore the bit ipath might have been too small to
269 * hold all of the paths here
271 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
)
272 printk(KERN_WARNING
"btrfs: %s at logical %llu on dev "
273 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
274 "length %llu, links %u (path: %s)\n", swarn
->errstr
,
275 swarn
->logical
, swarn
->dev
->name
,
276 (unsigned long long)swarn
->sector
, root
, inum
, offset
,
277 min(isize
- offset
, (u64
)PAGE_SIZE
), nlink
,
278 ipath
->fspath
->str
[i
]);
284 printk(KERN_WARNING
"btrfs: %s at logical %llu on dev "
285 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
286 "resolving failed with ret=%d\n", swarn
->errstr
,
287 swarn
->logical
, swarn
->dev
->name
,
288 (unsigned long long)swarn
->sector
, root
, inum
, offset
, ret
);
294 static void scrub_print_warning(const char *errstr
, struct scrub_bio
*sbio
,
297 struct btrfs_device
*dev
= sbio
->sdev
->dev
;
298 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
299 struct btrfs_path
*path
;
300 struct btrfs_key found_key
;
301 struct extent_buffer
*eb
;
302 struct btrfs_extent_item
*ei
;
303 struct scrub_warning swarn
;
308 unsigned long ptr
= 0;
309 const int bufsize
= 4096;
312 path
= btrfs_alloc_path();
314 swarn
.scratch_buf
= kmalloc(bufsize
, GFP_NOFS
);
315 swarn
.msg_buf
= kmalloc(bufsize
, GFP_NOFS
);
316 swarn
.sector
= (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9;
317 swarn
.logical
= sbio
->logical
+ ix
* PAGE_SIZE
;
318 swarn
.errstr
= errstr
;
320 swarn
.msg_bufsize
= bufsize
;
321 swarn
.scratch_bufsize
= bufsize
;
323 if (!path
|| !swarn
.scratch_buf
|| !swarn
.msg_buf
)
326 ret
= extent_from_logical(fs_info
, swarn
.logical
, path
, &found_key
);
330 extent_offset
= swarn
.logical
- found_key
.objectid
;
331 swarn
.extent_item_size
= found_key
.offset
;
334 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
335 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
337 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
339 ret
= tree_backref_for_extent(&ptr
, eb
, ei
, item_size
,
340 &ref_root
, &ref_level
);
341 printk(KERN_WARNING
"%s at logical %llu on dev %s, "
342 "sector %llu: metadata %s (level %d) in tree "
343 "%llu\n", errstr
, swarn
.logical
, dev
->name
,
344 (unsigned long long)swarn
.sector
,
345 ref_level
? "node" : "leaf",
346 ret
< 0 ? -1 : ref_level
,
347 ret
< 0 ? -1 : ref_root
);
351 iterate_extent_inodes(fs_info
, path
, found_key
.objectid
,
353 scrub_print_warning_inode
, &swarn
);
357 btrfs_free_path(path
);
358 kfree(swarn
.scratch_buf
);
359 kfree(swarn
.msg_buf
);
362 static int scrub_fixup_readpage(u64 inum
, u64 offset
, u64 root
, void *ctx
)
364 struct page
*page
= NULL
;
366 struct scrub_fixup_nodatasum
*fixup
= ctx
;
369 struct btrfs_key key
;
370 struct inode
*inode
= NULL
;
371 u64 end
= offset
+ PAGE_SIZE
- 1;
372 struct btrfs_root
*local_root
;
375 key
.type
= BTRFS_ROOT_ITEM_KEY
;
376 key
.offset
= (u64
)-1;
377 local_root
= btrfs_read_fs_root_no_name(fixup
->root
->fs_info
, &key
);
378 if (IS_ERR(local_root
))
379 return PTR_ERR(local_root
);
381 key
.type
= BTRFS_INODE_ITEM_KEY
;
384 inode
= btrfs_iget(fixup
->root
->fs_info
->sb
, &key
, local_root
, NULL
);
386 return PTR_ERR(inode
);
388 index
= offset
>> PAGE_CACHE_SHIFT
;
390 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
396 if (PageUptodate(page
)) {
397 struct btrfs_mapping_tree
*map_tree
;
398 if (PageDirty(page
)) {
400 * we need to write the data to the defect sector. the
401 * data that was in that sector is not in memory,
402 * because the page was modified. we must not write the
403 * modified page to that sector.
405 * TODO: what could be done here: wait for the delalloc
406 * runner to write out that page (might involve
407 * COW) and see whether the sector is still
408 * referenced afterwards.
410 * For the meantime, we'll treat this error
411 * incorrectable, although there is a chance that a
412 * later scrub will find the bad sector again and that
413 * there's no dirty page in memory, then.
418 map_tree
= &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
;
419 ret
= repair_io_failure(map_tree
, offset
, PAGE_SIZE
,
420 fixup
->logical
, page
,
426 * we need to get good data first. the general readpage path
427 * will call repair_io_failure for us, we just have to make
428 * sure we read the bad mirror.
430 ret
= set_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
431 EXTENT_DAMAGED
, GFP_NOFS
);
433 /* set_extent_bits should give proper error */
440 ret
= extent_read_full_page(&BTRFS_I(inode
)->io_tree
, page
,
443 wait_on_page_locked(page
);
445 corrected
= !test_range_bit(&BTRFS_I(inode
)->io_tree
, offset
,
446 end
, EXTENT_DAMAGED
, 0, NULL
);
448 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, offset
, end
,
449 EXTENT_DAMAGED
, GFP_NOFS
);
461 if (ret
== 0 && corrected
) {
463 * we only need to call readpage for one of the inodes belonging
464 * to this extent. so make iterate_extent_inodes stop
472 static void scrub_fixup_nodatasum(struct btrfs_work
*work
)
475 struct scrub_fixup_nodatasum
*fixup
;
476 struct scrub_dev
*sdev
;
477 struct btrfs_trans_handle
*trans
= NULL
;
478 struct btrfs_fs_info
*fs_info
;
479 struct btrfs_path
*path
;
480 int uncorrectable
= 0;
482 fixup
= container_of(work
, struct scrub_fixup_nodatasum
, work
);
484 fs_info
= fixup
->root
->fs_info
;
486 path
= btrfs_alloc_path();
488 spin_lock(&sdev
->stat_lock
);
489 ++sdev
->stat
.malloc_errors
;
490 spin_unlock(&sdev
->stat_lock
);
495 trans
= btrfs_join_transaction(fixup
->root
);
502 * the idea is to trigger a regular read through the standard path. we
503 * read a page from the (failed) logical address by specifying the
504 * corresponding copynum of the failed sector. thus, that readpage is
506 * that is the point where on-the-fly error correction will kick in
507 * (once it's finished) and rewrite the failed sector if a good copy
510 ret
= iterate_inodes_from_logical(fixup
->logical
, fixup
->root
->fs_info
,
511 path
, scrub_fixup_readpage
,
519 spin_lock(&sdev
->stat_lock
);
520 ++sdev
->stat
.corrected_errors
;
521 spin_unlock(&sdev
->stat_lock
);
524 if (trans
&& !IS_ERR(trans
))
525 btrfs_end_transaction(trans
, fixup
->root
);
527 spin_lock(&sdev
->stat_lock
);
528 ++sdev
->stat
.uncorrectable_errors
;
529 spin_unlock(&sdev
->stat_lock
);
530 printk_ratelimited(KERN_ERR
"btrfs: unable to fixup "
531 "(nodatasum) error at logical %llu\n",
535 btrfs_free_path(path
);
538 /* see caller why we're pretending to be paused in the scrub counters */
539 mutex_lock(&fs_info
->scrub_lock
);
540 atomic_dec(&fs_info
->scrubs_running
);
541 atomic_dec(&fs_info
->scrubs_paused
);
542 mutex_unlock(&fs_info
->scrub_lock
);
543 atomic_dec(&sdev
->fixup_cnt
);
544 wake_up(&fs_info
->scrub_pause_wait
);
545 wake_up(&sdev
->list_wait
);
549 * scrub_recheck_error gets called when either verification of the page
550 * failed or the bio failed to read, e.g. with EIO. In the latter case,
551 * recheck_error gets called for every page in the bio, even though only
554 static int scrub_recheck_error(struct scrub_bio
*sbio
, int ix
)
556 struct scrub_dev
*sdev
= sbio
->sdev
;
557 u64 sector
= (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9;
558 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
559 DEFAULT_RATELIMIT_BURST
);
562 if (scrub_fixup_io(READ
, sbio
->sdev
->dev
->bdev
, sector
,
563 sbio
->bio
->bi_io_vec
[ix
].bv_page
) == 0) {
564 if (scrub_fixup_check(sbio
, ix
) == 0)
567 if (__ratelimit(&_rs
))
568 scrub_print_warning("i/o error", sbio
, ix
);
570 if (__ratelimit(&_rs
))
571 scrub_print_warning("checksum error", sbio
, ix
);
574 spin_lock(&sdev
->stat_lock
);
575 ++sdev
->stat
.read_errors
;
576 spin_unlock(&sdev
->stat_lock
);
578 scrub_fixup(sbio
, ix
);
582 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
)
587 u64 flags
= sbio
->spag
[ix
].flags
;
589 page
= sbio
->bio
->bi_io_vec
[ix
].bv_page
;
590 buffer
= kmap_atomic(page
, KM_USER0
);
591 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
592 ret
= scrub_checksum_data(sbio
->sdev
,
593 sbio
->spag
+ ix
, buffer
);
594 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
595 ret
= scrub_checksum_tree_block(sbio
->sdev
,
597 sbio
->logical
+ ix
* PAGE_SIZE
,
602 kunmap_atomic(buffer
, KM_USER0
);
607 static void scrub_fixup_end_io(struct bio
*bio
, int err
)
609 complete((struct completion
*)bio
->bi_private
);
612 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
)
614 struct scrub_dev
*sdev
= sbio
->sdev
;
615 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
616 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
617 struct btrfs_bio
*bbio
= NULL
;
618 struct scrub_fixup_nodatasum
*fixup
;
619 u64 logical
= sbio
->logical
+ ix
* PAGE_SIZE
;
623 DECLARE_COMPLETION_ONSTACK(complete
);
625 if ((sbio
->spag
[ix
].flags
& BTRFS_EXTENT_FLAG_DATA
) &&
626 (sbio
->spag
[ix
].have_csum
== 0)) {
627 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
631 fixup
->logical
= logical
;
632 fixup
->root
= fs_info
->extent_root
;
633 fixup
->mirror_num
= sbio
->spag
[ix
].mirror_num
;
635 * increment scrubs_running to prevent cancel requests from
636 * completing as long as a fixup worker is running. we must also
637 * increment scrubs_paused to prevent deadlocking on pause
638 * requests used for transactions commits (as the worker uses a
639 * transaction context). it is safe to regard the fixup worker
640 * as paused for all matters practical. effectively, we only
641 * avoid cancellation requests from completing.
643 mutex_lock(&fs_info
->scrub_lock
);
644 atomic_inc(&fs_info
->scrubs_running
);
645 atomic_inc(&fs_info
->scrubs_paused
);
646 mutex_unlock(&fs_info
->scrub_lock
);
647 atomic_inc(&sdev
->fixup_cnt
);
648 fixup
->work
.func
= scrub_fixup_nodatasum
;
649 btrfs_queue_worker(&fs_info
->scrub_workers
, &fixup
->work
);
654 ret
= btrfs_map_block(map_tree
, REQ_WRITE
, logical
, &length
,
656 if (ret
|| !bbio
|| length
< PAGE_SIZE
) {
658 "scrub_fixup: btrfs_map_block failed us for %llu\n",
659 (unsigned long long)logical
);
664 if (bbio
->num_stripes
== 1)
665 /* there aren't any replicas */
669 * first find a good copy
671 for (i
= 0; i
< bbio
->num_stripes
; ++i
) {
672 if (i
+ 1 == sbio
->spag
[ix
].mirror_num
)
675 if (scrub_fixup_io(READ
, bbio
->stripes
[i
].dev
->bdev
,
676 bbio
->stripes
[i
].physical
>> 9,
677 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
678 /* I/O-error, this is not a good copy */
682 if (scrub_fixup_check(sbio
, ix
) == 0)
685 if (i
== bbio
->num_stripes
)
688 if (!sdev
->readonly
) {
690 * bi_io_vec[ix].bv_page now contains good data, write it back
692 if (scrub_fixup_io(WRITE
, sdev
->dev
->bdev
,
693 (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9,
694 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
695 /* I/O-error, writeback failed, give up */
701 spin_lock(&sdev
->stat_lock
);
702 ++sdev
->stat
.corrected_errors
;
703 spin_unlock(&sdev
->stat_lock
);
705 printk_ratelimited(KERN_ERR
"btrfs: fixed up error at logical %llu\n",
706 (unsigned long long)logical
);
711 spin_lock(&sdev
->stat_lock
);
712 ++sdev
->stat
.uncorrectable_errors
;
713 spin_unlock(&sdev
->stat_lock
);
715 printk_ratelimited(KERN_ERR
"btrfs: unable to fixup (regular) error at "
716 "logical %llu\n", (unsigned long long)logical
);
719 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
722 struct bio
*bio
= NULL
;
724 DECLARE_COMPLETION_ONSTACK(complete
);
726 bio
= bio_alloc(GFP_NOFS
, 1);
728 bio
->bi_sector
= sector
;
729 bio_add_page(bio
, page
, PAGE_SIZE
, 0);
730 bio
->bi_end_io
= scrub_fixup_end_io
;
731 bio
->bi_private
= &complete
;
734 /* this will also unplug the queue */
735 wait_for_completion(&complete
);
737 ret
= !test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
742 static void scrub_bio_end_io(struct bio
*bio
, int err
)
744 struct scrub_bio
*sbio
= bio
->bi_private
;
745 struct scrub_dev
*sdev
= sbio
->sdev
;
746 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
751 btrfs_queue_worker(&fs_info
->scrub_workers
, &sbio
->work
);
754 static void scrub_checksum(struct btrfs_work
*work
)
756 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
757 struct scrub_dev
*sdev
= sbio
->sdev
;
767 for (i
= 0; i
< sbio
->count
; ++i
)
768 ret
|= scrub_recheck_error(sbio
, i
);
770 spin_lock(&sdev
->stat_lock
);
771 ++sdev
->stat
.unverified_errors
;
772 spin_unlock(&sdev
->stat_lock
);
775 sbio
->bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
776 sbio
->bio
->bi_flags
|= 1 << BIO_UPTODATE
;
777 sbio
->bio
->bi_phys_segments
= 0;
778 sbio
->bio
->bi_idx
= 0;
780 for (i
= 0; i
< sbio
->count
; i
++) {
782 bi
= &sbio
->bio
->bi_io_vec
[i
];
784 bi
->bv_len
= PAGE_SIZE
;
788 for (i
= 0; i
< sbio
->count
; ++i
) {
789 page
= sbio
->bio
->bi_io_vec
[i
].bv_page
;
790 buffer
= kmap_atomic(page
, KM_USER0
);
791 flags
= sbio
->spag
[i
].flags
;
792 logical
= sbio
->logical
+ i
* PAGE_SIZE
;
794 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
795 ret
= scrub_checksum_data(sdev
, sbio
->spag
+ i
, buffer
);
796 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
797 ret
= scrub_checksum_tree_block(sdev
, sbio
->spag
+ i
,
799 } else if (flags
& BTRFS_EXTENT_FLAG_SUPER
) {
801 (void)scrub_checksum_super(sbio
, buffer
);
805 kunmap_atomic(buffer
, KM_USER0
);
807 ret
= scrub_recheck_error(sbio
, i
);
809 spin_lock(&sdev
->stat_lock
);
810 ++sdev
->stat
.unverified_errors
;
811 spin_unlock(&sdev
->stat_lock
);
817 scrub_free_bio(sbio
->bio
);
819 spin_lock(&sdev
->list_lock
);
820 sbio
->next_free
= sdev
->first_free
;
821 sdev
->first_free
= sbio
->index
;
822 spin_unlock(&sdev
->list_lock
);
823 atomic_dec(&sdev
->in_flight
);
824 wake_up(&sdev
->list_wait
);
827 static int scrub_checksum_data(struct scrub_dev
*sdev
,
828 struct scrub_page
*spag
, void *buffer
)
830 u8 csum
[BTRFS_CSUM_SIZE
];
833 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
835 if (!spag
->have_csum
)
838 crc
= btrfs_csum_data(root
, buffer
, crc
, PAGE_SIZE
);
839 btrfs_csum_final(crc
, csum
);
840 if (memcmp(csum
, spag
->csum
, sdev
->csum_size
))
843 spin_lock(&sdev
->stat_lock
);
844 ++sdev
->stat
.data_extents_scrubbed
;
845 sdev
->stat
.data_bytes_scrubbed
+= PAGE_SIZE
;
847 ++sdev
->stat
.csum_errors
;
848 spin_unlock(&sdev
->stat_lock
);
853 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
854 struct scrub_page
*spag
, u64 logical
,
857 struct btrfs_header
*h
;
858 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
859 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
860 u8 csum
[BTRFS_CSUM_SIZE
];
866 * we don't use the getter functions here, as we
867 * a) don't have an extent buffer and
868 * b) the page is already kmapped
870 h
= (struct btrfs_header
*)buffer
;
872 if (logical
!= le64_to_cpu(h
->bytenr
))
875 if (spag
->generation
!= le64_to_cpu(h
->generation
))
878 if (memcmp(h
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
881 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
885 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
886 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
887 btrfs_csum_final(crc
, csum
);
888 if (memcmp(csum
, h
->csum
, sdev
->csum_size
))
891 spin_lock(&sdev
->stat_lock
);
892 ++sdev
->stat
.tree_extents_scrubbed
;
893 sdev
->stat
.tree_bytes_scrubbed
+= PAGE_SIZE
;
895 ++sdev
->stat
.csum_errors
;
897 ++sdev
->stat
.verify_errors
;
898 spin_unlock(&sdev
->stat_lock
);
900 return fail
|| crc_fail
;
903 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
)
905 struct btrfs_super_block
*s
;
907 struct scrub_dev
*sdev
= sbio
->sdev
;
908 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
909 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
910 u8 csum
[BTRFS_CSUM_SIZE
];
914 s
= (struct btrfs_super_block
*)buffer
;
915 logical
= sbio
->logical
;
917 if (logical
!= le64_to_cpu(s
->bytenr
))
920 if (sbio
->spag
[0].generation
!= le64_to_cpu(s
->generation
))
923 if (memcmp(s
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
926 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
927 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
928 btrfs_csum_final(crc
, csum
);
929 if (memcmp(csum
, s
->csum
, sbio
->sdev
->csum_size
))
934 * if we find an error in a super block, we just report it.
935 * They will get written with the next transaction commit
938 spin_lock(&sdev
->stat_lock
);
939 ++sdev
->stat
.super_errors
;
940 spin_unlock(&sdev
->stat_lock
);
946 static int scrub_submit(struct scrub_dev
*sdev
)
948 struct scrub_bio
*sbio
;
952 if (sdev
->curr
== -1)
955 sbio
= sdev
->bios
[sdev
->curr
];
957 bio
= bio_alloc(GFP_NOFS
, sbio
->count
);
961 bio
->bi_private
= sbio
;
962 bio
->bi_end_io
= scrub_bio_end_io
;
963 bio
->bi_bdev
= sdev
->dev
->bdev
;
964 bio
->bi_sector
= sbio
->physical
>> 9;
966 for (i
= 0; i
< sbio
->count
; ++i
) {
970 page
= alloc_page(GFP_NOFS
);
974 ret
= bio_add_page(bio
, page
, PAGE_SIZE
, 0);
983 atomic_inc(&sdev
->in_flight
);
985 submit_bio(READ
, bio
);
995 static int scrub_page(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
996 u64 physical
, u64 flags
, u64 gen
, int mirror_num
,
999 struct scrub_bio
*sbio
;
1003 * grab a fresh bio or wait for one to become available
1005 while (sdev
->curr
== -1) {
1006 spin_lock(&sdev
->list_lock
);
1007 sdev
->curr
= sdev
->first_free
;
1008 if (sdev
->curr
!= -1) {
1009 sdev
->first_free
= sdev
->bios
[sdev
->curr
]->next_free
;
1010 sdev
->bios
[sdev
->curr
]->next_free
= -1;
1011 sdev
->bios
[sdev
->curr
]->count
= 0;
1012 spin_unlock(&sdev
->list_lock
);
1014 spin_unlock(&sdev
->list_lock
);
1015 wait_event(sdev
->list_wait
, sdev
->first_free
!= -1);
1018 sbio
= sdev
->bios
[sdev
->curr
];
1019 if (sbio
->count
== 0) {
1020 sbio
->physical
= physical
;
1021 sbio
->logical
= logical
;
1022 } else if (sbio
->physical
+ sbio
->count
* PAGE_SIZE
!= physical
||
1023 sbio
->logical
+ sbio
->count
* PAGE_SIZE
!= logical
) {
1026 ret
= scrub_submit(sdev
);
1031 sbio
->spag
[sbio
->count
].flags
= flags
;
1032 sbio
->spag
[sbio
->count
].generation
= gen
;
1033 sbio
->spag
[sbio
->count
].have_csum
= 0;
1034 sbio
->spag
[sbio
->count
].mirror_num
= mirror_num
;
1036 sbio
->spag
[sbio
->count
].have_csum
= 1;
1037 memcpy(sbio
->spag
[sbio
->count
].csum
, csum
, sdev
->csum_size
);
1040 if (sbio
->count
== SCRUB_PAGES_PER_BIO
|| force
) {
1043 ret
= scrub_submit(sdev
);
1051 static int scrub_find_csum(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1054 struct btrfs_ordered_sum
*sum
= NULL
;
1057 unsigned long num_sectors
;
1058 u32 sectorsize
= sdev
->dev
->dev_root
->sectorsize
;
1060 while (!list_empty(&sdev
->csum_list
)) {
1061 sum
= list_first_entry(&sdev
->csum_list
,
1062 struct btrfs_ordered_sum
, list
);
1063 if (sum
->bytenr
> logical
)
1065 if (sum
->bytenr
+ sum
->len
> logical
)
1068 ++sdev
->stat
.csum_discards
;
1069 list_del(&sum
->list
);
1076 num_sectors
= sum
->len
/ sectorsize
;
1077 for (i
= 0; i
< num_sectors
; ++i
) {
1078 if (sum
->sums
[i
].bytenr
== logical
) {
1079 memcpy(csum
, &sum
->sums
[i
].sum
, sdev
->csum_size
);
1084 if (ret
&& i
== num_sectors
- 1) {
1085 list_del(&sum
->list
);
1091 /* scrub extent tries to collect up to 64 kB for each bio */
1092 static int scrub_extent(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
1093 u64 physical
, u64 flags
, u64 gen
, int mirror_num
)
1096 u8 csum
[BTRFS_CSUM_SIZE
];
1099 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
1102 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
1103 /* push csums to sbio */
1104 have_csum
= scrub_find_csum(sdev
, logical
, l
, csum
);
1106 ++sdev
->stat
.no_csum
;
1108 ret
= scrub_page(sdev
, logical
, l
, physical
, flags
, gen
,
1109 mirror_num
, have_csum
? csum
: NULL
, 0);
1119 static noinline_for_stack
int scrub_stripe(struct scrub_dev
*sdev
,
1120 struct map_lookup
*map
, int num
, u64 base
, u64 length
)
1122 struct btrfs_path
*path
;
1123 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
1124 struct btrfs_root
*root
= fs_info
->extent_root
;
1125 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
1126 struct btrfs_extent_item
*extent
;
1127 struct blk_plug plug
;
1134 struct extent_buffer
*l
;
1135 struct btrfs_key key
;
1141 u64 increment
= map
->stripe_len
;
1146 do_div(nstripes
, map
->stripe_len
);
1147 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
1148 offset
= map
->stripe_len
* num
;
1149 increment
= map
->stripe_len
* map
->num_stripes
;
1151 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
1152 int factor
= map
->num_stripes
/ map
->sub_stripes
;
1153 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
1154 increment
= map
->stripe_len
* factor
;
1155 mirror_num
= num
% map
->sub_stripes
+ 1;
1156 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
1157 increment
= map
->stripe_len
;
1158 mirror_num
= num
% map
->num_stripes
+ 1;
1159 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
1160 increment
= map
->stripe_len
;
1161 mirror_num
= num
% map
->num_stripes
+ 1;
1163 increment
= map
->stripe_len
;
1167 path
= btrfs_alloc_path();
1172 path
->search_commit_root
= 1;
1173 path
->skip_locking
= 1;
1176 * find all extents for each stripe and just read them to get
1177 * them into the page cache
1178 * FIXME: we can do better. build a more intelligent prefetching
1180 logical
= base
+ offset
;
1181 physical
= map
->stripes
[num
].physical
;
1183 for (i
= 0; i
< nstripes
; ++i
) {
1184 key
.objectid
= logical
;
1185 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1186 key
.offset
= (u64
)0;
1188 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1193 * we might miss half an extent here, but that doesn't matter,
1194 * as it's only the prefetch
1198 slot
= path
->slots
[0];
1199 if (slot
>= btrfs_header_nritems(l
)) {
1200 ret
= btrfs_next_leaf(root
, path
);
1208 btrfs_item_key_to_cpu(l
, &key
, slot
);
1210 if (key
.objectid
>= logical
+ map
->stripe_len
)
1215 btrfs_release_path(path
);
1216 logical
+= increment
;
1217 physical
+= map
->stripe_len
;
1222 * collect all data csums for the stripe to avoid seeking during
1223 * the scrub. This might currently (crc32) end up to be about 1MB
1226 blk_start_plug(&plug
);
1228 logical
= base
+ offset
+ start_stripe
* increment
;
1229 for (i
= start_stripe
; i
< nstripes
; ++i
) {
1230 ret
= btrfs_lookup_csums_range(csum_root
, logical
,
1231 logical
+ map
->stripe_len
- 1,
1232 &sdev
->csum_list
, 1);
1236 logical
+= increment
;
1240 * now find all extents for each stripe and scrub them
1242 logical
= base
+ offset
+ start_stripe
* increment
;
1243 physical
= map
->stripes
[num
].physical
+ start_stripe
* map
->stripe_len
;
1245 for (i
= start_stripe
; i
< nstripes
; ++i
) {
1249 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
1250 atomic_read(&sdev
->cancel_req
)) {
1255 * check to see if we have to pause
1257 if (atomic_read(&fs_info
->scrub_pause_req
)) {
1258 /* push queued extents */
1260 wait_event(sdev
->list_wait
,
1261 atomic_read(&sdev
->in_flight
) == 0);
1262 atomic_inc(&fs_info
->scrubs_paused
);
1263 wake_up(&fs_info
->scrub_pause_wait
);
1264 mutex_lock(&fs_info
->scrub_lock
);
1265 while (atomic_read(&fs_info
->scrub_pause_req
)) {
1266 mutex_unlock(&fs_info
->scrub_lock
);
1267 wait_event(fs_info
->scrub_pause_wait
,
1268 atomic_read(&fs_info
->scrub_pause_req
) == 0);
1269 mutex_lock(&fs_info
->scrub_lock
);
1271 atomic_dec(&fs_info
->scrubs_paused
);
1272 mutex_unlock(&fs_info
->scrub_lock
);
1273 wake_up(&fs_info
->scrub_pause_wait
);
1274 scrub_free_csums(sdev
);
1279 key
.objectid
= logical
;
1280 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
1281 key
.offset
= (u64
)0;
1283 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1287 ret
= btrfs_previous_item(root
, path
, 0,
1288 BTRFS_EXTENT_ITEM_KEY
);
1292 /* there's no smaller item, so stick with the
1294 btrfs_release_path(path
);
1295 ret
= btrfs_search_slot(NULL
, root
, &key
,
1304 slot
= path
->slots
[0];
1305 if (slot
>= btrfs_header_nritems(l
)) {
1306 ret
= btrfs_next_leaf(root
, path
);
1314 btrfs_item_key_to_cpu(l
, &key
, slot
);
1316 if (key
.objectid
+ key
.offset
<= logical
)
1319 if (key
.objectid
>= logical
+ map
->stripe_len
)
1322 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
)
1325 extent
= btrfs_item_ptr(l
, slot
,
1326 struct btrfs_extent_item
);
1327 flags
= btrfs_extent_flags(l
, extent
);
1328 generation
= btrfs_extent_generation(l
, extent
);
1330 if (key
.objectid
< logical
&&
1331 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
1333 "btrfs scrub: tree block %llu spanning "
1334 "stripes, ignored. logical=%llu\n",
1335 (unsigned long long)key
.objectid
,
1336 (unsigned long long)logical
);
1341 * trim extent to this stripe
1343 if (key
.objectid
< logical
) {
1344 key
.offset
-= logical
- key
.objectid
;
1345 key
.objectid
= logical
;
1347 if (key
.objectid
+ key
.offset
>
1348 logical
+ map
->stripe_len
) {
1349 key
.offset
= logical
+ map
->stripe_len
-
1353 ret
= scrub_extent(sdev
, key
.objectid
, key
.offset
,
1354 key
.objectid
- logical
+ physical
,
1355 flags
, generation
, mirror_num
);
1362 btrfs_release_path(path
);
1363 logical
+= increment
;
1364 physical
+= map
->stripe_len
;
1365 spin_lock(&sdev
->stat_lock
);
1366 sdev
->stat
.last_physical
= physical
;
1367 spin_unlock(&sdev
->stat_lock
);
1369 /* push queued extents */
1373 blk_finish_plug(&plug
);
1375 btrfs_free_path(path
);
1376 return ret
< 0 ? ret
: 0;
1379 static noinline_for_stack
int scrub_chunk(struct scrub_dev
*sdev
,
1380 u64 chunk_tree
, u64 chunk_objectid
, u64 chunk_offset
, u64 length
)
1382 struct btrfs_mapping_tree
*map_tree
=
1383 &sdev
->dev
->dev_root
->fs_info
->mapping_tree
;
1384 struct map_lookup
*map
;
1385 struct extent_map
*em
;
1389 read_lock(&map_tree
->map_tree
.lock
);
1390 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
1391 read_unlock(&map_tree
->map_tree
.lock
);
1396 map
= (struct map_lookup
*)em
->bdev
;
1397 if (em
->start
!= chunk_offset
)
1400 if (em
->len
< length
)
1403 for (i
= 0; i
< map
->num_stripes
; ++i
) {
1404 if (map
->stripes
[i
].dev
== sdev
->dev
) {
1405 ret
= scrub_stripe(sdev
, map
, i
, chunk_offset
, length
);
1411 free_extent_map(em
);
1416 static noinline_for_stack
1417 int scrub_enumerate_chunks(struct scrub_dev
*sdev
, u64 start
, u64 end
)
1419 struct btrfs_dev_extent
*dev_extent
= NULL
;
1420 struct btrfs_path
*path
;
1421 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1422 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1429 struct extent_buffer
*l
;
1430 struct btrfs_key key
;
1431 struct btrfs_key found_key
;
1432 struct btrfs_block_group_cache
*cache
;
1434 path
= btrfs_alloc_path();
1439 path
->search_commit_root
= 1;
1440 path
->skip_locking
= 1;
1442 key
.objectid
= sdev
->dev
->devid
;
1444 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1448 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1452 if (path
->slots
[0] >=
1453 btrfs_header_nritems(path
->nodes
[0])) {
1454 ret
= btrfs_next_leaf(root
, path
);
1461 slot
= path
->slots
[0];
1463 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
1465 if (found_key
.objectid
!= sdev
->dev
->devid
)
1468 if (btrfs_key_type(&found_key
) != BTRFS_DEV_EXTENT_KEY
)
1471 if (found_key
.offset
>= end
)
1474 if (found_key
.offset
< key
.offset
)
1477 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1478 length
= btrfs_dev_extent_length(l
, dev_extent
);
1480 if (found_key
.offset
+ length
<= start
) {
1481 key
.offset
= found_key
.offset
+ length
;
1482 btrfs_release_path(path
);
1486 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1487 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1488 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1491 * get a reference on the corresponding block group to prevent
1492 * the chunk from going away while we scrub it
1494 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
1499 ret
= scrub_chunk(sdev
, chunk_tree
, chunk_objectid
,
1500 chunk_offset
, length
);
1501 btrfs_put_block_group(cache
);
1505 key
.offset
= found_key
.offset
+ length
;
1506 btrfs_release_path(path
);
1509 btrfs_free_path(path
);
1512 * ret can still be 1 from search_slot or next_leaf,
1513 * that's not an error
1515 return ret
< 0 ? ret
: 0;
1518 static noinline_for_stack
int scrub_supers(struct scrub_dev
*sdev
)
1524 struct btrfs_device
*device
= sdev
->dev
;
1525 struct btrfs_root
*root
= device
->dev_root
;
1527 gen
= root
->fs_info
->last_trans_committed
;
1529 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1530 bytenr
= btrfs_sb_offset(i
);
1531 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>= device
->total_bytes
)
1534 ret
= scrub_page(sdev
, bytenr
, PAGE_SIZE
, bytenr
,
1535 BTRFS_EXTENT_FLAG_SUPER
, gen
, i
, NULL
, 1);
1539 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1545 * get a reference count on fs_info->scrub_workers. start worker if necessary
1547 static noinline_for_stack
int scrub_workers_get(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_init_workers(&fs_info
->scrub_workers
, "scrub",
1554 fs_info
->thread_pool_size
, &fs_info
->generic_worker
);
1555 fs_info
->scrub_workers
.idle_thresh
= 4;
1556 btrfs_start_workers(&fs_info
->scrub_workers
, 1);
1558 ++fs_info
->scrub_workers_refcnt
;
1559 mutex_unlock(&fs_info
->scrub_lock
);
1564 static noinline_for_stack
void scrub_workers_put(struct btrfs_root
*root
)
1566 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1568 mutex_lock(&fs_info
->scrub_lock
);
1569 if (--fs_info
->scrub_workers_refcnt
== 0)
1570 btrfs_stop_workers(&fs_info
->scrub_workers
);
1571 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
1572 mutex_unlock(&fs_info
->scrub_lock
);
1576 int btrfs_scrub_dev(struct btrfs_root
*root
, u64 devid
, u64 start
, u64 end
,
1577 struct btrfs_scrub_progress
*progress
, int readonly
)
1579 struct scrub_dev
*sdev
;
1580 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1582 struct btrfs_device
*dev
;
1584 if (btrfs_fs_closing(root
->fs_info
))
1588 * check some assumptions
1590 if (root
->sectorsize
!= PAGE_SIZE
||
1591 root
->sectorsize
!= root
->leafsize
||
1592 root
->sectorsize
!= root
->nodesize
) {
1593 printk(KERN_ERR
"btrfs_scrub: size assumptions fail\n");
1597 ret
= scrub_workers_get(root
);
1601 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1602 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1603 if (!dev
|| dev
->missing
) {
1604 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1605 scrub_workers_put(root
);
1608 mutex_lock(&fs_info
->scrub_lock
);
1610 if (!dev
->in_fs_metadata
) {
1611 mutex_unlock(&fs_info
->scrub_lock
);
1612 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1613 scrub_workers_put(root
);
1617 if (dev
->scrub_device
) {
1618 mutex_unlock(&fs_info
->scrub_lock
);
1619 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1620 scrub_workers_put(root
);
1621 return -EINPROGRESS
;
1623 sdev
= scrub_setup_dev(dev
);
1625 mutex_unlock(&fs_info
->scrub_lock
);
1626 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1627 scrub_workers_put(root
);
1628 return PTR_ERR(sdev
);
1630 sdev
->readonly
= readonly
;
1631 dev
->scrub_device
= sdev
;
1633 atomic_inc(&fs_info
->scrubs_running
);
1634 mutex_unlock(&fs_info
->scrub_lock
);
1635 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1637 down_read(&fs_info
->scrub_super_lock
);
1638 ret
= scrub_supers(sdev
);
1639 up_read(&fs_info
->scrub_super_lock
);
1642 ret
= scrub_enumerate_chunks(sdev
, start
, end
);
1644 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1645 atomic_dec(&fs_info
->scrubs_running
);
1646 wake_up(&fs_info
->scrub_pause_wait
);
1648 wait_event(sdev
->list_wait
, atomic_read(&sdev
->fixup_cnt
) == 0);
1651 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1653 mutex_lock(&fs_info
->scrub_lock
);
1654 dev
->scrub_device
= NULL
;
1655 mutex_unlock(&fs_info
->scrub_lock
);
1657 scrub_free_dev(sdev
);
1658 scrub_workers_put(root
);
1663 int btrfs_scrub_pause(struct btrfs_root
*root
)
1665 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1667 mutex_lock(&fs_info
->scrub_lock
);
1668 atomic_inc(&fs_info
->scrub_pause_req
);
1669 while (atomic_read(&fs_info
->scrubs_paused
) !=
1670 atomic_read(&fs_info
->scrubs_running
)) {
1671 mutex_unlock(&fs_info
->scrub_lock
);
1672 wait_event(fs_info
->scrub_pause_wait
,
1673 atomic_read(&fs_info
->scrubs_paused
) ==
1674 atomic_read(&fs_info
->scrubs_running
));
1675 mutex_lock(&fs_info
->scrub_lock
);
1677 mutex_unlock(&fs_info
->scrub_lock
);
1682 int btrfs_scrub_continue(struct btrfs_root
*root
)
1684 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1686 atomic_dec(&fs_info
->scrub_pause_req
);
1687 wake_up(&fs_info
->scrub_pause_wait
);
1691 int btrfs_scrub_pause_super(struct btrfs_root
*root
)
1693 down_write(&root
->fs_info
->scrub_super_lock
);
1697 int btrfs_scrub_continue_super(struct btrfs_root
*root
)
1699 up_write(&root
->fs_info
->scrub_super_lock
);
1703 int btrfs_scrub_cancel(struct btrfs_root
*root
)
1705 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1707 mutex_lock(&fs_info
->scrub_lock
);
1708 if (!atomic_read(&fs_info
->scrubs_running
)) {
1709 mutex_unlock(&fs_info
->scrub_lock
);
1713 atomic_inc(&fs_info
->scrub_cancel_req
);
1714 while (atomic_read(&fs_info
->scrubs_running
)) {
1715 mutex_unlock(&fs_info
->scrub_lock
);
1716 wait_event(fs_info
->scrub_pause_wait
,
1717 atomic_read(&fs_info
->scrubs_running
) == 0);
1718 mutex_lock(&fs_info
->scrub_lock
);
1720 atomic_dec(&fs_info
->scrub_cancel_req
);
1721 mutex_unlock(&fs_info
->scrub_lock
);
1726 int btrfs_scrub_cancel_dev(struct btrfs_root
*root
, struct btrfs_device
*dev
)
1728 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1729 struct scrub_dev
*sdev
;
1731 mutex_lock(&fs_info
->scrub_lock
);
1732 sdev
= dev
->scrub_device
;
1734 mutex_unlock(&fs_info
->scrub_lock
);
1737 atomic_inc(&sdev
->cancel_req
);
1738 while (dev
->scrub_device
) {
1739 mutex_unlock(&fs_info
->scrub_lock
);
1740 wait_event(fs_info
->scrub_pause_wait
,
1741 dev
->scrub_device
== NULL
);
1742 mutex_lock(&fs_info
->scrub_lock
);
1744 mutex_unlock(&fs_info
->scrub_lock
);
1748 int btrfs_scrub_cancel_devid(struct btrfs_root
*root
, u64 devid
)
1750 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1751 struct btrfs_device
*dev
;
1755 * we have to hold the device_list_mutex here so the device
1756 * does not go away in cancel_dev. FIXME: find a better solution
1758 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1759 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1761 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1764 ret
= btrfs_scrub_cancel_dev(root
, dev
);
1765 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1770 int btrfs_scrub_progress(struct btrfs_root
*root
, u64 devid
,
1771 struct btrfs_scrub_progress
*progress
)
1773 struct btrfs_device
*dev
;
1774 struct scrub_dev
*sdev
= NULL
;
1776 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1777 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1779 sdev
= dev
->scrub_device
;
1781 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1782 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1784 return dev
? (sdev
? 0 : -ENOTCONN
) : -ENODEV
;