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