Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.20
      1 /* $NetBSD: lfs.c,v 1.20 2006/03/17 15:53:46 rumble 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_user.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 extern void pwarn(const char *, ...);
    105 
    106 extern struct uvnodelst vnodelist;
    107 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
    108 extern int nvnodes;
    109 
    110 int fsdirty = 0;
    111 void (*panic_func)(int, const char *, va_list) = my_vpanic;
    112 
    113 /*
    114  * LFS buffer and uvnode operations
    115  */
    116 
    117 int
    118 lfs_vop_strategy(struct ubuf * bp)
    119 {
    120 	int count;
    121 
    122 	if (bp->b_flags & B_READ) {
    123 		count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    124 		    dbtob(bp->b_blkno));
    125 		if (count == bp->b_bcount)
    126 			bp->b_flags |= B_DONE;
    127 	} else {
    128 		count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    129 		    dbtob(bp->b_blkno));
    130 		if (count == 0) {
    131 			perror("pwrite");
    132 			return -1;
    133 		}
    134 		bp->b_flags &= ~B_DELWRI;
    135 		reassignbuf(bp, bp->b_vp);
    136 	}
    137 	return 0;
    138 }
    139 
    140 int
    141 lfs_vop_bwrite(struct ubuf * bp)
    142 {
    143 	struct lfs *fs;
    144 
    145 	fs = bp->b_vp->v_fs;
    146 	if (!(bp->b_flags & B_DELWRI)) {
    147 		fs->lfs_avail -= btofsb(fs, bp->b_bcount);
    148 	}
    149 	bp->b_flags |= B_DELWRI | B_LOCKED;
    150 	reassignbuf(bp, bp->b_vp);
    151 	brelse(bp);
    152 	return 0;
    153 }
    154 
    155 /*
    156  * ufs_bmaparray does the bmap conversion, and if requested returns the
    157  * array of logical blocks which must be traversed to get to a block.
    158  * Each entry contains the offset into that block that gets you to the
    159  * next block and the disk address of the block (if it is assigned).
    160  */
    161 int
    162 ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
    163 {
    164 	struct inode *ip;
    165 	struct ubuf *bp;
    166 	struct indir a[NIADDR + 1], *xap;
    167 	daddr_t daddr;
    168 	daddr_t metalbn;
    169 	int error, num;
    170 
    171 	ip = VTOI(vp);
    172 
    173 	if (bn >= 0 && bn < NDADDR) {
    174 		if (nump != NULL)
    175 			*nump = 0;
    176 		*bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
    177 		if (*bnp == 0)
    178 			*bnp = -1;
    179 		return (0);
    180 	}
    181 	xap = ap == NULL ? a : ap;
    182 	if (!nump)
    183 		nump = &num;
    184 	if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
    185 		return (error);
    186 
    187 	num = *nump;
    188 
    189 	/* Get disk address out of indirect block array */
    190 	daddr = ip->i_ffs1_ib[xap->in_off];
    191 
    192 	for (bp = NULL, ++xap; --num; ++xap) {
    193 		/* Exit the loop if there is no disk address assigned yet and
    194 		 * the indirect block isn't in the cache, or if we were
    195 		 * looking for an indirect block and we've found it. */
    196 
    197 		metalbn = xap->in_lbn;
    198 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
    199 			break;
    200 		/*
    201 		 * If we get here, we've either got the block in the cache
    202 		 * or we have a disk address for it, go fetch it.
    203 		 */
    204 		if (bp)
    205 			brelse(bp);
    206 
    207 		xap->in_exists = 1;
    208 		bp = getblk(vp, metalbn, fs->lfs_bsize);
    209 
    210 		if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
    211 			bp->b_blkno = fsbtodb(fs, daddr);
    212 			bp->b_flags |= B_READ;
    213 			VOP_STRATEGY(bp);
    214 		}
    215 		daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
    216 	}
    217 	if (bp)
    218 		brelse(bp);
    219 
    220 	daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
    221 	*bnp = daddr == 0 ? -1 : daddr;
    222 	return (0);
    223 }
    224 
    225 /*
    226  * Create an array of logical block number/offset pairs which represent the
    227  * path of indirect blocks required to access a data block.  The first "pair"
    228  * contains the logical block number of the appropriate single, double or
    229  * triple indirect block and the offset into the inode indirect block array.
    230  * Note, the logical block number of the inode single/double/triple indirect
    231  * block appears twice in the array, once with the offset into the i_ffs1_ib and
    232  * once with the offset into the page itself.
    233  */
    234 int
    235 ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
    236 {
    237 	daddr_t metalbn, realbn;
    238 	int64_t blockcnt;
    239 	int lbc;
    240 	int i, numlevels, off;
    241 	int lognindir, indir;
    242 
    243 	metalbn = 0;    /* XXXGCC -Wuninitialized [sh3] */
    244 
    245 	if (nump)
    246 		*nump = 0;
    247 	numlevels = 0;
    248 	realbn = bn;
    249 	if (bn < 0)
    250 		bn = -bn;
    251 
    252 	lognindir = -1;
    253 	for (indir = fs->lfs_nindir; indir; indir >>= 1)
    254 		++lognindir;
    255 
    256 	/* Determine the number of levels of indirection.  After this loop is
    257 	 * done, blockcnt indicates the number of data blocks possible at the
    258 	 * given level of indirection, and NIADDR - i is the number of levels
    259 	 * of indirection needed to locate the requested block. */
    260 
    261 	bn -= NDADDR;
    262 	for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
    263 		if (i == 0)
    264 			return (EFBIG);
    265 
    266 		lbc += lognindir;
    267 		blockcnt = (int64_t) 1 << lbc;
    268 
    269 		if (bn < blockcnt)
    270 			break;
    271 	}
    272 
    273 	/* Calculate the address of the first meta-block. */
    274 	metalbn = -((realbn >= 0 ? realbn : -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, hash;
    344 
    345 	vp = (struct uvnode *) malloc(sizeof(*vp));
    346 	if (vp == NULL)
    347 		err(1, NULL);
    348 	memset(vp, 0, sizeof(*vp));
    349 	vp->v_fd = fd;
    350 	vp->v_fs = fs;
    351 	vp->v_usecount = 0;
    352 	vp->v_strategy_op = lfs_vop_strategy;
    353 	vp->v_bwrite_op = lfs_vop_bwrite;
    354 	vp->v_bmap_op = lfs_vop_bmap;
    355 	LIST_INIT(&vp->v_cleanblkhd);
    356 	LIST_INIT(&vp->v_dirtyblkhd);
    357 
    358 	ip = (struct inode *) malloc(sizeof(*ip));
    359 	if (ip == NULL)
    360 		err(1, NULL);
    361 	memset(ip, 0, sizeof(*ip));
    362 
    363 	ip->i_din.ffs1_din = (struct ufs1_dinode *)
    364 	    malloc(sizeof(struct ufs1_dinode));
    365 	if (ip->i_din.ffs1_din == NULL)
    366 		err(1, NULL);
    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 	if (ip->inode_ext.lfs == NULL)
    372 		err(1, NULL);
    373 	memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
    374 	vp->v_data = ip;
    375 	/* ip->i_vnode = vp; */
    376 	ip->i_number = ino;
    377 	ip->i_lockf = 0;
    378 	ip->i_diroff = 0;
    379 	ip->i_lfs_effnblks = 0;
    380 	ip->i_flag = 0;
    381 
    382 	/* Load inode block and find inode */
    383 	if (daddr > 0) {
    384 		bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
    385 		bp->b_flags |= B_AGE;
    386 		dip = lfs_ifind(fs, ino, bp);
    387 		if (dip == NULL) {
    388 			brelse(bp);
    389 			free(ip);
    390 			free(vp);
    391 			return NULL;
    392 		}
    393 		memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
    394 		brelse(bp);
    395 	}
    396 	ip->i_number = ino;
    397 	/* ip->i_devvp = fs->lfs_devvp; */
    398 	ip->i_lfs = fs;
    399 
    400 	ip->i_ffs_effnlink = ip->i_ffs1_nlink;
    401 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
    402 	ip->i_lfs_osize = ip->i_ffs1_size;
    403 #if 0
    404 	if (fs->lfs_version > 1) {
    405 		ip->i_ffs1_atime = ts.tv_sec;
    406 		ip->i_ffs1_atimensec = ts.tv_nsec;
    407 	}
    408 #endif
    409 
    410 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    411 	for (i = 0; i < NDADDR; i++)
    412 		if (ip->i_ffs1_db[i] != 0)
    413 			ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
    414 
    415 	++nvnodes;
    416 	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
    417 	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
    418 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
    419 
    420 	return vp;
    421 }
    422 
    423 static struct uvnode *
    424 lfs_vget(void *vfs, ino_t ino)
    425 {
    426 	struct lfs *fs = (struct lfs *)vfs;
    427 	ufs_daddr_t daddr;
    428 	struct ubuf *bp;
    429 	IFILE *ifp;
    430 
    431 	LFS_IENTRY(ifp, fs, ino, bp);
    432 	daddr = ifp->if_daddr;
    433 	brelse(bp);
    434 	if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg)
    435 		return NULL;
    436 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
    437 }
    438 
    439 /* Check superblock magic number and checksum */
    440 static int
    441 check_sb(struct lfs *fs)
    442 {
    443 	u_int32_t checksum;
    444 
    445 	if (fs->lfs_magic != LFS_MAGIC) {
    446 		printf("Superblock magic number (0x%lx) does not match "
    447 		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
    448 		       (unsigned long) LFS_MAGIC);
    449 		return 1;
    450 	}
    451 	/* checksum */
    452 	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
    453 	if (fs->lfs_cksum != checksum) {
    454 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
    455 		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
    456 		return 1;
    457 	}
    458 	return 0;
    459 }
    460 
    461 /* Initialize LFS library; load superblocks and choose which to use. */
    462 struct lfs *
    463 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
    464 {
    465 	struct uvnode *devvp;
    466 	struct ubuf *bp;
    467 	int tryalt;
    468 	struct lfs *fs, *altfs;
    469 	int error;
    470 
    471 	vfs_init();
    472 
    473 	devvp = (struct uvnode *) malloc(sizeof(*devvp));
    474 	if (devvp == NULL)
    475 		err(1, NULL);
    476 	memset(devvp, 0, sizeof(*devvp));
    477 	devvp->v_fs = NULL;
    478 	devvp->v_fd = devfd;
    479 	devvp->v_strategy_op = raw_vop_strategy;
    480 	devvp->v_bwrite_op = raw_vop_bwrite;
    481 	devvp->v_bmap_op = raw_vop_bmap;
    482 	LIST_INIT(&devvp->v_cleanblkhd);
    483 	LIST_INIT(&devvp->v_dirtyblkhd);
    484 
    485 	tryalt = 0;
    486 	if (dummy_read) {
    487 		if (sblkno == 0)
    488 			sblkno = btodb(LFS_LABELPAD);
    489 		fs = (struct lfs *) malloc(sizeof(*fs));
    490 		if (fs == NULL)
    491 			err(1, NULL);
    492 		memset(fs, 0, sizeof(*fs));
    493 		fs->lfs_devvp = devvp;
    494 	} else {
    495 		if (sblkno == 0) {
    496 			sblkno = btodb(LFS_LABELPAD);
    497 			tryalt = 1;
    498 		} else if (debug) {
    499 			printf("No -b flag given, not attempting to verify checkpoint\n");
    500 		}
    501 		error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
    502 		fs = (struct lfs *) malloc(sizeof(*fs));
    503 		if (fs == NULL)
    504 			err(1, NULL);
    505 		memset(fs, 0, sizeof(*fs));
    506 		fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    507 		fs->lfs_devvp = devvp;
    508 		bp->b_flags |= B_INVAL;
    509 		brelse(bp);
    510 
    511 		if (tryalt) {
    512 			error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
    513 		    	LFS_SBPAD, NOCRED, &bp);
    514 			altfs = (struct lfs *) malloc(sizeof(*altfs));
    515 			if (altfs == NULL)
    516 				err(1, NULL);
    517 			memset(altfs, 0, sizeof(*altfs));
    518 			altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    519 			altfs->lfs_devvp = devvp;
    520 			bp->b_flags |= B_INVAL;
    521 			brelse(bp);
    522 
    523 			if (check_sb(fs) || fs->lfs_idaddr <= 0) {
    524 				if (debug)
    525 					printf("Primary superblock is no good, using first alternate\n");
    526 				free(fs);
    527 				fs = altfs;
    528 			} else {
    529 				/* If both superblocks check out, try verification */
    530 				if (check_sb(altfs)) {
    531 					if (debug)
    532 						printf("First alternate superblock is no good, using primary\n");
    533 					free(altfs);
    534 				} else {
    535 					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    536 						free(altfs);
    537 					} else {
    538 						free(fs);
    539 						fs = altfs;
    540 					}
    541 				}
    542 			}
    543 		}
    544 		if (check_sb(fs)) {
    545 			free(fs);
    546 			return NULL;
    547 		}
    548 	}
    549 
    550 	/* Compatibility */
    551 	if (fs->lfs_version < 2) {
    552 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    553 		fs->lfs_ibsize = fs->lfs_bsize;
    554 		fs->lfs_start = fs->lfs_sboffs[0];
    555 		fs->lfs_tstamp = fs->lfs_otstamp;
    556 		fs->lfs_fsbtodb = 0;
    557 	}
    558 
    559 	if (!dummy_read) {
    560 		fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
    561 		if (fs->lfs_suflags == NULL)
    562 			err(1, NULL);
    563 		fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    564 		if (fs->lfs_suflags[0] == NULL)
    565 			err(1, NULL);
    566 		fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
    567 		if (fs->lfs_suflags[1] == NULL)
    568 			err(1, NULL);
    569 	}
    570 
    571 	if (idaddr == 0)
    572 		idaddr = fs->lfs_idaddr;
    573 	else
    574 		fs->lfs_idaddr = idaddr;
    575 	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
    576 	fs->lfs_ivnode = lfs_raw_vget(fs,
    577 		(dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
    578 		idaddr);
    579 
    580 	register_vget((void *)fs, lfs_vget);
    581 
    582 	return fs;
    583 }
    584 
    585 /*
    586  * Check partial segment validity between fs->lfs_offset and the given goal.
    587  *
    588  * If goal == 0, just keep on going until the segments stop making sense,
    589  * and return the address of the last valid partial segment.
    590  *
    591  * If goal != 0, return the address of the first partial segment that failed,
    592  * or "goal" if we reached it without failure (the partial segment *at* goal
    593  * need not be valid).
    594  */
    595 ufs_daddr_t
    596 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
    597 {
    598 	ufs_daddr_t daddr, odaddr;
    599 	SEGSUM *sp;
    600 	int bc, flag;
    601 	struct ubuf *bp;
    602 	ufs_daddr_t nodirop_daddr;
    603 	u_int64_t serial;
    604 
    605 	odaddr = -1;
    606 	daddr = osb->lfs_offset;
    607 	nodirop_daddr = daddr;
    608 	serial = osb->lfs_serial;
    609 	while (daddr != goal) {
    610 		flag = 0;
    611 oncemore:
    612 		/* Read in summary block */
    613 		bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
    614 		sp = (SEGSUM *)bp->b_data;
    615 
    616 		/*
    617 		 * Could be a superblock instead of a segment summary.
    618 		 * XXX should use gseguse, but right now we need to do more
    619 		 * setup before we can...fix this
    620 		 */
    621 		if (sp->ss_magic != SS_MAGIC ||
    622 		    sp->ss_ident != osb->lfs_ident ||
    623 		    sp->ss_serial < serial ||
    624 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
    625 			sizeof(sp->ss_sumsum))) {
    626 			brelse(bp);
    627 			if (flag == 0) {
    628 				flag = 1;
    629 				daddr += btofsb(osb, LFS_SBPAD);
    630 				goto oncemore;
    631 			}
    632 			break;
    633 		}
    634 		++serial;
    635 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    636 		if (bc == 0) {
    637 			brelse(bp);
    638 			break;
    639 		}
    640 		assert (bc > 0);
    641 		odaddr = daddr;
    642 		daddr += btofsb(osb, osb->lfs_sumsize + bc);
    643 		if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
    644 		    dtosn(osb, daddr) != dtosn(osb, daddr +
    645 			btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
    646 			daddr = sp->ss_next;
    647 		}
    648 		if (!(sp->ss_flags & SS_CONT))
    649 			nodirop_daddr = daddr;
    650 		brelse(bp);
    651 	}
    652 
    653 	if (goal == 0)
    654 		return nodirop_daddr;
    655 	else
    656 		return daddr;
    657 }
    658 
    659 /* Use try_verify to check whether the newer superblock is valid. */
    660 struct lfs *
    661 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    662 {
    663 	ufs_daddr_t daddr;
    664 	struct lfs *osb, *nsb;
    665 
    666 	/*
    667 	 * Verify the checkpoint of the newer superblock,
    668 	 * if the timestamp/serial number of the two superblocks is
    669 	 * different.
    670 	 */
    671 
    672 	osb = NULL;
    673 	if (debug)
    674 		printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
    675 		    (long long) sb1->lfs_serial);
    676 
    677 	if ((sb0->lfs_version == 1 &&
    678 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
    679 	    (sb0->lfs_version > 1 &&
    680 		sb0->lfs_serial != sb1->lfs_serial)) {
    681 		if (sb0->lfs_version == 1) {
    682 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
    683 				osb = sb1;
    684 				nsb = sb0;
    685 			} else {
    686 				osb = sb0;
    687 				nsb = sb1;
    688 			}
    689 		} else {
    690 			if (sb0->lfs_serial > sb1->lfs_serial) {
    691 				osb = sb1;
    692 				nsb = sb0;
    693 			} else {
    694 				osb = sb0;
    695 				nsb = sb1;
    696 			}
    697 		}
    698 		if (debug) {
    699 			printf("Attempting to verify newer checkpoint...");
    700 			fflush(stdout);
    701 		}
    702 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
    703 
    704 		if (debug)
    705 			printf("done.\n");
    706 		if (daddr == nsb->lfs_offset) {
    707 			pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
    708 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    709 			sbdirty();
    710 		} else {
    711 			pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    712 		}
    713 		return (daddr == nsb->lfs_offset ? nsb : osb);
    714 	}
    715 	/* Nothing to check */
    716 	return osb;
    717 }
    718 
    719 /* Verify a partial-segment summary; return the number of bytes on disk. */
    720 int
    721 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
    722 	      struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
    723 {
    724 	FINFO *fp;
    725 	int bc;			/* Bytes in partial segment */
    726 	int nblocks;
    727 	ufs_daddr_t seg_addr, daddr;
    728 	ufs_daddr_t *dp, *idp;
    729 	struct ubuf *bp;
    730 	int i, j, k, datac, len;
    731 	long sn;
    732 	u_int32_t *datap;
    733 	u_int32_t ccksum;
    734 
    735 	sn = dtosn(fs, pseg_addr);
    736 	seg_addr = sntod(fs, sn);
    737 
    738 	/* We've already checked the sumsum, just do the data bounds and sum */
    739 
    740 	/* Count the blocks. */
    741 	nblocks = howmany(sp->ss_ninos, INOPB(fs));
    742 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
    743 	assert(bc >= 0);
    744 
    745 	fp = (FINFO *) (sp + 1);
    746 	for (i = 0; i < sp->ss_nfinfo; i++) {
    747 		nblocks += fp->fi_nblocks;
    748 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    749 					   << fs->lfs_bshift);
    750 		assert(bc >= 0);
    751 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    752 	}
    753 	datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
    754 	if (datap == NULL)
    755 		err(1, NULL);
    756 	datac = 0;
    757 
    758 	dp = (ufs_daddr_t *) sp;
    759 	dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
    760 	dp--;
    761 
    762 	idp = dp;
    763 	daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
    764 	fp = (FINFO *) (sp + 1);
    765 	for (i = 0, j = 0;
    766 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
    767 		if (i >= sp->ss_nfinfo && *idp != daddr) {
    768 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
    769 			      ": found %d, wanted %d\n",
    770 			      pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
    771 			if (debug)
    772 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    773 				      daddr);
    774 			break;
    775 		}
    776 		while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
    777 			bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
    778 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    779 			brelse(bp);
    780 
    781 			++j;
    782 			daddr += btofsb(fs, fs->lfs_ibsize);
    783 			--idp;
    784 		}
    785 		if (i < sp->ss_nfinfo) {
    786 			if (func)
    787 				func(daddr, fp);
    788 			for (k = 0; k < fp->fi_nblocks; k++) {
    789 				len = (k == fp->fi_nblocks - 1 ?
    790 				       fp->fi_lastlength
    791 				       : fs->lfs_bsize);
    792 				bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
    793 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    794 				brelse(bp);
    795 				daddr += btofsb(fs, len);
    796 			}
    797 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    798 		}
    799 	}
    800 
    801 	if (datac != nblocks) {
    802 		pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
    803 		    (long long) pseg_addr, nblocks, datac);
    804 	}
    805 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    806 	/* Check the data checksum */
    807 	if (ccksum != sp->ss_datasum) {
    808 		pwarn("Partial segment at 0x%" PRIx32 " data checksum"
    809 		      " mismatch: given 0x%x, computed 0x%x\n",
    810 		      pseg_addr, sp->ss_datasum, ccksum);
    811 		free(datap);
    812 		return 0;
    813 	}
    814 	free(datap);
    815 	assert(bc >= 0);
    816 	return bc;
    817 }
    818 
    819 /* print message and exit */
    820 void
    821 my_vpanic(int fatal, const char *fmt, va_list ap)
    822 {
    823         (void) vprintf(fmt, ap);
    824 	exit(8);
    825 }
    826 
    827 void
    828 call_panic(const char *fmt, ...)
    829 {
    830 	va_list ap;
    831 
    832 	va_start(ap, fmt);
    833         panic_func(1, fmt, ap);
    834 	va_end(ap);
    835 }
    836 
    837 /* Allocate a new inode. */
    838 struct uvnode *
    839 lfs_valloc(struct lfs *fs, ino_t ino)
    840 {
    841 	struct ubuf *bp, *cbp;
    842 	struct ifile *ifp;
    843 	ino_t new_ino;
    844 	int error;
    845 	int new_gen;
    846 	CLEANERINFO *cip;
    847 
    848 	/* Get the head of the freelist. */
    849 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    850 
    851 	/*
    852 	 * Remove the inode from the free list and write the new start
    853 	 * of the free list into the superblock.
    854 	 */
    855 	LFS_IENTRY(ifp, fs, new_ino, bp);
    856 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    857 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    858 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    859 
    860 	new_gen = ifp->if_version; /* version was updated by vfree */
    861 	brelse(bp);
    862 
    863 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    864 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    865 		if ((error = extend_ifile(fs)) != 0) {
    866 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    867 			return NULL;
    868 		}
    869 	}
    870 
    871 	/* Set superblock modified bit and increment file count. */
    872         sbdirty();
    873 	++fs->lfs_nfiles;
    874 
    875         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
    876 }
    877 
    878 /*
    879  * Add a new block to the Ifile, to accommodate future file creations.
    880  */
    881 int
    882 extend_ifile(struct lfs *fs)
    883 {
    884 	struct uvnode *vp;
    885 	struct inode *ip;
    886 	IFILE *ifp;
    887 	IFILE_V1 *ifp_v1;
    888 	struct ubuf *bp, *cbp;
    889 	daddr_t i, blkno, max;
    890 	ino_t oldlast;
    891 	CLEANERINFO *cip;
    892 
    893 	vp = fs->lfs_ivnode;
    894 	ip = VTOI(vp);
    895 	blkno = lblkno(fs, ip->i_ffs1_size);
    896 
    897 	bp = getblk(vp, blkno, fs->lfs_bsize);	/* XXX VOP_BALLOC() */
    898 	ip->i_ffs1_size += fs->lfs_bsize;
    899 
    900 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    901 		fs->lfs_ifpb;
    902 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    903 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    904 	max = i + fs->lfs_ifpb;
    905 	fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize);
    906 
    907 	if (fs->lfs_version == 1) {
    908 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    909 			ifp_v1->if_version = 1;
    910 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    911 			ifp_v1->if_nextfree = ++i;
    912 		}
    913 		ifp_v1--;
    914 		ifp_v1->if_nextfree = oldlast;
    915 	} else {
    916 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    917 			ifp->if_version = 1;
    918 			ifp->if_daddr = LFS_UNUSED_DADDR;
    919 			ifp->if_nextfree = ++i;
    920 		}
    921 		ifp--;
    922 		ifp->if_nextfree = oldlast;
    923 	}
    924 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    925 
    926 	LFS_BWRITE_LOG(bp);
    927 
    928 	return 0;
    929 }
    930 
    931