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