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