Skip to content

Commit fd708b8

Browse files
josefbacikkdave
authored andcommitted
Btrfs: add a extent ref verify tool
We were having corruption issues that were tied back to problems with the extent tree. In order to track them down I built this tool to try and find the culprit, which was pretty successful. If you compile with this tool on it will live verify every ref update that the fs makes and make sure it is consistent and valid. I've run this through with xfstests and haven't gotten any false positives. Thanks, Signed-off-by: Josef Bacik <jbacik@fb.com> Reviewed-by: David Sterba <dsterba@suse.com> [ update error messages, add fixup from Dan Carpenter to handle errors of read_tree_block ] Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 84f7d8e commit fd708b8

7 files changed

Lines changed: 1138 additions & 0 deletions

File tree

‎fs/btrfs/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,14 @@ config BTRFS_ASSERT
9191
any of the assertions trip. This is meant for btrfs developers only.
9292

9393
If unsure, say N.
94+
95+
config BTRFS_FS_REF_VERIFY
96+
bool "Btrfs with the ref verify tool compiled in"
97+
depends on BTRFS_FS
98+
default n
99+
help
100+
Enable run-time extent reference verification instrumentation. This
101+
is meant to be used by btrfs developers for tracking down extent
102+
reference problems or verifying they didn't break something.
103+
104+
If unsure, say N.

‎fs/btrfs/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
1313

1414
btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o
1515
btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
16+
btrfs-$(CONFIG_BTRFS_FS_REF_VERIFY) += ref-verify.o
1617

1718
btrfs-$(CONFIG_BTRFS_FS_RUN_SANITY_TESTS) += tests/free-space-tests.o \
1819
tests/extent-buffer-tests.o tests/btrfs-tests.o \

‎fs/btrfs/ctree.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,11 @@ struct btrfs_fs_info {
10971097
u32 nodesize;
10981098
u32 sectorsize;
10991099
u32 stripesize;
1100+
1101+
#ifdef CONFIG_BTRFS_FS_REF_VERIFY
1102+
spinlock_t ref_verify_lock;
1103+
struct rb_root block_tree;
1104+
#endif
11001105
};
11011106

11021107
static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)

‎fs/btrfs/disk-io.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "qgroup.h"
5252
#include "compression.h"
5353
#include "tree-checker.h"
54+
#include "ref-verify.h"
5455

5556
#ifdef CONFIG_X86
5657
#include <asm/cpufeature.h>
@@ -2509,6 +2510,7 @@ int open_ctree(struct super_block *sb,
25092510
/* readahead state */
25102511
INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
25112512
spin_lock_init(&fs_info->reada_lock);
2513+
btrfs_init_ref_verify(fs_info);
25122514

25132515
fs_info->thread_pool_size = min_t(unsigned long,
25142516
num_online_cpus() + 2, 8);
@@ -2920,6 +2922,9 @@ int open_ctree(struct super_block *sb,
29202922
if (ret)
29212923
goto fail_trans_kthread;
29222924

2925+
if (btrfs_build_ref_tree(fs_info))
2926+
btrfs_err(fs_info, "couldn't build ref tree");
2927+
29232928
/* do not make disk changes in broken FS or nologreplay is given */
29242929
if (btrfs_super_log_root(disk_super) != 0 &&
29252930
!btrfs_test_opt(fs_info, NOLOGREPLAY)) {
@@ -3785,6 +3790,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
37853790
cleanup_srcu_struct(&fs_info->subvol_srcu);
37863791

37873792
btrfs_free_stripe_hash_table(fs_info);
3793+
btrfs_free_ref_cache(fs_info);
37883794

37893795
__btrfs_free_block_rsv(root->orphan_block_rsv);
37903796
root->orphan_block_rsv = NULL;

‎fs/btrfs/extent-tree.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "math.h"
3939
#include "sysfs.h"
4040
#include "qgroup.h"
41+
#include "ref-verify.h"
4142

4243
#undef SCRAMBLE_DELAYED_REFS
4344

@@ -2188,6 +2189,9 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
21882189
BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
21892190
root_objectid == BTRFS_TREE_LOG_OBJECTID);
21902191

2192+
btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2193+
owner, offset, BTRFS_ADD_DELAYED_REF);
2194+
21912195
if (owner < BTRFS_FIRST_FREE_OBJECTID) {
21922196
ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
21932197
num_bytes, parent,
@@ -7280,6 +7284,10 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
72807284
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
72817285
int old_ref_mod, new_ref_mod;
72827286

7287+
btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7288+
root->root_key.objectid,
7289+
btrfs_header_level(buf), 0,
7290+
BTRFS_DROP_DELAYED_REF);
72837291
ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
72847292
buf->len, parent,
72857293
root->root_key.objectid,
@@ -7343,6 +7351,11 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
73437351
if (btrfs_is_testing(fs_info))
73447352
return 0;
73457353

7354+
if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7355+
btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7356+
root_objectid, owner, offset,
7357+
BTRFS_DROP_DELAYED_REF);
7358+
73467359
/*
73477360
* tree log blocks never actually go into the extent allocation
73487361
* tree, just update pinning info and exit early.
@@ -8318,6 +8331,10 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
83188331

83198332
BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
83208333

8334+
btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8335+
root->root_key.objectid, owner, offset,
8336+
BTRFS_ADD_DELAYED_EXTENT);
8337+
83218338
ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
83228339
ins->offset, 0,
83238340
root->root_key.objectid, owner,
@@ -8542,6 +8559,9 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
85428559
extent_op->is_data = false;
85438560
extent_op->level = level;
85448561

8562+
btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8563+
root_objectid, level, 0,
8564+
BTRFS_ADD_DELAYED_EXTENT);
85458565
ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
85468566
ins.offset, parent,
85478567
root_objectid, level,
@@ -10391,6 +10411,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1039110411
* remove it.
1039210412
*/
1039310413
free_excluded_extents(fs_info, block_group);
10414+
btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10415+
block_group->key.offset);
1039410416

1039510417
memcpy(&key, &block_group->key, sizeof(key));
1039610418
index = get_block_group_index(block_group);

0 commit comments

Comments
 (0)