check-all revision 1.3 1 1.1 perseant #!/usr/pkg/bin/perl
2 1.3 perseant #
3 1.3 perseant # $NetBSD: check-all,v 1.3 2006/04/27 22:37:54 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.1 perseant $rollid = 0;
54 1.1 perseant open(DUMPLFS, "dumplfs $rdev |");
55 1.1 perseant
56 1.1 perseant # Look for "roll_id" so we don't use garbage
57 1.1 perseant while (<DUMPLFS>) {
58 1.2 perseant if ($ssize == 0 && m/ssize *([0-9]*)/) {
59 1.2 perseant $ssize = $1;
60 1.2 perseant }
61 1.2 perseant if ($fsize == 0 && m/fsize *([0-9]*)/) {
62 1.2 perseant $fsize = $1;
63 1.2 perseant }
64 1.1 perseant if (m/roll_id *([x0-9a-f]*)/) {
65 1.1 perseant $rollid = $1;
66 1.1 perseant last;
67 1.1 perseant }
68 1.1 perseant }
69 1.1 perseant
70 1.1 perseant # Now look for inodes and segment summaries. Build a hash table of these
71 1.1 perseant # based on serial number. Ignore any with serial numbers lower than $sstart.
72 1.1 perseant
73 1.1 perseant %iloc = ();
74 1.2 perseant %snloc = ();
75 1.2 perseant %sumloc = ();
76 1.2 perseant print "Reading segments:";
77 1.1 perseant while (<DUMPLFS>) {
78 1.1 perseant if (m/roll_id *([0-9a-f]*)/) {
79 1.1 perseant # print "rollid $1\n";
80 1.1 perseant if ("0x$1" ne $rollid) {
81 1.1 perseant # Skip the rest of this segment
82 1.3 perseant print "{skip bad rollid 0x$1}";
83 1.1 perseant while(<DUMPLFS>) {
84 1.1 perseant last if m/SEGMENT/;
85 1.1 perseant }
86 1.2 perseant # Fall through
87 1.1 perseant }
88 1.1 perseant }
89 1.2 perseant if (m/roll_id.*serial *([0-9]*)/) {
90 1.1 perseant $serno = $1;
91 1.2 perseant $snloc{$serno} = $segnum;
92 1.2 perseant $sumloc{$serno} = $sumloc;
93 1.3 perseant print "($serno)";
94 1.1 perseant if ($serno < $sstart) {
95 1.1 perseant # Skip the rest of this partial segment
96 1.3 perseant #print "{skip bad serno $serno}";
97 1.1 perseant while(<DUMPLFS>) {
98 1.2 perseant last if m/Segment Summary/ ||
99 1.2 perseant m/SEGMENT/;
100 1.1 perseant }
101 1.2 perseant # Fall through
102 1.1 perseant }
103 1.1 perseant }
104 1.2 perseant if (m/Segment Summary Info at 0x([0-9a-f]*)/) {
105 1.2 perseant $sumloc = $1;
106 1.3 perseant next;
107 1.2 perseant }
108 1.1 perseant if (m/0x([0-9a-f]*)/) {
109 1.1 perseant foreach $ss (split "0x", $_) {
110 1.1 perseant if ($ss =~ m/^([0-9a-f][0-9a-f]*)/) {
111 1.1 perseant # print "iblk 0x$1\n";
112 1.1 perseant $daddr = $1;
113 1.1 perseant if (m/[^0-9]1v1/) {
114 1.1 perseant # print "** ifblk 0x$daddr\n";
115 1.1 perseant $iloc{$serno} = $daddr;
116 1.2 perseant $lastaddr = $daddr;
117 1.1 perseant }
118 1.1 perseant }
119 1.1 perseant }
120 1.1 perseant }
121 1.2 perseant if (m/SEGMENT *([0-9]*)/) {
122 1.2 perseant $segnum = $1;
123 1.3 perseant print "[$segnum]";
124 1.1 perseant }
125 1.1 perseant }
126 1.2 perseant print "\n";
127 1.1 perseant close(DUMPLFS);
128 1.1 perseant
129 1.3 perseant # Complain about missing partial-segments
130 1.3 perseant for ($i = $sstart; $i < $serno; ++$i) {
131 1.3 perseant if (hex $sumloc{$i} == 0 && $i > 0) {
132 1.3 perseant print "Oops, couldn't find pseg $i\n";
133 1.3 perseant }
134 1.3 perseant }
135 1.3 perseant
136 1.2 perseant # If there were no checkpoints, print *something*
137 1.2 perseant if ($#iloc == 0) {
138 1.2 perseant print "0 $sstart 0\n";
139 1.2 perseant exit 0;
140 1.2 perseant }
141 1.2 perseant
142 1.1 perseant #
143 1.1 perseant # Now fsck each checkpoint in turn, beginning with $sstart.
144 1.2 perseant # Because the log wraps we will have to reconstruct the filesystem image
145 1.2 perseant # as it existed at each checkpoint before running fsck.
146 1.2 perseant #
147 1.1 perseant # Look for lines containing only caps or "!", but ignore known
148 1.1 perseant # false positives.
149 1.1 perseant #
150 1.1 perseant $error = 0;
151 1.2 perseant $lastgood = $sstart - 1;
152 1.1 perseant open(LOG, ">>check-all.log");
153 1.2 perseant print "Available checkpoints:";
154 1.2 perseant print LOG "Available checkpoints:";
155 1.1 perseant foreach $k (sort { $a <=> $b } keys %iloc) {
156 1.1 perseant $a = $iloc{$k};
157 1.2 perseant print " $a";
158 1.2 perseant print LOG " $a";
159 1.2 perseant }
160 1.2 perseant print "\n";
161 1.2 perseant print LOG "\n";
162 1.2 perseant
163 1.3 perseant #
164 1.3 perseant # Copy the partial segments $_[0]--$_[1] from the raw device onto
165 1.3 perseant # the working file. Return the next partial-segment serial number
166 1.3 perseant # after the last one we copied (usually $_[1] + 1, except in case of
167 1.3 perseant # an error).
168 1.3 perseant #
169 1.3 perseant sub copypseg
170 1.3 perseant {
171 1.3 perseant my ($blstart, $blstop, $segstop, $cmd);
172 1.3 perseant my ($totalstart, $totalstop);
173 1.3 perseant
174 1.3 perseant $totalstart = 0;
175 1.3 perseant $totalstop = 0;
176 1.3 perseant for ($i = $_[0]; $i <= $_[1]; ++$i) {
177 1.3 perseant $blstart = hex $sumloc{$i};
178 1.3 perseant last if $blstart <= 0;
179 1.3 perseant $totalstart = $blstart if $totalstart == 0;
180 1.3 perseant $blstop = hex $sumloc{$i + 1};
181 1.3 perseant $segstop = ((int ($blstart / $fps)) + 1) * $fps;
182 1.3 perseant if ($segstop < $blstop || $blstop < $blstart) {
183 1.3 perseant #print "Adjusting $blstop -> $segstop\n";
184 1.3 perseant $blstop = $segstop;
185 1.3 perseant }
186 1.3 perseant $totalstop = $blstop;
187 1.3 perseant
188 1.3 perseant print "pseg $i: write blocks $blstart-", $blstop - 1, "\n";
189 1.3 perseant $blstart = $blstop;
190 1.3 perseant }
191 1.3 perseant $cmd = "dd if=$rdev of=$wfile bs=$fsize seek=$totalstart " .
192 1.3 perseant "skip=$totalstart conv=notrunc count=" .
193 1.3 perseant ($totalstop - $totalstart);
194 1.3 perseant # print "$cmd\n";
195 1.3 perseant system("$cmd >/dev/null 2>&1");
196 1.3 perseant
197 1.3 perseant return $i;
198 1.3 perseant }
199 1.3 perseant
200 1.2 perseant print "Recreating filesystem image as of $sstart:\n";
201 1.2 perseant if ($sstart == 0) {
202 1.2 perseant $cmd = "dd if=$rdev of=$wfile bs=1m conv=swab,oldebcdic"; # garbage
203 1.2 perseant } else {
204 1.2 perseant $cmd = "dd if=$gfile of=$wfile bs=1m";
205 1.2 perseant }
206 1.2 perseant print "$cmd\n";
207 1.2 perseant system("$cmd >/dev/null 2>&1");
208 1.2 perseant
209 1.3 perseant print "Copying over first superblock\n";
210 1.3 perseant system("dd if=$rdev of=$wfile bs=8k count=2 conv=notrunc >/dev/null 2>&1");
211 1.3 perseant
212 1.2 perseant $blstart = 0;
213 1.2 perseant $fps = $ssize / $fsize;
214 1.3 perseant $oind = ($sstart ? $sstart : 1);
215 1.2 perseant foreach $k (sort { $a <=> $b } keys %iloc) {
216 1.2 perseant $a = $iloc{$k};
217 1.2 perseant
218 1.2 perseant if (hex($a) > hex($lastaddr)) {
219 1.2 perseant print "Skipping out-of-place checkpoint $k at $a\n";
220 1.2 perseant next;
221 1.2 perseant }
222 1.2 perseant
223 1.3 perseant print "Recreate fs state at checkpoint pseg $k\n";
224 1.3 perseant $oind = ©pseg($oind, $k);
225 1.2 perseant
226 1.2 perseant $cmd = "fsck_lfs -n -f -i 0x$a $wfile";
227 1.2 perseant print "$cmd\n";
228 1.2 perseant print LOG "$cmd\n";
229 1.2 perseant open(FSCK, "$cmd 2>&1 |");
230 1.1 perseant while(<FSCK>) {
231 1.1 perseant print LOG;
232 1.1 perseant chomp;
233 1.1 perseant
234 1.1 perseant # Known false positives (mismatch between sb and ifile,
235 1.1 perseant # which should be expected given we're using an arbitrarily
236 1.3 perseant # old version of the ifile)
237 1.1 perseant if (m/AVAIL GIVEN/ ||
238 1.1 perseant m/BFREE GIVEN/ ||
239 1.1 perseant m/DMETA GIVEN/ ||
240 1.2 perseant m/NCLEAN GIVEN/ ||
241 1.2 perseant m/FREE BUT NOT ON FREE LIST/ || # UNWRITTEN inodes OK
242 1.2 perseant m/FREE LIST HEAD IN SUPERBLOCK/ ) {
243 1.1 perseant next;
244 1.1 perseant }
245 1.1 perseant
246 1.1 perseant # Fsck reports errors in ALL CAPS
247 1.1 perseant # But don't count hex numbers as "lowercase".
248 1.1 perseant s/0x[0-9a-f]*//g;
249 1.1 perseant if (m/[A-Z]/ && ! m/[a-z]/) {
250 1.1 perseant $error = 1;
251 1.2 perseant $errsn = $k;
252 1.2 perseant $errstr = "1 $k 0x$a";
253 1.1 perseant last;
254 1.1 perseant }
255 1.1 perseant }
256 1.1 perseant close(FSCK);
257 1.1 perseant last if $error;
258 1.2 perseant $lastgood = $k; # record last good serial number
259 1.2 perseant }
260 1.3 perseant if (!$errstr) {
261 1.3 perseant print "Bring filesystem state up to log wrap\n";
262 1.3 perseant $lastgood = ©pseg($oind, 100000000000) - 1;
263 1.2 perseant
264 1.2 perseant print "Copying this good image to $gfile\n";
265 1.2 perseant system("dd bs=1m if=$rdev of=$gfile >/dev/null 2>&1");
266 1.3 perseant print "0 $lastgood 0x$a\n";
267 1.2 perseant exit 0;
268 1.2 perseant }
269 1.2 perseant
270 1.2 perseant #
271 1.3 perseant # Ifile write-checking paranoia.
272 1.3 perseant #
273 1.2 perseant # If we found an error, try to find which blocks of the Ifile inode changed
274 1.2 perseant # between the last good checkpoint and this checkpoint; and which blocks
275 1.2 perseant # *should* have changed. This means (1) which segments were written; and
276 1.2 perseant # (2) which inodes were written. The 0 block of the Ifile should always
277 1.2 perseant # have changed since lfs_avail is always in flux.
278 1.2 perseant #
279 1.2 perseant
280 1.2 perseant $cmd = "dumplfs";
281 1.2 perseant $oseg = -1;
282 1.2 perseant %iblk = ();
283 1.2 perseant %iblk_done = ();
284 1.2 perseant %why = ();
285 1.2 perseant $iblk{0} = 1;
286 1.2 perseant for ($i = $lastgood + 1; $i <= $errsn; $i++) {
287 1.2 perseant if ($oseg != $snloc{$i}) {
288 1.2 perseant $oseg = 0 + $snloc{$i};
289 1.2 perseant $cmd .= " -s$oseg";
290 1.2 perseant }
291 1.1 perseant }
292 1.2 perseant $cmd .= " $rdev";
293 1.1 perseant
294 1.2 perseant open(DUMPLFS, "$cmd |");
295 1.2 perseant while(<DUMPLFS>) {
296 1.2 perseant if (m/ifpb *([0-9]*)/) {
297 1.2 perseant $ifpb = $1;
298 1.2 perseant }
299 1.2 perseant if (m/sepb *([0-9]*)/) {
300 1.2 perseant $sepb = $1;
301 1.2 perseant }
302 1.2 perseant if (m/cleansz *([0-9]*)/) {
303 1.2 perseant $cleansz = $1;
304 1.2 perseant }
305 1.2 perseant if (m/segtabsz *([0-9]*)/) {
306 1.2 perseant $segtabsz = $1;
307 1.2 perseant }
308 1.2 perseant last if m/SEGMENT/;
309 1.2 perseant }
310 1.2 perseant while(<DUMPLFS>) {
311 1.2 perseant chomp;
312 1.2 perseant
313 1.2 perseant # Skip over partial segments outside our range of interest
314 1.2 perseant if (m/roll_id.*serial *([0-9]*)/) {
315 1.2 perseant $serno = $1;
316 1.2 perseant if ($serno <= $lastgood || $serno > $errsn) {
317 1.2 perseant # Skip the rest of this partial segment
318 1.2 perseant while(<DUMPLFS>) {
319 1.2 perseant last if m/Segment Summary/ || m/SEGMENT/;
320 1.2 perseant }
321 1.2 perseant next;
322 1.2 perseant }
323 1.2 perseant }
324 1.2 perseant
325 1.2 perseant # Look for inodes
326 1.2 perseant if (m/Inode addresses/) {
327 1.2 perseant s/^[^{]*{/ /o;
328 1.2 perseant s/}[^{]*$/ /o;
329 1.2 perseant s/}[^{]*{/,/og;
330 1.2 perseant s/v[0-9]*//og;
331 1.2 perseant @ilist = split(',');
332 1.2 perseant foreach $i (@ilist) {
333 1.2 perseant $i =~ s/ *//og;
334 1.2 perseant next if $i == 1;
335 1.2 perseant $iaddr = $cleansz + $segtabsz + int ($i / $ifpb);
336 1.2 perseant $iblk{$iaddr} = 1;
337 1.2 perseant $why{$iaddr} .= " $i";
338 1.2 perseant }
339 1.2 perseant }
340 1.2 perseant
341 1.2 perseant # Look for Ifile blocks actually written
342 1.2 perseant if (m/FINFO for inode: ([0-9]*) version/) {
343 1.2 perseant $i = $1;
344 1.2 perseant $inoblkmode = ($i == 1);
345 1.2 perseant }
346 1.2 perseant if ($inoblkmode && m/^[-\t 0-9]*$/) {
347 1.2 perseant s/\t/ /og;
348 1.2 perseant s/^ *//o;
349 1.2 perseant s/ *$//o;
350 1.2 perseant @bn = split(' ');
351 1.2 perseant foreach $b (@bn) {
352 1.2 perseant $iblk_done{$b} = 1;
353 1.2 perseant }
354 1.2 perseant }
355 1.2 perseant }
356 1.2 perseant close(DUMPLFS);
357 1.2 perseant
358 1.2 perseant # Report found and missing Ifile blocks
359 1.2 perseant print "Ifile blocks found:";
360 1.2 perseant foreach $b (sort { $a <=> $b } keys %iblk) {
361 1.2 perseant if ($iblk_done{$b} == 1) {
362 1.2 perseant print " $b";
363 1.2 perseant }
364 1.2 perseant }
365 1.2 perseant print "\n";
366 1.2 perseant
367 1.2 perseant print "Ifile blocks missing:";
368 1.2 perseant foreach $b (sort { $a <=> $b } keys %iblk) {
369 1.2 perseant if ($iblk_done{$b} == 0) {
370 1.2 perseant $why{$b} =~ s/^ *//o;
371 1.2 perseant print " $b ($why{$b})";
372 1.2 perseant }
373 1.1 perseant }
374 1.2 perseant print "\n";
375 1.2 perseant
376 1.2 perseant print "$errstr\n";
377 1.2 perseant exit 0;
378