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