Commit 15d1ff81 authored by Liu Bo's avatar Liu Bo Committed by Chris Mason

Btrfs: fix deadlock during allocating chunks

This deadlock comes from xfstests 251.

We'll hold the chunk_mutex throughout the whole of a chunk allocation.
But if we find that we've used up system chunk space, we need to allocate a
new system chunk, but this will lead to a recursion of chunk allocation and end
up with a deadlock on chunk_mutex.
So instead we need to allocate the system chunk first if we find we're in ENOSPC.
Signed-off-by: default avatarLiu Bo <liubo2009@cn.fujitsu.com>
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 2bcc0328
...@@ -3445,6 +3445,50 @@ static int should_alloc_chunk(struct btrfs_root *root, ...@@ -3445,6 +3445,50 @@ static int should_alloc_chunk(struct btrfs_root *root,
return 1; return 1;
} }
static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
{
u64 num_dev;
if (type & BTRFS_BLOCK_GROUP_RAID10 ||
type & BTRFS_BLOCK_GROUP_RAID0)
num_dev = root->fs_info->fs_devices->rw_devices;
else if (type & BTRFS_BLOCK_GROUP_RAID1)
num_dev = 2;
else
num_dev = 1; /* DUP or single */
/* metadata for updaing devices and chunk tree */
return btrfs_calc_trans_metadata_size(root, num_dev + 1);
}
static void check_system_chunk(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 type)
{
struct btrfs_space_info *info;
u64 left;
u64 thresh;
info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
spin_lock(&info->lock);
left = info->total_bytes - info->bytes_used - info->bytes_pinned -
info->bytes_reserved - info->bytes_readonly;
spin_unlock(&info->lock);
thresh = get_system_chunk_thresh(root, type);
if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
left, thresh, type);
dump_space_info(info, 0, 0);
}
if (left < thresh) {
u64 flags;
flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
btrfs_alloc_chunk(trans, root, flags);
}
}
static int do_chunk_alloc(struct btrfs_trans_handle *trans, static int do_chunk_alloc(struct btrfs_trans_handle *trans,
struct btrfs_root *extent_root, u64 alloc_bytes, struct btrfs_root *extent_root, u64 alloc_bytes,
u64 flags, int force) u64 flags, int force)
...@@ -3515,6 +3559,12 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans, ...@@ -3515,6 +3559,12 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
force_metadata_allocation(fs_info); force_metadata_allocation(fs_info);
} }
/*
* Check if we have enough space in SYSTEM chunk because we may need
* to update devices.
*/
check_system_chunk(trans, extent_root, flags);
ret = btrfs_alloc_chunk(trans, extent_root, flags); ret = btrfs_alloc_chunk(trans, extent_root, flags);
if (ret < 0 && ret != -ENOSPC) if (ret < 0 && ret != -ENOSPC)
goto out; goto out;
......
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