btrfs: free space tree optimization and cleanups
cover btrfs: free space tree optimization and cleanups
From: Filipe Manana <[email protected]>
A free space tree optimization (last patch in the series), and the rest mostly cleanups and preparatory work. Details in the change logs.
Filipe Manana (16):
btrfs: remove pointless out label from add_new_free_space_info()
btrfs: remove pointless out label from update_free_space_extent_count()
btrfs: make extent_buffer_test_bit() return a boolean instead
btrfs: make free_space_test_bit() return a boolean instead
btrfs: remove pointless out label from modify_free_space_bitmap()
btrfs: remove pointless out label from remove_free_space_extent()
btrfs: remove pointless out label from add_free_space_extent()
btrfs: remove pointless out label from load_free_space_bitmaps()
btrfs: remove pointless out label from load_free_space_extents()
btrfs: add btrfs prefix to free space tree exported functions
btrfs: rename free_space_set_bits() and make it less confusing
btrfs: turn remove argument of modify_free_space_bitmap() to boolean
btrfs: avoid double slot decrement at btrfs_convert_free_space_to_extents()
btrfs: use fs_info from local variable in btrfs_convert_free_space_to_extents()
btrfs: add and use helper to determine if using bitmaps in free space tree
btrfs: cache if we are using free space bitmaps for a block group
fs/btrfs/block-group.c | 10 +- fs/btrfs/block-group.h | 5 + fs/btrfs/extent-tree.c | 4 +- fs/btrfs/extent_io.c | 4 +- fs/btrfs/extent_io.h | 4 +- fs/btrfs/free-space-tree.c | 293 ++++++++++++------------- fs/btrfs/free-space-tree.h | 52 ++--- fs/btrfs/tests/extent-io-tests.c | 14 +- fs/btrfs/tests/free-space-tree-tests.c | 93 ++++---- 9 files changed, 234 insertions(+), 245 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/16 btrfs: remove pointless out label from add_new_free_space_info()
From: Filipe Manana <[email protected]>
We can just return directly if btrfs_insert_empty_item() fails, there is no need to release the path before returning, as all callers (or upper in the call chain) will free the path if they get an error from the call to add_new_free_space_info(), which is also a common pattern everywhere in btrfs. Finally there's no need to set 'ret' to 0 if the call to btrfs_insert_empty_item() didn't fail, since 'ret' is already 0.
--- fs/btrfs/free-space-tree.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/free-space-tree.c +2 -5
@@ -82,18 +82,15 @@ static int add_new_free_space_info(struct btrfs_trans_handle *trans, ret =btrfs_insert_empty_item (trans, root, path, &key,sizeof (*info));if (ret) - goto out; +return ret; leaf = path->nodes[0 ]; info =btrfs_item_ptr (leaf, path->slots[0 ], struct btrfs_free_space_info);btrfs_set_free_space_extent_count (leaf, info,0 );btrfs_set_free_space_flags (leaf, info,0 ); - - ret =0 ; -out:btrfs_release_path (path); -return ret; +return 0 ; }EXPORT_FOR_TESTS
Signature · 1 line
2.47.2
PATCH 2/16 btrfs: remove pointless out label from update_free_space_extent_count()
In reply to [email protected]From: Filipe Manana <[email protected]>
Just return directly, we don't need the label since all we do under it is to return.
--- fs/btrfs/free-space-tree.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/free-space-tree.c +3 -5
@@ -491,10 +491,9 @@ static int update_free_space_extent_count(struct btrfs_trans_handle *trans,return 0 ; info =search_free_space_info (trans, block_group, path,1 ); -if (IS_ERR (info)) { - ret =PTR_ERR (info); - goto out; - } +if (IS_ERR (info)) +return PTR_ERR (info); + flags =btrfs_free_space_flags (path->nodes[0 ], info); extent_count =btrfs_free_space_extent_count (path->nodes[0 ], info);
@@ -510,7 +509,6 @@ static int update_free_space_extent_count(struct btrfs_trans_handle *trans, ret =convert_free_space_to_extents (trans, block_group, path); } -out:return ret; }
Signature · 1 line
2.47.2
PATCH 3/16 btrfs: make extent_buffer_test_bit() return a boolean instead
In reply to [email protected]From: Filipe Manana <[email protected]>
All the callers want is to determine if a bit is set and all of them call the function and do a double negation (!!) on its result to get a boolean. So change it to return a boolean and simplify callers.
--- fs/btrfs/extent_io.c | 4 ++-- fs/btrfs/extent_io.h | 4 ++-- fs/btrfs/free-space-tree.c | 2 +- fs/btrfs/tests/extent-io-tests.c | 14 +++++++------- 4 files changed, 12 insertions(+), 12 deletions(-)
fs/btrfs/extent_io.c +2 -2
@@ -4108,8 +4108,8 @@ static inline void eb_bitmap_offset(const struct extent_buffer *eb,* @start: offset of the bitmap item in the extent buffer * @nr: bit number to test */ -int extent_buffer_test_bit (const struct extent_buffer *eb,unsigned long start, -unsigned long nr) +bool extent_buffer_test_bit (const struct extent_buffer *eb,unsigned long start, +unsigned long nr) {unsigned long i;size_t offset;
fs/btrfs/extent_io.h +2 -2
@@ -345,8 +345,8 @@ void memmove_extent_buffer(const struct extent_buffer *dst, unsigned long len);void memzero_extent_buffer (const struct extent_buffer *eb,unsigned long start,unsigned long len); -int extent_buffer_test_bit (const struct extent_buffer *eb,unsigned long start, -unsigned long pos); +bool extent_buffer_test_bit (const struct extent_buffer *eb,unsigned long start, +unsigned long pos);void extent_buffer_bitmap_set (const struct extent_buffer *eb,unsigned long start,unsigned long pos,unsigned long len); void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
fs/btrfs/free-space-tree.c +1 -1
@@ -532,7 +532,7 @@ int free_space_test_bit(struct btrfs_block_group *block_group, ptr =btrfs_item_ptr_offset (leaf, path->slots[0 ]); i =div_u64 (offset - found_start, block_group->fs_info->sectorsize); -return !!extent_buffer_test_bit (leaf, ptr, i); +return extent_buffer_test_bit (leaf, ptr, i); } static void free_space_set_bits(struct btrfs_trans_handle *trans,
fs/btrfs/tests/extent-io-tests.c +7 -7
@@ -343,11 +343,11 @@ static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb)unsigned long i;for (i =0 ; i < eb->len *BITS_PER_BYTE ; i++) { -int bit, bit1; +bool bit_set, bit1_set; - bit = !!test_bit (i, bitmap); - bit1 = !!extent_buffer_test_bit (eb,0 , i); -if (bit1 != bit) { + bit_set =test_bit (i, bitmap); + bit1_set =extent_buffer_test_bit (eb,0 , i); +if (bit1_set != bit_set) {u8 has;u8 expect;
@@ -360,9 +360,9 @@ static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb)return -EINVAL ; } - bit1 = !!extent_buffer_test_bit (eb, i /BITS_PER_BYTE , - i %BITS_PER_BYTE ); -if (bit1 != bit) { + bit1_set =extent_buffer_test_bit (eb, i /BITS_PER_BYTE , + i %BITS_PER_BYTE ); +if (bit1_set != bit_set) {u8 has;u8 expect;
Signature · 1 line
2.47.2
PATCH 4/16 btrfs: make free_space_test_bit() return a boolean instead
In reply to [email protected]From: Filipe Manana <[email protected]>
The function returns the result of another function that returns a boolean (extent_buffer_test_bit()), and all the callers need is a boolean an not an integer. So change its return type from int to bool, and modify the callers to store results in booleans instead of integers, which also makes them simpler.
--- fs/btrfs/free-space-tree.c | 37 ++++++++++++++++++------------------- fs/btrfs/free-space-tree.h | 4 ++-- 2 files changed, 20 insertions(+), 21 deletions(-)
fs/btrfs/free-space-tree.c +18 -19
@@ -513,8 +513,8 @@ static int update_free_space_extent_count(struct btrfs_trans_handle *trans, }EXPORT_FOR_TESTS -int free_space_test_bit(struct btrfs_block_group *block_group, -struct btrfs_path *path,u64 offset) +bool free_space_test_bit(struct btrfs_block_group *block_group, +struct btrfs_path *path,u64 offset) {struct extent_buffer *leaf;struct btrfs_key key;
@@ -612,7 +612,8 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,struct btrfs_key key;u64 end = start + size;u64 cur_start, cur_size; -int prev_bit, next_bit; +bool prev_bit_set = false; +bool next_bit_set = false;int new_extents;int ret;
@@ -631,7 +632,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,if (ret) goto out; - prev_bit =free_space_test_bit (block_group, path, prev_block); + prev_bit_set =free_space_test_bit (block_group, path, prev_block);/* The previous block may have been in the previous bitmap. */ btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]);
@@ -648,8 +649,6 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,0 ,1 );if (ret) goto out; - - prev_bit =-1 ; }/*
@@ -681,28 +680,26 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, goto out; } - next_bit =free_space_test_bit (block_group, path, end); - }else { - next_bit =-1 ; + next_bit_set =free_space_test_bit (block_group, path, end); }if (remove) { new_extents =-1 ; -if (prev_bit ==1 ) { +if (prev_bit_set) {/* Leftover on the left. */ new_extents++; } -if (next_bit ==1 ) { +if (next_bit_set) {/* Leftover on the right. */ new_extents++; } }else { new_extents =1 ; -if (prev_bit ==1 ) { +if (prev_bit_set) {/* Merging with neighbor on the left. */ new_extents--; } -if (next_bit ==1 ) { +if (next_bit_set) {/* Merging with neighbor on the right. */ new_extents--; }
@@ -1552,7 +1549,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,struct btrfs_fs_info *fs_info;struct btrfs_root *root;struct btrfs_key key; -int prev_bit =0 , bit; +bool prev_bit_set = false;/* Initialize to silence GCC. */ u64 extent_start =0 ;u64 end, offset;
@@ -1583,10 +1580,12 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl, offset = key.objectid;while (offset < key.objectid + key.offset) { - bit =free_space_test_bit (block_group, path, offset); -if (prev_bit ==0 && bit ==1 ) { +bool bit_set; + + bit_set =free_space_test_bit (block_group, path, offset); +if (!prev_bit_set && bit_set) { extent_start = offset; - }else if (prev_bit ==1 && bit ==0 ) { + }else if (prev_bit_set && !bit_set) {u64 space_added; ret = btrfs_add_new_free_space(block_group,
@@ -1602,11 +1601,11 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl, } extent_count++; } - prev_bit = bit; + prev_bit_set = bit_set; offset += fs_info->sectorsize; } } -if (prev_bit ==1 ) { +if (prev_bit_set) { ret =btrfs_add_new_free_space (block_group, extent_start, end,NULL );if (ret) goto out;
fs/btrfs/free-space-tree.h +2 -2
@@ -53,8 +53,8 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,int convert_free_space_to_extents (struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group,struct btrfs_path *path); -int free_space_test_bit (struct btrfs_block_group *block_group, -struct btrfs_path *path,u64 offset); +bool free_space_test_bit (struct btrfs_block_group *block_group, +struct btrfs_path *path,u64 offset);#endif #endif
Signature · 1 line
2.47.2
PATCH 5/16 btrfs: remove pointless out label from modify_free_space_bitmap()
In reply to [email protected]From: Filipe Manana <[email protected]>
All we do under the label is to return, so there's no point in having it, just return directly whenever we get an error.
--- fs/btrfs/free-space-tree.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
fs/btrfs/free-space-tree.c +6 -10
@@ -630,7 +630,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,0 ,1 );if (ret) - goto out; +return ret; prev_bit_set =free_space_test_bit (block_group, path, prev_block);
@@ -639,7 +639,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,if (start >= key.objectid + key.offset) { ret =free_space_next_bitmap (trans, root, path);if (ret) - goto out; +return ret; } }else { key.objectid = start;
@@ -648,7 +648,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,0 ,1 );if (ret) - goto out; +return ret; }/*
@@ -664,7 +664,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,break ; ret =free_space_next_bitmap (trans, root, path);if (ret) - goto out; +return ret; }/*
@@ -677,7 +677,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,if (end >= key.objectid + key.offset) { ret =free_space_next_bitmap (trans, root, path);if (ret) - goto out; +return ret; } next_bit_set =free_space_test_bit (block_group, path, end);
@@ -706,11 +706,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, }btrfs_release_path (path); - ret =update_free_space_extent_count (trans, block_group, path, - new_extents); - -out: -return ret; +return update_free_space_extent_count (trans, block_group, path, new_extents); } static int remove_free_space_extent(struct btrfs_trans_handle *trans,
Signature · 1 line
2.47.2
PATCH 6/16 btrfs: remove pointless out label from remove_free_space_extent()
In reply to [email protected]From: Filipe Manana <[email protected]>
All we do under the label is to return, so there's no point in having it, just return directly whenever we get an error.
--- fs/btrfs/free-space-tree.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
fs/btrfs/free-space-tree.c +5 -9
@@ -727,7 +727,7 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,-1 ,1 );if (ret) - goto out; +return ret;btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]);
@@ -759,7 +759,7 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans,/* Delete the existing key (cases 1-4). */ ret =btrfs_del_item (trans, root, path);if (ret) - goto out; +return ret;/* Add a key for leftovers at the beginning (cases 3 and 4). */ if (start > found_start) {
@@ -770,7 +770,7 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans,btrfs_release_path (path); ret =btrfs_insert_empty_item (trans, root, path, &key,0 );if (ret) - goto out; +return ret; new_extents++; }
@@ -783,16 +783,12 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans,btrfs_release_path (path); ret =btrfs_insert_empty_item (trans, root, path, &key,0 );if (ret) - goto out; +return ret; new_extents++; }btrfs_release_path (path); - ret =update_free_space_extent_count (trans, block_group, path, - new_extents); - -out: -return ret; +return update_free_space_extent_count (trans, block_group, path, new_extents); }EXPORT_FOR_TESTS
Signature · 1 line
2.47.2
PATCH 7/16 btrfs: remove pointless out label from add_free_space_extent()
In reply to [email protected]From: Filipe Manana <[email protected]>
All we do under the label is to return, so there's no point in having it, just return directly whenever we get an error.
--- fs/btrfs/free-space-tree.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
fs/btrfs/free-space-tree.c +6 -10
@@ -900,7 +900,7 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,-1 ,1 );if (ret) - goto out; +return ret;btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]);
@@ -923,7 +923,7 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans,if (found_end == start) { ret =btrfs_del_item (trans, root, path);if (ret) - goto out; +return ret; new_key.objectid = found_start; new_key.offset += key.offset; new_extents--;
@@ -940,7 +940,7 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans, ret =btrfs_search_prev_slot (trans, root, &key, path,-1 ,1 );if (ret) - goto out; +return ret;btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]);
@@ -964,7 +964,7 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans,if (found_start == end) { ret =btrfs_del_item (trans, root, path);if (ret) - goto out; +return ret; new_key.offset += key.offset; new_extents--; }
@@ -974,14 +974,10 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans,/* Insert the new key (cases 1-4). */ ret =btrfs_insert_empty_item (trans, root, path, &new_key,0 );if (ret) - goto out; +return ret;btrfs_release_path (path); - ret =update_free_space_extent_count (trans, block_group, path, - new_extents); - -out: -return ret; +return update_free_space_extent_count (trans, block_group, path, new_extents); }EXPORT_FOR_TESTS
Signature · 1 line
2.47.2
PATCH 8/16 btrfs: remove pointless out label from load_free_space_bitmaps()
In reply to [email protected]From: Filipe Manana <[email protected]>
All we do under the label is to return, so there's no point in having it, just return directly whenever we get an error.
--- fs/btrfs/free-space-tree.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
fs/btrfs/free-space-tree.c +5 -8
@@ -1554,7 +1554,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,while (1 ) { ret =btrfs_next_item (root, path);if (ret <0 ) - goto out; +return ret;if (ret)break ;
@@ -1581,7 +1581,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl, offset, &space_added);if (ret) - goto out; +return ret; total_found += space_added;if (total_found >CACHING_CTL_WAKE_UP ) { total_found =0 ;
@@ -1596,7 +1596,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,if (prev_bit_set) { ret =btrfs_add_new_free_space (block_group, extent_start, end,NULL );if (ret) - goto out; +return ret; extent_count++; }
@@ -1606,13 +1606,10 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl, block_group->start, extent_count, expected_extent_count);DEBUG_WARN (); - ret = -EIO ; - goto out; +return -EIO ; } - ret =0 ; -out: -return ret; +return 0 ; } static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
Signature · 1 line
2.47.2
PATCH 9/16 btrfs: remove pointless out label from load_free_space_extents()
In reply to [email protected]From: Filipe Manana <[email protected]>
All we do under the label is to return, so there's no point in having it, just return directly whenever we get an error.
--- fs/btrfs/free-space-tree.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
fs/btrfs/free-space-tree.c +4 -7
@@ -1636,7 +1636,7 @@ static int load_free_space_extents(struct btrfs_caching_control *caching_ctl, ret =btrfs_next_item (root, path);if (ret <0 ) - goto out; +return ret;if (ret)break ;
@@ -1652,7 +1652,7 @@ static int load_free_space_extents(struct btrfs_caching_control *caching_ctl, key.objectid + key.offset, &space_added);if (ret) - goto out; +return ret; total_found += space_added;if (total_found >CACHING_CTL_WAKE_UP ) { total_found =0 ;
@@ -1667,13 +1667,10 @@ static int load_free_space_extents(struct btrfs_caching_control *caching_ctl, block_group->start, extent_count, expected_extent_count);DEBUG_WARN (); - ret = -EIO ; - goto out; +return -EIO ; } - ret =0 ; -out: -return ret; +return 0 ; }int load_free_space_tree (struct btrfs_caching_control *caching_ctl)
Signature · 1 line
2.47.2
PATCH 10/16 btrfs: add btrfs prefix to free space tree exported functions
In reply to [email protected]From: Filipe Manana <[email protected]>
A few of the free space tree exported functions have a 'btrfs_' prefix in their name, but most don't. Not only is this inconsistent, the preferred style is to have such a prefix, to avoid potential collisions in the future with other kernel code and offer a somewhat better readibility by making it obvious in calls sites that we are calling btrfs specific code.
So add the 'btrfs_' prefix to all free space tree functions that are missing it.
--- fs/btrfs/block-group.c | 10 +-- fs/btrfs/extent-tree.c | 4 +- fs/btrfs/free-space-tree.c | 95 +++++++++++++------------- fs/btrfs/free-space-tree.h | 52 +++++++------- fs/btrfs/tests/free-space-tree-tests.c | 93 ++++++++++++------------- 5 files changed, 125 insertions(+), 129 deletions(-)
fs/btrfs/block-group.c +5 -5
@@ -877,7 +877,7 @@ static noinline void caching_thread(struct btrfs_work *work)*/ if (btrfs_fs_compat_ro (fs_info,FREE_SPACE_TREE ) && !(test_bit (BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED , &fs_info->flags))) - ret =load_free_space_tree (caching_ctl); + ret =btrfs_load_free_space_tree (caching_ctl);else ret =load_extent_tree_free (caching_ctl); done:
@@ -1235,7 +1235,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,* another task to attempt to create another block group with the same * item key (and failing with -EEXIST and a transaction abort). */ - ret =remove_block_group_free_space (trans, block_group); + ret =btrfs_remove_block_group_free_space (trans, block_group);if (ret) goto out;
@@ -2372,7 +2372,7 @@ static int read_one_block_group(struct btrfs_fs_info *info, cache->global_root_id =btrfs_stack_block_group_chunk_objectid (bgi); cache->space_info =btrfs_find_space_info (info, cache->flags); -set_free_space_tree_thresholds (cache); +btrfs_set_free_space_tree_thresholds (cache);if (need_clear) {/*
@@ -2791,7 +2791,7 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) block_group->length);if (ret)btrfs_abort_transaction (trans, ret); -add_block_group_free_space (trans, block_group); +btrfs_add_block_group_free_space (trans, block_group);/* * If we restriped during balance, we may have added a new raid
@@ -2889,7 +2889,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *transet_bit (BLOCK_GROUP_FLAG_NEW , &cache->runtime_flags); cache->length = size; -set_free_space_tree_thresholds (cache); +btrfs_set_free_space_tree_thresholds (cache); cache->flags = type; cache->cached =BTRFS_CACHE_FINISHED ; cache->global_root_id =calculate_global_root_id (fs_info, cache->start);
fs/btrfs/extent-tree.c +2 -2
@@ -3000,7 +3000,7 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans,return ret; } - ret =add_to_free_space_tree (trans, bytenr, num_bytes); + ret =btrfs_add_to_free_space_tree (trans, bytenr, num_bytes);if (ret) {btrfs_abort_transaction (trans, ret);return ret;
@@ -4784,7 +4784,7 @@ static int alloc_reserved_extent(struct btrfs_trans_handle *trans, u64 bytenr,struct btrfs_fs_info *fs_info = trans->fs_info;int ret; - ret =remove_from_free_space_tree (trans, bytenr, num_bytes); + ret =btrfs_remove_from_free_space_tree (trans, bytenr, num_bytes);if (ret)return ret;
fs/btrfs/free-space-tree.c +47 -48
259 lines not on this page · raw message
fs/btrfs/free-space-tree.h +26 -26
65 lines not on this page · raw message
fs/btrfs/tests/free-space-tree-tests.c +45 -48
238 lines not on this page · raw message
Signature · 1 line
2.47.2
PATCH 11/16 btrfs: rename free_space_set_bits() and make it less confusing
In reply to [email protected]From: Filipe Manana <[email protected]>
The free_space_set_bits() is used both to set a range of bits or to clear range of bits, depending on the 'bit' argument value. So the name is very misleading since it's not used only to set bits. Furthermore the 'bit' argument is an integer when a boolean is all that is needed plus it's name is singular, which gives the idea the function operates on a single bit and not on a range of bits.
So rename the function to free_space_modify_bits(), rename the 'bit' argument to 'set_bits' and turn the argument to bool instead of int.
--- fs/btrfs/free-space-tree.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/free-space-tree.c +7 -7
@@ -535,10 +535,10 @@ bool btrfs_free_space_test_bit(struct btrfs_block_group *block_group,return extent_buffer_test_bit (leaf, ptr, i); } -static void free_space_set_bits (struct btrfs_trans_handle *trans, -struct btrfs_block_group *block_group, -struct btrfs_path *path,u64 *start,u64 *size, -int bit) +static void free_space_modify_bits (struct btrfs_trans_handle *trans, +struct btrfs_block_group *block_group, +struct btrfs_path *path,u64 *start,u64 *size, +bool set_bits) {struct btrfs_fs_info *fs_info = block_group->fs_info;struct extent_buffer *leaf;
@@ -562,7 +562,7 @@ static void free_space_set_bits(struct btrfs_trans_handle *trans, ptr =btrfs_item_ptr_offset (leaf, path->slots[0 ]); first = (*start - found_start) >> fs_info->sectorsize_bits; last = (end - found_start) >> fs_info->sectorsize_bits; -if (bit) +if (set_bits)extent_buffer_bitmap_set (leaf, ptr, first, last - first);else extent_buffer_bitmap_clear (leaf, ptr, first, last - first);
@@ -658,8 +658,8 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, cur_start = start; cur_size = size;while (1 ) { -free_space_set_bits (trans, block_group, path, &cur_start, &cur_size, - !remove); +free_space_modify_bits (trans, block_group, path, &cur_start, + &cur_size, !remove);if (cur_size ==0 )break ; ret =free_space_next_bitmap (trans, root, path);
Signature · 1 line
2.47.2
PATCH 12/16 btrfs: turn remove argument of modify_free_space_bitmap() to boolean
In reply to [email protected]From: Filipe Manana <[email protected]>
The argument is used as a boolean, so switch its type from int to bool.
--- fs/btrfs/free-space-tree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/free-space-tree.c +3 -3
@@ -606,7 +606,7 @@ static int free_space_next_bitmap(struct btrfs_trans_handle *trans,static int modify_free_space_bitmap (struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group,struct btrfs_path *path, -u64 start,u64 size,int remove) +u64 start,u64 size,bool remove) {struct btrfs_root *root =btrfs_free_space_root (block_group);struct btrfs_key key;
@@ -812,7 +812,7 @@ int __btrfs_remove_from_free_space_tree(struct btrfs_trans_handle *trans,if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) {return modify_free_space_bitmap (trans, block_group, path, - start, size,1 ); + start, size, true); }else {return remove_free_space_extent (trans, block_group, path, start, size);
@@ -1000,7 +1000,7 @@ int __btrfs_add_to_free_space_tree(struct btrfs_trans_handle *trans,if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) {return modify_free_space_bitmap (trans, block_group, path, - start, size,0 ); + start, size, false); }else {return add_free_space_extent (trans, block_group, path, start, size);
Signature · 1 line
2.47.2
PATCH 13/16 btrfs: avoid double slot decrement at btrfs_convert_free_space_to_extents()
In reply to [email protected]From: Filipe Manana <[email protected]>
There's no need to subtract 1 from path->slots[0] and then decrement the slot, we can reduce the generated assembly code by decrementing the slot and then use it.
Module size before:
$ size fs/btrfs/btrfs.ko
text data bss dec hex filename
1846220 162998 16136 2025354 1ee78a fs/btrfs/btrfs.koModule size after:
$ size fs/btrfs/btrfs.ko
text data bss dec hex filename
1846204 162998 16136 2025338 1ee77a fs/btrfs/btrfs.ko--- fs/btrfs/free-space-tree.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/free-space-tree.c +2 -2
@@ -406,12 +406,12 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, data_size =free_space_bitmap_size (fs_info, found_key.offset); - ptr =btrfs_item_ptr_offset (leaf, path->slots[0 ] -1 ); + path->slots[0 ]--; + ptr =btrfs_item_ptr_offset (leaf, path->slots[0 ]);read_extent_buffer (leaf, bitmap_cursor, ptr, data_size); nr++; - path->slots[0 ]--; }else {ASSERT (0 ); }
Signature · 1 line
2.47.2
PATCH 14/16 btrfs: use fs_info from local variable in btrfs_convert_free_space_to_extents()
In reply to [email protected]From: Filipe Manana <[email protected]>
There's no need to dereference the block group to extract fs_info as we have already stored fs_info in a local variable. So use the local variable instead.
--- fs/btrfs/free-space-tree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/free-space-tree.c +3 -3
@@ -438,16 +438,16 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path); - nrbits = block_group->length >> block_group->fs_info->sectorsize_bits; + nrbits = block_group->length >> fs_info->sectorsize_bits; start_bit =find_next_bit_le (bitmap, nrbits,0 );while (start_bit < nrbits) { end_bit =find_next_zero_bit_le (bitmap, nrbits, start_bit);ASSERT (start_bit < end_bit); - key.objectid = start + start_bit * block_group->fs_info->sectorsize; + key.objectid = start + start_bit * fs_info->sectorsize; key.type =BTRFS_FREE_SPACE_EXTENT_KEY ; - key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize; + key.offset = (end_bit - start_bit) * fs_info->sectorsize; ret =btrfs_insert_empty_item (trans, root, path, &key,0 );if (ret) {
Signature · 1 line
2.47.2
PATCH 15/16 btrfs: add and use helper to determine if using bitmaps in free space tree
In reply to [email protected]From: Filipe Manana <[email protected]>
When adding and removing free space to the free space tree, we need to lookup the respective block group's free info item in the free space tree, check its flags for the BTRFS_FREE_SPACE_USING_BITMAPS bit and then release the path.
Move these steps into a helper function and use it in both sites. This will also help avoiding duplicate code in a subsequent change.
--- fs/btrfs/free-space-tree.c | 50 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-)
fs/btrfs/free-space-tree.c +26 -24
@@ -791,32 +791,40 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans,return update_free_space_extent_count (trans, block_group, path, new_extents); } +static int use_bitmaps (struct btrfs_block_group *bg,struct btrfs_path *path) +{ +struct btrfs_free_space_info *info; +u32 flags; + + info =btrfs_search_free_space_info (NULL , bg, path,0 ); +if (IS_ERR (info)) +return PTR_ERR (info); + flags =btrfs_free_space_flags (path->nodes[0 ], info); +btrfs_release_path (path); + +return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; +} +EXPORT_FOR_TESTS int __btrfs_remove_from_free_space_tree(struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group,struct btrfs_path *path,u64 start,u64 size) { -struct btrfs_free_space_info *info; -u32 flags;int ret; ret =__add_block_group_free_space (trans, block_group, path);if (ret)return ret; - info =btrfs_search_free_space_info (NULL , block_group, path,0 ); -if (IS_ERR (info)) -return PTR_ERR (info); - flags =btrfs_free_space_flags (path->nodes[0 ], info); -btrfs_release_path (path); + ret =use_bitmaps (block_group, path); +if (ret <0 ) +return ret; -if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) { +if (ret)return modify_free_space_bitmap (trans, block_group, path, start, size, true); - }else { -return remove_free_space_extent (trans, block_group, path, - start, size); - } + +return remove_free_space_extent (trans, block_group, path, start, size); } int btrfs_remove_from_free_space_tree(struct btrfs_trans_handle *trans,
@@ -984,27 +992,21 @@ int __btrfs_add_to_free_space_tree(struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group, struct btrfs_path *path, u64 start, u64 size) { - struct btrfs_free_space_info *info; -u32 flags; int ret; ret =__add_block_group_free_space (trans, block_group, path);if (ret)return ret; - info =btrfs_search_free_space_info (NULL , block_group, path,0 ); -if (IS_ERR (info)) -return PTR_ERR (info); - flags =btrfs_free_space_flags (path->nodes[0 ], info); -btrfs_release_path (path); + ret =use_bitmaps (block_group, path); +if (ret <0 ) +return ret; -if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) { +if (ret)return modify_free_space_bitmap (trans, block_group, path, start, size, false); - }else { -return add_free_space_extent (trans, block_group, path, start, - size); - } + +return add_free_space_extent (trans, block_group, path, start, size); } int btrfs_add_to_free_space_tree(struct btrfs_trans_handle *trans,
Signature · 1 line
2.47.2
Re: [PATCH 15/16] btrfs: add and use helper to determine if using bitmaps in free space tree
Quoted 20 lines · On Tue, Jun 17, 2025 at 05:13:10PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
When adding and removing free space to the free space tree, we need to lookup the respective block group's free info item in the free space tree, check its flags for the
BTRFS_FREE_SPACE_USING_BITMAPSbit and then release the path.Move these steps into a helper function and use it in both sites. This will also help avoiding duplicate code in a subsequent change.
Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/free-space-tree.c | 50 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 24 deletions(-)fs/btrfs/free-space-tree.c +0
-0
fs/btrfs/free-space-tree.c static int remove_free_space_extent(struct btrfs_trans_handle *trans, · line 794
Quoted 4 lines
fs/btrfs/free-space-tree.c +1
-0@@ -791,32 +791,40 @@ static int remove_free_space_extent(struct btrfs_trans_handle *trans,return update_free_space_extent_count (trans, block_group, path, new_extents); } +static int use_bitmaps (struct btrfs_block_group *bg,struct btrfs_path *path)
I think 'using_bitmaps' makes more sense, given the name of the flag. Ditto for the next patch and the caching variables.
Quoted 85 lines
fs/btrfs/free-space-tree.c +25
-24+{ +struct btrfs_free_space_info *info; +u32 flags; + + info =btrfs_search_free_space_info (NULL , bg, path,0 ); +if (IS_ERR (info)) +return PTR_ERR (info); + flags =btrfs_free_space_flags (path->nodes[0 ], info); +btrfs_release_path (path); + +return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; +} +EXPORT_FOR_TESTS int __btrfs_remove_from_free_space_tree(struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group,struct btrfs_path *path,u64 start,u64 size) { -struct btrfs_free_space_info *info; -u32 flags;int ret; ret =__add_block_group_free_space (trans, block_group, path);if (ret)return ret; - info =btrfs_search_free_space_info (NULL , block_group, path,0 ); -if (IS_ERR (info)) -return PTR_ERR (info); - flags =btrfs_free_space_flags (path->nodes[0 ], info); -btrfs_release_path (path); + ret =use_bitmaps (block_group, path); +if (ret <0 ) +return ret; -if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) { +if (ret)return modify_free_space_bitmap (trans, block_group, path, start, size, true); - }else { -return remove_free_space_extent (trans, block_group, path, - start, size); - } + +return remove_free_space_extent (trans, block_group, path, start, size); } int btrfs_remove_from_free_space_tree(struct btrfs_trans_handle *trans,@@ -984,27 +992,21 @@ int __btrfs_add_to_free_space_tree(struct btrfs_trans_handle *trans,struct btrfs_block_group *block_group, struct btrfs_path *path, u64 start, u64 size) { - struct btrfs_free_space_info *info; -u32 flags; int ret; ret =__add_block_group_free_space (trans, block_group, path);if (ret)return ret; - info =btrfs_search_free_space_info (NULL , block_group, path,0 ); -if (IS_ERR (info)) -return PTR_ERR (info); - flags =btrfs_free_space_flags (path->nodes[0 ], info); -btrfs_release_path (path); + ret =use_bitmaps (block_group, path); +if (ret <0 ) +return ret; -if (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) { +if (ret)return modify_free_space_bitmap (trans, block_group, path, start, size, false); - }else { -return add_free_space_extent (trans, block_group, path, start, - size); - } + +return add_free_space_extent (trans, block_group, path, start, size); } int btrfs_add_to_free_space_tree(struct btrfs_trans_handle *trans,Signature · 1 line
2.47.2
PATCH 16/16 btrfs: cache if we are using free space bitmaps for a block group
In reply to [email protected]From: Filipe Manana <[email protected]>
Every time we add free space to the free space tree or we remove free space from the free space tree, we do a lookup for the block group's free space info item in the free space tree. This takes time, navigating the btree and we may block either on IO when reading extent buffers from disk or on extent buffer lock contention due to concurrency.
Instead of doing this lookup everytime, cache the result in the block structure and use it after the first lookup. This adds two boolean members to the block group structure but doesn't increase the structure's size.
The following script that runs fs_mark was used to measure the time spent on run_delayed_tree_ref(), since down that call chain we have calls to add and remove free space to/from the free space tree (calls to btrfs_add_to_free_space_tree() and btrfs_remove_from_free_space_tree()):
$ cat test.sh #!/bin/bash
DEV=/dev/nullb0
MNT=/mnt
FILES=100000
THREADS=$(nproc --all)
echo "performance" | \
tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor umount $DEV &> /dev/null
mkfs.btrfs -f $DEV
mount -o ssd $DEV $MNT
OPTS="-S 0 -L 5 -n $FILES -s 0 -t $THREADS -k"
for ((i = 1; i <= $THREADS; i++)); do
OPTS="$OPTS -d $MNT/d$i"
done
fs_mark $OPTS
umount $MNT
This is a heavy metadata test as it's exercising only file creation, so a lot of allocations of metadata extents, creating delayed refs for adding new metadata extents and dropping existing ones due to COW. The results of the times it took to execute run_delayed_tree_ref(), in nanoseconds, are the following.
Before this change:
Range: 1868.000 - 6482857.000; Mean: 10231.430; Median: 7005.000; Stddev: 27993.173
Percentiles: 90th: 13342.000; 95th: 23279.000; 99th: 82448.000 1868.000 - 4222.038: 270696 ############
4222.038 - 9541.029: 1201327 ##################################################### 9541.029 - 21559.383: 385436 #################
21559.383 - 48715.063: 64942 ###
48715.063 - 110073.800: 31454 #
110073.800 - 248714.944: 8218 | 248714.944 - 561977.042: 1030 |
561977.042 - 1269798.254: 295 |
1269798.254 - 2869132.711: 116 |
2869132.711 - 6482857.000: 28 |
After this change:
Range: 1554.000 - 4557014.000; Mean: 9168.164; Median: 6391.000; Stddev: 21467.060
Percentiles: 90th: 12478.000; 95th: 20964.000; 99th: 72234.000 1554.000 - 3453.820: 219004 ############
3453.820 - 7674.743: 980645 ##################################################### 7674.743 - 17052.574: 552486 ############################## 17052.574 - 37887.762: 68558 ####
37887.762 - 84178.322: 31557 ##
84178.322 - 187024.331: 12102 #
187024.331 - 415522.355: 1364 |
415522.355 - 923187.626: 256 |
923187.626 - 2051092.468: 125 |
2051092.468 - 4557014.000: 21 |
--- fs/btrfs/block-group.h | 5 +++++ fs/btrfs/free-space-tree.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)
fs/btrfs/block-group.h +5 -0
@@ -246,6 +246,11 @@ struct btrfs_block_group {/* Lock for free space tree operations. */ struct mutex free_space_lock; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps_cached; +/* * Number of extents in this block group used for swap files. * All accesses protected by the spinlock 'lock'.
fs/btrfs/free-space-tree.c +11 -1
@@ -287,6 +287,8 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags |=BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = true; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);
@@ -434,6 +436,8 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = false; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);
@@ -796,13 +800,19 @@ static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path)struct btrfs_free_space_info *info;u32 flags; +if (bg->use_free_space_bitmaps_cached) +return bg->use_free_space_bitmaps; + info =btrfs_search_free_space_info (NULL , bg, path,0 );if (IS_ERR (info))return PTR_ERR (info); flags =btrfs_free_space_flags (path->nodes[0 ], info);btrfs_release_path (path); -return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; + bg->use_free_space_bitmaps = (flags &BTRFS_FREE_SPACE_USING_BITMAPS ); + bg->use_free_space_bitmaps_cached = true; + +return bg->use_free_space_bitmaps; }EXPORT_FOR_TESTS
Signature · 1 line
2.47.2
Re: [PATCH 16/16] btrfs: cache if we are using free space bitmaps for a block group
Quoted 122 lines · On Tue, Jun 17, 2025 at 05:13:11PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Every time we add free space to the free space tree or we remove free space from the free space tree, we do a lookup for the block group's free space info item in the free space tree. This takes time, navigating the btree and we may block either on IO when reading extent buffers from disk or on extent buffer lock contention due to concurrency.
Instead of doing this lookup everytime, cache the result in the block structure and use it after the first lookup. This adds two boolean members to the block group structure but doesn't increase the structure's size.
The following script that runs
fs_markwas used to measure the time spent onrun_delayed_tree_ref(), since down that call chain we have calls to add and remove free space to/from the free space tree (calls tobtrfs_add_to_free_space_tree()andbtrfs_remove_from_free_space_tree()):$ cat test.sh #!/bin/bashDEV=/dev/nullb0 MNT=/mnt FILES=100000 THREADS=$(nproc --all) echo "performance" | \ tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governorumount $DEV &>
/dev/null
mkfs.btrfs -f $DEV
mount -o ssd $DEV $MNTOPTS="-S 0 -L 5 -n $FILES -s 0 -t $THREADS -k"
for ((i = 1; i <= $THREADS; i++)); doOPTS="$OPTS -d $MNT/d$i"done
fs_mark$OPTSumount $MNT
This is a heavy metadata test as it's exercising only file creation, so a lot of allocations of metadata extents, creating delayed refs for adding new metadata extents and dropping existing ones due to COW. The results of the times it took to execute
run_delayed_tree_ref(), in nanoseconds, are the following.Before this change:
Range: 1868.000 - 6482857.000; Mean: 10231.430; Median: 7005.000; Stddev: 27993.173
Percentiles: 90th: 13342.000; 95th: 23279.000; 99th: 82448.000 1868.000 - 4222.038: 270696 ############
4222.038 - 9541.029: 1201327 ##################################################### 9541.029 - 21559.383: 385436 #################
21559.383 - 48715.063: 64942 ###
48715.063 - 110073.800: 31454 #110073.800 - 248714.944: 8218 | 248714.944 - 561977.042: 1030 |561977.042 - 1269798.254: 295 |
1269798.254 - 2869132.711: 116 |
2869132.711 - 6482857.000: 28 |After this change:
Range: 1554.000 - 4557014.000; Mean: 9168.164; Median: 6391.000; Stddev: 21467.060
Percentiles: 90th: 12478.000; 95th: 20964.000; 99th: 72234.000 1554.000 - 3453.820: 219004 ############
3453.820 - 7674.743: 980645 ##################################################### 7674.743 - 17052.574: 552486 ############################## 17052.574 - 37887.762: 68558 ####
37887.762 - 84178.322: 31557 ##
84178.322 - 187024.331: 12102 #
187024.331 - 415522.355: 1364 |
415522.355 - 923187.626: 256 |
923187.626 - 2051092.468: 125 |
2051092.468 - 4557014.000: 21 |Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/block-group.h | 5 +++++ fs/btrfs/free-space-tree.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)fs/btrfs/block-group.h +5
-0@@ -246,6 +246,11 @@ struct btrfs_block_group {/* Lock for free space tree operations. */ struct mutex free_space_lock; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps_cached; +/* * Number of extents in this block group used for swap files. * All accesses protected by the spinlock 'lock'. fs/btrfs/free-space-tree.c +4
-0@@ -287,6 +287,8 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags |=BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = true; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);@@ -434,6 +436,8 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = false; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);
fs/btrfs/free-space-tree.c static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path) · line 805
Quoted 6 lines
fs/btrfs/free-space-tree.c +3
-0@@ -796,13 +800,19 @@ static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path)struct btrfs_free_space_info *info;u32 flags; +if (bg->use_free_space_bitmaps_cached) +return bg->use_free_space_bitmaps; +
I'm a little worried about what happens if the reader observes the writes out of order.
i.e., say T1 is calling btrfs_convert_free_space_to_bitmaps() and T2 is calling use_bitmaps(). Then if T2 observes use_free_space_bitmaps_cached set to true but not use_free_space_bitmaps set to true, it will get the wrong value.
Or is there some higher level locking that I missed protecting us?
Thanks,
Boris
Quoted 17 lines
fs/btrfs/free-space-tree.c +4
-1info =btrfs_search_free_space_info (NULL , bg, path,0 );if (IS_ERR (info))return PTR_ERR (info); flags =btrfs_free_space_flags (path->nodes[0 ], info);btrfs_release_path (path); -return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; + bg->use_free_space_bitmaps = (flags &BTRFS_FREE_SPACE_USING_BITMAPS ); + bg->use_free_space_bitmaps_cached = true; + +return bg->use_free_space_bitmaps; }EXPORT_FOR_TESTS Signature · 1 line
2.47.2
Re: [PATCH 16/16] btrfs: cache if we are using free space bitmaps for a block group
Quoted 134 lines · Boris Burkov
Quoted 122 lines · On Tue, Jun 17, 2025 at 05:13:11PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Every time we add free space to the free space tree or we remove free space from the free space tree, we do a lookup for the block group's free space info item in the free space tree. This takes time, navigating the btree and we may block either on IO when reading extent buffers from disk or on extent buffer lock contention due to concurrency.
Instead of doing this lookup everytime, cache the result in the block structure and use it after the first lookup. This adds two boolean members to the block group structure but doesn't increase the structure's size.
The following script that runs
fs_markwas used to measure the time spent onrun_delayed_tree_ref(), since down that call chain we have calls to add and remove free space to/from the free space tree (calls tobtrfs_add_to_free_space_tree()andbtrfs_remove_from_free_space_tree()):$ cat test.sh #!/bin/bashDEV=/dev/nullb0 MNT=/mnt FILES=100000 THREADS=$(nproc --all) echo "performance" | \ tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governorumount $DEV &>
/dev/null
mkfs.btrfs -f $DEV
mount -o ssd $DEV $MNTOPTS="-S 0 -L 5 -n $FILES -s 0 -t $THREADS -k"
for ((i = 1; i <= $THREADS; i++)); doOPTS="$OPTS -d $MNT/d$i"done
fs_mark$OPTSumount $MNT
This is a heavy metadata test as it's exercising only file creation, so a lot of allocations of metadata extents, creating delayed refs for adding new metadata extents and dropping existing ones due to COW. The results of the times it took to execute
run_delayed_tree_ref(), in nanoseconds, are the following.Before this change:
Range: 1868.000 - 6482857.000; Mean: 10231.430; Median: 7005.000; Stddev: 27993.173
Percentiles: 90th: 13342.000; 95th: 23279.000; 99th: 82448.000 1868.000 - 4222.038: 270696 ############
4222.038 - 9541.029: 1201327 ##################################################### 9541.029 - 21559.383: 385436 #################
21559.383 - 48715.063: 64942 ###
48715.063 - 110073.800: 31454 #110073.800 - 248714.944: 8218 | 248714.944 - 561977.042: 1030 |561977.042 - 1269798.254: 295 |
1269798.254 - 2869132.711: 116 |
2869132.711 - 6482857.000: 28 |After this change:
Range: 1554.000 - 4557014.000; Mean: 9168.164; Median: 6391.000; Stddev: 21467.060
Percentiles: 90th: 12478.000; 95th: 20964.000; 99th: 72234.000 1554.000 - 3453.820: 219004 ############
3453.820 - 7674.743: 980645 ##################################################### 7674.743 - 17052.574: 552486 ############################## 17052.574 - 37887.762: 68558 ####
37887.762 - 84178.322: 31557 ##
84178.322 - 187024.331: 12102 #
187024.331 - 415522.355: 1364 |
415522.355 - 923187.626: 256 |
923187.626 - 2051092.468: 125 |
2051092.468 - 4557014.000: 21 |Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/block-group.h | 5 +++++ fs/btrfs/free-space-tree.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)fs/btrfs/block-group.h +5
-0@@ -246,6 +246,11 @@ struct btrfs_block_group {/* Lock for free space tree operations. */ struct mutex free_space_lock; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps_cached; +/* * Number of extents in this block group used for swap files. * All accesses protected by the spinlock 'lock'. fs/btrfs/free-space-tree.c +4
-0@@ -287,6 +287,8 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags |=BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = true; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);@@ -434,6 +436,8 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = false; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);fs/btrfs/free-space-tree.c static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path) · line 805
Quoted 6 lines
fs/btrfs/free-space-tree.c +3
-0@@ -796,13 +800,19 @@ static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path)struct btrfs_free_space_info *info;u32 flags; +if (bg->use_free_space_bitmaps_cached) +return bg->use_free_space_bitmaps; +I'm a little worried about what happens if the reader observes the writes out of order.
Quoted 6 lines
i.e., say T1 is calling
btrfs_convert_free_space_to_bitmaps()and T2 is callinguse_bitmaps(). Then if T2 observesuse_free_space_bitmaps_cachedset to true but notuse_free_space_bitmapsset to true, it will get the wrong value.Or is there some higher level locking that I missed protecting us?
Yes, there is. It's the block group's free_space_lock mutex, taken at any entry point that modifies the free space tree.
Thanks.
Quoted 21 lines
Thanks,
BorisQuoted 17 lines
fs/btrfs/free-space-tree.c +4
-2info =btrfs_search_free_space_info (NULL , bg, path,0 );if (IS_ERR (info))return PTR_ERR (info); flags =btrfs_free_space_flags (path->nodes[0 ], info);btrfs_release_path (path); -return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; + bg->use_free_space_bitmaps = (flags &BTRFS_FREE_SPACE_USING_BITMAPS ); + bg->use_free_space_bitmaps_cached = true; + +return bg->use_free_space_bitmaps; }EXPORT_FOR_TESTS --2.47.2
Boris Burkov
Reviewed-byRe: [PATCH 16/16] btrfs: cache if we are using free space bitmaps for a block group
Quoted 140 lines · Filipe Manana
Quoted 139 lines · Boris Burkov
Quoted 122 lines · On Tue, Jun 17, 2025 at 05:13:11PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
Every time we add free space to the free space tree or we remove free space from the free space tree, we do a lookup for the block group's free space info item in the free space tree. This takes time, navigating the btree and we may block either on IO when reading extent buffers from disk or on extent buffer lock contention due to concurrency.
Instead of doing this lookup everytime, cache the result in the block structure and use it after the first lookup. This adds two boolean members to the block group structure but doesn't increase the structure's size.
The following script that runs
fs_markwas used to measure the time spent onrun_delayed_tree_ref(), since down that call chain we have calls to add and remove free space to/from the free space tree (calls tobtrfs_add_to_free_space_tree()andbtrfs_remove_from_free_space_tree()):$ cat test.sh #!/bin/bashDEV=/dev/nullb0 MNT=/mnt FILES=100000 THREADS=$(nproc --all) echo "performance" | \ tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governorumount $DEV &>
/dev/null
mkfs.btrfs -f $DEV
mount -o ssd $DEV $MNTOPTS="-S 0 -L 5 -n $FILES -s 0 -t $THREADS -k"
for ((i = 1; i <= $THREADS; i++)); doOPTS="$OPTS -d $MNT/d$i"done
fs_mark$OPTSumount $MNT
This is a heavy metadata test as it's exercising only file creation, so a lot of allocations of metadata extents, creating delayed refs for adding new metadata extents and dropping existing ones due to COW. The results of the times it took to execute
run_delayed_tree_ref(), in nanoseconds, are the following.Before this change:
Range: 1868.000 - 6482857.000; Mean: 10231.430; Median: 7005.000; Stddev: 27993.173
Percentiles: 90th: 13342.000; 95th: 23279.000; 99th: 82448.000 1868.000 - 4222.038: 270696 ############
4222.038 - 9541.029: 1201327 ##################################################### 9541.029 - 21559.383: 385436 #################
21559.383 - 48715.063: 64942 ###
48715.063 - 110073.800: 31454 #110073.800 - 248714.944: 8218 | 248714.944 - 561977.042: 1030 |561977.042 - 1269798.254: 295 |
1269798.254 - 2869132.711: 116 |
2869132.711 - 6482857.000: 28 |After this change:
Range: 1554.000 - 4557014.000; Mean: 9168.164; Median: 6391.000; Stddev: 21467.060
Percentiles: 90th: 12478.000; 95th: 20964.000; 99th: 72234.000 1554.000 - 3453.820: 219004 ############
3453.820 - 7674.743: 980645 ##################################################### 7674.743 - 17052.574: 552486 ############################## 17052.574 - 37887.762: 68558 ####
37887.762 - 84178.322: 31557 ##
84178.322 - 187024.331: 12102 #
187024.331 - 415522.355: 1364 |
415522.355 - 923187.626: 256 |
923187.626 - 2051092.468: 125 |
2051092.468 - 4557014.000: 21 |Signed-off-by Filipe Manana <[email protected]>--- fs/btrfs/block-group.h | 5 +++++ fs/btrfs/free-space-tree.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-)fs/btrfs/block-group.h +5
-0@@ -246,6 +246,11 @@ struct btrfs_block_group {/* Lock for free space tree operations. */ struct mutex free_space_lock; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps; +/* Protected by @free_space_lock. */ +bool use_free_space_bitmaps_cached; +/* * Number of extents in this block group used for swap files. * All accesses protected by the spinlock 'lock'. fs/btrfs/free-space-tree.c +4
-0@@ -287,6 +287,8 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags |=BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = true; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);@@ -434,6 +436,8 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans, leaf = path->nodes[0 ]; flags =btrfs_free_space_flags (leaf, info); flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS ; + block_group->use_free_space_bitmaps = false; + block_group->use_free_space_bitmaps_cached = true;btrfs_set_free_space_flags (leaf, info, flags); expected_extent_count =btrfs_free_space_extent_count (leaf, info);btrfs_release_path (path);fs/btrfs/free-space-tree.c static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path) · line 805
Quoted 6 lines
fs/btrfs/free-space-tree.c +3
-0@@ -796,13 +800,19 @@ static int use_bitmaps(struct btrfs_block_group *bg, struct btrfs_path *path)struct btrfs_free_space_info *info;u32 flags; +if (bg->use_free_space_bitmaps_cached) +return bg->use_free_space_bitmaps; +I'm a little worried about what happens if the reader observes the writes out of order.
i.e., say T1 is calling
btrfs_convert_free_space_to_bitmaps()and T2 is callinguse_bitmaps(). Then if T2 observesuse_free_space_bitmaps_cachedset to true but notuse_free_space_bitmapsset to true, it will get the wrong value.
Quoted 7 lines
Quoted 1 line
Or is there some higher level locking that I missed protecting us?
Yes, there is. It's the block group's
free_space_lockmutex, taken at any entry point that modifies the free space tree.Thanks.
Ah, yup, looks good, thanks.
You can add
to the whole series.
Quoted 21 lines
Quoted 21 lines
Thanks,
BorisQuoted 17 lines
fs/btrfs/free-space-tree.c +4
-2info =btrfs_search_free_space_info (NULL , bg, path,0 );if (IS_ERR (info))return PTR_ERR (info); flags =btrfs_free_space_flags (path->nodes[0 ], info);btrfs_release_path (path); -return (flags &BTRFS_FREE_SPACE_USING_BITMAPS ) ?1 :0 ; + bg->use_free_space_bitmaps = (flags &BTRFS_FREE_SPACE_USING_BITMAPS ); + bg->use_free_space_bitmaps_cached = true; + +return bg->use_free_space_bitmaps; }EXPORT_FOR_TESTS --2.47.2
Quoted 14 lines · On Tue, Jun 17, 2025 at 05:12:55PM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
A free space tree optimization (last patch in the series), and the rest mostly cleanups and preparatory work. Details in the change logs.
Filipe Manana (16):
btrfs: remove pointless out label fromadd_new_free_space_info()
btrfs: remove pointless out label fromupdate_free_space_extent_count()
btrfs: makeextent_buffer_test_bit()return a boolean instead
btrfs: makefree_space_test_bit()return a boolean instead
btrfs: remove pointless out label frommodify_free_space_bitmap()
btrfs: remove pointless out label fromremove_free_space_extent()
btrfs: remove pointless out label fromadd_free_space_extent()
btrfs: remove pointless out label fromload_free_space_bitmaps()
Quoted 8 lines
btrfs: remove pointless out label from
load_free_space_extents()
btrfs: add btrfs prefix to free space tree exported functions
btrfs: renamefree_space_set_bits()and make it less confusing
btrfs: turn remove argument ofmodify_free_space_bitmap()to boolean
btrfs: avoid double slot decrement atbtrfs_convert_free_space_to_extents()
btrfs: usefs_infofrom local variable inbtrfs_convert_free_space_to_extents()
btrfs: add and use helper to determine if using bitmaps in free space tree
btrfs: cache if we are using free space bitmaps for a block group