diff mbox series

[3/8] btrfs: reduce nesting and deduplicate error handling at btrfs_iget_path()

Message ID ca3427c0ca2b7984b0d074531cc31b0433b7eb56.1715169723.git.fdmanana@suse.com (mailing list archive)
State New
Headers show
Series btrfs: inode management and memory consumption improvements | expand

Commit Message

Filipe Manana May 8, 2024, 12:17 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

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.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/inode.c | 44 +++++++++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 23 deletions(-)

Comments

Qu Wenruo May 9, 2024, 12:23 a.m. UTC | #1
在 2024/5/8 21:47, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
>
> 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.
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu
> ---
>   fs/btrfs/inode.c | 44 +++++++++++++++++++++-----------------------
>   1 file changed, 21 insertions(+), 23 deletions(-)
>
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 85dbc19c2f6f..8ea9fd4c2b66 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -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)
diff mbox series

Patch

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 85dbc19c2f6f..8ea9fd4c2b66 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -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)