Skip to content

Commit 165ea85

Browse files
josefbacikkdave
authored andcommitted
btrfs: do not write supers if we have an fs error
Error injection testing uncovered a pretty severe problem where we could end up committing a super that pointed to the wrong tree roots, resulting in transid mismatch errors. The way we commit the transaction is we update the super copy with the current generations and bytenrs of the important roots, and then copy that into our super_for_commit. Then we allow transactions to continue again, we write out the dirty pages for the transaction, and then we write the super. If the write out fails we'll bail and skip writing the supers. However since we've allowed a new transaction to start, we can have a log attempting to sync at this point, which would be blocked on fs_info->tree_log_mutex. Once the commit fails we're allowed to do the log tree commit, which uses super_for_commit, which now points at fs tree's that were not written out. Fix this by checking BTRFS_FS_STATE_ERROR once we acquire the tree_log_mutex. This way if the transaction commit fails we're sure to see this bit set and we can skip writing the super out. This patch fixes this specific transid mismatch error I was seeing with this particular error path. CC: [email protected] # 5.12+ Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 503d1ac commit 165ea85

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

fs/btrfs/tree-log.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,6 +3302,22 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
33023302
* begins and releases it only after writing its superblock.
33033303
*/
33043304
mutex_lock(&fs_info->tree_log_mutex);
3305+
3306+
/*
3307+
* The previous transaction writeout phase could have failed, and thus
3308+
* marked the fs in an error state. We must not commit here, as we
3309+
* could have updated our generation in the super_for_commit and
3310+
* writing the super here would result in transid mismatches. If there
3311+
* is an error here just bail.
3312+
*/
3313+
if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
3314+
ret = -EIO;
3315+
btrfs_set_log_full_commit(trans);
3316+
btrfs_abort_transaction(trans, ret);
3317+
mutex_unlock(&fs_info->tree_log_mutex);
3318+
goto out_wake_log_root;
3319+
}
3320+
33053321
btrfs_set_super_log_root(fs_info->super_for_commit, log_root_start);
33063322
btrfs_set_super_log_root_level(fs_info->super_for_commit, log_root_level);
33073323
ret = write_all_supers(fs_info, 1);

0 commit comments

Comments
 (0)