Skip to contentSkip to conversation

btrfs: some cleanups in btrfs_find_orphan_roots()

PATCH In review [email protected] 5 participants 11 messages Dec 16, 2025 – Feb 8 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: some cleanups in btrfs_find_orphan_roots()

From: Filipe Manana <[email protected]>

Some cleanups in btrfs_find_orphan_roots(). Details in the changelogs.

Filipe Manana (4):
btrfs: use single return variable in btrfs_find_orphan_roots()
btrfs: remove redundant path release in btrfs_find_orphan_roots()
btrfs: don't call btrfs_handle_fs_error() after failure to join transaction
btrfs: don't call btrfs_handle_fs_error() after failure to delete orphan item

 fs/btrfs/root-tree.c | 47 ++++++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 26 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/4 btrfs: use single return variable in btrfs_find_orphan_roots()

From: Filipe Manana <[email protected]>

We use both 'ret' and 'err' which is a pattern that generates confusion and resulted in subtle bugs in the past. Remove 'err' and use only 'ret'. Also move simplify the error flow by directy returning from the function instead of breaking of the loop, since there are no resources to cleanup after the loop.

---
 fs/btrfs/root-tree.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)
fs/btrfs/root-tree.c +18 -22
index 6a7e297ab0a7..a7171112d638 100644
@@ -217,8 +217,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct btrfs_root *root;
-	int err = 0;
-	int ret;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -230,20 +228,19 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
 	while (1) {
 		u64 root_objectid;
+		int ret;
 
 		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
-		if (ret < 0) {
-			err = ret;
-			break;
-		}
+		if (ret < 0)
+			return ret;
 
 		leaf = path->nodes[0];
 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
 			ret = btrfs_next_leaf(tree_root, path);
 			if (ret < 0)
-				err = ret;
-			if (ret != 0)
-				break;
+				return ret;
+			else if (ret > 0)
+				return 0;
 			leaf = path->nodes[0];
 		}
 
@@ -252,34 +249,33 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
 		if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
 		    key.type != BTRFS_ORPHAN_ITEM_KEY)
-			break;
+			return 0;
 
 		root_objectid = key.offset;
 		key.offset++;
 
 		root = btrfs_get_fs_root(fs_info, root_objectid, false);
