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