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