2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 SUSE Linux Products GmbH. All Rights Reserved.
7 # Check if btrfs can correctly trim free space in block groups
9 # An ancient regression prevent btrfs from trimming free space inside
10 # existing block groups, if bytenr of block group starts beyond
11 # btrfs_super_block->total_bytes.
12 # However all bytenr in btrfs is in btrfs logical address space,
13 # where any bytenr in range [0, U64_MAX] is valid.
15 # Fixed by patch named "btrfs: Ensure btrfs_trim_fs can trim the whole fs".
18 _begin_fstest auto quick trim balance
20 # Import common functions.
23 # real QA test starts here
25 # Modify as appropriate.
30 # We need the allocated space to actually use that amount so the trim amount
31 # comes out correctly. Because we mark free extents as TRIMMED we won't trim
32 # the free extents on the second fstrim and thus we'll get a trimmed bytes at <
33 # half of the device if we have compression enabled, even though fs trim did the
38 fs_size=$((1024 * 1024 * 1024))
40 # Use small files to fill half of the fs
41 file_size=$(( 1024 * 1024 ))
42 nr_files=$(( $fs_size / $file_size / 2))
44 # Force to use single data and meta profile.
45 # Since the test relies on fstrim output, which will differ for different
47 _check_minimal_fs_size $fs_size
48 _scratch_mkfs -b $fs_size -m single -d single > /dev/null
51 _require_batched_discard "$SCRATCH_MNT"
53 for n in $(seq -w 0 $(( $nr_files - 1))); do
54 $XFS_IO_PROG -f -c "pwrite 0 $file_size" "$SCRATCH_MNT/file_$n" \
58 # Flush all buffer data into disk, to trigger chunk allocation
61 # Now we have take at least 50% of the filesystem, relocate all chunks twice
62 # so all chunks will start after 1G in logical space.
63 # (Btrfs chunk allocation will not rewind to reuse lower space)
64 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
66 # To avoid possible false ENOSPC alert on v4.15-rc1, seems to be a
67 # reserved space related bug (maybe related to outstanding space rework?),
68 # but that's another story.
71 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
73 # Now remove half of the files to make some holes for later trim.
74 # While still keep the chunk space fragmented, so no chunk will be freed
75 rm $SCRATCH_MNT/file_*[13579] -f
77 # Make sure space is freed
80 trimmed=$($FSTRIM_PROG -v "$SCRATCH_MNT" | _filter_fstrim)
81 echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%" \
84 # For correct full fs trim, both unallocated space (less than 50%)
85 # and free space in existing block groups (about 25%) should be trimmed.
86 # If less than 50% is trimmed, then only unallocated space is trimmed.
87 # BTW, without fix only 31% can be trimmed, while after fix it's 64%.
88 if [ $trimmed -lt $(( $fs_size / 2)) ]; then
89 echo "Free space in block groups not trimmed"
90 echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%"
93 echo "Silence is golden"