[5.10,072/102] btrfs: fix error handling in commit_fs_roots
diff mbox series

Message ID 20210305120906.823576586@linuxfoundation.org
State New, archived
Headers show
  • Untitled series #487857
Related show

Commit Message

Greg KH March 5, 2021, 12:21 p.m. UTC
From: Josef Bacik <josef@toxicpanda.com>

[ Upstream commit 4f4317c13a40194940acf4a71670179c4faca2b5 ]

While doing error injection I would sometimes get a corrupt file system.
This is because I was injecting errors at btrfs_search_slot, but would
only do it one time per stack.  This uncovered a problem in
commit_fs_roots, where if we get an error we would just break.  However
we're in a nested loop, the first loop being a loop to find all the
dirty fs roots, and then subsequent root updates would succeed clearing
the error value.

This isn't likely to happen in real scenarios, however we could
potentially get a random ENOMEM once and then not again, and we'd end up
with a corrupted file system.  Fix this by moving the error checking
around a bit to the main loop, as this is the only place where something
will fail, and return the error as soon as it occurs.

With this patch my reproducer no longer corrupts the file system.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
 fs/btrfs/transaction.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff mbox series

diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 96dbfc011f45..261a50708cb8 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -1320,7 +1320,6 @@  static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
 	struct btrfs_root *gang[8];
 	int i;
 	int ret;
-	int err = 0;
 	while (1) {
@@ -1332,6 +1331,8 @@  static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
 		for (i = 0; i < ret; i++) {
 			struct btrfs_root *root = gang[i];
+			int ret2;
 					(unsigned long)root->root_key.objectid,
@@ -1353,17 +1354,17 @@  static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
-			err = btrfs_update_root(trans, fs_info->tree_root,
+			ret2 = btrfs_update_root(trans, fs_info->tree_root,
+			if (ret2)
+				return ret2;
-			if (err)
-				break;
-	return err;
+	return 0;