Home | History | Annotate | Line # | Download | only in genfs
layer_subr.c revision 1.24
      1 /*	$NetBSD: layer_subr.c,v 1.24 2008/01/23 20:11:32 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.24 2008/01/23 20:11:32 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/kmem.h>
     81 #include <sys/malloc.h>
     82 
     83 #include <miscfs/specfs/specdev.h>
     84 #include <miscfs/genfs/layer.h>
     85 #include <miscfs/genfs/layer_extern.h>
     86 
     87 #define	NLAYERNODECACHE 16
     88 
     89 #ifdef LAYERFS_DIAGNOSTIC
     90 int layerfs_debug = 1;
     91 #endif
     92 
     93 /*
     94  * layer cache:
     95  * Each cache entry holds a reference to the lower vnode
     96  * along with a pointer to the alias vnode.  When an
     97  * entry is added the lower vnode is VREF'd.  When the
     98  * alias is removed the lower vnode is vrele'd.
     99  */
    100 
    101 /*
    102  * Initialise cache headers
    103  */
    104 void
    105 layerfs_init()
    106 {
    107 #ifdef LAYERFS_DIAGNOSTIC
    108 	if (layerfs_debug)
    109 		printf("layerfs_init\n");		/* printed during system boot */
    110 #endif
    111 }
    112 
    113 /*
    114  * Free global resources of layerfs.
    115  */
    116 void
    117 layerfs_done()
    118 {
    119 #ifdef LAYERFS_DIAGNOSTIC
    120 	if (layerfs_debug)
    121 		printf("layerfs_done\n");		/* printed on layerfs detach */
    122 #endif
    123 }
    124 
    125 /*
    126  * Return a locked, VREF'ed alias for lower vnode if already exists, else NULL.
    127  * The layermp's hashlock must be held on entry.
    128  * It will be held upon return iff we return NULL.
    129  */
    130 struct vnode *
    131 layer_node_find(mp, lowervp)
    132 	struct mount *mp;
    133 	struct vnode *lowervp;
    134 {
    135 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    136 	struct layer_node_hashhead *hd;
    137 	struct layer_node *a;
    138 	struct vnode *vp;
    139 	int error;
    140 
    141 	/*
    142 	 * Find hash base, and then search the (two-way) linked
    143 	 * list looking for a layer_node structure which is referencing
    144 	 * the lower vnode.  If found, the increment the layer_node
    145 	 * reference count (but NOT the lower vnode's VREF counter)
    146 	 * and return the vnode locked.
    147 	 */
    148 	hd = LAYER_NHASH(lmp, lowervp);
    149 loop:
    150 	LIST_FOREACH(a, hd, layer_hash) {
    151 		if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
    152 			vp = LAYERTOV(a);
    153 			mutex_enter(&vp->v_interlock);
    154 			/*
    155 			 * If we find a node being cleaned out, then
    156 			 * ignore it and continue.  A thread trying to
    157 			 * clean out the extant layer vnode needs to
    158 			 * acquire the shared lock (i.e. the lower
    159 			 * vnode's lock), which our caller already holds.
    160 			 * To allow the cleaning to succeed the current
    161 			 * thread must make progress.  So, for a brief
    162 			 * time more than one vnode in a layered file
    163 			 * system may refer to a single vnode in the
    164 			 * lower file system.
    165 			 */
    166 			if ((vp->v_iflag & VI_XLOCK) != 0) {
    167 				mutex_exit(&vp->v_interlock);
    168 				continue;
    169 			}
    170 			mutex_exit(&lmp->layerm_hashlock);
    171 			/*
    172 			 * We must not let vget() try to lock the layer
    173 			 * vp, since the lower vp is already locked and
    174 			 * locking the layer vp will involve locking
    175 			 * the lower vp (whether or not they actually
    176 			 * share a lock).  Instead, take the layer vp's
    177 			 * lock separately afterward, but only if it
    178 			 * does not share the lower vp's lock.
    179 			 */
    180 			error = vget(vp, LK_INTERLOCK | LK_NOWAIT);
    181 			if (error) {
    182 				kpause("layerfs", false, 1, NULL);
    183 				mutex_enter(&lmp->layerm_hashlock);
    184 				goto loop;
    185 			}
    186 			LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
    187 			return (vp);
    188 		}
    189 	}
    190 	return NULL;
    191 }
    192 
    193 
    194 /*
    195  * Make a new layer_node node.
    196  * Vp is the alias vnode, lowervp is the lower vnode.
    197  * Maintain a reference to lowervp.
    198  */
    199 int
    200 layer_node_alloc(mp, lowervp, vpp)
    201 	struct mount *mp;
    202 	struct vnode *lowervp;
    203 	struct vnode **vpp;
    204 {
    205 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    206 	struct layer_node_hashhead *hd;
    207 	struct layer_node *xp;
    208 	struct vnode *vp, *nvp;
    209 	int error;
    210 	extern int (**dead_vnodeop_p)(void *);
    211 
    212 	error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p, &vp);
    213 	if (error != 0)
    214 		return (error);
    215 	vp->v_type = lowervp->v_type;
    216 	mutex_enter(&vp->v_interlock);
    217 	vp->v_iflag |= VI_LAYER;
    218 	mutex_exit(&vp->v_interlock);
    219 
    220 	xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
    221 	if (xp == NULL) {
    222 		ungetnewvnode(vp);
    223 		return ENOMEM;
    224 	}
    225 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
    226 		MALLOC(vp->v_specinfo, struct specinfo *,
    227 		    sizeof(struct specinfo), M_VNODE, M_WAITOK);
    228 		vp->v_hashchain = NULL;
    229 		vp->v_rdev = lowervp->v_rdev;
    230 	}
    231 
    232 	vp->v_data = xp;
    233 	vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
    234 	    (lowervp->v_vflag & VV_MPSAFE);
    235 	xp->layer_vnode = vp;
    236 	xp->layer_lowervp = lowervp;
    237 	xp->layer_flags = 0;
    238 
    239 	/*
    240 	 * Before we insert our new node onto the hash chains,
    241 	 * check to see if someone else has beaten us to it.
    242 	 * (We could have slept in MALLOC.)
    243 	 */
    244 	mutex_enter(&lmp->layerm_hashlock);
    245 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
    246 		*vpp = nvp;
    247 
    248 		/* free the substructures we've allocated. */
    249 		kmem_free(xp, lmp->layerm_size);
    250 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    251 			FREE(vp->v_specinfo, M_VNODE);
    252 
    253 		vp->v_type = VBAD;		/* node is discarded */
    254 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
    255 		vrele(vp);			/* get rid of it. */
    256 		return (0);
    257 	}
    258 
    259 	/*
    260 	 * Now lock the new node. We rely on the fact that we were passed
    261 	 * a locked vnode. If the lower node is exporting a struct lock
    262 	 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
    263 	 * lower one, and both are now locked. If the lower node is exporting
    264 	 * NULL, then we copy that up and manually lock the upper node.
    265 	 *
    266 	 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
    267 	 * up the v_vnlock from below.
    268 	 */
    269 
    270 	vp->v_vnlock = lowervp->v_vnlock;
    271 	LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
    272 	KASSERT(error == 0);
    273 
    274 	/*
    275 	 * Insert the new node into the hash.
    276 	 * Add a reference to the lower node.
    277 	 */
    278 
    279 	*vpp = vp;
    280 	VREF(lowervp);
    281 	hd = LAYER_NHASH(lmp, lowervp);
    282 	LIST_INSERT_HEAD(hd, xp, layer_hash);
    283 	uvm_vnp_setsize(vp, 0);
    284 	mutex_exit(&lmp->layerm_hashlock);
    285 	return (0);
    286 }
    287 
    288 
    289 /*
    290  * Try to find an existing layer_node vnode refering
    291  * to it, otherwise make a new layer_node vnode which
    292  * contains a reference to the lower vnode.
    293  *
    294  * >>> we assume that the lower node is already locked upon entry, so we
    295  * propagate the lock state to upper node <<
    296  */
    297 int
    298 layer_node_create(mp, lowervp, newvpp)
    299 	struct mount *mp;
    300 	struct vnode *lowervp;
    301 	struct vnode **newvpp;
    302 {
    303 	struct vnode *aliasvp;
    304 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
    305 
    306 	mutex_enter(&lmp->layerm_hashlock);
    307 	aliasvp = layer_node_find(mp, lowervp);
    308 	if (aliasvp != NULL) {
    309 		/*
    310 		 * layer_node_find has taken another reference
    311 		 * to the alias vnode and moved the lock holding to
    312 		 * aliasvp
    313 		 */
    314 #ifdef LAYERFS_DIAGNOSTIC
    315 		if (layerfs_debug)
    316 			vprint("layer_node_create: exists", aliasvp);
    317 #endif
    318 	} else {
    319 		int error;
    320 
    321 		mutex_exit(&lmp->layerm_hashlock);
    322 
    323 		/*
    324 		 * Get new vnode.
    325 		 */
    326 #ifdef LAYERFS_DIAGNOSTIC
    327 		if (layerfs_debug)
    328 			printf("layer_node_create: create new alias vnode\n");
    329 #endif
    330 
    331 		/*
    332 		 * Make new vnode reference the layer_node.
    333 		 */
    334 		if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
    335 			return error;
    336 
    337 		/*
    338 		 * aliasvp is already VREF'd by getnewvnode()
    339 		 */
    340 	}
    341 
    342 	/*
    343 	 * Now that we have VREF'd the upper vnode, release the reference
    344 	 * to the lower node. The existence of the layer_node retains one
    345 	 * reference to the lower node.
    346 	 */
    347 	vrele(lowervp);
    348 
    349 #ifdef DIAGNOSTIC
    350 	if (lowervp->v_usecount < 1) {
    351 		/* Should never happen... */
    352 		vprint("layer_node_create: alias", aliasvp);
    353 		vprint("layer_node_create: lower", lowervp);
    354 		panic("layer_node_create: lower has 0 usecount.");
    355 	}
    356 #endif
    357 
    358 #ifdef LAYERFS_DIAGNOSTIC
    359 	if (layerfs_debug)
    360 		vprint("layer_node_create: alias", aliasvp);
    361 #endif
    362 	*newvpp = aliasvp;
    363 	return (0);
    364 }
    365 
    366 #ifdef LAYERFS_DIAGNOSTIC
    367 struct vnode *
    368 layer_checkvp(vp, fil, lno)
    369 	struct vnode *vp;
    370 	const char *fil;
    371 	int lno;
    372 {
    373 	struct layer_node *a = VTOLAYER(vp);
    374 #ifdef notyet
    375 	/*
    376 	 * Can't do this check because vop_reclaim runs
    377 	 * with a funny vop vector.
    378 	 *
    379 	 * WRS - no it doesnt...
    380 	 */
    381 	if (vp->v_op != layer_vnodeop_p) {
    382 		printf ("layer_checkvp: on non-layer-node\n");
    383 #ifdef notyet
    384 		while (layer_checkvp_barrier) /*WAIT*/ ;
    385 #endif
    386 		panic("layer_checkvp");
    387 	};
    388 #endif
    389 	if (a->layer_lowervp == NULL) {
    390 		/* Should never happen */
    391 		int i; u_long *p;
    392 		printf("vp = %p, ZERO ptr\n", vp);
    393 		for (p = (u_long *) a, i = 0; i < 8; i++)
    394 			printf(" %lx", p[i]);
    395 		printf("\n");
    396 		/* wait for debugger */
    397 		panic("layer_checkvp");
    398 	}
    399 	if (a->layer_lowervp->v_usecount < 1) {
    400 		int i; u_long *p;
    401 		printf("vp = %p, unref'ed lowervp\n", vp);
    402 		for (p = (u_long *) a, i = 0; i < 8; i++)
    403 			printf(" %lx", p[i]);
    404 		printf("\n");
    405 		/* wait for debugger */
    406 		panic ("layer with unref'ed lowervp");
    407 	};
    408 #ifdef notnow
    409 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
    410 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
    411 		a->layer_lowervp, a->layer_lowervp->v_usecount,
    412 		fil, lno);
    413 #endif
    414 	return a->layer_lowervp;
    415 }
    416 #endif
    417