btrfs: test incremental send after renaming and linking file
[xfstests-dev.git] / tests / btrfs / 011
1 #! /bin/bash
2 # FSQA Test No. btrfs/011
3 #
4 # Test of the btrfs replace operation.
5 #
6 # The amount of tests done depends on the number of devices in the
7 # SCRATCH_DEV_POOL. For full test coverage, at least 5 devices should
8 # be available (e.g. 5 partitions).
9 #
10 # The source and target devices for the replace operation are
11 # arbitrarily chosen out of SCRATCH_DEV_POOl. Since the target device
12 # mustn't be smaller than the source device, the requirement for this
13 # test is that all devices have _exactly_ the same size. If this is
14 # not the case, this test is not run.
15 #
16 # To check the filesystems after replacing a device, a scrub run is
17 # performed, a btrfsck run, and finally the filesystem is remounted.
18 #
19 #-----------------------------------------------------------------------
20 # Copyright (C) 2013 STRATO.  All rights reserved.
21 #
22 # This program is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU General Public License as
24 # published by the Free Software Foundation.
25 #
26 # This program is distributed in the hope that it would be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write the Free Software Foundation,
33 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
34 #
35 #-----------------------------------------------------------------------
36 #
37
38 seq=`basename $0`
39 seqres=$RESULT_DIR/$seq
40 echo "QA output created by $seq"
41
42 here=`pwd`
43 tmp=/tmp/$$
44 status=1
45 noise_pid=0
46
47 _cleanup()
48 {
49         if [ $noise_pid -ne 0 ] && ps -p $noise_pid | grep -q $noise_pid; then
50                 kill -TERM $noise_pid
51         fi
52         wait
53         rm -f $tmp.tmp
54 }
55 trap "_cleanup; exit \$status" 0 1 2 3 15
56
57 # get standard environment, filters and checks
58 . ./common/rc
59 . ./common/filter
60
61 # real QA test starts here
62 _supported_fs btrfs
63 _require_scratch_nocheck
64 _require_scratch_dev_pool 4
65 _require_btrfs_dump_super
66
67 rm -f $seqres.full
68 rm -f $tmp.tmp
69
70 echo "*** test btrfs replace"
71
72 workout()
73 {
74         local mkfs_options="$1"
75         local num_devs4raid="$2"
76         local with_cancel="$3"
77         local quick="$4"
78         local source_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $1}'`"
79         local target_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $NF}'`"
80         local fssize
81
82         echo >> $seqres.full
83         echo "---------workout \"$1\" $2 $3 $4-----------" >> $seqres.full
84
85         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt `expr $num_devs4raid + 1` ]; then
86                 echo "Skip workout $1 $2 $3 $4" >> $seqres.full
87                 echo "Too few devices in SCRATCH_DEV_POOL $SCRATCH_DEV_POOL, required: " `expr $num_devs4raid + 1` >> $seqres.full
88                 return 0
89         fi
90
91         # use min number of disks in order to fill up the disk to replace
92         # as much as possible
93         local used_devs_without_1st="`echo $SCRATCH_DEV_POOL | \
94                 awk '{ORS=\" \"; for (i = 2; i <= (NF - 1 < '$num_devs4raid' ? NF - 1 : '$num_devs4raid'); i++) print $i}'`"
95
96         # _scratch_mkfs adds the 1st device again (which is $SCRATCH_DEV)
97         _scratch_mkfs $mkfs_options $used_devs_without_1st >> $seqres.full 2>&1 || _fail "mkfs failed"
98
99         # create a filesystem on the target device just for the sake of
100         # being able to query its size with btrfs-show-super
101         $MKFS_BTRFS_PROG $MKFS_OPTIONS $target_dev >> $seqres.full 2>&1 || _fail "mkfs target_dev failed"
102
103         # The source and target devices for the replace operation are
104         # arbitrarily chosen out of the pool. Since the target device mustn't
105         # be smaller than the source device, the requirement for this test is
106         # that all devices have _exactly_ the same size. If this is not the
107         # case, this test is not run.
108         local num_lines=`$BTRFS_SHOW_SUPER_PROG $SCRATCH_DEV $used_devs_without_1st $target_dev | grep dev_item.total_bytes | uniq | wc -l`
109         if [ $num_lines -gt 1 ]; then
110                 _notrun "Different device sizes detected"
111         fi
112
113         if [ `$BTRFS_SHOW_SUPER_PROG $SCRATCH_DEV | grep dev_item.total_bytes | awk '{print $2}'` -lt 2500000000 ]; then
114                 _notrun "device size too small"
115         fi
116
117         _scratch_mount
118
119         echo "$BTRFS_UTIL_PROG filesystem show" >> $seqres.full
120         $BTRFS_UTIL_PROG filesystem show >> $seqres.full
121
122         # Generate metadata and some minimal user data, generate 500 times
123         # 20K extents in the data chunk and fill up metadata with inline
124         # extents.
125         for i in `seq 1 500`; do
126                 _ddt of=$SCRATCH_MNT/l$i bs=16385 count=1
127                 _ddt of=$SCRATCH_MNT/s$i bs=3800 count=1
128         done > /dev/null 2>&1
129
130         # Generate a template once and quickly copy it multiple times.
131         _ddt of=$SCRATCH_MNT/t0 bs=1M count=1 > /dev/null 2>&1
132
133         if [ "${quick}Q" = "thoroughQ" ]; then
134                 # The intention of this "thorough" test is to increase
135                 # the probability of random errors, in particular in
136                 # conjunction with the background noise generator and
137                 # a sync call while the replace operation is ongoing.
138                 fssize=2048
139         elif [ "${with_cancel}Q" = "cancelQ" ]; then
140                 # The goal is to produce enough data to prevent that the
141                 # replace operation finishes before the cancel request
142                 # is started.
143                 fssize=1024
144         else
145                 fssize=64
146         fi
147
148         # since the available size was tested before, do not tolerate
149         # any failures
150         for i in `seq $fssize`; do
151                 cp $SCRATCH_MNT/t0 $SCRATCH_MNT/t$i || _fail "cp failed"
152         done > /dev/null 2>> $seqres.full
153         sync; sync
154
155         btrfs_replace_test $source_dev $target_dev "" $with_cancel $quick
156         _scratch_unmount > /dev/null 2>&1
157
158         if echo $mkfs_options | egrep -qv "raid1|raid5|raid6|raid10" || \
159            [ "${with_cancel}Q" = "cancelQ" ]; then
160                 # the -r option has no effect without mirrors, skip -r test
161                 # in this case, and if only the canceling should be tested
162                 # as well
163                 return 0
164         fi
165
166         # One more time with the '-r' option this time. Instead of wasting
167         # time to populate the filesystem with data again, use the
168         # existing filesystem in the state as it is after the previous
169         # replace operation.
170         # If possible, use a strategy to select the source and target
171         # device so that we really change bits on the target disk, see
172         # below.
173
174         # The default: For the 2nd run, the new target drive is the old
175         # source drive, and the new source drive is the old target drive.
176         # Since except for the noise data, the copied data is already on
177         # the new target disk (which is the old source disk), this is not
178         # optimal to check whether data is copied correctly.
179         local tmp_dev="$source_dev"
180         source_dev="$target_dev"
181         target_dev="$tmp_dev"
182
183         # If we have at least one more device in the SCRATCH_DEV_POOL than
184         # used so far, use one of those for the new target devive.
185         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -gt `expr $num_devs4raid + 1` ]; then
186                 target_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $(NF-1)}'`"
187         fi
188
189         # If the filesystem is built out of more than one devices, use a
190         # different source device for this round.
191         if [ $num_devs4raid -gt 1 ]; then
192                 source_dev="`echo ${SCRATCH_DEV_POOL} | awk '{print $2}'`"
193         fi
194
195         # Mount similar to _scratch_mount, but since the SCRATCH_DEV (the
196         # 1st device in SCRATCH_DEV_POOL) was replaced by the previous
197         # btrfs replace operation, substitute SCRATCH_DEV with a device
198         # that is known to be part of the SCRATCH_MNT filesystem.
199         _mount -t $FSTYP `_scratch_mount_options | sed "s&${SCRATCH_DEV}&${source_dev}&"`
200         if [ $? -ne 0 ]; then
201                 echo "mount failed"
202                 return 1
203         fi
204
205         btrfs_replace_test $source_dev $target_dev "-r" $with_cancel $quick
206         _scratch_unmount > /dev/null 2>&1
207 }
208
209 btrfs_replace_test()
210 {
211         local source_dev="$1"
212         local target_dev="$2"
213         local replace_options="$3"
214         local with_cancel="$4"
215         local quick="$5"
216
217         # generate some (slow) background traffic in parallel to the
218         # replace operation. It is not a problem if cat fails early
219         # with ENOSPC.
220         cat /dev/urandom | od > $SCRATCH_MNT/noise 2>> $seqres.full &
221         noise_pid=$!
222
223         if [ "${with_cancel}Q" = "cancelQ" ]; then
224                 # background the replace operation (no '-B' option given)
225                 echo "$BTRFS_UTIL_PROG replace start -f $replace_options $source_dev $target_dev $SCRATCH_MNT" >> $seqres.full
226                 $BTRFS_UTIL_PROG replace start -f $replace_options $source_dev $target_dev $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace start failed"
227                 sleep 1
228                 echo "$BTRFS_UTIL_PROG replace cancel $SCRATCH_MNT" >> $seqres.full
229                 $BTRFS_UTIL_PROG replace cancel $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace cancel failed"
230
231                 # 'replace status' waits for the replace operation to finish
232                 # before the status is printed
233                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
234                 cat $tmp.tmp >> $seqres.full
235                 grep -q canceled $tmp.tmp || _fail "btrfs replace status (canceled) failed"
236         else
237                 if [ "${quick}Q" = "thoroughQ" ]; then
238                         # On current hardware, the thorough test runs
239                         # more than a second. This is a chance to force
240                         # a sync in the middle of the replace operation.
241                         (sleep 1; sync) > /dev/null 2>&1 &
242                 fi
243                 echo "$BTRFS_UTIL_PROG replace start -Bf $replace_options $source_dev $target_dev $SCRATCH_MNT" >> $seqres.full
244                 $BTRFS_UTIL_PROG replace start -Bf $replace_options $source_dev $target_dev $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs replace start failed"
245
246                 $BTRFS_UTIL_PROG replace status $SCRATCH_MNT > $tmp.tmp 2>&1
247                 cat $tmp.tmp >> $seqres.full
248                 grep -q finished $tmp.tmp || _fail "btrfs replace status (finished) failed"
249         fi
250
251         if ps -p $noise_pid | grep -q $noise_pid; then
252                 kill -TERM $noise_pid 2> /dev/null
253         fi
254         noise_pid=0
255         wait
256
257         # scrub tests on-disk data, that's the reason for the sync.
258         # With the '-B' option (don't background), any type of error causes
259         # exit values != 0, including detected correctable and uncorrectable
260         # errors on the device.
261         sync; sync
262         $BTRFS_UTIL_PROG scrub start -B $SCRATCH_MNT >> $seqres.full 2>&1 || _fail "btrfs scrub failed"
263
264         # Two tests are performed, the 1st is to btrfsck the filesystem,
265         # and the 2nd test is to mount the filesystem.
266         # Usually _check_btrfs_filesystem would perform the mount test,
267         # but it gets confused by the mount output that shows SCRATCH_MNT
268         # mounted but not being mounted to SCRATCH_DEV. This happens
269         # because in /proc/mounts the 2nd device of the filesystem is
270         # shown after the replace operation. Let's just do the mount
271         # test manually after _check_btrfs_filesystem is finished.
272         _scratch_unmount > /dev/null 2>&1
273         if [ "${with_cancel}Q" != "cancelQ" ]; then
274                 # after the replace operation, use the target_dev for everything
275                 echo "$BTRFS_UTIL_PROG filesystem show -d" >> $seqres.full
276                 $BTRFS_UTIL_PROG filesystem show -d >> $seqres.full
277                 echo "$BTRFS_UTIL_PROG filesystem show" >> $seqres.full
278                 $BTRFS_UTIL_PROG filesystem show >> $seqres.full
279                 echo "_check_btrfs_filesystem $target_dev" >> $seqres.full
280                 _check_btrfs_filesystem $target_dev
281                 _mount -t $FSTYP `_scratch_mount_options | sed "s&${SCRATCH_DEV}&${target_dev}&"`
282         else
283                 _check_btrfs_filesystem $source_dev
284                 _scratch_mount
285         fi
286 }
287
288 workout "-m single -d single" 1 no quick
289 workout "-m single -d single -M" 1 no quick
290 workout "-m dup -d single" 1 no quick
291 workout "-m dup -d single" 1 cancel quick
292 workout "-m dup -d dup -M" 1 no quick
293 workout "-m raid0 -d raid0" 2 no quick
294 workout "-m raid1 -d raid1" 2 no thorough
295 workout "-m raid5 -d raid5" 2 no quick
296 workout "-m raid6 -d raid6" 3 no quick
297 workout "-m raid10 -d raid10" 4 no quick
298
299 echo "*** done"
300 status=0
301 exit