fstests: move test group info to test files
[xfstests-dev.git] / tests / btrfs / 156
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 SUSE Linux Products GmbH.  All Rights Reserved.
4 #
5 # FS QA Test 156
6 #
7 # Check if btrfs can correctly trim free space in block groups
8 #
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.
14 #
15 # Fixed by patch named "btrfs: Ensure btrfs_trim_fs can trim the whole fs".
16 #
17 . ./common/preamble
18 _begin_fstest auto quick trim balance
19
20 # Import common functions.
21 . ./common/filter
22
23 # real QA test starts here
24
25 # Modify as appropriate.
26 _supported_fs btrfs
27 _require_scratch
28 _require_fstrim
29
30 # 1024fs size
31 fs_size=$((1024 * 1024 * 1024))
32
33 # Use small files to fill half of the fs
34 file_size=$(( 1024 * 1024 ))
35 nr_files=$(( $fs_size / $file_size / 2))
36
37 # Force to use single data and meta profile.
38 # Since the test relies on fstrim output, which will differ for different
39 # profiles
40 _scratch_mkfs -b $fs_size -m single -d single > /dev/null
41 _scratch_mount
42
43 _require_batched_discard "$SCRATCH_MNT"
44
45 for n in $(seq -w 0 $(( $nr_files - 1))); do
46         $XFS_IO_PROG -f -c "pwrite 0 $file_size" "$SCRATCH_MNT/file_$n" \
47                 > /dev/null
48 done
49
50 # Flush all buffer data into disk, to trigger chunk allocation
51 sync
52
53 # Now we have take at least 50% of the filesystem, relocate all chunks twice
54 # so all chunks will start after 1G in logical space.
55 # (Btrfs chunk allocation will not rewind to reuse lower space)
56 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
57
58 # To avoid possible false ENOSPC alert on v4.15-rc1, seems to be a
59 # reserved space related bug (maybe related to outstanding space rework?),
60 # but that's another story.
61 sync
62
63 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
64
65 # Now remove half of the files to make some holes for later trim.
66 # While still keep the chunk space fragmented, so no chunk will be freed
67 rm $SCRATCH_MNT/file_*[13579] -f
68
69 # Make sure space is freed
70 sync
71
72 trimmed=$($FSTRIM_PROG -v "$SCRATCH_MNT" | _filter_fstrim)
73 echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%" \
74         >> $seqres.full
75
76 # For correct full fs trim, both unallocated space (less than 50%)
77 # and free space in existing block groups (about 25%) should be trimmed.
78 # If less than 50% is trimmed, then only unallocated space is trimmed.
79 # BTW, without fix only 31% can be trimmed, while after fix it's 64%.
80 if [ $trimmed -lt $(( $fs_size / 2)) ]; then
81         echo "Free space in block groups not trimmed"
82         echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%"
83 fi
84
85 echo "Silence is golden"
86 # success, all done
87 status=0
88 exit