Skip to content

Commit a2de733

Browse files
author
Arne Jansen
committed
btrfs: scrub
This adds an initial implementation for scrub. It works quite straightforward. The usermode issues an ioctl for each device in the fs. For each device, it enumerates the allocated device chunks. For each chunk, the contained extents are enumerated and the data checksums fetched. The extents are read sequentially and the checksums verified. If an error occurs (checksum or EIO), a good copy is searched for. If one is found, the bad copy will be rewritten. All enumerations happen from the commit roots. During a transaction commit, the scrubs get paused and afterwards continue from the new roots. This commit is based on the series originally posted to linux-btrfs with some improvements that resulted from comments from David Sterba, Ilya Dryomov and Jan Schmidt. Signed-off-by: Arne Jansen <sensille@gmx.net>
1 parent 7cf96da commit a2de733

12 files changed

Lines changed: 1600 additions & 11 deletions

File tree

‎fs/btrfs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
77
extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
88
extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \
99
export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \
10-
compression.o delayed-ref.o relocation.o
10+
compression.o delayed-ref.o relocation.o scrub.o

‎fs/btrfs/ctree.h‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/mm.h>
2424
#include <linux/highmem.h>
2525
#include <linux/fs.h>
26+
#include <linux/rwsem.h>
2627
#include <linux/completion.h>
2728
#include <linux/backing-dev.h>
2829
#include <linux/wait.h>
@@ -33,6 +34,7 @@
3334
#include "extent_io.h"
3435
#include "extent_map.h"
3536
#include "async-thread.h"
37+
#include "ioctl.h"
3638

3739
struct btrfs_trans_handle;
3840
struct btrfs_transaction;
@@ -510,6 +512,12 @@ struct btrfs_extent_item_v0 {
510512
/* use full backrefs for extent pointers in the block */
511513
#define BTRFS_BLOCK_FLAG_FULL_BACKREF (1ULL << 8)
512514

515+
/*
516+
* this flag is only used internally by scrub and may be changed at any time
517+
* it is only declared here to avoid collisions
518+
*/
519+
#define BTRFS_EXTENT_FLAG_SUPER (1ULL << 48)
520+
513521
struct btrfs_tree_block_info {
514522
struct btrfs_disk_key key;
515523
u8 level;
@@ -1077,6 +1085,17 @@ struct btrfs_fs_info {
10771085

10781086
void *bdev_holder;
10791087

1088+
/* private scrub information */
1089+
struct mutex scrub_lock;
1090+
atomic_t scrubs_running;
1091+
atomic_t scrub_pause_req;
1092+
atomic_t scrubs_paused;
1093+
atomic_t scrub_cancel_req;
1094+
wait_queue_head_t scrub_pause_wait;
1095+
struct rw_semaphore scrub_super_lock;
1096+
int scrub_workers_refcnt;
1097+
struct btrfs_workers scrub_workers;
1098+
10801099
/* filesystem state */
10811100
u64 fs_state;
10821101
};
@@ -2472,8 +2491,8 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
24722491
int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
24732492
struct btrfs_root *root, struct btrfs_path *path,
24742493
u64 isize);
2475-
int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start,
2476-
u64 end, struct list_head *list);
2494+
int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
2495+
struct list_head *list, int search_commit);
24772496
/* inode.c */
24782497

24792498
/* RHEL and EL kernels have a patch that renames PG_checked to FsMisc */
@@ -2637,4 +2656,18 @@ void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
26372656
u64 *bytes_to_reserve);
26382657
void btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
26392658
struct btrfs_pending_snapshot *pending);
2659+
2660+
/* scrub.c */
2661+
int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
2662+
struct btrfs_scrub_progress *progress);
2663+
int btrfs_scrub_pause(struct btrfs_root *root);
2664+
int btrfs_scrub_pause_super(struct btrfs_root *root);
2665+
int btrfs_scrub_continue(struct btrfs_root *root);
2666+
int btrfs_scrub_continue_super(struct btrfs_root *root);
2667+
int btrfs_scrub_cancel(struct btrfs_root *root);
2668+
int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev);
2669+
int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid);
2670+
int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2671+
struct btrfs_scrub_progress *progress);
2672+
26402673
#endif

