Commit 868f401a authored by Zhaolei's avatar Zhaolei Committed by Chris Mason

btrfs: Use ref_cnt for set_block_group_ro()

More than one code call set_block_group_ro() and restore rw in fail.

Old code use bool bit to save blockgroup's ro state, it can not
support parallel case(it is confirmd exist in my debug log).

This patch use ref count to store ro state, and rename
set_block_group_ro/set_block_group_rw
to
inc_block_group_ro/dec_block_group_ro.
Signed-off-by: default avatarZhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: default avatarChris Mason <clm@fb.com>
parent d7cad238
...@@ -1300,7 +1300,7 @@ struct btrfs_block_group_cache { ...@@ -1300,7 +1300,7 @@ struct btrfs_block_group_cache {
/* for raid56, this is a full stripe, without parity */ /* for raid56, this is a full stripe, without parity */
unsigned long full_stripe_len; unsigned long full_stripe_len;
unsigned int ro:1; unsigned int ro;
unsigned int iref:1; unsigned int iref:1;
unsigned int has_caching_ctl:1; unsigned int has_caching_ctl:1;
unsigned int removed:1; unsigned int removed:1;
...@@ -3495,9 +3495,9 @@ int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, ...@@ -3495,9 +3495,9 @@ int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
void btrfs_block_rsv_release(struct btrfs_root *root, void btrfs_block_rsv_release(struct btrfs_root *root,
struct btrfs_block_rsv *block_rsv, struct btrfs_block_rsv *block_rsv,
u64 num_bytes); u64 num_bytes);
int btrfs_set_block_group_ro(struct btrfs_root *root, int btrfs_inc_block_group_ro(struct btrfs_root *root,
struct btrfs_block_group_cache *cache); struct btrfs_block_group_cache *cache);
void btrfs_set_block_group_rw(struct btrfs_root *root, void btrfs_dec_block_group_ro(struct btrfs_root *root,
struct btrfs_block_group_cache *cache); struct btrfs_block_group_cache *cache);
void btrfs_put_block_group_cache(struct btrfs_fs_info *info); void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo); u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
......
...@@ -8723,14 +8723,13 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) ...@@ -8723,14 +8723,13 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
return flags; return flags;
} }
static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
{ {
struct btrfs_space_info *sinfo = cache->space_info; struct btrfs_space_info *sinfo = cache->space_info;
u64 num_bytes; u64 num_bytes;
u64 min_allocable_bytes; u64 min_allocable_bytes;
int ret = -ENOSPC; int ret = -ENOSPC;
/* /*
* We need some metadata space and system metadata space for * We need some metadata space and system metadata space for
* allocating chunks in some corner cases until we force to set * allocating chunks in some corner cases until we force to set
...@@ -8747,6 +8746,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) ...@@ -8747,6 +8746,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
spin_lock(&cache->lock); spin_lock(&cache->lock);
if (cache->ro) { if (cache->ro) {
cache->ro++;
ret = 0; ret = 0;
goto out; goto out;
} }
...@@ -8758,7 +8758,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) ...@@ -8758,7 +8758,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
min_allocable_bytes <= sinfo->total_bytes) { min_allocable_bytes <= sinfo->total_bytes) {
sinfo->bytes_readonly += num_bytes; sinfo->bytes_readonly += num_bytes;
cache->ro = 1; cache->ro++;
list_add_tail(&cache->ro_list, &sinfo->ro_bgs); list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
ret = 0; ret = 0;
} }
...@@ -8768,7 +8768,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) ...@@ -8768,7 +8768,7 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
return ret; return ret;
} }
int btrfs_set_block_group_ro(struct btrfs_root *root, int btrfs_inc_block_group_ro(struct btrfs_root *root,
struct btrfs_block_group_cache *cache) struct btrfs_block_group_cache *cache)
{ {
...@@ -8776,8 +8776,6 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, ...@@ -8776,8 +8776,6 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
u64 alloc_flags; u64 alloc_flags;
int ret; int ret;
BUG_ON(cache->ro);
again: again:
trans = btrfs_join_transaction(root); trans = btrfs_join_transaction(root);
if (IS_ERR(trans)) if (IS_ERR(trans))
...@@ -8820,7 +8818,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, ...@@ -8820,7 +8818,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
goto out; goto out;
} }
ret = set_block_group_ro(cache, 0); ret = inc_block_group_ro(cache, 0);
if (!ret) if (!ret)
goto out; goto out;
alloc_flags = get_alloc_profile(root, cache->space_info->flags); alloc_flags = get_alloc_profile(root, cache->space_info->flags);
...@@ -8828,7 +8826,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, ...@@ -8828,7 +8826,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
CHUNK_ALLOC_FORCE); CHUNK_ALLOC_FORCE);
if (ret < 0) if (ret < 0)
goto out; goto out;
ret = set_block_group_ro(cache, 0); ret = inc_block_group_ro(cache, 0);
out: out:
if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
alloc_flags = update_block_group_flags(root, cache->flags); alloc_flags = update_block_group_flags(root, cache->flags);
...@@ -8891,7 +8889,7 @@ u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) ...@@ -8891,7 +8889,7 @@ u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
return free_bytes; return free_bytes;
} }
void btrfs_set_block_group_rw(struct btrfs_root *root, void btrfs_dec_block_group_ro(struct btrfs_root *root,
struct btrfs_block_group_cache *cache) struct btrfs_block_group_cache *cache)
{ {
struct btrfs_space_info *sinfo = cache->space_info; struct btrfs_space_info *sinfo = cache->space_info;
...@@ -8901,11 +8899,13 @@ void btrfs_set_block_group_rw(struct btrfs_root *root, ...@@ -8901,11 +8899,13 @@ void btrfs_set_block_group_rw(struct btrfs_root *root,
spin_lock(&sinfo->lock); spin_lock(&sinfo->lock);
spin_lock(&cache->lock); spin_lock(&cache->lock);
num_bytes = cache->key.offset - cache->reserved - cache->pinned - if (!--cache->ro) {
cache->bytes_super - btrfs_block_group_used(&cache->item); num_bytes = cache->key.offset - cache->reserved -
sinfo->bytes_readonly -= num_bytes; cache->pinned - cache->bytes_super -
cache->ro = 0; btrfs_block_group_used(&cache->item);
list_del_init(&cache->ro_list); sinfo->bytes_readonly -= num_bytes;
list_del_init(&cache->ro_list);
}
spin_unlock(&cache->lock); spin_unlock(&cache->lock);
spin_unlock(&sinfo->lock); spin_unlock(&sinfo->lock);
} }
...@@ -9421,7 +9421,7 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -9421,7 +9421,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
set_avail_alloc_bits(root->fs_info, cache->flags); set_avail_alloc_bits(root->fs_info, cache->flags);
if (btrfs_chunk_readonly(root, cache->key.objectid)) { if (btrfs_chunk_readonly(root, cache->key.objectid)) {
set_block_group_ro(cache, 1); inc_block_group_ro(cache, 1);
} else if (btrfs_block_group_used(&cache->item) == 0) { } else if (btrfs_block_group_used(&cache->item) == 0) {
spin_lock(&info->unused_bgs_lock); spin_lock(&info->unused_bgs_lock);
/* Should always be true but just in case. */ /* Should always be true but just in case. */
...@@ -9449,11 +9449,11 @@ int btrfs_read_block_groups(struct btrfs_root *root) ...@@ -9449,11 +9449,11 @@ int btrfs_read_block_groups(struct btrfs_root *root)
list_for_each_entry(cache, list_for_each_entry(cache,
&space_info->block_groups[BTRFS_RAID_RAID0], &space_info->block_groups[BTRFS_RAID_RAID0],
list) list)
set_block_group_ro(cache, 1); inc_block_group_ro(cache, 1);
list_for_each_entry(cache, list_for_each_entry(cache,
&space_info->block_groups[BTRFS_RAID_SINGLE], &space_info->block_groups[BTRFS_RAID_SINGLE],
list) list)
set_block_group_ro(cache, 1); inc_block_group_ro(cache, 1);
} }
init_global_block_rsv(info); init_global_block_rsv(info);
...@@ -9941,7 +9941,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) ...@@ -9941,7 +9941,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
spin_unlock(&block_group->lock); spin_unlock(&block_group->lock);
/* We don't want to force the issue, only flip if it's ok. */ /* We don't want to force the issue, only flip if it's ok. */
ret = set_block_group_ro(block_group, 0); ret = inc_block_group_ro(block_group, 0);
up_write(&space_info->groups_sem); up_write(&space_info->groups_sem);
if (ret < 0) { if (ret < 0) {
ret = 0; ret = 0;
...@@ -9955,7 +9955,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) ...@@ -9955,7 +9955,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
/* 1 for btrfs_orphan_reserve_metadata() */ /* 1 for btrfs_orphan_reserve_metadata() */
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) { if (IS_ERR(trans)) {
btrfs_set_block_group_rw(root, block_group); btrfs_dec_block_group_ro(root, block_group);
ret = PTR_ERR(trans); ret = PTR_ERR(trans);
goto next; goto next;
} }
...@@ -9982,14 +9982,14 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) ...@@ -9982,14 +9982,14 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
EXTENT_DIRTY, GFP_NOFS); EXTENT_DIRTY, GFP_NOFS);
if (ret) { if (ret) {
mutex_unlock(&fs_info->unused_bg_unpin_mutex); mutex_unlock(&fs_info->unused_bg_unpin_mutex);
btrfs_set_block_group_rw(root, block_group); btrfs_dec_block_group_ro(root, block_group);
goto end_trans; goto end_trans;
} }
ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
EXTENT_DIRTY, GFP_NOFS); EXTENT_DIRTY, GFP_NOFS);
if (ret) { if (ret) {
mutex_unlock(&fs_info->unused_bg_unpin_mutex); mutex_unlock(&fs_info->unused_bg_unpin_mutex);
btrfs_set_block_group_rw(root, block_group); btrfs_dec_block_group_ro(root, block_group);
goto end_trans; goto end_trans;
} }
mutex_unlock(&fs_info->unused_bg_unpin_mutex); mutex_unlock(&fs_info->unused_bg_unpin_mutex);
......
...@@ -4215,14 +4215,12 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) ...@@ -4215,14 +4215,12 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
rc->block_group = btrfs_lookup_block_group(fs_info, group_start); rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
BUG_ON(!rc->block_group); BUG_ON(!rc->block_group);
if (!rc->block_group->ro) { ret = btrfs_inc_block_group_ro(extent_root, rc->block_group);
ret = btrfs_set_block_group_ro(extent_root, rc->block_group); if (ret) {
if (ret) { err = ret;
err = ret; goto out;
goto out;
}
rw = 1;
} }
rw = 1;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) { if (!path) {
...@@ -4294,7 +4292,7 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start) ...@@ -4294,7 +4292,7 @@ int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0); WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
out: out:
if (err && rw) if (err && rw)
btrfs_set_block_group_rw(extent_root, rc->block_group); btrfs_dec_block_group_ro(extent_root, rc->block_group);
iput(rc->data_inode); iput(rc->data_inode);
btrfs_put_block_group(rc->block_group); btrfs_put_block_group(rc->block_group);
kfree(rc); kfree(rc);
......
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