diff mbox series

[2/2] btrfs: simplify error handling at btrfs_del_root_ref()

Message ID 739133f2ad552017ad7a57b23c0ccc5db83d9e1d.1661168931.git.fdmanana@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: fix lost error value deleting root reference | expand

Commit Message

Filipe Manana Aug. 22, 2022, 11:53 a.m. UTC
From: Filipe Manana <fdmanana@suse.com>

At btrfs_del_root_ref() we are using two return variables, named 'ret' and
'err'. This makes it harder to follow and easier to return the wrong value
in case an error happens - the previous patch in the series, which has the
subject "btrfs: fix silent failure when deleting root reference", fixed a
bug due to confusion created by these two variables.

So change the function to use a single variable for tracking the return
value of the function, using only 'ret', which is consistent with most of
the codebase.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/root-tree.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index d647cb2938c0..3975a24c3fdc 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -337,7 +337,6 @@  int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
 	unsigned long ptr;
-	int err = 0;
 	int ret;
 
 	path = btrfs_alloc_path();
@@ -350,7 +349,6 @@  int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 again:
 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
 	if (ret < 0) {
-		err = ret;
 		goto out;
 	} else if (ret == 0) {
 		leaf = path->nodes[0];
@@ -360,18 +358,28 @@  int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 		if ((btrfs_root_ref_dirid(leaf, ref) != dirid) ||
 		    (btrfs_root_ref_name_len(leaf, ref) != name_len) ||
 		    memcmp_extent_buffer(leaf, name, ptr, name_len)) {
-			err = -ENOENT;
+			ret = -ENOENT;
 			goto out;
 		}
 		*sequence = btrfs_root_ref_sequence(leaf, ref);
 
 		ret = btrfs_del_item(trans, tree_root, path);
-		if (ret) {
-			err = ret;
+		if (ret)
+			goto out;
+	} else {
+		if (key.type == BTRFS_ROOT_REF_KEY) {
+			/*
+			 * We've searched for a BTRFS_ROOT_BACKREF_KEY item and
+			 * for a BTRFS_ROOT_REF_KEY item, so error out.
+			 */
+			ret = -ENOENT;
 			goto out;
 		}
-	} else
-		err = -ENOENT;
+		/*
+		 * We have only searched for a BTRFS_ROOT_BACKREF_KEY item, so
+		 * fallback below to search for BTRFS_ROOT_REF_KEY item.
+		 */
+	}
 
 	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
 		btrfs_release_path(path);
@@ -383,7 +391,7 @@  int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 
 out:
 	btrfs_free_path(path);
-	return err;
+	return ret;
 }
 
 /*