Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.4
      1 /* $NetBSD: lfs.c,v 1.4 2003/07/12 11:47:05 yamt 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_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 	memset(fs, 0, sizeof(*fs));
    480 	fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    481 	fs->lfs_unlockvp = devvp;
    482 	bp->b_flags |= B_INVAL;
    483 	brelse(bp);
    484 
    485 	if (tryalt) {
    486 		error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
    487 		    LFS_SBPAD, NOCRED, &bp);
    488 		altfs = (struct lfs *) malloc(sizeof(*altfs));
    489 		memset(altfs, 0, sizeof(*altfs));
    490 		altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    491 		altfs->lfs_unlockvp = devvp;
    492 		bp->b_flags |= B_INVAL;
    493 		brelse(bp);
    494 
    495 		if (check_sb(fs)) {
    496 			if (debug)
    497 				printf("Primary superblock is no good, using first alternate\n");
    498 			free(fs);
    499 			fs = altfs;
    500 		} else {
    501 			/* If both superblocks check out, try verification */
    502 			if (check_sb(altfs)) {
    503 				if (debug)
    504 					printf("First alternate superblock is no good, using primary\n");
    505 				free(altfs);
    506 			} else {
    507 				if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    508 					free(altfs);
    509 				} else {
    510 					free(fs);
    511 					fs = altfs;
    512 				}
    513 			}
    514 		}
    515 	}
    516 	if (check_sb(fs)) {
    517 		free(fs);
    518 		return NULL;
    519 	}
    520 	/* Compatibility */
    521 	if (fs->lfs_version < 2) {
    522 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    523 		fs->lfs_ibsize = fs->lfs_bsize;
    524 		fs->lfs_start = fs->lfs_sboffs[0];
    525 		fs->lfs_tstamp = fs->lfs_otstamp;
    526 		fs->lfs_fsbtodb = 0;
    527 	}
    528 	fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
    529 	fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    530 	fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    531 
    532 	if (idaddr == 0)
    533 		idaddr = fs->lfs_idaddr;
    534 	fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr);
    535 
    536 	register_vget((void *)fs, lfs_vget);
    537 
    538 	return fs;
    539 }
    540 
    541 /*
    542  * Check partial segment validity between fs->lfs_offset and the given goal.
    543  * If goal == 0, just keep on going until the segments stop making sense.
    544  * Return the address of the first partial segment that failed.
    545  */
    546 ufs_daddr_t
    547 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
    548 {
    549 	ufs_daddr_t daddr, odaddr;
    550 	SEGSUM *sp;
    551 	int bc, flag;
    552 	struct ubuf *bp;
    553 	ufs_daddr_t nodirop_daddr;
    554 	u_int64_t serial;
    555 
    556 	daddr = osb->lfs_offset;
    557 	nodirop_daddr = daddr;
    558 	serial = osb->lfs_serial;
    559 	while (daddr != goal) {
    560 		flag = 0;
    561 oncemore:
    562 		/* Read in summary block */
    563 		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
    564 		sp = (SEGSUM *)bp->b_data;
    565 
    566 		/*
    567 		 * Could be a superblock instead of a segment summary.
    568 		 * XXX should use gseguse, but right now we need to do more
    569 		 * setup before we can...fix this
    570 		 */
    571 		if (sp->ss_magic != SS_MAGIC ||
    572 		    sp->ss_ident != osb->lfs_ident ||
    573 		    sp->ss_serial < serial ||
    574 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
    575 			sizeof(sp->ss_sumsum))) {
    576 			brelse(bp);
    577 			if (flag == 0) {
    578 				flag = 1;
    579 				daddr += btofsb(osb, LFS_SBPAD);
    580 				goto oncemore;
    581 			}
    582 			break;
    583 		}
    584 		++serial;
    585 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    586 		if (bc == 0) {
    587 			brelse(bp);
    588 			break;
    589 		}
    590 		assert (bc > 0);
    591 		odaddr = daddr;
    592 		daddr += btofsb(osb, osb->lfs_sumsize + bc);
    593 		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
    594 		    dtosn(osb, daddr) != dtosn(osb, daddr +
    595 			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
    596 			daddr = sp->ss_next;
    597 		}
    598 		if (!(sp->ss_flags & SS_CONT))
    599 			nodirop_daddr = daddr;
    600 		brelse(bp);
    601 	}
    602 
    603 	if (goal == 0)
    604 		return nodirop_daddr;
    605 	else
    606 		return daddr;
    607 }
    608 
    609 /* Use try_verify to check whether the newer superblock is valid. */
    610 struct lfs *
    611 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    612 {
    613 	ufs_daddr_t daddr;
    614 	struct lfs *osb, *nsb;
    615 
    616 	/*
    617 	 * Verify the checkpoint of the newer superblock,
    618 	 * if the timestamp/serial number of the two superblocks is
    619 	 * different.
    620 	 */
    621 
    622 	if (debug)
    623 		printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
    624 		    (long long) sb1->lfs_serial);
    625 
    626 	if ((sb0->lfs_version == 1 &&
    627 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
    628 	    (sb0->lfs_version > 1 &&
    629 		sb0->lfs_serial != sb1->lfs_serial)) {
    630 		if (sb0->lfs_version == 1) {
    631 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
    632 				osb = sb1;
    633 				nsb = sb0;
    634 			} else {
    635 				osb = sb0;
    636 				nsb = sb1;
    637 			}
    638 		} else {
    639 			if (sb0->lfs_serial > sb1->lfs_serial) {
    640 				osb = sb1;
    641 				nsb = sb0;
    642 			} else {
    643 				osb = sb0;
    644 				nsb = sb1;
    645 			}
    646 		}
    647 		if (debug) {
    648 			printf("Attempting to verify newer checkpoint...");
    649 			fflush(stdout);
    650 		}
    651 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
    652 
    653 		if (debug)
    654 			printf("done.\n");
    655 		if (daddr == nsb->lfs_offset) {
    656 			warnx("** Newer checkpoint verified, recovered %lld seconds of data\n",
    657 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    658 			sbdirty();
    659 		} else {
    660 			warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    661 		}
    662 		return (daddr == nsb->lfs_offset ? nsb : osb);
    663 	}
    664 	/* Nothing to check */
    665 	return osb;
    666 }
    667 
    668 /* Verify a partial-segment summary; return the number of bytes on disk. */
    669 int
    670 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
    671 	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
    672 {
    673 	FINFO *fp;
    674 	int bc;			/* Bytes in partial segment */
    675 	int nblocks;
    676 	ufs_daddr_t seg_addr, daddr;
    677 	ufs_daddr_t *dp, *idp;
    678 	struct ubuf *bp;
    679 	int i, j, k, datac, len;
    680 	long sn;
    681 	u_int32_t *datap;
    682 	u_int32_t ccksum;
    683 
    684 	sn = dtosn(fs, pseg_addr);
    685 	seg_addr = sntod(fs, sn);
    686 
    687 	/* We've already checked the sumsum, just do the data bounds and sum */
    688 
    689 	/* Count the blocks. */
    690 	nblocks = howmany(sp->ss_ninos, INOPB(fs));
    691 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
    692 	assert(bc >= 0);
    693 
    694 	fp = (FINFO *) (sp + 1);
    695 	for (i = 0; i < sp->ss_nfinfo; i++) {
    696 		nblocks += fp->fi_nblocks;
    697 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    698 					   << fs->lfs_bshift);
    699 		assert(bc >= 0);
    700 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    701 	}
    702 	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
    703 	datac = 0;
    704 
    705 	dp = (ufs_daddr_t *) sp;
    706 	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
    707 	dp--;
    708 
    709 	idp = dp;
    710 	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
    711 	fp = (FINFO *) (sp + 1);
    712 	for (i = 0, j = 0;
    713 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
    714 		if (i >= sp->ss_nfinfo && *idp != daddr) {
    715 			warnx("Not enough inode blocks in pseg at 0x%" PRIx32
    716 			      ": found %d, wanted %d\n",
    717 			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
    718 			if (debug)
    719 				warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    720 				      daddr);
    721 			break;
    722 		}
    723 		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
    724 			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
    725 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    726 			brelse(bp);
    727 
    728 			++j;
    729 			daddr += btofsb(fs, fs->lfs_ibsize);
    730 			--idp;
    731 		}
    732 		if (i < sp->ss_nfinfo) {
    733 			if (func)
    734 				func(daddr, fp);
    735 			for (k = 0; k < fp->fi_nblocks; k++) {
    736 				len = (k == fp->fi_nblocks - 1 ?
    737 				       fp->fi_lastlength
    738 				       : fs->lfs_bsize);
    739 				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
    740 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    741 				brelse(bp);
    742 				daddr += btofsb(fs, len);
    743 			}
    744 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    745 		}
    746 	}
    747 
    748 	if (datac != nblocks) {
    749 		warnx("Partial segment at 0x%llx expected %d blocks counted %d\n",
    750 		    (long long) pseg_addr, nblocks, datac);
    751 	}
    752 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    753 	/* Check the data checksum */
    754 	if (ccksum != sp->ss_datasum) {
    755 		warnx("Partial segment at 0x%" PRIx32 " data checksum"
    756 		      " mismatch: given 0x%x, computed 0x%x\n",
    757 		      pseg_addr, sp->ss_datasum, ccksum);
    758 		free(datap);
    759 		return 0;
    760 	}
    761 	free(datap);
    762 	assert(bc >= 0);
    763 	return bc;
    764 }
    765 
    766 /* print message and exit */
    767 void
    768 my_vpanic(int fatal, const char *fmt, va_list ap)
    769 {
    770         (void) vprintf(fmt, ap);
    771 	exit(8);
    772 }
    773 
    774 void
    775 call_panic(const char *fmt, ...)
    776 {
    777 	va_list ap;
    778 
    779 	va_start(ap, fmt);
    780         panic_func(1, fmt, ap);
    781 	va_end(ap);
    782 }
    783