Skip to content

Commit e3e39c7

Browse files
marcospskdave
authored andcommitted
btrfs: block-group: fix free-space bitmap threshold
[BUG] After commit 9afc664 ("btrfs: block-group: refactor how we read one block group item"), cache->length is being assigned after calling btrfs_create_block_group_cache. This causes a problem since set_free_space_tree_thresholds calculates the free-space threshold to decide if the free-space tree should convert from extents to bitmaps. The current code calls set_free_space_tree_thresholds with cache->length being 0, which then makes cache->bitmap_high_thresh zero. This implies the system will always use bitmap instead of extents, which is not desired if the block group is not fragmented. This behavior can be seen by a test that expects to repair systems with FREE_SPACE_EXTENT and FREE_SPACE_BITMAP, but the current code only created FREE_SPACE_BITMAP. [FIX] Call set_free_space_tree_thresholds after setting cache->length. There is now a WARN_ON in set_free_space_tree_thresholds to help preventing the same mistake to happen again in the future. Link: kdave/btrfs-progs#251 Fixes: 9afc664 ("btrfs: block-group: refactor how we read one block group item") CC: stable@vger.kernel.org # 5.8+ Reviewed-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent a84d5d4 commit e3e39c7

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

fs/btrfs/block-group.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,6 @@ static struct btrfs_block_group *btrfs_create_block_group_cache(
17981798

17991799
cache->fs_info = fs_info;
18001800
cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
1801-
set_free_space_tree_thresholds(cache);
18021801

18031802
cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
18041803

@@ -1912,6 +1911,8 @@ static int read_one_block_group(struct btrfs_fs_info *info,
19121911
if (ret < 0)
19131912
goto error;
19141913

1914+
set_free_space_tree_thresholds(cache);
1915+
19151916
if (need_clear) {
19161917
/*
19171918
* When we mount with old space cache, we need to
@@ -2132,6 +2133,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
21322133
return -ENOMEM;
21332134

21342135
cache->length = size;
2136+
set_free_space_tree_thresholds(cache);
21352137
cache->used = bytes_used;
21362138
cache->flags = type;
21372139
cache->last_byte_to_unpin = (u64)-1;

fs/btrfs/free-space-tree.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ void set_free_space_tree_thresholds(struct btrfs_block_group *cache)
2222
size_t bitmap_size;
2323
u64 num_bitmaps, total_bitmap_size;
2424

25+
if (WARN_ON(cache->length == 0))
26+
btrfs_warn(cache->fs_info, "block group %llu length is zero",
27+
cache->start);
28+
2529
/*
2630
* We convert to bitmaps when the disk space required for using extents
2731
* exceeds that required for using bitmaps.

0 commit comments

Comments
 (0)