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
31 _scratch_mkfs >/dev/null 2>&1
34 _require_batched_discard $SCRATCH_MNT
36 fssize=$($DF_PROG -k | grep "$SCRATCH_MNT" | grep "$SCRATCH_DEV" | awk '{print $3}')
38 beyond_eofs=$(_math "$fssize*2048")
39 max_64bit=$(_math "2^64 - 1")
41 # All these tests should return EINVAL
42 # since the start is beyond the end of
45 echo "[+] Start beyond the end of fs (should fail)"
46 out=$($FSTRIM_PROG -o $beyond_eofs $SCRATCH_MNT 2>&1)
47 [ $? -eq 0 ] && status=1
48 echo $out | _filter_scratch
50 echo "[+] Start beyond the end of fs with len set (should fail)"
51 out=$($FSTRIM_PROG -o $beyond_eofs -l1M $SCRATCH_MNT 2>&1)
52 [ $? -eq 0 ] && status=1
53 echo $out | _filter_scratch
55 echo "[+] Start = 2^64-1 (should fail)"
56 out=$($FSTRIM_PROG -o $max_64bit $SCRATCH_MNT 2>&1)
57 [ $? -eq 0 ] && status=1
58 echo $out | _filter_scratch
60 echo "[+] Start = 2^64-1 and len is set (should fail)"
61 out=$($FSTRIM_PROG -o $max_64bit -l1M $SCRATCH_MNT 2>&1)
62 [ $? -eq 0 ] && status=1
63 echo $out | _filter_scratch
66 _scratch_mkfs >/dev/null 2>&1
69 # All these tests should succeed
70 # since the length should be truncated
72 echo "[+] Default length (should succeed)"
73 $FSTRIM_PROG $SCRATCH_MNT
74 [ $? -ne 0 ] && status=1
75 echo "[+] Default length with start set (should succeed)"
76 $FSTRIM_PROG -o10M $SCRATCH_MNT
77 [ $? -ne 0 ] && status=1
78 echo "[+] Length beyond the end of fs (should succeed)"
79 $FSTRIM_PROG -l $beyond_eofs $SCRATCH_MNT
80 [ $? -ne 0 ] && status=1
81 echo "[+] Length beyond the end of fs with start set (should succeed)"
82 $FSTRIM_PROG -o10M -l $beyond_eofs $SCRATCH_MNT
83 [ $? -ne 0 ] && status=1
86 _scratch_mkfs >/dev/null 2>&1
89 # This is a bit fuzzy, but since the file system is fresh
90 # there should be at least (fssize/2) free space to trim.
91 # This is supposed to catch wrong FITRIM argument handling
92 bytes=$($FSTRIM_PROG -v -o10M $SCRATCH_MNT | _filter_fstrim)
94 if [ $bytes -gt $(_math "$fssize*1024") ]; then
96 echo "After the full fs discard $bytes bytes were discarded"\
97 "however the file system is $(_math "$fssize*1024") bytes long."
100 # Btrfs is special and this test does not apply to it
101 # It is because btrfs does not have not-yet-used parts of the device
102 # mapped and since we got here right after the mkfs, there is not
103 # enough free extents in the root tree.
104 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
106 echo "After the full fs discard $bytes bytes were discarded"\
107 "however the file system is $(_math "$fssize*1024") bytes long."
110 # Now to catch overflows due to fsblk->allocation group number conversion
111 # This is different for every file system and it also apply just to some of
112 # them. In order to add check specific for file system you're interested in
113 # compute the arguments as you need and make the file system with proper
116 # (2^32-1) + 2 (this is set to overflow 32bit variable by 2)
117 base=$(_math "2^32+1")
123 start=$(_math "$base*$agsize*$bsize")
125 export MKFS_OPTIONS="-F -b $bsize -g $agsize"
130 start=$(_math "$base*$agsize*$bsize")
132 export MKFS_OPTIONS="-f -d agsize=$(_math "$agsize*$bsize") -b size=$bsize"
135 # (2^32-1) * 4096 * 65536 == 32bit max size * block size * ag size
136 start=$(_math "(2^32 - 1) * 4096 * 65536")
142 _scratch_mkfs >/dev/null 2>&1
144 # It should fail since $start is beyond the end of file system
145 $FSTRIM_PROG -o$start -l10M $SCRATCH_MNT &> /dev/null
146 if [ $? -eq 0 ]; then
148 echo "It seems that fs logic handling start"\
153 _scratch_mkfs >/dev/null 2>&1
156 # len should be big enough to cover the whole file system, so if the
157 # number of discarded bytes is smaller than file system size/2 then it
158 # most likely overflowed
159 # Btrfs is special and this test does not apply to it
160 # It is because btrfs does not have not-yet-used parts of the device
161 # mapped and since we got here right after the mkfs, there is not
162 # enough free extents in the root tree.
163 bytes=$($FSTRIM_PROG -v -l$len $SCRATCH_MNT | _filter_fstrim)
164 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
166 echo "It seems that fs logic handling len argument overflows"