-		err = PTR_ERR_OR_ZERO(root);
-		if (err && err != -ENOENT) {
+		ret = PTR_ERR_OR_ZERO(root);
+		if (ret && ret != -ENOENT) {
 			break;
-		} else if (err == -ENOENT) {
+		} else if (ret == -ENOENT) {
 			struct btrfs_trans_handle *trans;
 
 			btrfs_release_path(path);
 
 			trans = btrfs_join_transaction(tree_root);
 			if (IS_ERR(trans)) {
-				err = PTR_ERR(trans);
-				btrfs_handle_fs_error(fs_info, err,
+				ret = PTR_ERR(trans);
+				btrfs_handle_fs_error(fs_info, ret,
 					    "Failed to start trans to delete orphan item");
-				break;
+				return ret;
 			}
-			err = btrfs_del_orphan_item(trans, tree_root,
-						    root_objectid);
+			ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
 			btrfs_end_transaction(trans);
-			if (err) {
-				btrfs_handle_fs_error(fs_info, err,
+			if (ret) {
+				btrfs_handle_fs_error(fs_info, ret,
 					    "Failed to delete root orphan item");
-				break;
+				return ret;
 			}
 			continue;
 		}
@@ -307,7 +303,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 		btrfs_put_root(root);
 	}
 
-	return err;
+	return 0;
 }
 
 /* drop the root item for 'key' from the tree root */
Signature · 1 line
2.47.2
#4 lore raw

PATCH 2/4 btrfs: remove redundant path release in btrfs_find_orphan_roots()

In reply to [email protected]

From: Filipe Manana <[email protected]>

There's no need to release the path in the if branch used when the root does not exists since we released the path before the call to btrfs_get_fs_root(). So remove that redundant btrfs_release_path() call.

---
 fs/btrfs/root-tree.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/root-tree.c +0 -2
index a7171112d638..40f9bc9485e8 100644
@@ -261,8 +261,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 		} else if (ret == -ENOENT) {
 			struct btrfs_trans_handle *trans;
 
-			btrfs_release_path(path);
-
 			trans = btrfs_join_transaction(tree_root);
 			if (IS_ERR(trans)) {
 				ret = PTR_ERR(trans);
Signature · 1 line
2.47.2
#5 lore raw

PATCH 3/4 btrfs: don't call btrfs_handle_fs_error() after failure to join transaction

In reply to [email protected]

From: Filipe Manana <[email protected]>

In btrfs_find_orphan_roots() we don't need to call btrfs_handle_fs_error() if we fail to join a transaction. This is because we haven't done anything yet regarding the current root and previous iterations of the loop dealt with other roots, so there's nothing we need to undo. Instead log an error message and return the error to the caller, which will result either in a mount failure or remount failure (the only contextes it's called from).

---
 fs/btrfs/root-tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +3 -2
index 40f9bc9485e8..a30c7bc3f0e5 100644
@@ -264,8 +264,9 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 			trans = btrfs_join_transaction(tree_root);
 			if (IS_ERR(trans)) {
 				ret = PTR_ERR(trans);
-				btrfs_handle_fs_error(fs_info, ret,
-					    "Failed to start trans to delete orphan item");
+				btrfs_err(fs_info,
+			  "failed to join transaction to delete orphan item: %d\n",
+					  ret);
 				return ret;
 			}
 			ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
Signature · 1 line
2.47.2
#8 lore raw

PATCH 4/4 btrfs: don't call btrfs_handle_fs_error() after failure to delete orphan item

In reply to [email protected]

From: Filipe Manana <[email protected]>

In btrfs_find_orphan_roots() we don't need to call btrfs_handle_fs_error() if we fail to delete the orphan item for the current root. This is because we haven't done anything yet regarding the current root and previous iterations of the loop dealt with other roots, so there's nothing we need to undo. Instead log an error message and return the error to the caller, which will result either in a mount failure or remount failure (the only contextes it's called from).

---
 fs/btrfs/root-tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +2 -2
index a30c7bc3f0e5..065c3842122a 100644
@@ -272,8 +272,8 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 			ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
 			btrfs_end_transaction(trans);
 			if (ret) {
-				btrfs_handle_fs_error(fs_info, ret,
-					    "Failed to delete root orphan item");
+				btrfs_err(fs_info,
+				  "failed to delete root orphan item: %d", ret);
 				return ret;
 			}
 			continue;
Signature · 1 line
2.47.2
#6 lore raw

Re: [PATCH 3/4] btrfs: don't call btrfs_handle_fs_error() after failure to join transaction

In reply to [email protected]
Quoted 19 lines · On Tue, Dec 16, 2025 at 04:33:18PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

In btrfs_find_orphan_roots() we don't need to call btrfs_handle_fs_error() if we fail to join a transaction. This is because we haven't done anything yet regarding the current root and previous iterations of the loop dealt with other roots, so there's nothing we need to undo. Instead log an error message and return the error to the caller, which will result either in a mount failure or remount failure (the only contextes it's called from).

---
 fs/btrfs/root-tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/root-tree.c +0 -0
index 40f9bc9485e8..a30c7bc3f0e5 100644

fs/btrfs/root-tree.c int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · line 268

