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