Home | History | Annotate | Line # | Download | only in genfs
layer_subr.c revision 1.22
      1 /*	$NetBSD: layer_subr.c,v 1.22 2007/10/10 20:42:28 ad 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.22 2007/10/10 20:42:28 ad 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 NULL.
    125  * The layermp's hashlock must be held on entry.
    126  * It will be held upon return iff we return NULL.
    127  */
    128 struct vnode *
    129 layer_node_find(mp, lowervp)
    130 	struct mount *mp;
    131 	struct vnode *lowervp;
    132 {
    133 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    134 	struct layer_node_hashhead *hd;
    135 	struct layer_node *a;
    136 	struct vnode *vp;
    137 	int error;
    138 
    139 	/*
    140 	 * Find hash base, and then search the (two-way) linked
    141 	 * list looking for a layer_node structure which is referencing
    142 	 * the lower vnode.  If found, the increment the layer_node
    143 	 * reference count (but NOT the lower vnode's VREF counter)
    144 	 * and return the vnode locked.
    145 	 */
    146 	hd = LAYER_NHASH(lmp, lowervp);
    147 loop:
    148 	LIST_FOREACH(a, hd, layer_hash) {
    149 		if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
    150 			vp = LAYERTOV(a);
    151 
    152 			/*
    153 			 * We must not let vget() try to lock the layer vp,
    154 			 * since the lower vp is already locked and locking the
    155 			 * layer vp will involve locking the lower vp (whether
    156 			 * or not they actually share a lock).  Instead, take
    157 			 * the layer vp's lock separately afterward, but only
    158 			 * if it does not share the lower vp's lock.
    159 			 */
    160 			mutex_exit(&lmp->layerm_hashlock);
    161 			error = vget(vp, 0);
    162 			if (error) {
    163 				mutex_enter(&lmp->layerm_hashlock);
    164 				goto loop;
    165 			}
    166 			LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
    167 			return (vp);
    168 		}
    169 	}
    170 	return NULL;
    171 }
    172 
    173 
    174 /*
    175  * Make a new layer_node node.
    176  * Vp is the alias vnode, lowervp is the lower vnode.
    177  * Maintain a reference to lowervp.
    178  */
    179 int
    180 layer_node_alloc(mp, lowervp, vpp)
    181 	struct mount *mp;
    182 	struct vnode *lowervp;
    183 	struct vnode **vpp;
    184 {
    185 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    186 	struct layer_node_hashhead *hd;
    187 	struct layer_node *xp;
    188 	struct vnode *vp, *nvp;
    189 	int error;
    190 	extern int (**dead_vnodeop_p)(void *);
    191 
    192 	if ((error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p,
    193 			&vp)) != 0)
    194 		return (error);
    195 	vp->v_type = lowervp->v_type;
    196 	simple_lock(&vp->v_interlock);
    197 	vp->v_iflag |= VI_LAYER;
    198 	simple_unlock(&vp->v_interlock);
    199 
    200 	xp = malloc(lmp->layerm_size, M_TEMP, M_WAITOK);
    201 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
    202 		MALLOC(vp->v_specinfo, struct specinfo *,
    203 		    sizeof(struct specinfo), M_VNODE, M_WAITOK);
    204 		vp->v_hashchain = NULL;
    205 		vp->v_rdev = lowervp->v_rdev;
    206 	}
    207 
    208 	vp->v_data = xp;
    209 	xp->layer_vnode = vp;
    210 	xp->layer_lowervp = lowervp;
    211 	xp->layer_flags = 0;
    212 
    213 	/*
    214 	 * Before we insert our new node onto the hash chains,
    215 	 * check to see if someone else has beaten us to it.
    216 	 * (We could have slept in MALLOC.)
    217 	 */
    218 	mutex_enter(&lmp->layerm_hashlock);
    219 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
    220 		*vpp = nvp;
    221 
    222 		/* free the substructures we've allocated. */
    223 		FREE(xp, M_TEMP);
    224 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    225 			FREE(vp->v_specinfo, M_VNODE);
    226 
    227 		vp->v_type = VBAD;		/* node is discarded */
    228 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
    229 		vrele(vp);			/* get rid of it. */
    230 		return (0);
    231 	}
    232 
    233 	/*
    234 	 * Now lock the new node. We rely on the fact that we were passed
    235 	 * a locked vnode. If the lower node is exporting a struct lock
    236 	 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
    237 	 * lower one, and both are now locked. If the lower node is exporting
    238 	 * NULL, then we copy that up and manually lock the upper node.
    239 	 *
    240 	 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
    241 	 * up the v_vnlock from below.
    242 	 */
    243 
    244 	vp->v_vnlock = lowervp->v_vnlock;
    245 	LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
    246 	KASSERT(error == 0);
    247 
    248 	/*
    249 	 * Insert the new node into the hash.
    250 	 * Add a reference to the lower node.
    251 	 */
    252 
    253 	*vpp = vp;
    254 	VREF(lowervp);
    255 	hd = LAYER_NHASH(lmp, lowervp);
    256 	LIST_INSERT_HEAD(hd, xp, layer_hash);
    257 	uvm_vnp_setsize(vp, 0);
    258 	mutex_exit(&lmp->layerm_hashlock);
    259 	return (0);
    260 }
    261 
    262 
    263 /*
    264  * Try to find an existing layer_node vnode refering
    265  * to it, otherwise make a new layer_node vnode which
    266  * contains a reference to the lower vnode.
    267  *
    268  * >>> we assume that the lower node is already locked upon entry, so we
    269  * propagate the lock state to upper node <<
    270  */
    271 int
    272 layer_node_create(mp, lowervp, newvpp)
    273 	struct mount *mp;
    274 	struct vnode *lowervp;
    275 	struct vnode **newvpp;
    276 {
    277 	struct vnode *aliasvp;
    278 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    279 
    280 	mutex_enter(&lmp->layerm_hashlock);
    281 	aliasvp = layer_node_find(mp, lowervp);
    282 	if (aliasvp != NULL) {
    283 		/*
    284 		 * layer_node_find has taken another reference
    285 		 * to the alias vnode and moved the lock holding to
    286 		 * aliasvp
    287 		 */
    288 #ifdef LAYERFS_DIAGNOSTIC
    289 		if (layerfs_debug)
    290 			vprint("layer_node_create: exists", aliasvp);
    291 #endif
    292 	} else {
    293 		int error;
    294 
    295 		mutex_exit(&lmp->layerm_hashlock);
    296 
    297 		/*
    298 		 * Get new vnode.
    299 		 */
    300 #ifdef LAYERFS_DIAGNOSTIC
    301 		if (layerfs_debug)
    302 			printf("layer_node_create: create new alias vnode\n");
    303 #endif
    304 
    305 		/*
    306 		 * Make new vnode reference the layer_node.
    307 		 */
    308 		if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
    309 			return error;
    310 
    311 		/*
    312 		 * aliasvp is already VREF'd by getnewvnode()
    313 		 */
    314 	}
    315 
    316 	/*
    317 	 * Now that we have VREF'd the upper vnode, release the reference
    318 	 * to the lower node. The existence of the layer_node retains one
    319 	 * reference to the lower node.
    320 	 */
    321 	vrele(lowervp);
    322 
    323 #ifdef DIAGNOSTIC
    324 	if (lowervp->v_usecount < 1) {
    325 		/* Should never happen... */
    326 		vprint("layer_node_create: alias", aliasvp);
    327 		vprint("layer_node_create: lower", lowervp);
    328 		panic("layer_node_create: lower has 0 usecount.");
    329 	}
    330 #endif
    331 
    332 #ifdef LAYERFS_DIAGNOSTIC
    333 	if (layerfs_debug)
    334 		vprint("layer_node_create: alias", aliasvp);
    335 #endif
    336 	*newvpp = aliasvp;
    337 	return (0);
    338 }
    339 
    340 #ifdef LAYERFS_DIAGNOSTIC
    341 struct vnode *
    342 layer_checkvp(vp, fil, lno)
    343 	struct vnode *vp;
    344 	const char *fil;
    345 	int lno;
    346 {
    347 	struct layer_node *a = VTOLAYER(vp);
    348 #ifdef notyet
    349 	/*
    350 	 * Can't do this check because vop_reclaim runs
    351 	 * with a funny vop vector.
    352 	 *
    353 	 * WRS - no it doesnt...
    354 	 */
    355 	if (vp->v_op != layer_vnodeop_p) {
    356 		printf ("layer_checkvp: on non-layer-node\n");
    357 #ifdef notyet
    358 		while (layer_checkvp_barrier) /*WAIT*/ ;
    359 #endif
    360 		panic("layer_checkvp");
    361 	};
    362 #endif
    363 	if (a->layer_lowervp == NULL) {
    364 		/* Should never happen */
    365 		int i; u_long *p;
    366 		printf("vp = %p, ZERO ptr\n", vp);
    367 		for (p = (u_long *) a, i = 0; i < 8; i++)
    368 			printf(" %lx", p[i]);
    369 		printf("\n");
    370 		/* wait for debugger */
    371 		panic("layer_checkvp");
    372 	}
    373 	if (a->layer_lowervp->v_usecount < 1) {
    374 		int i; u_long *p;
    375 		printf("vp = %p, unref'ed lowervp\n", vp);
    376 		for (p = (u_long *) a, i = 0; i < 8; i++)
    377 			printf(" %lx", p[i]);
    378 		printf("\n");
    379 		/* wait for debugger */
    380 		panic ("layer with unref'ed lowervp");
    381 	};
    382 #ifdef notnow
    383 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
    384 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
    385 		a->layer_lowervp, a->layer_lowervp->v_usecount,
    386 		fil, lno);
    387 #endif
    388 	return a->layer_lowervp;
    389 }
    390 #endif
    391