All of lore.kernel.org
 help / color / mirror / Atom feed
* [RESEND PATCH] btrfs: Refactor error handling in btrfs_zero_range
@ 2021-08-30 14:25 Nikolay Borisov
  2021-08-30 15:33 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Nikolay Borisov @ 2021-08-30 14:25 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Nikolay Borisov

The major complexity when it comes to error handling in btrfs_zero_range
stems from the code executed under the 'reserve_space' label. Rather
than it having an effect on the whole of btrfs_zero_range refactor it
so that error handling specific to the functions called in this branch
is contained only within the branch itself. This obviates the need for
having the 'space_reserved' local flag since btrfs_free_reserved_data_space
is called from the 2 error branches it's needed. Furthermore, this
rids the code from the out label and enables converting most 'goto out'
statements to simiple 'return ret' making the function easier to read.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---

I sent this patch some time ago but didn't get any replies so now I'm resending
it again.
 fs/btrfs/file.c | 70 +++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 37 deletions(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 7ff577005d0f..94f4353618d4 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -3143,23 +3143,19 @@ static int btrfs_zero_range(struct inode *inode,
 {
 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
 	struct extent_map *em;
-	struct extent_changeset *data_reserved = NULL;
 	int ret;
 	u64 alloc_hint = 0;
 	const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode));
 	u64 alloc_start = round_down(offset, sectorsize);
 	u64 alloc_end = round_up(offset + len, sectorsize);
 	u64 bytes_to_reserve = 0;
-	bool space_reserved = false;

 	inode_dio_wait(inode);

 	em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
 			      alloc_end - alloc_start);
-	if (IS_ERR(em)) {
-		ret = PTR_ERR(em);
-		goto out;
-	}
+	if (IS_ERR(em))
+		return PTR_ERR(em);

 	/*
 	 * Avoid hole punching and extent allocation for some cases. More cases
@@ -3180,9 +3176,8 @@ static int btrfs_zero_range(struct inode *inode,
 			 * needed.
 			 */
 			free_extent_map(em);
