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