xfstests: add _require_freeze and minor cleanups
[xfstests-dev.git] / 016
1 #! /bin/bash
2 # FS QA Test No. 016
3 #
4 # test end of log overwrite bug #796141
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #
22 #-----------------------------------------------------------------------
23 #
24 # creator
25 owner=dxm@sgi.com
26
27 #
28 # pv 796141
29 #
30 # create a new FS, mostly fill the log. Then wrap the log back to the
31 # start bit by bit to force wiping of stale blocks near the end of the
32 # log. Check the block after the log ends to check for corruption
33 #
34 # assumptions :
35 #    - given we're only touching a single inode, the block after the
36 #      log which is in the middle ag should never be touched.
37 #      if it changes, we assume the log is writing over it
38 #
39
40 seq=`basename $0`
41 echo "QA output created by $seq"
42
43 here=`pwd`
44 tmp=/tmp/$$
45 status=1
46
47 trap "_cleanup; exit \$status" 0 1 2 3 15
48
49 _cleanup()
50 {
51     cd /
52     rm -f $tmp.*
53     echo "*** unmount"
54     umount $SCRATCH_MNT 2>/dev/null
55 }
56
57 _block_filter()
58 {
59     sed -e 's/[0-9][0-9]*\.\.[0-9][0-9]*/BLOCKRANGE/g'
60 }
61
62 _init()
63 {
64     echo "*** reset partition"
65     $here/src/devzero -b 2048 -n 50 -v 198 $SCRATCH_DEV
66     echo "*** mkfs"
67     force_opts="-dsize=50m -lsize=$log_size"
68     #
69     # Do not discard blocks as we check for patterns in free space.
70     # 
71     # First, make sure that mkfs supports '-K' option by using its
72     # dry run (-N option) and then add it to the force_opts.
73     #
74     if _scratch_mkfs_xfs -N -K $force_opts >/dev/null 2>&1; then
75         force_opts="-K $force_opts"
76     fi
77     echo mkfs_xfs $force_opts $SCRATCH_DEV >>$seq.full
78     _scratch_mkfs_xfs $force_opts >$tmp.mkfs0 2>&1
79     [ $? -ne 0 ] && \
80         _notrun "Cannot mkfs for this test using MKFS_OPTIONS specified"
81     _filter_mkfs <$tmp.mkfs0 >/dev/null 2>$tmp.mkfs
82     . $tmp.mkfs
83     [ $logsunit -ne 0 ] && \
84         _notrun "Cannot run this test using log MKFS_OPTIONS specified"
85 }
86
87 _log_traffic()
88 {
89     count=$1
90     echo "*** generate log traffic"
91
92     out=$SCRATCH_MNT/$$.tmp
93
94     echo "   *** mount"
95     if ! _scratch_mount
96     then
97         echo "failed to mount $SCRATCH_DEV"
98         exit 1
99     fi
100
101     # having any quota enabled (acct/enfd) means extra log traffic - evil!
102     $here/src/feature -U $SCRATCH_DEV && \
103                 _notrun "UQuota are enabled, test needs controlled log traffic"
104     $here/src/feature -G $SCRATCH_DEV && \
105                 _notrun "GQuota are enabled, test needs controlled log traffic"
106     $here/src/feature -P $SCRATCH_DEV && \
107                 _notrun "PQuota are enabled, test needs controlled log traffic"
108
109     echo "   *** fiddle"
110     while [ $count -ge 0 ]
111     do
112         touch $out
113         sync
114         rm $out
115         sync
116         let "count = count - 1"
117     done
118
119     echo "   *** unmount"
120     if ! umount $SCRATCH_DEV
121     then
122         echo "failed to unmount $SCRATCH_DEV"
123         exit 1
124     fi
125 }
126
127 _log_size()
128 {
129     _scratch_xfs_logprint -tb | $AWK_PROG '
130         /log file: / || /log device: / { print $7}
131     '
132 }
133
134 _log_head()
135 {
136     _scratch_xfs_logprint -tb | $AWK_PROG '
137         /head:/ { print $5 }
138     '
139 }
140
141 # Get log stripe unit for v2 logs; if none specified,
142 # (or v1 log) just return "1" block
143
144 _log_sunit()
145 {
146     if [ ${lsunit:-0} -eq 0 ]; then
147         echo $dbsize
148     else
149         expr $lsunit \* $dbsize
150     fi
151 }
152
153 _after_log()
154 {
155     xfs_db -r $1 -c "sb" -c "print" | $AWK_PROG '
156         /logstart/  { logstart = $3 }
157         /logblocks/ { logblocks = $3 }
158         END {
159             print logstart + logblocks
160         }
161     '
162 }
163
164 _check_corrupt()
165 {
166     f="c6c6c6c6"
167     echo "*** check for corruption"
168     echo "expect $f..." >>$seq.full
169     xfs_db -r -c "fsblock $2" -c "print" $1 | head | tee -a $seq.full | \
170         grep -q -v "$f $f $f $f $f $f $f $f" && \
171             _fail "!!! block $2 corrupted!"
172 }
173
174 # get standard environment, filters and checks
175 . ./common.rc
176 . ./common.filter
177
178 # real QA test starts here
179 _supported_fs xfs
180 _supported_os Linux
181
182 rm -f $seq.full
183
184 # mkfs sizes
185 log_size=2097152
186 log_size_bb=`expr $log_size / 512`
187
188 _require_scratch
189 _init
190
191 block=`_after_log $SCRATCH_DEV`
192 echo "fsblock after log = $block"               >>$seq.full
193 _check_corrupt $SCRATCH_DEV $block
194
195 actual_log_size=`_log_size`
196 echo "log size = $actual_log_size BB"                      >>$seq.full
197 head=`_log_head`
198 echo "log position = $head"                     >>$seq.full
199 lsunit=`_log_sunit`
200 echo "log sunit = $lsunit"                      >>$seq.full
201
202 # sanity checks
203 [ $actual_log_size -eq $log_size_bb ] || \
204     _fail "!!! unexpected log size $size"
205 [ $head -eq 2 -o $head -eq $((lsunit/512)) ] || \
206     _fail "!!! unexpected initial log position $head vs. $((lsunit/512))"
207
208 # find how how many blocks per op for 100 ops
209 # ignore the fact that it will also include an unmount record etc...
210 # this should be small overall
211 echo "    lots of traffic for sampling" >>$seq.full
212 sample_size_ops=100
213 _log_traffic $sample_size_ops
214 head1=`_log_head`
215 num_blocks=`expr $head1 - $head`
216 blocks_per_op=`echo "scale=3; $num_blocks / $sample_size_ops" | bc`
217 echo "blocks_per_op = $blocks_per_op" >>$seq.full
218 num_expected_ops=`echo "$log_size_bb / $blocks_per_op" | bc`
219 echo "num_expected_ops = $num_expected_ops" >>$seq.full
220 num_expected_to_go=`echo "$num_expected_ops - $sample_size_ops" | bc`
221 echo "num_expected_to_go = $num_expected_to_go" >>$seq.full
222
223 echo "    lots more traffic" >>$seq.full
224 _log_traffic $num_expected_to_go
225 head=`_log_head`
226 echo "log position = $head"                     >>$seq.full
227
228 # e.g. 3891
229 near_end_min=`echo "0.95 * $log_size_bb" | bc | sed 's/\..*//'`
230 echo "near_end_min = $near_end_min" >>$seq.full
231
232 [ $head -gt $near_end_min -a $head -lt $log_size_bb ] || \
233     _fail "!!! unexpected near end log position $head"
234
235 for c in `seq 0 20`
236 do
237     echo "   little traffic"            >>$seq.full
238     _log_traffic 2
239     head=`_log_head`
240     echo "log position = $head"         >>$seq.full
241     _check_corrupt $SCRATCH_DEV $block
242 done
243
244 [ $head -lt 1000 ] || \
245     _fail "!!! unexpected wrapped log position $head"
246
247 # success, all done
248 status=0
249 exit