From c4164edeb5c6608f57085e87a5f1705e14322cad Mon Sep 17 00:00:00 2001 From: Josef Bacik Date: Mon, 7 Mar 2022 17:10:59 -0500 Subject: [PATCH] btrfs-progs: add a btrfs_delete_and_free_root helper The free space tree code already does this, but we need it for cleaning up per block group roots. Abstract this code out into a helper so that we can use it in multiple places in the future. Signed-off-by: Josef Bacik Signed-off-by: David Sterba --- kernel-shared/disk-io.c | 25 +++++++++++++++++++++++++ kernel-shared/disk-io.h | 2 ++ kernel-shared/free-space-tree.c | 24 +++--------------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index 59c46946..f3ddf9e3 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c @@ -2399,6 +2399,31 @@ int btrfs_set_buffer_uptodate(struct extent_buffer *eb) return set_extent_buffer_uptodate(eb); } +int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_root *tree_root = fs_info->tree_root; + int ret; + + ret = btrfs_del_root(trans, tree_root, &root->root_key); + if (ret) + return ret; + + list_del(&root->dirty_list); + ret = clean_tree_block(root->node); + if (ret) + return ret; + ret = btrfs_free_tree_block(trans, root, root->node, 0, 1); + if (ret) + return ret; + rb_erase(&root->rb_node, &fs_info->global_roots_tree); + free_extent_buffer(root->node); + free_extent_buffer(root->commit_root); + kfree(root); + return 0; +} + struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 objectid) diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h index 81d8670f..1e97b9ac 100644 --- a/kernel-shared/disk-io.h +++ b/kernel-shared/disk-io.h @@ -221,6 +221,8 @@ int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2); struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 objectid); +int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, + struct btrfs_root *root); struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr); struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_inf, u64 bytenr); struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index a82865d3..0a13b1d6 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1257,27 +1257,9 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) if (ret) goto abort; - ret = btrfs_del_root(trans, tree_root, &free_space_root->root_key); - if (ret) - goto abort; - - list_del(&free_space_root->dirty_list); - - ret = clean_tree_block(free_space_root->node); - if (ret) - goto abort; - ret = btrfs_free_tree_block(trans, free_space_root, - free_space_root->node, 0, 1); - if (ret) - goto abort; - - rb_erase(&free_space_root->rb_node, &fs_info->global_roots_tree); - free_extent_buffer(free_space_root->node); - free_extent_buffer(free_space_root->commit_root); - kfree(free_space_root); - - ret = btrfs_commit_transaction(trans, tree_root); - + ret = btrfs_delete_and_free_root(trans, free_space_root); + if (!ret) + ret = btrfs_commit_transaction(trans, tree_root); abort: return ret; }