Home | History | Annotate | Line # | Download | only in fsck_lfs
lfs.c revision 1.55
      1 /* $NetBSD: lfs.c,v 1.55 2015/08/12 18:27:01 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 = lfs_if_getdaddr(fs, ifp);
    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 (lfs_ss_getmagic(osb, sp) != SS_MAGIC ||
    615 		    lfs_ss_getident(osb, sp) != lfs_sb_getident(osb) ||
    616 		    lfs_ss_getserial(osb, sp) < serial ||	/* XXX strengthen this */
    617 		    lfs_ss_getsumsum(osb, sp) !=
    618 		            cksum((char *)sp + lfs_ss_getsumstart(osb),
    619 				  lfs_sb_getsumsize(osb) - lfs_ss_getsumstart(osb))) {
    620 			brelse(bp, 0);
    621 			if (debug) {
    622 				if (lfs_ss_getmagic(osb, sp) != SS_MAGIC)
    623 					pwarn("pseg at 0x%jx: "
    624 					      "wrong magic number\n",
    625 					      (uintmax_t)daddr);
    626 				else if (lfs_ss_getident(osb, sp) != lfs_sb_getident(osb))
    627 					pwarn("pseg at 0x%jx: "
    628 					      "expected ident %jx, got %jx\n",
    629 					      (uintmax_t)daddr,
    630 					      (uintmax_t)lfs_ss_getident(osb, sp),
    631 					      (uintmax_t)lfs_sb_getident(osb));
    632 				else if (lfs_ss_getserial(osb, sp) >= serial)
    633 					pwarn("pseg at 0x%jx: "
    634 					      "serial %d < %d\n",
    635 					      (uintmax_t)daddr,
    636 					      (int)lfs_ss_getserial(osb, sp), (int)serial);
    637 				else
    638 					pwarn("pseg at 0x%jx: "
    639 					      "summary checksum wrong\n",
    640 					      (uintmax_t)daddr);
    641 			}
    642 			break;
    643 		}
    644 		if (debug && lfs_ss_getserial(osb, sp) != serial)
    645 			pwarn("warning, serial=%d ss_serial=%d\n",
    646 				(int)serial, (int)lfs_ss_getserial(osb, sp));
    647 		++serial;
    648 		bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
    649 		if (bc == 0) {
    650 			brelse(bp, 0);
    651 			break;
    652 		}
    653 		if (debug)
    654 			pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr,
    655 			      (int)lfs_ss_getserial(osb, sp));
    656 		assert (bc > 0);
    657 		odaddr = daddr;
    658 		daddr += lfs_btofsb(osb, lfs_sb_getsumsize(osb) + bc);
    659 		if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) ||
    660 		    lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr +
    661 			lfs_btofsb(osb, lfs_sb_getsumsize(osb) + lfs_sb_getbsize(osb)) - 1)) {
    662 			daddr = lfs_ss_getnext(osb, sp);
    663 		}
    664 
    665 		/*
    666 		 * Check for the beginning and ending of a sequence of
    667 		 * dirops.  Writes from the cleaner never involve new
    668 		 * information, and are always checkpoints; so don't try
    669 		 * to roll forward through them.  Likewise, psegs written
    670 		 * by a previous roll-forward attempt are not interesting.
    671 		 */
    672 		if (lfs_ss_getflags(osb, sp) & (SS_CLEAN | SS_RFW))
    673 			hitclean = 1;
    674 		if (hitclean == 0 && (lfs_ss_getflags(osb, sp) & SS_CONT) == 0)
    675 			nodirop_daddr = daddr;
    676 
    677 		brelse(bp, 0);
    678 	}
    679 
    680 	if (goal == 0)
    681 		return nodirop_daddr;
    682 	else
    683 		return daddr;
    684 }
    685 
    686 /* Use try_verify to check whether the newer superblock is valid. */
    687 struct lfs *
    688 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
    689 {
    690 	ulfs_daddr_t daddr;
    691 	struct lfs *osb, *nsb;
    692 
    693 	/*
    694 	 * Verify the checkpoint of the newer superblock,
    695 	 * if the timestamp/serial number of the two superblocks is
    696 	 * different.
    697 	 */
    698 
    699 	osb = NULL;
    700 	if (debug)
    701 		pwarn("sb0 %ju, sb1 %ju",
    702 		      (uintmax_t) lfs_sb_getserial(sb0),
    703 		      (uintmax_t) lfs_sb_getserial(sb1));
    704 
    705 	if ((lfs_sb_getversion(sb0) == 1 &&
    706 		lfs_sb_getotstamp(sb0) != lfs_sb_getotstamp(sb1)) ||
    707 	    (lfs_sb_getversion(sb0) > 1 &&
    708 		lfs_sb_getserial(sb0) != lfs_sb_getserial(sb1))) {
    709 		if (lfs_sb_getversion(sb0) == 1) {
    710 			if (lfs_sb_getotstamp(sb0) > lfs_sb_getotstamp(sb1)) {
    711 				osb = sb1;
    712 				nsb = sb0;
    713 			} else {
    714 				osb = sb0;
    715 				nsb = sb1;
    716 			}
    717 		} else {
    718 			if (lfs_sb_getserial(sb0) > lfs_sb_getserial(sb1)) {
    719 				osb = sb1;
    720 				nsb = sb0;
    721 			} else {
    722 				osb = sb0;
    723 				nsb = sb1;
    724 			}
    725 		}
    726 		if (debug) {
    727 			printf("Attempting to verify newer checkpoint...");
    728 			fflush(stdout);
    729 		}
    730 		daddr = try_verify(osb, devvp, lfs_sb_getoffset(nsb), debug);
    731 
    732 		if (debug)
    733 			printf("done.\n");
    734 		if (daddr == lfs_sb_getoffset(nsb)) {
    735 			pwarn("** Newer checkpoint verified; recovered %jd seconds of data\n",
    736 			    (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    737 			sbdirty();
    738 		} else {
    739 			pwarn("** Newer checkpoint invalid; lost %jd seconds of data\n", (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
    740 		}
    741 		return (daddr == lfs_sb_getoffset(nsb) ? nsb : osb);
    742 	}
    743 	/* Nothing to check */
    744 	return osb;
    745 }
    746 
    747 /* Verify a partial-segment summary; return the number of bytes on disk. */
    748 int
    749 check_summary(struct lfs *fs, SEGSUM *sp, ulfs_daddr_t pseg_addr, int debug,
    750 	      struct uvnode *devvp, void (func(ulfs_daddr_t, FINFO *)))
    751 {
    752 	FINFO *fp;
    753 	int bc;			/* Bytes in partial segment */
    754 	int nblocks;
    755 	ulfs_daddr_t daddr;
    756 	ulfs_daddr_t *dp, *idp;
    757 	struct ubuf *bp;
    758 	int i, j, k, datac, len;
    759 	u_int32_t *datap;
    760 	u_int32_t ccksum;
    761 
    762 	/* We've already checked the sumsum, just do the data bounds and sum */
    763 
    764 	/* Count the blocks. */
    765 	nblocks = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs));
    766 	bc = nblocks << (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) : lfs_sb_getbshift(fs));
    767 	assert(bc >= 0);
    768 
    769 	fp = SEGSUM_FINFOBASE(fs, sp);
    770 	for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
    771 		nblocks += lfs_fi_getnblocks(fs, fp);
    772 		bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
    773 					   << lfs_sb_getbshift(fs));
    774 		assert(bc >= 0);
    775 		fp = NEXT_FINFO(fs, fp);
    776 		if (((char *)fp) - (char *)sp > lfs_sb_getsumsize(fs))
    777 			return 0;
    778 	}
    779 	datap = emalloc(nblocks * sizeof(*datap));
    780 	datac = 0;
    781 
    782 	dp = (ulfs_daddr_t *) sp;
    783 	dp += lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t);
    784 	dp--;
    785 
    786 	idp = dp;
    787 	daddr = pseg_addr + lfs_btofsb(fs, lfs_sb_getsumsize(fs));
    788 	fp = (FINFO *) (sp + 1);
    789 	for (i = 0, j = 0;
    790 	     i < lfs_ss_getnfinfo(fs, sp) || j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)); i++) {
    791 		if (i >= lfs_ss_getnfinfo(fs, sp) && *idp != daddr) {
    792 			pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
    793 			      ": found %d, wanted %d\n",
    794 			      pseg_addr, j, howmany(lfs_ss_getninos(fs, sp),
    795 						    LFS_INOPB(fs)));
    796 			if (debug)
    797 				pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
    798 				      daddr);
    799 			break;
    800 		}
    801 		while (j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) && *idp == daddr) {
    802 			bread(devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
    803 			    0, &bp);
    804 			datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    805 			brelse(bp, 0);
    806 
    807 			++j;
    808 			daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs));
    809 			--idp;
    810 		}
    811 		if (i < lfs_ss_getnfinfo(fs, sp)) {
    812 			if (func)
    813 				func(daddr, fp);
    814 			for (k = 0; k < lfs_fi_getnblocks(fs, fp); k++) {
    815 				len = (k == lfs_fi_getnblocks(fs, fp) - 1 ?
    816 				       lfs_fi_getlastlength(fs, fp)
    817 				       : lfs_sb_getbsize(fs));
    818 				bread(devvp, LFS_FSBTODB(fs, daddr), len,
    819 				    0, &bp);
    820 				datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
    821 				brelse(bp, 0);
    822 				daddr += lfs_btofsb(fs, len);
    823 			}
    824 			fp = NEXT_FINFO(fs, fp);
    825 		}
    826 	}
    827 
    828 	if (datac != nblocks) {
    829 		pwarn("Partial segment at 0x%jx expected %d blocks counted %d\n",
    830 		    (intmax_t)pseg_addr, nblocks, datac);
    831 	}
    832 	/* XXX ondisk32 */
    833 	ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
    834 	/* Check the data checksum */
    835 	if (ccksum != lfs_ss_getdatasum(fs, sp)) {
    836 		pwarn("Partial segment at 0x%jx data checksum"
    837 		      " mismatch: given 0x%x, computed 0x%x\n",
    838 		      (uintmax_t)pseg_addr, lfs_ss_getdatasum(fs, sp), ccksum);
    839 		free(datap);
    840 		return 0;
    841 	}
    842 	free(datap);
    843 	assert(bc >= 0);
    844 	return bc;
    845 }
    846 
    847 /* print message and exit */
    848 void
    849 my_vpanic(int fatal, const char *fmt, va_list ap)
    850 {
    851         (void) vprintf(fmt, ap);
    852 	exit(8);
    853 }
    854 
    855 void
    856 call_panic(const char *fmt, ...)
    857 {
    858 	va_list ap;
    859 
    860 	va_start(ap, fmt);
    861         panic_func(1, fmt, ap);
    862 	va_end(ap);
    863 }
    864 
    865 /* Allocate a new inode. */
    866 struct uvnode *
    867 lfs_valloc(struct lfs *fs, ino_t ino)
    868 {
    869 	struct ubuf *bp, *cbp;
    870 	IFILE *ifp;
    871 	ino_t new_ino;
    872 	int error;
    873 	CLEANERINFO *cip;
    874 
    875 	/* Get the head of the freelist. */
    876 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    877 
    878 	/*
    879 	 * Remove the inode from the free list and write the new start
    880 	 * of the free list into the superblock.
    881 	 */
    882 	LFS_IENTRY(ifp, fs, new_ino, bp);
    883 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
    884 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    885 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
    886 
    887 	brelse(bp, 0);
    888 
    889 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    890 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    891 		if ((error = extend_ifile(fs)) != 0) {
    892 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    893 			return NULL;
    894 		}
    895 	}
    896 
    897 	/* Set superblock modified bit and increment file count. */
    898         sbdirty();
    899 	lfs_sb_addnfiles(fs, 1);
    900 
    901         return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
    902 }
    903 
    904 #ifdef IN_FSCK_LFS
    905 void reset_maxino(ino_t);
    906 #endif
    907 
    908 /*
    909  * Add a new block to the Ifile, to accommodate future file creations.
    910  */
    911 int
    912 extend_ifile(struct lfs *fs)
    913 {
    914 	struct uvnode *vp;
    915 	struct inode *ip;
    916 	IFILE64 *ifp64;
    917 	IFILE32 *ifp32;
    918 	IFILE_V1 *ifp_v1;
    919 	struct ubuf *bp, *cbp;
    920 	daddr_t i, blkno, max;
    921 	ino_t oldlast;
    922 	CLEANERINFO *cip;
    923 
    924 	vp = fs->lfs_ivnode;
    925 	ip = VTOI(vp);
    926 	blkno = lfs_lblkno(fs, ip->i_ffs1_size);
    927 
    928 	lfs_balloc(vp, ip->i_ffs1_size, lfs_sb_getbsize(fs), &bp);
    929 	ip->i_ffs1_size += lfs_sb_getbsize(fs);
    930 	ip->i_flag |= IN_MODIFIED;
    931 
    932 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    933 		lfs_sb_getifpb(fs);
    934 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    935 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    936 	max = i + lfs_sb_getifpb(fs);
    937 	lfs_sb_subbfree(fs, lfs_btofsb(fs, lfs_sb_getbsize(fs)));
    938 
    939 	if (fs->lfs_is64) {
    940 		for (ifp64 = (IFILE64 *)bp->b_data; i < max; ++ifp64) {
    941 			ifp64->if_version = 1;
    942 			ifp64->if_daddr = LFS_UNUSED_DADDR;
    943 			ifp64->if_nextfree = ++i;
    944 		}
    945 		ifp64--;
    946 		ifp64->if_nextfree = oldlast;
    947 	} else if (lfs_sb_getversion(fs) > 1) {
    948 		for (ifp32 = (IFILE32 *)bp->b_data; i < max; ++ifp32) {
    949 			ifp32->if_version = 1;
    950 			ifp32->if_daddr = LFS_UNUSED_DADDR;
    951 			ifp32->if_nextfree = ++i;
    952 		}
    953 		ifp32--;
    954 		ifp32->if_nextfree = oldlast;
    955 	} else {
    956 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    957 			ifp_v1->if_version = 1;
    958 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    959 			ifp_v1->if_nextfree = ++i;
    960 		}
    961 		ifp_v1--;
    962 		ifp_v1->if_nextfree = oldlast;
    963 	}
    964 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    965 
    966 	LFS_BWRITE_LOG(bp);
    967 
    968 #ifdef IN_FSCK_LFS
    969 	reset_maxino(((ip->i_ffs1_size >> lfs_sb_getbshift(fs))
    970 		      - lfs_sb_getsegtabsz(fs)
    971 		      - lfs_sb_getcleansz(fs)) * lfs_sb_getifpb(fs));
    972 #endif
    973 	return 0;
    974 }
    975 
    976 /*
    977  * Allocate a block, and to inode and filesystem block accounting for it
    978  * and for any indirect blocks the may need to be created in order for
    979  * this block to be created.
    980  *
    981  * Blocks which have never been accounted for (i.e., which "do not exist")
    982  * have disk address 0, which is translated by ulfs_bmap to the special value
    983  * UNASSIGNED == -1, as in the historical ULFS.
    984  *
    985  * Blocks which have been accounted for but which have not yet been written
    986  * to disk are given the new special disk address UNWRITTEN == -2, so that
    987  * they can be differentiated from completely new blocks.
    988  */
    989 int
    990 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
    991 {
    992 	int offset;
    993 	daddr_t daddr, idaddr;
    994 	struct ubuf *ibp, *bp;
    995 	struct inode *ip;
    996 	struct lfs *fs;
    997 	struct indir indirs[ULFS_NIADDR+2], *idp;
    998 	daddr_t	lbn, lastblock;
    999 	int bcount;
   1000 	int error, frags, i, nsize, osize, num;
   1001 
   1002 	ip = VTOI(vp);
   1003 	fs = ip->i_lfs;
   1004 	offset = lfs_blkoff(fs, startoffset);
   1005 	lbn = lfs_lblkno(fs, startoffset);
   1006 
   1007 	/*
   1008 	 * Three cases: it's a block beyond the end of file, it's a block in
   1009 	 * the file that may or may not have been assigned a disk address or
   1010 	 * we're writing an entire block.
   1011 	 *
   1012 	 * Note, if the daddr is UNWRITTEN, the block already exists in
   1013 	 * the cache (it was read or written earlier).	If so, make sure
   1014 	 * we don't count it as a new block or zero out its contents. If
   1015 	 * it did not, make sure we allocate any necessary indirect
   1016 	 * blocks.
   1017 	 *
   1018 	 * If we are writing a block beyond the end of the file, we need to
   1019 	 * check if the old last block was a fragment.	If it was, we need
   1020 	 * to rewrite it.
   1021 	 */
   1022 
   1023 	if (bpp)
   1024 		*bpp = NULL;
   1025 
   1026 	/* Check for block beyond end of file and fragment extension needed. */
   1027 	lastblock = lfs_lblkno(fs, ip->i_ffs1_size);
   1028 	if (lastblock < ULFS_NDADDR && lastblock < lbn) {
   1029 		osize = lfs_blksize(fs, ip, lastblock);
   1030 		if (osize < lfs_sb_getbsize(fs) && osize > 0) {
   1031 			if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
   1032 						    lastblock,
   1033 						    (bpp ? &bp : NULL))))
   1034 				return (error);
   1035 			ip->i_ffs1_size = (lastblock + 1) * lfs_sb_getbsize(fs);
   1036 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1037 			if (bpp)
   1038 				(void) VOP_BWRITE(bp);
   1039 		}
   1040 	}
   1041 
   1042 	/*
   1043 	 * If the block we are writing is a direct block, it's the last
   1044 	 * block in the file, and offset + iosize is less than a full
   1045 	 * block, we can write one or more fragments.  There are two cases:
   1046 	 * the block is brand new and we should allocate it the correct
   1047 	 * size or it already exists and contains some fragments and
   1048 	 * may need to extend it.
   1049 	 */
   1050 	if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_ffs1_size) <= lbn) {
   1051 		osize = lfs_blksize(fs, ip, lbn);
   1052 		nsize = lfs_fragroundup(fs, offset + iosize);
   1053 		if (lfs_lblktosize(fs, lbn) >= ip->i_ffs1_size) {
   1054 			/* Brand new block or fragment */
   1055 			frags = lfs_numfrags(fs, nsize);
   1056 			if (bpp) {
   1057 				*bpp = bp = getblk(vp, lbn, nsize);
   1058 				bp->b_blkno = UNWRITTEN;
   1059 			}
   1060 			ip->i_lfs_effnblks += frags;
   1061 			lfs_sb_subbfree(fs, frags);
   1062 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1063 		} else {
   1064 			if (nsize <= osize) {
   1065 				/* No need to extend */
   1066 				if (bpp && (error = bread(vp, lbn, osize,
   1067 				    0, &bp)))
   1068 					return error;
   1069 			} else {
   1070 				/* Extend existing block */
   1071 				if ((error =
   1072 				     lfs_fragextend(vp, osize, nsize, lbn,
   1073 						    (bpp ? &bp : NULL))))
   1074 					return error;
   1075 			}
   1076 			if (bpp)
   1077 				*bpp = bp;
   1078 		}
   1079 		return 0;
   1080 	}
   1081 
   1082 	error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
   1083 	if (error)
   1084 		return (error);
   1085 
   1086 	daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
   1087 
   1088 	/*
   1089 	 * Do byte accounting all at once, so we can gracefully fail *before*
   1090 	 * we start assigning blocks.
   1091 	 */
   1092         frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */
   1093 	bcount = 0;
   1094 	if (daddr == UNASSIGNED) {
   1095 		bcount = frags;
   1096 	}
   1097 	for (i = 1; i < num; ++i) {
   1098 		if (!indirs[i].in_exists) {
   1099 			bcount += frags;
   1100 		}
   1101 	}
   1102 	lfs_sb_subbfree(fs, bcount);
   1103 	ip->i_lfs_effnblks += bcount;
   1104 
   1105 	if (daddr == UNASSIGNED) {
   1106 		if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
   1107 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1108 		}
   1109 
   1110 		/*
   1111 		 * Create new indirect blocks if necessary
   1112 		 */
   1113 		if (num > 1) {
   1114 			idaddr = ip->i_ffs1_ib[indirs[0].in_off];
   1115 			for (i = 1; i < num; ++i) {
   1116 				ibp = getblk(vp, indirs[i].in_lbn,
   1117 				    lfs_sb_getbsize(fs));
   1118 				if (!indirs[i].in_exists) {
   1119 					memset(ibp->b_data, 0, ibp->b_bufsize);
   1120 					ibp->b_blkno = UNWRITTEN;
   1121 				} else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
   1122 					ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
   1123 					ibp->b_flags |= B_READ;
   1124 					VOP_STRATEGY(ibp);
   1125 				}
   1126 				/*
   1127 				 * This block exists, but the next one may not.
   1128 				 * If that is the case mark it UNWRITTEN to
   1129                                  * keep the accounting straight.
   1130 				 */
   1131 				/* XXX ondisk32 */
   1132 				if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
   1133 					((int32_t *)ibp->b_data)[indirs[i].in_off] =
   1134 						UNWRITTEN;
   1135 				/* XXX ondisk32 */
   1136 				idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
   1137 				if ((error = VOP_BWRITE(ibp)))
   1138 					return error;
   1139 			}
   1140 		}
   1141 	}
   1142 
   1143 
   1144 	/*
   1145 	 * Get the existing block from the cache, if requested.
   1146 	 */
   1147 	if (bpp)
   1148 		*bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn));
   1149 
   1150 	/*
   1151 	 * The block we are writing may be a brand new block
   1152 	 * in which case we need to do accounting.
   1153 	 *
   1154 	 * We can tell a truly new block because ulfs_bmaparray will say
   1155 	 * it is UNASSIGNED.  Once we allocate it we will assign it the
   1156 	 * disk address UNWRITTEN.
   1157 	 */
   1158 	if (daddr == UNASSIGNED) {
   1159 		if (bpp) {
   1160 			/* Note the new address */
   1161 			bp->b_blkno = UNWRITTEN;
   1162 		}
   1163 
   1164 		switch (num) {
   1165 		    case 0:
   1166 			ip->i_ffs1_db[lbn] = UNWRITTEN;
   1167 			break;
   1168 		    case 1:
   1169 			ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
   1170 			break;
   1171 		    default:
   1172 			idp = &indirs[num - 1];
   1173 			if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs), 0, &ibp))
   1174 				panic("lfs_balloc: bread bno %lld",
   1175 				    (long long)idp->in_lbn);
   1176 			/* XXX ondisk32 */
   1177 			((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
   1178 			VOP_BWRITE(ibp);
   1179 		}
   1180 	} else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
   1181 		/*
   1182 		 * Not a brand new block, also not in the cache;
   1183 		 * read it in from disk.
   1184 		 */
   1185 		if (iosize == lfs_sb_getbsize(fs))
   1186 			/* Optimization: I/O is unnecessary. */
   1187 			bp->b_blkno = daddr;
   1188 		else {
   1189 			/*
   1190 			 * We need to read the block to preserve the
   1191 			 * existing bytes.
   1192 			 */
   1193 			bp->b_blkno = daddr;
   1194 			bp->b_flags |= B_READ;
   1195 			VOP_STRATEGY(bp);
   1196 			return 0;
   1197 		}
   1198 	}
   1199 
   1200 	return (0);
   1201 }
   1202 
   1203 int
   1204 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
   1205                struct ubuf **bpp)
   1206 {
   1207 	struct inode *ip;
   1208 	struct lfs *fs;
   1209 	int frags;
   1210 	int error;
   1211 
   1212 	ip = VTOI(vp);
   1213 	fs = ip->i_lfs;
   1214 	frags = (long)lfs_numfrags(fs, nsize - osize);
   1215 	error = 0;
   1216 
   1217 	/*
   1218 	 * If we are not asked to actually return the block, all we need
   1219 	 * to do is allocate space for it.  UBC will handle dirtying the
   1220 	 * appropriate things and making sure it all goes to disk.
   1221 	 * Don't bother to read in that case.
   1222 	 */
   1223 	if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
   1224 		brelse(*bpp, 0);
   1225 		goto out;
   1226 	}
   1227 
   1228 	lfs_sb_subbfree(fs, frags);
   1229 	ip->i_lfs_effnblks += frags;
   1230 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1231 
   1232 	if (bpp) {
   1233 		(*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
   1234 		(void)memset((*bpp)->b_data + osize, 0, nsize - osize);
   1235 	}
   1236 
   1237     out:
   1238 	return (error);
   1239 }
   1240