check-all revision 1.4 1 1.1 perseant #!/usr/pkg/bin/perl
2 1.3 perseant #
3 1.4 perseant # $NetBSD: check-all,v 1.4 2006/07/21 00:29:23 perseant Exp $
4 1.3 perseant #
5 1.3 perseant # Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
6 1.3 perseant # All rights reserved.
7 1.3 perseant #
8 1.3 perseant # This code is derived from software contributed to The NetBSD Foundation
9 1.3 perseant # by Konrad E. Schroder <perseant (at] hhhh.org>.
10 1.3 perseant #
11 1.3 perseant # Redistribution and use in source and binary forms, with or without
12 1.3 perseant # modification, are permitted provided that the following conditions
13 1.3 perseant # are met:
14 1.3 perseant # 1. Redistributions of source code must retain the above copyright
15 1.3 perseant # notice, this list of conditions and the following disclaimer.
16 1.3 perseant # 2. Redistributions in binary form must reproduce the above copyright
17 1.3 perseant # notice, this list of conditions and the following disclaimer in the
18 1.3 perseant # documentation and/or other materials provided with the distribution.
19 1.3 perseant # 3. All advertising materials mentioning features or use of this software
20 1.3 perseant # must display the following acknowledgement:
21 1.3 perseant # This product includes software developed by the NetBSD
22 1.3 perseant # Foundation, Inc. and its contributors.
23 1.3 perseant # 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.3 perseant # contributors may be used to endorse or promote products derived
25 1.3 perseant # from this software without specific prior written permission.
26 1.3 perseant #
27 1.3 perseant # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.3 perseant # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.3 perseant # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.3 perseant # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.3 perseant # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.3 perseant # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.3 perseant # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.3 perseant # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.3 perseant # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.3 perseant # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.3 perseant # POSSIBILITY OF SUCH DAMAGE.
38 1.3 perseant #
39 1.1 perseant
40 1.1 perseant #
41 1.1 perseant # Use dumplfs to find all locations of the Ifile inode on a given disk.
42 1.1 perseant # Order these by serial number and call fsck_lfs on the raw disk for each.
43 1.3 perseant # If any fsck gives errors (any line of all capital letters, with a few
44 1.3 perseant # exceptions) print an error code with the daddr of the failing Ifile inode
45 1.3 perseant # location.
46 1.1 perseant #
47 1.1 perseant
48 1.2 perseant $| = 1;
49 1.1 perseant $rdev = $ARGV[0];
50 1.2 perseant $gfile = $ARGV[1];
51 1.2 perseant $wfile = $ARGV[2];
52 1.2 perseant $sstart = $ARGV[3];
53 1.4 perseant $test_rfw = 1; # $ARGV[4];
54 1.1 perseant $rollid = 0;
55 1.1 perseant open(DUMPLFS, "dumplfs $rdev |");
56 1.1 perseant
57 1.1 perseant # Look for "roll_id" so we don't use garbage
58 1.1 perseant while (<DUMPLFS>) {
59 1.2 perseant if ($ssize == 0 && m/ssize *([0-9]*)/) {
60 1.2 perseant $ssize = $1;
61 1.2 perseant }
62 1.2 perseant if ($fsize == 0 && m/fsize *([0-9]*)/) {
63 1.2 perseant $fsize = $1;
64 1.2 perseant }
65 1.1 perseant if (m/roll_id *([x0-9a-f]*)/) {
66 1.1 perseant $rollid = $1;
67 1.1 perseant last;
68 1.1 perseant }
69 1.1 perseant }
70 1.1 perseant
71 1.1 perseant # Now look for inodes and segment summaries. Build a hash table of these
72 1.1 perseant # based on serial number. Ignore any with serial numbers lower than $sstart.
73 1.1 perseant
74 1.1 perseant %iloc = ();
75 1.2 perseant %snloc = ();
76 1.2 perseant %sumloc = ();
77 1.2 perseant print "Reading segments:";
78 1.1 perseant while (<DUMPLFS>) {
79 1.1 perseant if (m/roll_id *([0-9a-f]*)/) {
80 1.1 perseant # print "rollid $1\n";
81 1.1 perseant if ("0x$1" ne $rollid) {
82 1.1 perseant # Skip the rest of this segment
83 1.3 perseant print "{skip bad rollid 0x$1}";
84 1.1 perseant while(<DUMPLFS>) {
85 1.1 perseant last if m/SEGMENT/;
86 1.1 perseant }
87 1.2 perseant # Fall through
88 1.1 perseant }
89 1.1 perseant }
90 1.2 perseant if (m/roll_id.*serial *([0-9]*)/) {
91 1.1 perseant $serno = $1;
92 1.2 perseant $snloc{$serno} = $segnum;
93 1.2 perseant $sumloc{$serno} = $sumloc;
94 1.3 perseant print "($serno)";
95 1.1 perseant if ($serno < $sstart) {
96 1.1 perseant # Skip the rest of this partial segment
97 1.3 perseant #print "{skip bad serno $serno}";
98 1.1 perseant while(<DUMPLFS>) {
99 1.2 perseant last if m/Segment Summary/ ||
100 1.2 perseant m/SEGMENT/;
101 1.1 perseant }
102 1.2 perseant # Fall through
103 1.1 perseant }
104 1.1 perseant }
105 1.2 perseant if (m/Segment Summary Info at 0x([0-9a-f]*)/) {
106 1.2 perseant $sumloc = $1;
107 1.3 perseant next;
108 1.2 perseant }
109 1.1 perseant if (m/0x([0-9a-f]*)/) {
110 1.1 perseant foreach $ss (split "0x", $_) {
111 1.1 perseant if ($ss =~ m/^([0-9a-f][0-9a-f]*)/) {
112 1.1 perseant # print "iblk 0x$1\n";
113 1.1 perseant $daddr = $1;
114 1.1 perseant if (m/[^0-9]1v1/) {
115 1.1 perseant # print "** ifblk 0x$daddr\n";
116 1.1 perseant $iloc{$serno} = $daddr;
117 1.2 perseant $lastaddr = $daddr;
118 1.1 perseant }
119 1.1 perseant }
120 1.1 perseant }
121 1.1 perseant }
122 1.2 perseant if (m/SEGMENT *([0-9]*)/) {
123 1.2 perseant $segnum = $1;
124 1.3 perseant print "[$segnum]";
125 1.1 perseant }
126 1.1 perseant }
127 1.2 perseant print "\n";
128 1.1 perseant close(DUMPLFS);
129 1.1 perseant
130 1.3 perseant # Complain about missing partial-segments
131 1.3 perseant for ($i = $sstart; $i < $serno; ++$i) {
132 1.3 perseant if (hex $sumloc{$i} == 0 && $i > 0) {
133 1.3 perseant print "Oops, couldn't find pseg $i\n";
134 1.3 perseant }
135 1.3 perseant }
136 1.3 perseant
137 1.2 perseant # If there were no checkpoints, print *something*
138 1.2 perseant if ($#iloc == 0) {
139 1.2 perseant print "0 $sstart 0\n";
140 1.2 perseant exit 0;
141 1.2 perseant }
142 1.2 perseant
143 1.1 perseant #
144 1.1 perseant # Now fsck each checkpoint in turn, beginning with $sstart.
145 1.2 perseant # Because the log wraps we will have to reconstruct the filesystem image
146 1.2 perseant # as it existed at each checkpoint before running fsck.
147 1.2 perseant #
148 1.1 perseant # Look for lines containing only caps or "!", but ignore known
149 1.1 perseant # false positives.
150 1.1 perseant #
151 1.1 perseant $error = 0;
152 1.2 perseant $lastgood = $sstart - 1;
153 1.1 perseant open(LOG, ">>check-all.log");
154 1.2 perseant print "Available checkpoints:";
155 1.2 perseant print LOG "Available checkpoints:";
156 1.1 perseant foreach $k (sort { $a <=> $b } keys %iloc) {
157 1.1 perseant $a = $iloc{$k};
158 1.2 perseant print " $a";
159 1.2 perseant print LOG " $a";
160 1.2 perseant }
161 1.2 perseant print "\n";
162 1.2 perseant print LOG "\n";
163 1.2 perseant
164 1.3 perseant #
165 1.3 perseant # Copy the partial segments $_[0]--$_[1] from the raw device onto
166 1.3 perseant # the working file. Return the next partial-segment serial number
167 1.3 perseant # after the last one we copied (usually $_[1] + 1, except in case of
168 1.3 perseant # an error).
169 1.3 perseant #
170 1.3 perseant sub copypseg
171 1.3 perseant {
172 1.3 perseant my ($blstart, $blstop, $segstop, $cmd);
173 1.3 perseant my ($totalstart, $totalstop);
174 1.3 perseant
175 1.3 perseant $totalstart = 0;
176 1.3 perseant $totalstop = 0;
177 1.3 perseant for ($i = $_[0]; $i <= $_[1]; ++$i) {
178 1.3 perseant $blstart = hex $sumloc{$i};
179 1.3 perseant last if $blstart <= 0;
180 1.3 perseant $totalstart = $blstart if $totalstart == 0;
181 1.3 perseant $blstop = hex $sumloc{$i + 1};
182 1.3 perseant $segstop = ((int ($blstart / $fps)) + 1) * $fps;
183 1.3 perseant if ($segstop < $blstop || $blstop < $blstart) {
184 1.3 perseant #print "Adjusting $blstop -> $segstop\n";
185 1.3 perseant $blstop = $segstop;
186 1.3 perseant }
187 1.3 perseant $totalstop = $blstop;
188 1.3 perseant
189 1.4 perseant print "pseg $i: write blocks ", hex $blstart, "-", hex ($blstop - 1), "\n";
190 1.3 perseant $blstart = $blstop;
191 1.3 perseant }
192 1.3 perseant $cmd = "dd if=$rdev of=$wfile bs=$fsize seek=$totalstart " .
193 1.3 perseant "skip=$totalstart conv=notrunc count=" .
194 1.3 perseant ($totalstop - $totalstart);
195 1.3 perseant # print "$cmd\n";
196 1.3 perseant system("$cmd >/dev/null 2>&1");
197 1.3 perseant
198 1.3 perseant return $i;
199 1.3 perseant }
200 1.3 perseant
201 1.2 perseant print "Recreating filesystem image as of $sstart:\n";
202 1.2 perseant if ($sstart == 0) {
203 1.2 perseant $cmd = "dd if=$rdev of=$wfile bs=1m conv=swab,oldebcdic"; # garbage
204 1.2 perseant } else {
205 1.2 perseant $cmd = "dd if=$gfile of=$wfile bs=1m";
206 1.2 perseant }
207 1.2 perseant print "$cmd\n";
208 1.2 perseant system("$cmd >/dev/null 2>&1");
209 1.2 perseant
210 1.3 perseant print "Copying over first superblock\n";
211 1.3 perseant system("dd if=$rdev of=$wfile bs=8k count=2 conv=notrunc >/dev/null 2>&1");
212 1.3 perseant
213 1.4 perseant sub test_fsck
214 1.4 perseant {
215 1.4 perseant my $a = $_[0];
216 1.4 perseant my $flags = $_[1];
217 1.4 perseant my $printit = $_[2];
218 1.4 perseant my $output = "";
219 1.2 perseant
220 1.4 perseant $flags = "-n -f -i 0x$a $wfile" unless $flags;
221 1.2 perseant
222 1.4 perseant $cmd = "fsck_lfs $flags";
223 1.2 perseant print "$cmd\n";
224 1.2 perseant print LOG "$cmd\n";
225 1.2 perseant open(FSCK, "$cmd 2>&1 |");
226 1.1 perseant while(<FSCK>) {
227 1.1 perseant print LOG;
228 1.4 perseant $rline = "$_";
229 1.1 perseant chomp;
230 1.1 perseant
231 1.1 perseant # Known false positives (mismatch between sb and ifile,
232 1.1 perseant # which should be expected given we're using an arbitrarily
233 1.3 perseant # old version of the ifile)
234 1.1 perseant if (m/AVAIL GIVEN/ ||
235 1.1 perseant m/BFREE GIVEN/ ||
236 1.1 perseant m/DMETA GIVEN/ ||
237 1.2 perseant m/NCLEAN GIVEN/ ||
238 1.2 perseant m/FREE BUT NOT ON FREE LIST/ || # UNWRITTEN inodes OK
239 1.4 perseant m/FILE SYSTEM WAS MODIFIED/ ||
240 1.2 perseant m/FREE LIST HEAD IN SUPERBLOCK/ ) {
241 1.1 perseant next;
242 1.1 perseant }
243 1.1 perseant
244 1.1 perseant # Fsck reports errors in ALL CAPS
245 1.1 perseant # But don't count hex numbers as "lowercase".
246 1.4 perseant $oline = "$_";
247 1.1 perseant s/0x[0-9a-f]*//g;
248 1.1 perseant if (m/[A-Z]/ && ! m/[a-z]/) {
249 1.1 perseant $error = 1;
250 1.2 perseant $errsn = $k;
251 1.4 perseant $errstr = "1 $k 0x$a $oline";
252 1.4 perseant # last;
253 1.1 perseant }
254 1.4 perseant
255 1.4 perseant # Log everything we get, except for some things we
256 1.4 perseant # will see every single time.
257 1.4 perseant if (m/checkpoint invalid/ ||
258 1.4 perseant m/skipping free list check/ ||
259 1.4 perseant m/expect discrepancies/) {
260 1.4 perseant next;
261 1.4 perseant }
262 1.4 perseant $output .= $rline;
263 1.1 perseant }
264 1.1 perseant close(FSCK);
265 1.4 perseant
266 1.4 perseant if ($? != 0) {
267 1.4 perseant $error = 1;
268 1.4 perseant $errsn = $k;
269 1.4 perseant $errstr = "1 $k 0x$a <" . (hex $?) . ">";
270 1.4 perseant }
271 1.4 perseant
272 1.4 perseant if ($error || $printit) {
273 1.4 perseant print $output;
274 1.4 perseant }
275 1.4 perseant }
276 1.4 perseant
277 1.4 perseant $blstart = 0;
278 1.4 perseant $fps = $ssize / $fsize;
279 1.4 perseant $oind = ($sstart ? $sstart : 1);
280 1.4 perseant BIGLOOP: foreach $k (sort { $a <=> $b } keys %iloc) {
281 1.4 perseant $a = $iloc{$k};
282 1.4 perseant
283 1.4 perseant if (hex($a) > hex($lastaddr)) {
284 1.4 perseant print "Skipping out-of-place checkpoint $k at $a\n";
285 1.4 perseant next;
286 1.4 perseant }
287 1.4 perseant
288 1.4 perseant if ($test_rfw && $iloc{$oind - 1}) {
289 1.4 perseant for ($tk = $oind; $tk < $k; $tk++) {
290 1.4 perseant print "Test roll-forward agent at non-checkpoint pseg $tk\n";
291 1.4 perseant print LOG "Test roll-forward agent at non-checkpoint pseg $tk\n";
292 1.4 perseant ©pseg($oind, $tk);
293 1.4 perseant # Add -d flag here for verbose debugging info
294 1.4 perseant $flags = "-p -f -i 0x" . $iloc{$oind - 1} . " $wfile";
295 1.4 perseant &test_fsck($iloc{$oind - 1}, $flags, 1);
296 1.4 perseant last BIGLOOP if $error;
297 1.4 perseant
298 1.4 perseant # note lack of -i flag, since the roll-forward
299 1.4 perseant # will have rewritten the superblocks.
300 1.4 perseant &test_fsck($iloc{$oind - 1}, "-n -f $wfile", 0);
301 1.4 perseant last BIGLOOP if $error;
302 1.4 perseant }
303 1.4 perseant }
304 1.4 perseant
305 1.4 perseant print "Recreate fs state at checkpoint pseg $k (from " . ($oind - 1) .
306 1.4 perseant ")\n";
307 1.4 perseant $oind = ©pseg($oind, $k);
308 1.4 perseant
309 1.4 perseant &test_fsck($a, "", 0);
310 1.4 perseant
311 1.1 perseant last if $error;
312 1.2 perseant $lastgood = $k; # record last good serial number
313 1.2 perseant }
314 1.4 perseant
315 1.4 perseant if ($errstr) {
316 1.4 perseant print "$errstr\n";
317 1.4 perseant exit 0;
318 1.4 perseant }
319 1.4 perseant
320 1.3 perseant if (!$errstr) {
321 1.3 perseant print "Bring filesystem state up to log wrap\n";
322 1.3 perseant $lastgood = ©pseg($oind, 100000000000) - 1;
323 1.2 perseant
324 1.2 perseant print "Copying this good image to $gfile\n";
325 1.2 perseant system("dd bs=1m if=$rdev of=$gfile >/dev/null 2>&1");
326 1.3 perseant print "0 $lastgood 0x$a\n";
327 1.2 perseant exit 0;
328 1.2 perseant }
329 1.2 perseant
330 1.2 perseant #
331 1.3 perseant # Ifile write-checking paranoia.
332 1.3 perseant #
333 1.2 perseant # If we found an error, try to find which blocks of the Ifile inode changed
334 1.2 perseant # between the last good checkpoint and this checkpoint; and which blocks
335 1.2 perseant # *should* have changed. This means (1) which segments were written; and
336 1.2 perseant # (2) which inodes were written. The 0 block of the Ifile should always
337 1.2 perseant # have changed since lfs_avail is always in flux.
338 1.2 perseant #
339 1.2 perseant
340 1.2 perseant $cmd = "dumplfs";
341 1.2 perseant $oseg = -1;
342 1.2 perseant %iblk = ();
343 1.2 perseant %iblk_done = ();
344 1.2 perseant %why = ();
345 1.2 perseant $iblk{0} = 1;
346 1.2 perseant for ($i = $lastgood + 1; $i <= $errsn; $i++) {
347 1.2 perseant if ($oseg != $snloc{$i}) {
348 1.2 perseant $oseg = 0 + $snloc{$i};
349 1.2 perseant $cmd .= " -s$oseg";
350 1.2 perseant }
351 1.1 perseant }
352 1.2 perseant $cmd .= " $rdev";
353 1.1 perseant
354 1.2 perseant open(DUMPLFS, "$cmd |");
355 1.2 perseant while(<DUMPLFS>) {
356 1.2 perseant if (m/ifpb *([0-9]*)/) {
357 1.2 perseant $ifpb = $1;
358 1.2 perseant }
359 1.2 perseant if (m/sepb *([0-9]*)/) {
360 1.2 perseant $sepb = $1;
361 1.2 perseant }
362 1.2 perseant if (m/cleansz *([0-9]*)/) {
363 1.2 perseant $cleansz = $1;
364 1.2 perseant }
365 1.2 perseant if (m/segtabsz *([0-9]*)/) {
366 1.2 perseant $segtabsz = $1;
367 1.2 perseant }
368 1.2 perseant last if m/SEGMENT/;
369 1.2 perseant }
370 1.2 perseant while(<DUMPLFS>) {
371 1.2 perseant chomp;
372 1.2 perseant
373 1.2 perseant # Skip over partial segments outside our range of interest
374 1.2 perseant if (m/roll_id.*serial *([0-9]*)/) {
375 1.2 perseant $serno = $1;
376 1.2 perseant if ($serno <= $lastgood || $serno > $errsn) {
377 1.2 perseant # Skip the rest of this partial segment
378 1.2 perseant while(<DUMPLFS>) {
379 1.2 perseant last if m/Segment Summary/ || m/SEGMENT/;
380 1.2 perseant }
381 1.2 perseant next;
382 1.2 perseant }
383 1.2 perseant }
384 1.2 perseant
385 1.2 perseant # Look for inodes
386 1.2 perseant if (m/Inode addresses/) {
387 1.2 perseant s/^[^{]*{/ /o;
388 1.2 perseant s/}[^{]*$/ /o;
389 1.2 perseant s/}[^{]*{/,/og;
390 1.2 perseant s/v[0-9]*//og;
391 1.2 perseant @ilist = split(',');
392 1.2 perseant foreach $i (@ilist) {
393 1.2 perseant $i =~ s/ *//og;
394 1.2 perseant next if $i == 1;
395 1.2 perseant $iaddr = $cleansz + $segtabsz + int ($i / $ifpb);
396 1.2 perseant $iblk{$iaddr} = 1;
397 1.2 perseant $why{$iaddr} .= " $i";
398 1.2 perseant }
399 1.2 perseant }
400 1.2 perseant
401 1.2 perseant # Look for Ifile blocks actually written
402 1.2 perseant if (m/FINFO for inode: ([0-9]*) version/) {
403 1.2 perseant $i = $1;
404 1.2 perseant $inoblkmode = ($i == 1);
405 1.2 perseant }
406 1.2 perseant if ($inoblkmode && m/^[-\t 0-9]*$/) {
407 1.2 perseant s/\t/ /og;
408 1.2 perseant s/^ *//o;
409 1.2 perseant s/ *$//o;
410 1.2 perseant @bn = split(' ');
411 1.2 perseant foreach $b (@bn) {
412 1.2 perseant $iblk_done{$b} = 1;
413 1.2 perseant }
414 1.2 perseant }
415 1.2 perseant }
416 1.2 perseant close(DUMPLFS);
417 1.2 perseant
418 1.2 perseant # Report found and missing Ifile blocks
419 1.2 perseant print "Ifile blocks found:";
420 1.2 perseant foreach $b (sort { $a <=> $b } keys %iblk) {
421 1.2 perseant if ($iblk_done{$b} == 1) {
422 1.2 perseant print " $b";
423 1.2 perseant }
424 1.2 perseant }
425 1.2 perseant print "\n";
426 1.4 perseant
427 1.2 perseant print "Ifile blocks missing:";
428 1.2 perseant foreach $b (sort { $a <=> $b } keys %iblk) {
429 1.2 perseant if ($iblk_done{$b} == 0) {
430 1.2 perseant $why{$b} =~ s/^ *//o;
431 1.2 perseant print " $b ($why{$b})";
432 1.2 perseant }
433 1.1 perseant }
434 1.2 perseant print "\n";
435 1.2 perseant
436 1.2 perseant print "$errstr\n";
437 1.2 perseant exit 0;
438