common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / btrfs / 011
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2013 STRATO.  All rights reserved.
4 #
5 # FSQA Test No. btrfs/011
6 #
7 # Test of the btrfs replace operation.
8 #
9 # The amount of tests done depends on the number of devices in the
10 # SCRATCH_DEV_POOL. For full test coverage, at least 5 devices should
11 # be available (e.g. 5 partitions).
12 #
13 # The source and target devices for the replace operation are
14 # arbitrarily chosen out of SCRATCH_DEV_POOl. Since the target device
15 # mustn't be smaller than the source device, the requirement for this
16 # test is that all devices have _exactly_ the same size. If this is
17 # not the case, this test is not run.
18 #
19 # To check the filesystems after replacing a device, a scrub run is
20 # performed, a btrfsck run, and finally the filesystem is remounted.
21 #
22 seq=`basename $0`
23 seqres=$RESULT_DIR/$seq
24 echo "QA output created by $seq"
25
26 here=`pwd`
27 tmp=/tmp/$$
28 status=1
29 noise_pid=0
30
31 _cleanup()
32 {
33         if [ $noise_pid -ne 0 ] && ps -p $noise_pid | grep -q $noise_pid; then
34                 kill -TERM $noise_pid
35         fi
36         wait
37         rm -f $tmp.*
38         # we need this umount and couldn't rely on _require_scratch to umount
39         # it from next test, because we would replace SCRATCH_DEV, which is
40         # needed by _require_scratch, and make it umounted.
41         _scratch_unmount > /dev/null 2>&1
42 }
43 trap "_cleanup; exit \$status" 0 1 2 3 15
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48
49 # real QA test starts here
50 _supported_fs btrfs
51 _require_scratch_nocheck
52 _require_scratch_dev_pool 5
53 _require_scratch_dev_pool_equal_size
54 _require_command "$WIPEFS_PROG" wipefs
55
56 rm -f $seqres.full
57 rm -f $tmp.*
58
59 echo "*** test btrfs replace"
60
61 # In seconds
62 wait_time=1
63
64 fill_scratch()
65 {
66         local fssize=$1
67         local filler_pid
68
69         # Fill inline extents.
70         for i in `seq 1 500`; do
71                 _ddt of=$SCRATCH_MNT/s$i bs=3800 count=1
72         done > /dev/null 2>&1
73
74         # Fill data extents.
75         for i in `seq 1 500`; do
76                 _ddt of=$SCRATCH_MNT/l$i bs=16385 count=1
77         done > /dev/null 2>&1
78         _ddt of=$SCRATCH_MNT/t0 bs=1M count=1 > /dev/null 2>&1
79         for i in `seq $fssize`; do
80                 cp $SCRATCH_MNT/t0 $SCRATCH_MNT/t$i || _fail "cp failed"
81         done > /dev/null 2>> $seqres.full
82
83         # Ensure we have enough data so that dev-replace would take at least
84         # 2 * $wait_time, allowing we cancel the running replace.
85         # Some extra points:
86         # - Use XFS_IO_PROG instead of dd
87         #   fstests wraps dd, making it pretty hard to kill the real dd pid
88         # - Use 64K block size with Direct IO
89         #   64K is the same stripe size used in replace/scrub. Using Direct IO
90         #   ensure the IO speed is near device limit and comparable to replace
91         #   speed.
92         $XFS_IO_PROG -f -d -c "pwrite -b 64k 0 1E" "$SCRATCH_MNT/t_filler" &>\
93                 $tmp.filler_result &
94         filler_pid=$!
95         sleep $((2 * $wait_time))
96         kill -KILL $filler_pid &> /dev/null
97         wait $filler_pid &> /dev/null
98
99         # If the system is too fast and the fs is too small, then skip the test
100         if grep -q "No space left" $tmp.filler_result; then
101                 ls -alh $SCRATCH_MNT >> $seqres.full
102                 cat $tmp.filler_result >> $seqres.full
103                 _notrun "fs too small for this test"
104         fi
105         cat $tmp.filler_result
106         sync; sync
107 }
108
109 workout()
110 {
111         local mkfs_options="$1"
112         local num_devs4raid="$2"
113         local with_cancel="$3"
114         local fssize="$4"
115         local source_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $1}'`"
116         local quick="quick"
117
118         [[ $fssize != 64 ]] && quick="thorough"
119
120         echo -e "\\n---------workout \"$1\" $2 $3 $4-----------" >> $seqres.full
121
122         $WIPEFS_PROG -a $SCRATCH_DEV_POOL > /dev/null 2>&1
123         _scratch_dev_pool_get $num_devs4raid
124         _spare_dev_get
125
126         _scratch_pool_mkfs $mkfs_options >> $seqres.full 2>&1 ||\
127                 _fail "mkfs failed"
128
129         _scratch_mount
130         _require_fs_space $SCRATCH_MNT $((2 * 512 * 1024)) #2.5G
131
132         fill_scratch $fssize
133         _run_btrfs_util_prog filesystem show -m $SCRATCH_MNT
134
135         echo -e "Replace from $source_dev to $SPARE_DEV\\n" >> $seqres.full
136         btrfs_replace_test $source_dev $SPARE_DEV "" $with_cancel $quick
137
138         _run_btrfs_util_prog filesystem show -m $SCRATCH_MNT
139
140         # Skip -r test for configs without mirror OR replace cancel
141         if echo $mkfs_options | egrep -qv "raid1|raid5|raid6|raid10" || \
142            [ "${with_cancel}Q" = "cancelQ" ]; then
143                 _scratch_unmount > /dev/null 2>&1
144                 _scratch_dev_pool_put
145                 _spare_dev_put
146                 return 0
147         fi
148
149         # Due to above replace, now SPARE_DEV is part of the FS, check that.
150         $BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT |\
151                 grep -qs $SPARE_DEV$ ||\
152                 _fail "$SPARE_DEV is not part of SCRATCH_FS"
153
154         btrfs_replace_test $SPARE_DEV $source_dev "-r" $with_cancel $quick
155
156         _scratch_unmount > /dev/null 2>&1
157         _scratch_dev_pool_put
158         _spare_dev_put
159 }
160
161 btrfs_replace_test()
162 {
163         local source_dev="$1"
164         local target_dev="$2"
165         local replace_options="$3"
166         local with_cancel="$4"
167         local quick="$5"
168
169         # generate some (slow) background traffic in parallel to the
170         # replace operation. It is not a problem if cat fails early
171         # with ENOSPC.
172         cat /dev/urandom | od > $SCRATCH_MNT/noise 2>> $seqres.full &
173         noise_pid=$!
174
175         if [ "${with_cancel}Q" = "cancelQ" ]; then
176                 # background the replace operation (no '-B' option given)
177                 _run_btrfs_util_prog replace start -f $replace_options $source_dev $target_dev $SCRATCH_MNT
178                 sleep $wait_time
179                 _run_btrfs_util_prog replace cancel $SCRATCH_MNT
180
181                 # 'replace status' waits for the replace operation to finish
182                 # before the status is printed
183                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
184                 cat $tmp.tmp >> $seqres.full
185                 grep -q canceled $tmp.tmp || _fail "btrfs replace status (canceled) failed"
186         else
187                 if [ "${quick}Q" = "thoroughQ" ]; then
188                         # The thorough test runs around 2 * $wait_time seconds.
189                         # This is a chance to force a sync in the middle of the
190                         # replace operation.
191                         (sleep $wait_time; sync) > /dev/null 2>&1 &
192                 fi
193                 _run_btrfs_util_prog replace start -Bf $replace_options $source_dev $target_dev $SCRATCH_MNT
194
195                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
196                 cat $tmp.tmp >> $seqres.full
197                 grep -q finished $tmp.tmp || _fail "btrfs replace status (finished) failed"
198         fi
199
200         if ps -p $noise_pid | grep -q $noise_pid; then
201                 kill -TERM $noise_pid 2> /dev/null
202         fi
203         noise_pid=0
204         wait
205
206         # scrub tests on-disk data, that's the reason for the sync.
207         # With the '-B' option (don't background), any type of error causes
208         # exit values != 0, including detected correctable and uncorrectable
209         # errors on the device.
210         sync; sync
211         _run_btrfs_util_prog scrub start -B $SCRATCH_MNT
212
213         # Two tests are performed, the 1st is to btrfsck the filesystem,
214         # and the 2nd test is to mount the filesystem.
215         # Usually _check_btrfs_filesystem would perform the mount test,
216         # but it gets confused by the mount output that shows SCRATCH_MNT
217         # mounted but not being mounted to SCRATCH_DEV. This happens
218         # because in /proc/mounts the 2nd device of the filesystem is
219         # shown after the replace operation. Let's just do the mount
220         # test manually after _check_btrfs_filesystem is finished.
221         _scratch_unmount > /dev/null 2>&1
222         if [ "${with_cancel}Q" != "cancelQ" ]; then
223                 # after the replace operation, use the target_dev for everything
224                 echo "_check_btrfs_filesystem $target_dev" >> $seqres.full
225                 _check_btrfs_filesystem $target_dev
226                 _mount -t $FSTYP `_scratch_mount_options | sed "s&${SCRATCH_DEV}&${target_dev}&"`
227         else
228                 _check_btrfs_filesystem $source_dev
229                 _scratch_mount
230         fi
231 }
232
233 workout "-m single -d single" 1 no 64
234 workout "-m single -d single -M" 1 no 64
235 workout "-m dup -d single" 1 no 64
236 workout "-m dup -d single" 1 cancel 1024
237 workout "-m dup -d dup -M" 1 no 64
238 workout "-m raid0 -d raid0" 2 no 64
239 workout "-m raid1 -d raid1" 2 no 2048
240 workout "-m raid5 -d raid5" 2 no 64
241 workout "-m raid6 -d raid6" 3 no 64
242 workout "-m raid10 -d raid10" 4 no 64
243
244 echo "*** done"
245 status=0
246 exit