From: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
To: Josef Bacik <jbacik@fb.com>, <linux-btrfs@vger.kernel.org>
Subject: Re: [PATCH v3] btrfs: should block unused block groups deletion work when allocating data space
Date: Wed, 27 Jul 2016 09:26:22 +0800 [thread overview]
Message-ID: <57980DBE.9050702@cn.fujitsu.com> (raw)
In-Reply-To: <82bb9660-3245-a40c-4ac4-650a235c0d90@fb.com>
hello,
On 07/26/2016 11:51 PM, Josef Bacik wrote:
> On 07/26/2016 08:04 AM, Wang Xiaoguang wrote:
>> cleaner_kthread() may run at any time, in which it'll call
>> btrfs_delete_unused_bgs()
>> to delete unused block groups. Because this work is asynchronous, it
>> may also result
>> in false ENOSPC error. Please see below race window:
>>
>> CPU1 | CPU2
>> |
>> |-> btrfs_alloc_data_chunk_ondemand() |-> cleaner_kthread()
>> |-> do_chunk_alloc() | |
>> | assume it returns ENOSPC, which means | |
>> | btrfs_space_info is full and have free| |
>> | space to satisfy data request. | |
>> | | |- >
>> btrfs_delete_unused_bgs()
>> | | | it will
>> decrease btrfs_space_info
>> | | | total_bytes and make
>> | | | btrfs_space_info
>> is not full.
>> | | |
>> In this case, we may get ENOSPC error, but btrfs_space_info is not full.
>>
>> To fix this issue, in btrfs_alloc_data_chunk_ondemand(), if we need
>> to call
>> do_chunk_alloc() to allocating new chunk, we should block
>> btrfs_delete_unused_bgs().
>> So here we introduce a new struct rw_semaphore bg_delete_sem to do
>> this job.
>>
>> Signed-off-by: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
>> ---
>> v3: improve one code logic suggested by Josef, thanks.
>> ---
>> fs/btrfs/ctree.h | 1 +
>> fs/btrfs/disk-io.c | 1 +
>> fs/btrfs/extent-tree.c | 44
>> ++++++++++++++++++++++++++++++++++++++------
>> 3 files changed, 40 insertions(+), 6 deletions(-)
>>
>> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
>> index 7eb2913..bf0751d 100644
>> --- a/fs/btrfs/ctree.h
>> +++ b/fs/btrfs/ctree.h
>> @@ -800,6 +800,7 @@ struct btrfs_fs_info {
>> struct mutex cleaner_mutex;
>> struct mutex chunk_mutex;
>> struct mutex volume_mutex;
>> + struct rw_semaphore bg_delete_sem;
>>
>> /*
>> * this is taken to make sure we don't set block groups ro after
>> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
>> index 60ce119..f8609fd 100644
>> --- a/fs/btrfs/disk-io.c
>> +++ b/fs/btrfs/disk-io.c
>> @@ -2683,6 +2683,7 @@ int open_ctree(struct super_block *sb,
>> init_rwsem(&fs_info->commit_root_sem);
>> init_rwsem(&fs_info->cleanup_work_sem);
>> init_rwsem(&fs_info->subvol_sem);
>> + init_rwsem(&fs_info->bg_delete_sem);
>> sema_init(&fs_info->uuid_tree_rescan_sem, 1);
>>
>> btrfs_init_dev_replace_locks(fs_info);
>> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
>> index df8d756..50b440e 100644
>> --- a/fs/btrfs/extent-tree.c
>> +++ b/fs/btrfs/extent-tree.c
>> @@ -4111,6 +4111,7 @@ int btrfs_alloc_data_chunk_ondemand(struct
>> inode *inode, u64 bytes)
>> int ret = 0;
>> int need_commit = 2;
>> int have_pinned_space;
>> + int have_bg_delete_sem = 0;
>>
>> /* make sure bytes are sectorsize aligned */
>> bytes = ALIGN(bytes, root->sectorsize);
>> @@ -4121,8 +4122,11 @@ int btrfs_alloc_data_chunk_ondemand(struct
>> inode *inode, u64 bytes)
>> }
>>
>> data_sinfo = fs_info->data_sinfo;
>> - if (!data_sinfo)
>> + if (!data_sinfo) {
>> + down_read(&root->fs_info->bg_delete_sem);
>> + have_bg_delete_sem = 1;
>> goto alloc;
>> + }
>>
>> again:
>> /* make sure we have enough space to handle the data first */
>> @@ -4135,6 +4139,19 @@ again:
>> struct btrfs_trans_handle *trans;
>>
>> /*
>> + * We may need to allocate new chunk, so we should block
>> + * btrfs_delete_unused_bgs()
>> + */
>> + if (!have_bg_delete_sem) {
>> + spin_unlock(&data_sinfo->lock);
>> + down_read(&root->fs_info->bg_delete_sem);
>> + have_bg_delete_sem = 1;
>> + spin_lock(&data_sinfo->lock);
>> + if (used + bytes <= data_sinfo->total_bytes)
>
> Doh sorry I forgot to mention you need to re-calculate used here. Also
> I noticed we already have a delete_unused_bgs_mutex, can we drop that
> and replace it with bg_delete_sem as well? Thanks,
Sorry, I also forgot to re-calculate used...
OK, I'll try to replace delete_unused_bgs_mutex with bg_delete_sem,
please wait a while :)
Regards,
Xiaoguang Wang
>
> Josef
>
>
next prev parent reply other threads:[~2016-07-27 1:29 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-25 7:51 [PATCH v2 0/4] update bytes_may_use timely to avoid false ENOSPC issue Wang Xiaoguang
2016-07-25 7:51 ` [PATCH v2 1/4] btrfs: use correct offset for reloc_inode in prealloc_file_extent_cluster() Wang Xiaoguang
2016-07-25 7:51 ` [PATCH v2 2/4] btrfs: divide btrfs_update_reserved_bytes() into two functions Wang Xiaoguang
2016-07-25 7:51 ` [PATCH v2 3/4] btrfs: update btrfs_space_info's bytes_may_use timely Wang Xiaoguang
2016-07-25 13:33 ` Josef Bacik
2016-07-25 18:10 ` Josef Bacik
2016-07-25 7:51 ` [PATCH v2 4/4] btrfs: should block unused block groups deletion work when allocating data space Wang Xiaoguang
2016-07-25 13:32 ` Josef Bacik
2016-07-26 12:04 ` [PATCH v3] " Wang Xiaoguang
2016-07-26 15:51 ` Josef Bacik
2016-07-26 16:25 ` David Sterba
2016-07-27 1:26 ` Wang Xiaoguang [this message]
2016-07-27 5:50 ` [PATCH v4] " Wang Xiaoguang
-- strict thread matches above, loose matches on Subject: below --
2016-09-09 8:17 [PATCH v3] " Wang Xiaoguang
2016-09-09 8:25 ` Wang Xiaoguang
2016-09-09 9:02 ` David Sterba
2016-09-09 10:18 ` Holger Hoffstätte
2016-09-09 10:56 ` Holger Hoffstätte
2016-09-12 7:38 ` Wang Xiaoguang
2016-09-12 8:19 ` Holger Hoffstätte
2016-09-10 6:04 ` Stefan Priebe - Profihost AG
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=57980DBE.9050702@cn.fujitsu.com \
--to=wangxg.fnst@cn.fujitsu.com \
--cc=jbacik@fb.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.