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