layer_subr.c revision 1.29 1 /* $NetBSD: layer_subr.c,v 1.29 2010/06/06 08:01:31 hannken 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.29 2010/06/06 08:01:31 hannken 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(void)
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(void)
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(struct mount *mp, struct vnode *lowervp)
132 {
133 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
134 struct layer_node_hashhead *hd;
135 struct layer_node *a;
136 struct vnode *vp;
137 int error;
138
139 /*
140 * Find hash base, and then search the (two-way) linked
141 * list looking for a layer_node structure which is referencing
142 * the lower vnode. If found, the increment the layer_node
143 * reference count (but NOT the lower vnode's VREF counter)
144 * and return the vnode locked.
145 */
146 hd = LAYER_NHASH(lmp, lowervp);
147 loop:
148 LIST_FOREACH(a, hd, layer_hash) {
149 if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
150 vp = LAYERTOV(a);
151 mutex_enter(&vp->v_interlock);
152 /*
153 * If we find a node being cleaned out, then
154 * ignore it and continue. A thread trying to
155 * clean out the extant layer vnode needs to
156 * acquire the shared lock (i.e. the lower
157 * vnode's lock), which our caller already holds.
158 * To allow the cleaning to succeed the current
159 * thread must make progress. So, for a brief
160 * time more than one vnode in a layered file
161 * system may refer to a single vnode in the
162 * lower file system.
163 */
164 if ((vp->v_iflag & VI_XLOCK) != 0) {
165 mutex_exit(&vp->v_interlock);
166 continue;
167 }
168 mutex_exit(&lmp->layerm_hashlock);
169 /*
170 * We must not let vget() try to lock the layer
171 * vp, since the lower vp is already locked and
172 * locking the layer vp will involve locking
173 * the lower vp.
174 */
175 error = vget(vp, LK_INTERLOCK | LK_NOWAIT);
176 if (error) {
177 kpause("layerfs", false, 1, NULL);
178 mutex_enter(&lmp->layerm_hashlock);
179 goto loop;
180 }
181 return (vp);
182 }
183 }
184 return NULL;
185 }
186
187
188 /*
189 * Make a new layer_node node.
190 * Vp is the alias vnode, lowervp is the lower vnode.
191 * Maintain a reference to lowervp.
192 */
193 int
194 layer_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
195 {
196 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
197 struct layer_node_hashhead *hd;
198 struct layer_node *xp;
199 struct vnode *vp, *nvp;
200 int error;
201 extern int (**dead_vnodeop_p)(void *);
202
203 error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p, &vp);
204 if (error != 0)
205 return (error);
206 vp->v_type = lowervp->v_type;
207 mutex_enter(&vp->v_interlock);
208 vp->v_iflag |= VI_LAYER;
209 mutex_exit(&vp->v_interlock);
210
211 xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
212 if (xp == NULL) {
213 ungetnewvnode(vp);
214 return ENOMEM;
215 }
216 if (vp->v_type == VBLK || vp->v_type == VCHR) {
217 spec_node_init(vp, lowervp->v_rdev);
218 }
219
220 vp->v_data = xp;
221 vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
222 (lowervp->v_vflag & VV_MPSAFE);
223 xp->layer_vnode = vp;
224 xp->layer_lowervp = lowervp;
225 xp->layer_flags = 0;
226
227 /*
228 * Before we insert our new node onto the hash chains,
229 * check to see if someone else has beaten us to it.
230 * (We could have slept in MALLOC.)
231 */
232 mutex_enter(&lmp->layerm_hashlock);
233 if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
234 *vpp = nvp;
235
236 /* free the substructures we've allocated. */
237 kmem_free(xp, lmp->layerm_size);
238 if (vp->v_type == VBLK || vp->v_type == VCHR)
239 spec_node_destroy(vp);
240
241 vp->v_type = VBAD; /* node is discarded */
242 vp->v_op = dead_vnodeop_p; /* so ops will still work */
243 vrele(vp); /* get rid of it. */
244 return (0);
245 }
246
247 /*
248 * Insert the new node into the hash.
249 * Add a reference to the lower node.
250 */
251
252 *vpp = vp;
253 vref(lowervp);
254 hd = LAYER_NHASH(lmp, lowervp);
255 LIST_INSERT_HEAD(hd, xp, layer_hash);
256 uvm_vnp_setsize(vp, 0);
257 mutex_exit(&lmp->layerm_hashlock);
258 return (0);
259 }
260
261
262 /*
263 * Try to find an existing layer_node vnode refering
264 * to it, otherwise make a new layer_node vnode which
265 * contains a reference to the lower vnode.
266 *
267 * >>> we assume that the lower node is already locked upon entry, so we
268 * propagate the lock state to upper node <<
269 */
270 int
271 layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp)
272 {
273 struct vnode *aliasvp;
274 struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
275
276 mutex_enter(&lmp->layerm_hashlock);
277 aliasvp = layer_node_find(mp, lowervp);
278 if (aliasvp != NULL) {
279 /*
280 * layer_node_find has taken another reference
281 * to the alias vnode and moved the lock holding to
282 * aliasvp
283 */
284 #ifdef LAYERFS_DIAGNOSTIC
285 if (layerfs_debug)
286 vprint("layer_node_create: exists", aliasvp);
287 #endif
288 } else {
289 int error;
290
291 mutex_exit(&lmp->layerm_hashlock);
292
293 /*
294 * Get new vnode.
295 */
296 #ifdef LAYERFS_DIAGNOSTIC
297 if (layerfs_debug)
298 printf("layer_node_create: create new alias vnode\n");
299 #endif
300
301 /*
302 * Make new vnode reference the layer_node.
303 */
304 if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
305 return error;
306
307 /*
308 * aliasvp is already VREF'd by getnewvnode()
309 */
310 }
311
312 /*
313 * Now that we have VREF'd the upper vnode, release the reference
314 * to the lower node. The existence of the layer_node retains one
315 * reference to the lower node.
316 */
317 vrele(lowervp);
318
319 #ifdef DIAGNOSTIC
320 if (lowervp->v_usecount < 1) {
321 /* Should never happen... */
322 vprint("layer_node_create: alias", aliasvp);
323 vprint("layer_node_create: lower", lowervp);
324 panic("layer_node_create: lower has 0 usecount.");
325 }
326 #endif
327
328 #ifdef LAYERFS_DIAGNOSTIC
329 if (layerfs_debug)
330 vprint("layer_node_create: alias", aliasvp);
331 #endif
332 *newvpp = aliasvp;
333 return (0);
334 }
335
336 #ifdef LAYERFS_DIAGNOSTIC
337 struct vnode *
338 layer_checkvp(struct vnode *vp, const char *fil, int lno)
339 {
340 struct layer_node *a = VTOLAYER(vp);
341 #ifdef notyet
342 /*
343 * Can't do this check because vop_reclaim runs
344 * with a funny vop vector.
345 *
346 * WRS - no it doesnt...
347 */
348 if (vp->v_op != layer_vnodeop_p) {
349 printf ("layer_checkvp: on non-layer-node\n");
350 #ifdef notyet
351 while (layer_checkvp_barrier) /*WAIT*/ ;
352 #endif
353 panic("layer_checkvp");
354 };
355 #endif
356 if (a->layer_lowervp == NULL) {
357 /* Should never happen */
358 int i; u_long *p;
359 printf("vp = %p, ZERO ptr\n", vp);
360 for (p = (u_long *) a, i = 0; i < 8; i++)
361 printf(" %lx", p[i]);
362 printf("\n");
363 /* wait for debugger */
364 panic("layer_checkvp");
365 }
366 if (a->layer_lowervp->v_usecount < 1) {
367 int i; u_long *p;
368 printf("vp = %p, unref'ed lowervp\n", vp);
369 for (p = (u_long *) a, i = 0; i < 8; i++)
370 printf(" %lx", p[i]);
371 printf("\n");
372 /* wait for debugger */
373 panic ("layer with unref'ed lowervp");
374 };
375 #ifdef notnow
376 printf("layer %p/%d -> %p/%d [%s, %d]\n",
377 LAYERTOV(a), LAYERTOV(a)->v_usecount,
378 a->layer_lowervp, a->layer_lowervp->v_usecount,
379 fil, lno);
380 #endif
381 return a->layer_lowervp;
382 }
383 #endif
384