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