]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: use single return variable in btrfs_find_orphan_roots()
authorFilipe Manana <fdmanana@suse.com>
Tue, 16 Dec 2025 11:56:04 +0000 (11:56 +0000)
committerDavid Sterba <dsterba@suse.com>
Tue, 3 Feb 2026 06:49:11 +0000 (07:49 +0100)
We use both 'ret' and 'err' which is a pattern that generates confusion
and resulted in subtle bugs in the past. Remove 'err' and use only 'ret'.
Also move simplify the error flow by directly returning from the function
instead of breaking of the loop, since there are no resources to cleanup
after the loop.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/root-tree.c

index 6a7e297ab0a7a147a8e54231435df42eff8dbcc4..a7171112d638f64f9edbd68f60f37b2845411d1d 100644 (file)
@@ -217,8 +217,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
        BTRFS_PATH_AUTO_FREE(path);
        struct btrfs_key key;
        struct btrfs_root *root;
-       int err = 0;
-       int ret;
 
        path = btrfs_alloc_path();
        if (!path)
@@ -230,20 +228,19 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
        while (1) {
                u64 root_objectid;
+               int ret;
 
                ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
-               if (ret < 0) {
-                       err = ret;
-                       break;
-               }
+               if (ret < 0)
+                       return ret;
 
                leaf = path->nodes[0];
                if (path->slots[0] >= btrfs_header_nritems(leaf)) {
                        ret = btrfs_next_leaf(tree_root, path);
                        if (ret < 0)
-                               err = ret;
-                       if (ret != 0)
-                               break;
+                               return ret;
+                       else if (ret > 0)
+                               return 0;
                        leaf = path->nodes[0];
                }
 
@@ -252,34 +249,33 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
                if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
                    key.type != BTRFS_ORPHAN_ITEM_KEY)
-                       break;
+                       return 0;
 
                root_objectid = key.offset;
                key.offset++;
 
                root = btrfs_get_fs_root(fs_info, root_objectid, false);
-               err = PTR_ERR_OR_ZERO(root);
-               if (err && err != -ENOENT) {
+               ret = PTR_ERR_OR_ZERO(root);
+               if (ret && ret != -ENOENT) {
                        break;
-               } else if (err == -ENOENT) {
+               } else if (ret == -ENOENT) {
                        struct btrfs_trans_handle *trans;
 
                        btrfs_release_path(path);
 
                        trans = btrfs_join_transaction(tree_root);
                        if (IS_ERR(trans)) {
-                               err = PTR_ERR(trans);
-                               btrfs_handle_fs_error(fs_info, err,
+                               ret = PTR_ERR(trans);
+                               btrfs_handle_fs_error(fs_info, ret,
                                            "Failed to start trans to delete orphan item");
-                               break;
+                               return ret;
                        }
-                       err = btrfs_del_orphan_item(trans, tree_root,
-                                                   root_objectid);
+                       ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
                        btrfs_end_transaction(trans);
-                       if (err) {
-                               btrfs_handle_fs_error(fs_info, err,
+                       if (ret) {
+                               btrfs_handle_fs_error(fs_info, ret,
                                            "Failed to delete root orphan item");
-                               break;
+                               return ret;
                        }
                        continue;
                }
@@ -307,7 +303,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
                btrfs_put_root(root);
        }
 
-       return err;
+       return 0;
 }
 
 /* drop the root item for 'key' from the tree root */