Skip to content

Commit 3349b57

Browse files
josefbacikkdave
authored andcommitted
btrfs: convert block group bit field to use bit helpers
We use a bit field in the btrfs_block_group for different flags, however this is awkward because we have to hold the block_group->lock for any modification of any of these fields, and makes the code clunky for a few of these flags. Convert these to a properly flags setup so we can utilize the bit helpers. Reviewed-by: Johannes Thumshirn <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 723de71 commit 3349b57

File tree

9 files changed

+71
-56
lines changed

9 files changed

+71
-56
lines changed

fs/btrfs/block-group.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait)
772772
WARN_ON(cache->caching_ctl);
773773
cache->caching_ctl = caching_ctl;
774774
cache->cached = BTRFS_CACHE_STARTED;
775-
cache->has_caching_ctl = 1;
775+
set_bit(BLOCK_GROUP_FLAG_HAS_CACHING_CTL, &cache->runtime_flags);
776776
spin_unlock(&cache->lock);
777777

778778
write_lock(&fs_info->block_group_cache_lock);
@@ -988,11 +988,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
988988
kobject_put(kobj);
989989
}
990990

991-
if (block_group->has_caching_ctl)
991+
992+
if (test_bit(BLOCK_GROUP_FLAG_HAS_CACHING_CTL, &block_group->runtime_flags))
992993
caching_ctl = btrfs_get_caching_control(block_group);
993994
if (block_group->cached == BTRFS_CACHE_STARTED)
994995
btrfs_wait_block_group_cache_done(block_group);
995-
if (block_group->has_caching_ctl) {
996+
if (test_bit(BLOCK_GROUP_FLAG_HAS_CACHING_CTL, &block_group->runtime_flags)) {
996997
write_lock(&fs_info->block_group_cache_lock);
997998
if (!caching_ctl) {
998999
struct btrfs_caching_control *ctl;
@@ -1034,12 +1035,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10341035
< block_group->zone_unusable);
10351036
WARN_ON(block_group->space_info->disk_total
10361037
< block_group->length * factor);
1037-
WARN_ON(block_group->zone_is_active &&
1038+
WARN_ON(test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
1039+
&block_group->runtime_flags) &&
10381040
block_group->space_info->active_total_bytes
10391041
< block_group->length);
10401042
}
10411043
block_group->space_info->total_bytes -= block_group->length;
1042-
if (block_group->zone_is_active)
1044+
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
10431045
block_group->space_info->active_total_bytes -= block_group->length;
10441046
block_group->space_info->bytes_readonly -=
10451047
(block_group->length - block_group->zone_unusable);
@@ -1069,7 +1071,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10691071
goto out;
10701072

10711073
spin_lock(&block_group->lock);
1072-
block_group->removed = 1;
1074+
set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
1075+
10731076
/*
10741077
* At this point trimming or scrub can't start on this block group,
10751078
* because we removed the block group from the rbtree
@@ -2409,7 +2412,8 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
24092412
ret = insert_block_group_item(trans, block_group);
24102413
if (ret)
24112414
btrfs_abort_transaction(trans, ret);
2412-
if (!block_group->chunk_item_inserted) {
2415+
if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
2416+
&block_group->runtime_flags)) {
24132417
mutex_lock(&fs_info->chunk_mutex);
24142418
ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group);
24152419
mutex_unlock(&fs_info->chunk_mutex);
@@ -3955,7 +3959,8 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
39553959
while (block_group) {
39563960
btrfs_wait_block_group_cache_done(block_group);
39573961
spin_lock(&block_group->lock);
3958-
if (block_group->iref)
3962+
if (test_bit(BLOCK_GROUP_FLAG_IREF,
3963+
&block_group->runtime_flags))
39593964
break;
39603965
spin_unlock(&block_group->lock);
39613966
block_group = btrfs_next_block_group(block_group);
@@ -3968,7 +3973,7 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
39683973
}
39693974

39703975
inode = block_group->inode;
3971-
block_group->iref = 0;
3976+
clear_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags);
39723977
block_group->inode = NULL;
39733978
spin_unlock(&block_group->lock);
39743979
ASSERT(block_group->io_ctl.inode == NULL);
@@ -4110,7 +4115,7 @@ void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group)
41104115

41114116
spin_lock(&block_group->lock);
41124117
cleanup = (atomic_dec_and_test(&block_group->frozen) &&
4113-
block_group->removed);
4118+
test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags));
41144119
spin_unlock(&block_group->lock);
41154120

41164121
if (cleanup) {

fs/btrfs/block-group.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ enum btrfs_chunk_alloc_enum {
4646
CHUNK_ALLOC_FORCE_FOR_EXTENT,
4747
};
4848

49+
/* Block group flags set at runtime */
50+
enum btrfs_block_group_flags {
51+
BLOCK_GROUP_FLAG_IREF,
52+
BLOCK_GROUP_FLAG_HAS_CACHING_CTL,
53+
BLOCK_GROUP_FLAG_REMOVED,
54+
BLOCK_GROUP_FLAG_TO_COPY,
55+
BLOCK_GROUP_FLAG_RELOCATING_REPAIR,
56+
BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
57+
BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
58+
BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
59+
};
60+
4961
struct btrfs_caching_control {
5062
struct list_head list;
5163
struct mutex mutex;
@@ -95,16 +107,9 @@ struct btrfs_block_group {
95107

96108
/* For raid56, this is a full stripe, without parity */
97109
unsigned long full_stripe_len;
110+
unsigned long runtime_flags;
98111

99112
unsigned int ro;
100-
unsigned int iref:1;
101-
unsigned int has_caching_ctl:1;
102-
unsigned int removed:1;
103-
unsigned int to_copy:1;
104-
unsigned int relocating_repair:1;
105-
unsigned int chunk_item_inserted:1;
106-
unsigned int zone_is_active:1;
107-
unsigned int zoned_data_reloc_ongoing:1;
108113

109114
int disk_cache_state;
110115

fs/btrfs/dev-replace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static int mark_block_group_to_copy(struct btrfs_fs_info *fs_info,
546546
continue;
547547

548548
spin_lock(&cache->lock);
549-
cache->to_copy = 1;
549+
set_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
550550
spin_unlock(&cache->lock);
551551

552552
btrfs_put_block_group(cache);
@@ -577,7 +577,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
577577
return true;
578578

579579
spin_lock(&cache->lock);
580-
if (cache->removed) {
580+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
581581
spin_unlock(&cache->lock);
582582
return true;
583583
}
@@ -611,7 +611,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
611611

612612
/* Last stripe on this device */
613613
spin_lock(&cache->lock);
614-
cache->to_copy = 0;
614+
clear_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
615615
spin_unlock(&cache->lock);
616616

617617
return true;

fs/btrfs/extent-tree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,7 +3804,8 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
38043804
block_group->start == fs_info->data_reloc_bg ||
38053805
fs_info->data_reloc_bg == 0);
38063806

3807-
if (block_group->ro || block_group->zoned_data_reloc_ongoing) {
3807+
if (block_group->ro ||
3808+
test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) {
38083809
ret = 1;
38093810
goto out;
38103811
}
@@ -3881,7 +3882,7 @@ static int do_allocation_zoned(struct btrfs_block_group *block_group,
38813882
* regular extents) at the same time to the same zone, which
38823883
* easily break the write pointer.
38833884
*/
3884-
block_group->zoned_data_reloc_ongoing = 1;
3885+
set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags);
38853886
fs_info->data_reloc_bg = 0;
38863887
}
38873888
spin_unlock(&fs_info->relocation_bg_lock);

fs/btrfs/free-space-cache.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
126126
block_group->disk_cache_state = BTRFS_DC_CLEAR;
127127
}
128128

129-
if (!block_group->iref) {
129+
if (!test_and_set_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags))
130130
block_group->inode = igrab(inode);
131-
block_group->iref = 1;
132-
}
133131
spin_unlock(&block_group->lock);
134132

