Home | History | Annotate | Line # | Download | only in overlay
overlay_vfsops.c revision 1.73
      1  1.73   hannken /*	$NetBSD: overlay_vfsops.c,v 1.73 2022/11/04 11:20:39 hannken Exp $	*/
      2   1.1  wrstuden 
      3   1.1  wrstuden /*
      4   1.1  wrstuden  * Copyright (c) 1999, 2000 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.7       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.18       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.7       wiz  * Overlay Layer
     73   1.7       wiz  * (See overlay_vnops.c for a description of what this does.)
     74   1.1  wrstuden  */
     75   1.9     lukem 
     76   1.9     lukem #include <sys/cdefs.h>
     77  1.73   hannken __KERNEL_RCSID(0, "$NetBSD: overlay_vfsops.c,v 1.73 2022/11/04 11:20:39 hannken Exp $");
     78   1.1  wrstuden 
     79   1.1  wrstuden #include <sys/param.h>
     80   1.1  wrstuden #include <sys/systm.h>
     81  1.19    atatat #include <sys/sysctl.h>
     82   1.1  wrstuden #include <sys/time.h>
     83   1.1  wrstuden #include <sys/proc.h>
     84   1.1  wrstuden #include <sys/vnode.h>
     85   1.1  wrstuden #include <sys/mount.h>
     86   1.1  wrstuden #include <sys/namei.h>
     87  1.51    rumble #include <sys/module.h>
     88   1.1  wrstuden #include <miscfs/overlay/overlay.h>
     89   1.1  wrstuden #include <miscfs/genfs/layer_extern.h>
     90   1.1  wrstuden 
     91  1.54        ad MODULE(MODULE_CLASS_VFS, overlay, "layerfs");
     92  1.51    rumble 
     93  1.43     pooka VFS_PROTOS(ov);
     94   1.1  wrstuden 
     95   1.1  wrstuden #define	NOVERLAYNODECACHE	16
     96   1.1  wrstuden 
     97   1.1  wrstuden /*
     98   1.1  wrstuden  * Mount overlay layer
     99   1.1  wrstuden  */
    100   1.1  wrstuden int
    101  1.45     pooka ov_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    102   1.1  wrstuden {
    103  1.45     pooka 	struct lwp *l = curlwp;
    104   1.1  wrstuden 	int error = 0;
    105  1.40       dsl 	struct overlay_args *args = data;
    106   1.1  wrstuden 	struct vnode *lowerrootvp, *vp;
    107   1.1  wrstuden 	struct overlay_mount *nmp;
    108   1.1  wrstuden 	struct layer_mount *lmp;
    109   1.1  wrstuden 
    110   1.1  wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
    111   1.1  wrstuden 	printf("ov_mount(mp = %p)\n", mp);
    112   1.1  wrstuden #endif
    113   1.1  wrstuden 
    114  1.61      maxv 	if (args == NULL)
    115  1.61      maxv 		return EINVAL;
    116  1.40       dsl 	if (*data_len < sizeof *args)
    117  1.40       dsl 		return EINVAL;
    118  1.40       dsl 
    119  1.12  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    120  1.12  christos 		lmp = MOUNTTOLAYERMOUNT(mp);
    121  1.12  christos 		if (lmp == NULL)
    122  1.12  christos 			return EIO;
    123  1.40       dsl 		args->la.target = NULL;
    124  1.40       dsl 		*data_len = sizeof *args;
    125  1.40       dsl 		return 0;
    126  1.12  christos 	}
    127   1.1  wrstuden 
    128   1.1  wrstuden 	/*
    129  1.32      jmmv 	 * Update is not supported
    130   1.1  wrstuden 	 */
    131  1.32      jmmv 	if (mp->mnt_flag & MNT_UPDATE)
    132  1.32      jmmv 		return EOPNOTSUPP;
    133   1.1  wrstuden 
    134   1.1  wrstuden 	/*
    135   1.1  wrstuden 	 * Find lower node
    136   1.1  wrstuden 	 */
    137   1.1  wrstuden 	lowerrootvp = mp->mnt_vnodecovered;
    138  1.56   hannken 	vref(lowerrootvp);
    139  1.72   hannken 	if ((error = vn_lock(lowerrootvp, LK_EXCLUSIVE))) {
    140  1.56   hannken 		vrele(lowerrootvp);
    141   1.1  wrstuden 		return (error);
    142  1.56   hannken 	}
    143   1.1  wrstuden 
    144   1.1  wrstuden 	/*
    145   1.1  wrstuden 	 * First cut at fixing up upper mount point
    146   1.1  wrstuden 	 */
    147  1.57     rmind 	nmp = kmem_zalloc(sizeof(struct overlay_mount), KM_SLEEP);
    148   1.1  wrstuden 
    149  1.11     soren 	mp->mnt_data = nmp;
    150   1.1  wrstuden 
    151   1.1  wrstuden 	/*
    152   1.1  wrstuden 	 * Make sure that the mount point is sufficiently initialized
    153   1.1  wrstuden 	 * that the node create call will work.
    154   1.1  wrstuden 	 */
    155   1.4     assar 	vfs_getnewfsid(mp);
    156  1.73   hannken 	error = vfs_set_lowermount(mp, lowerrootvp->v_mount);
    157  1.73   hannken 	if (error) {
    158  1.73   hannken 		vput(lowerrootvp);
    159  1.73   hannken 		kmem_free(nmp, sizeof(struct overlay_mount));
    160  1.73   hannken 		return error;
    161  1.73   hannken 	}
    162   1.1  wrstuden 
    163   1.1  wrstuden 	nmp->ovm_size = sizeof (struct overlay_node);
    164   1.1  wrstuden 	nmp->ovm_tag = VT_OVERLAY;
    165   1.1  wrstuden 	nmp->ovm_bypass = layer_bypass;
    166   1.1  wrstuden 	nmp->ovm_vnodeop_p = overlay_vnodeop_p;
    167   1.1  wrstuden 
    168   1.1  wrstuden 	/*
    169   1.1  wrstuden 	 * Fix up overlay node for root vnode
    170   1.1  wrstuden 	 */
    171  1.58   hannken 	VOP_UNLOCK(lowerrootvp);
    172   1.1  wrstuden 	error = layer_node_create(mp, lowerrootvp, &vp);
    173   1.1  wrstuden 	/*
    174   1.1  wrstuden 	 * Make sure the fixup worked
    175   1.1  wrstuden 	 */
    176   1.1  wrstuden 	if (error) {
    177  1.58   hannken 		vrele(lowerrootvp);
    178  1.57     rmind 		kmem_free(nmp, sizeof(struct overlay_mount));
    179  1.57     rmind 		return error;
    180   1.1  wrstuden 	}
    181   1.1  wrstuden 
    182   1.1  wrstuden 	/*
    183   1.1  wrstuden 	 * Keep a held reference to the root vnode.
    184   1.1  wrstuden 	 * It is vrele'd in ov_unmount.
    185   1.1  wrstuden 	 */
    186  1.58   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    187  1.44        ad 	vp->v_vflag |= VV_ROOT;
    188   1.1  wrstuden 	nmp->ovm_rootvp = vp;
    189  1.58   hannken 	VOP_UNLOCK(vp);
    190   1.1  wrstuden 
    191  1.40       dsl 	error = set_statvfs_info(path, UIO_USERSPACE, args->la.target,
    192  1.41     pooka 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    193  1.66   hannken 	if (error)
    194  1.66   hannken 		return error;
    195  1.67   hannken 
    196  1.67   hannken 	if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
    197  1.67   hannken 		mp->mnt_flag |= MNT_LOCAL;
    198   1.1  wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
    199   1.1  wrstuden 	printf("ov_mount: lower %s, alias at %s\n",
    200   1.1  wrstuden 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    201   1.1  wrstuden #endif
    202  1.66   hannken 	return 0;
    203   1.1  wrstuden }
    204   1.1  wrstuden 
    205   1.1  wrstuden /*
    206   1.1  wrstuden  * Free reference to overlay layer
    207   1.1  wrstuden  */
    208   1.1  wrstuden int
    209  1.45     pooka ov_unmount(struct mount *mp, int mntflags)
    210   1.1  wrstuden {
    211   1.1  wrstuden 	struct vnode *overlay_rootvp = MOUNTTOOVERLAYMOUNT(mp)->ovm_rootvp;
    212  1.50        ad 	struct overlay_mount *omp;
    213   1.1  wrstuden 	int error;
    214   1.1  wrstuden 	int flags = 0;
    215   1.1  wrstuden 
    216   1.1  wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
    217   1.1  wrstuden 	printf("ov_unmount(mp = %p)\n", mp);
    218   1.1  wrstuden #endif
    219   1.1  wrstuden 
    220   1.1  wrstuden 	if (mntflags & MNT_FORCE)
    221   1.1  wrstuden 		flags |= FORCECLOSE;
    222   1.1  wrstuden 
    223  1.71        ad 	if (vrefcnt(overlay_rootvp) > 1 && (mntflags & MNT_FORCE) == 0)
    224   1.1  wrstuden 		return (EBUSY);
    225   1.1  wrstuden 	if ((error = vflush(mp, overlay_rootvp, flags)) != 0)
    226   1.1  wrstuden 		return (error);
    227   1.1  wrstuden 
    228   1.1  wrstuden #ifdef OVERLAYFS_DIAGNOSTIC
    229   1.1  wrstuden 	vprint("alias root of lower", overlay_rootvp);
    230  1.29     perry #endif
    231   1.1  wrstuden 	/*
    232  1.47        ad 	 * Blow it away for future re-use
    233   1.1  wrstuden 	 */
    234   1.1  wrstuden 	vgone(overlay_rootvp);
    235   1.1  wrstuden 	/*
    236   1.1  wrstuden 	 * Finally, throw away the overlay_mount structure
    237   1.1  wrstuden 	 */
    238  1.50        ad 	omp = mp->mnt_data;
    239  1.57     rmind 	kmem_free(omp, sizeof(struct overlay_mount));
    240  1.52    simonb 	mp->mnt_data = NULL;
    241   1.1  wrstuden 	return 0;
    242   1.1  wrstuden }
    243   1.1  wrstuden 
    244   1.6  jdolecek extern const struct vnodeopv_desc overlay_vnodeop_opv_desc;
    245   1.1  wrstuden 
    246   1.6  jdolecek const struct vnodeopv_desc * const ov_vnodeopv_descs[] = {
    247   1.1  wrstuden 	&overlay_vnodeop_opv_desc,
    248   1.1  wrstuden 	NULL,
    249   1.1  wrstuden };
    250   1.1  wrstuden 
    251   1.1  wrstuden struct vfsops overlay_vfsops = {
    252  1.60   hannken 	.vfs_name = MOUNT_OVERLAY,
    253  1.60   hannken 	.vfs_min_mount_data = sizeof (struct overlay_args),
    254  1.60   hannken 	.vfs_mount = ov_mount,
    255  1.60   hannken 	.vfs_start = layerfs_start,
    256  1.60   hannken 	.vfs_unmount = ov_unmount,
    257  1.60   hannken 	.vfs_root = layerfs_root,
    258  1.60   hannken 	.vfs_quotactl = layerfs_quotactl,
    259  1.60   hannken 	.vfs_statvfs = layerfs_statvfs,
    260  1.60   hannken 	.vfs_sync = layerfs_sync,
    261  1.62   hannken 	.vfs_loadvnode = layerfs_loadvnode,
    262  1.60   hannken 	.vfs_vget = layerfs_vget,
    263  1.60   hannken 	.vfs_fhtovp = layerfs_fhtovp,
    264  1.60   hannken 	.vfs_vptofh = layerfs_vptofh,
    265  1.60   hannken 	.vfs_init = layerfs_init,
    266  1.60   hannken 	.vfs_done = layerfs_done,
    267  1.60   hannken 	.vfs_snapshot = layerfs_snapshot,
    268  1.60   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    269  1.64   hannken 	.vfs_suspendctl = layerfs_suspendctl,
    270  1.60   hannken 	.vfs_renamelock_enter = layerfs_renamelock_enter,
    271  1.60   hannken 	.vfs_renamelock_exit = layerfs_renamelock_exit,
    272  1.60   hannken 	.vfs_fsync = (void *)eopnotsupp,
    273  1.60   hannken 	.vfs_opv_descs = ov_vnodeopv_descs
    274   1.1  wrstuden };
    275  1.51    rumble 
    276  1.69  pgoyette SYSCTL_SETUP(overlay_sysctl_setup, "overlay fs sysctl")
    277  1.69  pgoyette {
    278  1.69  pgoyette 
    279  1.70  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    280  1.69  pgoyette 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "overlay",
    281  1.69  pgoyette 		       SYSCTL_DESCR("Overlay file system"),
    282  1.69  pgoyette 		       NULL, 0, NULL, 0,
    283  1.69  pgoyette 		       CTL_VFS, CTL_CREATE, CTL_EOL);
    284  1.69  pgoyette }
    285  1.69  pgoyette 
    286  1.51    rumble static int
    287  1.51    rumble overlay_modcmd(modcmd_t cmd, void *arg)
    288  1.51    rumble {
    289  1.53    rumble 	int error;
    290  1.51    rumble 
    291  1.51    rumble 	switch (cmd) {
    292  1.51    rumble 	case MODULE_CMD_INIT:
    293  1.53    rumble 		error = vfs_attach(&overlay_vfsops);
    294  1.53    rumble 		if (error != 0)
    295  1.53    rumble 			break;
    296  1.53    rumble 		break;
    297  1.51    rumble 	case MODULE_CMD_FINI:
    298  1.53    rumble 		error = vfs_detach(&overlay_vfsops);
    299  1.53    rumble 		if (error != 0)
    300  1.53    rumble 			break;
    301  1.53    rumble 		break;
    302  1.51    rumble 	default:
    303  1.53    rumble 		error = ENOTTY;
    304  1.53    rumble 		break;
    305  1.51    rumble 	}
    306  1.53    rumble 
    307  1.53    rumble 	return (error);
    308  1.51    rumble }
    309