]> git.itanic.dy.fi Git - linux-stable/commitdiff
btrfs: fix error handling in commit_fs_roots
authorJosef Bacik <josef@toxicpanda.com>
Tue, 1 Dec 2020 14:53:23 +0000 (09:53 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 7 Mar 2021 11:18:59 +0000 (12:18 +0100)
[ 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

index 8829d89eb4afffe5a8cd2c7bda4b7ff5746f78a9..1b52c960682d63328e675a236cb8be0d3d8b7e54 100644 (file)
@@ -1249,7 +1249,6 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
        struct btrfs_root *gang[8];
        int i;
        int ret;
-       int err = 0;
 
        spin_lock(&fs_info->fs_roots_radix_lock);
        while (1) {
@@ -1261,6 +1260,8 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
                        break;
                for (i = 0; i < ret; i++) {
                        struct btrfs_root *root = gang[i];
+                       int ret2;
+
                        radix_tree_tag_clear(&fs_info->fs_roots_radix,
                                        (unsigned long)root->root_key.objectid,
                                        BTRFS_ROOT_TRANS_TAG);
@@ -1282,17 +1283,17 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
                                                    root->node);
                        }
 
-                       err = btrfs_update_root(trans, fs_info->tree_root,
+                       ret2 = btrfs_update_root(trans, fs_info->tree_root,
                                                &root->root_key,
                                                &root->root_item);
+                       if (ret2)
+                               return ret2;
                        spin_lock(&fs_info->fs_roots_radix_lock);
-                       if (err)
-                               break;
                        btrfs_qgroup_free_meta_all_pertrans(root);
                }
        }
        spin_unlock(&fs_info->fs_roots_radix_lock);
-       return err;
+       return 0;
 }
 
 /*