common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[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 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1        # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28         cd /
29         rm -f $tmp.*
30 }
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35
36 # remove previous $seqres.full before test
37 rm -f $seqres.full
38
39 # real QA test starts here
40
41 # Modify as appropriate.
42 _supported_fs btrfs
43 _supported_os Linux
44 _require_scratch
45 _require_fstrim
46
47 # 1024fs size
48 fs_size=$((1024 * 1024 * 1024))
49
50 # Use small files to fill half of the fs
51 file_size=$(( 1024 * 1024 ))
52 nr_files=$(( $fs_size / $file_size / 2))
53
54 # Force to use single data and meta profile.
55 # Since the test relies on fstrim output, which will differ for different
56 # profiles
57 _scratch_mkfs -b $fs_size -m single -d single > /dev/null
58 _scratch_mount
59
60 _require_batched_discard "$SCRATCH_MNT"
61
62 for n in $(seq -w 0 $(( $nr_files - 1))); do
63         $XFS_IO_PROG -f -c "pwrite 0 $file_size" "$SCRATCH_MNT/file_$n" \
64                 > /dev/null
65 done
66
67 # Flush all buffer data into disk, to trigger chunk allocation
68 sync
69
70 # Now we have take at least 50% of the filesystem, relocate all chunks twice
71 # so all chunks will start after 1G in logical space.
72 # (Btrfs chunk allocation will not rewind to reuse lower space)
73 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
74
75 # To avoid possible false ENOSPC alert on v4.15-rc1, seems to be a
76 # reserved space related bug (maybe related to outstanding space rework?),
77 # but that's another story.
78 sync
79
80 _run_btrfs_balance_start $SCRATCH_MNT >> $seqres.full
81
82 # Now remove half of the files to make some holes for later trim.
83 # While still keep the chunk space fragmented, so no chunk will be freed
84 rm $SCRATCH_MNT/file_*[13579] -f
85
86 # Make sure space is freed
87 sync
88
89 trimmed=$($FSTRIM_PROG -v "$SCRATCH_MNT" | _filter_fstrim)
90 echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%" \
91         >> $seqres.full
92
93 # For correct full fs trim, both unallocated space (less than 50%)
94 # and free space in existing block groups (about 25%) should be trimmed.
95 # If less than 50% is trimmed, then only unallocated space is trimmed.
96 # BTW, without fix only 31% can be trimmed, while after fix it's 64%.
97 if [ $trimmed -lt $(( $fs_size / 2)) ]; then
98         echo "Free space in block groups not trimmed"
99         echo "Trimmed=$trimmed total_size=$fs_size ratio=$(($trimmed * 100 / $fs_size))%"
100 fi
101
102 echo "Silence is golden"
103 # success, all done
104 status=0
105 exit