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