Home | History | Annotate | Line # | Download | only in kern
vfs_mount.c revision 1.63
      1 /*	$NetBSD: vfs_mount.c,v 1.63 2017/05/24 09:52:59 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.63 2017/05/24 09:52:59 hannken Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/kernel.h>
     74 
     75 #include <sys/atomic.h>
     76 #include <sys/buf.h>
     77 #include <sys/conf.h>
     78 #include <sys/fcntl.h>
     79 #include <sys/filedesc.h>
     80 #include <sys/device.h>
     81 #include <sys/kauth.h>
     82 #include <sys/kmem.h>
     83 #include <sys/module.h>
     84 #include <sys/mount.h>
     85 #include <sys/fstrans.h>
     86 #include <sys/namei.h>
     87 #include <sys/extattr.h>
     88 #include <sys/syscallargs.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/systm.h>
     91 #include <sys/vfs_syscalls.h>
     92 #include <sys/vnode_impl.h>
     93 
     94 #include <miscfs/genfs/genfs.h>
     95 #include <miscfs/specfs/specdev.h>
     96 
     97 enum mountlist_type {
     98 	ME_MOUNT,
     99 	ME_MARKER
    100 };
    101 struct mountlist_entry {
    102 	TAILQ_ENTRY(mountlist_entry) me_list;	/* Mount list. */
    103 	struct mount *me_mount;			/* Actual mount if ME_MOUNT,
    104 						   current mount else. */
    105 	enum mountlist_type me_type;		/* Mount or marker. */
    106 };
    107 struct mount_iterator {
    108 	struct mountlist_entry mi_entry;
    109 };
    110 
    111 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *,
    112     bool (*)(void *, struct vnode *), void *, bool);
    113 
    114 /* Root filesystem. */
    115 vnode_t *			rootvnode;
    116 
    117 /* Mounted filesystem list. */
    118 static TAILQ_HEAD(mountlist, mountlist_entry) mountlist;
    119 static kmutex_t			mountlist_lock;
    120 int vnode_offset_next_by_lru	/* XXX: ugly hack for pstat.c */
    121     = offsetof(vnode_impl_t, vi_lrulist.tqe_next);
    122 
    123 kmutex_t			mntvnode_lock;
    124 kmutex_t			vfs_list_lock;
    125 
    126 static specificdata_domain_t	mount_specificdata_domain;
    127 static kmutex_t			mntid_lock;
    128 
    129 static kmutex_t			mountgen_lock;
    130 static uint64_t			mountgen;
    131 
    132 void
    133 vfs_mount_sysinit(void)
    134 {
    135 
    136 	TAILQ_INIT(&mountlist);
    137 	mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
    138 	mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
    139 	mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
    140 
    141 	mount_specificdata_domain = specificdata_domain_create();
    142 	mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
    143 	mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
    144 	mountgen = 0;
    145 }
    146 
    147 struct mount *
    148 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp)
    149 {
    150 	struct mount *mp;
    151 	int error __diagused;
    152 	extern struct vfsops dead_vfsops;
    153 
    154 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
    155 	if (mp == NULL)
    156 		return NULL;
    157 
    158 	mp->mnt_op = vfsops;
    159 	mp->mnt_refcnt = 1;
    160 	TAILQ_INIT(&mp->mnt_vnodelist);
    161 	mutex_init(&mp->mnt_unmounting, MUTEX_DEFAULT, IPL_NONE);
    162 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    163 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    164 	mp->mnt_vnodecovered = vp;
    165 	mount_initspecific(mp);
    166 	if (vfsops != &dead_vfsops) {
    167 		error = fstrans_mount(mp);
    168 		KASSERT(error == 0);
    169 	}
    170 
    171 	mutex_enter(&mountgen_lock);
    172 	mp->mnt_gen = mountgen++;
    173 	mutex_exit(&mountgen_lock);
    174 
    175 	return mp;
    176 }
    177 
    178 /*
    179  * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and
    180  * initialize a mount structure for it.
    181  *
    182  * Devname is usually updated by mount(8) after booting.
    183  */
    184 int
    185 vfs_rootmountalloc(const char *fstypename, const char *devname,
    186     struct mount **mpp)
    187 {
    188 	struct vfsops *vfsp = NULL;
    189 	struct mount *mp;
    190 	int error __diagused;
    191 
    192 	mutex_enter(&vfs_list_lock);
    193 	LIST_FOREACH(vfsp, &vfs_list, vfs_list)
    194 		if (!strncmp(vfsp->vfs_name, fstypename,
    195 		    sizeof(mp->mnt_stat.f_fstypename)))
    196 			break;
    197 	if (vfsp == NULL) {
    198 		mutex_exit(&vfs_list_lock);
    199 		return (ENODEV);
    200 	}
    201 	vfsp->vfs_refcount++;
    202 	mutex_exit(&vfs_list_lock);
    203 
    204 	if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
    205 		return ENOMEM;
    206 	error = vfs_busy(mp);
    207 	KASSERT(error == 0);
    208 	mp->mnt_flag = MNT_RDONLY;
    209 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
    210 	    sizeof(mp->mnt_stat.f_fstypename));
    211 	mp->mnt_stat.f_mntonname[0] = '/';
    212 	mp->mnt_stat.f_mntonname[1] = '\0';
    213 	mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
    214 	    '\0';
    215 	(void)copystr(devname, mp->mnt_stat.f_mntfromname,
    216 	    sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
    217 	*mpp = mp;
    218 	return 0;
    219 }
    220 
    221 /*
    222  * vfs_getnewfsid: get a new unique fsid.
    223  */
    224 void
    225 vfs_getnewfsid(struct mount *mp)
    226 {
    227 	static u_short xxxfs_mntid;
    228 	fsid_t tfsid;
    229 	int mtype;
    230 
    231 	mutex_enter(&mntid_lock);
    232 	mtype = makefstype(mp->mnt_op->vfs_name);
    233 	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
    234 	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
    235 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    236 	if (xxxfs_mntid == 0)
    237 		++xxxfs_mntid;
    238 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
    239 	tfsid.__fsid_val[1] = mtype;
    240 	while (vfs_getvfs(&tfsid)) {
    241 		tfsid.__fsid_val[0]++;
    242 		xxxfs_mntid++;
    243 	}
    244 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
    245 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    246 	mutex_exit(&mntid_lock);
    247 }
    248 
    249 /*
    250  * Lookup a mount point by filesystem identifier.
    251  *
    252  * XXX Needs to add a reference to the mount point.
    253  */
    254 struct mount *
    255 vfs_getvfs(fsid_t *fsid)
    256 {
    257 	mount_iterator_t *iter;
    258 	struct mount *mp;
    259 
    260 	mountlist_iterator_init(&iter);
    261 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    262 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
    263 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
    264 			mountlist_iterator_destroy(iter);
    265 			return mp;
    266 		}
    267 	}
    268 	mountlist_iterator_destroy(iter);
    269 	return NULL;
    270 }
    271 
    272 /*
    273  * Take a reference to a mount structure.
    274  */
    275 void
    276 vfs_ref(struct mount *mp)
    277 {
    278 
    279 	KASSERT(mp->mnt_refcnt > 0 || mutex_owned(&mountlist_lock));
    280 
    281 	atomic_inc_uint(&mp->mnt_refcnt);
    282 }
    283 
    284 /*
    285  * Drop a reference to a mount structure, freeing if the last reference.
    286  */
    287 void
    288 vfs_rele(struct mount *mp)
    289 {
    290 
    291 	if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
    292 		return;
    293 	}
    294 
    295 	/*
    296 	 * Nothing else has visibility of the mount: we can now
    297 	 * free the data structures.
    298 	 */
    299 	KASSERT(mp->mnt_refcnt == 0);
    300 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
    301 	mutex_destroy(&mp->mnt_unmounting);
    302 	mutex_destroy(&mp->mnt_updating);
    303 	mutex_destroy(&mp->mnt_renamelock);
    304 	if (mp->mnt_op != NULL) {
    305 		vfs_delref(mp->mnt_op);
    306 	}
    307 	kmem_free(mp, sizeof(*mp));
    308 }
    309 
    310 /*
    311  * Mark a mount point as busy, and gain a new reference to it.  Used to
    312  * prevent the file system from being unmounted during critical sections.
    313  *
    314  * vfs_busy can be called multiple times and by multiple threads
    315  * and must be accompanied by the same number of vfs_unbusy calls.
    316  *
    317  * => The caller must hold a pre-existing reference to the mount.
    318  * => Will fail if the file system is being unmounted, or is unmounted.
    319  */
    320 static inline int
    321 _vfs_busy(struct mount *mp, bool wait)
    322 {
    323 
    324 	KASSERT(mp->mnt_refcnt > 0);
    325 
    326 	if (wait) {
    327 		fstrans_start(mp, FSTRANS_SHARED);
    328 		mutex_enter(&mp->mnt_unmounting);
    329 	} else {
    330 		if (fstrans_start_nowait(mp, FSTRANS_SHARED))
    331 			return EBUSY;
    332 		if (!mutex_tryenter(&mp->mnt_unmounting)) {
    333 			fstrans_done(mp);
    334 			return EBUSY;
    335 		}
    336 	}
    337 	if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
    338 		mutex_exit(&mp->mnt_unmounting);
    339 		fstrans_done(mp);
    340 		return ENOENT;
    341 	}
    342 	++mp->mnt_busynest;
    343 	KASSERT(mp->mnt_busynest != 0);
    344 	mutex_exit(&mp->mnt_unmounting);
    345 	vfs_ref(mp);
    346 	return 0;
    347 }
    348 
    349 int
    350 vfs_busy(struct mount *mp)
    351 {
    352 
    353 	return _vfs_busy(mp, true);
    354 }
    355 
    356 int
    357 vfs_trybusy(struct mount *mp)
    358 {
    359 
    360 	return _vfs_busy(mp, false);
    361 }
    362 
    363 /*
    364  * Unbusy a busy filesystem.
    365  *
    366  * Every successful vfs_busy() call must be undone by a vfs_unbusy() call.
    367  */
    368 void
    369 vfs_unbusy(struct mount *mp)
    370 {
    371 
    372 	KASSERT(mp->mnt_refcnt > 0);
    373 
    374 	mutex_enter(&mp->mnt_unmounting);
    375 	KASSERT(mp->mnt_busynest != 0);
    376 	mp->mnt_busynest--;
    377 	mutex_exit(&mp->mnt_unmounting);
    378 	fstrans_done(mp);
    379 	vfs_rele(mp);
    380 }
    381 
    382 struct vnode_iterator {
    383 	vnode_impl_t vi_vnode;
    384 };
    385 
    386 void
    387 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vnip)
    388 {
    389 	vnode_t *vp;
    390 	vnode_impl_t *vip;
    391 
    392 	vp = vnalloc_marker(mp);
    393 	vip = VNODE_TO_VIMPL(vp);
    394 
    395 	mutex_enter(&mntvnode_lock);
    396 	TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vip, vi_mntvnodes);
    397 	vp->v_usecount = 1;
    398 	mutex_exit(&mntvnode_lock);
    399 
    400 	*vnip = (struct vnode_iterator *)vip;
    401 }
    402 
    403 void
    404 vfs_vnode_iterator_destroy(struct vnode_iterator *vni)
    405 {
    406 	vnode_impl_t *mvip = &vni->vi_vnode;
    407 	vnode_t *mvp = VIMPL_TO_VNODE(mvip);
    408 
    409 	mutex_enter(&mntvnode_lock);
    410 	KASSERT(vnis_marker(mvp));
    411 	if (mvp->v_usecount != 0) {
    412 		TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvip, vi_mntvnodes);
    413 		mvp->v_usecount = 0;
    414 	}
    415 	mutex_exit(&mntvnode_lock);
    416 	vnfree_marker(mvp);
    417 }
    418 
    419 static struct vnode *
    420 vfs_vnode_iterator_next1(struct vnode_iterator *vni,
    421     bool (*f)(void *, struct vnode *), void *cl, bool do_wait)
    422 {
    423 	vnode_impl_t *mvip = &vni->vi_vnode;
    424 	struct mount *mp = VIMPL_TO_VNODE(mvip)->v_mount;
    425 	vnode_t *vp;
    426 	vnode_impl_t *vip;
    427 	int error;
    428 
    429 	KASSERT(vnis_marker(VIMPL_TO_VNODE(mvip)));
    430 
    431 	do {
    432 		mutex_enter(&mntvnode_lock);
    433 		vip = TAILQ_NEXT(mvip, vi_mntvnodes);
    434 		TAILQ_REMOVE(&mp->mnt_vnodelist, mvip, vi_mntvnodes);
    435 		VIMPL_TO_VNODE(mvip)->v_usecount = 0;
    436 again:
    437 		vp = VIMPL_TO_VNODE(vip);
    438 		if (vp == NULL) {
    439 	       		mutex_exit(&mntvnode_lock);
    440 	       		return NULL;
    441 		}
    442 		mutex_enter(vp->v_interlock);
    443 		if (vnis_marker(vp) ||
    444 		    vdead_check(vp, (do_wait ? 0 : VDEAD_NOWAIT)) ||
    445 		    (f && !(*f)(cl, vp))) {
    446 			mutex_exit(vp->v_interlock);
    447 			vip = TAILQ_NEXT(vip, vi_mntvnodes);
    448 			goto again;
    449 		}
    450 
    451 		TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vip, mvip, vi_mntvnodes);
    452 		VIMPL_TO_VNODE(mvip)->v_usecount = 1;
    453 		mutex_exit(&mntvnode_lock);
    454 		error = vcache_vget(vp);
    455 		KASSERT(error == 0 || error == ENOENT);
    456 	} while (error != 0);
    457 
    458 	return vp;
    459 }
    460 
    461 struct vnode *
    462 vfs_vnode_iterator_next(struct vnode_iterator *vni,
    463     bool (*f)(void *, struct vnode *), void *cl)
    464 {
    465 
    466 	return vfs_vnode_iterator_next1(vni, f, cl, false);
    467 }
    468 
    469 /*
    470  * Move a vnode from one mount queue to another.
    471  */
    472 void
    473 vfs_insmntque(vnode_t *vp, struct mount *mp)
    474 {
    475 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    476 	struct mount *omp;
    477 
    478 	KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
    479 	    vp->v_tag == VT_VFS);
    480 
    481 	mutex_enter(&mntvnode_lock);
    482 	/*
    483 	 * Delete from old mount point vnode list, if on one.
    484 	 */
    485 	if ((omp = vp->v_mount) != NULL)
    486 		TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vip, vi_mntvnodes);
    487 	/*
    488 	 * Insert into list of vnodes for the new mount point, if
    489 	 * available.  The caller must take a reference on the mount
    490 	 * structure and donate to the vnode.
    491 	 */
    492 	if ((vp->v_mount = mp) != NULL)
    493 		TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vip, vi_mntvnodes);
    494 	mutex_exit(&mntvnode_lock);
    495 
    496 	if (omp != NULL) {
    497 		/* Release reference to old mount. */
    498 		vfs_rele(omp);
    499 	}
    500 }
    501 
    502 /*
    503  * Remove any vnodes in the vnode table belonging to mount point mp.
    504  *
    505  * If FORCECLOSE is not specified, there should not be any active ones,
    506  * return error if any are found (nb: this is a user error, not a
    507  * system error). If FORCECLOSE is specified, detach any active vnodes
    508  * that are found.
    509  *
    510  * If WRITECLOSE is set, only flush out regular file vnodes open for
    511  * writing.
    512  *
    513  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
    514  */
    515 #ifdef DEBUG
    516 int busyprt = 0;	/* print out busy vnodes */
    517 struct ctldebug debug1 = { "busyprt", &busyprt };
    518 #endif
    519 
    520 static vnode_t *
    521 vflushnext(struct vnode_iterator *marker, int *when)
    522 {
    523 	if (hardclock_ticks > *when) {
    524 		yield();
    525 		*when = hardclock_ticks + hz / 10;
    526 	}
    527 	return vfs_vnode_iterator_next1(marker, NULL, NULL, true);
    528 }
    529 
    530 /*
    531  * Flush one vnode.  Referenced on entry, unreferenced on return.
    532  */
    533 static int
    534 vflush_one(vnode_t *vp, vnode_t *skipvp, int flags)
    535 {
    536 	int error;
    537 	struct vattr vattr;
    538 
    539 	if (vp == skipvp ||
    540 	    ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))) {
    541 		vrele(vp);
    542 		return 0;
    543 	}
    544 	/*
    545 	 * If WRITECLOSE is set, only flush out regular file
    546 	 * vnodes open for writing or open and unlinked.
    547 	 */
    548 	if ((flags & WRITECLOSE)) {
    549 		if (vp->v_type != VREG) {
    550 			vrele(vp);
    551 			return 0;
    552 		}
    553 		error = vn_lock(vp, LK_EXCLUSIVE);
    554 		if (error) {
    555 			KASSERT(error == ENOENT);
    556 			vrele(vp);
    557 			return 0;
    558 		}
    559 		error = VOP_FSYNC(vp, curlwp->l_cred, FSYNC_WAIT, 0, 0);
    560 		if (error == 0)
    561 			error = VOP_GETATTR(vp, &vattr, curlwp->l_cred);
    562 		VOP_UNLOCK(vp);
    563 		if (error) {
    564 			vrele(vp);
    565 			return error;
    566 		}
    567 		if (vp->v_writecount == 0 && vattr.va_nlink > 0) {
    568 			vrele(vp);
    569 			return 0;
    570 		}
    571 	}
    572 	/*
    573 	 * First try to recycle the vnode.
    574 	 */
    575 	if (vrecycle(vp))
    576 		return 0;
    577 	/*
    578 	 * If FORCECLOSE is set, forcibly close the vnode.
    579 	 */
    580 	if (flags & FORCECLOSE) {
    581 		vgone(vp);
    582 		return 0;
    583 	}
    584 	vrele(vp);
    585 	return EBUSY;
    586 }
    587 
    588 int
    589 vflush(struct mount *mp, vnode_t *skipvp, int flags)
    590 {
    591 	vnode_t *vp;
    592 	struct vnode_iterator *marker;
    593 	int busy, error, when, retries = 2;
    594 
    595 	do {
    596 		busy = error = when = 0;
    597 
    598 		/*
    599 		 * First, flush out any vnode references from the
    600 		 * deferred vrele list.
    601 		 */
    602 		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 	error = vfs_suspend(mp, 0);
    873 	if (error) {
    874 		return error;
    875 	}
    876 
    877 	/*
    878 	 * Abort unmount attempt when the filesystem is in use
    879 	 */
    880 	mutex_enter(&mp->mnt_unmounting);
    881 	if (mp->mnt_busynest != 0) {
    882 		mutex_exit(&mp->mnt_unmounting);
    883 		vfs_resume(mp);
    884 		return EBUSY;
    885 	}
    886 
    887 	/*
    888 	 * Abort unmount attempt when the filesystem is not mounted
    889 	 */
    890 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
    891 		mutex_exit(&mp->mnt_unmounting);
    892 		return ENOENT;
    893 	}
    894 
    895 	used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
    896 	used_extattr = mp->mnt_flag & MNT_EXTATTR;
    897 
    898 	mp->mnt_iflag |= IMNT_UNMOUNT;
    899 	mutex_enter(&mp->mnt_updating);
    900 	async = mp->mnt_flag & MNT_ASYNC;
    901 	mp->mnt_flag &= ~MNT_ASYNC;
    902 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    903 	if (used_syncer)
    904 		vfs_syncer_remove_from_worklist(mp);
    905 	error = 0;
    906 	if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
    907 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    908 	}
    909 	if (error == 0 || (flags & MNT_FORCE)) {
    910 		error = VFS_UNMOUNT(mp, flags);
    911 	}
    912 	if (error) {
    913 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    914 		mutex_exit(&mp->mnt_unmounting);
    915 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    916 			vfs_syncer_add_to_worklist(mp);
    917 		mp->mnt_flag |= async;
    918 		mutex_exit(&mp->mnt_updating);
    919 		vfs_resume(mp);
    920 		if (used_extattr) {
    921 			if (start_extattr(mp) != 0)
    922 				mp->mnt_flag &= ~MNT_EXTATTR;
    923 			else
    924 				mp->mnt_flag |= MNT_EXTATTR;
    925 		}
    926 		return (error);
    927 	}
    928 	mutex_exit(&mp->mnt_updating);
    929 
    930 	/*
    931 	 * release mnt_umounting lock here, because other code calls
    932 	 * vfs_busy() while holding the mountlist_lock.
    933 	 *
    934 	 * mark filesystem as gone to prevent further umounts
    935 	 * after mnt_umounting lock is gone, this also prevents
    936 	 * vfs_busy() from succeeding.
    937 	 */
    938 	mp->mnt_iflag |= IMNT_GONE;
    939 	mutex_exit(&mp->mnt_unmounting);
    940 	vfs_resume(mp);
    941 
    942 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    943 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
    944 		coveredvp->v_mountedhere = NULL;
    945 		VOP_UNLOCK(coveredvp);
    946 	}
    947 	mountlist_remove(mp);
    948 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    949 		panic("unmount: dangling vnode");
    950 	vfs_hooks_unmount(mp);
    951 
    952 	fstrans_unmount(mp);
    953 	vfs_rele(mp);	/* reference from mount() */
    954 	if (coveredvp != NULLVP) {
    955 		vrele(coveredvp);
    956 	}
    957 	return (0);
    958 }
    959 
    960 /*
    961  * Unmount all file systems.
    962  * We traverse the list in reverse order under the assumption that doing so
    963  * will avoid needing to worry about dependencies.
    964  */
    965 bool
    966 vfs_unmountall(struct lwp *l)
    967 {
    968 
    969 	printf("unmounting file systems...\n");
    970 	return vfs_unmountall1(l, true, true);
    971 }
    972 
    973 static void
    974 vfs_unmount_print(struct mount *mp, const char *pfx)
    975 {
    976 
    977 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
    978 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
    979 	    mp->mnt_stat.f_fstypename);
    980 }
    981 
    982 /*
    983  * Return the mount with the highest generation less than "gen".
    984  */
    985 static struct mount *
    986 vfs_unmount_next(uint64_t gen)
    987 {
    988 	mount_iterator_t *iter;
    989 	struct mount *mp, *nmp;
    990 
    991 	nmp = NULL;
    992 
    993 	mountlist_iterator_init(&iter);
    994 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    995 		if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) &&
    996 		    mp->mnt_gen < gen) {
    997 			if (nmp != NULL)
    998 				vfs_rele(nmp);
    999 			nmp = mp;
   1000 			vfs_ref(nmp);
   1001 		}
   1002 	}
   1003 	mountlist_iterator_destroy(iter);
   1004 
   1005 	return nmp;
   1006 }
   1007 
   1008 bool
   1009 vfs_unmount_forceone(struct lwp *l)
   1010 {
   1011 	struct mount *mp;
   1012 	int error;
   1013 
   1014 	mp = vfs_unmount_next(mountgen);
   1015 	if (mp == NULL) {
   1016 		return false;
   1017 	}
   1018 
   1019 #ifdef DEBUG
   1020 	printf("forcefully unmounting %s (%s)...\n",
   1021 	    mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
   1022 #endif
   1023 	if ((error = dounmount(mp, MNT_FORCE, l)) == 0) {
   1024 		vfs_unmount_print(mp, "forcefully ");
   1025 		return true;
   1026 	} else {
   1027 		vfs_rele(mp);
   1028 	}
   1029 
   1030 #ifdef DEBUG
   1031 	printf("forceful unmount of %s failed with error %d\n",
   1032 	    mp->mnt_stat.f_mntonname, error);
   1033 #endif
   1034 
   1035 	return false;
   1036 }
   1037 
   1038 bool
   1039 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
   1040 {
   1041 	struct mount *mp;
   1042 	bool any_error = false, progress = false;
   1043 	uint64_t gen;
   1044 	int error;
   1045 
   1046 	gen = mountgen;
   1047 	for (;;) {
   1048 		mp = vfs_unmount_next(gen);
   1049 		if (mp == NULL)
   1050 			break;
   1051 		gen = mp->mnt_gen;
   1052 
   1053 #ifdef DEBUG
   1054 		printf("unmounting %p %s (%s)...\n",
   1055 		    (void *)mp, mp->mnt_stat.f_mntonname,
   1056 		    mp->mnt_stat.f_mntfromname);
   1057 #endif
   1058 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
   1059 			vfs_unmount_print(mp, "");
   1060 			progress = true;
   1061 		} else {
   1062 			vfs_rele(mp);
   1063 			if (verbose) {
   1064 				printf("unmount of %s failed with error %d\n",
   1065 				    mp->mnt_stat.f_mntonname, error);
   1066 			}
   1067 			any_error = true;
   1068 		}
   1069 	}
   1070 	if (verbose) {
   1071 		printf("unmounting done\n");
   1072 	}
   1073 	if (any_error && verbose) {
   1074 		printf("WARNING: some file systems would not unmount\n");
   1075 	}
   1076 	return progress;
   1077 }
   1078 
   1079 void
   1080 vfs_sync_all(struct lwp *l)
   1081 {
   1082 	printf("syncing disks... ");
   1083 
   1084 	/* remove user processes from run queue */
   1085 	suspendsched();
   1086 	(void)spl0();
   1087 
   1088 	/* avoid coming back this way again if we panic. */
   1089 	doing_shutdown = 1;
   1090 
   1091 	do_sys_sync(l);
   1092 
   1093 	/* Wait for sync to finish. */
   1094 	if (buf_syncwait() != 0) {
   1095 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
   1096 		Debugger();
   1097 #endif
   1098 		printf("giving up\n");
   1099 		return;
   1100 	} else
   1101 		printf("done\n");
   1102 }
   1103 
   1104 /*
   1105  * Sync and unmount file systems before shutting down.
   1106  */
   1107 void
   1108 vfs_shutdown(void)
   1109 {
   1110 	lwp_t *l = curlwp;
   1111 
   1112 	vfs_sync_all(l);
   1113 
   1114 	/*
   1115 	 * If we have paniced - do not make the situation potentially
   1116 	 * worse by unmounting the file systems.
   1117 	 */
   1118 	if (panicstr != NULL) {
   1119 		return;
   1120 	}
   1121 
   1122 	/* Unmount file systems. */
   1123 	vfs_unmountall(l);
   1124 }
   1125 
   1126 /*
   1127  * Print a list of supported file system types (used by vfs_mountroot)
   1128  */
   1129 static void
   1130 vfs_print_fstypes(void)
   1131 {
   1132 	struct vfsops *v;
   1133 	int cnt = 0;
   1134 
   1135 	mutex_enter(&vfs_list_lock);
   1136 	LIST_FOREACH(v, &vfs_list, vfs_list)
   1137 		++cnt;
   1138 	mutex_exit(&vfs_list_lock);
   1139 
   1140 	if (cnt == 0) {
   1141 		printf("WARNING: No file system modules have been loaded.\n");
   1142 		return;
   1143 	}
   1144 
   1145 	printf("Supported file systems:");
   1146 	mutex_enter(&vfs_list_lock);
   1147 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1148 		printf(" %s", v->vfs_name);
   1149 	}
   1150 	mutex_exit(&vfs_list_lock);
   1151 	printf("\n");
   1152 }
   1153 
   1154 /*
   1155  * Mount the root file system.  If the operator didn't specify a
   1156  * file system to use, try all possible file systems until one
   1157  * succeeds.
   1158  */
   1159 int
   1160 vfs_mountroot(void)
   1161 {
   1162 	struct vfsops *v;
   1163 	int error = ENODEV;
   1164 
   1165 	if (root_device == NULL)
   1166 		panic("vfs_mountroot: root device unknown");
   1167 
   1168 	switch (device_class(root_device)) {
   1169 	case DV_IFNET:
   1170 		if (rootdev != NODEV)
   1171 			panic("vfs_mountroot: rootdev set for DV_IFNET "
   1172 			    "(0x%llx -> %llu,%llu)",
   1173 			    (unsigned long long)rootdev,
   1174 			    (unsigned long long)major(rootdev),
   1175 			    (unsigned long long)minor(rootdev));
   1176 		break;
   1177 
   1178 	case DV_DISK:
   1179 		if (rootdev == NODEV)
   1180 			panic("vfs_mountroot: rootdev not set for DV_DISK");
   1181 	        if (bdevvp(rootdev, &rootvp))
   1182 	                panic("vfs_mountroot: can't get vnode for rootdev");
   1183 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
   1184 		if (error) {
   1185 			printf("vfs_mountroot: can't open root device\n");
   1186 			return (error);
   1187 		}
   1188 		break;
   1189 
   1190 	case DV_VIRTUAL:
   1191 		break;
   1192 
   1193 	default:
   1194 		printf("%s: inappropriate for root file system\n",
   1195 		    device_xname(root_device));
   1196 		return (ENODEV);
   1197 	}
   1198 
   1199 	/*
   1200 	 * If user specified a root fs type, use it.  Make sure the
   1201 	 * specified type exists and has a mount_root()
   1202 	 */
   1203 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
   1204 		v = vfs_getopsbyname(rootfstype);
   1205 		error = EFTYPE;
   1206 		if (v != NULL) {
   1207 			if (v->vfs_mountroot != NULL) {
   1208 				error = (v->vfs_mountroot)();
   1209 			}
   1210 			v->vfs_refcount--;
   1211 		}
   1212 		goto done;
   1213 	}
   1214 
   1215 	/*
   1216 	 * Try each file system currently configured into the kernel.
   1217 	 */
   1218 	mutex_enter(&vfs_list_lock);
   1219 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1220 		if (v->vfs_mountroot == NULL)
   1221 			continue;
   1222 #ifdef DEBUG
   1223 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
   1224 #endif
   1225 		v->vfs_refcount++;
   1226 		mutex_exit(&vfs_list_lock);
   1227 		error = (*v->vfs_mountroot)();
   1228 		mutex_enter(&vfs_list_lock);
   1229 		v->vfs_refcount--;
   1230 		if (!error) {
   1231 			aprint_normal("root file system type: %s\n",
   1232 			    v->vfs_name);
   1233 			break;
   1234 		}
   1235 	}
   1236 	mutex_exit(&vfs_list_lock);
   1237 
   1238 	if (v == NULL) {
   1239 		vfs_print_fstypes();
   1240 		printf("no file system for %s", device_xname(root_device));
   1241 		if (device_class(root_device) == DV_DISK)
   1242 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
   1243 		printf("\n");
   1244 		error = EFTYPE;
   1245 	}
   1246 
   1247 done:
   1248 	if (error && device_class(root_device) == DV_DISK) {
   1249 		VOP_CLOSE(rootvp, FREAD, FSCRED);
   1250 		vrele(rootvp);
   1251 	}
   1252 	if (error == 0) {
   1253 		mount_iterator_t *iter;
   1254 		struct mount *mp;
   1255 		extern struct cwdinfo cwdi0;
   1256 
   1257 		mountlist_iterator_init(&iter);
   1258 		mp = mountlist_iterator_next(iter);
   1259 		KASSERT(mp != NULL);
   1260 		mountlist_iterator_destroy(iter);
   1261 
   1262 		mp->mnt_flag |= MNT_ROOTFS;
   1263 		mp->mnt_op->vfs_refcount++;
   1264 
   1265 		/*
   1266 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
   1267 		 * reference it.
   1268 		 */
   1269 		error = VFS_ROOT(mp, &rootvnode);
   1270 		if (error)
   1271 			panic("cannot find root vnode, error=%d", error);
   1272 		cwdi0.cwdi_cdir = rootvnode;
   1273 		vref(cwdi0.cwdi_cdir);
   1274 		VOP_UNLOCK(rootvnode);
   1275 		cwdi0.cwdi_rdir = NULL;
   1276 
   1277 		/*
   1278 		 * Now that root is mounted, we can fixup initproc's CWD
   1279 		 * info.  All other processes are kthreads, which merely
   1280 		 * share proc0's CWD info.
   1281 		 */
   1282 		initproc->p_cwdi->cwdi_cdir = rootvnode;
   1283 		vref(initproc->p_cwdi->cwdi_cdir);
   1284 		initproc->p_cwdi->cwdi_rdir = NULL;
   1285 		/*
   1286 		 * Enable loading of modules from the filesystem
   1287 		 */
   1288 		module_load_vfs_init();
   1289 
   1290 	}
   1291 	return (error);
   1292 }
   1293 
   1294 /*
   1295  * mount_specific_key_create --
   1296  *	Create a key for subsystem mount-specific data.
   1297  */
   1298 int
   1299 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1300 {
   1301 
   1302 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
   1303 }
   1304 
   1305 /*
   1306  * mount_specific_key_delete --
   1307  *	Delete a key for subsystem mount-specific data.
   1308  */
   1309 void
   1310 mount_specific_key_delete(specificdata_key_t key)
   1311 {
   1312 
   1313 	specificdata_key_delete(mount_specificdata_domain, key);
   1314 }
   1315 
   1316 /*
   1317  * mount_initspecific --
   1318  *	Initialize a mount's specificdata container.
   1319  */
   1320 void
   1321 mount_initspecific(struct mount *mp)
   1322 {
   1323 	int error __diagused;
   1324 
   1325 	error = specificdata_init(mount_specificdata_domain,
   1326 				  &mp->mnt_specdataref);
   1327 	KASSERT(error == 0);
   1328 }
   1329 
   1330 /*
   1331  * mount_finispecific --
   1332  *	Finalize a mount's specificdata container.
   1333  */
   1334 void
   1335 mount_finispecific(struct mount *mp)
   1336 {
   1337 
   1338 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
   1339 }
   1340 
   1341 /*
   1342  * mount_getspecific --
   1343  *	Return mount-specific data corresponding to the specified key.
   1344  */
   1345 void *
   1346 mount_getspecific(struct mount *mp, specificdata_key_t key)
   1347 {
   1348 
   1349 	return specificdata_getspecific(mount_specificdata_domain,
   1350 					 &mp->mnt_specdataref, key);
   1351 }
   1352 
   1353 /*
   1354  * mount_setspecific --
   1355  *	Set mount-specific data corresponding to the specified key.
   1356  */
   1357 void
   1358 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
   1359 {
   1360 
   1361 	specificdata_setspecific(mount_specificdata_domain,
   1362 				 &mp->mnt_specdataref, key, data);
   1363 }
   1364 
   1365 /*
   1366  * Check to see if a filesystem is mounted on a block device.
   1367  */
   1368 int
   1369 vfs_mountedon(vnode_t *vp)
   1370 {
   1371 	vnode_t *vq;
   1372 	int error = 0;
   1373 
   1374 	if (vp->v_type != VBLK)
   1375 		return ENOTBLK;
   1376 	if (spec_node_getmountedfs(vp) != NULL)
   1377 		return EBUSY;
   1378 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
   1379 		if (spec_node_getmountedfs(vq) != NULL)
   1380 			error = EBUSY;
   1381 		vrele(vq);
   1382 	}
   1383 
   1384 	return error;
   1385 }
   1386 
   1387 /*
   1388  * Check if a device pointed to by vp is mounted.
   1389  *
   1390  * Returns:
   1391  *   EINVAL	if it's not a disk
   1392  *   EBUSY	if it's a disk and mounted
   1393  *   0		if it's a disk and not mounted
   1394  */
   1395 int
   1396 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
   1397 {
   1398 	vnode_t *bvp;
   1399 	dev_t dev;
   1400 	int d_type;
   1401 
   1402 	bvp = NULL;
   1403 	d_type = D_OTHER;
   1404 
   1405 	if (iskmemvp(vp))
   1406 		return EINVAL;
   1407 
   1408 	switch (vp->v_type) {
   1409 	case VCHR: {
   1410 		const struct cdevsw *cdev;
   1411 
   1412 		dev = vp->v_rdev;
   1413 		cdev = cdevsw_lookup(dev);
   1414 		if (cdev != NULL) {
   1415 			dev_t blkdev;
   1416 
   1417 			blkdev = devsw_chr2blk(dev);
   1418 			if (blkdev != NODEV) {
   1419 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
   1420 					d_type = (cdev->d_flag & D_TYPEMASK);
   1421 					/* XXX: what if bvp disappears? */
   1422 					vrele(bvp);
   1423 				}
   1424 			}
   1425 		}
   1426 
   1427 		break;
   1428 		}
   1429 
   1430 	case VBLK: {
   1431 		const struct bdevsw *bdev;
   1432 
   1433 		dev = vp->v_rdev;
   1434 		bdev = bdevsw_lookup(dev);
   1435 		if (bdev != NULL)
   1436 			d_type = (bdev->d_flag & D_TYPEMASK);
   1437 
   1438 		bvp = vp;
   1439 
   1440 		break;
   1441 		}
   1442 
   1443 	default:
   1444 		break;
   1445 	}
   1446 
   1447 	if (d_type != D_DISK)
   1448 		return EINVAL;
   1449 
   1450 	if (bvpp != NULL)
   1451 		*bvpp = bvp;
   1452 
   1453 	/*
   1454 	 * XXX: This is bogus. We should be failing the request
   1455 	 * XXX: not only if this specific slice is mounted, but
   1456 	 * XXX: if it's on a disk with any other mounted slice.
   1457 	 */
   1458 	if (vfs_mountedon(bvp))
   1459 		return EBUSY;
   1460 
   1461 	return 0;
   1462 }
   1463 
   1464 /*
   1465  * Make a 'unique' number from a mount type name.
   1466  */
   1467 long
   1468 makefstype(const char *type)
   1469 {
   1470 	long rv;
   1471 
   1472 	for (rv = 0; *type; type++) {
   1473 		rv <<= 2;
   1474 		rv ^= *type;
   1475 	}
   1476 	return rv;
   1477 }
   1478 
   1479 static struct mountlist_entry *
   1480 mountlist_alloc(enum mountlist_type type, struct mount *mp)
   1481 {
   1482 	struct mountlist_entry *me;
   1483 
   1484 	me = kmem_zalloc(sizeof(*me), KM_SLEEP);
   1485 	me->me_mount = mp;
   1486 	me->me_type = type;
   1487 
   1488 	return me;
   1489 }
   1490 
   1491 static void
   1492 mountlist_free(struct mountlist_entry *me)
   1493 {
   1494 
   1495 	kmem_free(me, sizeof(*me));
   1496 }
   1497 
   1498 void
   1499 mountlist_iterator_init(mount_iterator_t **mip)
   1500 {
   1501 	struct mountlist_entry *me;
   1502 
   1503 	me = mountlist_alloc(ME_MARKER, NULL);
   1504 	mutex_enter(&mountlist_lock);
   1505 	TAILQ_INSERT_HEAD(&mountlist, me, me_list);
   1506 	mutex_exit(&mountlist_lock);
   1507 	*mip = (mount_iterator_t *)me;
   1508 }
   1509 
   1510 void
   1511 mountlist_iterator_destroy(mount_iterator_t *mi)
   1512 {
   1513 	struct mountlist_entry *marker = &mi->mi_entry;
   1514 
   1515 	if (marker->me_mount != NULL)
   1516 		vfs_unbusy(marker->me_mount);
   1517 
   1518 	mutex_enter(&mountlist_lock);
   1519 	TAILQ_REMOVE(&mountlist, marker, me_list);
   1520 	mutex_exit(&mountlist_lock);
   1521 
   1522 	mountlist_free(marker);
   1523 
   1524 }
   1525 
   1526 /*
   1527  * Return the next mount or NULL for this iterator.
   1528  * Mark it busy on success.
   1529  */
   1530 static inline struct mount *
   1531 _mountlist_iterator_next(mount_iterator_t *mi, bool wait)
   1532 {
   1533 	struct mountlist_entry *me, *marker = &mi->mi_entry;
   1534 	struct mount *mp;
   1535 	int error;
   1536 
   1537 	if (marker->me_mount != NULL) {
   1538 		vfs_unbusy(marker->me_mount);
   1539 		marker->me_mount = NULL;
   1540 	}
   1541 
   1542 	mutex_enter(&mountlist_lock);
   1543 	for (;;) {
   1544 		KASSERT(marker->me_type == ME_MARKER);
   1545 
   1546 		me = TAILQ_NEXT(marker, me_list);
   1547 		if (me == NULL) {
   1548 			/* End of list: keep marker and return. */
   1549 			mutex_exit(&mountlist_lock);
   1550 			return NULL;
   1551 		}
   1552 		TAILQ_REMOVE(&mountlist, marker, me_list);
   1553 		TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list);
   1554 
   1555 		/* Skip other markers. */
   1556 		if (me->me_type != ME_MOUNT)
   1557 			continue;
   1558 
   1559 		/* Take an initial reference for vfs_busy() below. */
   1560 		mp = me->me_mount;
   1561 		KASSERT(mp != NULL);
   1562 		vfs_ref(mp);
   1563 		mutex_exit(&mountlist_lock);
   1564 
   1565 		/* Try to mark this mount busy and return on success. */
   1566 		if (wait)
   1567 			error = vfs_busy(mp);
   1568 		else
   1569 			error = vfs_trybusy(mp);
   1570 		if (error == 0) {
   1571 			vfs_rele(mp);
   1572 			marker->me_mount = mp;
   1573 			return mp;
   1574 		}
   1575 		vfs_rele(mp);
   1576 		mutex_enter(&mountlist_lock);
   1577 	}
   1578 }
   1579 
   1580 struct mount *
   1581 mountlist_iterator_next(mount_iterator_t *mi)
   1582 {
   1583 
   1584 	return _mountlist_iterator_next(mi, true);
   1585 }
   1586 
   1587 struct mount *
   1588 mountlist_iterator_trynext(mount_iterator_t *mi)
   1589 {
   1590 
   1591 	return _mountlist_iterator_next(mi, false);
   1592 }
   1593 
   1594 /*
   1595  * Attach new mount to the end of the mount list.
   1596  */
   1597 void
   1598 mountlist_append(struct mount *mp)
   1599 {
   1600 	struct mountlist_entry *me;
   1601 
   1602 	me = mountlist_alloc(ME_MOUNT, mp);
   1603 	mutex_enter(&mountlist_lock);
   1604 	TAILQ_INSERT_TAIL(&mountlist, me, me_list);
   1605 	mutex_exit(&mountlist_lock);
   1606 }
   1607 
   1608 /*
   1609  * Remove mount from mount list.
   1610  */void
   1611 mountlist_remove(struct mount *mp)
   1612 {
   1613 	struct mountlist_entry *me;
   1614 
   1615 	mutex_enter(&mountlist_lock);
   1616 	TAILQ_FOREACH(me, &mountlist, me_list)
   1617 		if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1618 			break;
   1619 	KASSERT(me != NULL);
   1620 	TAILQ_REMOVE(&mountlist, me, me_list);
   1621 	mutex_exit(&mountlist_lock);
   1622 	mountlist_free(me);
   1623 }
   1624 
   1625 /*
   1626  * Unlocked variant to traverse the mountlist.
   1627  * To be used from DDB only.
   1628  */
   1629 struct mount *
   1630 _mountlist_next(struct mount *mp)
   1631 {
   1632 	struct mountlist_entry *me;
   1633 
   1634 	if (mp == NULL) {
   1635 		me = TAILQ_FIRST(&mountlist);
   1636 	} else {
   1637 		TAILQ_FOREACH(me, &mountlist, me_list)
   1638 			if (me->me_type == ME_MOUNT && me->me_mount == mp)
   1639 				break;
   1640 		if (me != NULL)
   1641 			me = TAILQ_NEXT(me, me_list);
   1642 	}
   1643 
   1644 	while (me != NULL && me->me_type != ME_MOUNT)
   1645 		me = TAILQ_NEXT(me, me_list);
   1646 
   1647 	return (me ? me->me_mount : NULL);
   1648 }
   1649