From: Filipe Manana Date: Thu, 15 Jan 2026 21:17:35 +0000 (+0000) Subject: btrfs: add and use helper to compute the available space for a block group X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c208aa0ef655bb9a3c379510802cadd0df512f0a;p=thirdparty%2Fkernel%2Flinux.git btrfs: add and use helper to compute the available space for a block group We have currently three places that compute how much available space a block group has. Add a helper function for this and use it in those places. Reviewed-by: Boris Burkov Reviewed-by: Johannes Thumshirn Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba --- diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index a1119f06b6d10..9d64cc60a42b5 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1376,8 +1376,7 @@ static int inc_block_group_ro(struct btrfs_block_group *cache, bool force) goto out; } - num_bytes = cache->length - cache->reserved - cache->pinned - - cache->bytes_super - cache->zone_unusable - cache->used; + num_bytes = btrfs_block_group_available_space(cache); /* * Data never overcommits, even in mixed mode, so do just the straight @@ -3089,7 +3088,6 @@ unlock_out: void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) { struct btrfs_space_info *sinfo = cache->space_info; - u64 num_bytes; BUG_ON(!cache->ro); @@ -3105,10 +3103,7 @@ void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); sinfo->bytes_readonly -= cache->zone_unusable; } - num_bytes = cache->length - cache->reserved - - cache->pinned - cache->bytes_super - - cache->zone_unusable - cache->used; - sinfo->bytes_readonly -= num_bytes; + sinfo->bytes_readonly -= btrfs_block_group_available_space(cache); list_del_init(&cache->ro_list); } spin_unlock(&cache->lock); diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h index 5f933455118c7..cd2d53d5b3157 100644 --- a/fs/btrfs/block-group.h +++ b/fs/btrfs/block-group.h @@ -295,6 +295,14 @@ static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group !(block_group->flags & BTRFS_BLOCK_GROUP_METADATA); } +static inline u64 btrfs_block_group_available_space(const struct btrfs_block_group *bg) +{ + lockdep_assert_held(&bg->lock); + + return (bg->length - bg->used - bg->pinned - bg->reserved - + bg->bytes_super - bg->zone_unusable); +} + #ifdef CONFIG_BTRFS_DEBUG int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group); #endif diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c index 857e4fd2c77e0..1d76242f5e0df 100644 --- a/fs/btrfs/space-info.c +++ b/fs/btrfs/space-info.c @@ -656,8 +656,7 @@ again: u64 avail; spin_lock(&cache->lock); - avail = cache->length - cache->used - cache->pinned - - cache->reserved - cache->bytes_super - cache->zone_unusable; + avail = btrfs_block_group_available_space(cache); btrfs_info(fs_info, "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %llu delalloc %llu super %llu zone_unusable (%llu bytes available) %s", cache->start, cache->length, cache->used, cache->pinned,