2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
38 * $FreeBSD: src/sys/miscfs/nullfs/null_subr.c,v 1.21.2.4 2001/06/26 04:20:09 bp Exp $
39 * $DragonFly: src/sys/vfs/nullfs/Attic/null_subr.c,v 1.17 2004/12/17 00:18:30 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
51 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
55 * Each cache entry holds a reference to the lower vnode
56 * along with a pointer to the alias vnode. When an
57 * entry is added the lower vnode is vref'd. When the
58 * alias is removed the lower vnode is vrele'd.
61 #define NULL_NHASH(vp) \
62 (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
64 static struct null_node **null_node_hashtbl;
65 static u_long null_node_hash;
66 static struct lwkt_token null_ihash_token;
68 static MALLOC_DEFINE(M_NULLFSHASH, "NULLFS hash", "NULLFS hash table");
69 MALLOC_DEFINE(M_NULLFSNODE, "NULLFS node", "NULLFS vnode private part");
71 static int null_node_alloc(struct mount *mp, struct vnode *lowervp,
74 null_node_find(struct mount *mp, struct vnode *lowervp);
77 * Initialise cache headers
80 nullfs_init(struct vfsconf *vfsp)
82 NULLFSDEBUG("nullfs_init\n"); /* printed during system boot */
84 while (null_node_hash < desiredvnodes)
86 null_node_hashtbl = malloc(sizeof(void *) * null_node_hash,
87 M_NULLFSHASH, M_WAITOK|M_ZERO);
89 lwkt_token_init(&null_ihash_token);
94 nullfs_uninit(struct vfsconf *vfsp)
96 if (null_node_hashtbl) {
97 free(null_node_hashtbl, M_NULLFSHASH);
98 null_node_hashtbl = NULL;
104 * Return a vref'ed alias for lower vnode if already exists, else 0.
105 * Lower vnode should be locked (but with no additional refs) on entry
106 * and will be unlocked on return if the search was successful, and left
107 * locked if the search was not successful.
109 static struct vnode *
110 null_node_find(struct mount *mp, struct vnode *lowervp)
112 struct thread *td = curthread; /* XXX */
113 struct null_node *np;
114 struct null_node *xp;
118 lwkt_gettoken(&ilock, &null_ihash_token);
120 for (np = *NULL_NHASH(lowervp); np; np = np->null_next) {
121 if (np->null_lowervp == lowervp && NULLTOV(np)->v_mount == mp) {
123 if (vget(vp, LK_EXCLUSIVE | LK_CANRECURSE, td)) {
124 printf ("null_node_find: vget failed.\n");
129 * vget() might have blocked, we have to check that
130 * our vnode is still valid.
132 xp = *NULL_NHASH(lowervp);
134 if (xp == np && xp->null_lowervp == lowervp &&
136 NULLTOV(xp)->v_mount == mp) {
142 printf ("null_node_find: node race, retry.\n");
147 * SUCCESS! Returned the locked and referenced vp
148 * and release the lock on lowervp.
150 VOP_UNLOCK(lowervp, 0, td);
151 lwkt_reltoken(&ilock);
157 * Failure, leave lowervp locked on return.
159 lwkt_reltoken(&ilock);
164 null_node_add(struct null_node *np)
166 struct null_node **npp;
167 struct null_node *n2;
170 lwkt_gettoken(&ilock, &null_ihash_token);
171 npp = NULL_NHASH(np->null_lowervp);
172 while ((n2 = *npp) != NULL) {
173 if (n2->null_lowervp == np->null_lowervp &&
174 n2->null_vnode->v_mount == np->null_vnode->v_mount) {
175 lwkt_reltoken(&ilock);
178 npp = &n2->null_next;
180 np->null_next = NULL;
182 lwkt_reltoken(&ilock);
187 null_node_rem(struct null_node *np)
189 struct null_node **npp;
190 struct null_node *n2;
193 lwkt_gettoken(&ilock, &null_ihash_token);
194 npp = NULL_NHASH(np->null_lowervp);
195 while ((n2 = *npp) != NULL) {
198 npp = &n2->null_next;
201 *npp = np->null_next;
202 np->null_next = NULL;
203 lwkt_reltoken(&ilock);
207 * Make a new null_node node. vp is the null mount vnode, lowervp is the
208 * lower vnode. Maintain a reference to (lowervp). lowervp must be
212 null_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
214 struct null_node *np;
222 * If we have already hashed the vp we can just return it.
224 *vpp = null_node_find(mp, lowervp);
229 * lowervp is locked but not referenced at this point.
231 MALLOC(np, struct null_node *, sizeof(struct null_node),
232 M_NULLFSNODE, M_WAITOK);
234 error = getnewvnode(VT_NULL, mp, vpp, 0, LK_CANRECURSE);
236 FREE(np, M_NULLFSNODE);
242 * Set up the np/vp relationship and set the lower vnode.
245 * When nullfs encounters sockets or device nodes, it
246 * has a hard time working with the normal vp union, probably
247 * because the device has not yet been opened. Needs investigation.
249 vp->v_type = lowervp->v_type;
250 if (vp->v_type == VCHR || vp->v_type == VBLK)
251 addaliasu(vp, lowervp->v_udev);
253 vp->v_un = lowervp->v_un; /* XXX why this assignment? */
255 np->null_lowervp = lowervp;
258 * Our new vnode is already VX locked (which is effective
259 * LK_THISLAYER, which is what we want).
263 * Try to add our new node to the hash table. If a collision
264 * occurs someone else beat us to it and we need to destroy the
267 if (null_node_add(np) != 0) {
268 free(np, M_NULLFSNODE);
274 * Finish up. Link the vnode and null_node together, ref lowervp
275 * for the null node. lowervp is already locked so the lock state
276 * is already properly synchronized.
278 * Set the vnode up to reclaim as quickly as possible
288 * Try to find an existing null_node vnode refering to the given underlying
289 * vnode (which should be locked and referenced). If no vnode found, create
290 * a new null_node vnode which contains a reference to the lower vnode.
293 null_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp)
295 struct vnode *aliasvp;
297 aliasvp = null_node_find(mp, lowervp);
300 * null_node_find() has unlocked lowervp for us, so we just
301 * have to get rid of the reference.
305 vprint("null_node_create: exists", aliasvp);
311 * Get new vnode. Note that lowervp is locked and referenced
312 * at this point (as it was passed to us).
314 NULLFSDEBUG("null_node_create: create new alias vnode\n");
317 * Make new vnode reference the null_node.
319 error = null_node_alloc(mp, lowervp, &aliasvp);
325 * aliasvp is already locked and ref'd by getnewvnode()
330 if (lowervp->v_usecount < 1) {
331 /* Should never happen... */
332 vprint ("null_node_create: alias ", aliasvp);
333 vprint ("null_node_create: lower ", lowervp);
334 panic ("null_node_create: lower has 0 usecount.");
339 vprint("null_node_create: alias", aliasvp);
340 vprint("null_node_create: lower", lowervp);
351 #define null_checkvp_barrier 1
353 #define null_checkvp_barrier 0
357 null_checkvp(struct vnode *vp, char *fil, int lno)
359 struct null_node *a = VTONULL(vp);
360 if (a->null_lowervp == NULLVP) {
361 /* Should never happen */
363 printf("vp = %p, ZERO ptr\n", (void *)vp);
364 for (p = (u_long *) a, i = 0; i < 8; i++)
365 printf(" %lx", p[i]);
367 /* wait for debugger */
368 while (null_checkvp_barrier) /*WAIT*/ ;
369 panic("null_checkvp");
371 if (a->null_lowervp->v_usecount < 1) {
373 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
374 for (p = (u_long *) a, i = 0; i < 8; i++)
375 printf(" %lx", p[i]);
377 /* wait for debugger */
378 while (null_checkvp_barrier) /*WAIT*/ ;
379 panic ("null with unref'ed lowervp");
382 printf("null %x/%d -> %x/%d [%s, %d]\n",
383 NULLTOV(a), NULLTOV(a)->v_usecount,
384 a->null_lowervp, a->null_lowervp->v_usecount,
387 return a->null_lowervp;