btrfs: make all btrfs tests that exercise balance use _run_btrfs_balance_start()
[xfstests-dev.git] / tests / btrfs / 187
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 187
6 #
7 # Stress send running in parallel with balance and deduplication against files
8 # that belong to the snapshots used by send. The goal is to verify that these
9 # operations running in parallel do not lead to send crashing (trigger assertion
10 # failures and BUG_ONs), or send finding an inconsistent snapshot that leads to
11 # a failure (reported in dmesg/syslog). The test needs big trees (snapshots)
12 # with large differences between the parent and send snapshots in order to hit
13 # such issues with a good probability.
14 #
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/attr
31 . ./common/filter
32 . ./common/reflink
33
34 # real QA test starts here
35 _supported_fs btrfs
36 _supported_os Linux
37 _require_scratch_dedupe
38 _require_attrs
39
40 rm -f $seqres.full
41
42 # We at least need 8GB of free space on $SCRATCH_DEV
43 _require_scratch_size $((8 * 1024 * 1024))
44
45 _scratch_mkfs >>$seqres.full 2>&1
46 _scratch_mount
47
48 dedupe_two_files()
49 {
50         trap "wait; exit" SIGTERM
51
52         local f1=$(find $SCRATCH_MNT/snap1 -type f | shuf -n 1)
53         local f2=$(find $SCRATCH_MNT/snap2 -type f | shuf -n 1)
54
55         if (( RANDOM % 2 )); then
56                 local tmp=$f1
57                 f1=$f2
58                 f2=$tmp
59         fi
60
61         # Ignore errors from dedupe. We just want to test for crashes and
62         # deadlocks.
63         $XFS_IO_PROG -r -c "dedupe $f1 0 0 64K" $f2 &> /dev/null
64 }
65
66 dedupe_files_loop()
67 {
68         trap "wait; exit" SIGTERM
69
70         while true; do
71                 for ((i = 1; i <= 5; i++)); do
72                         dedupe_two_files &
73                 done
74                 wait
75         done
76 }
77
78 balance_loop()
79 {
80         trap "wait; exit" SIGTERM
81
82         while true; do
83                 # Balance only metadata block groups, since this is makes it
84                 # easier to hit problems (crashes and errors) in send.
85                 # Ignore errors from balance. We just want to test for crashes
86                 # and deadlocks.
87                 _run_btrfs_balance_start -f -m $SCRATCH_MNT &> /dev/null
88                 sleep $((RANDOM % 3))
89         done
90 }
91
92 full_send_loop()
93 {
94         trap "wait; exit" SIGTERM
95
96         local count=$1
97
98         for ((i = 1; i <= $count; i++)); do
99                 # Ignore errors from send. We will check for errors later in
100                 # dmesg/syslog.
101                 $BTRFS_UTIL_PROG send -f /dev/null \
102                         $SCRATCH_MNT/snap1 &> /dev/null
103                 sleep $((RANDOM % 3))
104         done
105 }
106
107 inc_send_loop()
108 {
109         trap "wait; exit" SIGTERM
110
111         local count=$1
112
113         for ((i = 1; i <= $count; i++)); do
114                 # Ignore errors from send. We will check for errors later in
115                 # dmesg/syslog.
116                 $BTRFS_UTIL_PROG send -f /dev/null \
117                         -p $SCRATCH_MNT/snap1 $SCRATCH_MNT/snap2 &> /dev/null
118                 sleep $((RANDOM % 3))
119         done
120 }
121
122 write_files_loop()
123 {
124         local count=$1
125         local offset=$2
126
127         for ((i = 1; i <= $count; i++)); do
128                 $XFS_IO_PROG -f -c "pwrite -S 0xea 0 64K" \
129                         $SCRATCH_MNT/file_$((i + offset)) >/dev/null
130         done
131 }
132
133 set_xattrs_loop()
134 {
135         local count=$1
136         local offset=$2
137
138         for ((i = 1; i <= $count; i++)); do
139                 $SETFATTR_PROG -n 'user.x1' -v $xattr_value \
140                         $SCRATCH_MNT/file_$((i + offset))
141         done
142 }
143
144 # Number of files created before first snapshot. Must be divisable by 4.
145 nr_initial_files=40000
146 # Number of files created after the first snapshot. Must be divisable by 4.
147 nr_more_files=40000
148
149 # Create initial files.
150 step=$((nr_initial_files / 4))
151 for ((n = 0; n < 4; n++)); do
152         offset=$((step * $n))
153         write_files_loop $step $offset &
154         create_pids[$n]=$!
155 done
156 wait ${create_pids[@]}
157
158 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/snap1 \
159         | _filter_scratch
160
161 # Add some more files, so that that are substantial differences between the
162 # two test snapshots used for an incremental send later.
163
164 # Create more files.
165 step=$((nr_more_files / 4))
166 for ((n = 0; n < 4; n++)); do
167         offset=$((nr_initial_files + step * $n))
168         write_files_loop $step $offset &
169         create_pids[$n]=$!
170 done
171 wait ${create_pids[@]}
172
173 # Add some xattrs to all files, so that every leaf and node of the fs tree is
174 # COWed. Adding more files does only adds leafs and nodes to the tree's right
175 # side, since inode numbers are based on a counter and form the first part
176 # (objectid) of btree keys (we only modifying the right most leaf of the tree).
177 # Use large values for the xattrs to quickly increase the height of the tree.
178 xattr_value=$(printf '%0.sX' $(seq 1 3800))
179
180 # Split the work into 4 workers working on consecutive ranges to avoid contention
181 # on the same leafs as much as possible.
182 step=$(((nr_more_files + nr_initial_files) / 4))
183 for ((n = 0; n < 4; n++)); do
184         offset=$((step * $n))
185         set_xattrs_loop $step $offset &
186         setxattr_pids[$n]=$!
187 done
188 wait ${setxattr_pids[@]}
189
190 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/snap2 \
191         | _filter_scratch
192
193 full_send_loop 5 &
194 full_send_pid=$!
195
196 inc_send_loop 10 &
197 inc_send_pid=$!
198
199 dedupe_files_loop &
200 dedupe_pid=$!
201
202 balance_loop &
203 balance_pid=$!
204
205 wait $full_send_pid
206 wait $inc_send_pid
207
208 kill $dedupe_pid
209 wait $dedupe_pid
210
211 kill $balance_pid
212 wait $balance_pid
213
214 # Check for errors messages that happen due to inconsistent snapshot caused by
215 # deduplication and balance running in parallel with send, causing btree nodes
216 # and leafs to disappear and getting reused while send is using them.
217 #
218 # Example messages:
219 #
220 # BTRFS error (device sdc): did not find backref in send_root. inode=63292, \
221 #     offset=0, disk_byte=5228134400 found extent=5228134400
222 #
223 # BTRFS error (device sdc): parent transid verify failed on 32243712 wanted 24 \
224 #     found 27
225 #
226 _dmesg_since_test_start | egrep -e '\bBTRFS error \(device .*?\):'
227
228 status=0
229 exit