1 /*-
2 * Copyright (c) 2005 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/rwlock.h>
37 #include <sys/vnode.h>
38
39 static MALLOC_DEFINE(M_VFS_HASH, "vfs_hash", "VFS hash table");
40
41 static LIST_HEAD(vfs_hash_head, vnode) *vfs_hash_tbl;
42 static LIST_HEAD(,vnode) vfs_hash_side;
43 static u_long vfs_hash_mask;
44 static struct rwlock vfs_hash_lock;
45
46 static void
vfs_hashinit(void * dummy __unused)47 vfs_hashinit(void *dummy __unused)
48 {
49
50 vfs_hash_tbl = hashinit(desiredvnodes, M_VFS_HASH, &vfs_hash_mask);
51 rw_init(&vfs_hash_lock, "vfs hash");
52 LIST_INIT(&vfs_hash_side);
53 }
54
55 /* Must be SI_ORDER_SECOND so desiredvnodes is available */
56 SYSINIT(vfs_hash, SI_SUB_VFS, SI_ORDER_SECOND, vfs_hashinit, NULL);
57
58 u_int
vfs_hash_index(struct vnode * vp)59 vfs_hash_index(struct vnode *vp)
60 {
61
62 return (vp->v_hash + vp->v_mount->mnt_hashseed);
63 }
64
65 static struct vfs_hash_head *
vfs_hash_bucket(const struct mount * mp,u_int hash)66 vfs_hash_bucket(const struct mount *mp, u_int hash)
67 {
68
69 return (&vfs_hash_tbl[(hash + mp->mnt_hashseed) & vfs_hash_mask]);
70 }
71
72 int
vfs_hash_get(const struct mount * mp,u_int hash,int flags,struct thread * td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)73 vfs_hash_get(const struct mount *mp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
74 {
75 struct vnode *vp;
76 int error;
77
78 while (1) {
79 rw_rlock(&vfs_hash_lock);
80 LIST_FOREACH(vp, vfs_hash_bucket(mp, hash), v_hashlist) {
81 if (vp->v_hash != hash)
82 continue;
83 if (vp->v_mount != mp)
84 continue;
85 if (fn != NULL && fn(vp, arg))
86 continue;
87 vhold(vp);
88 rw_runlock(&vfs_hash_lock);
89 error = vget(vp, flags | LK_VNHELD, td);
90 if (error == ENOENT && (flags & LK_NOWAIT) == 0)
91 break;
92 if (error)
93 return (error);
94 *vpp = vp;
95 return (0);
96 }
97 if (vp == NULL) {
98 rw_runlock(&vfs_hash_lock);
99 *vpp = NULL;
100 return (0);
101 }
102 }
103 }
104
105 void
vfs_hash_remove(struct vnode * vp)106 vfs_hash_remove(struct vnode *vp)
107 {
108
109 rw_wlock(&vfs_hash_lock);
110 LIST_REMOVE(vp, v_hashlist);
111 rw_wunlock(&vfs_hash_lock);
112 }
113
114 int
vfs_hash_insert(struct vnode * vp,u_int hash,int flags,struct thread * td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)115 vfs_hash_insert(struct vnode *vp, u_int hash, int flags, struct thread *td, struct vnode **vpp, vfs_hash_cmp_t *fn, void *arg)
116 {
117 struct vnode *vp2;
118 int error;
119
120 *vpp = NULL;
121 while (1) {
122 rw_wlock(&vfs_hash_lock);
123 LIST_FOREACH(vp2,
124 vfs_hash_bucket(vp->v_mount, hash), v_hashlist) {
125 if (vp2->v_hash != hash)
126 continue;
127 if (vp2->v_mount != vp->v_mount)
128 continue;
129 if (fn != NULL && fn(vp2, arg))
130 continue;
131 vhold(vp2);
132 rw_wunlock(&vfs_hash_lock);
133 error = vget(vp2, flags | LK_VNHELD, td);
134 if (error == ENOENT && (flags & LK_NOWAIT) == 0)
135 break;
136 rw_wlock(&vfs_hash_lock);
137 LIST_INSERT_HEAD(&vfs_hash_side, vp, v_hashlist);
138 rw_wunlock(&vfs_hash_lock);
139 vput(vp);
140 if (!error)
141 *vpp = vp2;
142 return (error);
143 }
144 if (vp2 == NULL)
145 break;
146
147 }
148 vp->v_hash = hash;
149 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
150 rw_wunlock(&vfs_hash_lock);
151 return (0);
152 }
153
154 void
vfs_hash_rehash(struct vnode * vp,u_int hash)155 vfs_hash_rehash(struct vnode *vp, u_int hash)
156 {
157
158 rw_wlock(&vfs_hash_lock);
159 LIST_REMOVE(vp, v_hashlist);
160 LIST_INSERT_HEAD(vfs_hash_bucket(vp->v_mount, hash), vp, v_hashlist);
161 vp->v_hash = hash;
162 rw_wunlock(&vfs_hash_lock);
163 }
164
165 void
vfs_hash_changesize(int newmaxvnodes)166 vfs_hash_changesize(int newmaxvnodes)
167 {
168 struct vfs_hash_head *vfs_hash_newtbl, *vfs_hash_oldtbl;
169 u_long vfs_hash_newmask, vfs_hash_oldmask;
170 struct vnode *vp;
171 int i;
172
173 vfs_hash_newtbl = hashinit(newmaxvnodes, M_VFS_HASH,
174 &vfs_hash_newmask);
175 /* If same hash table size, nothing to do */
176 if (vfs_hash_mask == vfs_hash_newmask) {
177 free(vfs_hash_newtbl, M_VFS_HASH);
178 return;
179 }
180 /*
181 * Move everything from the old hash table to the new table.
182 * None of the vnodes in the table can be recycled because to
183 * do so, they have to be removed from the hash table.
184 */
185 rw_wlock(&vfs_hash_lock);
186 vfs_hash_oldtbl = vfs_hash_tbl;
187 vfs_hash_oldmask = vfs_hash_mask;
188 vfs_hash_tbl = vfs_hash_newtbl;
189 vfs_hash_mask = vfs_hash_newmask;
190 for (i = 0; i <= vfs_hash_oldmask; i++) {
191 while ((vp = LIST_FIRST(&vfs_hash_oldtbl[i])) != NULL) {
192 LIST_REMOVE(vp, v_hashlist);
193 LIST_INSERT_HEAD(
194 vfs_hash_bucket(vp->v_mount, vp->v_hash),
195 vp, v_hashlist);
196 }
197 }
198 rw_wunlock(&vfs_hash_lock);
199 free(vfs_hash_oldtbl, M_VFS_HASH);
200 }
201