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/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
29 #include "ordered-data.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 * - To enhance the performance, better read-ahead strategies for the
39 * extent-tree can be employed.
40 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
42 * - In case of a read error on files with nodatasum, map the file and read
43 * the extent to trigger a writeback of the good copy
44 * - track and record media errors, throw out bad devices
45 * - add a mode to also read unallocated space
46 * - make the prefetch cancellable
52 static void scrub_bio_end_io(struct bio
*bio
, int err
);
53 static void scrub_checksum(struct btrfs_work
*work
);
54 static int scrub_checksum_data(struct scrub_dev
*sdev
,
55 struct scrub_page
*spag
, void *buffer
);
56 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
57 struct scrub_page
*spag
, u64 logical
,
59 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
);
60 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
);
61 static void scrub_fixup_end_io(struct bio
*bio
, int err
);
62 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
64 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
);
66 #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
67 #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
70 u64 flags
; /* extent flags */
74 u8 csum
[BTRFS_CSUM_SIZE
];
79 struct scrub_dev
*sdev
;
84 struct scrub_page spag
[SCRUB_PAGES_PER_BIO
];
87 struct btrfs_work work
;
91 struct scrub_bio
*bios
[SCRUB_BIOS_PER_DEV
];
92 struct btrfs_device
*dev
;
97 wait_queue_head_t list_wait
;
99 struct list_head csum_list
;
105 struct btrfs_scrub_progress stat
;
106 spinlock_t stat_lock
;
109 static void scrub_free_csums(struct scrub_dev
*sdev
)
111 while (!list_empty(&sdev
->csum_list
)) {
112 struct btrfs_ordered_sum
*sum
;
113 sum
= list_first_entry(&sdev
->csum_list
,
114 struct btrfs_ordered_sum
, list
);
115 list_del(&sum
->list
);
120 static noinline_for_stack
void scrub_free_dev(struct scrub_dev
*sdev
)
124 struct page
*last_page
;
129 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
130 struct scrub_bio
*sbio
= sdev
->bios
[i
];
139 for (j
= 0; j
< bio
->bi_vcnt
; ++j
) {
140 if (bio
->bi_io_vec
[j
].bv_page
== last_page
)
142 last_page
= bio
->bi_io_vec
[j
].bv_page
;
143 __free_page(last_page
);
150 scrub_free_csums(sdev
);
154 static noinline_for_stack
155 struct scrub_dev
*scrub_setup_dev(struct btrfs_device
*dev
)
157 struct scrub_dev
*sdev
;
161 struct btrfs_fs_info
*fs_info
= dev
->dev_root
->fs_info
;
163 sdev
= kzalloc(sizeof(*sdev
), GFP_NOFS
);
167 for (i
= 0; i
< SCRUB_BIOS_PER_DEV
; ++i
) {
169 struct scrub_bio
*sbio
;
171 sbio
= kzalloc(sizeof(*sbio
), GFP_NOFS
);
174 sdev
->bios
[i
] = sbio
;
176 bio
= bio_kmalloc(GFP_NOFS
, SCRUB_PAGES_PER_BIO
);
184 sbio
->work
.func
= scrub_checksum
;
185 bio
->bi_private
= sdev
->bios
[i
];
186 bio
->bi_end_io
= scrub_bio_end_io
;
188 bio
->bi_bdev
= dev
->bdev
;
191 for (j
= 0; j
< SCRUB_PAGES_PER_BIO
; ++j
) {
193 page
= alloc_page(GFP_NOFS
);
197 ret
= bio_add_page(bio
, page
, PAGE_SIZE
, 0);
201 WARN_ON(bio
->bi_vcnt
!= SCRUB_PAGES_PER_BIO
);
203 if (i
!= SCRUB_BIOS_PER_DEV
-1)
204 sdev
->bios
[i
]->next_free
= i
+ 1;
206 sdev
->bios
[i
]->next_free
= -1;
208 sdev
->first_free
= 0;
210 atomic_set(&sdev
->in_flight
, 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
);
226 * scrub_recheck_error gets called when either verification of the page
227 * failed or the bio failed to read, e.g. with EIO. In the latter case,
228 * recheck_error gets called for every page in the bio, even though only
231 static void scrub_recheck_error(struct scrub_bio
*sbio
, int ix
)
234 if (scrub_fixup_io(READ
, sbio
->sdev
->dev
->bdev
,
235 (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9,
236 sbio
->bio
->bi_io_vec
[ix
].bv_page
) == 0) {
237 if (scrub_fixup_check(sbio
, ix
) == 0)
242 scrub_fixup(sbio
, ix
);
245 static int scrub_fixup_check(struct scrub_bio
*sbio
, int ix
)
250 u64 flags
= sbio
->spag
[ix
].flags
;
252 page
= sbio
->bio
->bi_io_vec
[ix
].bv_page
;
253 buffer
= kmap_atomic(page
, KM_USER0
);
254 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
255 ret
= scrub_checksum_data(sbio
->sdev
,
256 sbio
->spag
+ ix
, buffer
);
257 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
258 ret
= scrub_checksum_tree_block(sbio
->sdev
,
260 sbio
->logical
+ ix
* PAGE_SIZE
,
265 kunmap_atomic(buffer
, KM_USER0
);
270 static void scrub_fixup_end_io(struct bio
*bio
, int err
)
272 complete((struct completion
*)bio
->bi_private
);
275 static void scrub_fixup(struct scrub_bio
*sbio
, int ix
)
277 struct scrub_dev
*sdev
= sbio
->sdev
;
278 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
279 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
280 struct btrfs_multi_bio
*multi
= NULL
;
281 u64 logical
= sbio
->logical
+ ix
* PAGE_SIZE
;
285 DECLARE_COMPLETION_ONSTACK(complete
);
287 if ((sbio
->spag
[ix
].flags
& BTRFS_EXTENT_FLAG_DATA
) &&
288 (sbio
->spag
[ix
].have_csum
== 0)) {
290 * nodatasum, don't try to fix anything
291 * FIXME: we can do better, open the inode and trigger a
298 ret
= btrfs_map_block(map_tree
, REQ_WRITE
, logical
, &length
,
300 if (ret
|| !multi
|| length
< PAGE_SIZE
) {
302 "scrub_fixup: btrfs_map_block failed us for %llu\n",
303 (unsigned long long)logical
);
308 if (multi
->num_stripes
== 1)
309 /* there aren't any replicas */
313 * first find a good copy
315 for (i
= 0; i
< multi
->num_stripes
; ++i
) {
316 if (i
== sbio
->spag
[ix
].mirror_num
)
319 if (scrub_fixup_io(READ
, multi
->stripes
[i
].dev
->bdev
,
320 multi
->stripes
[i
].physical
>> 9,
321 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
322 /* I/O-error, this is not a good copy */
326 if (scrub_fixup_check(sbio
, ix
) == 0)
329 if (i
== multi
->num_stripes
)
332 if (!sdev
->readonly
) {
334 * bi_io_vec[ix].bv_page now contains good data, write it back
336 if (scrub_fixup_io(WRITE
, sdev
->dev
->bdev
,
337 (sbio
->physical
+ ix
* PAGE_SIZE
) >> 9,
338 sbio
->bio
->bi_io_vec
[ix
].bv_page
)) {
339 /* I/O-error, writeback failed, give up */
345 spin_lock(&sdev
->stat_lock
);
346 ++sdev
->stat
.corrected_errors
;
347 spin_unlock(&sdev
->stat_lock
);
349 if (printk_ratelimit())
350 printk(KERN_ERR
"btrfs: fixed up at %llu\n",
351 (unsigned long long)logical
);
356 spin_lock(&sdev
->stat_lock
);
357 ++sdev
->stat
.uncorrectable_errors
;
358 spin_unlock(&sdev
->stat_lock
);
360 if (printk_ratelimit())
361 printk(KERN_ERR
"btrfs: unable to fixup at %llu\n",
362 (unsigned long long)logical
);
365 static int scrub_fixup_io(int rw
, struct block_device
*bdev
, sector_t sector
,
368 struct bio
*bio
= NULL
;
370 DECLARE_COMPLETION_ONSTACK(complete
);
372 /* we are going to wait on this IO */
375 bio
= bio_alloc(GFP_NOFS
, 1);
377 bio
->bi_sector
= sector
;
378 bio_add_page(bio
, page
, PAGE_SIZE
, 0);
379 bio
->bi_end_io
= scrub_fixup_end_io
;
380 bio
->bi_private
= &complete
;
383 wait_for_completion(&complete
);
385 ret
= !test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
390 static void scrub_bio_end_io(struct bio
*bio
, int err
)
392 struct scrub_bio
*sbio
= bio
->bi_private
;
393 struct scrub_dev
*sdev
= sbio
->sdev
;
394 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
398 btrfs_queue_worker(&fs_info
->scrub_workers
, &sbio
->work
);
401 static void scrub_checksum(struct btrfs_work
*work
)
403 struct scrub_bio
*sbio
= container_of(work
, struct scrub_bio
, work
);
404 struct scrub_dev
*sdev
= sbio
->sdev
;
413 for (i
= 0; i
< sbio
->count
; ++i
)
414 scrub_recheck_error(sbio
, i
);
416 sbio
->bio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
417 sbio
->bio
->bi_flags
|= 1 << BIO_UPTODATE
;
418 sbio
->bio
->bi_phys_segments
= 0;
419 sbio
->bio
->bi_idx
= 0;
421 for (i
= 0; i
< sbio
->count
; i
++) {
423 bi
= &sbio
->bio
->bi_io_vec
[i
];
425 bi
->bv_len
= PAGE_SIZE
;
428 spin_lock(&sdev
->stat_lock
);
429 ++sdev
->stat
.read_errors
;
430 spin_unlock(&sdev
->stat_lock
);
433 for (i
= 0; i
< sbio
->count
; ++i
) {
434 page
= sbio
->bio
->bi_io_vec
[i
].bv_page
;
435 buffer
= kmap_atomic(page
, KM_USER0
);
436 flags
= sbio
->spag
[i
].flags
;
437 logical
= sbio
->logical
+ i
* PAGE_SIZE
;
439 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
440 ret
= scrub_checksum_data(sdev
, sbio
->spag
+ i
, buffer
);
441 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
442 ret
= scrub_checksum_tree_block(sdev
, sbio
->spag
+ i
,
444 } else if (flags
& BTRFS_EXTENT_FLAG_SUPER
) {
446 (void)scrub_checksum_super(sbio
, buffer
);
450 kunmap_atomic(buffer
, KM_USER0
);
452 scrub_recheck_error(sbio
, i
);
456 spin_lock(&sdev
->list_lock
);
457 sbio
->next_free
= sdev
->first_free
;
458 sdev
->first_free
= sbio
->index
;
459 spin_unlock(&sdev
->list_lock
);
460 atomic_dec(&sdev
->in_flight
);
461 wake_up(&sdev
->list_wait
);
464 static int scrub_checksum_data(struct scrub_dev
*sdev
,
465 struct scrub_page
*spag
, void *buffer
)
467 u8 csum
[BTRFS_CSUM_SIZE
];
470 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
472 if (!spag
->have_csum
)
475 crc
= btrfs_csum_data(root
, buffer
, crc
, PAGE_SIZE
);
476 btrfs_csum_final(crc
, csum
);
477 if (memcmp(csum
, spag
->csum
, sdev
->csum_size
))
480 spin_lock(&sdev
->stat_lock
);
481 ++sdev
->stat
.data_extents_scrubbed
;
482 sdev
->stat
.data_bytes_scrubbed
+= PAGE_SIZE
;
484 ++sdev
->stat
.csum_errors
;
485 spin_unlock(&sdev
->stat_lock
);
490 static int scrub_checksum_tree_block(struct scrub_dev
*sdev
,
491 struct scrub_page
*spag
, u64 logical
,
494 struct btrfs_header
*h
;
495 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
496 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
497 u8 csum
[BTRFS_CSUM_SIZE
];
503 * we don't use the getter functions here, as we
504 * a) don't have an extent buffer and
505 * b) the page is already kmapped
507 h
= (struct btrfs_header
*)buffer
;
509 if (logical
!= le64_to_cpu(h
->bytenr
))
512 if (spag
->generation
!= le64_to_cpu(h
->generation
))
515 if (memcmp(h
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
518 if (memcmp(h
->chunk_tree_uuid
, fs_info
->chunk_tree_uuid
,
522 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
523 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
524 btrfs_csum_final(crc
, csum
);
525 if (memcmp(csum
, h
->csum
, sdev
->csum_size
))
528 spin_lock(&sdev
->stat_lock
);
529 ++sdev
->stat
.tree_extents_scrubbed
;
530 sdev
->stat
.tree_bytes_scrubbed
+= PAGE_SIZE
;
532 ++sdev
->stat
.csum_errors
;
534 ++sdev
->stat
.verify_errors
;
535 spin_unlock(&sdev
->stat_lock
);
537 return fail
|| crc_fail
;
540 static int scrub_checksum_super(struct scrub_bio
*sbio
, void *buffer
)
542 struct btrfs_super_block
*s
;
544 struct scrub_dev
*sdev
= sbio
->sdev
;
545 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
546 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
547 u8 csum
[BTRFS_CSUM_SIZE
];
551 s
= (struct btrfs_super_block
*)buffer
;
552 logical
= sbio
->logical
;
554 if (logical
!= le64_to_cpu(s
->bytenr
))
557 if (sbio
->spag
[0].generation
!= le64_to_cpu(s
->generation
))
560 if (memcmp(s
->fsid
, fs_info
->fsid
, BTRFS_UUID_SIZE
))
563 crc
= btrfs_csum_data(root
, buffer
+ BTRFS_CSUM_SIZE
, crc
,
564 PAGE_SIZE
- BTRFS_CSUM_SIZE
);
565 btrfs_csum_final(crc
, csum
);
566 if (memcmp(csum
, s
->csum
, sbio
->sdev
->csum_size
))
571 * if we find an error in a super block, we just report it.
572 * They will get written with the next transaction commit
575 spin_lock(&sdev
->stat_lock
);
576 ++sdev
->stat
.super_errors
;
577 spin_unlock(&sdev
->stat_lock
);
583 static int scrub_submit(struct scrub_dev
*sdev
)
585 struct scrub_bio
*sbio
;
587 if (sdev
->curr
== -1)
590 sbio
= sdev
->bios
[sdev
->curr
];
592 sbio
->bio
->bi_sector
= sbio
->physical
>> 9;
593 sbio
->bio
->bi_size
= sbio
->count
* PAGE_SIZE
;
594 sbio
->bio
->bi_next
= NULL
;
595 sbio
->bio
->bi_flags
|= 1 << BIO_UPTODATE
;
596 sbio
->bio
->bi_comp_cpu
= -1;
597 sbio
->bio
->bi_bdev
= sdev
->dev
->bdev
;
600 atomic_inc(&sdev
->in_flight
);
602 submit_bio(0, sbio
->bio
);
607 static int scrub_page(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
608 u64 physical
, u64 flags
, u64 gen
, u64 mirror_num
,
611 struct scrub_bio
*sbio
;
615 * grab a fresh bio or wait for one to become available
617 while (sdev
->curr
== -1) {
618 spin_lock(&sdev
->list_lock
);
619 sdev
->curr
= sdev
->first_free
;
620 if (sdev
->curr
!= -1) {
621 sdev
->first_free
= sdev
->bios
[sdev
->curr
]->next_free
;
622 sdev
->bios
[sdev
->curr
]->next_free
= -1;
623 sdev
->bios
[sdev
->curr
]->count
= 0;
624 spin_unlock(&sdev
->list_lock
);
626 spin_unlock(&sdev
->list_lock
);
627 wait_event(sdev
->list_wait
, sdev
->first_free
!= -1);
630 sbio
= sdev
->bios
[sdev
->curr
];
631 if (sbio
->count
== 0) {
632 sbio
->physical
= physical
;
633 sbio
->logical
= logical
;
634 } else if (sbio
->physical
+ sbio
->count
* PAGE_SIZE
!= physical
) {
638 sbio
->spag
[sbio
->count
].flags
= flags
;
639 sbio
->spag
[sbio
->count
].generation
= gen
;
640 sbio
->spag
[sbio
->count
].have_csum
= 0;
641 sbio
->spag
[sbio
->count
].mirror_num
= mirror_num
;
643 sbio
->spag
[sbio
->count
].have_csum
= 1;
644 memcpy(sbio
->spag
[sbio
->count
].csum
, csum
, sdev
->csum_size
);
647 if (sbio
->count
== SCRUB_PAGES_PER_BIO
|| force
)
653 static int scrub_find_csum(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
656 struct btrfs_ordered_sum
*sum
= NULL
;
659 unsigned long num_sectors
;
660 u32 sectorsize
= sdev
->dev
->dev_root
->sectorsize
;
662 while (!list_empty(&sdev
->csum_list
)) {
663 sum
= list_first_entry(&sdev
->csum_list
,
664 struct btrfs_ordered_sum
, list
);
665 if (sum
->bytenr
> logical
)
667 if (sum
->bytenr
+ sum
->len
> logical
)
670 ++sdev
->stat
.csum_discards
;
671 list_del(&sum
->list
);
678 num_sectors
= sum
->len
/ sectorsize
;
679 for (i
= 0; i
< num_sectors
; ++i
) {
680 if (sum
->sums
[i
].bytenr
== logical
) {
681 memcpy(csum
, &sum
->sums
[i
].sum
, sdev
->csum_size
);
686 if (ret
&& i
== num_sectors
- 1) {
687 list_del(&sum
->list
);
693 /* scrub extent tries to collect up to 64 kB for each bio */
694 static int scrub_extent(struct scrub_dev
*sdev
, u64 logical
, u64 len
,
695 u64 physical
, u64 flags
, u64 gen
, u64 mirror_num
)
698 u8 csum
[BTRFS_CSUM_SIZE
];
701 u64 l
= min_t(u64
, len
, PAGE_SIZE
);
704 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
705 /* push csums to sbio */
706 have_csum
= scrub_find_csum(sdev
, logical
, l
, csum
);
708 ++sdev
->stat
.no_csum
;
710 ret
= scrub_page(sdev
, logical
, l
, physical
, flags
, gen
,
711 mirror_num
, have_csum
? csum
: NULL
, 0);
721 static noinline_for_stack
int scrub_stripe(struct scrub_dev
*sdev
,
722 struct map_lookup
*map
, int num
, u64 base
, u64 length
)
724 struct btrfs_path
*path
;
725 struct btrfs_fs_info
*fs_info
= sdev
->dev
->dev_root
->fs_info
;
726 struct btrfs_root
*root
= fs_info
->extent_root
;
727 struct btrfs_root
*csum_root
= fs_info
->csum_root
;
728 struct btrfs_extent_item
*extent
;
735 struct extent_buffer
*l
;
736 struct btrfs_key key
;
742 u64 increment
= map
->stripe_len
;
747 do_div(nstripes
, map
->stripe_len
);
748 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
749 offset
= map
->stripe_len
* num
;
750 increment
= map
->stripe_len
* map
->num_stripes
;
752 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
753 int factor
= map
->num_stripes
/ map
->sub_stripes
;
754 offset
= map
->stripe_len
* (num
/ map
->sub_stripes
);
755 increment
= map
->stripe_len
* factor
;
756 mirror_num
= num
% map
->sub_stripes
;
757 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
758 increment
= map
->stripe_len
;
759 mirror_num
= num
% map
->num_stripes
;
760 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
761 increment
= map
->stripe_len
;
762 mirror_num
= num
% map
->num_stripes
;
764 increment
= map
->stripe_len
;
768 path
= btrfs_alloc_path();
773 path
->search_commit_root
= 1;
774 path
->skip_locking
= 1;
777 * find all extents for each stripe and just read them to get
778 * them into the page cache
779 * FIXME: we can do better. build a more intelligent prefetching
781 logical
= base
+ offset
;
782 physical
= map
->stripes
[num
].physical
;
784 for (i
= 0; i
< nstripes
; ++i
) {
785 key
.objectid
= logical
;
786 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
789 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
794 slot
= path
->slots
[0];
795 btrfs_item_key_to_cpu(l
, &key
, slot
);
796 if (key
.objectid
!= logical
) {
797 ret
= btrfs_previous_item(root
, path
, 0,
798 BTRFS_EXTENT_ITEM_KEY
);
805 slot
= path
->slots
[0];
806 if (slot
>= btrfs_header_nritems(l
)) {
807 ret
= btrfs_next_leaf(root
, path
);
815 btrfs_item_key_to_cpu(l
, &key
, slot
);
817 if (key
.objectid
>= logical
+ map
->stripe_len
)
822 btrfs_release_path(path
);
823 logical
+= increment
;
824 physical
+= map
->stripe_len
;
829 * collect all data csums for the stripe to avoid seeking during
830 * the scrub. This might currently (crc32) end up to be about 1MB
834 logical
= base
+ offset
+ start_stripe
* increment
;
835 for (i
= start_stripe
; i
< nstripes
; ++i
) {
836 ret
= btrfs_lookup_csums_range(csum_root
, logical
,
837 logical
+ map
->stripe_len
- 1,
838 &sdev
->csum_list
, 1);
842 logical
+= increment
;
846 * now find all extents for each stripe and scrub them
848 logical
= base
+ offset
+ start_stripe
* increment
;
849 physical
= map
->stripes
[num
].physical
+ start_stripe
* map
->stripe_len
;
851 for (i
= start_stripe
; i
< nstripes
; ++i
) {
855 if (atomic_read(&fs_info
->scrub_cancel_req
) ||
856 atomic_read(&sdev
->cancel_req
)) {
861 * check to see if we have to pause
863 if (atomic_read(&fs_info
->scrub_pause_req
)) {
864 /* push queued extents */
866 wait_event(sdev
->list_wait
,
867 atomic_read(&sdev
->in_flight
) == 0);
868 atomic_inc(&fs_info
->scrubs_paused
);
869 wake_up(&fs_info
->scrub_pause_wait
);
870 mutex_lock(&fs_info
->scrub_lock
);
871 while (atomic_read(&fs_info
->scrub_pause_req
)) {
872 mutex_unlock(&fs_info
->scrub_lock
);
873 wait_event(fs_info
->scrub_pause_wait
,
874 atomic_read(&fs_info
->scrub_pause_req
) == 0);
875 mutex_lock(&fs_info
->scrub_lock
);
877 atomic_dec(&fs_info
->scrubs_paused
);
878 mutex_unlock(&fs_info
->scrub_lock
);
879 wake_up(&fs_info
->scrub_pause_wait
);
880 scrub_free_csums(sdev
);
885 key
.objectid
= logical
;
886 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
889 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
894 slot
= path
->slots
[0];
895 btrfs_item_key_to_cpu(l
, &key
, slot
);
896 if (key
.objectid
!= logical
) {
897 ret
= btrfs_previous_item(root
, path
, 0,
898 BTRFS_EXTENT_ITEM_KEY
);
905 slot
= path
->slots
[0];
906 if (slot
>= btrfs_header_nritems(l
)) {
907 ret
= btrfs_next_leaf(root
, path
);
915 btrfs_item_key_to_cpu(l
, &key
, slot
);
917 if (key
.objectid
+ key
.offset
<= logical
)
920 if (key
.objectid
>= logical
+ map
->stripe_len
)
923 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
)
926 extent
= btrfs_item_ptr(l
, slot
,
927 struct btrfs_extent_item
);
928 flags
= btrfs_extent_flags(l
, extent
);
929 generation
= btrfs_extent_generation(l
, extent
);
931 if (key
.objectid
< logical
&&
932 (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
934 "btrfs scrub: tree block %llu spanning "
935 "stripes, ignored. logical=%llu\n",
936 (unsigned long long)key
.objectid
,
937 (unsigned long long)logical
);
942 * trim extent to this stripe
944 if (key
.objectid
< logical
) {
945 key
.offset
-= logical
- key
.objectid
;
946 key
.objectid
= logical
;
948 if (key
.objectid
+ key
.offset
>
949 logical
+ map
->stripe_len
) {
950 key
.offset
= logical
+ map
->stripe_len
-
954 ret
= scrub_extent(sdev
, key
.objectid
, key
.offset
,
955 key
.objectid
- logical
+ physical
,
956 flags
, generation
, mirror_num
);
963 btrfs_release_path(path
);
964 logical
+= increment
;
965 physical
+= map
->stripe_len
;
966 spin_lock(&sdev
->stat_lock
);
967 sdev
->stat
.last_physical
= physical
;
968 spin_unlock(&sdev
->stat_lock
);
970 /* push queued extents */
974 btrfs_free_path(path
);
975 return ret
< 0 ? ret
: 0;
978 static noinline_for_stack
int scrub_chunk(struct scrub_dev
*sdev
,
979 u64 chunk_tree
, u64 chunk_objectid
, u64 chunk_offset
, u64 length
)
981 struct btrfs_mapping_tree
*map_tree
=
982 &sdev
->dev
->dev_root
->fs_info
->mapping_tree
;
983 struct map_lookup
*map
;
984 struct extent_map
*em
;
988 read_lock(&map_tree
->map_tree
.lock
);
989 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
990 read_unlock(&map_tree
->map_tree
.lock
);
995 map
= (struct map_lookup
*)em
->bdev
;
996 if (em
->start
!= chunk_offset
)
999 if (em
->len
< length
)
1002 for (i
= 0; i
< map
->num_stripes
; ++i
) {
1003 if (map
->stripes
[i
].dev
== sdev
->dev
) {
1004 ret
= scrub_stripe(sdev
, map
, i
, chunk_offset
, length
);
1010 free_extent_map(em
);
1015 static noinline_for_stack
1016 int scrub_enumerate_chunks(struct scrub_dev
*sdev
, u64 start
, u64 end
)
1018 struct btrfs_dev_extent
*dev_extent
= NULL
;
1019 struct btrfs_path
*path
;
1020 struct btrfs_root
*root
= sdev
->dev
->dev_root
;
1021 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1028 struct extent_buffer
*l
;
1029 struct btrfs_key key
;
1030 struct btrfs_key found_key
;
1031 struct btrfs_block_group_cache
*cache
;
1033 path
= btrfs_alloc_path();
1038 path
->search_commit_root
= 1;
1039 path
->skip_locking
= 1;
1041 key
.objectid
= sdev
->dev
->devid
;
1043 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1047 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1053 slot
= path
->slots
[0];
1055 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
1057 if (found_key
.objectid
!= sdev
->dev
->devid
)
1060 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1063 if (found_key
.offset
>= end
)
1066 if (found_key
.offset
< key
.offset
)
1069 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1070 length
= btrfs_dev_extent_length(l
, dev_extent
);
1072 if (found_key
.offset
+ length
<= start
) {
1073 key
.offset
= found_key
.offset
+ length
;
1074 btrfs_release_path(path
);
1078 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
1079 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
1080 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
1083 * get a reference on the corresponding block group to prevent
1084 * the chunk from going away while we scrub it
1086 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
1091 ret
= scrub_chunk(sdev
, chunk_tree
, chunk_objectid
,
1092 chunk_offset
, length
);
1093 btrfs_put_block_group(cache
);
1097 key
.offset
= found_key
.offset
+ length
;
1098 btrfs_release_path(path
);
1102 btrfs_free_path(path
);
1106 static noinline_for_stack
int scrub_supers(struct scrub_dev
*sdev
)
1112 struct btrfs_device
*device
= sdev
->dev
;
1113 struct btrfs_root
*root
= device
->dev_root
;
1115 gen
= root
->fs_info
->last_trans_committed
;
1117 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1118 bytenr
= btrfs_sb_offset(i
);
1119 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>= device
->total_bytes
)
1122 ret
= scrub_page(sdev
, bytenr
, PAGE_SIZE
, bytenr
,
1123 BTRFS_EXTENT_FLAG_SUPER
, gen
, i
, NULL
, 1);
1127 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1133 * get a reference count on fs_info->scrub_workers. start worker if necessary
1135 static noinline_for_stack
int scrub_workers_get(struct btrfs_root
*root
)
1137 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1139 mutex_lock(&fs_info
->scrub_lock
);
1140 if (fs_info
->scrub_workers_refcnt
== 0)
1141 btrfs_start_workers(&fs_info
->scrub_workers
, 1);
1142 ++fs_info
->scrub_workers_refcnt
;
1143 mutex_unlock(&fs_info
->scrub_lock
);
1148 static noinline_for_stack
void scrub_workers_put(struct btrfs_root
*root
)
1150 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1152 mutex_lock(&fs_info
->scrub_lock
);
1153 if (--fs_info
->scrub_workers_refcnt
== 0)
1154 btrfs_stop_workers(&fs_info
->scrub_workers
);
1155 WARN_ON(fs_info
->scrub_workers_refcnt
< 0);
1156 mutex_unlock(&fs_info
->scrub_lock
);
1160 int btrfs_scrub_dev(struct btrfs_root
*root
, u64 devid
, u64 start
, u64 end
,
1161 struct btrfs_scrub_progress
*progress
, int readonly
)
1163 struct scrub_dev
*sdev
;
1164 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1166 struct btrfs_device
*dev
;
1168 if (root
->fs_info
->closing
)
1172 * check some assumptions
1174 if (root
->sectorsize
!= PAGE_SIZE
||
1175 root
->sectorsize
!= root
->leafsize
||
1176 root
->sectorsize
!= root
->nodesize
) {
1177 printk(KERN_ERR
"btrfs_scrub: size assumptions fail\n");
1181 ret
= scrub_workers_get(root
);
1185 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1186 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1187 if (!dev
|| dev
->missing
) {
1188 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1189 scrub_workers_put(root
);
1192 mutex_lock(&fs_info
->scrub_lock
);
1194 if (!dev
->in_fs_metadata
) {
1195 mutex_unlock(&fs_info
->scrub_lock
);
1196 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1197 scrub_workers_put(root
);
1201 if (dev
->scrub_device
) {
1202 mutex_unlock(&fs_info
->scrub_lock
);
1203 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1204 scrub_workers_put(root
);
1205 return -EINPROGRESS
;
1207 sdev
= scrub_setup_dev(dev
);
1209 mutex_unlock(&fs_info
->scrub_lock
);
1210 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1211 scrub_workers_put(root
);
1212 return PTR_ERR(sdev
);
1214 sdev
->readonly
= readonly
;
1215 dev
->scrub_device
= sdev
;
1217 atomic_inc(&fs_info
->scrubs_running
);
1218 mutex_unlock(&fs_info
->scrub_lock
);
1219 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1221 down_read(&fs_info
->scrub_super_lock
);
1222 ret
= scrub_supers(sdev
);
1223 up_read(&fs_info
->scrub_super_lock
);
1226 ret
= scrub_enumerate_chunks(sdev
, start
, end
);
1228 wait_event(sdev
->list_wait
, atomic_read(&sdev
->in_flight
) == 0);
1230 atomic_dec(&fs_info
->scrubs_running
);
1231 wake_up(&fs_info
->scrub_pause_wait
);
1234 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1236 mutex_lock(&fs_info
->scrub_lock
);
1237 dev
->scrub_device
= NULL
;
1238 mutex_unlock(&fs_info
->scrub_lock
);
1240 scrub_free_dev(sdev
);
1241 scrub_workers_put(root
);
1246 int btrfs_scrub_pause(struct btrfs_root
*root
)
1248 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1250 mutex_lock(&fs_info
->scrub_lock
);
1251 atomic_inc(&fs_info
->scrub_pause_req
);
1252 while (atomic_read(&fs_info
->scrubs_paused
) !=
1253 atomic_read(&fs_info
->scrubs_running
)) {
1254 mutex_unlock(&fs_info
->scrub_lock
);
1255 wait_event(fs_info
->scrub_pause_wait
,
1256 atomic_read(&fs_info
->scrubs_paused
) ==
1257 atomic_read(&fs_info
->scrubs_running
));
1258 mutex_lock(&fs_info
->scrub_lock
);
1260 mutex_unlock(&fs_info
->scrub_lock
);
1265 int btrfs_scrub_continue(struct btrfs_root
*root
)
1267 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1269 atomic_dec(&fs_info
->scrub_pause_req
);
1270 wake_up(&fs_info
->scrub_pause_wait
);
1274 int btrfs_scrub_pause_super(struct btrfs_root
*root
)
1276 down_write(&root
->fs_info
->scrub_super_lock
);
1280 int btrfs_scrub_continue_super(struct btrfs_root
*root
)
1282 up_write(&root
->fs_info
->scrub_super_lock
);
1286 int btrfs_scrub_cancel(struct btrfs_root
*root
)
1288 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1290 mutex_lock(&fs_info
->scrub_lock
);
1291 if (!atomic_read(&fs_info
->scrubs_running
)) {
1292 mutex_unlock(&fs_info
->scrub_lock
);
1296 atomic_inc(&fs_info
->scrub_cancel_req
);
1297 while (atomic_read(&fs_info
->scrubs_running
)) {
1298 mutex_unlock(&fs_info
->scrub_lock
);
1299 wait_event(fs_info
->scrub_pause_wait
,
1300 atomic_read(&fs_info
->scrubs_running
) == 0);
1301 mutex_lock(&fs_info
->scrub_lock
);
1303 atomic_dec(&fs_info
->scrub_cancel_req
);
1304 mutex_unlock(&fs_info
->scrub_lock
);
1309 int btrfs_scrub_cancel_dev(struct btrfs_root
*root
, struct btrfs_device
*dev
)
1311 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1312 struct scrub_dev
*sdev
;
1314 mutex_lock(&fs_info
->scrub_lock
);
1315 sdev
= dev
->scrub_device
;
1317 mutex_unlock(&fs_info
->scrub_lock
);
1320 atomic_inc(&sdev
->cancel_req
);
1321 while (dev
->scrub_device
) {
1322 mutex_unlock(&fs_info
->scrub_lock
);
1323 wait_event(fs_info
->scrub_pause_wait
,
1324 dev
->scrub_device
== NULL
);
1325 mutex_lock(&fs_info
->scrub_lock
);
1327 mutex_unlock(&fs_info
->scrub_lock
);
1331 int btrfs_scrub_cancel_devid(struct btrfs_root
*root
, u64 devid
)
1333 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1334 struct btrfs_device
*dev
;
1338 * we have to hold the device_list_mutex here so the device
1339 * does not go away in cancel_dev. FIXME: find a better solution
1341 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1342 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1344 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1347 ret
= btrfs_scrub_cancel_dev(root
, dev
);
1348 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1353 int btrfs_scrub_progress(struct btrfs_root
*root
, u64 devid
,
1354 struct btrfs_scrub_progress
*progress
)
1356 struct btrfs_device
*dev
;
1357 struct scrub_dev
*sdev
= NULL
;
1359 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1360 dev
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1362 sdev
= dev
->scrub_device
;
1364 memcpy(progress
, &sdev
->stat
, sizeof(*progress
));
1365 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1367 return dev
? (sdev
? 0 : -ENOTCONN
) : -ENODEV
;