Skip to contentSkip to conversation

btrfs: fix lost error value deleting root reference

PATCH Superseded [email protected] 3 participants 12 messages Aug 22, 2022 – Aug 23, 2022 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: fix lost error value deleting root reference

From: Filipe Manana <[email protected]>

Fix a silent failure in case deleting a root reference fails when searching for an item. Then make btrfs_del_root_ref() less likely to run into such type of bug in the future. Details in the changelogs.

Filipe Manana (2):
btrfs: fix silent failure when deleting root reference
btrfs: simplify error handling at btrfs_del_root_ref()

 fs/btrfs/root-tree.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 btrfs: fix silent failure when deleting root reference

From: Filipe Manana <[email protected]>

At btrfs_del_root_ref(), if btrfs_search_slot() returns an error, we end up returning from the function with a value of 0 (success). This happens because the function returns the value stored in the variable 'err', which is 0, while the error value we got from btrfs_search_slot() is stored in the 'ret' variable.

So fix it by setting 'err' with the error value.

Fixes 8289ed9f93bef2 ("btrfs: replace the BUG_ON in btrfs_del_root_ref with proper error handling")
---
 fs/btrfs/root-tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +3 -2
index a64b26b16904..d647cb2938c0 100644
@@ -349,9 +349,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 	key.offset = ref_id;
 again:
 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
