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