layer_subr.c revision 1.7.2.1 1 /* $NetBSD: layer_subr.c,v 1.7.2.1 2001/06/21 20:07:35 nathanw 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. 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 uvm_vnp_setsize(vp, 0);
276 simple_unlock(&lmp->layerm_hashlock);
277 return (0);
278 }
279
280
281 /*
282 * Try to find an existing layer_node vnode refering
283 * to it, otherwise make a new layer_node vnode which
284 * contains a reference to the lower vnode.
285 *
286 * >>> we assume that the lower node is already locked upon entry, so we
287 * propagate the lock state to upper node <<
288 */
289 int
290 layer_node_create(mp, lowervp, newvpp)
291 struct mount *mp;
292 struct vnode *lowervp;
293 struct vnode **newvpp;
294 {
295 struct vnode *aliasvp;
296 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
297
298 if ((aliasvp = layer_node_find(mp, lowervp)) != NULL) {
299 /*
300 * layer_node_find has taken another reference
301 * to the alias vnode and moved the lock holding to
302 * aliasvp
303 */
304 #ifdef LAYERFS_DIAGNOSTIC
305 vprint("layer_node_create: exists", aliasvp);
306 #endif
307 } else {
308 int error;
309
310 /*
311 * Get new vnode.
312 */
313 #ifdef LAYERFS_DIAGNOSTIC
314 printf("layer_node_create: create new alias vnode\n");
315 #endif
316
317 /*
318 * Make new vnode reference the layer_node.
319 */
320 if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
321 return error;
322
323 /*
324 * aliasvp is already VREF'd by getnewvnode()
325 */
326 }
327
328 /*
329 * Now that we have VREF'd the upper vnode, release the reference
330 * to the lower node. The existance of the layer_node retains one
331 * reference to the lower node.
332 */
333 vrele(lowervp);
334
335 #ifdef DIAGNOSTIC
336 if (lowervp->v_usecount < 1) {
337 /* Should never happen... */
338 vprint("layer_node_create: alias", aliasvp);
339 vprint("layer_node_create: lower", lowervp);
340 panic("layer_node_create: lower has 0 usecount.");
341 };
342 #endif
343
344 #ifdef LAYERFS_DIAGNOSTIC
345 vprint("layer_node_create: alias", aliasvp);
346 #endif
347 *newvpp = aliasvp;
348 return (0);
349 }
350
351 struct vnode *
352 layer_checkvp(vp, fil, lno)
353 struct vnode *vp;
354 char *fil;
355 int lno;
356 {
357 struct layer_node *a = VTOLAYER(vp);
358 #ifdef notyet
359 /*
360 * Can't do this check because vop_reclaim runs
361 * with a funny vop vector.
362 *
363 * WRS - no it doesnt...
364 */
365 if (vp->v_op != layer_vnodeop_p) {
366 printf ("layer_checkvp: on non-layer-node\n");
367 #ifdef notyet
368 while (layer_checkvp_barrier) /*WAIT*/ ;
369 #endif
370 panic("layer_checkvp");
371 };
372 #endif
373 if (a->layer_lowervp == NULL) {
374 /* Should never happen */
375 int i; u_long *p;
376 printf("vp = %p, ZERO ptr\n", vp);
377 for (p = (u_long *) a, i = 0; i < 8; i++)
378 printf(" %lx", p[i]);
379 printf("\n");
380 /* wait for debugger */
381 panic("layer_checkvp");
382 }
383 if (a->layer_lowervp->v_usecount < 1) {
384 int i; u_long *p;
385 printf("vp = %p, unref'ed lowervp\n", vp);
386 for (p = (u_long *) a, i = 0; i < 8; i++)
387 printf(" %lx", p[i]);
388 printf("\n");
389 /* wait for debugger */
390 panic ("layer with unref'ed lowervp");
391 };
392 #ifdef notnow
393 printf("layer %p/%d -> %p/%d [%s, %d]\n",
394 LAYERTOV(a), LAYERTOV(a)->v_usecount,
395 a->layer_lowervp, a->layer_lowervp->v_usecount,
396 fil, lno);
397 #endif
398 return a->layer_lowervp;
399 }
400