135133
return inode;
@@ -241,8 +239,7 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
241239
clear_nlink(inode);
242240
/* One for the block groups ref */
243241
spin_lock(&block_group->lock);
244-
if (block_group->iref) {
245-
block_group->iref = 0;
242+
if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags)) {
246243
block_group->inode = NULL;
247244
spin_unlock(&block_group->lock);
248245
iput(inode);
@@ -2876,7 +2873,8 @@ void btrfs_dump_free_space(struct btrfs_block_group *block_group,
28762873
if (btrfs_is_zoned(fs_info)) {
28772874
btrfs_info(fs_info, "free space %llu active %d",
28782875
block_group->zone_capacity - block_group->alloc_offset,
2879-
block_group->zone_is_active);
2876+
test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
2877+
&block_group->runtime_flags));
28802878
return;
28812879
}
28822880

@@ -4008,7 +4006,7 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group,
40084006
*trimmed = 0;
40094007

40104008
spin_lock(&block_group->lock);
4011-
if (block_group->removed) {
4009+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
40124010
spin_unlock(&block_group->lock);
40134011
return 0;
40144012
}
@@ -4038,7 +4036,7 @@ int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
40384036
*trimmed = 0;
40394037

40404038
spin_lock(&block_group->lock);
4041-
if (block_group->removed) {
4039+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
40424040
spin_unlock(&block_group->lock);
40434041
return 0;
40444042
}
@@ -4060,7 +4058,7 @@ int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
40604058
*trimmed = 0;
40614059

