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