xref: /netbsd-src/sys/miscfs/genfs/layer_subr.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: layer_subr.c,v 1.6 2000/03/16 18:08:24 jdolecek 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 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 #ifdef LAYERFS_DIAGNOSTIC
104 	printf("layerfs_init\n");		/* printed during system boot */
105 #endif
106 }
107 
108 /*
109  * Free global resources of layerfs.
110  */
111 void
112 layerfs_done()
113 {
114 #ifdef LAYERFS_DIAGNOSTIC
115 	printf("layerfs_done\n");		/* printed on layerfs detach */
116 #endif
117 }
118 
119 /*
120  * Return a locked, VREF'ed alias for lower vnode if already exists, else 0.
121  */
122 struct vnode *
123 layer_node_find(mp, lowervp)
124 	struct mount *mp;
125 	struct vnode *lowervp;
126 {
127 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
128 	struct layer_node_hashhead *hd;
129 	struct layer_node *a;
130 	struct vnode *vp;
131 
132 	/*
133 	 * Find hash base, and then search the (two-way) linked
134 	 * list looking for a layer_node structure which is referencing
135 	 * the lower vnode.  If found, the increment the layer_node
136 	 * reference count (but NOT the lower vnode's VREF counter)
137 	 * and return the vnode locked.
138 	 */
139 	hd = LAYER_NHASH(lmp, lowervp);
140 loop:
141 	simple_lock(&lmp->layerm_hashlock);
142 	for (a = hd->lh_first; a != 0; a = a->layer_hash.le_next) {
143 		if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
144 			vp = LAYERTOV(a);
145 			simple_unlock(&lmp->layerm_hashlock);
146 			/*
147 			 * We must be careful here as the fact the lower
148 			 * vnode is locked will imply vp is locked unless
149 			 * someone has decided to start vclean'ing either
150 			 * vp or lowervp.
151 			 *
152 			 * So we try for an exclusive, recursive lock
153 			 * on the upper vnode. If it fails, vcleaning
154 			 * is in progress (so when we try again, we'll
155 			 * fail). If it succeeds, we now have double
156 			 * locked the bottom node. So we do an explicit
157 			 * VOP_UNLOCK on it to keep the counts right. Note
158 			 * that we will end up with the upper node and
159 			 * the lower node locked once.
160 			 */
161 			if (vget(vp, LK_EXCLUSIVE | LK_CANRECURSE)) {
162 				printf ("layer_node_find: vget failed.\n");
163 				goto loop;
164 			};
165 			VOP_UNLOCK(lowervp, 0);
166 			return (vp);
167 		}
168 	}
169 
170 	simple_unlock(&lmp->layerm_hashlock);
171 	return NULL;
172 }
173 
174 
175 /*
176  * Make a new layer_node node.
177  * Vp is the alias vnode, lowervp is the lower vnode.
178  * Maintain a reference to lowervp.
179  */
180 int
181 layer_node_alloc(mp, lowervp, vpp)
182 	struct mount *mp;
183 	struct vnode *lowervp;
184 	struct vnode **vpp;
185 {
186 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
187 	struct layer_node_hashhead *hd;
188 	struct layer_node *xp;
189 	struct vnode *vp, *nvp;
190 	int error;
191 	extern int (**dead_vnodeop_p) __P((void *));
192 
193 	if ((error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p,
194 			&vp)) != 0)
195 		return (error);
196 	vp->v_type = lowervp->v_type;
197 	vp->v_flag |= VLAYER;
198 
199 	MALLOC(xp, struct layer_node *, lmp->layerm_size, M_TEMP, M_WAITOK);
200 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
201 		MALLOC(vp->v_specinfo, struct specinfo *,
202 		    sizeof(struct specinfo), M_VNODE, M_WAITOK);
203 		vp->v_hashchain = NULL;
204 		vp->v_rdev = lowervp->v_rdev;
205 	}
206 
207 	vp->v_data = xp;
208 	xp->layer_vnode = vp;
209 	xp->layer_lowervp = lowervp;
210 	xp->layer_flags = 0;
211 	/*
212 	 * Before we insert our new node onto the hash chains,
213 	 * check to see if someone else has beaten us to it.
214 	 * (We could have slept in MALLOC.)
215 	 */
216 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
217 		*vpp = nvp;
218 
219 		/* free the substructures we've allocated. */
220 		FREE(xp, M_TEMP);
221 		if (vp->v_type == VBLK || vp->v_type == VCHR)
222 			FREE(vp->v_specinfo, M_VNODE);
223 
224 		vp->v_type = VBAD;		/* node is discarded */
225 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
226 		vrele(vp);			/* get rid of it. */
227 		return (0);
228 	}
229 
230 	simple_lock(&lmp->layerm_hashlock);
231 
232 	/*
233 	 * Now lock the new node. We rely on the fact that we were passed
234 	 * a locked vnode. If the lower node is exporting a struct lock
235 	 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
236 	 * lower one, and both are now locked. If the lower node is exporting
237 	 * NULL, then we copy that up and manually lock the upper node.
238 	 *
239 	 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
240 	 * up the v_vnlock from below.
241 	 */
242 
243 	vp->v_vnlock = lowervp->v_vnlock;
244 	LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
245 
246 	if (error) {
247 		/*
248 		 * How did we get a locking error? The node just came off
249 		 * of the free list, and we're the only routine which
250 		 * knows it's there...
251 		 */
252 		vp->v_vnlock = &vp->v_lock;
253 		*vpp = NULL;
254 
255 		/* free the substructures we've allocated. */
256 		FREE(xp, M_TEMP);
257 		if (vp->v_type == VBLK || vp->v_type == VCHR)
258 			FREE(vp->v_specinfo, M_VNODE);
259 
260 		vp->v_type = VBAD;		/* node is discarded */
261 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
262 		vrele(vp);			/* get rid of it. */
263 		return (error);
264 	}
265 	/*
266 	 * NetBSD used to do an inlined checkalias here. We do not, as
267 	 * we never flag device nodes as being aliased. The lowervp
268 	 * node will, when appropriate, be flaged as an alias.
269 	 */
270 
271 	*vpp = vp;
272 	VREF(lowervp);	/* Take into account reference held in layer_node */
273 	hd = LAYER_NHASH(lmp, lowervp);
274 	LIST_INSERT_HEAD(hd, xp, layer_hash);
275 	simple_unlock(&lmp->layerm_hashlock);
276 	return (0);
277 }
278 
279 
280 /*
281  * Try to find an existing layer_node vnode refering
282  * to it, otherwise make a new layer_node vnode which
283  * contains a reference to the lower vnode.
284  *
285  * >>> we assume that the lower node is already locked upon entry, so we
286  * propagate the lock state to upper node <<
287  */
288 int
289 layer_node_create(mp, lowervp, newvpp)
290 	struct mount *mp;
291 	struct vnode *lowervp;
292 	struct vnode **newvpp;
293 {
294 	struct vnode *aliasvp;
295 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
296 
297 	if ((aliasvp = layer_node_find(mp, lowervp)) != NULL) {
298 		/*
299 		 * layer_node_find has taken another reference
300 		 * to the alias vnode and moved the lock holding to
301 		 * aliasvp
302 		 */
303 #ifdef LAYERFS_DIAGNOSTIC
304 		vprint("layer_node_create: exists", aliasvp);
305 #endif
306 	} else {
307 		int error;
308 
309 		/*
310 		 * Get new vnode.
311 		 */
312 #ifdef LAYERFS_DIAGNOSTIC
313 		printf("layer_node_create: create new alias vnode\n");
314 #endif
315 
316 		/*
317 		 * Make new vnode reference the layer_node.
318 		 */
319 		if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
320 			return error;
321 
322 		/*
323 		 * aliasvp is already VREF'd by getnewvnode()
324 		 */
325 	}
326 
327 	/*
328 	 * Now that we have VREF'd the upper vnode, release the reference
329 	 * to the lower node. The existance of the layer_node retains one
330 	 * reference to the lower node.
331 	 */
332 	vrele(lowervp);
333 
334 #ifdef DIAGNOSTIC
335 	if (lowervp->v_usecount < 1) {
336 		/* Should never happen... */
337 		vprint("layer_node_create: alias", aliasvp);
338 		vprint("layer_node_create: lower", lowervp);
339 		panic("layer_node_create: lower has 0 usecount.");
340 	};
341 #endif
342 
343 #ifdef LAYERFS_DIAGNOSTIC
344 	vprint("layer_node_create: alias", aliasvp);
345 #endif
346 	*newvpp = aliasvp;
347 	return (0);
348 }
349 
350 struct vnode *
351 layer_checkvp(vp, fil, lno)
352 	struct vnode *vp;
353 	char *fil;
354 	int lno;
355 {
356 	struct layer_node *a = VTOLAYER(vp);
357 #ifdef notyet
358 	/*
359 	 * Can't do this check because vop_reclaim runs
360 	 * with a funny vop vector.
361 	 *
362 	 * WRS - no it doesnt...
363 	 */
364 	if (vp->v_op != layer_vnodeop_p) {
365 		printf ("layer_checkvp: on non-layer-node\n");
366 #ifdef notyet
367 		while (layer_checkvp_barrier) /*WAIT*/ ;
368 #endif
369 		panic("layer_checkvp");
370 	};
371 #endif
372 	if (a->layer_lowervp == NULL) {
373 		/* Should never happen */
374 		int i; u_long *p;
375 		printf("vp = %p, ZERO ptr\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_checkvp");
381 	}
382 	if (a->layer_lowervp->v_usecount < 1) {
383 		int i; u_long *p;
384 		printf("vp = %p, unref'ed lowervp\n", vp);
385 		for (p = (u_long *) a, i = 0; i < 8; i++)
386 			printf(" %lx", p[i]);
387 		printf("\n");
388 		/* wait for debugger */
389 		panic ("layer with unref'ed lowervp");
390 	};
391 #ifdef notnow
392 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
393 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
394 		a->layer_lowervp, a->layer_lowervp->v_usecount,
395 		fil, lno);
396 #endif
397 	return a->layer_lowervp;
398 }
399