Home | History | Annotate | Line # | Download | only in kern
vfs_mount.c revision 1.55
      1 /*	$NetBSD: vfs_mount.c,v 1.55 2017/04/17 08:31:02 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
     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 /*
     34  * Copyright (c) 1989, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.55 2017/04/17 08:31:02 hannken Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/kernel.h>
     74 
     75 #include <sys/atomic.h>
     76 #include <sys/buf.h>
     77 #include <sys/conf.h>
     78 #include <sys/fcntl.h>
     79 #include <sys/filedesc.h>
     80 #include <sys/device.h>
     81 #include <sys/kauth.h>
     82 #include <sys/kmem.h>
     83 #include <sys/module.h>
     84 #include <sys/mount.h>
     85 #include <sys/fstrans.h>
     86 #include <sys/namei.h>
     87 #include <sys/extattr.h>
     88 #include <sys/syscallargs.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/systm.h>
     91 #include <sys/vfs_syscalls.h>
     92 #include <sys/vnode_impl.h>
     93 
     94 #include <miscfs/genfs/genfs.h>
     95 #include <miscfs/specfs/specdev.h>
     96 
     97 enum mountlist_type {
     98 	ME_MOUNT,
     99 	ME_MARKER
    100 };
    101 struct mountlist_entry {
    102 	TAILQ_ENTRY(mountlist_entry) me_list;	/* Mount list. */
    103 	struct mount *me_mount;			/* Actual mount if ME_MOUNT,
    104 						   current mount else. */
    105 	enum mountlist_type me_type;		/* Mount or marker. */
    106 };
    107 struct mount_iterator {
    108 	struct mountlist_entry mi_entry;
    109 };
    110 
    111 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *,
    112     bool (*)(void *, struct vnode *), void *, bool);
    113 
    114 /* Root filesystem. */
    115 vnode_t *			rootvnode;
    116 
    117 /* Mounted filesystem list. */
    118 static TAILQ_HEAD(mountlist, mountlist_entry) mountlist;
    119 static kmutex_t			mountlist_lock;
    120 int vnode_offset_next_by_lru	/* XXX: ugly hack for pstat.c */
    121     = offsetof(vnode_impl_t, vi_lrulist.tqe_next);
    122 
    123 kmutex_t			mntvnode_lock;
    124 kmutex_t			vfs_list_lock;
    125 
    126 static specificdata_domain_t	mount_specificdata_domain;
    127 static kmutex_t			mntid_lock;
    128 
    129 static kmutex_t			mountgen_lock;
    130 static uint64_t			mountgen;
    131 
    132 void
    133 vfs_mount_sysinit(void)
    134 {
    135 
    136 	TAILQ_INIT(&mountlist);
    137 	mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
    138 	mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
    139 	mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
    140 
    141 	mount_specificdata_domain = specificdata_domain_create();
    142 	mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
    143 	mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
    144 	mountgen = 0;
    145 }
    146 
    147 struct mount *
    148 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp)
    149 {
    150 	struct mount *mp;
    151 	int error __diagused;
    152 
    153 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
    154 	if (mp == NULL)
    155 		return NULL;
    156 
    157 	mp->mnt_op = vfsops;
    158 	mp->mnt_refcnt = 1;
    159 	TAILQ_INIT(&mp->mnt_vnodelist);
    160 	mutex_init(&mp->mnt_unmounting, MUTEX_DEFAULT, IPL_NONE);
    161 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    162 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    163 	error = vfs_busy(mp, NULL);
    164 	KASSERT(error == 0);
    165 	mp->mnt_vnodecovered = vp;
    166 	mount_initspecific(mp);
    167 
    168 	mutex_enter(&mountgen_lock);
    169 	mp->mnt_gen = mountgen++;
    170 	mutex_exit(&mountgen_lock);
    171 
    172 	return mp;
    173 }
    174 
    175 /*
    176  * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and
    177  * initialize a mount structure for it.
    178  *
    179  * Devname is usually updated by mount(8) after booting.
    180  */
    181 int
    182 vfs_rootmountalloc(const char *fstypename, const char *devname,
    183     struct mount **mpp)
    184 {
    185 	struct vfsops *vfsp = NULL;
    186 	struct mount *mp;
    187 
    188 	mutex_enter(&vfs_list_lock);
    189 	LIST_FOREACH(vfsp, &vfs_list, vfs_list)
    190 		if (!strncmp(vfsp->vfs_name, fstypename,
    191 		    sizeof(mp->mnt_stat.f_fstypename)))
    192 			break;
    193 	if (vfsp == NULL) {
    194 		mutex_exit(&vfs_list_lock);
    195 		return (ENODEV);
    196 	}
    197 	vfsp->vfs_refcount++;
    198 	mutex_exit(&vfs_list_lock);
    199 
    200 	if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
    201 		return ENOMEM;
    202 	mp->mnt_flag = MNT_RDONLY;
    203 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
    204 	    sizeof(mp->mnt_stat.f_fstypename));
    205 	mp->mnt_stat.f_mntonname[0] = '/';
    206 	mp->mnt_stat.f_mntonname[1] = '\0';
    207 	mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
    208 	    '\0';
    209 	(void)copystr(devname, mp->mnt_stat.f_mntfromname,
    210 	    sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
    211 	*mpp = mp;
    212 	return 0;
    213 }
    214 
    215 /*
    216  * vfs_getnewfsid: get a new unique fsid.
    217  */
    218 void
    219 vfs_getnewfsid(struct mount *mp)
    220 {
    221 	static u_short xxxfs_mntid;
    222 	fsid_t tfsid;
    223 	int mtype;
    224 
    225 	mutex_enter(&mntid_lock);
    226 	mtype = makefstype(mp->mnt_op->vfs_name);
    227 	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
    228 	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
    229 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    230 	if (xxxfs_mntid == 0)
    231 		++xxxfs_mntid;
    232 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
    233 	tfsid.__fsid_val[1] = mtype;
    234 	while (vfs_getvfs(&tfsid)) {
    235 		tfsid.__fsid_val[0]++;
    236 		xxxfs_mntid++;
    237 	}
    238 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
    239 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    240 	mutex_exit(&mntid_lock);
    241 }
    242 
    243 /*
    244  * Lookup a mount point by filesystem identifier.
    245  *
    246  * XXX Needs to add a reference to the mount point.
    247  */
    248 struct mount *
    249 vfs_getvfs(fsid_t *fsid)
    250 {
    251 	mount_iterator_t *iter;
    252 	struct mount *mp;
    253 
    254 	mountlist_iterator_init(&iter);
    255 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    256 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
    257 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
    258 			mountlist_iterator_destroy(iter);
    259 			return mp;
    260 		}
    261 	}
    262 	mountlist_iterator_destroy(iter);
    263 	return NULL;
    264 }
    265 
    266 /*
    267  * Take a reference to a mount structure.
    268  */
    269 void
    270 vfs_ref(struct mount *mp)
    271 {
    272 
    273 	KASSERT(mp->mnt_refcnt > 0 || mutex_owned(&mountlist_lock));
    274 
    275 	atomic_inc_uint(&mp->mnt_refcnt);
    276 }
    277 
    278 /*
    279  * Drop a reference to a mount structure, freeing if the last reference.
    280  */
    281 void
    282 vfs_rele(struct mount *mp)
    283 {
    284 
    285 	if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
    286 		return;
    287 	}
    288 
    289 	/*
    290 	 * Nothing else has visibility of the mount: we can now
    291 	 * free the data structures.
    292 	 */
    293 	KASSERT(mp->mnt_refcnt == 0);
    294 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
    295 	mutex_destroy(&mp->mnt_unmounting);
    296 	mutex_destroy(&mp->mnt_updating);
    297 	mutex_destroy(&mp->mnt_renamelock);
    298 	if (mp->mnt_op != NULL) {
    299 		vfs_delref(mp->mnt_op);
    300 	}
    301 	kmem_free(mp, sizeof(*mp));
    302 }
    303 
    304 /*
    305  * Mark a mount point as busy, and gain a new reference to it.  Used to
    306  * prevent the file system from being unmounted during critical sections.
    307  *
    308  * vfs_busy can be called multiple times and by multiple threads
    309  * and must be accompanied by the same number of vfs_unbusy calls.
    310  *
    311  * => The caller must hold a pre-existing reference to the mount.
    312  * => Will fail if the file system is being unmounted, or is unmounted.
    313  */
    314 int
    315 vfs_busy(struct mount *mp, struct mount **nextp)
    316 {
    317 
    318 	KASSERT(mp->mnt_refcnt > 0);
    319 
    320 	KASSERT(nextp == NULL);
    321 
    322 	mutex_enter(&mp->mnt_unmounting);
    323 	if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
    324 		mutex_exit(&mp->mnt_unmounting);
    325 		return ENOENT;
    326 	}
    327 	++mp->mnt_busynest;
    328 	KASSERT(mp->mnt_busynest != 0);
    329 	mutex_exit(&mp->mnt_unmounting);
    330 	vfs_ref(mp);
    331 	return 0;
    332 }
    333 
    334 /*
    335  * Unbusy a busy filesystem.
    336  *
    337  * Every successful vfs_busy() call must be undone by a vfs_unbusy() call.
    338  *
    339  * => If keepref is true, preserve reference added by vfs_busy().
    340  * => If nextp != NULL, acquire mountlist_lock.
    341  */
    342 void
    343 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp)
    344 {
    345 
    346 	KASSERT(mp->mnt_refcnt > 0);
    347 
    348 	KASSERT(nextp == NULL);
    349 
    350 	mutex_enter(&mp->mnt_unmounting);
    351 	KASSERT(mp->mnt_busynest != 0);
    352 	mp->mnt_busynest--;
    353 	mutex_exit(&mp->mnt_unmounting);
    354 	if (!keepref) {
    355 		vfs_rele(mp);
    356 	}
    357 }
    358 
    359 struct vnode_iterator {
    360 	vnode_impl_t vi_vnode;
    361 };
    362 
    363 void
    364 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vnip)
    365 {
    366 	vnode_t *vp;
    367 	vnode_impl_t *vip;
    368 
    369 	vp = vnalloc_marker(mp);
    370 	vip = VNODE_TO_VIMPL(vp);
    371 
    372 	mutex_enter(&mntvnode_lock);
    373 	TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vip, vi_mntvnodes);
    374 	vp->v_usecount = 1;
    375 	mutex_exit(&mntvnode_lock);
    376 
    377 	*vnip = (struct vnode_iterator *)vip;
    378 }
    379 
    380 void
    381 vfs_vnode_iterator_destroy(struct vnode_iterator *vni)
    382 {
    383 	vnode_impl_t *mvip = &vni->vi_vnode;
    384 	vnode_t *mvp = VIMPL_TO_VNODE(mvip);
    385 
    386 	mutex_enter(&mntvnode_lock);
    387 	KASSERT(vnis_marker(mvp));
    388 	if (mvp->v_usecount != 0) {
    389 		TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvip, vi_mntvnodes);
    390 		mvp->v_usecount = 0;
    391 	}
    392 	mutex_exit(&mntvnode_lock);
    393 	vnfree_marker(mvp);
    394 }
    395 
    396 static struct vnode *
    397 vfs_vnode_iterator_next1(struct vnode_iterator *vni,
    398     bool (*f)(void *, struct vnode *), void *cl, bool do_wait)
    399 {
    400 	vnode_impl_t *mvip = &vni->vi_vnode;
    401 	struct mount *mp = VIMPL_TO_VNODE(mvip)->v_mount;
    402 	vnode_t *vp;
    403 	vnode_impl_t *vip;
    404 	int error;
    405 
    406 	KASSERT(vnis_marker(VIMPL_TO_VNODE(mvip)));
    407 
    408 	do {
    409 		mutex_enter(&mntvnode_lock);
    410 		vip = TAILQ_NEXT(mvip, vi_mntvnodes);
    411 		TAILQ_REMOVE(&mp->mnt_vnodelist, mvip, vi_mntvnodes);
    412 		VIMPL_TO_VNODE(mvip)->v_usecount = 0;
    413 again:
    414 		vp = VIMPL_TO_VNODE(vip);
    415 		if (vp == NULL) {
    416 	       		mutex_exit(&mntvnode_lock);
    417 	       		return NULL;
    418 		}
    419 		mutex_enter(vp->v_interlock);
    420 		if (vnis_marker(vp) ||
    421 		    vdead_check(vp, (do_wait ? 0 : VDEAD_NOWAIT)) ||
    422 		    (f && !(*f)(cl, vp))) {
    423 			mutex_exit(vp->v_interlock);
    424 			vip = TAILQ_NEXT(vip, vi_mntvnodes);
    425 			goto again;
    426 		}
    427 
    428 		TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vip, mvip, vi_mntvnodes);
    429 		VIMPL_TO_VNODE(mvip)->v_usecount = 1;
    430 		mutex_exit(&mntvnode_lock);
    431 		error = vcache_vget(vp);
    432 		KASSERT(error == 0 || error == ENOENT);
    433 	} while (error != 0);
    434 
    435 	return vp;
    436 }
    437 
    438 struct vnode *
    439 vfs_vnode_iterator_next(struct vnode_iterator *vni,
    440     bool (*f)(void *, struct vnode *), void *cl)
    441 {
    442 
    443 	return vfs_vnode_iterator_next1(vni, f, cl, false);
    444 }
    445 
    446 /*
    447  * Move a vnode from one mount queue to another.
    448  */
    449 void
    450 vfs_insmntque(vnode_t *vp, struct mount *mp)
    451 {
    452 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    453 	struct mount *omp;
    454 
    455 	KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
    456 	    vp->v_tag == VT_VFS);
    457 
    458 	mutex_enter(&mntvnode_lock);
    459 	/*
    460 	 * Delete from old mount point vnode list, if on one.
    461 	 */
    462 	if ((omp = vp->v_mount) != NULL)
    463 		TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vip, vi_mntvnodes);
    464 	/*
    465 	 * Insert into list of vnodes for the new mount point, if
    466 	 * available.  The caller must take a reference on the mount
    467 	 * structure and donate to the vnode.
    468 	 */
    469 	if ((vp->v_mount = mp) != NULL)
    470 		TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vip, vi_mntvnodes);
    471 	mutex_exit(&mntvnode_lock);
    472 
    473 	if (omp != NULL) {
    474 		/* Release reference to old mount. */
    475 		vfs_rele(omp);
    476 	}
    477 }
    478 
    479 /*
    480  * Remove any vnodes in the vnode table belonging to mount point mp.
    481  *
    482  * If FORCECLOSE is not specified, there should not be any active ones,
    483  * return error if any are found (nb: this is a user error, not a
    484  * system error). If FORCECLOSE is specified, detach any active vnodes
    485  * that are found.
    486  *
    487  * If WRITECLOSE is set, only flush out regular file vnodes open for
    488  * writing.
    489  *
    490  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
    491  */
    492 #ifdef DEBUG
    493 int busyprt = 0;	/* print out busy vnodes */
    494 struct ctldebug debug1 = { "busyprt", &busyprt };
    495 #endif
    496 
    497 static vnode_t *
    498 vflushnext(struct vnode_iterator *marker, int *when)
    499 {
    500 	if (hardclock_ticks > *when) {
    501 		yield();
    502 		*when = hardclock_ticks + hz / 10;
    503 	}
    504 	return vfs_vnode_iterator_next1(marker, NULL, NULL, true);
    505 }
    506 
    507 /*
    508  * Flush one vnode.  Referenced on entry, unreferenced on return.
    509  */
    510 static int
    511 vflush_one(vnode_t *vp, vnode_t *skipvp, int flags)
    512 {
    513 	int error;
    514 	struct vattr vattr;
    515 
    516 	if (vp == skipvp ||
    517 	    ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))) {
    518 		vrele(vp);
    519 		return 0;
    520 	}
    521 	/*
    522 	 * If WRITECLOSE is set, only flush out regular file
    523 	 * vnodes open for writing or open and unlinked.
    524 	 */
    525 	if ((flags & WRITECLOSE)) {
    526 		if (vp->v_type != VREG) {
    527 			vrele(vp);
    528 			return 0;
    529 		}
    530 		error = vn_lock(vp, LK_EXCLUSIVE);
    531 		if (error) {
    532 			KASSERT(error == ENOENT);
    533 			vrele(vp);
    534 			return 0;
    535 		}
    536 		error = VOP_FSYNC(vp, curlwp->l_cred, FSYNC_WAIT, 0, 0);
    537 		if (error == 0)
    538 			error = VOP_GETATTR(vp, &vattr, curlwp->l_cred);
    539 		VOP_UNLOCK(vp);
    540 		if (error) {
    541 			vrele(vp);
    542 			return error;
    543 		}
    544 		if (vp->v_writecount == 0 && vattr.va_nlink > 0) {
    545 			vrele(vp);
    546 			return 0;
    547 		}
    548 	}
    549 	/*
    550 	 * First try to recycle the vnode.
    551 	 */
    552 	if (vrecycle(vp))
    553 		return 0;
    554 	/*
    555 	 * If FORCECLOSE is set, forcibly close the vnode.
    556 	 */
    557 	if (flags & FORCECLOSE) {
    558 		vgone(vp);
    559 		return 0;
    560 	}
    561 	vrele(vp);
    562 	return EBUSY;
    563 }
    564 
    565 int
    566 vflush(struct mount *mp, vnode_t *skipvp, int flags)
    567 {
    568 	vnode_t *vp;
    569 	struct vnode_iterator *marker;
    570 	int busy, error, when, retries = 2;
    571 
    572 	do {
    573 		busy = error = when = 0;
    574 
    575 		/*
    576 		 * First, flush out any vnode references from the
    577 		 * deferred vrele list.
    578 		 */
    579 		vfs_drainvnodes();
    580 
    581 		vfs_vnode_iterator_init(mp, &marker);
    582 
    583 		while ((vp = vflushnext(marker, &when)) != NULL) {
    584 			error = vflush_one(vp, skipvp, flags);
    585 			if (error == EBUSY) {
    586 				error = 0;
    587 				busy++;
    588 #ifdef DEBUG
    589 				if (busyprt && retries == 0)
    590 					vprint("vflush: busy vnode", vp);
    591 #endif
    592 			} else if (error != 0) {
    593 				break;
    594 			}
    595 		}
    596 
    597 		vfs_vnode_iterator_destroy(marker);
    598 	} while (error == 0 && busy > 0 && retries-- > 0);
    599 
    600 	if (error)
    601 		return error;
    602 	if (busy)
    603 		return EBUSY;
    604 	return 0;
    605 }
    606 
    607 /*
    608  * Mount a file system.
    609  */
    610 
    611 /*
    612  * Scan all active processes to see if any of them have a current or root
    613  * directory onto which the new filesystem has just been  mounted. If so,
    614  * replace them with the new mount point.
    615  */
    616 static void
    617 mount_checkdirs(vnode_t *olddp)
    618 {
    619 	vnode_t *newdp, *rele1, *rele2;
    620 	struct cwdinfo *cwdi;
    621 	struct proc *p;
    622 	bool retry;
    623 
    624 	if (olddp->v_usecount == 1) {
    625 		return;
    626 	}
    627 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    628 		panic("mount: lost mount");
    629 
    630 	do {
    631 		retry = false;
    632 		mutex_enter(proc_lock);
    633 		PROCLIST_FOREACH(p, &allproc) {
    634 			if ((cwdi = p->p_cwdi) == NULL)
    635 				continue;
    636 			/*
    637 			 * Cannot change to the old directory any more,
    638 			 * so even if we see a stale value it is not a
    639 			 * problem.
    640 			 */
    641 			if (cwdi->cwdi_cdir != olddp &&
    642 			    cwdi->cwdi_rdir != olddp)
    643 				continue;
    644 			retry = true;
    645 			rele1 = NULL;
    646 			rele2 = NULL;
    647 			atomic_inc_uint(&cwdi->cwdi_refcnt);
    648 			mutex_exit(proc_lock);
    649 			rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    650 			if (cwdi->cwdi_cdir == olddp) {
    651 				rele1 = cwdi->cwdi_cdir;
    652 				vref(newdp);
    653 				cwdi->cwdi_cdir = newdp;
    654 			}
    655 			if (cwdi->cwdi_rdir == olddp) {
    656 				rele2 = cwdi->cwdi_rdir;
    657 				vref(newdp);
    658 				cwdi->cwdi_rdir = newdp;
    659 			}
    660 			rw_exit(&cwdi->cwdi_lock);
    661 			cwdfree(cwdi);
    662 			if (rele1 != NULL)
    663 				vrele(rele1);
    664 			if (rele2 != NULL)
    665 				vrele(rele2);
    666 			mutex_enter(proc_lock);
    667 			break;
    668 		}
    669 		mutex_exit(proc_lock);
    670 	} while (retry);
    671 
    672 	if (rootvnode == olddp) {
    673 		vrele(rootvnode);
    674 		vref(newdp);
    675 		rootvnode = newdp;
    676 	}
    677 	vput(newdp);
    678 }
    679 
    680 /*
    681  * Start extended attributes
    682  */
    683 static int
    684 start_extattr(struct mount *mp)
    685 {
    686 	int error;
    687 
    688 	error = VFS_EXTATTRCTL(mp, EXTATTR_CMD_START, NULL, 0, NULL);
    689 	if (error)
    690 		printf("%s: failed to start extattr: error = %d\n",
    691 		       mp->mnt_stat.f_mntonname, error);
    692 
    693 	return error;
    694 }
    695 
    696 int
    697 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
    698     const char *path, int flags, void *data, size_t *data_len)
    699 {
    700 	vnode_t *vp = *vpp;
    701 	struct mount *mp;
    702 	struct pathbuf *pb;
    703 	struct nameidata nd;
    704 	int error;
    705 
    706 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    707 	    KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
    708 	if (error) {
    709 		vfs_delref(vfsops);
    710 		return error;
    711 	}
    712 
    713 	/* Cannot make a non-dir a mount-point (from here anyway). */
    714 	if (vp->v_type != VDIR) {
    715 		vfs_delref(vfsops);
    716 		return ENOTDIR;
    717 	}
    718 
    719 	if (flags & MNT_EXPORTED) {
    720 		vfs_delref(vfsops);
    721 		return EINVAL;
    722 	}
    723 
    724 	if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) {
    725 		vfs_delref(vfsops);
    726 		return ENOMEM;
    727 	}
    728 
    729 	if ((error = fstrans_mount(mp)) != 0) {
    730 		vfs_unbusy(mp, false, NULL);
    731 		vfs_rele(mp);
    732 		return error;
    733 	}
    734 
    735 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
    736 
    737 	/*
    738 	 * The underlying file system may refuse the mount for
    739 	 * various reasons.  Allow the user to force it to happen.
    740 	 *
    741 	 * Set the mount level flags.
    742 	 */
    743 	mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
    744 
    745 	mutex_enter(&mp->mnt_updating);
    746 	error = VFS_MOUNT(mp, path, data, data_len);
    747 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    748 
    749 	if (error != 0)
    750 		goto err_unmounted;
    751 
    752 	/*
    753 	 * Validate and prepare the mount point.
    754 	 */
    755 	error = pathbuf_copyin(path, &pb);
    756 	if (error != 0) {
    757 		goto err_mounted;
    758 	}
    759 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    760 	error = namei(&nd);
    761 	pathbuf_destroy(pb);
    762 	if (error != 0) {
    763 		goto err_mounted;
    764 	}
    765 	if (nd.ni_vp != vp) {
    766 		vput(nd.ni_vp);
    767 		error = EINVAL;
    768 		goto err_mounted;
    769 	}
    770 	if (vp->v_mountedhere != NULL) {
    771 		vput(nd.ni_vp);
    772 		error = EBUSY;
    773 		goto err_mounted;
    774 	}
    775 	error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0);
    776 	if (error != 0) {
    777 		vput(nd.ni_vp);
    778 		goto err_mounted;
    779 	}
    780 
    781 	/*
    782 	 * Put the new filesystem on the mount list after root.
    783 	 */
    784 	cache_purge(vp);
    785 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    786 
    787 	mountlist_append(mp);
    788 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    789 		vfs_syncer_add_to_worklist(mp);
    790 	vp->v_mountedhere = mp;
    791 	vput(nd.ni_vp);
    792 
    793 	mount_checkdirs(vp);
    794 	mutex_exit(&mp->mnt_updating);
    795 
    796 	/* Hold an additional reference to the mount across VFS_START(). */
    797 	vfs_unbusy(mp, true, NULL);
    798 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    799 	error = VFS_START(mp, 0);
    800        if (error) {
    801 		vrele(vp);
    802 	} else if (flags & MNT_EXTATTR) {
    803 		(void)start_extattr(mp);
    804 	}
    805 	/* Drop reference held for VFS_START(). */
    806 	vfs_rele(mp);
    807 	*vpp = NULL;
    808 	return error;
    809 
    810 err_mounted:
    811 	if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
    812 		panic("Unmounting fresh file system failed");
    813 
    814 err_unmounted:
    815 	vp->v_mountedhere = NULL;
    816 	mutex_exit(&mp->mnt_updating);
    817 	fstrans_unmount(mp);
    818 	vfs_unbusy(mp, false, NULL);
    819 	vfs_rele(mp);
    820 
    821 	return error;
    822 }
    823 
    824 /*
    825  * Do the actual file system unmount.  File system is assumed to have
    826  * been locked by the caller.
    827  *
    828  * => Caller hold reference to the mount, explicitly for dounmount().
    829  */
    830 int
    831 dounmount(struct mount *mp, int flags, struct lwp *l)
    832 {
    833 	mount_iterator_t *iter;
    834 	struct mount *cmp;
    835 	vnode_t *coveredvp;
    836 	int error, async, used_syncer, used_extattr;
    837 
    838 #if NVERIEXEC > 0
    839 	error = veriexec_unmountchk(mp);
    840 	if (error)
    841 		return (error);
    842 #endif /* NVERIEXEC > 0 */
    843 
    844 	/*
    845 	 * No unmount below layered mounts.
    846 	 */
    847 	mountlist_iterator_init(&iter);
    848 	while ((cmp = mountlist_iterator_next(iter)) != NULL) {
    849 		if (cmp->mnt_lower == mp) {
    850 			mountlist_iterator_destroy(iter);
    851 			return EBUSY;
    852 		}
    853 	}
    854 	mountlist_iterator_destroy(iter);
    855 
    856 	/*
    857 	 * XXX Freeze syncer.  Must do this before locking the
    858 	 * mount point.  See dounmount() for details.
    859 	 */
    860 	mutex_enter(&syncer_mutex);
    861 
    862 	/*
    863 	 * Abort unmount attempt when the filesystem is in use
    864 	 */
    865 	mutex_enter(&mp->mnt_unmounting);
    866 	if (mp->mnt_busynest != 0) {
    867 		mutex_exit(&mp->mnt_unmounting);
    868 		mutex_exit(&syncer_mutex);
    869 		return EBUSY;
    870 	}
    871 
    872 	/*
    873 	 * Abort unmount attempt when the filesystem is not mounted
    874 	 */
    875 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
    876 		mutex_exit(&mp->mnt_unmounting);
    877 		mutex_exit(&syncer_mutex);
    878 		return ENOENT;
    879 	}
    880 
    881 	used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
    882 	used_extattr = mp->mnt_flag & MNT_EXTATTR;
    883 
    884 	/*
    885 	 * XXX Syncer must be frozen when we get here.  This should really
    886 	 * be done on a per-mountpoint basis, but the syncer doesn't work
    887 	 * like that.
    888 	 *
    889 	 * The caller of dounmount() must acquire syncer_mutex because
    890 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    891 	 * order, and we must preserve that order to avoid deadlock.
    892 	 *
    893 	 * So, if the file system did not use the syncer, now is
    894 	 * the time to release the syncer_mutex.
    895 	 */
    896 	if (used_syncer == 0) {
    897 		mutex_exit(&syncer_mutex);
    898 	}
    899 	mp->mnt_iflag |= IMNT_UNMOUNT;
    900 	mutex_enter(&mp->mnt_updating);
    901 	async = mp->mnt_flag & MNT_ASYNC;
    902 	mp->mnt_flag &= ~MNT_ASYNC;
    903 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    904 	if (used_syncer)
    905 		vfs_syncer_remove_from_worklist(mp);
    906 	error = 0;
    907 	if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
    908 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    909 	}
    910 	if (error == 0 || (flags & MNT_FORCE)) {
    911 		error = VFS_UNMOUNT(mp, flags);
    912 	}
    913 	if (error) {
    914 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    915 		mutex_exit(&mp->mnt_unmounting);
    916 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    917 			vfs_syncer_add_to_worklist(mp);
    918 		mp->mnt_flag |= async;
    919 		mutex_exit(&mp->mnt_updating);
    920 		if (used_syncer)
    921 			mutex_exit(&syncer_mutex);
    922 		if (used_extattr) {
    923 			if (start_extattr(mp) != 0)
    924 				mp->mnt_flag &= ~MNT_EXTATTR;
    925 			else
    926 				mp->mnt_flag |= MNT_EXTATTR;
    927 		}
    928 		return (error);
    929 	}
    930 	mutex_exit(&mp->mnt_updating);
    931 
    932 	/*
    933 	 * release mnt_umounting lock here, because other code calls
    934 	 * vfs_busy() while holding the mountlist_lock.
    935 	 *
    936 	 * mark filesystem as gone to prevent further umounts
    937 	 * after mnt_umounting lock is gone, this also prevents
    938 	 * vfs_busy() from succeeding.
    939 	 */
    940 	mp->mnt_iflag |= IMNT_GONE;
    941 	mutex_exit(&mp->mnt_unmounting);
    942 
    943 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    944 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
    945 		coveredvp->v_mountedhere = NULL;
    946 		VOP_UNLOCK(coveredvp);
    947 	}
    948 	mountlist_remove(mp);
    949 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    950 		panic("unmount: dangling vnode");
    951 	if (used_syncer)
    952 		mutex_exit(&syncer_mutex);
    953 	vfs_hooks_unmount(mp);
    954 
    955 	fstrans_unmount(mp);
    956 	vfs_rele(mp);	/* reference from mount() */
    957 	if (coveredvp != NULLVP) {
    958 		vrele(coveredvp);
    959 	}
    960 	return (0);
    961 }
    962 
    963 /*
    964  * Unmount all file systems.
    965  * We traverse the list in reverse order under the assumption that doing so
    966  * will avoid needing to worry about dependencies.
    967  */
    968 bool
    969 vfs_unmountall(struct lwp *l)
    970 {
    971 
    972 	printf("unmounting file systems...\n");
    973 	return vfs_unmountall1(l, true, true);
    974 }
    975 
    976 static void
    977 vfs_unmount_print(struct mount *mp, const char *pfx)
    978 {
    979 
    980 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
    981 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
    982 	    mp->mnt_stat.f_fstypename);
    983 }
    984 
    985 /*
    986  * Return the mount with the highest generation less than "gen".
    987  */
    988 static struct mount *
    989 vfs_unmount_next(uint64_t gen)
    990 {
    991 	mount_iterator_t *iter;
    992 	struct mount *mp, *nmp;
    993 
    994 	nmp = NULL;
    995 
    996 	mountlist_iterator_init(&iter);
    997 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    998 		if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) &&
    999 		    mp->mnt_gen < gen) {
   1000 			if (nmp != NULL)
   1001 				vfs_rele(nmp);
   1002 			nmp = mp;
   1003 			vfs_ref(nmp);
   1004 		}
   1005 	}
   1006 	mountlist_iterator_destroy(iter);
   1007 
   1008 	return nmp;
   1009 }
   1010 
   1011 bool
   1012 vfs_unmount_forceone(struct lwp *l)
   1013 {
   1014 	struct mount *mp;
   1015 	int error;
   1016 
   1017 	mp = vfs_unmount_next(mountgen);
   1018 	if (mp == NULL) {
   1019 		return false;
   1020 	}
   1021 
   1022 #ifdef DEBUG
   1023 	printf("forcefully unmounting %s (%s)...\n",
   1024 	    mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
   1025 #endif
   1026 	if ((error = dounmount(mp, MNT_FORCE, l)) == 0) {
   1027 		vfs_unmount_print(mp, "forcefully ");
   1028 		return true;
   1029 	} else {
   1030 		vfs_rele(mp);
   1031 	}
   1032 
   1033 #ifdef DEBUG
   1034 	printf("forceful unmount of %s failed with error %d\n",
   1035 	    mp->mnt_stat.f_mntonname, error);
   1036 #endif
   1037 
   1038 	return false;
   1039 }
   1040 
   1041 bool
   1042 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
   1043 {
   1044 	struct mount *mp;
   1045 	bool any_error = false, progress = false;
   1046 	uint64_t gen;
   1047 	int error;
   1048 
   1049 	gen = mountgen;
   1050 	for (;;) {
   1051 		mp = vfs_unmount_next(gen);
   1052 		if (mp == NULL)
   1053 			break;
   1054 		gen = mp->mnt_gen;
   1055 
   1056 #ifdef DEBUG
   1057 		printf("unmounting %p %s (%s)...\n",
   1058 		    (void *)mp, mp->mnt_stat.f_mntonname,
   1059 		    mp->mnt_stat.f_mntfromname);
   1060 #endif
   1061 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
   1062 			vfs_unmount_print(mp, "");
   1063 			progress = true;
   1064 		} else {
   1065 			vfs_rele(mp);
   1066 			if (verbose) {
   1067 				printf("unmount of %s failed with error %d\n",
   1068 				    mp->mnt_stat.f_mntonname, error);
   1069 			}
   1070 			any_error = true;
   1071 		}
   1072 	}
   1073 	if (verbose) {
   1074 		printf("unmounting done\n");
   1075 	}
   1076 	if (any_error && verbose) {
   1077 		printf("WARNING: some file systems would not unmount\n");
   1078 	}
   1079 	return progress;
   1080 }
   1081 
   1082 void
   1083 vfs_sync_all(struct lwp *l)
   1084 {
   1085 	printf("syncing disks... ");
   1086 
   1087 	/* remove user processes from run queue */
   1088 	suspendsched();
   1089 	(void)spl0();
   1090 
   1091 	/* avoid coming back this way again if we panic. */
   1092 	doing_shutdown = 1;
   1093 
   1094 	do_sys_sync(l);
   1095 
   1096 	/* Wait for sync to finish. */
   1097 	if (buf_syncwait() != 0) {
   1098 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
   1099 		Debugger();
   1100 #endif
   1101 		printf("giving up\n");
   1102 		return;
   1103 	} else
   1104 		printf("done\n");
   1105 }
   1106 
   1107 /*
   1108  * Sync and unmount file systems before shutting down.
   1109  */
   1110 void
   1111 vfs_shutdown(void)
   1112 {
   1113 	lwp_t *l = curlwp;
   1114 
   1115 	vfs_sync_all(l);
   1116 
   1117 	/*
   1118 	 * If we have paniced - do not make the situation potentially
   1119 	 * worse by unmounting the file systems.
   1120 	 */
   1121 	if (panicstr != NULL) {
   1122 		return;
   1123 	}
   1124 
   1125 	/* Unmount file systems. */
   1126 	vfs_unmountall(l);
   1127 }
   1128 
   1129 /*
   1130  * Print a list of supported file system types (used by vfs_mountroot)
   1131  */
   1132 static void
   1133 vfs_print_fstypes(void)
   1134 {
   1135 	struct vfsops *v;
   1136 	int cnt = 0;
   1137 
   1138 	mutex_enter(&vfs_list_lock);
   1139 	LIST_FOREACH(v, &vfs_list, vfs_list)
   1140 		++cnt;
   1141 	mutex_exit(&vfs_list_lock);
   1142 
   1143 	if (cnt == 0) {
   1144 		printf("WARNING: No file system modules have been loaded.\n");
   1145 		return;
   1146 	}
   1147 
   1148 	printf("Supported file systems:");
   1149 	mutex_enter(&vfs_list_lock);
   1150 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1151 		printf(" %s", v->vfs_name);
   1152 	}
   1153 	mutex_exit(&vfs_list_lock);
   1154 	printf("\n");
   1155 }
   1156 
   1157 /*
   1158  * Mount the root file system.  If the operator didn't specify a
   1159  * file system to use, try all possible file systems until one
   1160  * succeeds.
   1161  */
   1162 int
   1163 vfs_mountroot(void)
   1164 {
   1165 	struct vfsops *v;
   1166 	int error = ENODEV;
   1167 
   1168 	if (root_device == NULL)
   1169 		panic("vfs_mountroot: root device unknown");
   1170 
   1171 	switch (device_class(root_device)) {
   1172 	case DV_IFNET:
   1173 		if (rootdev != NODEV)
   1174 			panic("vfs_mountroot: rootdev set for DV_IFNET "
   1175 			    "(0x%llx -> %llu,%llu)",
   1176 			    (unsigned long long)rootdev,
   1177 			    (unsigned long long)major(rootdev),
   1178 			    (unsigned long long)minor(rootdev));
   1179 		break;
   1180 
   1181 	case DV_DISK:
   1182 		if (rootdev == NODEV)
   1183 			panic("vfs_mountroot: rootdev not set for DV_DISK");
   1184 	        if (bdevvp(rootdev, &rootvp))
   1185 	                panic("vfs_mountroot: can't get vnode for rootdev");
   1186 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
   1187 		if (error) {
   1188 			printf("vfs_mountroot: can't open root device\n");
   1189 			return (error);
   1190 		}
   1191 		break;
   1192 
   1193 	case DV_VIRTUAL:
   1194 		break;
   1195 
   1196 	default:
   1197 		printf("%s: inappropriate for root file system\n",
   1198 		    device_xname(root_device));
   1199 		return (ENODEV);
   1200 	}
   1201 
   1202 	/*
   1203 	 * If user specified a root fs type, use it.  Make sure the
   1204 	 * specified type exists and has a mount_root()
   1205 	 */
   1206 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
   1207 		v = vfs_getopsbyname(rootfstype);
   1208 		error = EFTYPE;
   1209 		if (v != NULL) {
   1210 			if (v->vfs_mountroot != NULL) {
   1211 				error = (v->vfs_mountroot)();
   1212 			}
   1213 			v->vfs_refcount--;
   1214 		}
   1215 		goto done;
   1216 	}
   1217 
   1218 	/*
   1219 	 * Try each file system currently configured into the kernel.
   1220 	 */
   1221 	mutex_enter(&vfs_list_lock);
   1222 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1223 		if (v->vfs_mountroot == NULL)
   1224 			continue;
   1225 #ifdef DEBUG
   1226 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
   1227 #endif
   1228 		v->vfs_refcount++;
   1229 		mutex_exit(&vfs_list_lock);
   1230 		error = (*v->vfs_mountroot)();
   1231 		mutex_enter(&vfs_list_lock);
   1232 		v->vfs_refcount--;
   1233 		if (!error) {
   1234 			aprint_normal("root file system type: %s\n",
   1235 			    v->vfs_name);
   1236 			break;
   1237 		}
   1238 	}
   1239 	mutex_exit(&vfs_list_lock);
   1240 
   1241 	if (v == NULL) {
   1242 		vfs_print_fstypes();
   1243 		printf("no file system for %s", device_xname(root_device));
   1244 		if (device_class(root_device) == DV_DISK)
   1245 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
   1246 		printf("\n");
   1247 		error = EFTYPE;
   1248 	}
   1249 
   1250 done:
   1251 	if (error && device_class(root_device) == DV_DISK) {
   1252 		VOP_CLOSE(rootvp, FREAD, FSCRED);
   1253 		vrele(rootvp);
   1254 	}
   1255 	if (error == 0) {
   1256 		mount_iterator_t *iter;
   1257 		struct mount *mp;
   1258 		extern struct cwdinfo cwdi0;
   1259 
   1260 		mountlist_iterator_init(&iter);
   1261 		mp = mountlist_iterator_next(iter);
   1262 		KASSERT(mp != NULL);
   1263 		mountlist_iterator_destroy(iter);
   1264 
   1265 		mp->mnt_flag |= MNT_ROOTFS;
   1266 		mp->mnt_op->vfs_refcount++;
   1267 		error = fstrans_mount(mp);
   1268 		KASSERT(error == 0);
   1269 
   1270 		/*
   1271 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
   1272 		 * reference it.
   1273 		 */
   1274 		error = VFS_ROOT(mp, &rootvnode);
   1275 		if (error)
   1276 			panic("cannot find root vnode, error=%d", error);
   1277 		cwdi0.cwdi_cdir = rootvnode;
   1278 		vref(cwdi0.cwdi_cdir);
   1279 		VOP_UNLOCK(rootvnode);
   1280 		cwdi0.cwdi_rdir = NULL;
   1281 
   1282 		/*
   1283 		 * Now that root is mounted, we can fixup initproc's CWD
   1284 		 * info.  All other processes are kthreads, which merely
   1285 		 * share proc0's CWD info.
   1286 		 */
   1287 		initproc->p_cwdi->cwdi_cdir = rootvnode;
   1288 		vref(initproc->p_cwdi->cwdi_cdir);
   1289 		initproc->p_cwdi->cwdi_rdir = NULL;
   1290 		/*
   1291 		 * Enable loading of modules from the filesystem
   1292 		 */
   1293 		module_load_vfs_init();
   1294 
   1295 	}
   1296 	return (error);
   1297 }
   1298 
   1299 /*
   1300  * mount_specific_key_create --
   1301  *	Create a key for subsystem mount-specific data.
   1302  */
   1303 int
   1304 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1305 {
   1306 
   1307 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
   1308 }
   1309 
   1310 /*
   1311  * mount_specific_key_delete --
   1312  *	Delete a key for subsystem mount-specific data.
   1313  */
   1314 void
   1315 mount_specific_key_delete(specificdata_key_t key)
   1316 {
   1317 
   1318 	specificdata_key_delete(mount_specificdata_domain, key);
   1319 }
   1320 
   1321 /*
   1322  * mount_initspecific --
   1323  *	Initialize a mount's specificdata container.
   1324  */
   1325 void
   1326 mount_initspecific(struct mount *mp)
   1327 {
   1328 	int error __diagused;
   1329 
   1330 	error = specificdata_init(mount_specificdata_domain,
   1331 				  &mp->mnt_specdataref);
   1332 	KASSERT(error == 0);
   1333 }
   1334 
   1335 /*
   1336  * mount_finispecific --
   1337  *	Finalize a mount's specificdata container.
   1338  */
   1339 void
   1340 mount_finispecific(struct mount *mp)
   1341 {
   1342 
   1343 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
   1344 }
   1345 
   1346 /*
   1347  * mount_getspecific --
   1348  *	Return mount-specific data corresponding to the specified key.
   1349  */
   1350 void *
   1351 mount_getspecific(struct mount *mp, specificdata_key_t key)
   1352 {
   1353 
   1354 	return specificdata_getspecific(mount_specificdata_domain,
   1355 					 &mp->mnt_specdataref, key);
   1356 }
   1357 
   1358 /*
   1359  * mount_setspecific --
   1360  *	Set mount-specific data corresponding to the specified key.
   1361  */
   1362 void
   1363 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
   1364 {
   1365 
   1366 	specificdata_setspecific(mount_specificdata_domain,
   1367 				 &mp->mnt_specdataref, key, data);
   1368 }
   1369 
   1370 /*
   1371  * Check to see if a filesystem is mounted on a block device.
   1372  */
   1373 int
   1374 vfs_mountedon(vnode_t *vp)
   1375 {
   1376 	vnode_t *vq;
   1377 	int error = 0;
   1378 
   1379 	if (vp->v_type != VBLK)
   1380 		return ENOTBLK;
   1381 	if (spec_node_getmountedfs(vp) != NULL)
   1382 		return EBUSY;
   1383 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
   1384 		if (spec_node_getmountedfs(vq) != NULL)
   1385 			error = EBUSY;
   1386 		vrele(vq);
   1387 	}
   1388 
   1389 	return error;
   1390 }
   1391 
   1392 /*
   1393  * Check if a device pointed to by vp is mounted.
   1394  *
   1395  * Returns:
   1396  *   EINVAL	if it's not a disk
   1397  *   EBUSY	if it's a disk and mounted
   1398  *   0		if it's a disk and not mounted
   1399  */
   1400 int
   1401 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
   1402 {
   1403 	vnode_t *bvp;
   1404 	dev_t dev;
   1405 	int d_type;
   1406 
   1407 	bvp = NULL;
   1408 	d_type = D_OTHER;
   1409 
   1410 	if (iskmemvp(vp))
   1411 		return EINVAL;
   1412 
   1413 	switch (vp->v_type) {
   1414 	case VCHR: {
   1415 		const struct cdevsw *cdev;
   1416 
   1417 		dev = vp->v_rdev;
   1418 		cdev = cdevsw_lookup(dev);
   1419 		if (cdev != NULL) {
   1420 			dev_t blkdev;
   1421 
   1422 			blkdev = devsw_chr2blk(dev);
   1423 			if (blkdev != NODEV) {
   1424 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
   1425 					d_type = (cdev->d_flag & D_TYPEMASK);
   1426 					/* XXX: what if bvp disappears? */
   1427 					vrele(bvp);
   1428 				}
   1429 			}
   1430 		}
   1431 
   1432 		break;
   1433 		}
   1434 
   1435 	case VBLK: {
   1436 		const struct bdevsw *bdev;
   1437 
   1438 		dev = vp->v_rdev;
   1439 		bdev = bdevsw_lookup(dev);
   1440 		if (bdev != NULL)
   1441 			d_type = (bdev->d_flag & D_TYPEMASK);
   1442 
   1443 		bvp = vp;
   1444 
   1445 		break;
   1446 		}
   1447 
   1448 	default:
   1449 		break;
   1450 	}
   1451 
   1452 	if (d_type != D_DISK)
   1453 		return EINVAL;
   1454 
   1455 	if (bvpp != NULL)
   1456 		*bvpp = bvp;
   1457 
   1458 	/*
   1459 	 * XXX: This is bogus. We should be failing the request
   1460 	 * XXX: not only if this specific slice is mounted, but
   1461 	 * XXX: if it's on a disk with any other mounted slice.
   1462 	 */
   1463 	if (vfs_mountedon(bvp))
   1464 		return EBUSY;
   1465 
   1466 	return 0;
   1467 }
   1468 
   1469 /*
   1470  * Make a 'unique' number from a mount type name.
   1471  */
   1472 long
   1473 makefstype(const char *type)
   1474 {
   1475 	long rv;
   1476 
   1477 	for (rv = 0; *type; type++) {
   1478 		rv <<= 2;
   1479 		rv ^= *type;
   1480 	}
   1481 	return rv;
   1482 }
   1483 
   1484 static struct mountlist_entry *
   1485 mountlist_alloc(enum mountlist_type type, struct mount *mp)
   1486 {
   1487 	struct mountlist_entry *me;
   1488 
   1489 	me = kmem_zalloc(sizeof(*me), KM_SLEEP);
   1490 	me->me_mount = mp;
   1491 	me->me_type = type;
   1492 
   1493 	return me;
   1494 }
   1495 
   1496 static void
   1497 mountlist_free(struct mountlist_entry *me)
   1498 {
   1499 
   1500 	kmem_free(me, sizeof(*me));
   1501 }
   1502 
   1503 void
   1504 mountlist_iterator_init(mount_iterator_t **mip)
   1505 {
   1506 	struct mountlist_entry *me;
   1507 
   1508 	me = mountlist_alloc(ME_MARKER, NULL);
   1509 	mutex_enter(&mountlist_lock);
   1510 	TAILQ_INSERT_HEAD(&mountlist, me, me_list);
   1511 	mutex_exit(&mountlist_lock);
   1512 	*mip = (mount_iterator_t *)me;
   1513 }
   1514 
   1515 void
   1516 mountlist_iterator_destroy(mount_iterator_t *mi)
   1517 {
   1518 	struct mountlist_entry *marker = &mi->mi_entry;
   1519 
   1520 	if (marker->me_mount != NULL)
   1521 		vfs_unbusy(marker->me_mount, false, NULL);
   1522 
   1523 	mutex_enter(&mountlist_lock);
   1524 	TAILQ_REMOVE(&mountlist, marker, me_list);
   1525 	mutex_exit(&mountlist_lock);
   1526 
   1527 	mountlist_free(marker);
   1528 
   1529 }
   1530 
   1531 /*
   1532  * Return the next mount or NULL for this iterator.
   1533  * Mark it busy on success.
   1534  */
   1535 struct mount *
   1536 mountlist_iterator_next(mount_iterator_t *mi)
   1537 {
   1538 	struct mountlist_entry *me, *marker = &mi->mi_entry;
   1539 	struct mount *mp;
   1540 
   1541 	if (marker->me_mount != NULL) {
   1542 		vfs_unbusy(marker->me_mount, false, NULL);
   1543 		marker->me_mount = NULL;
   1544 	}
   1545 
   1546 	mutex_enter(&mountlist_lock);
   1547 	for (;;) {
   1548 		KASSERT(marker->me_type == ME_MARKER);
   1549 
   1550 		me = TAILQ_NEXT(marker, me_list);
   1551 		if (me == NULL) {
   1552 			/* End of list: keep marker and return. */
   1553 			mutex_exit(&mountlist_lock);
   1554 			return NULL;
   1555 		}
   1556 		TAILQ_REMOVE(&mountlist, marker, me_list);
   1557 		TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list);
   1558 
   1559 		/* Skip other markers. */
   1560 		if (me->me_type != ME_MOUNT)
   1561 			continue;
   1562 
   1563 		/* Take an initial reference for vfs_busy() below. */
   1564 		mp = me->me_mount;
   1565 		KASSERT(mp != NULL);
   1566 		vfs_ref(mp);
   1567 		mutex_exit(&mountlist_lock);
   1568 
   1569 		/* Try to mark this mount busy and return on success. */
   1570 		if (vfs_busy(mp, NULL) == 0) {
   1571 			vfs_rele(mp);
   1572 			marker->me_mount = mp;
   1573 			return mp;
   1574 		}
   1575 		vfs_rele(mp);
   1576 		mutex_enter(&mountlist_lock);
   1577 	}
   1578 }
   1579 
   1580 /*
   1581  * Attach new mount to the end of the mount list.
   1582  */
   1583 void
   1584 mountlist_append(struct mount *mp)
   1585 {
   1586 	struct mountlist_entry *me;
   1587 
   1588 	me = mountlist_alloc(ME_MOUNT, mp);
   1589 	mutex_enter(&mountlist_lock);
   1590 	TAILQ_INSERT_TAIL(&mountlist, me, me_list);
   1591 	mutex_exit(&mountlist_lock);
   1592 }
   1593 
   1594 /*
   1595  * Remove mount from mount list.
   1596  */void
   1597 mountlist_remove(struct mount *mp)
   1598 {
   1599 	struct mountlist_entry *me;
   1600 
   1601 	mutex_enter(&mountlist_lock);
   1602 	TAILQ_FOREACH(me, &mountlist, me_list)
   1603 		if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1604 			break;
   1605 	KASSERT(me != NULL);
   1606 	TAILQ_REMOVE(&mountlist, me, me_list);
   1607 	mutex_exit(&mountlist_lock);
   1608 	mountlist_free(me);
   1609 }
   1610 
   1611 /*
   1612  * Unlocked variant to traverse the mountlist.
   1613  * To be used from DDB only.
   1614  */
   1615 struct mount *
   1616 _mountlist_next(struct mount *mp)
   1617 {
   1618 	struct mountlist_entry *me;
   1619 
   1620 	if (mp == NULL) {
   1621 		me = TAILQ_FIRST(&mountlist);
   1622 	} else {
   1623 		TAILQ_FOREACH(me, &mountlist, me_list)
   1624 			if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1625 				break;
   1626 		if (me != NULL)
   1627 			me = TAILQ_NEXT(me, me_list);
   1628 	}
   1629 
   1630 	while (me != NULL && me->me_type != ME_MOUNT)
   1631 		me = TAILQ_NEXT(me, me_list);
   1632 
   1633 	return (me ? me->me_mount : NULL);
   1634 }
   1635