Home | History | Annotate | Line # | Download | only in genfs
layer_subr.c revision 1.20
      1 /*	$NetBSD: layer_subr.c,v 1.20 2006/11/25 22:14:38 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 National Aeronautics & Space Administration
      5  * All rights reserved.
      6  *
      7  * This software was written by William Studenmund of the
      8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the National Aeronautics & Space Administration
     19  *    nor the names of its contributors may be used to endorse or promote
     20  *    products derived from this software without specific prior written
     21  *    permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
     27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 /*
     36  * Copyright (c) 1992, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * This code is derived from software donated to Berkeley by
     40  * Jan-Simon Pendry.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
     67  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.20 2006/11/25 22:14:38 elad Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/proc.h>
     76 #include <sys/time.h>
     77 #include <sys/vnode.h>
     78 #include <sys/mount.h>
     79 #include <sys/namei.h>
     80 #include <sys/malloc.h>
     81 #include <miscfs/specfs/specdev.h>
     82 #include <miscfs/genfs/layer.h>
     83 #include <miscfs/genfs/layer_extern.h>
     84 
     85 #define	NLAYERNODECACHE 16
     86 
     87 #ifdef LAYERFS_DIAGNOSTIC
     88 int layerfs_debug = 1;
     89 #endif
     90 
     91 /*
     92  * layer cache:
     93  * Each cache entry holds a reference to the lower vnode
     94  * along with a pointer to the alias vnode.  When an
     95  * entry is added the lower vnode is VREF'd.  When the
     96  * alias is removed the lower vnode is vrele'd.
     97  */
     98 
     99 /*
    100  * Initialise cache headers
    101  */
    102 void
    103 layerfs_init()
    104 {
    105 #ifdef LAYERFS_DIAGNOSTIC
    106 	if (layerfs_debug)
    107 		printf("layerfs_init\n");		/* printed during system boot */
    108 #endif
    109 }
    110 
    111 /*
    112  * Free global resources of layerfs.
    113  */
    114 void
    115 layerfs_done()
    116 {
    117 #ifdef LAYERFS_DIAGNOSTIC
    118 	if (layerfs_debug)
    119 		printf("layerfs_done\n");		/* printed on layerfs detach */
    120 #endif
    121 }
    122 
    123 /*
    124  * Return a locked, VREF'ed alias for lower vnode if already exists, else 0.
    125  */
    126 struct vnode *
    127 layer_node_find(mp, lowervp)
    128 	struct mount *mp;
    129 	struct vnode *lowervp;
    130 {
    131 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    132 	struct layer_node_hashhead *hd;
    133 	struct layer_node *a;
    134 	struct vnode *vp;
    135 
    136 	/*
    137 	 * Find hash base, and then search the (two-way) linked
    138 	 * list looking for a layer_node structure which is referencing
    139 	 * the lower vnode.  If found, the increment the layer_node
    140 	 * reference count (but NOT the lower vnode's VREF counter)
    141 	 * and return the vnode locked.
    142 	 */
    143 	hd = LAYER_NHASH(lmp, lowervp);
    144 loop:
    145 	simple_lock(&lmp->layerm_hashlock);
    146 	for (a = hd->lh_first; a != 0; a = a->layer_hash.le_next) {
    147 		if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
    148 			vp = LAYERTOV(a);
    149 			simple_unlock(&lmp->layerm_hashlock);
    150 			/*
    151 			 * We must be careful here as the fact the lower
    152 			 * vnode is locked will imply vp is locked unless
    153 			 * someone has decided to start vclean'ing either
    154 			 * vp or lowervp.
    155 			 *
    156 			 * So we try for an exclusive, recursive lock
    157 			 * on the upper vnode. If it fails, vcleaning
    158 			 * is in progress (so when we try again, we'll
    159 			 * fail). If it succeeds, we now have double
    160 			 * locked the bottom node. So we do an explicit
    161 			 * VOP_UNLOCK on it to keep the counts right. Note
    162 			 * that we will end up with the upper node and
    163 			 * the lower node locked once.
    164 			 */
    165 			if (vget(vp, LK_EXCLUSIVE | LK_CANRECURSE)) {
    166 				printf ("layer_node_find: vget failed.\n");
    167 				goto loop;
    168 			};
    169 			VOP_UNLOCK(lowervp, 0);
    170 			return (vp);
    171 		}
    172 	}
    173 
    174 	simple_unlock(&lmp->layerm_hashlock);
    175 	return NULL;
    176 }
    177 
    178 
    179 /*
    180  * Make a new layer_node node.
    181  * Vp is the alias vnode, lowervp is the lower vnode.
    182  * Maintain a reference to lowervp.
    183  */
    184 int
    185 layer_node_alloc(mp, lowervp, vpp)
    186 	struct mount *mp;
    187 	struct vnode *lowervp;
    188 	struct vnode **vpp;
    189 {
    190 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    191 	struct layer_node_hashhead *hd;
    192 	struct layer_node *xp;
    193 	struct vnode *vp, *nvp;
    194 	int error;
    195 	extern int (**dead_vnodeop_p)(void *);
    196 
    197 	if ((error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p,
    198 			&vp)) != 0)
    199 		return (error);
    200 	vp->v_type = lowervp->v_type;
    201 	vp->v_flag |= VLAYER;
    202 
    203 	xp = malloc(lmp->layerm_size, M_TEMP, M_WAITOK);
    204 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
    205 		MALLOC(vp->v_specinfo, struct specinfo *,
    206 		    sizeof(struct specinfo), M_VNODE, M_WAITOK);
    207 		vp->v_hashchain = NULL;
    208 		vp->v_rdev = lowervp->v_rdev;
    209 	}
    210 
    211 	vp->v_data = xp;
    212 	xp->layer_vnode = vp;
    213 	xp->layer_lowervp = lowervp;
    214 	xp->layer_flags = 0;
    215 	/*
    216 	 * Before we insert our new node onto the hash chains,
    217 	 * check to see if someone else has beaten us to it.
    218 	 * (We could have slept in MALLOC.)
    219 	 */
    220 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
    221 		*vpp = nvp;
    222 
    223 		/* free the substructures we've allocated. */
    224 		FREE(xp, M_TEMP);
    225 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    226 			FREE(vp->v_specinfo, M_VNODE);
    227 
    228 		vp->v_type = VBAD;		/* node is discarded */
    229 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
    230 		vrele(vp);			/* get rid of it. */
    231 		return (0);
    232 	}
    233 
    234 	simple_lock(&lmp->layerm_hashlock);
    235 
    236 	/*
    237 	 * Now lock the new node. We rely on the fact that we were passed
    238 	 * a locked vnode. If the lower node is exporting a struct lock
    239 	 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
    240 	 * lower one, and both are now locked. If the lower node is exporting
    241 	 * NULL, then we copy that up and manually lock the upper node.
    242 	 *
    243 	 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
    244 	 * up the v_vnlock from below.
    245 	 */
    246 
    247 	vp->v_vnlock = lowervp->v_vnlock;
    248 	LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
    249 
    250 	if (error) {
    251 		/*
    252 		 * How did we get a locking error? The node just came off
    253 		 * of the free list, and we're the only routine which
    254 		 * knows it's there...
    255 		 */
    256 		vp->v_vnlock = &vp->v_lock;
    257 		*vpp = NULL;
    258 
    259 		/* free the substructures we've allocated. */
    260 		FREE(xp, M_TEMP);
    261 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    262 			FREE(vp->v_specinfo, M_VNODE);
    263 
    264 		vp->v_type = VBAD;		/* node is discarded */
    265 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
    266 		vrele(vp);			/* get rid of it. */
    267 		simple_unlock(&lmp->layerm_hashlock);
    268 		return (error);
    269 	}
    270 	/*
    271 	 * NetBSD used to do an inlined checkalias here. We do not, as
    272 	 * we never flag device nodes as being aliased. The lowervp
    273 	 * node will, when appropriate, be flaged as an alias.
    274 	 */
    275 
    276 	*vpp = vp;
    277 	VREF(lowervp);	/* Take into account reference held in layer_node */
    278 	hd = LAYER_NHASH(lmp, lowervp);
    279 	LIST_INSERT_HEAD(hd, xp, layer_hash);
    280 	uvm_vnp_setsize(vp, 0);
    281 	simple_unlock(&lmp->layerm_hashlock);
    282 	return (0);
    283 }
    284 
    285 
    286 /*
    287  * Try to find an existing layer_node vnode refering
    288  * to it, otherwise make a new layer_node vnode which
    289  * contains a reference to the lower vnode.
    290  *
    291  * >>> we assume that the lower node is already locked upon entry, so we
    292  * propagate the lock state to upper node <<
    293  */
    294 int
    295 layer_node_create(mp, lowervp, newvpp)
    296 	struct mount *mp;
    297 	struct vnode *lowervp;
    298 	struct vnode **newvpp;
    299 {
    300 	struct vnode *aliasvp;
    301 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    302 
    303 	if ((aliasvp = layer_node_find(mp, lowervp)) != NULL) {
    304 		/*
    305 		 * layer_node_find has taken another reference
    306 		 * to the alias vnode and moved the lock holding to
    307 		 * aliasvp
    308 		 */
    309 #ifdef LAYERFS_DIAGNOSTIC
    310 		if (layerfs_debug)
    311 			vprint("layer_node_create: exists", aliasvp);
    312 #endif
    313 	} else {
    314 		int error;
    315 
    316 		/*
    317 		 * Get new vnode.
    318 		 */
    319 #ifdef LAYERFS_DIAGNOSTIC
    320 		if (layerfs_debug)
    321 			printf("layer_node_create: create new alias vnode\n");
    322 #endif
    323 
    324 		/*
    325 		 * Make new vnode reference the layer_node.
    326 		 */
    327 		if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
    328 			return error;
    329 
    330 		/*
    331 		 * aliasvp is already VREF'd by getnewvnode()
    332 		 */
    333 	}
    334 
    335 	/*
    336 	 * Now that we have VREF'd the upper vnode, release the reference
    337 	 * to the lower node. The existence of the layer_node retains one
    338 	 * reference to the lower node.
    339 	 */
    340 	vrele(lowervp);
    341 
    342 #ifdef DIAGNOSTIC
    343 	if (lowervp->v_usecount < 1) {
    344 		/* Should never happen... */
    345 		vprint("layer_node_create: alias", aliasvp);
    346 		vprint("layer_node_create: lower", lowervp);
    347 		panic("layer_node_create: lower has 0 usecount.");
    348 	};
    349 #endif
    350 
    351 #ifdef LAYERFS_DIAGNOSTIC
    352 	if (layerfs_debug)
    353 		vprint("layer_node_create: alias", aliasvp);
    354 #endif
    355 	*newvpp = aliasvp;
    356 	return (0);
    357 }
    358 
    359 #ifdef LAYERFS_DIAGNOSTIC
    360 struct vnode *
    361 layer_checkvp(vp, fil, lno)
    362 	struct vnode *vp;
    363 	const char *fil;
    364 	int lno;
    365 {
    366 	struct layer_node *a = VTOLAYER(vp);
    367 #ifdef notyet
    368 	/*
    369 	 * Can't do this check because vop_reclaim runs
    370 	 * with a funny vop vector.
    371 	 *
    372 	 * WRS - no it doesnt...
    373 	 */
    374 	if (vp->v_op != layer_vnodeop_p) {
    375 		printf ("layer_checkvp: on non-layer-node\n");
    376 #ifdef notyet
    377 		while (layer_checkvp_barrier) /*WAIT*/ ;
    378 #endif
    379 		panic("layer_checkvp");
    380 	};
    381 #endif
    382 	if (a->layer_lowervp == NULL) {
    383 		/* Should never happen */
    384 		int i; u_long *p;
    385 		printf("vp = %p, ZERO ptr\n", vp);
    386 		for (p = (u_long *) a, i = 0; i < 8; i++)
    387 			printf(" %lx", p[i]);
    388 		printf("\n");
    389 		/* wait for debugger */
    390 		panic("layer_checkvp");
    391 	}
    392 	if (a->layer_lowervp->v_usecount < 1) {
    393 		int i; u_long *p;
    394 		printf("vp = %p, unref'ed lowervp\n", vp);
    395 		for (p = (u_long *) a, i = 0; i < 8; i++)
    396 			printf(" %lx", p[i]);
    397 		printf("\n");
    398 		/* wait for debugger */
    399 		panic ("layer with unref'ed lowervp");
    400 	};
    401 #ifdef notnow
    402 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
    403 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
    404 		a->layer_lowervp, a->layer_lowervp->v_usecount,
    405 		fil, lno);
    406 #endif
    407 	return a->layer_lowervp;
    408 }
    409 #endif
    410