Home | History | Annotate | Line # | Download | only in kern
vfs_mount.c revision 1.58.2.3
      1 /*	$NetBSD: vfs_mount.c,v 1.58.2.3 2017/05/19 00:22:57 pgoyette 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.58.2.3 2017/05/19 00:22:57 pgoyette 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 		vrele_flush(mp);
    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 	error = vfs_suspend(mp, 0);
    879 	if (error) {
    880 		mutex_exit(&syncer_mutex);
    881 		return error;
    882 	}
    883 
    884 	/*
    885 	 * Abort unmount attempt when the filesystem is in use
    886 	 */
    887 	mutex_enter(&mp->mnt_unmounting);
    888 	if (mp->mnt_busynest != 0) {
    889 		mutex_exit(&mp->mnt_unmounting);
    890 		mutex_exit(&syncer_mutex);
    891 		vfs_resume(mp);
    892 		return EBUSY;
    893 	}
    894 
    895 	/*
    896 	 * Abort unmount attempt when the filesystem is not mounted
    897 	 */
    898 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
    899 		mutex_exit(&mp->mnt_unmounting);
    900 		mutex_exit(&syncer_mutex);
    901 		return ENOENT;
    902 	}
    903 
    904 	used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
    905 	used_extattr = mp->mnt_flag & MNT_EXTATTR;
    906 
    907 	/*
    908 	 * XXX Syncer must be frozen when we get here.  This should really
    909 	 * be done on a per-mountpoint basis, but the syncer doesn't work
    910 	 * like that.
    911 	 *
    912 	 * The caller of dounmount() must acquire syncer_mutex because
    913 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    914 	 * order, and we must preserve that order to avoid deadlock.
    915 	 *
    916 	 * So, if the file system did not use the syncer, now is
    917 	 * the time to release the syncer_mutex.
    918 	 */
    919 	if (used_syncer == 0) {
    920 		mutex_exit(&syncer_mutex);
    921 	}
    922 	mp->mnt_iflag |= IMNT_UNMOUNT;
    923 	mutex_enter(&mp->mnt_updating);
    924 	async = mp->mnt_flag & MNT_ASYNC;
    925 	mp->mnt_flag &= ~MNT_ASYNC;
    926 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    927 	if (used_syncer)
    928 		vfs_syncer_remove_from_worklist(mp);
    929 	error = 0;
    930 	if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
    931 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    932 	}
    933 	if (error == 0 || (flags & MNT_FORCE)) {
    934 		error = VFS_UNMOUNT(mp, flags);
    935 	}
    936 	if (error) {
    937 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    938 		mutex_exit(&mp->mnt_unmounting);
    939 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    940 			vfs_syncer_add_to_worklist(mp);
    941 		mp->mnt_flag |= async;
    942 		mutex_exit(&mp->mnt_updating);
    943 		vfs_resume(mp);
    944 		if (used_syncer)
    945 			mutex_exit(&syncer_mutex);
    946 		if (used_extattr) {
    947 			if (start_extattr(mp) != 0)
    948 				mp->mnt_flag &= ~MNT_EXTATTR;
    949 			else
    950 				mp->mnt_flag |= MNT_EXTATTR;
    951 		}
    952 		return (error);
    953 	}
    954 	mutex_exit(&mp->mnt_updating);
    955 
    956 	/*
    957 	 * release mnt_umounting lock here, because other code calls
    958 	 * vfs_busy() while holding the mountlist_lock.
    959 	 *
    960 	 * mark filesystem as gone to prevent further umounts
    961 	 * after mnt_umounting lock is gone, this also prevents
    962 	 * vfs_busy() from succeeding.
    963 	 */
    964 	mp->mnt_iflag |= IMNT_GONE;
    965 	mutex_exit(&mp->mnt_unmounting);
    966 	vfs_resume(mp);
    967 
    968 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    969 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
    970 		coveredvp->v_mountedhere = NULL;
    971 		VOP_UNLOCK(coveredvp);
    972 	}
    973 	mountlist_remove(mp);
    974 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    975 		panic("unmount: dangling vnode");
    976 	if (used_syncer)
    977 		mutex_exit(&syncer_mutex);
    978 	vfs_hooks_unmount(mp);
    979 
    980 	fstrans_unmount(mp);
    981 	vfs_rele(mp);	/* reference from mount() */
    982 	if (coveredvp != NULLVP) {
    983 		vrele(coveredvp);
    984 	}
    985 	return (0);
    986 }
    987 
    988 /*
    989  * Unmount all file systems.
    990  * We traverse the list in reverse order under the assumption that doing so
    991  * will avoid needing to worry about dependencies.
    992  */
    993 bool
    994 vfs_unmountall(struct lwp *l)
    995 {
    996 
    997 	printf("unmounting file systems...\n");
    998 	return vfs_unmountall1(l, true, true);
    999 }
   1000 
   1001 static void
   1002 vfs_unmount_print(struct mount *mp, const char *pfx)
   1003 {
   1004 
   1005 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
   1006 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
   1007 	    mp->mnt_stat.f_fstypename);
   1008 }
   1009 
   1010 /*
   1011  * Return the mount with the highest generation less than "gen".
   1012  */
   1013 static struct mount *
   1014 vfs_unmount_next(uint64_t gen)
   1015 {
   1016 	mount_iterator_t *iter;
   1017 	struct mount *mp, *nmp;
   1018 
   1019 	nmp = NULL;
   1020 
   1021 	mountlist_iterator_init(&iter);
   1022 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
   1023 		if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) &&
   1024 		    mp->mnt_gen < gen) {
   1025 			if (nmp != NULL)
   1026 				vfs_rele(nmp);
   1027 			nmp = mp;
   1028 			vfs_ref(nmp);
   1029 		}
   1030 	}
   1031 	mountlist_iterator_destroy(iter);
   1032 
   1033 	return nmp;
   1034 }
   1035 
   1036 bool
   1037 vfs_unmount_forceone(struct lwp *l)
   1038 {
   1039 	struct mount *mp;
   1040 	int error;
   1041 
   1042 	mp = vfs_unmount_next(mountgen);
   1043 	if (mp == NULL) {
   1044 		return false;
   1045 	}
   1046 
   1047 #ifdef DEBUG
   1048 	printf("forcefully unmounting %s (%s)...\n",
   1049 	    mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
   1050 #endif
   1051 	if ((error = dounmount(mp, MNT_FORCE, l)) == 0) {
   1052 		vfs_unmount_print(mp, "forcefully ");
   1053 		return true;
   1054 	} else {
   1055 		vfs_rele(mp);
   1056 	}
   1057 
   1058 #ifdef DEBUG
   1059 	printf("forceful unmount of %s failed with error %d\n",
   1060 	    mp->mnt_stat.f_mntonname, error);
   1061 #endif
   1062 
   1063 	return false;
   1064 }
   1065 
   1066 bool
   1067 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
   1068 {
   1069 	struct mount *mp;
   1070 	bool any_error = false, progress = false;
   1071 	uint64_t gen;
   1072 	int error;
   1073 
   1074 	gen = mountgen;
   1075 	for (;;) {
   1076 		mp = vfs_unmount_next(gen);
   1077 		if (mp == NULL)
   1078 			break;
   1079 		gen = mp->mnt_gen;
   1080 
   1081 #ifdef DEBUG
   1082 		printf("unmounting %p %s (%s)...\n",
   1083 		    (void *)mp, mp->mnt_stat.f_mntonname,
   1084 		    mp->mnt_stat.f_mntfromname);
   1085 #endif
   1086 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
   1087 			vfs_unmount_print(mp, "");
   1088 			progress = true;
   1089 		} else {
   1090 			vfs_rele(mp);
   1091 			if (verbose) {
   1092 				printf("unmount of %s failed with error %d\n",
   1093 				    mp->mnt_stat.f_mntonname, error);
   1094 			}
   1095 			any_error = true;
   1096 		}
   1097 	}
   1098 	if (verbose) {
   1099 		printf("unmounting done\n");
   1100 	}
   1101 	if (any_error && verbose) {
   1102 		printf("WARNING: some file systems would not unmount\n");
   1103 	}
   1104 	return progress;
   1105 }
   1106 
   1107 void
   1108 vfs_sync_all(struct lwp *l)
   1109 {
   1110 	printf("syncing disks... ");
   1111 
   1112 	/* remove user processes from run queue */
   1113 	suspendsched();
   1114 	(void)spl0();
   1115 
   1116 	/* avoid coming back this way again if we panic. */
   1117 	doing_shutdown = 1;
   1118 
   1119 	do_sys_sync(l);
   1120 
   1121 	/* Wait for sync to finish. */
   1122 	if (buf_syncwait() != 0) {
   1123 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
   1124 		Debugger();
   1125 #endif
   1126 		printf("giving up\n");
   1127 		return;
   1128 	} else
   1129 		printf("done\n");
   1130 }
   1131 
   1132 /*
   1133  * Sync and unmount file systems before shutting down.
   1134  */
   1135 void
   1136 vfs_shutdown(void)
   1137 {
   1138 	lwp_t *l = curlwp;
   1139 
   1140 	vfs_sync_all(l);
   1141 
   1142 	/*
   1143 	 * If we have paniced - do not make the situation potentially
   1144 	 * worse by unmounting the file systems.
   1145 	 */
   1146 	if (panicstr != NULL) {
   1147 		return;
   1148 	}
   1149 
   1150 	/* Unmount file systems. */
   1151 	vfs_unmountall(l);
   1152 }
   1153 
   1154 /*
   1155  * Print a list of supported file system types (used by vfs_mountroot)
   1156  */
   1157 static void
   1158 vfs_print_fstypes(void)
   1159 {
   1160 	struct vfsops *v;
   1161 	int cnt = 0;
   1162 
   1163 	mutex_enter(&vfs_list_lock);
   1164 	LIST_FOREACH(v, &vfs_list, vfs_list)
   1165 		++cnt;
   1166 	mutex_exit(&vfs_list_lock);
   1167 
   1168 	if (cnt == 0) {
   1169 		printf("WARNING: No file system modules have been loaded.\n");
   1170 		return;
   1171 	}
   1172 
   1173 	printf("Supported file systems:");
   1174 	mutex_enter(&vfs_list_lock);
   1175 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1176 		printf(" %s", v->vfs_name);
   1177 	}
   1178 	mutex_exit(&vfs_list_lock);
   1179 	printf("\n");
   1180 }
   1181 
   1182 /*
   1183  * Mount the root file system.  If the operator didn't specify a
   1184  * file system to use, try all possible file systems until one
   1185  * succeeds.
   1186  */
   1187 int
   1188 vfs_mountroot(void)
   1189 {
   1190 	struct vfsops *v;
   1191 	int error = ENODEV;
   1192 
   1193 	if (root_device == NULL)
   1194 		panic("vfs_mountroot: root device unknown");
   1195 
   1196 	switch (device_class(root_device)) {
   1197 	case DV_IFNET:
   1198 		if (rootdev != NODEV)
   1199 			panic("vfs_mountroot: rootdev set for DV_IFNET "
   1200 			    "(0x%llx -> %llu,%llu)",
   1201 			    (unsigned long long)rootdev,
   1202 			    (unsigned long long)major(rootdev),
   1203 			    (unsigned long long)minor(rootdev));
   1204 		break;
   1205 
   1206 	case DV_DISK:
   1207 		if (rootdev == NODEV)
   1208 			panic("vfs_mountroot: rootdev not set for DV_DISK");
   1209 	        if (bdevvp(rootdev, &rootvp))
   1210 	                panic("vfs_mountroot: can't get vnode for rootdev");
   1211 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
   1212 		if (error) {
   1213 			printf("vfs_mountroot: can't open root device\n");
   1214 			return (error);
   1215 		}
   1216 		break;
   1217 
   1218 	case DV_VIRTUAL:
   1219 		break;
   1220 
   1221 	default:
   1222 		printf("%s: inappropriate for root file system\n",
   1223 		    device_xname(root_device));
   1224 		return (ENODEV);
   1225 	}
   1226 
   1227 	/*
   1228 	 * If user specified a root fs type, use it.  Make sure the
   1229 	 * specified type exists and has a mount_root()
   1230 	 */
   1231 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
   1232 		v = vfs_getopsbyname(rootfstype);
   1233 		error = EFTYPE;
   1234 		if (v != NULL) {
   1235 			if (v->vfs_mountroot != NULL) {
   1236 				error = (v->vfs_mountroot)();
   1237 			}
   1238 			v->vfs_refcount--;
   1239 		}
   1240 		goto done;
   1241 	}
   1242 
   1243 	/*
   1244 	 * Try each file system currently configured into the kernel.
   1245 	 */
   1246 	mutex_enter(&vfs_list_lock);
   1247 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1248 		if (v->vfs_mountroot == NULL)
   1249 			continue;
   1250 #ifdef DEBUG
   1251 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
   1252 #endif
   1253 		v->vfs_refcount++;
   1254 		mutex_exit(&vfs_list_lock);
   1255 		error = (*v->vfs_mountroot)();
   1256 		mutex_enter(&vfs_list_lock);
   1257 		v->vfs_refcount--;
   1258 		if (!error) {
   1259 			aprint_normal("root file system type: %s\n",
   1260 			    v->vfs_name);
   1261 			break;
   1262 		}
   1263 	}
   1264 	mutex_exit(&vfs_list_lock);
   1265 
   1266 	if (v == NULL) {
   1267 		vfs_print_fstypes();
   1268 		printf("no file system for %s", device_xname(root_device));
   1269 		if (device_class(root_device) == DV_DISK)
   1270 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
   1271 		printf("\n");
   1272 		error = EFTYPE;
   1273 	}
   1274 
   1275 done:
   1276 	if (error && device_class(root_device) == DV_DISK) {
   1277 		VOP_CLOSE(rootvp, FREAD, FSCRED);
   1278 		vrele(rootvp);
   1279 	}
   1280 	if (error == 0) {
   1281 		mount_iterator_t *iter;
   1282 		struct mount *mp;
   1283 		extern struct cwdinfo cwdi0;
   1284 
   1285 		mountlist_iterator_init(&iter);
   1286 		mp = mountlist_iterator_next(iter);
   1287 		KASSERT(mp != NULL);
   1288 		mountlist_iterator_destroy(iter);
   1289 
   1290 		mp->mnt_flag |= MNT_ROOTFS;
   1291 		mp->mnt_op->vfs_refcount++;
   1292 
   1293 		/*
   1294 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
   1295 		 * reference it.
   1296 		 */
   1297 		error = VFS_ROOT(mp, &rootvnode);
   1298 		if (error)
   1299 			panic("cannot find root vnode, error=%d", error);
   1300 		cwdi0.cwdi_cdir = rootvnode;
   1301 		vref(cwdi0.cwdi_cdir);
   1302 		VOP_UNLOCK(rootvnode);
   1303 		cwdi0.cwdi_rdir = NULL;
   1304 
   1305 		/*
   1306 		 * Now that root is mounted, we can fixup initproc's CWD
   1307 		 * info.  All other processes are kthreads, which merely
   1308 		 * share proc0's CWD info.
   1309 		 */
   1310 		initproc->p_cwdi->cwdi_cdir = rootvnode;
   1311 		vref(initproc->p_cwdi->cwdi_cdir);
   1312 		initproc->p_cwdi->cwdi_rdir = NULL;
   1313 		/*
   1314 		 * Enable loading of modules from the filesystem
   1315 		 */
   1316 		module_load_vfs_init();
   1317 
   1318 	}
   1319 	return (error);
   1320 }
   1321 
   1322 /*
   1323  * mount_specific_key_create --
   1324  *	Create a key for subsystem mount-specific data.
   1325  */
   1326 int
   1327 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1328 {
   1329 
   1330 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
   1331 }
   1332 
   1333 /*
   1334  * mount_specific_key_delete --
   1335  *	Delete a key for subsystem mount-specific data.
   1336  */
   1337 void
   1338 mount_specific_key_delete(specificdata_key_t key)
   1339 {
   1340 
   1341 	specificdata_key_delete(mount_specificdata_domain, key);
   1342 }
   1343 
   1344 /*
   1345  * mount_initspecific --
   1346  *	Initialize a mount's specificdata container.
   1347  */
   1348 void
   1349 mount_initspecific(struct mount *mp)
   1350 {
   1351 	int error __diagused;
   1352 
   1353 	error = specificdata_init(mount_specificdata_domain,
   1354 				  &mp->mnt_specdataref);
   1355 	KASSERT(error == 0);
   1356 }
   1357 
   1358 /*
   1359  * mount_finispecific --
   1360  *	Finalize a mount's specificdata container.
   1361  */
   1362 void
   1363 mount_finispecific(struct mount *mp)
   1364 {
   1365 
   1366 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
   1367 }
   1368 
   1369 /*
   1370  * mount_getspecific --
   1371  *	Return mount-specific data corresponding to the specified key.
   1372  */
   1373 void *
   1374 mount_getspecific(struct mount *mp, specificdata_key_t key)
   1375 {
   1376 
   1377 	return specificdata_getspecific(mount_specificdata_domain,
   1378 					 &mp->mnt_specdataref, key);
   1379 }
   1380 
   1381 /*
   1382  * mount_setspecific --
   1383  *	Set mount-specific data corresponding to the specified key.
   1384  */
   1385 void
   1386 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
   1387 {
   1388 
   1389 	specificdata_setspecific(mount_specificdata_domain,
   1390 				 &mp->mnt_specdataref, key, data);
   1391 }
   1392 
   1393 /*
   1394  * Check to see if a filesystem is mounted on a block device.
   1395  */
   1396 int
   1397 vfs_mountedon(vnode_t *vp)
   1398 {
   1399 	vnode_t *vq;
   1400 	int error = 0;
   1401 
   1402 	if (vp->v_type != VBLK)
   1403 		return ENOTBLK;
   1404 	if (spec_node_getmountedfs(vp) != NULL)
   1405 		return EBUSY;
   1406 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
   1407 		if (spec_node_getmountedfs(vq) != NULL)
   1408 			error = EBUSY;
   1409 		vrele(vq);
   1410 	}
   1411 
   1412 	return error;
   1413 }
   1414 
   1415 /*
   1416  * Check if a device pointed to by vp is mounted.
   1417  *
   1418  * Returns:
   1419  *   EINVAL	if it's not a disk
   1420  *   EBUSY	if it's a disk and mounted
   1421  *   0		if it's a disk and not mounted
   1422  */
   1423 int
   1424 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
   1425 {
   1426 	vnode_t *bvp;
   1427 	dev_t dev;
   1428 	int d_type, ret;
   1429 	const struct cdevsw *cdev = NULL;
   1430 	const struct bdevsw *bdev = NULL;
   1431 
   1432 	bvp = NULL;
   1433 	d_type = D_OTHER;
   1434 
   1435 	if (iskmemvp(vp))
   1436 		return EINVAL;
   1437 
   1438 	switch (vp->v_type) {
   1439 	case VCHR: {
   1440 		dev = vp->v_rdev;
   1441 		cdev = cdevsw_lookup_acquire(dev);
   1442 		if (cdev != NULL) {
   1443 			dev_t blkdev;
   1444 
   1445 			blkdev = devsw_chr2blk(dev);
   1446 			if (blkdev != NODEV) {
   1447 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
   1448 					d_type = (cdev->d_flag & D_TYPEMASK);
   1449 					/* XXX: what if bvp disappears? */
   1450 					vrele(bvp);
   1451 				}
   1452 			}
   1453 		}
   1454 		break;
   1455 		}
   1456 
   1457 	case VBLK: {
   1458 		dev = vp->v_rdev;
   1459 		bdev = bdevsw_lookup_acquire(dev);
   1460 		if (bdev != NULL)
   1461 			d_type = (bdev->d_flag & D_TYPEMASK);
   1462 		bvp = vp;
   1463 
   1464 		break;
   1465 		}
   1466 
   1467 	default:
   1468 		break;
   1469 	}
   1470 
   1471 	if (d_type != D_DISK) {
   1472 		ret = EINVAL;
   1473 		goto out;
   1474 	}
   1475 
   1476 	if (bvpp != NULL)
   1477 		*bvpp = bvp;
   1478 
   1479 	/*
   1480 	 * XXX: This is bogus. We should be failing the request
   1481 	 * XXX: not only if this specific slice is mounted, but
   1482 	 * XXX: if it's on a disk with any other mounted slice.
   1483 	 */
   1484 	if (vfs_mountedon(bvp) != 0)
   1485 		ret = EBUSY;
   1486 	else
   1487 		ret = 0;
   1488 
   1489  out:
   1490 	if (bdev != NULL)
   1491 		bdevsw_release(bdev);
   1492 	if (cdev != NULL)
   1493 		cdevsw_release(cdev);
   1494 
   1495 	return ret;
   1496 }
   1497 
   1498 /*
   1499  * Make a 'unique' number from a mount type name.
   1500  */
   1501 long
   1502 makefstype(const char *type)
   1503 {
   1504 	long rv;
   1505 
   1506 	for (rv = 0; *type; type++) {
   1507 		rv <<= 2;
   1508 		rv ^= *type;
   1509 	}
   1510 	return rv;
   1511 }
   1512 
   1513 static struct mountlist_entry *
   1514 mountlist_alloc(enum mountlist_type type, struct mount *mp)
   1515 {
   1516 	struct mountlist_entry *me;
   1517 
   1518 	me = kmem_zalloc(sizeof(*me), KM_SLEEP);
   1519 	me->me_mount = mp;
   1520 	me->me_type = type;
   1521 
   1522 	return me;
   1523 }
   1524 
   1525 static void
   1526 mountlist_free(struct mountlist_entry *me)
   1527 {
   1528 
   1529 	kmem_free(me, sizeof(*me));
   1530 }
   1531 
   1532 void
   1533 mountlist_iterator_init(mount_iterator_t **mip)
   1534 {
   1535 	struct mountlist_entry *me;
   1536 
   1537 	me = mountlist_alloc(ME_MARKER, NULL);
   1538 	mutex_enter(&mountlist_lock);
   1539 	TAILQ_INSERT_HEAD(&mountlist, me, me_list);
   1540 	mutex_exit(&mountlist_lock);
   1541 	*mip = (mount_iterator_t *)me;
   1542 }
   1543 
   1544 void
   1545 mountlist_iterator_destroy(mount_iterator_t *mi)
   1546 {
   1547 	struct mountlist_entry *marker = &mi->mi_entry;
   1548 
   1549 	if (marker->me_mount != NULL)
   1550 		vfs_unbusy(marker->me_mount);
   1551 
   1552 	mutex_enter(&mountlist_lock);
   1553 	TAILQ_REMOVE(&mountlist, marker, me_list);
   1554 	mutex_exit(&mountlist_lock);
   1555 
   1556 	mountlist_free(marker);
   1557 
   1558 }
   1559 
   1560 /*
   1561  * Return the next mount or NULL for this iterator.
   1562  * Mark it busy on success.
   1563  */
   1564 static inline struct mount *
   1565 _mountlist_iterator_next(mount_iterator_t *mi, bool wait)
   1566 {
   1567 	struct mountlist_entry *me, *marker = &mi->mi_entry;
   1568 	struct mount *mp;
   1569 	int error;
   1570 
   1571 	if (marker->me_mount != NULL) {
   1572 		vfs_unbusy(marker->me_mount);
   1573 		marker->me_mount = NULL;
   1574 	}
   1575 
   1576 	mutex_enter(&mountlist_lock);
   1577 	for (;;) {
   1578 		KASSERT(marker->me_type == ME_MARKER);
   1579 
   1580 		me = TAILQ_NEXT(marker, me_list);
   1581 		if (me == NULL) {
   1582 			/* End of list: keep marker and return. */
   1583 			mutex_exit(&mountlist_lock);
   1584 			return NULL;
   1585 		}
   1586 		TAILQ_REMOVE(&mountlist, marker, me_list);
   1587 		TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list);
   1588 
   1589 		/* Skip other markers. */
   1590 		if (me->me_type != ME_MOUNT)
   1591 			continue;
   1592 
   1593 		/* Take an initial reference for vfs_busy() below. */
   1594 		mp = me->me_mount;
   1595 		KASSERT(mp != NULL);
   1596 		vfs_ref(mp);
   1597 		mutex_exit(&mountlist_lock);
   1598 
   1599 		/* Try to mark this mount busy and return on success. */
   1600 		if (wait)
   1601 			error = vfs_busy(mp);
   1602 		else
   1603 			error = vfs_trybusy(mp);
   1604 		if (error == 0) {
   1605 			vfs_rele(mp);
   1606 			marker->me_mount = mp;
   1607 			return mp;
   1608 		}
   1609 		vfs_rele(mp);
   1610 		mutex_enter(&mountlist_lock);
   1611 	}
   1612 }
   1613 
   1614 struct mount *
   1615 mountlist_iterator_next(mount_iterator_t *mi)
   1616 {
   1617 
   1618 	return _mountlist_iterator_next(mi, true);
   1619 }
   1620 
   1621 struct mount *
   1622 mountlist_iterator_trynext(mount_iterator_t *mi)
   1623 {
   1624 
   1625 	return _mountlist_iterator_next(mi, false);
   1626 }
   1627 
   1628 /*
   1629  * Attach new mount to the end of the mount list.
   1630  */
   1631 void
   1632 mountlist_append(struct mount *mp)
   1633 {
   1634 	struct mountlist_entry *me;
   1635 
   1636 	me = mountlist_alloc(ME_MOUNT, mp);
   1637 	mutex_enter(&mountlist_lock);
   1638 	TAILQ_INSERT_TAIL(&mountlist, me, me_list);
   1639 	mutex_exit(&mountlist_lock);
   1640 }
   1641 
   1642 /*
   1643  * Remove mount from mount list.
   1644  */void
   1645 mountlist_remove(struct mount *mp)
   1646 {
   1647 	struct mountlist_entry *me;
   1648 
   1649 	mutex_enter(&mountlist_lock);
   1650 	TAILQ_FOREACH(me, &mountlist, me_list)
   1651 		if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1652 			break;
   1653 	KASSERT(me != NULL);
   1654 	TAILQ_REMOVE(&mountlist, me, me_list);
   1655 	mutex_exit(&mountlist_lock);
   1656 	mountlist_free(me);
   1657 }
   1658 
   1659 /*
   1660  * Unlocked variant to traverse the mountlist.
   1661  * To be used from DDB only.
   1662  */
   1663 struct mount *
   1664 _mountlist_next(struct mount *mp)
   1665 {
   1666 	struct mountlist_entry *me;
   1667 
   1668 	if (mp == NULL) {
   1669 		me = TAILQ_FIRST(&mountlist);
   1670 	} else {
   1671 		TAILQ_FOREACH(me, &mountlist, me_list)
   1672 			if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1673 				break;
   1674 		if (me != NULL)
   1675 			me = TAILQ_NEXT(me, me_list);
   1676 	}
   1677 
   1678 	while (me != NULL && me->me_type != ME_MOUNT)
   1679 		me = TAILQ_NEXT(me, me_list);
   1680 
   1681 	return (me ? me->me_mount : NULL);
   1682 }
   1683