vfs_dirhash.c revision 1.10.4.2 1 1.10.4.2 yamt /* $NetBSD: vfs_dirhash.c,v 1.10.4.2 2009/05/04 08:13:49 yamt Exp $ */
2 1.10.4.2 yamt
3 1.10.4.2 yamt /*
4 1.10.4.2 yamt * Copyright (c) 2008 Reinoud Zandijk
5 1.10.4.2 yamt * All rights reserved.
6 1.10.4.2 yamt *
7 1.10.4.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.10.4.2 yamt * modification, are permitted provided that the following conditions
9 1.10.4.2 yamt * are met:
10 1.10.4.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.10.4.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.10.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.10.4.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.10.4.2 yamt * documentation and/or other materials provided with the distribution.
15 1.10.4.2 yamt *
16 1.10.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.10.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.10.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.10.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.10.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.10.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.10.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.10.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.10.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.10.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.10.4.2 yamt *
27 1.10.4.2 yamt */
28 1.10.4.2 yamt
29 1.10.4.2 yamt
30 1.10.4.2 yamt #include <sys/cdefs.h>
31 1.10.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: vfs_dirhash.c,v 1.10.4.2 2009/05/04 08:13:49 yamt Exp $");
32 1.10.4.2 yamt
33 1.10.4.2 yamt /* CLEAN UP! */
34 1.10.4.2 yamt #include <sys/param.h>
35 1.10.4.2 yamt #include <sys/kernel.h>
36 1.10.4.2 yamt #include <sys/buf.h>
37 1.10.4.2 yamt #include <sys/dirent.h>
38 1.10.4.2 yamt #include <sys/hash.h>
39 1.10.4.2 yamt #include <sys/mutex.h>
40 1.10.4.2 yamt #include <sys/pool.h>
41 1.10.4.2 yamt #include <sys/queue.h>
42 1.10.4.2 yamt #include <sys/vnode.h>
43 1.10.4.2 yamt #include <sys/sysctl.h>
44 1.10.4.2 yamt
45 1.10.4.2 yamt #include <sys/dirhash.h>
46 1.10.4.2 yamt
47 1.10.4.2 yamt #if 1
48 1.10.4.2 yamt # define DPRINTF(a) ;
49 1.10.4.2 yamt #else
50 1.10.4.2 yamt # define DPRINTF(a) printf a;
51 1.10.4.2 yamt #endif
52 1.10.4.2 yamt
53 1.10.4.2 yamt /*
54 1.10.4.2 yamt * The locking protocol of the dirhash structures is fairly simple:
55 1.10.4.2 yamt *
56 1.10.4.2 yamt * The global dirhash_queue is protected by the dirhashmutex. This lock is
57 1.10.4.2 yamt * internal only and is FS/mountpoint/vnode independent. On exit of the
58 1.10.4.2 yamt * exported functions this mutex is not helt.
59 1.10.4.2 yamt *
60 1.10.4.2 yamt * The dirhash structure is considered part of the vnode/inode/udf_node
61 1.10.4.2 yamt * structure and will thus use the lock that protects that vnode/inode.
62 1.10.4.2 yamt *
63 1.10.4.2 yamt * The dirhash entries are considered part of the dirhash structure and thus
64 1.10.4.2 yamt * are on the same lock.
65 1.10.4.2 yamt */
66 1.10.4.2 yamt
67 1.10.4.2 yamt static struct sysctllog *sysctl_log;
68 1.10.4.2 yamt static struct pool dirhash_pool;
69 1.10.4.2 yamt static struct pool dirhash_entry_pool;
70 1.10.4.2 yamt
71 1.10.4.2 yamt static kmutex_t dirhashmutex;
72 1.10.4.2 yamt static uint32_t maxdirhashsize = DIRHASH_SIZE;
73 1.10.4.2 yamt static uint32_t dirhashsize = 0;
74 1.10.4.2 yamt static TAILQ_HEAD(_dirhash, dirhash) dirhash_queue;
75 1.10.4.2 yamt
76 1.10.4.2 yamt
77 1.10.4.2 yamt void
78 1.10.4.2 yamt dirhash_init(void)
79 1.10.4.2 yamt {
80 1.10.4.2 yamt const struct sysctlnode *rnode, *cnode;
81 1.10.4.2 yamt size_t sz;
82 1.10.4.2 yamt uint32_t max_entries;
83 1.10.4.2 yamt
84 1.10.4.2 yamt /* initialise dirhash queue */
85 1.10.4.2 yamt TAILQ_INIT(&dirhash_queue);
86 1.10.4.2 yamt
87 1.10.4.2 yamt /* init dirhash pools */
88 1.10.4.2 yamt sz = sizeof(struct dirhash);
89 1.10.4.2 yamt pool_init(&dirhash_pool, sz, 0, 0, 0,
90 1.10.4.2 yamt "dirhpl", NULL, IPL_NONE);
91 1.10.4.2 yamt
92 1.10.4.2 yamt sz = sizeof(struct dirhash_entry);
93 1.10.4.2 yamt pool_init(&dirhash_entry_pool, sz, 0, 0, 0,
94 1.10.4.2 yamt "dirhepl", NULL, IPL_NONE);
95 1.10.4.2 yamt
96 1.10.4.2 yamt mutex_init(&dirhashmutex, MUTEX_DEFAULT, IPL_NONE);
97 1.10.4.2 yamt max_entries = maxdirhashsize / sz;
98 1.10.4.2 yamt pool_sethiwat(&dirhash_entry_pool, max_entries);
99 1.10.4.2 yamt dirhashsize = 0;
100 1.10.4.2 yamt
101 1.10.4.2 yamt /* create sysctl knobs and dials */
102 1.10.4.2 yamt sysctl_log = NULL;
103 1.10.4.2 yamt sysctl_createv(&sysctl_log, 0, NULL, &rnode,
104 1.10.4.2 yamt CTLFLAG_PERMANENT,
105 1.10.4.2 yamt CTLTYPE_NODE, "dirhash", NULL,
106 1.10.4.2 yamt NULL, 0, NULL, 0,
107 1.10.4.2 yamt CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
108 1.10.4.2 yamt sysctl_createv(&sysctl_log, 0, &rnode, &cnode,
109 1.10.4.2 yamt CTLFLAG_PERMANENT,
110 1.10.4.2 yamt CTLTYPE_INT, "memused",
111 1.10.4.2 yamt SYSCTL_DESCR("current dirhash memory usage"),
112 1.10.4.2 yamt NULL, 0, &dirhashsize, 0,
113 1.10.4.2 yamt CTL_CREATE, CTL_EOL);
114 1.10.4.2 yamt sysctl_createv(&sysctl_log, 0, &rnode, &cnode,
115 1.10.4.2 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
116 1.10.4.2 yamt CTLTYPE_INT, "maxmem",
117 1.10.4.2 yamt SYSCTL_DESCR("maximum dirhash memory usage"),
118 1.10.4.2 yamt NULL, 0, &maxdirhashsize, 0,
119 1.10.4.2 yamt CTL_CREATE, CTL_EOL);
120 1.10.4.2 yamt }
121 1.10.4.2 yamt
122 1.10.4.2 yamt
123 1.10.4.2 yamt #if 0
124 1.10.4.2 yamt void
125 1.10.4.2 yamt dirhash_finish(void)
126 1.10.4.2 yamt {
127 1.10.4.2 yamt pool_destroy(&dirhash_pool);
128 1.10.4.2 yamt pool_destroy(&dirhash_entry_pool);
129 1.10.4.2 yamt
130 1.10.4.2 yamt mutex_destroy(&dirhashmutex);
131 1.10.4.2 yamt
132 1.10.4.2 yamt /* sysctl_teardown(&sysctl_log); */
133 1.10.4.2 yamt }
134 1.10.4.2 yamt #endif
135 1.10.4.2 yamt
136 1.10.4.2 yamt
137 1.10.4.2 yamt /*
138 1.10.4.2 yamt * generic dirhash implementation
139 1.10.4.2 yamt */
140 1.10.4.2 yamt
141 1.10.4.2 yamt void
142 1.10.4.2 yamt dirhash_purge_entries(struct dirhash *dirh)
143 1.10.4.2 yamt {
144 1.10.4.2 yamt struct dirhash_entry *dirh_e;
145 1.10.4.2 yamt uint32_t hashline;
146 1.10.4.2 yamt
147 1.10.4.2 yamt if (dirh == NULL)
148 1.10.4.2 yamt return;
149 1.10.4.2 yamt
150 1.10.4.2 yamt if (dirh->size == 0)
151 1.10.4.2 yamt return;
152 1.10.4.2 yamt
153 1.10.4.2 yamt for (hashline = 0; hashline < DIRHASH_HASHSIZE; hashline++) {
154 1.10.4.2 yamt while ((dirh_e =
155 1.10.4.2 yamt LIST_FIRST(&dirh->entries[hashline])) != NULL) {
156 1.10.4.2 yamt LIST_REMOVE(dirh_e, next);
157 1.10.4.2 yamt pool_put(&dirhash_entry_pool, dirh_e);
158 1.10.4.2 yamt }
159 1.10.4.2 yamt }
160 1.10.4.2 yamt
161 1.10.4.2 yamt while ((dirh_e = LIST_FIRST(&dirh->free_entries)) != NULL) {
162 1.10.4.2 yamt LIST_REMOVE(dirh_e, next);
163 1.10.4.2 yamt pool_put(&dirhash_entry_pool, dirh_e);
164 1.10.4.2 yamt }
165 1.10.4.2 yamt
166 1.10.4.2 yamt dirh->flags &= ~DIRH_COMPLETE;
167 1.10.4.2 yamt dirh->flags |= DIRH_PURGED;
168 1.10.4.2 yamt
169 1.10.4.2 yamt dirhashsize -= dirh->size;
170 1.10.4.2 yamt dirh->size = 0;
171 1.10.4.2 yamt }
172 1.10.4.2 yamt
173 1.10.4.2 yamt
174 1.10.4.2 yamt void
175 1.10.4.2 yamt dirhash_purge(struct dirhash **dirhp)
176 1.10.4.2 yamt {
177 1.10.4.2 yamt struct dirhash *dirh = *dirhp;
178 1.10.4.2 yamt
179 1.10.4.2 yamt if (dirh == NULL)
180 1.10.4.2 yamt return;
181 1.10.4.2 yamt
182 1.10.4.2 yamt /* purge its entries */
183 1.10.4.2 yamt dirhash_purge_entries(dirh);
184 1.10.4.2 yamt
185 1.10.4.2 yamt /* recycle */
186 1.10.4.2 yamt mutex_enter(&dirhashmutex);
187 1.10.4.2 yamt TAILQ_REMOVE(&dirhash_queue, dirh, next);
188 1.10.4.2 yamt mutex_exit(&dirhashmutex);
189 1.10.4.2 yamt
190 1.10.4.2 yamt pool_put(&dirhash_pool, dirh);
191 1.10.4.2 yamt *dirhp = NULL;
192 1.10.4.2 yamt }
193 1.10.4.2 yamt
194 1.10.4.2 yamt
195 1.10.4.2 yamt void
196 1.10.4.2 yamt dirhash_get(struct dirhash **dirhp)
197 1.10.4.2 yamt {
198 1.10.4.2 yamt struct dirhash *dirh;
199 1.10.4.2 yamt uint32_t hashline;
200 1.10.4.2 yamt
201 1.10.4.2 yamt /* if no dirhash was given, allocate one */
202 1.10.4.2 yamt dirh = *dirhp;
203 1.10.4.2 yamt if (dirh == NULL) {
204 1.10.4.2 yamt dirh = pool_get(&dirhash_pool, PR_WAITOK);
205 1.10.4.2 yamt memset(dirh, 0, sizeof(struct dirhash));
206 1.10.4.2 yamt for (hashline = 0; hashline < DIRHASH_HASHSIZE; hashline++) {
207 1.10.4.2 yamt LIST_INIT(&dirh->entries[hashline]);
208 1.10.4.2 yamt }
209 1.10.4.2 yamt }
210 1.10.4.2 yamt
211 1.10.4.2 yamt /* implement LRU on the dirhash queue */
212 1.10.4.2 yamt mutex_enter(&dirhashmutex);
213 1.10.4.2 yamt if (*dirhp) {
214 1.10.4.2 yamt /* remove from queue to be requeued */
215 1.10.4.2 yamt TAILQ_REMOVE(&dirhash_queue, dirh, next);
216 1.10.4.2 yamt }
217 1.10.4.2 yamt dirh->refcnt++;
218 1.10.4.2 yamt TAILQ_INSERT_HEAD(&dirhash_queue, dirh, next);
219 1.10.4.2 yamt mutex_exit(&dirhashmutex);
220 1.10.4.2 yamt
221 1.10.4.2 yamt *dirhp = dirh;
222 1.10.4.2 yamt }
223 1.10.4.2 yamt
224 1.10.4.2 yamt
225 1.10.4.2 yamt void
226 1.10.4.2 yamt dirhash_put(struct dirhash *dirh)
227 1.10.4.2 yamt {
228 1.10.4.2 yamt
229 1.10.4.2 yamt mutex_enter(&dirhashmutex);
230 1.10.4.2 yamt dirh->refcnt--;
231 1.10.4.2 yamt mutex_exit(&dirhashmutex);
232 1.10.4.2 yamt }
233 1.10.4.2 yamt
234 1.10.4.2 yamt
235 1.10.4.2 yamt void
236 1.10.4.2 yamt dirhash_enter(struct dirhash *dirh,
237 1.10.4.2 yamt struct dirent *dirent, uint64_t offset, uint32_t entry_size, int new)
238 1.10.4.2 yamt {
239 1.10.4.2 yamt struct dirhash *del_dirh, *prev_dirh;
240 1.10.4.2 yamt struct dirhash_entry *dirh_e;
241 1.10.4.2 yamt uint32_t hashvalue, hashline;
242 1.10.4.2 yamt int entrysize;
243 1.10.4.2 yamt
244 1.10.4.2 yamt /* make sure we have a dirhash to work on */
245 1.10.4.2 yamt KASSERT(dirh);
246 1.10.4.2 yamt KASSERT(dirh->refcnt > 0);
247 1.10.4.2 yamt
248 1.10.4.2 yamt /* are we trying to re-enter an entry? */
249 1.10.4.2 yamt if (!new && (dirh->flags & DIRH_COMPLETE))
250 1.10.4.2 yamt return;
251 1.10.4.2 yamt
252 1.10.4.2 yamt /* calculate our hash */
253 1.10.4.2 yamt hashvalue = hash32_strn(dirent->d_name, dirent->d_namlen, HASH32_STR_INIT);
254 1.10.4.2 yamt hashline = hashvalue & DIRHASH_HASHMASK;
255 1.10.4.2 yamt
256 1.10.4.2 yamt /* lookup and insert entry if not there yet */
257 1.10.4.2 yamt LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
258 1.10.4.2 yamt /* check for hash collision */
259 1.10.4.2 yamt if (dirh_e->hashvalue != hashvalue)
260 1.10.4.2 yamt continue;
261 1.10.4.2 yamt if (dirh_e->offset != offset)
262 1.10.4.2 yamt continue;
263 1.10.4.2 yamt /* got it already */
264 1.10.4.2 yamt KASSERT(dirh_e->d_namlen == dirent->d_namlen);
265 1.10.4.2 yamt KASSERT(dirh_e->entry_size == entry_size);
266 1.10.4.2 yamt return;
267 1.10.4.2 yamt }
268 1.10.4.2 yamt
269 1.10.4.2 yamt DPRINTF(("dirhash enter %"PRIu64", %d, %d for `%*.*s`\n",
270 1.10.4.2 yamt offset, entry_size, dirent->d_namlen,
271 1.10.4.2 yamt dirent->d_namlen, dirent->d_namlen, dirent->d_name));
272 1.10.4.2 yamt
273 1.10.4.2 yamt /* check if entry is in free space list */
274 1.10.4.2 yamt LIST_FOREACH(dirh_e, &dirh->free_entries, next) {
275 1.10.4.2 yamt if (dirh_e->offset == offset) {
276 1.10.4.2 yamt DPRINTF(("\tremoving free entry\n"));
277 1.10.4.2 yamt LIST_REMOVE(dirh_e, next);
278 1.10.4.2 yamt pool_put(&dirhash_entry_pool, dirh_e);
279 1.10.4.2 yamt break;
280 1.10.4.2 yamt }
281 1.10.4.2 yamt }
282 1.10.4.2 yamt
283 1.10.4.2 yamt /* ensure we are not passing the dirhash limit */
284 1.10.4.2 yamt entrysize = sizeof(struct dirhash_entry);
285 1.10.4.2 yamt if (dirhashsize + entrysize > maxdirhashsize) {
286 1.10.4.2 yamt /* we walk the dirhash_queue, so need to lock it */
287 1.10.4.2 yamt mutex_enter(&dirhashmutex);
288 1.10.4.2 yamt del_dirh = TAILQ_LAST(&dirhash_queue, _dirhash);
289 1.10.4.2 yamt KASSERT(del_dirh);
290 1.10.4.2 yamt while (dirhashsize + entrysize > maxdirhashsize) {
291 1.10.4.2 yamt /* no use trying to delete myself */
292 1.10.4.2 yamt if (del_dirh == dirh)
293 1.10.4.2 yamt break;
294 1.10.4.2 yamt prev_dirh = TAILQ_PREV(del_dirh, _dirhash, next);
295 1.10.4.2 yamt if (del_dirh->refcnt == 0)
296 1.10.4.2 yamt dirhash_purge_entries(del_dirh);
297 1.10.4.2 yamt del_dirh = prev_dirh;
298 1.10.4.2 yamt }
299 1.10.4.2 yamt mutex_exit(&dirhashmutex);
300 1.10.4.2 yamt }
301 1.10.4.2 yamt
302 1.10.4.2 yamt /* add to the hashline */
303 1.10.4.2 yamt dirh_e = pool_get(&dirhash_entry_pool, PR_WAITOK);
304 1.10.4.2 yamt memset(dirh_e, 0, sizeof(struct dirhash_entry));
305 1.10.4.2 yamt
306 1.10.4.2 yamt dirh_e->hashvalue = hashvalue;
307 1.10.4.2 yamt dirh_e->offset = offset;
308 1.10.4.2 yamt dirh_e->d_namlen = dirent->d_namlen;
309 1.10.4.2 yamt dirh_e->entry_size = entry_size;
310 1.10.4.2 yamt
311 1.10.4.2 yamt dirh->size += sizeof(struct dirhash_entry);
312 1.10.4.2 yamt dirhashsize += sizeof(struct dirhash_entry);
313 1.10.4.2 yamt LIST_INSERT_HEAD(&dirh->entries[hashline], dirh_e, next);
314 1.10.4.2 yamt }
315 1.10.4.2 yamt
316 1.10.4.2 yamt
317 1.10.4.2 yamt void
318 1.10.4.2 yamt dirhash_enter_freed(struct dirhash *dirh, uint64_t offset,
319 1.10.4.2 yamt uint32_t entry_size)
320 1.10.4.2 yamt {
321 1.10.4.2 yamt struct dirhash_entry *dirh_e;
322 1.10.4.2 yamt
323 1.10.4.2 yamt /* make sure we have a dirhash to work on */
324 1.10.4.2 yamt KASSERT(dirh);
325 1.10.4.2 yamt KASSERT(dirh->refcnt > 0);
326 1.10.4.2 yamt
327 1.10.4.2 yamt /* check for double entry of free space */
328 1.10.4.2 yamt LIST_FOREACH(dirh_e, &dirh->free_entries, next) {
329 1.10.4.2 yamt KASSERT(dirh_e->offset != offset);
330 1.10.4.2 yamt }
331 1.10.4.2 yamt
332 1.10.4.2 yamt DPRINTF(("dirhash enter FREED %"PRIu64", %d\n",
333 1.10.4.2 yamt offset, entry_size));
334 1.10.4.2 yamt dirh_e = pool_get(&dirhash_entry_pool, PR_WAITOK);
335 1.10.4.2 yamt memset(dirh_e, 0, sizeof(struct dirhash_entry));
336 1.10.4.2 yamt
337 1.10.4.2 yamt dirh_e->hashvalue = 0; /* not relevant */
338 1.10.4.2 yamt dirh_e->offset = offset;
339 1.10.4.2 yamt dirh_e->d_namlen = 0; /* not relevant */
340 1.10.4.2 yamt dirh_e->entry_size = entry_size;
341 1.10.4.2 yamt
342 1.10.4.2 yamt /* XXX it might be preferable to append them at the tail */
343 1.10.4.2 yamt LIST_INSERT_HEAD(&dirh->free_entries, dirh_e, next);
344 1.10.4.2 yamt dirh->size += sizeof(struct dirhash_entry);
345 1.10.4.2 yamt dirhashsize += sizeof(struct dirhash_entry);
346 1.10.4.2 yamt }
347 1.10.4.2 yamt
348 1.10.4.2 yamt
349 1.10.4.2 yamt void
350 1.10.4.2 yamt dirhash_remove(struct dirhash *dirh, struct dirent *dirent,
351 1.10.4.2 yamt uint64_t offset, uint32_t entry_size)
352 1.10.4.2 yamt {
353 1.10.4.2 yamt struct dirhash_entry *dirh_e;
354 1.10.4.2 yamt uint32_t hashvalue, hashline;
355 1.10.4.2 yamt
356 1.10.4.2 yamt DPRINTF(("dirhash remove %"PRIu64", %d for `%*.*s`\n",
357 1.10.4.2 yamt offset, entry_size,
358 1.10.4.2 yamt dirent->d_namlen, dirent->d_namlen, dirent->d_name));
359 1.10.4.2 yamt
360 1.10.4.2 yamt /* make sure we have a dirhash to work on */
361 1.10.4.2 yamt KASSERT(dirh);
362 1.10.4.2 yamt KASSERT(dirh->refcnt > 0);
363 1.10.4.2 yamt
364 1.10.4.2 yamt /* calculate our hash */
365 1.10.4.2 yamt hashvalue = hash32_strn(dirent->d_name, dirent->d_namlen, HASH32_STR_INIT);
366 1.10.4.2 yamt hashline = hashvalue & DIRHASH_HASHMASK;
367 1.10.4.2 yamt
368 1.10.4.2 yamt /* lookup entry */
369 1.10.4.2 yamt LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
370 1.10.4.2 yamt /* check for hash collision */
371 1.10.4.2 yamt if (dirh_e->hashvalue != hashvalue)
372 1.10.4.2 yamt continue;
373 1.10.4.2 yamt if (dirh_e->offset != offset)
374 1.10.4.2 yamt continue;
375 1.10.4.2 yamt
376 1.10.4.2 yamt /* got it! */
377 1.10.4.2 yamt KASSERT(dirh_e->d_namlen == dirent->d_namlen);
378 1.10.4.2 yamt KASSERT(dirh_e->entry_size == entry_size);
379 1.10.4.2 yamt LIST_REMOVE(dirh_e, next);
380 1.10.4.2 yamt dirh->size -= sizeof(struct dirhash_entry);
381 1.10.4.2 yamt dirhashsize -= sizeof(struct dirhash_entry);
382 1.10.4.2 yamt
383 1.10.4.2 yamt dirhash_enter_freed(dirh, offset, entry_size);
384 1.10.4.2 yamt return;
385 1.10.4.2 yamt }
386 1.10.4.2 yamt
387 1.10.4.2 yamt /* not found! */
388 1.10.4.2 yamt panic("dirhash_remove couldn't find entry in hash table\n");
389 1.10.4.2 yamt }
390 1.10.4.2 yamt
391 1.10.4.2 yamt
392 1.10.4.2 yamt /*
393 1.10.4.2 yamt * BUGALERT: don't use result longer than needed, never past the node lock.
394 1.10.4.2 yamt * Call with NULL *result initially and it will return nonzero if again.
395 1.10.4.2 yamt */
396 1.10.4.2 yamt int
397 1.10.4.2 yamt dirhash_lookup(struct dirhash *dirh, const char *d_name, int d_namlen,
398 1.10.4.2 yamt struct dirhash_entry **result)
399 1.10.4.2 yamt {
400 1.10.4.2 yamt struct dirhash_entry *dirh_e;
401 1.10.4.2 yamt uint32_t hashvalue, hashline;
402 1.10.4.2 yamt
403 1.10.4.2 yamt /* make sure we have a dirhash to work on */
404 1.10.4.2 yamt KASSERT(dirh);
405 1.10.4.2 yamt KASSERT(dirh->refcnt > 0);
406 1.10.4.2 yamt
407 1.10.4.2 yamt /* start where we were */
408 1.10.4.2 yamt if (*result) {
409 1.10.4.2 yamt dirh_e = *result;
410 1.10.4.2 yamt
411 1.10.4.2 yamt /* retrieve information to avoid recalculation and advance */
412 1.10.4.2 yamt hashvalue = dirh_e->hashvalue;
413 1.10.4.2 yamt dirh_e = LIST_NEXT(*result, next);
414 1.10.4.2 yamt } else {
415 1.10.4.2 yamt /* calculate our hash and lookup all entries in hashline */
416 1.10.4.2 yamt hashvalue = hash32_strn(d_name, d_namlen, HASH32_STR_INIT);
417 1.10.4.2 yamt hashline = hashvalue & DIRHASH_HASHMASK;
418 1.10.4.2 yamt dirh_e = LIST_FIRST(&dirh->entries[hashline]);
419 1.10.4.2 yamt }
420 1.10.4.2 yamt
421 1.10.4.2 yamt for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
422 1.10.4.2 yamt /* check for hash collision */
423 1.10.4.2 yamt if (dirh_e->hashvalue != hashvalue)
424 1.10.4.2 yamt continue;
425 1.10.4.2 yamt if (dirh_e->d_namlen != d_namlen)
426 1.10.4.2 yamt continue;
427 1.10.4.2 yamt /* might have an entry in the cache */
428 1.10.4.2 yamt *result = dirh_e;
429 1.10.4.2 yamt return 1;
430 1.10.4.2 yamt }
431 1.10.4.2 yamt
432 1.10.4.2 yamt *result = NULL;
433 1.10.4.2 yamt return 0;
434 1.10.4.2 yamt }
435 1.10.4.2 yamt
436 1.10.4.2 yamt
437 1.10.4.2 yamt /*
438 1.10.4.2 yamt * BUGALERT: don't use result longer than needed, never past the node lock.
439 1.10.4.2 yamt * Call with NULL *result initially and it will return nonzero if again.
440 1.10.4.2 yamt */
441 1.10.4.2 yamt
442 1.10.4.2 yamt int
443 1.10.4.2 yamt dirhash_lookup_freed(struct dirhash *dirh, uint32_t min_entrysize,
444 1.10.4.2 yamt struct dirhash_entry **result)
445 1.10.4.2 yamt {
446 1.10.4.2 yamt struct dirhash_entry *dirh_e;
447 1.10.4.2 yamt
448 1.10.4.2 yamt /* make sure we have a dirhash to work on */
449 1.10.4.2 yamt KASSERT(dirh);
450 1.10.4.2 yamt KASSERT(dirh->refcnt > 0);
451 1.10.4.2 yamt
452 1.10.4.2 yamt /* start where we were */
453 1.10.4.2 yamt if (*result) {
454 1.10.4.2 yamt dirh_e = LIST_NEXT(*result, next);
455 1.10.4.2 yamt } else {
456 1.10.4.2 yamt /* lookup all entries that match */
457 1.10.4.2 yamt dirh_e = LIST_FIRST(&dirh->free_entries);
458 1.10.4.2 yamt }
459 1.10.4.2 yamt
460 1.10.4.2 yamt for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
461 1.10.4.2 yamt /* check for minimum size */
462 1.10.4.2 yamt if (dirh_e->entry_size < min_entrysize)
463 1.10.4.2 yamt continue;
464 1.10.4.2 yamt /* might be a candidate */
465 1.10.4.2 yamt *result = dirh_e;
466 1.10.4.2 yamt return 1;
467 1.10.4.2 yamt }
468 1.10.4.2 yamt
469 1.10.4.2 yamt *result = NULL;
470 1.10.4.2 yamt return 0;
471 1.10.4.2 yamt }
472