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