Commit c208aa0e authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba
Browse files

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: default avatarBoris Burkov <boris@bur.io>
Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent b322fa5f
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -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 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
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);
+8 −0
Original line number Diff line number Diff line
@@ -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
+1 −2
Original line number Diff line number Diff line
@@ -656,8 +656,7 @@ void btrfs_dump_space_info(struct btrfs_space_info *info, u64 bytes,
		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,