Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.43
      1 /* $NetBSD: lfs.c,v 1.43 2014/07/13 02:44:21 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 #undef vnode
     77 
     78 #include <assert.h>
     79 #include <err.h>
     80 #include <errno.h>
     81 #include <stdarg.h>
     82 #include <stdio.h>
     83 #include <stdlib.h>
     84 #include <string.h>
     85 #include <unistd.h>
     86 #include <util.h>
     87 
     88 #include "bufcache.h"
     89 #include "vnode.h"
     90 #include "lfs_user.h"
     91 #include "segwrite.h"
     92 #include "kernelops.h"
     93 
     94 #define panic call_panic
     95 
     96 extern u_int32_t cksum(void *, size_t);
     97 extern u_int32_t lfs_sb_cksum(struct dlfs *);
     98 extern void pwarn(const char *, ...);
     99 
    100 extern struct uvnodelst vnodelist;
    101 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
    102 extern int nvnodes;
    103 
    104 long dev_bsize = DEV_BSIZE;
    105 
    106 static int
    107 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
    108 
    109 int fsdirty = 0;
    110 void (*panic_func)(int, const char *, va_list) = my_vpanic;
    111 
    112 /*
    113  * LFS buffer and uvnode operations
    114  */
    115 
    116 int
    117 lfs_vop_strategy(struct ubuf * bp)
    118 {
    119 	int count;
    120 
    121 	if (bp->b_flags & B_READ) {
    122 		count = kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    123 		    bp->b_blkno * dev_bsize);
    124 		if (count == bp->b_bcount)
    125 			bp->b_flags |= B_DONE;
    126 	} else {
    127 		count = kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    128 		    bp->b_blkno * dev_bsize);
    129 		if (count == 0) {
    130 			perror("pwrite");
    131 			return -1;
    132 		}
    133 		bp->b_flags &= ~B_DELWRI;
    134 		reassignbuf(bp, bp->b_vp);
    135 	}
    136 	return 0;
    137 }
    138 
    139 int
    140 lfs_vop_bwrite(struct ubuf * bp)
    141 {
    142 	struct lfs *fs;
    143 
    144 	fs = bp->b_vp->v_fs;
    145 	if (!(bp->b_flags & B_DELWRI)) {
    146 		fs->lfs_avail -= lfs_btofsb(fs, bp->b_bcount);
    147 	}
    148 	bp->b_flags |= B_DELWRI | B_LOCKED;
    149 	reassignbuf(bp, bp->b_vp);
    150 	brelse(bp, 0);
    151 	return 0;
    152 }
    153 
    154 /*
    155  * ulfs_bmaparray does the bmap conversion, and if requested returns the
    156  * array of logical blocks which must be traversed to get to a block.
    157  * Each entry contains the offset into that block that gets you to the
    158  * next block and the disk address of the block (if it is assigned).
    159  */
    160 int
    161 ulfs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
    162 {
    163 	struct inode *ip;
    164 	struct ubuf *bp;
    165 	struct indir a[ULFS_NIADDR + 1], *xap;
    166 	daddr_t daddr;
    167 	daddr_t metalbn;
    168 	int error, num;
    169 
    170 	ip = VTOI(vp);
    171 
    172 	if (bn >= 0 && bn < ULFS_NDADDR) {
    173 		if (nump != NULL)
    174 			*nump = 0;
    175 		*bnp = LFS_FSBTODB(fs, ip->i_ffs1_db[bn]);
    176 		if (*bnp == 0)
    177 			*bnp = -1;
    178 		return (0);
    179 	}
    180 	xap = ap == NULL ? a : ap;
    181 	if (!nump)
    182 		nump = &num;
    183 	if ((error = ulfs_getlbns(fs, vp, bn, xap, nump)) != 0)
    184 		return (error);
    185 
    186 	num = *nump;
    187 
    188 	/* Get disk address out of indirect block array */
    189 	daddr = ip->i_ffs1_ib[xap->in_off];
    190 
    191 	for (bp = NULL, ++xap; --num; ++xap) {
    192 		/* Exit the loop if there is no disk address assigned yet and
    193 		 * the indirect block isn't in the cache, or if we were
    194 		 * looking for an indirect block and we've found it. */
    195 
    196 		metalbn = xap->in_lbn;
    197 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
    198 			break;
    199 		/*
    200 		 * If we get here, we've either got the block in the cache
    201 		 * or we have a disk address for it, go fetch it.
    202 		 */
    203 		if (bp)
    204 			brelse(bp, 0);
    205 
    206 		xap->in_exists = 1;
    207 		bp = getblk(vp, metalbn, fs->lfs_bsize);
    208 
    209 		if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
    210 			bp->b_blkno = LFS_FSBTODB(fs, daddr);
    211 			bp->b_flags |= B_READ;
    212 			VOP_STRATEGY(bp);
    213 		}
    214 		daddr = ((ulfs_daddr_t *) bp->b_data)[xap->in_off];
    215 	}
    216 	if (bp)
    217 		brelse(bp, 0);
    218 
    219 	daddr = LFS_FSBTODB(fs, (ulfs_daddr_t) daddr);
    220 	*bnp = daddr == 0 ? -1 : daddr;
    221 	return (0);
    222 }
    223 
    224 /*
    225  * Create an array of logical block number/offset pairs which represent the
    226  * path of indirect blocks required to access a data block.  The first "pair"
    227  * contains the logical block number of the appropriate single, double or
    228  * triple indirect block and the offset into the inode indirect block array.
    229  * Note, the logical block number of the inode single/double/triple indirect
    230  * block appears twice in the array, once with the offset into the i_ffs1_ib and
    231  * once with the offset into the page itself.
    232  */
    233 int
    234 ulfs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
    235 {
    236 	daddr_t metalbn, realbn;
    237 	int64_t blockcnt;
    238 	int lbc;
    239 	int i, numlevels, off;
    240 	int lognindir, indir;
    241 
    242 	metalbn = 0;    /* XXXGCC -Wuninitialized [sh3] */
    243 
    244 	if (nump)
    245 		*nump = 0;
    246 	numlevels = 0;
    247 	realbn = bn;
    248 	if (bn < 0)
    249 		bn = -bn;
    250 
    251 	lognindir = -1;
    252 	for (indir = fs->lfs_nindir; indir; indir >>= 1)
    253 		++lognindir;
    254 
    255 	/* Determine the number of levels of indirection.  After this loop is
    256 	 * done, blockcnt indicates the number of data blocks possible at the
    257 	 * given level of indirection, and ULFS_NIADDR - i is the number of levels
    258 	 * of indirection needed to locate the requested block. */
    259 
    260 	bn -= ULFS_NDADDR;
    261 	for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) {
    262 		if (i == 0)
    263 			return (EFBIG);
    264 
    265 		lbc += lognindir;
    266 		blockcnt = (int64_t) 1 << lbc;
    267 
    268 		if (bn < blockcnt)
    269 			break;
    270 	}
    271 
    272 	/* Calculate the address of the first meta-block. */
    273 	metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i);
    274 
    275 	/* At each iteration, off is the offset into the bap array which is an
    276 	 * array of disk addresses at the current level of indirection. The
    277 	 * logical block number and the offset in that block are stored into
    278 	 * the argument array. */
    279 	ap->in_lbn = metalbn;
    280 	ap->in_off = off = ULFS_NIADDR - i;
    281 	ap->in_exists = 0;
    282 	ap++;
    283 	for (++numlevels; i <= ULFS_NIADDR; i++) {
    284 		/* If searching for a meta-data block, quit when found. */
    285 		if (metalbn == realbn)
    286 			break;
    287 
    288 		lbc -= lognindir;
    289 		blockcnt = (int64_t) 1 << lbc;
    290 		off = (bn >> lbc) & (fs->lfs_nindir - 1);
    291 
    292 		++numlevels;
    293 		ap->in_lbn = metalbn;
    294 		ap->in_off = off;
    295 		ap->in_exists = 0;
    296 		++ap;
    297 
    298 		metalbn -= -1 + (off << lbc);
    299 	}
    300 	if (nump)
    301 		*nump = numlevels;
    302 	return (0);
    303 }
    304 
    305 int
    306 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
    307 {
    308 	return ulfs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
    309 }
    310 
    311 /* Search a block for a specific dinode. */
    312 struct ulfs1_dinode *
    313 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
    314 {
    315 	struct ulfs1_dinode *dip = (struct ulfs1_dinode *) bp->b_data;
    316 	struct ulfs1_dinode *ldip, *fin;
    317 
    318 	fin = dip + LFS_INOPB(fs);
    319 
    320 	/*
    321 	 * Read the inode block backwards, since later versions of the
    322 	 * inode will supercede earlier ones.  Though it is unlikely, it is
    323 	 * possible that the same inode will appear in the same inode block.
    324 	 */
    325 	for (ldip = fin - 1; ldip >= dip; --ldip)
    326 		if (ldip->di_inumber == ino)
    327 			return (ldip);
    328 	return NULL;
    329 }
    330 
    331 /*
    332  * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
    333  * XXX it currently loses atime information.
    334  */
    335 struct uvnode *
    336 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ulfs_daddr_t daddr)
    337 {
    338 	struct uvnode *vp;
    339 	struct inode *ip;
    340 	struct ulfs1_dinode *dip;
    341 	struct ubuf *bp;
    342 	int i, hash;
    343 
    344 	vp = ecalloc(1, sizeof(*vp));
    345 	vp->v_fd = fd;
    346 	vp->v_fs = fs;
    347 	vp->v_usecount = 0;
    348 	vp->v_strategy_op = lfs_vop_strategy;
    349 	vp->v_bwrite_op = lfs_vop_bwrite;
    350 	vp->v_bmap_op = lfs_vop_bmap;
    351 	LIST_INIT(&vp->v_cleanblkhd);
    352 	LIST_INIT(&vp->v_dirtyblkhd);
    353 
    354 	ip = ecalloc(1, sizeof(*ip));
    355 
    356 	ip->i_din.ffs1_din = ecalloc(1, sizeof(*ip->i_din.ffs1_din));
    357 
    358 	/* Initialize the inode -- from lfs_vcreate. */
    359 	ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs));
    360 	vp->v_data = ip;
    361 	/* ip->i_vnode = vp; */
    362 	ip->i_number = ino;
    363 	ip->i_lockf = 0;
    364 	ip->i_lfs_effnblks = 0;
    365 	ip->i_flag = 0;
    366 
    367 	/* Load inode block and find inode */
    368 	if (daddr > 0) {
    369 		bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), fs->lfs_ibsize,
    370 		    NULL, 0, &bp);
    371 		bp->b_flags |= B_AGE;
    372 		dip = lfs_ifind(fs, ino, bp);
    373 		if (dip == NULL) {
    374 			brelse(bp, 0);
    375 			free(ip);
    376 			free(vp);
    377 			return NULL;
    378 		}
    379 		memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
    380 		brelse(bp, 0);
    381 	}
    382 	ip->i_number = ino;
    383 	/* ip->i_devvp = fs->lfs_devvp; */
    384 	ip->i_lfs = fs;
    385 
    386 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
    387 	ip->i_lfs_osize = ip->i_ffs1_size;
    388 #if 0
    389 	if (fs->lfs_version > 1) {
    390 		ip->i_ffs1_atime = ts.tv_sec;
    391 		ip->i_ffs1_atimensec = ts.tv_nsec;
    392 	}
    393 #endif
    394 
    395 	memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
    396 	for (i = 0; i < ULFS_NDADDR; i++)
    397 		if (ip->i_ffs1_db[i] != 0)
    398 			ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
    399 
    400 	++nvnodes;
    401 	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
    402 	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
    403 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
    404 
    405 	return vp;
    406 }
    407 
    408 static struct uvnode *
    409 lfs_vget(void *vfs, ino_t ino)
    410 {
    411 	struct lfs *fs = (struct lfs *)vfs;
    412 	ulfs_daddr_t daddr;
    413 	struct ubuf *bp;
    414 	IFILE *ifp;
    415 
    416 	LFS_IENTRY(ifp, fs, ino, bp);
    417 	daddr = ifp->if_daddr;
    418 	brelse(bp, 0);
    419 	if (daddr <= 0 || lfs_dtosn(fs, daddr) >= fs->lfs_nseg)
    420 		return NULL;
    421 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
    422 }
    423 
    424 /* Check superblock magic number and checksum */
    425 static int
    426 check_sb(struct lfs *fs)
    427 {
    428 	u_int32_t checksum;
    429 
    430 	if (fs->lfs_magic != LFS_MAGIC) {
    431 		printf("Superblock magic number (0x%lx) does not match "
    432 		       "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
    433 		       (unsigned long) LFS_MAGIC);
    434 		return 1;
    435 	}
    436 	/* checksum */
    437 	checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
    438 	if (fs->lfs_cksum != checksum) {
    439 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
    440 		    (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
    441 		return 1;
    442 	}
    443 	return 0;
    444 }
    445 
    446 /* Initialize LFS library; load superblocks and choose which to use. */
    447 struct lfs *
    448 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
    449 {
    450 	struct uvnode *devvp;
    451 	struct ubuf *bp;
    452 	int tryalt;
    453 	struct lfs *fs, *altfs;
    454 
    455 	vfs_init();
    456 
    457 	devvp = ecalloc(1, sizeof(*devvp));
    458 	devvp->v_fs = NULL;
    459 	devvp->v_fd = devfd;
    460 	devvp->v_strategy_op = raw_vop_strategy;
    461 	devvp->v_bwrite_op = raw_vop_bwrite;
    462 	devvp->v_bmap_op = raw_vop_bmap;
    463 	LIST_INIT(&devvp->v_cleanblkhd);
    464 	LIST_INIT(&devvp->v_dirtyblkhd);
    465 
    466 	tryalt = 0;
    467 	if (dummy_read) {
    468 		if (sblkno == 0)
    469 			sblkno = LFS_LABELPAD / dev_bsize;
    470 		fs = ecalloc(1, sizeof(*fs));
    471 		fs->lfs_devvp = devvp;
    472 	} else {
    473 		if (sblkno == 0) {
    474 			sblkno = LFS_LABELPAD / dev_bsize;
    475 			tryalt = 1;
    476 		} else if (debug) {
    477 			printf("No -b flag given, not attempting to verify checkpoint\n");
    478 		}
    479 
    480 		dev_bsize = DEV_BSIZE;
    481 
    482 		(void)bread(devvp, sblkno, LFS_SBPAD, NOCRED, 0, &bp);
    483 		fs = ecalloc(1, sizeof(*fs));
    484 		fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    485 		fs->lfs_devvp = devvp;
    486 		bp->b_flags |= B_INVAL;
    487 		brelse(bp, 0);
    488 
    489 		dev_bsize = fs->lfs_fsize >> fs->lfs_fsbtodb;
    490 
    491 		if (tryalt) {
    492 			(void)bread(devvp, LFS_FSBTODB(fs, fs->lfs_sboffs[1]),
    493 		    	LFS_SBPAD, NOCRED, 0, &bp);
    494 			altfs = ecalloc(1, sizeof(*altfs));
    495 			altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
    496 			altfs->lfs_devvp = devvp;
    497 			bp->b_flags |= B_INVAL;
    498 			brelse(bp, 0);
    499 
    500 			if (check_sb(fs) || fs->lfs_idaddr <= 0) {
    501 				if (debug)
    502 					printf("Primary superblock is no good, using first alternate\n");
    503 				free(fs);
    504 				fs = altfs;
    505 			} else {
    506 				/* If both superblocks check out, try verification */
    507 				if (check_sb(altfs)) {
    508 					if (debug)
    509 						printf("First alternate superblock is no good, using primary\n");
    510 					free(altfs);
    511 				} else {
    512 					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    513 						free(altfs);
    514 					} else {
    515 						free(fs);
    516 						fs = altfs;
    517 					}
    518 				}
    519 			}
    520 		}
    521 		if (check_sb(fs)) {
    522 			free(fs);
    523 			return NULL;
    524 		}
    525 	}
    526 
    527 	/* Compatibility */
    528 	if (fs->lfs_version < 2) {
    529 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    530 		fs->lfs_ibsize = fs->lfs_bsize;
    531 		fs->lfs_start = fs->lfs_sboffs[0];
    532 		fs->lfs_tstamp = fs->lfs_otstamp;
    533 		fs->lfs_fsbtodb = 0;
    534 	}
    535 
    536 	if (!dummy_read) {
    537 		fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *));
    538 		fs->lfs_suflags[0] = emalloc(fs->lfs_nseg * sizeof(u_int32_t));
    539 		fs->lfs_suflags[1] = emalloc(fs->lfs_nseg * sizeof(u_int32_t));
    540 	}
    541 
    542 	if (idaddr == 0)
    543 		idaddr = fs->lfs_idaddr;
    544 	else
    545 		fs->lfs_idaddr = idaddr;
    546 	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
    547 	fs->lfs_ivnode = lfs_raw_vget(fs,
    548 		(dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
    549 		idaddr);
    550 	if (fs->lfs_ivnode == NULL)
    551 		return NULL;
    552 
    553 	register_vget((void *)fs, lfs_vget);
    554 
    555 	return fs;
    556 }
    557 
    558 /*
    559  * Check partial segment validity between fs->lfs_offset and the given goal.
    560  *
    561  * If goal == 0, just keep on going until the segments stop making sense,
    562  * and return the address of the last valid partial segment.
    563  *
    564  * If goal != 0, return the address of the first partial segment that failed,
    565  * or "goal" if we reached it without failure (the partial segment *at* goal
    566  * need not be valid).
    567  */
    568 ulfs_daddr_t
    569 try_verify(struct lfs *osb, struct uvnode *devvp, ulfs_daddr_t goal, int debug)
    570 {
    571 	ulfs_daddr_t daddr, odaddr;
    572 	SEGSUM *sp;
    573 	int i, bc, hitclean;
    574 	struct ubuf *bp;
    575 	ulfs_daddr_t nodirop_daddr;
    576 	u_int64_t serial;
    577 
    578 	bc = 0;
    579 	hitclean = 0;
    580 	odaddr = -1;
    581 	daddr = osb->lfs_offset;
    582 	nodirop_daddr = daddr;
    583 	serial = osb->lfs_serial;
    584 	while (daddr != goal) {
    585 		/*
    586 		 * Don't mistakenly read a superblock, if there is one here.
    587 		 */
    588 		if (lfs_sntod(osb, lfs_dtosn(osb, daddr)) == daddr) {
    589 			if (daddr == osb->lfs_start)
    590 				daddr += lfs_btofsb(osb, LFS_LABELPAD);
    591 			for (i = 0; i < LFS_MAXNUMSB; i++) {
    592 				if (osb->lfs_sboffs[i] < daddr)
    593 					break;
    594 				if (osb->lfs_sboffs[i] == daddr)
    595 					daddr += lfs_btofsb(osb, LFS_SBPAD);
    596 			}
    597 		}
    598 
    599 		/* Read in summary block */
    600 		bread(devvp, LFS_FSBTODB(osb, daddr), osb->lfs_sumsize,
    601 		    NULL, 0, &bp);
    602 		sp = (SEGSUM *)bp->b_data;
    603 
    604 		/*
    605 		 * Check for a valid segment summary belonging to our fs.
    606 		 */
    607 		if (sp->ss_magic != SS_MAGIC ||
    608 		    sp->ss_ident != osb->lfs_ident ||
    609 		    sp->ss_serial < serial ||	/* XXX strengthen this */
    610 		    sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
    611 			sizeof(sp->ss_sumsum))) {
    612 			brelse(bp, 0);
    613 			if (debug) {
    614 				if (sp->ss_magic != SS_MAGIC)
    615 					pwarn("pseg at 0x%x: "
    616 					      "wrong magic number\n",
    617 					      (int)daddr);
    618 				else if (sp->ss_ident != osb->lfs_ident)
    619 					pwarn("pseg at 0x%x: "
    620 					      "expected ident %llx, got %llx\n",
    621 					      (int)daddr,
    622 					      (long long)sp->ss_ident,
    623 					      (long long)osb->lfs_ident);
    624 				else if (sp->ss_serial >= serial)
    625 					pwarn("pseg at 0x%x: "
    626 					      "serial %d < %d\n", (int)daddr,
    627 					      (int)sp->ss_serial, (int)serial);
    628 				else
    629 					pwarn("pseg at 0x%x: "
    630 					      "summary checksum wrong\n",
    631 					      (int)daddr);
    632 			}
    633 			break;
    634 		}
    635 		if (debug && sp->ss_serial != serial)
    636 			pwarn("warning, serial=%d ss_serial=%d\n",
    637 				(int)serial, (int)sp->ss_serial);
    638 		++serial;
    639 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    640 		if (bc == 0) {
    641 			brelse(bp, 0);
    642 			break;
    643 		}
    644 		if (debug)
    645 			pwarn("summary good: 0x%x/%d\n", (int)daddr,
    646 			      (int)sp->ss_serial);
    647 		assert (bc > 0);
    648 		odaddr = daddr;
    649 		daddr += lfs_btofsb(osb, osb->lfs_sumsize + bc);
    650 		if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) ||
    651 		    lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr +
    652 			lfs_btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize) - 1)) {
    653 			daddr = sp->ss_next;
    654 		}
    655 
    656 		/*
    657 		 * Check for the beginning and ending of a sequence of
    658 		 * dirops.  Writes from the cleaner never involve new
    659 		 * information, and are always checkpoints; so don't try
    660 		 * to roll forward through them.  Likewise, psegs written
    661 		 * by a previous roll-forward attempt are not interesting.
    662 		 */
    663 		if (sp->ss_flags & (SS_CLEAN | SS_RFW))
    664 			hitclean = 1;
    665 		if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0)
    666 			nodirop_daddr = daddr;
    667 
    668 		brelse(bp, 0);
    669 	}
    670 
    671 	if (goal == 0)
    672 		return nodirop_daddr;
    673 	else
    674 		return daddr;
    675 }
    676 
    677 /* Use try_verify to check whether the newer superblock is valid. */
    678 struct lfs *
    679 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    680 {
    681 	ulfs_daddr_t daddr;
    682 	struct lfs *osb, *nsb;
    683 
    684 	/*
    685 	 * Verify the checkpoint of the newer superblock,
    686 	 * if the timestamp/serial number of the two superblocks is
    687 	 * different.
    688 	 */
    689 
    690 	osb = NULL;
    691 	if (debug)
    692 		pwarn("sb0 %lld, sb1 %lld",
    693 		      (long long) sb0->lfs_serial,
    694 		      (long long) sb1->lfs_serial);
    695 
    696 	if ((sb0->lfs_version == 1 &&
    697 		sb0->lfs_otstamp != sb1->lfs_otstamp) ||
    698 	    (sb0->lfs_version > 1 &&
    699 		sb0->lfs_serial != sb1->lfs_serial)) {
    700 		if (sb0->lfs_version == 1) {
    701 			if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
    702 				osb = sb1;
    703 				nsb = sb0;
    704 			} else {
    705 				osb = sb0;
    706 				nsb = sb1;
    707 			}
    708 		} else {
    709 			if (sb0->lfs_serial > sb1->lfs_serial) {
    710 				osb = sb1;
    711 				nsb = sb0;
    712 			} else {
    713 				osb = sb0;
    714 				nsb = sb1;
    715 			}
    716 		}
    717 		if (debug) {
    718 			printf("Attempting to verify newer checkpoint...");
    719 			fflush(stdout);
    720 		}
    721 		daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
    722 
    723 		if (debug)
    724 			printf("done.\n");
    725 		if (daddr == nsb->lfs_offset) {
    726 			pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
    727 			    (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    728 			sbdirty();
    729 		} else {
    730 			pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
    731 		}
    732 		return (daddr == nsb->lfs_offset ? nsb : osb);
    733 	}
    734 	/* Nothing to check */
    735 	return osb;
    736 }
    737 
    738 /* Verify a partial-segment summary; return the number of bytes on disk. */
    739 int
    740 check_summary(struct lfs *fs, SEGSUM *sp, ulfs_daddr_t pseg_addr, int debug,
    741 	      struct uvnode *devvp, void (func(ulfs_daddr_t, FINFO *)))
    742 {
    743 	FINFO *fp;
    744 	int bc;			/* Bytes in partial segment */
    745 	int nblocks;
    746 	ulfs_daddr_t daddr;
    747 	ulfs_daddr_t *dp, *idp;
    748 	struct ubuf *bp;
    749 	int i, j, k, datac, len;
    750 	u_int32_t *datap;
    751 	u_int32_t ccksum;
    752 
    753 	/* We've already checked the sumsum, just do the data bounds and sum */
    754 
    755 	/* Count the blocks. */
    756 	nblocks = howmany(sp->ss_ninos, LFS_INOPB(fs));
    757 	bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
    758 	assert(bc >= 0);
    759 
    760 	fp = (FINFO *) (sp + 1);
    761 	for (i = 0; i < sp->ss_nfinfo; i++) {
    762 		nblocks += fp->fi_nblocks;
    763 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    764 					   << fs->lfs_bshift);
    765 		assert(bc >= 0);
    766 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    767 		if (((char *)fp) - (char *)sp > fs->lfs_sumsize)
    768 			return 0;
    769 	}
    770 	datap = emalloc(nblocks * sizeof(*datap));
    771 	datac = 0;
    772 
    773 	dp = (ulfs_daddr_t *) sp;
    774 	dp += fs->lfs_sumsize / sizeof(ulfs_daddr_t);
    775 	dp--;
    776 
    777 	idp = dp;
    778 	daddr = pseg_addr + lfs_btofsb(fs, fs->lfs_sumsize);
    779 	fp = (FINFO *) (sp + 1);
    780 	for (i = 0, j = 0;
    781 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, LFS_INOPB(fs)); i++) {
    782 		if (i >= sp->ss_nfinfo && *idp != daddr) {
    783 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
    784 			      ": found %d, wanted %d\n",
    785 			      pseg_addr, j, howmany(sp->ss_ninos, LFS_INOPB(fs)));
    786 			if (debug)
    787 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    788 				      daddr);
    789 			break;
    790 		}
    791 		while (j < howmany(sp->ss_ninos, LFS_INOPB(fs)) && *idp == daddr) {
    792 			bread(devvp, LFS_FSBTODB(fs, daddr), fs->lfs_ibsize,
    793 			    NOCRED, 0, &bp);
    794 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    795 			brelse(bp, 0);
    796 
    797 			++j;
    798 			daddr += lfs_btofsb(fs, fs->lfs_ibsize);
    799 			--idp;
    800 		}
    801 		if (i < sp->ss_nfinfo) {
    802 			if (func)
    803 				func(daddr, fp);
    804 			for (k = 0; k < fp->fi_nblocks; k++) {
    805 				len = (k == fp->fi_nblocks - 1 ?
    806 				       fp->fi_lastlength
    807 				       : fs->lfs_bsize);
    808 				bread(devvp, LFS_FSBTODB(fs, daddr), len,
    809 				    NOCRED, 0, &bp);
    810 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    811 				brelse(bp, 0);
    812 				daddr += lfs_btofsb(fs, len);
    813 			}
    814 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    815 		}
    816 	}
    817 
    818 	if (datac != nblocks) {
    819 		pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
    820 		    (long long) pseg_addr, nblocks, datac);
    821 	}
    822 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    823 	/* Check the data checksum */
    824 	if (ccksum != sp->ss_datasum) {
    825 		pwarn("Partial segment at 0x%" PRIx32 " data checksum"
    826 		      " mismatch: given 0x%x, computed 0x%x\n",
    827 		      pseg_addr, sp->ss_datasum, ccksum);
    828 		free(datap);
    829 		return 0;
    830 	}
    831 	free(datap);
    832 	assert(bc >= 0);
    833 	return bc;
    834 }
    835 
    836 /* print message and exit */
    837 void
    838 my_vpanic(int fatal, const char *fmt, va_list ap)
    839 {
    840         (void) vprintf(fmt, ap);
    841 	exit(8);
    842 }
    843 
    844 void
    845 call_panic(const char *fmt, ...)
    846 {
    847 	va_list ap;
    848 
    849 	va_start(ap, fmt);
    850         panic_func(1, fmt, ap);
    851 	va_end(ap);
    852 }
    853 
    854 /* Allocate a new inode. */
    855 struct uvnode *
    856 lfs_valloc(struct lfs *fs, ino_t ino)
    857 {
    858 	struct ubuf *bp, *cbp;
    859 	struct ifile *ifp;
    860 	ino_t new_ino;
    861 	int error;
    862 	CLEANERINFO *cip;
    863 
    864 	/* Get the head of the freelist. */
    865 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    866 
    867 	/*
    868 	 * Remove the inode from the free list and write the new start
    869 	 * of the free list into the superblock.
    870 	 */
    871 	LFS_IENTRY(ifp, fs, new_ino, bp);
    872 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    873 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    874 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    875 
    876 	brelse(bp, 0);
    877 
    878 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    879 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    880 		if ((error = extend_ifile(fs)) != 0) {
    881 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    882 			return NULL;
    883 		}
    884 	}
    885 
    886 	/* Set superblock modified bit and increment file count. */
    887         sbdirty();
    888 	++fs->lfs_nfiles;
    889 
    890         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
    891 }
    892 
    893 #ifdef IN_FSCK_LFS
    894 void reset_maxino(ino_t);
    895 #endif
    896 
    897 /*
    898  * Add a new block to the Ifile, to accommodate future file creations.
    899  */
    900 int
    901 extend_ifile(struct lfs *fs)
    902 {
    903 	struct uvnode *vp;
    904 	struct inode *ip;
    905 	IFILE *ifp;
    906 	IFILE_V1 *ifp_v1;
    907 	struct ubuf *bp, *cbp;
    908 	daddr_t i, blkno, max;
    909 	ino_t oldlast;
    910 	CLEANERINFO *cip;
    911 
    912 	vp = fs->lfs_ivnode;
    913 	ip = VTOI(vp);
    914 	blkno = lfs_lblkno(fs, ip->i_ffs1_size);
    915 
    916 	lfs_balloc(vp, ip->i_ffs1_size, fs->lfs_bsize, &bp);
    917 	ip->i_ffs1_size += fs->lfs_bsize;
    918 	ip->i_flag |= IN_MODIFIED;
    919 
    920 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    921 		fs->lfs_ifpb;
    922 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    923 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    924 	max = i + fs->lfs_ifpb;
    925 	fs->lfs_bfree -= lfs_btofsb(fs, fs->lfs_bsize);
    926 
    927 	if (fs->lfs_version == 1) {
    928 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    929 			ifp_v1->if_version = 1;
    930 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    931 			ifp_v1->if_nextfree = ++i;
    932 		}
    933 		ifp_v1--;
    934 		ifp_v1->if_nextfree = oldlast;
    935 	} else {
    936 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    937 			ifp->if_version = 1;
    938 			ifp->if_daddr = LFS_UNUSED_DADDR;
    939 			ifp->if_nextfree = ++i;
    940 		}
    941 		ifp--;
    942 		ifp->if_nextfree = oldlast;
    943 	}
    944 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    945 
    946 	LFS_BWRITE_LOG(bp);
    947 
    948 #ifdef IN_FSCK_LFS
    949 	reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz -
    950 		     fs->lfs_cleansz) * fs->lfs_ifpb);
    951 #endif
    952 	return 0;
    953 }
    954 
    955 /*
    956  * Allocate a block, and to inode and filesystem block accounting for it
    957  * and for any indirect blocks the may need to be created in order for
    958  * this block to be created.
    959  *
    960  * Blocks which have never been accounted for (i.e., which "do not exist")
    961  * have disk address 0, which is translated by ulfs_bmap to the special value
    962  * UNASSIGNED == -1, as in the historical ULFS.
    963  *
    964  * Blocks which have been accounted for but which have not yet been written
    965  * to disk are given the new special disk address UNWRITTEN == -2, so that
    966  * they can be differentiated from completely new blocks.
    967  */
    968 int
    969 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
    970 {
    971 	int offset;
    972 	daddr_t daddr, idaddr;
    973 	struct ubuf *ibp, *bp;
    974 	struct inode *ip;
    975 	struct lfs *fs;
    976 	struct indir indirs[ULFS_NIADDR+2], *idp;
    977 	daddr_t	lbn, lastblock;
    978 	int bcount;
    979 	int error, frags, i, nsize, osize, num;
    980 
    981 	ip = VTOI(vp);
    982 	fs = ip->i_lfs;
    983 	offset = lfs_blkoff(fs, startoffset);
    984 	lbn = lfs_lblkno(fs, startoffset);
    985 
    986 	/*
    987 	 * Three cases: it's a block beyond the end of file, it's a block in
    988 	 * the file that may or may not have been assigned a disk address or
    989 	 * we're writing an entire block.
    990 	 *
    991 	 * Note, if the daddr is UNWRITTEN, the block already exists in
    992 	 * the cache (it was read or written earlier).	If so, make sure
    993 	 * we don't count it as a new block or zero out its contents. If
    994 	 * it did not, make sure we allocate any necessary indirect
    995 	 * blocks.
    996 	 *
    997 	 * If we are writing a block beyond the end of the file, we need to
    998 	 * check if the old last block was a fragment.	If it was, we need
    999 	 * to rewrite it.
   1000 	 */
   1001 
   1002 	if (bpp)
   1003 		*bpp = NULL;
   1004 
   1005 	/* Check for block beyond end of file and fragment extension needed. */
   1006 	lastblock = lfs_lblkno(fs, ip->i_ffs1_size);
   1007 	if (lastblock < ULFS_NDADDR && lastblock < lbn) {
   1008 		osize = lfs_blksize(fs, ip, lastblock);
   1009 		if (osize < fs->lfs_bsize && osize > 0) {
   1010 			if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
   1011 						    lastblock,
   1012 						    (bpp ? &bp : NULL))))
   1013 				return (error);
   1014 			ip->i_ffs1_size = (lastblock + 1) * fs->lfs_bsize;
   1015 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1016 			if (bpp)
   1017 				(void) VOP_BWRITE(bp);
   1018 		}
   1019 	}
   1020 
   1021 	/*
   1022 	 * If the block we are writing is a direct block, it's the last
   1023 	 * block in the file, and offset + iosize is less than a full
   1024 	 * block, we can write one or more fragments.  There are two cases:
   1025 	 * the block is brand new and we should allocate it the correct
   1026 	 * size or it already exists and contains some fragments and
   1027 	 * may need to extend it.
   1028 	 */
   1029 	if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_ffs1_size) <= lbn) {
   1030 		osize = lfs_blksize(fs, ip, lbn);
   1031 		nsize = lfs_fragroundup(fs, offset + iosize);
   1032 		if (lfs_lblktosize(fs, lbn) >= ip->i_ffs1_size) {
   1033 			/* Brand new block or fragment */
   1034 			frags = lfs_numfrags(fs, nsize);
   1035 			if (bpp) {
   1036 				*bpp = bp = getblk(vp, lbn, nsize);
   1037 				bp->b_blkno = UNWRITTEN;
   1038 			}
   1039 			ip->i_lfs_effnblks += frags;
   1040 			fs->lfs_bfree -= frags;
   1041 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1042 		} else {
   1043 			if (nsize <= osize) {
   1044 				/* No need to extend */
   1045 				if (bpp && (error = bread(vp, lbn, osize,
   1046 				    NOCRED, 0, &bp)))
   1047 					return error;
   1048 			} else {
   1049 				/* Extend existing block */
   1050 				if ((error =
   1051 				     lfs_fragextend(vp, osize, nsize, lbn,
   1052 						    (bpp ? &bp : NULL))))
   1053 					return error;
   1054 			}
   1055 			if (bpp)
   1056 				*bpp = bp;
   1057 		}
   1058 		return 0;
   1059 	}
   1060 
   1061 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
   1062 	if (error)
   1063 		return (error);
   1064 
   1065 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
   1066 
   1067 	/*
   1068 	 * Do byte accounting all at once, so we can gracefully fail *before*
   1069 	 * we start assigning blocks.
   1070 	 */
   1071         frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */
   1072 	bcount = 0;
   1073 	if (daddr == UNASSIGNED) {
   1074 		bcount = frags;
   1075 	}
   1076 	for (i = 1; i < num; ++i) {
   1077 		if (!indirs[i].in_exists) {
   1078 			bcount += frags;
   1079 		}
   1080 	}
   1081 	fs->lfs_bfree -= bcount;
   1082 	ip->i_lfs_effnblks += bcount;
   1083 
   1084 	if (daddr == UNASSIGNED) {
   1085 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
   1086 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1087 		}
   1088 
   1089 		/*
   1090 		 * Create new indirect blocks if necessary
   1091 		 */
   1092 		if (num > 1) {
   1093 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
   1094 			for (i = 1; i < num; ++i) {
   1095 				ibp = getblk(vp, indirs[i].in_lbn,
   1096 				    fs->lfs_bsize);
   1097 				if (!indirs[i].in_exists) {
   1098 					memset(ibp->b_data, 0, ibp->b_bufsize);
   1099 					ibp->b_blkno = UNWRITTEN;
   1100 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
   1101 					ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
   1102 					ibp->b_flags |= B_READ;
   1103 					VOP_STRATEGY(ibp);
   1104 				}
   1105 				/*
   1106 				 * This block exists, but the next one may not.
   1107 				 * If that is the case mark it UNWRITTEN to
   1108                                  * keep the accounting straight.
   1109 				 */
   1110 				/* XXX ondisk32 */
   1111 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
   1112 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
   1113 						UNWRITTEN;
   1114 				/* XXX ondisk32 */
   1115 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
   1116 				if ((error = VOP_BWRITE(ibp)))
   1117 					return error;
   1118 			}
   1119 		}
   1120 	}
   1121 
   1122 
   1123 	/*
   1124 	 * Get the existing block from the cache, if requested.
   1125 	 */
   1126 	if (bpp)
   1127 		*bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn));
   1128 
   1129 	/*
   1130 	 * The block we are writing may be a brand new block
   1131 	 * in which case we need to do accounting.
   1132 	 *
   1133 	 * We can tell a truly new block because ulfs_bmaparray will say
   1134 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
   1135 	 * disk address UNWRITTEN.
   1136 	 */
   1137 	if (daddr == UNASSIGNED) {
   1138 		if (bpp) {
   1139 			/* Note the new address */
   1140 			bp->b_blkno = UNWRITTEN;
   1141 		}
   1142 
   1143 		switch (num) {
   1144 		    case 0:
   1145 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1146 			break;
   1147 		    case 1:
   1148 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1149 			break;
   1150 		    default:
   1151 			idp = &indirs[num - 1];
   1152 			if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
   1153 				  0, &ibp))
   1154 				panic("lfs_balloc: bread bno %lld",
   1155 				    (long long)idp->in_lbn);
   1156 			/* XXX ondisk32 */
   1157 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
   1158 			VOP_BWRITE(ibp);
   1159 		}
   1160 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
   1161 		/*
   1162 		 * Not a brand new block, also not in the cache;
   1163 		 * read it in from disk.
   1164 		 */
   1165 		if (iosize == fs->lfs_bsize)
   1166 			/* Optimization: I/O is unnecessary. */
   1167 			bp->b_blkno = daddr;
   1168 		else {
   1169 			/*
   1170 			 * We need to read the block to preserve the
   1171 			 * existing bytes.
   1172 			 */
   1173 			bp->b_blkno = daddr;
   1174 			bp->b_flags |= B_READ;
   1175 			VOP_STRATEGY(bp);
   1176 			return 0;
   1177 		}
   1178 	}
   1179 
   1180 	return (0);
   1181 }
   1182 
   1183 int
   1184 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
   1185                struct ubuf **bpp)
   1186 {
   1187 	struct inode *ip;
   1188 	struct lfs *fs;
   1189 	int frags;
   1190 	int error;
   1191 
   1192 	ip = VTOI(vp);
   1193 	fs = ip->i_lfs;
   1194 	frags = (long)lfs_numfrags(fs, nsize - osize);
   1195 	error = 0;
   1196 
   1197 	/*
   1198 	 * If we are not asked to actually return the block, all we need
   1199 	 * to do is allocate space for it.  UBC will handle dirtying the
   1200 	 * appropriate things and making sure it all goes to disk.
   1201 	 * Don't bother to read in that case.
   1202 	 */
   1203 	if (bpp && (error = bread(vp, lbn, osize, NOCRED, 0, bpp))) {
   1204 		brelse(*bpp, 0);
   1205 		goto out;
   1206 	}
   1207 
   1208 	fs->lfs_bfree -= frags;
   1209 	ip->i_lfs_effnblks += frags;
   1210 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1211 
   1212 	if (bpp) {
   1213 		(*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
   1214 		(void)memset((*bpp)->b_data + osize, 0, nsize - osize);
   1215 	}
   1216 
   1217     out:
   1218 	return (error);
   1219 }
   1220