layer_subr.c revision 1.23 1 /* $NetBSD: layer_subr.c,v 1.23 2008/01/02 11:48:59 ad 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.23 2008/01/02 11:48:59 ad 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/kmem.h>
81 #include <sys/malloc.h>
82
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 #ifdef LAYERFS_DIAGNOSTIC
90 int layerfs_debug = 1;
91 #endif
92
93 /*
94 * layer cache:
95 * Each cache entry holds a reference to the lower vnode
96 * along with a pointer to the alias vnode. When an
97 * entry is added the lower vnode is VREF'd. When the
98 * alias is removed the lower vnode is vrele'd.
99 */
100
101 /*
102 * Initialise cache headers
103 */
104 void
105 layerfs_init()
106 {
107 #ifdef LAYERFS_DIAGNOSTIC
108 if (layerfs_debug)
109 printf("layerfs_init\n"); /* printed during system boot */
110 #endif
111 }
112
113 /*
114 * Free global resources of layerfs.
115 */
116 void
117 layerfs_done()
118 {
119 #ifdef LAYERFS_DIAGNOSTIC
120 if (layerfs_debug)
121 printf("layerfs_done\n"); /* printed on layerfs detach */
122 #endif
123 }
124
125 /*
126 * Return a locked, VREF'ed alias for lower vnode if already exists, else NULL.
127 * The layermp's hashlock must be held on entry.
128 * It will be held upon return iff we return NULL.
129 */
130 struct vnode *
131 layer_node_find(mp, lowervp)
132 struct mount *mp;
133 struct vnode *lowervp;
134 {
135 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
136 struct layer_node_hashhead *hd;
137 struct layer_node *a;
138 struct vnode *vp;
139 int error;
140
141 /*
142 * Find hash base, and then search the (two-way) linked
143 * list looking for a layer_node structure which is referencing
144 * the lower vnode. If found, the increment the layer_node
145 * reference count (but NOT the lower vnode's VREF counter)
146 * and return the vnode locked.
147 */
148 hd = LAYER_NHASH(lmp, lowervp);
149 loop:
150 LIST_FOREACH(a, hd, layer_hash) {
151 if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
152 vp = LAYERTOV(a);
153
154 /*
155 * We must not let vget() try to lock the layer vp,
156 * since the lower vp is already locked and locking the
157 * layer vp will involve locking the lower vp (whether
158 * or not they actually share a lock). Instead, take
159 * the layer vp's lock separately afterward, but only
160 * if it does not share the lower vp's lock.
161 */
162 mutex_enter(&vp->v_interlock);
163 mutex_exit(&lmp->layerm_hashlock);
164 error = vget(vp, LK_INTERLOCK);
165 if (error) {
166 mutex_enter(&lmp->layerm_hashlock);
167 goto loop;
168 }
169 LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
170 return (vp);
171 }
172 }
173 return NULL;
174 }
175
176
177 /*
178 * Make a new layer_node node.
179 * Vp is the alias vnode, lowervp is the lower vnode.
180 * Maintain a reference to lowervp.
181 */
182 int
183 layer_node_alloc(mp, lowervp, vpp)
184 struct mount *mp;
185 struct vnode *lowervp;
186 struct vnode **vpp;
187 {
188 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
189 struct layer_node_hashhead *hd;
190 struct layer_node *xp;
191 struct vnode *vp, *nvp;
192 int error;
193 extern int (**dead_vnodeop_p)(void *);
194
195 error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p, &vp);
196 if (error != 0)
197 return (error);
198 vp->v_type = lowervp->v_type;
199 mutex_enter(&vp->v_interlock);
200 vp->v_iflag |= VI_LAYER;
201 mutex_exit(&vp->v_interlock);
202
203 xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
204 if (xp == NULL) {
205 ungetnewvnode(vp);
206 return ENOMEM;
207 }
208 if (vp->v_type == VBLK || vp->v_type == VCHR) {
209 MALLOC(vp->v_specinfo, struct specinfo *,
210 sizeof(struct specinfo), M_VNODE, M_WAITOK);
211 vp->v_hashchain = NULL;
212 vp->v_rdev = lowervp->v_rdev;
213 }
214
215 vp->v_data = xp;
216 vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
217 (lowervp->v_vflag & VV_MPSAFE);
218 xp->layer_vnode = vp;
219 xp->layer_lowervp = lowervp;
220 xp->layer_flags = 0;
221
222 /*
223 * Before we insert our new node onto the hash chains,
224 * check to see if someone else has beaten us to it.
225 * (We could have slept in MALLOC.)
226 */
227 mutex_enter(&lmp->layerm_hashlock);
228 if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
229 *vpp = nvp;
230
231 /* free the substructures we've allocated. */
232 kmem_free(xp, lmp->layerm_size);
233 if (vp->v_type == VBLK || vp->v_type == VCHR)
234 FREE(vp->v_specinfo, M_VNODE);
235
236 vp->v_type = VBAD; /* node is discarded */
237 vp->v_op = dead_vnodeop_p; /* so ops will still work */
238 vrele(vp); /* get rid of it. */
239 return (0);
240 }
241
242 /*
243 * Now lock the new node. We rely on the fact that we were passed
244 * a locked vnode. If the lower node is exporting a struct lock
245 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
246 * lower one, and both are now locked. If the lower node is exporting
247 * NULL, then we copy that up and manually lock the upper node.
248 *
249 * LAYERFS_UPPERLOCK already has the test, so we use it after copying
250 * up the v_vnlock from below.
251 */
252
253 vp->v_vnlock = lowervp->v_vnlock;
254 LAYERFS_UPPERLOCK(vp, LK_EXCLUSIVE, error);
255 KASSERT(error == 0);
256
257 /*
258 * Insert the new node into the hash.
259 * Add a reference to the lower node.
260 */
261
262 *vpp = vp;
263 VREF(lowervp);
264 hd = LAYER_NHASH(lmp, lowervp);
265 LIST_INSERT_HEAD(hd, xp, layer_hash);
266 uvm_vnp_setsize(vp, 0);
267 mutex_exit(&lmp->layerm_hashlock);
268 return (0);
269 }
270
271
272 /*
273 * Try to find an existing layer_node vnode refering
274 * to it, otherwise make a new layer_node vnode which
275 * contains a reference to the lower vnode.
276 *
277 * >>> we assume that the lower node is already locked upon entry, so we
278 * propagate the lock state to upper node <<
279 */
280 int
281 layer_node_create(mp, lowervp, newvpp)
282 struct mount *mp;
283 struct vnode *lowervp;
284 struct vnode **newvpp;
285 {
286 struct vnode *aliasvp;
287 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
288
289 mutex_enter(&lmp->layerm_hashlock);
290 aliasvp = layer_node_find(mp, lowervp);
291 if (aliasvp != NULL) {
292 /*
293 * layer_node_find has taken another reference
294 * to the alias vnode and moved the lock holding to
295 * aliasvp
296 */
297 #ifdef LAYERFS_DIAGNOSTIC
298 if (layerfs_debug)
299 vprint("layer_node_create: exists", aliasvp);
300 #endif
301 } else {
302 int error;
303
304 mutex_exit(&lmp->layerm_hashlock);
305
306 /*
307 * Get new vnode.
308 */
309 #ifdef LAYERFS_DIAGNOSTIC
310 if (layerfs_debug)
311 printf("layer_node_create: create new alias vnode\n");
312 #endif
313
314 /*
315 * Make new vnode reference the layer_node.
316 */
317 if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
318 return error;
319
320 /*
321 * aliasvp is already VREF'd by getnewvnode()
322 */
323 }
324
325 /*
326 * Now that we have VREF'd the upper vnode, release the reference
327 * to the lower node. The existence of the layer_node retains one
328 * reference to the lower node.
329 */
330 vrele(lowervp);
331
332 #ifdef DIAGNOSTIC
333 if (lowervp->v_usecount < 1) {
334 /* Should never happen... */
335 vprint("layer_node_create: alias", aliasvp);
336 vprint("layer_node_create: lower", lowervp);
337 panic("layer_node_create: lower has 0 usecount.");
338 }
339 #endif
340
341 #ifdef LAYERFS_DIAGNOSTIC
342 if (layerfs_debug)
343 vprint("layer_node_create: alias", aliasvp);
344 #endif
345 *newvpp = aliasvp;
346 return (0);
347 }
348
349 #ifdef LAYERFS_DIAGNOSTIC
350 struct vnode *
351 layer_checkvp(vp, fil, lno)
352 struct vnode *vp;
353 const 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 #endif
400