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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
45 #include <sys/vnode.h>
47 #include <fs/nullfs/null.h>
49 #define LOG2_SIZEVNODE 8 /* log2(sizeof struct vnode) */
50 #define NNULLNODECACHE 16
54 * Each cache entry holds a reference to the lower vnode
55 * along with a pointer to the alias vnode. When an
56 * entry is added the lower vnode is VREF'd. When the
57 * alias is removed the lower vnode is vrele'd.
60 #define NULL_NHASH(vp) \
61 (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
63 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
64 static u_long null_node_hash;
65 struct mtx null_hashmtx;
67 static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
68 MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
70 static struct vnode * null_hashins(struct mount *, struct null_node *);
73 * Initialise cache headers
80 NULLFSDEBUG("nullfs_init\n"); /* printed during system boot */
81 null_node_hashtbl = hashinit(NNULLNODECACHE, M_NULLFSHASH, &null_node_hash);
82 mtx_init(&null_hashmtx, "nullhs", NULL, MTX_DEF);
91 mtx_destroy(&null_hashmtx);
92 free(null_node_hashtbl, M_NULLFSHASH);
97 * Return a VREF'ed alias for lower vnode if already exists, else 0.
98 * Lower vnode should be locked on entry and will be left locked on exit.
101 null_hashget(mp, lowervp)
103 struct vnode *lowervp;
105 struct null_node_hashhead *hd;
109 ASSERT_VOP_LOCKED(lowervp, "null_hashget");
112 * Find hash base, and then search the (two-way) linked
113 * list looking for a null_node structure which is referencing
114 * the lower vnode. If found, the increment the null_node
115 * reference count (but NOT the lower vnode's VREF counter).
117 hd = NULL_NHASH(lowervp);
118 mtx_lock(&null_hashmtx);
119 LIST_FOREACH(a, hd, null_hash) {
120 if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
122 * Since we have the lower node locked the nullfs
123 * node can not be in the process of recycling. If
124 * it had been recycled before we grabed the lower
125 * lock it would not have been found on the hash.
129 mtx_unlock(&null_hashmtx);
133 mtx_unlock(&null_hashmtx);
138 * Act like null_hashget, but add passed null_node to hash if no existing
141 static struct vnode *
144 struct null_node *xp;
146 struct null_node_hashhead *hd;
147 struct null_node *oxp;
150 hd = NULL_NHASH(xp->null_lowervp);
151 mtx_lock(&null_hashmtx);
152 LIST_FOREACH(oxp, hd, null_hash) {
153 if (oxp->null_lowervp == xp->null_lowervp &&
154 NULLTOV(oxp)->v_mount == mp) {
156 * See null_hashget for a description of this
161 mtx_unlock(&null_hashmtx);
165 LIST_INSERT_HEAD(hd, xp, null_hash);
166 mtx_unlock(&null_hashmtx);
171 null_destroy_proto(struct vnode *vp, void *xp)
174 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
177 vp->v_vnlock = &vp->v_lock;
178 vp->v_op = &dead_vnodeops;
182 free(xp, M_NULLFSNODE);
186 null_insmntque_dtr(struct vnode *vp, void *xp)
189 vput(((struct null_node *)xp)->null_lowervp);
190 null_destroy_proto(vp, xp);
194 * Make a new or get existing nullfs node.
195 * Vp is the alias vnode, lowervp is the lower vnode.
197 * The lowervp assumed to be locked and having "spare" reference. This routine
198 * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
199 * the caller's "spare" reference to created nullfs vnode.
202 null_nodeget(mp, lowervp, vpp)
204 struct vnode *lowervp;
207 struct null_node *xp;
211 ASSERT_VOP_LOCKED(lowervp, "lowervp");
212 KASSERT(lowervp->v_usecount >= 1, ("Unreferenced vnode %p", lowervp));
214 /* Lookup the hash firstly. */
215 *vpp = null_hashget(mp, lowervp);
222 * The insmntque1() call below requires the exclusive lock on
223 * the nullfs vnode. Upgrade the lock now if hash failed to
224 * provide ready to use vnode.
226 if (VOP_ISLOCKED(lowervp) != LK_EXCLUSIVE) {
227 vn_lock(lowervp, LK_UPGRADE | LK_RETRY);
228 if ((lowervp->v_iflag & VI_DOOMED) != 0) {
235 * We do not serialize vnode creation, instead we will check for
236 * duplicates later, when adding new vnode to hash.
237 * Note that duplicate can only appear in hash if the lowervp is
240 * Do the MALLOC before the getnewvnode since doing so afterward
241 * might cause a bogus v_data pointer to get dereferenced
242 * elsewhere if MALLOC should block.
244 xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
246 error = getnewvnode("null", mp, &null_vnodeops, &vp);
249 free(xp, M_NULLFSNODE);
254 xp->null_lowervp = lowervp;
255 vp->v_type = lowervp->v_type;
257 vp->v_vnlock = lowervp->v_vnlock;
258 if (vp->v_vnlock == NULL)
259 panic("null_nodeget: Passed a NULL vnlock.\n");
260 error = insmntque1(vp, mp, null_insmntque_dtr, xp);
264 * Atomically insert our new node into the hash or vget existing
265 * if someone else has beaten us to it.
267 *vpp = null_hashins(mp, xp);
270 null_destroy_proto(vp, xp);
279 * Remove node from hash.
283 struct null_node *xp;
286 mtx_lock(&null_hashmtx);
287 LIST_REMOVE(xp, null_hash);
288 mtx_unlock(&null_hashmtx);
294 null_checkvp(vp, fil, lno)
299 struct null_node *a = VTONULL(vp);
303 * Can't do this check because vop_reclaim runs
304 * with a funny vop vector.
306 if (vp->v_op != null_vnodeop_p) {
307 printf ("null_checkvp: on non-null-node\n");
308 panic("null_checkvp");
311 if (a->null_lowervp == NULLVP) {
312 /* Should never happen */
313 panic("null_checkvp %p", vp);
315 VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
316 if (a->null_lowervp->v_usecount < 1)
317 panic ("null with unref'ed lowervp, vp %p lvp %p",
318 vp, a->null_lowervp);
319 VI_UNLOCK(a->null_lowervp);
321 printf("null %x/%d -> %x/%d [%s, %d]\n",
322 NULLTOV(a), vrefcnt(NULLTOV(a)),
323 a->null_lowervp, vrefcnt(a->null_lowervp),
326 return (a->null_lowervp);