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