Home | History | Annotate | Line # | Download | only in nullfs
null_vfsops.c revision 1.68.8.2
      1  1.68.8.2  pooka /*	$NetBSD: null_vfsops.c,v 1.68.8.2 2007/07/31 21:14:17 pooka Exp $	*/
      2  1.68.8.2  pooka 
      3  1.68.8.2  pooka /*
      4  1.68.8.2  pooka  * Copyright (c) 1999 National Aeronautics & Space Administration
      5  1.68.8.2  pooka  * All rights reserved.
      6  1.68.8.2  pooka  *
      7  1.68.8.2  pooka  * This software was written by William Studenmund of the
      8  1.68.8.2  pooka  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9  1.68.8.2  pooka  *
     10  1.68.8.2  pooka  * Redistribution and use in source and binary forms, with or without
     11  1.68.8.2  pooka  * modification, are permitted provided that the following conditions
     12  1.68.8.2  pooka  * are met:
     13  1.68.8.2  pooka  * 1. Redistributions of source code must retain the above copyright
     14  1.68.8.2  pooka  *    notice, this list of conditions and the following disclaimer.
     15  1.68.8.2  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.68.8.2  pooka  *    notice, this list of conditions and the following disclaimer in the
     17  1.68.8.2  pooka  *    documentation and/or other materials provided with the distribution.
     18  1.68.8.2  pooka  * 3. Neither the name of the National Aeronautics & Space Administration
     19  1.68.8.2  pooka  *    nor the names of its contributors may be used to endorse or promote
     20  1.68.8.2  pooka  *    products derived from this software without specific prior written
     21  1.68.8.2  pooka  *    permission.
     22  1.68.8.2  pooka  *
     23  1.68.8.2  pooka  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24  1.68.8.2  pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.68.8.2  pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.68.8.2  pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27  1.68.8.2  pooka  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28  1.68.8.2  pooka  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.68.8.2  pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.68.8.2  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.68.8.2  pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.68.8.2  pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.68.8.2  pooka  * POSSIBILITY OF SUCH DAMAGE.
     34  1.68.8.2  pooka  */
     35  1.68.8.2  pooka /*
     36  1.68.8.2  pooka  * Copyright (c) 1992, 1993, 1995
     37  1.68.8.2  pooka  *	The Regents of the University of California.  All rights reserved.
     38  1.68.8.2  pooka  *
     39  1.68.8.2  pooka  * This code is derived from software donated to Berkeley by
     40  1.68.8.2  pooka  * Jan-Simon Pendry.
     41  1.68.8.2  pooka  *
     42  1.68.8.2  pooka  * Redistribution and use in source and binary forms, with or without
     43  1.68.8.2  pooka  * modification, are permitted provided that the following conditions
     44  1.68.8.2  pooka  * are met:
     45  1.68.8.2  pooka  * 1. Redistributions of source code must retain the above copyright
     46  1.68.8.2  pooka  *    notice, this list of conditions and the following disclaimer.
     47  1.68.8.2  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.68.8.2  pooka  *    notice, this list of conditions and the following disclaimer in the
     49  1.68.8.2  pooka  *    documentation and/or other materials provided with the distribution.
     50  1.68.8.2  pooka  * 3. Neither the name of the University nor the names of its contributors
     51  1.68.8.2  pooka  *    may be used to endorse or promote products derived from this software
     52  1.68.8.2  pooka  *    without specific prior written permission.
     53  1.68.8.2  pooka  *
     54  1.68.8.2  pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  1.68.8.2  pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  1.68.8.2  pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  1.68.8.2  pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  1.68.8.2  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  1.68.8.2  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  1.68.8.2  pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  1.68.8.2  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  1.68.8.2  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  1.68.8.2  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  1.68.8.2  pooka  * SUCH DAMAGE.
     65  1.68.8.2  pooka  *
     66  1.68.8.2  pooka  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
     67  1.68.8.2  pooka  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     68  1.68.8.2  pooka  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     69  1.68.8.2  pooka  */
     70  1.68.8.2  pooka 
     71  1.68.8.2  pooka /*
     72  1.68.8.2  pooka  * Null Layer
     73  1.68.8.2  pooka  * (See null_vnops.c for a description of what this does.)
     74  1.68.8.2  pooka  */
     75  1.68.8.2  pooka 
     76  1.68.8.2  pooka #include <sys/cdefs.h>
     77  1.68.8.2  pooka __KERNEL_RCSID(0, "$NetBSD: null_vfsops.c,v 1.68.8.2 2007/07/31 21:14:17 pooka Exp $");
     78  1.68.8.2  pooka 
     79  1.68.8.2  pooka #include <sys/param.h>
     80  1.68.8.2  pooka #include <sys/systm.h>
     81  1.68.8.2  pooka #include <sys/sysctl.h>
     82  1.68.8.2  pooka #include <sys/time.h>
     83  1.68.8.2  pooka #include <sys/proc.h>
     84  1.68.8.2  pooka #include <sys/vnode.h>
     85  1.68.8.2  pooka #include <sys/mount.h>
     86  1.68.8.2  pooka #include <sys/namei.h>
     87  1.68.8.2  pooka #include <sys/malloc.h>
     88  1.68.8.2  pooka 
     89  1.68.8.2  pooka #include <miscfs/nullfs/null.h>
     90  1.68.8.2  pooka #include <miscfs/genfs/layer_extern.h>
     91  1.68.8.2  pooka 
     92  1.68.8.2  pooka VFS_PROTOS(nullfs);
     93  1.68.8.2  pooka 
     94  1.68.8.2  pooka /*
     95  1.68.8.2  pooka  * Mount null layer
     96  1.68.8.2  pooka  */
     97  1.68.8.2  pooka int
     98  1.68.8.2  pooka nullfs_mount(mp, path, data, data_len, l)
     99  1.68.8.2  pooka 	struct mount *mp;
    100  1.68.8.2  pooka 	const char *path;
    101  1.68.8.2  pooka 	void *data;
    102  1.68.8.2  pooka 	size_t *data_len;
    103  1.68.8.2  pooka 	struct lwp *l;
    104  1.68.8.2  pooka {
    105  1.68.8.2  pooka 	struct nameidata nd;
    106  1.68.8.2  pooka 	struct null_args *args = data;
    107  1.68.8.2  pooka 	struct vnode *lowerrootvp, *vp;
    108  1.68.8.2  pooka 	struct null_mount *nmp;
    109  1.68.8.2  pooka 	struct layer_mount *lmp;
    110  1.68.8.2  pooka 	int error = 0;
    111  1.68.8.2  pooka 
    112  1.68.8.2  pooka #ifdef NULLFS_DIAGNOSTIC
    113  1.68.8.2  pooka 	printf("nullfs_mount(mp = %p)\n", mp);
    114  1.68.8.2  pooka #endif
    115  1.68.8.2  pooka 
    116  1.68.8.2  pooka 	if (*data_len < sizeof *args)
    117  1.68.8.2  pooka 		return EINVAL;
    118  1.68.8.2  pooka 
    119  1.68.8.2  pooka 	if (mp->mnt_flag & MNT_GETARGS) {
    120  1.68.8.2  pooka 		lmp = MOUNTTOLAYERMOUNT(mp);
    121  1.68.8.2  pooka 		if (lmp == NULL)
    122  1.68.8.2  pooka 			return EIO;
    123  1.68.8.2  pooka 		args->la.target = NULL;
    124  1.68.8.2  pooka 		*data_len = sizeof *args;
    125  1.68.8.2  pooka 		return 0;
    126  1.68.8.2  pooka 	}
    127  1.68.8.2  pooka 
    128  1.68.8.2  pooka 	/*
    129  1.68.8.2  pooka 	 * Update is not supported
    130  1.68.8.2  pooka 	 */
    131  1.68.8.2  pooka 	if (mp->mnt_flag & MNT_UPDATE)
    132  1.68.8.2  pooka 		return EOPNOTSUPP;
    133  1.68.8.2  pooka 
    134  1.68.8.2  pooka 	/*
    135  1.68.8.2  pooka 	 * Find lower node
    136  1.68.8.2  pooka 	 */
    137  1.68.8.2  pooka 	NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF,
    138  1.68.8.2  pooka 		UIO_USERSPACE, args->la.target, l);
    139  1.68.8.2  pooka 	if ((error = namei(&nd)) != 0)
    140  1.68.8.2  pooka 		return (error);
    141  1.68.8.2  pooka 
    142  1.68.8.2  pooka 	/*
    143  1.68.8.2  pooka 	 * Sanity check on lower vnode
    144  1.68.8.2  pooka 	 */
    145  1.68.8.2  pooka 	lowerrootvp = nd.ni_vp;
    146  1.68.8.2  pooka 
    147  1.68.8.2  pooka 	/*
    148  1.68.8.2  pooka 	 * First cut at fixing up upper mount point
    149  1.68.8.2  pooka 	 */
    150  1.68.8.2  pooka 	nmp = (struct null_mount *) malloc(sizeof(struct null_mount),
    151  1.68.8.2  pooka 	    M_UFSMNT, M_WAITOK);		/* XXX */
    152  1.68.8.2  pooka 	memset(nmp, 0, sizeof(struct null_mount));
    153  1.68.8.2  pooka 
    154  1.68.8.2  pooka 	mp->mnt_data = nmp;
    155  1.68.8.2  pooka 	nmp->nullm_vfs = lowerrootvp->v_mount;
    156  1.68.8.2  pooka 	if (nmp->nullm_vfs->mnt_flag & MNT_LOCAL)
    157  1.68.8.2  pooka 		mp->mnt_flag |= MNT_LOCAL;
    158  1.68.8.2  pooka 
    159  1.68.8.2  pooka 	/*
    160  1.68.8.2  pooka 	 * Make sure that the mount point is sufficiently initialized
    161  1.68.8.2  pooka 	 * that the node create call will work.
    162  1.68.8.2  pooka 	 */
    163  1.68.8.2  pooka 	vfs_getnewfsid(mp);
    164  1.68.8.2  pooka 
    165  1.68.8.2  pooka 	nmp->nullm_size = sizeof(struct null_node);
    166  1.68.8.2  pooka 	nmp->nullm_tag = VT_NULL;
    167  1.68.8.2  pooka 	nmp->nullm_bypass = layer_bypass;
    168  1.68.8.2  pooka 	nmp->nullm_alloc = layer_node_alloc;	/* the default alloc is fine */
    169  1.68.8.2  pooka 	nmp->nullm_vnodeop_p = null_vnodeop_p;
    170  1.68.8.2  pooka 	simple_lock_init(&nmp->nullm_hashlock);
    171  1.68.8.2  pooka 	nmp->nullm_node_hashtbl = hashinit(desiredvnodes, HASH_LIST, M_CACHE,
    172  1.68.8.2  pooka 	    M_WAITOK, &nmp->nullm_node_hash);
    173  1.68.8.2  pooka 
    174  1.68.8.2  pooka 	/*
    175  1.68.8.2  pooka 	 * Fix up null node for root vnode
    176  1.68.8.2  pooka 	 */
    177  1.68.8.2  pooka 	error = layer_node_create(mp, lowerrootvp, &vp);
    178  1.68.8.2  pooka 	/*
    179  1.68.8.2  pooka 	 * Make sure the fixup worked
    180  1.68.8.2  pooka 	 */
    181  1.68.8.2  pooka 	if (error) {
    182  1.68.8.2  pooka 		vput(lowerrootvp);
    183  1.68.8.2  pooka 		hashdone(nmp->nullm_node_hashtbl, M_CACHE);
    184  1.68.8.2  pooka 		free(nmp, M_UFSMNT);	/* XXX */
    185  1.68.8.2  pooka 		return (error);
    186  1.68.8.2  pooka 	}
    187  1.68.8.2  pooka 	/*
    188  1.68.8.2  pooka 	 * Unlock the node
    189  1.68.8.2  pooka 	 */
    190  1.68.8.2  pooka 	VOP_UNLOCK(vp, 0);
    191  1.68.8.2  pooka 
    192  1.68.8.2  pooka 	/*
    193  1.68.8.2  pooka 	 * Keep a held reference to the root vnode.
    194  1.68.8.2  pooka 	 * It is vrele'd in nullfs_unmount.
    195  1.68.8.2  pooka 	 */
    196  1.68.8.2  pooka 	vp->v_flag |= VROOT;
    197  1.68.8.2  pooka 	nmp->nullm_rootvp = vp;
    198  1.68.8.2  pooka 
    199  1.68.8.2  pooka 	error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
    200  1.68.8.2  pooka 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    201  1.68.8.2  pooka #ifdef NULLFS_DIAGNOSTIC
    202  1.68.8.2  pooka 	printf("nullfs_mount: lower %s, alias at %s\n",
    203  1.68.8.2  pooka 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    204  1.68.8.2  pooka #endif
    205  1.68.8.2  pooka 	return error;
    206  1.68.8.2  pooka }
    207  1.68.8.2  pooka 
    208  1.68.8.2  pooka /*
    209  1.68.8.2  pooka  * Free reference to null layer
    210  1.68.8.2  pooka  */
    211  1.68.8.2  pooka int
    212  1.68.8.2  pooka nullfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    213  1.68.8.2  pooka {
    214  1.68.8.2  pooka 	struct null_mount *nmp = MOUNTTONULLMOUNT(mp);
    215  1.68.8.2  pooka 	struct vnode *null_rootvp = nmp->nullm_rootvp;
    216  1.68.8.2  pooka 	int error;
    217  1.68.8.2  pooka 	int flags = 0;
    218  1.68.8.2  pooka 
    219  1.68.8.2  pooka #ifdef NULLFS_DIAGNOSTIC
    220  1.68.8.2  pooka 	printf("nullfs_unmount(mp = %p)\n", mp);
    221  1.68.8.2  pooka #endif
    222  1.68.8.2  pooka 
    223  1.68.8.2  pooka 	if (mntflags & MNT_FORCE)
    224  1.68.8.2  pooka 		flags |= FORCECLOSE;
    225  1.68.8.2  pooka 
    226  1.68.8.2  pooka 	if (null_rootvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    227  1.68.8.2  pooka 		return (EBUSY);
    228  1.68.8.2  pooka 	if ((error = vflush(mp, null_rootvp, flags)) != 0)
    229  1.68.8.2  pooka 		return (error);
    230  1.68.8.2  pooka 
    231  1.68.8.2  pooka #ifdef NULLFS_DIAGNOSTIC
    232  1.68.8.2  pooka 	vprint("alias root of lower", null_rootvp);
    233  1.68.8.2  pooka #endif
    234  1.68.8.2  pooka 	/*
    235  1.68.8.2  pooka 	 * Release reference on underlying root vnode
    236  1.68.8.2  pooka 	 */
    237  1.68.8.2  pooka 	vrele(null_rootvp);
    238  1.68.8.2  pooka 
    239  1.68.8.2  pooka 	/*
    240  1.68.8.2  pooka 	 * And blow it away for future re-use
    241  1.68.8.2  pooka 	 */
    242  1.68.8.2  pooka 	vgone(null_rootvp);
    243  1.68.8.2  pooka 
    244  1.68.8.2  pooka 	/*
    245  1.68.8.2  pooka 	 * Finally, throw away the null_mount structure
    246  1.68.8.2  pooka 	 */
    247  1.68.8.2  pooka 	hashdone(nmp->nullm_node_hashtbl, M_CACHE);
    248  1.68.8.2  pooka 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    249  1.68.8.2  pooka 	mp->mnt_data = NULL;
    250  1.68.8.2  pooka 	return (0);
    251  1.68.8.2  pooka }
    252  1.68.8.2  pooka 
    253  1.68.8.2  pooka SYSCTL_SETUP(sysctl_vfs_null_setup, "sysctl vfs.null subtree setup")
    254  1.68.8.2  pooka {
    255  1.68.8.2  pooka 
    256  1.68.8.2  pooka 	sysctl_createv(clog, 0, NULL, NULL,
    257  1.68.8.2  pooka 		       CTLFLAG_PERMANENT,
    258  1.68.8.2  pooka 		       CTLTYPE_NODE, "vfs", NULL,
    259  1.68.8.2  pooka 		       NULL, 0, NULL, 0,
    260  1.68.8.2  pooka 		       CTL_VFS, CTL_EOL);
    261  1.68.8.2  pooka 	sysctl_createv(clog, 0, NULL, NULL,
    262  1.68.8.2  pooka 		       CTLFLAG_PERMANENT,
    263  1.68.8.2  pooka 		       CTLTYPE_NODE, "null",
    264  1.68.8.2  pooka 		       SYSCTL_DESCR("Loopback file system"),
    265  1.68.8.2  pooka 		       NULL, 0, NULL, 0,
    266  1.68.8.2  pooka 		       CTL_VFS, 9, CTL_EOL);
    267  1.68.8.2  pooka 	/*
    268  1.68.8.2  pooka 	 * XXX the "9" above could be dynamic, thereby eliminating one
    269  1.68.8.2  pooka 	 * more instance of the "number to vfs" mapping problem, but
    270  1.68.8.2  pooka 	 * "9" is the order as taken from sys/mount.h
    271  1.68.8.2  pooka 	 */
    272  1.68.8.2  pooka }
    273  1.68.8.2  pooka 
    274  1.68.8.2  pooka extern const struct vnodeopv_desc null_vnodeop_opv_desc;
    275  1.68.8.2  pooka 
    276  1.68.8.2  pooka const struct vnodeopv_desc * const nullfs_vnodeopv_descs[] = {
    277  1.68.8.2  pooka 	&null_vnodeop_opv_desc,
    278  1.68.8.2  pooka 	NULL,
    279  1.68.8.2  pooka };
    280  1.68.8.2  pooka 
    281  1.68.8.2  pooka struct vfsops nullfs_vfsops = {
    282  1.68.8.2  pooka 	MOUNT_NULL,
    283  1.68.8.2  pooka 	sizeof (struct null_args),
    284  1.68.8.2  pooka 	nullfs_mount,
    285  1.68.8.2  pooka 	layerfs_start,
    286  1.68.8.2  pooka 	nullfs_unmount,
    287  1.68.8.2  pooka 	layerfs_root,
    288  1.68.8.2  pooka 	layerfs_quotactl,
    289  1.68.8.2  pooka 	layerfs_statvfs,
    290  1.68.8.2  pooka 	layerfs_sync,
    291  1.68.8.2  pooka 	layerfs_vget,
    292  1.68.8.2  pooka 	layerfs_fhtovp,
    293  1.68.8.2  pooka 	layerfs_vptofh,
    294  1.68.8.2  pooka 	layerfs_init,
    295  1.68.8.2  pooka 	NULL,
    296  1.68.8.2  pooka 	layerfs_done,
    297  1.68.8.2  pooka 	NULL,				/* vfs_mountroot */
    298  1.68.8.2  pooka 	layerfs_snapshot,
    299  1.68.8.2  pooka 	vfs_stdextattrctl,
    300  1.68.8.2  pooka 	(void *)eopnotsupp,		/* vfs_suspendctl */
    301  1.68.8.2  pooka 	nullfs_vnodeopv_descs,
    302  1.68.8.2  pooka 	0,
    303  1.68.8.2  pooka 	{ NULL, NULL },
    304  1.68.8.2  pooka };
    305  1.68.8.2  pooka VFS_ATTACH(nullfs_vfsops);
    306