misc: tag all tests that examine crash recovery in a loop
[xfstests-dev.git] / tests / btrfs / 192
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test 192
6 #
7 # Test btrfs consistency after each FUA for a workload with snapshot creation
8 # and removal
9 #
10 . ./common/preamble
11 _begin_fstest auto replay snapshot stress recoveryloop
12
13 # Override the default cleanup function.
14 _cleanup()
15 {
16         cd /
17         kill -q $pid1 &> /dev/null
18         kill -q $pid2 &> /dev/null
19         "$KILLALL_PROG" -q $FSSTRESS_PROG &> /dev/null
20         wait
21         _log_writes_cleanup &> /dev/null
22         rm -f $tmp.*
23 }
24
25 # Import common functions.
26 . ./common/filter
27 . ./common/attr
28 . ./common/dmlogwrites
29
30 # real QA test starts here
31
32 # Modify as appropriate.
33 _supported_fs btrfs
34
35 _require_command "$KILLALL_PROG" killall
36 _require_command "$BLKDISCARD_PROG" blkdiscard
37 _require_btrfs_fs_feature "no_holes"
38 _require_btrfs_mkfs_feature "no-holes"
39 _require_log_writes
40 _require_scratch
41 _require_attrs
42
43 # We require a 4K nodesize to ensure the test isn't too slow
44 if [ $(get_page_size) -ne 4096 ]; then
45         _notrun "This test doesn't support non-4K page size yet"
46 fi
47
48 runtime=30
49 nr_cpus=$("$here/src/feature" -o)
50 # cap nr_cpus to 8 to avoid spending too much time on hosts with many cpus
51 if [ $nr_cpus -gt 8 ]; then
52         nr_cpus=8
53 fi
54 fsstress_args=$(_scale_fsstress_args -w -d $SCRATCH_MNT -n 99999 -p $nr_cpus \
55                 $FSSTRESS_AVOID)
56 _log_writes_init $SCRATCH_DEV
57
58 # Discard the whole devices so when some tree pointer is wrong, it won't point
59 # to some older valid tree blocks, so we can detect it.
60 $BLKDISCARD_PROG $LOGWRITES_DMDEV > /dev/null 2>&1
61
62 # Use no-holes to avoid warnings of missing file extent items (expected
63 # for holes due to mix of buffered and direct IO writes).
64 # And use 4K nodesize to bump tree height.
65 _log_writes_mkfs -O no-holes -n 4k >> $seqres.full
66 _log_writes_mount
67
68 $BTRFS_UTIL_PROG subvolume create $SCRATCH_MNT/src > /dev/null
69 mkdir -p $SCRATCH_MNT/snapshots
70 mkdir -p $SCRATCH_MNT/src/padding
71
72 random_file()
73 {
74         local basedir=$1
75         echo "$basedir/$(ls $basedir | sort -R | tail -1)"
76 }
77
78 snapshot_workload()
79 {
80         trap "wait; exit" SIGTERM
81
82         local i=0
83         while true; do
84                 $BTRFS_UTIL_PROG subvolume snapshot \
85                         $SCRATCH_MNT/src $SCRATCH_MNT/snapshots/$i \
86                         > /dev/null
87                 # Do something small to make snapshots different
88                 rm -f "$(random_file $SCRATCH_MNT/src/padding)"
89                 rm -f "$(random_file $SCRATCH_MNT/src/padding)"
90                 touch "$(random_file $SCRATCH_MNT/src/padding)"
91                 touch "$SCRATCH_MNT/src/padding/random_$RANDOM"
92
93                 i=$(($i + 1))
94                 sleep 1
95         done
96 }
97
98 delete_workload()
99 {
100         trap "wait; exit" SIGTERM
101
102         while true; do
103                 sleep 2
104                 $BTRFS_UTIL_PROG subvolume delete \
105                         "$(random_file $SCRATCH_MNT/snapshots)" \
106                         > /dev/null 2>&1
107         done
108 }
109
110 # Replay and check each fua/flush (specified by $2) point.
111 #
112 # Since dm-log-writes records bio sequentially, even just replaying a range
113 # still needs to iterate all records before the end point.
114 # When number of records grows, it will be unacceptably slow, thus we need
115 # to use relay-log itself to trigger fsck, avoid unnecessary seek.
116 log_writes_fast_replay_check()
117 {
118         local check_point=$1
119         local blkdev=$2
120         local fsck_command="$BTRFS_UTIL_PROG check $blkdev"
121         local ret
122
123         [ -z "$check_point" -o -z "$blkdev" ] && _fail \
124         "check_point and blkdev must be specified for log_writes_fast_replay_check"
125
126         $here/src/log-writes/replay-log --log $LOGWRITES_DEV \
127                 --replay $blkdev --check $check_point --fsck "$fsck_command" \
128                 &> $tmp.full_fsck
129         ret=$?
130         tail -n 150 $tmp.full_fsck > $seqres.full
131         [ $ret -ne 0 ] && _fail "fsck failed during replay"
132 }
133
134 xattr_value=$(printf '%0.sX' $(seq 1 3800))
135
136 # Bumping tree height to level 2.
137 for ((i = 0; i < 64; i++)); do
138         touch "$SCRATCH_MNT/src/padding/$i"
139         $SETFATTR_PROG -n 'user.x1' -v $xattr_value "$SCRATCH_MNT/src/padding/$i"
140 done
141
142 _log_writes_mark prepare
143
144 snapshot_workload &
145 pid1=$!
146 delete_workload &
147 pid2=$!
148
149 "$FSSTRESS_PROG" $fsstress_args > /dev/null &
150 sleep $runtime
151
152 "$KILLALL_PROG" -q "$FSSTRESS_PROG" &> /dev/null
153 kill $pid1 &> /dev/null
154 kill $pid2 &> /dev/null
155 wait
156 _log_writes_unmount
157 _log_writes_remove
158
159 log_writes_fast_replay_check fua "$SCRATCH_DEV"
160
161 echo "Silence is golden"
162
163 # success, all done
164 status=0
165 exit