2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2011 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
7 # Purpose of this test is to check FITRIM argument handling to make sure
8 # that the argument processing is right and that it does not overflow.
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
17 trap "exit \$status" 0 1 2 3 15
21 # get standard environment, filters and checks
25 # real QA test starts here
30 _scratch_mkfs >/dev/null 2>&1
33 _require_batched_discard $SCRATCH_MNT
35 fssize=$($DF_PROG -k | grep "$SCRATCH_MNT" | grep "$SCRATCH_DEV" | awk '{print $3}')
37 beyond_eofs=$(_math "$fssize*2048")
38 max_64bit=$(_math "2^64 - 1")
40 # All these tests should return EINVAL
41 # since the start is beyond the end of
44 echo "[+] Start beyond the end of fs (should fail)"
45 out=$($FSTRIM_PROG -o $beyond_eofs $SCRATCH_MNT 2>&1)
46 [ $? -eq 0 ] && status=1
47 echo $out | _filter_scratch
49 echo "[+] Start beyond the end of fs with len set (should fail)"
50 out=$($FSTRIM_PROG -o $beyond_eofs -l1M $SCRATCH_MNT 2>&1)
51 [ $? -eq 0 ] && status=1
52 echo $out | _filter_scratch
54 echo "[+] Start = 2^64-1 (should fail)"
55 out=$($FSTRIM_PROG -o $max_64bit $SCRATCH_MNT 2>&1)
56 [ $? -eq 0 ] && status=1
57 echo $out | _filter_scratch
59 echo "[+] Start = 2^64-1 and len is set (should fail)"
60 out=$($FSTRIM_PROG -o $max_64bit -l1M $SCRATCH_MNT 2>&1)
61 [ $? -eq 0 ] && status=1
62 echo $out | _filter_scratch
65 _scratch_mkfs >/dev/null 2>&1
68 # All these tests should succeed
69 # since the length should be truncated
71 echo "[+] Default length (should succeed)"
72 $FSTRIM_PROG $SCRATCH_MNT
73 [ $? -ne 0 ] && status=1
74 echo "[+] Default length with start set (should succeed)"
75 $FSTRIM_PROG -o10M $SCRATCH_MNT
76 [ $? -ne 0 ] && status=1
77 echo "[+] Length beyond the end of fs (should succeed)"
78 $FSTRIM_PROG -l $beyond_eofs $SCRATCH_MNT
79 [ $? -ne 0 ] && status=1
80 echo "[+] Length beyond the end of fs with start set (should succeed)"
81 $FSTRIM_PROG -o10M -l $beyond_eofs $SCRATCH_MNT
82 [ $? -ne 0 ] && status=1
85 _scratch_mkfs >/dev/null 2>&1
88 # This is a bit fuzzy, but since the file system is fresh
89 # there should be at least (fssize/2) free space to trim.
90 # This is supposed to catch wrong FITRIM argument handling
91 bytes=$($FSTRIM_PROG -v -o10M $SCRATCH_MNT | _filter_fstrim)
93 if [ $bytes -gt $(_math "$fssize*1024") ]; then
95 echo "After the full fs discard $bytes bytes were discarded"\
96 "however the file system is $(_math "$fssize*1024") bytes long."
99 # Btrfs is special and this test does not apply to it
100 # It is because btrfs does not have not-yet-used parts of the device
101 # mapped and since we got here right after the mkfs, there is not
102 # enough free extents in the root tree.
103 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
105 echo "After the full fs discard $bytes bytes were discarded"\
106 "however the file system is $(_math "$fssize*1024") bytes long."
109 # Now to catch overflows due to fsblk->allocation group number conversion
110 # This is different for every file system and it also apply just to some of
111 # them. In order to add check specific for file system you're interested in
112 # compute the arguments as you need and make the file system with proper
115 # (2^32-1) + 2 (this is set to overflow 32bit variable by 2)
116 base=$(_math "2^32+1")
122 start=$(_math "$base*$agsize*$bsize")
124 export MKFS_OPTIONS="-F -b $bsize -g $agsize"
129 start=$(_math "$base*$agsize*$bsize")
131 export MKFS_OPTIONS="-f -d agsize=$(_math "$agsize*$bsize") -b size=$bsize"
134 # (2^32-1) * 4096 * 65536 == 32bit max size * block size * ag size
135 start=$(_math "(2^32 - 1) * 4096 * 65536")
141 _scratch_mkfs >/dev/null 2>&1
143 # It should fail since $start is beyond the end of file system
144 $FSTRIM_PROG -o$start -l10M $SCRATCH_MNT &> /dev/null
145 if [ $? -eq 0 ]; then
147 echo "It seems that fs logic handling start"\
152 _scratch_mkfs >/dev/null 2>&1
155 # len should be big enough to cover the whole file system, so if the
156 # number of discarded bytes is smaller than file system size/2 then it
157 # most likely overflowed
158 # Btrfs is special and this test does not apply to it
159 # It is because btrfs does not have not-yet-used parts of the device
160 # mapped and since we got here right after the mkfs, there is not
161 # enough free extents in the root tree.
162 bytes=$($FSTRIM_PROG -v -l$len $SCRATCH_MNT | _filter_fstrim)
163 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
165 echo "It seems that fs logic handling len argument overflows"