Commit 60376ce4 authored by Josef Bacik's avatar Josef Bacik Committed by Chris Mason

Btrfs: fix race in sync and freeze again

I screwed this up, there is a race between checking if there is a running
transaction and actually starting a transaction in sync where we could race
with a freezer and get ourselves into trouble.  To fix this we need to make
a new join type to only do the try lock on the freeze stuff.  If it fails
we'll return EPERM and just return from sync.  This fixes a hang Liu Bo
reported when running xfstest 68 in a loop.  Thanks,
Reported-by: default avatarLiu Bo <bo.li.liu@oracle.com>
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
parent b3ae244e
...@@ -854,16 +854,13 @@ int btrfs_sync_fs(struct super_block *sb, int wait) ...@@ -854,16 +854,13 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
btrfs_wait_ordered_extents(root, 0, 0); btrfs_wait_ordered_extents(root, 0, 0);
spin_lock(&fs_info->trans_lock); trans = btrfs_join_transaction_freeze(root);
if (!fs_info->running_transaction) { if (IS_ERR(trans)) {
spin_unlock(&fs_info->trans_lock); /* Frozen, don't bother */
return 0; if (PTR_ERR(trans) == -EPERM)
} return 0;
spin_unlock(&fs_info->trans_lock);
trans = btrfs_join_transaction(root);
if (IS_ERR(trans))
return PTR_ERR(trans); return PTR_ERR(trans);
}
return btrfs_commit_transaction(trans, root); return btrfs_commit_transaction(trans, root);
} }
......
...@@ -272,6 +272,7 @@ enum btrfs_trans_type { ...@@ -272,6 +272,7 @@ enum btrfs_trans_type {
TRANS_JOIN, TRANS_JOIN,
TRANS_USERSPACE, TRANS_USERSPACE,
TRANS_JOIN_NOLOCK, TRANS_JOIN_NOLOCK,
TRANS_JOIN_FREEZE,
}; };
static int may_wait_transaction(struct btrfs_root *root, int type) static int may_wait_transaction(struct btrfs_root *root, int type)
...@@ -341,7 +342,11 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, ...@@ -341,7 +342,11 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
if (!h) if (!h)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
sb_start_intwrite(root->fs_info->sb); if (!__sb_start_write(root->fs_info->sb, SB_FREEZE_FS, false)) {
if (type == TRANS_JOIN_FREEZE)
return ERR_PTR(-EPERM);
sb_start_intwrite(root->fs_info->sb);
}
if (may_wait_transaction(root, type)) if (may_wait_transaction(root, type))
wait_current_trans(root); wait_current_trans(root);
...@@ -424,6 +429,11 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root ...@@ -424,6 +429,11 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root
return start_transaction(root, 0, TRANS_USERSPACE, 0); return start_transaction(root, 0, TRANS_USERSPACE, 0);
} }
struct btrfs_trans_handle *btrfs_join_transaction_freeze(struct btrfs_root *root)
{
return start_transaction(root, 0, TRANS_JOIN_FREEZE, 0);
}
/* wait for a transaction commit to be fully complete */ /* wait for a transaction commit to be fully complete */
static noinline void wait_for_commit(struct btrfs_root *root, static noinline void wait_for_commit(struct btrfs_root *root,
struct btrfs_transaction *commit) struct btrfs_transaction *commit)
......
...@@ -102,6 +102,7 @@ struct btrfs_trans_handle *btrfs_start_transaction_noflush( ...@@ -102,6 +102,7 @@ struct btrfs_trans_handle *btrfs_start_transaction_noflush(
struct btrfs_root *root, int num_items); struct btrfs_root *root, int num_items);
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_join_transaction_freeze(struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root); struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid); int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid);
int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment