2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/rwlock.h>
39 #include <sys/vnode.h>
41 static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table");
43 static LIST_HEAD(vfs_hash_head, vnode) *vfs_hash_tbl;
44 static LIST_HEAD(,vnode) vfs_hash_side;
45 static u_long vfs_hash_mask;
46 static struct rwlock __exclusive_cache_line vfs_hash_lock;
49 vfs_hashinit(void *dummy __unused)
52 vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask);
53 rw_init(&vfs_hash_lock, "vfs hash");
54 LIST_INIT(&vfs_hash_side);
57 /* Must be SI_ORDER_SECOND so desiredvnodes is available */
58 SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL);
61 vfs_hash_index(struct vnode *vp)
64 return (vp->v_hash + vp->v_mount->mnt_hashseed);
67 static struct vfs_hash_head *
68 vfs_hash_bucket(const struct mount *mp, u_int hash)
71 return (&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]);
75 vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td,
76 struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
83 rw_rlock(&vfs_hash_lock);
84 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
85 if (vp->v_hash != hash)
87 if (vp->v_mount != mp)
89 if (fn != NULL && fn(vp, arg))
92 rw_runlock(&vfs_hash_lock);
93 error = vget_finish(vp, flags, vs);
94 if (error == ENOENT && (flags & LK_NOWAIT) == 0)
98 if (vp->v_hash != hash ||
99 (fn != NULL && fn(vp, arg))) {
101 /* Restart the bucket walk. */
108 rw_runlock(&vfs_hash_lock);
116 vfs_hash_ref(const struct mount *mp, u_int hash, struct thread *td,
117 struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
122 rw_rlock(&vfs_hash_lock);
123 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
124 if (vp->v_hash != hash)
126 if (vp->v_mount != mp)
128 if (fn != NULL && fn(vp, arg))
131 rw_runlock(&vfs_hash_lock);
138 rw_runlock(&vfs_hash_lock);
146 vfs_hash_remove(struct vnode *vp)
149 rw_wlock(&vfs_hash_lock);
150 LIST_REMOVE(vp, v_hashlist);
151 rw_wunlock(&vfs_hash_lock);
155 vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td,
156 struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
164 rw_wlock(&vfs_hash_lock);
166 vfs_hash_bucket(vp->v_mount, hash), v_hashlist) {
167 if (vp2->v_hash != hash)
169 if (vp2->v_mount != vp->v_mount)
171 if (fn != NULL && fn(vp2, arg))
174 rw_wunlock(&vfs_hash_lock);
175 error = vget_finish(vp2, flags, vs);
176 if (error == ENOENT && (flags & LK_NOWAIT) == 0)
178 rw_wlock(&vfs_hash_lock);
179 LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist);
180 rw_wunlock(&vfs_hash_lock);
191 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
192 rw_wunlock(&vfs_hash_lock);
197 vfs_hash_rehash(struct vnode *vp, u_int hash)
199 ASSERT_VOP_ELOCKED(vp, "rehash requires excl lock");
201 rw_wlock(&vfs_hash_lock);
202 LIST_REMOVE(vp, v_hashlist);
203 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
205 rw_wunlock(&vfs_hash_lock);
209 vfs_hash_changesize(u_long newmaxvnodes)
211 struct vfs_hash_head *vfs_hash_newtbl, *vfs_hash_oldtbl;
212 u_long vfs_hash_newmask, vfs_hash_oldmask;
216 vfs_hash_newtbl = hashinit(newmaxvnodes, M_VFS_HASH,
218 /* If same hash table size, nothing to do */
219 if (vfs_hash_mask == vfs_hash_newmask) {
220 free(vfs_hash_newtbl, M_VFS_HASH);
224 * Move everything from the old hash table to the new table.
225 * None of the vnodes in the table can be recycled because to
226 * do so, they have to be removed from the hash table.
228 rw_wlock(&vfs_hash_lock);
229 vfs_hash_oldtbl = vfs_hash_tbl;
230 vfs_hash_oldmask = vfs_hash_mask;
231 vfs_hash_tbl = vfs_hash_newtbl;
232 vfs_hash_mask = vfs_hash_newmask;
233 for (i = 0; i <= vfs_hash_oldmask; i++) {
234 while ((vp = LIST_FIRST(&vfs_hash_oldtbl[i])) != NULL) {
235 LIST_REMOVE(vp, v_hashlist);
237 vfs_hash_bucket(vp->v_mount, vp->v_hash),
241 rw_wunlock(&vfs_hash_lock);
242 free(vfs_hash_oldtbl, M_VFS_HASH);