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