btrfs: reduce size of struct find_free_extent_ctl
cover btrfs: reduce size of struct find_free_extent_ctl
From: Filipe Manana <[email protected]>
Reduce the size of struct find_free_extent_ctl and use bool type for some arguments that are defined as int but used as booleans. Details in the change logs.
Filipe Manana (2):
btrfs: use booleans for delalloc arguments and struct find_free_extent_ctl
btrfs: place all boolean fields together in struct find_free_extent_ctl
fs/btrfs/block-group.c | 2 +- fs/btrfs/block-group.h | 2 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent-tree.c | 16 +++++++--------- fs/btrfs/extent-tree.h | 24 ++++++++++++------------ fs/btrfs/inode.c | 8 ++++---- 6 files changed, 26 insertions(+), 28 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/2 btrfs: use booleans for delalloc arguments and struct find_free_extent_ctl
From: Filipe Manana <[email protected]>
The struct find_free_extent_ctl uses an int for the 'delalloc' field but it's always used as a boolean, and its value is used to be passed to several functions to signal if we are dealing with delalloc. The same goes for the 'is_data' argument from btrfs_reserve_extent(). So change the type from int to bool and move the field definition in the find_free_extent_ctl structure so that it's close to other bool fields and reduces the size of the structure from 144 down to 136 bytes (at the moment it's only declared in the stack of btrfs_reserve_extent(), never allocated otherwise).
--- fs/btrfs/block-group.c | 2 +- fs/btrfs/block-group.h | 2 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent-tree.c | 16 +++++++--------- fs/btrfs/extent-tree.h | 4 ++-- fs/btrfs/inode.c | 8 ++++---- 6 files changed, 16 insertions(+), 18 deletions(-)
fs/btrfs/block-group.c +1 -1
@@ -3802,7 +3802,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,* reservation and return -EAGAIN, otherwise this function always succeeds. */ int btrfs_add_reserved_bytes (struct btrfs_block_group *cache, -u64 ram_bytes,u64 num_bytes,int delalloc, +u64 ram_bytes,u64 num_bytes,bool delalloc,bool force_wrong_size_class) {struct btrfs_space_info *space_info = cache->space_info;
fs/btrfs/block-group.h +1 -1
@@ -345,7 +345,7 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);int btrfs_update_block_group (struct btrfs_trans_handle *trans,u64 bytenr,u64 num_bytes,bool alloc);int btrfs_add_reserved_bytes (struct btrfs_block_group *cache, -u64 ram_bytes,u64 num_bytes,int delalloc, +u64 ram_bytes,u64 num_bytes,bool delalloc,bool force_wrong_size_class);void btrfs_free_reserved_bytes (struct btrfs_block_group *cache,u64 num_bytes,bool is_delalloc);
fs/btrfs/direct-io.c +1 -1
@@ -186,7 +186,7 @@ static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, alloc_hint =btrfs_get_extent_allocation_hint (inode, start, len); again: ret =btrfs_reserve_extent (root, len, len, fs_info->sectorsize, -0 , alloc_hint, &ins,1 ,1 ); +0 , alloc_hint, &ins, true, true);if (ret == -EAGAIN ) {ASSERT (btrfs_is_zoned (fs_info)); wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH,
fs/btrfs/extent-tree.c +7 -9
@@ -3587,15 +3587,14 @@ enum btrfs_loop_type { };static inline void -btrfs_lock_block_group (struct btrfs_block_group *cache, -int delalloc) +btrfs_lock_block_group (struct btrfs_block_group *cache,bool delalloc) {if (delalloc)down_read (&cache->data_rwsem); }static inline void btrfs_grab_block_group (struct btrfs_block_group *cache, -int delalloc) +bool delalloc) {btrfs_get_block_group (cache);if (delalloc)
@@ -3605,7 +3604,7 @@ static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,static struct btrfs_block_group *btrfs_lock_cluster (struct btrfs_block_group *block_group,struct btrfs_free_cluster *cluster, -int delalloc) +bool delalloc) __acquires(&cluster->refill_lock) {struct btrfs_block_group *used_bg =NULL ;
@@ -3642,8 +3641,7 @@ static struct btrfs_block_group *btrfs_lock_cluster( }static inline void -btrfs_release_block_group (struct btrfs_block_group *cache, -int delalloc) +btrfs_release_block_group (struct btrfs_block_group *cache,bool delalloc) {if (delalloc)up_read (&cache->data_rwsem);
@@ -4033,7 +4031,7 @@ static int do_allocation(struct btrfs_block_group *block_group,static void release_block_group (struct btrfs_block_group *block_group,struct find_free_extent_ctl *ffe_ctl, -int delalloc) +bool delalloc) {switch (ffe_ctl->policy) {case BTRFS_EXTENT_ALLOC_CLUSTERED :
@@ -4689,7 +4687,7 @@ static noinline int find_free_extent(struct btrfs_root *root,int btrfs_reserve_extent (struct btrfs_root *root,u64 ram_bytes,u64 num_bytes,u64 min_alloc_size,u64 empty_size,u64 hint_byte, -struct btrfs_key *ins,int is_data,int delalloc) +struct btrfs_key *ins,bool is_data,bool delalloc) {struct btrfs_fs_info *fs_info = root->fs_info;struct find_free_extent_ctl ffe_ctl = {};
@@ -5166,7 +5164,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,return ERR_CAST (block_rsv); ret =btrfs_reserve_extent (root, blocksize, blocksize, blocksize, - empty_size, hint, &ins,0 ,0 ); + empty_size, hint, &ins, false, false);if (ret) goto out_unuse;
fs/btrfs/extent-tree.h +2 -2
@@ -30,7 +30,6 @@ struct find_free_extent_ctl {u64 min_alloc_size;u64 empty_size;u64 flags; -int delalloc;/* Where to start the search inside the bg */ u64 search_start;
@@ -40,6 +39,7 @@ struct find_free_extent_ctl {struct btrfs_free_cluster *last_ptr;bool use_cluster; +bool delalloc;bool have_caching_bg;bool orig_have_caching_bg;
@@ -137,7 +137,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, struct btrfs_key *ins);int btrfs_reserve_extent (struct btrfs_root *root,u64 ram_bytes,u64 num_bytes,u64 min_alloc_size,u64 empty_size,u64 hint_byte, -struct btrfs_key *ins,int is_data,int delalloc); +struct btrfs_key *ins,bool is_data,bool delalloc);int btrfs_inc_ref (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,bool full_backref); int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
fs/btrfs/inode.c +4 -4
@@ -1136,7 +1136,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk, ret =btrfs_reserve_extent (root, async_extent->ram_size, async_extent->compressed_size, async_extent->compressed_size, -0 , *alloc_hint, &ins,1 ,1 ); +0 , *alloc_hint, &ins, true, true);if (ret) {/* * We can't reserve contiguous space for the compressed size.
@@ -1359,7 +1359,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode, ret =btrfs_reserve_extent (root, num_bytes, num_bytes, min_alloc_size,0 , alloc_hint, - &ins,1 ,1 ); + &ins, true, true);if (ret == -EAGAIN ) {/* * btrfs_reserve_extent only returns -EAGAIN for zoned
@@ -9106,7 +9106,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,*/ cur_bytes =min (cur_bytes, last_alloc); ret =btrfs_reserve_extent (root, cur_bytes, cur_bytes, - min_size,0 , *alloc_hint, &ins,1 ,0 ); + min_size,0 , *alloc_hint, &ins, true, false);if (ret)break ;
@@ -9914,7 +9914,7 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from, } ret =btrfs_reserve_extent (root, disk_num_bytes, disk_num_bytes, - disk_num_bytes,0 ,0 , &ins,1 ,1 ); + disk_num_bytes,0 ,0 , &ins, true, true);if (ret) goto out_delalloc_release; extent_reserved = true;
Signature · 1 line
2.47.2
Re: [PATCH 1/2] btrfs: use booleans for delalloc arguments and struct find_free_extent_ctl
Quoted 26 lines · 在 2025/11/17 22:56, [email protected] 写道:
From: Filipe Manana <[email protected]>
The struct
find_free_extent_ctluses an int for the 'delalloc' field but it's always used as a boolean, and its value is used to be passed to several functions to signal if we are dealing with delalloc. The same goes for the 'is_data' argument frombtrfs_reserve_extent(). So change the type from int to bool and move the field definition in thefind_free_extent_ctlstructure so that it's close to other bool fields and reduces the size of the structure from 144 down to 136 bytes (at the moment it's only declared in the stack ofbtrfs_reserve_extent(), never allocated otherwise).Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/block-group.c | 2 +- fs/btrfs/block-group.h | 2 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent-tree.c | 16 +++++++--------- fs/btrfs/extent-tree.h | 4 ++-- fs/btrfs/inode.c | 8 ++++---- 6 files changed, 16 insertions(+), 18 deletions(-)fs/btrfs/block-group.c +0
-0
fs/btrfs/block-group.c int btrfs_update_block_group(struct btrfs_trans_handle *trans, · line 3806
Quoted 6 lines
fs/btrfs/block-group.c +1
-1@@ -3802,7 +3802,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,* reservation and return -EAGAIN, otherwise this function always succeeds. */ int btrfs_add_reserved_bytes (struct btrfs_block_group *cache, -u64 ram_bytes,u64 num_bytes,int delalloc, +u64 ram_bytes,u64 num_bytes,bool delalloc,bool force_wrong_size_class)
Unrelated to the patch, as it still looks good to me.
I'm a little concerned about the double bool parameters.
It always looks like a timed bomb, as for newer callers it's not that hard to get the true/false sequence wrong.
For the structure member I'm completely fine with boolean members, but for function parameters can we avoid double bools?
Like using flags or enum instead so that the function calls are more clear about their special behaviors?
Thanks,
Qu
Quoted 165 lines
fs/btrfs/block-group.c +0
-0{struct btrfs_space_info *space_info = cache->space_info;fs/btrfs/block-group.h +1
-1@@ -345,7 +345,7 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);int btrfs_update_block_group (struct btrfs_trans_handle *trans,u64 bytenr,u64 num_bytes,bool alloc);int btrfs_add_reserved_bytes (struct btrfs_block_group *cache, -u64 ram_bytes,u64 num_bytes,int delalloc, +u64 ram_bytes,u64 num_bytes,bool delalloc,bool force_wrong_size_class);void btrfs_free_reserved_bytes (struct btrfs_block_group *cache,u64 num_bytes,bool is_delalloc);fs/btrfs/direct-io.c +1
-1@@ -186,7 +186,7 @@ static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode, alloc_hint =btrfs_get_extent_allocation_hint (inode, start, len); again: ret =btrfs_reserve_extent (root, len, len, fs_info->sectorsize, -0 , alloc_hint, &ins,1 ,1 ); +0 , alloc_hint, &ins, true, true);if (ret == -EAGAIN ) {ASSERT (btrfs_is_zoned (fs_info)); wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH,fs/btrfs/extent-tree.c +7
-9@@ -3587,15 +3587,14 @@ enum btrfs_loop_type { };static inline void -btrfs_lock_block_group (struct btrfs_block_group *cache, -int delalloc) +btrfs_lock_block_group (struct btrfs_block_group *cache,bool delalloc) {if (delalloc)down_read (&cache->data_rwsem); }static inline void btrfs_grab_block_group (struct btrfs_block_group *cache, -int delalloc) +bool delalloc) {btrfs_get_block_group (cache);if (delalloc)@@ -3605,7 +3604,7 @@ static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,static struct btrfs_block_group *btrfs_lock_cluster (struct btrfs_block_group *block_group,struct btrfs_free_cluster *cluster, -int delalloc) +bool delalloc) __acquires(&cluster->refill_lock) {struct btrfs_block_group *used_bg =NULL ;@@ -3642,8 +3641,7 @@ static struct btrfs_block_group *btrfs_lock_cluster( }static inline void -btrfs_release_block_group (struct btrfs_block_group *cache, -int delalloc) +btrfs_release_block_group (struct btrfs_block_group *cache,bool delalloc) {if (delalloc)up_read (&cache->data_rwsem);@@ -4033,7 +4031,7 @@ static int do_allocation(struct btrfs_block_group *block_group,static void release_block_group (struct btrfs_block_group *block_group,struct find_free_extent_ctl *ffe_ctl, -int delalloc) +bool delalloc) {switch (ffe_ctl->policy) {case BTRFS_EXTENT_ALLOC_CLUSTERED :@@ -4689,7 +4687,7 @@ static noinline int find_free_extent(struct btrfs_root *root,int btrfs_reserve_extent (struct btrfs_root *root,u64 ram_bytes,u64 num_bytes,u64 min_alloc_size,u64 empty_size,u64 hint_byte, -struct btrfs_key *ins,int is_data,int delalloc) +struct btrfs_key *ins,bool is_data,bool delalloc) {struct btrfs_fs_info *fs_info = root->fs_info;struct find_free_extent_ctl ffe_ctl = {};@@ -5166,7 +5164,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,return ERR_CAST (block_rsv); ret =btrfs_reserve_extent (root, blocksize, blocksize, blocksize, - empty_size, hint, &ins,0 ,0 ); + empty_size, hint, &ins, false, false);if (ret) goto out_unuse;fs/btrfs/extent-tree.h +2
-2@@ -30,7 +30,6 @@ struct find_free_extent_ctl {u64 min_alloc_size;u64 empty_size;u64 flags; -int delalloc;/* Where to start the search inside the bg */ u64 search_start;@@ -40,6 +39,7 @@ struct find_free_extent_ctl {struct btrfs_free_cluster *last_ptr;bool use_cluster; +bool delalloc;bool have_caching_bg;bool orig_have_caching_bg;@@ -137,7 +137,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, struct btrfs_key *ins);int btrfs_reserve_extent (struct btrfs_root *root,u64 ram_bytes,u64 num_bytes,u64 min_alloc_size,u64 empty_size,u64 hint_byte, -struct btrfs_key *ins,int is_data,int delalloc); +struct btrfs_key *ins,bool is_data,bool delalloc);int btrfs_inc_ref (struct btrfs_trans_handle *trans,struct btrfs_root *root,struct extent_buffer *buf,bool full_backref); int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,fs/btrfs/inode.c +4
-4@@ -1136,7 +1136,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk, ret =btrfs_reserve_extent (root, async_extent->ram_size, async_extent->compressed_size, async_extent->compressed_size, -0 , *alloc_hint, &ins,1 ,1 ); +0 , *alloc_hint, &ins, true, true);if (ret) {/* * We can't reserve contiguous space for the compressed size. @@ -1359,7 +1359,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode, ret =btrfs_reserve_extent (root, num_bytes, num_bytes, min_alloc_size,0 , alloc_hint, - &ins,1 ,1 ); + &ins, true, true);if (ret == -EAGAIN ) {/* * btrfs_reserve_extent only returns -EAGAIN for zoned @@ -9106,7 +9106,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,*/ cur_bytes =min (cur_bytes, last_alloc); ret =btrfs_reserve_extent (root, cur_bytes, cur_bytes, - min_size,0 , *alloc_hint, &ins,1 ,0 ); + min_size,0 , *alloc_hint, &ins, true, false);if (ret)break ;@@ -9914,7 +9914,7 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from, } ret =btrfs_reserve_extent (root, disk_num_bytes, disk_num_bytes, - disk_num_bytes,0 ,0 , &ins,1 ,1 ); + disk_num_bytes,0 ,0 , &ins, true, true);if (ret) goto out_delalloc_release; extent_reserved = true;
PATCH 2/2 btrfs: place all boolean fields together in struct find_free_extent_ctl
In reply to [email protected]From: Filipe Manana <[email protected]>
Move the 'retry_uncached' and 'hint' fields close to the other boolean fields so that we remove a hole from the structure and reduce its size from 136 bytes down to 128 bytes. Currently this structure is only allocated in the stack of btrfs_reserve_extent().
--- fs/btrfs/extent-tree.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
fs/btrfs/extent-tree.h +10 -10
@@ -49,6 +49,16 @@ struct find_free_extent_ctl {/* Allocation is called for data relocation */ bool for_data_reloc; +/* +* Set to true if we're retrying the allocation on this block group +* after waiting for caching progress, this is so that we retry only +* once before moving on to another block group. +*/ +bool retry_uncached; + +/* Whether or not the allocator is currently following a hint */ +bool hinted; +/* RAID index, converted from flags */ int index;
@@ -57,13 +67,6 @@ struct find_free_extent_ctl {*/ int loop; -/* -* Set to true if we're retrying the allocation on this block group -* after waiting for caching progress, this is so that we retry only -* once before moving on to another block group. -*/ -bool retry_uncached; -/* If current block group is cached */ int cached;
@@ -82,9 +85,6 @@ struct find_free_extent_ctl {/* Allocation policy */ enum btrfs_extent_allocation_policy policy; -/* Whether or not the allocator is currently following a hint */ -bool hinted; -/* Size class of block groups to prefer in early loops */ enum btrfs_block_group_size_class size_class; };
Signature · 1 line
2.47.2
Looks good,
Quoted 6 lines · 在 2025/11/17 22:56, [email protected] 写道:
From: Filipe Manana <[email protected]>
Reduce the size of struct
find_free_extent_ctland use bool type for some arguments that are defined as int but used as booleans. Details in the change logs.
Thanks,
Qu
Quoted 12 lines
Filipe Manana (2):
btrfs: use booleans for delalloc arguments and structfind_free_extent_ctl
btrfs: place all boolean fields together in structfind_free_extent_ctlfs/btrfs/block-group.c | 2 +- fs/btrfs/block-group.h | 2 +- fs/btrfs/direct-io.c | 2 +- fs/btrfs/extent-tree.c | 16 +++++++--------- fs/btrfs/extent-tree.h | 24 ++++++++++++------------ fs/btrfs/inode.c | 8 ++++---- 6 files changed, 26 insertions(+), 28 deletions(-)