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