fstests: move test group info to test files
[xfstests-dev.git] / tests / generic / 251
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
4 #
5 # FS QA Test No. 251
6 #
7 # This test was created in order to verify filesystem FITRIM implementation.
8 # By many concurrent copy and remove operations and checking that files
9 # does not change after copied into SCRATCH_MNT test if FITRIM implementation
10 # corrupts the filesystem (data/metadata).
11 #
12 . ./common/preamble
13 _begin_fstest ioctl trim
14
15 tmp=`mktemp -d`
16 trap "_cleanup; exit \$status" 0 1 3
17 trap "_destroy; exit \$status" 2 15
18 chpid=0
19 mypid=$$
20
21 # Import common functions.
22 . ./common/filter
23
24 # real QA test starts here
25 _supported_fs generic
26 _require_scratch
27 _scratch_mkfs >/dev/null 2>&1
28 _scratch_mount
29 _require_batched_discard $SCRATCH_MNT
30
31 # Override the default cleanup function.
32 _cleanup()
33 {
34         rm -rf $tmp
35 }
36
37 _destroy()
38 {
39         kill $pids $fstrim_pid 2> /dev/null
40         wait $pids $fstrim_pid 2> /dev/null
41         rm -rf $tmp
42 }
43
44 _destroy_fstrim()
45 {
46         kill $fpid 2> /dev/null
47         wait $fpid 2> /dev/null
48 }
49
50 _fail()
51 {
52         echo "$1"
53         kill $mypid 2> /dev/null
54 }
55
56 _guess_max_minlen()
57 {
58         mmlen=100000
59         while [ $mmlen -gt 1 ]; do
60                 $FSTRIM_PROG -l $(($mmlen*2))k -m ${mmlen}k $SCRATCH_MNT &> /dev/null && break
61                 mmlen=$(($mmlen/2))
62         done
63         echo $mmlen
64 }
65
66 ##
67 # Background FSTRIM loop. We are trimming the device in the loop and for
68 # test coverage, we are doing whole device trim followed by several smaller
69 # trims.
70 ##
71 fstrim_loop()
72 {
73         trap "_destroy_fstrim; exit \$status" 2 15
74         fsize=$($DF_PROG | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $3}')
75         mmlen=$(_guess_max_minlen)
76
77         while true ; do
78                 step=$((RANDOM*$RANDOM+4))
79                 minlen=$(((RANDOM*($RANDOM%2+1))%$mmlen))
80                 start=$RANDOM
81                 if [ $((RANDOM%10)) -gt 7 ]; then
82                         $FSTRIM_PROG $SCRATCH_MNT &
83                         fpid=$!
84                         wait $fpid
85                 fi
86                 while [ $start -lt $fsize ] ; do
87                         $FSTRIM_PROG -m ${minlen}k -o ${start}k -l ${step}k $SCRATCH_MNT &
88                         fpid=$!
89                         wait $fpid
90                         start=$(( $start + $step ))
91                 done
92         done
93 }
94
95 function check_sums() {
96         (
97         cd $SCRATCH_MNT/$p
98         find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
99         )
100
101         diff $tmp/content.sums $tmp/stress.$$.$p
102         if [ $? -ne 0 ]; then
103                 _fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
104         fi
105         rm -f $tmp/stress.$$.$p
106 }
107
108 function run_process() {
109         local p=$1
110         repeat=10
111
112         sleep $((5*$p))s &
113         export chpid=$! && wait $chpid &> /dev/null
114         chpid=0
115
116         while [ $repeat -gt 0 ]; do
117
118                 # Remove old directories.
119                 rm -rf $SCRATCH_MNT/$p
120                 export chpid=$! && wait $chpid &> /dev/null
121
122                 # Copy content -> partition.
123                 mkdir $SCRATCH_MNT/$p
124                 cp -axT $content/ $SCRATCH_MNT/$p/
125                 export chpid=$! && wait $chpid &> /dev/null
126
127                 check_sums
128                 repeat=$(( $repeat - 1 ))
129         done
130 }
131
132 nproc=20
133 content=$here
134
135 mkdir -p $tmp
136
137 (
138 cd $content
139 find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
140 )
141
142 echo -n "Running the test: "
143 pids=""
144 fstrim_loop &
145 fstrim_pid=$!
146 p=1
147 while [ $p -le $nproc ]; do
148         run_process $p &
149         pids="$pids $!"
150         p=$(($p+1))
151 done
152 echo "done."
153
154 wait $pids
155 kill $fstrim_pid
156 wait $fstrim_pid
157
158 status=0
159
160 exit