Quoted 7 lines
fs/btrfs/root-tree.c +2 -2
@@ -264,8 +264,9 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 			trans = btrfs_join_transaction(tree_root);
 			if (IS_ERR(trans)) {
 				ret = PTR_ERR(trans);
-				btrfs_handle_fs_error(fs_info, ret,
-					    "Failed to start trans to delete orphan item");
+				btrfs_err(fs_info,
+			  "failed to join transaction to delete orphan item: %d\n",

btrfs_err and the other helpers don't need "\n"

#7 lore raw

Re: [PATCH 3/4] btrfs: don't call btrfs_handle_fs_error() after failure to join transaction

Quoted 16 lines · David Sterba
Quoted 14 lines · On Tue, Dec 16, 2025 at 04:33:18PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

In btrfs_find_orphan_roots() we don't need to call btrfs_handle_fs_error() if we fail to join a transaction. This is because we haven't done anything yet regarding the current root and previous iterations of the loop dealt with other roots, so there's nothing we need to undo. Instead log an error message and return the error to the caller, which will result either in a mount failure or remount failure (the only contextes it's called from).

---
 fs/btrfs/root-tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 14 lines

fs/btrfs/root-tree.c int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · line 268

Quoted 12 lines
fs/btrfs/root-tree.c +2 -2
index 40f9bc9485e8..a30c7bc3f0e5 100644
@@ -264,8 +264,9 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
                      trans = btrfs_join_transaction(tree_root);
                      if (IS_ERR(trans)) {
                              ret = PTR_ERR(trans);
-                             btrfs_handle_fs_error(fs_info, ret,
-                                         "Failed to start trans to delete orphan item");
+                             btrfs_err(fs_info,
+                       "failed to join transaction to delete orphan item: %d\n",

btrfs_err and the other helpers don't need "\n"

Yes, I missed that in this patch, the other similar ones don't have the new line.
Will remove on commit time if there are no other changes needed.

Reviewed-by on the v1 series
#9 lore raw
In reply to [email protected]
Quoted 3 lines · 在 2025/12/17 03:03, [email protected] 写道:

From: Filipe Manana <[email protected]>

Some cleanups in btrfs_find_orphan_roots(). Details in the changelogs.

Except the tailing "\n" is unnecessary in the 3rd patch.

Thanks,
Qu

Quoted 10 lines

Filipe Manana (4):
btrfs: use single return variable in btrfs_find_orphan_roots()
btrfs: remove redundant path release in btrfs_find_orphan_roots()
btrfs: don't call btrfs_handle_fs_error() after failure to join transaction
btrfs: don't call btrfs_handle_fs_error() after failure to delete orphan item

  fs/btrfs/root-tree.c | 47 ++++++++++++++++++++------------------------
  1 file changed, 21 insertions(+), 26 deletions(-)
Reviewed-by on the v1 series
#10 lore raw
In reply to [email protected]
Quoted 4 lines · On 12/16/25 5:39 PM, [email protected] wrote:

From: Filipe Manana <[email protected]>

Some cleanups in btrfs_find_orphan_roots(). Details in the changelogs.

Quoted 9 lines

Filipe Manana (4):
btrfs: use single return variable in btrfs_find_orphan_roots()
btrfs: remove redundant path release in btrfs_find_orphan_roots()
btrfs: don't call btrfs_handle_fs_error() after failure to join transaction
btrfs: don't call btrfs_handle_fs_error() after failure to delete orphan item

  fs/btrfs/root-tree.c | 47 ++++++++++++++++++++------------------------
  1 file changed, 21 insertions(+), 26 deletions(-)

with aforementioned "\n" fixed

#2 lore raw

Re: [PATCH 1/4] btrfs: use single return variable in btrfs_find_orphan_roots()

In reply to [email protected]
Quoted 7 lines · [email protected] wrote:

From: Filipe Manana <[email protected]>

We use both 'ret' and 'err' which is a pattern that generates confusion and resulted in subtle bugs in the past. Remove 'err' and use only 'ret'. Also move simplify the error flow by directy returning from the function instead of breaking of the loop, since there are no resources to cleanup after the loop.

Hi everyone,

I'm running the btrfs for-next branch, along with the encryption patch set through my AI review prompts:

https://github.com/masoncl/review-prompts

I'm working on some btrfs specific instructions, but hopefully these reviews are already useful. I've kept the headers to make it easier to double check that I'm actually replying to the right email, but please let me know if any parts of this are incorrect:

commit 8670a25ecb2fbc35d4e58f8f522e7d5b735d6778
Author: Filipe Manana <[email protected]>

btrfs: use single return variable in btrfs_find_orphan_roots()

This commit refactors btrfs_find_orphan_roots() to use a single return variable 'ret' instead of both 'ret' and 'err', and simplifies the error flow by directly returning from the function instead of breaking out of the loop.

Quoted 4 lines
fs/btrfs/root-tree.c +0 -0
index 6a7e297ab0a7a..a7171112d638f 100644

[ ... ]

Quoted 42 lines
fs/btrfs/root-tree.c +7 -10
@@ -217,8 +217,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct btrfs_root *root;
-	int err = 0;
-	int ret;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -230,20 +228,19 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
 	while (1) {
 		u64 root_objectid;
+		int ret;
 
 		ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
-		if (ret < 0) {
-			err = ret;
-			break;
-		}
+		if (ret < 0)
+			return ret;
 
 		leaf = path->nodes[0];
 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
 			ret = btrfs_next_leaf(tree_root, path);
 			if (ret < 0)
-				err = ret;
-			if (ret != 0)
-				break;
+				return ret;
+			else if (ret > 0)
+				return 0;
 			leaf = path->nodes[0];
 		}
 
@@ -252,34 +249,33 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
 		if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
 		    key.type != BTRFS_ORPHAN_ITEM_KEY)
-			break;
+			return 0;

fs/btrfs/root-tree.c int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · line 260

Quoted 10 lines
fs/btrfs/root-tree.c +2 -2
 
 		root_objectid = key.offset;
 		key.offset++;
 
 		root = btrfs_get_fs_root(fs_info, root_objectid, false);
-		err = PTR_ERR_OR_ZERO(root);
-		if (err && err != -ENOENT) {
+		ret = PTR_ERR_OR_ZERO(root);
+		if (ret && ret != -ENOENT) {
 			break;
                        ^^^^

Does this code lose the error code from btrfs_get_fs_root()?

The refactoring moved 'int ret' from function scope (line 217 in the old code) to loop scope (inside the 'while (1)' loop). When btrfs_get_fs_root() fails with an error like -ENOMEM or -EIO, the code breaks out of the loop. However, the 'ret' variable containing the error is scoped to the loop body and goes out of scope after the break.

The function then returns 0 at line 303, losing the error code. This violates the commit message's stated intent to "directly return from the function instead of breaking of the loop."

Looking at btrfs_get_fs_root() in fs/btrfs/disk-io.c, it can return:
- -ENOMEM if path allocation fails (line 1316)
- -EIO or other errors from btrfs_read_tree_root() (line 1301)
- errors from btrfs_init_fs_root() (line 1310)
- -EIO from btrfs_search_slot() (line 1323)

If btrfs_get_fs_root() fails during mount due to memory pressure or I/O errors, the mount will succeed instead of failing. This can lead to filesystem corruption because the orphan root handling is incomplete but the mount proceeds anyway.

Shouldn't this 'break;' be 'return ret;' to match all the other error paths in the refactored function?

Quoted 30 lines
fs/btrfs/root-tree.c +8 -9
-		} else if (err == -ENOENT) {
+		} else if (ret == -ENOENT) {
 			struct btrfs_trans_handle *trans;
 
 			btrfs_release_path(path);
 
 			trans = btrfs_join_transaction(tree_root);
 			if (IS_ERR(trans)) {
-				err = PTR_ERR(trans);
-				btrfs_handle_fs_error(fs_info, err,
+				ret = PTR_ERR(trans);
+				btrfs_handle_fs_error(fs_info, ret,
 					    "Failed to start trans to delete orphan item");
-				break;
+				return ret;
 			}
-			err = btrfs_del_orphan_item(trans, tree_root,
-						    root_objectid);
+			ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
 			btrfs_end_transaction(trans);
-			if (err) {
-				btrfs_handle_fs_error(fs_info, err,
+			if (ret) {
+				btrfs_handle_fs_error(fs_info, ret,
 					    "Failed to delete root orphan item");
-				break;
+				return ret;
 			}
 			continue;
 		}
#3 lore raw

Re: [PATCH 1/4] btrfs: use single return variable in btrfs_find_orphan_roots()

Quoted 93 lines · Chris Mason
Quoted 7 lines · [email protected] wrote:

From: Filipe Manana <[email protected]>

We use both 'ret' and 'err' which is a pattern that generates confusion and resulted in subtle bugs in the past. Remove 'err' and use only 'ret'. Also move simplify the error flow by directy returning from the function instead of breaking of the loop, since there are no resources to cleanup after the loop.

Hi everyone,

I'm running the btrfs for-next branch, along with the encryption patch set through my AI review prompts:

https://github.com/masoncl/review-prompts

I'm working on some btrfs specific instructions, but hopefully these reviews are already useful. I've kept the headers to make it easier to double check that I'm actually replying to the right email, but please let me know if any parts of this are incorrect:

commit 8670a25ecb2fbc35d4e58f8f522e7d5b735d6778
Author: Filipe Manana <[email protected]>

btrfs: use single return variable in btrfs_find_orphan_roots()

This commit refactors btrfs_find_orphan_roots() to use a single return variable 'ret' instead of both 'ret' and 'err', and simplifies the error flow by directly returning from the function instead of breaking out of the loop.

Quoted 4 lines
fs/btrfs/root-tree.c +0 -0
index 6a7e297ab0a7a..a7171112d638f 100644

[ ... ]

Quoted 42 lines
fs/btrfs/root-tree.c +7 -10
@@ -217,8 +217,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
      BTRFS_PATH_AUTO_FREE(path);
      struct btrfs_key key;
      struct btrfs_root *root;
-     int err = 0;
-     int ret;
 
      path = btrfs_alloc_path();
      if (!path)
@@ -230,20 +228,19 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
      while (1) {
              u64 root_objectid;
+             int ret;
 
              ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
-             if (ret < 0) {
-                     err = ret;
-                     break;
-             }
+             if (ret < 0)
+                     return ret;
 
              leaf = path->nodes[0];
              if (path->slots[0] >= btrfs_header_nritems(leaf)) {
                      ret = btrfs_next_leaf(tree_root, path);
                      if (ret < 0)
-                             err = ret;
-                     if (ret != 0)
-                             break;
+                             return ret;
+                     else if (ret > 0)
+                             return 0;
                      leaf = path->nodes[0];
              }
 
@@ -252,34 +249,33 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
              if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
                  key.type != BTRFS_ORPHAN_ITEM_KEY)
-                     break;
+                     return 0;

fs/btrfs/root-tree.c int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info) · line 260

Quoted 10 lines
fs/btrfs/root-tree.c +2 -2
 
              root_objectid = key.offset;
              key.offset++;
 
              root = btrfs_get_fs_root(fs_info, root_objectid, false);
-             err = PTR_ERR_OR_ZERO(root);
-             if (err && err != -ENOENT) {
+             ret = PTR_ERR_OR_ZERO(root);
+             if (ret && ret != -ENOENT) {
                      break;
                        ^^^^
Quoted 1 line

Does this code lose the error code from btrfs_get_fs_root()?

It does indeed. Fix sent here:

https://lore.kernel.org/linux-btrfs/6400dc5103b5ddbe543961b4e865e311ab396790.1770580302.git.fdmanana@suse.com/

Thanks.

Quoted 56 lines

The refactoring moved 'int ret' from function scope (line 217 in the old code) to loop scope (inside the 'while (1)' loop). When btrfs_get_fs_root() fails with an error like -ENOMEM or -EIO, the code breaks out of the loop. However, the 'ret' variable containing the error is scoped to the loop body and goes out of scope after the break.

The function then returns 0 at line 303, losing the error code. This violates the commit message's stated intent to "directly return from the function instead of breaking of the loop."

Looking at btrfs_get_fs_root() in fs/btrfs/disk-io.c, it can return:
- -ENOMEM if path allocation fails (line 1316)
- -EIO or other errors from btrfs_read_tree_root() (line 1301)
- errors from btrfs_init_fs_root() (line 1310)
- -EIO from btrfs_search_slot() (line 1323)

If btrfs_get_fs_root() fails during mount due to memory pressure or I/O errors, the mount will succeed instead of failing. This can lead to filesystem corruption because the orphan root handling is incomplete but the mount proceeds anyway.

Shouldn't this 'break;' be 'return ret;' to match all the other error paths in the refactored function?

Quoted 30 lines
fs/btrfs/root-tree.c +8 -9
-             } else if (err == -ENOENT) {
+             } else if (ret == -ENOENT) {
                      struct btrfs_trans_handle *trans;
 
                      btrfs_release_path(path);
 
                      trans = btrfs_join_transaction(tree_root);
                      if (IS_ERR(trans)) {
-                             err = PTR_ERR(trans);
-                             btrfs_handle_fs_error(fs_info, err,
+                             ret = PTR_ERR(trans);
+                             btrfs_handle_fs_error(fs_info, ret,
                                          "Failed to start trans to delete orphan item");
-                             break;
+                             return ret;
                      }
-                     err = btrfs_del_orphan_item(trans, tree_root,
-                                                 root_objectid);
+                     ret = btrfs_del_orphan_item(trans, tree_root, root_objectid);
                      btrfs_end_transaction(trans);
-                     if (err) {
-                             btrfs_handle_fs_error(fs_info, err,
+                     if (ret) {
+                             btrfs_handle_fs_error(fs_info, ret,
                                          "Failed to delete root orphan item");
-                             break;
+                             return ret;
                      }
                      continue;
              }