generic: test for file fsync after moving it to a new parent directory
[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 seq=`basename $0`
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
15
16 here=`pwd`
17 tmp=`mktemp -d`
18 status=1    # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 3
20 trap "_destroy; exit \$status" 2 15
21 chpid=0
22 mypid=$$
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # real QA test starts here
29 _supported_fs generic
30 _supported_os Linux
31 _require_scratch
32 _scratch_mkfs >/dev/null 2>&1
33 _scratch_mount
34 _require_batched_discard $SCRATCH_MNT
35
36 _cleanup()
37 {
38         rm -rf $tmp
39 }
40
41 _destroy()
42 {
43         kill $pids $fstrim_pid 2> /dev/null
44         wait $pids $fstrim_pid 2> /dev/null
45         rm -rf $tmp
46 }
47
48 _destroy_fstrim()
49 {
50         kill $fpid 2> /dev/null
51         wait $fpid 2> /dev/null
52 }
53
54 _fail()
55 {
56         echo "$1"
57         kill $mypid 2> /dev/null
58 }
59
60 _guess_max_minlen()
61 {
62         mmlen=100000
63         while [ $mmlen -gt 1 ]; do
64                 $FSTRIM_PROG -l $(($mmlen*2))k -m ${mmlen}k $SCRATCH_MNT &> /dev/null && break
65                 mmlen=$(($mmlen/2))
66         done
67         echo $mmlen
68 }
69
70 ##
71 # Background FSTRIM loop. We are trimming the device in the loop and for
72 # test coverage, we are doing whole device trim followed by several smaller
73 # trims.
74 ##
75 fstrim_loop()
76 {
77         trap "_destroy_fstrim; exit \$status" 2 15
78         fsize=$($DF_PROG | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $3}')
79         mmlen=$(_guess_max_minlen)
80
81         while true ; do
82                 step=$((RANDOM*$RANDOM+4))
83                 minlen=$(((RANDOM*($RANDOM%2+1))%$mmlen))
84                 start=$RANDOM
85                 if [ $((RANDOM%10)) -gt 7 ]; then
86                         $FSTRIM_PROG $SCRATCH_MNT &
87                         fpid=$!
88                         wait $fpid
89                 fi
90                 while [ $start -lt $fsize ] ; do
91                         $FSTRIM_PROG -m ${minlen}k -o ${start}k -l ${step}k $SCRATCH_MNT &
92                         fpid=$!
93                         wait $fpid
94                         start=$(( $start + $step ))
95                 done
96         done
97 }
98
99 function check_sums() {
100         (
101         cd $SCRATCH_MNT/$p
102         find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
103         )
104
105         diff $tmp/content.sums $tmp/stress.$$.$p
106         if [ $? -ne 0 ]; then
107                 _fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
108         fi
109         rm -f $tmp/stress.$$.$p
110 }
111
112 function run_process() {
113         local p=$1
114         repeat=10
115
116         sleep $((5*$p))s &
117         export chpid=$! && wait $chpid &> /dev/null
118         chpid=0
119
120         while [ $repeat -gt 0 ]; do
121
122                 # Remove old directories.
123                 rm -rf $SCRATCH_MNT/$p
124                 export chpid=$! && wait $chpid &> /dev/null
125
126                 # Copy content -> partition.
127                 mkdir $SCRATCH_MNT/$p
128                 cp -axT $content/ $SCRATCH_MNT/$p/
129                 export chpid=$! && wait $chpid &> /dev/null
130
131                 check_sums
132                 repeat=$(( $repeat - 1 ))
133         done
134 }
135
136 nproc=20
137 content=$here
138
139 mkdir -p $tmp
140
141 (
142 cd $content
143 find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
144 )
145
146 echo -n "Running the test: "
147 pids=""
148 fstrim_loop &
149 fstrim_pid=$!
150 p=1
151 while [ $p -le $nproc ]; do
152         run_process $p &
153         pids="$pids $!"
154         p=$(($p+1))
155 done
156 echo "done."
157
158 wait $pids
159 kill $fstrim_pid
160 wait $fstrim_pid
161
162 status=0
163
164 exit