Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.52
      1 /* $NetBSD: lfs.c,v 1.52 2015/08/02 18:18:09 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 struct ulfs1_dinode *
    314 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
    315 {
    316 	struct ulfs1_dinode *dip = (struct ulfs1_dinode *) bp->b_data;
    317 	struct ulfs1_dinode *ldip, *fin;
    318 
    319 	fin = dip + 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 (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, ulfs_daddr_t daddr)
    338 {
    339 	struct uvnode *vp;
    340 	struct inode *ip;
    341 	struct ulfs1_dinode *dip;
    342 	struct ubuf *bp;
    343 	int i, hash;
    344 
    345 	vp = ecalloc(1, sizeof(*vp));
    346 	vp->v_fd = fd;
    347 	vp->v_fs = fs;
    348 	vp->v_usecount = 0;
    349 	vp->v_strategy_op = lfs_vop_strategy;
    350 	vp->v_bwrite_op = lfs_vop_bwrite;
    351 	vp->v_bmap_op = lfs_vop_bmap;
    352 	LIST_INIT(&vp->v_cleanblkhd);
    353 	LIST_INIT(&vp->v_dirtyblkhd);
    354 
    355 	ip = ecalloc(1, sizeof(*ip));
    356 
    357 	ip->i_din.ffs1_din = ecalloc(1, sizeof(*ip->i_din.ffs1_din));
    358 
    359 	/* Initialize the inode -- from lfs_vcreate. */
    360 	ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs));
    361 	vp->v_data = ip;
    362 	/* ip->i_vnode = vp; */
    363 	ip->i_number = ino;
    364 	ip->i_lockf = 0;
    365 	ip->i_lfs_effnblks = 0;
    366 	ip->i_flag = 0;
    367 
    368 	/* Load inode block and find inode */
    369 	if (daddr > 0) {
    370 		bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
    371 		    0, &bp);
    372 		bp->b_flags |= B_AGE;
    373 		dip = lfs_ifind(fs, ino, bp);
    374 		if (dip == NULL) {
    375 			brelse(bp, 0);
    376 			free(ip);
    377 			free(vp);
    378 			return NULL;
    379 		}
    380 		memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
    381 		brelse(bp, 0);
    382 	}
    383 	ip->i_number = ino;
    384 	/* ip->i_devvp = fs->lfs_devvp; */
    385 	ip->i_lfs = fs;
    386 
    387 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
    388 	ip->i_lfs_osize = ip->i_ffs1_size;
    389 #if 0
    390 	if (fs->lfs_version > 1) {
    391 		ip->i_ffs1_atime = ts.tv_sec;
    392 		ip->i_ffs1_atimensec = ts.tv_nsec;
    393 	}
    394 #endif
    395 
    396 	memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
    397 	for (i = 0; i < ULFS_NDADDR; i++)
    398 		if (ip->i_ffs1_db[i] != 0)
    399 			ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
    400 
    401 	++nvnodes;
    402 	hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
    403 	LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
    404 	LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
    405 
    406 	return vp;
    407 }
    408 
    409 static struct uvnode *
    410 lfs_vget(void *vfs, ino_t ino)
    411 {
    412 	struct lfs *fs = (struct lfs *)vfs;
    413 	ulfs_daddr_t daddr;
    414 	struct ubuf *bp;
    415 	IFILE *ifp;
    416 
    417 	LFS_IENTRY(ifp, fs, ino, bp);
    418 	daddr = ifp->if_daddr;
    419 	brelse(bp, 0);
    420 	if (daddr <= 0 || lfs_dtosn(fs, daddr) >= lfs_sb_getnseg(fs))
    421 		return NULL;
    422 	return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
    423 }
    424 
    425 /* Check superblock magic number and checksum */
    426 static int
    427 check_sb(struct lfs *fs)
    428 {
    429 	u_int32_t checksum;
    430 	u_int32_t magic;
    431 
    432 	/* we can read the magic out of either the 32-bit or 64-bit dlfs */
    433 	magic = fs->lfs_dlfs_u.u_32.dlfs_magic;
    434 
    435 	if (magic != LFS_MAGIC) {
    436 		printf("Superblock magic number (0x%lx) does not match "
    437 		       "expected 0x%lx\n", (unsigned long) magic,
    438 		       (unsigned long) LFS_MAGIC);
    439 		return 1;
    440 	}
    441 	/* checksum */
    442 	checksum = lfs_sb_cksum(fs);
    443 	if (lfs_sb_getcksum(fs) != checksum) {
    444 		printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
    445 		    (unsigned long) lfs_sb_getcksum(fs), (unsigned long) checksum);
    446 		return 1;
    447 	}
    448 	return 0;
    449 }
    450 
    451 /* Initialize LFS library; load superblocks and choose which to use. */
    452 struct lfs *
    453 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
    454 {
    455 	struct uvnode *devvp;
    456 	struct ubuf *bp;
    457 	int tryalt;
    458 	struct lfs *fs, *altfs;
    459 
    460 	vfs_init();
    461 
    462 	devvp = ecalloc(1, sizeof(*devvp));
    463 	devvp->v_fs = NULL;
    464 	devvp->v_fd = devfd;
    465 	devvp->v_strategy_op = raw_vop_strategy;
    466 	devvp->v_bwrite_op = raw_vop_bwrite;
    467 	devvp->v_bmap_op = raw_vop_bmap;
    468 	LIST_INIT(&devvp->v_cleanblkhd);
    469 	LIST_INIT(&devvp->v_dirtyblkhd);
    470 
    471 	tryalt = 0;
    472 	if (dummy_read) {
    473 		if (sblkno == 0)
    474 			sblkno = LFS_LABELPAD / dev_bsize;
    475 		fs = ecalloc(1, sizeof(*fs));
    476 		fs->lfs_devvp = devvp;
    477 	} else {
    478 		if (sblkno == 0) {
    479 			sblkno = LFS_LABELPAD / dev_bsize;
    480 			tryalt = 1;
    481 		} else if (debug) {
    482 			printf("No -b flag given, not attempting to verify checkpoint\n");
    483 		}
    484 
    485 		dev_bsize = DEV_BSIZE;
    486 
    487 		(void)bread(devvp, sblkno, LFS_SBPAD, 0, &bp);
    488 		fs = ecalloc(1, sizeof(*fs));
    489 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
    490 		memcpy(&fs->lfs_dlfs_u, bp->b_data, sizeof(struct dlfs));
    491 		fs->lfs_devvp = devvp;
    492 		bp->b_flags |= B_INVAL;
    493 		brelse(bp, 0);
    494 
    495 		dev_bsize = lfs_sb_getfsize(fs) >> lfs_sb_getfsbtodb(fs);
    496 
    497 		if (tryalt) {
    498 			(void)bread(devvp, LFS_FSBTODB(fs, lfs_sb_getsboff(fs, 1)),
    499 		    	LFS_SBPAD, 0, &bp);
    500 			altfs = ecalloc(1, sizeof(*altfs));
    501 			memcpy(&altfs->lfs_dlfs_u, bp->b_data,
    502 			       sizeof(struct dlfs));
    503 			altfs->lfs_devvp = devvp;
    504 			bp->b_flags |= B_INVAL;
    505 			brelse(bp, 0);
    506 
    507 			if (check_sb(fs) || lfs_sb_getidaddr(fs) <= 0) {
    508 				if (debug)
    509 					printf("Primary superblock is no good, using first alternate\n");
    510 				free(fs);
    511 				fs = altfs;
    512 			} else {
    513 				/* If both superblocks check out, try verification */
    514 				if (check_sb(altfs)) {
    515 					if (debug)
    516 						printf("First alternate superblock is no good, using primary\n");
    517 					free(altfs);
    518 				} else {
    519 					if (lfs_verify(fs, altfs, devvp, debug) == fs) {
    520 						free(altfs);
    521 					} else {
    522 						free(fs);
    523 						fs = altfs;
    524 					}
    525 				}
    526 			}
    527 		}
    528 		if (check_sb(fs)) {
    529 			free(fs);
    530 			return NULL;
    531 		}
    532 	}
    533 
    534 	/* Compatibility */
    535 	if (lfs_sb_getversion(fs) < 2) {
    536 		lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
    537 		lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
    538 		lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
    539 		lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
    540 		lfs_sb_setfsbtodb(fs, 0);
    541 	}
    542 
    543 	if (!dummy_read) {
    544 		fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *));
    545 		fs->lfs_suflags[0] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    546 		fs->lfs_suflags[1] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
    547 	}
    548 
    549 	if (idaddr == 0)
    550 		idaddr = lfs_sb_getidaddr(fs);
    551 	else
    552 		lfs_sb_setidaddr(fs, idaddr);
    553 	/* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
    554 	fs->lfs_ivnode = lfs_raw_vget(fs,
    555 		(dummy_read ? LFS_IFILE_INUM : lfs_sb_getifile(fs)),
    556 		devvp->v_fd, idaddr);
    557 	if (fs->lfs_ivnode == NULL)
    558 		return NULL;
    559 
    560 	register_vget((void *)fs, lfs_vget);
    561 
    562 	return fs;
    563 }
    564 
    565 /*
    566  * Check partial segment validity between fs->lfs_offset and the given goal.
    567  *
    568  * If goal == 0, just keep on going until the segments stop making sense,
    569  * and return the address of the last valid partial segment.
    570  *
    571  * If goal != 0, return the address of the first partial segment that failed,
    572  * or "goal" if we reached it without failure (the partial segment *at* goal
    573  * need not be valid).
    574  */
    575 ulfs_daddr_t
    576 try_verify(struct lfs *osb, struct uvnode *devvp, ulfs_daddr_t goal, int debug)
    577 {
    578 	ulfs_daddr_t daddr, odaddr;
    579 	SEGSUM *sp;
    580 	int i, bc, hitclean;
    581 	struct ubuf *bp;
    582 	ulfs_daddr_t nodirop_daddr;
    583 	u_int64_t serial;
    584 
    585 	bc = 0;
    586 	hitclean = 0;
    587 	odaddr = -1;
    588 	daddr = lfs_sb_getoffset(osb);
    589 	nodirop_daddr = daddr;
    590 	serial = lfs_sb_getserial(osb);
    591 	while (daddr != goal) {
    592 		/*
    593 		 * Don't mistakenly read a superblock, if there is one here.
    594 		 */
    595 		if (lfs_sntod(osb, lfs_dtosn(osb, daddr)) == daddr) {
    596 			if (daddr == lfs_sb_gets0addr(osb))
    597 				daddr += lfs_btofsb(osb, LFS_LABELPAD);
    598 			for (i = 0; i < LFS_MAXNUMSB; i++) {
    599 				if (lfs_sb_getsboff(osb, i) < daddr)
    600 					break;
    601 				if (lfs_sb_getsboff(osb, i) == daddr)
    602 					daddr += lfs_btofsb(osb, LFS_SBPAD);
    603 			}
    604 		}
    605 
    606 		/* Read in summary block */
    607 		bread(devvp, LFS_FSBTODB(osb, daddr), lfs_sb_getsumsize(osb),
    608 		    0, &bp);
    609 		sp = (SEGSUM *)bp->b_data;
    610 
    611 		/*
    612 		 * Check for a valid segment summary belonging to our fs.
    613 		 */
    614 		if (sp->ss_magic != SS_MAGIC ||
    615 		    sp->ss_ident != lfs_sb_getident(osb) ||
    616 		    sp->ss_serial < serial ||	/* XXX strengthen this */
    617 		    sp->ss_sumsum != cksum(&sp->ss_datasum, lfs_sb_getsumsize(osb) -
    618 			sizeof(sp->ss_sumsum))) {
    619 			brelse(bp, 0);
    620 			if (debug) {
    621 				if (sp->ss_magic != SS_MAGIC)
    622 					pwarn("pseg at 0x%jx: "
    623 					      "wrong magic number\n",
    624 					      (uintmax_t)daddr);
    625 				else if (sp->ss_ident != lfs_sb_getident(osb))
    626 					pwarn("pseg at 0x%jx: "
    627 					      "expected ident %jx, got %jx\n",
    628 					      (uintmax_t)daddr,
    629 					      (uintmax_t)sp->ss_ident,
    630 					      (uintmax_t)lfs_sb_getident(osb));
    631 				else if (sp->ss_serial >= serial)
    632 					pwarn("pseg at 0x%jx: "
    633 					      "serial %d < %d\n",
    634 					      (uintmax_t)daddr,
    635 					      (int)sp->ss_serial, (int)serial);
    636 				else
    637 					pwarn("pseg at 0x%jx: "
    638 					      "summary checksum wrong\n",
    639 					      (uintmax_t)daddr);
    640 			}
    641 			break;
    642 		}
    643 		if (debug && sp->ss_serial != serial)
    644 			pwarn("warning, serial=%d ss_serial=%d\n",
    645 				(int)serial, (int)sp->ss_serial);
    646 		++serial;
    647 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    648 		if (bc == 0) {
    649 			brelse(bp, 0);
    650 			break;
    651 		}
    652 		if (debug)
    653 			pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr,
    654 			      (int)sp->ss_serial);
    655 		assert (bc > 0);
    656 		odaddr = daddr;
    657 		daddr += lfs_btofsb(osb, lfs_sb_getsumsize(osb) + bc);
    658 		if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) ||
    659 		    lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr +
    660 			lfs_btofsb(osb, lfs_sb_getsumsize(osb) + lfs_sb_getbsize(osb)) - 1)) {
    661 			daddr = sp->ss_next;
    662 		}
    663 
    664 		/*
    665 		 * Check for the beginning and ending of a sequence of
    666 		 * dirops.  Writes from the cleaner never involve new
    667 		 * information, and are always checkpoints; so don't try
    668 		 * to roll forward through them.  Likewise, psegs written
    669 		 * by a previous roll-forward attempt are not interesting.
    670 		 */
    671 		if (sp->ss_flags & (SS_CLEAN | SS_RFW))
    672 			hitclean = 1;
    673 		if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0)
    674 			nodirop_daddr = daddr;
    675 
    676 		brelse(bp, 0);
    677 	}
    678 
    679 	if (goal == 0)
    680 		return nodirop_daddr;
    681 	else
    682 		return daddr;
    683 }
    684 
    685 /* Use try_verify to check whether the newer superblock is valid. */
    686 struct lfs *
    687 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    688 {
    689 	ulfs_daddr_t daddr;
    690 	struct lfs *osb, *nsb;
    691 
    692 	/*
    693 	 * Verify the checkpoint of the newer superblock,
    694 	 * if the timestamp/serial number of the two superblocks is
    695 	 * different.
    696 	 */
    697 
    698 	osb = NULL;
    699 	if (debug)
    700 		pwarn("sb0 %ju, sb1 %ju",
    701 		      (uintmax_t) lfs_sb_getserial(sb0),
    702 		      (uintmax_t) lfs_sb_getserial(sb1));
    703 
    704 	if ((lfs_sb_getversion(sb0) == 1 &&
    705 		lfs_sb_getotstamp(sb0) != lfs_sb_getotstamp(sb1)) ||
    706 	    (lfs_sb_getversion(sb0) > 1 &&
    707 		lfs_sb_getserial(sb0) != lfs_sb_getserial(sb1))) {
    708 		if (lfs_sb_getversion(sb0) == 1) {
    709 			if (lfs_sb_getotstamp(sb0) > lfs_sb_getotstamp(sb1)) {
    710 				osb = sb1;
    711 				nsb = sb0;
    712 			} else {
    713 				osb = sb0;
    714 				nsb = sb1;
    715 			}
    716 		} else {
    717 			if (lfs_sb_getserial(sb0) > lfs_sb_getserial(sb1)) {
    718 				osb = sb1;
    719 				nsb = sb0;
    720 			} else {
    721 				osb = sb0;
    722 				nsb = sb1;
    723 			}
    724 		}
    725 		if (debug) {
    726 			printf("Attempting to verify newer checkpoint...");
    727 			fflush(stdout);
    728 		}
    729 		daddr = try_verify(osb, devvp, lfs_sb_getoffset(nsb), debug);
    730 
    731 		if (debug)
    732 			printf("done.\n");
    733 		if (daddr == lfs_sb_getoffset(nsb)) {
    734 			pwarn("** Newer checkpoint verified; recovered %jd seconds of data\n",
    735 			    (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    736 			sbdirty();
    737 		} else {
    738 			pwarn("** Newer checkpoint invalid; lost %jd seconds of data\n", (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    739 		}
    740 		return (daddr == lfs_sb_getoffset(nsb) ? nsb : osb);
    741 	}
    742 	/* Nothing to check */
    743 	return osb;
    744 }
    745 
    746 /* Verify a partial-segment summary; return the number of bytes on disk. */
    747 int
    748 check_summary(struct lfs *fs, SEGSUM *sp, ulfs_daddr_t pseg_addr, int debug,
    749 	      struct uvnode *devvp, void (func(ulfs_daddr_t, FINFO *)))
    750 {
    751 	FINFO *fp;
    752 	int bc;			/* Bytes in partial segment */
    753 	int nblocks;
    754 	ulfs_daddr_t daddr;
    755 	ulfs_daddr_t *dp, *idp;
    756 	struct ubuf *bp;
    757 	int i, j, k, datac, len;
    758 	u_int32_t *datap;
    759 	u_int32_t ccksum;
    760 
    761 	/* We've already checked the sumsum, just do the data bounds and sum */
    762 
    763 	/* Count the blocks. */
    764 	nblocks = howmany(sp->ss_ninos, LFS_INOPB(fs));
    765 	bc = nblocks << (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) : lfs_sb_getbshift(fs));
    766 	assert(bc >= 0);
    767 
    768 	fp = (FINFO *) (sp + 1);
    769 	for (i = 0; i < sp->ss_nfinfo; i++) {
    770 		nblocks += fp->fi_nblocks;
    771 		bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    772 					   << lfs_sb_getbshift(fs));
    773 		assert(bc >= 0);
    774 		fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    775 		if (((char *)fp) - (char *)sp > lfs_sb_getsumsize(fs))
    776 			return 0;
    777 	}
    778 	datap = emalloc(nblocks * sizeof(*datap));
    779 	datac = 0;
    780 
    781 	dp = (ulfs_daddr_t *) sp;
    782 	dp += lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t);
    783 	dp--;
    784 
    785 	idp = dp;
    786 	daddr = pseg_addr + lfs_btofsb(fs, lfs_sb_getsumsize(fs));
    787 	fp = (FINFO *) (sp + 1);
    788 	for (i = 0, j = 0;
    789 	     i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, LFS_INOPB(fs)); i++) {
    790 		if (i >= sp->ss_nfinfo && *idp != daddr) {
    791 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
    792 			      ": found %d, wanted %d\n",
    793 			      pseg_addr, j, howmany(sp->ss_ninos, LFS_INOPB(fs)));
    794 			if (debug)
    795 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    796 				      daddr);
    797 			break;
    798 		}
    799 		while (j < howmany(sp->ss_ninos, LFS_INOPB(fs)) && *idp == daddr) {
    800 			bread(devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
    801 			    0, &bp);
    802 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    803 			brelse(bp, 0);
    804 
    805 			++j;
    806 			daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs));
    807 			--idp;
    808 		}
    809 		if (i < sp->ss_nfinfo) {
    810 			if (func)
    811 				func(daddr, fp);
    812 			for (k = 0; k < fp->fi_nblocks; k++) {
    813 				len = (k == fp->fi_nblocks - 1 ?
    814 				       fp->fi_lastlength
    815 				       : lfs_sb_getbsize(fs));
    816 				bread(devvp, LFS_FSBTODB(fs, daddr), len,
    817 				    0, &bp);
    818 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    819 				brelse(bp, 0);
    820 				daddr += lfs_btofsb(fs, len);
    821 			}
    822 			fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
    823 		}
    824 	}
    825 
    826 	if (datac != nblocks) {
    827 		pwarn("Partial segment at 0x%jx expected %d blocks counted %d\n",
    828 		    (intmax_t)pseg_addr, nblocks, datac);
    829 	}
    830 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    831 	/* Check the data checksum */
    832 	if (ccksum != sp->ss_datasum) {
    833 		pwarn("Partial segment at 0x%jx data checksum"
    834 		      " mismatch: given 0x%x, computed 0x%x\n",
    835 		      (uintmax_t)pseg_addr, sp->ss_datasum, ccksum);
    836 		free(datap);
    837 		return 0;
    838 	}
    839 	free(datap);
    840 	assert(bc >= 0);
    841 	return bc;
    842 }
    843 
    844 /* print message and exit */
    845 void
    846 my_vpanic(int fatal, const char *fmt, va_list ap)
    847 {
    848         (void) vprintf(fmt, ap);
    849 	exit(8);
    850 }
    851 
    852 void
    853 call_panic(const char *fmt, ...)
    854 {
    855 	va_list ap;
    856 
    857 	va_start(ap, fmt);
    858         panic_func(1, fmt, ap);
    859 	va_end(ap);
    860 }
    861 
    862 /* Allocate a new inode. */
    863 struct uvnode *
    864 lfs_valloc(struct lfs *fs, ino_t ino)
    865 {
    866 	struct ubuf *bp, *cbp;
    867 	struct ifile *ifp;
    868 	ino_t new_ino;
    869 	int error;
    870 	CLEANERINFO *cip;
    871 
    872 	/* Get the head of the freelist. */
    873 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    874 
    875 	/*
    876 	 * Remove the inode from the free list and write the new start
    877 	 * of the free list into the superblock.
    878 	 */
    879 	LFS_IENTRY(ifp, fs, new_ino, bp);
    880 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    881 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    882 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    883 
    884 	brelse(bp, 0);
    885 
    886 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    887 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    888 		if ((error = extend_ifile(fs)) != 0) {
    889 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    890 			return NULL;
    891 		}
    892 	}
    893 
    894 	/* Set superblock modified bit and increment file count. */
    895         sbdirty();
    896 	lfs_sb_addnfiles(fs, 1);
    897 
    898         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
    899 }
    900 
    901 #ifdef IN_FSCK_LFS
    902 void reset_maxino(ino_t);
    903 #endif
    904 
    905 /*
    906  * Add a new block to the Ifile, to accommodate future file creations.
    907  */
    908 int
    909 extend_ifile(struct lfs *fs)
    910 {
    911 	struct uvnode *vp;
    912 	struct inode *ip;
    913 	IFILE *ifp;
    914 	IFILE_V1 *ifp_v1;
    915 	struct ubuf *bp, *cbp;
    916 	daddr_t i, blkno, max;
    917 	ino_t oldlast;
    918 	CLEANERINFO *cip;
    919 
    920 	vp = fs->lfs_ivnode;
    921 	ip = VTOI(vp);
    922 	blkno = lfs_lblkno(fs, ip->i_ffs1_size);
    923 
    924 	lfs_balloc(vp, ip->i_ffs1_size, lfs_sb_getbsize(fs), &bp);
    925 	ip->i_ffs1_size += lfs_sb_getbsize(fs);
    926 	ip->i_flag |= IN_MODIFIED;
    927 
    928 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    929 		lfs_sb_getifpb(fs);
    930 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    931 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    932 	max = i + lfs_sb_getifpb(fs);
    933 	lfs_sb_subbfree(fs, lfs_btofsb(fs, lfs_sb_getbsize(fs)));
    934 
    935 	if (lfs_sb_getversion(fs) == 1) {
    936 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    937 			ifp_v1->if_version = 1;
    938 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    939 			ifp_v1->if_nextfree = ++i;
    940 		}
    941 		ifp_v1--;
    942 		ifp_v1->if_nextfree = oldlast;
    943 	} else {
    944 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    945 			ifp->if_version = 1;
    946 			ifp->if_daddr = LFS_UNUSED_DADDR;
    947 			ifp->if_nextfree = ++i;
    948 		}
    949 		ifp--;
    950 		ifp->if_nextfree = oldlast;
    951 	}
    952 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    953 
    954 	LFS_BWRITE_LOG(bp);
    955 
    956 #ifdef IN_FSCK_LFS
    957 	reset_maxino(((ip->i_ffs1_size >> lfs_sb_getbshift(fs))
    958 		      - lfs_sb_getsegtabsz(fs)
    959 		      - lfs_sb_getcleansz(fs)) * lfs_sb_getifpb(fs));
    960 #endif
    961 	return 0;
    962 }
    963 
    964 /*
    965  * Allocate a block, and to inode and filesystem block accounting for it
    966  * and for any indirect blocks the may need to be created in order for
    967  * this block to be created.
    968  *
    969  * Blocks which have never been accounted for (i.e., which "do not exist")
    970  * have disk address 0, which is translated by ulfs_bmap to the special value
    971  * UNASSIGNED == -1, as in the historical ULFS.
    972  *
    973  * Blocks which have been accounted for but which have not yet been written
    974  * to disk are given the new special disk address UNWRITTEN == -2, so that
    975  * they can be differentiated from completely new blocks.
    976  */
    977 int
    978 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
    979 {
    980 	int offset;
    981 	daddr_t daddr, idaddr;
    982 	struct ubuf *ibp, *bp;
    983 	struct inode *ip;
    984 	struct lfs *fs;
    985 	struct indir indirs[ULFS_NIADDR+2], *idp;
    986 	daddr_t	lbn, lastblock;
    987 	int bcount;
    988 	int error, frags, i, nsize, osize, num;
    989 
    990 	ip = VTOI(vp);
    991 	fs = ip->i_lfs;
    992 	offset = lfs_blkoff(fs, startoffset);
    993 	lbn = lfs_lblkno(fs, startoffset);
    994 
    995 	/*
    996 	 * Three cases: it's a block beyond the end of file, it's a block in
    997 	 * the file that may or may not have been assigned a disk address or
    998 	 * we're writing an entire block.
    999 	 *
   1000 	 * Note, if the daddr is UNWRITTEN, the block already exists in
   1001 	 * the cache (it was read or written earlier).	If so, make sure
   1002 	 * we don't count it as a new block or zero out its contents. If
   1003 	 * it did not, make sure we allocate any necessary indirect
   1004 	 * blocks.
   1005 	 *
   1006 	 * If we are writing a block beyond the end of the file, we need to
   1007 	 * check if the old last block was a fragment.	If it was, we need
   1008 	 * to rewrite it.
   1009 	 */
   1010 
   1011 	if (bpp)
   1012 		*bpp = NULL;
   1013 
   1014 	/* Check for block beyond end of file and fragment extension needed. */
   1015 	lastblock = lfs_lblkno(fs, ip->i_ffs1_size);
   1016 	if (lastblock < ULFS_NDADDR && lastblock < lbn) {
   1017 		osize = lfs_blksize(fs, ip, lastblock);
   1018 		if (osize < lfs_sb_getbsize(fs) && osize > 0) {
   1019 			if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
   1020 						    lastblock,
   1021 						    (bpp ? &bp : NULL))))
   1022 				return (error);
   1023 			ip->i_ffs1_size = (lastblock + 1) * lfs_sb_getbsize(fs);
   1024 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1025 			if (bpp)
   1026 				(void) VOP_BWRITE(bp);
   1027 		}
   1028 	}
   1029 
   1030 	/*
   1031 	 * If the block we are writing is a direct block, it's the last
   1032 	 * block in the file, and offset + iosize is less than a full
   1033 	 * block, we can write one or more fragments.  There are two cases:
   1034 	 * the block is brand new and we should allocate it the correct
   1035 	 * size or it already exists and contains some fragments and
   1036 	 * may need to extend it.
   1037 	 */
   1038 	if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_ffs1_size) <= lbn) {
   1039 		osize = lfs_blksize(fs, ip, lbn);
   1040 		nsize = lfs_fragroundup(fs, offset + iosize);
   1041 		if (lfs_lblktosize(fs, lbn) >= ip->i_ffs1_size) {
   1042 			/* Brand new block or fragment */
   1043 			frags = lfs_numfrags(fs, nsize);
   1044 			if (bpp) {
   1045 				*bpp = bp = getblk(vp, lbn, nsize);
   1046 				bp->b_blkno = UNWRITTEN;
   1047 			}
   1048 			ip->i_lfs_effnblks += frags;
   1049 			lfs_sb_subbfree(fs, frags);
   1050 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1051 		} else {
   1052 			if (nsize <= osize) {
   1053 				/* No need to extend */
   1054 				if (bpp && (error = bread(vp, lbn, osize,
   1055 				    0, &bp)))
   1056 					return error;
   1057 			} else {
   1058 				/* Extend existing block */
   1059 				if ((error =
   1060 				     lfs_fragextend(vp, osize, nsize, lbn,
   1061 						    (bpp ? &bp : NULL))))
   1062 					return error;
   1063 			}
   1064 			if (bpp)
   1065 				*bpp = bp;
   1066 		}
   1067 		return 0;
   1068 	}
   1069 
   1070 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
   1071 	if (error)
   1072 		return (error);
   1073 
   1074 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
   1075 
   1076 	/*
   1077 	 * Do byte accounting all at once, so we can gracefully fail *before*
   1078 	 * we start assigning blocks.
   1079 	 */
   1080         frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */
   1081 	bcount = 0;
   1082 	if (daddr == UNASSIGNED) {
   1083 		bcount = frags;
   1084 	}
   1085 	for (i = 1; i < num; ++i) {
   1086 		if (!indirs[i].in_exists) {
   1087 			bcount += frags;
   1088 		}
   1089 	}
   1090 	lfs_sb_subbfree(fs, bcount);
   1091 	ip->i_lfs_effnblks += bcount;
   1092 
   1093 	if (daddr == UNASSIGNED) {
   1094 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
   1095 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1096 		}
   1097 
   1098 		/*
   1099 		 * Create new indirect blocks if necessary
   1100 		 */
   1101 		if (num > 1) {
   1102 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
   1103 			for (i = 1; i < num; ++i) {
   1104 				ibp = getblk(vp, indirs[i].in_lbn,
   1105 				    lfs_sb_getbsize(fs));
   1106 				if (!indirs[i].in_exists) {
   1107 					memset(ibp->b_data, 0, ibp->b_bufsize);
   1108 					ibp->b_blkno = UNWRITTEN;
   1109 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
   1110 					ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
   1111 					ibp->b_flags |= B_READ;
   1112 					VOP_STRATEGY(ibp);
   1113 				}
   1114 				/*
   1115 				 * This block exists, but the next one may not.
   1116 				 * If that is the case mark it UNWRITTEN to
   1117                                  * keep the accounting straight.
   1118 				 */
   1119 				/* XXX ondisk32 */
   1120 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
   1121 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
   1122 						UNWRITTEN;
   1123 				/* XXX ondisk32 */
   1124 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
   1125 				if ((error = VOP_BWRITE(ibp)))
   1126 					return error;
   1127 			}
   1128 		}
   1129 	}
   1130 
   1131 
   1132 	/*
   1133 	 * Get the existing block from the cache, if requested.
   1134 	 */
   1135 	if (bpp)
   1136 		*bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn));
   1137 
   1138 	/*
   1139 	 * The block we are writing may be a brand new block
   1140 	 * in which case we need to do accounting.
   1141 	 *
   1142 	 * We can tell a truly new block because ulfs_bmaparray will say
   1143 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
   1144 	 * disk address UNWRITTEN.
   1145 	 */
   1146 	if (daddr == UNASSIGNED) {
   1147 		if (bpp) {
   1148 			/* Note the new address */
   1149 			bp->b_blkno = UNWRITTEN;
   1150 		}
   1151 
   1152 		switch (num) {
   1153 		    case 0:
   1154 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1155 			break;
   1156 		    case 1:
   1157 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1158 			break;
   1159 		    default:
   1160 			idp = &indirs[num - 1];
   1161 			if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs), 0, &ibp))
   1162 				panic("lfs_balloc: bread bno %lld",
   1163 				    (long long)idp->in_lbn);
   1164 			/* XXX ondisk32 */
   1165 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
   1166 			VOP_BWRITE(ibp);
   1167 		}
   1168 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
   1169 		/*
   1170 		 * Not a brand new block, also not in the cache;
   1171 		 * read it in from disk.
   1172 		 */
   1173 		if (iosize == lfs_sb_getbsize(fs))
   1174 			/* Optimization: I/O is unnecessary. */
   1175 			bp->b_blkno = daddr;
   1176 		else {
   1177 			/*
   1178 			 * We need to read the block to preserve the
   1179 			 * existing bytes.
   1180 			 */
   1181 			bp->b_blkno = daddr;
   1182 			bp->b_flags |= B_READ;
   1183 			VOP_STRATEGY(bp);
   1184 			return 0;
   1185 		}
   1186 	}
   1187 
   1188 	return (0);
   1189 }
   1190 
   1191 int
   1192 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
   1193                struct ubuf **bpp)
   1194 {
   1195 	struct inode *ip;
   1196 	struct lfs *fs;
   1197 	int frags;
   1198 	int error;
   1199 
   1200 	ip = VTOI(vp);
   1201 	fs = ip->i_lfs;
   1202 	frags = (long)lfs_numfrags(fs, nsize - osize);
   1203 	error = 0;
   1204 
   1205 	/*
   1206 	 * If we are not asked to actually return the block, all we need
   1207 	 * to do is allocate space for it.  UBC will handle dirtying the
   1208 	 * appropriate things and making sure it all goes to disk.
   1209 	 * Don't bother to read in that case.
   1210 	 */
   1211 	if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
   1212 		brelse(*bpp, 0);
   1213 		goto out;
   1214 	}
   1215 
   1216 	lfs_sb_subbfree(fs, frags);
   1217 	ip->i_lfs_effnblks += frags;
   1218 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1219 
   1220 	if (bpp) {
   1221 		(*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
   1222 		(void)memset((*bpp)->b_data + osize, 0, nsize - osize);
   1223 	}
   1224 
   1225     out:
   1226 	return (error);
   1227 }
   1228