Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.1
      1 /* $NetBSD: lfs.c,v 1.1 2003/03/28 08:09:53 perseant 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 (at) 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_ffs_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_ffs_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_ffs_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 dinode *
    318 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
    319 {
    320 	struct dinode *dip = (struct dinode *) bp->b_data;
    321 	struct 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 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 	/* Initialize the inode -- from lfs_vcreate. */
    366 	ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext));
    367 	memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
    368 	vp->v_data = ip;
    369 	/* ip->i_vnode = vp; */
    370 	ip->i_number = ino;
    371 	ip->i_lockf = 0;
    372 	ip->i_diroff = 0;
    373 	ip->i_ffs_mode = 0;
    374 	ip->i_ffs_size = 0;
    375 	ip->i_ffs_blocks = 0;
    376 	ip->i_lfs_effnblks = 0;
    377 	ip->i_flag = 0;
    378 
    379 	/* Load inode block and find inode */
    380 	bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
    381 	bp->b_flags |= B_AGE;
    382 	dip = lfs_ifind(fs, ino, bp);
    383 	if (dip == NULL) {
    384 		brelse(bp);
    385 		free(vp);
    386 		return NULL;
    387 	}
    388 	memcpy(&ip->i_din.ffs_din, dip, sizeof(*dip));
    389 	brelse(bp);
    390 	ip->i_number = ino;
    391 	/* ip->i_devvp = fs->lfs_unlockvp; */
    392 	ip->i_lfs = fs;
    393 
    394 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
    395 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
    396 	ip->i_lfs_osize = ip->i_ffs_size;
    397 #if 0
    398 	if (fs->lfs_version > 1) {
    399 		ip->i_ffs_atime = ts.tv_sec;
    400 		ip->i_ffs_atimensec = ts.tv_nsec;
    401 	}
    402 #endif
    403 
    404 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    405 	for (i = 0; i < NDADDR; i++)
    406 		if (ip->i_ffs_db[i] != 0)
    407 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
    408 
    409 	return vp;
    410 }
    411 
    412 static struct uvnode *
    413 lfs_vget(void *vfs, ino_t ino)
    414 {
    415 	struct lfs *fs = (struct lfs *)vfs;
    416 	ufs_daddr_t daddr;
    417 	struct ubuf *bp;
    418 	IFILE *ifp;
    419 
    420 	LFS_IENTRY(ifp, fs, ino, bp);
    421 	daddr = ifp->if_daddr;
    422 	brelse(bp);
    423 	if (daddr == 0)
    424 		return NULL;
    425 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
    426 }
    427 
    428 /* Check superblock magic number and checksum */
    429 static int
    430 check_sb(struct lfs *fs)
    431 {
    432 	u_int32_t checksum;
    433 
    434 	if (fs->lfs_magic != LFS_MAGIC) {
    435 		printf("Superblock magic number (0x%lx) does not match "
    436 		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
    437 		       (unsigned long) LFS_MAGIC);
    438 		return 1;
    439 	}
    440 	/* checksum */
    441 	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
    442 	if (fs->lfs_cksum != checksum) {
    443 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
    444 		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
    445 		return 1;
    446 	}
    447 	return 0;
    448 }
    449 
    450 /* Initialize LFS library; load superblocks and choose which to use. */
    451 struct lfs *
    452 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
    453 {
    454 	struct uvnode *devvp;
    455 	struct ubuf *bp;
    456 	int tryalt;
    457 	struct lfs *fs, *altfs;
    458 	int error;
    459 
    460 	vfs_init();
    461 
    462 	devvp = (struct uvnode *) malloc(sizeof(*devvp));
    463 	devvp->v_fs = NULL;
    464 	devvp->v_fd = devfd;
    465 	devvp->v_strategy_op = raw_vop_strategy;
    466 	devvp->v_bwrite_op = raw_vop_bwrite;
    467 	devvp->v_bmap_op = raw_vop_bmap;
    468 
    469 	tryalt = 0;
    470 	if (sblkno == 0) {
    471 		sblkno = btodb(LFS_LABELPAD);
    472 		tryalt = 1;
    473 	} else if (debug) {
    474 		printf("No -b flag given, not attempting to verify checkpoint\n");
    475 	}
    476 	error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
    477 	fs = (struct lfs *) malloc(sizeof(*fs));
    478 	*fs = *((struct lfs *) bp->b_data);
    479 	fs->lfs_unlockvp = devvp;
    480 	bp->b_flags |= B_INVAL;
    481 	brelse(bp);
    482 
    483 	if (tryalt) {
    484 		error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
    485 		    LFS_SBPAD, NOCRED, &bp);
    486 		altfs = (struct lfs *) malloc(sizeof(*fs));
    487 		*altfs = *((struct lfs *) bp->b_data);
    488 		altfs->lfs_unlockvp = devvp;
    489 		bp->b_flags |= B_INVAL;
    490 		brelse(bp);
    491 
    492 		if (check_sb(fs)) {
    493 			if (debug)
    494 				printf("Primary superblock is no good, using first alternate\n");
    495 			free(fs);
    496 			fs = altfs;
    497 		} else {
    498 			/* If both superblocks check out, try verification */
    499 			if (check_sb(altfs)) {
    500 				if (debug)
    501 					printf("First alternate superblock is no good, using primary\n");
    502 				free(altfs);
    503 			} else {
    504 				if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    505 					free(altfs);
    506 				} else {
    507 					free(fs);
    508 					fs = altfs;
    509 				}
    510 			}
    511 		}
    512 	}
    513 	if (check_sb(fs)) {
    514 		free(fs);
    515 		return NULL;
    516 	}
    517 	/* Compatibility */
    518 	if (fs->lfs_version < 2) {
    519 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    520 		fs->lfs_ibsize = fs->lfs_bsize;
    521 		fs->lfs_start = fs->lfs_sboffs[0];
    522 		fs->lfs_tstamp = fs->lfs_otstamp;
    523 		fs->lfs_fsbtodb = 0;
    524 	}
    525 	fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
    526 	fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    527 	fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    528 
    529 	if (idaddr == 0)
    530 		idaddr = fs->lfs_idaddr;
    531 	fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr);
    532 
    533 	register_vget((void *)fs, lfs_vget);
    534 
    535 	return fs;
    536 }
    537 
    538 /*
    539  * Check partial segment validity between fs->lfs_offset and the given goal.
    540  * If goal == 0, just keep on going until the segments stop making sense.
    541  * Return the address of the first partial segment that failed.
    542  */
    543 ufs_daddr_t
    544 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
    545 {
    546 	ufs_daddr_t daddr, odaddr;
    547 	SEGSUM *sp;
    548 	int bc, flag;
    549 	struct ubuf *bp;
    550 	ufs_daddr_t nodirop_daddr;
    551 	u_int64_t serial;
    552 
    553 	daddr = osb->lfs_offset;
    554 	nodirop_daddr = daddr;
    555 	serial = osb->lfs_serial;
    556 	while (daddr != goal) {
    557 		flag = 0;
    558 oncemore:
    559 		/* Read in summary block */
    560 		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
    561 		sp = (SEGSUM *)bp->b_data;
    562 
    563 		/*
    564 		 * Could be a superblock instead of a segment summary.
    565 		 * XXX should use gseguse, but right now we need to do more
    566 		 * setup before we can...fix this
    567 		 */
    568 		if (sp->ss_magic != SS_MAGIC ||
    569 		    sp->ss_ident != osb->lfs_ident ||
    570 		    sp->ss_serial < serial ||
    571 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
    572 			sizeof(sp->ss_sumsum))) {
    573 			brelse(bp);
    574 			if (flag == 0) {
    575 				flag = 1;
    576 				daddr += btofsb(osb, LFS_SBPAD);
    577 				goto oncemore;
    578 			}
    579 			break;
    580 		}
    581 		++serial;
    582 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    583 		if (bc == 0) {
    584 			brelse(bp);
    585 			break;
    586 		}
    587 		assert (bc > 0);
    588 		odaddr = daddr;
    589 		daddr += btofsb(osb, osb->lfs_sumsize + bc);
    590 		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
    591 		    dtosn(osb, daddr) != dtosn(osb, daddr +
    592 			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
    593 			daddr = sp->ss_next;
    594 		}
    595 		if (!(sp->ss_flags & SS_CONT))
    596 			nodirop_daddr = daddr;
    597 		brelse(bp);
    598 	}
    599 
    600 	if (goal == 0)
    601 		return nodirop_daddr;
    602 	else
    603 		return daddr;
    604 }
    605 
    606 /* Use try_verify to check whether the newer superblock is valid. */
    607 struct lfs *
    608 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    609 {
    610 	ufs_daddr_t daddr;
    611 	struct lfs *osb, *nsb;
    612 
    613 	/*
    614 	 * Verify the checkpoint of the newer superblock,
    615 	 * if the timestamp/serial number of the two superblocks is
    616 	 * different.
    617 	 */
    618 
    619 	if (debug)
    620 		printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
    621 		    (long long) sb1->lfs_serial);
    622 
    623 	if ((sb0->lfs_version == 1 &&
    624 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
    625 	    (sb0->lfs_version > 1 &&
    626 		sb0->lfs_serial != sb1->lfs_serial)) {
    627 		if (sb0->lfs_version == 1) {
    628 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
    629 				osb = sb1;
    630 				nsb = sb0;
    631 			} else {
    632 				osb = sb0;
    633 				nsb = sb1;
    634 			}
    635 		} else {
    636 			if (sb0->lfs_serial > sb1->lfs_serial) {
    637 				osb = sb1;
    638 				nsb = sb0;
    639 			} else {
    640 				osb = sb0;
    641 				nsb = sb1;
    642 			}
    643 		}
    644 		if (debug) {
    645 			printf("Attempting to verify newer checkpoint...");
    646 			fflush(stdout);
    647 		}
    648 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
    649 
    650 		if (debug)
    651 			printf("done.\n");
    652 		if (daddr == nsb->lfs_offset) {
    653 			warnx("** Newer checkpoint verified, recovered %lld seconds of data\n",
    654 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    655 			sbdirty();
    656 		} else {
    657 			warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    658 		}
    659 		return (daddr == nsb->lfs_offset ? nsb : osb);
    660 	}
    661 	/* Nothing to check */
    662 	return osb;
    663 }
    664 
    665 /* Verify a partial-segment summary; return the number of bytes on disk. */
    666 int
    667 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
    668 	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
    669 {
    670 	FINFO *fp;
    671 	int bc;			/* Bytes in partial segment */
    672 	int nblocks;
    673 	ufs_daddr_t seg_addr, daddr;
    674 	ufs_daddr_t *dp, *idp;
    675 	struct ubuf *bp;
    676 	int i, j, k, datac, len;
    677 	long sn;
    678 	u_int32_t *datap;
    679 	u_int32_t ccksum;
    680 
    681 	sn = dtosn(fs, pseg_addr);
    682 	seg_addr = sntod(fs, sn);
    683 
    684 	/* We've already checked the sumsum, just do the data bounds and sum */
    685 
    686 	/* Count the blocks. */
    687 	nblocks = howmany(sp->ss_ninos, INOPB(fs));
    688 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
    689 	assert(bc >= 0);
    690 
    691 	fp = (FINFO *) (sp + 1);
    692 	for (i = 0; i < sp->ss_nfinfo; i++) {
    693 		nblocks += fp->fi_nblocks;
    694 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    695 					   << fs->lfs_bshift);
    696 		assert(bc >= 0);
    697 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    698 	}
    699 	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
    700 	datac = 0;
    701 
    702 	dp = (ufs_daddr_t *) sp;
    703 	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
    704 	dp--;
    705 
    706 	idp = dp;
    707 	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
    708 	fp = (FINFO *) (sp + 1);
    709 	for (i = 0, j = 0;
    710 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
    711 		if (i >= sp->ss_nfinfo && *idp != daddr) {
    712 			warnx("Not enough inode blocks in pseg at 0x" PRIx32
    713 			      ": found %d, wanted %d\n",
    714 			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
    715 			if (debug)
    716 				warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    717 				      daddr);
    718 			break;
    719 		}
    720 		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
    721 			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
    722 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    723 			brelse(bp);
    724 
    725 			++j;
    726 			daddr += btofsb(fs, fs->lfs_ibsize);
    727 			--idp;
    728 		}
    729 		if (i < sp->ss_nfinfo) {
    730 			if (func)
    731 				func(daddr, fp);
    732 			for (k = 0; k < fp->fi_nblocks; k++) {
    733 				len = (k == fp->fi_nblocks - 1 ?
    734 				       fp->fi_lastlength
    735 				       : fs->lfs_bsize);
    736 				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
    737 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    738 				brelse(bp);
    739 				daddr += btofsb(fs, len);
    740 			}
    741 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    742 		}
    743 	}
    744 
    745 	if (datac != nblocks) {
    746 		warnx("Partial segment at 0x%llx expected %d blocks counted %d\n",
    747 		    (long long) pseg_addr, nblocks, datac);
    748 	}
    749 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    750 	/* Check the data checksum */
    751 	if (ccksum != sp->ss_datasum) {
    752 		warnx("Partial segment at 0x%" PRIx32 " data checksum"
    753 		      " mismatch: given 0x%x, computed 0x%x\n",
    754 		      pseg_addr, sp->ss_datasum, ccksum);
    755 		free(datap);
    756 		return 0;
    757 	}
    758 	free(datap);
    759 	assert(bc >= 0);
    760 	return bc;
    761 }
    762 
    763 /* print message and exit */
    764 void
    765 my_vpanic(int fatal, const char *fmt, va_list ap)
    766 {
    767         (void) vprintf(fmt, ap);
    768 	exit(8);
    769 }
    770 
    771 void
    772 call_panic(const char *fmt, ...)
    773 {
    774 	va_list ap;
    775 
    776 	va_start(ap, fmt);
    777         panic_func(1, fmt, ap);
    778 	va_end(ap);
    779 }
    780