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