xfs/530: skip test if user MKFS_OPTIONS screw up formatting
[xfstests-dev.git] / tests / xfs / 016
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 016
6 #
7 # test end of log overwrite bug #796141
8 #
9 #
10 # pv 796141
11 #
12 # create a new FS, mostly fill the log. Then wrap the log back to the
13 # start bit by bit to force wiping of stale blocks near the end of the
14 # log. Check the block after the log ends to check for corruption
15 #
16 # assumptions :
17 #    - given we're only touching a single inode, the block after the
18 #      log which is in the middle ag should never be touched.
19 #      if it changes, we assume the log is writing over it
20 #
21
22 . ./common/preamble
23 _begin_fstest rw auto quick
24
25 # Override the default cleanup function.
26 _cleanup()
27 {
28     cd /
29     rm -f $tmp.*
30     echo "*** unmount"
31     _scratch_unmount 2>/dev/null
32 }
33
34 _block_filter()
35 {
36     sed -e 's/[0-9][0-9]*\.\.[0-9][0-9]*/BLOCKRANGE/g'
37 }
38
39 _init()
40 {
41     echo "*** determine log size"
42     local sz_mb=50
43     local dsize="-d size=${sz_mb}m"
44     local lsize="-l size=$(_scratch_find_xfs_min_logblocks $dsize)b"
45     local force_opts="$dsize $lsize"
46     _scratch_mkfs_xfs $force_opts >> $seqres.full 2>&1
47
48     # set log_size and log_size_bb globally
49     log_size_bb=`_log_size`
50     log_size=$((log_size_bb * 512))
51     echo "log_size_bb = $log_size_bb log_size = $log_size" >> $seqres.full
52
53     echo "*** reset partition"
54     $here/src/devzero -b 2048 -n $sz_mb -v 198 $SCRATCH_DEV # write 0xc6
55     echo "*** mkfs"
56     #
57     # Do not discard blocks as we check for patterns in free space.
58     # 
59     # First, make sure that mkfs supports '-K' option by using its
60     # dry run (-N option) and then add it to the force_opts.
61     #
62     if _scratch_mkfs_xfs -N -K $force_opts >/dev/null 2>&1; then
63         force_opts="-K $force_opts"
64     fi
65     echo mkfs_xfs $force_opts $SCRATCH_DEV >>$seqres.full
66     _scratch_mkfs_xfs $force_opts >$tmp.mkfs0 2>&1
67     [ $? -ne 0 ] && \
68         _notrun "Cannot mkfs for this test using MKFS_OPTIONS specified"
69     _filter_mkfs <$tmp.mkfs0 >/dev/null 2>$tmp.mkfs
70     . $tmp.mkfs
71     [ $logsunit -ne 0 ] && \
72         _notrun "Cannot run this test using log MKFS_OPTIONS specified"
73
74     # quotas generate extra log traffic so force it off
75     _qmount_option noquota
76 }
77
78 _log_traffic()
79 {
80     count=$1
81     echo "*** generate log traffic"
82
83     out=$SCRATCH_MNT/$$.tmp
84
85     echo "   *** mount"
86     if ! _try_scratch_mount
87     then
88         echo "failed to mount $SCRATCH_DEV"
89         exit 1
90     fi
91
92     # having any quota enabled (acct/enfd) means extra log traffic - evil!
93     $here/src/feature -U $SCRATCH_DEV && \
94                 _notrun "UQuota are enabled, test needs controlled log traffic"
95     $here/src/feature -G $SCRATCH_DEV && \
96                 _notrun "GQuota are enabled, test needs controlled log traffic"
97     $here/src/feature -P $SCRATCH_DEV && \
98                 _notrun "PQuota are enabled, test needs controlled log traffic"
99
100     echo "   *** fiddle"
101     while [ $count -ge 0 ]
102     do
103         touch $out
104         sync
105         rm $out
106         sync
107         let "count = count - 1"
108     done
109
110     echo "   *** unmount"
111     if ! _scratch_unmount
112     then
113         echo "failed to unmount $SCRATCH_DEV"
114         exit 1
115     fi
116 }
117
118 _log_size()
119 {
120     _scratch_xfs_logprint -tb | $AWK_PROG '
121         /log file: / || /log device: / { print $7}
122     '
123 }
124
125 _log_head()
126 {
127     _scratch_xfs_logprint -tb | $AWK_PROG '
128         /head:/ { print $5 }
129     '
130 }
131
132 # Get log stripe unit for v2 logs; if none specified,
133 # (or v1 log) just return "1" block
134
135 _log_sunit()
136 {
137     if [ ${lsunit:-0} -eq 0 ]; then
138         echo $dbsize
139     else
140         expr $lsunit \* $dbsize
141     fi
142 }
143
144 _after_log()
145 {
146     _scratch_xfs_db -r -c "sb" -c "print" | $AWK_PROG '
147         /logstart/  { logstart = $3 }
148         /logblocks/ { logblocks = $3 }
149         END {
150             print logstart + logblocks
151         }
152     '
153 }
154
155 _check_corrupt()
156 {
157     f="c6c6c6c6"
158     echo "*** check for corruption"
159     echo "expect $f..." >>$seqres.full
160     _scratch_xfs_db -r -c "fsblock $2" -c "print" | head | tee -a $seqres.full | \
161         grep -q -v "$f $f $f $f $f $f $f $f" && \
162             _fail "!!! block $2 corrupted!"
163 }
164
165 # Import common functions.
166 . ./common/filter
167 . ./common/quota
168
169 # real QA test starts here
170 _supported_fs xfs
171
172 _require_scratch
173 _init
174
175 block=`_after_log $SCRATCH_DEV`
176 echo "fsblock after log = $block"               >>$seqres.full
177 _check_corrupt $SCRATCH_DEV $block
178
179 actual_log_size=`_log_size`
180 echo "log size = $actual_log_size BB"                      >>$seqres.full
181 head=`_log_head`
182 echo "log position = $head"                     >>$seqres.full
183 lsunit=`_log_sunit`
184 echo "log sunit = $lsunit"                      >>$seqres.full
185
186 # sanity checks
187 [ $actual_log_size -eq $log_size_bb ] || \
188     _fail "!!! unexpected log size $size"
189 [ $head -eq 2 -o $head -eq $((lsunit/512)) ] || \
190     _fail "!!! unexpected initial log position $head vs. $((lsunit/512))"
191
192 # Step 1: Run 200 ops to estimate how how many log blocks are used for each op.
193 # Ignore the fact that it will also include an unmount record; this should be
194 # small overall.
195 echo "    lots of traffic for sampling" >>$seqres.full
196 sample_size_ops=200
197 _log_traffic $sample_size_ops
198 head1=`_log_head`
199 num_blocks=`expr $head1 - $head`
200 blocks_per_op=`echo "scale=3; $num_blocks / $sample_size_ops" | bc`
201 echo "log position = $head1; old log position: $head" >> $seqres.full
202 echo "blocks_per_op = $blocks_per_op" >>$seqres.full
203
204 # Step 2: Quickly advance the log from wherever step 1 left us to the point
205 # where the log is now 80% full on its first cycle.
206
207 # Estimate the number of ops needed to get the log head close to but not past
208 # near_end_min for a single mount.  We'd rather fall short and have to step our
209 # way closer to the end than run past the end, so our target for this second
210 # step is to fill 80% of the first cycle of the log.
211 num_expected_ops=$(( 8 * $(echo "$log_size_bb / $blocks_per_op" | bc) / 10))
212 echo "num_expected_ops = $num_expected_ops" >>$seqres.full
213
214 # Compute the number of ops needed to get from wherever we are right now in
215 # the log cycle to the 80% point.
216 num_expected_to_go=`echo "$num_expected_ops - $sample_size_ops" | bc`
217 echo "num_expected_to_go = $num_expected_to_go" >>$seqres.full
218
219 echo "    lots more traffic" >>$seqres.full
220 _log_traffic $num_expected_to_go
221 head=`_log_head`
222 echo "log position = $head"                     >>$seqres.full
223
224 # Step 3: Gradually advance log traffic to get us from wherever step 2 left us
225 # to the point where the log is within approximately 20 ops of wrapping into
226 # the second cycle.
227
228 # Since this is a log wrapping test, it's critical to push the log head to the
229 # point where it will wrap around within twenty rounds of ops.  Compute the
230 # expected value of the log head when we get to this point.  This "/ 1" piece
231 # tricks bc into printing integer numbers.
232 near_end_min=$(echo "$log_size_bb - (20 * $blocks_per_op / 1)" | bc)
233 echo "near_end_min = $near_end_min" >>$seqres.full
234
235 # Step us (in batches of 10 ops) to our goal.
236 while [ $head -lt $near_end_min ]; do
237         echo "    bump traffic from $head towards $near_end_min" >> $seqres.full
238         _log_traffic 10 > /dev/null 2>&1
239         head=$(_log_head)
240 done
241
242 [ $head -gt $near_end_min -a $head -lt $log_size_bb ] || \
243     _fail "!!! unexpected near end log position $head"
244
245 # Step 4: Try to wrap the log, checking for corruption with each advance.
246 # This is the functionality that we're actually trying to test.  We will try
247 # 40 ops (in batches of 2) to try to wrap the log.
248 for c in `seq 0 20`
249 do
250     echo "   little traffic"            >>$seqres.full
251     _log_traffic 2
252     head=`_log_head`
253     echo "log position = $head"         >>$seqres.full
254     _check_corrupt $SCRATCH_DEV $block
255 done
256
257 [ $head -lt 1000 ] || \
258     _fail "!!! unexpected wrapped log position $head"
259
260 # success, all done
261 status=0
262 exit