Home | History | Annotate | Line # | Download | only in nullfs
null_vnops.c revision 1.10
      1  1.10        pk /*	$NetBSD: null_vnops.c,v 1.10 1997/05/17 20:32:53 pk Exp $	*/
      2   1.2       cgd 
      3   1.1   mycroft /*
      4   1.1   mycroft  * Copyright (c) 1992, 1993
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * This code is derived from software contributed to Berkeley by
      8   1.1   mycroft  * John Heidemann of the UCLA Ficus project.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     19   1.1   mycroft  *    must display the following acknowledgement:
     20   1.1   mycroft  *	This product includes software developed by the University of
     21   1.1   mycroft  *	California, Berkeley and its contributors.
     22   1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     23   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     24   1.1   mycroft  *    without specific prior written permission.
     25   1.1   mycroft  *
     26   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1   mycroft  * SUCH DAMAGE.
     37   1.1   mycroft  *
     38   1.2       cgd  *	@(#)null_vnops.c	8.1 (Berkeley) 6/10/93
     39   1.1   mycroft  *
     40   1.1   mycroft  * Ancestors:
     41   1.1   mycroft  *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
     42   1.1   mycroft  *	Id: lofs_vnops.c,v 1.11 1992/05/30 10:05:43 jsp Exp
     43   1.1   mycroft  *	...and...
     44   1.1   mycroft  *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
     45   1.1   mycroft  */
     46   1.1   mycroft 
     47   1.1   mycroft /*
     48   1.1   mycroft  * Null Layer
     49   1.1   mycroft  *
     50   1.1   mycroft  * (See mount_null(8) for more information.)
     51   1.1   mycroft  *
     52   1.1   mycroft  * The null layer duplicates a portion of the file system
     53   1.1   mycroft  * name space under a new name.  In this respect, it is
     54   1.1   mycroft  * similar to the loopback file system.  It differs from
     55   1.1   mycroft  * the loopback fs in two respects:  it is implemented using
     56   1.1   mycroft  * a stackable layers techniques, and it's "null-node"s stack above
     57   1.1   mycroft  * all lower-layer vnodes, not just over directory vnodes.
     58   1.1   mycroft  *
     59   1.1   mycroft  * The null layer has two purposes.  First, it serves as a demonstration
     60   1.1   mycroft  * of layering by proving a layer which does nothing.  (It actually
     61   1.1   mycroft  * does everything the loopback file system does, which is slightly
     62   1.1   mycroft  * more than nothing.)  Second, the null layer can serve as a prototype
     63   1.1   mycroft  * layer.  Since it provides all necessary layer framework,
     64   1.1   mycroft  * new file system layers can be created very easily be starting
     65   1.1   mycroft  * with a null layer.
     66   1.1   mycroft  *
     67   1.1   mycroft  * The remainder of this man page examines the null layer as a basis
     68   1.1   mycroft  * for constructing new layers.
     69   1.1   mycroft  *
     70   1.1   mycroft  *
     71   1.1   mycroft  * INSTANTIATING NEW NULL LAYERS
     72   1.1   mycroft  *
     73   1.1   mycroft  * New null layers are created with mount_null(8).
     74   1.1   mycroft  * Mount_null(8) takes two arguments, the pathname
     75   1.1   mycroft  * of the lower vfs (target-pn) and the pathname where the null
     76   1.1   mycroft  * layer will appear in the namespace (alias-pn).  After
     77   1.1   mycroft  * the null layer is put into place, the contents
     78   1.1   mycroft  * of target-pn subtree will be aliased under alias-pn.
     79   1.1   mycroft  *
     80   1.1   mycroft  *
     81   1.1   mycroft  * OPERATION OF A NULL LAYER
     82   1.1   mycroft  *
     83   1.1   mycroft  * The null layer is the minimum file system layer,
     84   1.1   mycroft  * simply bypassing all possible operations to the lower layer
     85   1.1   mycroft  * for processing there.  The majority of its activity centers
     86   1.1   mycroft  * on the bypass routine, though which nearly all vnode operations
     87   1.1   mycroft  * pass.
     88   1.1   mycroft  *
     89   1.1   mycroft  * The bypass routine accepts arbitrary vnode operations for
     90   1.1   mycroft  * handling by the lower layer.  It begins by examing vnode
     91   1.1   mycroft  * operation arguments and replacing any null-nodes by their
     92   1.1   mycroft  * lower-layer equivlants.  It then invokes the operation
     93   1.1   mycroft  * on the lower layer.  Finally, it replaces the null-nodes
     94   1.1   mycroft  * in the arguments and, if a vnode is return by the operation,
     95   1.1   mycroft  * stacks a null-node on top of the returned vnode.
     96   1.1   mycroft  *
     97   1.1   mycroft  * Although bypass handles most operations,
     98   1.1   mycroft  * vop_getattr, _inactive, _reclaim, and _print are not bypassed.
     99   1.1   mycroft  * Vop_getattr must change the fsid being returned.
    100   1.1   mycroft  * Vop_inactive and vop_reclaim are not bypassed so that
    101   1.1   mycroft  * they can handle freeing null-layer specific data.
    102   1.1   mycroft  * Vop_print is not bypassed to avoid excessive debugging
    103   1.1   mycroft  * information.
    104   1.1   mycroft  *
    105   1.1   mycroft  *
    106   1.1   mycroft  * INSTANTIATING VNODE STACKS
    107   1.1   mycroft  *
    108   1.1   mycroft  * Mounting associates the null layer with a lower layer,
    109   1.1   mycroft  * effect stacking two VFSes.  Vnode stacks are instead
    110   1.1   mycroft  * created on demand as files are accessed.
    111   1.1   mycroft  *
    112   1.1   mycroft  * The initial mount creates a single vnode stack for the
    113   1.1   mycroft  * root of the new null layer.  All other vnode stacks
    114   1.1   mycroft  * are created as a result of vnode operations on
    115   1.1   mycroft  * this or other null vnode stacks.
    116   1.1   mycroft  *
    117   1.1   mycroft  * New vnode stacks come into existance as a result of
    118   1.1   mycroft  * an operation which returns a vnode.
    119   1.1   mycroft  * The bypass routine stacks a null-node above the new
    120   1.1   mycroft  * vnode before returning it to the caller.
    121   1.1   mycroft  *
    122   1.1   mycroft  * For example, imagine mounting a null layer with
    123   1.1   mycroft  * "mount_null /usr/include /dev/layer/null".
    124   1.1   mycroft  * Changing directory to /dev/layer/null will assign
    125   1.1   mycroft  * the root null-node (which was created when the null layer was mounted).
    126   1.1   mycroft  * Now consider opening "sys".  A vop_lookup would be
    127   1.1   mycroft  * done on the root null-node.  This operation would bypass through
    128   1.1   mycroft  * to the lower layer which would return a vnode representing
    129   1.1   mycroft  * the UFS "sys".  Null_bypass then builds a null-node
    130   1.1   mycroft  * aliasing the UFS "sys" and returns this to the caller.
    131   1.1   mycroft  * Later operations on the null-node "sys" will repeat this
    132   1.1   mycroft  * process when constructing other vnode stacks.
    133   1.1   mycroft  *
    134   1.1   mycroft  *
    135   1.1   mycroft  * CREATING OTHER FILE SYSTEM LAYERS
    136   1.1   mycroft  *
    137   1.1   mycroft  * One of the easiest ways to construct new file system layers is to make
    138   1.1   mycroft  * a copy of the null layer, rename all files and variables, and
    139   1.1   mycroft  * then begin modifing the copy.  Sed can be used to easily rename
    140   1.1   mycroft  * all variables.
    141   1.1   mycroft  *
    142   1.1   mycroft  * The umap layer is an example of a layer descended from the
    143   1.1   mycroft  * null layer.
    144   1.1   mycroft  *
    145   1.1   mycroft  *
    146   1.1   mycroft  * INVOKING OPERATIONS ON LOWER LAYERS
    147   1.1   mycroft  *
    148   1.1   mycroft  * There are two techniques to invoke operations on a lower layer
    149   1.1   mycroft  * when the operation cannot be completely bypassed.  Each method
    150   1.1   mycroft  * is appropriate in different situations.  In both cases,
    151   1.1   mycroft  * it is the responsibility of the aliasing layer to make
    152   1.1   mycroft  * the operation arguments "correct" for the lower layer
    153   1.1   mycroft  * by mapping an vnode arguments to the lower layer.
    154   1.1   mycroft  *
    155   1.1   mycroft  * The first approach is to call the aliasing layer's bypass routine.
    156   1.1   mycroft  * This method is most suitable when you wish to invoke the operation
    157   1.1   mycroft  * currently being hanldled on the lower layer.  It has the advantage
    158   1.1   mycroft  * that the bypass routine already must do argument mapping.
    159   1.1   mycroft  * An example of this is null_getattrs in the null layer.
    160   1.1   mycroft  *
    161   1.1   mycroft  * A second approach is to directly invoked vnode operations on
    162   1.1   mycroft  * the lower layer with the VOP_OPERATIONNAME interface.
    163   1.1   mycroft  * The advantage of this method is that it is easy to invoke
    164   1.1   mycroft  * arbitrary operations on the lower layer.  The disadvantage
    165   1.1   mycroft  * is that vnodes arguments must be manualy mapped.
    166   1.1   mycroft  *
    167   1.1   mycroft  */
    168   1.1   mycroft 
    169   1.1   mycroft #include <sys/param.h>
    170   1.1   mycroft #include <sys/systm.h>
    171   1.1   mycroft #include <sys/proc.h>
    172   1.1   mycroft #include <sys/time.h>
    173   1.1   mycroft #include <sys/types.h>
    174   1.1   mycroft #include <sys/vnode.h>
    175   1.1   mycroft #include <sys/mount.h>
    176   1.1   mycroft #include <sys/namei.h>
    177   1.1   mycroft #include <sys/malloc.h>
    178   1.1   mycroft #include <sys/buf.h>
    179   1.1   mycroft #include <miscfs/nullfs/null.h>
    180   1.1   mycroft 
    181   1.1   mycroft 
    182   1.1   mycroft int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
    183   1.1   mycroft 
    184   1.5  christos int	null_bypass __P((void *));
    185   1.5  christos int	null_getattr __P((void *));
    186   1.5  christos int	null_inactive __P((void *));
    187   1.5  christos int	null_reclaim __P((void *));
    188   1.5  christos int	null_print __P((void *));
    189   1.5  christos int	null_strategy __P((void *));
    190   1.5  christos int	null_bwrite __P((void *));
    191   1.7       jtk int	null_lock __P((void *));
    192   1.7       jtk int	null_unlock __P((void *));
    193   1.7       jtk int	null_islocked __P((void *));
    194   1.7       jtk int	null_lookup __P((void *));
    195   1.7       jtk 
    196   1.1   mycroft /*
    197   1.1   mycroft  * This is the 10-Apr-92 bypass routine.
    198   1.1   mycroft  *    This version has been optimized for speed, throwing away some
    199   1.1   mycroft  * safety checks.  It should still always work, but it's not as
    200   1.1   mycroft  * robust to programmer errors.
    201   1.1   mycroft  *    Define SAFETY to include some error checking code.
    202   1.1   mycroft  *
    203   1.1   mycroft  * In general, we map all vnodes going down and unmap them on the way back.
    204   1.1   mycroft  * As an exception to this, vnodes can be marked "unmapped" by setting
    205   1.1   mycroft  * the Nth bit in operation's vdesc_flags.
    206   1.1   mycroft  *
    207   1.1   mycroft  * Also, some BSD vnode operations have the side effect of vrele'ing
    208   1.1   mycroft  * their arguments.  With stacking, the reference counts are held
    209   1.1   mycroft  * by the upper node, not the lower one, so we must handle these
    210   1.1   mycroft  * side-effects here.  This is not of concern in Sun-derived systems
    211   1.1   mycroft  * since there are no such side-effects.
    212   1.1   mycroft  *
    213   1.1   mycroft  * This makes the following assumptions:
    214   1.1   mycroft  * - only one returned vpp
    215   1.1   mycroft  * - no INOUT vpp's (Sun's vop_open has one of these)
    216   1.1   mycroft  * - the vnode operation vector of the first vnode should be used
    217   1.1   mycroft  *   to determine what implementation of the op should be invoked
    218   1.1   mycroft  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
    219   1.1   mycroft  *   problems on rmdir'ing mount points and renaming?)
    220   1.1   mycroft  */
    221   1.1   mycroft int
    222   1.5  christos null_bypass(v)
    223   1.5  christos 	void *v;
    224   1.5  christos {
    225   1.1   mycroft 	struct vop_generic_args /* {
    226   1.1   mycroft 		struct vnodeop_desc *a_desc;
    227   1.1   mycroft 		<other random data follows, presumably>
    228   1.5  christos 	} */ *ap = v;
    229   1.1   mycroft 	register struct vnode **this_vp_p;
    230   1.1   mycroft 	int error;
    231   1.1   mycroft 	struct vnode *old_vps[VDESC_MAX_VPS];
    232   1.1   mycroft 	struct vnode **vps_p[VDESC_MAX_VPS];
    233   1.1   mycroft 	struct vnode ***vppp;
    234   1.1   mycroft 	struct vnodeop_desc *descp = ap->a_desc;
    235   1.1   mycroft 	int reles, i;
    236   1.1   mycroft 
    237   1.1   mycroft 	if (null_bug_bypass)
    238   1.9  christos 		printf ("null_bypass: %s\n", descp->vdesc_name);
    239   1.1   mycroft 
    240   1.1   mycroft #ifdef SAFETY
    241   1.1   mycroft 	/*
    242   1.1   mycroft 	 * We require at least one vp.
    243   1.1   mycroft 	 */
    244   1.1   mycroft 	if (descp->vdesc_vp_offsets == NULL ||
    245   1.1   mycroft 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
    246   1.1   mycroft 		panic ("null_bypass: no vp's in map.\n");
    247   1.1   mycroft #endif
    248   1.1   mycroft 
    249   1.1   mycroft 	/*
    250   1.1   mycroft 	 * Map the vnodes going in.
    251   1.1   mycroft 	 * Later, we'll invoke the operation based on
    252   1.1   mycroft 	 * the first mapped vnode's operation vector.
    253   1.1   mycroft 	 */
    254   1.1   mycroft 	reles = descp->vdesc_flags;
    255   1.1   mycroft 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
    256   1.1   mycroft 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
    257   1.1   mycroft 			break;   /* bail out at end of list */
    258   1.1   mycroft 		vps_p[i] = this_vp_p =
    259   1.1   mycroft 			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
    260   1.1   mycroft 		/*
    261   1.1   mycroft 		 * We're not guaranteed that any but the first vnode
    262   1.1   mycroft 		 * are of our type.  Check for and don't map any
    263   1.1   mycroft 		 * that aren't.  (We must always map first vp or vclean fails.)
    264   1.1   mycroft 		 */
    265   1.3   mycroft 		if (i && (*this_vp_p == NULLVP ||
    266   1.3   mycroft 		    (*this_vp_p)->v_op != null_vnodeop_p)) {
    267   1.3   mycroft 			old_vps[i] = NULLVP;
    268   1.1   mycroft 		} else {
    269   1.1   mycroft 			old_vps[i] = *this_vp_p;
    270   1.1   mycroft 			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
    271   1.1   mycroft 			/*
    272   1.1   mycroft 			 * XXX - Several operations have the side effect
    273   1.1   mycroft 			 * of vrele'ing their vp's.  We must account for
    274   1.1   mycroft 			 * that.  (This should go away in the future.)
    275   1.1   mycroft 			 */
    276   1.1   mycroft 			if (reles & 1)
    277   1.1   mycroft 				VREF(*this_vp_p);
    278   1.1   mycroft 		}
    279   1.1   mycroft 
    280   1.1   mycroft 	}
    281   1.1   mycroft 
    282   1.1   mycroft 	/*
    283   1.1   mycroft 	 * Call the operation on the lower layer
    284   1.1   mycroft 	 * with the modified argument structure.
    285   1.1   mycroft 	 */
    286   1.1   mycroft 	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
    287   1.1   mycroft 
    288   1.1   mycroft 	/*
    289   1.1   mycroft 	 * Maintain the illusion of call-by-value
    290   1.1   mycroft 	 * by restoring vnodes in the argument structure
    291   1.1   mycroft 	 * to their original value.
    292   1.1   mycroft 	 */
    293   1.1   mycroft 	reles = descp->vdesc_flags;
    294   1.1   mycroft 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
    295   1.1   mycroft 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
    296   1.1   mycroft 			break;   /* bail out at end of list */
    297   1.3   mycroft 		if (old_vps[i] != NULLVP) {
    298   1.1   mycroft 			*(vps_p[i]) = old_vps[i];
    299   1.7       jtk 			if (reles & 1) {
    300   1.7       jtk 				/* they really vput them, so we must drop
    301   1.7       jtk 				   our locks (but mark underneath as
    302   1.7       jtk 				   unlocked first).
    303   1.7       jtk 				   Beware of vnode duplication--put it once,
    304   1.7       jtk 				   and rele the rest.  Check this
    305   1.7       jtk 				   by looking at our upper flag. */
    306   1.7       jtk 			    if (VTONULL(*(vps_p[i]))->null_flags & NULL_LOCKED) {
    307   1.7       jtk 				    VTONULL(*(vps_p[i]))->null_flags &= ~NULL_LLOCK;
    308   1.7       jtk 				    vput(*(vps_p[i]));
    309   1.7       jtk 			    } else
    310   1.7       jtk 				    vrele(*(vps_p[i]));
    311   1.7       jtk 			}
    312   1.1   mycroft 		}
    313   1.1   mycroft 	}
    314   1.1   mycroft 
    315   1.1   mycroft 	/*
    316   1.1   mycroft 	 * Map the possible out-going vpp
    317   1.1   mycroft 	 * (Assumes that the lower layer always returns
    318   1.1   mycroft 	 * a VREF'ed vpp unless it gets an error.)
    319   1.1   mycroft 	 */
    320   1.1   mycroft 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
    321   1.1   mycroft 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
    322   1.1   mycroft 	    !error) {
    323   1.1   mycroft 		/*
    324   1.1   mycroft 		 * XXX - even though some ops have vpp returned vp's,
    325   1.1   mycroft 		 * several ops actually vrele this before returning.
    326   1.1   mycroft 		 * We must avoid these ops.
    327   1.1   mycroft 		 * (This should go away when these ops are regularized.)
    328   1.1   mycroft 		 */
    329   1.1   mycroft 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
    330   1.1   mycroft 			goto out;
    331   1.1   mycroft 		vppp = VOPARG_OFFSETTO(struct vnode***,
    332   1.1   mycroft 				 descp->vdesc_vpp_offset,ap);
    333   1.7       jtk 		/*
    334   1.7       jtk 		 * This assumes that **vppp is a locked vnode (it is always
    335   1.7       jtk 		 * so as of this writing, NetBSD-current 1995/02/16)
    336   1.7       jtk 		 */
    337   1.7       jtk 		/*
    338   1.7       jtk 		 * (don't want to lock it if being called on behalf
    339   1.7       jtk 		 * of lookup--it plays weird locking games depending
    340   1.7       jtk 		 * on whether or not it's looking up ".", "..", etc.
    341   1.7       jtk 		 */
    342   1.7       jtk 		error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp,
    343   1.7       jtk 					 descp == &vop_lookup_desc ? 0 : 1);
    344   1.1   mycroft 	}
    345   1.1   mycroft 
    346   1.1   mycroft  out:
    347   1.1   mycroft 	return (error);
    348   1.1   mycroft }
    349   1.1   mycroft 
    350   1.1   mycroft 
    351   1.1   mycroft /*
    352   1.1   mycroft  *  We handle getattr only to change the fsid.
    353   1.1   mycroft  */
    354   1.1   mycroft int
    355   1.5  christos null_getattr(v)
    356   1.5  christos 	void *v;
    357   1.5  christos {
    358   1.1   mycroft 	struct vop_getattr_args /* {
    359   1.1   mycroft 		struct vnode *a_vp;
    360   1.1   mycroft 		struct vattr *a_vap;
    361   1.1   mycroft 		struct ucred *a_cred;
    362   1.1   mycroft 		struct proc *a_p;
    363   1.5  christos 	} */ *ap = v;
    364   1.1   mycroft 	int error;
    365  1.10        pk 	if ((error = null_bypass(ap)) != 0)
    366   1.1   mycroft 		return (error);
    367   1.1   mycroft 	/* Requires that arguments be restored. */
    368   1.1   mycroft 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
    369   1.1   mycroft 	return (0);
    370   1.1   mycroft }
    371   1.1   mycroft 
    372   1.1   mycroft 
    373   1.1   mycroft int
    374   1.5  christos null_inactive(v)
    375   1.5  christos 	void *v;
    376   1.1   mycroft {
    377   1.1   mycroft 	/*
    378   1.1   mycroft 	 * Do nothing (and _don't_ bypass).
    379   1.1   mycroft 	 * Wait to vrele lowervp until reclaim,
    380   1.1   mycroft 	 * so that until then our null_node is in the
    381   1.1   mycroft 	 * cache and reusable.
    382   1.1   mycroft 	 *
    383   1.1   mycroft 	 * NEEDSWORK: Someday, consider inactive'ing
    384   1.1   mycroft 	 * the lowervp and then trying to reactivate it
    385   1.1   mycroft 	 * with capabilities (v_id)
    386   1.1   mycroft 	 * like they do in the name lookup cache code.
    387   1.1   mycroft 	 * That's too much work for now.
    388   1.1   mycroft 	 */
    389   1.1   mycroft 	return (0);
    390   1.1   mycroft }
    391   1.1   mycroft 
    392   1.1   mycroft int
    393   1.5  christos null_reclaim(v)
    394   1.5  christos 	void *v;
    395   1.5  christos {
    396   1.1   mycroft 	struct vop_reclaim_args /* {
    397   1.1   mycroft 		struct vnode *a_vp;
    398   1.5  christos 	} */ *ap = v;
    399   1.1   mycroft 	struct vnode *vp = ap->a_vp;
    400   1.1   mycroft 	struct null_node *xp = VTONULL(vp);
    401   1.1   mycroft 	struct vnode *lowervp = xp->null_lowervp;
    402   1.1   mycroft 
    403   1.1   mycroft 	/*
    404   1.1   mycroft 	 * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p,
    405   1.1   mycroft 	 * so we can't call VOPs on ourself.
    406   1.1   mycroft 	 */
    407   1.1   mycroft 	/* After this assignment, this node will not be re-used. */
    408   1.1   mycroft 	xp->null_lowervp = NULL;
    409   1.4   mycroft 	LIST_REMOVE(xp, null_hash);
    410   1.1   mycroft 	FREE(vp->v_data, M_TEMP);
    411   1.1   mycroft 	vp->v_data = NULL;
    412   1.1   mycroft 	vrele (lowervp);
    413   1.1   mycroft 	return (0);
    414   1.1   mycroft }
    415   1.1   mycroft 
    416   1.1   mycroft 
    417   1.1   mycroft int
    418   1.5  christos null_print(v)
    419   1.5  christos 	void *v;
    420   1.5  christos {
    421   1.1   mycroft 	struct vop_print_args /* {
    422   1.1   mycroft 		struct vnode *a_vp;
    423   1.5  christos 	} */ *ap = v;
    424   1.1   mycroft 	register struct vnode *vp = ap->a_vp;
    425   1.7       jtk 	register struct null_node *nn = VTONULL(vp);
    426   1.7       jtk 
    427   1.9  christos 	printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
    428   1.7       jtk #ifdef DIAGNOSTIC
    429   1.9  christos 	printf("%s%s owner pid %d retpc %p retret %p\n",
    430   1.8  christos 	    (nn->null_flags & NULL_LOCKED) ? "(LOCKED) " : "",
    431   1.8  christos 	    (nn->null_flags & NULL_LLOCK) ? "(LLOCK) " : "",
    432   1.8  christos 	    nn->null_pid, nn->null_lockpc, nn->null_lockpc2);
    433   1.7       jtk #else
    434   1.9  christos 	printf("%s%s\n",
    435   1.8  christos 	    (nn->null_flags & NULL_LOCKED) ? "(LOCKED) " : "",
    436   1.8  christos 	    (nn->null_flags & NULL_LLOCK) ? "(LLOCK) " : "");
    437   1.7       jtk #endif
    438   1.7       jtk 	vprint("nullfs lowervp", NULLVPTOLOWERVP(vp));
    439   1.1   mycroft 	return (0);
    440   1.1   mycroft }
    441   1.1   mycroft 
    442   1.1   mycroft 
    443   1.1   mycroft /*
    444   1.1   mycroft  * XXX - vop_strategy must be hand coded because it has no
    445   1.1   mycroft  * vnode in its arguments.
    446   1.1   mycroft  * This goes away with a merged VM/buffer cache.
    447   1.1   mycroft  */
    448   1.1   mycroft int
    449   1.5  christos null_strategy(v)
    450   1.5  christos 	void *v;
    451   1.5  christos {
    452   1.1   mycroft 	struct vop_strategy_args /* {
    453   1.1   mycroft 		struct buf *a_bp;
    454   1.5  christos 	} */ *ap = v;
    455   1.1   mycroft 	struct buf *bp = ap->a_bp;
    456   1.1   mycroft 	int error;
    457   1.1   mycroft 	struct vnode *savedvp;
    458   1.1   mycroft 
    459   1.1   mycroft 	savedvp = bp->b_vp;
    460   1.1   mycroft 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
    461   1.1   mycroft 
    462   1.1   mycroft 	error = VOP_STRATEGY(bp);
    463   1.1   mycroft 
    464   1.1   mycroft 	bp->b_vp = savedvp;
    465   1.1   mycroft 
    466   1.1   mycroft 	return (error);
    467   1.1   mycroft }
    468   1.1   mycroft 
    469   1.1   mycroft 
    470   1.1   mycroft /*
    471   1.1   mycroft  * XXX - like vop_strategy, vop_bwrite must be hand coded because it has no
    472   1.1   mycroft  * vnode in its arguments.
    473   1.1   mycroft  * This goes away with a merged VM/buffer cache.
    474   1.1   mycroft  */
    475   1.1   mycroft int
    476   1.5  christos null_bwrite(v)
    477   1.5  christos 	void *v;
    478   1.5  christos {
    479   1.1   mycroft 	struct vop_bwrite_args /* {
    480   1.1   mycroft 		struct buf *a_bp;
    481   1.5  christos 	} */ *ap = v;
    482   1.1   mycroft 	struct buf *bp = ap->a_bp;
    483   1.1   mycroft 	int error;
    484   1.1   mycroft 	struct vnode *savedvp;
    485   1.1   mycroft 
    486   1.1   mycroft 	savedvp = bp->b_vp;
    487   1.1   mycroft 	bp->b_vp = NULLVPTOLOWERVP(bp->b_vp);
    488   1.1   mycroft 
    489   1.1   mycroft 	error = VOP_BWRITE(bp);
    490   1.1   mycroft 
    491   1.1   mycroft 	bp->b_vp = savedvp;
    492   1.1   mycroft 
    493   1.1   mycroft 	return (error);
    494   1.1   mycroft }
    495   1.1   mycroft 
    496   1.1   mycroft /*
    497   1.7       jtk  * We need a separate null lock routine, to avoid deadlocks at reclaim time.
    498   1.7       jtk  * If a process holds the lower-vnode locked when it tries to reclaim
    499   1.7       jtk  * the null upper-vnode, _and_ null_bypass is used as the locking operation,
    500   1.7       jtk  * then a process can end up locking against itself.
    501   1.7       jtk  * This has been observed when a null mount is set up to "tunnel" beneath a
    502   1.7       jtk  * union mount (that setup is useful if you still wish to be able to access
    503   1.7       jtk  * the non-union version of either the above or below union layer)
    504   1.7       jtk  */
    505   1.7       jtk int
    506   1.7       jtk null_lock(v)
    507   1.7       jtk 	void *v;
    508   1.7       jtk {
    509   1.7       jtk 	struct vop_lock_args *ap = v;
    510   1.7       jtk 	struct vnode *vp = ap->a_vp;
    511   1.7       jtk 	struct null_node *nn;
    512   1.7       jtk 
    513   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    514   1.7       jtk 	vprint("null_lock_e", ap->a_vp);
    515   1.9  christos 	printf("retpc=%lx, retretpc=%lx\n", RETURN_PC(0), RETURN_PC(1));
    516   1.7       jtk #endif
    517   1.7       jtk start:
    518   1.7       jtk 	while (vp->v_flag & VXLOCK) {
    519   1.7       jtk 		vp->v_flag |= VXWANT;
    520   1.7       jtk 		tsleep((caddr_t)vp, PINOD, "nulllock1", 0);
    521   1.7       jtk 	}
    522   1.7       jtk 
    523   1.7       jtk 	nn = VTONULL(vp);
    524   1.7       jtk 
    525   1.7       jtk 	if ((nn->null_flags & NULL_LLOCK) == 0 &&
    526   1.7       jtk 	    (vp->v_usecount != 0)) {
    527   1.7       jtk 		/*
    528   1.7       jtk 		 * only lock underlying node if we haven't locked it yet
    529   1.7       jtk 		 * for null ops, and our refcount is nonzero.  If usecount
    530   1.7       jtk 		 * is zero, we are probably being reclaimed so we need to
    531   1.7       jtk 		 * keep our hands off the lower node.
    532   1.7       jtk 		 */
    533   1.7       jtk 		VOP_LOCK(nn->null_lowervp);
    534   1.7       jtk 		nn->null_flags |= NULL_LLOCK;
    535   1.7       jtk 	}
    536   1.7       jtk 
    537   1.7       jtk 	if (nn->null_flags & NULL_LOCKED) {
    538   1.7       jtk #ifdef DIAGNOSTIC
    539   1.7       jtk 		if (curproc && nn->null_pid == curproc->p_pid &&
    540   1.7       jtk 		    nn->null_pid > -1 && curproc->p_pid > -1) {
    541   1.7       jtk 			vprint("self-lock", vp);
    542   1.7       jtk 			panic("null: locking against myself");
    543   1.7       jtk 		}
    544   1.7       jtk #endif
    545   1.7       jtk 		nn->null_flags |= NULL_WANTED;
    546   1.7       jtk 		tsleep((caddr_t)nn, PINOD, "nulllock2", 0);
    547   1.7       jtk 		goto start;
    548   1.7       jtk 	}
    549   1.7       jtk 
    550   1.7       jtk #ifdef DIAGNOSTIC
    551   1.7       jtk 	if (curproc)
    552   1.7       jtk 		nn->null_pid = curproc->p_pid;
    553   1.7       jtk 	else
    554   1.7       jtk 		nn->null_pid = -1;
    555   1.7       jtk 	nn->null_lockpc = RETURN_PC(0);
    556   1.7       jtk 	nn->null_lockpc2 = RETURN_PC(1);
    557   1.7       jtk #endif
    558   1.7       jtk 
    559   1.7       jtk 	nn->null_flags |= NULL_LOCKED;
    560   1.7       jtk 	return (0);
    561   1.7       jtk }
    562   1.7       jtk 
    563   1.7       jtk int
    564   1.7       jtk null_unlock(v)
    565   1.7       jtk 	void *v;
    566   1.7       jtk {
    567   1.7       jtk 	struct vop_lock_args *ap = v;
    568   1.7       jtk 	struct null_node *nn = VTONULL(ap->a_vp);
    569   1.7       jtk 
    570   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    571   1.7       jtk 	vprint("null_unlock_e", ap->a_vp);
    572   1.7       jtk #endif
    573   1.7       jtk #ifdef DIAGNOSTIC
    574   1.7       jtk 	if ((nn->null_flags & NULL_LOCKED) == 0) {
    575   1.7       jtk 		vprint("null_unlock", ap->a_vp);
    576   1.7       jtk 		panic("null: unlocking unlocked node");
    577   1.7       jtk 	}
    578   1.7       jtk 	if (curproc && nn->null_pid != curproc->p_pid &&
    579   1.7       jtk 	    curproc->p_pid > -1 && nn->null_pid > -1) {
    580   1.7       jtk 		vprint("null_unlock", ap->a_vp);
    581   1.7       jtk 		panic("null: unlocking other process's null node");
    582   1.7       jtk 	}
    583   1.7       jtk #endif
    584   1.7       jtk 	nn->null_flags &= ~NULL_LOCKED;
    585   1.7       jtk 
    586   1.7       jtk 	if ((nn->null_flags & NULL_LLOCK) != 0)
    587   1.7       jtk 		VOP_UNLOCK(nn->null_lowervp);
    588   1.7       jtk 
    589   1.7       jtk 	nn->null_flags &= ~NULL_LLOCK;
    590   1.7       jtk 
    591   1.7       jtk 	if (nn->null_flags & NULL_WANTED) {
    592   1.7       jtk 		nn->null_flags &= ~NULL_WANTED;
    593   1.7       jtk 		wakeup((caddr_t)nn);
    594   1.7       jtk 	}
    595   1.7       jtk #ifdef DIAGNOSTIC
    596   1.7       jtk 	nn->null_pid = 0;
    597   1.7       jtk 	nn->null_lockpc = nn->null_lockpc2 = 0;
    598   1.7       jtk #endif
    599   1.7       jtk 	return (0);
    600   1.7       jtk }
    601   1.7       jtk 
    602   1.7       jtk int
    603   1.7       jtk null_islocked(v)
    604   1.7       jtk 	void *v;
    605   1.7       jtk {
    606   1.7       jtk 	struct vop_islocked_args *ap = v;
    607   1.7       jtk 	return ((VTONULL(ap->a_vp)->null_flags & NULL_LOCKED) ? 1 : 0);
    608   1.7       jtk }
    609   1.7       jtk 
    610   1.7       jtk int
    611   1.7       jtk null_lookup(v)
    612   1.7       jtk 	void *v;
    613   1.7       jtk {
    614   1.7       jtk 	register struct vop_lookup_args /* {
    615   1.7       jtk 		struct vnodeop_desc *a_desc;
    616   1.7       jtk 		struct vnode *a_dvp;
    617   1.7       jtk 		struct vnode **a_vpp;
    618   1.7       jtk 		struct componentname *a_cnp;
    619   1.7       jtk 	} */ *ap = v;
    620   1.7       jtk 	register int error;
    621   1.7       jtk 	register struct vnode *dvp;
    622   1.7       jtk 	int flags = ap->a_cnp->cn_flags;
    623   1.7       jtk 
    624   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    625   1.9  christos 	printf("null_lookup: dvp=%lx, name='%s'\n",
    626   1.8  christos 	    ap->a_dvp, ap->a_cnp->cn_nameptr);
    627   1.7       jtk #endif
    628   1.7       jtk 	/*
    629   1.7       jtk 	 * the starting dir (ap->a_dvp) comes in locked.
    630   1.7       jtk 	 */
    631   1.7       jtk 
    632   1.7       jtk 	/* set LOCKPARENT to hold on to it until done below */
    633   1.7       jtk 	ap->a_cnp->cn_flags |= LOCKPARENT;
    634   1.7       jtk 	error = null_bypass(ap);
    635   1.7       jtk 	if (!(flags & LOCKPARENT))
    636   1.7       jtk 		ap->a_cnp->cn_flags &= ~LOCKPARENT;
    637   1.7       jtk 
    638   1.7       jtk 	if (error)
    639   1.7       jtk 		/*
    640   1.7       jtk 		 * starting dir is still locked/has been relocked
    641   1.7       jtk 		 * on error return.
    642   1.7       jtk 		 */
    643   1.7       jtk 		return error;
    644   1.7       jtk 
    645   1.7       jtk 	if (ap->a_dvp != *ap->a_vpp) {
    646   1.7       jtk 		/*
    647   1.7       jtk 		 * Lookup returns node locked; we mark both lower and
    648   1.7       jtk 		 * upper nodes as locked by setting the lower lock
    649   1.7       jtk 		 * flag (it came back locked), and then call lock to
    650   1.7       jtk 		 * set upper lock flag & record pid, etc.  see
    651   1.7       jtk 		 * null_node_create()
    652   1.7       jtk 		 */
    653   1.7       jtk 		VTONULL(*ap->a_vpp)->null_flags |= NULL_LLOCK;
    654   1.7       jtk 
    655   1.7       jtk 		dvp = ap->a_dvp;
    656   1.7       jtk 		if (flags & ISDOTDOT) {
    657   1.7       jtk 			/*
    658   1.7       jtk 			 * If we're looking up `..' and this isn't the
    659   1.7       jtk 			 * last component, then the starting directory
    660   1.7       jtk 			 * ("parent") is _unlocked_ as a side-effect
    661   1.7       jtk 			 * of lookups.  This is to avoid deadlocks:
    662   1.7       jtk 			 * lock order is always parent, child, so
    663   1.7       jtk 			 * looking up `..'  requires dropping the lock
    664   1.7       jtk 			 * on the starting directory.
    665   1.7       jtk 			 */
    666   1.7       jtk 			/* see ufs_lookup() for hairy ugly locking protocol
    667   1.7       jtk 			   examples */
    668   1.7       jtk 			/*
    669   1.7       jtk 			 * underlying starting dir comes back locked if flags &
    670   1.7       jtk 			 * LOCKPARENT (which we artificially set above) and
    671   1.7       jtk 			 * ISLASTCN.
    672   1.7       jtk 			 */
    673   1.7       jtk 			if (flags & ISLASTCN) {
    674   1.7       jtk 				VTONULL(dvp)->null_flags |= NULL_LLOCK;	/* no-op, right? */
    675   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    676   1.7       jtk 				if (!VOP_ISLOCKED(VTONULL(dvp)->null_lowervp)) {
    677   1.7       jtk 					vprint("lowerdvp not locked after lookup\n", dvp);
    678   1.7       jtk 					panic("null_lookup not locked");
    679   1.7       jtk 				}
    680   1.7       jtk #endif
    681   1.7       jtk 			} else {
    682   1.7       jtk 				VTONULL(dvp)->null_flags &= ~NULL_LLOCK;
    683   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    684   1.7       jtk 				if (VOP_ISLOCKED(VTONULL(dvp)->null_lowervp)) {
    685   1.7       jtk 					vprint("lowerdvp locked after lookup?\n", dvp);
    686   1.7       jtk 					panic("null_lookup locked");
    687   1.7       jtk 				}
    688   1.7       jtk #endif
    689   1.7       jtk 			}
    690   1.7       jtk 			/*
    691   1.7       jtk 			 * locking order: drop lock on lower-in-tree
    692   1.7       jtk 			 * element, then get lock on higher-in-tree
    693   1.7       jtk 			 * element, then (if needed) re-fetch lower
    694   1.7       jtk 			 * lock.  No need for vget() since we hold a
    695   1.7       jtk 			 * refcount to the starting directory
    696   1.7       jtk 			 */
    697   1.7       jtk 			VOP_UNLOCK(dvp);
    698   1.7       jtk 			VOP_LOCK(*ap->a_vpp);
    699   1.7       jtk 			/*
    700   1.7       jtk 			 * we should return our directory locked if
    701   1.7       jtk 			 * (flags & LOCKPARENT) and (flags & ISLASTCN)
    702   1.7       jtk 			 */
    703   1.7       jtk 			if ((flags & LOCKPARENT) && (flags & ISLASTCN))
    704   1.7       jtk 				VOP_LOCK(dvp);
    705   1.7       jtk 		} else {
    706   1.7       jtk 			/*
    707   1.7       jtk 			 * Normal directory locking order: we hold the starting
    708   1.7       jtk 			 * directory locked; now lock our layer of the target.
    709   1.7       jtk 			 */
    710   1.7       jtk 			VOP_LOCK(*ap->a_vpp);
    711   1.7       jtk 			/*
    712   1.7       jtk 			 * underlying starting dir comes back locked
    713   1.7       jtk 			 * if lockparent (we set it) and no error
    714   1.7       jtk 			 * (this leg) and ISLASTCN
    715   1.7       jtk 			 */
    716   1.7       jtk 			if (flags & ISLASTCN) {
    717   1.7       jtk 				VTONULL(dvp)->null_flags |= NULL_LLOCK;	/* no op, right? */
    718   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    719   1.7       jtk 				if (!VOP_ISLOCKED(VTONULL(dvp)->null_lowervp)) {
    720   1.7       jtk 					vprint("lowerdvp not locked after lookup\n", dvp);
    721   1.7       jtk 					panic("null_lookup not locked");
    722   1.7       jtk 				}
    723   1.7       jtk #endif
    724   1.7       jtk 			} else {
    725   1.7       jtk 				VTONULL(dvp)->null_flags &= ~NULL_LLOCK;
    726   1.7       jtk #ifdef NULLFS_DIAGNOSTIC
    727   1.7       jtk 				if (VOP_ISLOCKED(VTONULL(dvp)->null_lowervp)) {
    728   1.7       jtk 					vprint("lowerdvp locked after lookup?\n", dvp);
    729   1.7       jtk 					panic("null_lookup locked");
    730   1.7       jtk 				}
    731   1.7       jtk #endif
    732   1.7       jtk 			}
    733   1.7       jtk 			/*
    734   1.7       jtk 			 * we should return our directory unlocked if
    735   1.7       jtk 			 * our caller didn't want the parent locked,
    736   1.7       jtk 			 * !(flags & LOCKPARENT), or we're not at the
    737   1.7       jtk 			 * end yet, !(flags & ISLASTCN)
    738   1.7       jtk 			 */
    739   1.7       jtk 			if (!(flags & LOCKPARENT) || !(flags & ISLASTCN))
    740   1.7       jtk 				VOP_UNLOCK(dvp);
    741   1.7       jtk 		}
    742   1.7       jtk 	}
    743   1.7       jtk 	return error;
    744   1.7       jtk }
    745   1.7       jtk 
    746   1.7       jtk /*
    747   1.1   mycroft  * Global vfs data structures
    748   1.1   mycroft  */
    749   1.5  christos int (**null_vnodeop_p) __P((void *));
    750   1.1   mycroft struct vnodeopv_entry_desc null_vnodeop_entries[] = {
    751   1.5  christos 	{ &vop_default_desc,	null_bypass },
    752   1.1   mycroft 
    753   1.5  christos 	{ &vop_getattr_desc,	null_getattr },
    754   1.5  christos 	{ &vop_inactive_desc,	null_inactive },
    755   1.5  christos 	{ &vop_reclaim_desc,	null_reclaim },
    756   1.5  christos 	{ &vop_print_desc,	null_print },
    757   1.7       jtk 
    758   1.7       jtk 	{ &vop_lock_desc,	null_lock },
    759   1.7       jtk 	{ &vop_unlock_desc,	null_unlock },
    760   1.7       jtk 	{ &vop_islocked_desc,	null_islocked },
    761   1.7       jtk 	{ &vop_lookup_desc,	null_lookup }, /* special locking frob */
    762   1.1   mycroft 
    763   1.5  christos 	{ &vop_strategy_desc,	null_strategy },
    764   1.5  christos 	{ &vop_bwrite_desc,	null_bwrite },
    765   1.1   mycroft 
    766   1.5  christos 	{ (struct vnodeop_desc*)NULL,	(int(*) __P((void *)))NULL }
    767   1.1   mycroft };
    768   1.1   mycroft struct vnodeopv_desc null_vnodeop_opv_desc =
    769   1.1   mycroft 	{ &null_vnodeop_p, null_vnodeop_entries };
    770