-			ret = btrfs_fallocate_update_isize(inode, offset + len,
-							   mode);
-			goto out;
+			return btrfs_fallocate_update_isize(inode, offset + len,
+							    mode);
 		}
 		/*
 		 * Part of the range is already a prealloc extent, so operate
@@ -3200,26 +3195,22 @@ static int btrfs_zero_range(struct inode *inode,
 	    BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
 		em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
 				      sectorsize);
-		if (IS_ERR(em)) {
-			ret = PTR_ERR(em);
-			goto out;
-		}
+		if (IS_ERR(em))
+			return PTR_ERR(em);

 		if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
 			free_extent_map(em);
-			ret = btrfs_fallocate_update_isize(inode, offset + len,
-							   mode);
-			goto out;
+			return  btrfs_fallocate_update_isize(inode,
+							offset + len, mode);
 		}
 		if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
 			free_extent_map(em);
 			ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
 						   0);
-			if (!ret)
-				ret = btrfs_fallocate_update_isize(inode,
-								   offset + len,
-								   mode);
-			return ret;
+			if (ret)
+				return ret;
+			return btrfs_fallocate_update_isize(inode,
+							    offset + len, mode);
 		}
 		free_extent_map(em);
 		alloc_start = round_down(offset, sectorsize);
@@ -3240,14 +3231,14 @@ static int btrfs_zero_range(struct inode *inode,
 		ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
 							    offset);
 		if (ret < 0)
-			goto out;
+			return ret;
 		if (ret == RANGE_BOUNDARY_HOLE) {
 			alloc_start = round_down(offset, sectorsize);
 			ret = 0;
 		} else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
 			ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
 			if (ret)
-				goto out;
+				return ret;
 		} else {
 			ret = 0;
 		}
@@ -3257,7 +3248,7 @@ static int btrfs_zero_range(struct inode *inode,
 		ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
 							    offset + len);
 		if (ret < 0)
-			goto out;
+			return ret;
 		if (ret == RANGE_BOUNDARY_HOLE) {
 			alloc_end = round_up(offset + len, sectorsize);
 			ret = 0;
@@ -3265,7 +3256,7 @@ static int btrfs_zero_range(struct inode *inode,
 			ret = btrfs_truncate_block(BTRFS_I(inode), offset + len,
 						   0, 1);
 			if (ret)
-				goto out;
+				return ret;
 		} else {
 			ret = 0;
 		}
@@ -3273,6 +3264,7 @@ static int btrfs_zero_range(struct inode *inode,

 reserve_space:
 	if (alloc_start < alloc_end) {
+		struct extent_changeset *data_reserved = NULL;
 		struct extent_state *cached_state = NULL;
 		const u64 lockstart = alloc_start;
 		const u64 lockend = alloc_end - 1;
@@ -3281,18 +3273,27 @@ static int btrfs_zero_range(struct inode *inode,
 		ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
 						      bytes_to_reserve);
 		if (ret < 0)
-			goto out;
-		space_reserved = true;
+			return ret;
 		ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
 						  &cached_state);
-		if (ret)
-			goto out;
+		if (ret) {
+			btrfs_free_reserved_data_space(BTRFS_I(inode),
+						       data_reserved,
+						       alloc_start,
+						       bytes_to_reserve);
+			return ret;
+		}
 		ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
 						alloc_start, bytes_to_reserve);
 		if (ret) {
+			btrfs_free_reserved_data_space(BTRFS_I(inode),
+						       data_reserved,
+						       alloc_start,
+						       bytes_to_reserve);
 			unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
 					     lockend, &cached_state);
-			goto out;
+			extent_changeset_free(data_reserved);
+			return ret;
 		}
 		ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
 						alloc_end - alloc_start,
@@ -3302,16 +3303,11 @@ static int btrfs_zero_range(struct inode *inode,
 				     lockend, &cached_state);
 		/* btrfs_prealloc_file_range releases reserved space on error */
 		if (ret) {
-			space_reserved = false;
-			goto out;
+			extent_changeset_free(data_reserved);
+			return ret;
 		}
 	}
 	ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
- out:
-	if (ret && space_reserved)
-		btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
-					       alloc_start, bytes_to_reserve);
-	extent_changeset_free(data_reserved);

 	return ret;
 }
--
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RESEND PATCH] btrfs: Refactor error handling in btrfs_zero_range
  2021-08-30 14:25 [RESEND PATCH] btrfs: Refactor error handling in btrfs_zero_range Nikolay Borisov
@ 2021-08-30 15:33 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2021-08-30 15:33 UTC (permalink / raw)
  To: Nikolay Borisov; +Cc: linux-btrfs

On Mon, Aug 30, 2021 at 05:25:47PM +0300, Nikolay Borisov wrote:
> The major complexity when it comes to error handling in btrfs_zero_range
> stems from the code executed under the 'reserve_space' label. Rather
> than it having an effect on the whole of btrfs_zero_range refactor it
> so that error handling specific to the functions called in this branch
> is contained only within the branch itself. This obviates the need for
> having the 'space_reserved' local flag since btrfs_free_reserved_data_space
> is called from the 2 error branches it's needed. Furthermore, this
> rids the code from the out label and enables converting most 'goto out'
> statements to simiple 'return ret' making the function easier to read.
> 
> Signed-off-by: Nikolay Borisov <nborisov@suse.com>
> ---
> 
> I sent this patch some time ago but didn't get any replies so now I'm resending
> it again.

I looked up previous postings, both marked as dropped but without mail
replies so I think we discussed that off list. Looking at it again I
don't like the course of the change, replacing gotos with returns, the
pattern does not look like the one where it's ok.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-30 15:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-30 14:25 [RESEND PATCH] btrfs: Refactor error handling in btrfs_zero_range Nikolay Borisov
2021-08-30 15:33 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.