Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.56
      1 /* $NetBSD: lfs.c,v 1.56 2015/08/12 18:28:00 dholland 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  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 /*
     31  * Copyright (c) 1989, 1991, 1993
     32  *	The Regents of the University of California.  All rights reserved.
     33  * (c) UNIX System Laboratories, Inc.
     34  * All or some portions of this file are derived from material licensed
     35  * to the University of California by American Telephone and Telegraph
     36  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     37  * the permission of UNIX System Laboratories, Inc.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)ufs_bmap.c	8.8 (Berkeley) 8/11/95
     64  */
     65 
     66 
     67 #include <sys/types.h>
     68 #include <sys/param.h>
     69 #include <sys/time.h>
     70 #include <sys/buf.h>
     71 #include <sys/mount.h>
     72 
     73 #define vnode uvnode
     74 #include <ufs/lfs/lfs.h>
     75 #include <ufs/lfs/lfs_inode.h>
     76 #include <ufs/lfs/lfs_accessors.h>
     77 #undef vnode
     78 
     79 #include <assert.h>
     80 #include <err.h>
     81 #include <errno.h>
     82 #include <stdarg.h>
     83 #include <stdio.h>
     84 #include <stdlib.h>
     85 #include <string.h>
     86 #include <unistd.h>
     87 #include <util.h>
     88 
     89 #include "bufcache.h"
     90 #include "vnode.h"
     91 #include "lfs_user.h"
     92 #include "segwrite.h"
     93 #include "kernelops.h"
     94 
     95 #define panic call_panic
     96 
     97 extern u_int32_t cksum(void *, size_t);
     98 extern u_int32_t lfs_sb_cksum(struct lfs *);
     99 extern void pwarn(const char *, ...);
    100 
    101 extern struct uvnodelst vnodelist;
    102 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
    103 extern int nvnodes;
    104 
    105 long dev_bsize = DEV_BSIZE;
    106 
    107 static int
    108 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
    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 = kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    124 		    bp->b_blkno * dev_bsize);
    125 		if (count == bp->b_bcount)
    126 			bp->b_flags |= B_DONE;
    127 	} else {
    128 		count = kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    129 		    bp->b_blkno * dev_bsize);
    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 		lfs_sb_subavail(fs, lfs_btofsb(fs, bp->b_bcount));
    148 	}
    149 	bp->b_flags |= B_DELWRI | B_LOCKED;
    150 	reassignbuf(bp, bp->b_vp);
    151 	brelse(bp, 0);
    152 	return 0;
    153 }
    154 
    155 /*
    156  * ulfs_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 ulfs_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[ULFS_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 < ULFS_NDADDR) {
    174 		if (nump != NULL)
    175 			*nump = 0;
    176 		*bnp = LFS_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 = ulfs_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, 0);
    206 
    207 		xap->in_exists = 1;
    208 		bp = getblk(vp, metalbn, lfs_sb_getbsize(fs));
    209 
    210 		if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
    211 			bp->b_blkno = LFS_FSBTODB(fs, daddr);
    212 			bp->b_flags |= B_READ;
    213 			VOP_STRATEGY(bp);
    214 		}
    215 		daddr = ((ulfs_daddr_t *) bp->b_data)[xap->in_off];
    216 	}
    217 	if (bp)
    218 		brelse(bp, 0);
    219 
    220 	daddr = LFS_FSBTODB(fs, (ulfs_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 ulfs_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 = lfs_sb_getnindir(fs); 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 ULFS_NIADDR - i is the number of levels
    259 	 * of indirection needed to locate the requested block. */
    260 
    261 	bn -= ULFS_NDADDR;
    262 	for (lbc = 0, i = ULFS_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 + ULFS_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 = ULFS_NIADDR - i;
    282 	ap->in_exists = 0;
    283 	ap++;
    284 	for (++numlevels; i <= ULFS_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) & (lfs_sb_getnindir(fs) - 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 ulfs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
    310 }
    311 
    312 /* Search a block for a specific dinode. */
    313 union lfs_dinode *
    314 lfs_ifind(struct lfs *fs, ino_t ino, struct ubuf *bp)
    315 {
    316 	union lfs_dinode *ldip;
    317 	unsigned i, num;
    318 
    319 	num = LFS_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 (i = num; i-- > 0; ) {
    327 		ldip = DINO_IN_BLOCK(fs, bp->b_data, i);
    328 		if (lfs_dino_getinumber(fs, ldip) == ino)
    329 			return (ldip);
    330 	}
    331 	return NULL;
    332 }
    333 
    334 /*
    335  * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
    336  * XXX it currently loses atime information.
    337  */
    338 struct uvnode *
    339 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ulfs_daddr_t daddr)
    340 {
    341 	struct uvnode *vp;
    342 	struct inode *ip;
    343 	union lfs_dinode *dip;
    344 	struct ubuf *bp;
    345 	int i, hash;
    346 
    347 	vp = ecalloc(1, sizeof(*vp));
    348 	vp->v_fd = fd;
    349 	vp->v_fs = fs;
    350 	vp->v_usecount = 0;
    351 	vp->v_strategy_op = lfs_vop_strategy;
    352 	vp->v_bwrite_op = lfs_vop_bwrite;
    353 	vp->v_bmap_op = lfs_vop_bmap;
    354 	LIST_INIT(&vp->v_cleanblkhd);
    355 	LIST_INIT(&vp->v_dirtyblkhd);
    356 
    357 	ip = ecalloc(1, sizeof(*ip));
    358 
    359 	dip = ecalloc(1, sizeof(*dip));
    360 	// XXX bogus cast
    361 	ip->i_din.ffs1_din = (struct lfs32_dinode *)dip;
    362 
    363 	/* Initialize the inode -- from lfs_vcreate. */
    364 	ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs));
    365 	vp->v_data = ip;
    366 	/* ip->i_vnode = vp; */
    367 	ip->i_number = ino;
    368 	ip->i_lockf = 0;
    369 	ip->i_lfs_effnblks = 0;
    370 	ip->i_flag = 0;
    371 
    372 	/* Load inode block and find inode */
    373 	if (daddr > 0) {
    374 		bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
    375 		    0, &bp);
    376 		bp->b_flags |= B_AGE;
    377 		dip = lfs_ifind(fs, ino, bp);
    378 		if (dip == NULL) {
    379 			brelse(bp, 0);
    380 			free(ip);
    381 			free(vp);
    382 			return NULL;
    383 		}
    384 		// XXX this should go away
    385 		if (fs->lfs_is64) {
    386 			memcpy(ip->i_din.ffs2_din, &dip->u_64, sizeof(dip->u_64));
    387 		} else {
    388 			memcpy(ip->i_din.ffs1_din, &dip->u_32, sizeof(dip->u_32));
    389 		}
    390 		brelse(bp, 0);
    391 	}
    392 	ip->i_number = ino;
    393 	/* ip->i_devvp = fs->lfs_devvp; */
    394 	ip->i_lfs = fs;
    395 
    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, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
    406 	for (i = 0; i < ULFS_NDADDR; i++)
    407 		if (ip->i_ffs1_db[i] != 0)
    408 			ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
    409 
    410 	++nvnodes;
    411 	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
    412 	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
    413 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
    414 
    415 	return vp;
    416 }
    417 
    418 static struct uvnode *
    419 lfs_vget(void *vfs, ino_t ino)
    420 {
    421 	struct lfs *fs = (struct lfs *)vfs;
    422 	ulfs_daddr_t daddr;
    423 	struct ubuf *bp;
    424 	IFILE *ifp;
    425 
    426 	LFS_IENTRY(ifp, fs, ino, bp);
    427 	daddr = lfs_if_getdaddr(fs, ifp);
    428 	brelse(bp, 0);
    429 	if (daddr <= 0 || lfs_dtosn(fs, daddr) >= lfs_sb_getnseg(fs))
    430 		return NULL;
    431 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
    432 }
    433 
    434 /* Check superblock magic number and checksum */
    435 static int
    436 check_sb(struct lfs *fs)
    437 {
    438 	u_int32_t checksum;
    439 	u_int32_t magic;
    440 
    441 	/* we can read the magic out of either the 32-bit or 64-bit dlfs */
    442 	magic = fs->lfs_dlfs_u.u_32.dlfs_magic;
    443 
    444 	if (magic != LFS_MAGIC) {
    445 		printf("Superblock magic number (0x%lx) does not match "
    446 		       "expected 0x%lx\n", (unsigned long) magic,
    447 		       (unsigned long) LFS_MAGIC);
    448 		return 1;
    449 	}
    450 	/* checksum */
    451 	checksum = lfs_sb_cksum(fs);
    452 	if (lfs_sb_getcksum(fs) != checksum) {
    453 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
    454 		    (unsigned long) lfs_sb_getcksum(fs), (unsigned long) checksum);
    455 		return 1;
    456 	}
    457 	return 0;
    458 }
    459 
    460 /* Initialize LFS library; load superblocks and choose which to use. */
    461 struct lfs *
    462 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
    463 {
    464 	struct uvnode *devvp;
    465 	struct ubuf *bp;
    466 	int tryalt;
    467 	struct lfs *fs, *altfs;
    468 
    469 	vfs_init();
    470 
    471 	devvp = ecalloc(1, sizeof(*devvp));
    472 	devvp->v_fs = NULL;
    473 	devvp->v_fd = devfd;
    474 	devvp->v_strategy_op = raw_vop_strategy;
    475 	devvp->v_bwrite_op = raw_vop_bwrite;
    476 	devvp->v_bmap_op = raw_vop_bmap;
    477 	LIST_INIT(&devvp->v_cleanblkhd);
    478 	LIST_INIT(&devvp->v_dirtyblkhd);
    479 
    480 	tryalt = 0;
    481 	if (dummy_read) {
    482 		if (sblkno == 0)
    483 			sblkno = LFS_LABELPAD / dev_bsize;
    484 		fs = ecalloc(1, sizeof(*fs));
    485 		fs->lfs_devvp = devvp;
    486 	} else {
    487 		if (sblkno == 0) {
    488 			sblkno = LFS_LABELPAD / dev_bsize;
    489 			tryalt = 1;
    490 		} else if (debug) {
    491 			printf("No -b flag given, not attempting to verify checkpoint\n");
    492 		}
    493 
    494 		dev_bsize = DEV_BSIZE;
    495 
    496 		(void)bread(devvp, sblkno, LFS_SBPAD, 0, &bp);
    497 		fs = ecalloc(1, sizeof(*fs));
    498 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
    499 		memcpy(&fs->lfs_dlfs_u, bp->b_data, sizeof(struct dlfs));
    500 		fs->lfs_devvp = devvp;
    501 		bp->b_flags |= B_INVAL;
    502 		brelse(bp, 0);
    503 
    504 		dev_bsize = lfs_sb_getfsize(fs) >> lfs_sb_getfsbtodb(fs);
    505 
    506 		if (tryalt) {
    507 			(void)bread(devvp, LFS_FSBTODB(fs, lfs_sb_getsboff(fs, 1)),
    508 		    	LFS_SBPAD, 0, &bp);
    509 			altfs = ecalloc(1, sizeof(*altfs));
    510 			memcpy(&altfs->lfs_dlfs_u, bp->b_data,
    511 			       sizeof(struct dlfs));
    512 			altfs->lfs_devvp = devvp;
    513 			bp->b_flags |= B_INVAL;
    514 			brelse(bp, 0);
    515 
    516 			if (check_sb(fs) || lfs_sb_getidaddr(fs) <= 0) {
    517 				if (debug)
    518 					printf("Primary superblock is no good, using first alternate\n");
    519 				free(fs);
    520 				fs = altfs;
    521 			} else {
    522 				/* If both superblocks check out, try verification */
    523 				if (check_sb(altfs)) {
    524 					if (debug)
    525 						printf("First alternate superblock is no good, using primary\n");
    526 					free(altfs);
    527 				} else {
    528 					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    529 						free(altfs);
    530 					} else {
    531 						free(fs);
    532 						fs = altfs;
    533 					}
    534 				}
    535 			}
    536 		}
    537 		if (check_sb(fs)) {
    538 			free(fs);
    539 			return NULL;
    540 		}
    541 	}
    542 
    543 	/* Compatibility */
    544 	if (lfs_sb_getversion(fs) < 2) {
    545 		lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
    546 		lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
    547 		lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
    548 		lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
    549 		lfs_sb_setfsbtodb(fs, 0);
    550 	}
    551 
    552 	if (!dummy_read) {
    553 		fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *));
    554 		fs->lfs_suflags[0] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    555 		fs->lfs_suflags[1] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    556 	}
    557 
    558 	if (idaddr == 0)
    559 		idaddr = lfs_sb_getidaddr(fs);
    560 	else
    561 		lfs_sb_setidaddr(fs, idaddr);
    562 	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
    563 	fs->lfs_ivnode = lfs_raw_vget(fs,
    564 		(dummy_read ? LFS_IFILE_INUM : lfs_sb_getifile(fs)),
    565 		devvp->v_fd, idaddr);
    566 	if (fs->lfs_ivnode == NULL)
    567 		return NULL;
    568 
    569 	register_vget((void *)fs, lfs_vget);
    570 
    571 	return fs;
    572 }
    573 
    574 /*
    575  * Check partial segment validity between fs->lfs_offset and the given goal.
    576  *
    577  * If goal == 0, just keep on going until the segments stop making sense,
    578  * and return the address of the last valid partial segment.
    579  *
    580  * If goal != 0, return the address of the first partial segment that failed,
    581  * or "goal" if we reached it without failure (the partial segment *at* goal
    582  * need not be valid).
    583  */
    584 ulfs_daddr_t
    585 try_verify(struct lfs *osb, struct uvnode *devvp, ulfs_daddr_t goal, int debug)
    586 {
    587 	ulfs_daddr_t daddr, odaddr;
    588 	SEGSUM *sp;
    589 	int i, bc, hitclean;
    590 	struct ubuf *bp;
    591 	ulfs_daddr_t nodirop_daddr;
    592 	u_int64_t serial;
    593 
    594 	bc = 0;
    595 	hitclean = 0;
    596 	odaddr = -1;
    597 	daddr = lfs_sb_getoffset(osb);
    598 	nodirop_daddr = daddr;
    599 	serial = lfs_sb_getserial(osb);
    600 	while (daddr != goal) {
    601 		/*
    602 		 * Don't mistakenly read a superblock, if there is one here.
    603 		 */
    604 		if (lfs_sntod(osb, lfs_dtosn(osb, daddr)) == daddr) {
    605 			if (daddr == lfs_sb_gets0addr(osb))
    606 				daddr += lfs_btofsb(osb, LFS_LABELPAD);
    607 			for (i = 0; i < LFS_MAXNUMSB; i++) {
    608 				if (lfs_sb_getsboff(osb, i) < daddr)
    609 					break;
    610 				if (lfs_sb_getsboff(osb, i) == daddr)
    611 					daddr += lfs_btofsb(osb, LFS_SBPAD);
    612 			}
    613 		}
    614 
    615 		/* Read in summary block */
    616 		bread(devvp, LFS_FSBTODB(osb, daddr), lfs_sb_getsumsize(osb),
    617 		    0, &bp);
    618 		sp = (SEGSUM *)bp->b_data;
    619 
    620 		/*
    621 		 * Check for a valid segment summary belonging to our fs.
    622 		 */
    623 		if (lfs_ss_getmagic(osb, sp) != SS_MAGIC ||
    624 		    lfs_ss_getident(osb, sp) != lfs_sb_getident(osb) ||
    625 		    lfs_ss_getserial(osb, sp) < serial ||	/* XXX strengthen this */
    626 		    lfs_ss_getsumsum(osb, sp) !=
    627 		            cksum((char *)sp + lfs_ss_getsumstart(osb),
    628 				  lfs_sb_getsumsize(osb) - lfs_ss_getsumstart(osb))) {
    629 			brelse(bp, 0);
    630 			if (debug) {
    631 				if (lfs_ss_getmagic(osb, sp) != SS_MAGIC)
    632 					pwarn("pseg at 0x%jx: "
    633 					      "wrong magic number\n",
    634 					      (uintmax_t)daddr);
    635 				else if (lfs_ss_getident(osb, sp) != lfs_sb_getident(osb))
    636 					pwarn("pseg at 0x%jx: "
    637 					      "expected ident %jx, got %jx\n",
    638 					      (uintmax_t)daddr,
    639 					      (uintmax_t)lfs_ss_getident(osb, sp),
    640 					      (uintmax_t)lfs_sb_getident(osb));
    641 				else if (lfs_ss_getserial(osb, sp) >= serial)
    642 					pwarn("pseg at 0x%jx: "
    643 					      "serial %d < %d\n",
    644 					      (uintmax_t)daddr,
    645 					      (int)lfs_ss_getserial(osb, sp), (int)serial);
    646 				else
    647 					pwarn("pseg at 0x%jx: "
    648 					      "summary checksum wrong\n",
    649 					      (uintmax_t)daddr);
    650 			}
    651 			break;
    652 		}
    653 		if (debug && lfs_ss_getserial(osb, sp) != serial)
    654 			pwarn("warning, serial=%d ss_serial=%d\n",
    655 				(int)serial, (int)lfs_ss_getserial(osb, sp));
    656 		++serial;
    657 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    658 		if (bc == 0) {
    659 			brelse(bp, 0);
    660 			break;
    661 		}
    662 		if (debug)
    663 			pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr,
    664 			      (int)lfs_ss_getserial(osb, sp));
    665 		assert (bc > 0);
    666 		odaddr = daddr;
    667 		daddr += lfs_btofsb(osb, lfs_sb_getsumsize(osb) + bc);
    668 		if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) ||
    669 		    lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr +
    670 			lfs_btofsb(osb, lfs_sb_getsumsize(osb) + lfs_sb_getbsize(osb)) - 1)) {
    671 			daddr = lfs_ss_getnext(osb, sp);
    672 		}
    673 
    674 		/*
    675 		 * Check for the beginning and ending of a sequence of
    676 		 * dirops.  Writes from the cleaner never involve new
    677 		 * information, and are always checkpoints; so don't try
    678 		 * to roll forward through them.  Likewise, psegs written
    679 		 * by a previous roll-forward attempt are not interesting.
    680 		 */
    681 		if (lfs_ss_getflags(osb, sp) & (SS_CLEAN | SS_RFW))
    682 			hitclean = 1;
    683 		if (hitclean == 0 && (lfs_ss_getflags(osb, sp) & SS_CONT) == 0)
    684 			nodirop_daddr = daddr;
    685 
    686 		brelse(bp, 0);
    687 	}
    688 
    689 	if (goal == 0)
    690 		return nodirop_daddr;
    691 	else
    692 		return daddr;
    693 }
    694 
    695 /* Use try_verify to check whether the newer superblock is valid. */
    696 struct lfs *
    697 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    698 {
    699 	ulfs_daddr_t daddr;
    700 	struct lfs *osb, *nsb;
    701 
    702 	/*
    703 	 * Verify the checkpoint of the newer superblock,
    704 	 * if the timestamp/serial number of the two superblocks is
    705 	 * different.
    706 	 */
    707 
    708 	osb = NULL;
    709 	if (debug)
    710 		pwarn("sb0 %ju, sb1 %ju",
    711 		      (uintmax_t) lfs_sb_getserial(sb0),
    712 		      (uintmax_t) lfs_sb_getserial(sb1));
    713 
    714 	if ((lfs_sb_getversion(sb0) == 1 &&
    715 		lfs_sb_getotstamp(sb0) != lfs_sb_getotstamp(sb1)) ||
    716 	    (lfs_sb_getversion(sb0) > 1 &&
    717 		lfs_sb_getserial(sb0) != lfs_sb_getserial(sb1))) {
    718 		if (lfs_sb_getversion(sb0) == 1) {
    719 			if (lfs_sb_getotstamp(sb0) > lfs_sb_getotstamp(sb1)) {
    720 				osb = sb1;
    721 				nsb = sb0;
    722 			} else {
    723 				osb = sb0;
    724 				nsb = sb1;
    725 			}
    726 		} else {
    727 			if (lfs_sb_getserial(sb0) > lfs_sb_getserial(sb1)) {
    728 				osb = sb1;
    729 				nsb = sb0;
    730 			} else {
    731 				osb = sb0;
    732 				nsb = sb1;
    733 			}
    734 		}
    735 		if (debug) {
    736 			printf("Attempting to verify newer checkpoint...");
    737 			fflush(stdout);
    738 		}
    739 		daddr = try_verify(osb, devvp, lfs_sb_getoffset(nsb), debug);
    740 
    741 		if (debug)
    742 			printf("done.\n");
    743 		if (daddr == lfs_sb_getoffset(nsb)) {
    744 			pwarn("** Newer checkpoint verified; recovered %jd seconds of data\n",
    745 			    (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    746 			sbdirty();
    747 		} else {
    748 			pwarn("** Newer checkpoint invalid; lost %jd seconds of data\n", (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    749 		}
    750 		return (daddr == lfs_sb_getoffset(nsb) ? nsb : osb);
    751 	}
    752 	/* Nothing to check */
    753 	return osb;
    754 }
    755 
    756 /* Verify a partial-segment summary; return the number of bytes on disk. */
    757 int
    758 check_summary(struct lfs *fs, SEGSUM *sp, ulfs_daddr_t pseg_addr, int debug,
    759 	      struct uvnode *devvp, void (func(ulfs_daddr_t, FINFO *)))
    760 {
    761 	FINFO *fp;
    762 	int bc;			/* Bytes in partial segment */
    763 	int nblocks;
    764 	ulfs_daddr_t daddr;
    765 	ulfs_daddr_t *dp, *idp;
    766 	struct ubuf *bp;
    767 	int i, j, k, datac, len;
    768 	u_int32_t *datap;
    769 	u_int32_t ccksum;
    770 
    771 	/* We've already checked the sumsum, just do the data bounds and sum */
    772 
    773 	/* Count the blocks. */
    774 	nblocks = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs));
    775 	bc = nblocks << (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) : lfs_sb_getbshift(fs));
    776 	assert(bc >= 0);
    777 
    778 	fp = SEGSUM_FINFOBASE(fs, sp);
    779 	for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
    780 		nblocks += lfs_fi_getnblocks(fs, fp);
    781 		bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
    782 					   << lfs_sb_getbshift(fs));
    783 		assert(bc >= 0);
    784 		fp = NEXT_FINFO(fs, fp);
    785 		if (((char *)fp) - (char *)sp > lfs_sb_getsumsize(fs))
    786 			return 0;
    787 	}
    788 	datap = emalloc(nblocks * sizeof(*datap));
    789 	datac = 0;
    790 
    791 	dp = (ulfs_daddr_t *) sp;
    792 	dp += lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t);
    793 	dp--;
    794 
    795 	idp = dp;
    796 	daddr = pseg_addr + lfs_btofsb(fs, lfs_sb_getsumsize(fs));
    797 	fp = (FINFO *) (sp + 1);
    798 	for (i = 0, j = 0;
    799 	     i < lfs_ss_getnfinfo(fs, sp) || j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)); i++) {
    800 		if (i >= lfs_ss_getnfinfo(fs, sp) && *idp != daddr) {
    801 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
    802 			      ": found %d, wanted %d\n",
    803 			      pseg_addr, j, howmany(lfs_ss_getninos(fs, sp),
    804 						    LFS_INOPB(fs)));
    805 			if (debug)
    806 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    807 				      daddr);
    808 			break;
    809 		}
    810 		while (j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) && *idp == daddr) {
    811 			bread(devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
    812 			    0, &bp);
    813 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    814 			brelse(bp, 0);
    815 
    816 			++j;
    817 			daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs));
    818 			--idp;
    819 		}
    820 		if (i < lfs_ss_getnfinfo(fs, sp)) {
    821 			if (func)
    822 				func(daddr, fp);
    823 			for (k = 0; k < lfs_fi_getnblocks(fs, fp); k++) {
    824 				len = (k == lfs_fi_getnblocks(fs, fp) - 1 ?
    825 				       lfs_fi_getlastlength(fs, fp)
    826 				       : lfs_sb_getbsize(fs));
    827 				bread(devvp, LFS_FSBTODB(fs, daddr), len,
    828 				    0, &bp);
    829 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    830 				brelse(bp, 0);
    831 				daddr += lfs_btofsb(fs, len);
    832 			}
    833 			fp = NEXT_FINFO(fs, fp);
    834 		}
    835 	}
    836 
    837 	if (datac != nblocks) {
    838 		pwarn("Partial segment at 0x%jx expected %d blocks counted %d\n",
    839 		    (intmax_t)pseg_addr, nblocks, datac);
    840 	}
    841 	/* XXX ondisk32 */
    842 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    843 	/* Check the data checksum */
    844 	if (ccksum != lfs_ss_getdatasum(fs, sp)) {
    845 		pwarn("Partial segment at 0x%jx data checksum"
    846 		      " mismatch: given 0x%x, computed 0x%x\n",
    847 		      (uintmax_t)pseg_addr, lfs_ss_getdatasum(fs, sp), ccksum);
    848 		free(datap);
    849 		return 0;
    850 	}
    851 	free(datap);
    852 	assert(bc >= 0);
    853 	return bc;
    854 }
    855 
    856 /* print message and exit */
    857 void
    858 my_vpanic(int fatal, const char *fmt, va_list ap)
    859 {
    860         (void) vprintf(fmt, ap);
    861 	exit(8);
    862 }
    863 
    864 void
    865 call_panic(const char *fmt, ...)
    866 {
    867 	va_list ap;
    868 
    869 	va_start(ap, fmt);
    870         panic_func(1, fmt, ap);
    871 	va_end(ap);
    872 }
    873 
    874 /* Allocate a new inode. */
    875 struct uvnode *
    876 lfs_valloc(struct lfs *fs, ino_t ino)
    877 {
    878 	struct ubuf *bp, *cbp;
    879 	IFILE *ifp;
    880 	ino_t new_ino;
    881 	int error;
    882 	CLEANERINFO *cip;
    883 
    884 	/* Get the head of the freelist. */
    885 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    886 
    887 	/*
    888 	 * Remove the inode from the free list and write the new start
    889 	 * of the free list into the superblock.
    890 	 */
    891 	LFS_IENTRY(ifp, fs, new_ino, bp);
    892 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
    893 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    894 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
    895 
    896 	brelse(bp, 0);
    897 
    898 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    899 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    900 		if ((error = extend_ifile(fs)) != 0) {
    901 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    902 			return NULL;
    903 		}
    904 	}
    905 
    906 	/* Set superblock modified bit and increment file count. */
    907         sbdirty();
    908 	lfs_sb_addnfiles(fs, 1);
    909 
    910         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
    911 }
    912 
    913 #ifdef IN_FSCK_LFS
    914 void reset_maxino(ino_t);
    915 #endif
    916 
    917 /*
    918  * Add a new block to the Ifile, to accommodate future file creations.
    919  */
    920 int
    921 extend_ifile(struct lfs *fs)
    922 {
    923 	struct uvnode *vp;
    924 	struct inode *ip;
    925 	IFILE64 *ifp64;
    926 	IFILE32 *ifp32;
    927 	IFILE_V1 *ifp_v1;
    928 	struct ubuf *bp, *cbp;
    929 	daddr_t i, blkno, max;
    930 	ino_t oldlast;
    931 	CLEANERINFO *cip;
    932 
    933 	vp = fs->lfs_ivnode;
    934 	ip = VTOI(vp);
    935 	blkno = lfs_lblkno(fs, ip->i_ffs1_size);
    936 
    937 	lfs_balloc(vp, ip->i_ffs1_size, lfs_sb_getbsize(fs), &bp);
    938 	ip->i_ffs1_size += lfs_sb_getbsize(fs);
    939 	ip->i_flag |= IN_MODIFIED;
    940 
    941 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    942 		lfs_sb_getifpb(fs);
    943 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    944 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    945 	max = i + lfs_sb_getifpb(fs);
    946 	lfs_sb_subbfree(fs, lfs_btofsb(fs, lfs_sb_getbsize(fs)));
    947 
    948 	if (fs->lfs_is64) {
    949 		for (ifp64 = (IFILE64 *)bp->b_data; i < max; ++ifp64) {
    950 			ifp64->if_version = 1;
    951 			ifp64->if_daddr = LFS_UNUSED_DADDR;
    952 			ifp64->if_nextfree = ++i;
    953 		}
    954 		ifp64--;
    955 		ifp64->if_nextfree = oldlast;
    956 	} else if (lfs_sb_getversion(fs) > 1) {
    957 		for (ifp32 = (IFILE32 *)bp->b_data; i < max; ++ifp32) {
    958 			ifp32->if_version = 1;
    959 			ifp32->if_daddr = LFS_UNUSED_DADDR;
    960 			ifp32->if_nextfree = ++i;
    961 		}
    962 		ifp32--;
    963 		ifp32->if_nextfree = oldlast;
    964 	} else {
    965 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    966 			ifp_v1->if_version = 1;
    967 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    968 			ifp_v1->if_nextfree = ++i;
    969 		}
    970 		ifp_v1--;
    971 		ifp_v1->if_nextfree = oldlast;
    972 	}
    973 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    974 
    975 	LFS_BWRITE_LOG(bp);
    976 
    977 #ifdef IN_FSCK_LFS
    978 	reset_maxino(((ip->i_ffs1_size >> lfs_sb_getbshift(fs))
    979 		      - lfs_sb_getsegtabsz(fs)
    980 		      - lfs_sb_getcleansz(fs)) * lfs_sb_getifpb(fs));
    981 #endif
    982 	return 0;
    983 }
    984 
    985 /*
    986  * Allocate a block, and to inode and filesystem block accounting for it
    987  * and for any indirect blocks the may need to be created in order for
    988  * this block to be created.
    989  *
    990  * Blocks which have never been accounted for (i.e., which "do not exist")
    991  * have disk address 0, which is translated by ulfs_bmap to the special value
    992  * UNASSIGNED == -1, as in the historical ULFS.
    993  *
    994  * Blocks which have been accounted for but which have not yet been written
    995  * to disk are given the new special disk address UNWRITTEN == -2, so that
    996  * they can be differentiated from completely new blocks.
    997  */
    998 int
    999 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
   1000 {
   1001 	int offset;
   1002 	daddr_t daddr, idaddr;
   1003 	struct ubuf *ibp, *bp;
   1004 	struct inode *ip;
   1005 	struct lfs *fs;
   1006 	struct indir indirs[ULFS_NIADDR+2], *idp;
   1007 	daddr_t	lbn, lastblock;
   1008 	int bcount;
   1009 	int error, frags, i, nsize, osize, num;
   1010 
   1011 	ip = VTOI(vp);
   1012 	fs = ip->i_lfs;
   1013 	offset = lfs_blkoff(fs, startoffset);
   1014 	lbn = lfs_lblkno(fs, startoffset);
   1015 
   1016 	/*
   1017 	 * Three cases: it's a block beyond the end of file, it's a block in
   1018 	 * the file that may or may not have been assigned a disk address or
   1019 	 * we're writing an entire block.
   1020 	 *
   1021 	 * Note, if the daddr is UNWRITTEN, the block already exists in
   1022 	 * the cache (it was read or written earlier).	If so, make sure
   1023 	 * we don't count it as a new block or zero out its contents. If
   1024 	 * it did not, make sure we allocate any necessary indirect
   1025 	 * blocks.
   1026 	 *
   1027 	 * If we are writing a block beyond the end of the file, we need to
   1028 	 * check if the old last block was a fragment.	If it was, we need
   1029 	 * to rewrite it.
   1030 	 */
   1031 
   1032 	if (bpp)
   1033 		*bpp = NULL;
   1034 
   1035 	/* Check for block beyond end of file and fragment extension needed. */
   1036 	lastblock = lfs_lblkno(fs, ip->i_ffs1_size);
   1037 	if (lastblock < ULFS_NDADDR && lastblock < lbn) {
   1038 		osize = lfs_blksize(fs, ip, lastblock);
   1039 		if (osize < lfs_sb_getbsize(fs) && osize > 0) {
   1040 			if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
   1041 						    lastblock,
   1042 						    (bpp ? &bp : NULL))))
   1043 				return (error);
   1044 			ip->i_ffs1_size = (lastblock + 1) * lfs_sb_getbsize(fs);
   1045 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1046 			if (bpp)
   1047 				(void) VOP_BWRITE(bp);
   1048 		}
   1049 	}
   1050 
   1051 	/*
   1052 	 * If the block we are writing is a direct block, it's the last
   1053 	 * block in the file, and offset + iosize is less than a full
   1054 	 * block, we can write one or more fragments.  There are two cases:
   1055 	 * the block is brand new and we should allocate it the correct
   1056 	 * size or it already exists and contains some fragments and
   1057 	 * may need to extend it.
   1058 	 */
   1059 	if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_ffs1_size) <= lbn) {
   1060 		osize = lfs_blksize(fs, ip, lbn);
   1061 		nsize = lfs_fragroundup(fs, offset + iosize);
   1062 		if (lfs_lblktosize(fs, lbn) >= ip->i_ffs1_size) {
   1063 			/* Brand new block or fragment */
   1064 			frags = lfs_numfrags(fs, nsize);
   1065 			if (bpp) {
   1066 				*bpp = bp = getblk(vp, lbn, nsize);
   1067 				bp->b_blkno = UNWRITTEN;
   1068 			}
   1069 			ip->i_lfs_effnblks += frags;
   1070 			lfs_sb_subbfree(fs, frags);
   1071 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1072 		} else {
   1073 			if (nsize <= osize) {
   1074 				/* No need to extend */
   1075 				if (bpp && (error = bread(vp, lbn, osize,
   1076 				    0, &bp)))
   1077 					return error;
   1078 			} else {
   1079 				/* Extend existing block */
   1080 				if ((error =
   1081 				     lfs_fragextend(vp, osize, nsize, lbn,
   1082 						    (bpp ? &bp : NULL))))
   1083 					return error;
   1084 			}
   1085 			if (bpp)
   1086 				*bpp = bp;
   1087 		}
   1088 		return 0;
   1089 	}
   1090 
   1091 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
   1092 	if (error)
   1093 		return (error);
   1094 
   1095 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
   1096 
   1097 	/*
   1098 	 * Do byte accounting all at once, so we can gracefully fail *before*
   1099 	 * we start assigning blocks.
   1100 	 */
   1101         frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */
   1102 	bcount = 0;
   1103 	if (daddr == UNASSIGNED) {
   1104 		bcount = frags;
   1105 	}
   1106 	for (i = 1; i < num; ++i) {
   1107 		if (!indirs[i].in_exists) {
   1108 			bcount += frags;
   1109 		}
   1110 	}
   1111 	lfs_sb_subbfree(fs, bcount);
   1112 	ip->i_lfs_effnblks += bcount;
   1113 
   1114 	if (daddr == UNASSIGNED) {
   1115 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
   1116 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1117 		}
   1118 
   1119 		/*
   1120 		 * Create new indirect blocks if necessary
   1121 		 */
   1122 		if (num > 1) {
   1123 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
   1124 			for (i = 1; i < num; ++i) {
   1125 				ibp = getblk(vp, indirs[i].in_lbn,
   1126 				    lfs_sb_getbsize(fs));
   1127 				if (!indirs[i].in_exists) {
   1128 					memset(ibp->b_data, 0, ibp->b_bufsize);
   1129 					ibp->b_blkno = UNWRITTEN;
   1130 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
   1131 					ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
   1132 					ibp->b_flags |= B_READ;
   1133 					VOP_STRATEGY(ibp);
   1134 				}
   1135 				/*
   1136 				 * This block exists, but the next one may not.
   1137 				 * If that is the case mark it UNWRITTEN to
   1138                                  * keep the accounting straight.
   1139 				 */
   1140 				/* XXX ondisk32 */
   1141 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
   1142 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
   1143 						UNWRITTEN;
   1144 				/* XXX ondisk32 */
   1145 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
   1146 				if ((error = VOP_BWRITE(ibp)))
   1147 					return error;
   1148 			}
   1149 		}
   1150 	}
   1151 
   1152 
   1153 	/*
   1154 	 * Get the existing block from the cache, if requested.
   1155 	 */
   1156 	if (bpp)
   1157 		*bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn));
   1158 
   1159 	/*
   1160 	 * The block we are writing may be a brand new block
   1161 	 * in which case we need to do accounting.
   1162 	 *
   1163 	 * We can tell a truly new block because ulfs_bmaparray will say
   1164 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
   1165 	 * disk address UNWRITTEN.
   1166 	 */
   1167 	if (daddr == UNASSIGNED) {
   1168 		if (bpp) {
   1169 			/* Note the new address */
   1170 			bp->b_blkno = UNWRITTEN;
   1171 		}
   1172 
   1173 		switch (num) {
   1174 		    case 0:
   1175 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1176 			break;
   1177 		    case 1:
   1178 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1179 			break;
   1180 		    default:
   1181 			idp = &indirs[num - 1];
   1182 			if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs), 0, &ibp))
   1183 				panic("lfs_balloc: bread bno %lld",
   1184 				    (long long)idp->in_lbn);
   1185 			/* XXX ondisk32 */
   1186 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
   1187 			VOP_BWRITE(ibp);
   1188 		}
   1189 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
   1190 		/*
   1191 		 * Not a brand new block, also not in the cache;
   1192 		 * read it in from disk.
   1193 		 */
   1194 		if (iosize == lfs_sb_getbsize(fs))
   1195 			/* Optimization: I/O is unnecessary. */
   1196 			bp->b_blkno = daddr;
   1197 		else {
   1198 			/*
   1199 			 * We need to read the block to preserve the
   1200 			 * existing bytes.
   1201 			 */
   1202 			bp->b_blkno = daddr;
   1203 			bp->b_flags |= B_READ;
   1204 			VOP_STRATEGY(bp);
   1205 			return 0;
   1206 		}
   1207 	}
   1208 
   1209 	return (0);
   1210 }
   1211 
   1212 int
   1213 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
   1214                struct ubuf **bpp)
   1215 {
   1216 	struct inode *ip;
   1217 	struct lfs *fs;
   1218 	int frags;
   1219 	int error;
   1220 
   1221 	ip = VTOI(vp);
   1222 	fs = ip->i_lfs;
   1223 	frags = (long)lfs_numfrags(fs, nsize - osize);
   1224 	error = 0;
   1225 
   1226 	/*
   1227 	 * If we are not asked to actually return the block, all we need
   1228 	 * to do is allocate space for it.  UBC will handle dirtying the
   1229 	 * appropriate things and making sure it all goes to disk.
   1230 	 * Don't bother to read in that case.
   1231 	 */
   1232 	if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
   1233 		brelse(*bpp, 0);
   1234 		goto out;
   1235 	}
   1236 
   1237 	lfs_sb_subbfree(fs, frags);
   1238 	ip->i_lfs_effnblks += frags;
   1239 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1240 
   1241 	if (bpp) {
   1242 		(*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
   1243 		(void)memset((*bpp)->b_data + osize, 0, nsize - osize);
   1244 	}
   1245 
   1246     out:
   1247 	return (error);
   1248 }
   1249