xfstests: make install support common/ and tests/ dirs
[xfstests-dev.git] / tests / generic / 260
1 #!/bin/bash
2 # FS QA Test No. 260
3 #
4 # Purpose of this test is to check FITRIM argument handling to make sure
5 # that the argument processing is right and that it does not overflow.
6 #
7 #-----------------------------------------------------------------------
8 # Copyright 2011 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation.
13 #
14 # This program is distributed in the hope that it would be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write the Free Software Foundation,
21 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 #-----------------------------------------------------------------------
23
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=`mktemp -d`
30 status=0
31 trap "exit \$status" 0 1 2 3 15
32 chpid=0
33 mypid=$$
34
35 # get standard environment, filters and checks
36 . ./common/rc
37 . ./common/filter
38
39 # real QA test starts here
40 _supported_fs generic
41 _supported_os Linux
42 _require_math
43 _require_fstrim
44
45 _require_scratch
46 _scratch_mkfs >/dev/null 2>&1
47 _scratch_mount
48
49 _test_batched_discard $SCRATCH_MNT || _notrun "FITRIM not supported on $SCRATCH_DEV"
50
51 fssize=$(df -k | grep "$SCRATCH_MNT" | grep "$SCRATCH_DEV"  | awk '{print $2}')
52
53 beyond_eofs=$(_math "$fssize*2048")
54 max_64bit=$(_math "2^64 - 1")
55
56 # All these tests should return EINVAL
57 # since the start is beyond the end of
58 # the file system
59
60 echo "[+] Start beyond the end of fs (should fail)"
61 out=$($FSTRIM_PROG -o $beyond_eofs $SCRATCH_MNT 2>&1)
62 [ $? -eq 0 ] && status=1
63 echo $out | _filter_scratch
64
65 echo "[+] Start beyond the end of fs with len set (should fail)"
66 out=$($FSTRIM_PROG -o $beyond_eofs -l1M $SCRATCH_MNT 2>&1)
67 [ $? -eq 0 ] && status=1
68 echo $out | _filter_scratch
69
70 echo "[+] Start = 2^64-1 (should fail)"
71 out=$($FSTRIM_PROG -o $max_64bit $SCRATCH_MNT 2>&1)
72 [ $? -eq 0 ] && status=1
73 echo $out | _filter_scratch
74
75 echo "[+] Start = 2^64-1 and len is set (should fail)"
76 out=$($FSTRIM_PROG -o $max_64bit -l1M $SCRATCH_MNT 2>&1)
77 [ $? -eq 0 ] && status=1
78 echo $out | _filter_scratch
79
80 _scratch_unmount
81 _scratch_mkfs >/dev/null 2>&1
82 _scratch_mount
83
84 # All these tests should succeed
85 # since the length should be truncated
86
87 echo "[+] Default length (should succeed)"
88 $FSTRIM_PROG $SCRATCH_MNT
89 [ $? -ne 0 ] && status=1
90 echo "[+] Default length with start set (should succeed)"
91 $FSTRIM_PROG -o10M $SCRATCH_MNT
92 [ $? -ne 0 ] && status=1
93 echo "[+] Length beyond the end of fs (should succeed)"
94 $FSTRIM_PROG -l $beyond_eofs $SCRATCH_MNT
95 [ $? -ne 0 ] && status=1
96 echo "[+] Length beyond the end of fs with start set (should succeed)"
97 $FSTRIM_PROG -o10M -l $beyond_eofs $SCRATCH_MNT
98 [ $? -ne 0 ] && status=1
99
100 _scratch_unmount
101 _scratch_mkfs >/dev/null 2>&1
102 _scratch_mount
103
104 # This is a bit fuzzy, but since the file system is fresh
105 # there should be at least (fssize/2) free space to trim.
106 # This is supposed to catch wrong FITRIM argument handling
107 out=$($FSTRIM_PROG -v -o10M $SCRATCH_MNT)
108 nopref=${out##*: }
109 bytes=${nopref%% *}
110
111 if [ $bytes -gt $(_math "$fssize*1024") ]; then
112         status=1
113         echo "After the full fs discard $bytes bytes were discarded"\
114              "however the file system is $(_math "$fssize*1024") bytes long."
115 fi
116
117 # Btrfs is special and this test does not apply to it
118 # It is because btrfs does not have not-yet-used parts of the device
119 # mapped and since we got here right after the mkfs, there is not
120 # enough free extents in the root tree.
121 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
122         status=1
123         echo "After the full fs discard $bytes bytes were discarded"\
124              "however the file system is $(_math "$fssize*1024") bytes long."
125 fi
126
127 # Now to catch overflows due to fsblk->allocation group number conversion
128 # This is different for every file system and it also apply just to some of
129 # them. In order to add check specific for file system you're interested in
130 # compute the arguments as you need and make the file system with proper
131 # alignment
132
133 # (2^32-1) + 2 (this is set to overflow 32bit variable by 2)
134 base=$(_math "2^32+1")
135
136 case $FSTYP in
137         ext[34])
138                 agsize=32768
139                 bsize=4096
140                 start=$(_math "$base*$agsize*$bsize")
141                 len=$start
142                 export MKFS_OPTIONS="-F -b $bsize -g $agsize"
143                 ;;
144         xfs)
145                 agsize=65538
146                 bsize=4096
147                 start=$(_math "$base*$agsize*$bsize")
148                 len=$start
149                 export MKFS_OPTIONS="-f -d agsize=$(_math "$agsize*$bsize") -b size=$bsize"
150                 ;;
151         *)
152                 # (2^32-1) * 4096 * 65536 == 32bit max size * block size * ag size
153                 start=$(_math "(2^32 - 1) * 4096 * 65536")
154                 len=$start
155                 ;;
156 esac
157
158 _scratch_unmount
159 _scratch_mkfs >/dev/null 2>&1
160 _scratch_mount
161 # It should fail since $start is beyond the end of file system
162 $FSTRIM_PROG -o$start -l10M $SCRATCH_MNT &> /dev/null
163 if [ $? -eq 0 ]; then
164         status=1
165         echo "It seems that fs logic handling start"\
166              "argument overflows"
167 fi
168
169 _scratch_unmount
170 _scratch_mkfs >/dev/null 2>&1
171 _scratch_mount
172
173 # len should be big enough to cover the whole file system, so if the
174 # number of discarded bytes is smaller than file system size/2 then it
175 # most likely overflowed
176 # Btrfs is special and this test does not apply to it
177 # It is because btrfs does not have not-yet-used parts of the device
178 # mapped and since we got here right after the mkfs, there is not
179 # enough free extents in the root tree.
180 out=$($FSTRIM_PROG -v -l$len $SCRATCH_MNT)
181 nopref=${out##*: }
182 bytes=${nopref%% *}
183 if [ $bytes -le $(_math "$fssize*512") ] && [ $FSTYP != "btrfs" ]; then
184         status=1
185         echo "It seems that fs logic handling len argument overflows"
186 fi
187
188 echo "Test done"
189 exit