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