xref: /netbsd-src/sbin/fsck_lfs/lfs.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /* $NetBSD: lfs.c,v 1.3 2003/05/08 18:39:09 petrov Exp $ */
2 /*-
3  * Copyright (c) 2003 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Konrad E. Schroder <perseant@hhhh.org>.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the NetBSD
20  *	Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  * Copyright (c) 1989, 1991, 1993
39  *	The Regents of the University of California.  All rights reserved.
40  * (c) UNIX System Laboratories, Inc.
41  * All or some portions of this file are derived from material licensed
42  * to the University of California by American Telephone and Telegraph
43  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
44  * the permission of UNIX System Laboratories, Inc.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  *	@(#)ufs_bmap.c	8.8 (Berkeley) 8/11/95
75  */
76 
77 
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <sys/time.h>
81 #include <sys/buf.h>
82 #include <sys/mount.h>
83 
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufsmount.h>
86 #define vnode uvnode
87 #include <ufs/lfs/lfs.h>
88 #undef vnode
89 
90 #include <assert.h>
91 #include <err.h>
92 #include <errno.h>
93 #include <stdarg.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <unistd.h>
98 
99 #include "bufcache.h"
100 #include "vnode.h"
101 #include "lfs.h"
102 #include "segwrite.h"
103 
104 #define panic call_panic
105 
106 extern u_int32_t cksum(void *, size_t);
107 extern u_int32_t lfs_sb_cksum(struct dlfs *);
108 
109 extern struct uvnodelst vnodelist;
110 extern struct uvnodelst getvnodelist;
111 extern int nvnodes;
112 
113 int fsdirty = 0;
114 void (*panic_func)(int, const char *, va_list) = my_vpanic;
115 
116 /*
117  * LFS buffer and uvnode operations
118  */
119 
120 int
121 lfs_vop_strategy(struct ubuf * bp)
122 {
123 	int count;
124 
125 	if (bp->b_flags & B_READ) {
126 		count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
127 		    dbtob(bp->b_blkno));
128 		if (count == bp->b_bcount)
129 			bp->b_flags |= B_DONE;
130 	} else {
131 		count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
132 		    dbtob(bp->b_blkno));
133 		if (count == 0) {
134 			perror("pwrite");
135 			return -1;
136 		}
137 		bp->b_flags &= ~B_DELWRI;
138 		reassignbuf(bp, bp->b_vp);
139 	}
140 	return 0;
141 }
142 
143 int
144 lfs_vop_bwrite(struct ubuf * bp)
145 {
146 	struct lfs *fs;
147 
148 	fs = bp->b_vp->v_fs;
149 	if (!(bp->b_flags & B_DELWRI)) {
150 		fs->lfs_avail -= btofsb(fs, bp->b_bcount);
151 	}
152 	bp->b_flags |= B_DELWRI | B_LOCKED;
153 	reassignbuf(bp, bp->b_vp);
154 	brelse(bp);
155 	return 0;
156 }
157 
158 /*
159  * ufs_bmaparray does the bmap conversion, and if requested returns the
160  * array of logical blocks which must be traversed to get to a block.
161  * Each entry contains the offset into that block that gets you to the
162  * next block and the disk address of the block (if it is assigned).
163  */
164 int
165 ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
166 {
167 	struct inode *ip;
168 	struct ubuf *bp;
169 	struct indir a[NIADDR + 1], *xap;
170 	daddr_t daddr;
171 	daddr_t metalbn;
172 	int error, num;
173 
174 	ip = VTOI(vp);
175 
176 	if (bn >= 0 && bn < NDADDR) {
177 		if (nump != NULL)
178 			*nump = 0;
179 		*bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
180 		if (*bnp == 0)
181 			*bnp = -1;
182 		return (0);
183 	}
184 	xap = ap == NULL ? a : ap;
185 	if (!nump)
186 		nump = &num;
187 	if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
188 		return (error);
189 
190 	num = *nump;
191 
192 	/* Get disk address out of indirect block array */
193 	daddr = ip->i_ffs1_ib[xap->in_off];
194 
195 	for (bp = NULL, ++xap; --num; ++xap) {
196 		/* Exit the loop if there is no disk address assigned yet and
197 		 * the indirect block isn't in the cache, or if we were
198 		 * looking for an indirect block and we've found it. */
199 
200 		metalbn = xap->in_lbn;
201 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
202 			break;
203 		/*
204 		 * If we get here, we've either got the block in the cache
205 		 * or we have a disk address for it, go fetch it.
206 		 */
207 		if (bp)
208 			brelse(bp);
209 
210 		xap->in_exists = 1;
211 		bp = getblk(vp, metalbn, fs->lfs_bsize);
212 
213 		if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
214 			bp->b_blkno = fsbtodb(fs, daddr);
215 			bp->b_flags |= B_READ;
216 			VOP_STRATEGY(bp);
217 		}
218 		daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
219 	}
220 	if (bp)
221 		brelse(bp);
222 
223 	daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
224 	*bnp = daddr == 0 ? -1 : daddr;
225 	return (0);
226 }
227 
228 /*
229  * Create an array of logical block number/offset pairs which represent the
230  * path of indirect blocks required to access a data block.  The first "pair"
231  * contains the logical block number of the appropriate single, double or
232  * triple indirect block and the offset into the inode indirect block array.
233  * Note, the logical block number of the inode single/double/triple indirect
234  * block appears twice in the array, once with the offset into the i_ffs1_ib and
235  * once with the offset into the page itself.
236  */
237 int
238 ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
239 {
240 	daddr_t metalbn, realbn;
241 	int64_t blockcnt;
242 	int lbc;
243 	int i, numlevels, off;
244 	int lognindir, indir;
245 
246 	if (nump)
247 		*nump = 0;
248 	numlevels = 0;
249 	realbn = bn;
250 	if (bn < 0)
251 		bn = -bn;
252 
253 	lognindir = -1;
254 	for (indir = fs->lfs_nindir; indir; indir >>= 1)
255 		++lognindir;
256 
257 	/* Determine the number of levels of indirection.  After this loop is
258 	 * done, blockcnt indicates the number of data blocks possible at the
259 	 * given level of indirection, and NIADDR - i is the number of levels
260 	 * of indirection needed to locate the requested block. */
261 
262 	bn -= NDADDR;
263 	for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
264 		if (i == 0)
265 			return (EFBIG);
266 
267 		lbc += lognindir;
268 		blockcnt = (int64_t) 1 << lbc;
269 
270 		if (bn < blockcnt)
271 			break;
272 	}
273 
274 	/* Calculate the address of the first meta-block. */
275 	if (realbn >= 0)
276 		metalbn = -(realbn - bn + NIADDR - i);
277 	else
278 		metalbn = -(-realbn - bn + NIADDR - i);
279 
280 	/* At each iteration, off is the offset into the bap array which is an
281 	 * array of disk addresses at the current level of indirection. The
282 	 * logical block number and the offset in that block are stored into
283 	 * the argument array. */
284 	ap->in_lbn = metalbn;
285 	ap->in_off = off = NIADDR - i;
286 	ap->in_exists = 0;
287 	ap++;
288 	for (++numlevels; i <= NIADDR; i++) {
289 		/* If searching for a meta-data block, quit when found. */
290 		if (metalbn == realbn)
291 			break;
292 
293 		lbc -= lognindir;
294 		blockcnt = (int64_t) 1 << lbc;
295 		off = (bn >> lbc) & (fs->lfs_nindir - 1);
296 
297 		++numlevels;
298 		ap->in_lbn = metalbn;
299 		ap->in_off = off;
300 		ap->in_exists = 0;
301 		++ap;
302 
303 		metalbn -= -1 + (off << lbc);
304 	}
305 	if (nump)
306 		*nump = numlevels;
307 	return (0);
308 }
309 
310 int
311 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
312 {
313 	return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
314 }
315 
316 /* Search a block for a specific dinode. */
317 struct ufs1_dinode *
318 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
319 {
320 	struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data;
321 	struct ufs1_dinode *ldip, *fin;
322 
323 	fin = dip + INOPB(fs);
324 
325 	/*
326 	 * Read the inode block backwards, since later versions of the
327 	 * inode will supercede earlier ones.  Though it is unlikely, it is
328 	 * possible that the same inode will appear in the same inode block.
329 	 */
330 	for (ldip = fin - 1; ldip >= dip; --ldip)
331 		if (ldip->di_inumber == ino)
332 			return (ldip);
333 	return NULL;
334 }
335 
336 /*
337  * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
338  * XXX it currently loses atime information.
339  */
340 struct uvnode *
341 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
342 {
343 	struct uvnode *vp;
344 	struct inode *ip;
345 	struct ufs1_dinode *dip;
346 	struct ubuf *bp;
347 	int i;
348 
349 	vp = (struct uvnode *) malloc(sizeof(*vp));
350 	memset(vp, 0, sizeof(*vp));
351 	vp->v_fd = fd;
352 	vp->v_fs = fs;
353 	vp->v_usecount = 0;
354 	vp->v_strategy_op = lfs_vop_strategy;
355 	vp->v_bwrite_op = lfs_vop_bwrite;
356 	vp->v_bmap_op = lfs_vop_bmap;
357 
358 	++nvnodes;
359 	LIST_INSERT_HEAD(&getvnodelist, vp, v_getvnodes);
360 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
361 
362 	vp->v_data = ip = (struct inode *) malloc(sizeof(*ip));
363 	memset(ip, 0, sizeof(*ip));
364 
365 	ip->i_din.ffs1_din = (struct ufs1_dinode *)
366 	    malloc(sizeof(struct ufs1_dinode));
367 	memset(ip->i_din.ffs1_din, 0, sizeof (struct ufs1_dinode));
368 
369 	/* Initialize the inode -- from lfs_vcreate. */
370 	ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext));
371 	memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
372 	vp->v_data = ip;
373 	/* ip->i_vnode = vp; */
374 	ip->i_number = ino;
375 	ip->i_lockf = 0;
376 	ip->i_diroff = 0;
377 	ip->i_lfs_effnblks = 0;
378 	ip->i_flag = 0;
379 
380 	/* Load inode block and find inode */
381 	bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
382 	bp->b_flags |= B_AGE;
383 	dip = lfs_ifind(fs, ino, bp);
384 	if (dip == NULL) {
385 		brelse(bp);
386 		free(vp);
387 		return NULL;
388 	}
389 	memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
390 	brelse(bp);
391 	ip->i_number = ino;
392 	/* ip->i_devvp = fs->lfs_unlockvp; */
393 	ip->i_lfs = fs;
394 
395 	ip->i_ffs_effnlink = ip->i_ffs1_nlink;
396 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
397 	ip->i_lfs_osize = ip->i_ffs1_size;
398 #if 0
399 	if (fs->lfs_version > 1) {
400 		ip->i_ffs1_atime = ts.tv_sec;
401 		ip->i_ffs1_atimensec = ts.tv_nsec;
402 	}
403 #endif
404 
405 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
406 	for (i = 0; i < NDADDR; i++)
407 		if (ip->i_ffs1_db[i] != 0)
408 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
409 
410 	return vp;
411 }
412 
413 static struct uvnode *
414 lfs_vget(void *vfs, ino_t ino)
415 {
416 	struct lfs *fs = (struct lfs *)vfs;
417 	ufs_daddr_t daddr;
418 	struct ubuf *bp;
419 	IFILE *ifp;
420 
421 	LFS_IENTRY(ifp, fs, ino, bp);
422 	daddr = ifp->if_daddr;
423 	brelse(bp);
424 	if (daddr == 0)
425 		return NULL;
426 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
427 }
428 
429 /* Check superblock magic number and checksum */
430 static int
431 check_sb(struct lfs *fs)
432 {
433 	u_int32_t checksum;
434 
435 	if (fs->lfs_magic != LFS_MAGIC) {
436 		printf("Superblock magic number (0x%lx) does not match "
437 		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
438 		       (unsigned long) LFS_MAGIC);
439 		return 1;
440 	}
441 	/* checksum */
442 	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
443 	if (fs->lfs_cksum != checksum) {
444 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
445 		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
446 		return 1;
447 	}
448 	return 0;
449 }
450 
451 /* Initialize LFS library; load superblocks and choose which to use. */
452 struct lfs *
453 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
454 {
455 	struct uvnode *devvp;
456 	struct ubuf *bp;
457 	int tryalt;
458 	struct lfs *fs, *altfs;
459 	int error;
460 
461 	vfs_init();
462 
463 	devvp = (struct uvnode *) malloc(sizeof(*devvp));
464 	devvp->v_fs = NULL;
465 	devvp->v_fd = devfd;
466 	devvp->v_strategy_op = raw_vop_strategy;
467 	devvp->v_bwrite_op = raw_vop_bwrite;
468 	devvp->v_bmap_op = raw_vop_bmap;
469 
470 	tryalt = 0;
471 	if (sblkno == 0) {
472 		sblkno = btodb(LFS_LABELPAD);
473 		tryalt = 1;
474 	} else if (debug) {
475 		printf("No -b flag given, not attempting to verify checkpoint\n");
476 	}
477 	error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
478 	fs = (struct lfs *) malloc(sizeof(*fs));
479 	*fs = *((struct lfs *) bp->b_data);
480 	fs->lfs_unlockvp = devvp;
481 	bp->b_flags |= B_INVAL;
482 	brelse(bp);
483 
484 	if (tryalt) {
485 		error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
486 		    LFS_SBPAD, NOCRED, &bp);
487 		altfs = (struct lfs *) malloc(sizeof(*fs));
488 		*altfs = *((struct lfs *) bp->b_data);
489 		altfs->lfs_unlockvp = devvp;
490 		bp->b_flags |= B_INVAL;
491 		brelse(bp);
492 
493 		if (check_sb(fs)) {
494 			if (debug)
495 				printf("Primary superblock is no good, using first alternate\n");
496 			free(fs);
497 			fs = altfs;
498 		} else {
499 			/* If both superblocks check out, try verification */
500 			if (check_sb(altfs)) {
501 				if (debug)
502 					printf("First alternate superblock is no good, using primary\n");
503 				free(altfs);
504 			} else {
505 				if (lfs_verify(fs, altfs, devvp, debug) == fs) {
506 					free(altfs);
507 				} else {
508 					free(fs);
509 					fs = altfs;
510 				}
511 			}
512 		}
513 	}
514 	if (check_sb(fs)) {
515 		free(fs);
516 		return NULL;
517 	}
518 	/* Compatibility */
519 	if (fs->lfs_version < 2) {
520 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
521 		fs->lfs_ibsize = fs->lfs_bsize;
522 		fs->lfs_start = fs->lfs_sboffs[0];
523 		fs->lfs_tstamp = fs->lfs_otstamp;
524 		fs->lfs_fsbtodb = 0;
525 	}
526 	fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
527 	fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
528 	fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
529 
530 	if (idaddr == 0)
531 		idaddr = fs->lfs_idaddr;
532 	fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr);
533 
534 	register_vget((void *)fs, lfs_vget);
535 
536 	return fs;
537 }
538 
539 /*
540  * Check partial segment validity between fs->lfs_offset and the given goal.
541  * If goal == 0, just keep on going until the segments stop making sense.
542  * Return the address of the first partial segment that failed.
543  */
544 ufs_daddr_t
545 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
546 {
547 	ufs_daddr_t daddr, odaddr;
548 	SEGSUM *sp;
549 	int bc, flag;
550 	struct ubuf *bp;
551 	ufs_daddr_t nodirop_daddr;
552 	u_int64_t serial;
553 
554 	daddr = osb->lfs_offset;
555 	nodirop_daddr = daddr;
556 	serial = osb->lfs_serial;
557 	while (daddr != goal) {
558 		flag = 0;
559 oncemore:
560 		/* Read in summary block */
561 		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
562 		sp = (SEGSUM *)bp->b_data;
563 
564 		/*
565 		 * Could be a superblock instead of a segment summary.
566 		 * XXX should use gseguse, but right now we need to do more
567 		 * setup before we can...fix this
568 		 */
569 		if (sp->ss_magic != SS_MAGIC ||
570 		    sp->ss_ident != osb->lfs_ident ||
571 		    sp->ss_serial < serial ||
572 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
573 			sizeof(sp->ss_sumsum))) {
574 			brelse(bp);
575 			if (flag == 0) {
576 				flag = 1;
577 				daddr += btofsb(osb, LFS_SBPAD);
578 				goto oncemore;
579 			}
580 			break;
581 		}
582 		++serial;
583 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
584 		if (bc == 0) {
585 			brelse(bp);
586 			break;
587 		}
588 		assert (bc > 0);
589 		odaddr = daddr;
590 		daddr += btofsb(osb, osb->lfs_sumsize + bc);
591 		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
592 		    dtosn(osb, daddr) != dtosn(osb, daddr +
593 			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
594 			daddr = sp->ss_next;
595 		}
596 		if (!(sp->ss_flags & SS_CONT))
597 			nodirop_daddr = daddr;
598 		brelse(bp);
599 	}
600 
601 	if (goal == 0)
602 		return nodirop_daddr;
603 	else
604 		return daddr;
605 }
606 
607 /* Use try_verify to check whether the newer superblock is valid. */
608 struct lfs *
609 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
610 {
611 	ufs_daddr_t daddr;
612 	struct lfs *osb, *nsb;
613 
614 	/*
615 	 * Verify the checkpoint of the newer superblock,
616 	 * if the timestamp/serial number of the two superblocks is
617 	 * different.
618 	 */
619 
620 	if (debug)
621 		printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
622 		    (long long) sb1->lfs_serial);
623 
624 	if ((sb0->lfs_version == 1 &&
625 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
626 	    (sb0->lfs_version > 1 &&
627 		sb0->lfs_serial != sb1->lfs_serial)) {
628 		if (sb0->lfs_version == 1) {
629 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
630 				osb = sb1;
631 				nsb = sb0;
632 			} else {
633 				osb = sb0;
634 				nsb = sb1;
635 			}
636 		} else {
637 			if (sb0->lfs_serial > sb1->lfs_serial) {
638 				osb = sb1;
639 				nsb = sb0;
640 			} else {
641 				osb = sb0;
642 				nsb = sb1;
643 			}
644 		}
645 		if (debug) {
646 			printf("Attempting to verify newer checkpoint...");
647 			fflush(stdout);
648 		}
649 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
650 
651 		if (debug)
652 			printf("done.\n");
653 		if (daddr == nsb->lfs_offset) {
654 			warnx("** Newer checkpoint verified, recovered %lld seconds of data\n",
655 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
656 			sbdirty();
657 		} else {
658 			warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
659 		}
660 		return (daddr == nsb->lfs_offset ? nsb : osb);
661 	}
662 	/* Nothing to check */
663 	return osb;
664 }
665 
666 /* Verify a partial-segment summary; return the number of bytes on disk. */
667 int
668 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
669 	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
670 {
671 	FINFO *fp;
672 	int bc;			/* Bytes in partial segment */
673 	int nblocks;
674 	ufs_daddr_t seg_addr, daddr;
675 	ufs_daddr_t *dp, *idp;
676 	struct ubuf *bp;
677 	int i, j, k, datac, len;
678 	long sn;
679 	u_int32_t *datap;
680 	u_int32_t ccksum;
681 
682 	sn = dtosn(fs, pseg_addr);
683 	seg_addr = sntod(fs, sn);
684 
685 	/* We've already checked the sumsum, just do the data bounds and sum */
686 
687 	/* Count the blocks. */
688 	nblocks = howmany(sp->ss_ninos, INOPB(fs));
689 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
690 	assert(bc >= 0);
691 
692 	fp = (FINFO *) (sp + 1);
693 	for (i = 0; i < sp->ss_nfinfo; i++) {
694 		nblocks += fp->fi_nblocks;
695 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
696 					   << fs->lfs_bshift);
697 		assert(bc >= 0);
698 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
699 	}
700 	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
701 	datac = 0;
702 
703 	dp = (ufs_daddr_t *) sp;
704 	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
705 	dp--;
706 
707 	idp = dp;
708 	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
709 	fp = (FINFO *) (sp + 1);
710 	for (i = 0, j = 0;
711 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
712 		if (i >= sp->ss_nfinfo && *idp != daddr) {
713 			warnx("Not enough inode blocks in pseg at 0x%" PRIx32
714 			      ": found %d, wanted %d\n",
715 			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
716 			if (debug)
717 				warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp,
718 				      daddr);
719 			break;
720 		}
721 		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
722 			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
723 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
724 			brelse(bp);
725 
726 			++j;
727 			daddr += btofsb(fs, fs->lfs_ibsize);
728 			--idp;
729 		}
730 		if (i < sp->ss_nfinfo) {
731 			if (func)
732 				func(daddr, fp);
733 			for (k = 0; k < fp->fi_nblocks; k++) {
734 				len = (k == fp->fi_nblocks - 1 ?
735 				       fp->fi_lastlength
736 				       : fs->lfs_bsize);
737 				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
738 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
739 				brelse(bp);
740 				daddr += btofsb(fs, len);
741 			}
742 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
743 		}
744 	}
745 
746 	if (datac != nblocks) {
747 		warnx("Partial segment at 0x%llx expected %d blocks counted %d\n",
748 		    (long long) pseg_addr, nblocks, datac);
749 	}
750 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
751 	/* Check the data checksum */
752 	if (ccksum != sp->ss_datasum) {
753 		warnx("Partial segment at 0x%" PRIx32 " data checksum"
754 		      " mismatch: given 0x%x, computed 0x%x\n",
755 		      pseg_addr, sp->ss_datasum, ccksum);
756 		free(datap);
757 		return 0;
758 	}
759 	free(datap);
760 	assert(bc >= 0);
761 	return bc;
762 }
763 
764 /* print message and exit */
765 void
766 my_vpanic(int fatal, const char *fmt, va_list ap)
767 {
768         (void) vprintf(fmt, ap);
769 	exit(8);
770 }
771 
772 void
773 call_panic(const char *fmt, ...)
774 {
775 	va_list ap;
776 
777 	va_start(ap, fmt);
778         panic_func(1, fmt, ap);
779 	va_end(ap);
780 }
781