btrfs: make sure btrfs can handle full fs trim correctly
Ancient commit
f4c697e6406d ("btrfs: return EINVAL if start >
total_bytes in fitrim ioctl") introduced a regression where btrfs
may fail to trim any free space in existing block groups.
It's caused by confusion with btrfs_super_block->total_bytes and
btrfs logical address space.
Unlike physical address, any aligned bytenr in range [0, U64_MAX) is
valid in btrfs logical address space, and it's chunk mapping
mechanism of btrfs to handle the logical<->physical mapping.
The test case will craft a btrfs with the following features:
0) Single data/meta profile
Make trimmed bytes reporting and chunk allocation more predictable.
1) All chunks start beyond super_block->total_bytes (1G)
By relocating these blocks several times.
2) Unallocated space is less than 50% of the whole fs
3) Fragmented data chunks
Data chunks will be full of fragments, 50% of data chunks will be
free space.
So in theory fstrim should be able to trim over 50% space of the fs.
(after fix, 64% of the fs can be trimmed)
While the regression makes btrfs only able to trim unallocated
space, which is less than 50% of the total space.
(without fix, it's only 31%)
Fixed by patch named "btrfs: Ensure btrfs_trim_fs can trim the whole
fs".
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Eryu Guan <eguan@redhat.com>
Signed-off-by: Eryu Guan <eguan@redhat.com>