btrfs: move transaction abort to the error site in btrfs_create_free_space_tree()
authorDavid Sterba <dsterba@suse.com>
Tue, 23 Jan 2024 23:23:49 +0000 (00:23 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 4 Mar 2024 15:24:48 +0000 (16:24 +0100)
The recommended pattern for transaction abort after error is to place it
right after the error is handled. That way it's easier to locate where
it failed and help debugging.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/free-space-tree.c

index 888185265f4b4cfa32aa3cbbdb5940a8084963f0..bdc2341c43e4a9b56f5a0b6d5cc4734b6e028d57 100644 (file)
@@ -1176,12 +1176,16 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
                                            BTRFS_FREE_SPACE_TREE_OBJECTID);
        if (IS_ERR(free_space_root)) {
                ret = PTR_ERR(free_space_root);
-               goto abort;
+               btrfs_abort_transaction(trans, ret);
+               btrfs_end_transaction(trans);
+               goto out_clear;
        }
        ret = btrfs_global_root_insert(free_space_root);
        if (ret) {
                btrfs_put_root(free_space_root);
-               goto abort;
+               btrfs_abort_transaction(trans, ret);
+               btrfs_end_transaction(trans);
+               goto out_clear;
        }
 
        node = rb_first_cached(&fs_info->block_group_cache_tree);
@@ -1189,8 +1193,11 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
                block_group = rb_entry(node, struct btrfs_block_group,
                                       cache_node);
                ret = populate_free_space_tree(trans, block_group);
-               if (ret)
-                       goto abort;
+               if (ret) {
+                       btrfs_abort_transaction(trans, ret);
+                       btrfs_end_transaction(trans);
+                       goto out_clear;
+               }
                node = rb_next(node);
        }
 
@@ -1206,11 +1213,9 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
        clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
        return ret;
 
-abort:
+out_clear:
        clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
        clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
-       btrfs_abort_transaction(trans, ret);
-       btrfs_end_transaction(trans);
        return ret;
 }