‎fs/btrfs/disk-io.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,17 @@ struct btrfs_root *open_ctree(struct super_block *sb,
17731773
INIT_LIST_HEAD(&fs_info->ordered_extents);
17741774
spin_lock_init(&fs_info->ordered_extent_lock);
17751775

1776+
mutex_init(&fs_info->scrub_lock);
1777+
atomic_set(&fs_info->scrubs_running, 0);
1778+
atomic_set(&fs_info->scrub_pause_req, 0);
1779+
atomic_set(&fs_info->scrubs_paused, 0);
1780+
atomic_set(&fs_info->scrub_cancel_req, 0);
1781+
init_waitqueue_head(&fs_info->scrub_pause_wait);
1782+
init_rwsem(&fs_info->scrub_super_lock);
1783+
fs_info->scrub_workers_refcnt = 0;
1784+
btrfs_init_workers(&fs_info->scrub_workers, "scrub",
1785+
fs_info->thread_pool_size, &fs_info->generic_worker);
1786+
17761787
sb->s_blocksize = 4096;
17771788
sb->s_blocksize_bits = blksize_bits(4096);
17781789
sb->s_bdi = &fs_info->bdi;
@@ -2599,6 +2610,7 @@ int close_ctree(struct btrfs_root *root)
25992610
fs_info->closing = 1;
26002611
smp_mb();
26012612

2613+
btrfs_scrub_cancel(root);
26022614
btrfs_put_block_group_cache(fs_info);
26032615

26042616
/*

‎fs/btrfs/file-item.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
266266
}
267267

268268
int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
269-
struct list_head *list)
269+
struct list_head *list, int search_commit)
270270
{
271271
struct btrfs_key key;
272272
struct btrfs_path *path;
@@ -283,6 +283,12 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
283283
path = btrfs_alloc_path();
284284
BUG_ON(!path);
285285

286+
if (search_commit) {
287+
path->skip_locking = 1;
288+
path->reada = 2;
289+
path->search_commit_root = 1;
290+
}
291+
286292
key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
287293
key.offset = start;
288294
key.type = BTRFS_EXTENT_CSUM_KEY;

‎fs/btrfs/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ static noinline int csum_exist_in_range(struct btrfs_root *root,
10071007
LIST_HEAD(list);
10081008

10091009
ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1010-
bytenr + num_bytes - 1, &list);
1010+
bytenr + num_bytes - 1, &list, 0);
10111011
if (ret == 0 && list_empty(&list))
10121012
return 0;
10131013

‎fs/btrfs/ioctl.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ struct btrfs_ioctl_vol_args_v2 {
4242
char name[BTRFS_SUBVOL_NAME_MAX + 1];
4343
};
4444

45+
/*
46+
* structure to report errors and progress to userspace, either as a
47+
* result of a finished scrub, a canceled scrub or a progress inquiry
48+
*/
49+
struct btrfs_scrub_progress {
50+
__u64 data_extents_scrubbed; /* # of data extents scrubbed */
51+
__u64 tree_extents_scrubbed; /* # of tree extents scrubbed */
52+
__u64 data_bytes_scrubbed; /* # of data bytes scrubbed */
53+
__u64 tree_bytes_scrubbed; /* # of tree bytes scrubbed */
54+
__u64 read_errors; /* # of read errors encountered (EIO) */
55+
__u64 csum_errors; /* # of failed csum checks */
56+
__u64 verify_errors; /* # of occurences, where the metadata
57+
* of a tree block did not match the
58+
* expected values, like generation or
59+
* logical */
60+
__u64 no_csum; /* # of 4k data block for which no csum
61+
* is present, probably the result of
62+
* data written with nodatasum */
63+
__u64 csum_discards; /* # of csum for which no data was found
64+
* in the extent tree. */
65+
__u64 super_errors; /* # of bad super blocks encountered */
66+
__u64 malloc_errors; /* # of internal kmalloc errors. These
67+
* will likely cause an incomplete
68+
* scrub */
69+
__u64 uncorrectable_errors; /* # of errors where either no intact
70+
* copy was found or the writeback
71+
* failed */
72+
__u64 corrected_errors; /* # of errors corrected */
73+
__u64 last_physical; /* last physical address scrubbed. In
74+
* case a scrub was aborted, this can
75+
* be used to restart the scrub */
76+
__u64 unverified_errors; /* # of occurences where a read for a
77+
* full (64k) bio failed, but the re-
78+
* check succeeded for each 4k piece.
79+
* Intermittent error. */
80+
};
81+
4582
#define BTRFS_INO_LOOKUP_PATH_MAX 4080
4683
struct btrfs_ioctl_ino_lookup_args {
4784
__u64 treeid;

‎fs/btrfs/relocation.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4242,7 +4242,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
42424242

42434243
disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
42444244
ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4245-
disk_bytenr + len - 1, &list);
4245+
disk_bytenr + len - 1, &list, 0);
42464246

42474247
while (!list_empty(&list)) {
42484248
sums = list_entry(list.next, struct btrfs_ordered_sum, list);

0 commit comments

Comments
 (0)