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