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