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