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