generic/520: Remove sync in clean_dir
[xfstests-dev.git] / tests / generic / 260
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2011 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
4 #
5 # FS QA Test No. 260
6 #
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.
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13
14 here=`pwd`
15 tmp=/tmp/$$
16 status=0
17 trap "exit \$status" 0 1 2 3 15
18 chpid=0
19 mypid=$$
20
21 # get standard environment, filters and checks
22 . ./common/rc
23 . ./common/filter
24
25 # real QA test starts here
26 _supported_fs generic
27 _supported_os Linux
28 _require_math
29
30 _require_scratch
31 _scratch_mkfs >/dev/null 2>&1
32 _scratch_mount
33
34 _require_batched_discard $SCRATCH_MNT
35
36 fssize=$($DF_PROG -k | grep "$SCRATCH_MNT" | grep "$SCRATCH_DEV"  | awk '{print $3}')
37
38 beyond_eofs=$(_math "$fssize*2048")
39 max_64bit=$(_math "2^64 - 1")
40
41 # All these tests should return EINVAL
42 # since the start is beyond the end of
43 # the file system
44
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
49
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
54
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
59
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
64
65 _scratch_unmount
66 _scratch_mkfs >/dev/null 2>&1
67 _scratch_mount
68
69 # All these tests should succeed
70 # since the length should be truncated
71
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
84
85 _scratch_unmount
86 _scratch_mkfs >/dev/null 2>&1
87 _scratch_mount
88
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)
93
94 if [ $bytes -gt $(_math "$fssize*1024") ]; then
95         status=1
96         echo "After the full fs discard $bytes bytes were discarded"\
97              "however the file system is $(_math "$fssize*1024") bytes long."
98 fi
99
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
105         status=1
106         echo "After the full fs discard $bytes bytes were discarded"\
107              "however the file system is $(_math "$fssize*1024") bytes long."
108 fi
109
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
114 # alignment
115
116 # (2^32-1) + 2 (this is set to overflow 32bit variable by 2)
117 base=$(_math "2^32+1")
118
119 case $FSTYP in
120         ext[34])
121                 agsize=32768
122                 bsize=4096
123                 start=$(_math "$base*$agsize*$bsize")
124                 len=$start
125                 export MKFS_OPTIONS="-F -b $bsize -g $agsize"
126                 ;;
127         xfs)
128                 agsize=65538
129                 bsize=4096
130                 start=$(_math "$base*$agsize*$bsize")
131                 len=$start
132                 export MKFS_OPTIONS="-f -d agsize=$(_math "$agsize*$bsize") -b size=$bsize"
133                 ;;
134         *)
135                 # (2^32-1) * 4096 * 65536 == 32bit max size * block size * ag size
136                 start=$(_math "(2^32 - 1) * 4096 * 65536")
137                 len=$start
138                 ;;
139 esac
140
141 _scratch_unmount
142 _scratch_mkfs >/dev/null 2>&1
143 _scratch_mount
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
147         status=1
148         echo "It seems that fs logic handling start"\
149              "argument overflows"
150 fi
151
152 _scratch_unmount
153 _scratch_mkfs >/dev/null 2>&1
154 _scratch_mount
155
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
165         status=1
166         echo "It seems that fs logic handling len argument overflows"
167 fi
168
169 echo "Test done"
170 exit