@@ -902,6 +902,10 @@ struct btrfs_fs_info {
spinlock_t ref_verify_lock;
struct rb_root block_tree;
#endif
+
+#ifdef CONFIG_BTRFS_DEBUG
+ struct list_head alloced_roots;
+#endif
};
static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
@@ -1103,6 +1107,10 @@ struct btrfs_root {
#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
u64 alloc_bytenr;
#endif
+
+#ifdef CONFIG_BTRFS_DEBUG
+ struct list_head leak_list;
+#endif
};
struct btrfs_clone_extent_info {
@@ -1204,6 +1204,14 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
spin_lock_init(&root->root_item_lock);
btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
+#ifdef CONFIG_BTRFS_DEBUG
+ INIT_LIST_HEAD(&root->leak_list);
+ if (!dummy) {
+ spin_lock(&fs_info->fs_roots_radix_lock);
+ list_add_tail(&root->leak_list, &fs_info->alloced_roots);
+ spin_unlock(&fs_info->fs_roots_radix_lock);
+ }
+#endif
}
static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
@@ -1434,6 +1442,7 @@ struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
goto find_fail;
}
root->commit_root = btrfs_root_node(root);
+
out:
btrfs_free_path(path);
return root;
@@ -1533,6 +1542,31 @@ int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
return ret;
}
+static void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
+{
+#ifdef CONFIG_BTRFS_DEBUG
+ struct btrfs_root *root;
+
+ /*
+ * If we didn't get to open_ctree our alloced_roots won't be init'ed, so
+ * do this check just in case.
+ */
+ if (fs_info->alloced_roots.next == NULL)
+ return;
+
+ while (!list_empty(&fs_info->alloced_roots)) {
+ root = list_first_entry(&fs_info->alloced_roots,
+ struct btrfs_root, leak_list);
+ btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+ root->root_key.objectid, root->root_key.offset,
+ refcount_read(&root->refs));
+ while (refcount_read(&root->refs) > 1)
+ btrfs_put_fs_root(root);
+ btrfs_put_fs_root(root);
+ }
+#endif
+}
+
void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
{
percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
@@ -1553,6 +1587,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
btrfs_put_fs_root(fs_info->uuid_root);
btrfs_put_fs_root(fs_info->free_space_root);
btrfs_put_fs_root(fs_info->fs_root);
+ btrfs_check_leaked_roots(fs_info);
kfree(fs_info->super_copy);
kfree(fs_info->super_for_commit);
kvfree(fs_info);
@@ -2680,6 +2715,9 @@ static int init_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
INIT_LIST_HEAD(&fs_info->space_info);
INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
INIT_LIST_HEAD(&fs_info->unused_bgs);
+#ifdef CONFIG_BTRFS_DEBUG
+ INIT_LIST_HEAD(&fs_info->alloced_roots);
+#endif
extent_map_tree_init(&fs_info->mapping_tree);
btrfs_init_block_rsv(&fs_info->global_block_rsv,
BTRFS_BLOCK_RSV_GLOBAL);
@@ -99,8 +99,14 @@ static inline void btrfs_put_fs_root(struct btrfs_root *root)
if (!root)
return;
- if (refcount_dec_and_test(&root->refs))
+ if (refcount_dec_and_test(&root->refs)) {
+#ifdef CONFIG_BTRFS_DEBUG
+ spin_lock(&root->fs_info->fs_roots_radix_lock);
+ list_del_init(&root->leak_list);
+ spin_unlock(&root->fs_info->fs_roots_radix_lock);
+#endif
kfree(root);
+ }
}
void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
Now that we're going to start relying on getting ref counting right for roots, add a list to track allocated roots and print out any roots that aren't free'd up at free_fs_info time. Hide this behind CONFIG_BTRFS_DEBUG because this will just be used for developers to verify they aren't breaking things. Signed-off-by: Josef Bacik <josef@toxicpanda.com> --- fs/btrfs/ctree.h | 8 ++++++++ fs/btrfs/disk-io.c | 38 ++++++++++++++++++++++++++++++++++++++ fs/btrfs/disk-io.h | 8 +++++++- 3 files changed, 53 insertions(+), 1 deletion(-)