Home | History | Annotate | Line # | Download | only in genfs
layer_vfsops.c revision 1.31
      1  1.31       dsl /*	$NetBSD: layer_vfsops.c,v 1.31 2009/03/14 15:36:22 dsl Exp $	*/
      2   1.1  wrstuden 
      3   1.1  wrstuden /*
      4   1.1  wrstuden  * Copyright (c) 1999 National Aeronautics & Space Administration
      5   1.1  wrstuden  * All rights reserved.
      6   1.1  wrstuden  *
      7   1.1  wrstuden  * This software was written by William Studenmund of the
      8   1.3       wiz  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9   1.1  wrstuden  *
     10   1.1  wrstuden  * Redistribution and use in source and binary forms, with or without
     11   1.1  wrstuden  * modification, are permitted provided that the following conditions
     12   1.1  wrstuden  * are met:
     13   1.1  wrstuden  * 1. Redistributions of source code must retain the above copyright
     14   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer.
     15   1.1  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  wrstuden  *    documentation and/or other materials provided with the distribution.
     18   1.2     soren  * 3. Neither the name of the National Aeronautics & Space Administration
     19   1.1  wrstuden  *    nor the names of its contributors may be used to endorse or promote
     20   1.1  wrstuden  *    products derived from this software without specific prior written
     21   1.1  wrstuden  *    permission.
     22   1.1  wrstuden  *
     23   1.1  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24   1.1  wrstuden  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25   1.1  wrstuden  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26   1.1  wrstuden  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27   1.1  wrstuden  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28   1.1  wrstuden  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29   1.1  wrstuden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30   1.1  wrstuden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31   1.1  wrstuden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32   1.1  wrstuden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33   1.1  wrstuden  * POSSIBILITY OF SUCH DAMAGE.
     34   1.1  wrstuden  */
     35   1.1  wrstuden /*
     36   1.1  wrstuden  * Copyright (c) 1992, 1993, 1995
     37   1.1  wrstuden  *	The Regents of the University of California.  All rights reserved.
     38   1.1  wrstuden  *
     39   1.1  wrstuden  * This code is derived from software donated to Berkeley by
     40   1.1  wrstuden  * Jan-Simon Pendry.
     41   1.1  wrstuden  *
     42   1.1  wrstuden  * Redistribution and use in source and binary forms, with or without
     43   1.1  wrstuden  * modification, are permitted provided that the following conditions
     44   1.1  wrstuden  * are met:
     45   1.1  wrstuden  * 1. Redistributions of source code must retain the above copyright
     46   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer.
     47   1.1  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     49   1.1  wrstuden  *    documentation and/or other materials provided with the distribution.
     50  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     51   1.1  wrstuden  *    may be used to endorse or promote products derived from this software
     52   1.1  wrstuden  *    without specific prior written permission.
     53   1.1  wrstuden  *
     54   1.1  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55   1.1  wrstuden  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56   1.1  wrstuden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57   1.1  wrstuden  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58   1.1  wrstuden  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59   1.1  wrstuden  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60   1.1  wrstuden  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61   1.1  wrstuden  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62   1.1  wrstuden  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63   1.1  wrstuden  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64   1.1  wrstuden  * SUCH DAMAGE.
     65   1.1  wrstuden  *
     66   1.1  wrstuden  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
     67   1.1  wrstuden  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     68   1.1  wrstuden  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     69   1.1  wrstuden  */
     70   1.1  wrstuden 
     71   1.1  wrstuden /*
     72   1.1  wrstuden  * generic layer vfs ops.
     73   1.1  wrstuden  */
     74   1.4     lukem 
     75   1.4     lukem #include <sys/cdefs.h>
     76  1.31       dsl __KERNEL_RCSID(0, "$NetBSD: layer_vfsops.c,v 1.31 2009/03/14 15:36:22 dsl Exp $");
     77   1.1  wrstuden 
     78   1.1  wrstuden #include <sys/param.h>
     79  1.11    atatat #include <sys/sysctl.h>
     80   1.1  wrstuden #include <sys/systm.h>
     81   1.1  wrstuden #include <sys/time.h>
     82   1.1  wrstuden #include <sys/proc.h>
     83   1.1  wrstuden #include <sys/vnode.h>
     84   1.1  wrstuden #include <sys/mount.h>
     85   1.1  wrstuden #include <sys/namei.h>
     86   1.1  wrstuden #include <sys/malloc.h>
     87  1.23      elad #include <sys/kauth.h>
     88  1.30        ad #include <sys/module.h>
     89  1.23      elad 
     90   1.1  wrstuden #include <miscfs/genfs/layer.h>
     91   1.1  wrstuden #include <miscfs/genfs/layer_extern.h>
     92   1.1  wrstuden 
     93  1.30        ad MODULE(MODULE_CLASS_MISC, layerfs, NULL);
     94  1.30        ad 
     95  1.30        ad static int
     96  1.30        ad layerfs_modcmd(modcmd_t cmd, void *arg)
     97  1.30        ad {
     98  1.30        ad 
     99  1.30        ad 	switch (cmd) {
    100  1.30        ad 	case MODULE_CMD_INIT:
    101  1.30        ad 		return 0;
    102  1.30        ad 	case MODULE_CMD_FINI:
    103  1.30        ad 		return 0;
    104  1.30        ad 	default:
    105  1.30        ad 		return ENOTTY;
    106  1.30        ad 	}
    107  1.30        ad 
    108  1.30        ad 	return 0;
    109  1.30        ad }
    110  1.30        ad 
    111   1.1  wrstuden /*
    112   1.1  wrstuden  * VFS start.  Nothing needed here - the start routine
    113   1.1  wrstuden  * on the underlying filesystem will have been called
    114   1.1  wrstuden  * when that filesystem was mounted.
    115   1.1  wrstuden  */
    116   1.1  wrstuden int
    117  1.27     pooka layerfs_start(struct mount *mp, int flags)
    118   1.1  wrstuden {
    119   1.1  wrstuden 
    120  1.25  christos #ifdef notyet
    121  1.27     pooka 	return VFS_START(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, flags);
    122  1.25  christos #else
    123  1.25  christos 	return 0;
    124  1.25  christos #endif
    125   1.1  wrstuden }
    126   1.1  wrstuden 
    127   1.1  wrstuden int
    128  1.31       dsl layerfs_root(struct mount *mp, struct vnode **vpp)
    129   1.1  wrstuden {
    130   1.1  wrstuden 	struct vnode *vp;
    131   1.1  wrstuden 
    132   1.1  wrstuden #ifdef LAYERFS_DIAGNOSTIC
    133  1.20       erh 	if (layerfs_debug)
    134  1.20       erh 		printf("layerfs_root(mp = %p, vp = %p->%p)\n", mp,
    135  1.20       erh 		    MOUNTTOLAYERMOUNT(mp)->layerm_rootvp,
    136  1.20       erh 		    LAYERVPTOLOWERVP(MOUNTTOLAYERMOUNT(mp)->layerm_rootvp));
    137   1.1  wrstuden #endif
    138   1.1  wrstuden 
    139   1.1  wrstuden 	/*
    140   1.1  wrstuden 	 * Return locked reference to root.
    141   1.1  wrstuden 	 */
    142   1.1  wrstuden 	vp = MOUNTTOLAYERMOUNT(mp)->layerm_rootvp;
    143   1.1  wrstuden 	if (vp == NULL) {
    144   1.1  wrstuden 		*vpp = NULL;
    145   1.1  wrstuden 		return (EINVAL);
    146   1.1  wrstuden 	}
    147   1.1  wrstuden 	VREF(vp);
    148   1.1  wrstuden 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    149   1.1  wrstuden 	*vpp = vp;
    150   1.1  wrstuden 	return 0;
    151   1.1  wrstuden }
    152   1.1  wrstuden 
    153   1.1  wrstuden int
    154  1.31       dsl layerfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
    155   1.1  wrstuden {
    156   1.1  wrstuden 
    157  1.27     pooka 	return VFS_QUOTACTL(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, cmd, uid, arg);
    158   1.1  wrstuden }
    159   1.1  wrstuden 
    160   1.1  wrstuden int
    161  1.31       dsl layerfs_statvfs(struct mount *mp, struct statvfs *sbp)
    162   1.1  wrstuden {
    163   1.1  wrstuden 	int error;
    164  1.28        ad 	struct statvfs *sbuf;
    165  1.28        ad 
    166  1.28        ad 	sbuf = kmem_alloc(sizeof(*sbuf), KM_SLEEP);
    167  1.28        ad 	if (sbuf == NULL)
    168  1.28        ad 		return ENOMEM;
    169   1.1  wrstuden 
    170   1.1  wrstuden #ifdef LAYERFS_DIAGNOSTIC
    171  1.20       erh 	if (layerfs_debug)
    172  1.20       erh 		printf("layerfs_statvfs(mp = %p, vp = %p->%p)\n", mp,
    173  1.20       erh 		    MOUNTTOLAYERMOUNT(mp)->layerm_rootvp,
    174  1.20       erh 		    LAYERVPTOLOWERVP(MOUNTTOLAYERMOUNT(mp)->layerm_rootvp));
    175   1.1  wrstuden #endif
    176  1.17  christos 
    177  1.17  christos 	(void)memset(sbuf, 0, sizeof(*sbuf));
    178  1.17  christos 
    179  1.27     pooka 	error = VFS_STATVFS(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, sbuf);
    180  1.17  christos  	if (error)
    181  1.17  christos 		goto done;
    182  1.17  christos 
    183  1.17  christos 	/* now copy across the "interesting" information and fake the rest */
    184  1.17  christos 	sbp->f_flag = sbuf->f_flag;
    185  1.17  christos 	sbp->f_bsize = sbuf->f_bsize;
    186  1.17  christos 	sbp->f_frsize = sbuf->f_frsize;
    187  1.17  christos 	sbp->f_iosize = sbuf->f_iosize;
    188  1.17  christos 	sbp->f_blocks = sbuf->f_blocks;
    189  1.17  christos 	sbp->f_bfree = sbuf->f_bfree;
    190  1.17  christos 	sbp->f_bavail = sbuf->f_bavail;
    191  1.17  christos 	sbp->f_bresvd = sbuf->f_bresvd;
    192  1.17  christos 	sbp->f_files = sbuf->f_files;
    193  1.17  christos 	sbp->f_ffree = sbuf->f_ffree;
    194  1.17  christos 	sbp->f_favail = sbuf->f_favail;
    195  1.17  christos 	sbp->f_fresvd = sbuf->f_fresvd;
    196  1.17  christos 	sbp->f_namemax = sbuf->f_namemax;
    197  1.14  christos 	copy_statvfs_info(sbp, mp);
    198  1.17  christos done:
    199  1.28        ad 	kmem_free(sbuf, sizeof(*sbuf));
    200  1.17  christos 	return error;
    201   1.1  wrstuden }
    202   1.1  wrstuden 
    203   1.1  wrstuden int
    204  1.26  christos layerfs_sync(struct mount *mp, int waitfor,
    205  1.27     pooka     kauth_cred_t cred)
    206   1.1  wrstuden {
    207   1.1  wrstuden 
    208   1.1  wrstuden 	/*
    209   1.1  wrstuden 	 * XXX - Assumes no data cached at layer.
    210   1.1  wrstuden 	 */
    211   1.1  wrstuden 	return (0);
    212   1.1  wrstuden }
    213   1.1  wrstuden 
    214   1.1  wrstuden int
    215  1.31       dsl layerfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    216   1.1  wrstuden {
    217   1.1  wrstuden 	int error;
    218   1.1  wrstuden 	struct vnode *vp;
    219   1.1  wrstuden 
    220  1.22  christos 	if ((error = VFS_VGET(MOUNTTOLAYERMOUNT(mp)->layerm_vfs,
    221  1.22  christos 	    ino, &vp))) {
    222   1.1  wrstuden 		*vpp = NULL;
    223   1.1  wrstuden 		return (error);
    224   1.1  wrstuden 	}
    225   1.1  wrstuden 	if ((error = layer_node_create(mp, vp, vpp))) {
    226   1.1  wrstuden 		vput(vp);
    227   1.1  wrstuden 		*vpp = NULL;
    228   1.1  wrstuden 		return (error);
    229   1.1  wrstuden 	}
    230   1.1  wrstuden 
    231   1.1  wrstuden 	return (0);
    232   1.1  wrstuden }
    233   1.1  wrstuden 
    234   1.1  wrstuden int
    235  1.31       dsl layerfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
    236   1.1  wrstuden {
    237   1.1  wrstuden 	int error;
    238   1.1  wrstuden 	struct vnode *vp;
    239   1.1  wrstuden 
    240  1.22  christos 	if ((error = VFS_FHTOVP(MOUNTTOLAYERMOUNT(mp)->layerm_vfs,
    241  1.22  christos 	    fidp, &vp)))
    242   1.1  wrstuden 		return (error);
    243   1.1  wrstuden 
    244   1.1  wrstuden 	if ((error = layer_node_create(mp, vp, vpp))) {
    245   1.1  wrstuden 		vput(vp);
    246   1.1  wrstuden 		*vpp = NULL;
    247   1.1  wrstuden 		return (error);
    248   1.1  wrstuden 	}
    249   1.1  wrstuden 
    250   1.1  wrstuden 	return (0);
    251   1.1  wrstuden }
    252   1.1  wrstuden 
    253   1.1  wrstuden int
    254  1.31       dsl layerfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    255   1.1  wrstuden {
    256   1.1  wrstuden 
    257  1.24    martin 	return (VFS_VPTOFH(LAYERVPTOLOWERVP(vp), fhp, fh_size));
    258   1.1  wrstuden }
    259   1.1  wrstuden 
    260  1.19  wrstuden /*
    261  1.19  wrstuden  * layerfs_snapshot - handle a snapshot through a layered file system
    262  1.19  wrstuden  *
    263  1.19  wrstuden  * At present, we do NOT support snapshotting through a layered file
    264  1.19  wrstuden  * system as the ffs implementation changes v_vnlock of the snapshot
    265  1.19  wrstuden  * vnodes to point to one common lock. As there is no way for us to
    266  1.19  wrstuden  * absolutely pass this change up the stack, a layered file system
    267  1.19  wrstuden  * would end up referencing the wrong lock.
    268  1.19  wrstuden  *
    269  1.19  wrstuden  * This routine serves as a central resource for this behavior; all
    270  1.19  wrstuden  * layered file systems don't need to worry about the above. Also, if
    271  1.19  wrstuden  * things get fixed, all layers get the benefit.
    272  1.19  wrstuden  */
    273  1.19  wrstuden int
    274  1.26  christos layerfs_snapshot(struct mount *mp, struct vnode *vp,
    275  1.26  christos     struct timespec *ts)
    276  1.19  wrstuden {
    277  1.19  wrstuden 	return (EOPNOTSUPP);
    278  1.19  wrstuden }
    279  1.19  wrstuden 
    280  1.12    atatat SYSCTL_SETUP(sysctl_vfs_layerfs_setup, "sysctl vfs.layerfs subtree setup")
    281   1.1  wrstuden {
    282  1.20       erh 	const struct sysctlnode *layerfs_node = NULL;
    283  1.11    atatat 
    284  1.13    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    285  1.13    atatat 		       CTLFLAG_PERMANENT,
    286  1.11    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    287  1.11    atatat 		       NULL, 0, NULL, 0,
    288  1.11    atatat 		       CTL_VFS, CTL_EOL);
    289  1.20       erh 	sysctl_createv(clog, 0, NULL, &layerfs_node,
    290  1.13    atatat 		       CTLFLAG_PERMANENT,
    291  1.18    atatat 		       CTLTYPE_NODE, "layerfs",
    292  1.18    atatat 		       SYSCTL_DESCR("Generic layered file system"),
    293  1.11    atatat 		       NULL, 0, NULL, 0,
    294  1.11    atatat 		       CTL_VFS, CTL_CREATE);
    295  1.20       erh 
    296  1.20       erh #ifdef LAYERFS_DIAGNOSTIC
    297  1.20       erh 	sysctl_createv(clog, 0, &layerfs_node, NULL,
    298  1.20       erh 	               CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    299  1.20       erh 	               CTLTYPE_INT,
    300  1.20       erh 	               "debug",
    301  1.20       erh 	               SYSCTL_DESCR("Verbose debugging messages"),
    302  1.20       erh 	               NULL, 0, &layerfs_debug, 0,
    303  1.20       erh 	               CTL_CREATE, CTL_EOL);
    304  1.20       erh #endif
    305  1.20       erh 
    306  1.11    atatat 	/*
    307  1.11    atatat 	 * other subtrees should really be aliases to this, but since
    308  1.11    atatat 	 * they can't tell if layerfs has been instantiated yet, they
    309  1.11    atatat 	 * can't do that...not easily.  not yet.  :-)
    310  1.11    atatat 	 */
    311   1.1  wrstuden }
    312  1.29  dholland 
    313  1.29  dholland int
    314  1.29  dholland layerfs_renamelock_enter(struct mount *mp)
    315  1.29  dholland {
    316  1.29  dholland 	return VFS_RENAMELOCK_ENTER(MOUNTTOLAYERMOUNT(mp)->layerm_vfs);
    317  1.29  dholland }
    318  1.29  dholland 
    319  1.29  dholland void
    320  1.29  dholland layerfs_renamelock_exit(struct mount *mp)
    321  1.29  dholland {
    322  1.29  dholland 	VFS_RENAMELOCK_EXIT(MOUNTTOLAYERMOUNT(mp)->layerm_vfs);
    323  1.29  dholland }
    324