Home | History | Annotate | Line # | Download | only in genfs
layer_vfsops.c revision 1.54
      1  1.54        ad /*	$NetBSD: layer_vfsops.c,v 1.54 2020/02/23 15:46:41 ad 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.33     rmind 
     36   1.1  wrstuden /*
     37   1.1  wrstuden  * Copyright (c) 1992, 1993, 1995
     38   1.1  wrstuden  *	The Regents of the University of California.  All rights reserved.
     39   1.1  wrstuden  *
     40   1.1  wrstuden  * This code is derived from software donated to Berkeley by
     41   1.1  wrstuden  * Jan-Simon Pendry.
     42   1.1  wrstuden  *
     43   1.1  wrstuden  * Redistribution and use in source and binary forms, with or without
     44   1.1  wrstuden  * modification, are permitted provided that the following conditions
     45   1.1  wrstuden  * are met:
     46   1.1  wrstuden  * 1. Redistributions of source code must retain the above copyright
     47   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer.
     48   1.1  wrstuden  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1  wrstuden  *    notice, this list of conditions and the following disclaimer in the
     50   1.1  wrstuden  *    documentation and/or other materials provided with the distribution.
     51  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     52   1.1  wrstuden  *    may be used to endorse or promote products derived from this software
     53   1.1  wrstuden  *    without specific prior written permission.
     54   1.1  wrstuden  *
     55   1.1  wrstuden  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1  wrstuden  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1  wrstuden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1  wrstuden  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1  wrstuden  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1  wrstuden  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1  wrstuden  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1  wrstuden  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1  wrstuden  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1  wrstuden  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1  wrstuden  * SUCH DAMAGE.
     66   1.1  wrstuden  *
     67   1.1  wrstuden  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
     68   1.1  wrstuden  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     69   1.1  wrstuden  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     70   1.1  wrstuden  */
     71   1.1  wrstuden 
     72   1.1  wrstuden /*
     73  1.33     rmind  * Generic layer VFS operations.
     74   1.1  wrstuden  */
     75   1.4     lukem 
     76   1.4     lukem #include <sys/cdefs.h>
     77  1.54        ad __KERNEL_RCSID(0, "$NetBSD: layer_vfsops.c,v 1.54 2020/02/23 15:46:41 ad Exp $");
     78   1.1  wrstuden 
     79   1.1  wrstuden #include <sys/param.h>
     80  1.11    atatat #include <sys/sysctl.h>
     81   1.1  wrstuden #include <sys/systm.h>
     82   1.1  wrstuden #include <sys/vnode.h>
     83   1.1  wrstuden #include <sys/mount.h>
     84   1.1  wrstuden #include <sys/namei.h>
     85  1.23      elad #include <sys/kauth.h>
     86  1.30        ad #include <sys/module.h>
     87  1.23      elad 
     88  1.44   hannken #include <miscfs/specfs/specdev.h>
     89  1.47   hannken #include <miscfs/genfs/genfs.h>
     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.40  pgoyette SYSCTL_SETUP_PROTO(sysctl_vfs_layerfs_setup);
     94  1.40  pgoyette 
     95  1.30        ad MODULE(MODULE_CLASS_MISC, layerfs, NULL);
     96  1.30        ad 
     97  1.30        ad static int
     98  1.30        ad layerfs_modcmd(modcmd_t cmd, void *arg)
     99  1.30        ad {
    100  1.30        ad 
    101  1.30        ad 	switch (cmd) {
    102  1.30        ad 	case MODULE_CMD_INIT:
    103  1.30        ad 		return 0;
    104  1.30        ad 	case MODULE_CMD_FINI:
    105  1.30        ad 		return 0;
    106  1.30        ad 	default:
    107  1.30        ad 		return ENOTTY;
    108  1.30        ad 	}
    109  1.30        ad 	return 0;
    110  1.30        ad }
    111  1.30        ad 
    112   1.1  wrstuden /*
    113  1.33     rmind  * VFS start.  Nothing needed here - the start routine on the underlying
    114  1.33     rmind  * filesystem will have been called 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.49   hannken 	return VFS_START(mp->mnt_lower, 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.53        ad layerfs_root(struct mount *mp, int lktype, struct vnode **vpp)
    129   1.1  wrstuden {
    130   1.1  wrstuden 	struct vnode *vp;
    131   1.1  wrstuden 
    132   1.1  wrstuden 	vp = MOUNTTOLAYERMOUNT(mp)->layerm_rootvp;
    133   1.1  wrstuden 	if (vp == NULL) {
    134   1.1  wrstuden 		*vpp = NULL;
    135  1.33     rmind 		return EINVAL;
    136   1.1  wrstuden 	}
    137  1.33     rmind 	/*
    138  1.33     rmind 	 * Return root vnode with locked and with a reference held.
    139  1.33     rmind 	 */
    140  1.32     pooka 	vref(vp);
    141  1.53        ad 	vn_lock(vp, lktype | LK_RETRY);
    142   1.1  wrstuden 	*vpp = vp;
    143   1.1  wrstuden 	return 0;
    144   1.1  wrstuden }
    145   1.1  wrstuden 
    146   1.1  wrstuden int
    147  1.39  dholland layerfs_quotactl(struct mount *mp, struct quotactl_args *args)
    148   1.1  wrstuden {
    149   1.1  wrstuden 
    150  1.49   hannken 	return VFS_QUOTACTL(mp->mnt_lower, args);
    151   1.1  wrstuden }
    152   1.1  wrstuden 
    153   1.1  wrstuden int
    154  1.31       dsl layerfs_statvfs(struct mount *mp, struct statvfs *sbp)
    155   1.1  wrstuden {
    156  1.33     rmind 	struct statvfs *sbuf;
    157   1.1  wrstuden 	int error;
    158  1.28        ad 
    159  1.33     rmind 	sbuf = kmem_zalloc(sizeof(*sbuf), KM_SLEEP);
    160  1.49   hannken 	error = VFS_STATVFS(mp->mnt_lower, sbuf);
    161  1.33     rmind 	if (error) {
    162  1.17  christos 		goto done;
    163  1.33     rmind 	}
    164  1.33     rmind 	/* Copy across the relevant data and fake the rest. */
    165  1.17  christos 	sbp->f_flag = sbuf->f_flag;
    166  1.17  christos 	sbp->f_bsize = sbuf->f_bsize;
    167  1.17  christos 	sbp->f_frsize = sbuf->f_frsize;
    168  1.17  christos 	sbp->f_iosize = sbuf->f_iosize;
    169  1.17  christos 	sbp->f_blocks = sbuf->f_blocks;
    170  1.17  christos 	sbp->f_bfree = sbuf->f_bfree;
    171  1.17  christos 	sbp->f_bavail = sbuf->f_bavail;
    172  1.17  christos 	sbp->f_bresvd = sbuf->f_bresvd;
    173  1.17  christos 	sbp->f_files = sbuf->f_files;
    174  1.17  christos 	sbp->f_ffree = sbuf->f_ffree;
    175  1.17  christos 	sbp->f_favail = sbuf->f_favail;
    176  1.17  christos 	sbp->f_fresvd = sbuf->f_fresvd;
    177  1.17  christos 	sbp->f_namemax = sbuf->f_namemax;
    178  1.14  christos 	copy_statvfs_info(sbp, mp);
    179  1.17  christos done:
    180  1.28        ad 	kmem_free(sbuf, sizeof(*sbuf));
    181  1.17  christos 	return error;
    182   1.1  wrstuden }
    183   1.1  wrstuden 
    184   1.1  wrstuden int
    185  1.26  christos layerfs_sync(struct mount *mp, int waitfor,
    186  1.27     pooka     kauth_cred_t cred)
    187   1.1  wrstuden {
    188   1.1  wrstuden 
    189   1.1  wrstuden 	/*
    190   1.1  wrstuden 	 * XXX - Assumes no data cached at layer.
    191   1.1  wrstuden 	 */
    192  1.33     rmind 	return 0;
    193   1.1  wrstuden }
    194   1.1  wrstuden 
    195   1.1  wrstuden int
    196  1.44   hannken layerfs_loadvnode(struct mount *mp, struct vnode *vp,
    197  1.44   hannken     const void *key, size_t key_len, const void **new_key)
    198  1.44   hannken {
    199  1.44   hannken 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    200  1.44   hannken 	struct vnode *lowervp;
    201  1.44   hannken 	struct layer_node *xp;
    202  1.44   hannken 
    203  1.44   hannken 	KASSERT(key_len == sizeof(struct vnode *));
    204  1.44   hannken 	memcpy(&lowervp, key, key_len);
    205  1.44   hannken 
    206  1.44   hannken 	xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
    207  1.44   hannken 
    208  1.54        ad 	/* Share the interlock and vmobjlock with the lower node. */
    209  1.54        ad 	vshareilock(vp, lowervp);
    210  1.54        ad 	rw_obj_hold(lowervp->v_uobj.vmobjlock);
    211  1.54        ad 	uvm_obj_setlock(&vp->v_uobj, lowervp->v_uobj.vmobjlock);
    212  1.44   hannken 
    213  1.44   hannken 	vp->v_tag = lmp->layerm_tag;
    214  1.44   hannken 	vp->v_type = lowervp->v_type;
    215  1.44   hannken 	vp->v_op = lmp->layerm_vnodeop_p;
    216  1.44   hannken 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    217  1.44   hannken 		spec_node_init(vp, lowervp->v_rdev);
    218  1.44   hannken 	vp->v_data = xp;
    219  1.44   hannken 	xp->layer_vnode = vp;
    220  1.44   hannken 	xp->layer_lowervp = lowervp;
    221  1.44   hannken 	xp->layer_flags = 0;
    222  1.44   hannken 	uvm_vnp_setsize(vp, 0);
    223  1.44   hannken 
    224  1.44   hannken 	/*  Add a reference to the lower node. */
    225  1.44   hannken 	vref(lowervp);
    226  1.44   hannken 	*new_key = &xp->layer_lowervp;
    227  1.44   hannken 	return 0;
    228  1.44   hannken }
    229  1.44   hannken 
    230  1.44   hannken int
    231  1.53        ad layerfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
    232   1.1  wrstuden {
    233  1.33     rmind 	struct vnode *vp;
    234   1.1  wrstuden 	int error;
    235   1.1  wrstuden 
    236  1.53        ad 	error = VFS_VGET(mp->mnt_lower, ino, lktype, &vp);
    237  1.33     rmind 	if (error) {
    238   1.1  wrstuden 		*vpp = NULL;
    239  1.33     rmind 		return error;
    240   1.1  wrstuden 	}
    241  1.42   hannken 	VOP_UNLOCK(vp);
    242  1.33     rmind 	error = layer_node_create(mp, vp, vpp);
    243  1.33     rmind 	if (error) {
    244  1.42   hannken 		vrele(vp);
    245  1.42   hannken 		*vpp = NULL;
    246  1.42   hannken 		return error;
    247  1.42   hannken 	}
    248  1.53        ad 	error = vn_lock(*vpp, lktype);
    249  1.42   hannken 	if (error) {
    250  1.42   hannken 		vrele(*vpp);
    251   1.1  wrstuden 		*vpp = NULL;
    252  1.33     rmind 		return error;
    253   1.1  wrstuden 	}
    254  1.33     rmind 	return 0;
    255   1.1  wrstuden }
    256   1.1  wrstuden 
    257   1.1  wrstuden int
    258  1.53        ad layerfs_fhtovp(struct mount *mp, struct fid *fidp, int lktype,
    259  1.53        ad     struct vnode **vpp)
    260   1.1  wrstuden {
    261  1.33     rmind 	struct vnode *vp;
    262   1.1  wrstuden 	int error;
    263   1.1  wrstuden 
    264  1.53        ad 	error = VFS_FHTOVP(mp->mnt_lower, fidp, lktype, &vp);
    265  1.33     rmind 	if (error) {
    266  1.42   hannken 		*vpp = NULL;
    267  1.33     rmind 		return error;
    268  1.33     rmind 	}
    269  1.42   hannken 	VOP_UNLOCK(vp);
    270  1.33     rmind 	error = layer_node_create(mp, vp, vpp);
    271  1.33     rmind 	if (error) {
    272   1.1  wrstuden 		vput(vp);
    273   1.1  wrstuden 		*vpp = NULL;
    274   1.1  wrstuden 		return (error);
    275   1.1  wrstuden 	}
    276  1.53        ad 	error = vn_lock(*vpp, lktype);
    277  1.42   hannken 	if (error) {
    278  1.42   hannken 		vrele(*vpp);
    279  1.42   hannken 		*vpp = NULL;
    280  1.42   hannken 		return error;
    281  1.42   hannken 	}
    282  1.33     rmind 	return 0;
    283   1.1  wrstuden }
    284   1.1  wrstuden 
    285   1.1  wrstuden int
    286  1.31       dsl layerfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    287   1.1  wrstuden {
    288   1.1  wrstuden 
    289  1.33     rmind 	return VFS_VPTOFH(LAYERVPTOLOWERVP(vp), fhp, fh_size);
    290   1.1  wrstuden }
    291   1.1  wrstuden 
    292  1.19  wrstuden /*
    293  1.19  wrstuden  * layerfs_snapshot - handle a snapshot through a layered file system
    294  1.19  wrstuden  *
    295  1.19  wrstuden  * At present, we do NOT support snapshotting through a layered file
    296  1.19  wrstuden  * system as the ffs implementation changes v_vnlock of the snapshot
    297  1.19  wrstuden  * vnodes to point to one common lock. As there is no way for us to
    298  1.19  wrstuden  * absolutely pass this change up the stack, a layered file system
    299  1.19  wrstuden  * would end up referencing the wrong lock.
    300  1.19  wrstuden  *
    301  1.19  wrstuden  * This routine serves as a central resource for this behavior; all
    302  1.19  wrstuden  * layered file systems don't need to worry about the above. Also, if
    303  1.19  wrstuden  * things get fixed, all layers get the benefit.
    304  1.19  wrstuden  */
    305  1.19  wrstuden int
    306  1.26  christos layerfs_snapshot(struct mount *mp, struct vnode *vp,
    307  1.26  christos     struct timespec *ts)
    308  1.19  wrstuden {
    309  1.33     rmind 
    310  1.33     rmind 	return EOPNOTSUPP;
    311  1.19  wrstuden }
    312  1.19  wrstuden 
    313  1.47   hannken /*
    314  1.47   hannken  * layerfs_suspendctl - suspend a layered file system
    315  1.47   hannken  *
    316  1.47   hannken  * Here we should suspend the lower file system(s) too.  At present
    317  1.47   hannken  * this will deadlock as we don't know which to suspend first.
    318  1.47   hannken  *
    319  1.47   hannken  * This routine serves as a central resource for this behavior; all
    320  1.47   hannken  * layered file systems don't need to worry about the above. Also, if
    321  1.47   hannken  * things get fixed, all layers get the benefit.
    322  1.47   hannken  */
    323  1.47   hannken int
    324  1.47   hannken layerfs_suspendctl(struct mount *mp, int cmd)
    325  1.47   hannken {
    326  1.47   hannken 
    327  1.47   hannken 	return genfs_suspendctl(mp, cmd);
    328  1.47   hannken }
    329  1.47   hannken 
    330  1.12    atatat SYSCTL_SETUP(sysctl_vfs_layerfs_setup, "sysctl vfs.layerfs subtree setup")
    331   1.1  wrstuden {
    332  1.20       erh 	const struct sysctlnode *layerfs_node = NULL;
    333  1.11    atatat 
    334  1.20       erh 	sysctl_createv(clog, 0, NULL, &layerfs_node,
    335  1.41  pgoyette #ifdef _MODULE
    336  1.41  pgoyette 		       0,
    337  1.41  pgoyette #else
    338  1.13    atatat 		       CTLFLAG_PERMANENT,
    339  1.41  pgoyette #endif
    340  1.18    atatat 		       CTLTYPE_NODE, "layerfs",
    341  1.18    atatat 		       SYSCTL_DESCR("Generic layered file system"),
    342  1.11    atatat 		       NULL, 0, NULL, 0,
    343  1.43     pooka 		       CTL_VFS, CTL_CREATE, CTL_EOL);
    344  1.20       erh 
    345  1.20       erh #ifdef LAYERFS_DIAGNOSTIC
    346  1.20       erh 	sysctl_createv(clog, 0, &layerfs_node, NULL,
    347  1.41  pgoyette #ifndef _MODULE
    348  1.41  pgoyette 	               CTLFLAG_PERMANENT |
    349  1.41  pgoyette #endif
    350  1.41  pgoyette 		       CTLFLAG_READWRITE,
    351  1.20       erh 	               CTLTYPE_INT,
    352  1.20       erh 	               "debug",
    353  1.20       erh 	               SYSCTL_DESCR("Verbose debugging messages"),
    354  1.20       erh 	               NULL, 0, &layerfs_debug, 0,
    355  1.20       erh 	               CTL_CREATE, CTL_EOL);
    356  1.20       erh #endif
    357  1.20       erh 
    358  1.11    atatat 	/*
    359  1.11    atatat 	 * other subtrees should really be aliases to this, but since
    360  1.11    atatat 	 * they can't tell if layerfs has been instantiated yet, they
    361  1.11    atatat 	 * can't do that...not easily.  not yet.  :-)
    362  1.11    atatat 	 */
    363   1.1  wrstuden }
    364  1.29  dholland 
    365  1.29  dholland int
    366  1.29  dholland layerfs_renamelock_enter(struct mount *mp)
    367  1.29  dholland {
    368  1.33     rmind 
    369  1.49   hannken 	return VFS_RENAMELOCK_ENTER(mp->mnt_lower);
    370  1.29  dholland }
    371  1.29  dholland 
    372  1.29  dholland void
    373  1.29  dholland layerfs_renamelock_exit(struct mount *mp)
    374  1.29  dholland {
    375  1.33     rmind 
    376  1.49   hannken 	VFS_RENAMELOCK_EXIT(mp->mnt_lower);
    377  1.29  dholland }
    378