Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.223
      1 /*	$NetBSD: lfs_vfsops.c,v 1.223 2006/10/12 01:32:51 christos 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.223 2006/10/12 01:32:51 christos 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 #include <sys/kauth.h>
     99 
    100 #include <miscfs/specfs/specdev.h>
    101 
    102 #include <ufs/ufs/quota.h>
    103 #include <ufs/ufs/inode.h>
    104 #include <ufs/ufs/ufsmount.h>
    105 #include <ufs/ufs/ufs_extern.h>
    106 
    107 #include <uvm/uvm.h>
    108 #include <uvm/uvm_stat.h>
    109 #include <uvm/uvm_pager.h>
    110 #include <uvm/uvm_pdaemon.h>
    111 
    112 #include <ufs/lfs/lfs.h>
    113 #include <ufs/lfs/lfs_extern.h>
    114 
    115 #include <miscfs/genfs/genfs.h>
    116 #include <miscfs/genfs/genfs_node.h>
    117 
    118 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
    119 static boolean_t lfs_issequential_hole(const struct ufsmount *,
    120     daddr_t, daddr_t);
    121 
    122 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
    123 
    124 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
    125 extern const struct vnodeopv_desc lfs_specop_opv_desc;
    126 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
    127 
    128 pid_t lfs_writer_daemon = 0;
    129 int lfs_do_flush = 0;
    130 #ifdef LFS_KERNEL_RFW
    131 int lfs_do_rfw = 0;
    132 #endif
    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 	0,
    161 	{ NULL, NULL },
    162 };
    163 VFS_ATTACH(lfs_vfsops);
    164 
    165 const struct genfs_ops lfs_genfsops = {
    166 	.gop_size = lfs_gop_size,
    167 	.gop_alloc = ufs_gop_alloc,
    168 	.gop_write = lfs_gop_write,
    169 	.gop_markupdate = ufs_gop_markupdate,
    170 };
    171 
    172 static const struct ufs_ops lfs_ufsops = {
    173 	.uo_itimes = NULL,
    174 	.uo_update = lfs_update,
    175 	.uo_truncate = lfs_truncate,
    176 	.uo_valloc = lfs_valloc,
    177 	.uo_vfree = lfs_vfree,
    178 	.uo_balloc = lfs_balloc,
    179 };
    180 
    181 /*
    182  * XXX Same structure as FFS inodes?  Should we share a common pool?
    183  */
    184 POOL_INIT(lfs_inode_pool, sizeof(struct inode), 0, 0, 0, "lfsinopl",
    185     &pool_allocator_nointr);
    186 POOL_INIT(lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "lfsdinopl",
    187     &pool_allocator_nointr);
    188 POOL_INIT(lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0, "lfsinoextpl",
    189     &pool_allocator_nointr);
    190 POOL_INIT(lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0, "lfslbnpool",
    191     &pool_allocator_nointr);
    192 
    193 /*
    194  * The writer daemon.  UVM keeps track of how many dirty pages we are holding
    195  * in lfs_subsys_pages; the daemon flushes the filesystem when this value
    196  * crosses the (user-defined) threshhold LFS_MAX_PAGES.
    197  */
    198 static void
    199 lfs_writerd(void *arg __unused)
    200 {
    201 	struct mount *mp, *nmp;
    202 	struct lfs *fs;
    203 	int loopcount;
    204 
    205 	lfs_writer_daemon = curproc->p_pid;
    206 
    207 	simple_lock(&lfs_subsys_lock);
    208 	for (;;) {
    209 		ltsleep(&lfs_writer_daemon, PVM | PNORELOCK, "lfswriter", hz/10,
    210 		    &lfs_subsys_lock);
    211 
    212 		/*
    213 		 * Look through the list of LFSs to see if any of them
    214 		 * have requested pageouts.
    215 		 */
    216 		simple_lock(&mountlist_slock);
    217 		for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    218 		     mp = nmp) {
    219 			if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    220 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    221 				continue;
    222 			}
    223 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    224 				    MFSNAMELEN) == 0) {
    225 				fs = VFSTOUFS(mp)->um_lfs;
    226 				simple_lock(&fs->lfs_interlock);
    227 				if (fs->lfs_pdflush) {
    228 					DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
    229 					fs->lfs_pdflush = 0;
    230 					lfs_flush_fs(fs, 0);
    231 					simple_unlock(&fs->lfs_interlock);
    232 				} else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
    233 					DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
    234 					simple_unlock(&fs->lfs_interlock);
    235 					lfs_writer_enter(fs, "wrdirop");
    236 					lfs_flush_pchain(fs);
    237 					lfs_writer_leave(fs);
    238 				} else
    239 					simple_unlock(&fs->lfs_interlock);
    240 			}
    241 
    242 			simple_lock(&mountlist_slock);
    243 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    244 			vfs_unbusy(mp);
    245 		}
    246 		simple_unlock(&mountlist_slock);
    247 
    248 		/*
    249 		 * If global state wants a flush, flush everything.
    250 		 */
    251 		simple_lock(&lfs_subsys_lock);
    252 		loopcount = 0;
    253 		if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
    254 			locked_queue_bytes > LFS_MAX_BYTES ||
    255 			lfs_subsys_pages > LFS_MAX_PAGES) {
    256 
    257 			if (lfs_do_flush) {
    258 				DLOG((DLOG_FLUSH, "daemon: lfs_do_flush\n"));
    259 			}
    260 			if (locked_queue_count > LFS_MAX_BUFS) {
    261 				DLOG((DLOG_FLUSH, "daemon: lqc = %d, max %d\n",
    262 				      locked_queue_count, LFS_MAX_BUFS));
    263 			}
    264 			if (locked_queue_bytes > LFS_MAX_BYTES) {
    265 				DLOG((DLOG_FLUSH, "daemon: lqb = %ld, max %ld\n",
    266 				      locked_queue_bytes, LFS_MAX_BYTES));
    267 			}
    268 			if (lfs_subsys_pages > LFS_MAX_PAGES) {
    269 				DLOG((DLOG_FLUSH, "daemon: lssp = %d, max %d\n",
    270 				      lfs_subsys_pages, LFS_MAX_PAGES));
    271 			}
    272 
    273 			lfs_flush(NULL, SEGM_WRITERD, 0);
    274 			lfs_do_flush = 0;
    275 		}
    276 	}
    277 	/* NOTREACHED */
    278 }
    279 
    280 /*
    281  * Initialize the filesystem, most work done by ufs_init.
    282  */
    283 void
    284 lfs_init()
    285 {
    286 #ifdef _LKM
    287 	malloc_type_attach(M_SEGMENT);
    288 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    289 	    "lfsinopl", &pool_allocator_nointr);
    290 	pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
    291 	    "lfsdinopl", &pool_allocator_nointr);
    292 	pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
    293 	    "lfsinoextpl", &pool_allocator_nointr);
    294 	pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
    295 	    "lfslbnpool", &pool_allocator_nointr);
    296 #endif
    297 	ufs_init();
    298 
    299 #ifdef DEBUG
    300 	memset(lfs_log, 0, sizeof(lfs_log));
    301 #endif
    302 	simple_lock_init(&lfs_subsys_lock);
    303 }
    304 
    305 void
    306 lfs_reinit()
    307 {
    308 	ufs_reinit();
    309 }
    310 
    311 void
    312 lfs_done()
    313 {
    314 	ufs_done();
    315 #ifdef _LKM
    316 	pool_destroy(&lfs_inode_pool);
    317 	pool_destroy(&lfs_dinode_pool);
    318 	pool_destroy(&lfs_inoext_pool);
    319 	pool_destroy(&lfs_lbnentry_pool);
    320 	malloc_type_detach(M_SEGMENT);
    321 #endif
    322 }
    323 
    324 /*
    325  * Called by main() when ufs is going to be mounted as root.
    326  */
    327 int
    328 lfs_mountroot()
    329 {
    330 	extern struct vnode *rootvp;
    331 	struct mount *mp;
    332 	struct lwp *l = curlwp;	/* XXX */
    333 	int error;
    334 
    335 	if (device_class(root_device) != DV_DISK)
    336 		return (ENODEV);
    337 
    338 	if (rootdev == NODEV)
    339 		return (ENODEV);
    340 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    341 		vrele(rootvp);
    342 		return (error);
    343 	}
    344 	if ((error = lfs_mountfs(rootvp, mp, l))) {
    345 		mp->mnt_op->vfs_refcount--;
    346 		vfs_unbusy(mp);
    347 		free(mp, M_MOUNT);
    348 		return (error);
    349 	}
    350 	simple_lock(&mountlist_slock);
    351 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    352 	simple_unlock(&mountlist_slock);
    353 	(void)lfs_statvfs(mp, &mp->mnt_stat, l);
    354 	vfs_unbusy(mp);
    355 	setrootfstime((time_t)(VFSTOUFS(mp)->um_lfs->lfs_tstamp));
    356 	return (0);
    357 }
    358 
    359 /*
    360  * VFS Operations.
    361  *
    362  * mount system call
    363  */
    364 int
    365 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct lwp *l)
    366 {
    367 	struct vnode *devvp;
    368 	struct ufs_args args;
    369 	struct ufsmount *ump = NULL;
    370 	struct lfs *fs = NULL;				/* LFS */
    371 	int error, update;
    372 	mode_t accessmode;
    373 
    374 	if (mp->mnt_flag & MNT_GETARGS) {
    375 		ump = VFSTOUFS(mp);
    376 		if (ump == NULL)
    377 			return EIO;
    378 		args.fspec = NULL;
    379 		return copyout(&args, data, sizeof(args));
    380 	}
    381 	error = copyin(data, &args, sizeof (struct ufs_args));
    382 	if (error)
    383 		return (error);
    384 
    385 	update = mp->mnt_flag & MNT_UPDATE;
    386 
    387 	/* Check arguments */
    388 	if (args.fspec != NULL) {
    389 		/*
    390 		 * Look up the name and verify that it's sane.
    391 		 */
    392 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    393 		if ((error = namei(ndp)) != 0)
    394 			return (error);
    395 		devvp = ndp->ni_vp;
    396 
    397 		if (!update) {
    398 			/*
    399 			 * Be sure this is a valid block device
    400 			 */
    401 			if (devvp->v_type != VBLK)
    402 				error = ENOTBLK;
    403 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    404 				error = ENXIO;
    405 		} else {
    406 			/*
    407 			 * Be sure we're still naming the same device
    408 			 * used for our initial mount
    409 			 */
    410 			ump = VFSTOUFS(mp);
    411 			if (devvp != ump->um_devvp)
    412 				error = EINVAL;
    413 		}
    414 	} else {
    415 		if (!update) {
    416 			/* New mounts must have a filename for the device */
    417 			return (EINVAL);
    418 		} else {
    419 			/* Use the extant mount */
    420 			ump = VFSTOUFS(mp);
    421 			devvp = ump->um_devvp;
    422 			vref(devvp);
    423 		}
    424 	}
    425 
    426 
    427 	/*
    428 	 * If mount by non-root, then verify that user has necessary
    429 	 * permissions on the device.
    430 	 */
    431 	if (error == 0 && kauth_cred_geteuid(l->l_cred) != 0) {
    432 		accessmode = VREAD;
    433 		if (update ?
    434 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    435 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    436 			accessmode |= VWRITE;
    437 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    438 		error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
    439 		VOP_UNLOCK(devvp, 0);
    440 	}
    441 
    442 	if (error) {
    443 		vrele(devvp);
    444 		return (error);
    445 	}
    446 
    447 	if (!update) {
    448 		int flags;
    449 
    450 		/*
    451 		 * Disallow multiple mounts of the same device.
    452 		 * Disallow mounting of a device that is currently in use
    453 		 * (except for root, which might share swap device for
    454 		 * miniroot).
    455 		 */
    456 		error = vfs_mountedon(devvp);
    457 		if (error)
    458 			goto fail;
    459 		if (vcount(devvp) > 1 && devvp != rootvp) {
    460 			error = EBUSY;
    461 			goto fail;
    462 		}
    463 		if (mp->mnt_flag & MNT_RDONLY)
    464 			flags = FREAD;
    465 		else
    466 			flags = FREAD|FWRITE;
    467 		error = VOP_OPEN(devvp, flags, FSCRED, l);
    468 		if (error)
    469 			goto fail;
    470 		error = lfs_mountfs(devvp, mp, l);		/* LFS */
    471 		if (error) {
    472 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    473 			(void)VOP_CLOSE(devvp, flags, NOCRED, l);
    474 			VOP_UNLOCK(devvp, 0);
    475 			goto fail;
    476 		}
    477 
    478 		ump = VFSTOUFS(mp);
    479 		fs = ump->um_lfs;
    480 	} else {
    481 		/*
    482 		 * Update the mount.
    483 		 */
    484 
    485 		/*
    486 		 * The initial mount got a reference on this
    487 		 * device, so drop the one obtained via
    488 		 * namei(), above.
    489 		 */
    490 		vrele(devvp);
    491 
    492 		ump = VFSTOUFS(mp);
    493 		fs = ump->um_lfs;
    494 		if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    495 			/*
    496 			 * Changing from read-only to read/write.
    497 			 * Note in the superblocks that we're writing.
    498 			 */
    499 			fs->lfs_ronly = 0;
    500 			if (fs->lfs_pflags & LFS_PF_CLEAN) {
    501 				fs->lfs_pflags &= ~LFS_PF_CLEAN;
    502 				lfs_writesuper(fs, fs->lfs_sboffs[0]);
    503 				lfs_writesuper(fs, fs->lfs_sboffs[1]);
    504 			}
    505 		}
    506 		if (args.fspec == NULL)
    507 			return EINVAL;
    508 	}
    509 
    510 	error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    511 	    UIO_USERSPACE, mp, l);
    512 	if (error == 0)
    513 		(void)strncpy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname,
    514 			      sizeof(fs->lfs_fsmnt));
    515 	return error;
    516 
    517 fail:
    518 	vrele(devvp);
    519 	return (error);
    520 }
    521 
    522 
    523 /*
    524  * Common code for mount and mountroot
    525  * LFS specific
    526  */
    527 int
    528 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    529 {
    530 	struct dlfs *tdfs, *dfs, *adfs;
    531 	struct lfs *fs;
    532 	struct ufsmount *ump;
    533 	struct vnode *vp;
    534 	struct buf *bp, *abp;
    535 	struct partinfo dpart;
    536 	dev_t dev;
    537 	int error, i, ronly, secsize, fsbsize;
    538 	kauth_cred_t cred;
    539 	CLEANERINFO *cip;
    540 	SEGUSE *sup;
    541 	daddr_t sb_addr;
    542 
    543 	cred = l ? l->l_cred : NOCRED;
    544 
    545 	/*
    546 	 * Flush out any old buffers remaining from a previous use.
    547 	 */
    548 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    549 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    550 	VOP_UNLOCK(devvp, 0);
    551 	if (error)
    552 		return (error);
    553 
    554 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    555 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, l) != 0)
    556 		secsize = DEV_BSIZE;
    557 	else
    558 		secsize = dpart.disklab->d_secsize;
    559 
    560 	/* Don't free random space on error. */
    561 	bp = NULL;
    562 	abp = NULL;
    563 	ump = NULL;
    564 
    565 	sb_addr = LFS_LABELPAD / secsize;
    566 	while (1) {
    567 		/* Read in the superblock. */
    568 		error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
    569 		if (error)
    570 			goto out;
    571 		dfs = (struct dlfs *)bp->b_data;
    572 
    573 		/* Check the basics. */
    574 		if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
    575 		    dfs->dlfs_version > LFS_VERSION ||
    576 		    dfs->dlfs_bsize < sizeof(struct dlfs)) {
    577 			DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
    578 			error = EINVAL;		/* XXX needs translation */
    579 			goto out;
    580 		}
    581 		if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT) {
    582 			DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
    583 			       dfs->dlfs_inodefmt));
    584 			error = EINVAL;
    585 			goto out;
    586 		}
    587 
    588 		if (dfs->dlfs_version == 1)
    589 			fsbsize = secsize;
    590 		else {
    591 			fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
    592 				dfs->dlfs_fsbtodb);
    593 			/*
    594 			 * Could be, if the frag size is large enough, that we
    595 			 * don't have the "real" primary superblock.  If that's
    596 			 * the case, get the real one, and try again.
    597 			 */
    598 			if (sb_addr != dfs->dlfs_sboffs[0] <<
    599 				       dfs->dlfs_fsbtodb) {
    600 				DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
    601 				      " 0x%llx is not right, trying 0x%llx\n",
    602 				      (long long)sb_addr,
    603 				      (long long)(dfs->dlfs_sboffs[0] <<
    604 						  dfs->dlfs_fsbtodb)));
    605 				sb_addr = dfs->dlfs_sboffs[0] <<
    606 					  dfs->dlfs_fsbtodb;
    607 				brelse(bp);
    608 				continue;
    609 			}
    610 		}
    611 		break;
    612 	}
    613 
    614 	/*
    615 	 * Check the second superblock to see which is newer; then mount
    616 	 * using the older of the two.	This is necessary to ensure that
    617 	 * the filesystem is valid if it was not unmounted cleanly.
    618 	 */
    619 
    620 	if (dfs->dlfs_sboffs[1] &&
    621 	    dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
    622 	{
    623 		error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
    624 			LFS_SBPAD, cred, &abp);
    625 		if (error)
    626 			goto out;
    627 		adfs = (struct dlfs *)abp->b_data;
    628 
    629 		if (dfs->dlfs_version == 1) {
    630 			/* 1s resolution comparison */
    631 			if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
    632 				tdfs = adfs;
    633 			else
    634 				tdfs = dfs;
    635 		} else {
    636 			/* monotonic infinite-resolution comparison */
    637 			if (adfs->dlfs_serial < dfs->dlfs_serial)
    638 				tdfs = adfs;
    639 			else
    640 				tdfs = dfs;
    641 		}
    642 
    643 		/* Check the basics. */
    644 		if (tdfs->dlfs_magic != LFS_MAGIC ||
    645 		    tdfs->dlfs_bsize > MAXBSIZE ||
    646 		    tdfs->dlfs_version > LFS_VERSION ||
    647 		    tdfs->dlfs_bsize < sizeof(struct dlfs)) {
    648 			DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
    649 			      " sanity failed\n"));
    650 			error = EINVAL;		/* XXX needs translation */
    651 			goto out;
    652 		}
    653 	} else {
    654 		DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock"
    655 		      " daddr=0x%x\n", dfs->dlfs_sboffs[1]));
    656 		error = EINVAL;
    657 		goto out;
    658 	}
    659 
    660 	/* Allocate the mount structure, copy the superblock into it. */
    661 	fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
    662 	memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
    663 
    664 	/* Compatibility */
    665 	if (fs->lfs_version < 2) {
    666 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    667 		fs->lfs_ibsize = fs->lfs_bsize;
    668 		fs->lfs_start = fs->lfs_sboffs[0];
    669 		fs->lfs_tstamp = fs->lfs_otstamp;
    670 		fs->lfs_fsbtodb = 0;
    671 	}
    672 	if (fs->lfs_resvseg == 0)
    673 		fs->lfs_resvseg = MIN(fs->lfs_minfreeseg - 1, \
    674 			MAX(MIN_RESV_SEGS, fs->lfs_minfreeseg / 2 + 1));
    675 
    676 	/*
    677 	 * If we aren't going to be able to write meaningfully to this
    678 	 * filesystem, and were not mounted readonly, bomb out now.
    679 	 */
    680 	if (fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
    681 		DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
    682 		      " we need BUFPAGES >= %lld\n",
    683 		      (long long)((bufmem_hiwater / bufmem_lowater) *
    684 				  LFS_INVERSE_MAX_BYTES(
    685 					  fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
    686 		free(fs, M_UFSMNT);
    687 		error = EFBIG; /* XXX needs translation */
    688 		goto out;
    689 	}
    690 
    691 	/* Before rolling forward, lock so vget will sleep for other procs */
    692 	if (l != NULL) {
    693 		fs->lfs_flags = LFS_NOTYET;
    694 		fs->lfs_rfpid = l->l_proc->p_pid;
    695 	}
    696 
    697 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
    698 	ump->um_lfs = fs;
    699 	ump->um_ops = &lfs_ufsops;
    700 	ump->um_fstype = UFS1;
    701 	if (sizeof(struct lfs) < LFS_SBPAD) {			/* XXX why? */
    702 		bp->b_flags |= B_INVAL;
    703 		abp->b_flags |= B_INVAL;
    704 	}
    705 	brelse(bp);
    706 	bp = NULL;
    707 	brelse(abp);
    708 	abp = NULL;
    709 
    710 	/* Set up the I/O information */
    711 	fs->lfs_devbsize = secsize;
    712 	fs->lfs_iocount = 0;
    713 	fs->lfs_diropwait = 0;
    714 	fs->lfs_activesb = 0;
    715 	fs->lfs_uinodes = 0;
    716 	fs->lfs_ravail = 0;
    717 	fs->lfs_favail = 0;
    718 	fs->lfs_sbactive = 0;
    719 
    720 	/* Set up the ifile and lock aflags */
    721 	fs->lfs_doifile = 0;
    722 	fs->lfs_writer = 0;
    723 	fs->lfs_dirops = 0;
    724 	fs->lfs_nadirop = 0;
    725 	fs->lfs_seglock = 0;
    726 	fs->lfs_pdflush = 0;
    727 	fs->lfs_sleepers = 0;
    728 	fs->lfs_pages = 0;
    729 	simple_lock_init(&fs->lfs_interlock);
    730 	lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
    731 	lockinit(&fs->lfs_iflock, PINOD, "lfs_iflock", 0, 0);
    732 	lockinit(&fs->lfs_stoplock, PINOD, "lfs_stoplock", 0, 0);
    733 
    734 	/* Set the file system readonly/modify bits. */
    735 	fs->lfs_ronly = ronly;
    736 	if (ronly == 0)
    737 		fs->lfs_fmod = 1;
    738 
    739 	/* Initialize the mount structure. */
    740 	dev = devvp->v_rdev;
    741 	mp->mnt_data = ump;
    742 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    743 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
    744 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    745 	mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
    746 	mp->mnt_stat.f_iosize = fs->lfs_bsize;
    747 	mp->mnt_flag |= MNT_LOCAL;
    748 	mp->mnt_fs_bshift = fs->lfs_bshift;
    749 	ump->um_flags = 0;
    750 	ump->um_mountp = mp;
    751 	ump->um_dev = dev;
    752 	ump->um_devvp = devvp;
    753 	ump->um_bptrtodb = fs->lfs_fsbtodb;
    754 	ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
    755 	ump->um_nindir = fs->lfs_nindir;
    756 	ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
    757 	for (i = 0; i < MAXQUOTAS; i++)
    758 		ump->um_quotas[i] = NULLVP;
    759 	ump->um_maxsymlinklen = fs->lfs_maxsymlinklen;
    760 	ump->um_dirblksiz = DIRBLKSIZ;
    761 	ump->um_maxfilesize = fs->lfs_maxfilesize;
    762 	if (ump->um_maxsymlinklen > 0)
    763 		mp->mnt_iflag |= IMNT_DTYPE;
    764 	devvp->v_specmountpoint = mp;
    765 
    766 	/* Set up reserved memory for pageout */
    767 	lfs_setup_resblks(fs);
    768 	/* Set up vdirop tailq */
    769 	TAILQ_INIT(&fs->lfs_dchainhd);
    770 	/* and paging tailq */
    771 	TAILQ_INIT(&fs->lfs_pchainhd);
    772 	/* and delayed segment accounting for truncation list */
    773 	LIST_INIT(&fs->lfs_segdhd);
    774 
    775 	/*
    776 	 * We use the ifile vnode for almost every operation.  Instead of
    777 	 * retrieving it from the hash table each time we retrieve it here,
    778 	 * artificially increment the reference count and keep a pointer
    779 	 * to it in the incore copy of the superblock.
    780 	 */
    781 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
    782 		DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
    783 		goto out;
    784 	}
    785 	fs->lfs_ivnode = vp;
    786 	VREF(vp);
    787 
    788 	/* Set up inode bitmap and order free list */
    789 	lfs_order_freelist(fs);
    790 
    791 	/* Set up segment usage flags for the autocleaner. */
    792 	fs->lfs_nactive = 0;
    793 	fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
    794 						M_SEGMENT, M_WAITOK);
    795 	fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
    796 						 M_SEGMENT, M_WAITOK);
    797 	fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
    798 						 M_SEGMENT, M_WAITOK);
    799 	memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
    800 	for (i = 0; i < fs->lfs_nseg; i++) {
    801 		int changed;
    802 
    803 		LFS_SEGENTRY(sup, fs, i, bp);
    804 		changed = 0;
    805 		if (!ronly) {
    806 			if (sup->su_nbytes == 0 &&
    807 			    !(sup->su_flags & SEGUSE_EMPTY)) {
    808 				sup->su_flags |= SEGUSE_EMPTY;
    809 				++changed;
    810 			} else if (!(sup->su_nbytes == 0) &&
    811 				   (sup->su_flags & SEGUSE_EMPTY)) {
    812 				sup->su_flags &= ~SEGUSE_EMPTY;
    813 				++changed;
    814 			}
    815 			if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
    816 				sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
    817 				++changed;
    818 			}
    819 		}
    820 		fs->lfs_suflags[0][i] = sup->su_flags;
    821 		if (changed)
    822 			LFS_WRITESEGENTRY(sup, fs, i, bp);
    823 		else
    824 			brelse(bp);
    825 	}
    826 
    827 #ifdef LFS_KERNEL_RFW
    828 	lfs_roll_forward(fs, mp, l);
    829 #endif
    830 
    831 	/* If writing, sb is not clean; record in case of immediate crash */
    832 	if (!fs->lfs_ronly) {
    833 		fs->lfs_pflags &= ~LFS_PF_CLEAN;
    834 		lfs_writesuper(fs, fs->lfs_sboffs[0]);
    835 		lfs_writesuper(fs, fs->lfs_sboffs[1]);
    836 	}
    837 
    838 	/* Allow vget now that roll-forward is complete */
    839 	fs->lfs_flags &= ~(LFS_NOTYET);
    840 	wakeup(&fs->lfs_flags);
    841 
    842 	/*
    843 	 * Initialize the ifile cleaner info with information from
    844 	 * the superblock.
    845 	 */
    846 	LFS_CLEANERINFO(cip, fs, bp);
    847 	cip->clean = fs->lfs_nclean;
    848 	cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
    849 	cip->avail = fs->lfs_avail;
    850 	cip->bfree = fs->lfs_bfree;
    851 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    852 
    853 	/*
    854 	 * Mark the current segment as ACTIVE, since we're going to
    855 	 * be writing to it.
    856 	 */
    857 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
    858 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    859 	fs->lfs_nactive++;
    860 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);  /* Ifile */
    861 
    862 	/* Now that roll-forward is done, unlock the Ifile */
    863 	vput(vp);
    864 
    865 	/* Start the pagedaemon-anticipating daemon */
    866 	if (lfs_writer_daemon == 0 &&
    867 	    kthread_create1(lfs_writerd, NULL, NULL, "lfs_writer") != 0)
    868 		panic("fork lfs_writer");
    869 
    870 	return (0);
    871 
    872 out:
    873 	if (bp)
    874 		brelse(bp);
    875 	if (abp)
    876 		brelse(abp);
    877 	if (ump) {
    878 		free(ump->um_lfs, M_UFSMNT);
    879 		free(ump, M_UFSMNT);
    880 		mp->mnt_data = NULL;
    881 	}
    882 
    883 	return (error);
    884 }
    885 
    886 /*
    887  * unmount system call
    888  */
    889 int
    890 lfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    891 {
    892 	struct ufsmount *ump;
    893 	struct lfs *fs;
    894 	int error, flags, ronly;
    895 	int s;
    896 
    897 	flags = 0;
    898 	if (mntflags & MNT_FORCE)
    899 		flags |= FORCECLOSE;
    900 
    901 	ump = VFSTOUFS(mp);
    902 	fs = ump->um_lfs;
    903 
    904 	/* Two checkpoints */
    905 	lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
    906 	lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
    907 
    908 	/* wake up the cleaner so it can die */
    909 	lfs_wakeup_cleaner(fs);
    910 	simple_lock(&fs->lfs_interlock);
    911 	while (fs->lfs_sleepers)
    912 		ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
    913 			&fs->lfs_interlock);
    914 	simple_unlock(&fs->lfs_interlock);
    915 
    916 #ifdef QUOTA
    917 	if (mp->mnt_flag & MNT_QUOTA) {
    918 		int i;
    919 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
    920 		if (error)
    921 			return (error);
    922 		for (i = 0; i < MAXQUOTAS; i++) {
    923 			if (ump->um_quotas[i] == NULLVP)
    924 				continue;
    925 			quotaoff(l, mp, i);
    926 		}
    927 		/*
    928 		 * Here we fall through to vflush again to ensure
    929 		 * that we have gotten rid of all the system vnodes.
    930 		 */
    931 	}
    932 #endif
    933 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
    934 		return (error);
    935 	if ((error = VFS_SYNC(mp, 1, l->l_cred, l)) != 0)
    936 		return (error);
    937 	s = splbio();
    938 	if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
    939 		panic("lfs_unmount: still dirty blocks on ifile vnode");
    940 	splx(s);
    941 
    942 	/* Explicitly write the superblock, to update serial and pflags */
    943 	fs->lfs_pflags |= LFS_PF_CLEAN;
    944 	lfs_writesuper(fs, fs->lfs_sboffs[0]);
    945 	lfs_writesuper(fs, fs->lfs_sboffs[1]);
    946 	simple_lock(&fs->lfs_interlock);
    947 	while (fs->lfs_iocount)
    948 		ltsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
    949 			&fs->lfs_interlock);
    950 	simple_unlock(&fs->lfs_interlock);
    951 
    952 	/* Finish with the Ifile, now that we're done with it */
    953 	vrele(fs->lfs_ivnode);
    954 	vgone(fs->lfs_ivnode);
    955 
    956 	ronly = !fs->lfs_ronly;
    957 	if (ump->um_devvp->v_type != VBAD)
    958 		ump->um_devvp->v_specmountpoint = NULL;
    959 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    960 	error = VOP_CLOSE(ump->um_devvp,
    961 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, l);
    962 	vput(ump->um_devvp);
    963 
    964 	/* Complain about page leakage */
    965 	if (fs->lfs_pages > 0)
    966 		printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
    967 			fs->lfs_pages, lfs_subsys_pages);
    968 
    969 	/* Free per-mount data structures */
    970 	free(fs->lfs_ino_bitmap, M_SEGMENT);
    971 	free(fs->lfs_suflags[0], M_SEGMENT);
    972 	free(fs->lfs_suflags[1], M_SEGMENT);
    973 	free(fs->lfs_suflags, M_SEGMENT);
    974 	lfs_free_resblks(fs);
    975 	free(fs, M_UFSMNT);
    976 	free(ump, M_UFSMNT);
    977 
    978 	mp->mnt_data = NULL;
    979 	mp->mnt_flag &= ~MNT_LOCAL;
    980 	return (error);
    981 }
    982 
    983 /*
    984  * Get file system statistics.
    985  *
    986  * NB: We don't lock to access the superblock here, because it's not
    987  * really that important if we get it wrong.
    988  */
    989 int
    990 lfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l __unused)
    991 {
    992 	struct lfs *fs;
    993 	struct ufsmount *ump;
    994 
    995 	ump = VFSTOUFS(mp);
    996 	fs = ump->um_lfs;
    997 	if (fs->lfs_magic != LFS_MAGIC)
    998 		panic("lfs_statvfs: magic");
    999 
   1000 	sbp->f_bsize = fs->lfs_bsize;
   1001 	sbp->f_frsize = fs->lfs_fsize;
   1002 	sbp->f_iosize = fs->lfs_bsize;
   1003 	sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks);
   1004 
   1005 	sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
   1006 	KASSERT(sbp->f_bfree <= fs->lfs_dsize);
   1007 #if 0
   1008 	if (sbp->f_bfree < 0)
   1009 		sbp->f_bfree = 0;
   1010 #endif
   1011 
   1012 	sbp->f_bresvd = fsbtofrags(fs, LFS_EST_RSVD(fs));
   1013 	if (sbp->f_bfree > sbp->f_bresvd)
   1014 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1015 	else
   1016 		sbp->f_bavail = 0;
   1017 
   1018 	sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
   1019 	sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
   1020 	sbp->f_favail = sbp->f_ffree;
   1021 	sbp->f_fresvd = 0;
   1022 	copy_statvfs_info(sbp, mp);
   1023 	return (0);
   1024 }
   1025 
   1026 /*
   1027  * Go through the disk queues to initiate sandbagged IO;
   1028  * go through the inodes to write those that have been modified;
   1029  * initiate the writing of the super block if it has been modified.
   1030  *
   1031  * Note: we are always called with the filesystem marked `MPBUSY'.
   1032  */
   1033 int
   1034 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred __unused,
   1035     struct lwp *l __unused)
   1036 {
   1037 	int error;
   1038 	struct lfs *fs;
   1039 
   1040 	fs = VFSTOUFS(mp)->um_lfs;
   1041 	if (fs->lfs_ronly)
   1042 		return 0;
   1043 
   1044 	/* Snapshots should not hose the syncer */
   1045 	/*
   1046 	 * XXX Sync can block here anyway, since we don't have a very
   1047 	 * XXX good idea of how much data is pending.  If it's more
   1048 	 * XXX than a segment and lfs_nextseg is close to the end of
   1049 	 * XXX the log, we'll likely block.
   1050 	 */
   1051 	simple_lock(&fs->lfs_interlock);
   1052 	if (fs->lfs_nowrap && fs->lfs_nextseg < fs->lfs_curseg) {
   1053 		simple_unlock(&fs->lfs_interlock);
   1054 		return 0;
   1055 	}
   1056 	simple_unlock(&fs->lfs_interlock);
   1057 
   1058 	lfs_writer_enter(fs, "lfs_dirops");
   1059 
   1060 	/* All syncs must be checkpoints until roll-forward is implemented. */
   1061 	DLOG((DLOG_FLUSH, "lfs_sync at 0x%x\n", fs->lfs_offset));
   1062 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
   1063 	lfs_writer_leave(fs);
   1064 #ifdef QUOTA
   1065 	qsync(mp);
   1066 #endif
   1067 	return (error);
   1068 }
   1069 
   1070 extern struct lock ufs_hashlock;
   1071 
   1072 /*
   1073  * Look up an LFS dinode number to find its incore vnode.  If not already
   1074  * in core, read it in from the specified device.  Return the inode locked.
   1075  * Detection and handling of mount points must be done by the calling routine.
   1076  */
   1077 int
   1078 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1079 {
   1080 	struct lfs *fs;
   1081 	struct ufs1_dinode *dip;
   1082 	struct inode *ip;
   1083 	struct buf *bp;
   1084 	struct ifile *ifp;
   1085 	struct vnode *vp;
   1086 	struct ufsmount *ump;
   1087 	daddr_t daddr;
   1088 	dev_t dev;
   1089 	int error, retries;
   1090 	struct timespec ts;
   1091 
   1092 	memset(&ts, 0, sizeof ts);	/* XXX gcc */
   1093 
   1094 	ump = VFSTOUFS(mp);
   1095 	dev = ump->um_dev;
   1096 	fs = ump->um_lfs;
   1097 
   1098 	/*
   1099 	 * If the filesystem is not completely mounted yet, suspend
   1100 	 * any access requests (wait for roll-forward to complete).
   1101 	 */
   1102 	simple_lock(&fs->lfs_interlock);
   1103 	while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
   1104 		ltsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
   1105 			&fs->lfs_interlock);
   1106 	simple_unlock(&fs->lfs_interlock);
   1107 
   1108 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1109 		return (0);
   1110 
   1111 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1112 		*vpp = NULL;
   1113 		 return (error);
   1114 	}
   1115 
   1116 	do {
   1117 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1118 			ungetnewvnode(vp);
   1119 			return (0);
   1120 		}
   1121 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1122 
   1123 	/* Translate the inode number to a disk address. */
   1124 	if (ino == LFS_IFILE_INUM)
   1125 		daddr = fs->lfs_idaddr;
   1126 	else {
   1127 		/* XXX bounds-check this too */
   1128 		LFS_IENTRY(ifp, fs, ino, bp);
   1129 		daddr = ifp->if_daddr;
   1130 		if (fs->lfs_version > 1) {
   1131 			ts.tv_sec = ifp->if_atime_sec;
   1132 			ts.tv_nsec = ifp->if_atime_nsec;
   1133 		}
   1134 
   1135 		brelse(bp);
   1136 		if (daddr == LFS_UNUSED_DADDR) {
   1137 			*vpp = NULLVP;
   1138 			ungetnewvnode(vp);
   1139 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1140 			return (ENOENT);
   1141 		}
   1142 	}
   1143 
   1144 	/* Allocate/init new vnode/inode. */
   1145 	lfs_vcreate(mp, ino, vp);
   1146 
   1147 	/*
   1148 	 * Put it onto its hash chain and lock it so that other requests for
   1149 	 * this inode will block if they arrive while we are sleeping waiting
   1150 	 * for old data structures to be purged or for the contents of the
   1151 	 * disk portion of this inode to be read.
   1152 	 */
   1153 	ip = VTOI(vp);
   1154 	ufs_ihashins(ip);
   1155 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1156 
   1157 	/*
   1158 	 * XXX
   1159 	 * This may not need to be here, logically it should go down with
   1160 	 * the i_devvp initialization.
   1161 	 * Ask Kirk.
   1162 	 */
   1163 	ip->i_lfs = ump->um_lfs;
   1164 
   1165 	/* Read in the disk contents for the inode, copy into the inode. */
   1166 	retries = 0;
   1167     again:
   1168 	error = bread(ump->um_devvp, fsbtodb(fs, daddr),
   1169 		(fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
   1170 		NOCRED, &bp);
   1171 	if (error) {
   1172 		/*
   1173 		 * The inode does not contain anything useful, so it would
   1174 		 * be misleading to leave it on its hash chain. With mode
   1175 		 * still zero, it will be unlinked and returned to the free
   1176 		 * list by vput().
   1177 		 */
   1178 		vput(vp);
   1179 		brelse(bp);
   1180 		*vpp = NULL;
   1181 		return (error);
   1182 	}
   1183 
   1184 	dip = lfs_ifind(fs, ino, bp);
   1185 	if (dip == NULL) {
   1186 		/* Assume write has not completed yet; try again */
   1187 		bp->b_flags |= B_INVAL;
   1188 		brelse(bp);
   1189 		++retries;
   1190 		if (retries > LFS_IFIND_RETRIES) {
   1191 #ifdef DEBUG
   1192 			/* If the seglock is held look at the bpp to see
   1193 			   what is there anyway */
   1194 			simple_lock(&fs->lfs_interlock);
   1195 			if (fs->lfs_seglock > 0) {
   1196 				struct buf **bpp;
   1197 				struct ufs1_dinode *dp;
   1198 				int i;
   1199 
   1200 				for (bpp = fs->lfs_sp->bpp;
   1201 				     bpp != fs->lfs_sp->cbpp; ++bpp) {
   1202 					if ((*bpp)->b_vp == fs->lfs_ivnode &&
   1203 					    bpp != fs->lfs_sp->bpp) {
   1204 						/* Inode block */
   1205 						printf("lfs_vget: block 0x%" PRIx64 ": ",
   1206 						       (*bpp)->b_blkno);
   1207 						dp = (struct ufs1_dinode *)(*bpp)->b_data;
   1208 						for (i = 0; i < INOPB(fs); i++)
   1209 							if (dp[i].di_u.inumber)
   1210 								printf("%d ", dp[i].di_u.inumber);
   1211 						printf("\n");
   1212 					}
   1213 				}
   1214 			}
   1215 			simple_unlock(&fs->lfs_interlock);
   1216 #endif /* DEBUG */
   1217 			panic("lfs_vget: dinode not found");
   1218 		}
   1219 		simple_lock(&fs->lfs_interlock);
   1220 		if (fs->lfs_iocount) {
   1221 			DLOG((DLOG_VNODE, "lfs_vget: dinode %d not found, retrying...\n", ino));
   1222 			(void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
   1223 				      "lfs ifind", 1, &fs->lfs_interlock);
   1224 		} else
   1225 			retries = LFS_IFIND_RETRIES;
   1226 		simple_unlock(&fs->lfs_interlock);
   1227 		goto again;
   1228 	}
   1229 	*ip->i_din.ffs1_din = *dip;
   1230 	brelse(bp);
   1231 
   1232 	if (fs->lfs_version > 1) {
   1233 		ip->i_ffs1_atime = ts.tv_sec;
   1234 		ip->i_ffs1_atimensec = ts.tv_nsec;
   1235 	}
   1236 
   1237 	lfs_vinit(mp, &vp);
   1238 
   1239 	*vpp = vp;
   1240 
   1241 	KASSERT(VOP_ISLOCKED(vp));
   1242 
   1243 	return (0);
   1244 }
   1245 
   1246 /*
   1247  * File handle to vnode
   1248  */
   1249 int
   1250 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1251 {
   1252 	struct lfid lfh;
   1253 	struct buf *bp;
   1254 	IFILE *ifp;
   1255 	int32_t daddr;
   1256 	struct lfs *fs;
   1257 
   1258 	if (fhp->fid_len != sizeof(struct lfid))
   1259 		return EINVAL;
   1260 
   1261 	memcpy(&lfh, fhp, sizeof(lfh));
   1262 	if (lfh.lfid_ino < LFS_IFILE_INUM)
   1263 		return ESTALE;
   1264 
   1265 	fs = VFSTOUFS(mp)->um_lfs;
   1266 	if (lfh.lfid_ident != fs->lfs_ident)
   1267 		return ESTALE;
   1268 
   1269 	if (lfh.lfid_ino >
   1270 	    ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
   1271 	     fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
   1272 		return ESTALE;
   1273 
   1274 	if (ufs_ihashlookup(VFSTOUFS(mp)->um_dev, lfh.lfid_ino) == NULLVP) {
   1275 		LFS_IENTRY(ifp, fs, lfh.lfid_ino, bp);
   1276 		daddr = ifp->if_daddr;
   1277 		brelse(bp);
   1278 		if (daddr == LFS_UNUSED_DADDR)
   1279 			return ESTALE;
   1280 	}
   1281 
   1282 	return (ufs_fhtovp(mp, &lfh.lfid_ufid, vpp));
   1283 }
   1284 
   1285 /*
   1286  * Vnode pointer to File handle
   1287  */
   1288 /* ARGSUSED */
   1289 int
   1290 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1291 {
   1292 	struct inode *ip;
   1293 	struct lfid lfh;
   1294 
   1295 	if (*fh_size < sizeof(struct lfid)) {
   1296 		*fh_size = sizeof(struct lfid);
   1297 		return E2BIG;
   1298 	}
   1299 	*fh_size = sizeof(struct lfid);
   1300 	ip = VTOI(vp);
   1301 	memset(&lfh, 0, sizeof(lfh));
   1302 	lfh.lfid_len = sizeof(struct lfid);
   1303 	lfh.lfid_ino = ip->i_number;
   1304 	lfh.lfid_gen = ip->i_gen;
   1305 	lfh.lfid_ident = ip->i_lfs->lfs_ident;
   1306 	memcpy(fhp, &lfh, sizeof(lfh));
   1307 	return (0);
   1308 }
   1309 
   1310 static int
   1311 sysctl_lfs_dostats(SYSCTLFN_ARGS)
   1312 {
   1313 	extern struct lfs_stats lfs_stats;
   1314 	extern int lfs_dostats;
   1315 	int error;
   1316 
   1317 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1318 	if (error || newp == NULL)
   1319 		return (error);
   1320 
   1321 	if (lfs_dostats == 0)
   1322 		memset(&lfs_stats, 0, sizeof(lfs_stats));
   1323 
   1324 	return (0);
   1325 }
   1326 
   1327 struct shortlong {
   1328 	const char *sname;
   1329 	const char *lname;
   1330 };
   1331 
   1332 SYSCTL_SETUP(sysctl_vfs_lfs_setup, "sysctl vfs.lfs subtree setup")
   1333 {
   1334 	int i;
   1335 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
   1336 		   lfs_fs_pagetrip;
   1337 #ifdef DEBUG
   1338 	extern int lfs_debug_log_subsys[DLOG_MAX];
   1339 	struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
   1340 		{ "rollforward", "Debug roll-forward code" },
   1341 		{ "alloc",	"Debug inode allocation and free list" },
   1342 		{ "avail",	"Debug space-available-now accounting" },
   1343 		{ "flush",	"Debug flush triggers" },
   1344 		{ "lockedlist",	"Debug locked list accounting" },
   1345 		{ "vnode_verbose", "Verbose per-vnode-written debugging" },
   1346 		{ "vnode",	"Debug vnode use during segment write" },
   1347 		{ "segment",	"Debug segment writing" },
   1348 		{ "seguse",	"Debug segment used-bytes accounting" },
   1349 		{ "cleaner",	"Debug cleaning routines" },
   1350 		{ "mount",	"Debug mount/unmount routines" },
   1351 		{ "pagecache",	"Debug UBC interactions" },
   1352 		{ "dirop",	"Debug directory-operation accounting" },
   1353 		{ "malloc",	"Debug private malloc accounting" },
   1354 	};
   1355 #endif /* DEBUG */
   1356 	struct shortlong stat_names[] = { /* Must match lfs.h! */
   1357 		{ "segsused",	    "Number of new segments allocated" },
   1358 		{ "psegwrites",	    "Number of partial-segment writes" },
   1359 		{ "psyncwrites",    "Number of synchronous partial-segment"
   1360 				    " writes" },
   1361 		{ "pcleanwrites",   "Number of partial-segment writes by the"
   1362 				    " cleaner" },
   1363 		{ "blocktot",       "Number of blocks written" },
   1364 		{ "cleanblocks",    "Number of blocks written by the cleaner" },
   1365 		{ "ncheckpoints",   "Number of checkpoints made" },
   1366 		{ "nwrites",        "Number of whole writes" },
   1367 		{ "nsync_writes",   "Number of synchronous writes" },
   1368 		{ "wait_exceeded",  "Number of times writer waited for"
   1369 				    " cleaner" },
   1370 		{ "write_exceeded", "Number of times writer invoked flush" },
   1371 		{ "flush_invoked",  "Number of times flush was invoked" },
   1372 		{ "vflush_invoked", "Number of time vflush was called" },
   1373 		{ "clean_inlocked", "Number of vnodes skipped for VXLOCK" },
   1374 		{ "clean_vnlocked", "Number of vnodes skipped for vget failure" },
   1375 		{ "segs_reclaimed", "Number of segments reclaimed" },
   1376 	};
   1377 
   1378 	sysctl_createv(clog, 0, NULL, NULL,
   1379 		       CTLFLAG_PERMANENT,
   1380 		       CTLTYPE_NODE, "vfs", NULL,
   1381 		       NULL, 0, NULL, 0,
   1382 		       CTL_VFS, CTL_EOL);
   1383 	sysctl_createv(clog, 0, NULL, NULL,
   1384 		       CTLFLAG_PERMANENT,
   1385 		       CTLTYPE_NODE, "lfs",
   1386 		       SYSCTL_DESCR("Log-structured file system"),
   1387 		       NULL, 0, NULL, 0,
   1388 		       CTL_VFS, 5, CTL_EOL);
   1389 	/*
   1390 	 * XXX the "5" above could be dynamic, thereby eliminating one
   1391 	 * more instance of the "number to vfs" mapping problem, but
   1392 	 * "5" is the order as taken from sys/mount.h
   1393 	 */
   1394 
   1395 	sysctl_createv(clog, 0, NULL, NULL,
   1396 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1397 		       CTLTYPE_INT, "flushindir", NULL,
   1398 		       NULL, 0, &lfs_writeindir, 0,
   1399 		       CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
   1400 	sysctl_createv(clog, 0, NULL, NULL,
   1401 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1402 		       CTLTYPE_INT, "clean_vnhead", NULL,
   1403 		       NULL, 0, &lfs_clean_vnhead, 0,
   1404 		       CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
   1405 	sysctl_createv(clog, 0, NULL, NULL,
   1406 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1407 		       CTLTYPE_INT, "dostats",
   1408 		       SYSCTL_DESCR("Maintain statistics on LFS operations"),
   1409 		       sysctl_lfs_dostats, 0, &lfs_dostats, 0,
   1410 		       CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
   1411 	sysctl_createv(clog, 0, NULL, NULL,
   1412 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1413 		       CTLTYPE_INT, "pagetrip",
   1414 		       SYSCTL_DESCR("How many dirty pages in fs triggers"
   1415 				    " a flush"),
   1416 		       NULL, 0, &lfs_fs_pagetrip, 0,
   1417 		       CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
   1418 #ifdef LFS_KERNEL_RFW
   1419 	sysctl_createv(clog, 0, NULL, NULL,
   1420 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1421 		       CTLTYPE_INT, "rfw",
   1422 		       SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
   1423 		       NULL, 0, &lfs_do_rfw, 0,
   1424 		       CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
   1425 #endif
   1426 
   1427 	sysctl_createv(clog, 0, NULL, NULL,
   1428 		       CTLFLAG_PERMANENT,
   1429 		       CTLTYPE_NODE, "stats",
   1430 		       SYSCTL_DESCR("Debugging options"),
   1431 		       NULL, 0, NULL, 0,
   1432 		       CTL_VFS, 5, LFS_STATS, CTL_EOL);
   1433 	for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
   1434 		sysctl_createv(clog, 0, NULL, NULL,
   1435 			       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1436 			       CTLTYPE_INT, stat_names[i].sname,
   1437 			       SYSCTL_DESCR(stat_names[i].lname),
   1438 			       NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
   1439 			       0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
   1440 	}
   1441 
   1442 #ifdef DEBUG
   1443 	sysctl_createv(clog, 0, NULL, NULL,
   1444 		       CTLFLAG_PERMANENT,
   1445 		       CTLTYPE_NODE, "debug",
   1446 		       SYSCTL_DESCR("Debugging options"),
   1447 		       NULL, 0, NULL, 0,
   1448 		       CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
   1449 	for (i = 0; i < DLOG_MAX; i++) {
   1450 		sysctl_createv(clog, 0, NULL, NULL,
   1451 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1452 			       CTLTYPE_INT, dlog_names[i].sname,
   1453 			       SYSCTL_DESCR(dlog_names[i].lname),
   1454 			       NULL, 0, &(lfs_debug_log_subsys[i]), 0,
   1455 			       CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
   1456 	}
   1457 #endif
   1458 }
   1459 
   1460 /*
   1461  * ufs_bmaparray callback function for writing.
   1462  *
   1463  * Since blocks will be written to the new segment anyway,
   1464  * we don't care about current daddr of them.
   1465  */
   1466 static boolean_t
   1467 lfs_issequential_hole(const struct ufsmount *ump __unused,
   1468     daddr_t daddr0, daddr_t daddr1)
   1469 {
   1470 	daddr0 = (daddr_t)((int32_t)daddr0); /* XXX ondisk32 */
   1471 	daddr1 = (daddr_t)((int32_t)daddr1); /* XXX ondisk32 */
   1472 
   1473 	KASSERT(daddr0 == UNWRITTEN ||
   1474 	    (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR));
   1475 	KASSERT(daddr1 == UNWRITTEN ||
   1476 	    (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR));
   1477 
   1478 	/* NOTE: all we want to know here is 'hole or not'. */
   1479 	/* NOTE: UNASSIGNED is converted to 0 by ufs_bmaparray. */
   1480 
   1481 	/*
   1482 	 * treat UNWRITTENs and all resident blocks as 'contiguous'
   1483 	 */
   1484 	if (daddr0 != 0 && daddr1 != 0)
   1485 		return TRUE;
   1486 
   1487 	/*
   1488 	 * both are in hole?
   1489 	 */
   1490 	if (daddr0 == 0 && daddr1 == 0)
   1491 		return TRUE; /* all holes are 'contiguous' for us. */
   1492 
   1493 	return FALSE;
   1494 }
   1495 
   1496 /*
   1497  * lfs_gop_write functions exactly like genfs_gop_write, except that
   1498  * (1) it requires the seglock to be held by its caller, and sp->fip
   1499  *     to be properly initialized (it will return without re-initializing
   1500  *     sp->fip, and without calling lfs_writeseg).
   1501  * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
   1502  *     to determine how large a block it can write at once (though it does
   1503  *     still use VOP_BMAP to find holes in the file);
   1504  * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
   1505  *     (leaving lfs_writeseg to deal with the cluster blocks, so we might
   1506  *     now have clusters of clusters, ick.)
   1507  */
   1508 static int
   1509 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1510     int flags __unused)
   1511 {
   1512 	int i, s, error, run, haveeof = 0;
   1513 	int fs_bshift;
   1514 	vaddr_t kva;
   1515 	off_t eof, offset, startoffset = 0;
   1516 	size_t bytes, iobytes, skipbytes;
   1517 	daddr_t lbn, blkno;
   1518 	struct vm_page *pg;
   1519 	struct buf *mbp, *bp;
   1520 	struct vnode *devvp = VTOI(vp)->i_devvp;
   1521 	struct inode *ip = VTOI(vp);
   1522 	struct lfs *fs = ip->i_lfs;
   1523 	struct segment *sp = fs->lfs_sp;
   1524 	UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
   1525 
   1526 	ASSERT_SEGLOCK(fs);
   1527 
   1528 	/* The Ifile lives in the buffer cache */
   1529 	KASSERT(vp != fs->lfs_ivnode);
   1530 
   1531 	/*
   1532 	 * We don't want to fill the disk before the cleaner has a chance
   1533 	 * to make room for us.  If we're in danger of doing that, fail
   1534 	 * with EAGAIN.  The caller will have to notice this, unlock
   1535 	 * so the cleaner can run, relock and try again.
   1536 	 *
   1537 	 * We must write everything, however, if our vnode is being
   1538 	 * reclaimed.
   1539 	 */
   1540 	if (LFS_STARVED_FOR_SEGS(fs) && vp != fs->lfs_flushvp)
   1541 		goto tryagain;
   1542 
   1543 	/*
   1544 	 * Sometimes things slip past the filters in lfs_putpages,
   1545 	 * and the pagedaemon tries to write pages---problem is
   1546 	 * that the pagedaemon never acquires the segment lock.
   1547 	 *
   1548 	 * Alternatively, pages that were clean when we called
   1549 	 * genfs_putpages may have become dirty in the meantime.  In this
   1550 	 * case the segment header is not properly set up for blocks
   1551 	 * to be added to it.
   1552 	 *
   1553 	 * Unbusy and unclean the pages, and put them on the ACTIVE
   1554 	 * queue under the hypothesis that they couldn't have got here
   1555 	 * unless they were modified *quite* recently.
   1556 	 *
   1557 	 * XXXUBC that last statement is an oversimplification of course.
   1558 	 */
   1559 	if (!LFS_SEGLOCK_HELD(fs) ||
   1560 	    (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) ||
   1561 	    (pgs[0]->offset & fs->lfs_bmask) != 0) {
   1562 		goto tryagain;
   1563 	}
   1564 
   1565 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1566 	    vp, pgs, npages, flags);
   1567 
   1568 	GOP_SIZE(vp, vp->v_size, &eof, 0);
   1569 	haveeof = 1;
   1570 
   1571 	if (vp->v_type == VREG)
   1572 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1573 	else
   1574 		fs_bshift = DEV_BSHIFT;
   1575 	error = 0;
   1576 	pg = pgs[0];
   1577 	startoffset = pg->offset;
   1578 	KASSERT(eof >= 0);
   1579 	if (startoffset >= eof) {
   1580 		goto tryagain;
   1581 	} else
   1582 		bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1583 	skipbytes = 0;
   1584 
   1585 	KASSERT(bytes != 0);
   1586 
   1587 	/* Swap PG_DELWRI for PG_PAGEOUT */
   1588 	for (i = 0; i < npages; i++)
   1589 		if (pgs[i]->flags & PG_DELWRI) {
   1590 			KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
   1591 			pgs[i]->flags &= ~PG_DELWRI;
   1592 			pgs[i]->flags |= PG_PAGEOUT;
   1593 			uvmexp.paging++;
   1594 			uvm_lock_pageq();
   1595 			uvm_pageunwire(pgs[i]);
   1596 			uvm_unlock_pageq();
   1597 		}
   1598 
   1599 	/*
   1600 	 * Check to make sure we're starting on a block boundary.
   1601 	 * We'll check later to make sure we always write entire
   1602 	 * blocks (or fragments).
   1603 	 */
   1604 	if (startoffset & fs->lfs_bmask)
   1605 		printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
   1606 		       startoffset, fs->lfs_bmask,
   1607 		       startoffset & fs->lfs_bmask);
   1608 	KASSERT((startoffset & fs->lfs_bmask) == 0);
   1609 	if (bytes & fs->lfs_ffmask) {
   1610 		printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
   1611 		panic("lfs_gop_write: non-integer blocks");
   1612 	}
   1613 
   1614 	/*
   1615 	 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
   1616 	 * If we would, write what we have and try again.  If we don't
   1617 	 * have anything to write, we'll have to sleep.
   1618 	 */
   1619 	if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   1620 				      (((SEGSUM *)(sp->segsum))->ss_nfinfo < 1 ?
   1621 				       UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
   1622 		DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
   1623 #if 0
   1624 		      " with nfinfo=%d at offset 0x%x\n",
   1625 		      (int)((SEGSUM *)(sp->segsum))->ss_nfinfo,
   1626 		      (unsigned)fs->lfs_offset));
   1627 #endif
   1628 		lfs_updatemeta(sp);
   1629 		lfs_release_finfo(fs);
   1630 		(void) lfs_writeseg(fs, sp);
   1631 
   1632 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   1633 
   1634 		/*
   1635 		 * Having given up all of the pager_map we were holding,
   1636 		 * we can now wait for aiodoned to reclaim it for us
   1637 		 * without fear of deadlock.
   1638 		 */
   1639 		kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   1640 				     UVMPAGER_MAPIN_WAITOK);
   1641 	}
   1642 
   1643 	s = splbio();
   1644 	simple_lock(&global_v_numoutput_slock);
   1645 	vp->v_numoutput += 2; /* one for biodone, one for aiodone */
   1646 	simple_unlock(&global_v_numoutput_slock);
   1647 	splx(s);
   1648 
   1649 	mbp = getiobuf();
   1650 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1651 	    vp, mbp, vp->v_numoutput, bytes);
   1652 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1653 	mbp->b_data = (void *)kva;
   1654 	mbp->b_resid = mbp->b_bcount = bytes;
   1655 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
   1656 	mbp->b_iodone = uvm_aio_biodone;
   1657 	mbp->b_vp = vp;
   1658 
   1659 	bp = NULL;
   1660 	for (offset = startoffset;
   1661 	    bytes > 0;
   1662 	    offset += iobytes, bytes -= iobytes) {
   1663 		lbn = offset >> fs_bshift;
   1664 		error = ufs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
   1665 		    lfs_issequential_hole);
   1666 		if (error) {
   1667 			UVMHIST_LOG(ubchist, "ufs_bmaparray() -> %d",
   1668 			    error,0,0,0);
   1669 			skipbytes += bytes;
   1670 			bytes = 0;
   1671 			break;
   1672 		}
   1673 
   1674 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   1675 		    bytes);
   1676 		if (blkno == (daddr_t)-1) {
   1677 			skipbytes += iobytes;
   1678 			continue;
   1679 		}
   1680 
   1681 		/*
   1682 		 * Discover how much we can really pack into this buffer.
   1683 		 */
   1684 		/* If no room in the current segment, finish it up */
   1685 		if (sp->sum_bytes_left < sizeof(int32_t) ||
   1686 		    sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
   1687 			int vers;
   1688 
   1689 			lfs_updatemeta(sp);
   1690 			vers = sp->fip->fi_version;
   1691 			lfs_release_finfo(fs);
   1692 			(void) lfs_writeseg(fs, sp);
   1693 
   1694 			lfs_acquire_finfo(fs, ip->i_number, vers);
   1695 		}
   1696 		/* Check both for space in segment and space in segsum */
   1697 		iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
   1698 					<< fs_bshift);
   1699 		iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
   1700 				       << fs_bshift);
   1701 		KASSERT(iobytes > 0);
   1702 
   1703 		/* if it's really one i/o, don't make a second buf */
   1704 		if (offset == startoffset && iobytes == bytes) {
   1705 			bp = mbp;
   1706 			/* correct overcount if there is no second buffer */
   1707 			s = splbio();
   1708 			simple_lock(&global_v_numoutput_slock);
   1709 			--vp->v_numoutput;
   1710 			simple_unlock(&global_v_numoutput_slock);
   1711 			splx(s);
   1712 		} else {
   1713 			bp = getiobuf();
   1714 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1715 			    vp, bp, vp->v_numoutput, 0);
   1716 			bp->b_data = (char *)kva +
   1717 			    (vaddr_t)(offset - pg->offset);
   1718 			bp->b_resid = bp->b_bcount = iobytes;
   1719 			bp->b_flags = B_BUSY|B_WRITE|B_CALL;
   1720 			bp->b_iodone = uvm_aio_biodone1;
   1721 		}
   1722 
   1723 		/* XXX This is silly ... is this necessary? */
   1724 		bp->b_vp = NULL;
   1725 		s = splbio();
   1726 		bgetvp(vp, bp);
   1727 		splx(s);
   1728 
   1729 		bp->b_lblkno = lblkno(fs, offset);
   1730 		bp->b_private = mbp;
   1731 		if (devvp->v_type == VBLK) {
   1732 			bp->b_dev = devvp->v_rdev;
   1733 		}
   1734 		VOP_BWRITE(bp);
   1735 		while (lfs_gatherblock(sp, bp, NULL))
   1736 			continue;
   1737 	}
   1738 
   1739 	if (skipbytes) {
   1740 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   1741 		s = splbio();
   1742 		if (error) {
   1743 			mbp->b_flags |= B_ERROR;
   1744 			mbp->b_error = error;
   1745 		}
   1746 		mbp->b_resid -= skipbytes;
   1747 		if (mbp->b_resid == 0) {
   1748 			biodone(mbp);
   1749 		}
   1750 		splx(s);
   1751 	}
   1752 	UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
   1753 	return (0);
   1754 
   1755     tryagain:
   1756 	/*
   1757 	 * We can't write the pages, for whatever reason.
   1758 	 * Clean up after ourselves, and make the caller try again.
   1759 	 */
   1760 	simple_lock(&vp->v_interlock);
   1761 
   1762 	/* Tell why we're here, if we know */
   1763 	if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
   1764 		DLOG((DLOG_PAGE, "lfs_gop_write: clean pages dirtied\n"));
   1765 	} else if ((pgs[0]->offset & fs->lfs_bmask) != 0) {
   1766 		DLOG((DLOG_PAGE, "lfs_gop_write: not on block boundary\n"));
   1767 	} else if (haveeof && startoffset >= eof) {
   1768 		DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
   1769 		      " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
   1770 		      pgs[0]->offset, eof, npages));
   1771 	} else if (LFS_STARVED_FOR_SEGS(fs)) {
   1772 		DLOG((DLOG_PAGE, "lfs_gop_write: avail too low\n"));
   1773 	} else {
   1774 		DLOG((DLOG_PAGE, "lfs_gop_write: seglock not held\n"));
   1775 	}
   1776 
   1777 	uvm_lock_pageq();
   1778 	for (i = 0; i < npages; i++) {
   1779 		pg = pgs[i];
   1780 
   1781 		if (pg->flags & PG_PAGEOUT)
   1782 			uvmexp.paging--;
   1783 		if (pg->flags & PG_DELWRI) {
   1784 			uvm_pageunwire(pg);
   1785 		}
   1786 		uvm_pageactivate(pg);
   1787 		pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
   1788 		DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
   1789 			vp, pg->offset));
   1790 		DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
   1791 		DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
   1792 		DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
   1793 		DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
   1794 		DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
   1795 		      pg->wire_count));
   1796 		DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
   1797 		      pg->loan_count));
   1798 	}
   1799 	/* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
   1800 	uvm_page_unbusy(pgs, npages);
   1801 	uvm_unlock_pageq();
   1802 	simple_unlock(&vp->v_interlock);
   1803 	return EAGAIN;
   1804 }
   1805 
   1806 /*
   1807  * finish vnode/inode initialization.
   1808  * used by lfs_vget and lfs_fastvget.
   1809  */
   1810 void
   1811 lfs_vinit(struct mount *mp, struct vnode **vpp)
   1812 {
   1813 	struct vnode *vp = *vpp;
   1814 	struct inode *ip = VTOI(vp);
   1815 	struct ufsmount *ump = VFSTOUFS(mp);
   1816 	int i;
   1817 
   1818 	ip->i_mode = ip->i_ffs1_mode;
   1819 	ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
   1820 	ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
   1821 	ip->i_flags = ip->i_ffs1_flags;
   1822 	ip->i_gen = ip->i_ffs1_gen;
   1823 	ip->i_uid = ip->i_ffs1_uid;
   1824 	ip->i_gid = ip->i_ffs1_gid;
   1825 
   1826 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
   1827 	ip->i_lfs_odnlink = ip->i_ffs1_nlink;
   1828 
   1829 	/*
   1830 	 * Initialize the vnode from the inode, check for aliases.  In all
   1831 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1832 	 */
   1833 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1834 	ip = VTOI(vp);
   1835 
   1836 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   1837 	if (vp->v_type != VLNK || ip->i_size >= ip->i_ump->um_maxsymlinklen) {
   1838 		struct lfs *fs = ump->um_lfs;
   1839 #ifdef DEBUG
   1840 		for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
   1841 		    i < NDADDR; i++) {
   1842 			if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
   1843 			    i == 0)
   1844 				continue;
   1845 			if (ip->i_ffs1_db[i] != 0) {
   1846 inconsistent:
   1847 				lfs_dump_dinode(ip->i_din.ffs1_din);
   1848 				panic("inconsistent inode");
   1849 			}
   1850 		}
   1851 		for ( ; i < NDADDR + NIADDR; i++) {
   1852 			if (ip->i_ffs1_ib[i - NDADDR] != 0) {
   1853 				goto inconsistent;
   1854 			}
   1855 		}
   1856 #endif /* DEBUG */
   1857 		for (i = 0; i < NDADDR; i++)
   1858 			if (ip->i_ffs1_db[i] != 0)
   1859 				ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   1860 	}
   1861 
   1862 #ifdef DIAGNOSTIC
   1863 	if (vp->v_type == VNON) {
   1864 # ifdef DEBUG
   1865 		lfs_dump_dinode(ip->i_din.ffs1_din);
   1866 # endif
   1867 		panic("lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
   1868 		      (unsigned long long)ip->i_number,
   1869 		      (ip->i_mode & IFMT) >> 12);
   1870 	}
   1871 #endif /* DIAGNOSTIC */
   1872 
   1873 	/*
   1874 	 * Finish inode initialization now that aliasing has been resolved.
   1875 	 */
   1876 
   1877 	ip->i_devvp = ump->um_devvp;
   1878 	VREF(ip->i_devvp);
   1879 	genfs_node_init(vp, &lfs_genfsops);
   1880 	uvm_vnp_setsize(vp, ip->i_size);
   1881 
   1882 	/* Initialize hiblk from file size */
   1883 	ip->i_lfs_hiblk = lblkno(ip->i_lfs, ip->i_size + ip->i_lfs->lfs_bsize - 1) - 1;
   1884 
   1885 	*vpp = vp;
   1886 }
   1887 
   1888 /*
   1889  * Resize the filesystem to contain the specified number of segments.
   1890  */
   1891 int
   1892 lfs_resize_fs(struct lfs *fs, int newnsegs)
   1893 {
   1894 	SEGUSE *sup;
   1895 	struct buf *bp, *obp;
   1896 	daddr_t olast, nlast, ilast, noff, start, end;
   1897 	struct vnode *ivp;
   1898 	struct inode *ip;
   1899 	int error, badnews, inc, oldnsegs;
   1900 	int sbbytes, csbbytes, gain, cgain;
   1901 	int i;
   1902 
   1903 	/* Only support v2 and up */
   1904 	if (fs->lfs_version < 2)
   1905 		return EOPNOTSUPP;
   1906 
   1907 	/* If we're doing nothing, do it fast */
   1908 	oldnsegs = fs->lfs_nseg;
   1909 	if (newnsegs == oldnsegs)
   1910 		return 0;
   1911 
   1912 	/* We always have to have two superblocks */
   1913 	if (newnsegs <= dtosn(fs, fs->lfs_sboffs[1]))
   1914 		return EFBIG;
   1915 
   1916 	ivp = fs->lfs_ivnode;
   1917 	ip = VTOI(ivp);
   1918 	error = 0;
   1919 
   1920 	/* Take the segment lock so no one else calls lfs_newseg() */
   1921 	lfs_seglock(fs, SEGM_PROT);
   1922 
   1923 	/*
   1924 	 * Make sure the segments we're going to be losing, if any,
   1925 	 * are in fact empty.  We hold the seglock, so their status
   1926 	 * cannot change underneath us.  Count the superblocks we lose,
   1927 	 * while we're at it.
   1928 	 */
   1929 	sbbytes = csbbytes = 0;
   1930 	cgain = 0;
   1931 	for (i = newnsegs; i < oldnsegs; i++) {
   1932 		LFS_SEGENTRY(sup, fs, i, bp);
   1933 		badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
   1934 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
   1935 			sbbytes += LFS_SBPAD;
   1936 		if (!(sup->su_flags & SEGUSE_DIRTY)) {
   1937 			++cgain;
   1938 			if (sup->su_flags & SEGUSE_SUPERBLOCK)
   1939 				csbbytes += LFS_SBPAD;
   1940 		}
   1941 		brelse(bp);
   1942 		if (badnews) {
   1943 			error = EBUSY;
   1944 			goto out;
   1945 		}
   1946 	}
   1947 
   1948 	/* Note old and new segment table endpoints, and old ifile size */
   1949 	olast = fs->lfs_cleansz + fs->lfs_segtabsz;
   1950 	nlast = howmany(newnsegs, fs->lfs_sepb) + fs->lfs_cleansz;
   1951 	ilast = ivp->v_size >> fs->lfs_bshift;
   1952 	noff = nlast - olast;
   1953 
   1954 	/*
   1955 	 * Make sure no one can use the Ifile while we change it around.
   1956 	 * Even after taking the iflock we need to make sure no one still
   1957 	 * is holding Ifile buffers, so we get each one, to drain them.
   1958 	 * (XXX this could be done better.)
   1959 	 */
   1960 	simple_lock(&fs->lfs_interlock);
   1961 	lockmgr(&fs->lfs_iflock, LK_EXCLUSIVE, &fs->lfs_interlock);
   1962 	simple_unlock(&fs->lfs_interlock);
   1963 	vn_lock(ivp, LK_EXCLUSIVE | LK_RETRY);
   1964 	for (i = 0; i < ilast; i++) {
   1965 		bread(ivp, i, fs->lfs_bsize, NOCRED, &bp);
   1966 		brelse(bp);
   1967 	}
   1968 
   1969 	/* Allocate new Ifile blocks */
   1970 	for (i = ilast; i < ilast + noff; i++) {
   1971 		if (lfs_balloc(ivp, i * fs->lfs_bsize, fs->lfs_bsize, NOCRED, 0,
   1972 			       &bp) != 0)
   1973 			panic("balloc extending ifile");
   1974 		memset(bp->b_data, 0, fs->lfs_bsize);
   1975 		VOP_BWRITE(bp);
   1976 	}
   1977 
   1978 	/* Register new ifile size */
   1979 	ip->i_size += noff * fs->lfs_bsize;
   1980 	ip->i_ffs1_size = ip->i_size;
   1981 	uvm_vnp_setsize(ivp, ip->i_size);
   1982 
   1983 	/* Copy the inode table to its new position */
   1984 	if (noff != 0) {
   1985 		if (noff < 0) {
   1986 			start = nlast;
   1987 			end = ilast + noff;
   1988 			inc = 1;
   1989 		} else {
   1990 			start = ilast + noff - 1;
   1991 			end = nlast - 1;
   1992 			inc = -1;
   1993 		}
   1994 		for (i = start; i != end; i += inc) {
   1995 			if (bread(ivp, i, fs->lfs_bsize, NOCRED, &bp) != 0)
   1996 				panic("resize: bread dst blk failed");
   1997 			if (bread(ivp, i - noff, fs->lfs_bsize, NOCRED, &obp))
   1998 				panic("resize: bread src blk failed");
   1999 			memcpy(bp->b_data, obp->b_data, fs->lfs_bsize);
   2000 			VOP_BWRITE(bp);
   2001 			brelse(obp);
   2002 		}
   2003 	}
   2004 
   2005 	/* If we are expanding, write the new empty SEGUSE entries */
   2006 	if (newnsegs > oldnsegs) {
   2007 		for (i = oldnsegs; i < newnsegs; i++) {
   2008 			if ((error = bread(ivp, i / fs->lfs_sepb +
   2009 					   fs->lfs_cleansz,
   2010 					   fs->lfs_bsize, NOCRED, &bp)) != 0)
   2011 				panic("lfs: ifile read: %d", error);
   2012 			while ((i + 1) % fs->lfs_sepb && i < newnsegs) {
   2013 				sup = &((SEGUSE *)bp->b_data)[i % fs->lfs_sepb];
   2014 				memset(sup, 0, sizeof(*sup));
   2015 				i++;
   2016 			}
   2017 			VOP_BWRITE(bp);
   2018 		}
   2019 	}
   2020 
   2021 	/* Zero out unused superblock offsets */
   2022 	for (i = 2; i < LFS_MAXNUMSB; i++)
   2023 		if (dtosn(fs, fs->lfs_sboffs[i]) >= newnsegs)
   2024 			fs->lfs_sboffs[i] = 0x0;
   2025 
   2026 	/*
   2027 	 * Correct superblock entries that depend on fs size.
   2028 	 * The computations of these are as follows:
   2029 	 *
   2030 	 * size  = segtod(fs, nseg)
   2031 	 * dsize = segtod(fs, nseg - minfreeseg) - btofsb(#super * LFS_SBPAD)
   2032 	 * bfree = dsize - btofsb(fs, bsize * nseg / 2) - blocks_actually_used
   2033 	 * avail = segtod(fs, nclean) - btofsb(#clean_super * LFS_SBPAD)
   2034 	 *         + (segtod(fs, 1) - (offset - curseg))
   2035 	 *	   - segtod(fs, minfreeseg - (minfreeseg / 2))
   2036 	 *
   2037 	 * XXX - we should probably adjust minfreeseg as well.
   2038 	 */
   2039 	gain = (newnsegs - oldnsegs);
   2040 	fs->lfs_nseg = newnsegs;
   2041 	fs->lfs_segtabsz = nlast - fs->lfs_cleansz;
   2042 	fs->lfs_size += gain * btofsb(fs, fs->lfs_ssize);
   2043 	fs->lfs_dsize += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes);
   2044 	fs->lfs_bfree += gain * btofsb(fs, fs->lfs_ssize) - btofsb(fs, sbbytes)
   2045 		       - gain * btofsb(fs, fs->lfs_bsize / 2);
   2046 	if (gain > 0) {
   2047 		fs->lfs_nclean += gain;
   2048 		fs->lfs_avail += gain * btofsb(fs, fs->lfs_ssize);
   2049 	} else {
   2050 		fs->lfs_nclean -= cgain;
   2051 		fs->lfs_avail -= cgain * btofsb(fs, fs->lfs_ssize) -
   2052 				 btofsb(fs, csbbytes);
   2053 	}
   2054 
   2055 	/* Resize segment flag cache */
   2056 	fs->lfs_suflags[0] = (u_int32_t *)realloc(fs->lfs_suflags[0],
   2057 						  fs->lfs_nseg * sizeof(u_int32_t),
   2058 						  M_SEGMENT, M_WAITOK);
   2059 	fs->lfs_suflags[1] = (u_int32_t *)realloc(fs->lfs_suflags[1],
   2060 						  fs->lfs_nseg * sizeof(u_int32_t),
   2061 						  M_SEGMENT, M_WAITOK);
   2062 	for (i = oldnsegs; i < newnsegs; i++)
   2063 		fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
   2064 
   2065 	/* Truncate Ifile if necessary */
   2066 	if (noff < 0)
   2067 		lfs_truncate(ivp, ivp->v_size + (noff << fs->lfs_bshift), 0,
   2068 			     NOCRED, curlwp);
   2069 
   2070 	/* Update cleaner info so the cleaner can die */
   2071 	bread(ivp, 0, fs->lfs_bsize, NOCRED, &bp);
   2072 	((CLEANERINFO *)bp->b_data)->clean = fs->lfs_nclean;
   2073 	((CLEANERINFO *)bp->b_data)->dirty = fs->lfs_nseg - fs->lfs_nclean;
   2074 	VOP_BWRITE(bp);
   2075 
   2076 	/* Let Ifile accesses proceed */
   2077 	VOP_UNLOCK(ivp, 0);
   2078 	simple_lock(&fs->lfs_interlock);
   2079 	lockmgr(&fs->lfs_iflock, LK_RELEASE, &fs->lfs_interlock);
   2080 	simple_unlock(&fs->lfs_interlock);
   2081 
   2082     out:
   2083 	lfs_segunlock(fs);
   2084 	return error;
   2085 }
   2086