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