Btrfs: add a extent ref verify tool V3
PATCH 1/1 Btrfs: add a extent ref verify tool V3
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 <[email protected]>
---V2->V3:
-use inline stubs instead of defines for the off case
V1->V2:
-fixed unlock problems when hitting errors
-handle shared refs properly when building cache
-deal with tree log replay properly
fs/btrfs/Kconfig | 10 + fs/btrfs/Makefile | 1 + fs/btrfs/ctree.c | 2 +- fs/btrfs/ctree.h | 7 + fs/btrfs/disk-io.c | 14 + fs/btrfs/extent-tree.c | 18 + fs/btrfs/ref-verify.c | 994 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ref-verify.h | 51 +++ fs/btrfs/relocation.c | 1 + 9 files changed, 1097 insertions(+), 1 deletion(-) create mode 100644 fs/btrfs/ref-verify.c create mode 100644 fs/btrfs/ref-verify.h
fs/btrfs/Kconfig +10 -0
@@ -88,3 +88,13 @@ config BTRFS_ASSERT any of the assertions trip. This is meant for btrfs developers only. If unsure, say N. + +config BTRFS_FS_REF_VERIFY + bool "Btrfs with the ref verify tool compiled in" + depends on BTRFS_FS + help + Enable run-time extent reference verification instrumentation. This + is meant to be used by btrfs developers for tracking down extent + reference problems or verifying they didn't break something. + + If unsure, say N.
fs/btrfs/Makefile +1 -0
@@ -13,6 +13,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o +btrfs-$(CONFIG_BTRFS_FS_REF_VERIFY) += ref-verify.o btrfs-$(CONFIG_BTRFS_FS_RUN_SANITY_TESTS) += tests/free-space-tests.o \ tests/extent-buffer-tests.o tests/btrfs-tests.o \
fs/btrfs/ctree.c +1 -1
@@ -202,7 +202,7 @@ struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)* tree until you end up with a lock on the root. A locked buffer * is returned, with a reference held. */ -static struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root) +struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root) {struct extent_buffer *eb;
fs/btrfs/ctree.h +7 -0
@@ -1691,6 +1691,12 @@ struct btrfs_fs_info {struct semaphore uuid_tree_rescan_sem; unsigned int update_uuid_tree_gen:1; + +#ifdef CONFIG_BTRFS_FS_REF_VERIFY +spinlock_t ref_verify_lock; +struct rb_root block_tree; +bool ref_verify_enabled; +#endif };struct btrfs_subvolume_writers {
@@ -3404,6 +3410,7 @@ void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path, struct btrfs_key *new_key);struct extent_buffer *btrfs_root_node (struct btrfs_root *root);struct extent_buffer *btrfs_lock_root_node (struct btrfs_root *root); +struct extent_buffer *btrfs_read_lock_root_node (struct btrfs_root *root);int btrfs_find_next_key (struct btrfs_root *root,struct btrfs_path *path,struct btrfs_key *key,int lowest_level,u64 min_trans);
fs/btrfs/disk-io.c +14 -0
@@ -50,6 +50,7 @@#include "raid56.h" #include "sysfs.h" #include "qgroup.h" +#include "ref-verify.h" #ifdef CONFIG_X86 #include <asm/cpufeature.h>
@@ -2295,6 +2296,11 @@ int open_ctree(struct super_block *sb,#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY fs_info->check_integrity_print_mask =0 ;#endif +#ifdef CONFIG_BTRFS_FS_REF_VERIFY +spin_lock_init (&fs_info->ref_verify_lock); + fs_info->block_tree =RB_ROOT ; + fs_info->ref_verify_enabled = true; +#endif spin_lock_init (&fs_info->balance_lock);mutex_init (&fs_info->balance_mutex);
@@ -2843,6 +2849,13 @@ retry_root_backup:if (ret) goto fail_trans_kthread; +#ifdef CONFIG_BTRFS_FS_REF_VERIFY +if (btrfs_build_ref_tree (fs_info)) { + fs_info->ref_verify_enabled = false; +printk (KERN_ERR "BTRFS: couldn't build ref tree\n" ); + } +#endif +/* do not make disk changes in broken FS */ if (btrfs_super_log_root (disk_super) !=0 ) {u64 bytenr =btrfs_super_log_root (disk_super);
@@ -3669,6 +3682,7 @@ int close_ctree(struct btrfs_root *root)cleanup_srcu_struct (&fs_info->subvol_srcu);btrfs_free_stripe_hash_table (fs_info); +btrfs_free_ref_cache (fs_info);btrfs_free_block_rsv (root, root->orphan_block_rsv); root->orphan_block_rsv =NULL ;
fs/btrfs/extent-tree.c +18 -0
@@ -37,6 +37,7 @@#include "math.h" #include "sysfs.h" #include "qgroup.h" +#include "ref-verify.h" #undef SCRAMBLE_DELAYED_REFS
@@ -1963,6 +1964,9 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,BUG_ON (owner <BTRFS_FIRST_FREE_OBJECTID && root_objectid ==BTRFS_TREE_LOG_OBJECTID ); +btrfs_ref_tree_mod (root, bytenr, num_bytes, parent, root_objectid, + owner, offset,BTRFS_ADD_DELAYED_REF ); +if (owner <BTRFS_FIRST_FREE_OBJECTID ) { ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, num_bytes,
@@ -5999,6 +6003,9 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,int ret;if (root->root_key.objectid !=BTRFS_TREE_LOG_OBJECTID ) { +btrfs_ref_tree_mod (root, buf->start, buf->len, parent, + root->objectid,btrfs_header_level (buf), +0 ,BTRFS_DROP_DELAYED_REF ); ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, buf->start, buf->len, parent, root->root_key.objectid,
@@ -6059,6 +6066,9 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,#endif add_pinned_bytes (root->fs_info, num_bytes, owner, root_objectid); +btrfs_ref_tree_mod (root, bytenr, num_bytes, parent, root_objectid, + owner, offset,BTRFS_DROP_DELAYED_REF ); +/* * tree log blocks never actually go into the extent allocation * tree, just update pinning info and exit early.
@@ -6893,6 +6903,9 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,BUG_ON (root_objectid ==BTRFS_TREE_LOG_OBJECTID ); +btrfs_ref_tree_mod (root, ins->objectid, ins->offset,0 , root_objectid, + owner, offset,BTRFS_ADD_DELAYED_EXTENT ); + ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, ins->offset, 0, root_objectid, owner, offset,
@@ -7104,6 +7117,9 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, extent_op->is_data =0 ; extent_op->level = level; +btrfs_ref_tree_mod (root, ins.objectid, ins.offset, parent, + root_objectid, level,0 , +BTRFS_ADD_DELAYED_EXTENT ); ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, ins.objectid, ins.offset, parent, root_objectid,
@@ -8783,6 +8799,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,* remove it. */ free_excluded_extents (root, block_group); +btrfs_free_ref_tree_range (root->fs_info, block_group->key.objectid, + block_group->key.offset);memcpy (&key, &block_group->key,sizeof (key)); index =get_block_group_index (block_group);
fs/btrfs/ref-verify.c new file +994 -0
994 lines not on this page · raw message
fs/btrfs/ref-verify.h new file +51 -0
51 lines not on this page · raw message
fs/btrfs/relocation.c +1 -0
@@ -31,6 +31,7 @@#include "async-thread.h" #include "free-space-cache.h" #include "inode-map.h" +#include "ref-verify.h" /* * backref_node, mapping_node and tree_block start with this
Signature · 1 line
1.8.3.1