]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: add and use helper to compute the available space for a block group
authorFilipe Manana <fdmanana@suse.com>
Thu, 15 Jan 2026 21:17:35 +0000 (21:17 +0000)
committerDavid Sterba <dsterba@suse.com>
Tue, 3 Feb 2026 06:51:44 +0000 (07:51 +0100)
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 <boris@bur.io>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/space-info.c

index a1119f06b6d10680f9562c027cff1d645ab62d6c..9d64cc60a42b5fdf33dba065ac8f6bfd65b56ba3 100644 (file)
@@ -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);
index 5f933455118c79f6d3173f8a1959af3ef15ad9db..cd2d53d5b3157c7f490c61af705a9fc29cdbb2d9 100644 (file)
@@ -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
index 857e4fd2c77e0cdb71706b953a1b7a4f45d49ef6..1d76242f5e0df1335201e3e82116c392be9db07d 100644 (file)
@@ -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,