layer_subr.c revision 1.29 1 1.29 hannken /* $NetBSD: layer_subr.c,v 1.29 2010/06/06 08:01:31 hannken Exp $ */
2 1.1 wrstuden
3 1.1 wrstuden /*
4 1.1 wrstuden * Copyright (c) 1999 National Aeronautics & Space Administration
5 1.1 wrstuden * All rights reserved.
6 1.1 wrstuden *
7 1.1 wrstuden * This software was written by William Studenmund of the
8 1.8 wiz * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9 1.1 wrstuden *
10 1.1 wrstuden * Redistribution and use in source and binary forms, with or without
11 1.1 wrstuden * modification, are permitted provided that the following conditions
12 1.1 wrstuden * are met:
13 1.1 wrstuden * 1. Redistributions of source code must retain the above copyright
14 1.1 wrstuden * notice, this list of conditions and the following disclaimer.
15 1.1 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wrstuden * notice, this list of conditions and the following disclaimer in the
17 1.1 wrstuden * documentation and/or other materials provided with the distribution.
18 1.5 soren * 3. Neither the name of the National Aeronautics & Space Administration
19 1.1 wrstuden * nor the names of its contributors may be used to endorse or promote
20 1.1 wrstuden * products derived from this software without specific prior written
21 1.1 wrstuden * permission.
22 1.1 wrstuden *
23 1.1 wrstuden * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24 1.1 wrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 wrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27 1.1 wrstuden * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 1.1 wrstuden * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 wrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 wrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 wrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 wrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 wrstuden * POSSIBILITY OF SUCH DAMAGE.
34 1.1 wrstuden */
35 1.1 wrstuden /*
36 1.1 wrstuden * Copyright (c) 1992, 1993
37 1.1 wrstuden * The Regents of the University of California. All rights reserved.
38 1.1 wrstuden *
39 1.1 wrstuden * This code is derived from software donated to Berkeley by
40 1.1 wrstuden * Jan-Simon Pendry.
41 1.1 wrstuden *
42 1.1 wrstuden * Redistribution and use in source and binary forms, with or without
43 1.1 wrstuden * modification, are permitted provided that the following conditions
44 1.1 wrstuden * are met:
45 1.1 wrstuden * 1. Redistributions of source code must retain the above copyright
46 1.1 wrstuden * notice, this list of conditions and the following disclaimer.
47 1.1 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 wrstuden * notice, this list of conditions and the following disclaimer in the
49 1.1 wrstuden * documentation and/or other materials provided with the distribution.
50 1.15 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 wrstuden * may be used to endorse or promote products derived from this software
52 1.1 wrstuden * without specific prior written permission.
53 1.1 wrstuden *
54 1.1 wrstuden * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 wrstuden * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 wrstuden * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 wrstuden * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 wrstuden * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 wrstuden * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 wrstuden * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 wrstuden * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 wrstuden * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 wrstuden * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 wrstuden * SUCH DAMAGE.
65 1.1 wrstuden *
66 1.1 wrstuden * from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
67 1.1 wrstuden * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
68 1.1 wrstuden */
69 1.9 lukem
70 1.9 lukem #include <sys/cdefs.h>
71 1.29 hannken __KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.29 2010/06/06 08:01:31 hannken Exp $");
72 1.1 wrstuden
73 1.1 wrstuden #include <sys/param.h>
74 1.1 wrstuden #include <sys/systm.h>
75 1.1 wrstuden #include <sys/proc.h>
76 1.1 wrstuden #include <sys/time.h>
77 1.1 wrstuden #include <sys/vnode.h>
78 1.1 wrstuden #include <sys/mount.h>
79 1.1 wrstuden #include <sys/namei.h>
80 1.23 ad #include <sys/kmem.h>
81 1.1 wrstuden #include <sys/malloc.h>
82 1.23 ad
83 1.1 wrstuden #include <miscfs/specfs/specdev.h>
84 1.1 wrstuden #include <miscfs/genfs/layer.h>
85 1.1 wrstuden #include <miscfs/genfs/layer_extern.h>
86 1.1 wrstuden
87 1.1 wrstuden #define NLAYERNODECACHE 16
88 1.1 wrstuden
89 1.16 erh #ifdef LAYERFS_DIAGNOSTIC
90 1.16 erh int layerfs_debug = 1;
91 1.16 erh #endif
92 1.16 erh
93 1.1 wrstuden /*
94 1.1 wrstuden * layer cache:
95 1.1 wrstuden * Each cache entry holds a reference to the lower vnode
96 1.1 wrstuden * along with a pointer to the alias vnode. When an
97 1.1 wrstuden * entry is added the lower vnode is VREF'd. When the
98 1.1 wrstuden * alias is removed the lower vnode is vrele'd.
99 1.1 wrstuden */
100 1.1 wrstuden
101 1.1 wrstuden /*
102 1.1 wrstuden * Initialise cache headers
103 1.1 wrstuden */
104 1.1 wrstuden void
105 1.27 cegger layerfs_init(void)
106 1.1 wrstuden {
107 1.6 jdolecek #ifdef LAYERFS_DIAGNOSTIC
108 1.16 erh if (layerfs_debug)
109 1.16 erh printf("layerfs_init\n"); /* printed during system boot */
110 1.6 jdolecek #endif
111 1.6 jdolecek }
112 1.1 wrstuden
113 1.6 jdolecek /*
114 1.6 jdolecek * Free global resources of layerfs.
115 1.6 jdolecek */
116 1.6 jdolecek void
117 1.27 cegger layerfs_done(void)
118 1.6 jdolecek {
119 1.1 wrstuden #ifdef LAYERFS_DIAGNOSTIC
120 1.16 erh if (layerfs_debug)
121 1.16 erh printf("layerfs_done\n"); /* printed on layerfs detach */
122 1.1 wrstuden #endif
123 1.1 wrstuden }
124 1.1 wrstuden
125 1.1 wrstuden /*
126 1.21 chs * Return a locked, VREF'ed alias for lower vnode if already exists, else NULL.
127 1.21 chs * The layermp's hashlock must be held on entry.
128 1.21 chs * It will be held upon return iff we return NULL.
129 1.1 wrstuden */
130 1.1 wrstuden struct vnode *
131 1.26 dsl layer_node_find(struct mount *mp, struct vnode *lowervp)
132 1.1 wrstuden {
133 1.1 wrstuden struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
134 1.1 wrstuden struct layer_node_hashhead *hd;
135 1.1 wrstuden struct layer_node *a;
136 1.1 wrstuden struct vnode *vp;
137 1.21 chs int error;
138 1.1 wrstuden
139 1.1 wrstuden /*
140 1.1 wrstuden * Find hash base, and then search the (two-way) linked
141 1.1 wrstuden * list looking for a layer_node structure which is referencing
142 1.1 wrstuden * the lower vnode. If found, the increment the layer_node
143 1.1 wrstuden * reference count (but NOT the lower vnode's VREF counter)
144 1.1 wrstuden * and return the vnode locked.
145 1.1 wrstuden */
146 1.1 wrstuden hd = LAYER_NHASH(lmp, lowervp);
147 1.1 wrstuden loop:
148 1.21 chs LIST_FOREACH(a, hd, layer_hash) {
149 1.1 wrstuden if (a->layer_lowervp == lowervp && LAYERTOV(a)->v_mount == mp) {
150 1.1 wrstuden vp = LAYERTOV(a);
151 1.24 ad mutex_enter(&vp->v_interlock);
152 1.1 wrstuden /*
153 1.24 ad * If we find a node being cleaned out, then
154 1.24 ad * ignore it and continue. A thread trying to
155 1.24 ad * clean out the extant layer vnode needs to
156 1.24 ad * acquire the shared lock (i.e. the lower
157 1.24 ad * vnode's lock), which our caller already holds.
158 1.24 ad * To allow the cleaning to succeed the current
159 1.24 ad * thread must make progress. So, for a brief
160 1.24 ad * time more than one vnode in a layered file
161 1.24 ad * system may refer to a single vnode in the
162 1.24 ad * lower file system.
163 1.1 wrstuden */
164 1.24 ad if ((vp->v_iflag & VI_XLOCK) != 0) {
165 1.24 ad mutex_exit(&vp->v_interlock);
166 1.24 ad continue;
167 1.24 ad }
168 1.22 ad mutex_exit(&lmp->layerm_hashlock);
169 1.24 ad /*
170 1.24 ad * We must not let vget() try to lock the layer
171 1.24 ad * vp, since the lower vp is already locked and
172 1.24 ad * locking the layer vp will involve locking
173 1.29 hannken * the lower vp.
174 1.24 ad */
175 1.24 ad error = vget(vp, LK_INTERLOCK | LK_NOWAIT);
176 1.21 chs if (error) {
177 1.24 ad kpause("layerfs", false, 1, NULL);
178 1.22 ad mutex_enter(&lmp->layerm_hashlock);
179 1.1 wrstuden goto loop;
180 1.21 chs }
181 1.1 wrstuden return (vp);
182 1.1 wrstuden }
183 1.1 wrstuden }
184 1.1 wrstuden return NULL;
185 1.1 wrstuden }
186 1.1 wrstuden
187 1.1 wrstuden
188 1.1 wrstuden /*
189 1.1 wrstuden * Make a new layer_node node.
190 1.1 wrstuden * Vp is the alias vnode, lowervp is the lower vnode.
191 1.1 wrstuden * Maintain a reference to lowervp.
192 1.1 wrstuden */
193 1.1 wrstuden int
194 1.26 dsl layer_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
195 1.1 wrstuden {
196 1.1 wrstuden struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
197 1.1 wrstuden struct layer_node_hashhead *hd;
198 1.1 wrstuden struct layer_node *xp;
199 1.1 wrstuden struct vnode *vp, *nvp;
200 1.1 wrstuden int error;
201 1.17 xtraeme extern int (**dead_vnodeop_p)(void *);
202 1.1 wrstuden
203 1.23 ad error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p, &vp);
204 1.23 ad if (error != 0)
205 1.1 wrstuden return (error);
206 1.1 wrstuden vp->v_type = lowervp->v_type;
207 1.23 ad mutex_enter(&vp->v_interlock);
208 1.22 ad vp->v_iflag |= VI_LAYER;
209 1.23 ad mutex_exit(&vp->v_interlock);
210 1.1 wrstuden
211 1.23 ad xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
212 1.23 ad if (xp == NULL) {
213 1.23 ad ungetnewvnode(vp);
214 1.23 ad return ENOMEM;
215 1.23 ad }
216 1.1 wrstuden if (vp->v_type == VBLK || vp->v_type == VCHR) {
217 1.25 ad spec_node_init(vp, lowervp->v_rdev);
218 1.1 wrstuden }
219 1.1 wrstuden
220 1.1 wrstuden vp->v_data = xp;
221 1.23 ad vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
222 1.23 ad (lowervp->v_vflag & VV_MPSAFE);
223 1.1 wrstuden xp->layer_vnode = vp;
224 1.1 wrstuden xp->layer_lowervp = lowervp;
225 1.1 wrstuden xp->layer_flags = 0;
226 1.21 chs
227 1.1 wrstuden /*
228 1.1 wrstuden * Before we insert our new node onto the hash chains,
229 1.1 wrstuden * check to see if someone else has beaten us to it.
230 1.1 wrstuden * (We could have slept in MALLOC.)
231 1.1 wrstuden */
232 1.22 ad mutex_enter(&lmp->layerm_hashlock);
233 1.1 wrstuden if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
234 1.1 wrstuden *vpp = nvp;
235 1.1 wrstuden
236 1.1 wrstuden /* free the substructures we've allocated. */
237 1.23 ad kmem_free(xp, lmp->layerm_size);
238 1.1 wrstuden if (vp->v_type == VBLK || vp->v_type == VCHR)
239 1.25 ad spec_node_destroy(vp);
240 1.1 wrstuden
241 1.1 wrstuden vp->v_type = VBAD; /* node is discarded */
242 1.1 wrstuden vp->v_op = dead_vnodeop_p; /* so ops will still work */
243 1.1 wrstuden vrele(vp); /* get rid of it. */
244 1.1 wrstuden return (0);
245 1.1 wrstuden }
246 1.1 wrstuden
247 1.1 wrstuden /*
248 1.21 chs * Insert the new node into the hash.
249 1.21 chs * Add a reference to the lower node.
250 1.1 wrstuden */
251 1.1 wrstuden
252 1.1 wrstuden *vpp = vp;
253 1.28 pooka vref(lowervp);
254 1.1 wrstuden hd = LAYER_NHASH(lmp, lowervp);
255 1.1 wrstuden LIST_INSERT_HEAD(hd, xp, layer_hash);
256 1.7 chs uvm_vnp_setsize(vp, 0);
257 1.22 ad mutex_exit(&lmp->layerm_hashlock);
258 1.1 wrstuden return (0);
259 1.1 wrstuden }
260 1.1 wrstuden
261 1.1 wrstuden
262 1.1 wrstuden /*
263 1.1 wrstuden * Try to find an existing layer_node vnode refering
264 1.1 wrstuden * to it, otherwise make a new layer_node vnode which
265 1.1 wrstuden * contains a reference to the lower vnode.
266 1.1 wrstuden *
267 1.1 wrstuden * >>> we assume that the lower node is already locked upon entry, so we
268 1.1 wrstuden * propagate the lock state to upper node <<
269 1.1 wrstuden */
270 1.1 wrstuden int
271 1.26 dsl layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp)
272 1.1 wrstuden {
273 1.1 wrstuden struct vnode *aliasvp;
274 1.1 wrstuden struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
275 1.1 wrstuden
276 1.22 ad mutex_enter(&lmp->layerm_hashlock);
277 1.21 chs aliasvp = layer_node_find(mp, lowervp);
278 1.21 chs if (aliasvp != NULL) {
279 1.1 wrstuden /*
280 1.1 wrstuden * layer_node_find has taken another reference
281 1.1 wrstuden * to the alias vnode and moved the lock holding to
282 1.1 wrstuden * aliasvp
283 1.1 wrstuden */
284 1.1 wrstuden #ifdef LAYERFS_DIAGNOSTIC
285 1.16 erh if (layerfs_debug)
286 1.16 erh vprint("layer_node_create: exists", aliasvp);
287 1.1 wrstuden #endif
288 1.1 wrstuden } else {
289 1.1 wrstuden int error;
290 1.1 wrstuden
291 1.22 ad mutex_exit(&lmp->layerm_hashlock);
292 1.21 chs
293 1.1 wrstuden /*
294 1.1 wrstuden * Get new vnode.
295 1.1 wrstuden */
296 1.1 wrstuden #ifdef LAYERFS_DIAGNOSTIC
297 1.16 erh if (layerfs_debug)
298 1.16 erh printf("layer_node_create: create new alias vnode\n");
299 1.1 wrstuden #endif
300 1.1 wrstuden
301 1.1 wrstuden /*
302 1.1 wrstuden * Make new vnode reference the layer_node.
303 1.1 wrstuden */
304 1.1 wrstuden if ((error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp)) != 0)
305 1.1 wrstuden return error;
306 1.1 wrstuden
307 1.1 wrstuden /*
308 1.1 wrstuden * aliasvp is already VREF'd by getnewvnode()
309 1.1 wrstuden */
310 1.1 wrstuden }
311 1.1 wrstuden
312 1.1 wrstuden /*
313 1.1 wrstuden * Now that we have VREF'd the upper vnode, release the reference
314 1.19 wiz * to the lower node. The existence of the layer_node retains one
315 1.1 wrstuden * reference to the lower node.
316 1.1 wrstuden */
317 1.1 wrstuden vrele(lowervp);
318 1.1 wrstuden
319 1.1 wrstuden #ifdef DIAGNOSTIC
320 1.1 wrstuden if (lowervp->v_usecount < 1) {
321 1.1 wrstuden /* Should never happen... */
322 1.1 wrstuden vprint("layer_node_create: alias", aliasvp);
323 1.1 wrstuden vprint("layer_node_create: lower", lowervp);
324 1.1 wrstuden panic("layer_node_create: lower has 0 usecount.");
325 1.21 chs }
326 1.1 wrstuden #endif
327 1.1 wrstuden
328 1.1 wrstuden #ifdef LAYERFS_DIAGNOSTIC
329 1.16 erh if (layerfs_debug)
330 1.16 erh vprint("layer_node_create: alias", aliasvp);
331 1.1 wrstuden #endif
332 1.1 wrstuden *newvpp = aliasvp;
333 1.1 wrstuden return (0);
334 1.1 wrstuden }
335 1.1 wrstuden
336 1.16 erh #ifdef LAYERFS_DIAGNOSTIC
337 1.1 wrstuden struct vnode *
338 1.26 dsl layer_checkvp(struct vnode *vp, const char *fil, int lno)
339 1.1 wrstuden {
340 1.1 wrstuden struct layer_node *a = VTOLAYER(vp);
341 1.1 wrstuden #ifdef notyet
342 1.1 wrstuden /*
343 1.1 wrstuden * Can't do this check because vop_reclaim runs
344 1.1 wrstuden * with a funny vop vector.
345 1.1 wrstuden *
346 1.1 wrstuden * WRS - no it doesnt...
347 1.1 wrstuden */
348 1.1 wrstuden if (vp->v_op != layer_vnodeop_p) {
349 1.1 wrstuden printf ("layer_checkvp: on non-layer-node\n");
350 1.1 wrstuden #ifdef notyet
351 1.1 wrstuden while (layer_checkvp_barrier) /*WAIT*/ ;
352 1.1 wrstuden #endif
353 1.1 wrstuden panic("layer_checkvp");
354 1.1 wrstuden };
355 1.1 wrstuden #endif
356 1.1 wrstuden if (a->layer_lowervp == NULL) {
357 1.1 wrstuden /* Should never happen */
358 1.1 wrstuden int i; u_long *p;
359 1.1 wrstuden printf("vp = %p, ZERO ptr\n", vp);
360 1.1 wrstuden for (p = (u_long *) a, i = 0; i < 8; i++)
361 1.1 wrstuden printf(" %lx", p[i]);
362 1.1 wrstuden printf("\n");
363 1.1 wrstuden /* wait for debugger */
364 1.1 wrstuden panic("layer_checkvp");
365 1.1 wrstuden }
366 1.1 wrstuden if (a->layer_lowervp->v_usecount < 1) {
367 1.1 wrstuden int i; u_long *p;
368 1.1 wrstuden printf("vp = %p, unref'ed lowervp\n", vp);
369 1.1 wrstuden for (p = (u_long *) a, i = 0; i < 8; i++)
370 1.1 wrstuden printf(" %lx", p[i]);
371 1.1 wrstuden printf("\n");
372 1.1 wrstuden /* wait for debugger */
373 1.1 wrstuden panic ("layer with unref'ed lowervp");
374 1.1 wrstuden };
375 1.1 wrstuden #ifdef notnow
376 1.1 wrstuden printf("layer %p/%d -> %p/%d [%s, %d]\n",
377 1.1 wrstuden LAYERTOV(a), LAYERTOV(a)->v_usecount,
378 1.1 wrstuden a->layer_lowervp, a->layer_lowervp->v_usecount,
379 1.1 wrstuden fil, lno);
380 1.1 wrstuden #endif
381 1.1 wrstuden return a->layer_lowervp;
382 1.1 wrstuden }
383 1.16 erh #endif
384