2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/rwlock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
47 #include <sys/vnode.h>
49 #include <fs/nullfs/null.h>
53 * Each cache entry holds a reference to the lower vnode
54 * along with a pointer to the alias vnode. When an
55 * entry is added the lower vnode is VREF'd. When the
56 * alias is removed the lower vnode is vrele'd.
59 #define NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask])
61 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
62 static struct rwlock null_hash_lock;
63 static u_long null_hash_mask;
65 static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
66 MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
68 static void null_hashins(struct mount *, struct null_node *);
71 * Initialise cache headers
74 nullfs_init(struct vfsconf *vfsp)
77 null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH,
79 rw_init(&null_hash_lock, "nullhs");
84 nullfs_uninit(struct vfsconf *vfsp)
87 rw_destroy(&null_hash_lock);
88 hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask);
93 * Return a VREF'ed alias for lower vnode if already exists, else 0.
94 * Lower vnode should be locked on entry and will be left locked on exit.
97 null_hashget_locked(struct mount *mp, struct vnode *lowervp)
99 struct null_node_hashhead *hd;
103 ASSERT_VOP_LOCKED(lowervp, "null_hashget");
104 rw_assert(&null_hash_lock, RA_LOCKED);
107 * Find hash base, and then search the (two-way) linked
108 * list looking for a null_node structure which is referencing
109 * the lower vnode. If found, the increment the null_node
110 * reference count (but NOT the lower vnode's VREF counter).
112 hd = NULL_NHASH(lowervp);
113 LIST_FOREACH(a, hd, null_hash) {
114 if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
116 * Since we have the lower node locked the nullfs
117 * node can not be in the process of recycling. If
118 * it had been recycled before we grabed the lower
119 * lock it would not have been found on the hash.
130 null_hashget(struct mount *mp, struct vnode *lowervp)
132 struct null_node_hashhead *hd;
135 hd = NULL_NHASH(lowervp);
139 rw_rlock(&null_hash_lock);
140 vp = null_hashget_locked(mp, lowervp);
141 rw_runlock(&null_hash_lock);
147 null_hashins(struct mount *mp, struct null_node *xp)
149 struct null_node_hashhead *hd;
151 struct null_node *oxp;
154 rw_assert(&null_hash_lock, RA_WLOCKED);
156 hd = NULL_NHASH(xp->null_lowervp);
158 LIST_FOREACH(oxp, hd, null_hash) {
159 if (oxp->null_lowervp == xp->null_lowervp &&
160 NULLTOV(oxp)->v_mount == mp) {
161 VNASSERT(0, NULLTOV(oxp),
162 ("vnode already in hash"));
166 LIST_INSERT_HEAD(hd, xp, null_hash);
170 null_destroy_proto(struct vnode *vp, void *xp)
173 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
176 vp->v_vnlock = &vp->v_lock;
177 vp->v_op = &dead_vnodeops;
181 free(xp, M_NULLFSNODE);
185 * Make a new or get existing nullfs node.
186 * Vp is the alias vnode, lowervp is the lower vnode.
188 * The lowervp assumed to be locked and having "spare" reference. This routine
189 * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
190 * the caller's "spare" reference to created nullfs vnode.
193 null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
195 struct null_node *xp;
199 ASSERT_VOP_LOCKED(lowervp, "lowervp");
200 VNPASS(lowervp->v_usecount > 0, lowervp);
202 /* Lookup the hash firstly. */
203 *vpp = null_hashget(mp, lowervp);
210 * We do not serialize vnode creation, instead we will check for
211 * duplicates later, when adding new vnode to hash.
212 * Note that duplicate can only appear in hash if the lowervp is
215 xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK);
217 error = getnewvnode("nullfs", mp, &null_vnodeops, &vp);
220 free(xp, M_NULLFSNODE);
224 VNPASS(vp->v_object == NULL, vp);
225 VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) == 0, vp);
227 rw_wlock(&null_hash_lock);
229 xp->null_lowervp = lowervp;
231 vp->v_type = lowervp->v_type;
233 vp->v_vnlock = lowervp->v_vnlock;
234 *vpp = null_hashget_locked(mp, lowervp);
236 rw_wunlock(&null_hash_lock);
238 null_destroy_proto(vp, xp);
243 * We might miss the case where lower vnode sets VIRF_PGREAD
244 * some time after construction, which is typical case.
245 * null_open rechecks.
247 if ((vn_irflag_read(lowervp) & VIRF_PGREAD) != 0) {
248 MPASS(lowervp->v_object != NULL);
249 vp->v_object = lowervp->v_object;
250 vn_irflag_set(vp, VIRF_PGREAD);
252 if (lowervp == MOUNTTONULLMOUNT(mp)->nullm_lowerrootvp)
253 vp->v_vflag |= VV_ROOT;
255 error = insmntque1(vp, mp);
257 rw_wunlock(&null_hash_lock);
260 null_destroy_proto(vp, xp);
264 null_hashins(mp, xp);
265 rw_wunlock(&null_hash_lock);
272 * Remove node from hash.
275 null_hashrem(struct null_node *xp)
278 rw_wlock(&null_hash_lock);
279 LIST_REMOVE(xp, null_hash);
280 rw_wunlock(&null_hash_lock);
286 null_checkvp(struct vnode *vp, char *fil, int lno)
288 struct null_node *a = VTONULL(vp);
292 * Can't do this check because vop_reclaim runs
293 * with a funny vop vector.
295 if (vp->v_op != null_vnodeop_p) {
296 printf ("null_checkvp: on non-null-node\n");
297 panic("null_checkvp");
300 if (a->null_lowervp == NULLVP) {
301 /* Should never happen */
302 panic("null_checkvp %p", vp);
304 VI_LOCK_FLAGS(a->null_lowervp, MTX_DUPOK);
305 if (a->null_lowervp->v_usecount < 1)
306 panic ("null with unref'ed lowervp, vp %p lvp %p",
307 vp, a->null_lowervp);
308 VI_UNLOCK(a->null_lowervp);
310 printf("null %x/%d -> %x/%d [%s, %d]\n",
311 NULLTOV(a), vrefcnt(NULLTOV(a)),
312 a->null_lowervp, vrefcnt(a->null_lowervp),
315 return (a->null_lowervp);