Skip to content

Commit 4203e96

Browse files
josefbacikkdave
authored andcommitted
btrfs: fix incorrect updating of log root tree
We've historically had reports of being unable to mount file systems because the tree log root couldn't be read. Usually this is the "parent transid failure", but could be any of the related errors, including "fsid mismatch" or "bad tree block", depending on which block got allocated. The modification of the individual log root items are serialized on the per-log root root_mutex. This means that any modification to the per-subvol log root_item is completely protected. However we update the root item in the log root tree outside of the log root tree log_mutex. We do this in order to allow multiple subvolumes to be updated in each log transaction. This is problematic however because when we are writing the log root tree out we update the super block with the _current_ log root node information. Since these two operations happen independently of each other, you can end up updating the log root tree in between writing out the dirty blocks and setting the super block to point at the current root. This means we'll point at the new root node that hasn't been written out, instead of the one we should be pointing at. Thus whatever garbage or old block we end up pointing at complains when we mount the file system later and try to replay the log. Fix this by copying the log's root item into a local root item copy. Then once we're safely under the log_root_tree->log_mutex we update the root item in the log_root_tree. This way we do not modify the log_root_tree while we're committing it, fixing the problem. CC: [email protected] # 4.4+ Reviewed-by: Chris Mason <[email protected]> Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent c67d970 commit 4203e96

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

fs/btrfs/tree-log.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,18 +2932,19 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
29322932
* in the tree of log roots
29332933
*/
29342934
static int update_log_root(struct btrfs_trans_handle *trans,
2935-
struct btrfs_root *log)
2935+
struct btrfs_root *log,
2936+
struct btrfs_root_item *root_item)
29362937
{
29372938
struct btrfs_fs_info *fs_info = log->fs_info;
29382939
int ret;
29392940

29402941
if (log->log_transid == 1) {
29412942
/* insert root item on the first sync */
29422943
ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2943-
&log->root_key, &log->root_item);
2944+
&log->root_key, root_item);
29442945
} else {
29452946
ret = btrfs_update_root(trans, fs_info->log_root_tree,
2946-
&log->root_key, &log->root_item);
2947+
&log->root_key, root_item);
29472948
}
29482949
return ret;
29492950
}
@@ -3041,6 +3042,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
30413042
struct btrfs_fs_info *fs_info = root->fs_info;
30423043
struct btrfs_root *log = root->log_root;
30433044
struct btrfs_root *log_root_tree = fs_info->log_root_tree;
3045+
struct btrfs_root_item new_root_item;
30443046
int log_transid = 0;
30453047
struct btrfs_log_ctx root_log_ctx;
30463048
struct blk_plug plug;
@@ -3104,17 +3106,25 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
31043106
goto out;
31053107
}
31063108

3109+
/*
3110+
* We _must_ update under the root->log_mutex in order to make sure we
3111+
* have a consistent view of the log root we are trying to commit at
3112+
* this moment.
3113+
*
3114+
* We _must_ copy this into a local copy, because we are not holding the
3115+
* log_root_tree->log_mutex yet. This is important because when we
3116+
* commit the log_root_tree we must have a consistent view of the
3117+
* log_root_tree when we update the super block to point at the
3118+
* log_root_tree bytenr. If we update the log_root_tree here we'll race
3119+
* with the commit and possibly point at the new block which we may not
3120+
* have written out.
3121+
*/
31073122
btrfs_set_root_node(&log->root_item, log->node);
3123+
memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
31083124

31093125
root->log_transid++;
31103126
log->log_transid = root->log_transid;
31113127
root->log_start_pid = 0;
3112-
/*
3113-
* Update or create log root item under the root's log_mutex to prevent
3114-
* races with concurrent log syncs that can lead to failure to update
3115-
* log root item because it was not created yet.
3116-
*/
3117-
ret = update_log_root(trans, log);
31183128
/*
31193129
* IO has been started, blocks of the log tree have WRITTEN flag set
31203130
* in their headers. new modifications of the log will be written to
@@ -3135,6 +3145,14 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
31353145
mutex_unlock(&log_root_tree->log_mutex);
31363146

31373147
mutex_lock(&log_root_tree->log_mutex);
3148+
3149+
/*
3150+
* Now we are safe to update the log_root_tree because we're under the
3151+
* log_mutex, and we're a current writer so we're holding the commit
3152+
* open until we drop the log_mutex.
3153+
*/
3154+
ret = update_log_root(trans, log, &new_root_item);
3155+
31383156
if (atomic_dec_and_test(&log_root_tree->log_writers)) {
31393157
/* atomic_dec_and_test implies a barrier */
31403158
cond_wake_up_nomb(&log_root_tree->log_writer_wait);

0 commit comments

Comments
 (0)