Misc freespace cache cleanups
cover Misc freespace cache cleanups
Here's an assortment of freespace cache cleanups. 2 patches to consolidated some functions and 2 other to simplify/clarify arguments of called functions. This all results in a nice reduction of lines of code as well as code size:
$./scripts/bloat-o-meter fs/btrfs/free-space-cache.o free-space-cache.patched add/remove: 0/0 grow/shrink: 0/8 up/down: 0/-549 (-549)
Function old new delta
__btrfs_add_free_space 1124 1123 -1 btrfs_find_space_for_alloc 1042 1039 -3 btrfs_remove_free_space 648 644 -4 do_trimming 550 530 -20 btrfs_add_free_space 75 55 -20 btrfs_add_free_space_async_trimmed 79 57 -22 try_merge_free_space 602 505 -97
steal_from_bitmap 1522 1140 -382
Total: Before=29299, After=28750, chg -1.87%
Nikolay Borisov (4):
btrfs: consolidate bitmap_clear_bits/__bitmap_clear_bits
btrfs: consolidate unlink_free_space/__unlink_free_space functions
btrfs: make __btrfs_add_free_space take just block group reference
btrfs: change name and type of private member of btrfs_free_space_ctl
fs/btrfs/free-space-cache.c | 115 ++++++++++++++---------------------- fs/btrfs/free-space-cache.h | 8 +-- 2 files changed, 48 insertions(+), 75 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/4 btrfs: consolidate bitmap_clear_bits/__bitmap_clear_bits
The only difference is the former adjusts btrfs_free_space::bytes member. Consolidate the two function into 1 and add a bool parameter which controls whether the adjustment is made or not. No functional changes.
--- fs/btrfs/free-space-cache.c | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-)
fs/btrfs/free-space-cache.c +13 -24
@@ -45,7 +45,7 @@ static void free_bitmap(struct btrfs_free_space_ctl *ctl, struct btrfs_free_space *bitmap_info);static void bitmap_clear_bits (struct btrfs_free_space_ctl *ctl,struct btrfs_free_space *info,u64 offset, -u64 bytes); +u64 bytes,bool update_stats); static struct inode *__lookup_free_space_inode(struct btrfs_root *root, struct btrfs_path *path,
@@ -886,7 +886,7 @@ static int copy_free_space_cache(struct btrfs_block_group *block_group, bytes);if (ret)break ; -bitmap_clear_bits (ctl, info, offset, bytes); +bitmap_clear_bits (ctl, info, offset, bytes, true); offset = info->offset; bytes = ctl->unit; }
@@ -1740,9 +1740,9 @@ static int link_free_space(struct btrfs_free_space_ctl *ctl,return ret; } -static inline void __bitmap_clear_bits (struct btrfs_free_space_ctl *ctl, -struct btrfs_free_space *info, -u64 offset,u64 bytes) +static inline void bitmap_clear_bits (struct btrfs_free_space_ctl *ctl, +struct btrfs_free_space *info, +u64 offset,u64 bytes,bool update_stat) {unsigned long start, count, end;int extent_delta =-1 ;
@@ -1769,14 +1769,9 @@ static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, ctl->discardable_extents[BTRFS_STAT_CURR ] += extent_delta; ctl->discardable_bytes[BTRFS_STAT_CURR ] -= bytes; } -} -static void bitmap_clear_bits (struct btrfs_free_space_ctl *ctl, -struct btrfs_free_space *info,u64 offset, -u64 bytes) -{ -__bitmap_clear_bits (ctl, info, offset, bytes); - ctl->free_space -= bytes; +if (update_stat) + ctl->free_space -= bytes; } static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
@@ -2007,7 +2002,7 @@ static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,/* Cannot clear past the end of the bitmap */ search_bytes =min (search_bytes, end - search_start +1 ); -bitmap_clear_bits (ctl, bitmap_info, search_start, search_bytes); +bitmap_clear_bits (ctl, bitmap_info, search_start, search_bytes, true); *offset += search_bytes; *bytes -= search_bytes;
@@ -2358,10 +2353,7 @@ static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,if (!btrfs_free_space_trimmed (bitmap)) info->trim_state =BTRFS_TRIM_STATE_UNTRIMMED ; -if (update_stat) -bitmap_clear_bits (ctl, bitmap, end, bytes); -else -__bitmap_clear_bits (ctl, bitmap, end, bytes); +bitmap_clear_bits (ctl, bitmap, end, bytes, update_stat);if (!bitmap->bytes)free_bitmap (ctl, bitmap);
@@ -2415,10 +2407,7 @@ static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,if (!btrfs_free_space_trimmed (bitmap)) info->trim_state =BTRFS_TRIM_STATE_UNTRIMMED ; -if (update_stat) -bitmap_clear_bits (ctl, bitmap, info->offset, bytes); -else -__bitmap_clear_bits (ctl, bitmap, info->offset, bytes); +bitmap_clear_bits (ctl, bitmap, info->offset, bytes, update_stat);if (!bitmap->bytes)free_bitmap (ctl, bitmap);
@@ -2972,7 +2961,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group, ret = offset;if (entry->bitmap) { -bitmap_clear_bits (ctl, entry, offset, bytes); +bitmap_clear_bits (ctl, entry, offset, bytes, true);if (!btrfs_free_space_trimmed (entry))atomic64_add (bytes, &discard_ctl->discard_bytes_saved);
@@ -3074,7 +3063,7 @@ static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group, } ret = search_start; -__bitmap_clear_bits (ctl, entry, ret, bytes); +bitmap_clear_bits (ctl, entry, ret, bytes, false);return ret; }
@@ -3824,7 +3813,7 @@ static int trim_bitmaps(struct btrfs_block_group *block_group, bytes > (max_discard_size + minlen)) bytes = max_discard_size; -bitmap_clear_bits (ctl, entry, start, bytes); +bitmap_clear_bits (ctl, entry, start, bytes, true);if (entry->bytes ==0 )free_bitmap (ctl, entry);
Signature · 1 line
2.17.1
PATCH 2/4 btrfs: consolidate unlink_free_space/__unlink_free_space functions
In reply to Nikolay BorisovThe only difference between the two is whether btrfs_free_space::bytes is adjusted. Instead of having 2 separate functions control this behavior via an additional parameter and make them one function instead. No functional changes.
--- fs/btrfs/free-space-cache.c | 41 +++++++++++++++---------------------- 1 file changed, 16 insertions(+), 25 deletions(-)
fs/btrfs/free-space-cache.c +16 -25
@@ -37,7 +37,7 @@ struct btrfs_trim_range {static int link_free_space (struct btrfs_free_space_ctl *ctl,struct btrfs_free_space *info);static void unlink_free_space (struct btrfs_free_space_ctl *ctl, -struct btrfs_free_space *info); +struct btrfs_free_space *info,bool update_stat);static int search_bitmap (struct btrfs_free_space_ctl *ctl,struct btrfs_free_space *bitmap_info,u64 *offset,u64 *bytes,bool for_alloc);
@@ -872,7 +872,7 @@ static int copy_free_space_cache(struct btrfs_block_group *block_group,while (!ret && (n =rb_first (&ctl->free_space_offset)) !=NULL ) { info = rb_entry(n, struct btrfs_free_space, offset_index);if (!info->bitmap) { -unlink_free_space (ctl, info); +unlink_free_space (ctl, info, true); ret =btrfs_add_free_space (block_group, info->offset, info->bytes);kmem_cache_free (btrfs_free_space_cachep, info);
@@ -1699,9 +1699,9 @@ tree_search_offset(struct btrfs_free_space_ctl *ctl,return entry; } -static inline void -__unlink_free_space (struct btrfs_free_space_ctl *ctl, -struct btrfs_free_space *info) +static inline void unlink_free_space (struct btrfs_free_space_ctl *ctl, +struct btrfs_free_space *info, +bool update_stat) {rb_erase (&info->offset_index, &ctl->free_space_offset); ctl->free_extents--;
@@ -1710,13 +1710,10 @@ __unlink_free_space(struct btrfs_free_space_ctl *ctl, ctl->discardable_extents[BTRFS_STAT_CURR ]--; ctl->discardable_bytes[BTRFS_STAT_CURR ] -= info->bytes; } -} -static void unlink_free_space (struct btrfs_free_space_ctl *ctl, -struct btrfs_free_space *info) -{ -__unlink_free_space (ctl, info); - ctl->free_space -= info->bytes; +if (update_stat) + ctl->free_space -= info->bytes; + } static int link_free_space(struct btrfs_free_space_ctl *ctl,
@@ -1964,7 +1961,7 @@ static void free_bitmap(struct btrfs_free_space_ctl *ctl, ctl->discardable_bytes[BTRFS_STAT_CURR ] -= bitmap_info->bytes; } -unlink_free_space (ctl, bitmap_info); +unlink_free_space (ctl, bitmap_info, true);kmem_cache_free (btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);kmem_cache_free (btrfs_free_space_cachep, bitmap_info); ctl->total_bitmaps--;
@@ -2301,10 +2298,7 @@ static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,/* See try_merge_free_space() comment. */ if (right_info && !right_info->bitmap && (!is_trimmed ||btrfs_free_space_trimmed (right_info))) { -if (update_stat) -unlink_free_space (ctl, right_info); -else -__unlink_free_space (ctl, right_info); +unlink_free_space (ctl, right_info, update_stat); info->bytes += right_info->bytes;kmem_cache_free (btrfs_free_space_cachep, right_info); merged = true;
@@ -2314,10 +2308,7 @@ static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,if (left_info && !left_info->bitmap && left_info->offset + left_info->bytes == offset && (!is_trimmed ||btrfs_free_space_trimmed (left_info))) { -if (update_stat) -unlink_free_space (ctl, left_info); -else -__unlink_free_space (ctl, left_info); +unlink_free_space (ctl, left_info, update_stat); info->offset = left_info->offset; info->bytes += left_info->bytes;kmem_cache_free (btrfs_free_space_cachep, left_info);
@@ -2681,7 +2672,7 @@ int btrfs_remove_free_space(struct btrfs_block_group *block_group, re_search = false;if (!info->bitmap) { -unlink_free_space (ctl, info); +unlink_free_space (ctl, info, true);if (offset == info->offset) {u64 to_free =min (bytes, info->bytes);
@@ -2864,7 +2855,7 @@ static void __btrfs_remove_free_space_cache_locked(while ((node =rb_last (&ctl->free_space_offset)) !=NULL ) { info = rb_entry(node, struct btrfs_free_space, offset_index);if (!info->bitmap) { -unlink_free_space (ctl, info); +unlink_free_space (ctl, info, true);kmem_cache_free (btrfs_free_space_cachep, info); }else {free_bitmap (ctl, info);
@@ -2969,7 +2960,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,if (!entry->bytes)free_bitmap (ctl, entry); }else { -unlink_free_space (ctl, entry); +unlink_free_space (ctl, entry, true); align_gap_len = offset - entry->offset; align_gap = entry->offset; align_gap_trim_state = entry->trim_state;
@@ -3600,7 +3591,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,mutex_unlock (&ctl->cache_writeout_mutex); goto next; } -unlink_free_space (ctl, entry); +unlink_free_space (ctl, entry, true);/* * Let bytes = BTRFS_MAX_DISCARD_SIZE + X. * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
@@ -3626,7 +3617,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group, goto next; } -unlink_free_space (ctl, entry); +unlink_free_space (ctl, entry, true);kmem_cache_free (btrfs_free_space_cachep, entry); }
Signature · 1 line
2.17.1
PATCH 3/4 btrfs: make __btrfs_add_free_space take just block group reference
In reply to Nikolay BorisovThere is no point in the function taking an fs_info and a btrfs_free_space because the ctl passed always belongs to the block group. Furthermore fs_info can be referenced from the block group. No functional changes.
--- fs/btrfs/free-space-cache.c | 25 ++++++++++--------------- fs/btrfs/free-space-cache.h | 6 ++---- 2 files changed, 12 insertions(+), 19 deletions(-)
fs/btrfs/free-space-cache.c +10 -15
@@ -2442,12 +2442,12 @@ static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, } } -int __btrfs_add_free_space (struct btrfs_fs_info *fs_info, -struct btrfs_free_space_ctl *ctl, +int __btrfs_add_free_space (struct btrfs_block_group *block_group,u64 offset,u64 bytes,enum btrfs_trim_state trim_state) { -struct btrfs_block_group *block_group = ctl->private; +struct btrfs_fs_info *fs_info = block_group->fs_info; +struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;struct btrfs_free_space *info;int ret =0 ;u64 filter_bytes = bytes;
@@ -2578,9 +2578,7 @@ int btrfs_add_free_space(struct btrfs_block_group *block_group,if (btrfs_test_opt (block_group->fs_info,DISCARD_SYNC )) trim_state =BTRFS_TRIM_STATE_TRIMMED ; -return __btrfs_add_free_space (block_group->fs_info, - block_group->free_space_ctl, - bytenr, size, trim_state); +return __btrfs_add_free_space (block_group, bytenr, size, trim_state); } int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
@@ -2611,9 +2609,7 @@ int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group, btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC)) trim_state =BTRFS_TRIM_STATE_TRIMMED ; -return __btrfs_add_free_space (block_group->fs_info, - block_group->free_space_ctl, - bytenr, size, trim_state); +return __btrfs_add_free_space (block_group, bytenr, size, trim_state); } int btrfs_remove_free_space(struct btrfs_block_group *block_group,
@@ -2708,7 +2704,7 @@ int btrfs_remove_free_space(struct btrfs_block_group *block_group, }spin_unlock (&ctl->tree_lock); - ret =__btrfs_add_free_space (block_group->fs_info, ctl, + ret =__btrfs_add_free_space (block_group, offset + bytes, old_end - (offset + bytes), info->trim_state);
@@ -2982,8 +2978,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,spin_unlock (&ctl->tree_lock);if (align_gap_len) -__btrfs_add_free_space (block_group->fs_info, ctl, - align_gap, align_gap_len, +__btrfs_add_free_space (block_group, align_gap, align_gap_len, align_gap_trim_state);return ret; }
@@ -3511,13 +3506,13 @@ static int do_trimming(struct btrfs_block_group *block_group,mutex_lock (&ctl->cache_writeout_mutex);if (reserved_start < start) -__btrfs_add_free_space (fs_info, ctl, reserved_start, +__btrfs_add_free_space (block_group, reserved_start, start - reserved_start, reserved_trim_state);if (start + bytes < reserved_start + reserved_bytes) -__btrfs_add_free_space (fs_info, ctl, end, reserved_end - end, +__btrfs_add_free_space (block_group, end, reserved_end - end, reserved_trim_state); -__btrfs_add_free_space (fs_info, ctl, start, bytes, trim_state); +__btrfs_add_free_space (block_group, start, bytes, trim_state);list_del (&trim_entry->list);mutex_unlock (&ctl->cache_writeout_mutex);
fs/btrfs/free-space-cache.h +2 -4
@@ -101,10 +101,8 @@ int btrfs_write_out_cache(struct btrfs_trans_handle *trans,void btrfs_init_free_space_ctl (struct btrfs_block_group *block_group,struct btrfs_free_space_ctl *ctl); -int __btrfs_add_free_space (struct btrfs_fs_info *fs_info, -struct btrfs_free_space_ctl *ctl, -u64 bytenr,u64 size, -enum btrfs_trim_state trim_state); +int __btrfs_add_free_space (struct btrfs_block_group *block_group,u64 bytenr, +u64 size,enum btrfs_trim_state trim_state);int btrfs_add_free_space (struct btrfs_block_group *block_group,u64 bytenr,u64 size); int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
Signature · 1 line
2.17.1
PATCH 4/4 btrfs: change name and type of private member of btrfs_free_space_ctl
In reply to Nikolay Borisovbtrfs_free_space_ctl::private is either unset or it always points to struct btrfs_block_group when it is set. So there's no point in keeping the unhelpful 'private' name and keeping it an untyped pointer. Change both the type and name to be self-describing. No functional changes.
--- fs/btrfs/free-space-cache.c | 12 ++++++------ fs/btrfs/free-space-cache.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/free-space-cache.c +6 -6
@@ -666,7 +666,7 @@ static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,static void recalculate_thresholds (struct btrfs_free_space_ctl *ctl) { -struct btrfs_block_group *block_group = ctl->private; +struct btrfs_block_group *block_group = ctl->block_group;u64 max_bytes;u64 bitmap_bytes;u64 extent_bytes;
@@ -2084,7 +2084,7 @@ static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,static bool use_bitmap (struct btrfs_free_space_ctl *ctl,struct btrfs_free_space *info) { -struct btrfs_block_group *block_group = ctl->private; +struct btrfs_block_group *block_group = ctl->block_group;struct btrfs_fs_info *fs_info = block_group->fs_info;bool forced = false;
@@ -2153,7 +2153,7 @@ static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,return 0 ;if (ctl->op == &free_space_op) - block_group = ctl->private; + block_group = ctl->block_group; again:/* * Since we link bitmaps right into the cluster we need to see if we
@@ -2769,7 +2769,7 @@ void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,spin_lock_init (&ctl->tree_lock); ctl->unit = fs_info->sectorsize; ctl->start = block_group->start; - ctl->private = block_group; + ctl->block_group = block_group; ctl->op = &free_space_op;INIT_LIST_HEAD (&ctl->trimming_ranges);mutex_init (&ctl->cache_writeout_mutex);
@@ -2865,8 +2865,8 @@ void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) {spin_lock (&ctl->tree_lock);__btrfs_remove_free_space_cache_locked (ctl); -if (ctl->private) -btrfs_discard_update_discardable (ctl->private); +if (ctl->block_group) +btrfs_discard_update_discardable (ctl->block_group);spin_unlock (&ctl->tree_lock); }
fs/btrfs/free-space-cache.h +1 -1
@@ -54,7 +54,7 @@ struct btrfs_free_space_ctl {s32 discardable_extents[BTRFS_STAT_NR_ENTRIES ];s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES ];const struct btrfs_free_space_op *op; -void *private; +struct btrfs_block_group *block_group;struct mutex cache_writeout_mutex;struct list_head trimming_ranges; };
Signature · 1 line
2.17.1
Quoted 18 lines · Nikolay Borisov
Here's an assortment of freespace cache cleanups. 2 patches to consolidated some functions and 2 other to simplify/clarify arguments of called functions. This all results in a nice reduction of lines of code as well as code size:
$./scripts/bloat-o-meter
fs/btrfs/free-space-cache.ofree-space-cache.patched add/remove: 0/0 grow/shrink: 0/8 up/down: 0/-549 (-549)
Function old new delta__btrfs_add_free_space 1124 1123 -1 btrfs_find_space_for_alloc 1042 1039 -3 btrfs_remove_free_space 648 644 -4 do_trimming 550 530 -20 btrfs_add_free_space 75 55 -20 btrfs_add_free_space_async_trimmed 79 57 -22 try_merge_free_space 602 505 -97
steal_from_bitmap1522 1140 -382
Total: Before=29299, After=28750, chg -1.87%
Quoted 5 lines
Nikolay Borisov (4):
btrfs: consolidate bitmap_clear_bits/__bitmap_clear_bits
btrfs: consolidate unlink_free_space/__unlink_free_space functions
btrfs: make__btrfs_add_free_spacetake just block group reference
btrfs: change name and type of private member ofbtrfs_free_space_ctl
Added to misc-next, thanks.