Home | History | Annotate | Line # | Download | only in genfs
layer_vnops.c revision 1.8
      1 /*	$NetBSD: layer_vnops.c,v 1.8 2001/11/10 13:33:42 lukem 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. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
     71  *
     72  * Ancestors:
     73  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
     74  *	$Id: layer_vnops.c,v 1.8 2001/11/10 13:33:42 lukem Exp $
     75  *	...and...
     76  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
     77  */
     78 
     79 /*
     80  * Null Layer vnode routines.
     81  *
     82  * (See mount_null(8) for more information.)
     83  *
     84  * The layer.h, layer_extern.h, layer_vfs.c, and layer_vnops.c files provide
     85  * the core implimentation of the null file system and most other stacked
     86  * fs's. The description below refers to the null file system, but the
     87  * services provided by the layer* files are useful for all layered fs's.
     88  *
     89  * The null layer duplicates a portion of the file system
     90  * name space under a new name.  In this respect, it is
     91  * similar to the loopback file system.  It differs from
     92  * the loopback fs in two respects:  it is implemented using
     93  * a stackable layers techniques, and it's "null-node"s stack above
     94  * all lower-layer vnodes, not just over directory vnodes.
     95  *
     96  * The null layer has two purposes.  First, it serves as a demonstration
     97  * of layering by proving a layer which does nothing.  (It actually
     98  * does everything the loopback file system does, which is slightly
     99  * more than nothing.)  Second, the null layer can serve as a prototype
    100  * layer.  Since it provides all necessary layer framework,
    101  * new file system layers can be created very easily be starting
    102  * with a null layer.
    103  *
    104  * The remainder of the man page examines the null layer as a basis
    105  * for constructing new layers.
    106  *
    107  *
    108  * INSTANTIATING NEW NULL LAYERS
    109  *
    110  * New null layers are created with mount_null(8).
    111  * Mount_null(8) takes two arguments, the pathname
    112  * of the lower vfs (target-pn) and the pathname where the null
    113  * layer will appear in the namespace (alias-pn).  After
    114  * the null layer is put into place, the contents
    115  * of target-pn subtree will be aliased under alias-pn.
    116  *
    117  * It is conceivable that other overlay filesystems will take different
    118  * parameters. For instance, data migration or access controll layers might
    119  * only take one pathname which will serve both as the target-pn and
    120  * alias-pn described above.
    121  *
    122  *
    123  * OPERATION OF A NULL LAYER
    124  *
    125  * The null layer is the minimum file system layer,
    126  * simply bypassing all possible operations to the lower layer
    127  * for processing there.  The majority of its activity centers
    128  * on the bypass routine, though which nearly all vnode operations
    129  * pass.
    130  *
    131  * The bypass routine accepts arbitrary vnode operations for
    132  * handling by the lower layer.  It begins by examing vnode
    133  * operation arguments and replacing any layered nodes by their
    134  * lower-layer equivlants.  It then invokes the operation
    135  * on the lower layer.  Finally, it replaces the layered nodes
    136  * in the arguments and, if a vnode is return by the operation,
    137  * stacks a layered node on top of the returned vnode.
    138  *
    139  * The bypass routine in this file, layer_bypass(), is suitable for use
    140  * by many different layered filesystems. It can be used by multiple
    141  * filesystems simultaneously. Alternatively, a layered fs may provide
    142  * its own bypass routine, in which case layer_bypass() should be used as
    143  * a model. For instance, the main functionality provided by umapfs, the user
    144  * identity mapping file system, is handled by a custom bypass routine.
    145  *
    146  * Typically a layered fs registers its selected bypass routine as the
    147  * default vnode operation in its vnodeopv_entry_desc table. Additionally
    148  * the filesystem must store the bypass entry point in the layerm_bypass
    149  * field of struct layer_mount. All other layer routines in this file will
    150  * use the layerm_bypass routine.
    151  *
    152  * Although the bypass routine handles most operations outright, a number
    153  * of operations are special cased, and handled by the layered fs. One
    154  * group, layer_setattr, layer_getattr, layer_access, layer_open, and
    155  * layer_fsync, perform layer-specific manipulation in addition to calling
    156  * the bypass routine. The other group
    157 
    158  * Although bypass handles most operations, vop_getattr, vop_lock,
    159  * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
    160  * bypassed. Vop_getattr must change the fsid being returned.
    161  * Vop_lock and vop_unlock must handle any locking for the
    162  * current vnode as well as pass the lock request down.
    163  * Vop_inactive and vop_reclaim are not bypassed so that
    164  * they can handle freeing null-layer specific data. Vop_print
    165  * is not bypassed to avoid excessive debugging information.
    166  * Also, certain vnode operations change the locking state within
    167  * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
    168  * and symlink). Ideally these operations should not change the
    169  * lock state, but should be changed to let the caller of the
    170  * function unlock them. Otherwise all intermediate vnode layers
    171  * (such as union, umapfs, etc) must catch these functions to do
    172  * the necessary locking at their layer.
    173  *
    174  *
    175  * INSTANTIATING VNODE STACKS
    176  *
    177  * Mounting associates the null layer with a lower layer,
    178  * effect stacking two VFSes.  Vnode stacks are instead
    179  * created on demand as files are accessed.
    180  *
    181  * The initial mount creates a single vnode stack for the
    182  * root of the new null layer.  All other vnode stacks
    183  * are created as a result of vnode operations on
    184  * this or other null vnode stacks.
    185  *
    186  * New vnode stacks come into existance as a result of
    187  * an operation which returns a vnode.
    188  * The bypass routine stacks a null-node above the new
    189  * vnode before returning it to the caller.
    190  *
    191  * For example, imagine mounting a null layer with
    192  * "mount_null /usr/include /dev/layer/null".
    193  * Changing directory to /dev/layer/null will assign
    194  * the root null-node (which was created when the null layer was mounted).
    195  * Now consider opening "sys".  A vop_lookup would be
    196  * done on the root null-node.  This operation would bypass through
    197  * to the lower layer which would return a vnode representing
    198  * the UFS "sys".  layer_bypass then builds a null-node
    199  * aliasing the UFS "sys" and returns this to the caller.
    200  * Later operations on the null-node "sys" will repeat this
    201  * process when constructing other vnode stacks.
    202  *
    203  *
    204  * CREATING OTHER FILE SYSTEM LAYERS
    205  *
    206  * One of the easiest ways to construct new file system layers is to make
    207  * a copy of the null layer, rename all files and variables, and
    208  * then begin modifing the copy.  Sed can be used to easily rename
    209  * all variables.
    210  *
    211  * The umap layer is an example of a layer descended from the
    212  * null layer.
    213  *
    214  *
    215  * INVOKING OPERATIONS ON LOWER LAYERS
    216  *
    217  * There are two techniques to invoke operations on a lower layer
    218  * when the operation cannot be completely bypassed.  Each method
    219  * is appropriate in different situations.  In both cases,
    220  * it is the responsibility of the aliasing layer to make
    221  * the operation arguments "correct" for the lower layer
    222  * by mapping an vnode arguments to the lower layer.
    223  *
    224  * The first approach is to call the aliasing layer's bypass routine.
    225  * This method is most suitable when you wish to invoke the operation
    226  * currently being hanldled on the lower layer.  It has the advantage
    227  * that the bypass routine already must do argument mapping.
    228  * An example of this is null_getattrs in the null layer.
    229  *
    230  * A second approach is to directly invoked vnode operations on
    231  * the lower layer with the VOP_OPERATIONNAME interface.
    232  * The advantage of this method is that it is easy to invoke
    233  * arbitrary operations on the lower layer.  The disadvantage
    234  * is that vnodes arguments must be manualy mapped.
    235  *
    236  */
    237 
    238 #include <sys/cdefs.h>
    239 __KERNEL_RCSID(0, "$NetBSD: layer_vnops.c,v 1.8 2001/11/10 13:33:42 lukem Exp $");
    240 
    241 #include <sys/param.h>
    242 #include <sys/systm.h>
    243 #include <sys/proc.h>
    244 #include <sys/time.h>
    245 #include <sys/types.h>
    246 #include <sys/vnode.h>
    247 #include <sys/mount.h>
    248 #include <sys/namei.h>
    249 #include <sys/malloc.h>
    250 #include <sys/buf.h>
    251 #include <miscfs/genfs/layer.h>
    252 #include <miscfs/genfs/layer_extern.h>
    253 #include <miscfs/genfs/genfs.h>
    254 
    255 
    256 /*
    257  * This is the 08-June-99 bypass routine, based on the 10-Apr-92 bypass
    258  *		routine by John Heidemann.
    259  *	The new element for this version is that the whole nullfs
    260  * system gained the concept of locks on the lower node, and locks on
    261  * our nodes. When returning from a call to the lower layer, we may
    262  * need to update lock state ONLY on our layer. The LAYERFS_UPPER*LOCK()
    263  * macros provide this functionality.
    264  *    The 10-Apr-92 version was optimized for speed, throwing away some
    265  * safety checks.  It should still always work, but it's not as
    266  * robust to programmer errors.
    267  *    Define SAFETY to include some error checking code.
    268  *
    269  * In general, we map all vnodes going down and unmap them on the way back.
    270  *
    271  * Also, some BSD vnode operations have the side effect of vrele'ing
    272  * their arguments.  With stacking, the reference counts are held
    273  * by the upper node, not the lower one, so we must handle these
    274  * side-effects here.  This is not of concern in Sun-derived systems
    275  * since there are no such side-effects.
    276  *
    277  * New for the 08-June-99 version: we also handle operations which unlock
    278  * the passed-in node (typically they vput the node).
    279  *
    280  * This makes the following assumptions:
    281  * - only one returned vpp
    282  * - no INOUT vpp's (Sun's vop_open has one of these)
    283  * - the vnode operation vector of the first vnode should be used
    284  *   to determine what implementation of the op should be invoked
    285  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
    286  *   problems on rmdir'ing mount points and renaming?)
    287  */
    288 int
    289 layer_bypass(v)
    290 	void *v;
    291 {
    292 	struct vop_generic_args /* {
    293 		struct vnodeop_desc *a_desc;
    294 		<other random data follows, presumably>
    295 	} */ *ap = v;
    296 	int (**our_vnodeop_p) __P((void *));
    297 	struct vnode **this_vp_p;
    298 	int error, error1;
    299 	struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
    300 	struct vnode **vps_p[VDESC_MAX_VPS];
    301 	struct vnode ***vppp;
    302 	struct vnodeop_desc *descp = ap->a_desc;
    303 	int reles, i, flags;
    304 
    305 #ifdef SAFETY
    306 	/*
    307 	 * We require at least one vp.
    308 	 */
    309 	if (descp->vdesc_vp_offsets == NULL ||
    310 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
    311 		panic ("layer_bypass: no vp's in map.\n");
    312 #endif
    313 
    314 	vps_p[0] = VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[0],ap);
    315 	vp0 = *vps_p[0];
    316 	flags = MOUNTTOLAYERMOUNT(vp0->v_mount)->layerm_flags;
    317 	our_vnodeop_p = vp0->v_op;
    318 
    319 	if (flags & LAYERFS_MBYPASSDEBUG)
    320 		printf ("layer_bypass: %s\n", descp->vdesc_name);
    321 
    322 	/*
    323 	 * Map the vnodes going in.
    324 	 * Later, we'll invoke the operation based on
    325 	 * the first mapped vnode's operation vector.
    326 	 */
    327 	reles = descp->vdesc_flags;
    328 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
    329 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
    330 			break;   /* bail out at end of list */
    331 		vps_p[i] = this_vp_p =
    332 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],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_loookup 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 	}
    407 
    408  out:
    409 	return (error);
    410 }
    411 
    412 /*
    413  * We have to carry on the locking protocol on the layer vnodes
    414  * as we progress through the tree. We also have to enforce read-only
    415  * if this layer is mounted read-only.
    416  */
    417 int
    418 layer_lookup(v)
    419 	void *v;
    420 {
    421 	struct vop_lookup_args /* {
    422 		struct vnodeop_desc *a_desc;
    423 		struct vnode * a_dvp;
    424 		struct vnode ** a_vpp;
    425 		struct componentname * a_cnp;
    426 	} */ *ap = v;
    427 	struct componentname *cnp = ap->a_cnp;
    428 	int flags = cnp->cn_flags;
    429 	struct vnode *dvp, *vp, *ldvp;
    430 	int error, r;
    431 
    432 	dvp = ap->a_dvp;
    433 
    434 	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    435 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    436 		return (EROFS);
    437 
    438 	ldvp = LAYERVPTOLOWERVP(dvp);
    439 	ap->a_dvp = ldvp;
    440 	error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
    441 	vp = *ap->a_vpp;
    442 
    443 	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
    444 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    445 	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
    446 		error = EROFS;
    447 	/*
    448 	 * We must do the same locking and unlocking at this layer as
    449 	 * is done in the layers below us. It used to be we would try
    450 	 * to guess based on what was set with the flags and error codes.
    451 	 *
    452 	 * But that doesn't work. So now we have the underlying VOP_LOOKUP
    453 	 * tell us if it released the parent vnode, and we adjust the
    454 	 * upper node accordingly. We can't just look at the lock states
    455 	 * of the lower nodes as someone else might have come along and
    456 	 * locked the parent node after our call to VOP_LOOKUP locked it.
    457 	 */
    458 	if ((cnp->cn_flags & PDIRUNLOCK)) {
    459 		LAYERFS_UPPERUNLOCK(dvp, 0, r);
    460 	}
    461 	if (ldvp == vp) {
    462 		/*
    463 		 * Did lookup on "." or ".." in the root node of a mount point.
    464 		 * So we return dvp after a VREF.
    465 		 */
    466 		*ap->a_vpp = dvp;
    467 		VREF(dvp);
    468 		vrele(vp);
    469 	} else if (vp != NULL) {
    470 		error = layer_node_create(dvp->v_mount, vp, ap->a_vpp);
    471 	}
    472 	return (error);
    473 }
    474 
    475 /*
    476  * Setattr call. Disallow write attempts if the layer is mounted read-only.
    477  */
    478 int
    479 layer_setattr(v)
    480 	void *v;
    481 {
    482 	struct vop_setattr_args /* {
    483 		struct vnodeop_desc *a_desc;
    484 		struct vnode *a_vp;
    485 		struct vattr *a_vap;
    486 		struct ucred *a_cred;
    487 		struct proc *a_p;
    488 	} */ *ap = v;
    489 	struct vnode *vp = ap->a_vp;
    490 	struct vattr *vap = ap->a_vap;
    491 
    492   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
    493 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
    494 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
    495 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
    496 		return (EROFS);
    497 	if (vap->va_size != VNOVAL) {
    498  		switch (vp->v_type) {
    499  		case VDIR:
    500  			return (EISDIR);
    501  		case VCHR:
    502  		case VBLK:
    503  		case VSOCK:
    504  		case VFIFO:
    505 			return (0);
    506 		case VREG:
    507 		case VLNK:
    508  		default:
    509 			/*
    510 			 * Disallow write attempts if the filesystem is
    511 			 * mounted read-only.
    512 			 */
    513 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    514 				return (EROFS);
    515 		}
    516 	}
    517 	return (LAYERFS_DO_BYPASS(vp, ap));
    518 }
    519 
    520 /*
    521  *  We handle getattr only to change the fsid.
    522  */
    523 int
    524 layer_getattr(v)
    525 	void *v;
    526 {
    527 	struct vop_getattr_args /* {
    528 		struct vnode *a_vp;
    529 		struct vattr *a_vap;
    530 		struct ucred *a_cred;
    531 		struct proc *a_p;
    532 	} */ *ap = v;
    533 	struct vnode *vp = ap->a_vp;
    534 	int error;
    535 
    536 	if ((error = LAYERFS_DO_BYPASS(vp, ap)) != 0)
    537 		return (error);
    538 	/* Requires that arguments be restored. */
    539 	ap->a_vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
    540 	return (0);
    541 }
    542 
    543 int
    544 layer_access(v)
    545 	void *v;
    546 {
    547 	struct vop_access_args /* {
    548 		struct vnode *a_vp;
    549 		int  a_mode;
    550 		struct ucred *a_cred;
    551 		struct proc *a_p;
    552 	} */ *ap = v;
    553 	struct vnode *vp = ap->a_vp;
    554 	mode_t mode = ap->a_mode;
    555 
    556 	/*
    557 	 * Disallow write attempts on read-only layers;
    558 	 * unless the file is a socket, fifo, or a block or
    559 	 * character device resident on the file system.
    560 	 */
    561 	if (mode & VWRITE) {
    562 		switch (vp->v_type) {
    563 		case VDIR:
    564 		case VLNK:
    565 		case VREG:
    566 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    567 				return (EROFS);
    568 			break;
    569 		default:
    570 			break;
    571 		}
    572 	}
    573 	return (LAYERFS_DO_BYPASS(vp, ap));
    574 }
    575 
    576 /*
    577  * We must handle open to be able to catch MNT_NODEV and friends.
    578  */
    579 int
    580 layer_open(v)
    581 	void *v;
    582 {
    583 	struct vop_open_args *ap = v;
    584 	struct vnode *vp = ap->a_vp;
    585 	enum vtype lower_type = LAYERVPTOLOWERVP(vp)->v_type;
    586 
    587 	if (((lower_type == VBLK) || (lower_type == VCHR)) &&
    588 	    (vp->v_mount->mnt_flag & MNT_NODEV))
    589 		return ENXIO;
    590 
    591 	return LAYERFS_DO_BYPASS(vp, ap);
    592 }
    593 
    594 /*
    595  * We need to process our own vnode lock and then clear the
    596  * interlock flag as it applies only to our vnode, not the
    597  * vnodes below us on the stack.
    598  */
    599 int
    600 layer_lock(v)
    601 	void *v;
    602 {
    603 	struct vop_lock_args /* {
    604 		struct vnode *a_vp;
    605 		int a_flags;
    606 		struct proc *a_p;
    607 	} */ *ap = v;
    608 	struct vnode *vp = ap->a_vp, *lowervp;
    609 	int	flags = ap->a_flags, error;
    610 
    611 	if (vp->v_vnlock != NULL) {
    612 		/*
    613 		 * The lower level has exported a struct lock to us. Use
    614 		 * it so that all vnodes in the stack lock and unlock
    615 		 * simultaneously. Note: we don't DRAIN the lock as DRAIN
    616 		 * decommissions the lock - just because our vnode is
    617 		 * going away doesn't mean the struct lock below us is.
    618 		 * LK_EXCLUSIVE is fine.
    619 		 */
    620 		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
    621 			return(lockmgr(vp->v_vnlock,
    622 				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
    623 				&vp->v_interlock));
    624 		} else
    625 			return(lockmgr(vp->v_vnlock, flags, &vp->v_interlock));
    626 	} else {
    627 		/*
    628 		 * Ahh well. It would be nice if the fs we're over would
    629 		 * export a struct lock for us to use, but it doesn't.
    630 		 *
    631 		 * To prevent race conditions involving doing a lookup
    632 		 * on "..", we have to lock the lower node, then lock our
    633 		 * node. Most of the time it won't matter that we lock our
    634 		 * node (as any locking would need the lower one locked
    635 		 * first). But we can LK_DRAIN the upper lock as a step
    636 		 * towards decomissioning it.
    637 		 */
    638 		lowervp = LAYERVPTOLOWERVP(vp);
    639 		if (flags & LK_INTERLOCK) {
    640 			simple_unlock(&vp->v_interlock);
    641 			flags &= ~LK_INTERLOCK;
    642 		}
    643 		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
    644 			error = VOP_LOCK(lowervp,
    645 				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE);
    646 		} else
    647 			error = VOP_LOCK(lowervp, flags);
    648 		if (error)
    649 			return (error);
    650 		if ((error = lockmgr(&vp->v_lock, flags, &vp->v_interlock))) {
    651 			VOP_UNLOCK(lowervp, 0);
    652 		}
    653 		return (error);
    654 	}
    655 }
    656 
    657 /*
    658  */
    659 int
    660 layer_unlock(v)
    661 	void *v;
    662 {
    663 	struct vop_unlock_args /* {
    664 		struct vnode *a_vp;
    665 		int a_flags;
    666 		struct proc *a_p;
    667 	} */ *ap = v;
    668 	struct vnode *vp = ap->a_vp;
    669 	int	flags = ap->a_flags;
    670 
    671 	if (vp->v_vnlock != NULL) {
    672 		return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
    673 			&vp->v_interlock));
    674 	} else {
    675 		if (flags & LK_INTERLOCK) {
    676 			simple_unlock(&vp->v_interlock);
    677 			flags &= ~LK_INTERLOCK;
    678 		}
    679 		VOP_UNLOCK(LAYERVPTOLOWERVP(vp), flags);
    680 		return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
    681 			&vp->v_interlock));
    682 	}
    683 }
    684 
    685 /*
    686  * As long as genfs_nolock is in use, don't call VOP_ISLOCKED(lowervp)
    687  * if vp->v_vnlock == NULL as genfs_noislocked will always report 0.
    688  */
    689 int
    690 layer_islocked(v)
    691 	void *v;
    692 {
    693 	struct vop_islocked_args /* {
    694 		struct vnode *a_vp;
    695 	} */ *ap = v;
    696 	struct vnode *vp = ap->a_vp;
    697 
    698 	if (vp->v_vnlock != NULL)
    699 		return (lockstatus(vp->v_vnlock));
    700 	else
    701 		return (lockstatus(&vp->v_lock));
    702 }
    703 
    704 /*
    705  * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother
    706  * syncing the underlying vnodes, since they'll be fsync'ed when
    707  * reclaimed; otherwise,
    708  * pass it through to the underlying layer.
    709  *
    710  * XXX Do we still need to worry about shallow fsync?
    711  */
    712 
    713 int
    714 layer_fsync(v)
    715 	void *v;
    716 {
    717 	struct vop_fsync_args /* {
    718 		struct vnode *a_vp;
    719 		struct ucred *a_cred;
    720 		int  a_flags;
    721 		off_t offlo;
    722 		off_t offhi;
    723 		struct proc *a_p;
    724 	} */ *ap = v;
    725 
    726 	if (ap->a_flags & FSYNC_RECLAIM) {
    727 		return 0;
    728 	}
    729 
    730 	return (LAYERFS_DO_BYPASS(ap->a_vp, ap));
    731 }
    732 
    733 
    734 int
    735 layer_inactive(v)
    736 	void *v;
    737 {
    738 	struct vop_inactive_args /* {
    739 		struct vnode *a_vp;
    740 		struct proc *a_p;
    741 	} */ *ap = v;
    742 	struct vnode *vp = ap->a_vp;
    743 
    744 	/*
    745 	 * Do nothing (and _don't_ bypass).
    746 	 * Wait to vrele lowervp until reclaim,
    747 	 * so that until then our layer_node is in the
    748 	 * cache and reusable.
    749 	 *
    750 	 * NEEDSWORK: Someday, consider inactive'ing
    751 	 * the lowervp and then trying to reactivate it
    752 	 * with capabilities (v_id)
    753 	 * like they do in the name lookup cache code.
    754 	 * That's too much work for now.
    755 	 */
    756 	VOP_UNLOCK(vp, 0);
    757 
    758 	/* ..., but don't cache the device node. */
    759 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    760 		vgone(vp);
    761 	return (0);
    762 }
    763 
    764 int
    765 layer_reclaim(v)
    766 	void *v;
    767 {
    768 	struct vop_reclaim_args /* {
    769 		struct vnode *a_vp;
    770 		struct proc *a_p;
    771 	} */ *ap = v;
    772 	struct vnode *vp = ap->a_vp;
    773 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(vp->v_mount);
    774 	struct layer_node *xp = VTOLAYER(vp);
    775 	struct vnode *lowervp = xp->layer_lowervp;
    776 
    777 	/*
    778 	 * Note: in vop_reclaim, the node's struct lock has been
    779 	 * decomissioned, so we have to be careful about calling
    780 	 * VOP's on ourself. Even if we turned a LK_DRAIN into an
    781 	 * LK_EXCLUSIVE in layer_lock, we still must be careful as VXLOCK is
    782 	 * 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 	simple_lock(&lmp->layerm_hashlock);
    796 	LIST_REMOVE(xp, layer_hash);
    797 	simple_unlock(&lmp->layerm_hashlock);
    798 	FREE(vp->v_data, M_TEMP);
    799 	vp->v_data = NULL;
    800 	vrele (lowervp);
    801 	return (0);
    802 }
    803 
    804 /*
    805  * We just feed the returned vnode up to the caller - there's no need
    806  * to build a layer node on top of the node on which we're going to do
    807  * i/o. :-)
    808  */
    809 int
    810 layer_bmap(v)
    811 	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(v)
    829 	void *v;
    830 {
    831 	struct vop_print_args /* {
    832 		struct vnode *a_vp;
    833 	} */ *ap = v;
    834 	struct vnode *vp = ap->a_vp;
    835 	printf ("\ttag VT_LAYERFS, vp=%p, lowervp=%p\n", vp, LAYERVPTOLOWERVP(vp));
    836 	return (0);
    837 }
    838 
    839 /*
    840  * XXX - vop_strategy must be hand coded because it has no
    841  * vnode in its arguments.
    842  * This goes away with a merged VM/buffer cache.
    843  */
    844 int
    845 layer_strategy(v)
    846 	void *v;
    847 {
    848 	struct vop_strategy_args /* {
    849 		struct buf *a_bp;
    850 	} */ *ap = v;
    851 	struct buf *bp = ap->a_bp;
    852 	int error;
    853 	struct vnode *savedvp;
    854 
    855 	savedvp = bp->b_vp;
    856 	bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp);
    857 
    858 	error = VOP_STRATEGY(bp);
    859 
    860 	bp->b_vp = savedvp;
    861 
    862 	return (error);
    863 }
    864 
    865 /*
    866  * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
    867  * vnode in its arguments.
    868  * This goes away with a merged VM/buffer cache.
    869  */
    870 int
    871 layer_bwrite(v)
    872 	void *v;
    873 {
    874 	struct vop_bwrite_args /* {
    875 		struct buf *a_bp;
    876 	} */ *ap = v;
    877 	struct buf *bp = ap->a_bp;
    878 	int error;
    879 	struct vnode *savedvp;
    880 
    881 	savedvp = bp->b_vp;
    882 	bp->b_vp = LAYERVPTOLOWERVP(bp->b_vp);
    883 
    884 	error = VOP_BWRITE(bp);
    885 
    886 	bp->b_vp = savedvp;
    887 
    888 	return (error);
    889 }
    890