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.12 2004/08/17 18:57:34 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) */
52 #define NNULLNODECACHE 16
56 * Each cache entry holds a reference to the lower vnode
57 * along with a pointer to the alias vnode. When an
58 * entry is added the lower vnode is vref'd. When the
59 * alias is removed the lower vnode is vrele'd.
62 #define NULL_NHASH(vp) \
63 (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
65 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
66 static u_long null_node_hash;
67 struct lock null_hashlock;
69 static MALLOC_DEFINE(M_NULLFSHASH, "NULLFS hash", "NULLFS hash table");
70 MALLOC_DEFINE(M_NULLFSNODE, "NULLFS node", "NULLFS vnode private part");
72 static int null_node_alloc(struct mount *mp, struct vnode *lowervp,
75 null_node_find(struct mount *mp, struct vnode *lowervp);
78 * Initialise cache headers
81 nullfs_init(struct vfsconf *vfsp)
83 NULLFSDEBUG("nullfs_init\n"); /* printed during system boot */
84 null_node_hashtbl = hashinit(NNULLNODECACHE, M_NULLFSHASH, &null_node_hash);
85 lockinit(&null_hashlock, 0, "nullhs", 0, 0);
90 nullfs_uninit(struct vfsconf *vfsp)
92 if (null_node_hashtbl) {
93 free(null_node_hashtbl, M_NULLFSHASH);
99 * Return a vref'ed alias for lower vnode if already exists, else 0.
100 * Lower vnode should be locked on entry and will be left locked on exit.
102 static struct vnode *
103 null_node_find(struct mount *mp, struct vnode *lowervp)
105 struct thread *td = curthread; /* XXX */
106 struct null_node_hashhead *hd;
111 * Find hash base, and then search the (two-way) linked
112 * list looking for a null_node structure which is referencing
113 * the lower vnode. If found, the increment the null_node
114 * reference count (but NOT the lower vnode's vref counter).
116 hd = NULL_NHASH(lowervp);
118 lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
119 LIST_FOREACH(a, hd, null_hash) {
120 if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
122 lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
124 * We need vget for the VXLOCK
125 * stuff, but we don't want to lock
128 if (vget(vp, NULL, LK_EXCLUSIVE | LK_CANRECURSE, td)) {
129 printf ("null_node_find: vget failed.\n");
132 VOP_UNLOCK(lowervp, NULL, 0, td);
136 lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
143 * Make a new null_node node.
144 * Vp is the alias vnode, lofsvp is the lower vnode.
145 * Maintain a reference to (lowervp).
148 null_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
150 struct thread *td = curthread; /* XXX */
151 struct null_node_hashhead *hd;
152 struct null_node *xp;
153 struct vnode *othervp, *vp;
157 * Do the MALLOC before the getnewvnode since doing so afterward
158 * might cause a bogus v_data pointer to get dereferenced
159 * elsewhere if MALLOC should block.
161 MALLOC(xp, struct null_node *, sizeof(struct null_node),
162 M_NULLFSNODE, M_WAITOK);
164 error = getnewvnode(VT_NULL, mp, mp->mnt_vn_ops, vpp);
166 FREE(xp, M_NULLFSNODE);
171 vp->v_type = lowervp->v_type;
175 * When nullfs encounters sockets or device nodes, it
176 * has a hard time working with the normal vp union.
177 * This still needs to be investigated.
179 if (vp->v_type == VCHR || vp->v_type == VBLK)
180 addaliasu(vp, lowervp->v_udev);
182 vp->v_un = lowervp->v_un;
183 lockinit(&xp->null_lock, 0, "nullnode", 0, LK_CANRECURSE);
186 xp->null_lowervp = lowervp;
188 * Before we insert our new node onto the hash chains,
189 * check to see if someone else has beaten us to it.
190 * (We could have slept in MALLOC.)
192 othervp = null_node_find(mp, lowervp);
195 FREE(xp, M_NULLFSNODE);
196 vp->v_type = VBAD; /* node is discarded */
204 * Now lock the new node. We rely on the fact that we were passed
205 * a locked vnode. If the lower node is exporting a struct lock
206 * (v_vnlock != NULL) then we just set the upper v_vnlock to the
207 * lower one, and both are now locked. If the lower node is exporting
208 * NULL, then we copy that up and manually lock the new vnode.
211 lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, td);
212 vp->v_vnlock = lowervp->v_vnlock;
213 error = VOP_LOCK(vp, NULL, LK_EXCLUSIVE | LK_THISLAYER, td);
215 panic("null_node_alloc: can't lock new vnode\n");
218 hd = NULL_NHASH(lowervp);
219 LIST_INSERT_HEAD(hd, xp, null_hash);
220 lockmgr(&null_hashlock, LK_RELEASE, NULL, td);
226 * Try to find an existing null_node vnode refering to the given underlying
227 * vnode (which should be locked). If no vnode found, create a new null_node
228 * vnode which contains a reference to the lower vnode.
231 null_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp)
233 struct vnode *aliasvp;
235 aliasvp = null_node_find(mp, lowervp);
238 * null_node_find has taken another reference
239 * to the alias vnode.
243 vprint("null_node_create: exists", aliasvp);
251 NULLFSDEBUG("null_node_create: create new alias vnode\n");
254 * Make new vnode reference the null_node.
256 error = null_node_alloc(mp, lowervp, &aliasvp);
261 * aliasvp is already vref'd by getnewvnode()
266 if (lowervp->v_usecount < 1) {
267 /* Should never happen... */
268 vprint ("null_node_create: alias ", aliasvp);
269 vprint ("null_node_create: lower ", lowervp);
270 panic ("null_node_create: lower has 0 usecount.");
275 vprint("null_node_create: alias", aliasvp);
276 vprint("null_node_create: lower", lowervp);
287 #define null_checkvp_barrier 1
289 #define null_checkvp_barrier 0
293 null_checkvp(struct vnode *vp, char *fil, int lno)
295 struct null_node *a = VTONULL(vp);
296 if (a->null_lowervp == NULLVP) {
297 /* Should never happen */
299 printf("vp = %p, ZERO ptr\n", (void *)vp);
300 for (p = (u_long *) a, i = 0; i < 8; i++)
301 printf(" %lx", p[i]);
303 /* wait for debugger */
304 while (null_checkvp_barrier) /*WAIT*/ ;
305 panic("null_checkvp");
307 if (a->null_lowervp->v_usecount < 1) {
309 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
310 for (p = (u_long *) a, i = 0; i < 8; i++)
311 printf(" %lx", p[i]);
313 /* wait for debugger */
314 while (null_checkvp_barrier) /*WAIT*/ ;
315 panic ("null with unref'ed lowervp");
318 printf("null %x/%d -> %x/%d [%s, %d]\n",
319 NULLTOV(a), NULLTOV(a)->v_usecount,
320 a->null_lowervp, a->null_lowervp->v_usecount,
323 return a->null_lowervp;