274: Many fixups
[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 _scratch_mkfs >/dev/null 2>&1
48 _scratch_mount
49
50 _cleanup()
51 {
52         rm -rf $tmp
53 }
54
55 _destroy()
56 {
57         kill $pids $fstrim_pid 2> /dev/null
58         wait $pids $fstrim_pid 2> /dev/null
59         rm -rf $tmp
60 }
61
62 _destroy_fstrim()
63 {
64         kill $fpid 2> /dev/null
65         wait $fpid 2> /dev/null
66 }
67
68 _fail()
69 {
70         echo "$1"
71         kill $mypid 2> /dev/null
72 }
73
74 _check_fstrim_support()
75 {
76         $here/src/fstrim -l 10M $SCRATCH_MNT &> /dev/null
77 }
78
79 _guess_max_minlen()
80 {
81         mmlen=100000
82         while [ $mmlen -gt 1 ]; do
83                 $here/src/fstrim -l $(($mmlen*2))k -m ${mmlen}k $SCRATCH_MNT &> /dev/null && break
84                 mmlen=$(($mmlen/2))
85         done
86         echo $mmlen
87 }
88
89 ##
90 # Background FSTRIM loop. We are trimming the device in the loop and for
91 # test coverage, we are doing whole device trim followed by several smaller
92 # trims.
93 ##
94 fstrim_loop()
95 {
96         trap "_destroy_fstrim; exit \$status" 2 15
97         fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
98         mmlen=$(_guess_max_minlen)
99
100         while true ; do
101                 step=$((RANDOM*$RANDOM))
102                 minlen=$(((RANDOM*($RANDOM%2+1))%$mmlen))
103                 start=$RANDOM
104                 if [ $((RANDOM%10)) -gt 7 ]; then
105                         $here/src/fstrim $SCRATCH_MNT &
106                         fpid=$!
107                         wait $fpid
108                 fi
109                 while [ $start -lt $fsize ] ; do
110                         $here/src/fstrim -m ${minlen}k -s ${start}k -l ${step}k $SCRATCH_MNT &
111                         fpid=$!
112                         wait $fpid
113                         start=$(( $start + $step ))
114                 done
115         done
116 }
117
118 function check_sums() {
119         (
120         cd $SCRATCH_MNT/$p
121         find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
122         )
123
124         diff $tmp/content.sums $tmp/stress.$$.$p
125         if [ $? -ne 0 ]; then
126                 _fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
127         fi
128         rm -f $tmp/stress.$$.$p
129 }
130
131 function run_process() {
132         local p=$1
133         repeat=10
134
135         sleep $((5*$p))s &
136         export chpid=$! && wait $chpid &> /dev/null
137         chpid=0
138
139         while [ $repeat -gt 0 ]; do
140
141                 # Remove old directories.
142                 rm -rf $SCRATCH_MNT/$p
143                 export chpid=$! && wait $chpid &> /dev/null
144
145                 # Copy content -> partition.
146                 mkdir $SCRATCH_MNT/$p
147                 cp -axT $content/ $SCRATCH_MNT/$p/
148                 export chpid=$! && wait $chpid &> /dev/null
149
150                 check_sums
151                 repeat=$(( $repeat - 1 ))
152         done
153 }
154
155 nproc=20
156 content=$here
157
158 # Check for FITRIM support
159 echo -n "Checking FITRIM support: "
160 _check_fstrim_support || _notrun "FSTRIM is not supported"
161 echo "done."
162
163 mkdir -p $tmp
164
165 (
166 cd $content
167 find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
168 )
169
170 echo -n "Running the test: "
171 pids=""
172 fstrim_loop &
173 fstrim_pid=$!
174 p=1
175 while [ $p -le $nproc ]; do
176         run_process $p &
177         pids="$pids $!"
178         p=$(($p+1))
179 done
180 echo "done."
181
182 wait $pids
183 kill $fstrim_pid
184 wait $fstrim_pid
185
186 status=0
187
188 exit