Home | History | Annotate | Line # | Download | only in genfs
layer_vnops.c revision 1.28.2.2.6.1
      1 /*	$NetBSD: layer_vnops.c,v 1.28.2.2.6.1 2012/11/14 20:15:37 riz 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  * Copyright (c) 1992, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * John Heidemann of the UCLA Ficus project.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
     67  *
     68  * Ancestors:
     69  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
     70  *	$Id: layer_vnops.c,v 1.28.2.2.6.1 2012/11/14 20:15:37 riz Exp $
     71  *	$Id: layer_vnops.c,v 1.28.2.2.6.1 2012/11/14 20:15:37 riz Exp $
     72  *	...and...
     73  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
     74  */
     75 
     76 /*
     77  * Null Layer vnode routines.
     78  *
     79  * (See mount_null(8) for more information.)
     80  *
     81  * The layer.h, layer_extern.h, layer_vfs.c, and layer_vnops.c files provide
     82  * the core implementation of the null file system and most other stacked
     83  * fs's. The description below refers to the null file system, but the
     84  * services provided by the layer* files are useful for all layered fs's.
     85  *
     86  * The null layer duplicates a portion of the file system
     87  * name space under a new name.  In this respect, it is
     88  * similar to the loopback file system.  It differs from
     89  * the loopback fs in two respects:  it is implemented using
     90  * a stackable layers techniques, and it's "null-node"s stack above
     91  * all lower-layer vnodes, not just over directory vnodes.
     92  *
     93  * The null layer has two purposes.  First, it serves as a demonstration
     94  * of layering by proving a layer which does nothing.  (It actually
     95  * does everything the loopback file system does, which is slightly
     96  * more than nothing.)  Second, the null layer can serve as a prototype
     97  * layer.  Since it provides all necessary layer framework,
     98  * new file system layers can be created very easily be starting
     99  * with a null layer.
    100  *
    101  * The remainder of the man page examines the null layer as a basis
    102  * for constructing new layers.
    103  *
    104  *
    105  * INSTANTIATING NEW NULL LAYERS
    106  *
    107  * New null layers are created with mount_null(8).
    108  * Mount_null(8) takes two arguments, the pathname
    109  * of the lower vfs (target-pn) and the pathname where the null
    110  * layer will appear in the namespace (alias-pn).  After
    111  * the null layer is put into place, the contents
    112  * of target-pn subtree will be aliased under alias-pn.
    113  *
    114  * It is conceivable that other overlay filesystems will take different
    115  * parameters. For instance, data migration or access controll layers might
    116  * only take one pathname which will serve both as the target-pn and
    117  * alias-pn described above.
    118  *
    119  *
    120  * OPERATION OF A NULL LAYER
    121  *
    122  * The null layer is the minimum file system layer,
    123  * simply bypassing all possible operations to the lower layer
    124  * for processing there.  The majority of its activity centers
    125  * on the bypass routine, through which nearly all vnode operations
    126  * pass.
    127  *
    128  * The bypass routine accepts arbitrary vnode operations for
    129  * handling by the lower layer.  It begins by examing vnode
    130  * operation arguments and replacing any layered nodes by their
    131  * lower-layer equivalents.  It then invokes the operation
    132  * on the lower layer.  Finally, it replaces the layered nodes
    133  * in the arguments and, if a vnode is return by the operation,
    134  * stacks a layered node on top of the returned vnode.
    135  *
    136  * The bypass routine in this file, layer_bypass(), is suitable for use
    137  * by many different layered filesystems. It can be used by multiple
    138  * filesystems simultaneously. Alternatively, a layered fs may provide
    139  * its own bypass routine, in which case layer_bypass() should be used as
    140  * a model. For instance, the main functionality provided by umapfs, the user
    141  * identity mapping file system, is handled by a custom bypass routine.
    142  *
    143  * Typically a layered fs registers its selected bypass routine as the
    144  * default vnode operation in its vnodeopv_entry_desc table. Additionally
    145  * the filesystem must store the bypass entry point in the layerm_bypass
    146  * field of struct layer_mount. All other layer routines in this file will
    147  * use the layerm_bypass routine.
    148  *
    149  * Although the bypass routine handles most operations outright, a number
    150  * of operations are special cased, and handled by the layered fs. One
    151  * group, layer_setattr, layer_getattr, layer_access, layer_open, and
    152  * layer_fsync, perform layer-specific manipulation in addition to calling
    153  * the bypass routine. The other group
    154 
    155  * Although bypass handles most operations, vop_getattr, vop_lock,
    156  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
    157  * bypassed. Vop_getattr must change the fsid being returned.
    158  * Vop_lock and vop_unlock must handle any locking for the
    159  * current vnode as well as pass the lock request down.
    160  * Vop_inactive and vop_reclaim are not bypassed so that
    161  * they can handle freeing null-layer specific data. Vop_print
    162  * is not bypassed to avoid excessive debugging information.
    163  * Also, certain vnode operations change the locking state within
    164  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
    165  * and symlink). Ideally these operations should not change the
    166  * lock state, but should be changed to let the caller of the
    167  * function unlock them. Otherwise all intermediate vnode layers
    168  * (such as union, umapfs, etc) must catch these functions to do
    169  * the necessary locking at their layer.
    170  *
    171  *
    172  * INSTANTIATING VNODE STACKS
    173  *
    174  * Mounting associates the null layer with a lower layer,
    175  * effect stacking two VFSes.  Vnode stacks are instead
    176  * created on demand as files are accessed.
    177  *
    178  * The initial mount creates a single vnode stack for the
    179  * root of the new null layer.  All other vnode stacks
    180  * are created as a result of vnode operations on
    181  * this or other null vnode stacks.
    182  *
    183  * New vnode stacks come into existence as a result of
    184  * an operation which returns a vnode.
    185  * The bypass routine stacks a null-node above the new
    186  * vnode before returning it to the caller.
    187  *
    188  * For example, imagine mounting a null layer with
    189  * "mount_null /usr/include /dev/layer/null".
    190  * Changing directory to /dev/layer/null will assign
    191  * the root null-node (which was created when the null layer was mounted).
    192  * Now consider opening "sys".  A vop_lookup would be
    193  * done on the root null-node.  This operation would bypass through
    194  * to the lower layer which would return a vnode representing
    195  * the UFS "sys".  layer_bypass then builds a null-node
    196  * aliasing the UFS "sys" and returns this to the caller.
    197  * Later operations on the null-node "sys" will repeat this
    198  * process when constructing other vnode stacks.
    199  *
    200  *
    201  * CREATING OTHER FILE SYSTEM LAYERS
    202  *
    203  * One of the easiest ways to construct new file system layers is to make
    204  * a copy of the null layer, rename all files and variables, and
    205  * then begin modifing the copy.  Sed can be used to easily rename
    206  * all variables.
    207  *
    208  * The umap layer is an example of a layer descended from the
    209  * null layer.
    210  *
    211  *
    212  * INVOKING OPERATIONS ON LOWER LAYERS
    213  *
    214  * There are two techniques to invoke operations on a lower layer
    215  * when the operation cannot be completely bypassed.  Each method
    216  * is appropriate in different situations.  In both cases,
    217  * it is the responsibility of the aliasing layer to make
    218  * the operation arguments "correct" for the lower layer
    219  * by mapping an vnode arguments to the lower layer.
    220  *
    221  * The first approach is to call the aliasing layer's bypass routine.
    222  * This method is most suitable when you wish to invoke the operation
    223  * currently being handled on the lower layer.  It has the advantage
    224  * that the bypass routine already must do argument mapping.
    225  * An example of this is null_getattrs in the null layer.
    226  *
    227  * A second approach is to directly invoke vnode operations on
    228  * the lower layer with the VOP_OPERATIONNAME interface.
    229  * The advantage of this method is that it is easy to invoke
    230  * arbitrary operations on the lower layer.  The disadvantage
    231  * is that vnodes' arguments must be manually mapped.
    232  *
    233  */
    234 
    235 #include <sys/cdefs.h>
    236 __KERNEL_RCSID(0, "$NetBSD: layer_vnops.c,v 1.28.2.2.6.1 2012/11/14 20:15:37 riz Exp $");
    237 
    238 #include <sys/param.h>
    239 #include <sys/systm.h>
    240 #include <sys/proc.h>
    241 #include <sys/time.h>
    242 #include <sys/vnode.h>
    243 #include <sys/mount.h>
    244 #include <sys/namei.h>
    245 #include <sys/malloc.h>
    246 #include <sys/buf.h>
    247 #include <sys/kauth.h>
    248 
    249 #include <miscfs/genfs/layer.h>
    250 #include <miscfs/genfs/layer_extern.h>
    251 #include <miscfs/genfs/genfs.h>
    252 
    253 
    254 /*
    255  * This is the 08-June-99 bypass routine, based on the 10-Apr-92 bypass
    256  *		routine by John Heidemann.
    257  *	The new element for this version is that the whole nullfs
    258  * system gained the concept of locks on the lower node, and locks on
    259  * our nodes. When returning from a call to the lower layer, we may
    260  * need to update lock state ONLY on our layer. The LAYERFS_UPPER*LOCK()
    261  * macros provide this functionality.
    262  *    The 10-Apr-92 version was optimized for speed, throwing away some
    263  * safety checks.  It should still always work, but it's not as
    264  * robust to programmer errors.
    265  *    Define SAFETY to include some error checking code.
    266  *
    267  * In general, we map all vnodes going down and unmap them on the way back.
    268  *
    269  * Also, some BSD vnode operations have the side effect of vrele'ing
    270  * their arguments.  With stacking, the reference counts are held
    271  * by the upper node, not the lower one, so we must handle these
    272  * side-effects here.  This is not of concern in Sun-derived systems
    273  * since there are no such side-effects.
    274  *
    275  * New for the 08-June-99 version: we also handle operations which unlock
    276  * the passed-in node (typically they vput the node).
    277  *
    278  * This makes the following assumptions:
    279  * - only one returned vpp
    280  * - no INOUT vpp's (Sun's vop_open has one of these)
    281  * - the vnode operation vector of the first vnode should be used
    282  *   to determine what implementation of the op should be invoked
    283  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
    284  *   problems on rmdir'ing mount points and renaming?)
    285  */
    286 int
    287 layer_bypass(v)
    288 	void *v;
    289 {
    290 	struct vop_generic_args /* {
    291 		struct vnodeop_desc *a_desc;
    292 		<other random data follows, presumably>
    293 	} */ *ap = v;
    294 	int (**our_vnodeop_p)(void *);
    295 	struct vnode **this_vp_p;
    296 	int error, error1;
    297 	struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
    298 	struct vnode **vps_p[VDESC_MAX_VPS];
    299 	struct vnode ***vppp;
    300 	struct vnodeop_desc *descp = ap->a_desc;
    301 	int reles, i, flags;
    302 
    303 #ifdef SAFETY
    304 	/*
    305 	 * We require at least one vp.
    306 	 */
    307 	if (descp->vdesc_vp_offsets == NULL ||
    308 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
    309 		panic("%s: no vp's in map.\n", __func__);
    310 #endif
    311 
    312 	vps_p[0] =
    313 	    VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
    314 	vp0 = *vps_p[0];
    315 	flags = MOUNTTOLAYERMOUNT(vp0->v_mount)->layerm_flags;
    316 	our_vnodeop_p = vp0->v_op;
    317 
    318 	if (flags & LAYERFS_MBYPASSDEBUG)
    319 		printf("%s: %s\n", __func__, descp->vdesc_name);
    320 
    321 	/*
    322 	 * Map the vnodes going in.
    323 	 * Later, we'll invoke the operation based on
    324 	 * the first mapped vnode's operation vector.
    325 	 */
    326 	reles = descp->vdesc_flags;
    327 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
    328 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
    329 			break;   /* bail out at end of list */
    330 		vps_p[i] = this_vp_p =
    331 		    VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i],
    332 		    ap);
    333 		/*
    334 		 * We're not guaranteed that any but the first vnode
    335 		 * are of our type.  Check for and don't map any
    336 		 * that aren't.  (We must always map first vp or vclean fails.)
    337 		 */
    338 		if (i && (*this_vp_p == NULL ||
    339 		    (*this_vp_p)->v_op != our_vnodeop_p)) {
    340 			old_vps[i] = NULL;
    341 		} else {
    342 			old_vps[i] = *this_vp_p;
    343 			*(vps_p[i]) = LAYERVPTOLOWERVP(*this_vp_p);
    344 			/*
    345 			 * XXX - Several operations have the side effect
    346 			 * of vrele'ing their vp's.  We must account for
    347 			 * that.  (This should go away in the future.)
    348 			 */
    349 			if (reles & VDESC_VP0_WILLRELE)
    350 				VREF(*this_vp_p);
    351 		}
    352 
    353 	}
    354 
    355 	/*
    356 	 * Call the operation on the lower layer
    357 	 * with the modified argument structure.
    358 	 */
    359 	error = VCALL(*vps_p[0], descp->vdesc_offset, ap);
    360 
    361 	/*
    362 	 * Maintain the illusion of call-by-value
    363 	 * by restoring vnodes in the argument structure
    364 	 * to their original value.
    365 	 */
    366 	reles = descp->vdesc_flags;
    367 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
    368 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
    369 			break;   /* bail out at end of list */
    370 		if (old_vps[i]) {
    371 			*(vps_p[i]) = old_vps[i];
    372 			if (reles & VDESC_VP0_WILLUNLOCK)
    373 				LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1);
    374 			if (reles & VDESC_VP0_WILLRELE)
    375 				vrele(*(vps_p[i]));
    376 		}
    377 	}
    378 
    379 	/*
    380 	 * Map the possible out-going vpp
    381 	 * (Assumes that the lower layer always returns
    382 	 * a VREF'ed vpp unless it gets an error.)
    383 	 */
    384 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
    385 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
    386 	    !error) {
    387 		/*
    388 		 * XXX - even though some ops have vpp returned vp's,
    389 		 * several ops actually vrele this before returning.
    390 		 * We must avoid these ops.
    391 		 * (This should go away when these ops are regularized.)
    392 		 */
    393 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
    394 			goto out;
    395 		vppp = VOPARG_OFFSETTO(struct vnode***,
    396 				 descp->vdesc_vpp_offset, ap);
    397 		/*
    398 		 * Only vop_lookup, vop_create, vop_makedir, vop_bmap,
    399 		 * vop_mknod, and vop_symlink return vpp's. vop_bmap
    400 		 * doesn't call bypass as the lower vpp is fine (we're just
    401 		 * going to do i/o on it). vop_lookup doesn't call bypass
    402 		 * as a lookup on "." would generate a locking error.
    403 		 * So all the calls which get us here have a locked vpp. :-)
    404 		 */
    405 		error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp);
    406 		if (error) {
    407 			vput(**vppp);
    408 			**vppp = NULL;
    409 		}
    410 	}
    411 
    412  out:
    413 	return (error);
    414 }
    415 
    416 /*
    417  * We have to carry on the locking protocol on the layer vnodes
    418  * as we progress through the tree. We also have to enforce read-only
    419  * if this layer is mounted read-only.
    420  */
    421 int
    422 layer_lookup(v)
    423 	void *v;
    424 {
    425 	struct vop_lookup_args /* {
    426 		struct vnodeop_desc *a_desc;
    427 		struct vnode * a_dvp;
    428 		struct vnode ** a_vpp;
    429 		struct componentname * a_cnp;
    430 	} */ *ap = v;
    431 	struct componentname *cnp = ap->a_cnp;
    432 	int flags = cnp->cn_flags;
    433 	struct vnode *dvp, *lvp, *ldvp;
    434 	int error;
    435 
    436 	dvp = ap->a_dvp;
    437 
    438 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    439 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
    440 		*ap->a_vpp = NULL;
    441 		return (EROFS);
    442 	}
    443 
    444 	ldvp = LAYERVPTOLOWERVP(dvp);
    445 	ap->a_dvp = ldvp;
    446 	error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
    447 	lvp = *ap->a_vpp;
    448 	*ap->a_vpp = NULL;
    449 
    450 	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
    451 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    452 	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
    453 		error = EROFS;
    454 
    455 	/*
    456 	 * We must do the same locking and unlocking at this layer as
    457 	 * is done in the layers below us.
    458 	 */
    459 	if (ldvp == lvp) {
    460 
    461 		/*
    462 		 * Did lookup on "." or ".." in the root node of a mount point.
    463 		 * So we return dvp after a VREF.
    464 		 */
    465 		VREF(dvp);
    466 		*ap->a_vpp = dvp;
    467 		vrele(lvp);
    468 	} else if (lvp != NULL) {
    469 		/* dvp, ldvp and vp are all locked */
    470 		error = layer_node_create(dvp->v_mount, lvp, ap->a_vpp);
    471 		if (error) {
    472 			vput(lvp);
    473 		}
    474 	}
    475 	return (error);
    476 }
    477 
    478 /*
    479  * Setattr call. Disallow write attempts if the layer is mounted read-only.
    480  */
    481 int
    482 layer_setattr(v)
    483 	void *v;
    484 {
    485 	struct vop_setattr_args /* {
    486 		struct vnodeop_desc *a_desc;
    487 		struct vnode *a_vp;
    488 		struct vattr *a_vap;
    489 		kauth_cred_t a_cred;
    490 		struct lwp *a_l;
    491 	} */ *ap = v;
    492 	struct vnode *vp = ap->a_vp;
    493 	struct vattr *vap = ap->a_vap;
    494 
    495   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
    496 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    497 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    498 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    499 		return (EROFS);
    500 	if (vap->va_size != VNOVAL) {
    501  		switch (vp->v_type) {
    502  		case VDIR:
    503  			return (EISDIR);
    504  		case VCHR:
    505  		case VBLK:
    506  		case VSOCK:
    507  		case VFIFO:
    508 			return (0);
    509 		case VREG:
    510 		case VLNK:
    511  		default:
    512 			/*
    513 			 * Disallow write attempts if the filesystem is
    514 			 * mounted read-only.
    515 			 */
    516 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    517 				return (EROFS);
    518 		}
    519 	}
    520 	return (LAYERFS_DO_BYPASS(vp, ap));
    521 }
    522 
    523 /*
    524  *  We handle getattr only to change the fsid.
    525  */
    526 int
    527 layer_getattr(v)
    528 	void *v;
    529 {
    530 	struct vop_getattr_args /* {
    531 		struct vnode *a_vp;
    532 		struct vattr *a_vap;
    533 		kauth_cred_t a_cred;
    534 		struct lwp *a_l;
    535 	} */ *ap = v;
    536 	struct vnode *vp = ap->a_vp;
    537 	int error;
    538 
    539 	if ((error = LAYERFS_DO_BYPASS(vp, ap)) != 0)
    540 		return (error);
    541 	/* Requires that arguments be restored. */
    542 	ap->a_vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    543 	return (0);
    544 }
    545 
    546 int
    547 layer_access(v)
    548 	void *v;
    549 {
    550 	struct vop_access_args /* {
    551 		struct vnode *a_vp;
    552 		int  a_mode;
    553 		kauth_cred_t a_cred;
    554 		struct lwp *a_l;
    555 	} */ *ap = v;
    556 	struct vnode *vp = ap->a_vp;
    557 	mode_t mode = ap->a_mode;
    558 
    559 	/*
    560 	 * Disallow write attempts on read-only layers;
    561 	 * unless the file is a socket, fifo, or a block or
    562 	 * character device resident on the file system.
    563 	 */
    564 	if (mode & VWRITE) {
    565 		switch (vp->v_type) {
    566 		case VDIR:
    567 		case VLNK:
    568 		case VREG:
    569 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    570 				return (EROFS);
    571 			break;
    572 		default:
    573 			break;
    574 		}
    575 	}
    576 	return (LAYERFS_DO_BYPASS(vp, ap));
    577 }
    578 
    579 /*
    580  * We must handle open to be able to catch MNT_NODEV and friends.
    581  */
    582 int
    583 layer_open(v)
    584 	void *v;
    585 {
    586 	struct vop_open_args *ap = v;
    587 	struct vnode *vp = ap->a_vp;
    588 	enum vtype lower_type = LAYERVPTOLOWERVP(vp)->v_type;
    589 
    590 	if (((lower_type == VBLK) || (lower_type == VCHR)) &&
    591 	    (vp->v_mount->mnt_flag & MNT_NODEV))
    592 		return ENXIO;
    593 
    594 	return LAYERFS_DO_BYPASS(vp, ap);
    595 }
    596 
    597 /*
    598  * We need to process our own vnode lock and then clear the
    599  * interlock flag as it applies only to our vnode, not the
    600  * vnodes below us on the stack.
    601  */
    602 int
    603 layer_lock(v)
    604 	void *v;
    605 {
    606 	struct vop_lock_args /* {
    607 		struct vnode *a_vp;
    608 		int a_flags;
    609 		struct proc *a_p;
    610 	} */ *ap = v;
    611 	struct vnode *vp = ap->a_vp, *lowervp;
    612 	int	flags = ap->a_flags, error;
    613 
    614 	if (vp->v_vnlock != NULL) {
    615 		/*
    616 		 * The lower level has exported a struct lock to us. Use
    617 		 * it so that all vnodes in the stack lock and unlock
    618 		 * simultaneously. Note: we don't DRAIN the lock as DRAIN
    619 		 * decommissions the lock - just because our vnode is
    620 		 * going away doesn't mean the struct lock below us is.
    621 		 * LK_EXCLUSIVE is fine.
    622 		 */
    623 		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
    624 			return(lockmgr(vp->v_vnlock,
    625 				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
    626 				&vp->v_interlock));
    627 		} else
    628 			return(lockmgr(vp->v_vnlock, flags, &vp->v_interlock));
    629 	} else {
    630 		/*
    631 		 * Ahh well. It would be nice if the fs we're over would
    632 		 * export a struct lock for us to use, but it doesn't.
    633 		 *
    634 		 * To prevent race conditions involving doing a lookup
    635 		 * on "..", we have to lock the lower node, then lock our
    636 		 * node. Most of the time it won't matter that we lock our
    637 		 * node (as any locking would need the lower one locked
    638 		 * first). But we can LK_DRAIN the upper lock as a step
    639 		 * towards decomissioning it.
    640 		 */
    641 		lowervp = LAYERVPTOLOWERVP(vp);
    642 		if (flags & LK_INTERLOCK) {
    643 			simple_unlock(&vp->v_interlock);
    644 			flags &= ~LK_INTERLOCK;
    645 		}
    646 		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
    647 			error = VOP_LOCK(lowervp,
    648 				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE);
    649 		} else
    650 			error = VOP_LOCK(lowervp, flags);
    651 		if (error)
    652 			return (error);
    653 		if ((error = lockmgr(&vp->v_lock, flags, &vp->v_interlock))) {
    654 			VOP_UNLOCK(lowervp, 0);
    655 		}
    656 		return (error);
    657 	}
    658 }
    659 
    660 /*
    661  */
    662 int
    663 layer_unlock(v)
    664 	void *v;
    665 {
    666 	struct vop_unlock_args /* {
    667 		struct vnode *a_vp;
    668 		int a_flags;
    669 		struct proc *a_p;
    670 	} */ *ap = v;
    671 	struct vnode *vp = ap->a_vp;
    672 	int	flags = ap->a_flags;
    673 
    674 	if (vp->v_vnlock != NULL) {
    675 		return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
    676 			&vp->v_interlock));
    677 	} else {
    678 		if (flags & LK_INTERLOCK) {
    679 			simple_unlock(&vp->v_interlock);
    680 			flags &= ~LK_INTERLOCK;
    681 		}
    682 		VOP_UNLOCK(LAYERVPTOLOWERVP(vp), flags);
    683 		return (lockmgr(&vp->v_lock, flags | LK_RELEASE,
    684 			&vp->v_interlock));
    685 	}
    686 }
    687 
    688 int
    689 layer_islocked(v)
    690 	void *v;
    691 {
    692 	struct vop_islocked_args /* {
    693 		struct vnode *a_vp;
    694 	} */ *ap = v;
    695 	struct vnode *vp = ap->a_vp;
    696 	int lkstatus;
    697 
    698 	if (vp->v_vnlock != NULL)
    699 		return lockstatus(vp->v_vnlock);
    700 
    701 	lkstatus = VOP_ISLOCKED(LAYERVPTOLOWERVP(vp));
    702 	if (lkstatus)
    703 		return lkstatus;
    704 
    705 	return lockstatus(&vp->v_lock);
    706 }
    707 
    708 /*
    709  * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother
    710  * syncing the underlying vnodes, since they'll be fsync'ed when
    711  * reclaimed; otherwise,
    712  * pass it through to the underlying layer.
    713  *
    714  * XXX Do we still need to worry about shallow fsync?
    715  */
    716 
    717 int
    718 layer_fsync(v)
    719 	void *v;
    720 {
    721 	struct vop_fsync_args /* {
    722 		struct vnode *a_vp;
    723 		kauth_cred_t a_cred;
    724 		int  a_flags;
    725 		off_t offlo;
    726 		off_t offhi;
    727 		struct lwp *a_l;
    728 	} */ *ap = v;
    729 
    730 	if (ap->a_flags & FSYNC_RECLAIM) {
    731 		return 0;
    732 	}
    733 
    734 	return (LAYERFS_DO_BYPASS(ap->a_vp, ap));
    735 }
    736 
    737 
    738 int
    739 layer_inactive(v)
    740 	void *v;
    741 {
    742 	struct vop_inactive_args /* {
    743 		struct vnode *a_vp;
    744 		struct lwp *a_l;
    745 	} */ *ap = v;
    746 	struct vnode *vp = ap->a_vp;
    747 
    748 	/*
    749 	 * Do nothing (and _don't_ bypass).
    750 	 * Wait to vrele lowervp until reclaim,
    751 	 * so that until then our layer_node is in the
    752 	 * cache and reusable.
    753 	 *
    754 	 * NEEDSWORK: Someday, consider inactive'ing
    755 	 * the lowervp and then trying to reactivate it
    756 	 * with capabilities (v_id)
    757 	 * like they do in the name lookup cache code.
    758 	 * That's too much work for now.
    759 	 */
    760 	VOP_UNLOCK(vp, 0);
    761 
    762 	/*
    763 	 * ..., but don't cache the device node. Also, if we did a
    764 	 * remove, don't cache the node.
    765 	 */
    766 	if (vp->v_type == VBLK || vp->v_type == VCHR
    767 	    || (VTOLAYER(vp)->layer_flags & LAYERFS_REMOVED))
    768 		vgone(vp);
    769 	return (0);
    770 }
    771 
    772 int
    773 layer_remove(v)
    774 	void *v;
    775 {
    776 	struct vop_remove_args /* {
    777 		struct vonde		*a_dvp;
    778 		struct vnode		*a_vp;
    779 		struct componentname	*a_cnp;
    780 	} */ *ap = v;
    781 
    782 	int		error;
    783 	struct vnode	*vp = ap->a_vp;
    784 
    785 	vref(vp);
    786 	if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0)
    787 		VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
    788 
    789 	vrele(vp);
    790 
    791 	return (error);
    792 }
    793 
    794 int
    795 layer_rename(v)
    796 	void *v;
    797 {
    798 	struct vop_rename_args  /* {
    799 		struct vnode		*a_fdvp;
    800 		struct vnode		*a_fvp;
    801 		struct componentname	*a_fcnp;
    802 		struct vnode		*a_tdvp;
    803 		struct vnode		*a_tvp;
    804 		struct componentname	*a_tcnp;
    805 	} */ *ap = v;
    806 
    807 	int error;
    808 	struct vnode *fdvp = ap->a_fdvp;
    809 	struct vnode *tvp;
    810 
    811 	tvp = ap->a_tvp;
    812 	if (tvp) {
    813 		if (tvp->v_mount != fdvp->v_mount)
    814 			tvp = NULL;
    815 		else
    816 			vref(tvp);
    817 	}
    818 	error = LAYERFS_DO_BYPASS(fdvp, ap);
    819 	if (tvp) {
    820 		if (error == 0)
    821 			VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED;
    822 		vrele(tvp);
    823 	}
    824 
    825 	return (error);
    826 }
    827 
    828 int
    829 layer_rmdir(v)
    830 	void *v;
    831 {
    832 	struct vop_rmdir_args /* {
    833 		struct vnode		*a_dvp;
    834 		struct vnode		*a_vp;
    835 		struct componentname	*a_cnp;
    836 	} */ *ap = v;
    837 	int		error;
    838 	struct vnode	*vp = ap->a_vp;
    839 
    840 	vref(vp);
    841 	if ((error = LAYERFS_DO_BYPASS(vp, ap)) == 0)
    842 		VTOLAYER(vp)->layer_flags |= LAYERFS_REMOVED;
    843 
    844 	vrele(vp);
    845 
    846 	return (error);
    847 }
    848 
    849 int
    850 layer_reclaim(v)
    851 	void *v;
    852 {
    853 	struct vop_reclaim_args /* {
    854 		struct vnode *a_vp;
    855 		struct lwp *a_l;
    856 	} */ *ap = v;
    857 	struct vnode *vp = ap->a_vp;
    858 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(vp->v_mount);
    859 	struct layer_node *xp = VTOLAYER(vp);
    860 	struct vnode *lowervp = xp->layer_lowervp;
    861 
    862 	/*
    863 	 * Note: in vop_reclaim, the node's struct lock has been
    864 	 * decomissioned, so we have to be careful about calling
    865 	 * VOP's on ourself. Even if we turned a LK_DRAIN into an
    866 	 * LK_EXCLUSIVE in layer_lock, we still must be careful as VXLOCK is
    867 	 * set.
    868 	 */
    869 	/* After this assignment, this node will not be re-used. */
    870 	if ((vp == lmp->layerm_rootvp)) {
    871 		/*
    872 		 * Oops! We no longer have a root node. Most likely reason is
    873 		 * that someone forcably unmunted the underlying fs.
    874 		 *
    875 		 * Now getting the root vnode will fail. We're dead. :-(
    876 		 */
    877 		lmp->layerm_rootvp = NULL;
    878 	}
    879 	xp->layer_lowervp = NULL;
    880 	simple_lock(&lmp->layerm_hashlock);
    881 	LIST_REMOVE(xp, layer_hash);
    882 	simple_unlock(&lmp->layerm_hashlock);
    883 	FREE(vp->v_data, M_TEMP);
    884 	vp->v_data = NULL;
    885 	vrele(lowervp);
    886 	return (0);
    887 }
    888 
    889 /*
    890  * We just feed the returned vnode up to the caller - there's no need
    891  * to build a layer node on top of the node on which we're going to do
    892  * i/o. :-)
    893  */
    894 int
    895 layer_bmap(v)
    896 	void *v;
    897 {
    898 	struct vop_bmap_args /* {
    899 		struct vnode *a_vp;
    900 		daddr_t  a_bn;
    901 		struct vnode **a_vpp;
    902 		daddr_t *a_bnp;
    903 		int *a_runp;
    904 	} */ *ap = v;
    905 	struct vnode *vp;
    906 
    907 	ap->a_vp = vp = LAYERVPTOLOWERVP(ap->a_vp);
    908 
    909 	return (VCALL(vp, ap->a_desc->vdesc_offset, ap));
    910 }
    911 
    912 int
    913 layer_print(v)
    914 	void *v;
    915 {
    916 	struct vop_print_args /* {
    917 		struct vnode *a_vp;
    918 	} */ *ap = v;
    919 	struct vnode *vp = ap->a_vp;
    920 	printf ("\ttag VT_LAYERFS, vp=%p, lowervp=%p\n", vp, LAYERVPTOLOWERVP(vp));
    921 	return (0);
    922 }
    923 
    924 /*
    925  * XXX - vop_bwrite must be hand coded because it has no
    926  * vnode in its arguments.
    927  * This goes away with a merged VM/buffer cache.
    928  */
    929 int
    930 layer_bwrite(v)
    931 	void *v;
    932 {
    933 	struct vop_bwrite_args /* {
    934 		struct buf *a_bp;
    935 	} */ *ap = v;
    936 	struct buf *bp = ap->a_bp;
    937 	int error;
    938 	struct vnode *savedvp;
    939 
    940 	savedvp = bp->b_vp;
    941 	bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp);
    942 
    943 	error = VOP_BWRITE(bp);
    944 
    945 	bp->b_vp = savedvp;
    946 
    947 	return (error);
    948 }
    949 
    950 int
    951 layer_getpages(v)
    952 	void *v;
    953 {
    954 	struct vop_getpages_args /* {
    955 		struct vnode *a_vp;
    956 		voff_t a_offset;
    957 		struct vm_page **a_m;
    958 		int *a_count;
    959 		int a_centeridx;
    960 		vm_prot_t a_access_type;
    961 		int a_advice;
    962 		int a_flags;
    963 	} */ *ap = v;
    964 	struct vnode *vp = ap->a_vp;
    965 	int error;
    966 
    967 	/*
    968 	 * just pass the request on to the underlying layer.
    969 	 */
    970 
    971 	if (ap->a_flags & PGO_LOCKED) {
    972 		return EBUSY;
    973 	}
    974 	ap->a_vp = LAYERVPTOLOWERVP(vp);
    975 	simple_unlock(&vp->v_interlock);
    976 	simple_lock(&ap->a_vp->v_interlock);
    977 	error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
    978 	return error;
    979 }
    980 
    981 int
    982 layer_putpages(v)
    983 	void *v;
    984 {
    985 	struct vop_putpages_args /* {
    986 		struct vnode *a_vp;
    987 		voff_t a_offlo;
    988 		voff_t a_offhi;
    989 		int a_flags;
    990 	} */ *ap = v;
    991 	struct vnode *vp = ap->a_vp;
    992 	int error;
    993 
    994 	/*
    995 	 * just pass the request on to the underlying layer.
    996 	 */
    997 
    998 	ap->a_vp = LAYERVPTOLOWERVP(vp);
    999 	simple_unlock(&vp->v_interlock);
   1000 	if (ap->a_flags & PGO_RECLAIM) {
   1001 		return 0;
   1002 	}
   1003 	simple_lock(&ap->a_vp->v_interlock);
   1004 	error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
   1005 	return error;
   1006 }
   1007