40624060
spin_lock(&block_group->lock);
4063-
if (block_group->removed) {
4061+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)) {
40644062
spin_unlock(&block_group->lock);
40654063
return 0;
40664064
}

fs/btrfs/scrub.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,
32573257
}
32583258
/* Block group removed? */
32593259
spin_lock(&bg->lock);
3260-
if (bg->removed) {
3260+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
32613261
spin_unlock(&bg->lock);
32623262
ret = 0;
32633263
break;
@@ -3597,7 +3597,7 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
35973597
* kthread or relocation.
35983598
*/
35993599
spin_lock(&bg->lock);
3600-
if (!bg->removed)
3600+
if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags))
36013601
ret = -EINVAL;
36023602
spin_unlock(&bg->lock);
36033603

@@ -3756,7 +3756,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
37563756

37573757
if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
37583758
spin_lock(&cache->lock);
3759-
if (!cache->to_copy) {
3759+
if (!test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags)) {
37603760
spin_unlock(&cache->lock);
37613761
btrfs_put_block_group(cache);
37623762
goto skip;
@@ -3773,7 +3773,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
37733773
* repair extents.
37743774
*/
37753775
spin_lock(&cache->lock);
3776-
if (cache->removed) {
3776+
if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
37773777
spin_unlock(&cache->lock);
37783778
btrfs_put_block_group(cache);
37793779
goto skip;
@@ -3933,8 +3933,8 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
39333933
* balance is triggered or it becomes used and unused again.
39343934
*/
39353935
spin_lock(&cache->lock);
3936-
if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3937-
cache->used == 0) {
3936+
if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags) &&
3937+
!cache->ro && cache->reserved == 0 && cache->used == 0) {
39383938
spin_unlock(&cache->lock);
39393939
if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
39403940
btrfs_discard_queue_work(&fs_info->discard_ctl,

fs/btrfs/space-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
305305
ASSERT(found);
306306
spin_lock(&found->lock);
307307
found->total_bytes += block_group->length;
308-
if (block_group->zone_is_active)
308+
if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags))
309309
found->active_total_bytes += block_group->length;
310310
found->disk_total += block_group->length * factor;
311311
found->bytes_used += block_group->used;

fs/btrfs/volumes.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5595,7 +5595,7 @@ int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
55955595
if (ret)
55965596
goto out;
55975597

5598-
bg->chunk_item_inserted = 1;
5598+
set_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED, &bg->runtime_flags);
55995599

56005600
if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
56015601
ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
@@ -6154,7 +6154,7 @@ static bool is_block_group_to_copy(struct btrfs_fs_info *fs_info, u64 logical)
61546154
cache = btrfs_lookup_block_group(fs_info, logical);
61556155

61566156
spin_lock(&cache->lock);
6157-
ret = cache->to_copy;
6157+
ret = test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags);
61586158
spin_unlock(&cache->lock);
61596159

61606160
btrfs_put_block_group(cache);
@@ -8244,7 +8244,7 @@ static int relocating_repair_kthread(void *data)
82448244
if (!cache)
82458245
goto out;
82468246

8247-
if (!cache->relocating_repair)
8247+
if (!test_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags))
82488248
goto out;
82498249

82508250
ret = btrfs_may_alloc_data_chunk(fs_info, target);
@@ -8282,12 +8282,11 @@ bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical)
82828282
return true;
82838283

82848284
spin_lock(&cache->lock);
8285-
if (cache->relocating_repair) {
8285+
if (test_and_set_bit(BLOCK_GROUP_FLAG_RELOCATING_REPAIR, &cache->runtime_flags)) {
82868286
spin_unlock(&cache->lock);
82878287
btrfs_put_block_group(cache);
82888288
return true;
82898289
}
8290-
cache->relocating_repair = 1;
82918290
spin_unlock(&cache->lock);
82928291

82938292
kthread_run(relocating_repair_kthread, cache,

0 commit comments

Comments
 (0)