Home | History | Annotate | Line # | Download | only in genfs
layer_vfsops.c revision 1.49
      1 /*	$NetBSD: layer_vfsops.c,v 1.49 2017/04/11 07:51:37 hannken 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.49 2017/04/11 07:51:37 hannken 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 #ifdef _MODULE
    101 	static struct sysctllog *layerfs_clog = NULL;
    102 #endif
    103 
    104 	switch (cmd) {
    105 	case MODULE_CMD_INIT:
    106 #ifdef _MODULE
    107 		sysctl_vfs_layerfs_setup(&layerfs_clog);
    108 #endif
    109 		return 0;
    110 	case MODULE_CMD_FINI:
    111 #ifdef _MODULE
    112 		sysctl_teardown(&layerfs_clog);
    113 #endif
    114 		return 0;
    115 	default:
    116 		return ENOTTY;
    117 	}
    118 	return 0;
    119 }
    120 
    121 /*
    122  * VFS start.  Nothing needed here - the start routine on the underlying
    123  * filesystem will have been called when that filesystem was mounted.
    124  */
    125 int
    126 layerfs_start(struct mount *mp, int flags)
    127 {
    128 
    129 #ifdef notyet
    130 	return VFS_START(mp->mnt_lower, flags);
    131 #else
    132 	return 0;
    133 #endif
    134 }
    135 
    136 int
    137 layerfs_root(struct mount *mp, struct vnode **vpp)
    138 {
    139 	struct vnode *vp;
    140 
    141 	vp = MOUNTTOLAYERMOUNT(mp)->layerm_rootvp;
    142 	if (vp == NULL) {
    143 		*vpp = NULL;
    144 		return EINVAL;
    145 	}
    146 	/*
    147 	 * Return root vnode with locked and with a reference held.
    148 	 */
    149 	vref(vp);
    150 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    151 	*vpp = vp;
    152 	return 0;
    153 }
    154 
    155 int
    156 layerfs_quotactl(struct mount *mp, struct quotactl_args *args)
    157 {
    158 
    159 	return VFS_QUOTACTL(mp->mnt_lower, args);
    160 }
    161 
    162 int
    163 layerfs_statvfs(struct mount *mp, struct statvfs *sbp)
    164 {
    165 	struct statvfs *sbuf;
    166 	int error;
    167 
    168 	sbuf = kmem_zalloc(sizeof(*sbuf), KM_SLEEP);
    169 	if (sbuf == NULL) {
    170 		return ENOMEM;
    171 	}
    172 	error = VFS_STATVFS(mp->mnt_lower, sbuf);
    173 	if (error) {
    174 		goto done;
    175 	}
    176 	/* Copy across the relevant data and fake the rest. */
    177 	sbp->f_flag = sbuf->f_flag;
    178 	sbp->f_bsize = sbuf->f_bsize;
    179 	sbp->f_frsize = sbuf->f_frsize;
    180 	sbp->f_iosize = sbuf->f_iosize;
    181 	sbp->f_blocks = sbuf->f_blocks;
    182 	sbp->f_bfree = sbuf->f_bfree;
    183 	sbp->f_bavail = sbuf->f_bavail;
    184 	sbp->f_bresvd = sbuf->f_bresvd;
    185 	sbp->f_files = sbuf->f_files;
    186 	sbp->f_ffree = sbuf->f_ffree;
    187 	sbp->f_favail = sbuf->f_favail;
    188 	sbp->f_fresvd = sbuf->f_fresvd;
    189 	sbp->f_namemax = sbuf->f_namemax;
    190 	copy_statvfs_info(sbp, mp);
    191 done:
    192 	kmem_free(sbuf, sizeof(*sbuf));
    193 	return error;
    194 }
    195 
    196 int
    197 layerfs_sync(struct mount *mp, int waitfor,
    198     kauth_cred_t cred)
    199 {
    200 
    201 	/*
    202 	 * XXX - Assumes no data cached at layer.
    203 	 */
    204 	return 0;
    205 }
    206 
    207 int
    208 layerfs_loadvnode(struct mount *mp, struct vnode *vp,
    209     const void *key, size_t key_len, const void **new_key)
    210 {
    211 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    212 	struct vnode *lowervp;
    213 	struct layer_node *xp;
    214 
    215 	KASSERT(key_len == sizeof(struct vnode *));
    216 	memcpy(&lowervp, key, key_len);
    217 
    218 	xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
    219 	if (xp == NULL)
    220 		return ENOMEM;
    221 
    222 	/* Share the interlock with the lower node. */
    223 	mutex_obj_hold(lowervp->v_interlock);
    224 	uvm_obj_setlock(&vp->v_uobj, lowervp->v_interlock);
    225 
    226 	/* Share the lock with the lower node. */
    227 	vshare_lock(vp, lowervp);
    228 
    229 	vp->v_tag = lmp->layerm_tag;
    230 	vp->v_type = lowervp->v_type;
    231 	vp->v_op = lmp->layerm_vnodeop_p;
    232 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    233 		spec_node_init(vp, lowervp->v_rdev);
    234 	vp->v_data = xp;
    235 	xp->layer_vnode = vp;
    236 	xp->layer_lowervp = lowervp;
    237 	xp->layer_flags = 0;
    238 	uvm_vnp_setsize(vp, 0);
    239 
    240 	/*  Add a reference to the lower node. */
    241 	vref(lowervp);
    242 	*new_key = &xp->layer_lowervp;
    243 	return 0;
    244 }
    245 
    246 int
    247 layerfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    248 {
    249 	struct vnode *vp;
    250 	int error;
    251 
    252 	error = VFS_VGET(mp->mnt_lower, ino, &vp);
    253 	if (error) {
    254 		*vpp = NULL;
    255 		return error;
    256 	}
    257 	VOP_UNLOCK(vp);
    258 	error = layer_node_create(mp, vp, vpp);
    259 	if (error) {
    260 		vrele(vp);
    261 		*vpp = NULL;
    262 		return error;
    263 	}
    264 	error = vn_lock(*vpp, LK_EXCLUSIVE);
    265 	if (error) {
    266 		vrele(*vpp);
    267 		*vpp = NULL;
    268 		return error;
    269 	}
    270 	return 0;
    271 }
    272 
    273 int
    274 layerfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
    275 {
    276 	struct vnode *vp;
    277 	int error;
    278 
    279 	error = VFS_FHTOVP(mp->mnt_lower, fidp, &vp);
    280 	if (error) {
    281 		*vpp = NULL;
    282 		return error;
    283 	}
    284 	VOP_UNLOCK(vp);
    285 	error = layer_node_create(mp, vp, vpp);
    286 	if (error) {
    287 		vput(vp);
    288 		*vpp = NULL;
    289 		return (error);
    290 	}
    291 	error = vn_lock(*vpp, LK_EXCLUSIVE);
    292 	if (error) {
    293 		vrele(*vpp);
    294 		*vpp = NULL;
    295 		return error;
    296 	}
    297 	return 0;
    298 }
    299 
    300 int
    301 layerfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    302 {
    303 
    304 	return VFS_VPTOFH(LAYERVPTOLOWERVP(vp), fhp, fh_size);
    305 }
    306 
    307 /*
    308  * layerfs_snapshot - handle a snapshot through a layered file system
    309  *
    310  * At present, we do NOT support snapshotting through a layered file
    311  * system as the ffs implementation changes v_vnlock of the snapshot
    312  * vnodes to point to one common lock. As there is no way for us to
    313  * absolutely pass this change up the stack, a layered file system
    314  * would end up referencing the wrong lock.
    315  *
    316  * This routine serves as a central resource for this behavior; all
    317  * layered file systems don't need to worry about the above. Also, if
    318  * things get fixed, all layers get the benefit.
    319  */
    320 int
    321 layerfs_snapshot(struct mount *mp, struct vnode *vp,
    322     struct timespec *ts)
    323 {
    324 
    325 	return EOPNOTSUPP;
    326 }
    327 
    328 /*
    329  * layerfs_suspendctl - suspend a layered file system
    330  *
    331  * Here we should suspend the lower file system(s) too.  At present
    332  * this will deadlock as we don't know which to suspend first.
    333  *
    334  * This routine serves as a central resource for this behavior; all
    335  * layered file systems don't need to worry about the above. Also, if
    336  * things get fixed, all layers get the benefit.
    337  */
    338 int
    339 layerfs_suspendctl(struct mount *mp, int cmd)
    340 {
    341 
    342 	return genfs_suspendctl(mp, cmd);
    343 }
    344 
    345 SYSCTL_SETUP(sysctl_vfs_layerfs_setup, "sysctl vfs.layerfs subtree setup")
    346 {
    347 	const struct sysctlnode *layerfs_node = NULL;
    348 
    349 	sysctl_createv(clog, 0, NULL, &layerfs_node,
    350 #ifdef _MODULE
    351 		       0,
    352 #else
    353 		       CTLFLAG_PERMANENT,
    354 #endif
    355 		       CTLTYPE_NODE, "layerfs",
    356 		       SYSCTL_DESCR("Generic layered file system"),
    357 		       NULL, 0, NULL, 0,
    358 		       CTL_VFS, CTL_CREATE, CTL_EOL);
    359 
    360 #ifdef LAYERFS_DIAGNOSTIC
    361 	sysctl_createv(clog, 0, &layerfs_node, NULL,
    362 #ifndef _MODULE
    363 	               CTLFLAG_PERMANENT |
    364 #endif
    365 		       CTLFLAG_READWRITE,
    366 	               CTLTYPE_INT,
    367 	               "debug",
    368 	               SYSCTL_DESCR("Verbose debugging messages"),
    369 	               NULL, 0, &layerfs_debug, 0,
    370 	               CTL_CREATE, CTL_EOL);
    371 #endif
    372 
    373 	/*
    374 	 * other subtrees should really be aliases to this, but since
    375 	 * they can't tell if layerfs has been instantiated yet, they
    376 	 * can't do that...not easily.  not yet.  :-)
    377 	 */
    378 }
    379 
    380 int
    381 layerfs_renamelock_enter(struct mount *mp)
    382 {
    383 
    384 	return VFS_RENAMELOCK_ENTER(mp->mnt_lower);
    385 }
    386 
    387 void
    388 layerfs_renamelock_exit(struct mount *mp)
    389 {
    390 
    391 	VFS_RENAMELOCK_EXIT(mp->mnt_lower);
    392 }
    393