-	if (ret < 0)
+	if (ret < 0) {
+		err = ret;
 		goto out;
-	if (ret == 0) {
+	} else if (ret == 0) {
 		leaf = path->nodes[0];
 		ref = btrfs_item_ptr(leaf, path->slots[0],
 				     struct btrfs_root_ref);
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/2 btrfs: simplify error handling at btrfs_del_root_ref()

In reply to [email protected]

From: Filipe Manana <[email protected]>

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.

---
 fs/btrfs/root-tree.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
fs/btrfs/root-tree.c +16 -8
index d647cb2938c0..3975a24c3fdc 100644
@@ -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;
 }
 
 /*
Signature · 1 line
2.35.1
#3 lore raw

v2 cover btrfs: fix lost error value deleting root reference

In reply to [email protected]

From: Filipe Manana <[email protected]>

Fix a silent failure in case deleting a root reference fails when searching for an item. Then make btrfs_del_root_ref() less likely to run into such type of bug in the future. Details in the changelogs.

V2: Fix patch 2/2. If the BTRFS_ROOT_BACKREF_KEY item is not found, but
the BTRFS_ROOT_REF_KEY is found, then before we would return -ENOENT and now we were returning 0 (unless an error happened when deleting the BTRFS_ROOT_REF_KEY item). Just return -ENOENT whenever one of the items is not found, all the callers abort the transaction if btrfs_del_root_ref() returns an error.

Filipe Manana (2):
btrfs: fix silent failure when deleting root reference
btrfs: simplify error handling at btrfs_del_root_ref()

 fs/btrfs/root-tree.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
Signature · 1 line
2.35.1
#4 lore raw

PATCH v2 1/2 btrfs: fix silent failure when deleting root reference

From: Filipe Manana <[email protected]>

At btrfs_del_root_ref(), if btrfs_search_slot() returns an error, we end up returning from the function with a value of 0 (success). This happens because the function returns the value stored in the variable 'err', which is 0, while the error value we got from btrfs_search_slot() is stored in the 'ret' variable.

So fix it by setting 'err' with the error value.

Fixes 8289ed9f93bef2 ("btrfs: replace the BUG_ON in btrfs_del_root_ref with proper error handling")
---
 fs/btrfs/root-tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +3 -2
index a64b26b16904..d647cb2938c0 100644
@@ -349,9 +349,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 	key.offset = ref_id;
 again:
 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
-	if (ret < 0)
+	if (ret < 0) {
+		err = ret;
 		goto out;
-	if (ret == 0) {
+	} else if (ret == 0) {
 		leaf = path->nodes[0];
 		ref = btrfs_item_ptr(leaf, path->slots[0],
 				     struct btrfs_root_ref);
Signature · 1 line
2.35.1
Reviewed-by on v2 1/2
#5 lore raw

Re: [PATCH v2 1/2] btrfs: fix silent failure when deleting root reference

Quoted 8 lines · On 2022/8/22 22:47, [email protected] wrote:

From: Filipe Manana <[email protected]>

At btrfs_del_root_ref(), if btrfs_search_slot() returns an error, we end up returning from the function with a value of 0 (success). This happens because the function returns the value stored in the variable 'err', which is 0, while the error value we got from btrfs_search_slot() is stored in the 'ret' variable.

Quoted 4 lines

So fix it by setting 'err' with the error value.

Fixes 8289ed9f93bef2 ("btrfs: replace the BUG_ON in btrfs_del_root_ref with proper error handling")

Thanks for catching this,
Qu

Quoted 9 lines
---
  fs/btrfs/root-tree.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +0 -0
index a64b26b16904..d647cb2938c0 100644

fs/btrfs/root-tree.c int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id, · line 354

Quoted 8 lines
fs/btrfs/root-tree.c +2 -2
@@ -349,9 +349,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
  	key.offset = ref_id;
  again:
  	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
-	if (ret < 0)
+	if (ret < 0) {
+		err = ret;
  		goto out;
-	if (ret == 0) {

Just a small nitpick here, since above if (ret < 0) branch will call "goto out", we don't need the "else" branch.
The old "if (ret == 0) {" should be good enough.

Thanks,
Qu

Quoted 4 lines
fs/btrfs/root-tree.c +1 -0
+	} else if (ret == 0) {
  		leaf = path->nodes[0];
  		ref = btrfs_item_ptr(leaf, path->slots[0],
  				     struct btrfs_root_ref);
#6 lore raw

Re: [PATCH v2 1/2] btrfs: fix silent failure when deleting root reference

Quoted 34 lines · Qu Wenruo
Quoted 8 lines · On 2022/8/22 22:47, [email protected] wrote:

From: Filipe Manana <[email protected]>

At btrfs_del_root_ref(), if btrfs_search_slot() returns an error, we end up returning from the function with a value of 0 (success). This happens because the function returns the value stored in the variable 'err', which is 0, while the error value we got from btrfs_search_slot() is stored in the 'ret' variable.

Quoted 4 lines

So fix it by setting 'err' with the error value.

Fixes 8289ed9f93bef2 ("btrfs: replace the BUG_ON in btrfs_del_root_ref with proper error handling")

Thanks for catching this,
Qu

Quoted 13 lines
---
  fs/btrfs/root-tree.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +0 -1
index a64b26b16904..d647cb2938c0 100644
@@ -349,9 +349,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
      key.offset = ref_id;
  again:
      ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
-     if (ret < 0)
Quoted 8 lines

fs/btrfs/root-tree.c int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id, · line 354

Quoted 4 lines
fs/btrfs/root-tree.c +2 -1
+     if (ret < 0) {
+             err = ret;
              goto out;
-     if (ret == 0) {

Just a small nitpick here, since above if (ret < 0) branch will call "goto out", we don't need the "else" branch.
The old "if (ret == 0) {" should be good enough.

Yes, it's not about correctness. It's just my preferred style. I find it more intuitive to have a single if-else that tests the same variable instead
of having it tested by two distinct if statements.

Quoted 8 lines

Thanks,
Qu

Quoted 4 lines
fs/btrfs/root-tree.c +1 -0
+     } else if (ret == 0) {
              leaf = path->nodes[0];
              ref = btrfs_item_ptr(leaf, path->slots[0],
                                   struct btrfs_root_ref);
#7 lore raw

Re: [PATCH v2 1/2] btrfs: fix silent failure when deleting root reference

Quoted 23 lines · Filipe Manana
Quoted 13 lines · Qu Wenruo
Quoted 13 lines
---
  fs/btrfs/root-tree.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +0 -1
index a64b26b16904..d647cb2938c0 100644
@@ -349,9 +349,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
      key.offset = ref_id;
  again:
      ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
-     if (ret < 0)
Quoted 8 lines

fs/btrfs/root-tree.c int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id, · line 354

Quoted 4 lines
fs/btrfs/root-tree.c +2 -1
+     if (ret < 0) {
+             err = ret;
              goto out;
-     if (ret == 0) {

Just a small nitpick here, since above if (ret < 0) branch will call "goto out", we don't need the "else" branch.
The old "if (ret == 0) {" should be good enough.

Quoted 4 lines

Yes, it's not about correctness. It's just my preferred style. I find it more intuitive to have a single if-else that tests the same variable instead
of having it tested by two distinct if statements.

Agreed with that style.

#8 lore raw

PATCH v2 2/2 btrfs: simplify error handling at btrfs_del_root_ref()

In reply to [email protected]

From: Filipe Manana <[email protected]>

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.

---
 fs/btrfs/root-tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
fs/btrfs/root-tree.c +7 -9
index d647cb2938c0..e1f599d7a916 100644
@@ -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,18 @@ 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
-		err = -ENOENT;
+	} else {
+		ret = -ENOENT;
+		goto out;
+	}
 
 	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
 		btrfs_release_path(path);
@@ -383,7 +381,7 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 
 out:
 	btrfs_free_path(path);
-	return err;
+	return ret;
 }
 
 /*
Signature · 1 line
2.35.1
Reviewed-by on v2 2/2
#9 lore raw

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

Quoted 8 lines · On 2022/8/22 22:47, [email protected] wrote:

From: Filipe Manana <[email protected]>

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.

Quoted 5 lines

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.

Although one small nitpick inlined below.

Quoted 39 lines
---
  fs/btrfs/root-tree.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
fs/btrfs/root-tree.c +2 -5
index d647cb2938c0..e1f599d7a916 100644
@@ -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,18 @@ 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;

fs/btrfs/root-tree.c int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id, · line 375

Quoted 10 lines
fs/btrfs/root-tree.c +4 -3
-		}
-	} else
-		err = -ENOENT;
+	} else {
+		ret = -ENOENT;
+		goto out;
+	}
 
  	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  		btrfs_release_path(path);

To the if () check here can also be a cause of confusion.

Can we split it into two dedicated btrfs_search_slot() calls (instead of current goto again with different keys) in a separate patch?

I guess that's why the v1 version had some error got overriden, right?

Thanks,
Qu

Quoted 9 lines
fs/btrfs/root-tree.c +1 -1
@@ -383,7 +381,7 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 
  out:
  	btrfs_free_path(path);
-	return err;
+	return ret;
  }
 
  /*
#10 lore raw

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

Quoted 73 lines · Qu Wenruo
Quoted 8 lines · On 2022/8/22 22:47, [email protected] wrote:

From: Filipe Manana <[email protected]>

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.

Quoted 5 lines

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.

Although one small nitpick inlined below.

Quoted 39 lines
---
  fs/btrfs/root-tree.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
fs/btrfs/root-tree.c +2 -5
index d647cb2938c0..e1f599d7a916 100644
@@ -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,18 @@ 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;

fs/btrfs/root-tree.c int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id, · line 375

Quoted 10 lines
fs/btrfs/root-tree.c +4 -3
-		}
-	} else
-		err = -ENOENT;
+	} else {
+		ret = -ENOENT;
+		goto out;
+	}
 
  	if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  		btrfs_release_path(path);

To the if () check here can also be a cause of confusion.

Quoted 4 lines

Can we split it into two dedicated btrfs_search_slot() calls (instead of current goto again with different keys) in a separate patch?

I guess that's why the v1 version had some error got overriden, right?

The problem in v1 was because I didn't think properly.
I was under the wrong idea that we could have either one key type or the other, that it was to deal with some old format - like the v0 backref thing.

Then I realized we got all v0 compatibility stuff removed some years ago, and that this is something different - both keys must always exist. In other words, it was not because of the if + goto or because of having two variables for the return value.

I'm not sure getting rid of the if + goto logic and duplicating the deletion is better. It would duplicate a lot of code. Either way, my intention of this patch is really just to have a single variable for the return value instead of two.

Quoted 12 lines

Thanks,
Qu

Quoted 9 lines
fs/btrfs/root-tree.c +1 -1
@@ -383,7 +381,7 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
 
  out:
  	btrfs_free_path(path);
-	return err;
+	return ret;
  }
 
  /*
#11 lore raw
In reply to [email protected]
Quoted 13 lines · On Mon, Aug 22, 2022 at 03:47:08PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

Fix a silent failure in case deleting a root reference fails when searching for an item. Then make btrfs_del_root_ref() less likely to run into such type of bug in the future. Details in the changelogs.

V2: Fix patch 2/2. If the BTRFS_ROOT_BACKREF_KEY item is not found, but
the BTRFS_ROOT_REF_KEY is found, then before we would return -ENOENT and now we were returning 0 (unless an error happened when deleting the BTRFS_ROOT_REF_KEY item). Just return -ENOENT whenever one of the items is not found, all the callers abort the transaction if btrfs_del_root_ref() returns an error.

Quoted 3 lines

Filipe Manana (2):
btrfs: fix silent failure when deleting root reference
btrfs: simplify error handling at btrfs_del_root_ref()

Added to misc-next, thanks.