Home | History | Annotate | Line # | Download | only in kern
vfs_mount.c revision 1.27
      1 /*	$NetBSD: vfs_mount.c,v 1.27 2014/03/05 09:37:29 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.27 2014/03/05 09:37:29 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/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 	TAILQ_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 __diagused;
    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 (!TAILQ_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 	TAILQ_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 = TAILQ_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 = TAILQ_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 struct vnode_iterator {
    379 	struct vnode vi_vnode;
    380 };
    381 
    382 void
    383 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vipp)
    384 {
    385 	struct vnode *vp;
    386 
    387 	vp = vnalloc(mp);
    388 
    389 	mutex_enter(&mntvnode_lock);
    390 	TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
    391 	vp->v_usecount = 1;
    392 	mutex_exit(&mntvnode_lock);
    393 
    394 	*vipp = (struct vnode_iterator *)vp;
    395 }
    396 
    397 void
    398 vfs_vnode_iterator_destroy(struct vnode_iterator *vi)
    399 {
    400 	struct vnode *mvp = &vi->vi_vnode;
    401 
    402 	mutex_enter(&mntvnode_lock);
    403 	KASSERT(ISSET(mvp->v_iflag, VI_MARKER));
    404 	if (mvp->v_usecount != 0)
    405 		TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvp, v_mntvnodes);
    406 	mutex_exit(&mntvnode_lock);
    407 	vnfree(mvp);
    408 }
    409 
    410 bool
    411 vfs_vnode_iterator_next(struct vnode_iterator *vi, struct vnode **vpp)
    412 {
    413 	struct vnode *mvp = &vi->vi_vnode;
    414 	struct mount *mp = mvp->v_mount;
    415 	struct vnode *vp;
    416 	int error;
    417 
    418 	KASSERT(ISSET(mvp->v_iflag, VI_MARKER));
    419 
    420 	do {
    421 		mutex_enter(&mntvnode_lock);
    422 		vp = TAILQ_NEXT(mvp, v_mntvnodes);
    423 		TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes);
    424 		mvp->v_usecount = 0;
    425 		if (vp == NULL) {
    426 	       		mutex_exit(&mntvnode_lock);
    427 			*vpp = NULL;
    428 	       		return false;
    429 		}
    430 
    431 		mutex_enter(vp->v_interlock);
    432 		while ((vp->v_iflag & VI_MARKER) != 0) {
    433 			mutex_exit(vp->v_interlock);
    434 			vp = TAILQ_NEXT(vp, v_mntvnodes);
    435 			if (vp == NULL) {
    436 				mutex_exit(&mntvnode_lock);
    437 				*vpp = NULL;
    438 				return false;
    439 			}
    440 			mutex_enter(vp->v_interlock);
    441 		}
    442 
    443 		TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes);
    444 		mvp->v_usecount = 1;
    445 		mutex_exit(&mntvnode_lock);
    446 		error = vget(vp, 0);
    447 		KASSERT(error == 0 || error == ENOENT);
    448 	} while (error != 0);
    449 
    450 	*vpp = vp;
    451 	return true;
    452 }
    453 
    454 /*
    455  * Move a vnode from one mount queue to another.
    456  */
    457 void
    458 vfs_insmntque(vnode_t *vp, struct mount *mp)
    459 {
    460 	struct mount *omp;
    461 
    462 	KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
    463 	    vp->v_tag == VT_VFS);
    464 
    465 	mutex_enter(&mntvnode_lock);
    466 	/*
    467 	 * Delete from old mount point vnode list, if on one.
    468 	 */
    469 	if ((omp = vp->v_mount) != NULL)
    470 		TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes);
    471 	/*
    472 	 * Insert into list of vnodes for the new mount point, if
    473 	 * available.  The caller must take a reference on the mount
    474 	 * structure and donate to the vnode.
    475 	 */
    476 	if ((vp->v_mount = mp) != NULL)
    477 		TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
    478 	mutex_exit(&mntvnode_lock);
    479 
    480 	if (omp != NULL) {
    481 		/* Release reference to old mount. */
    482 		vfs_destroy(omp);
    483 	}
    484 }
    485 
    486 /*
    487  * Remove any vnodes in the vnode table belonging to mount point mp.
    488  *
    489  * If FORCECLOSE is not specified, there should not be any active ones,
    490  * return error if any are found (nb: this is a user error, not a
    491  * system error). If FORCECLOSE is specified, detach any active vnodes
    492  * that are found.
    493  *
    494  * If WRITECLOSE is set, only flush out regular file vnodes open for
    495  * writing.
    496  *
    497  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
    498  */
    499 #ifdef DEBUG
    500 int busyprt = 0;	/* print out busy vnodes */
    501 struct ctldebug debug1 = { "busyprt", &busyprt };
    502 #endif
    503 
    504 static vnode_t *
    505 vflushnext(struct vnode_iterator *marker, int *when)
    506 {
    507 	struct vnode *vp;
    508 
    509 	if (hardclock_ticks > *when) {
    510 		yield();
    511 		*when = hardclock_ticks + hz / 10;
    512 	}
    513 	if (vfs_vnode_iterator_next(marker, &vp))
    514 		return vp;
    515 	return NULL;
    516 }
    517 
    518 int
    519 vflush(struct mount *mp, vnode_t *skipvp, int flags)
    520 {
    521 	vnode_t *vp;
    522 	struct vnode_iterator *marker;
    523 	int busy = 0, when = 0;
    524 
    525 	/* First, flush out any vnode references from vrele_list. */
    526 	vrele_flush();
    527 
    528 	vfs_vnode_iterator_init(mp, &marker);
    529 	while ((vp = vflushnext(marker, &when)) != NULL) {
    530 		/*
    531 		 * Skip over a selected vnode.
    532 		 */
    533 		if (vp == skipvp) {
    534 			vrele(vp);
    535 			continue;
    536 		}
    537 		/*
    538 		 * Skip over a vnodes marked VSYSTEM.
    539 		 */
    540 		if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
    541 			vrele(vp);
    542 			continue;
    543 		}
    544 		/*
    545 		 * If WRITECLOSE is set, only flush out regular file
    546 		 * vnodes open for writing.
    547 		 */
    548 		if ((flags & WRITECLOSE) && vp->v_type == VREG) {
    549 			mutex_enter(vp->v_interlock);
    550 			if (vp->v_writecount == 0) {
    551 				mutex_exit(vp->v_interlock);
    552 				vrele(vp);
    553 				continue;
    554 			}
    555 			mutex_exit(vp->v_interlock);
    556 		}
    557 		/*
    558 		 * First try to recycle the vnode.
    559 		 */
    560 		if (vrecycle(vp))
    561 			continue;
    562 		/*
    563 		 * If FORCECLOSE is set, forcibly close the vnode.
    564 		 */
    565 		if (flags & FORCECLOSE) {
    566 			vgone(vp);
    567 			continue;
    568 		}
    569 #ifdef DEBUG
    570 		if (busyprt)
    571 			vprint("vflush: busy vnode", vp);
    572 #endif
    573 		vrele(vp);
    574 		busy++;
    575 	}
    576 	vfs_vnode_iterator_destroy(marker);
    577 	if (busy)
    578 		return (EBUSY);
    579 	return (0);
    580 }
    581 
    582 /*
    583  * Mount a file system.
    584  */
    585 
    586 /*
    587  * Scan all active processes to see if any of them have a current or root
    588  * directory onto which the new filesystem has just been  mounted. If so,
    589  * replace them with the new mount point.
    590  */
    591 static void
    592 mount_checkdirs(vnode_t *olddp)
    593 {
    594 	vnode_t *newdp, *rele1, *rele2;
    595 	struct cwdinfo *cwdi;
    596 	struct proc *p;
    597 	bool retry;
    598 
    599 	if (olddp->v_usecount == 1) {
    600 		return;
    601 	}
    602 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    603 		panic("mount: lost mount");
    604 
    605 	do {
    606 		retry = false;
    607 		mutex_enter(proc_lock);
    608 		PROCLIST_FOREACH(p, &allproc) {
    609 			if ((cwdi = p->p_cwdi) == NULL)
    610 				continue;
    611 			/*
    612 			 * Cannot change to the old directory any more,
    613 			 * so even if we see a stale value it is not a
    614 			 * problem.
    615 			 */
    616 			if (cwdi->cwdi_cdir != olddp &&
    617 			    cwdi->cwdi_rdir != olddp)
    618 				continue;
    619 			retry = true;
    620 			rele1 = NULL;
    621 			rele2 = NULL;
    622 			atomic_inc_uint(&cwdi->cwdi_refcnt);
    623 			mutex_exit(proc_lock);
    624 			rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    625 			if (cwdi->cwdi_cdir == olddp) {
    626 				rele1 = cwdi->cwdi_cdir;
    627 				vref(newdp);
    628 				cwdi->cwdi_cdir = newdp;
    629 			}
    630 			if (cwdi->cwdi_rdir == olddp) {
    631 				rele2 = cwdi->cwdi_rdir;
    632 				vref(newdp);
    633 				cwdi->cwdi_rdir = newdp;
    634 			}
    635 			rw_exit(&cwdi->cwdi_lock);
    636 			cwdfree(cwdi);
    637 			if (rele1 != NULL)
    638 				vrele(rele1);
    639 			if (rele2 != NULL)
    640 				vrele(rele2);
    641 			mutex_enter(proc_lock);
    642 			break;
    643 		}
    644 		mutex_exit(proc_lock);
    645 	} while (retry);
    646 
    647 	if (rootvnode == olddp) {
    648 		vrele(rootvnode);
    649 		vref(newdp);
    650 		rootvnode = newdp;
    651 	}
    652 	vput(newdp);
    653 }
    654 
    655 int
    656 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
    657     const char *path, int flags, void *data, size_t *data_len)
    658 {
    659 	vnode_t *vp = *vpp;
    660 	struct mount *mp;
    661 	struct pathbuf *pb;
    662 	struct nameidata nd;
    663 	int error;
    664 
    665 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    666 	    KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
    667 	if (error) {
    668 		vfs_delref(vfsops);
    669 		return error;
    670 	}
    671 
    672 	/* Cannot make a non-dir a mount-point (from here anyway). */
    673 	if (vp->v_type != VDIR) {
    674 		vfs_delref(vfsops);
    675 		return ENOTDIR;
    676 	}
    677 
    678 	if (flags & MNT_EXPORTED) {
    679 		vfs_delref(vfsops);
    680 		return EINVAL;
    681 	}
    682 
    683 	if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) {
    684 		vfs_delref(vfsops);
    685 		return ENOMEM;
    686 	}
    687 
    688 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
    689 
    690 	/*
    691 	 * The underlying file system may refuse the mount for
    692 	 * various reasons.  Allow the user to force it to happen.
    693 	 *
    694 	 * Set the mount level flags.
    695 	 */
    696 	mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
    697 
    698 	mutex_enter(&mp->mnt_updating);
    699 	error = VFS_MOUNT(mp, path, data, data_len);
    700 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    701 
    702 	if (error != 0)
    703 		goto err_unmounted;
    704 
    705 	/*
    706 	 * Validate and prepare the mount point.
    707 	 */
    708 	error = pathbuf_copyin(path, &pb);
    709 	if (error != 0) {
    710 		goto err_mounted;
    711 	}
    712 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    713 	error = namei(&nd);
    714 	pathbuf_destroy(pb);
    715 	if (error != 0) {
    716 		goto err_mounted;
    717 	}
    718 	if (nd.ni_vp != vp) {
    719 		vput(nd.ni_vp);
    720 		error = EINVAL;
    721 		goto err_mounted;
    722 	}
    723 	if (vp->v_mountedhere != NULL) {
    724 		vput(nd.ni_vp);
    725 		error = EBUSY;
    726 		goto err_mounted;
    727 	}
    728 	error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0);
    729 	if (error != 0) {
    730 		vput(nd.ni_vp);
    731 		goto err_mounted;
    732 	}
    733 
    734 	/*
    735 	 * Put the new filesystem on the mount list after root.
    736 	 */
    737 	cache_purge(vp);
    738 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    739 
    740 	mutex_enter(&mountlist_lock);
    741 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    742 	mutex_exit(&mountlist_lock);
    743 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    744 		error = vfs_allocate_syncvnode(mp);
    745 	if (error == 0)
    746 		vp->v_mountedhere = mp;
    747 	vput(nd.ni_vp);
    748 	if (error != 0)
    749 		goto err_onmountlist;
    750 
    751 	mount_checkdirs(vp);
    752 	mutex_exit(&mp->mnt_updating);
    753 
    754 	/* Hold an additional reference to the mount across VFS_START(). */
    755 	vfs_unbusy(mp, true, NULL);
    756 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    757 	error = VFS_START(mp, 0);
    758        if (error) {
    759 		vrele(vp);
    760        } else if (flags & MNT_EXTATTR) {
    761 	       error = VFS_EXTATTRCTL(vp->v_mountedhere,
    762 		   EXTATTR_CMD_START, NULL, 0, NULL);
    763 	       if (error)
    764 		       printf("%s: failed to start extattr: error = %d\n",
    765 			   vp->v_mountedhere->mnt_stat.f_mntonname, error);
    766        }
    767 	/* Drop reference held for VFS_START(). */
    768 	vfs_destroy(mp);
    769 	*vpp = NULL;
    770 	return error;
    771 
    772 err_onmountlist:
    773 	mutex_enter(&mountlist_lock);
    774 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
    775 	mp->mnt_iflag |= IMNT_GONE;
    776 	mutex_exit(&mountlist_lock);
    777 
    778 err_mounted:
    779 	if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
    780 		panic("Unmounting fresh file system failed");
    781 
    782 err_unmounted:
    783 	vp->v_mountedhere = NULL;
    784 	mutex_exit(&mp->mnt_updating);
    785 	vfs_unbusy(mp, false, NULL);
    786 	vfs_destroy(mp);
    787 
    788 	return error;
    789 }
    790 
    791 /*
    792  * Do the actual file system unmount.  File system is assumed to have
    793  * been locked by the caller.
    794  *
    795  * => Caller hold reference to the mount, explicitly for dounmount().
    796  */
    797 int
    798 dounmount(struct mount *mp, int flags, struct lwp *l)
    799 {
    800 	vnode_t *coveredvp;
    801 	int error, async, used_syncer;
    802 
    803 #if NVERIEXEC > 0
    804 	error = veriexec_unmountchk(mp);
    805 	if (error)
    806 		return (error);
    807 #endif /* NVERIEXEC > 0 */
    808 
    809 	/*
    810 	 * XXX Freeze syncer.  Must do this before locking the
    811 	 * mount point.  See dounmount() for details.
    812 	 */
    813 	mutex_enter(&syncer_mutex);
    814 
    815 	/*
    816 	 * Abort unmount attempt when the filesystem is in use
    817 	 */
    818 	mutex_enter(&mp->mnt_unmounting);
    819 	if (mp->mnt_busynest != 0) {
    820 		mutex_exit(&mp->mnt_unmounting);
    821 		mutex_exit(&syncer_mutex);
    822 		return EBUSY;
    823 	}
    824 
    825 	/*
    826 	 * Abort unmount attempt when the filesystem is not mounted
    827 	 */
    828 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
    829 		mutex_exit(&mp->mnt_unmounting);
    830 		mutex_exit(&syncer_mutex);
    831 		return ENOENT;
    832 	}
    833 
    834 	used_syncer = (mp->mnt_syncer != NULL);
    835 
    836 	/*
    837 	 * XXX Syncer must be frozen when we get here.  This should really
    838 	 * be done on a per-mountpoint basis, but the syncer doesn't work
    839 	 * like that.
    840 	 *
    841 	 * The caller of dounmount() must acquire syncer_mutex because
    842 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    843 	 * order, and we must preserve that order to avoid deadlock.
    844 	 *
    845 	 * So, if the file system did not use the syncer, now is
    846 	 * the time to release the syncer_mutex.
    847 	 */
    848 	if (used_syncer == 0) {
    849 		mutex_exit(&syncer_mutex);
    850 	}
    851 	mp->mnt_iflag |= IMNT_UNMOUNT;
    852 	mutex_enter(&mp->mnt_updating);
    853 	async = mp->mnt_flag & MNT_ASYNC;
    854 	mp->mnt_flag &= ~MNT_ASYNC;
    855 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    856 	if (mp->mnt_syncer != NULL)
    857 		vfs_deallocate_syncvnode(mp);
    858 	error = 0;
    859 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    860 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    861 	}
    862 	if (error == 0 || (flags & MNT_FORCE)) {
    863 		error = VFS_UNMOUNT(mp, flags);
    864 	}
    865 	if (error) {
    866 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    867 		mutex_exit(&mp->mnt_unmounting);
    868 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    869 			(void) vfs_allocate_syncvnode(mp);
    870 		mp->mnt_flag |= async;
    871 		mutex_exit(&mp->mnt_updating);
    872 		if (used_syncer)
    873 			mutex_exit(&syncer_mutex);
    874 		return (error);
    875 	}
    876 	mutex_exit(&mp->mnt_updating);
    877 
    878 	/*
    879 	 * release mnt_umounting lock here, because other code calls
    880 	 * vfs_busy() while holding the mountlist_lock.
    881 	 *
    882 	 * mark filesystem as gone to prevent further umounts
    883 	 * after mnt_umounting lock is gone, this also prevents
    884 	 * vfs_busy() from succeeding.
    885 	 */
    886 	mp->mnt_iflag |= IMNT_GONE;
    887 	mutex_exit(&mp->mnt_unmounting);
    888 
    889 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    890 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
    891 		coveredvp->v_mountedhere = NULL;
    892 		VOP_UNLOCK(coveredvp);
    893 	}
    894 	mutex_enter(&mountlist_lock);
    895 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
    896 	mutex_exit(&mountlist_lock);
    897 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    898 		panic("unmount: dangling vnode");
    899 	if (used_syncer)
    900 		mutex_exit(&syncer_mutex);
    901 	vfs_hooks_unmount(mp);
    902 
    903 	vfs_destroy(mp);	/* reference from mount() */
    904 	if (coveredvp != NULLVP) {
    905 		vrele(coveredvp);
    906 	}
    907 	return (0);
    908 }
    909 
    910 /*
    911  * Unmount all file systems.
    912  * We traverse the list in reverse order under the assumption that doing so
    913  * will avoid needing to worry about dependencies.
    914  */
    915 bool
    916 vfs_unmountall(struct lwp *l)
    917 {
    918 
    919 	printf("unmounting file systems...\n");
    920 	return vfs_unmountall1(l, true, true);
    921 }
    922 
    923 static void
    924 vfs_unmount_print(struct mount *mp, const char *pfx)
    925 {
    926 
    927 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
    928 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
    929 	    mp->mnt_stat.f_fstypename);
    930 }
    931 
    932 bool
    933 vfs_unmount_forceone(struct lwp *l)
    934 {
    935 	struct mount *mp, *nmp;
    936 	int error;
    937 
    938 	nmp = NULL;
    939 
    940 	TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
    941 		if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) {
    942 			nmp = mp;
    943 		}
    944 	}
    945 	if (nmp == NULL) {
    946 		return false;
    947 	}
    948 
    949 #ifdef DEBUG
    950 	printf("forcefully unmounting %s (%s)...\n",
    951 	    nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname);
    952 #endif
    953 	atomic_inc_uint(&nmp->mnt_refcnt);
    954 	if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) {
    955 		vfs_unmount_print(nmp, "forcefully ");
    956 		return true;
    957 	} else {
    958 		vfs_destroy(nmp);
    959 	}
    960 
    961 #ifdef DEBUG
    962 	printf("forceful unmount of %s failed with error %d\n",
    963 	    nmp->mnt_stat.f_mntonname, error);
    964 #endif
    965 
    966 	return false;
    967 }
    968 
    969 bool
    970 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
    971 {
    972 	struct mount *mp, *nmp;
    973 	bool any_error = false, progress = false;
    974 	int error;
    975 
    976 	TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) {
    977 #ifdef DEBUG
    978 		printf("unmounting %p %s (%s)...\n",
    979 		    (void *)mp, mp->mnt_stat.f_mntonname,
    980 		    mp->mnt_stat.f_mntfromname);
    981 #endif
    982 		atomic_inc_uint(&mp->mnt_refcnt);
    983 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
    984 			vfs_unmount_print(mp, "");
    985 			progress = true;
    986 		} else {
    987 			vfs_destroy(mp);
    988 			if (verbose) {
    989 				printf("unmount of %s failed with error %d\n",
    990 				    mp->mnt_stat.f_mntonname, error);
    991 			}
    992 			any_error = true;
    993 		}
    994 	}
    995 	if (verbose) {
    996 		printf("unmounting done\n");
    997 	}
    998 	if (any_error && verbose) {
    999 		printf("WARNING: some file systems would not unmount\n");
   1000 	}
   1001 	return progress;
   1002 }
   1003 
   1004 void
   1005 vfs_sync_all(struct lwp *l)
   1006 {
   1007 	printf("syncing disks... ");
   1008 
   1009 	/* remove user processes from run queue */
   1010 	suspendsched();
   1011 	(void)spl0();
   1012 
   1013 	/* avoid coming back this way again if we panic. */
   1014 	doing_shutdown = 1;
   1015 
   1016 	do_sys_sync(l);
   1017 
   1018 	/* Wait for sync to finish. */
   1019 	if (buf_syncwait() != 0) {
   1020 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
   1021 		Debugger();
   1022 #endif
   1023 		printf("giving up\n");
   1024 		return;
   1025 	} else
   1026 		printf("done\n");
   1027 }
   1028 
   1029 /*
   1030  * Sync and unmount file systems before shutting down.
   1031  */
   1032 void
   1033 vfs_shutdown(void)
   1034 {
   1035 	lwp_t *l = curlwp;
   1036 
   1037 	vfs_sync_all(l);
   1038 
   1039 	/*
   1040 	 * If we have paniced - do not make the situation potentially
   1041 	 * worse by unmounting the file systems.
   1042 	 */
   1043 	if (panicstr != NULL) {
   1044 		return;
   1045 	}
   1046 
   1047 	/* Unmount file systems. */
   1048 	vfs_unmountall(l);
   1049 }
   1050 
   1051 /*
   1052  * Print a list of supported file system types (used by vfs_mountroot)
   1053  */
   1054 static void
   1055 vfs_print_fstypes(void)
   1056 {
   1057 	struct vfsops *v;
   1058 	int cnt = 0;
   1059 
   1060 	mutex_enter(&vfs_list_lock);
   1061 	LIST_FOREACH(v, &vfs_list, vfs_list)
   1062 		++cnt;
   1063 	mutex_exit(&vfs_list_lock);
   1064 
   1065 	if (cnt == 0) {
   1066 		printf("WARNING: No file system modules have been loaded.\n");
   1067 		return;
   1068 	}
   1069 
   1070 	printf("Supported file systems:");
   1071 	mutex_enter(&vfs_list_lock);
   1072 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1073 		printf(" %s", v->vfs_name);
   1074 	}
   1075 	mutex_exit(&vfs_list_lock);
   1076 	printf("\n");
   1077 }
   1078 
   1079 /*
   1080  * Mount the root file system.  If the operator didn't specify a
   1081  * file system to use, try all possible file systems until one
   1082  * succeeds.
   1083  */
   1084 int
   1085 vfs_mountroot(void)
   1086 {
   1087 	struct vfsops *v;
   1088 	int error = ENODEV;
   1089 
   1090 	if (root_device == NULL)
   1091 		panic("vfs_mountroot: root device unknown");
   1092 
   1093 	switch (device_class(root_device)) {
   1094 	case DV_IFNET:
   1095 		if (rootdev != NODEV)
   1096 			panic("vfs_mountroot: rootdev set for DV_IFNET "
   1097 			    "(0x%llx -> %llu,%llu)",
   1098 			    (unsigned long long)rootdev,
   1099 			    (unsigned long long)major(rootdev),
   1100 			    (unsigned long long)minor(rootdev));
   1101 		break;
   1102 
   1103 	case DV_DISK:
   1104 		if (rootdev == NODEV)
   1105 			panic("vfs_mountroot: rootdev not set for DV_DISK");
   1106 	        if (bdevvp(rootdev, &rootvp))
   1107 	                panic("vfs_mountroot: can't get vnode for rootdev");
   1108 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
   1109 		if (error) {
   1110 			printf("vfs_mountroot: can't open root device\n");
   1111 			return (error);
   1112 		}
   1113 		break;
   1114 
   1115 	case DV_VIRTUAL:
   1116 		break;
   1117 
   1118 	default:
   1119 		printf("%s: inappropriate for root file system\n",
   1120 		    device_xname(root_device));
   1121 		return (ENODEV);
   1122 	}
   1123 
   1124 	/*
   1125 	 * If user specified a root fs type, use it.  Make sure the
   1126 	 * specified type exists and has a mount_root()
   1127 	 */
   1128 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
   1129 		v = vfs_getopsbyname(rootfstype);
   1130 		error = EFTYPE;
   1131 		if (v != NULL) {
   1132 			if (v->vfs_mountroot != NULL) {
   1133 				error = (v->vfs_mountroot)();
   1134 			}
   1135 			v->vfs_refcount--;
   1136 		}
   1137 		goto done;
   1138 	}
   1139 
   1140 	/*
   1141 	 * Try each file system currently configured into the kernel.
   1142 	 */
   1143 	mutex_enter(&vfs_list_lock);
   1144 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1145 		if (v->vfs_mountroot == NULL)
   1146 			continue;
   1147 #ifdef DEBUG
   1148 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
   1149 #endif
   1150 		v->vfs_refcount++;
   1151 		mutex_exit(&vfs_list_lock);
   1152 		error = (*v->vfs_mountroot)();
   1153 		mutex_enter(&vfs_list_lock);
   1154 		v->vfs_refcount--;
   1155 		if (!error) {
   1156 			aprint_normal("root file system type: %s\n",
   1157 			    v->vfs_name);
   1158 			break;
   1159 		}
   1160 	}
   1161 	mutex_exit(&vfs_list_lock);
   1162 
   1163 	if (v == NULL) {
   1164 		vfs_print_fstypes();
   1165 		printf("no file system for %s", device_xname(root_device));
   1166 		if (device_class(root_device) == DV_DISK)
   1167 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
   1168 		printf("\n");
   1169 		error = EFTYPE;
   1170 	}
   1171 
   1172 done:
   1173 	if (error && device_class(root_device) == DV_DISK) {
   1174 		VOP_CLOSE(rootvp, FREAD, FSCRED);
   1175 		vrele(rootvp);
   1176 	}
   1177 	if (error == 0) {
   1178 		struct mount *mp;
   1179 		extern struct cwdinfo cwdi0;
   1180 
   1181 		mp = TAILQ_FIRST(&mountlist);
   1182 		mp->mnt_flag |= MNT_ROOTFS;
   1183 		mp->mnt_op->vfs_refcount++;
   1184 
   1185 		/*
   1186 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
   1187 		 * reference it.
   1188 		 */
   1189 		error = VFS_ROOT(mp, &rootvnode);
   1190 		if (error)
   1191 			panic("cannot find root vnode, error=%d", error);
   1192 		cwdi0.cwdi_cdir = rootvnode;
   1193 		vref(cwdi0.cwdi_cdir);
   1194 		VOP_UNLOCK(rootvnode);
   1195 		cwdi0.cwdi_rdir = NULL;
   1196 
   1197 		/*
   1198 		 * Now that root is mounted, we can fixup initproc's CWD
   1199 		 * info.  All other processes are kthreads, which merely
   1200 		 * share proc0's CWD info.
   1201 		 */
   1202 		initproc->p_cwdi->cwdi_cdir = rootvnode;
   1203 		vref(initproc->p_cwdi->cwdi_cdir);
   1204 		initproc->p_cwdi->cwdi_rdir = NULL;
   1205 		/*
   1206 		 * Enable loading of modules from the filesystem
   1207 		 */
   1208 		module_load_vfs_init();
   1209 
   1210 	}
   1211 	return (error);
   1212 }
   1213 
   1214 /*
   1215  * mount_specific_key_create --
   1216  *	Create a key for subsystem mount-specific data.
   1217  */
   1218 int
   1219 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1220 {
   1221 
   1222 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
   1223 }
   1224 
   1225 /*
   1226  * mount_specific_key_delete --
   1227  *	Delete a key for subsystem mount-specific data.
   1228  */
   1229 void
   1230 mount_specific_key_delete(specificdata_key_t key)
   1231 {
   1232 
   1233 	specificdata_key_delete(mount_specificdata_domain, key);
   1234 }
   1235 
   1236 /*
   1237  * mount_initspecific --
   1238  *	Initialize a mount's specificdata container.
   1239  */
   1240 void
   1241 mount_initspecific(struct mount *mp)
   1242 {
   1243 	int error __diagused;
   1244 
   1245 	error = specificdata_init(mount_specificdata_domain,
   1246 				  &mp->mnt_specdataref);
   1247 	KASSERT(error == 0);
   1248 }
   1249 
   1250 /*
   1251  * mount_finispecific --
   1252  *	Finalize a mount's specificdata container.
   1253  */
   1254 void
   1255 mount_finispecific(struct mount *mp)
   1256 {
   1257 
   1258 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
   1259 }
   1260 
   1261 /*
   1262  * mount_getspecific --
   1263  *	Return mount-specific data corresponding to the specified key.
   1264  */
   1265 void *
   1266 mount_getspecific(struct mount *mp, specificdata_key_t key)
   1267 {
   1268 
   1269 	return specificdata_getspecific(mount_specificdata_domain,
   1270 					 &mp->mnt_specdataref, key);
   1271 }
   1272 
   1273 /*
   1274  * mount_setspecific --
   1275  *	Set mount-specific data corresponding to the specified key.
   1276  */
   1277 void
   1278 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
   1279 {
   1280 
   1281 	specificdata_setspecific(mount_specificdata_domain,
   1282 				 &mp->mnt_specdataref, key, data);
   1283 }
   1284 
   1285 /*
   1286  * Check to see if a filesystem is mounted on a block device.
   1287  */
   1288 int
   1289 vfs_mountedon(vnode_t *vp)
   1290 {
   1291 	vnode_t *vq;
   1292 	int error = 0;
   1293 
   1294 	if (vp->v_type != VBLK)
   1295 		return ENOTBLK;
   1296 	if (spec_node_getmountedfs(vp) != NULL)
   1297 		return EBUSY;
   1298 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
   1299 		if (spec_node_getmountedfs(vq) != NULL)
   1300 			error = EBUSY;
   1301 		vrele(vq);
   1302 	}
   1303 
   1304 	return error;
   1305 }
   1306 
   1307 /*
   1308  * Check if a device pointed to by vp is mounted.
   1309  *
   1310  * Returns:
   1311  *   EINVAL	if it's not a disk
   1312  *   EBUSY	if it's a disk and mounted
   1313  *   0		if it's a disk and not mounted
   1314  */
   1315 int
   1316 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
   1317 {
   1318 	vnode_t *bvp;
   1319 	dev_t dev;
   1320 	int d_type;
   1321 
   1322 	bvp = NULL;
   1323 	d_type = D_OTHER;
   1324 
   1325 	if (iskmemvp(vp))
   1326 		return EINVAL;
   1327 
   1328 	switch (vp->v_type) {
   1329 	case VCHR: {
   1330 		const struct cdevsw *cdev;
   1331 
   1332 		dev = vp->v_rdev;
   1333 		cdev = cdevsw_lookup(dev);
   1334 		if (cdev != NULL) {
   1335 			dev_t blkdev;
   1336 
   1337 			blkdev = devsw_chr2blk(dev);
   1338 			if (blkdev != NODEV) {
   1339 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
   1340 					d_type = (cdev->d_flag & D_TYPEMASK);
   1341 					/* XXX: what if bvp disappears? */
   1342 					vrele(bvp);
   1343 				}
   1344 			}
   1345 		}
   1346 
   1347 		break;
   1348 		}
   1349 
   1350 	case VBLK: {
   1351 		const struct bdevsw *bdev;
   1352 
   1353 		dev = vp->v_rdev;
   1354 		bdev = bdevsw_lookup(dev);
   1355 		if (bdev != NULL)
   1356 			d_type = (bdev->d_flag & D_TYPEMASK);
   1357 
   1358 		bvp = vp;
   1359 
   1360 		break;
   1361 		}
   1362 
   1363 	default:
   1364 		break;
   1365 	}
   1366 
   1367 	if (d_type != D_DISK)
   1368 		return EINVAL;
   1369 
   1370 	if (bvpp != NULL)
   1371 		*bvpp = bvp;
   1372 
   1373 	/*
   1374 	 * XXX: This is bogus. We should be failing the request
   1375 	 * XXX: not only if this specific slice is mounted, but
   1376 	 * XXX: if it's on a disk with any other mounted slice.
   1377 	 */
   1378 	if (vfs_mountedon(bvp))
   1379 		return EBUSY;
   1380 
   1381 	return 0;
   1382 }
   1383 
   1384 /*
   1385  * Make a 'unique' number from a mount type name.
   1386  */
   1387 long
   1388 makefstype(const char *type)
   1389 {
   1390 	long rv;
   1391 
   1392 	for (rv = 0; *type; type++) {
   1393 		rv <<= 2;
   1394 		rv ^= *type;
   1395 	}
   1396 	return rv;
   1397 }
   1398 
   1399 void
   1400 mountlist_append(struct mount *mp)
   1401 {
   1402 	mutex_enter(&mountlist_lock);
   1403 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
   1404 	mutex_exit(&mountlist_lock);
   1405 }
   1406