Home | History | Annotate | Line # | Download | only in kern
vfs_mount.c revision 1.40.2.5
      1 /*	$NetBSD: vfs_mount.c,v 1.40.2.5 2017/03/20 06:57:48 pgoyette 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.40.2.5 2017/03/20 06:57:48 pgoyette 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 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
    715 
    716 	/*
    717 	 * The underlying file system may refuse the mount for
    718 	 * various reasons.  Allow the user to force it to happen.
    719 	 *
    720 	 * Set the mount level flags.
    721 	 */
    722 	mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
    723 
    724 	mutex_enter(&mp->mnt_updating);
    725 	error = VFS_MOUNT(mp, path, data, data_len);
    726 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    727 
    728 	if (error != 0)
    729 		goto err_unmounted;
    730 
    731 	if (mp->mnt_lower == NULL) {
    732 		error = fstrans_mount(mp);
    733 		if (error)
    734 			goto err_mounted;
    735 	}
    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 	struct mount *cmp;
    821 	vnode_t *coveredvp;
    822 	int error, async, used_syncer, used_extattr;
    823 
    824 #if NVERIEXEC > 0
    825 	error = veriexec_unmountchk(mp);
    826 	if (error)
    827 		return (error);
    828 #endif /* NVERIEXEC > 0 */
    829 
    830 	/*
    831 	 * No unmount below layered mounts.
    832 	 */
    833 	mutex_enter(&mountlist_lock);
    834 	TAILQ_FOREACH(cmp, &mountlist, mnt_list) {
    835 		if (cmp->mnt_lower == mp) {
    836 			mutex_exit(&mountlist_lock);
    837 			return EBUSY;
    838 		}
    839 	}
    840 	mutex_exit(&mountlist_lock);
    841 
    842 	/*
    843 	 * XXX Freeze syncer.  Must do this before locking the
    844 	 * mount point.  See dounmount() for details.
    845 	 */
    846 	mutex_enter(&syncer_mutex);
    847 
    848 	/*
    849 	 * Abort unmount attempt when the filesystem is in use
    850 	 */
    851 	mutex_enter(&mp->mnt_unmounting);
    852 	if (mp->mnt_busynest != 0) {
    853 		mutex_exit(&mp->mnt_unmounting);
    854 		mutex_exit(&syncer_mutex);
    855 		return EBUSY;
    856 	}
    857 
    858 	/*
    859 	 * Abort unmount attempt when the filesystem is not mounted
    860 	 */
    861 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
    862 		mutex_exit(&mp->mnt_unmounting);
    863 		mutex_exit(&syncer_mutex);
    864 		return ENOENT;
    865 	}
    866 
    867 	used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
    868 	used_extattr = mp->mnt_flag & MNT_EXTATTR;
    869 
    870 	/*
    871 	 * XXX Syncer must be frozen when we get here.  This should really
    872 	 * be done on a per-mountpoint basis, but the syncer doesn't work
    873 	 * like that.
    874 	 *
    875 	 * The caller of dounmount() must acquire syncer_mutex because
    876 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    877 	 * order, and we must preserve that order to avoid deadlock.
    878 	 *
    879 	 * So, if the file system did not use the syncer, now is
    880 	 * the time to release the syncer_mutex.
    881 	 */
    882 	if (used_syncer == 0) {
    883 		mutex_exit(&syncer_mutex);
    884 	}
    885 	mp->mnt_iflag |= IMNT_UNMOUNT;
    886 	mutex_enter(&mp->mnt_updating);
    887 	async = mp->mnt_flag & MNT_ASYNC;
    888 	mp->mnt_flag &= ~MNT_ASYNC;
    889 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    890 	if (used_syncer)
    891 		vfs_syncer_remove_from_worklist(mp);
    892 	error = 0;
    893 	if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
    894 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    895 	}
    896 	if (error == 0 || (flags & MNT_FORCE)) {
    897 		error = VFS_UNMOUNT(mp, flags);
    898 	}
    899 	if (error) {
    900 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    901 		mutex_exit(&mp->mnt_unmounting);
    902 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    903 			vfs_syncer_add_to_worklist(mp);
    904 		mp->mnt_flag |= async;
    905 		mutex_exit(&mp->mnt_updating);
    906 		if (used_syncer)
    907 			mutex_exit(&syncer_mutex);
    908 		if (used_extattr) {
    909 			if (start_extattr(mp) != 0)
    910 				mp->mnt_flag &= ~MNT_EXTATTR;
    911 			else
    912 				mp->mnt_flag |= MNT_EXTATTR;
    913 		}
    914 		return (error);
    915 	}
    916 	mutex_exit(&mp->mnt_updating);
    917 
    918 	/*
    919 	 * release mnt_umounting lock here, because other code calls
    920 	 * vfs_busy() while holding the mountlist_lock.
    921 	 *
    922 	 * mark filesystem as gone to prevent further umounts
    923 	 * after mnt_umounting lock is gone, this also prevents
    924 	 * vfs_busy() from succeeding.
    925 	 */
    926 	mp->mnt_iflag |= IMNT_GONE;
    927 	mutex_exit(&mp->mnt_unmounting);
    928 
    929 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    930 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
    931 		coveredvp->v_mountedhere = NULL;
    932 		VOP_UNLOCK(coveredvp);
    933 	}
    934 	mutex_enter(&mountlist_lock);
    935 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
    936 	mutex_exit(&mountlist_lock);
    937 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    938 		panic("unmount: dangling vnode");
    939 	if (used_syncer)
    940 		mutex_exit(&syncer_mutex);
    941 	vfs_hooks_unmount(mp);
    942 
    943 	fstrans_unmount(mp);
    944 	vfs_destroy(mp);	/* reference from mount() */
    945 	if (coveredvp != NULLVP) {
    946 		vrele(coveredvp);
    947 	}
    948 	return (0);
    949 }
    950 
    951 /*
    952  * Unmount all file systems.
    953  * We traverse the list in reverse order under the assumption that doing so
    954  * will avoid needing to worry about dependencies.
    955  */
    956 bool
    957 vfs_unmountall(struct lwp *l)
    958 {
    959 
    960 	printf("unmounting file systems...\n");
    961 	return vfs_unmountall1(l, true, true);
    962 }
    963 
    964 static void
    965 vfs_unmount_print(struct mount *mp, const char *pfx)
    966 {
    967 
    968 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
    969 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
    970 	    mp->mnt_stat.f_fstypename);
    971 }
    972 
    973 bool
    974 vfs_unmount_forceone(struct lwp *l)
    975 {
    976 	struct mount *mp, *nmp;
    977 	int error;
    978 
    979 	nmp = NULL;
    980 
    981 	TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
    982 		if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) {
    983 			nmp = mp;
    984 		}
    985 	}
    986 	if (nmp == NULL) {
    987 		return false;
    988 	}
    989 
    990 #ifdef DEBUG
    991 	printf("forcefully unmounting %s (%s)...\n",
    992 	    nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname);
    993 #endif
    994 	atomic_inc_uint(&nmp->mnt_refcnt);
    995 	if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) {
    996 		vfs_unmount_print(nmp, "forcefully ");
    997 		return true;
    998 	} else {
    999 		vfs_destroy(nmp);
   1000 	}
   1001 
   1002 #ifdef DEBUG
   1003 	printf("forceful unmount of %s failed with error %d\n",
   1004 	    nmp->mnt_stat.f_mntonname, error);
   1005 #endif
   1006 
   1007 	return false;
   1008 }
   1009 
   1010 bool
   1011 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
   1012 {
   1013 	struct mount *mp, *nmp;
   1014 	bool any_error = false, progress = false;
   1015 	int error;
   1016 
   1017 	TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) {
   1018 #ifdef DEBUG
   1019 		printf("unmounting %p %s (%s)...\n",
   1020 		    (void *)mp, mp->mnt_stat.f_mntonname,
   1021 		    mp->mnt_stat.f_mntfromname);
   1022 #endif
   1023 		atomic_inc_uint(&mp->mnt_refcnt);
   1024 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
   1025 			vfs_unmount_print(mp, "");
   1026 			progress = true;
   1027 		} else {
   1028 			vfs_destroy(mp);
   1029 			if (verbose) {
   1030 				printf("unmount of %s failed with error %d\n",
   1031 				    mp->mnt_stat.f_mntonname, error);
   1032 			}
   1033 			any_error = true;
   1034 		}
   1035 	}
   1036 	if (verbose) {
   1037 		printf("unmounting done\n");
   1038 	}
   1039 	if (any_error && verbose) {
   1040 		printf("WARNING: some file systems would not unmount\n");
   1041 	}
   1042 	return progress;
   1043 }
   1044 
   1045 void
   1046 vfs_sync_all(struct lwp *l)
   1047 {
   1048 	printf("syncing disks... ");
   1049 
   1050 	/* remove user processes from run queue */
   1051 	suspendsched();
   1052 	(void)spl0();
   1053 
   1054 	/* avoid coming back this way again if we panic. */
   1055 	doing_shutdown = 1;
   1056 
   1057 	do_sys_sync(l);
   1058 
   1059 	/* Wait for sync to finish. */
   1060 	if (buf_syncwait() != 0) {
   1061 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
   1062 		Debugger();
   1063 #endif
   1064 		printf("giving up\n");
   1065 		return;
   1066 	} else
   1067 		printf("done\n");
   1068 }
   1069 
   1070 /*
   1071  * Sync and unmount file systems before shutting down.
   1072  */
   1073 void
   1074 vfs_shutdown(void)
   1075 {
   1076 	lwp_t *l = curlwp;
   1077 
   1078 	vfs_sync_all(l);
   1079 
   1080 	/*
   1081 	 * If we have paniced - do not make the situation potentially
   1082 	 * worse by unmounting the file systems.
   1083 	 */
   1084 	if (panicstr != NULL) {
   1085 		return;
   1086 	}
   1087 
   1088 	/* Unmount file systems. */
   1089 	vfs_unmountall(l);
   1090 }
   1091 
   1092 /*
   1093  * Print a list of supported file system types (used by vfs_mountroot)
   1094  */
   1095 static void
   1096 vfs_print_fstypes(void)
   1097 {
   1098 	struct vfsops *v;
   1099 	int cnt = 0;
   1100 
   1101 	mutex_enter(&vfs_list_lock);
   1102 	LIST_FOREACH(v, &vfs_list, vfs_list)
   1103 		++cnt;
   1104 	mutex_exit(&vfs_list_lock);
   1105 
   1106 	if (cnt == 0) {
   1107 		printf("WARNING: No file system modules have been loaded.\n");
   1108 		return;
   1109 	}
   1110 
   1111 	printf("Supported file systems:");
   1112 	mutex_enter(&vfs_list_lock);
   1113 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1114 		printf(" %s", v->vfs_name);
   1115 	}
   1116 	mutex_exit(&vfs_list_lock);
   1117 	printf("\n");
   1118 }
   1119 
   1120 /*
   1121  * Mount the root file system.  If the operator didn't specify a
   1122  * file system to use, try all possible file systems until one
   1123  * succeeds.
   1124  */
   1125 int
   1126 vfs_mountroot(void)
   1127 {
   1128 	struct vfsops *v;
   1129 	int error = ENODEV;
   1130 
   1131 	if (root_device == NULL)
   1132 		panic("vfs_mountroot: root device unknown");
   1133 
   1134 	switch (device_class(root_device)) {
   1135 	case DV_IFNET:
   1136 		if (rootdev != NODEV)
   1137 			panic("vfs_mountroot: rootdev set for DV_IFNET "
   1138 			    "(0x%llx -> %llu,%llu)",
   1139 			    (unsigned long long)rootdev,
   1140 			    (unsigned long long)major(rootdev),
   1141 			    (unsigned long long)minor(rootdev));
   1142 		break;
   1143 
   1144 	case DV_DISK:
   1145 		if (rootdev == NODEV)
   1146 			panic("vfs_mountroot: rootdev not set for DV_DISK");
   1147 	        if (bdevvp(rootdev, &rootvp))
   1148 	                panic("vfs_mountroot: can't get vnode for rootdev");
   1149 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
   1150 		if (error) {
   1151 			printf("vfs_mountroot: can't open root device\n");
   1152 			return (error);
   1153 		}
   1154 		break;
   1155 
   1156 	case DV_VIRTUAL:
   1157 		break;
   1158 
   1159 	default:
   1160 		printf("%s: inappropriate for root file system\n",
   1161 		    device_xname(root_device));
   1162 		return (ENODEV);
   1163 	}
   1164 
   1165 	/*
   1166 	 * If user specified a root fs type, use it.  Make sure the
   1167 	 * specified type exists and has a mount_root()
   1168 	 */
   1169 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
   1170 		v = vfs_getopsbyname(rootfstype);
   1171 		error = EFTYPE;
   1172 		if (v != NULL) {
   1173 			if (v->vfs_mountroot != NULL) {
   1174 				error = (v->vfs_mountroot)();
   1175 			}
   1176 			v->vfs_refcount--;
   1177 		}
   1178 		goto done;
   1179 	}
   1180 
   1181 	/*
   1182 	 * Try each file system currently configured into the kernel.
   1183 	 */
   1184 	mutex_enter(&vfs_list_lock);
   1185 	LIST_FOREACH(v, &vfs_list, vfs_list) {
   1186 		if (v->vfs_mountroot == NULL)
   1187 			continue;
   1188 #ifdef DEBUG
   1189 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
   1190 #endif
   1191 		v->vfs_refcount++;
   1192 		mutex_exit(&vfs_list_lock);
   1193 		error = (*v->vfs_mountroot)();
   1194 		mutex_enter(&vfs_list_lock);
   1195 		v->vfs_refcount--;
   1196 		if (!error) {
   1197 			aprint_normal("root file system type: %s\n",
   1198 			    v->vfs_name);
   1199 			break;
   1200 		}
   1201 	}
   1202 	mutex_exit(&vfs_list_lock);
   1203 
   1204 	if (v == NULL) {
   1205 		vfs_print_fstypes();
   1206 		printf("no file system for %s", device_xname(root_device));
   1207 		if (device_class(root_device) == DV_DISK)
   1208 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
   1209 		printf("\n");
   1210 		error = EFTYPE;
   1211 	}
   1212 
   1213 done:
   1214 	if (error && device_class(root_device) == DV_DISK) {
   1215 		VOP_CLOSE(rootvp, FREAD, FSCRED);
   1216 		vrele(rootvp);
   1217 	}
   1218 	if (error == 0) {
   1219 		struct mount *mp;
   1220 		extern struct cwdinfo cwdi0;
   1221 
   1222 		mp = TAILQ_FIRST(&mountlist);
   1223 		mp->mnt_flag |= MNT_ROOTFS;
   1224 		mp->mnt_op->vfs_refcount++;
   1225 		error = fstrans_mount(mp);
   1226 		KASSERT(error == 0);
   1227 
   1228 		/*
   1229 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
   1230 		 * reference it.
   1231 		 */
   1232 		error = VFS_ROOT(mp, &rootvnode);
   1233 		if (error)
   1234 			panic("cannot find root vnode, error=%d", error);
   1235 		cwdi0.cwdi_cdir = rootvnode;
   1236 		vref(cwdi0.cwdi_cdir);
   1237 		VOP_UNLOCK(rootvnode);
   1238 		cwdi0.cwdi_rdir = NULL;
   1239 
   1240 		/*
   1241 		 * Now that root is mounted, we can fixup initproc's CWD
   1242 		 * info.  All other processes are kthreads, which merely
   1243 		 * share proc0's CWD info.
   1244 		 */
   1245 		initproc->p_cwdi->cwdi_cdir = rootvnode;
   1246 		vref(initproc->p_cwdi->cwdi_cdir);
   1247 		initproc->p_cwdi->cwdi_rdir = NULL;
   1248 		/*
   1249 		 * Enable loading of modules from the filesystem
   1250 		 */
   1251 		module_load_vfs_init();
   1252 
   1253 	}
   1254 	return (error);
   1255 }
   1256 
   1257 /*
   1258  * mount_specific_key_create --
   1259  *	Create a key for subsystem mount-specific data.
   1260  */
   1261 int
   1262 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1263 {
   1264 
   1265 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
   1266 }
   1267 
   1268 /*
   1269  * mount_specific_key_delete --
   1270  *	Delete a key for subsystem mount-specific data.
   1271  */
   1272 void
   1273 mount_specific_key_delete(specificdata_key_t key)
   1274 {
   1275 
   1276 	specificdata_key_delete(mount_specificdata_domain, key);
   1277 }
   1278 
   1279 /*
   1280  * mount_initspecific --
   1281  *	Initialize a mount's specificdata container.
   1282  */
   1283 void
   1284 mount_initspecific(struct mount *mp)
   1285 {
   1286 	int error __diagused;
   1287 
   1288 	error = specificdata_init(mount_specificdata_domain,
   1289 				  &mp->mnt_specdataref);
   1290 	KASSERT(error == 0);
   1291 }
   1292 
   1293 /*
   1294  * mount_finispecific --
   1295  *	Finalize a mount's specificdata container.
   1296  */
   1297 void
   1298 mount_finispecific(struct mount *mp)
   1299 {
   1300 
   1301 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
   1302 }
   1303 
   1304 /*
   1305  * mount_getspecific --
   1306  *	Return mount-specific data corresponding to the specified key.
   1307  */
   1308 void *
   1309 mount_getspecific(struct mount *mp, specificdata_key_t key)
   1310 {
   1311 
   1312 	return specificdata_getspecific(mount_specificdata_domain,
   1313 					 &mp->mnt_specdataref, key);
   1314 }
   1315 
   1316 /*
   1317  * mount_setspecific --
   1318  *	Set mount-specific data corresponding to the specified key.
   1319  */
   1320 void
   1321 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
   1322 {
   1323 
   1324 	specificdata_setspecific(mount_specificdata_domain,
   1325 				 &mp->mnt_specdataref, key, data);
   1326 }
   1327 
   1328 /*
   1329  * Check to see if a filesystem is mounted on a block device.
   1330  */
   1331 int
   1332 vfs_mountedon(vnode_t *vp)
   1333 {
   1334 	vnode_t *vq;
   1335 	int error = 0;
   1336 
   1337 	if (vp->v_type != VBLK)
   1338 		return ENOTBLK;
   1339 	if (spec_node_getmountedfs(vp) != NULL)
   1340 		return EBUSY;
   1341 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
   1342 		if (spec_node_getmountedfs(vq) != NULL)
   1343 			error = EBUSY;
   1344 		vrele(vq);
   1345 	}
   1346 
   1347 	return error;
   1348 }
   1349 
   1350 /*
   1351  * Check if a device pointed to by vp is mounted.
   1352  *
   1353  * Returns:
   1354  *   EINVAL	if it's not a disk
   1355  *   EBUSY	if it's a disk and mounted
   1356  *   0		if it's a disk and not mounted
   1357  */
   1358 int
   1359 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
   1360 {
   1361 	vnode_t *bvp;
   1362 	dev_t dev;
   1363 	int d_type, ret;
   1364 	const struct cdevsw *cdev = NULL;
   1365 	const struct bdevsw *bdev = NULL;
   1366 
   1367 	bvp = NULL;
   1368 	d_type = D_OTHER;
   1369 
   1370 	if (iskmemvp(vp))
   1371 		return EINVAL;
   1372 
   1373 	switch (vp->v_type) {
   1374 	case VCHR: {
   1375 		dev = vp->v_rdev;
   1376 		cdev = cdevsw_lookup_acquire(dev);
   1377 		if (cdev != NULL) {
   1378 			dev_t blkdev;
   1379 
   1380 			blkdev = devsw_chr2blk(dev);
   1381 			if (blkdev != NODEV) {
   1382 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
   1383 					d_type = (cdev->d_flag & D_TYPEMASK);
   1384 					/* XXX: what if bvp disappears? */
   1385 					vrele(bvp);
   1386 				}
   1387 			}
   1388 		}
   1389 		break;
   1390 		}
   1391 
   1392 	case VBLK: {
   1393 		dev = vp->v_rdev;
   1394 		bdev = bdevsw_lookup_acquire(dev);
   1395 		if (bdev != NULL)
   1396 			d_type = (bdev->d_flag & D_TYPEMASK);
   1397 		bvp = vp;
   1398 
   1399 		break;
   1400 		}
   1401 
   1402 	default:
   1403 		break;
   1404 	}
   1405 
   1406 	if (d_type != D_DISK) {
   1407 		ret = EINVAL;
   1408 		goto out;
   1409 	}
   1410 
   1411 	if (bvpp != NULL)
   1412 		*bvpp = bvp;
   1413 
   1414 	/*
   1415 	 * XXX: This is bogus. We should be failing the request
   1416 	 * XXX: not only if this specific slice is mounted, but
   1417 	 * XXX: if it's on a disk with any other mounted slice.
   1418 	 */
   1419 	if (vfs_mountedon(bvp) != 0)
   1420 		ret = EBUSY;
   1421 	else
   1422 		ret = 0;
   1423 
   1424  out:
   1425 	if (bdev != NULL)
   1426 		bdevsw_release(bdev);
   1427 	if (cdev != NULL)
   1428 		cdevsw_release(cdev);
   1429 
   1430 	return ret;
   1431 }
   1432 
   1433 /*
   1434  * Make a 'unique' number from a mount type name.
   1435  */
   1436 long
   1437 makefstype(const char *type)
   1438 {
   1439 	long rv;
   1440 
   1441 	for (rv = 0; *type; type++) {
   1442 		rv <<= 2;
   1443 		rv ^= *type;
   1444 	}
   1445 	return rv;
   1446 }
   1447 
   1448 void
   1449 mountlist_append(struct mount *mp)
   1450 {
   1451 	mutex_enter(&mountlist_lock);
   1452 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
   1453 	mutex_exit(&mountlist_lock);
   1454 }
   1455