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