xfstests: Test FITRIM where length is smaller than FSB
[xfstests-dev.git] / 251
1 #!/bin/bash
2 # FS QA Test No. 251
3 #
4 # This test was created in order to verify filesystem FITRIM implementation.
5 # By many concurrent copy and remove operations and checking that files
6 # does not change after copied into SCRATCH_MNT test if FITRIM implementation
7 # corrupts the filesystem (data/metadata).
8 #
9 #-----------------------------------------------------------------------
10 # Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as
14 # published by the Free Software Foundation.
15 #
16 # This program is distributed in the hope that it would be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write the Free Software Foundation,
23 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 #-----------------------------------------------------------------------
25
26 owner=lczerner@redhat.com
27
28 seq=`basename $0`
29 echo "QA output created by $seq"
30
31 here=`pwd`
32 tmp=`mktemp -d`
33 status=1    # failure is the default!
34 trap "_cleanup; exit \$status" 0 1 3
35 trap "_destroy; exit \$status" 2 15
36 chpid=0
37 mypid=$$
38
39 # get standard environment, filters and checks
40 . ./common.rc
41 . ./common.filter
42
43 # real QA test starts here
44 _supported_fs generic
45 _supported_os Linux
46 _require_scratch
47 _require_fstrim
48 _scratch_mkfs >/dev/null 2>&1
49 _scratch_mount
50
51 _cleanup()
52 {
53         rm -rf $tmp
54 }
55
56 _destroy()
57 {
58         kill $pids $fstrim_pid 2> /dev/null
59         wait $pids $fstrim_pid 2> /dev/null
60         rm -rf $tmp
61 }
62
63 _destroy_fstrim()
64 {
65         kill $fpid 2> /dev/null
66         wait $fpid 2> /dev/null
67 }
68
69 _fail()
70 {
71         echo "$1"
72         kill $mypid 2> /dev/null
73 }
74
75 _guess_max_minlen()
76 {
77         mmlen=100000
78         while [ $mmlen -gt 1 ]; do
79                 $FSTRIM_PROG -l $(($mmlen*2))k -m ${mmlen}k $SCRATCH_MNT &> /dev/null && break
80                 mmlen=$(($mmlen/2))
81         done
82         echo $mmlen
83 }
84
85 ##
86 # Background FSTRIM loop. We are trimming the device in the loop and for
87 # test coverage, we are doing whole device trim followed by several smaller
88 # trims.
89 ##
90 fstrim_loop()
91 {
92         trap "_destroy_fstrim; exit \$status" 2 15
93         fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
94         mmlen=$(_guess_max_minlen)
95
96         while true ; do
97                 step=$((RANDOM*$RANDOM))
98                 minlen=$(((RANDOM*($RANDOM%2+1))%$mmlen))
99                 start=$RANDOM
100                 if [ $((RANDOM%10)) -gt 7 ]; then
101                         $FSTRIM_PROG $SCRATCH_MNT &
102                         fpid=$!
103                         wait $fpid
104                 fi
105                 while [ $start -lt $fsize ] ; do
106                         $FSTRIM_PROG -m ${minlen}k -o ${start}k -l ${step}k $SCRATCH_MNT &
107                         fpid=$!
108                         wait $fpid
109                         start=$(( $start + $step ))
110                 done
111         done
112 }
113
114 function check_sums() {
115         (
116         cd $SCRATCH_MNT/$p
117         find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
118         )
119
120         diff $tmp/content.sums $tmp/stress.$$.$p
121         if [ $? -ne 0 ]; then
122                 _fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
123         fi
124         rm -f $tmp/stress.$$.$p
125 }
126
127 function run_process() {
128         local p=$1
129         repeat=10
130
131         sleep $((5*$p))s &
132         export chpid=$! && wait $chpid &> /dev/null
133         chpid=0
134
135         while [ $repeat -gt 0 ]; do
136
137                 # Remove old directories.
138                 rm -rf $SCRATCH_MNT/$p
139                 export chpid=$! && wait $chpid &> /dev/null
140
141                 # Copy content -> partition.
142                 mkdir $SCRATCH_MNT/$p
143                 cp -axT $content/ $SCRATCH_MNT/$p/
144                 export chpid=$! && wait $chpid &> /dev/null
145
146                 check_sums
147                 repeat=$(( $repeat - 1 ))
148         done
149 }
150
151 nproc=20
152 content=$here
153
154 # Check for FITRIM support
155 echo -n "Checking FITRIM support: "
156 _test_batched_discard $SCRATCH_MNT || _notrun "FITRIM not supported on $SCRATCH_DEV"
157 echo "done."
158
159 mkdir -p $tmp
160
161 (
162 cd $content
163 find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
164 )
165
166 echo -n "Running the test: "
167 pids=""
168 fstrim_loop &
169 fstrim_pid=$!
170 p=1
171 while [ $p -le $nproc ]; do
172         run_process $p &
173         pids="$pids $!"
174         p=$(($p+1))
175 done
176 echo "done."
177
178 wait $pids
179 kill $fstrim_pid
180 wait $fstrim_pid
181
182 status=0
183
184 exit