btrfs: Remove err variable from btrfs_get_extent
diff mbox series

Message ID 20200803095846.3623-1-nborisov@suse.com
State New
Headers show
Series
  • btrfs: Remove err variable from btrfs_get_extent
Related show

Commit Message

Nikolay Borisov Aug. 3, 2020, 9:58 a.m. UTC
There's no practical reason too use 'err' as a variable to convey
errors. In fact it's value is either set explicitly in the beginning of
the function or it simply takes the value of 'ret'. Not conforming to
the usual pattern of having ret be the only variable used to convey
errors makes the code more error prone to bugs. In fact one such bug
was introduced by 6bf9e4bd6a27 ("btrfs: inode: Verify inode mode toi
avoid NULL pointer dereference") by assigning the error value to 'ret'
and not 'err'.

Let's fix that issue and make the function less tricky by leaving only
ret to convey error values.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
David, I'm not sure what's the best way forward - merge Pavel's patch and CC it
for stable and then apply this one just to upstream or use this one for both
upstream + stable ?


 fs/btrfs/inode.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

--
2.17.1

Comments

David Sterba Aug. 24, 2020, 4:59 p.m. UTC | #1
On Mon, Aug 03, 2020 at 12:58:46PM +0300, Nikolay Borisov wrote:
> @@ -6713,32 +6708,33 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
>  		goto insert;
>  	}
>  not_found:
> +	ret = 0;
>  	em->start = start;
>  	em->orig_start = start;
>  	em->len = len;
>  	em->block_start = EXTENT_MAP_HOLE;
>  insert:
> +	ret = 0;

It's not necessary to set ret = 0 in not_found, there's no conditional
until this line keeping it for 'insert' should be sufficient.

>  	btrfs_release_path(path);
>  	if (em->start > start || extent_map_end(em) <= start) {
>  		btrfs_err(fs_info,
>  			  "bad extent! em: [%llu %llu] passed [%llu %llu]",
>  			  em->start, em->len, start, len);
> -		err = -EIO;
> +		ret = -EIO;
>  		goto out;
>  	}
> 
> -	err = 0;
>  	write_lock(&em_tree->lock);
> -	err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
> +	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
>  	write_unlock(&em_tree->lock);
>  out:
>  	btrfs_free_path(path);
> 
>  	trace_btrfs_get_extent(root, inode, em);
> 
> -	if (err) {
> +	if (ret) {
>  		free_extent_map(em);
> -		return ERR_PTR(err);
> +		return ERR_PTR(ret);
>  	}
>  	return em;
>  }
> --
> 2.17.1

Patch
diff mbox series

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 0df881e39f57..670acf4115a0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6528,8 +6528,7 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 				    u64 start, u64 len)
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
-	int ret;
-	int err = 0;
+	int ret = 0;
 	u64 extent_start = 0;
 	u64 extent_end = 0;
 	u64 objectid = btrfs_ino(inode);
@@ -6557,7 +6556,7 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 	}
 	em = alloc_extent_map();
 	if (!em) {
-		err = -ENOMEM;
+		ret = -ENOMEM;
 		goto out;
 	}
 	em->start = EXTENT_MAP_HOLE;
@@ -6567,7 +6566,7 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,

 	path = btrfs_alloc_path();
 	if (!path) {
-		err = -ENOMEM;
+		ret = -ENOMEM;
 		goto out;
 	}

@@ -6582,7 +6581,6 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,

 	ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
 	if (ret < 0) {
-		err = ret;
 		goto out;
 	} else if (ret > 0) {
 		if (path->slots[0] == 0)
@@ -6635,12 +6633,11 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		path->slots[0]++;
 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
 			ret = btrfs_next_leaf(root, path);
-			if (ret < 0) {
-				err = ret;
+			if (ret < 0)
 				goto out;
-			} else if (ret > 0) {
+			else if (ret > 0)
 				goto not_found;
-			}
+
 			leaf = path->nodes[0];
 		}
 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
@@ -6691,10 +6688,8 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 			    BTRFS_COMPRESS_NONE) {
 				ret = uncompress_inline(path, page, pg_offset,
 							extent_offset, item);
-				if (ret) {
-					err = ret;
+				if (ret)
 					goto out;
-				}
 			} else {
 				map = kmap(page);
 				read_extent_buffer(leaf, map + pg_offset, ptr,
@@ -6713,32 +6708,33 @@  struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
 		goto insert;
 	}
 not_found:
+	ret = 0;
 	em->start = start;
 	em->orig_start = start;
 	em->len = len;
 	em->block_start = EXTENT_MAP_HOLE;
 insert:
+	ret = 0;
 	btrfs_release_path(path);
 	if (em->start > start || extent_map_end(em) <= start) {
 		btrfs_err(fs_info,
 			  "bad extent! em: [%llu %llu] passed [%llu %llu]",
 			  em->start, em->len, start, len);
-		err = -EIO;
+		ret = -EIO;
 		goto out;
 	}

-	err = 0;
 	write_lock(&em_tree->lock);
-	err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
+	ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
 	write_unlock(&em_tree->lock);
 out:
 	btrfs_free_path(path);

 	trace_btrfs_get_extent(root, inode, em);

-	if (err) {
+	if (ret) {
 		free_extent_map(em);
-		return ERR_PTR(err);
+		return ERR_PTR(ret);
 	}
 	return em;
 }