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