layer_subr.c revision 1.3.2.2 1 /* $NetBSD: layer_subr.c,v 1.3.2.2 1999/08/02 22:27:34 thorpej 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_rdev = lowervp->v_rdev;
194 }
195
196 vp->v_data = xp;
197 xp->layer_vnode = vp;
198 xp->layer_lowervp = lowervp;
199 xp->layer_flags = 0;
200 /*
201 * Before we insert our new node onto the hash chains,
202 * check to see if someone else has beaten us to it.
203 * (We could have slept in MALLOC.)
204 */
205 if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
206 *vpp = nvp;
207
208 /* free the substructures we've allocated. */
209 FREE(xp, M_TEMP);
210 if (vp->v_type == VBLK || vp->v_type == VCHR)
211 FREE(vp->v_specinfo, M_VNODE);
212
213 vp->v_type = VBAD; /* node is discarded */
214 vp->v_op = dead_vnodeop_p; /* so ops will still work */
215 vrele(vp); /* get rid of it. */
216 return (0);
217 }
218
219 simple_lock(&lmp->layerm_hashlock);
220
221 /*
222 * Now lock the new node. We rely on the fact that we were passed
223 * a locked vnode. If the lower node is exporting a struct lock
224 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
225 * lower one, and both are now locked. If the lower node is exporting
226 * NULL, then we copy that up and manually lock the upper node.
227 *
228 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
229 * up the v_vnlock from below.
230 */
231
232 vp->v_vnlock = lowervp->v_vnlock;
233 LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
234
235 if (error) {
236 /*
237 * How did we get a locking error? The node just came off
238 * of the free list, and we're the only routine which
239 * knows it's there...
240 */
241 vp->v_vnlock = &vp->v_lock;
242 *vpp = NULL;
243
244 /* free the substructures we've allocated. */
245 FREE(xp, M_TEMP);
246 if (vp->v_type == VBLK || vp->v_type == VCHR)
247 FREE(vp->v_specinfo, M_VNODE);
248
249 vp->v_type = VBAD; /* node is discarded */
250 vp->v_op = dead_vnodeop_p; /* so ops will still work */
251 vrele(vp); /* get rid of it. */
252 return (error);
253 }
254 /*
255 * NetBSD used to do an inlined checkalias here. We do not, as
256 * we never flag device nodes as being aliased. The lowervp
257 * node will, when appropriate, be flaged as an alias.
258 */
259
260 *vpp = vp;
261 VREF(lowervp); /* Take into account reference held in layer_node */
262 hd = LAYER_NHASH(lmp, lowervp);
263 LIST_INSERT_HEAD(hd, xp, layer_hash);
264 simple_unlock(&lmp->layerm_hashlock);
265 return (0);
266 }
267
268
269 /*
270 * Try to find an existing layer_node vnode refering
271 * to it, otherwise make a new layer_node vnode which
272 * contains a reference to the lower vnode.
273 *
274 * >>> we assume that the lower node is already locked upon entry, so we
275 * propagate the lock state to upper node <<
276 */
277 int
278 layer_node_create(mp, lowervp, newvpp)
279 struct mount *mp;
280 struct vnode *lowervp;
281 struct vnode **newvpp;
282 {
283 struct vnode *aliasvp;
284 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
285
286 if ((aliasvp = layer_node_find(mp, lowervp)) != NULL) {
287 /*
288 * layer_node_find has taken another reference
289 * to the alias vnode and moved the lock holding to
290 * aliasvp
291 */
292 #ifdef LAYERFS_DIAGNOSTIC
293 vprint("layer_node_create: exists", aliasvp);
294 #endif
295 } else {
296 int error;
297
298 /*
299 * Get new vnode.
300 */
301 #ifdef LAYERFS_DIAGNOSTIC
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 existance 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 vprint("layer_node_create: alias", aliasvp);
334 #endif
335 *newvpp = aliasvp;
336 return (0);
337 }
338
339 struct vnode *
340 layer_checkvp(vp, fil, lno)
341 struct vnode *vp;
342 char *fil;
343 int lno;
344 {
345 struct layer_node *a = VTOLAYER(vp);
346 #ifdef notyet
347 /*
348 * Can't do this check because vop_reclaim runs
349 * with a funny vop vector.
350 *
351 * WRS - no it doesnt...
352 */
353 if (vp->v_op != layer_vnodeop_p) {
354 printf ("layer_checkvp: on non-layer-node\n");
355 #ifdef notyet
356 while (layer_checkvp_barrier) /*WAIT*/ ;
357 #endif
358 panic("layer_checkvp");
359 };
360 #endif
361 if (a->layer_lowervp == NULL) {
362 /* Should never happen */
363 int i; u_long *p;
364 printf("vp = %p, ZERO ptr\n", vp);
365 for (p = (u_long *) a, i = 0; i < 8; i++)
366 printf(" %lx", p[i]);
367 printf("\n");
368 /* wait for debugger */
369 panic("layer_checkvp");
370 }
371 if (a->layer_lowervp->v_usecount < 1) {
372 int i; u_long *p;
373 printf("vp = %p, unref'ed lowervp\n", vp);
374 for (p = (u_long *) a, i = 0; i < 8; i++)
375 printf(" %lx", p[i]);
376 printf("\n");
377 /* wait for debugger */
378 panic ("layer with unref'ed lowervp");
379 };
380 #ifdef notnow
381 printf("layer %p/%d -> %p/%d [%s, %d]\n",
382 LAYERTOV(a), LAYERTOV(a)->v_usecount,
383 a->layer_lowervp, a->layer_lowervp->v_usecount,
384 fil, lno);
385 #endif
386 return a->layer_lowervp;
387 }
388