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: Liu Bo <liubo2009@cn.fujitsu.com> --- fs/btrfs/extent-tree.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 39 insertions(+), 0 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 37e0a80..0793ddc 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -3396,6 +3396,39 @@ static int should_alloc_chunk(struct btrfs_root *root, return 1; } +static void check_system_chunk(struct btrfs_trans_handle *trans, + struct btrfs_root *root, int check) +{ + struct btrfs_space_info *info; + u64 left; + u64 thresh; + u64 num_dev = 0; + + 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); + + mutex_lock(&root->fs_info->fs_devices->device_list_mutex); + num_dev = root->fs_info->fs_devices->rw_devices; + mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); + + thresh = btrfs_calc_trans_metadata_size(root, num_dev + 1); + if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) + dump_space_info(info, 0, 0); + + if (check) + return; + + 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, struct btrfs_root *extent_root, u64 alloc_bytes, u64 flags, int force) @@ -3468,6 +3501,12 @@ again: 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, 0); + ret = btrfs_alloc_chunk(trans, extent_root, flags); if (ret < 0 && ret != -ENOSPC) goto out; -- 1.6.5.2 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Andrea Gelmini
2012-Mar-15 15:08 UTC
Re: [PATCH v2] Btrfs: fix deadlock during allocating chunks
On Wed, Mar 14, 2012 at 07:17:09PM +0800, Liu Bo wrote:> 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.I''m using this patch without problem since you published it (compressed /home with hourly snapshot delete/creation). Thanks a lot for your work, Andrea -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html