Skip to content

Commit

Permalink
btrfs: always abort the transaction if we abort a trans handle
Browse files Browse the repository at this point in the history
[ Upstream commit 5963ffc ]

While stress testing our error handling I noticed that sometimes we
would still commit the transaction even though we had aborted the
transaction.

Currently we track if a trans handle has dirtied any metadata, and if it
hasn't we mark the filesystem as having an error (so no new transactions
can be started), but we will allow the current transaction to complete
as we do not mark the transaction itself as having been aborted.

This sounds good in theory, but we were not properly tracking IO errors
in btrfs_finish_ordered_io, and thus committing the transaction with
bogus free space data.  This isn't necessarily a problem per-se with the
free space cache, as the other guards in place would have kept us from
accepting the free space cache as valid, but highlights a real world
case where we had a bug and could have corrupted the filesystem because
of it.

This "skip abort on empty trans handle" is nice in theory, but assumes
we have perfect error handling everywhere, which we clearly do not.
Also we do not allow further transactions to be started, so all this
does is save the last transaction that was happening, which doesn't
necessarily gain us anything other than the potential for real
corruption.

Remove this particular bit of code, if we decide we need to abort the
transaction then abort the current one and keep us from doing real harm
to the file system, regardless of whether this specific trans handle
dirtied anything or not.

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>
  • Loading branch information
josefbacik authored and gregkh committed Jul 14, 2021
1 parent 0f9a0de commit 5f10bdd
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 25 deletions.
5 changes: 1 addition & 4 deletions fs/btrfs/ctree.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
trans->transid, fs_info->generation);

if (!should_cow_block(trans, root, buf)) {
trans->dirty = true;
*cow_ret = buf;
return 0;
}
Expand Down Expand Up @@ -1788,10 +1787,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
* then we don't want to set the path blocking,
* so we test it here
*/
if (!should_cow_block(trans, root, b)) {
trans->dirty = true;
if (!should_cow_block(trans, root, b))
goto cow_done;
}

/*
* must have write locks on this node and the
Expand Down
1 change: 0 additions & 1 deletion fs/btrfs/extent-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4784,7 +4784,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
buf->start + buf->len - 1, GFP_NOFS);
}
trans->dirty = true;
/* this returns a buffer locked for blocking */
return buf;
}
Expand Down
11 changes: 0 additions & 11 deletions fs/btrfs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,6 @@ void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info = trans->fs_info;

WRITE_ONCE(trans->aborted, errno);
/* Nothing used. The other threads that have joined this
* transaction may be able to continue. */
if (!trans->dirty && list_empty(&trans->new_bgs)) {
const char *errstr;

errstr = btrfs_decode_error(errno);
btrfs_warn(fs_info,
"%s:%d: Aborting unused transaction(%s).",
function, line, errstr);
return;
}
WRITE_ONCE(trans->transaction->aborted, errno);
/* Wake up anybody who may be waiting on this transaction */
wake_up(&fs_info->transaction_wait);
Expand Down
8 changes: 0 additions & 8 deletions fs/btrfs/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -2076,14 +2076,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)

ASSERT(refcount_read(&trans->use_count) == 1);

/*
* Some places just start a transaction to commit it. We need to make
* sure that if this commit fails that the abort code actually marks the
* transaction as failed, so set trans->dirty to make the abort code do
* the right thing.
*/
trans->dirty = true;

/* Stop the commit early if ->aborted is set */
if (TRANS_ABORTED(cur_trans)) {
ret = cur_trans->aborted;
Expand Down
1 change: 0 additions & 1 deletion fs/btrfs/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ struct btrfs_trans_handle {
bool allocating_chunk;
bool can_flush_pending_bgs;
bool reloc_reserved;
bool dirty;
bool in_fsync;
struct btrfs_root *root;
struct btrfs_fs_info *fs_info;
Expand Down

0 comments on commit 5f10bdd

Please sign in to comment.