]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/ufs/ufs/ufs_dirhash.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / ufs / ufs / ufs_dirhash.c
1 /*-
2  * Copyright (c) 2001, 2002 Ian Dowse.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * This implements a hash-based lookup scheme for UFS directories.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_ufs.h"
34
35 #ifdef UFS_DIRHASH
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
43 #include <sys/fnv_hash.h>
44 #include <sys/proc.h>
45 #include <sys/bio.h>
46 #include <sys/buf.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/refcount.h>
50 #include <sys/sysctl.h>
51 #include <sys/sx.h>
52 #include <vm/uma.h>
53
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/dir.h>
57 #include <ufs/ufs/dirhash.h>
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/ufs_extern.h>
61
62 #define WRAPINCR(val, limit)    (((val) + 1 == (limit)) ? 0 : ((val) + 1))
63 #define WRAPDECR(val, limit)    (((val) == 0) ? ((limit) - 1) : ((val) - 1))
64 #define OFSFMT(vp)              ((vp)->v_mount->mnt_maxsymlinklen <= 0)
65 #define BLKFREE2IDX(n)          ((n) > DH_NFSTATS ? DH_NFSTATS : (n))
66
67 static MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables");
68
69 static SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem");
70
71 static int ufs_mindirhashsize = DIRBLKSIZ * 5;
72 SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW,
73     &ufs_mindirhashsize,
74     0, "minimum directory size in bytes for which to use hashed lookup");
75 static int ufs_dirhashmaxmem = 2 * 1024 * 1024;
76 SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem,
77     0, "maximum allowed dirhash memory usage");
78 static int ufs_dirhashmem;
79 SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem,
80     0, "current dirhash memory usage");
81 static int ufs_dirhashcheck = 0;
82 SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck,
83     0, "enable extra sanity tests");
84
85
86 static int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen);
87 static void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff);
88 static void ufsdirhash_delslot(struct dirhash *dh, int slot);
89 static int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen,
90            doff_t offset);
91 static doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset);
92 static int ufsdirhash_recycle(int wanted);
93 static void ufsdirhash_free_locked(struct inode *ip);
94
95 static uma_zone_t       ufsdirhash_zone;
96
97 #define DIRHASHLIST_LOCK()              mtx_lock(&ufsdirhash_mtx)
98 #define DIRHASHLIST_UNLOCK()            mtx_unlock(&ufsdirhash_mtx)
99 #define DIRHASH_BLKALLOC_WAITOK()       uma_zalloc(ufsdirhash_zone, M_WAITOK)
100 #define DIRHASH_BLKFREE(ptr)            uma_zfree(ufsdirhash_zone, (ptr))
101 #define DIRHASH_ASSERT_LOCKED(dh)                                       \
102     sx_assert(&(dh)->dh_lock, SA_LOCKED)
103
104 /* Dirhash list; recently-used entries are near the tail. */
105 static TAILQ_HEAD(, dirhash) ufsdirhash_list;
106
107 /* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */
108 static struct mtx       ufsdirhash_mtx;
109
110 /*
111  * Locking:
112  *
113  * The relationship between inode and dirhash is protected either by an
114  * exclusive vnode lock or the vnode interlock where a shared vnode lock
115  * may be used.  The dirhash_mtx is acquired after the dirhash lock.  To
116  * handle teardown races, code wishing to lock the dirhash for an inode
117  * when using a shared vnode lock must obtain a private reference on the
118  * dirhash while holding the vnode interlock.  They can drop it once they
119  * have obtained the dirhash lock and verified that the dirhash wasn't
120  * recycled while they waited for the dirhash lock.
121  *
122  * ufsdirhash_build() acquires a shared lock on the dirhash when it is
123  * successful.  This lock is released after a call to ufsdirhash_lookup().
124  *
125  * Functions requiring exclusive access use ufsdirhash_acquire() which may
126  * free a dirhash structure that was recycled by ufsdirhash_recycle().
127  *
128  * The dirhash lock may be held across io operations.
129  *
130  * WITNESS reports a lock order reversal between the "bufwait" lock
131  * and the "dirhash" lock.  However, this specific reversal will not
132  * cause a deadlock.  To get a deadlock, one would have to lock a
133  * buffer followed by the dirhash while a second thread locked a
134  * buffer while holding the dirhash lock.  The second order can happen
135  * under a shared or exclusive vnode lock for the associated directory
136  * in lookup().  The first order, however, can only happen under an
137  * exclusive vnode lock (e.g. unlink(), rename(), etc.).  Thus, for
138  * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold
139  * an exclusive vnode lock.  That exclusive vnode lock will prevent
140  * any other threads from doing a "dirhash" -> "bufwait" order.
141  */
142
143 static void
144 ufsdirhash_hold(struct dirhash *dh)
145 {
146
147         refcount_acquire(&dh->dh_refcount);
148 }
149
150 static void
151 ufsdirhash_drop(struct dirhash *dh)
152 {
153
154         if (refcount_release(&dh->dh_refcount)) {
155                 sx_destroy(&dh->dh_lock);
156                 free(dh, M_DIRHASH);
157         }
158 }
159
160 /*
161  * Release the lock on a dirhash.
162  */
163 static void
164 ufsdirhash_release(struct dirhash *dh)
165 {
166
167         sx_unlock(&dh->dh_lock);
168 }
169
170 /*
171  * Either acquire an existing hash locked shared or create a new hash and
172  * return it exclusively locked.  May return NULL if the allocation fails.
173  *
174  * The vnode interlock is used to protect the i_dirhash pointer from
175  * simultaneous access while only a shared vnode lock is held.
176  */
177 static struct dirhash *
178 ufsdirhash_create(struct inode *ip)
179 {
180         struct dirhash *ndh;
181         struct dirhash *dh;
182         struct vnode *vp;
183         int error;
184
185         error = 0;
186         ndh = dh = NULL;
187         vp = ip->i_vnode;
188         for (;;) {
189                 /* Racy check for i_dirhash to prefetch an dirhash structure. */
190                 if (ip->i_dirhash == NULL && ndh == NULL) {
191                         MALLOC(ndh, struct dirhash *, sizeof *dh, M_DIRHASH,
192                             M_NOWAIT | M_ZERO);
193                         if (ndh == NULL)
194                                 return (NULL);
195                         refcount_init(&ndh->dh_refcount, 1);
196                         sx_init(&ndh->dh_lock, "dirhash");
197                         sx_xlock(&ndh->dh_lock);
198                 }
199                 /*
200                  * Check i_dirhash.  If it's NULL just try to use a
201                  * preallocated structure.  If none exists loop and try again.
202                  */
203                 VI_LOCK(vp);
204                 dh = ip->i_dirhash;
205                 if (dh == NULL) {
206                         ip->i_dirhash = ndh;
207                         VI_UNLOCK(vp);
208                         if (ndh == NULL)
209                                 continue;
210                         return (ndh);
211                 }
212                 ufsdirhash_hold(dh);
213                 VI_UNLOCK(vp);
214
215                 /* Acquire a shared lock on existing hashes. */
216                 sx_slock(&dh->dh_lock);
217
218                 /* The hash could've been recycled while we were waiting. */
219                 VI_LOCK(vp);
220                 if (ip->i_dirhash != dh) {
221                         VI_UNLOCK(vp);
222                         ufsdirhash_release(dh);
223                         ufsdirhash_drop(dh);
224                         continue;
225                 }
226                 VI_UNLOCK(vp);
227                 ufsdirhash_drop(dh);
228
229                 /* If the hash is still valid we've succeeded. */
230                 if (dh->dh_hash != NULL)
231                         break;
232                 /*
233                  * If the hash is NULL it has been recycled.  Try to upgrade
234                  * so we can recreate it.  If we fail the upgrade, drop our
235                  * lock and try again.
236                  */
237                 if (sx_try_upgrade(&dh->dh_lock))
238                         break;
239                 sx_sunlock(&dh->dh_lock);
240         }
241         /* Free the preallocated structure if it was not necessary. */
242         if (ndh) {
243                 ufsdirhash_release(ndh);
244                 ufsdirhash_drop(ndh);
245         }
246         return (dh);
247 }
248
249 /*
250  * Acquire an exclusive lock on an existing hash.  Requires an exclusive
251  * vnode lock to protect the i_dirhash pointer.  hashes that have been
252  * recycled are reclaimed here and NULL is returned.
253  */
254 static struct dirhash *
255 ufsdirhash_acquire(struct inode *ip)
256 {
257         struct dirhash *dh;
258         struct vnode *vp;
259
260         ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__);
261
262         vp = ip->i_vnode;
263         dh = ip->i_dirhash;
264         if (dh == NULL)
265                 return (NULL);
266         sx_xlock(&dh->dh_lock);
267         if (dh->dh_hash != NULL)
268                 return (dh);
269         ufsdirhash_free_locked(ip);
270         return (NULL);
271 }
272
273 /*
274  * Acquire exclusively and free the hash pointed to by ip.  Works with a
275  * shared or exclusive vnode lock.
276  */
277 void
278 ufsdirhash_free(struct inode *ip)
279 {
280         struct dirhash *dh;
281         struct vnode *vp;
282
283         vp = ip->i_vnode;
284         for (;;) {
285                 /* Grab a reference on this inode's dirhash if it has one. */
286                 VI_LOCK(vp);
287                 dh = ip->i_dirhash;
288                 if (dh == NULL) {
289                         VI_UNLOCK(vp);
290                         return;
291                 }
292                 ufsdirhash_hold(dh);
293                 VI_UNLOCK(vp);
294
295                 /* Exclusively lock the dirhash. */
296                 sx_xlock(&dh->dh_lock);
297
298                 /* If this dirhash still belongs to this inode, then free it. */
299                 VI_LOCK(vp);
300                 if (ip->i_dirhash == dh) {
301                         VI_UNLOCK(vp);
302                         ufsdirhash_drop(dh);
303                         break;
304                 }
305                 VI_UNLOCK(vp);
306
307                 /*
308                  * This inode's dirhash has changed while we were
309                  * waiting for the dirhash lock, so try again.
310                  */
311                 ufsdirhash_release(dh);
312                 ufsdirhash_drop(dh);
313         }
314         ufsdirhash_free_locked(ip);
315 }
316
317 /*
318  * Attempt to build up a hash table for the directory contents in
319  * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
320  */
321 int
322 ufsdirhash_build(struct inode *ip)
323 {
324         struct dirhash *dh;
325         struct buf *bp = NULL;
326         struct direct *ep;
327         struct vnode *vp;
328         doff_t bmask, pos;
329         int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
330
331         /* Take care of a decreased sysctl value. */
332         while (ufs_dirhashmem > ufs_dirhashmaxmem)
333                 if (ufsdirhash_recycle(0) != 0)
334                         return (-1);
335
336         /* Check if we can/should use dirhash. */
337         if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) ||
338             ip->i_effnlink == 0) {
339                 if (ip->i_dirhash)
340                         ufsdirhash_free(ip);
341                 return (-1);
342         }
343         dh = ufsdirhash_create(ip);
344         if (dh == NULL)
345                 return (-1);
346         if (dh->dh_hash != NULL)
347                 return (0);
348
349         vp = ip->i_vnode;
350         /* Allocate 50% more entries than this dir size could ever need. */
351         KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size"));
352         nslots = ip->i_size / DIRECTSIZ(1);
353         nslots = (nslots * 3 + 1) / 2;
354         narrays = howmany(nslots, DH_NBLKOFF);
355         nslots = narrays * DH_NBLKOFF;
356         dirblocks = howmany(ip->i_size, DIRBLKSIZ);
357         nblocks = (dirblocks * 3 + 1) / 2;
358         memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
359             narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
360             nblocks * sizeof(*dh->dh_blkfree);
361         DIRHASHLIST_LOCK();
362         if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) {
363                 DIRHASHLIST_UNLOCK();
364                 if (memreqd > ufs_dirhashmaxmem / 2)
365                         goto fail;
366                 /* Try to free some space. */
367                 if (ufsdirhash_recycle(memreqd) != 0)
368                         goto fail;
369                 /* Enough was freed, and list has been locked. */
370         }
371         ufs_dirhashmem += memreqd;
372         DIRHASHLIST_UNLOCK();
373
374         /* Initialise the hash table and block statistics. */
375         dh->dh_memreq = memreqd;
376         dh->dh_narrays = narrays;
377         dh->dh_hlen = nslots;
378         dh->dh_nblk = nblocks;
379         dh->dh_dirblks = dirblocks;
380         for (i = 0; i < DH_NFSTATS; i++)
381                 dh->dh_firstfree[i] = -1;
382         dh->dh_firstfree[DH_NFSTATS] = 0;
383         dh->dh_hused = 0;
384         dh->dh_seqopt = 0;
385         dh->dh_seqoff = 0;
386         dh->dh_score = DH_SCOREINIT;
387
388         /*
389          * Use non-blocking mallocs so that we will revert to a linear
390          * lookup on failure rather than potentially blocking forever.
391          */
392         MALLOC(dh->dh_hash, doff_t **, narrays * sizeof(dh->dh_hash[0]),
393             M_DIRHASH, M_NOWAIT | M_ZERO);
394         if (dh->dh_hash == NULL)
395                 goto fail;
396         MALLOC(dh->dh_blkfree, u_int8_t *, nblocks * sizeof(dh->dh_blkfree[0]),
397             M_DIRHASH, M_NOWAIT);
398         if (dh->dh_blkfree == NULL)
399                 goto fail;
400         for (i = 0; i < narrays; i++) {
401                 if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL)
402                         goto fail;
403                 for (j = 0; j < DH_NBLKOFF; j++)
404                         dh->dh_hash[i][j] = DIRHASH_EMPTY;
405         }
406         for (i = 0; i < dirblocks; i++)
407                 dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN;
408         bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
409         pos = 0;
410         while (pos < ip->i_size) {
411                 /* If necessary, get the next directory block. */
412                 if ((pos & bmask) == 0) {
413                         if (bp != NULL)
414                                 brelse(bp);
415                         if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0)
416                                 goto fail;
417                 }
418
419                 /* Add this entry to the hash. */
420                 ep = (struct direct *)((char *)bp->b_data + (pos & bmask));
421                 if (ep->d_reclen == 0 || ep->d_reclen >
422                     DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) {
423                         /* Corrupted directory. */
424                         brelse(bp);
425                         goto fail;
426                 }
427                 if (ep->d_ino != 0) {
428                         /* Add the entry (simplified ufsdirhash_add). */
429                         slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen);
430                         while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
431                                 slot = WRAPINCR(slot, dh->dh_hlen);
432                         dh->dh_hused++;
433                         DH_ENTRY(dh, slot) = pos;
434                         ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep));
435                 }
436                 pos += ep->d_reclen;
437         }
438
439         if (bp != NULL)
440                 brelse(bp);
441         DIRHASHLIST_LOCK();
442         TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list);
443         dh->dh_onlist = 1;
444         DIRHASHLIST_UNLOCK();
445         sx_downgrade(&dh->dh_lock);
446         return (0);
447
448 fail:
449         ufsdirhash_free_locked(ip);
450         return (-1);
451 }
452
453 /*
454  * Free any hash table associated with inode 'ip'.
455  */
456 static void
457 ufsdirhash_free_locked(struct inode *ip)
458 {
459         struct dirhash *dh;
460         struct vnode *vp;
461         int i;
462
463         DIRHASH_ASSERT_LOCKED(ip->i_dirhash);
464
465         /*
466          * Clear the pointer in the inode to prevent new threads from
467          * finding the dead structure.
468          */
469         vp = ip->i_vnode;
470         VI_LOCK(vp);
471         dh = ip->i_dirhash;
472         ip->i_dirhash = NULL;
473         VI_UNLOCK(vp);
474
475         /*
476          * Remove the hash from the list since we are going to free its
477          * memory.
478          */
479         DIRHASHLIST_LOCK();
480         if (dh->dh_onlist)
481                 TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
482         ufs_dirhashmem -= dh->dh_memreq;
483         DIRHASHLIST_UNLOCK();
484
485         /*
486          * At this point, any waiters for the lock should hold their
487          * own reference on the dirhash structure.  They will drop
488          * that reference once they grab the vnode interlock and see
489          * that ip->i_dirhash is NULL.
490          */
491         sx_xunlock(&dh->dh_lock);
492
493         /*
494          * Handle partially recycled as well as fully constructed hashes.
495          */
496         if (dh->dh_hash != NULL) {
497                 for (i = 0; i < dh->dh_narrays; i++)
498                         if (dh->dh_hash[i] != NULL)
499                                 DIRHASH_BLKFREE(dh->dh_hash[i]);
500                 FREE(dh->dh_hash, M_DIRHASH);
501                 if (dh->dh_blkfree != NULL)
502                         FREE(dh->dh_blkfree, M_DIRHASH);
503         }
504
505         /*
506          * Drop the inode's reference to the data structure.
507          */
508         ufsdirhash_drop(dh);
509 }
510
511 /*
512  * Find the offset of the specified name within the given inode.
513  * Returns 0 on success, ENOENT if the entry does not exist, or
514  * EJUSTRETURN if the caller should revert to a linear search.
515  *
516  * If successful, the directory offset is stored in *offp, and a
517  * pointer to a struct buf containing the entry is stored in *bpp. If
518  * prevoffp is non-NULL, the offset of the previous entry within
519  * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
520  * is the first in a block, the start of the block is used).
521  *
522  * Must be called with the hash locked.  Returns with the hash unlocked.
523  */
524 int
525 ufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp,
526     struct buf **bpp, doff_t *prevoffp)
527 {
528         struct dirhash *dh, *dh_next;
529         struct direct *dp;
530         struct vnode *vp;
531         struct buf *bp;
532         doff_t blkoff, bmask, offset, prevoff;
533         int i, slot;
534         int error;
535
536         dh = ip->i_dirhash;
537         KASSERT(dh != NULL && dh->dh_hash != NULL,
538             ("ufsdirhash_lookup: Invalid dirhash %p\n", dh));
539         DIRHASH_ASSERT_LOCKED(dh);
540         /*
541          * Move this dirhash towards the end of the list if it has a
542          * score higher than the next entry, and acquire the dh_lock.
543          */
544         DIRHASHLIST_LOCK();
545         if (TAILQ_NEXT(dh, dh_list) != NULL) {
546                 /*
547                  * If the new score will be greater than that of the next
548                  * entry, then move this entry past it. With both mutexes
549                  * held, dh_next won't go away, but its dh_score could
550                  * change; that's not important since it is just a hint.
551                  */
552                 if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
553                     dh->dh_score >= dh_next->dh_score) {
554                         KASSERT(dh->dh_onlist, ("dirhash: not on list"));
555                         TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
556                         TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh,
557                             dh_list);
558                 }
559         }
560         /* Update the score. */
561         if (dh->dh_score < DH_SCOREMAX)
562                 dh->dh_score++;
563         DIRHASHLIST_UNLOCK();
564
565         vp = ip->i_vnode;
566         bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
567         blkoff = -1;
568         bp = NULL;
569 restart:
570         slot = ufsdirhash_hash(dh, name, namelen);
571
572         if (dh->dh_seqopt) {
573                 /*
574                  * Sequential access optimisation. dh_seqoff contains the
575                  * offset of the directory entry immediately following
576                  * the last entry that was looked up. Check if this offset
577                  * appears in the hash chain for the name we are looking for.
578                  */
579                 for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
580                     i = WRAPINCR(i, dh->dh_hlen))
581                         if (offset == dh->dh_seqoff)
582                                 break;
583                 if (offset == dh->dh_seqoff) {
584                         /*
585                          * We found an entry with the expected offset. This
586                          * is probably the entry we want, but if not, the
587                          * code below will turn off seqopt and retry.
588                          */ 
589                         slot = i;
590                 } else 
591                         dh->dh_seqopt = 0;
592         }
593
594         for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
595             slot = WRAPINCR(slot, dh->dh_hlen)) {
596                 if (offset == DIRHASH_DEL)
597                         continue;
598                 if (offset < 0 || offset >= ip->i_size)
599                         panic("ufsdirhash_lookup: bad offset in hash array");
600                 if ((offset & ~bmask) != blkoff) {
601                         if (bp != NULL)
602                                 brelse(bp);
603                         blkoff = offset & ~bmask;
604                         if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) {
605                                 error = EJUSTRETURN;
606                                 goto fail;
607                         }
608                 }
609                 dp = (struct direct *)(bp->b_data + (offset & bmask));
610                 if (dp->d_reclen == 0 || dp->d_reclen >
611                     DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) {
612                         /* Corrupted directory. */
613                         error = EJUSTRETURN;
614                         goto fail;
615                 }
616                 if (dp->d_namlen == namelen &&
617                     bcmp(dp->d_name, name, namelen) == 0) {
618                         /* Found. Get the prev offset if needed. */
619                         if (prevoffp != NULL) {
620                                 if (offset & (DIRBLKSIZ - 1)) {
621                                         prevoff = ufsdirhash_getprev(dp,
622                                             offset);
623                                         if (prevoff == -1) {
624                                                 error = EJUSTRETURN;
625                                                 goto fail;
626                                         }
627                                 } else
628                                         prevoff = offset;
629                                 *prevoffp = prevoff;
630                         }
631
632                         /* Check for sequential access, and update offset. */
633                         if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
634                                 dh->dh_seqopt = 1;
635                         dh->dh_seqoff = offset + DIRSIZ(0, dp);
636                         *bpp = bp;
637                         *offp = offset;
638                         ufsdirhash_release(dh);
639                         return (0);
640                 }
641
642                 /*
643                  * When the name doesn't match in the seqopt case, go back
644                  * and search normally.
645                  */
646                 if (dh->dh_seqopt) {
647                         dh->dh_seqopt = 0;
648                         goto restart;
649                 }
650         }
651         error = ENOENT;
652 fail:
653         ufsdirhash_release(dh);
654         if (bp != NULL)
655                 brelse(bp);
656         return (error);
657 }
658
659 /*
660  * Find a directory block with room for 'slotneeded' bytes. Returns
661  * the offset of the directory entry that begins the free space.
662  * This will either be the offset of an existing entry that has free
663  * space at the end, or the offset of an entry with d_ino == 0 at
664  * the start of a DIRBLKSIZ block.
665  *
666  * To use the space, the caller may need to compact existing entries in
667  * the directory. The total number of bytes in all of the entries involved
668  * in the compaction is stored in *slotsize. In other words, all of
669  * the entries that must be compacted are exactly contained in the
670  * region beginning at the returned offset and spanning *slotsize bytes.
671  *
672  * Returns -1 if no space was found, indicating that the directory
673  * must be extended.
674  */
675 doff_t
676 ufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
677 {
678         struct direct *dp;
679         struct dirhash *dh;
680         struct buf *bp;
681         doff_t pos, slotstart;
682         int dirblock, error, freebytes, i;
683
684         dh = ip->i_dirhash;
685         KASSERT(dh != NULL && dh->dh_hash != NULL,
686             ("ufsdirhash_findfree: Invalid dirhash %p\n", dh));
687         DIRHASH_ASSERT_LOCKED(dh);
688
689         /* Find a directory block with the desired free space. */
690         dirblock = -1;
691         for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
692                 if ((dirblock = dh->dh_firstfree[i]) != -1)
693                         break;
694         if (dirblock == -1)
695                 return (-1);
696
697         KASSERT(dirblock < dh->dh_nblk &&
698             dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN),
699             ("ufsdirhash_findfree: bad stats"));
700         pos = dirblock * DIRBLKSIZ;
701         error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp);
702         if (error)
703                 return (-1);
704
705         /* Find the first entry with free space. */
706         for (i = 0; i < DIRBLKSIZ; ) {
707                 if (dp->d_reclen == 0) {
708                         brelse(bp);
709                         return (-1);
710                 }
711                 if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp))
712                         break;
713                 i += dp->d_reclen;
714                 dp = (struct direct *)((char *)dp + dp->d_reclen);
715         }
716         if (i > DIRBLKSIZ) {
717                 brelse(bp);
718                 return (-1);
719         }
720         slotstart = pos + i;
721
722         /* Find the range of entries needed to get enough space */
723         freebytes = 0;
724         while (i < DIRBLKSIZ && freebytes < slotneeded) {
725                 freebytes += dp->d_reclen;
726                 if (dp->d_ino != 0)
727                         freebytes -= DIRSIZ(0, dp);
728                 if (dp->d_reclen == 0) {
729                         brelse(bp);
730                         return (-1);
731                 }
732                 i += dp->d_reclen;
733                 dp = (struct direct *)((char *)dp + dp->d_reclen);
734         }
735         if (i > DIRBLKSIZ) {
736                 brelse(bp);
737                 return (-1);
738         }
739         if (freebytes < slotneeded)
740                 panic("ufsdirhash_findfree: free mismatch");
741         brelse(bp);
742         *slotsize = pos + i - slotstart;
743         return (slotstart);
744 }
745
746 /*
747  * Return the start of the unused space at the end of a directory, or
748  * -1 if there are no trailing unused blocks.
749  */
750 doff_t
751 ufsdirhash_enduseful(struct inode *ip)
752 {
753
754         struct dirhash *dh;
755         int i;
756
757         dh = ip->i_dirhash;
758         DIRHASH_ASSERT_LOCKED(dh);
759         KASSERT(dh != NULL && dh->dh_hash != NULL,
760             ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh));
761
762         if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN)
763                 return (-1);
764
765         for (i = dh->dh_dirblks - 1; i >= 0; i--)
766                 if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
767                         break;
768
769         return ((doff_t)(i + 1) * DIRBLKSIZ);
770 }
771
772 /*
773  * Insert information into the hash about a new directory entry. dirp
774  * points to a struct direct containing the entry, and offset specifies
775  * the offset of this entry.
776  */
777 void
778 ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset)
779 {
780         struct dirhash *dh;
781         int slot;
782
783         if ((dh = ufsdirhash_acquire(ip)) == NULL)
784                 return;
785         
786         KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
787             ("ufsdirhash_add: bad offset"));
788         /*
789          * Normal hash usage is < 66%. If the usage gets too high then
790          * remove the hash entirely and let it be rebuilt later.
791          */
792         if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
793                 ufsdirhash_free_locked(ip);
794                 return;
795         }
796
797         /* Find a free hash slot (empty or deleted), and add the entry. */
798         slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen);
799         while (DH_ENTRY(dh, slot) >= 0)
800                 slot = WRAPINCR(slot, dh->dh_hlen);
801         if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
802                 dh->dh_hused++;
803         DH_ENTRY(dh, slot) = offset;
804
805         /* Update the per-block summary info. */
806         ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp));
807         ufsdirhash_release(dh);
808 }
809
810 /*
811  * Remove the specified directory entry from the hash. The entry to remove
812  * is defined by the name in `dirp', which must exist at the specified
813  * `offset' within the directory.
814  */
815 void
816 ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset)
817 {
818         struct dirhash *dh;
819         int slot;
820
821         if ((dh = ufsdirhash_acquire(ip)) == NULL)
822                 return;
823
824         KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
825             ("ufsdirhash_remove: bad offset"));
826         /* Find the entry */
827         slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset);
828
829         /* Remove the hash entry. */
830         ufsdirhash_delslot(dh, slot);
831
832         /* Update the per-block summary info. */
833         ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp));
834         ufsdirhash_release(dh);
835 }
836
837 /*
838  * Change the offset associated with a directory entry in the hash. Used
839  * when compacting directory blocks.
840  */
841 void
842 ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff,
843     doff_t newoff)
844 {
845         struct dirhash *dh;
846         int slot;
847
848         if ((dh = ufsdirhash_acquire(ip)) == NULL)
849                 return;
850
851         KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ &&
852             newoff < dh->dh_dirblks * DIRBLKSIZ,
853             ("ufsdirhash_move: bad offset"));
854         /* Find the entry, and update the offset. */
855         slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff);
856         DH_ENTRY(dh, slot) = newoff;
857         ufsdirhash_release(dh);
858 }
859
860 /*
861  * Inform dirhash that the directory has grown by one block that
862  * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
863  */
864 void
865 ufsdirhash_newblk(struct inode *ip, doff_t offset)
866 {
867         struct dirhash *dh;
868         int block;
869
870         if ((dh = ufsdirhash_acquire(ip)) == NULL)
871                 return;
872
873         KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ,
874             ("ufsdirhash_newblk: bad offset"));
875         block = offset / DIRBLKSIZ;
876         if (block >= dh->dh_nblk) {
877                 /* Out of space; must rebuild. */
878                 ufsdirhash_free_locked(ip);
879                 return;
880         }
881         dh->dh_dirblks = block + 1;
882
883         /* Account for the new free block. */
884         dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN;
885         if (dh->dh_firstfree[DH_NFSTATS] == -1)
886                 dh->dh_firstfree[DH_NFSTATS] = block;
887         ufsdirhash_release(dh);
888 }
889
890 /*
891  * Inform dirhash that the directory is being truncated.
892  */
893 void
894 ufsdirhash_dirtrunc(struct inode *ip, doff_t offset)
895 {
896         struct dirhash *dh;
897         int block, i;
898
899         if ((dh = ufsdirhash_acquire(ip)) == NULL)
900                 return;
901
902         KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ,
903             ("ufsdirhash_dirtrunc: bad offset"));
904         block = howmany(offset, DIRBLKSIZ);
905         /*
906          * If the directory shrinks to less than 1/8 of dh_nblk blocks
907          * (about 20% of its original size due to the 50% extra added in
908          * ufsdirhash_build) then free it, and let the caller rebuild
909          * if necessary.
910          */
911         if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
912                 ufsdirhash_free_locked(ip);
913                 return;
914         }
915
916         /*
917          * Remove any `first free' information pertaining to the
918          * truncated blocks. All blocks we're removing should be
919          * completely unused.
920          */
921         if (dh->dh_firstfree[DH_NFSTATS] >= block)
922                 dh->dh_firstfree[DH_NFSTATS] = -1;
923         for (i = block; i < dh->dh_dirblks; i++)
924                 if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
925                         panic("ufsdirhash_dirtrunc: blocks in use");
926         for (i = 0; i < DH_NFSTATS; i++)
927                 if (dh->dh_firstfree[i] >= block)
928                         panic("ufsdirhash_dirtrunc: first free corrupt");
929         dh->dh_dirblks = block;
930         ufsdirhash_release(dh);
931 }
932
933 /*
934  * Debugging function to check that the dirhash information about
935  * a directory block matches its actual contents. Panics if a mismatch
936  * is detected.
937  *
938  * On entry, `buf' should point to the start of an in-core
939  * DIRBLKSIZ-sized directory block, and `offset' should contain the
940  * offset from the start of the directory of that block.
941  */
942 void
943 ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset)
944 {
945         struct dirhash *dh;
946         struct direct *dp;
947         int block, ffslot, i, nfree;
948
949         if (!ufs_dirhashcheck)
950                 return;
951         if ((dh = ufsdirhash_acquire(ip)) == NULL)
952                 return;
953
954         block = offset / DIRBLKSIZ;
955         if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks)
956                 panic("ufsdirhash_checkblock: bad offset");
957
958         nfree = 0;
959         for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) {
960                 dp = (struct direct *)(buf + i);
961                 if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ)
962                         panic("ufsdirhash_checkblock: bad dir");
963
964                 if (dp->d_ino == 0) {
965 #if 0
966                         /*
967                          * XXX entries with d_ino == 0 should only occur
968                          * at the start of a DIRBLKSIZ block. However the
969                          * ufs code is tolerant of such entries at other
970                          * offsets, and fsck does not fix them.
971                          */
972                         if (i != 0)
973                                 panic("ufsdirhash_checkblock: bad dir inode");
974 #endif
975                         nfree += dp->d_reclen;
976                         continue;
977                 }
978
979                 /* Check that the entry exists (will panic if it doesn't). */
980                 ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i);
981
982                 nfree += dp->d_reclen - DIRSIZ(0, dp);
983         }
984         if (i != DIRBLKSIZ)
985                 panic("ufsdirhash_checkblock: bad dir end");
986
987         if (dh->dh_blkfree[block] * DIRALIGN != nfree)
988                 panic("ufsdirhash_checkblock: bad free count");
989
990         ffslot = BLKFREE2IDX(nfree / DIRALIGN);
991         for (i = 0; i <= DH_NFSTATS; i++)
992                 if (dh->dh_firstfree[i] == block && i != ffslot)
993                         panic("ufsdirhash_checkblock: bad first-free");
994         if (dh->dh_firstfree[ffslot] == -1)
995                 panic("ufsdirhash_checkblock: missing first-free entry");
996         ufsdirhash_release(dh);
997 }
998
999 /*
1000  * Hash the specified filename into a dirhash slot.
1001  */
1002 static int
1003 ufsdirhash_hash(struct dirhash *dh, char *name, int namelen)
1004 {
1005         u_int32_t hash;
1006
1007         /*
1008          * We hash the name and then some other bit of data that is
1009          * invariant over the dirhash's lifetime. Otherwise names
1010          * differing only in the last byte are placed close to one
1011          * another in the table, which is bad for linear probing.
1012          */
1013         hash = fnv_32_buf(name, namelen, FNV1_32_INIT);
1014         hash = fnv_32_buf(&dh, sizeof(dh), hash);
1015         return (hash % dh->dh_hlen);
1016 }
1017
1018 /*
1019  * Adjust the number of free bytes in the block containing `offset'
1020  * by the value specified by `diff'.
1021  *
1022  * The caller must ensure we have exclusive access to `dh'; normally
1023  * that means that dh_lock should be held, but this is also called
1024  * from ufsdirhash_build() where exclusive access can be assumed.
1025  */
1026 static void
1027 ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff)
1028 {
1029         int block, i, nfidx, ofidx;
1030
1031         /* Update the per-block summary info. */
1032         block = offset / DIRBLKSIZ;
1033         KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks,
1034              ("dirhash bad offset"));
1035         ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
1036         dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
1037         nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
1038
1039         /* Update the `first free' list if necessary. */
1040         if (ofidx != nfidx) {
1041                 /* If removing, scan forward for the next block. */
1042                 if (dh->dh_firstfree[ofidx] == block) {
1043                         for (i = block + 1; i < dh->dh_dirblks; i++)
1044                                 if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
1045                                         break;
1046                         dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
1047                 }
1048
1049                 /* Make this the new `first free' if necessary */
1050                 if (dh->dh_firstfree[nfidx] > block ||
1051                     dh->dh_firstfree[nfidx] == -1)
1052                         dh->dh_firstfree[nfidx] = block;
1053         }
1054 }
1055
1056 /*
1057  * Find the specified name which should have the specified offset.
1058  * Returns a slot number, and panics on failure.
1059  *
1060  * `dh' must be locked on entry and remains so on return.
1061  */
1062 static int
1063 ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset)
1064 {
1065         int slot;
1066
1067         DIRHASH_ASSERT_LOCKED(dh);
1068
1069         /* Find the entry. */
1070         KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full"));
1071         slot = ufsdirhash_hash(dh, name, namelen);
1072         while (DH_ENTRY(dh, slot) != offset &&
1073             DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
1074                 slot = WRAPINCR(slot, dh->dh_hlen);
1075         if (DH_ENTRY(dh, slot) != offset)
1076                 panic("ufsdirhash_findslot: '%.*s' not found", namelen, name);
1077
1078         return (slot);
1079 }
1080
1081 /*
1082  * Remove the entry corresponding to the specified slot from the hash array.
1083  *
1084  * `dh' must be locked on entry and remains so on return.
1085  */
1086 static void
1087 ufsdirhash_delslot(struct dirhash *dh, int slot)
1088 {
1089         int i;
1090
1091         DIRHASH_ASSERT_LOCKED(dh);
1092
1093         /* Mark the entry as deleted. */
1094         DH_ENTRY(dh, slot) = DIRHASH_DEL;
1095
1096         /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
1097         for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
1098                 i = WRAPINCR(i, dh->dh_hlen);
1099         if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
1100                 i = WRAPDECR(i, dh->dh_hlen);
1101                 while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
1102                         DH_ENTRY(dh, i) = DIRHASH_EMPTY;
1103                         dh->dh_hused--;
1104                         i = WRAPDECR(i, dh->dh_hlen);
1105                 }
1106                 KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen"));
1107         }
1108 }
1109
1110 /*
1111  * Given a directory entry and its offset, find the offset of the
1112  * previous entry in the same DIRBLKSIZ-sized block. Returns an
1113  * offset, or -1 if there is no previous entry in the block or some
1114  * other problem occurred.
1115  */
1116 static doff_t
1117 ufsdirhash_getprev(struct direct *dirp, doff_t offset)
1118 {
1119         struct direct *dp;
1120         char *blkbuf;
1121         doff_t blkoff, prevoff;
1122         int entrypos, i;
1123
1124         blkoff = offset & ~(DIRBLKSIZ - 1);     /* offset of start of block */
1125         entrypos = offset & (DIRBLKSIZ - 1);    /* entry relative to block */
1126         blkbuf = (char *)dirp - entrypos;
1127         prevoff = blkoff;
1128
1129         /* If `offset' is the start of a block, there is no previous entry. */
1130         if (entrypos == 0)
1131                 return (-1);
1132
1133         /* Scan from the start of the block until we get to the entry. */
1134         for (i = 0; i < entrypos; i += dp->d_reclen) {
1135                 dp = (struct direct *)(blkbuf + i);
1136                 if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos)
1137                         return (-1);    /* Corrupted directory. */
1138                 prevoff = blkoff + i;
1139         }
1140         return (prevoff);
1141 }
1142
1143 /*
1144  * Try to free up `wanted' bytes by stealing memory from existing
1145  * dirhashes. Returns zero with list locked if successful.
1146  */
1147 static int
1148 ufsdirhash_recycle(int wanted)
1149 {
1150         struct dirhash *dh;
1151         doff_t **hash;
1152         u_int8_t *blkfree;
1153         int i, mem, narrays;
1154
1155         DIRHASHLIST_LOCK();
1156         dh = TAILQ_FIRST(&ufsdirhash_list);
1157         while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) {
1158                 /* Decrement the score; only recycle if it becomes zero. */
1159                 if (dh == NULL || --dh->dh_score > 0) {
1160                         DIRHASHLIST_UNLOCK();
1161                         return (-1);
1162                 }
1163                 /*
1164                  * If we can't lock it it's in use and we don't want to
1165                  * recycle it anyway.
1166                  */
1167                 if (!sx_try_xlock(&dh->dh_lock)) {
1168                         dh = TAILQ_NEXT(dh, dh_list);
1169                         continue;
1170                 }
1171                 KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list"));
1172
1173                 /* Remove it from the list and detach its memory. */
1174                 TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
1175                 dh->dh_onlist = 0;
1176                 hash = dh->dh_hash;
1177                 dh->dh_hash = NULL;
1178                 blkfree = dh->dh_blkfree;
1179                 dh->dh_blkfree = NULL;
1180                 narrays = dh->dh_narrays;
1181                 mem = dh->dh_memreq;
1182                 dh->dh_memreq = 0;
1183
1184                 /* Unlock everything, free the detached memory. */
1185                 ufsdirhash_release(dh);
1186                 DIRHASHLIST_UNLOCK();
1187                 for (i = 0; i < narrays; i++)
1188                         DIRHASH_BLKFREE(hash[i]);
1189                 FREE(hash, M_DIRHASH);
1190                 FREE(blkfree, M_DIRHASH);
1191
1192                 /* Account for the returned memory, and repeat if necessary. */
1193                 DIRHASHLIST_LOCK();
1194                 ufs_dirhashmem -= mem;
1195                 dh = TAILQ_FIRST(&ufsdirhash_list);
1196         }
1197         /* Success; return with list locked. */
1198         return (0);
1199 }
1200
1201
1202 void
1203 ufsdirhash_init()
1204 {
1205         ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t),
1206             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1207         mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF);
1208         TAILQ_INIT(&ufsdirhash_list);
1209 }
1210
1211 void
1212 ufsdirhash_uninit()
1213 {
1214         KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit"));
1215         uma_zdestroy(ufsdirhash_zone);
1216         mtx_destroy(&ufsdirhash_mtx);
1217 }
1218
1219 #endif /* UFS_DIRHASH */