Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.200
      1 /*	$NetBSD: lfs_vfsops.c,v 1.200 2006/04/08 00:26:34 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*-
     39  * Copyright (c) 1989, 1991, 1993, 1994
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_vfsops.c	8.20 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.200 2006/04/08 00:26:34 perseant Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_quota.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/proc.h>
     80 #include <sys/kernel.h>
     81 #include <sys/vnode.h>
     82 #include <sys/mount.h>
     83 #include <sys/kthread.h>
     84 #include <sys/buf.h>
     85 #include <sys/device.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/file.h>
     88 #include <sys/disklabel.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/errno.h>
     91 #include <sys/malloc.h>
     92 #include <sys/pool.h>
     93 #include <sys/socket.h>
     94 #include <sys/syslog.h>
     95 #include <uvm/uvm_extern.h>
     96 #include <sys/sysctl.h>
     97 #include <sys/conf.h>
     98 
     99 #include <miscfs/specfs/specdev.h>
    100 
    101 #include <ufs/ufs/quota.h>
    102 #include <ufs/ufs/inode.h>
    103 #include <ufs/ufs/ufsmount.h>
    104 #include <ufs/ufs/ufs_extern.h>
    105 
    106 #include <uvm/uvm.h>
    107 #include <uvm/uvm_stat.h>
    108 #include <uvm/uvm_pager.h>
    109 #include <uvm/uvm_pdaemon.h>
    110 
    111 #include <ufs/lfs/lfs.h>
    112 #include <ufs/lfs/lfs_extern.h>
    113 
    114 #include <miscfs/genfs/genfs.h>
    115 #include <miscfs/genfs/genfs_node.h>
    116 
    117 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
    118 static boolean_t lfs_issequential_hole(const struct ufsmount *,
    119     daddr_t, daddr_t);
    120 
    121 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
    122 static void warn_ifile_size(struct lfs *);
    123 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
    124     struct ucred *, int, int *, struct lwp *);
    125 
    126 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
    127 extern const struct vnodeopv_desc lfs_specop_opv_desc;
    128 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
    129 
    130 pid_t lfs_writer_daemon = 0;
    131 int lfs_do_flush = 0;
    132 int lfs_do_rfw = 0;
    133 
    134 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
    135 	&lfs_vnodeop_opv_desc,
    136 	&lfs_specop_opv_desc,
    137 	&lfs_fifoop_opv_desc,
    138 	NULL,
    139 };
    140 
    141 struct vfsops lfs_vfsops = {
    142 	MOUNT_LFS,
    143 	lfs_mount,
    144 	ufs_start,
    145 	lfs_unmount,
    146 	ufs_root,
    147 	ufs_quotactl,
    148 	lfs_statvfs,
    149 	lfs_sync,
    150 	lfs_vget,
    151 	lfs_fhtovp,
    152 	lfs_vptofh,
    153 	lfs_init,
    154 	lfs_reinit,
    155 	lfs_done,
    156 	lfs_mountroot,
    157 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    158 	vfs_stdextattrctl,
    159 	lfs_vnodeopv_descs,
    160 };
    161 VFS_ATTACH(lfs_vfsops);
    162 
    163 const struct genfs_ops lfs_genfsops = {
    164 	.gop_size = lfs_gop_size,
    165 	.gop_alloc = ufs_gop_alloc,
    166 	.gop_write = lfs_gop_write,
    167 	.gop_markupdate = ufs_gop_markupdate,
    168 };
    169 
    170 static const struct ufs_ops lfs_ufsops = {
    171 	.uo_itimes = NULL,
    172 	.uo_update = lfs_update,
    173 	.uo_truncate = lfs_truncate,
    174 	.uo_valloc = lfs_valloc,
    175 	.uo_vfree = lfs_vfree,
    176 	.uo_balloc = lfs_balloc,
    177 };
    178 
    179 /*
    180  * XXX Same structure as FFS inodes?  Should we share a common pool?
    181  */
    182 POOL_INIT(lfs_inode_pool, sizeof(struct inode), 0, 0, 0, "lfsinopl",
    183     &pool_allocator_nointr);
    184 POOL_INIT(lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "lfsdinopl",
    185     &pool_allocator_nointr);
    186 POOL_INIT(lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0, "lfsinoextpl",
    187     &pool_allocator_nointr);
    188 POOL_INIT(lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0, "lfslbnpool",
    189     &pool_allocator_nointr);
    190 
    191 /*
    192  * The writer daemon.  UVM keeps track of how many dirty pages we are holding
    193  * in lfs_subsys_pages; the daemon flushes the filesystem when this value
    194  * crosses the (user-defined) threshhold LFS_MAX_PAGES.
    195  */
    196 static void
    197 lfs_writerd(void *arg)
    198 {
    199 	struct mount *mp, *nmp;
    200 	struct lfs *fs;
    201 	int loopcount;
    202 
    203 	lfs_writer_daemon = curproc->p_pid;
    204 
    205 	simple_lock(&lfs_subsys_lock);
    206 	for (;;) {
    207 		ltsleep(&lfs_writer_daemon, PVM | PNORELOCK, "lfswriter", hz/10,
    208 		    &lfs_subsys_lock);
    209 
    210 		/*
    211 		 * Look through the list of LFSs to see if any of them
    212 		 * have requested pageouts.
    213 		 */
    214 		simple_lock(&mountlist_slock);
    215 		for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    216 		     mp = nmp) {
    217 			if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    218 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    219 				continue;
    220 			}
    221 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    222 				    MFSNAMELEN) == 0) {
    223 				fs = VFSTOUFS(mp)->um_lfs;
    224 				simple_lock(&fs->lfs_interlock);
    225 				if (fs->lfs_pdflush) {
    226 					DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
    227 					fs->lfs_pdflush = 0;
    228 					lfs_flush_fs(fs, 0);
    229 					simple_unlock(&fs->lfs_interlock);
    230 				} else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
    231 					DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
    232 					simple_unlock(&fs->lfs_interlock);
    233 					lfs_writer_enter(fs, "wrdirop");
    234 					lfs_flush_pchain(fs);
    235 					lfs_writer_leave(fs);
    236 				}
    237 			}
    238 
    239 			simple_lock(&mountlist_slock);
    240 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    241 			vfs_unbusy(mp);
    242 		}
    243 		simple_unlock(&mountlist_slock);
    244 
    245 		/*
    246 		 * If global state wants a flush, flush everything.
    247 		 */
    248 		simple_lock(&lfs_subsys_lock);
    249 		loopcount = 0;
    250 		if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
    251 			locked_queue_bytes > LFS_MAX_BYTES ||
    252 			lfs_subsys_pages > LFS_MAX_PAGES) {
    253 
    254 			if (lfs_do_flush)
    255 				DLOG((DLOG_FLUSH, "daemon: lfs_do_flush\n"));
    256 			if (locked_queue_count > LFS_MAX_BUFS)
    257 				DLOG((DLOG_FLUSH, "daemon: lqc = %d, max %d\n",
    258 				      locked_queue_count, LFS_MAX_BUFS));
    259 			if (locked_queue_bytes > LFS_MAX_BYTES)
    260 				DLOG((DLOG_FLUSH, "daemon: lqb = %ld, max %ld\n",
    261 				      locked_queue_bytes, LFS_MAX_BYTES));
    262 			if (lfs_subsys_pages > LFS_MAX_PAGES)
    263 				DLOG((DLOG_FLUSH, "daemon: lssp = %d, max %d\n",
    264 				      lfs_subsys_pages, LFS_MAX_PAGES));
    265 
    266 			lfs_flush(NULL, SEGM_WRITERD, 0);
    267 			lfs_do_flush = 0;
    268 		}
    269 	}
    270 	/* NOTREACHED */
    271 }
    272 
    273 /*
    274  * Initialize the filesystem, most work done by ufs_init.
    275  */
    276 void
    277 lfs_init()
    278 {
    279 #ifdef _LKM
    280 	malloc_type_attach(M_SEGMENT);
    281 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    282 	    "lfsinopl", &pool_allocator_nointr);
    283 	pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
    284 	    "lfsdinopl", &pool_allocator_nointr);
    285 	pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
    286 	    "lfsinoextpl", &pool_allocator_nointr);
    287 	pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
    288 	    "lfslbnpool", &pool_allocator_nointr);
    289 #endif
    290 	ufs_init();
    291 
    292 #ifdef DEBUG
    293 	memset(lfs_log, 0, sizeof(lfs_log));
    294 #endif
    295 	simple_lock_init(&lfs_subsys_lock);
    296 }
    297 
    298 void
    299 lfs_reinit()
    300 {
    301 	ufs_reinit();
    302 }
    303 
    304 void
    305 lfs_done()
    306 {
    307 	ufs_done();
    308 #ifdef _LKM
    309 	pool_destroy(&lfs_inode_pool);
    310 	pool_destroy(&lfs_dinode_pool);
    311 	pool_destroy(&lfs_inoext_pool);
    312 	pool_destroy(&lfs_lbnentry_pool);
    313 	malloc_type_detach(M_SEGMENT);
    314 #endif
    315 }
    316 
    317 /*
    318  * Called by main() when ufs is going to be mounted as root.
    319  */
    320 int
    321 lfs_mountroot()
    322 {
    323 	extern struct vnode *rootvp;
    324 	struct mount *mp;
    325 	struct lwp *l = curlwp;	/* XXX */
    326 	int error;
    327 
    328 	if (device_class(root_device) != DV_DISK)
    329 		return (ENODEV);
    330 
    331 	if (rootdev == NODEV)
    332 		return (ENODEV);
    333 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    334 		vrele(rootvp);
    335 		return (error);
    336 	}
    337 	if ((error = lfs_mountfs(rootvp, mp, l))) {
    338 		mp->mnt_op->vfs_refcount--;
    339 		vfs_unbusy(mp);
    340 		free(mp, M_MOUNT);
    341 		return (error);
    342 	}
    343 	simple_lock(&mountlist_slock);
    344 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    345 	simple_unlock(&mountlist_slock);
    346 	(void)lfs_statvfs(mp, &mp->mnt_stat, l);
    347 	vfs_unbusy(mp);
    348 	setrootfstime((time_t)(VFSTOUFS(mp)->um_lfs->lfs_tstamp));
    349 	return (0);
    350 }
    351 
    352 /*
    353  * VFS Operations.
    354  *
    355  * mount system call
    356  */
    357 int
    358 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct lwp *l)
    359 {
    360 	struct vnode *devvp;
    361 	struct ufs_args args;
    362 	struct ufsmount *ump = NULL;
    363 	struct lfs *fs = NULL;				/* LFS */
    364 	struct proc *p;
    365 	int error, update;
    366 	mode_t accessmode;
    367 
    368 	p = l->l_proc;
    369 	if (mp->mnt_flag & MNT_GETARGS) {
    370 		ump = VFSTOUFS(mp);
    371 		if (ump == NULL)
    372 			return EIO;
    373 		args.fspec = NULL;
    374 		return copyout(&args, data, sizeof(args));
    375 	}
    376 	error = copyin(data, &args, sizeof (struct ufs_args));
    377 	if (error)
    378 		return (error);
    379 
    380 	update = mp->mnt_flag & MNT_UPDATE;
    381 
    382 	/* Check arguments */
    383 	if (args.fspec != NULL) {
    384 		/*
    385 		 * Look up the name and verify that it's sane.
    386 		 */
    387 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    388 		if ((error = namei(ndp)) != 0)
    389 			return (error);
    390 		devvp = ndp->ni_vp;
    391 
    392 		if (!update) {
    393 			/*
    394 			 * Be sure this is a valid block device
    395 			 */
    396 			if (devvp->v_type != VBLK)
    397 				error = ENOTBLK;
    398 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    399 				error = ENXIO;
    400 		} else {
    401 			/*
    402 			 * Be sure we're still naming the same device
    403 			 * used for our initial mount
    404 			 */
    405 			ump = VFSTOUFS(mp);
    406 			if (devvp != ump->um_devvp)
    407 				error = EINVAL;
    408 		}
    409 	} else {
    410 		if (!update) {
    411 			/* New mounts must have a filename for the device */
    412 			return (EINVAL);
    413 		} else {
    414 			/* Use the extant mount */
    415 			ump = VFSTOUFS(mp);
    416 			devvp = ump->um_devvp;
    417 			vref(devvp);
    418 		}
    419 	}
    420 
    421 
    422 	/*
    423 	 * If mount by non-root, then verify that user has necessary
    424 	 * permissions on the device.
    425 	 */
    426 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    427 		accessmode = VREAD;
    428 		if (update ?
    429 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    430 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    431 			accessmode |= VWRITE;
    432 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    433 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, l);
    434 		VOP_UNLOCK(devvp, 0);
    435 	}
    436 
    437 	if (error) {
    438 		vrele(devvp);
    439 		return (error);
    440 	}
    441 
    442 	if (!update) {
    443 		int flags;
    444 
    445 		/*
    446 		 * Disallow multiple mounts of the same device.
    447 		 * Disallow mounting of a device that is currently in use
    448 		 * (except for root, which might share swap device for
    449 		 * miniroot).
    450 		 */
    451 		error = vfs_mountedon(devvp);
    452 		if (error)
    453 			goto fail;
    454 		if (vcount(devvp) > 1 && devvp != rootvp) {
    455 			error = EBUSY;
    456 			goto fail;
    457 		}
    458 		if (mp->mnt_flag & MNT_RDONLY)
    459 			flags = FREAD;
    460 		else
    461 			flags = FREAD|FWRITE;
    462 		error = VOP_OPEN(devvp, flags, FSCRED, l);
    463 		if (error)
    464 			goto fail;
    465 		error = lfs_mountfs(devvp, mp, l);		/* LFS */
    466 		if (error) {
    467 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    468 			(void)VOP_CLOSE(devvp, flags, NOCRED, l);
    469 			VOP_UNLOCK(devvp, 0);
    470 			goto fail;
    471 		}
    472 
    473 		ump = VFSTOUFS(mp);
    474 		fs = ump->um_lfs;
    475 	} else {
    476 		/*
    477 		 * Update the mount.
    478 		 */
    479 
    480 		/*
    481 		 * The initial mount got a reference on this
    482 		 * device, so drop the one obtained via
    483 		 * namei(), above.
    484 		 */
    485 		vrele(devvp);
    486 
    487 		ump = VFSTOUFS(mp);
    488 		fs = ump->um_lfs;
    489 		if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    490 			/*
    491 			 * Changing from read-only to read/write.
    492 			 * Note in the superblocks that we're writing.
    493 			 */
    494 			fs->lfs_ronly = 0;
    495 			if (fs->lfs_pflags & LFS_PF_CLEAN) {
    496 				fs->lfs_pflags &= ~LFS_PF_CLEAN;
    497 				lfs_writesuper(fs, fs->lfs_sboffs[0]);
    498 				lfs_writesuper(fs, fs->lfs_sboffs[1]);
    499 			}
    500 		}
    501 		if (args.fspec == NULL)
    502 			return EINVAL;
    503 	}
    504 
    505 	error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    506 	    UIO_USERSPACE, mp, l);
    507 	if (error == 0)
    508 		(void)strncpy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname,
    509 			      sizeof(fs->lfs_fsmnt));
    510 	return error;
    511 
    512 fail:
    513 	vrele(devvp);
    514 	return (error);
    515 }
    516 
    517 /*
    518  * Roll-forward code.
    519  */
    520 
    521 /*
    522  * Load the appropriate indirect block, and change the appropriate pointer.
    523  * Mark the block dirty.  Do segment and avail accounting.
    524  */
    525 static int
    526 update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
    527 	    daddr_t ndaddr, size_t size, struct lwp *l)
    528 {
    529 	int error;
    530 	struct vnode *vp;
    531 	struct inode *ip;
    532 #ifdef DEBUG
    533 	daddr_t odaddr;
    534 	struct indir a[NIADDR];
    535 	int num;
    536 	int i;
    537 #endif /* DEBUG */
    538 	struct buf *bp;
    539 	SEGUSE *sup;
    540 
    541 	KASSERT(lbn >= 0);	/* no indirect blocks */
    542 
    543 	if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
    544 		DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
    545 		      " returned %d\n", ino, error));
    546 		return error;
    547 	}
    548 
    549 	if ((error = lfs_balloc(vp, (lbn << fs->lfs_bshift), size,
    550 				NOCRED, 0, &bp)) != 0) {
    551 		vput(vp);
    552 		return (error);
    553 	}
    554 	/* No need to write, the block is already on disk */
    555 	if (bp->b_flags & B_DELWRI) {
    556 		LFS_UNLOCK_BUF(bp);
    557 		fs->lfs_avail += btofsb(fs, bp->b_bcount);
    558 	}
    559 	bp->b_flags |= B_INVAL;
    560 	brelse(bp);
    561 
    562 	/*
    563 	 * Extend the file, if it is not large enough already.
    564 	 * XXX this is not exactly right, we don't know how much of the
    565 	 * XXX last block is actually used.  We hope that an inode will
    566 	 * XXX appear later to give the correct size.
    567 	 */
    568 	ip = VTOI(vp);
    569 	if (ip->i_size <= (lbn << fs->lfs_bshift)) {
    570 		u_int64_t newsize;
    571 
    572 		if (lbn < NDADDR)
    573 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
    574 				(size - fs->lfs_fsize) + 1;
    575 		else
    576 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) + 1;
    577 
    578 		if (ip->i_size < newsize) {
    579 			ip->i_size = newsize;
    580 			/*
    581 			 * tell vm our new size for the case the inode won't
    582 			 * appear later.
    583 			 */
    584 			uvm_vnp_setsize(vp, newsize);
    585 		}
    586 	}
    587 
    588 	lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
    589 
    590 	LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    591 	sup->su_nbytes += size;
    592 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    593 
    594 	/* differences here should be due to UNWRITTEN indirect blocks. */
    595 	KASSERT((lblkno(fs, ip->i_size) > NDADDR &&
    596 	    ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
    597 	    ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
    598 
    599 #ifdef DEBUG
    600 	/* Now look again to make sure it worked */
    601 	ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
    602 	for (i = num; i > 0; i--) {
    603 		if (!a[i].in_exists)
    604 			panic("update_meta: absent %d lv indirect block", i);
    605 	}
    606 	if (dbtofsb(fs, odaddr) != ndaddr)
    607 		DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
    608 		      PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
    609 #endif /* DEBUG */
    610 	vput(vp);
    611 	return 0;
    612 }
    613 
    614 static int
    615 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred,
    616 	      struct lwp *l)
    617 {
    618 	struct vnode *devvp, *vp;
    619 	struct inode *ip;
    620 	struct ufs1_dinode *dip;
    621 	struct buf *dbp, *ibp;
    622 	int error;
    623 	daddr_t daddr;
    624 	IFILE *ifp;
    625 	SEGUSE *sup;
    626 
    627 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    628 
    629 	/*
    630 	 * Get the inode, update times and perms.
    631 	 * DO NOT update disk blocks, we do that separately.
    632 	 */
    633 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
    634 	if (error) {
    635 		DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
    636 		return error;
    637 	}
    638 	dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
    639 	while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
    640 		if (dip->di_inumber > LFS_IFILE_INUM) {
    641 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    642 					      l, &vp);
    643 			if (error) {
    644 				DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
    645 				      " returned %d\n", error));
    646 				continue;
    647 			}
    648 			ip = VTOI(vp);
    649 			if (dip->di_size != ip->i_size)
    650 				lfs_truncate(vp, dip->di_size, 0, NOCRED, l);
    651 			/* Get mode, link count, size, and times */
    652 			memcpy(ip->i_din.ffs1_din, dip,
    653 			       offsetof(struct ufs1_dinode, di_db[0]));
    654 
    655 			/* Then the rest, except di_blocks */
    656 			ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
    657 			ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
    658 			ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
    659 			ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
    660 
    661 			ip->i_mode = ip->i_ffs1_mode;
    662 			ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
    663 			ip->i_size = ip->i_ffs1_size;
    664 
    665 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    666 
    667 			/* Re-initialize to get type right */
    668 			ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    669 				  &vp);
    670 			vput(vp);
    671 
    672 			/* Record change in location */
    673 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    674 			daddr = ifp->if_daddr;
    675 			ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
    676 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    677 			/* And do segment accounting */
    678 			if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
    679 				if (daddr > 0) {
    680 					LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
    681 						     ibp);
    682 					sup->su_nbytes -= sizeof (struct ufs1_dinode);
    683 					LFS_WRITESEGENTRY(sup, fs,
    684 							  dtosn(fs, daddr),
    685 							  ibp);
    686 				}
    687 				LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    688 					     ibp);
    689 				sup->su_nbytes += sizeof (struct ufs1_dinode);
    690 				LFS_WRITESEGENTRY(sup, fs,
    691 						  dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    692 						  ibp);
    693 			}
    694 		}
    695 	}
    696 	dbp->b_flags |= B_AGE;
    697 	brelse(dbp);
    698 
    699 	return 0;
    700 }
    701 
    702 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    703 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    704 
    705 static daddr_t
    706 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    707 	     struct ucred *cred, int flags, int *pseg_flags, struct lwp *l)
    708 {
    709 	struct vnode *devvp;
    710 	struct buf *bp, *dbp;
    711 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    712 	SEGSUM *ssp;
    713 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    714 	daddr_t oldoffset;
    715 	int32_t *iaddr;	/* XXX ondisk32 */
    716 	FINFO *fip;
    717 	SEGUSE *sup;
    718 	size_t size;
    719 
    720 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    721 	/*
    722 	 * If the segment has a superblock and we're at the top
    723 	 * of the segment, skip the superblock.
    724 	 */
    725 	if (sntod(fs, dtosn(fs, offset)) == offset) {
    726 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
    727 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    728 			offset += btofsb(fs, LFS_SBPAD);
    729 		brelse(bp);
    730 	}
    731 
    732 	/* Read in the segment summary */
    733 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_sumsize, cred, &bp);
    734 	if (error)
    735 		return -1;
    736 
    737 	/* Check summary checksum */
    738 	ssp = (SEGSUM *)bp->b_data;
    739 	if (flags & CHECK_CKSUM) {
    740 		if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
    741 					   fs->lfs_sumsize -
    742 					   sizeof(ssp->ss_sumsum))) {
    743 			DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
    744 			offset = -1;
    745 			goto err1;
    746 		}
    747 		if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
    748 			DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
    749 			offset = -1;
    750 			goto err1;
    751 		}
    752 		if (ssp->ss_create < fs->lfs_tstamp) {
    753 			DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
    754 			offset = -1;
    755 			goto err1;
    756 		}
    757 	}
    758 	if (fs->lfs_version > 1) {
    759 		if (ssp->ss_serial != nextserial) {
    760 			DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
    761 			      "\n", offset));
    762 			offset = -1;
    763 			goto err1;
    764 		}
    765 		if (ssp->ss_ident != fs->lfs_ident) {
    766 			DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
    767 			      PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset));
    768 			offset = -1;
    769 			goto err1;
    770 		}
    771 	}
    772 	if (pseg_flags)
    773 		*pseg_flags = ssp->ss_flags;
    774 	oldoffset = offset;
    775 	offset += btofsb(fs, fs->lfs_sumsize);
    776 
    777 	ninos = howmany(ssp->ss_ninos, INOPB(fs));
    778 	/* XXX ondisk32 */
    779 	iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
    780 	if (flags & CHECK_CKSUM) {
    781 		/* Count blocks */
    782 		nblocks = 0;
    783 		fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    784 		for (i = 0; i < ssp->ss_nfinfo; ++i) {
    785 			nblocks += fip->fi_nblocks;
    786 			if (fip->fi_nblocks <= 0)
    787 				break;
    788 			/* XXX ondisk32 */
    789 			fip = (FINFO *)(((char *)fip) + FINFOSIZE +
    790 					(fip->fi_nblocks * sizeof(int32_t)));
    791 		}
    792 		nblocks += ninos;
    793 		/* Create the sum array */
    794 		datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
    795 					      M_SEGMENT, M_WAITOK);
    796 	}
    797 
    798 	/* Handle individual blocks */
    799 	fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    800 	for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
    801 		/* Inode block? */
    802 		if (ninos && *iaddr == offset) {
    803 			if (flags & CHECK_CKSUM) {
    804 				/* Read in the head and add to the buffer */
    805 				error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
    806 					      cred, &dbp);
    807 				if (error) {
    808 					offset = -1;
    809 					goto err2;
    810 				}
    811 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    812 				dbp->b_flags |= B_AGE;
    813 				brelse(dbp);
    814 			}
    815 			if (flags & CHECK_UPDATE) {
    816 				if ((error = update_inoblk(fs, offset, cred, l))
    817 				    != 0) {
    818 					offset = -1;
    819 					goto err2;
    820 				}
    821 			}
    822 			offset += btofsb(fs, fs->lfs_ibsize);
    823 			--iaddr;
    824 			--ninos;
    825 			--i; /* compensate */
    826 			continue;
    827 		}
    828 		size = fs->lfs_bsize;
    829 		for (j = 0; j < fip->fi_nblocks; ++j) {
    830 			if (j == fip->fi_nblocks - 1)
    831 				size = fip->fi_lastlength;
    832 			if (flags & CHECK_CKSUM) {
    833 				error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
    834 				if (error) {
    835 					offset = -1;
    836 					goto err2;
    837 				}
    838 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    839 				dbp->b_flags |= B_AGE;
    840 				brelse(dbp);
    841 			}
    842 			/* Account for and update any direct blocks */
    843 			if ((flags & CHECK_UPDATE) &&
    844 			   fip->fi_ino > LFS_IFILE_INUM &&
    845 			   fip->fi_blocks[j] >= 0) {
    846 				update_meta(fs, fip->fi_ino, fip->fi_version,
    847 					    fip->fi_blocks[j], offset, size, l);
    848 			}
    849 			offset += btofsb(fs, size);
    850 		}
    851 		/* XXX ondisk32 */
    852 		fip = (FINFO *)(((char *)fip) + FINFOSIZE
    853 				+ fip->fi_nblocks * sizeof(int32_t));
    854 	}
    855 	/* Checksum the array, compare */
    856 	if ((flags & CHECK_CKSUM) &&
    857 	   ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
    858 	{
    859 		DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
    860 		      " (wanted %x got %x)\n",
    861 		      offset, ssp->ss_datasum, cksum(datap, nblocks *
    862 						     sizeof(u_long))));
    863 		offset = -1;
    864 		goto err2;
    865 	}
    866 
    867 	/* If we're at the end of the segment, move to the next */
    868 	if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
    869 	   dtosn(fs, offset)) {
    870 		if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
    871 			offset = -1;
    872 			goto err2;
    873 		}
    874 		offset = ssp->ss_next;
    875 		DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
    876 		       " -> segment %d\n", offset, dtosn(fs,offset)));
    877 	}
    878 
    879 	if (flags & CHECK_UPDATE) {
    880 		fs->lfs_avail -= (offset - oldoffset);
    881 		/* Don't clog the buffer queue */
    882 		simple_lock(&lfs_subsys_lock);
    883 		if (locked_queue_count > LFS_MAX_BUFS ||
    884 		    locked_queue_bytes > LFS_MAX_BYTES) {
    885 			lfs_flush(fs, SEGM_CKP, 0);
    886 		}
    887 		simple_unlock(&lfs_subsys_lock);
    888 	}
    889 
    890     err2:
    891 	if (flags & CHECK_CKSUM)
    892 		free(datap, M_SEGMENT);
    893     err1:
    894 	bp->b_flags |= B_AGE;
    895 	brelse(bp);
    896 
    897 	/* XXX should we update the serial number even for bad psegs? */
    898 	if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
    899 		fs->lfs_serial = nextserial;
    900 	return offset;
    901 }
    902 
    903 /*
    904  * Common code for mount and mountroot
    905  * LFS specific
    906  */
    907 int
    908 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    909 {
    910 	struct dlfs *tdfs, *dfs, *adfs;
    911 	struct lfs *fs;
    912 	struct ufsmount *ump;
    913 	struct vnode *vp;
    914 	struct buf *bp, *abp;
    915 	struct partinfo dpart;
    916 	struct proc *p;
    917 	dev_t dev;
    918 	int error, i, ronly, secsize, fsbsize;
    919 	struct ucred *cred;
    920 	CLEANERINFO *cip;
    921 	SEGUSE *sup;
    922 	int flags, dirty, do_rollforward;
    923 	daddr_t offset, oldoffset, lastgoodpseg, sb_addr;
    924 	int sn, curseg;
    925 
    926 	p = l ? l->l_proc : NULL;
    927 	cred = p ? p->p_ucred : NOCRED;
    928 
    929 	/*
    930 	 * Flush out any old buffers remaining from a previous use.
    931 	 */
    932 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    933 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    934 	VOP_UNLOCK(devvp, 0);
    935 	if (error)
    936 		return (error);
    937 
    938 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    939 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
    940 		secsize = DEV_BSIZE;
    941 	else
    942 		secsize = dpart.disklab->d_secsize;
    943 
    944 	/* Don't free random space on error. */
    945 	bp = NULL;
    946 	abp = NULL;
    947 	ump = NULL;
    948 
    949 	sb_addr = LFS_LABELPAD / secsize;
    950 	while (1) {
    951 		/* Read in the superblock. */
    952 		error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
    953 		if (error)
    954 			goto out;
    955 		dfs = (struct dlfs *)bp->b_data;
    956 
    957 		/* Check the basics. */
    958 		if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
    959 		    dfs->dlfs_version > LFS_VERSION ||
    960 		    dfs->dlfs_bsize < sizeof(struct dlfs)) {
    961 			DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
    962 			error = EINVAL;		/* XXX needs translation */
    963 			goto out;
    964 		}
    965 		if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) {
    966 			DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
    967 			       dfs->dlfs_inodefmt));
    968 			error = EINVAL;
    969 			goto out;
    970 		}
    971 
    972 		if (dfs->dlfs_version == 1)
    973 			fsbsize = secsize;
    974 		else {
    975 			fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
    976 				dfs->dlfs_fsbtodb);
    977 			/*
    978 			 * Could be, if the frag size is large enough, that we
    979 			 * don't have the "real" primary superblock.  If that's
    980 			 * the case, get the real one, and try again.
    981 			 */
    982 			if (sb_addr != dfs->dlfs_sboffs[0] <<
    983 				       dfs->dlfs_fsbtodb) {
    984 				DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
    985 				      " 0x%llx is not right, trying 0x%llx\n",
    986 				      (long long)sb_addr,
    987 				      (long long)(dfs->dlfs_sboffs[0] <<
    988 						  dfs->dlfs_fsbtodb)));
    989 				sb_addr = dfs->dlfs_sboffs[0] <<
    990 					  dfs->dlfs_fsbtodb;
    991 				brelse(bp);
    992 				continue;
    993 			}
    994 		}
    995 		break;
    996 	}
    997 
    998 	/*
    999 	 * Check the second superblock to see which is newer; then mount
   1000 	 * using the older of the two.	This is necessary to ensure that
   1001 	 * the filesystem is valid if it was not unmounted cleanly.
   1002 	 */
   1003 
   1004 	if (dfs->dlfs_sboffs[1] &&
   1005 	    dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
   1006 	{
   1007 		error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
   1008 			LFS_SBPAD, cred, &abp);
   1009 		if (error)
   1010 			goto out;
   1011 		adfs = (struct dlfs *)abp->b_data;
   1012 
   1013 		if (dfs->dlfs_version == 1) {
   1014 			/* 1s resolution comparison */
   1015 			if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
   1016 				tdfs = adfs;
   1017 			else
   1018 				tdfs = dfs;
   1019 		} else {
   1020 			/* monotonic infinite-resolution comparison */
   1021 			if (adfs->dlfs_serial < dfs->dlfs_serial)
   1022 				tdfs = adfs;
   1023 			else
   1024 				tdfs = dfs;
   1025 		}
   1026 
   1027 		/* Check the basics. */
   1028 		if (tdfs->dlfs_magic != LFS_MAGIC ||
   1029 		    tdfs->dlfs_bsize > MAXBSIZE ||
   1030 		    tdfs->dlfs_version > LFS_VERSION ||
   1031 		    tdfs->dlfs_bsize < sizeof(struct dlfs)) {
   1032 			DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
   1033 			      " sanity failed\n"));
   1034 			error = EINVAL;		/* XXX needs translation */
   1035 			goto out;
   1036 		}
   1037 	} else {
   1038 		DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock"
   1039 		      " daddr=0x%x\n", dfs->dlfs_sboffs[1]));
   1040 		error = EINVAL;
   1041 		goto out;
   1042 	}
   1043 
   1044 	/* Allocate the mount structure, copy the superblock into it. */
   1045 	fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
   1046 	memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
   1047 
   1048 	/* Compatibility */
   1049 	if (fs->lfs_version < 2) {
   1050 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
   1051 		fs->lfs_ibsize = fs->lfs_bsize;
   1052 		fs->lfs_start = fs->lfs_sboffs[0];
   1053 		fs->lfs_tstamp = fs->lfs_otstamp;
   1054 		fs->lfs_fsbtodb = 0;
   1055 	}
   1056 
   1057 	/*
   1058 	 * If we aren't going to be able to write meaningfully to this
   1059 	 * filesystem, and were not mounted readonly, bomb out now.
   1060 	 */
   1061 	if (fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
   1062 		DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
   1063 		      " we need BUFPAGES >= %lld\n",
   1064 		      (long long)((bufmem_hiwater / bufmem_lowater) *
   1065 				  LFS_INVERSE_MAX_BYTES(
   1066 					  fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
   1067 		free(fs, M_UFSMNT);
   1068 		error = EFBIG; /* XXX needs translation */
   1069 		goto out;
   1070 	}
   1071 
   1072 	/* Before rolling forward, lock so vget will sleep for other procs */
   1073 	fs->lfs_flags = LFS_NOTYET;
   1074 	fs->lfs_rfpid = p->p_pid;
   1075 
   1076 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
   1077 	ump->um_lfs = fs;
   1078 	ump->um_ops = &lfs_ufsops;
   1079 	ump->um_fstype = UFS1;
   1080 	if (sizeof(struct lfs) < LFS_SBPAD) {			/* XXX why? */
   1081 		bp->b_flags |= B_INVAL;
   1082 		abp->b_flags |= B_INVAL;
   1083 	}
   1084 	brelse(bp);
   1085 	bp = NULL;
   1086 	brelse(abp);
   1087 	abp = NULL;
   1088 
   1089 	/* Set up the I/O information */
   1090 	fs->lfs_devbsize = secsize;
   1091 	fs->lfs_iocount = 0;
   1092 	fs->lfs_diropwait = 0;
   1093 	fs->lfs_activesb = 0;
   1094 	fs->lfs_uinodes = 0;
   1095 	fs->lfs_ravail = 0;
   1096 	fs->lfs_favail = 0;
   1097 	fs->lfs_sbactive = 0;
   1098 
   1099 	/* Set up the ifile and lock aflags */
   1100 	fs->lfs_doifile = 0;
   1101 	fs->lfs_writer = 0;
   1102 	fs->lfs_dirops = 0;
   1103 	fs->lfs_nadirop = 0;
   1104 	fs->lfs_seglock = 0;
   1105 	fs->lfs_pdflush = 0;
   1106 	fs->lfs_sleepers = 0;
   1107 	fs->lfs_pages = 0;
   1108 	simple_lock_init(&fs->lfs_interlock);
   1109 	lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
   1110 	lockinit(&fs->lfs_iflock, PINOD, "lfs_iflock", 0, 0);
   1111 
   1112 	/* Set the file system readonly/modify bits. */
   1113 	fs->lfs_ronly = ronly;
   1114 	if (ronly == 0)
   1115 		fs->lfs_fmod = 1;
   1116 
   1117 	/* Initialize the mount structure. */
   1118 	dev = devvp->v_rdev;
   1119 	mp->mnt_data = ump;
   1120 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1121 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
   1122 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1123 	mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
   1124 	mp->mnt_stat.f_iosize = fs->lfs_bsize;
   1125 	mp->mnt_flag |= MNT_LOCAL;
   1126 	mp->mnt_fs_bshift = fs->lfs_bshift;
   1127 	ump->um_flags = 0;
   1128 	ump->um_mountp = mp;
   1129 	ump->um_dev = dev;
   1130 	ump->um_devvp = devvp;
   1131 	ump->um_bptrtodb = fs->lfs_fsbtodb;
   1132 	ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
   1133 	ump->um_nindir = fs->lfs_nindir;
   1134 	ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
   1135 	for (i = 0; i < MAXQUOTAS; i++)
   1136 		ump->um_quotas[i] = NULLVP;
   1137 	ump->um_maxsymlinklen = fs->lfs_maxsymlinklen;
   1138 	ump->um_dirblksiz = DIRBLKSIZ;
   1139 	ump->um_maxfilesize = fs->lfs_maxfilesize;
   1140 	if (ump->um_maxsymlinklen > 0)
   1141 		mp->mnt_iflag |= IMNT_DTYPE;
   1142 	devvp->v_specmountpoint = mp;
   1143 
   1144 	/* Set up reserved memory for pageout */
   1145 	lfs_setup_resblks(fs);
   1146 	/* Set up vdirop tailq */
   1147 	TAILQ_INIT(&fs->lfs_dchainhd);
   1148 	/* and paging tailq */
   1149 	TAILQ_INIT(&fs->lfs_pchainhd);
   1150 
   1151 	/*
   1152 	 * We use the ifile vnode for almost every operation.  Instead of
   1153 	 * retrieving it from the hash table each time we retrieve it here,
   1154 	 * artificially increment the reference count and keep a pointer
   1155 	 * to it in the incore copy of the superblock.
   1156 	 */
   1157 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
   1158 		DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
   1159 		goto out;
   1160 	}
   1161 	fs->lfs_ivnode = vp;
   1162 	VREF(vp);
   1163 
   1164 	/* Set up inode bitmap and order free list */
   1165 	lfs_order_freelist(fs);
   1166 
   1167 	/* Set up segment usage flags for the autocleaner. */
   1168 	fs->lfs_nactive = 0;
   1169 	fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
   1170 						M_SEGMENT, M_WAITOK);
   1171 	fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
   1172 						 M_SEGMENT, M_WAITOK);
   1173 	fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
   1174 						 M_SEGMENT, M_WAITOK);
   1175 	memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
   1176 	for (i = 0; i < fs->lfs_nseg; i++) {
   1177 		int changed;
   1178 
   1179 		LFS_SEGENTRY(sup, fs, i, bp);
   1180 		changed = 0;
   1181 		if (!ronly) {
   1182 			if (sup->su_nbytes == 0 &&
   1183 			    !(sup->su_flags & SEGUSE_EMPTY)) {
   1184 				sup->su_flags |= SEGUSE_EMPTY;
   1185 				++changed;
   1186 			} else if (!(sup->su_nbytes == 0) &&
   1187 				   (sup->su_flags & SEGUSE_EMPTY)) {
   1188 				sup->su_flags &= ~SEGUSE_EMPTY;
   1189 				++changed;
   1190 			}
   1191 			if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
   1192 				sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
   1193 				++changed;
   1194 			}
   1195 		}
   1196 		fs->lfs_suflags[0][i] = sup->su_flags;
   1197 		if (changed)
   1198 			LFS_WRITESEGENTRY(sup, fs, i, bp);
   1199 		else
   1200 			brelse(bp);
   1201 	}
   1202 
   1203 	/*
   1204 	 * Roll forward.
   1205 	 *
   1206 	 * We don't roll forward for v1 filesystems, because
   1207 	 * of the danger that the clock was turned back between the last
   1208 	 * checkpoint and crash.  This would roll forward garbage.
   1209 	 *
   1210 	 * v2 filesystems don't have this problem because they use a
   1211 	 * monotonically increasing serial number instead of a timestamp.
   1212 	 */
   1213 	do_rollforward = (!(fs->lfs_pflags & LFS_PF_CLEAN) &&
   1214 			  lfs_do_rfw && fs->lfs_version > 1);
   1215 	if (do_rollforward) {
   1216 		u_int64_t nextserial;
   1217 		/*
   1218 		 * Phase I: Find the address of the last good partial
   1219 		 * segment that was written after the checkpoint.  Mark
   1220 		 * the segments in question dirty, so they won't be
   1221 		 * reallocated.
   1222 		 */
   1223 		lastgoodpseg = oldoffset = offset = fs->lfs_offset;
   1224 		flags = 0x0;
   1225 		DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
   1226 		      PRIx64 "\n", offset));
   1227 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
   1228 		if (!(sup->su_flags & SEGUSE_DIRTY))
   1229 			--fs->lfs_nclean;
   1230 		sup->su_flags |= SEGUSE_DIRTY;
   1231 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
   1232 		nextserial = fs->lfs_serial + 1;
   1233 		while ((offset = check_segsum(fs, offset, nextserial,
   1234 		    cred, CHECK_CKSUM, &flags, l)) > 0) {
   1235 			nextserial++;
   1236 			if (sntod(fs, oldoffset) != sntod(fs, offset)) {
   1237 				LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
   1238 					     bp);
   1239 				if (!(sup->su_flags & SEGUSE_DIRTY))
   1240 					--fs->lfs_nclean;
   1241 				sup->su_flags |= SEGUSE_DIRTY;
   1242 				LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
   1243 					     bp);
   1244 			}
   1245 
   1246 			DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
   1247 			      PRIx64 "\n", offset));
   1248 			if (flags & SS_DIROP) {
   1249 				DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
   1250 				      PRIx64 "\n", oldoffset));
   1251 				if (!(flags & SS_CONT))
   1252 				     DLOG((DLOG_RF, "lfs_mountfs: dirops end "
   1253 					   "at 0x%" PRIx64 "\n", oldoffset));
   1254 			}
   1255 			if (!(flags & SS_CONT))
   1256 				lastgoodpseg = offset;
   1257 			oldoffset = offset;
   1258 		}
   1259 		if (flags & SS_CONT) {
   1260 			DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
   1261 			      "dirops discarded\n"));
   1262 		}
   1263 		DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
   1264 		      "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
   1265 		oldoffset = fs->lfs_offset;
   1266 		if (fs->lfs_offset != lastgoodpseg) {
   1267 			/* Don't overwrite what we're trying to preserve */
   1268 			offset = fs->lfs_offset;
   1269 			fs->lfs_offset = lastgoodpseg;
   1270 			fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
   1271 			for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
   1272 				sn = (sn + 1) % fs->lfs_nseg;
   1273 				if (sn == curseg)
   1274 					panic("lfs_mountfs: no clean segments");
   1275 				LFS_SEGENTRY(sup, fs, sn, bp);
   1276 				dirty = (sup->su_flags & SEGUSE_DIRTY);
   1277 				brelse(bp);
   1278 				if (!dirty)
   1279 					break;
   1280 			}
   1281 			fs->lfs_nextseg = sntod(fs, sn);
   1282 
   1283 			/*
   1284 			 * Phase II: Roll forward from the first superblock.
   1285 			 */
   1286 			while (offset != lastgoodpseg) {
   1287 				DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
   1288 				      PRIx64 "\n", offset));
   1289 				offset = check_segsum(fs, offset,
   1290 				    fs->lfs_serial + 1, cred, CHECK_UPDATE,
   1291 				    NULL, l);
   1292 			}
   1293 
   1294 			/*
   1295 			 * Finish: flush our changes to disk.
   1296 			 */
   1297 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1298 			DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
   1299 			      "recovered %lld blocks\n",
   1300 			      (long long)(lastgoodpseg - oldoffset)));
   1301 		}
   1302 		DLOG((DLOG_RF, "LFS roll forward complete\n"));
   1303 	}
   1304 	/* If writing, sb is not clean; record in case of immediate crash */
   1305 	if (!fs->lfs_ronly) {
   1306 		fs->lfs_pflags &= ~LFS_PF_CLEAN;
   1307 		lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1308 		lfs_writesuper(fs, fs->lfs_sboffs[1]);
   1309 	}
   1310 
   1311 	/* Allow vget now that roll-forward is complete */
   1312 	fs->lfs_flags &= ~(LFS_NOTYET);
   1313 	wakeup(&fs->lfs_flags);
   1314 
   1315 	/*
   1316 	 * Initialize the ifile cleaner info with information from
   1317 	 * the superblock.
   1318 	 */
   1319 	LFS_CLEANERINFO(cip, fs, bp);
   1320 	cip->clean = fs->lfs_nclean;
   1321 	cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
   1322 	cip->avail = fs->lfs_avail;
   1323 	cip->bfree = fs->lfs_bfree;
   1324 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
   1325 
   1326 	/*
   1327 	 * Mark the current segment as ACTIVE, since we're going to
   1328 	 * be writing to it.
   1329 	 */
   1330 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
   1331 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1332 	fs->lfs_nactive++;
   1333 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);  /* Ifile */
   1334 
   1335 	/* Now that roll-forward is done, unlock the Ifile */
   1336 	vput(vp);
   1337 
   1338 	/* Comment on ifile size if it is too large */
   1339 	warn_ifile_size(fs);
   1340 
   1341 	/* Start the pagedaemon-anticipating daemon */
   1342 	if (lfs_writer_daemon == 0 &&
   1343 	    kthread_create1(lfs_writerd, NULL, NULL, "lfs_writer") != 0)
   1344 		panic("fork lfs_writer");
   1345 
   1346 	return (0);
   1347 
   1348 out:
   1349 	if (bp)
   1350 		brelse(bp);
   1351 	if (abp)
   1352 		brelse(abp);
   1353 	if (ump) {
   1354 		free(ump->um_lfs, M_UFSMNT);
   1355 		free(ump, M_UFSMNT);
   1356 		mp->mnt_data = NULL;
   1357 	}
   1358 
   1359 	return (error);
   1360 }
   1361 
   1362 /*
   1363  * unmount system call
   1364  */
   1365 int
   1366 lfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
   1367 {
   1368 	struct ufsmount *ump;
   1369 	struct lfs *fs;
   1370 	int error, flags, ronly;
   1371 	int s;
   1372 
   1373 	flags = 0;
   1374 	if (mntflags & MNT_FORCE)
   1375 		flags |= FORCECLOSE;
   1376 
   1377 	ump = VFSTOUFS(mp);
   1378 	fs = ump->um_lfs;
   1379 
   1380 	/* Two checkpoints */
   1381 	lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1382 	lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1383 
   1384 	/* wake up the cleaner so it can die */
   1385 	wakeup(&fs->lfs_nextseg);
   1386 	wakeup(&lfs_allclean_wakeup);
   1387 	simple_lock(&fs->lfs_interlock);
   1388 	while (fs->lfs_sleepers)
   1389 		ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
   1390 			&fs->lfs_interlock);
   1391 	simple_unlock(&fs->lfs_interlock);
   1392 
   1393 #ifdef QUOTA
   1394 	if (mp->mnt_flag & MNT_QUOTA) {
   1395 		int i;
   1396 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
   1397 		if (error)
   1398 			return (error);
   1399 		for (i = 0; i < MAXQUOTAS; i++) {
   1400 			if (ump->um_quotas[i] == NULLVP)
   1401 				continue;
   1402 			quotaoff(l, mp, i);
   1403 		}
   1404 		/*
   1405 		 * Here we fall through to vflush again to ensure
   1406 		 * that we have gotten rid of all the system vnodes.
   1407 		 */
   1408 	}
   1409 #endif
   1410 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
   1411 		return (error);
   1412 	if ((error = VFS_SYNC(mp, 1, l->l_proc->p_ucred, l)) != 0)
   1413 		return (error);
   1414 	s = splbio();
   1415 	if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
   1416 		panic("lfs_unmount: still dirty blocks on ifile vnode");
   1417 	splx(s);
   1418 
   1419 	/* Comment on ifile size if it has become too large */
   1420 	if (!(fs->lfs_flags & LFS_WARNED))
   1421 		warn_ifile_size(fs);
   1422 
   1423 	/* Explicitly write the superblock, to update serial and pflags */
   1424 	fs->lfs_pflags |= LFS_PF_CLEAN;
   1425 	lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1426 	lfs_writesuper(fs, fs->lfs_sboffs[1]);
   1427 	simple_lock(&fs->lfs_interlock);
   1428 	while (fs->lfs_iocount)
   1429 		ltsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
   1430 			&fs->lfs_interlock);
   1431 	simple_unlock(&fs->lfs_interlock);
   1432 
   1433 	/* Finish with the Ifile, now that we're done with it */
   1434 	vrele(fs->lfs_ivnode);
   1435 	vgone(fs->lfs_ivnode);
   1436 
   1437 	ronly = !fs->lfs_ronly;
   1438 	if (ump->um_devvp->v_type != VBAD)
   1439 		ump->um_devvp->v_specmountpoint = NULL;
   1440 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1441 	error = VOP_CLOSE(ump->um_devvp,
   1442 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, l);
   1443 	vput(ump->um_devvp);
   1444 
   1445 	/* Complain about page leakage */
   1446 	if (fs->lfs_pages > 0)
   1447 		printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
   1448 			fs->lfs_pages, lfs_subsys_pages);
   1449 
   1450 	/* Free per-mount data structures */
   1451 	free(fs->lfs_suflags[0], M_SEGMENT);
   1452 	free(fs->lfs_suflags[1], M_SEGMENT);
   1453 	free(fs->lfs_suflags, M_SEGMENT);
   1454 	lfs_free_resblks(fs);
   1455 	free(fs, M_UFSMNT);
   1456 	free(ump, M_UFSMNT);
   1457 
   1458 	mp->mnt_data = NULL;
   1459 	mp->mnt_flag &= ~MNT_LOCAL;
   1460 	return (error);
   1461 }
   1462 
   1463 /*
   1464  * Get file system statistics.
   1465  *
   1466  * NB: We don't lock to access the superblock here, because it's not
   1467  * really that important if we get it wrong.
   1468  */
   1469 int
   1470 lfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
   1471 {
   1472 	struct lfs *fs;
   1473 	struct ufsmount *ump;
   1474 
   1475 	ump = VFSTOUFS(mp);
   1476 	fs = ump->um_lfs;
   1477 	if (fs->lfs_magic != LFS_MAGIC)
   1478 		panic("lfs_statvfs: magic");
   1479 
   1480 	sbp->f_bsize = fs->lfs_bsize;
   1481 	sbp->f_frsize = fs->lfs_fsize;
   1482 	sbp->f_iosize = fs->lfs_bsize;
   1483 	sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks);
   1484 
   1485 	sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
   1486 	KASSERT(sbp->f_bfree <= fs->lfs_dsize);
   1487 	if (sbp->f_bfree < 0)
   1488 		sbp->f_bfree = 0;
   1489 
   1490 	sbp->f_bresvd = fsbtofrags(fs, LFS_EST_RSVD(fs));
   1491 	if (sbp->f_bfree > sbp->f_bresvd)
   1492 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1493 	else
   1494 		sbp->f_bavail = 0;
   1495 
   1496 	sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
   1497 	sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
   1498 	sbp->f_favail = sbp->f_ffree;
   1499 	sbp->f_fresvd = 0;
   1500 	copy_statvfs_info(sbp, mp);
   1501 	return (0);
   1502 }
   1503 
   1504 /*
   1505  * Go through the disk queues to initiate sandbagged IO;
   1506  * go through the inodes to write those that have been modified;
   1507  * initiate the writing of the super block if it has been modified.
   1508  *
   1509  * Note: we are always called with the filesystem marked `MPBUSY'.
   1510  */
   1511 int
   1512 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct lwp *l)
   1513 {
   1514 	int error;
   1515 	struct lfs *fs;
   1516 
   1517 	fs = VFSTOUFS(mp)->um_lfs;
   1518 	if (fs->lfs_ronly)
   1519 		return 0;
   1520 	lfs_writer_enter(fs, "lfs_dirops");
   1521 
   1522 	/* All syncs must be checkpoints until roll-forward is implemented. */
   1523 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
   1524 	lfs_writer_leave(fs);
   1525 #ifdef QUOTA
   1526 	qsync(mp);
   1527 #endif
   1528 	return (error);
   1529 }
   1530 
   1531 extern struct lock ufs_hashlock;
   1532 
   1533 /*
   1534  * Look up an LFS dinode number to find its incore vnode.  If not already
   1535  * in core, read it in from the specified device.  Return the inode locked.
   1536  * Detection and handling of mount points must be done by the calling routine.
   1537  */
   1538 int
   1539 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1540 {
   1541 	struct lfs *fs;
   1542 	struct ufs1_dinode *dip;
   1543 	struct inode *ip;
   1544 	struct buf *bp;
   1545 	struct ifile *ifp;
   1546 	struct vnode *vp;
   1547 	struct ufsmount *ump;
   1548 	daddr_t daddr;
   1549 	dev_t dev;
   1550 	int error, retries;
   1551 	struct timespec ts;
   1552 
   1553 	ump = VFSTOUFS(mp);
   1554 	dev = ump->um_dev;
   1555 	fs = ump->um_lfs;
   1556 
   1557 	/*
   1558 	 * If the filesystem is not completely mounted yet, suspend
   1559 	 * any access requests (wait for roll-forward to complete).
   1560 	 */
   1561 	simple_lock(&fs->lfs_interlock);
   1562 	while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
   1563 		ltsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
   1564 			&fs->lfs_interlock);
   1565 	simple_unlock(&fs->lfs_interlock);
   1566 
   1567 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1568 		return (0);
   1569 
   1570 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1571 		*vpp = NULL;
   1572 		 return (error);
   1573 	}
   1574 
   1575 	do {
   1576 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1577 			ungetnewvnode(vp);
   1578 			return (0);
   1579 		}
   1580 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1581 
   1582 	/* Translate the inode number to a disk address. */
   1583 	if (ino == LFS_IFILE_INUM)
   1584 		daddr = fs->lfs_idaddr;
   1585 	else {
   1586 		/* XXX bounds-check this too */
   1587 		LFS_IENTRY(ifp, fs, ino, bp);
   1588 		daddr = ifp->if_daddr;
   1589 		if (fs->lfs_version > 1) {
   1590 			ts.tv_sec = ifp->if_atime_sec;
   1591 			ts.tv_nsec = ifp->if_atime_nsec;
   1592 		}
   1593 
   1594 		brelse(bp);
   1595 		if (daddr == LFS_UNUSED_DADDR) {
   1596 			*vpp = NULLVP;
   1597 			ungetnewvnode(vp);
   1598 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1599 			return (ENOENT);
   1600 		}
   1601 	}
   1602 
   1603 	/* Allocate/init new vnode/inode. */
   1604 	lfs_vcreate(mp, ino, vp);
   1605 
   1606 	/*
   1607 	 * Put it onto its hash chain and lock it so that other requests for
   1608 	 * this inode will block if they arrive while we are sleeping waiting
   1609 	 * for old data structures to be purged or for the contents of the
   1610 	 * disk portion of this inode to be read.
   1611 	 */
   1612 	ip = VTOI(vp);
   1613 	ufs_ihashins(ip);
   1614 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1615 
   1616 	/*
   1617 	 * XXX
   1618 	 * This may not need to be here, logically it should go down with
   1619 	 * the i_devvp initialization.
   1620 	 * Ask Kirk.
   1621 	 */
   1622 	ip->i_lfs = ump->um_lfs;
   1623 
   1624 	/* Read in the disk contents for the inode, copy into the inode. */
   1625 	retries = 0;
   1626     again:
   1627 	error = bread(ump->um_devvp, fsbtodb(fs, daddr),
   1628 		(fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
   1629 		NOCRED, &bp);
   1630 	if (error) {
   1631 		/*
   1632 		 * The inode does not contain anything useful, so it would
   1633 		 * be misleading to leave it on its hash chain. With mode
   1634 		 * still zero, it will be unlinked and returned to the free
   1635 		 * list by vput().
   1636 		 */
   1637 		vput(vp);
   1638 		brelse(bp);
   1639 		*vpp = NULL;
   1640 		return (error);
   1641 	}
   1642 
   1643 	dip = lfs_ifind(fs, ino, bp);
   1644 	if (dip == NULL) {
   1645 		/* Assume write has not completed yet; try again */
   1646 		bp->b_flags |= B_INVAL;
   1647 		brelse(bp);
   1648 		++retries;
   1649 		if (retries > LFS_IFIND_RETRIES) {
   1650 #ifdef DEBUG
   1651 			/* If the seglock is held look at the bpp to see
   1652 			   what is there anyway */
   1653 			simple_lock(&fs->lfs_interlock);
   1654 			if (fs->lfs_seglock > 0) {
   1655 				struct buf **bpp;
   1656 				struct ufs1_dinode *dp;
   1657 				int i;
   1658 
   1659 				for (bpp = fs->lfs_sp->bpp;
   1660 				     bpp != fs->lfs_sp->cbpp; ++bpp) {
   1661 					if ((*bpp)->b_vp == fs->lfs_ivnode &&
   1662 					    bpp != fs->lfs_sp->bpp) {
   1663 						/* Inode block */
   1664 						printf("lfs_vget: block 0x%" PRIx64 ": ",
   1665 						       (*bpp)->b_blkno);
   1666 						dp = (struct ufs1_dinode *)(*bpp)->b_data;
   1667 						for (i = 0; i < INOPB(fs); i++)
   1668 							if (dp[i].di_u.inumber)
   1669 								printf("%d ", dp[i].di_u.inumber);
   1670 						printf("\n");
   1671 					}
   1672 				}
   1673 			}
   1674 			simple_unlock(&fs->lfs_interlock);
   1675 #endif /* DEBUG */
   1676 			panic("lfs_vget: dinode not found");
   1677 		}
   1678 		simple_lock(&fs->lfs_interlock);
   1679 		if (fs->lfs_iocount) {
   1680 			DLOG((DLOG_VNODE, "lfs_vget: dinode %d not found, retrying...\n", ino));
   1681 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
   1682 				      "lfs ifind", 1, &fs->lfs_interlock);
   1683 		} else
   1684 			retries = LFS_IFIND_RETRIES;
   1685 		simple_unlock(&fs->lfs_interlock);
   1686 		goto again;
   1687 	}
   1688 	*ip->i_din.ffs1_din = *dip;
   1689 	brelse(bp);
   1690 
   1691 	if (fs->lfs_version > 1) {
   1692 		ip->i_ffs1_atime = ts.tv_sec;
   1693 		ip->i_ffs1_atimensec = ts.tv_nsec;
   1694 	}
   1695 
   1696 	lfs_vinit(mp, &vp);
   1697 
   1698 	*vpp = vp;
   1699 
   1700 	KASSERT(VOP_ISLOCKED(vp));
   1701 
   1702 	return (0);
   1703 }
   1704 
   1705 /*
   1706  * File handle to vnode
   1707  */
   1708 int
   1709 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1710 {
   1711 	struct lfid *lfhp;
   1712 	struct buf *bp;
   1713 	IFILE *ifp;
   1714 	int32_t daddr;
   1715 	struct lfs *fs;
   1716 
   1717 	lfhp = (struct lfid *)fhp;
   1718 	if (lfhp->lfid_ino < LFS_IFILE_INUM)
   1719 		return ESTALE;
   1720 
   1721 	fs = VFSTOUFS(mp)->um_lfs;
   1722 	if (lfhp->lfid_ident != fs->lfs_ident)
   1723 		return ESTALE;
   1724 
   1725 	if (lfhp->lfid_ino >
   1726 	    ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
   1727 	     fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
   1728 		return ESTALE;
   1729 
   1730 	if (ufs_ihashlookup(VFSTOUFS(mp)->um_dev, lfhp->lfid_ino) == NULLVP) {
   1731 		LFS_IENTRY(ifp, fs, lfhp->lfid_ino, bp);
   1732 		daddr = ifp->if_daddr;
   1733 		brelse(bp);
   1734 		if (daddr == LFS_UNUSED_DADDR)
   1735 			return ESTALE;
   1736 	}
   1737 
   1738 	return (ufs_fhtovp(mp, &lfhp->lfid_ufid, vpp));
   1739 }
   1740 
   1741 /*
   1742  * Vnode pointer to File handle
   1743  */
   1744 /* ARGSUSED */
   1745 int
   1746 lfs_vptofh(struct vnode *vp, struct fid *fhp)
   1747 {
   1748 	struct inode *ip;
   1749 	struct lfid *lfhp;
   1750 
   1751 	ip = VTOI(vp);
   1752 	lfhp = (struct lfid *)fhp;
   1753 	lfhp->lfid_len = sizeof(struct lfid);
   1754 	lfhp->lfid_ino = ip->i_number;
   1755 	lfhp->lfid_gen = ip->i_gen;
   1756 	lfhp->lfid_ident = ip->i_lfs->lfs_ident;
   1757 	return (0);
   1758 }
   1759 
   1760 static int
   1761 sysctl_lfs_dostats(SYSCTLFN_ARGS)
   1762 {
   1763 	extern struct lfs_stats lfs_stats;
   1764 	extern int lfs_dostats;
   1765 	int error;
   1766 
   1767 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1768 	if (error || newp == NULL)
   1769 		return (error);
   1770 
   1771 	if (lfs_dostats == 0)
   1772 		memset(&lfs_stats, 0, sizeof(lfs_stats));
   1773 
   1774 	return (0);
   1775 }
   1776 
   1777 struct shortlong {
   1778 	const char *sname;
   1779 	const char *lname;
   1780 };
   1781 
   1782 SYSCTL_SETUP(sysctl_vfs_lfs_setup, "sysctl vfs.lfs subtree setup")
   1783 {
   1784 	int i;
   1785 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
   1786 		   lfs_fs_pagetrip;
   1787 #ifdef DEBUG
   1788 	extern int lfs_debug_log_subsys[DLOG_MAX];
   1789 	struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
   1790 		{ "rollforward", "Debug roll-forward code" },
   1791 		{ "alloc",	"Debug inode allocation and free list" },
   1792 		{ "avail",	"Debug space-available-now accounting" },
   1793 		{ "flush",	"Debug flush triggers" },
   1794 		{ "lockedlist",	"Debug locked list accounting" },
   1795 		{ "vnode_verbose", "Verbose per-vnode-written debugging" },
   1796 		{ "vnode",	"Debug vnode use during segment write" },
   1797 		{ "segment",	"Debug segment writing" },
   1798 		{ "seguse",	"Debug segment used-bytes accounting" },
   1799 		{ "cleaner",	"Debug cleaning routines" },
   1800 		{ "mount",	"Debug mount/unmount routines" },
   1801 		{ "pagecache",	"Debug UBC interactions" },
   1802 		{ "dirop",	"Debug directory-operation accounting" },
   1803 		{ "malloc",	"Debug private malloc accounting" },
   1804 	};
   1805 #endif /* DEBUG */
   1806 	struct shortlong stat_names[] = { /* Must match lfs.h! */
   1807 		{ "segsused",	    "Number of new segments allocated" },
   1808 		{ "psegwrites",	    "Number of partial-segment writes" },
   1809 		{ "psyncwrites",    "Number of synchronous partial-segment"
   1810 				    " writes" },
   1811 		{ "pcleanwrites",   "Number of partial-segment writes by the"
   1812 				    " cleaner" },
   1813 		{ "blocktot",       "Number of blocks written" },
   1814 		{ "cleanblocks",    "Number of blocks written by the cleaner" },
   1815 		{ "ncheckpoints",   "Number of checkpoints made" },
   1816 		{ "nwrites",        "Number of whole writes" },
   1817 		{ "nsync_writes",   "Number of synchronous writes" },
   1818 		{ "wait_exceeded",  "Number of times writer waited for"
   1819 				    " cleaner" },
   1820 		{ "write_exceeded", "Number of times writer invoked flush" },
   1821 		{ "flush_invoked",  "Number of times flush was invoked" },
   1822 		{ "vflush_invoked", "Number of time vflush was called" },
   1823 		{ "clean_inlocked", "Number of vnodes skipped for VXLOCK" },
   1824 		{ "clean_vnlocked", "Number of vnodes skipped for vget failure" },
   1825 		{ "segs_reclaimed", "Number of segments reclaimed" },
   1826 	};
   1827 
   1828 	sysctl_createv(clog, 0, NULL, NULL,
   1829 		       CTLFLAG_PERMANENT,
   1830 		       CTLTYPE_NODE, "vfs", NULL,
   1831 		       NULL, 0, NULL, 0,
   1832 		       CTL_VFS, CTL_EOL);
   1833 	sysctl_createv(clog, 0, NULL, NULL,
   1834 		       CTLFLAG_PERMANENT,
   1835 		       CTLTYPE_NODE, "lfs",
   1836 		       SYSCTL_DESCR("Log-structured file system"),
   1837 		       NULL, 0, NULL, 0,
   1838 		       CTL_VFS, 5, CTL_EOL);
   1839 	/*
   1840 	 * XXX the "5" above could be dynamic, thereby eliminating one
   1841 	 * more instance of the "number to vfs" mapping problem, but
   1842 	 * "5" is the order as taken from sys/mount.h
   1843 	 */
   1844 
   1845 	sysctl_createv(clog, 0, NULL, NULL,
   1846 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1847 		       CTLTYPE_INT, "flushindir", NULL,
   1848 		       NULL, 0, &lfs_writeindir, 0,
   1849 		       CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
   1850 	sysctl_createv(clog, 0, NULL, NULL,
   1851 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1852 		       CTLTYPE_INT, "clean_vnhead", NULL,
   1853 		       NULL, 0, &lfs_clean_vnhead, 0,
   1854 		       CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
   1855 	sysctl_createv(clog, 0, NULL, NULL,
   1856 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1857 		       CTLTYPE_INT, "dostats",
   1858 		       SYSCTL_DESCR("Maintain statistics on LFS operations"),
   1859 		       sysctl_lfs_dostats, 0, &lfs_dostats, 0,
   1860 		       CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
   1861 	sysctl_createv(clog, 0, NULL, NULL,
   1862 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1863 		       CTLTYPE_INT, "pagetrip",
   1864 		       SYSCTL_DESCR("How many dirty pages in fs triggers"
   1865 				    " a flush"),
   1866 		       NULL, 0, &lfs_fs_pagetrip, 0,
   1867 		       CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
   1868 	sysctl_createv(clog, 0, NULL, NULL,
   1869 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1870 		       CTLTYPE_INT, "rfw",
   1871 		       SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
   1872 		       NULL, 0, &lfs_do_rfw, 0,
   1873 		       CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
   1874 
   1875 	sysctl_createv(clog, 0, NULL, NULL,
   1876 		       CTLFLAG_PERMANENT,
   1877 		       CTLTYPE_NODE, "stats",
   1878 		       SYSCTL_DESCR("Debugging options"),
   1879 		       NULL, 0, NULL, 0,
   1880 		       CTL_VFS, 5, LFS_STATS, CTL_EOL);
   1881 	for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
   1882 		sysctl_createv(clog, 0, NULL, NULL,
   1883 			       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1884 			       CTLTYPE_INT, stat_names[i].sname,
   1885 			       SYSCTL_DESCR(stat_names[i].lname),
   1886 			       NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
   1887 			       0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
   1888 	}
   1889 
   1890 #ifdef DEBUG
   1891 	sysctl_createv(clog, 0, NULL, NULL,
   1892 		       CTLFLAG_PERMANENT,
   1893 		       CTLTYPE_NODE, "debug",
   1894 		       SYSCTL_DESCR("Debugging options"),
   1895 		       NULL, 0, NULL, 0,
   1896 		       CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
   1897 	for (i = 0; i < DLOG_MAX; i++) {
   1898 		sysctl_createv(clog, 0, NULL, NULL,
   1899 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1900 			       CTLTYPE_INT, dlog_names[i].sname,
   1901 			       SYSCTL_DESCR(dlog_names[i].lname),
   1902 			       NULL, 0, &(lfs_debug_log_subsys[i]), 0,
   1903 			       CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
   1904 	}
   1905 #endif
   1906 }
   1907 
   1908 /*
   1909  * ufs_bmaparray callback function for writing.
   1910  *
   1911  * Since blocks will be written to the new segment anyway,
   1912  * we don't care about current daddr of them.
   1913  */
   1914 static boolean_t
   1915 lfs_issequential_hole(const struct ufsmount *ump,
   1916     daddr_t daddr0, daddr_t daddr1)
   1917 {
   1918 	daddr0 = (daddr_t)((int32_t)daddr0); /* XXX ondisk32 */
   1919 	daddr1 = (daddr_t)((int32_t)daddr1); /* XXX ondisk32 */
   1920 
   1921 	KASSERT(daddr0 == UNWRITTEN ||
   1922 	    (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR));
   1923 	KASSERT(daddr1 == UNWRITTEN ||
   1924 	    (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR));
   1925 
   1926 	/* NOTE: all we want to know here is 'hole or not'. */
   1927 	/* NOTE: UNASSIGNED is converted to 0 by ufs_bmaparray. */
   1928 
   1929 	/*
   1930 	 * treat UNWRITTENs and all resident blocks as 'contiguous'
   1931 	 */
   1932 	if (daddr0 != 0 && daddr1 != 0)
   1933 		return TRUE;
   1934 
   1935 	/*
   1936 	 * both are in hole?
   1937 	 */
   1938 	if (daddr0 == 0 && daddr1 == 0)
   1939 		return TRUE; /* all holes are 'contiguous' for us. */
   1940 
   1941 	return FALSE;
   1942 }
   1943 
   1944 /*
   1945  * lfs_gop_write functions exactly like genfs_gop_write, except that
   1946  * (1) it requires the seglock to be held by its caller, and sp->fip
   1947  *     to be properly initialized (it will return without re-initializing
   1948  *     sp->fip, and without calling lfs_writeseg).
   1949  * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
   1950  *     to determine how large a block it can write at once (though it does
   1951  *     still use VOP_BMAP to find holes in the file);
   1952  * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
   1953  *     (leaving lfs_writeseg to deal with the cluster blocks, so we might
   1954  *     now have clusters of clusters, ick.)
   1955  */
   1956 static int
   1957 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1958 {
   1959 	int i, s, error, run;
   1960 	int fs_bshift;
   1961 	vaddr_t kva;
   1962 	off_t eof, offset, startoffset = 0;
   1963 	size_t bytes, iobytes, skipbytes;
   1964 	daddr_t lbn, blkno;
   1965 	struct vm_page *pg;
   1966 	struct buf *mbp, *bp;
   1967 	struct vnode *devvp = VTOI(vp)->i_devvp;
   1968 	struct inode *ip = VTOI(vp);
   1969 	struct lfs *fs = ip->i_lfs;
   1970 	struct segment *sp = fs->lfs_sp;
   1971 	UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
   1972 
   1973 	ASSERT_SEGLOCK(fs);
   1974 
   1975 	/* The Ifile lives in the buffer cache */
   1976 	KASSERT(vp != fs->lfs_ivnode);
   1977 
   1978         /*
   1979          * We don't want to fill the disk before the cleaner has a chance
   1980          * to make room for us.  If we're in danger of doing that, fail
   1981          * with EAGAIN.  The caller will have to notice this, unlock
   1982          * so the cleaner can run, relock and try again.
   1983          */
   1984         if (LFS_STARVED_FOR_SEGS(fs))
   1985                 goto tryagain;
   1986 
   1987 	/*
   1988 	 * Sometimes things slip past the filters in lfs_putpages,
   1989 	 * and the pagedaemon tries to write pages---problem is
   1990 	 * that the pagedaemon never acquires the segment lock.
   1991 	 *
   1992 	 * Alternatively, pages that were clean when we called
   1993 	 * genfs_putpages may have become dirty in the meantime.  In this
   1994 	 * case the segment header is not properly set up for blocks
   1995 	 * to be added to it.
   1996 	 *
   1997 	 * Unbusy and unclean the pages, and put them on the ACTIVE
   1998 	 * queue under the hypothesis that they couldn't have got here
   1999 	 * unless they were modified *quite* recently.
   2000 	 *
   2001 	 * XXXUBC that last statement is an oversimplification of course.
   2002 	 */
   2003 	if (!LFS_SEGLOCK_HELD(fs) ||
   2004 	    (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) ||
   2005 	    (pgs[0]->offset & fs->lfs_bmask) != 0) {
   2006 		goto tryagain;
   2007 	}
   2008 
   2009 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   2010 	    vp, pgs, npages, flags);
   2011 
   2012 	GOP_SIZE(vp, vp->v_size, &eof, 0);
   2013 
   2014 	if (vp->v_type == VREG)
   2015 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   2016 	else
   2017 		fs_bshift = DEV_BSHIFT;
   2018 	error = 0;
   2019 	pg = pgs[0];
   2020 	startoffset = pg->offset;
   2021 	KASSERT(eof >= 0);
   2022 	if (startoffset >= eof) {
   2023 		goto tryagain;
   2024 	} else
   2025 		bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   2026 	skipbytes = 0;
   2027 
   2028 	KASSERT(bytes != 0);
   2029 
   2030 	/* Swap PG_DELWRI for PG_PAGEOUT */
   2031 	for (i = 0; i < npages; i++)
   2032 		if (pgs[i]->flags & PG_DELWRI) {
   2033 			KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
   2034 			pgs[i]->flags &= ~PG_DELWRI;
   2035 			pgs[i]->flags |= PG_PAGEOUT;
   2036 			uvmexp.paging++;
   2037 			uvm_lock_pageq();
   2038 			uvm_pageunwire(pgs[i]);
   2039 			uvm_unlock_pageq();
   2040 		}
   2041 
   2042 	/*
   2043 	 * Check to make sure we're starting on a block boundary.
   2044 	 * We'll check later to make sure we always write entire
   2045 	 * blocks (or fragments).
   2046 	 */
   2047 	if (startoffset & fs->lfs_bmask)
   2048 		printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
   2049 		       startoffset, fs->lfs_bmask,
   2050 		       startoffset & fs->lfs_bmask);
   2051 	KASSERT((startoffset & fs->lfs_bmask) == 0);
   2052 	if (bytes & fs->lfs_ffmask) {
   2053 		printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
   2054 		panic("lfs_gop_write: non-integer blocks");
   2055 	}
   2056 
   2057 	/*
   2058 	 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
   2059 	 * If we would, write what we have and try again.  If we don't
   2060 	 * have anything to write, we'll have to sleep.
   2061 	 */
   2062 	if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   2063 				      (((SEGSUM *)(sp->segsum))->ss_nfinfo < 1 ?
   2064 				       UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
   2065 		int vers;
   2066 
   2067 		DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
   2068 #if 0
   2069 		      " with nfinfo=%d at offset 0x%x\n",
   2070 		      (int)((SEGSUM *)(sp->segsum))->ss_nfinfo,
   2071 		      (unsigned)fs->lfs_offset));
   2072 #endif
   2073 		if (sp->fip->fi_nblocks == 0) {
   2074 			/* Don't write zero-length finfos */
   2075 			--((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2076 			sp->sum_bytes_left += FINFOSIZE;
   2077 		} else
   2078 			lfs_updatemeta(sp);
   2079 
   2080 		vers = sp->fip->fi_version;
   2081 		(void) lfs_writeseg(fs, sp);
   2082 
   2083 		sp->fip->fi_version = vers;
   2084 		sp->fip->fi_ino = ip->i_number;
   2085 		/* Add the current file to the segment summary. */
   2086 		++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2087 		sp->sum_bytes_left -= FINFOSIZE;
   2088 
   2089 		/*
   2090 		 * Having given up all of the pager_map we were holding,
   2091 		 * we can now wait for aiodoned to reclaim it for us
   2092 		 * without fear of deadlock.
   2093 		 */
   2094 		kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   2095 				     UVMPAGER_MAPIN_WAITOK);
   2096 	}
   2097 
   2098 	s = splbio();
   2099 	simple_lock(&global_v_numoutput_slock);
   2100 	vp->v_numoutput += 2; /* one for biodone, one for aiodone */
   2101 	simple_unlock(&global_v_numoutput_slock);
   2102 	splx(s);
   2103 
   2104 	mbp = getiobuf();
   2105 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   2106 	    vp, mbp, vp->v_numoutput, bytes);
   2107 	mbp->b_bufsize = npages << PAGE_SHIFT;
   2108 	mbp->b_data = (void *)kva;
   2109 	mbp->b_resid = mbp->b_bcount = bytes;
   2110 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
   2111 	mbp->b_iodone = uvm_aio_biodone;
   2112 	mbp->b_vp = vp;
   2113 
   2114 	bp = NULL;
   2115 	for (offset = startoffset;
   2116 	    bytes > 0;
   2117 	    offset += iobytes, bytes -= iobytes) {
   2118 		lbn = offset >> fs_bshift;
   2119 		error = ufs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
   2120 		    lfs_issequential_hole);
   2121 		if (error) {
   2122 			UVMHIST_LOG(ubchist, "ufs_bmaparray() -> %d",
   2123 			    error,0,0,0);
   2124 			skipbytes += bytes;
   2125 			bytes = 0;
   2126 			break;
   2127 		}
   2128 
   2129 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   2130 		    bytes);
   2131 		if (blkno == (daddr_t)-1) {
   2132 			skipbytes += iobytes;
   2133 			continue;
   2134 		}
   2135 
   2136 		/*
   2137 		 * Discover how much we can really pack into this buffer.
   2138 		 */
   2139 		/* If no room in the current segment, finish it up */
   2140 		if (sp->sum_bytes_left < sizeof(int32_t) ||
   2141 		    sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
   2142 			int vers;
   2143 
   2144 			lfs_updatemeta(sp);
   2145 
   2146 			vers = sp->fip->fi_version;
   2147 			(void) lfs_writeseg(fs, sp);
   2148 
   2149 			sp->fip->fi_version = vers;
   2150 			sp->fip->fi_ino = ip->i_number;
   2151 			/* Add the current file to the segment summary. */
   2152 			++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2153 			sp->sum_bytes_left -= FINFOSIZE;
   2154 		}
   2155 		/* Check both for space in segment and space in segsum */
   2156 		iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
   2157 					<< fs_bshift);
   2158 		iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
   2159 				       << fs_bshift);
   2160 		KASSERT(iobytes > 0);
   2161 
   2162 		/* if it's really one i/o, don't make a second buf */
   2163 		if (offset == startoffset && iobytes == bytes) {
   2164 			bp = mbp;
   2165 			/* correct overcount if there is no second buffer */
   2166 			s = splbio();
   2167 			simple_lock(&global_v_numoutput_slock);
   2168 			--vp->v_numoutput;
   2169 			simple_unlock(&global_v_numoutput_slock);
   2170 			splx(s);
   2171 		} else {
   2172 			bp = getiobuf();
   2173 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   2174 			    vp, bp, vp->v_numoutput, 0);
   2175 			bp->b_data = (char *)kva +
   2176 			    (vaddr_t)(offset - pg->offset);
   2177 			bp->b_resid = bp->b_bcount = iobytes;
   2178 			bp->b_flags = B_BUSY|B_WRITE|B_CALL;
   2179 			bp->b_iodone = uvm_aio_biodone1;
   2180 		}
   2181 
   2182 		/* XXX This is silly ... is this necessary? */
   2183 		bp->b_vp = NULL;
   2184 		s = splbio();
   2185 		bgetvp(vp, bp);
   2186 		splx(s);
   2187 
   2188 		bp->b_lblkno = lblkno(fs, offset);
   2189 		bp->b_private = mbp;
   2190 		if (devvp->v_type == VBLK) {
   2191 			bp->b_dev = devvp->v_rdev;
   2192 		}
   2193 		VOP_BWRITE(bp);
   2194 		while (lfs_gatherblock(sp, bp, NULL))
   2195 			continue;
   2196 	}
   2197 
   2198 	if (skipbytes) {
   2199 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   2200 		s = splbio();
   2201 		if (error) {
   2202 			mbp->b_flags |= B_ERROR;
   2203 			mbp->b_error = error;
   2204 		}
   2205 		mbp->b_resid -= skipbytes;
   2206 		if (mbp->b_resid == 0) {
   2207 			biodone(mbp);
   2208 		}
   2209 		splx(s);
   2210 	}
   2211 	UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
   2212 	return (0);
   2213 
   2214     tryagain:
   2215 	/*
   2216 	 * We can't write the pages, for whatever reason.
   2217 	 * Clean up after ourselves, and make the caller try again.
   2218 	 */
   2219 	simple_lock(&vp->v_interlock);
   2220 
   2221 	/* Tell why we're here, if we know */
   2222 	if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE)
   2223 		DLOG((DLOG_PAGE, "lfs_gop_write: clean pages dirtied\n"));
   2224 	else if ((pgs[0]->offset & fs->lfs_bmask) != 0)
   2225 		DLOG((DLOG_PAGE, "lfs_gop_write: not on block boundary\n"));
   2226 	else if (startoffset >= eof)
   2227 		DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
   2228 		      " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
   2229 		      pgs[0]->offset, eof, npages));
   2230 	else if (LFS_STARVED_FOR_SEGS(fs))
   2231 		DLOG((DLOG_PAGE, "lfs_gop_write: avail too low\n"));
   2232 	else
   2233 		DLOG((DLOG_PAGE, "lfs_gop_write: seglock not held\n"));
   2234 
   2235 	uvm_lock_pageq();
   2236 	for (i = 0; i < npages; i++) {
   2237 		pg = pgs[i];
   2238 
   2239 		if (pg->flags & PG_PAGEOUT)
   2240 			uvmexp.paging--;
   2241 		if (pg->flags & PG_DELWRI) {
   2242 			uvm_pageunwire(pg);
   2243 		}
   2244 		uvm_pageactivate(pg);
   2245 		pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
   2246 		DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
   2247 			vp, pg->offset));
   2248 		DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
   2249 		DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
   2250 		DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
   2251 		DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
   2252 		DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
   2253 		      pg->wire_count));
   2254 		DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
   2255 		      pg->loan_count));
   2256 	}
   2257 	/* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
   2258 	uvm_page_unbusy(pgs, npages);
   2259 	uvm_unlock_pageq();
   2260 	simple_unlock(&vp->v_interlock);
   2261 	return EAGAIN;
   2262 }
   2263 
   2264 /*
   2265  * finish vnode/inode initialization.
   2266  * used by lfs_vget and lfs_fastvget.
   2267  */
   2268 void
   2269 lfs_vinit(struct mount *mp, struct vnode **vpp)
   2270 {
   2271 	struct vnode *vp = *vpp;
   2272 	struct inode *ip = VTOI(vp);
   2273 	struct ufsmount *ump = VFSTOUFS(mp);
   2274 	int i;
   2275 
   2276 	ip->i_mode = ip->i_ffs1_mode;
   2277 	ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
   2278 	ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
   2279 	ip->i_flags = ip->i_ffs1_flags;
   2280 	ip->i_gen = ip->i_ffs1_gen;
   2281 	ip->i_uid = ip->i_ffs1_uid;
   2282 	ip->i_gid = ip->i_ffs1_gid;
   2283 
   2284 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
   2285 
   2286 	/*
   2287 	 * Initialize the vnode from the inode, check for aliases.  In all
   2288 	 * cases re-init ip, the underlying vnode/inode may have changed.
   2289 	 */
   2290 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   2291 	ip = VTOI(vp);
   2292 
   2293 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   2294 	if (vp->v_type != VLNK || ip->i_size >= ip->i_ump->um_maxsymlinklen) {
   2295 		struct lfs *fs = ump->um_lfs;
   2296 #ifdef DEBUG
   2297 		for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
   2298 		    i < NDADDR; i++) {
   2299 			if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
   2300 			    i == 0)
   2301 				continue;
   2302 			if (ip->i_ffs1_db[i] != 0) {
   2303 inconsistent:
   2304 				lfs_dump_dinode(ip->i_din.ffs1_din);
   2305 				panic("inconsistent inode");
   2306 			}
   2307 		}
   2308 		for ( ; i < NDADDR + NIADDR; i++) {
   2309 			if (ip->i_ffs1_ib[i - NDADDR] != 0) {
   2310 				goto inconsistent;
   2311 			}
   2312 		}
   2313 #endif /* DEBUG */
   2314 		for (i = 0; i < NDADDR; i++)
   2315 			if (ip->i_ffs1_db[i] != 0)
   2316 				ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   2317 	}
   2318 
   2319 #ifdef DIAGNOSTIC
   2320 	if (vp->v_type == VNON) {
   2321 # ifdef DEBUG
   2322 		lfs_dump_dinode(ip->i_din.ffs1_din);
   2323 # endif
   2324 		panic("lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
   2325 		      (unsigned long long)ip->i_number,
   2326 		      (ip->i_mode & IFMT) >> 12);
   2327 	}
   2328 #endif /* DIAGNOSTIC */
   2329 
   2330 	/*
   2331 	 * Finish inode initialization now that aliasing has been resolved.
   2332 	 */
   2333 
   2334 	ip->i_devvp = ump->um_devvp;
   2335 	VREF(ip->i_devvp);
   2336 	genfs_node_init(vp, &lfs_genfsops);
   2337 	uvm_vnp_setsize(vp, ip->i_size);
   2338 
   2339 	/* Initialize hiblk from file size */
   2340 	ip->i_lfs_hiblk = lblkno(ip->i_lfs, ip->i_size + ip->i_lfs->lfs_bsize - 1) - 1;
   2341 
   2342 	*vpp = vp;
   2343 }
   2344 
   2345 /*
   2346  * Warn if the inode portion of the Ifile is too large to be contained
   2347  * in the buffer cache, according to LFS_MAX_BUFS / LFS_MAX_BYTES.
   2348  * XXX the estimates don't take multiple LFSs into account.
   2349  */
   2350 static void
   2351 warn_ifile_size(struct lfs *fs)
   2352 {
   2353 	KASSERT(LFS_MAX_BUFS > 0);
   2354 	KASSERT(LFS_MAX_BYTES > 0);
   2355 	if (((fs->lfs_ivnode->v_size >> fs->lfs_bshift) - fs->lfs_segtabsz) >
   2356 	    LFS_MAX_BUFS) {
   2357 		simple_lock(&fs->lfs_interlock);
   2358 		fs->lfs_flags |= LFS_WARNED;
   2359 		simple_unlock(&fs->lfs_interlock);
   2360 		log(LOG_WARNING, "lfs_mountfs: inode part of ifile of length %"
   2361 				 PRId64 " cannot fit in %d buffers\n",
   2362 				 fs->lfs_ivnode->v_size -
   2363 				 (fs->lfs_segtabsz << fs->lfs_bshift),
   2364 				 LFS_MAX_BUFS);
   2365 		log(LOG_WARNING, "lfs_mountfs: please consider increasing NBUF"
   2366 				 " to at least %" PRId64 "\n",
   2367 				 LFS_INVERSE_MAX_BUFS((fs->lfs_ivnode->v_size >>
   2368 						       fs->lfs_bshift) -
   2369 						      fs->lfs_segtabsz));
   2370 	} else if ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) > LFS_MAX_BUFS) {
   2371 		/* Same thing but LOG_NOTICE */
   2372 		simple_lock(&fs->lfs_interlock);
   2373 		fs->lfs_flags |= LFS_WARNED;
   2374 		simple_unlock(&fs->lfs_interlock);
   2375 		log(LOG_NOTICE, "lfs_mountfs: entire ifile of length %"
   2376 				PRId64 " cannot fit in %d buffers\n",
   2377 				fs->lfs_ivnode->v_size, LFS_MAX_BUFS);
   2378 		log(LOG_NOTICE, "lfs_mountfs: please consider increasing NBUF"
   2379 				" to at least %" PRId64 "\n",
   2380 				LFS_INVERSE_MAX_BUFS(fs->lfs_ivnode->v_size >>
   2381 						     fs->lfs_bshift));
   2382 	}
   2383 
   2384 	if (fs->lfs_ivnode->v_size - (fs->lfs_segtabsz << fs->lfs_bshift) >
   2385 	    LFS_MAX_BYTES) {
   2386 		simple_lock(&fs->lfs_interlock);
   2387 		fs->lfs_flags |= LFS_WARNED;
   2388 		simple_unlock(&fs->lfs_interlock);
   2389 		log(LOG_WARNING, "lfs_mountfs: inode part of ifile of length %"
   2390 				 PRId64 " cannot fit in %lu bytes\n",
   2391 				 fs->lfs_ivnode->v_size - (fs->lfs_segtabsz <<
   2392 							   fs->lfs_bshift),
   2393 				 LFS_MAX_BYTES);
   2394 		log(LOG_WARNING, "lfs_mountfs: please consider increasing"
   2395 				 " BUFPAGES to at least %" PRId64 "\n",
   2396 				 LFS_INVERSE_MAX_BYTES(fs->lfs_ivnode->v_size -
   2397 						       (fs->lfs_segtabsz <<
   2398 							fs->lfs_bshift)) >>
   2399 				 PAGE_SHIFT);
   2400 	} else if(fs->lfs_ivnode->v_size > LFS_MAX_BYTES) {
   2401 		simple_lock(&fs->lfs_interlock);
   2402 		fs->lfs_flags |= LFS_WARNED;
   2403 		simple_unlock(&fs->lfs_interlock);
   2404 		log(LOG_NOTICE, "lfs_mountfs: entire ifile of length %" PRId64
   2405 				" cannot fit in %lu buffer bytes\n",
   2406 				fs->lfs_ivnode->v_size, LFS_MAX_BYTES);
   2407 		log(LOG_NOTICE, "lfs_mountfs: please consider increasing"
   2408 				" BUFPAGES to at least %" PRId64 "\n",
   2409 				LFS_INVERSE_MAX_BYTES(fs->lfs_ivnode->v_size -
   2410 						      (fs->lfs_segtabsz <<
   2411 						       fs->lfs_bshift)) >>
   2412 				PAGE_SHIFT);
   2413 	}
   2414 }
   2415 
   2416 /*
   2417  * Resize the filesystem to contain the specified number of segments.
   2418  */
   2419 int
   2420 lfs_resize_fs(struct lfs *fs, int newnsegs)
   2421 {
   2422 	SEGUSE *sup;
   2423 	struct buf *bp, *obp;
   2424 	daddr_t olast, nlast, ilast, noff, start, end;
   2425 	struct vnode *ivp;
   2426 	struct inode *ip;
   2427 	int error, badnews, inc, oldnsegs;
   2428 	int sbbytes, csbbytes, gain, cgain;
   2429 	int i;
   2430 
   2431 	/* Only support v2 and up */
   2432 	if (fs->lfs_version < 2)
   2433 		return EOPNOTSUPP;
   2434 
   2435 	/* If we're doing nothing, do it fast */
   2436 	oldnsegs = fs->lfs_nseg;
   2437 	if (newnsegs == oldnsegs)
   2438 		return 0;
   2439 
   2440 	/* We always have to have two superblocks */
   2441 	if (newnsegs <= dtosn(fs, fs->lfs_sboffs[1]))
   2442 		return EFBIG;
   2443 
   2444 	ivp = fs->lfs_ivnode;
   2445 	ip = VTOI(ivp);
   2446 	error = 0;
   2447 
   2448 	/* Take the segment lock so no one else calls lfs_newseg() */
   2449 	lfs_seglock(fs, SEGM_PROT);
   2450 
   2451 	/*
   2452 	 * Make sure the segments we're going to be losing, if any,
   2453 	 * are in fact empty.  We hold the seglock, so their status
   2454 	 * cannot change underneath us.  Count the superblocks we lose,
   2455 	 * while we're at it.
   2456 	 */
   2457 	sbbytes = csbbytes = 0;
   2458 	cgain = 0;
   2459 	for (i = newnsegs; i < oldnsegs; i++) {
   2460 		LFS_SEGENTRY(sup, fs, i, bp);
   2461 		badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
   2462 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
   2463 			sbbytes += LFS_SBPAD;
   2464 		if (!(sup->su_flags & SEGUSE_DIRTY)) {
   2465 			++cgain;
   2466 			if (sup->su_flags & SEGUSE_SUPERBLOCK)
   2467 				csbbytes += LFS_SBPAD;
   2468 		}
   2469 		brelse(bp);
   2470 		if (badnews) {
   2471 			error = EBUSY;
   2472 			goto out;
   2473 		}
   2474 	}
   2475 
   2476 	/* Note old and new segment table endpoints, and old ifile size */
   2477 	olast = fs->lfs_cleansz + fs->lfs_segtabsz;
   2478 	nlast = howmany(newnsegs, fs->lfs_sepb) + fs->lfs_cleansz;
   2479 	ilast = ivp->v_size >> fs->lfs_bshift;
   2480 	noff = nlast - olast;
   2481 
   2482 	/*
   2483 	 * Make sure no one can use the Ifile while we change it around.
   2484 	 * Even after taking the iflock we need to make sure no one still
   2485 	 * is holding Ifile buffers, so we get each one, to drain them.
   2486 	 * (XXX this could be done better.)
   2487 	 */
   2488 	simple_lock(&fs->lfs_interlock);
   2489 	lockmgr(&fs->lfs_iflock, LK_EXCLUSIVE, &fs->lfs_interlock);
   2490 	simple_unlock(&fs->lfs_interlock);
   2491 	vn_lock(ivp, LK_EXCLUSIVE | LK_RETRY);
   2492 	for (i = 0; i < ilast; i++) {
   2493 		bread(ivp, i, fs->lfs_bsize, NOCRED, &bp);
   2494 		brelse(bp);
   2495 	}
   2496 
   2497 	/* Allocate new Ifile blocks */
   2498 	for (i = ilast; i < ilast + noff; i++) {
   2499 		if (lfs_balloc(ivp, i * fs->lfs_bsize, fs->lfs_bsize, NOCRED, 0,
   2500 			       &bp) != 0)
   2501 			panic("balloc extending ifile");
   2502 		memset(bp->b_data, 0, fs->lfs_bsize);
   2503 		VOP_BWRITE(bp);
   2504 	}
   2505 
   2506 	/* Register new ifile size */
   2507 	ip->i_size += noff * fs->lfs_bsize;
   2508 	ip->i_ffs1_size = ip->i_size;
   2509 	uvm_vnp_setsize(ivp, ip->i_size);
   2510 
   2511 	/* Copy the inode table to its new position */
   2512 	if (noff != 0) {
   2513 		if (noff < 0) {
   2514 			start = nlast;
   2515 			end = ilast + noff;
   2516 			inc = 1;
   2517 		} else {
   2518 			start = ilast + noff - 1;
   2519 			end = nlast - 1;
   2520 			inc = -1;
   2521 		}
   2522 		for (i = start; i != end; i += inc) {
   2523 			if (bread(ivp, i, fs->lfs_bsize, NOCRED, &bp) != 0)
   2524 				panic("resize: bread dst blk failed");
   2525 			if (bread(ivp, i - noff, fs->lfs_bsize, NOCRED, &obp))
   2526 				panic("resize: bread src blk failed");
   2527 			memcpy(bp->b_data, obp->b_data, fs->lfs_bsize);
   2528 			VOP_BWRITE(bp);
   2529 			brelse(obp);
   2530 		}
   2531 	}
   2532 
   2533 	/* If we are expanding, write the new empty SEGUSE entries */
   2534 	if (newnsegs > oldnsegs) {
   2535 		for (i = oldnsegs; i < newnsegs; i++) {
   2536 			if ((error = bread(ivp, i / fs->lfs_sepb +
   2537 					   fs->lfs_cleansz,
   2538 					   fs->lfs_bsize, NOCRED, &bp)) != 0)
   2539 				panic("lfs: ifile read: %d", error);
   2540 			while ((i + 1) % fs->lfs_sepb && i < newnsegs) {
   2541 				sup = &((SEGUSE *)bp->b_data)[i % fs->lfs_sepb];
   2542 				memset(sup, 0, sizeof(*sup));
   2543 				i++;
   2544 			}
   2545 			VOP_BWRITE(bp);
   2546 		}
   2547 	}
   2548 
   2549 	/* Zero out unused superblock offsets */
   2550 	for (i = 2; i < LFS_MAXNUMSB; i++)
   2551 		if (dtosn(fs, fs->lfs_sboffs[i]) >= newnsegs)
   2552 			fs->lfs_sboffs[i] = 0x0;
   2553 
   2554 	/*
   2555 	 * Correct superblock entries that depend on fs size.
   2556 	 * The computations of these are as follows:
   2557 	 *
   2558 	 * size  = segtod(fs, nseg)
   2559 	 * dsize = segtod(fs, nseg - minfreeseg) - btofsb(#super * LFS_SBPAD)
   2560 	 * bfree = dsize - btofsb(fs, bsize * nseg / 2) - blocks_actually_used
   2561 	 * avail = segtod(fs, nclean) - btofsb(#clean_super * LFS_SBPAD)
   2562 	 *         + (segtod(fs, 1) - (offset - curseg))
   2563 	 *	   - segtod(fs, minfreeseg - (minfreeseg / 2))
   2564 	 *
   2565 	 * XXX - we should probably adjust minfreeseg as well.
   2566 	 */
   2567 	gain = (newnsegs - oldnsegs);
   2568 	fs->lfs_nseg = newnsegs;
   2569 	fs->lfs_segtabsz = nlast - fs->lfs_cleansz;
   2570 	fs->lfs_size += gain * btofsb(fs, fs->lfs_ssize);
   2571 	fs->lfs_dsize += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes);
   2572 	fs->lfs_bfree += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes)
   2573 		       - gain * btofsb(fs, fs->lfs_bsize / 2);
   2574 	if (gain > 0) {
   2575 		fs->lfs_nclean += gain;
   2576 		fs->lfs_avail += gain * btofsb(fs, fs->lfs_ssize);
   2577 	} else {
   2578 		fs->lfs_nclean -= cgain;
   2579 		fs->lfs_avail -= cgain * btofsb(fs, fs->lfs_ssize) -
   2580 				 btofsb(fs, csbbytes);
   2581 	}
   2582 
   2583 	/* Resize segment flag cache */
   2584 	fs->lfs_suflags[0] = (u_int32_t *)realloc(fs->lfs_suflags[0],
   2585 						  fs->lfs_nseg * sizeof(u_int32_t),
   2586 						  M_SEGMENT, M_WAITOK);
   2587 	fs->lfs_suflags[1] = (u_int32_t *)realloc(fs->lfs_suflags[0],
   2588 						  fs->lfs_nseg * sizeof(u_int32_t),
   2589 						  M_SEGMENT, M_WAITOK);
   2590 	for (i = oldnsegs; i < newnsegs; i++)
   2591 		fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
   2592 
   2593 	/* Truncate Ifile if necessary */
   2594 	if (noff < 0)
   2595 		lfs_truncate(ivp, ivp->v_size + (noff << fs->lfs_bshift), 0,
   2596 			     NOCRED, curlwp);
   2597 
   2598 	/* Update cleaner info so the cleaner can die */
   2599 	bread(ivp, 0, fs->lfs_bsize, NOCRED, &bp);
   2600 	((CLEANERINFO *)bp->b_data)->clean = fs->lfs_nclean;
   2601 	((CLEANERINFO *)bp->b_data)->dirty = fs->lfs_nseg - fs->lfs_nclean;
   2602 	VOP_BWRITE(bp);
   2603 
   2604 	/* Let Ifile accesses proceed */
   2605 	VOP_UNLOCK(ivp, 0);
   2606 	simple_lock(&fs->lfs_interlock);
   2607 	lockmgr(&fs->lfs_iflock, LK_RELEASE, &fs->lfs_interlock);
   2608 	simple_unlock(&fs->lfs_interlock);
   2609 
   2610     out:
   2611 	lfs_segunlock(fs);
   2612 	return error;
   2613 }
   2614