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