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