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