btrfs: fix error handling in commit_fs_roots
authorJosef Bacik <josef@toxicpanda.com>
Tue, 1 Dec 2020 14:53:23 +0000 (09:53 -0500)
committerDavid Sterba <dsterba@suse.com>
Mon, 8 Feb 2021 21:58:49 +0000 (22:58 +0100)
commit4f4317c13a40194940acf4a71670179c4faca2b5
treee5440d4c16b4dda028e1a429e03ad72a7db5a8e2
parente0756cfc7d7cd08c98a53b6009c091a3f6a50be6
btrfs: fix error handling in commit_fs_roots

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>
fs/btrfs/transaction.c