]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
btrfs: reduce nesting and deduplicate error handling at btrfs_iget_path()
authorFilipe Manana <fdmanana@suse.com>
Mon, 29 Apr 2024 13:01:09 +0000 (14:01 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 11 Jul 2024 13:33:17 +0000 (15:33 +0200)
Make btrfs_iget_path() simpler and easier to read by avoiding nesting of
if-then-else statements and having an error label to do all the error
handling instead of repeating it a couple times.

Reviewed-by: Qu Wenruo <wqu@suse.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/inode.c

index 0e667fecfcb23a920cb427fc996caa68037b6c84..e05915133fd0c9cd4f1e12e42f7f361fe053c2d8 100644 (file)
@@ -5598,37 +5598,35 @@ struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
                              struct btrfs_root *root, struct btrfs_path *path)
 {
        struct inode *inode;
+       int ret;
 
        inode = btrfs_iget_locked(s, ino, root);
        if (!inode)
                return ERR_PTR(-ENOMEM);
 
-       if (inode->i_state & I_NEW) {
-               int ret;
+       if (!(inode->i_state & I_NEW))
+               return inode;
 
-               ret = btrfs_read_locked_inode(inode, path);
-               if (!ret) {
-                       ret = btrfs_add_inode_to_root(BTRFS_I(inode), true);
-                       if (ret) {
-                               iget_failed(inode);
-                               inode = ERR_PTR(ret);
-                       } else {
-                               unlock_new_inode(inode);
-                       }
-               } else {
-                       iget_failed(inode);
-                       /*
-                        * ret > 0 can come from btrfs_search_slot called by
-                        * btrfs_read_locked_inode, this means the inode item
-                        * was not found.
-                        */
-                       if (ret > 0)
-                               ret = -ENOENT;
-                       inode = ERR_PTR(ret);
-               }
-       }
+       ret = btrfs_read_locked_inode(inode, path);
+       /*
+        * ret > 0 can come from btrfs_search_slot called by
+        * btrfs_read_locked_inode(), this means the inode item was not found.
+        */
+       if (ret > 0)
+               ret = -ENOENT;
+       if (ret < 0)
+               goto error;
+
+       ret = btrfs_add_inode_to_root(BTRFS_I(inode), true);
+       if (ret < 0)
+               goto error;
+
+       unlock_new_inode(inode);
 
        return inode;
+error:
+       iget_failed(inode);
+       return ERR_PTR(ret);
 }
 
 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)