Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.384
      1 /*	$NetBSD: lfs_vfsops.c,v 1.384 2025/09/02 00:24:10 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007, 2007
      5  *     The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Konrad E. Schroder <perseant (at) hhhh.org>.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 /*-
     33  * Copyright (c) 1989, 1991, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)lfs_vfsops.c	8.20 (Berkeley) 6/10/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.384 2025/09/02 00:24:10 perseant Exp $");
     65 
     66 #if defined(_KERNEL_OPT)
     67 #include "opt_lfs.h"
     68 #include "opt_quota.h"
     69 #include "opt_uvmhist.h"
     70 #endif
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/namei.h>
     75 #include <sys/proc.h>
     76 #include <sys/kernel.h>
     77 #include <sys/vnode.h>
     78 #include <sys/mount.h>
     79 #include <sys/kthread.h>
     80 #include <sys/buf.h>
     81 #include <sys/device.h>
     82 #include <sys/file.h>
     83 #include <sys/disklabel.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/errno.h>
     86 #include <sys/malloc.h>
     87 #include <sys/pool.h>
     88 #include <sys/socket.h>
     89 #include <sys/syslog.h>
     90 #include <sys/sysctl.h>
     91 #include <sys/conf.h>
     92 #include <sys/kauth.h>
     93 #include <sys/module.h>
     94 #include <sys/syscallvar.h>
     95 #include <sys/syscall.h>
     96 #include <sys/syscallargs.h>
     97 
     98 #include <miscfs/specfs/specdev.h>
     99 
    100 #include <ufs/lfs/ulfs_quotacommon.h>
    101 #include <ufs/lfs/ulfs_inode.h>
    102 #include <ufs/lfs/ulfsmount.h>
    103 #include <ufs/lfs/ulfs_bswap.h>
    104 #include <ufs/lfs/ulfs_extern.h>
    105 
    106 #ifdef UVMHIST
    107 #include <uvm/uvm.h>
    108 #endif
    109 #include <uvm/uvm_extern.h>
    110 #include <uvm/uvm_object.h>
    111 #include <uvm/uvm_page.h>
    112 #include <uvm/uvm_stat.h>
    113 
    114 #include <ufs/lfs/lfs.h>
    115 #include <ufs/lfs/lfs_accessors.h>
    116 #include <ufs/lfs/lfs_kernel.h>
    117 #include <ufs/lfs/lfs_extern.h>
    118 
    119 #include <miscfs/genfs/genfs.h>
    120 #include <miscfs/genfs/genfs_node.h>
    121 
    122 MODULE(MODULE_CLASS_VFS, lfs, NULL);
    123 
    124 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
    125 static int lfs_mountfs(struct vnode *, struct mount *, struct lwp *);
    126 static int lfs_flushfiles(struct mount *, int);
    127 
    128 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
    129 extern const struct vnodeopv_desc lfs_specop_opv_desc;
    130 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
    131 
    132 struct lwp * lfs_writer_daemon = NULL;
    133 kcondvar_t lfs_writerd_cv;
    134 
    135 struct workqueue *lfs_wq = NULL;
    136 
    137 int lfs_do_flush = 0;
    138 #ifdef LFS_KERNEL_RFW
    139 int lfs_do_rfw = 0;
    140 #endif
    141 
    142 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
    143 	&lfs_vnodeop_opv_desc,
    144 	&lfs_specop_opv_desc,
    145 	&lfs_fifoop_opv_desc,
    146 	NULL,
    147 };
    148 
    149 struct vfsops lfs_vfsops = {
    150 	.vfs_name = MOUNT_LFS,
    151 	.vfs_min_mount_data = sizeof (struct ulfs_args),
    152 	.vfs_mount = lfs_mount,
    153 	.vfs_start = ulfs_start,
    154 	.vfs_unmount = lfs_unmount,
    155 	.vfs_root = ulfs_root,
    156 	.vfs_quotactl = ulfs_quotactl,
    157 	.vfs_statvfs = lfs_statvfs,
    158 	.vfs_sync = lfs_sync,
    159 	.vfs_vget = lfs_vget,
    160 	.vfs_loadvnode = lfs_loadvnode,
    161 	.vfs_newvnode = lfs_newvnode,
    162 	.vfs_fhtovp = lfs_fhtovp,
    163 	.vfs_vptofh = lfs_vptofh,
    164 	.vfs_init = lfs_init,
    165 	.vfs_reinit = lfs_reinit,
    166 	.vfs_done = lfs_done,
    167 	.vfs_mountroot = lfs_mountroot,
    168 	.vfs_snapshot = (void *)eopnotsupp,
    169 	.vfs_extattrctl = lfs_extattrctl,
    170 	.vfs_suspendctl = genfs_suspendctl,
    171 	.vfs_renamelock_enter = genfs_renamelock_enter,
    172 	.vfs_renamelock_exit = genfs_renamelock_exit,
    173 	.vfs_fsync = (void *)eopnotsupp,
    174 	.vfs_opv_descs = lfs_vnodeopv_descs
    175 };
    176 
    177 const struct genfs_ops lfs_genfsops = {
    178 	.gop_size = lfs_gop_size,
    179 	.gop_alloc = ulfs_gop_alloc,
    180 	.gop_write = lfs_gop_write,
    181 	.gop_markupdate = ulfs_gop_markupdate,
    182 	.gop_putrange = genfs_gop_putrange,
    183 };
    184 
    185 struct shortlong {
    186 	const char *sname;
    187 	const char *lname;
    188 };
    189 
    190 static int
    191 sysctl_lfs_dostats(SYSCTLFN_ARGS)
    192 {
    193 	extern struct lfs_stats lfs_stats;
    194 	extern int lfs_dostats;
    195 	int error;
    196 
    197 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    198 	if (error || newp == NULL)
    199 		return (error);
    200 
    201 	if (lfs_dostats == 0)
    202 		memset(&lfs_stats, 0, sizeof(lfs_stats));
    203 
    204 	return (0);
    205 }
    206 
    207 SYSCTL_SETUP(lfs_sysctl_setup, "lfs sysctl")
    208 {
    209 	int i;
    210 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
    211 		   lfs_fs_pagetrip, lfs_ignore_lazy_sync;
    212 #ifdef DEBUG
    213 	extern int lfs_debug_log_subsys[DLOG_MAX];
    214 	struct shortlong dlog_names[DLOG_MAX] = { /* Must match lfs.h ! */
    215 		{ "rollforward", "Debug roll-forward code" },
    216 		{ "alloc",	"Debug inode allocation and free list" },
    217 		{ "avail",	"Debug space-available-now accounting" },
    218 		{ "flush",	"Debug flush triggers" },
    219 		{ "lockedlist",	"Debug locked list accounting" },
    220 		{ "vnode_verbose", "Verbose per-vnode-written debugging" },
    221 		{ "vnode",	"Debug vnode use during segment write" },
    222 		{ "segment",	"Debug segment writing" },
    223 		{ "seguse",	"Debug segment used-bytes accounting" },
    224 		{ "cleaner",	"Debug cleaning routines" },
    225 		{ "mount",	"Debug mount/unmount routines" },
    226 		{ "pagecache",	"Debug UBC interactions" },
    227 		{ "dirop",	"Debug directory-operation accounting" },
    228 		{ "malloc",	"Debug private malloc accounting" },
    229 	};
    230 #endif /* DEBUG */
    231 	struct shortlong stat_names[] = { /* Must match lfs.h! */
    232 		{ "segsused",	    "Number of new segments allocated" },
    233 		{ "psegwrites",	    "Number of partial-segment writes" },
    234 		{ "psyncwrites",    "Number of synchronous partial-segment"
    235 				    " writes" },
    236 		{ "pcleanwrites",   "Number of partial-segment writes by the"
    237 				    " cleaner" },
    238 		{ "blocktot",       "Number of blocks written" },
    239 		{ "cleanblocks",    "Number of blocks written by the cleaner" },
    240 		{ "ncheckpoints",   "Number of checkpoints made" },
    241 		{ "nwrites",        "Number of whole writes" },
    242 		{ "nsync_writes",   "Number of synchronous writes" },
    243 		{ "wait_exceeded",  "Number of times writer waited for"
    244 				    " cleaner" },
    245 		{ "write_exceeded", "Number of times writer invoked flush" },
    246 		{ "flush_invoked",  "Number of times flush was invoked" },
    247 		{ "vflush_invoked", "Number of time vflush was called" },
    248 		{ "clean_inlocked", "Number of vnodes skipped for being dead" },
    249 		{ "clean_vnlocked", "Number of vnodes skipped for vget failure" },
    250 		{ "segs_reclaimed", "Number of segments reclaimed" },
    251 	};
    252 
    253 	sysctl_createv(clog, 0, NULL, NULL,
    254 		       CTLFLAG_PERMANENT,
    255 		       CTLTYPE_NODE, "lfs",
    256 		       SYSCTL_DESCR("Log-structured file system"),
    257 		       NULL, 0, NULL, 0,
    258 		       CTL_VFS, 5, CTL_EOL);
    259 	/*
    260 	 * XXX the "5" above could be dynamic, thereby eliminating one
    261 	 * more instance of the "number to vfs" mapping problem, but
    262 	 * "5" is the order as taken from sys/mount.h
    263 	 */
    264 
    265 	sysctl_createv(clog, 0, NULL, NULL,
    266 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    267 		       CTLTYPE_INT, "flushindir", NULL,
    268 		       NULL, 0, &lfs_writeindir, 0,
    269 		       CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
    270 	sysctl_createv(clog, 0, NULL, NULL,
    271 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    272 		       CTLTYPE_INT, "clean_vnhead", NULL,
    273 		       NULL, 0, &lfs_clean_vnhead, 0,
    274 		       CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
    275 	sysctl_createv(clog, 0, NULL, NULL,
    276 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    277 		       CTLTYPE_INT, "dostats",
    278 		       SYSCTL_DESCR("Maintain statistics on LFS operations"),
    279 		       sysctl_lfs_dostats, 0, &lfs_dostats, 0,
    280 		       CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
    281 	sysctl_createv(clog, 0, NULL, NULL,
    282 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    283 		       CTLTYPE_INT, "pagetrip",
    284 		       SYSCTL_DESCR("How many dirty pages in fs triggers"
    285 				    " a flush"),
    286 		       NULL, 0, &lfs_fs_pagetrip, 0,
    287 		       CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
    288 	sysctl_createv(clog, 0, NULL, NULL,
    289 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    290 		       CTLTYPE_INT, "ignore_lazy_sync",
    291 		       SYSCTL_DESCR("Lazy Sync is ignored entirely"),
    292 		       NULL, 0, &lfs_ignore_lazy_sync, 0,
    293 		       CTL_VFS, 5, LFS_IGNORE_LAZY_SYNC, CTL_EOL);
    294 #ifdef LFS_KERNEL_RFW
    295 	sysctl_createv(clog, 0, NULL, NULL,
    296 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    297 		       CTLTYPE_INT, "rfw",
    298 		       SYSCTL_DESCR("Use in-kernel roll-forward on mount"),
    299 		       NULL, 0, &lfs_do_rfw, 0,
    300 		       CTL_VFS, 5, LFS_DO_RFW, CTL_EOL);
    301 #endif
    302 
    303 	sysctl_createv(clog, 0, NULL, NULL,
    304 		       CTLFLAG_PERMANENT,
    305 		       CTLTYPE_NODE, "stats",
    306 		       SYSCTL_DESCR("Debugging options"),
    307 		       NULL, 0, NULL, 0,
    308 		       CTL_VFS, 5, LFS_STATS, CTL_EOL);
    309 	for (i = 0; i < sizeof(struct lfs_stats) / sizeof(u_int); i++) {
    310 		sysctl_createv(clog, 0, NULL, NULL,
    311 			       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
    312 			       CTLTYPE_INT, stat_names[i].sname,
    313 			       SYSCTL_DESCR(stat_names[i].lname),
    314 			       NULL, 0, &(((u_int *)&lfs_stats.segsused)[i]),
    315 			       0, CTL_VFS, 5, LFS_STATS, i, CTL_EOL);
    316 	}
    317 
    318 #ifdef DEBUG
    319 	sysctl_createv(clog, 0, NULL, NULL,
    320 		       CTLFLAG_PERMANENT,
    321 		       CTLTYPE_NODE, "debug",
    322 		       SYSCTL_DESCR("Debugging options"),
    323 		       NULL, 0, NULL, 0,
    324 		       CTL_VFS, 5, LFS_DEBUGLOG, CTL_EOL);
    325 	for (i = 0; i < DLOG_MAX; i++) {
    326 		sysctl_createv(clog, 0, NULL, NULL,
    327 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    328 			       CTLTYPE_INT, dlog_names[i].sname,
    329 			       SYSCTL_DESCR(dlog_names[i].lname),
    330 			       NULL, 0, &(lfs_debug_log_subsys[i]), 0,
    331 			       CTL_VFS, 5, LFS_DEBUGLOG, i, CTL_EOL);
    332 	}
    333 #endif
    334 }
    335 
    336 /* old cleaner syscall interface.  see VOP_FCNTL() */
    337 static const struct syscall_package lfs_syscalls[] = {
    338 	{ SYS_lfs_bmapv,	0, (sy_call_t *)sys_lfs_bmapv		},
    339 	{ SYS_lfs_markv,	0, (sy_call_t *)sys_lfs_markv		},
    340 	{ SYS___lfs_segwait50,	0, (sy_call_t *)sys___lfs_segwait50	},
    341 	{ SYS_lfs_segclean,	0, (sy_call_t *)sys_lfs_segclean	},
    342 	{ 0, 0, NULL },
    343 };
    344 
    345 static int
    346 lfs_modcmd(modcmd_t cmd, void *arg)
    347 {
    348 	int error;
    349 
    350 	switch (cmd) {
    351 	case MODULE_CMD_INIT:
    352 		error = syscall_establish(NULL, lfs_syscalls);
    353 		if (error)
    354 			return error;
    355 		error = vfs_attach(&lfs_vfsops);
    356 		if (error != 0) {
    357 			syscall_disestablish(NULL, lfs_syscalls);
    358 			break;
    359 		}
    360 		cv_init(&lfs_allclean_wakeup, "segment");
    361 		break;
    362 	case MODULE_CMD_FINI:
    363 		error = vfs_detach(&lfs_vfsops);
    364 		if (error != 0)
    365 			break;
    366 		syscall_disestablish(NULL, lfs_syscalls);
    367 		cv_destroy(&lfs_allclean_wakeup);
    368 		break;
    369 	default:
    370 		error = ENOTTY;
    371 		break;
    372 	}
    373 
    374 	return (error);
    375 }
    376 
    377 /*
    378  * XXX Same structure as FFS inodes?  Should we share a common pool?
    379  */
    380 struct pool lfs_inode_pool;
    381 struct pool lfs_dinode_pool;
    382 struct pool lfs_inoext_pool;
    383 struct pool lfs_lbnentry_pool;
    384 
    385 /*
    386  * The writer daemon.  UVM keeps track of how many dirty pages we are holding
    387  * in lfs_subsys_pages; the daemon flushes the filesystem when this value
    388  * crosses the (user-defined) threshold LFS_MAX_PAGES.
    389  */
    390 static void
    391 lfs_writerd(void *arg)
    392 {
    393 	mount_iterator_t *iter;
    394  	struct mount *mp;
    395  	struct lfs *fs;
    396 	struct vfsops *vfs = NULL;
    397  	int fsflags;
    398 	int lfsc;
    399 	int wrote_something = 0;
    400 
    401 	mutex_enter(&lfs_lock);
    402 	KASSERTMSG(lfs_writer_daemon == NULL, "more than one LFS writer daemon");
    403 	lfs_writer_daemon = curlwp;
    404 	mutex_exit(&lfs_lock);
    405 
    406 	/* Take an extra reference to the LFS vfsops. */
    407 	vfs = vfs_getopsbyname(MOUNT_LFS);
    408 
    409  	mutex_enter(&lfs_lock);
    410  	for (;;) {
    411 		KASSERT(mutex_owned(&lfs_lock));
    412 		if (wrote_something == 0)
    413 			cv_timedwait(&lfs_writerd_cv, &lfs_lock, hz/10 + 1);
    414 		KASSERT(mutex_owned(&lfs_lock));
    415 		wrote_something = 0;
    416 
    417 		/*
    418 		 * If global state wants a flush, flush everything.
    419 		 */
    420 		if (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
    421 			locked_queue_bytes > LFS_MAX_BYTES ||
    422 			lfs_subsys_pages > LFS_MAX_PAGES) {
    423 
    424 			if (lfs_do_flush) {
    425 				DLOG((DLOG_FLUSH, "lfs_writerd: lfs_do_flush\n"));
    426 			}
    427 			if (locked_queue_count > LFS_MAX_BUFS) {
    428 				DLOG((DLOG_FLUSH, "lfs_writerd: lqc = %d, max %d\n",
    429 				      locked_queue_count, LFS_MAX_BUFS));
    430 			}
    431 			if (locked_queue_bytes > LFS_MAX_BYTES) {
    432 				DLOG((DLOG_FLUSH, "lfs_writerd: lqb = %ld, max %ld\n",
    433 				      locked_queue_bytes, LFS_MAX_BYTES));
    434 			}
    435 			if (lfs_subsys_pages > LFS_MAX_PAGES) {
    436 				DLOG((DLOG_FLUSH, "lfs_writerd: lssp = %d, max %d\n",
    437 				      lfs_subsys_pages, LFS_MAX_PAGES));
    438 			}
    439 
    440 			lfs_flush(NULL, SEGM_WRITERD, 0);
    441 			lfs_do_flush = 0;
    442 			KASSERT(mutex_owned(&lfs_lock));
    443 			continue;
    444 		}
    445 		KASSERT(mutex_owned(&lfs_lock));
    446 		mutex_exit(&lfs_lock);
    447 
    448  		/*
    449  		 * Look through the list of LFSs to see if any of them
    450  		 * have requested pageouts.
    451  		 */
    452  		mountlist_iterator_init(&iter);
    453 		lfsc = 0;
    454 		while ((mp = mountlist_iterator_next(iter)) != NULL) {
    455 			KASSERT(!mutex_owned(&lfs_lock));
    456  			if (strncmp(mp->mnt_stat.f_fstypename, MOUNT_LFS,
    457  			    sizeof(mp->mnt_stat.f_fstypename)) == 0) {
    458 				++lfsc;
    459  				fs = VFSTOULFS(mp)->um_lfs;
    460 				daddr_t ooffset = 0;
    461 				fsflags = SEGM_SINGLE;
    462 
    463  				mutex_enter(&lfs_lock);
    464 				ooffset = lfs_sb_getoffset(fs);
    465 
    466 				if (lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs) && fs->lfs_nowrap) {
    467 					/* Don't try to write if we're suspended */
    468 					mutex_exit(&lfs_lock);
    469 					continue;
    470 				}
    471 				if (LFS_STARVED_FOR_SEGS(fs)) {
    472 					mutex_exit(&lfs_lock);
    473 
    474 					DLOG((DLOG_FLUSH, "lfs_writerd: need cleaning before writing possible\n"));
    475 					lfs_wakeup_cleaner(fs);
    476 					continue;
    477 				}
    478 
    479  				if ((fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    480  				     lfs_dirvcount > LFS_MAX_DIROP) &&
    481 				    fs->lfs_dirops == 0) {
    482 					fsflags &= ~SEGM_SINGLE;
    483  					fsflags |= SEGM_CKP;
    484 					DLOG((DLOG_FLUSH, "lfs_writerd: checkpoint\n"));
    485 					lfs_flush_fs(fs, fsflags);
    486 				} else if (fs->lfs_pdflush) {
    487  					DLOG((DLOG_FLUSH, "lfs_writerd: pdflush set\n"));
    488  					lfs_flush_fs(fs, fsflags);
    489  				} else if (!TAILQ_EMPTY(&fs->lfs_pchainhd)) {
    490  					DLOG((DLOG_FLUSH, "lfs_writerd: pchain non-empty\n"));
    491  					mutex_exit(&lfs_lock);
    492  					lfs_writer_enter(fs, "wrdirop");
    493  					lfs_flush_pchain(fs);
    494  					lfs_writer_leave(fs);
    495 					mutex_enter(&lfs_lock);
    496 				}
    497 				if (lfs_sb_getoffset(fs) != ooffset)
    498 					++wrote_something;
    499 				mutex_exit(&lfs_lock);
    500  			}
    501 			KASSERT(!mutex_owned(&lfs_lock));
    502  		}
    503 		if (lfsc == 0) {
    504 			mutex_enter(&lfs_lock);
    505 			lfs_writer_daemon = NULL;
    506 			mutex_exit(&lfs_lock);
    507 			mountlist_iterator_destroy(iter);
    508 			break;
    509 		}
    510  		mountlist_iterator_destroy(iter);
    511 
    512  		mutex_enter(&lfs_lock);
    513  	}
    514 	KASSERT(!mutex_owned(&lfs_lock));
    515 
    516 	/* Give up our extra reference so the module can be unloaded. */
    517 	mutex_enter(&vfs_list_lock);
    518 	if (vfs != NULL)
    519 		vfs->vfs_refcount--;
    520 	mutex_exit(&vfs_list_lock);
    521 
    522 	/* Done! */
    523 	kthread_exit(0);
    524 }
    525 
    526 /*
    527  * Initialize the filesystem, most work done by ulfs_init.
    528  */
    529 void
    530 lfs_init(void)
    531 {
    532 
    533 	/*
    534 	 * XXX: should we use separate pools for 32-bit and 64-bit
    535 	 * dinodes?
    536 	 */
    537 	malloc_type_attach(M_SEGMENT);
    538 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    539 	    "lfsinopl", &pool_allocator_nointr, IPL_NONE);
    540 	pool_init(&lfs_dinode_pool, sizeof(union lfs_dinode), 0, 0, 0,
    541 	    "lfsdinopl", &pool_allocator_nointr, IPL_NONE);
    542 	pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
    543 	    "lfsinoextpl", &pool_allocator_nointr, IPL_NONE);
    544 	pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
    545 	    "lfslbnpool", &pool_allocator_nointr, IPL_NONE);
    546 	ulfs_init();
    547 
    548 #ifdef DEBUG
    549 	memset(lfs_log, 0, sizeof(lfs_log));
    550 #endif
    551 	mutex_init(&lfs_lock, MUTEX_DEFAULT, IPL_NONE);
    552 	cv_init(&lfs_writerd_cv, "lfswrite");
    553 	cv_init(&locked_queue_cv, "lfsbuf");
    554 	cv_init(&lfs_writing_cv, "lfsflush");
    555 	workqueue_create(&lfs_wq, "lfsiowq", lfs_cluster_wq, NULL,
    556 					PRI_BIO, IPL_BIO, WQ_MPSAFE);
    557 }
    558 
    559 void
    560 lfs_reinit(void)
    561 {
    562 	ulfs_reinit();
    563 }
    564 
    565 void
    566 lfs_done(void)
    567 {
    568 	ulfs_done();
    569 	mutex_destroy(&lfs_lock);
    570 	cv_destroy(&lfs_writerd_cv);
    571 	cv_destroy(&locked_queue_cv);
    572 	cv_destroy(&lfs_writing_cv);
    573 	workqueue_destroy(lfs_wq);
    574 	pool_destroy(&lfs_inode_pool);
    575 	pool_destroy(&lfs_dinode_pool);
    576 	pool_destroy(&lfs_inoext_pool);
    577 	pool_destroy(&lfs_lbnentry_pool);
    578 	malloc_type_detach(M_SEGMENT);
    579 }
    580 
    581 /*
    582  * Called by main() when ulfs is going to be mounted as root.
    583  */
    584 int
    585 lfs_mountroot(void)
    586 {
    587 	extern struct vnode *rootvp;
    588 	struct lfs *fs = NULL;				/* LFS */
    589 	struct mount *mp;
    590 	struct lwp *l = curlwp;
    591 	struct ulfsmount *ump;
    592 	int error;
    593 
    594 	if (device_class(root_device) != DV_DISK)
    595 		return (ENODEV);
    596 
    597 	if (rootdev == NODEV)
    598 		return (ENODEV);
    599 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    600 		vrele(rootvp);
    601 		return (error);
    602 	}
    603 	if ((error = lfs_mountfs(rootvp, mp, l))) {
    604 		vfs_unbusy(mp);
    605 		vfs_rele(mp);
    606 		return (error);
    607 	}
    608 	mountlist_append(mp);
    609 	ump = VFSTOULFS(mp);
    610 	fs = ump->um_lfs;
    611 	lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
    612 	(void)lfs_statvfs(mp, &mp->mnt_stat);
    613 	vfs_unbusy(mp);
    614 	setrootfstime((time_t)lfs_sb_gettstamp(VFSTOULFS(mp)->um_lfs));
    615 	return (0);
    616 }
    617 
    618 /*
    619  * VFS Operations.
    620  *
    621  * mount system call
    622  */
    623 int
    624 lfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    625 {
    626 	struct lwp *l = curlwp;
    627 	struct vnode *devvp;
    628 	struct ulfs_args *args = data;
    629 	struct ulfsmount *ump = NULL;
    630 	struct lfs *fs = NULL;				/* LFS */
    631 	int error = 0, update;
    632 	mode_t accessmode;
    633 
    634 	if (args == NULL)
    635 		return EINVAL;
    636 	if (*data_len < sizeof *args)
    637 		return EINVAL;
    638 
    639 	if (mp->mnt_flag & MNT_GETARGS) {
    640 		ump = VFSTOULFS(mp);
    641 		if (ump == NULL)
    642 			return EIO;
    643 		args->fspec = NULL;
    644 		*data_len = sizeof *args;
    645 		return 0;
    646 	}
    647 
    648 	update = mp->mnt_flag & MNT_UPDATE;
    649 
    650 	/* Check arguments */
    651 	if (args->fspec != NULL) {
    652 		/*
    653 		 * Look up the name and verify that it's sane.
    654 		 */
    655 		error = namei_simple_user(args->fspec,
    656 					NSM_FOLLOW_NOEMULROOT, &devvp);
    657 		if (error != 0)
    658 			return (error);
    659 
    660 		if (!update) {
    661 			/*
    662 			 * Be sure this is a valid block device
    663 			 */
    664 			if (devvp->v_type != VBLK)
    665 				error = ENOTBLK;
    666 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    667 				error = ENXIO;
    668 		} else {
    669 			/*
    670 			 * Be sure we're still naming the same device
    671 			 * used for our initial mount
    672 			 *
    673 			 * XXX dholland 20151010: if namei gives us a
    674 			 * different vnode for the same device,
    675 			 * wouldn't it be better to use it going
    676 			 * forward rather than ignore it in favor of
    677 			 * the old one?
    678 			 */
    679 			ump = VFSTOULFS(mp);
    680 			fs = ump->um_lfs;
    681 			if (devvp != fs->lfs_devvp) {
    682 				if (devvp->v_rdev != fs->lfs_devvp->v_rdev)
    683 					error = EINVAL;
    684 				else {
    685 					vrele(devvp);
    686 					devvp = fs->lfs_devvp;
    687 					vref(devvp);
    688 				}
    689 			}
    690 		}
    691 	} else {
    692 		if (!update) {
    693 			/* New mounts must have a filename for the device */
    694 			return (EINVAL);
    695 		} else {
    696 			/* Use the extant mount */
    697 			ump = VFSTOULFS(mp);
    698 			fs = ump->um_lfs;
    699 			devvp = fs->lfs_devvp;
    700 			vref(devvp);
    701 		}
    702 	}
    703 
    704 
    705 	/*
    706 	 * If mount by non-root, then verify that user has necessary
    707 	 * permissions on the device.
    708 	 */
    709 	if (error == 0) {
    710 		accessmode = VREAD;
    711 		if (update ?
    712 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    713 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    714 			accessmode |= VWRITE;
    715 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    716 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    717 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
    718 		    KAUTH_ARG(accessmode));
    719 		VOP_UNLOCK(devvp);
    720 	}
    721 
    722 	if (error) {
    723 		vrele(devvp);
    724 		return (error);
    725 	}
    726 
    727 	if (!update) {
    728 		int flags;
    729 
    730 		if (mp->mnt_flag & MNT_RDONLY)
    731 			flags = FREAD;
    732 		else
    733 			flags = FREAD|FWRITE;
    734 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    735 		error = VOP_OPEN(devvp, flags, FSCRED);
    736 		VOP_UNLOCK(devvp);
    737 		if (error)
    738 			goto fail;
    739 		error = lfs_mountfs(devvp, mp, l);		/* LFS */
    740 		if (error) {
    741 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    742 			(void)VOP_CLOSE(devvp, flags, NOCRED);
    743 			VOP_UNLOCK(devvp);
    744 			goto fail;
    745 		}
    746 
    747 		ump = VFSTOULFS(mp);
    748 		fs = ump->um_lfs;
    749 	} else {
    750 		/*
    751 		 * Update the mount.
    752 		 */
    753 
    754 		/*
    755 		 * The initial mount got a reference on this
    756 		 * device, so drop the one obtained via
    757 		 * namei(), above.
    758 		 */
    759 		vrele(devvp);
    760 
    761 		ump = VFSTOULFS(mp);
    762 		fs = ump->um_lfs;
    763 
    764 		if (!fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDONLY)) {
    765 			/*
    766 			 * Changing from read/write to read-only.
    767 			 */
    768 			int flags = WRITECLOSE;
    769 			if (mp->mnt_flag & MNT_FORCE)
    770 				flags |= FORCECLOSE;
    771 			error = lfs_flushfiles(mp, flags);
    772 			if (error)
    773 				return error;
    774 			fs->lfs_ronly = 1;
    775 		} else if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    776 			/*
    777 			 * Changing from read-only to read/write.
    778 			 * Note in the superblocks that we're writing.
    779 			 */
    780 
    781 			/* XXX: quotas should have been on even if readonly */
    782 			if (fs->lfs_use_quota2) {
    783 #ifdef LFS_QUOTA2
    784 				error = lfs_quota2_mount(mp);
    785 #else
    786 				uprintf("%s: no kernel support for this "
    787 					"filesystem's quotas\n",
    788 					mp->mnt_stat.f_mntonname);
    789 				if (mp->mnt_flag & MNT_FORCE) {
    790 					uprintf("%s: mounting anyway; "
    791 						"fsck afterwards\n",
    792 						mp->mnt_stat.f_mntonname);
    793 				} else {
    794 					error = EINVAL;
    795 				}
    796 #endif
    797 				if (error) {
    798 					return error;
    799 				}
    800 			}
    801 
    802 			fs->lfs_ronly = 0;
    803 			if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) {
    804 				lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
    805 				lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
    806 				lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
    807 			}
    808 		}
    809 
    810 		if (args->fspec == NULL)
    811 			return 0;
    812 	}
    813 
    814 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    815 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    816 	if (error == 0)
    817 		lfs_sb_setfsmnt(fs, mp->mnt_stat.f_mntonname);
    818 	return error;
    819 
    820 fail:
    821 	vrele(devvp);
    822 	return (error);
    823 }
    824 
    825 /*
    826  * Helper for mountfs. Note that the fs pointer may be a dummy one
    827  * pointing into a superblock buffer. (Which is gross; see below.)
    828  */
    829 static int
    830 lfs_checkmagic(struct lfs *fs)
    831 {
    832 	switch (fs->lfs_dlfs_u.u_32.dlfs_magic) {
    833 	    case LFS_MAGIC:
    834 		fs->lfs_is64 = false;
    835 		fs->lfs_dobyteswap = false;
    836 		break;
    837 	    case LFS64_MAGIC:
    838 		fs->lfs_is64 = true;
    839 		fs->lfs_dobyteswap = false;
    840 		break;
    841 #ifdef LFS_EI
    842 	    case LFS_MAGIC_SWAPPED:
    843 		fs->lfs_is64 = false;
    844 		fs->lfs_dobyteswap = true;
    845 		break;
    846 	    case LFS64_MAGIC_SWAPPED:
    847 		fs->lfs_is64 = true;
    848 		fs->lfs_dobyteswap = true;
    849 		break;
    850 #endif
    851 	    default:
    852 		/* XXX needs translation */
    853 		return EINVAL;
    854 	}
    855 	return 0;
    856 }
    857 
    858 /*
    859  * Common code for mount and mountroot
    860  * LFS specific
    861  */
    862 int
    863 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    864 {
    865 	struct lfs *primarysb, *altsb, *thesb;
    866 	struct buf *primarybuf, *altbuf;
    867 	struct lfs *fs;
    868 	struct ulfsmount *ump;
    869 	struct vnode *vp;
    870 	dev_t dev;
    871 	int error, i, ronly, fsbsize;
    872 	kauth_cred_t cred;
    873 	CLEANERINFO *cip;
    874 	SEGUSE *sup;
    875 	daddr_t sb_addr;
    876 	ino_t *orphan;
    877 	size_t norphan;
    878 
    879 	cred = l ? l->l_cred : NOCRED;
    880 
    881 	/* The superblock is supposed to be 512 bytes. */
    882 	__CTASSERT(sizeof(struct dlfs) == DEV_BSIZE);
    883 
    884 	/*
    885 	 * Flush out any old buffers remaining from a previous use.
    886 	 */
    887 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    888 	error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
    889 	VOP_UNLOCK(devvp);
    890 	if (error)
    891 		return (error);
    892 
    893 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    894 
    895 	/* Don't free random space on error. */
    896 	primarybuf = NULL;
    897 	altbuf = NULL;
    898 	ump = NULL;
    899 
    900 	sb_addr = LFS_LABELPAD / DEV_BSIZE;
    901 	while (1) {
    902 		/*
    903 		 * Read in the superblock.
    904 		 *
    905 		 * Note that because LFS_SBPAD is substantially larger
    906 		 * (8K) than the actual on-disk superblock (512 bytes)
    907 		 * the buffer contains enough space to be used as a
    908 		 * whole struct lfs (in-memory superblock) - we do this
    909 		 * only so we can set and use the is64 and dobyteswap
    910 		 * members. XXX this is gross and the logic here should
    911 		 * be reworked.
    912 		 */
    913 		error = bread(devvp, sb_addr, LFS_SBPAD, 0, &primarybuf);
    914 		if (error)
    915 			goto out;
    916 		primarysb = (struct lfs *)primarybuf->b_data;
    917 
    918 		/* Check the basics. */
    919 		error = lfs_checkmagic(primarysb);
    920 		if (error) {
    921 			DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock wrong magic\n"));
    922 			goto out;
    923 		}
    924 		if (lfs_sb_getbsize(primarysb) > MAXBSIZE ||
    925 		    lfs_sb_getversion(primarysb) > LFS_VERSION ||
    926 		    lfs_sb_getbsize(primarysb) < sizeof(struct dlfs)) {
    927 			DLOG((DLOG_MOUNT, "lfs_mountfs: primary superblock sanity failed\n"));
    928 			/* XXX needs translation */
    929 			error = EINVAL;
    930 			goto out;
    931 		}
    932 		if (lfs_sb_getinodefmt(primarysb) > LFS_MAXINODEFMT) {
    933 			DLOG((DLOG_MOUNT, "lfs_mountfs: unknown inode format %d\n",
    934 			       lfs_sb_getinodefmt(primarysb)));
    935 			error = EINVAL;
    936 			goto out;
    937 		}
    938 
    939 		if (lfs_sb_getversion(primarysb) == 1)
    940 			fsbsize = DEV_BSIZE;
    941 		else {
    942 			fsbsize = 1 << lfs_sb_getffshift(primarysb);
    943 			/*
    944 			 * Could be, if the frag size is large enough, that we
    945 			 * don't have the "real" primary superblock.  If that's
    946 			 * the case, get the real one, and try again.
    947 			 */
    948 			if (sb_addr != (lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))) {
    949 				DLOG((DLOG_MOUNT, "lfs_mountfs: sb daddr"
    950 				      " 0x%llx is not right, trying 0x%llx\n",
    951 				      (long long)sb_addr,
    952 				      (long long)(lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT))));
    953 				sb_addr = lfs_sb_getsboff(primarysb, 0) << (lfs_sb_getffshift(primarysb) - DEV_BSHIFT);
    954 				brelse(primarybuf, BC_INVAL);
    955 				continue;
    956 			}
    957 		}
    958 		break;
    959 	}
    960 
    961 	/*
    962 	 * Check the second superblock to see which is newer; then mount
    963 	 * using the older of the two.	This is necessary to ensure that
    964 	 * the filesystem is valid if it was not unmounted cleanly.
    965 	 */
    966 
    967 	if (lfs_sb_getsboff(primarysb, 1) &&
    968 	    lfs_sb_getsboff(primarysb, 1) - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
    969 	{
    970 		error = bread(devvp, lfs_sb_getsboff(primarysb, 1) * (fsbsize / DEV_BSIZE),
    971 			LFS_SBPAD, 0, &altbuf);
    972 		if (error)
    973 			goto out;
    974 		altsb = (struct lfs *)altbuf->b_data;
    975 
    976 		/*
    977 		 * Note: this used to do the sanity check only if the
    978 		 * timestamp/serial comparison required use of altsb;
    979 		 * this way is less tolerant, but if altsb is corrupted
    980 		 * enough that the magic number, version, and blocksize
    981 		 * are bogus, why would the timestamp or serial fields
    982 		 * mean anything either? If this kind of thing happens,
    983 		 * you need to fsck anyway.
    984 		 */
    985 
    986 		error = lfs_checkmagic(altsb);
    987 		if (error)
    988 			goto out;
    989 
    990 		/* Check the basics. */
    991 		if (lfs_sb_getbsize(altsb) > MAXBSIZE ||
    992 		    lfs_sb_getversion(altsb) > LFS_VERSION ||
    993 		    lfs_sb_getbsize(altsb) < sizeof(struct dlfs)) {
    994 			DLOG((DLOG_MOUNT, "lfs_mountfs: alt superblock"
    995 			      " sanity failed\n"));
    996 			error = EINVAL;		/* XXX needs translation */
    997 			goto out;
    998 		}
    999 
   1000 		if (lfs_sb_getversion(primarysb) == 1) {
   1001 			/* 1s resolution comparison */
   1002 			if (lfs_sb_gettstamp(altsb) < lfs_sb_gettstamp(primarysb))
   1003 				thesb = altsb;
   1004 			else
   1005 				thesb = primarysb;
   1006 		} else {
   1007 			/* monotonic infinite-resolution comparison */
   1008 			if (lfs_sb_getserial(altsb) < lfs_sb_getserial(primarysb))
   1009 				thesb = altsb;
   1010 			else
   1011 				thesb = primarysb;
   1012 		}
   1013 	} else {
   1014 		DLOG((DLOG_MOUNT, "lfs_mountfs: invalid alt superblock location"
   1015 		      " daddr=0x%x\n", lfs_sb_getsboff(primarysb, 1)));
   1016 		error = EINVAL;
   1017 		goto out;
   1018 	}
   1019 
   1020 	/*
   1021 	 * Allocate the mount structure, copy the superblock into it.
   1022 	 * Note that the 32-bit and 64-bit superblocks are the same size.
   1023 	 */
   1024 	fs = kmem_zalloc(sizeof(struct lfs), KM_SLEEP);
   1025 	memcpy(&fs->lfs_dlfs_u.u_32, &thesb->lfs_dlfs_u.u_32,
   1026 	       sizeof(struct dlfs));
   1027 	fs->lfs_is64 = thesb->lfs_is64;
   1028 	fs->lfs_dobyteswap = thesb->lfs_dobyteswap;
   1029 	fs->lfs_hasolddirfmt = false; /* set for real below */
   1030 
   1031 	/* Compatibility */
   1032 	if (lfs_sb_getversion(fs) < 2) {
   1033 		lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
   1034 		lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
   1035 		lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
   1036 		lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
   1037 		lfs_sb_setfsbtodb(fs, 0);
   1038 	}
   1039 	if (lfs_sb_getresvseg(fs) == 0)
   1040 		lfs_sb_setresvseg(fs, MIN(lfs_sb_getminfreeseg(fs) - 1, \
   1041 			MAX(MIN_RESV_SEGS, lfs_sb_getminfreeseg(fs) / 2 + 1)));
   1042 
   1043 	/*
   1044 	 * If we aren't going to be able to write meaningfully to this
   1045 	 * filesystem, and were not mounted readonly, bomb out now.
   1046 	 */
   1047 	if (lfs_fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
   1048 		DLOG((DLOG_MOUNT, "lfs_mount: to mount this filesystem read/write,"
   1049 		      " we need BUFPAGES >= %lld\n",
   1050 		      (long long)((bufmem_hiwater / bufmem_lowater) *
   1051 				  LFS_INVERSE_MAX_BYTES(
   1052 					  lfs_fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT)));
   1053 		kmem_free(fs, sizeof(struct lfs));
   1054 		error = EFBIG; /* XXX needs translation */
   1055 		goto out;
   1056 	}
   1057 
   1058 	/* Before rolling forward, lock so vget will sleep for other procs */
   1059 	if (l != NULL) {
   1060 		fs->lfs_flags = LFS_NOTYET;
   1061 		fs->lfs_rfpid = l->l_proc->p_pid;
   1062 	}
   1063 
   1064 	ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
   1065 	ump->um_lfs = fs;
   1066 	ump->um_fstype = fs->lfs_is64 ? ULFS2 : ULFS1;
   1067 	/* ump->um_cleaner_thread = NULL; */
   1068 	brelse(primarybuf, BC_INVAL);
   1069 	brelse(altbuf, BC_INVAL);
   1070 	primarybuf = NULL;
   1071 	altbuf = NULL;
   1072 
   1073 
   1074 	/* Set up the I/O information */
   1075 	fs->lfs_devbsize = DEV_BSIZE;
   1076 	fs->lfs_iocount = 0;
   1077 	fs->lfs_diropwait = 0;
   1078 	fs->lfs_activesb = 0;
   1079 	lfs_sb_setuinodes(fs, 0);
   1080 	fs->lfs_ravail = 0;
   1081 	fs->lfs_favail = 0;
   1082 	fs->lfs_sbactive = 0;
   1083 
   1084 	/* Set up the ifile and lock aflags */
   1085 	fs->lfs_doifile = 0;
   1086 	fs->lfs_writer = 0;
   1087 	fs->lfs_dirops = 0;
   1088 	fs->lfs_nadirop = 0;
   1089 	fs->lfs_seglock = 0;
   1090 	fs->lfs_pdflush = 0;
   1091 	fs->lfs_sleepers = 0;
   1092 	fs->lfs_pages = 0;
   1093 	rw_init(&fs->lfs_fraglock);
   1094 	rw_init(&fs->lfs_iflock);
   1095 	cv_init(&fs->lfs_sleeperscv, "lfs_slp");
   1096 	cv_init(&fs->lfs_diropscv, "lfs_dirop");
   1097 	cv_init(&fs->lfs_stopcv, "lfsstop");
   1098 	cv_init(&fs->lfs_nextsegsleep, "segment");
   1099 
   1100 	/* Set the file system readonly/modify bits. */
   1101 	fs->lfs_ronly = ronly;
   1102 	if (ronly == 0)
   1103 		fs->lfs_fmod = 1;
   1104 
   1105 	/* Device we're using */
   1106 	dev = devvp->v_rdev;
   1107 	fs->lfs_dev = dev;
   1108 	fs->lfs_devvp = devvp;
   1109 
   1110 	/* ulfs-level information */
   1111 	fs->um_flags = 0;
   1112 	fs->um_bptrtodb = lfs_sb_getffshift(fs) - DEV_BSHIFT;
   1113 	fs->um_seqinc = lfs_sb_getfrag(fs);
   1114 	fs->um_nindir = lfs_sb_getnindir(fs);
   1115 	fs->um_lognindir = ffs(lfs_sb_getnindir(fs)) - 1;
   1116 	fs->um_maxsymlinklen = lfs_sb_getmaxsymlinklen(fs);
   1117 	fs->um_dirblksiz = LFS_DIRBLKSIZ;
   1118 	fs->um_maxfilesize = lfs_sb_getmaxfilesize(fs);
   1119 
   1120 	/* quota stuff */
   1121 	/* XXX: these need to come from the on-disk superblock to be used */
   1122 	fs->lfs_use_quota2 = 0;
   1123 	fs->lfs_quota_magic = 0;
   1124 	fs->lfs_quota_flags = 0;
   1125 	fs->lfs_quotaino[0] = 0;
   1126 	fs->lfs_quotaino[1] = 0;
   1127 
   1128 	/* Initialize the mount structure. */
   1129 	mp->mnt_data = ump;
   1130 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1131 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
   1132 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1133 	mp->mnt_stat.f_namemax = LFS_MAXNAMLEN;
   1134 	mp->mnt_stat.f_iosize = lfs_sb_getbsize(fs);
   1135 	mp->mnt_flag |= MNT_LOCAL;
   1136 	mp->mnt_iflag |= IMNT_SHRLOOKUP;
   1137 	mp->mnt_fs_bshift = lfs_sb_getbshift(fs);
   1138 	mp->mnt_iflag |= IMNT_CAN_RWTORO;
   1139 	if (fs->um_maxsymlinklen > 0)
   1140 		mp->mnt_iflag |= IMNT_DTYPE;
   1141 	else
   1142 		fs->lfs_hasolddirfmt = true;
   1143 
   1144 	ump->um_mountp = mp;
   1145 	for (i = 0; i < ULFS_MAXQUOTAS; i++)
   1146 		ump->um_quotas[i] = NULLVP;
   1147 	spec_node_setmountedfs(devvp, mp);
   1148 
   1149 	/* Set up reserved memory for pageout */
   1150 	lfs_setup_resblks(fs);
   1151 	/* Set up vdirop tailq */
   1152 	TAILQ_INIT(&fs->lfs_dchainhd);
   1153 	/* and paging tailq */
   1154 	TAILQ_INIT(&fs->lfs_pchainhd);
   1155 	/* and delayed segment accounting for truncation list */
   1156 	LIST_INIT(&fs->lfs_segdhd);
   1157 
   1158 	/*
   1159 	 * We use the ifile vnode for almost every operation.  Instead of
   1160 	 * retrieving it from the hash table each time we retrieve it here,
   1161 	 * artificially increment the reference count and keep a pointer
   1162 	 * to it in the incore copy of the superblock.
   1163 	 */
   1164 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, LK_EXCLUSIVE, &vp)) != 0) {
   1165 		DLOG((DLOG_MOUNT, "lfs_mountfs: ifile vget failed, error=%d\n", error));
   1166 		goto out;
   1167 	}
   1168 	fs->lfs_ivnode = vp;
   1169 	vref(vp);
   1170 
   1171 	/* Set up inode bitmap, order free list, and gather orphans.  */
   1172 	lfs_order_freelist(fs, &orphan, &norphan);
   1173 
   1174 	/* Set up segment usage flags for the autocleaner. */
   1175 	fs->lfs_nactive = 0;
   1176 	fs->lfs_suflags = malloc(2 * sizeof(u_int32_t *),
   1177 				 M_SEGMENT, M_WAITOK);
   1178 	fs->lfs_suflags[0] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
   1179 				    M_SEGMENT, M_WAITOK);
   1180 	fs->lfs_suflags[1] = malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t),
   1181 				    M_SEGMENT, M_WAITOK);
   1182 	memset(fs->lfs_suflags[1], 0, lfs_sb_getnseg(fs) * sizeof(u_int32_t));
   1183 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
   1184 		int changed;
   1185 		struct buf *bp;
   1186 
   1187 		LFS_SEGENTRY(sup, fs, i, bp);
   1188 		changed = 0;
   1189 		if (!ronly) {
   1190 			if (sup->su_nbytes == 0 &&
   1191 			    !(sup->su_flags & SEGUSE_EMPTY)) {
   1192 				sup->su_flags |= SEGUSE_EMPTY;
   1193 				++changed;
   1194 			} else if (!(sup->su_nbytes == 0) &&
   1195 				   (sup->su_flags & SEGUSE_EMPTY)) {
   1196 				sup->su_flags &= ~SEGUSE_EMPTY;
   1197 				++changed;
   1198 			}
   1199 			if (sup->su_flags & (SEGUSE_ACTIVE|SEGUSE_INVAL)) {
   1200 				sup->su_flags &= ~(SEGUSE_ACTIVE|SEGUSE_INVAL);
   1201 				++changed;
   1202 			}
   1203 		}
   1204 		fs->lfs_suflags[0][i] = sup->su_flags;
   1205 		if (changed)
   1206 			LFS_WRITESEGENTRY(sup, fs, i, bp);
   1207 		else
   1208 			brelse(bp, 0);
   1209 	}
   1210 
   1211 	/* Free the orphans we discovered while ordering the freelist.  */
   1212 	lfs_free_orphans(fs, orphan, norphan);
   1213 
   1214 	/*
   1215 	 * XXX: if the fs has quotas, quotas should be on even if
   1216 	 * readonly. Otherwise you can't query the quota info!
   1217 	 * However, that's not how the quota2 code got written and I
   1218 	 * don't know if it'll behave itself if enabled while
   1219 	 * readonly, so for now use the same enable logic as ffs.
   1220 	 *
   1221 	 * XXX: also, if you use the -f behavior allowed here (and
   1222 	 * equivalently above for remount) it will corrupt the fs. It
   1223 	 * ought not to allow that. It should allow mounting readonly
   1224 	 * if there are quotas and the kernel doesn't have the quota
   1225 	 * code, but only readonly.
   1226 	 *
   1227 	 * XXX: and if you use the -f behavior allowed here it will
   1228 	 * likely crash at unmount time (or remount time) because we
   1229 	 * think quotas are active.
   1230 	 *
   1231 	 * Although none of this applies until there's a way to set
   1232 	 * lfs_use_quota2 and have quotas in the fs at all.
   1233 	 */
   1234 	if (!ronly && fs->lfs_use_quota2) {
   1235 #ifdef LFS_QUOTA2
   1236 		error = lfs_quota2_mount(mp);
   1237 #else
   1238 		uprintf("%s: no kernel support for this filesystem's quotas\n",
   1239 			mp->mnt_stat.f_mntonname);
   1240 		if (mp->mnt_flag & MNT_FORCE) {
   1241 			uprintf("%s: mounting anyway; fsck afterwards\n",
   1242 				mp->mnt_stat.f_mntonname);
   1243 		} else {
   1244 			error = EINVAL;
   1245 		}
   1246 #endif
   1247 		if (error) {
   1248 			/* XXX XXX must clean up the stuff immediately above */
   1249 			printf("lfs_mountfs: sorry, leaking some memory\n");
   1250 			goto out;
   1251 		}
   1252 	}
   1253 
   1254 #ifdef LFS_KERNEL_RFW
   1255 	lfs_roll_forward(fs, mp, l);
   1256 #endif
   1257 
   1258 	/* If writing, sb is not clean; record in case of immediate crash */
   1259 	if (!fs->lfs_ronly) {
   1260 		lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) & ~LFS_PF_CLEAN);
   1261 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
   1262 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
   1263 	}
   1264 
   1265 	/* Allow vget now that roll-forward is complete */
   1266 	fs->lfs_flags &= ~(LFS_NOTYET);
   1267 	wakeup(&fs->lfs_flags);
   1268 
   1269 	/*
   1270 	 * Initialize the ifile cleaner info with information from
   1271 	 * the superblock.
   1272 	 */
   1273 	{
   1274 		struct buf *bp;
   1275 
   1276 		LFS_CLEANERINFO(cip, fs, bp);
   1277 		lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
   1278 		lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
   1279 		lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
   1280 		lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
   1281 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
   1282 	}
   1283 
   1284 	/*
   1285 	 * Mark the current segment as ACTIVE, since we're going to
   1286 	 * be writing to it.
   1287 	 */
   1288 	{
   1289 		struct buf *bp;
   1290 
   1291 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp);
   1292 		sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1293 		fs->lfs_nactive++;
   1294 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getoffset(fs)), bp);  /* Ifile */
   1295 	}
   1296 
   1297 	/* Now that roll-forward is done, unlock the Ifile */
   1298 	vput(vp);
   1299 
   1300 	/* Start the pagedaemon-anticipating daemon */
   1301 	mutex_enter(&lfs_lock);
   1302 	if (lfs_writer_daemon == NULL &&
   1303 	    kthread_create(PRI_BIO, 0, NULL,
   1304 	    lfs_writerd, NULL, NULL, "lfs_writer") != 0)
   1305 		panic("fork lfs_writer");
   1306 	mutex_exit(&lfs_lock);
   1307 
   1308 	printf("WARNING: the log-structured file system is experimental\n"
   1309 	    "WARNING: it may cause system crashes and/or corrupt data\n");
   1310 
   1311 	return (0);
   1312 
   1313 out:
   1314 	if (primarybuf)
   1315 		brelse(primarybuf, BC_INVAL);
   1316 	if (altbuf)
   1317 		brelse(altbuf, BC_INVAL);
   1318 	if (ump) {
   1319 		kmem_free(ump->um_lfs, sizeof(struct lfs));
   1320 		kmem_free(ump, sizeof(*ump));
   1321 		mp->mnt_data = NULL;
   1322 	}
   1323 
   1324 	return (error);
   1325 }
   1326 
   1327 /*
   1328  * unmount system call
   1329  */
   1330 int
   1331 lfs_unmount(struct mount *mp, int mntflags)
   1332 {
   1333 	struct ulfsmount *ump;
   1334 	struct lfs *fs;
   1335 	int error, ronly;
   1336 
   1337 	ump = VFSTOULFS(mp);
   1338 	fs = ump->um_lfs;
   1339 
   1340 	error = lfs_flushfiles(mp, mntflags & MNT_FORCE ? FORCECLOSE : 0);
   1341 	if (error)
   1342 		return error;
   1343 
   1344 	/* Finish with the Ifile, now that we're done with it */
   1345 	vgone(fs->lfs_ivnode);
   1346 
   1347 	ronly = !fs->lfs_ronly;
   1348 	if (fs->lfs_devvp->v_type != VBAD)
   1349 		spec_node_setmountedfs(fs->lfs_devvp, NULL);
   1350 	vn_lock(fs->lfs_devvp, LK_EXCLUSIVE | LK_RETRY);
   1351 	error = VOP_CLOSE(fs->lfs_devvp,
   1352 	    ronly ? FREAD : FREAD|FWRITE, NOCRED);
   1353 	vput(fs->lfs_devvp);
   1354 
   1355 	/* Complain about page leakage */
   1356 	if (fs->lfs_pages > 0)
   1357 		printf("lfs_unmount: still claim %d pages (%d in subsystem)\n",
   1358 			fs->lfs_pages, lfs_subsys_pages);
   1359 
   1360 	/* Free per-mount data structures */
   1361 	free(fs->lfs_ino_bitmap, M_SEGMENT);
   1362 	free(fs->lfs_suflags[0], M_SEGMENT);
   1363 	free(fs->lfs_suflags[1], M_SEGMENT);
   1364 	free(fs->lfs_suflags, M_SEGMENT);
   1365 	lfs_free_resblks(fs);
   1366 	cv_destroy(&fs->lfs_sleeperscv);
   1367 	cv_destroy(&fs->lfs_diropscv);
   1368 	cv_destroy(&fs->lfs_stopcv);
   1369 	cv_destroy(&fs->lfs_nextsegsleep);
   1370 
   1371 	rw_destroy(&fs->lfs_fraglock);
   1372 	rw_destroy(&fs->lfs_iflock);
   1373 
   1374 	kmem_free(fs, sizeof(struct lfs));
   1375 	kmem_free(ump, sizeof(*ump));
   1376 
   1377 	mp->mnt_data = NULL;
   1378 	mp->mnt_flag &= ~MNT_LOCAL;
   1379 	return (error);
   1380 }
   1381 
   1382 static int
   1383 lfs_flushfiles(struct mount *mp, int flags)
   1384 {
   1385 	struct lwp *l = curlwp;
   1386 	struct ulfsmount *ump;
   1387 	struct lfs *fs;
   1388 	struct vnode *vp;
   1389 	int error;
   1390 
   1391 	ump = VFSTOULFS(mp);
   1392 	fs = ump->um_lfs;
   1393 
   1394 	/* Two checkpoints */
   1395 	if (!fs->lfs_ronly) {
   1396 		lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1397 		lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1398 	}
   1399 
   1400 	/* wake up the cleaner so it can die */
   1401 	/* XXX: shouldn't this be *after* the error cases below? */
   1402 	lfs_wakeup_cleaner(fs);
   1403 	mutex_enter(&lfs_lock);
   1404 	while (fs->lfs_sleepers)
   1405 		cv_wait(&fs->lfs_sleeperscv, &lfs_lock);
   1406 	mutex_exit(&lfs_lock);
   1407 
   1408 #ifdef LFS_EXTATTR
   1409 	if (ump->um_fstype == ULFS1) {
   1410 		if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) {
   1411 			ulfs_extattr_stop(mp, curlwp);
   1412 		}
   1413 		if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED) {
   1414 			ulfs_extattr_uepm_destroy(&ump->um_extattr);
   1415 			mp->mnt_flag &= ~MNT_EXTATTR;
   1416 		}
   1417 	}
   1418 #endif
   1419 #ifdef LFS_QUOTA
   1420         if ((error = lfsquota1_umount(mp, flags)) != 0)
   1421 		return (error);
   1422 #endif
   1423 #ifdef LFS_QUOTA2
   1424         if ((error = lfsquota2_umount(mp, flags)) != 0)
   1425 		return (error);
   1426 #endif
   1427 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
   1428 		return (error);
   1429 	if ((error = VFS_SYNC(mp, 1, l->l_cred)) != 0)
   1430 		return (error);
   1431 	vp = fs->lfs_ivnode;
   1432 	mutex_enter(vp->v_interlock);
   1433 	if (LIST_FIRST(&vp->v_dirtyblkhd))
   1434 		panic("lfs_unmount: still dirty blocks on ifile vnode");
   1435 	mutex_exit(vp->v_interlock);
   1436 
   1437 	/* Explicitly write the superblock, to update serial and pflags */
   1438 	if (!fs->lfs_ronly) {
   1439 		lfs_sb_setpflags(fs, lfs_sb_getpflags(fs) | LFS_PF_CLEAN);
   1440 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
   1441 		lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
   1442 	}
   1443 	mutex_enter(&lfs_lock);
   1444 	while (fs->lfs_iocount)
   1445 		mtsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0,
   1446 			&lfs_lock);
   1447 	mutex_exit(&lfs_lock);
   1448 
   1449 	return 0;
   1450 }
   1451 
   1452 /*
   1453  * Get file system statistics.
   1454  *
   1455  * NB: We don't lock to access the superblock here, because it's not
   1456  * really that important if we get it wrong.
   1457  */
   1458 int
   1459 lfs_statvfs(struct mount *mp, struct statvfs *sbp)
   1460 {
   1461 	struct lfs *fs;
   1462 	struct ulfsmount *ump;
   1463 
   1464 	ump = VFSTOULFS(mp);
   1465 	fs = ump->um_lfs;
   1466 
   1467 	sbp->f_bsize = lfs_sb_getbsize(fs);
   1468 	sbp->f_frsize = lfs_sb_getfsize(fs);
   1469 	sbp->f_iosize = lfs_sb_getbsize(fs);
   1470 	sbp->f_blocks = LFS_EST_NONMETA(fs) - VTOI(fs->lfs_ivnode)->i_lfs_effnblks;
   1471 
   1472 	sbp->f_bfree = LFS_EST_BFREE(fs);
   1473 	/*
   1474 	 * XXX this should be lfs_sb_getsize (measured in frags)
   1475 	 * rather than dsize (measured in diskblocks). However,
   1476 	 * getsize needs a format version check (for version 1 it
   1477 	 * needs to be blockstofrags'd) so for the moment I'm going to
   1478 	 * leave this...  it won't fire wrongly as frags are at least
   1479 	 * as big as diskblocks.
   1480 	 */
   1481 	KASSERT(sbp->f_bfree <= lfs_sb_getdsize(fs));
   1482 #if 0
   1483 	if (sbp->f_bfree < 0)
   1484 		sbp->f_bfree = 0;
   1485 #endif
   1486 
   1487 	sbp->f_bresvd = LFS_EST_RSVD(fs);
   1488 	if (sbp->f_bfree > sbp->f_bresvd)
   1489 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1490 	else
   1491 		sbp->f_bavail = 0;
   1492 
   1493 	/* XXX: huh? - dholland 20150728 */
   1494 	sbp->f_files = lfs_sb_getbfree(fs) / lfs_btofsb(fs, lfs_sb_getibsize(fs))
   1495 	    * LFS_INOPB(fs);
   1496 	sbp->f_ffree = sbp->f_files - lfs_sb_getnfiles(fs);
   1497 	sbp->f_favail = sbp->f_ffree;
   1498 	sbp->f_fresvd = 0;
   1499 	copy_statvfs_info(sbp, mp);
   1500 	return (0);
   1501 }
   1502 
   1503 /*
   1504  * Go through the disk queues to initiate sandbagged IO;
   1505  * go through the inodes to write those that have been modified;
   1506  * initiate the writing of the super block if it has been modified.
   1507  */
   1508 int
   1509 lfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1510 {
   1511 	int error;
   1512 	struct lfs *fs;
   1513 
   1514 	fs = VFSTOULFS(mp)->um_lfs;
   1515 	if (fs->lfs_ronly)
   1516 		return 0;
   1517 
   1518 	/* Snapshots should not hose the syncer */
   1519 	/*
   1520 	 * XXX Sync can block here anyway, since we don't have a very
   1521 	 * XXX good idea of how much data is pending.  If it's more
   1522 	 * XXX than a segment and lfs_nextseg is close to the end of
   1523 	 * XXX the log, we'll likely block.
   1524 	 */
   1525 	mutex_enter(&lfs_lock);
   1526 	if (fs->lfs_nowrap && lfs_sb_getnextseg(fs) < lfs_sb_getcurseg(fs)) {
   1527 		mutex_exit(&lfs_lock);
   1528 		return 0;
   1529 	}
   1530 	mutex_exit(&lfs_lock);
   1531 
   1532 	lfs_writer_enter(fs, "lfs_dirops");
   1533 
   1534 	/* All syncs must be checkpoints until roll-forward is implemented. */
   1535 	DLOG((DLOG_FLUSH, "lfs_sync at 0x%jx\n",
   1536 	      (uintmax_t)lfs_sb_getoffset(fs)));
   1537 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
   1538 	lfs_writer_leave(fs);
   1539 #ifdef LFS_QUOTA
   1540 	lfs_qsync(mp);
   1541 #endif
   1542 	return (error);
   1543 }
   1544 
   1545 /*
   1546  * Look up an LFS dinode number to find its incore vnode.  If not already
   1547  * in core, read it in from the specified device.  Return the inode locked.
   1548  * Detection and handling of mount points must be done by the calling routine.
   1549  */
   1550 int
   1551 lfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
   1552 {
   1553 	int error;
   1554 
   1555 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
   1556 	if (error)
   1557 		return error;
   1558 	error = vn_lock(*vpp, lktype);
   1559 	if (error) {
   1560 		vrele(*vpp);
   1561 		*vpp = NULL;
   1562 		return error;
   1563 	}
   1564 
   1565 	return 0;
   1566 }
   1567 
   1568 /*
   1569  * Create a new vnode/inode pair and initialize what fields we can.
   1570  */
   1571 static void
   1572 lfs_init_vnode(struct ulfsmount *ump, ino_t ino, struct vnode *vp)
   1573 {
   1574 	struct lfs *fs = ump->um_lfs;
   1575 	struct inode *ip;
   1576 	union lfs_dinode *dp;
   1577 
   1578 	ASSERT_NO_SEGLOCK(fs);
   1579 
   1580 	/* Initialize the inode. */
   1581 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
   1582 	memset(ip, 0, sizeof(*ip));
   1583 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
   1584 	memset(dp, 0, sizeof(*dp));
   1585 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
   1586 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
   1587 	ip->i_din = dp;
   1588 	ip->i_ump = ump;
   1589 	ip->i_vnode = vp;
   1590 	ip->i_dev = fs->lfs_dev;
   1591 	lfs_dino_setinumber(fs, dp, ino);
   1592 	ip->i_number = ino;
   1593 	ip->i_lfs = fs;
   1594 	ip->i_lfs_effnblks = 0;
   1595 	SPLAY_INIT(&ip->i_lfs_lbtree);
   1596 	ip->i_lfs_nbtree = 0;
   1597 	LIST_INIT(&ip->i_lfs_segdhd);
   1598 
   1599 	vp->v_tag = VT_LFS;
   1600 	vp->v_op = lfs_vnodeop_p;
   1601 	vp->v_data = ip;
   1602 }
   1603 
   1604 /*
   1605  * Undo lfs_init_vnode().
   1606  */
   1607 static void
   1608 lfs_deinit_vnode(struct ulfsmount *ump, struct vnode *vp)
   1609 {
   1610 	struct inode *ip = VTOI(vp);
   1611 
   1612 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1613 	pool_put(&lfs_dinode_pool, ip->i_din);
   1614 	pool_put(&lfs_inode_pool, ip);
   1615 	vp->v_data = NULL;
   1616 }
   1617 
   1618 /*
   1619  * Read an inode from disk and initialize this vnode / inode pair.
   1620  * Caller assures no other thread will try to load this inode.
   1621  */
   1622 int
   1623 lfs_loadvnode(struct mount *mp, struct vnode *vp,
   1624     const void *key, size_t key_len, const void **new_key)
   1625 {
   1626 	struct lfs *fs;
   1627 	union lfs_dinode *dip;
   1628 	struct inode *ip;
   1629 	struct buf *bp;
   1630 	IFILE *ifp;
   1631 	struct ulfsmount *ump;
   1632 	ino_t ino;
   1633 	daddr_t daddr;
   1634 	int error, retries;
   1635 	struct timespec ts;
   1636 
   1637 	KASSERT(key_len == sizeof(ino));
   1638 	memcpy(&ino, key, key_len);
   1639 
   1640 	memset(&ts, 0, sizeof ts);	/* XXX gcc */
   1641 
   1642 	ump = VFSTOULFS(mp);
   1643 	fs = ump->um_lfs;
   1644 
   1645 	/*
   1646 	 * If the filesystem is not completely mounted yet, suspend
   1647 	 * any access requests (wait for roll-forward to complete).
   1648 	 */
   1649 	mutex_enter(&lfs_lock);
   1650 	while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
   1651 		mtsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0,
   1652 			&lfs_lock);
   1653 	mutex_exit(&lfs_lock);
   1654 
   1655 	/* Translate the inode number to a disk address. */
   1656 	if (ino == LFS_IFILE_INUM)
   1657 		daddr = lfs_sb_getidaddr(fs);
   1658 	else {
   1659 		/* XXX bounds-check this too */
   1660 		LFS_IENTRY(ifp, fs, ino, bp);
   1661 		daddr = lfs_if_getdaddr(fs, ifp);
   1662 		if (lfs_sb_getversion(fs) > 1) {
   1663 			ts.tv_sec = lfs_if_getatime_sec(fs, ifp);
   1664 			ts.tv_nsec = lfs_if_getatime_nsec(fs, ifp);
   1665 		}
   1666 
   1667 		brelse(bp, 0);
   1668 		if (daddr == LFS_UNUSED_DADDR)
   1669 			return (ENOENT);
   1670 	}
   1671 
   1672 	/* Allocate/init new vnode/inode. */
   1673 	lfs_init_vnode(ump, ino, vp);
   1674 	ip = VTOI(vp);
   1675 
   1676 	/* If the cleaner supplied the inode, use it. */
   1677 	if (curlwp == fs->lfs_cleaner_thread && fs->lfs_cleaner_hint != NULL &&
   1678 	    fs->lfs_cleaner_hint->bi_lbn == LFS_UNUSED_LBN) {
   1679 		dip = fs->lfs_cleaner_hint->bi_bp;
   1680 		if (fs->lfs_is64) {
   1681 			error = copyin(dip, &ip->i_din->u_64,
   1682 				       sizeof(struct lfs64_dinode));
   1683 		} else {
   1684 			error = copyin(dip, &ip->i_din->u_32,
   1685 				       sizeof(struct lfs32_dinode));
   1686 		}
   1687 		if (error) {
   1688 			lfs_deinit_vnode(ump, vp);
   1689 			return error;
   1690 		}
   1691 		KASSERT(ip->i_number == ino);
   1692 		goto out;
   1693 	}
   1694 
   1695 	/* Read in the disk contents for the inode, copy into the inode. */
   1696 	retries = 0;
   1697 again:
   1698 	error = bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
   1699 		(lfs_sb_getversion(fs) == 1 ? lfs_sb_getbsize(fs) : lfs_sb_getibsize(fs)),
   1700 		0, &bp);
   1701 	if (error) {
   1702 		lfs_deinit_vnode(ump, vp);
   1703 		return error;
   1704 	}
   1705 
   1706 	dip = lfs_ifind(fs, ino, bp);
   1707 	if (dip == NULL) {
   1708 		/* Assume write has not completed yet; try again */
   1709 		brelse(bp, BC_INVAL);
   1710 		++retries;
   1711 		if (retries <= LFS_IFIND_RETRIES) {
   1712 			mutex_enter(&lfs_lock);
   1713 			if (fs->lfs_iocount) {
   1714 				DLOG((DLOG_VNODE,
   1715 				    "%s: dinode %d not found, retrying...\n",
   1716 				    __func__, ino));
   1717 				(void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
   1718 					      "lfs ifind", 1, &lfs_lock);
   1719 			} else
   1720 				retries = LFS_IFIND_RETRIES;
   1721 			mutex_exit(&lfs_lock);
   1722 			goto again;
   1723 		}
   1724 #ifdef DEBUG
   1725 		/* If the seglock is held look at the bpp to see
   1726 		   what is there anyway */
   1727 		mutex_enter(&lfs_lock);
   1728 		if (fs->lfs_seglock > 0) {
   1729 			struct buf **bpp;
   1730 			union lfs_dinode *dp;
   1731 			int i;
   1732 
   1733 			for (bpp = fs->lfs_sp->bpp;
   1734 			     bpp != fs->lfs_sp->cbpp; ++bpp) {
   1735 				if ((*bpp)->b_vp == fs->lfs_ivnode &&
   1736 				    bpp != fs->lfs_sp->bpp) {
   1737 					/* Inode block */
   1738 					printf("%s: block 0x%" PRIx64 ": ",
   1739 					       __func__, (*bpp)->b_blkno);
   1740 					for (i = 0; i < LFS_INOPB(fs); i++) {
   1741 						dp = DINO_IN_BLOCK(fs,
   1742 						    (*bpp)->b_data, i);
   1743 						if (lfs_dino_getinumber(fs, dp))
   1744 							printf("%ju ",
   1745 							    (uintmax_t)lfs_dino_getinumber(fs, dp));
   1746 					}
   1747 					printf("\n");
   1748 				}
   1749 			}
   1750 		}
   1751 		mutex_exit(&lfs_lock);
   1752 #endif /* DEBUG */
   1753 		panic("lfs_loadvnode: dinode not found");
   1754 	}
   1755 	lfs_copy_dinode(fs, ip->i_din, dip);
   1756 	brelse(bp, 0);
   1757 
   1758 out:
   1759 	if (lfs_sb_getversion(fs) > 1) {
   1760 		lfs_dino_setatime(fs, ip->i_din, ts.tv_sec);
   1761 		lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec);
   1762 	}
   1763 
   1764 	lfs_vinit(mp, &vp);
   1765 
   1766 	*new_key = &ip->i_number;
   1767 	return 0;
   1768 }
   1769 
   1770 /*
   1771  * Create a new inode and initialize this vnode / inode pair.
   1772  */
   1773 int
   1774 lfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
   1775     struct vattr *vap, kauth_cred_t cred, void *extra,
   1776     size_t *key_len, const void **new_key)
   1777 {
   1778 	ino_t ino;
   1779 	struct inode *ip;
   1780 	struct ulfsmount *ump;
   1781 	struct lfs *fs;
   1782 	int error, mode, gen;
   1783 
   1784 	KASSERT(dvp != NULL || vap->va_fileid > 0);
   1785 	KASSERT(dvp != NULL && dvp->v_mount == mp);
   1786 	KASSERT(vap->va_type != VNON);
   1787 
   1788 	*key_len = sizeof(ino);
   1789 	ump = VFSTOULFS(mp);
   1790 	fs = ump->um_lfs;
   1791 	mode = MAKEIMODE(vap->va_type, vap->va_mode);
   1792 
   1793 	/*
   1794 	 * Allocate fresh inode.  With "dvp == NULL" take the inode number
   1795 	 * and version from "vap".
   1796 	*/
   1797 	if (dvp == NULL) {
   1798 		ino = vap->va_fileid;
   1799 		gen = vap->va_gen;
   1800 		error = lfs_valloc_fixed(fs, ino, gen);
   1801 	} else {
   1802 		error = lfs_valloc(dvp, mode, cred, &ino, &gen);
   1803 	}
   1804 	if (error)
   1805 		return error;
   1806 
   1807 	/* Attach inode to vnode. */
   1808 	lfs_init_vnode(ump, ino, vp);
   1809 	ip = VTOI(vp);
   1810 
   1811 	mutex_enter(&lfs_lock);
   1812 	LFS_SET_UINO(ip, IN_CHANGE);
   1813 	mutex_exit(&lfs_lock);
   1814 
   1815 	/* Note no blocks yet */
   1816 	ip->i_lfs_hiblk = -1;
   1817 
   1818 	/* Set a new generation number for this inode. */
   1819 	ip->i_gen = gen;
   1820 	lfs_dino_setgen(fs, ip->i_din, gen);
   1821 
   1822 	memset(ip->i_lfs_fragsize, 0,
   1823 	    ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
   1824 
   1825 	/* Set uid / gid. */
   1826 	if (cred == NOCRED || cred == FSCRED) {
   1827 		ip->i_gid = 0;
   1828 		ip->i_uid = 0;
   1829 	} else {
   1830 		ip->i_gid = VTOI(dvp)->i_gid;
   1831 		ip->i_uid = kauth_cred_geteuid(cred);
   1832 	}
   1833 	DIP_ASSIGN(ip, gid, ip->i_gid);
   1834 	DIP_ASSIGN(ip, uid, ip->i_uid);
   1835 
   1836 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
   1837 	error = lfs_chkiq(ip, 1, cred, 0);
   1838 	if (error) {
   1839 		lfs_vfree(dvp, ino, mode);
   1840 		lfs_deinit_vnode(ump, vp);
   1841 
   1842 		return error;
   1843 	}
   1844 #endif
   1845 
   1846 	/* Set type and finalize. */
   1847 	ip->i_flags = 0;
   1848 	DIP_ASSIGN(ip, flags, 0);
   1849 	ip->i_mode = mode;
   1850 	DIP_ASSIGN(ip, mode, mode);
   1851 	if (vap->va_rdev != VNOVAL) {
   1852 		/*
   1853 		 * Want to be able to use this to make badblock
   1854 		 * inodes, so don't truncate the dev number.
   1855 		 */
   1856 		// XXX clean this up
   1857 		if (ump->um_fstype == ULFS1)
   1858 			ip->i_din->u_32.di_rdev = ulfs_rw32(vap->va_rdev,
   1859 			    ULFS_MPNEEDSWAP(fs));
   1860 		else
   1861 			ip->i_din->u_64.di_rdev = ulfs_rw64(vap->va_rdev,
   1862 			    ULFS_MPNEEDSWAP(fs));
   1863 	}
   1864 	lfs_vinit(mp, &vp);
   1865 
   1866 	*new_key = &ip->i_number;
   1867 	return 0;
   1868 }
   1869 
   1870 /*
   1871  * File handle to vnode
   1872  */
   1873 int
   1874 lfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
   1875 {
   1876 	struct lfid lfh;
   1877 	struct lfs *fs;
   1878 
   1879 	if (fhp->fid_len != sizeof(struct lfid))
   1880 		return EINVAL;
   1881 
   1882 	memcpy(&lfh, fhp, sizeof(lfh));
   1883 	if (lfh.lfid_ino < LFS_IFILE_INUM)
   1884 		return ESTALE;
   1885 
   1886 	fs = VFSTOULFS(mp)->um_lfs;
   1887 	if (lfh.lfid_ident != lfs_sb_getident(fs))
   1888 		return ESTALE;
   1889 
   1890 	if (lfh.lfid_ino >
   1891 	    ((lfs_dino_getsize(fs, VTOI(fs->lfs_ivnode)->i_din) >> lfs_sb_getbshift(fs)) -
   1892 	     lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs))
   1893 		return ESTALE;
   1894 
   1895 	return (ulfs_fhtovp(mp, &lfh.lfid_ufid, lktype, vpp));
   1896 }
   1897 
   1898 /*
   1899  * Vnode pointer to File handle
   1900  */
   1901 /* ARGSUSED */
   1902 int
   1903 lfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1904 {
   1905 	struct inode *ip;
   1906 	struct lfid lfh;
   1907 
   1908 	if (*fh_size < sizeof(struct lfid)) {
   1909 		*fh_size = sizeof(struct lfid);
   1910 		return E2BIG;
   1911 	}
   1912 	*fh_size = sizeof(struct lfid);
   1913 	ip = VTOI(vp);
   1914 	memset(&lfh, 0, sizeof(lfh));
   1915 	lfh.lfid_len = sizeof(struct lfid);
   1916 	lfh.lfid_ino = ip->i_number;
   1917 	lfh.lfid_gen = ip->i_gen;
   1918 	lfh.lfid_ident = lfs_sb_getident(ip->i_lfs);
   1919 	memcpy(fhp, &lfh, sizeof(lfh));
   1920 	return (0);
   1921 }
   1922 
   1923 /*
   1924  * ulfs_bmaparray callback function for writing.
   1925  *
   1926  * Since blocks will be written to the new segment anyway,
   1927  * we don't care about current daddr of them.
   1928  */
   1929 static bool
   1930 lfs_issequential_hole(const struct lfs *fs,
   1931     daddr_t daddr0, daddr_t daddr1)
   1932 {
   1933 	(void)fs; /* not used */
   1934 
   1935 	KASSERT(daddr0 == UNWRITTEN ||
   1936 	    (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR(fs)));
   1937 	KASSERT(daddr1 == UNWRITTEN ||
   1938 	    (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR(fs)));
   1939 
   1940 	/* NOTE: all we want to know here is 'hole or not'. */
   1941 	/* NOTE: UNASSIGNED is converted to 0 by ulfs_bmaparray. */
   1942 
   1943 	/*
   1944 	 * treat UNWRITTENs and all resident blocks as 'contiguous'
   1945 	 */
   1946 	if (daddr0 != 0 && daddr1 != 0)
   1947 		return true;
   1948 
   1949 	/*
   1950 	 * both are in hole?
   1951 	 */
   1952 	if (daddr0 == 0 && daddr1 == 0)
   1953 		return true; /* all holes are 'contiguous' for us. */
   1954 
   1955 	return false;
   1956 }
   1957 
   1958 /*
   1959  * lfs_gop_write functions exactly like genfs_gop_write, except that
   1960  * (1) it requires the seglock to be held by its caller, and sp->fip
   1961  *     to be properly initialized (it will return without re-initializing
   1962  *     sp->fip, and without calling lfs_writeseg).
   1963  * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
   1964  *     to determine how large a block it can write at once (though it does
   1965  *     still use VOP_BMAP to find holes in the file);
   1966  * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
   1967  *     (leaving lfs_writeseg to deal with the cluster blocks, so we might
   1968  *     now have clusters of clusters, ick.)
   1969  */
   1970 static int
   1971 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
   1972     int flags)
   1973 {
   1974 	int i, error, run, haveeof = 0;
   1975 	int fs_bshift;
   1976 	vaddr_t kva;
   1977 	off_t eof, offset, startoffset = 0;
   1978 	size_t bytes, iobytes, skipbytes;
   1979 	bool async = (flags & PGO_SYNCIO) == 0;
   1980 	daddr_t lbn, blkno;
   1981 	struct vm_page *pg;
   1982 	struct buf *mbp, *bp;
   1983 	struct vnode *devvp = VTOI(vp)->i_devvp;
   1984 	struct inode *ip = VTOI(vp);
   1985 	struct lfs *fs = ip->i_lfs;
   1986 	struct segment *sp = fs->lfs_sp;
   1987 	SEGSUM *ssp;
   1988 	UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
   1989 	const char * failreason = NULL;
   1990 
   1991 	ASSERT_SEGLOCK(fs);
   1992 
   1993 	/* The Ifile lives in the buffer cache */
   1994 	KASSERT(vp != fs->lfs_ivnode);
   1995 
   1996 	/*
   1997 	 * We don't want to fill the disk before the cleaner has a chance
   1998 	 * to make room for us.  If we're in danger of doing that, fail
   1999 	 * with EAGAIN.  The caller will have to notice this, unlock
   2000 	 * so the cleaner can run, relock and try again.
   2001 	 *
   2002 	 * We must write everything, however, if our vnode is being
   2003 	 * reclaimed.
   2004 	 */
   2005 	mutex_enter(vp->v_interlock);
   2006 	if (LFS_STARVED_FOR_SEGS(fs) && vdead_check(vp, VDEAD_NOWAIT) == 0) {
   2007 		mutex_exit(vp->v_interlock);
   2008 		failreason = "Starved for segs and not flushing vp";
   2009  		goto tryagain;
   2010 	}
   2011 	mutex_exit(vp->v_interlock);
   2012 
   2013 	/*
   2014 	 * Sometimes things slip past the filters in lfs_putpages,
   2015 	 * and the pagedaemon tries to write pages---problem is
   2016 	 * that the pagedaemon never acquires the segment lock.
   2017 	 *
   2018 	 * Alternatively, pages that were clean when we called
   2019 	 * genfs_putpages may have become dirty in the meantime.  In this
   2020 	 * case the segment header is not properly set up for blocks
   2021 	 * to be added to it.
   2022 	 *
   2023 	 * Unbusy and unclean the pages, and put them on the ACTIVE
   2024 	 * queue under the hypothesis that they couldn't have got here
   2025 	 * unless they were modified *quite* recently.
   2026 	 *
   2027 	 * XXXUBC that last statement is an oversimplification of course.
   2028 	 */
   2029 	if (!LFS_SEGLOCK_HELD(fs)) {
   2030 		failreason = "Seglock not held";
   2031 		goto tryagain;
   2032 	}
   2033 	if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) {
   2034 		failreason = "Inode with no_gop_write";
   2035 		goto tryagain;
   2036 	}
   2037 	if ((pgs[0]->offset & lfs_sb_getbmask(fs)) != 0) {
   2038 		failreason = "Bad page offset";
   2039 		goto tryagain;
   2040 	}
   2041 
   2042 	UVMHIST_LOG(ubchist, "vp %#jx pgs %#jx npages %jd flags 0x%jx",
   2043 	    (uintptr_t)vp, (uintptr_t)pgs, npages, flags);
   2044 
   2045 	GOP_SIZE(vp, vp->v_size, &eof, 0);
   2046 	haveeof = 1;
   2047 
   2048 	if (vp->v_type == VREG)
   2049 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   2050 	else
   2051 		fs_bshift = DEV_BSHIFT;
   2052 	error = 0;
   2053 	pg = pgs[0];
   2054 	startoffset = pg->offset;
   2055 	KASSERT(eof >= 0);
   2056 
   2057 	if (startoffset >= eof) {
   2058 		failreason = "Offset beyond EOF";
   2059 		goto tryagain;
   2060 	} else
   2061 		bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   2062 	skipbytes = 0;
   2063 
   2064 	KASSERT(bytes != 0);
   2065 
   2066 	/* Swap PG_DELWRI for PG_PAGEOUT */
   2067 	for (i = 0; i < npages; i++) {
   2068 		if (pgs[i]->flags & PG_DELWRI) {
   2069 			KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
   2070 			pgs[i]->flags &= ~PG_DELWRI;
   2071 			pgs[i]->flags |= PG_PAGEOUT;
   2072 			uvm_pageout_start(1);
   2073 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
   2074 			uvm_pagelock(pgs[i]);
   2075 			uvm_pageunwire(pgs[i]);
   2076 			uvm_pageunlock(pgs[i]);
   2077 			rw_exit(vp->v_uobj.vmobjlock);
   2078 		}
   2079 	}
   2080 
   2081 	/*
   2082 	 * Check to make sure we're starting on a block boundary.
   2083 	 * We'll check later to make sure we always write entire
   2084 	 * blocks (or fragments).
   2085 	 */
   2086 	if (startoffset & lfs_sb_getbmask(fs))
   2087 		printf("%" PRId64 " & %" PRIu64 " = %" PRId64 "\n",
   2088 		       startoffset, lfs_sb_getbmask(fs),
   2089 		       startoffset & lfs_sb_getbmask(fs));
   2090 	KASSERT((startoffset & lfs_sb_getbmask(fs)) == 0);
   2091 	if (bytes & lfs_sb_getffmask(fs)) {
   2092 		printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
   2093 		panic("lfs_gop_write: non-integer blocks");
   2094 	}
   2095 
   2096 	/*
   2097 	 * We could deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
   2098 	 * If we would, write what we have and try again.  If we don't
   2099 	 * have anything to write, we'll have to sleep.
   2100 	 */
   2101 	ssp = (SEGSUM *)sp->segsum;
   2102 	if ((kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   2103 				      (lfs_ss_getnfinfo(fs, ssp) < 1 ?
   2104 				       UVMPAGER_MAPIN_WAITOK : 0))) == 0x0) {
   2105 		DLOG((DLOG_PAGE, "lfs_gop_write: forcing write\n"));
   2106 #if 0
   2107 		      " with nfinfo=%d at offset 0x%jx\n",
   2108 		      (int)lfs_ss_getnfinfo(fs, ssp),
   2109 		      (uintmax_t)lfs_sb_getoffset(fs)));
   2110 #endif
   2111 		lfs_updatemeta(sp);
   2112 		lfs_release_finfo(fs);
   2113 		(void) lfs_writeseg(fs, sp);
   2114 
   2115 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2116 
   2117 		/*
   2118 		 * Having given up all of the pager_map we were holding,
   2119 		 * we can now wait for aiodoned to reclaim it for us
   2120 		 * without fear of deadlock.
   2121 		 */
   2122 		kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WRITE |
   2123 				     UVMPAGER_MAPIN_WAITOK);
   2124 	}
   2125 
   2126 	mbp = getiobuf(NULL, true);
   2127 	UVMHIST_LOG(ubchist, "vp %#jx mbp %#jx num now %jd bytes 0x%jx",
   2128 	    (uintptr_t)vp, (uintptr_t)mbp, vp->v_numoutput, bytes);
   2129 	mbp->b_bufsize = npages << PAGE_SHIFT;
   2130 	mbp->b_data = (void *)kva;
   2131 	mbp->b_resid = mbp->b_bcount = bytes;
   2132 	mbp->b_cflags |= BC_BUSY|BC_AGE;
   2133 	mbp->b_iodone = uvm_aio_aiodone;
   2134 
   2135 	bp = NULL;
   2136 	for (offset = startoffset;
   2137 	    bytes > 0;
   2138 	    offset += iobytes, bytes -= iobytes) {
   2139 		lbn = offset >> fs_bshift;
   2140 		error = ulfs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
   2141 		    lfs_issequential_hole);
   2142 		if (error) {
   2143 			UVMHIST_LOG(ubchist, "ulfs_bmaparray() -> %jd",
   2144 			    error,0,0,0);
   2145 			skipbytes += bytes;
   2146 			bytes = 0;
   2147 			break;
   2148 		}
   2149 
   2150 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   2151 		    bytes);
   2152 		if (blkno == (daddr_t)-1) {
   2153 			skipbytes += iobytes;
   2154 			continue;
   2155 		}
   2156 
   2157 		/*
   2158 		 * Discover how much we can really pack into this buffer.
   2159 		 */
   2160 		/* If no room in the current segment, finish it up */
   2161 		if (sp->sum_bytes_left < sizeof(int32_t) ||
   2162 		    sp->seg_bytes_left < (1 << lfs_sb_getbshift(fs))) {
   2163 			int vers;
   2164 
   2165 			lfs_updatemeta(sp);
   2166 			vers = lfs_fi_getversion(fs, sp->fip);
   2167 			lfs_release_finfo(fs);
   2168 			(void) lfs_writeseg(fs, sp);
   2169 
   2170 			lfs_acquire_finfo(fs, ip->i_number, vers);
   2171 		}
   2172 		/* Check both for space in segment and space in segsum */
   2173 		iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
   2174 					<< fs_bshift);
   2175 		iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
   2176 				       << fs_bshift);
   2177 		KASSERT(iobytes > 0);
   2178 
   2179 		/* if it's really one i/o, don't make a second buf */
   2180 		if (offset == startoffset && iobytes == bytes) {
   2181 			bp = mbp;
   2182 			/*
   2183 			 * All the LFS output is done by the segwriter.  It
   2184 			 * will increment numoutput by one for all the bufs it
   2185 			 * receives.  However this buffer needs one extra to
   2186 			 * account for aiodone.
   2187 			 */
   2188 			mutex_enter(vp->v_interlock);
   2189 			vp->v_numoutput++;
   2190 			mutex_exit(vp->v_interlock);
   2191 		} else {
   2192 			bp = getiobuf(NULL, true);
   2193 			UVMHIST_LOG(ubchist, "vp %#jx bp %#jx num now %jd",
   2194 			    (uintptr_t)vp, (uintptr_t)bp, vp->v_numoutput, 0);
   2195 			nestiobuf_setup(mbp, bp, offset - pg->offset, iobytes);
   2196 			/*
   2197 			 * LFS doesn't like async I/O here, dies with
   2198 			 * an assert in lfs_bwrite().  Is that assert
   2199 			 * valid?  I retained non-async behaviour when
   2200 			 * converted this to use nestiobuf --pooka
   2201 			 */
   2202 			bp->b_flags &= ~B_ASYNC;
   2203 		}
   2204 
   2205 		/* XXX This is silly ... is this necessary? */
   2206 		mutex_enter(&bufcache_lock);
   2207 		mutex_enter(vp->v_interlock);
   2208 		bgetvp(vp, bp);
   2209 		mutex_exit(vp->v_interlock);
   2210 		mutex_exit(&bufcache_lock);
   2211 
   2212 		bp->b_lblkno = lfs_lblkno(fs, offset);
   2213 		bp->b_private = mbp;
   2214 		if (devvp->v_type == VBLK) {
   2215 			bp->b_dev = devvp->v_rdev;
   2216 		}
   2217 		VOP_BWRITE(bp->b_vp, bp);
   2218 		while (lfs_gatherblock(sp, bp, NULL))
   2219 			continue;
   2220 	}
   2221 
   2222 	nestiobuf_done(mbp, skipbytes, error);
   2223 	if (skipbytes) {
   2224 		UVMHIST_LOG(ubchist, "skipbytes %jd", skipbytes, 0,0,0);
   2225 	}
   2226 	UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
   2227 
   2228 	if (!async) {
   2229 		/* Start a segment write. */
   2230 		UVMHIST_LOG(ubchist, "flushing", 0,0,0,0);
   2231 		mutex_enter(&lfs_lock);
   2232 		lfs_flush(fs, 0, 1);
   2233 		mutex_exit(&lfs_lock);
   2234 	}
   2235 
   2236 	if ((sp->seg_flags & SEGM_SINGLE) && lfs_sb_getcurseg(fs) != fs->lfs_startseg)
   2237 		return EAGAIN;
   2238 
   2239 	return (0);
   2240 
   2241     tryagain:
   2242 	/*
   2243 	 * We can't write the pages, for whatever reason.
   2244 	 * Clean up after ourselves, and make the caller try again.
   2245 	 */
   2246 	mutex_enter(vp->v_interlock);
   2247 
   2248 	/* Tell why we're here, if we know */
   2249 	if (failreason != NULL) {
   2250 		DLOG((DLOG_PAGE, "lfs_gop_write: %s\n", failreason));
   2251 	}
   2252 	if (haveeof && startoffset >= eof) {
   2253  		DLOG((DLOG_PAGE, "lfs_gop_write: ino %d start 0x%" PRIx64
   2254  		      " eof 0x%" PRIx64 " npages=%d\n", VTOI(vp)->i_number,
   2255  		      pgs[0]->offset, eof, npages));
   2256 	}
   2257 
   2258 	for (i = 0; i < npages; i++) {
   2259 		pg = pgs[i];
   2260 
   2261 		if (pg->flags & PG_PAGEOUT)
   2262 			uvm_pageout_done(1);
   2263 		uvm_pagelock(pg);
   2264 		if (pg->flags & PG_DELWRI) {
   2265 			uvm_pageunwire(pg);
   2266 		}
   2267 		uvm_pageactivate(pg);
   2268 		uvm_pageunlock(pg);
   2269 		pg->flags &= ~(PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
   2270 		uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
   2271 		DLOG((DLOG_PAGE, "pg[%d] = %p (vp %p off %" PRIx64 ")\n", i, pg,
   2272 			vp, pg->offset));
   2273 		DLOG((DLOG_PAGE, "pg[%d]->flags = %x\n", i, pg->flags));
   2274 		DLOG((DLOG_PAGE, "pg[%d]->pqflags = %x\n", i, pg->pqflags));
   2275 		DLOG((DLOG_PAGE, "pg[%d]->uanon = %p\n", i, pg->uanon));
   2276 		DLOG((DLOG_PAGE, "pg[%d]->uobject = %p\n", i, pg->uobject));
   2277 		DLOG((DLOG_PAGE, "pg[%d]->wire_count = %d\n", i,
   2278 		      pg->wire_count));
   2279 		DLOG((DLOG_PAGE, "pg[%d]->loan_count = %d\n", i,
   2280 		      pg->loan_count));
   2281 	}
   2282 	uvm_page_unbusy(pgs, npages);
   2283 	mutex_exit(vp->v_interlock);
   2284 	return EAGAIN;
   2285 }
   2286 
   2287 /*
   2288  * finish vnode/inode initialization.
   2289  * used by lfs_vget.
   2290  */
   2291 void
   2292 lfs_vinit(struct mount *mp, struct vnode **vpp)
   2293 {
   2294 	struct vnode *vp = *vpp;
   2295 	struct inode *ip = VTOI(vp);
   2296 	struct ulfsmount *ump = VFSTOULFS(mp);
   2297 	struct lfs *fs = ump->um_lfs;
   2298 	int i;
   2299 
   2300 	ip->i_mode = lfs_dino_getmode(fs, ip->i_din);
   2301 	ip->i_nlink = lfs_dino_getnlink(fs, ip->i_din);
   2302 	ip->i_lfs_osize = ip->i_size = lfs_dino_getsize(fs, ip->i_din);
   2303 	ip->i_flags = lfs_dino_getflags(fs, ip->i_din);
   2304 	ip->i_gen = lfs_dino_getgen(fs, ip->i_din);
   2305 	ip->i_uid = lfs_dino_getuid(fs, ip->i_din);
   2306 	ip->i_gid = lfs_dino_getgid(fs, ip->i_din);
   2307 
   2308 	ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din);
   2309 	ip->i_lfs_odnlink = lfs_dino_getnlink(fs, ip->i_din);
   2310 
   2311 	/*
   2312 	 * Initialize the vnode from the inode, check for aliases.  In all
   2313 	 * cases re-init ip, the underlying vnode/inode may have changed.
   2314 	 */
   2315 	ulfs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   2316 	ip = VTOI(vp);
   2317 
   2318 	memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
   2319 	if (vp->v_type != VLNK || ip->i_size >= ip->i_lfs->um_maxsymlinklen) {
   2320 #ifdef DEBUG
   2321 		for (i = (ip->i_size + lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs);
   2322 		    i < ULFS_NDADDR; i++) {
   2323 			if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
   2324 			    i == 0)
   2325 				continue;
   2326 			if (lfs_dino_getdb(fs, ip->i_din, i) != 0) {
   2327 				lfs_dump_dinode(fs, ip->i_din);
   2328 				panic("inconsistent inode (direct)");
   2329 			}
   2330 		}
   2331 		for ( ; i < ULFS_NDADDR + ULFS_NIADDR; i++) {
   2332 			if (lfs_dino_getib(fs, ip->i_din, i - ULFS_NDADDR) != 0) {
   2333 				lfs_dump_dinode(fs, ip->i_din);
   2334 				panic("inconsistent inode (indirect)");
   2335 			}
   2336 		}
   2337 #endif /* DEBUG */
   2338 		for (i = 0; i < ULFS_NDADDR; i++)
   2339 			if (lfs_dino_getdb(fs, ip->i_din, i) != 0)
   2340 				ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
   2341 	}
   2342 
   2343 	KASSERTMSG((vp->v_type != VNON),
   2344 	    "lfs_vinit: ino %llu is type VNON! (ifmt=%o)\n",
   2345 	    (unsigned long long)ip->i_number,
   2346 	    (ip->i_mode & LFS_IFMT) >> 12);
   2347 
   2348 	/*
   2349 	 * Finish inode initialization now that aliasing has been resolved.
   2350 	 */
   2351 
   2352 	ip->i_devvp = fs->lfs_devvp;
   2353 	vref(ip->i_devvp);
   2354 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
   2355 	ulfsquota_init(ip);
   2356 #endif
   2357 	genfs_node_init(vp, &lfs_genfsops);
   2358 	uvm_vnp_setsize(vp, ip->i_size);
   2359 
   2360 	/* Initialize hiblk from file size */
   2361 	ip->i_lfs_hiblk = lfs_lblkno(ip->i_lfs, ip->i_size + lfs_sb_getbsize(ip->i_lfs) - 1) - 1;
   2362 
   2363 	*vpp = vp;
   2364 }
   2365 
   2366 /*
   2367  * Resize the filesystem to contain the specified number of segments.
   2368  */
   2369 int
   2370 lfs_resize_fs(struct lfs *fs, int newnsegs)
   2371 {
   2372 	SEGUSE *sup;
   2373 	CLEANERINFO *cip;
   2374 	struct buf *bp, *obp;
   2375 	daddr_t olast, nlast, ilast, noff, start, end;
   2376 	struct vnode *ivp;
   2377 	struct inode *ip;
   2378 	int error, badnews, inc, oldnsegs;
   2379 	int sbbytes, csbbytes, gain, cgain;
   2380 	int i;
   2381 
   2382 	/* Only support v2 and up */
   2383 	if (lfs_sb_getversion(fs) < 2)
   2384 		return EOPNOTSUPP;
   2385 
   2386 	/* If we're doing nothing, do it fast */
   2387 	oldnsegs = lfs_sb_getnseg(fs);
   2388 	if (newnsegs == oldnsegs)
   2389 		return 0;
   2390 
   2391 	/* We always have to have two superblocks */
   2392 	if (newnsegs <= lfs_dtosn(fs, lfs_sb_getsboff(fs, 1)))
   2393 		/* XXX this error code is rather nonsense */
   2394 		return EFBIG;
   2395 
   2396 	ivp = fs->lfs_ivnode;
   2397 	ip = VTOI(ivp);
   2398 	error = 0;
   2399 
   2400 	/* Take the segment lock so no one else calls lfs_newseg() */
   2401 	lfs_seglock(fs, SEGM_PROT);
   2402 
   2403 	/*
   2404 	 * Make sure the segments we're going to be losing, if any,
   2405 	 * are in fact empty.  We hold the seglock, so their status
   2406 	 * cannot change underneath us.  Count the superblocks we lose,
   2407 	 * while we're at it.
   2408 	 */
   2409 	sbbytes = csbbytes = 0;
   2410 	cgain = 0;
   2411 	for (i = newnsegs; i < oldnsegs; i++) {
   2412 		LFS_SEGENTRY(sup, fs, i, bp);
   2413 		badnews = sup->su_nbytes || !(sup->su_flags & SEGUSE_INVAL);
   2414 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
   2415 			sbbytes += LFS_SBPAD;
   2416 		if (!(sup->su_flags & SEGUSE_DIRTY)) {
   2417 			++cgain;
   2418 			if (sup->su_flags & SEGUSE_SUPERBLOCK)
   2419 				csbbytes += LFS_SBPAD;
   2420 		}
   2421 		brelse(bp, 0);
   2422 		if (badnews) {
   2423 			error = EBUSY;
   2424 			goto out;
   2425 		}
   2426 	}
   2427 
   2428 	/* Note old and new segment table endpoints, and old ifile size */
   2429 	olast = lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs);
   2430 	nlast = howmany(newnsegs, lfs_sb_getsepb(fs)) + lfs_sb_getcleansz(fs);
   2431 	ilast = ivp->v_size >> lfs_sb_getbshift(fs);
   2432 	noff = nlast - olast;
   2433 
   2434 	/*
   2435 	 * Make sure no one can use the Ifile while we change it around.
   2436 	 * Even after taking the iflock we need to make sure no one still
   2437 	 * is holding Ifile buffers, so we get each one, to drain them.
   2438 	 * (XXX this could be done better.)
   2439 	 */
   2440 	rw_enter(&fs->lfs_iflock, RW_WRITER);
   2441 	for (i = 0; i < ilast; i++) {
   2442 		/* XXX what to do if bread fails? */
   2443 		bread(ivp, i, lfs_sb_getbsize(fs), 0, &bp);
   2444 		brelse(bp, 0);
   2445 	}
   2446 
   2447 	/* Allocate new Ifile blocks */
   2448 	for (i = ilast; i < ilast + noff; i++) {
   2449 		if (lfs_balloc(ivp, i * lfs_sb_getbsize(fs), lfs_sb_getbsize(fs), NOCRED, 0,
   2450 			       &bp) != 0)
   2451 			panic("balloc extending ifile");
   2452 		memset(bp->b_data, 0, lfs_sb_getbsize(fs));
   2453 		VOP_BWRITE(bp->b_vp, bp);
   2454 	}
   2455 
   2456 	/* Register new ifile size */
   2457 	ip->i_size += noff * lfs_sb_getbsize(fs);
   2458 	lfs_dino_setsize(fs, ip->i_din, ip->i_size);
   2459 	uvm_vnp_setsize(ivp, ip->i_size);
   2460 
   2461 	/* Copy the inode table to its new position */
   2462 	if (noff != 0) {
   2463 		if (noff < 0) {
   2464 			start = nlast;
   2465 			end = ilast + noff;
   2466 			inc = 1;
   2467 		} else {
   2468 			start = ilast + noff - 1;
   2469 			end = nlast - 1;
   2470 			inc = -1;
   2471 		}
   2472 		for (i = start; i != end; i += inc) {
   2473 			if (bread(ivp, i, lfs_sb_getbsize(fs),
   2474 			    B_MODIFY, &bp) != 0)
   2475 				panic("resize: bread dst blk failed");
   2476 			if (bread(ivp, i - noff, lfs_sb_getbsize(fs),
   2477 			    0, &obp))
   2478 				panic("resize: bread src blk failed");
   2479 			memcpy(bp->b_data, obp->b_data, lfs_sb_getbsize(fs));
   2480 			VOP_BWRITE(bp->b_vp, bp);
   2481 			brelse(obp, 0);
   2482 		}
   2483 	}
   2484 
   2485 	/* If we are expanding, write the new empty SEGUSE entries */
   2486 	if (newnsegs > oldnsegs) {
   2487 		for (i = oldnsegs; i < newnsegs; i++) {
   2488 			if ((error = bread(ivp, i / lfs_sb_getsepb(fs) +
   2489 					   lfs_sb_getcleansz(fs), lfs_sb_getbsize(fs),
   2490 					   B_MODIFY, &bp)) != 0)
   2491 				panic("lfs: ifile read: %d", error);
   2492 			while ((i + 1) % lfs_sb_getsepb(fs) && i < newnsegs) {
   2493 				sup = &((SEGUSE *)bp->b_data)[i % lfs_sb_getsepb(fs)];
   2494 				memset(sup, 0, sizeof(*sup));
   2495 				i++;
   2496 			}
   2497 			VOP_BWRITE(bp->b_vp, bp);
   2498 		}
   2499 	}
   2500 
   2501 	/* Zero out unused superblock offsets */
   2502 	for (i = 2; i < LFS_MAXNUMSB; i++)
   2503 		if (lfs_dtosn(fs, lfs_sb_getsboff(fs, i)) >= newnsegs)
   2504 			lfs_sb_setsboff(fs, i, 0x0);
   2505 
   2506 	/*
   2507 	 * Correct superblock entries that depend on fs size.
   2508 	 * The computations of these are as follows:
   2509 	 *
   2510 	 * size  = lfs_segtod(fs, nseg)
   2511 	 * dsize = lfs_segtod(fs, nseg - minfreeseg) - lfs_btofsb(#super * LFS_SBPAD)
   2512 	 * bfree = dsize - lfs_btofsb(fs, bsize * nseg / 2) - blocks_actually_used
   2513 	 * avail = lfs_segtod(fs, nclean) - lfs_btofsb(#clean_super * LFS_SBPAD)
   2514 	 *         + (lfs_segtod(fs, 1) - (offset - curseg))
   2515 	 *	   - lfs_segtod(fs, minfreeseg - (minfreeseg / 2))
   2516 	 *
   2517 	 * XXX - we should probably adjust minfreeseg as well.
   2518 	 */
   2519 	gain = (newnsegs - oldnsegs);
   2520 	lfs_sb_setnseg(fs, newnsegs);
   2521 	lfs_sb_setsegtabsz(fs, nlast - lfs_sb_getcleansz(fs));
   2522 	lfs_sb_addsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
   2523 	lfs_sb_adddsize(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes));
   2524 	lfs_sb_addbfree(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)) - lfs_btofsb(fs, sbbytes)
   2525 		       - gain * lfs_btofsb(fs, lfs_sb_getbsize(fs) / 2));
   2526 	if (gain > 0) {
   2527 		lfs_sb_addnclean(fs, gain);
   2528 		lfs_sb_addavail(fs, gain * lfs_btofsb(fs, lfs_sb_getssize(fs)));
   2529 	} else {
   2530 		lfs_sb_subnclean(fs, cgain);
   2531 		lfs_sb_subavail(fs, cgain * lfs_btofsb(fs, lfs_sb_getssize(fs)) -
   2532 				 lfs_btofsb(fs, csbbytes));
   2533 	}
   2534 
   2535 	/* Resize segment flag cache */
   2536 	fs->lfs_suflags[0] = realloc(fs->lfs_suflags[0],
   2537 	    lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
   2538 	fs->lfs_suflags[1] = realloc(fs->lfs_suflags[1],
   2539 	    lfs_sb_getnseg(fs) * sizeof(u_int32_t), M_SEGMENT, M_WAITOK);
   2540 	for (i = oldnsegs; i < newnsegs; i++)
   2541 		fs->lfs_suflags[0][i] = fs->lfs_suflags[1][i] = 0x0;
   2542 
   2543 	/* Truncate Ifile if necessary */
   2544 	if (noff < 0)
   2545 		lfs_truncate(ivp, ivp->v_size + (noff << lfs_sb_getbshift(fs)), 0,
   2546 		    NOCRED);
   2547 
   2548 	/* Update cleaner info so the cleaner can die */
   2549 	/* XXX what to do if bread fails? */
   2550 	bread(ivp, 0, lfs_sb_getbsize(fs), B_MODIFY, &bp);
   2551 	cip = bp->b_data;
   2552 	lfs_ci_setclean(fs, cip, lfs_sb_getnclean(fs));
   2553 	lfs_ci_setdirty(fs, cip, lfs_sb_getnseg(fs) - lfs_sb_getnclean(fs));
   2554 	VOP_BWRITE(bp->b_vp, bp);
   2555 
   2556 	/* Let Ifile accesses proceed */
   2557 	rw_exit(&fs->lfs_iflock);
   2558 
   2559     out:
   2560 	lfs_segunlock(fs);
   2561 	return error;
   2562 }
   2563 
   2564 /*
   2565  * Extended attribute dispatch
   2566  */
   2567 int
   2568 lfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
   2569 	       int attrnamespace, const char *attrname)
   2570 {
   2571 #ifdef LFS_EXTATTR
   2572 	struct ulfsmount *ump;
   2573 
   2574 	ump = VFSTOULFS(mp);
   2575 	if (ump->um_fstype == ULFS1) {
   2576 		return ulfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
   2577 	}
   2578 #endif
   2579 	return vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname);
   2580 }
   2581