Home | History | Annotate | Line # | Download | only in lfs
ulfs_dirhash.c revision 1.17.18.1
      1  1.17.18.1    martin /*	$NetBSD: ulfs_dirhash.c,v 1.17.18.1 2020/04/08 14:09:04 martin Exp $	*/
      2       1.17  dholland /*  from NetBSD: ufs_dirhash.c,v 1.37 2014/12/20 00:28:05 christos Exp  */
      3        1.1  dholland 
      4        1.1  dholland /*
      5        1.1  dholland  * Copyright (c) 2001, 2002 Ian Dowse.  All rights reserved.
      6        1.1  dholland  *
      7        1.1  dholland  * Redistribution and use in source and binary forms, with or without
      8        1.1  dholland  * modification, are permitted provided that the following conditions
      9        1.1  dholland  * are met:
     10        1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     11        1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     12        1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  dholland  *    documentation and/or other materials provided with the distribution.
     15        1.1  dholland  *
     16        1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19        1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1  dholland  * SUCH DAMAGE.
     27        1.1  dholland  *
     28        1.1  dholland  * $FreeBSD: src/sys/ufs/ufs/ufs_dirhash.c,v 1.3.2.8 2004/12/08 11:54:13 dwmalone Exp $
     29        1.1  dholland  */
     30        1.1  dholland 
     31        1.1  dholland #include <sys/cdefs.h>
     32  1.17.18.1    martin __KERNEL_RCSID(0, "$NetBSD: ulfs_dirhash.c,v 1.17.18.1 2020/04/08 14:09:04 martin Exp $");
     33        1.1  dholland 
     34        1.1  dholland /*
     35        1.3  dholland  * This implements a hash-based lookup scheme for ULFS directories.
     36        1.1  dholland  */
     37        1.1  dholland 
     38        1.1  dholland #include <sys/param.h>
     39        1.1  dholland #include <sys/systm.h>
     40        1.1  dholland #include <sys/kernel.h>
     41        1.1  dholland #include <sys/kmem.h>
     42        1.1  dholland #include <sys/types.h>
     43        1.1  dholland #include <sys/hash.h>
     44        1.1  dholland #include <sys/proc.h>
     45        1.1  dholland #include <sys/buf.h>
     46        1.1  dholland #include <sys/vnode.h>
     47        1.1  dholland #include <sys/mount.h>
     48        1.1  dholland #include <sys/pool.h>
     49        1.1  dholland #include <sys/sysctl.h>
     50        1.1  dholland #include <sys/atomic.h>
     51        1.1  dholland 
     52        1.9  dholland #include <ufs/lfs/lfs.h>
     53        1.9  dholland #include <ufs/lfs/lfs_accessors.h>
     54        1.2  dholland #include <ufs/lfs/ulfs_inode.h>
     55        1.2  dholland #include <ufs/lfs/ulfs_dirhash.h>
     56        1.2  dholland #include <ufs/lfs/ulfsmount.h>
     57        1.2  dholland #include <ufs/lfs/ulfs_bswap.h>
     58        1.2  dholland #include <ufs/lfs/ulfs_extern.h>
     59        1.1  dholland 
     60        1.1  dholland #define WRAPINCR(val, limit)	(((val) + 1 == (limit)) ? 0 : ((val) + 1))
     61        1.1  dholland #define WRAPDECR(val, limit)	(((val) == 0) ? ((limit) - 1) : ((val) - 1))
     62        1.7  dholland #define OFSFMT(ip)		((ip)->i_lfs->um_maxsymlinklen <= 0)
     63        1.1  dholland #define BLKFREE2IDX(n)		((n) > DH_NFSTATS ? DH_NFSTATS : (n))
     64        1.1  dholland 
     65        1.3  dholland static u_int ulfs_dirhashminblks = 5;
     66        1.3  dholland static u_int ulfs_dirhashmaxmem = 2 * 1024 * 1024;
     67        1.3  dholland static u_int ulfs_dirhashmem;
     68        1.3  dholland static u_int ulfs_dirhashcheck = 0;
     69        1.1  dholland 
     70        1.3  dholland static int ulfsdirhash_hash(struct dirhash *dh, const char *name, int namelen);
     71        1.3  dholland static void ulfsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff,
     72        1.1  dholland 	   int dirblksiz);
     73        1.3  dholland static void ulfsdirhash_delslot(struct dirhash *dh, int slot);
     74        1.3  dholland static int ulfsdirhash_findslot(struct dirhash *dh, const char *name,
     75        1.1  dholland 	   int namelen, doff_t offset);
     76       1.14  dholland static doff_t ulfsdirhash_getprev(struct lfs *fs, LFS_DIRHEADER *dp,
     77       1.10  dholland 	   doff_t offset, int dirblksiz);
     78        1.3  dholland static int ulfsdirhash_recycle(int wanted);
     79        1.1  dholland 
     80        1.3  dholland static pool_cache_t ulfsdirhashblk_cache;
     81        1.3  dholland static pool_cache_t ulfsdirhash_cache;
     82        1.1  dholland 
     83        1.3  dholland #define DIRHASHLIST_LOCK()		mutex_enter(&ulfsdirhash_lock)
     84        1.3  dholland #define DIRHASHLIST_UNLOCK()		mutex_exit(&ulfsdirhash_lock)
     85        1.1  dholland #define DIRHASH_LOCK(dh)		mutex_enter(&(dh)->dh_lock)
     86        1.1  dholland #define DIRHASH_UNLOCK(dh)		mutex_exit(&(dh)->dh_lock)
     87        1.1  dholland #define DIRHASH_BLKALLOC()		\
     88        1.3  dholland     pool_cache_get(ulfsdirhashblk_cache, PR_NOWAIT)
     89        1.1  dholland #define DIRHASH_BLKFREE(ptr)		\
     90        1.3  dholland     pool_cache_put(ulfsdirhashblk_cache, ptr)
     91        1.1  dholland 
     92        1.1  dholland /* Dirhash list; recently-used entries are near the tail. */
     93        1.3  dholland static TAILQ_HEAD(, dirhash) ulfsdirhash_list;
     94        1.1  dholland 
     95        1.3  dholland /* Protects: ulfsdirhash_list, `dh_list' field, ulfs_dirhashmem. */
     96        1.3  dholland static kmutex_t ulfsdirhash_lock;
     97        1.1  dholland 
     98        1.3  dholland static struct sysctllog *ulfsdirhash_sysctl_log;
     99        1.1  dholland 
    100        1.1  dholland /*
    101        1.1  dholland  * Locking order:
    102        1.3  dholland  *	ulfsdirhash_lock
    103        1.1  dholland  *	dh_lock
    104        1.1  dholland  *
    105        1.1  dholland  * The dh_lock mutex should be acquired either via the inode lock, or via
    106        1.3  dholland  * ulfsdirhash_lock. Only the owner of the inode may free the associated
    107        1.1  dholland  * dirhash, but anything can steal its memory and set dh_hash to NULL.
    108        1.1  dholland  */
    109        1.1  dholland 
    110        1.1  dholland /*
    111        1.1  dholland  * Attempt to build up a hash table for the directory contents in
    112        1.1  dholland  * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
    113        1.1  dholland  */
    114        1.1  dholland int
    115        1.3  dholland ulfsdirhash_build(struct inode *ip)
    116        1.1  dholland {
    117        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    118        1.1  dholland 	struct dirhash *dh;
    119        1.1  dholland 	struct buf *bp = NULL;
    120       1.14  dholland 	LFS_DIRHEADER *ep;
    121        1.1  dholland 	struct vnode *vp;
    122        1.1  dholland 	doff_t bmask, pos;
    123        1.1  dholland 	int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
    124        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    125        1.1  dholland 
    126        1.1  dholland 	/* Check if we can/should use dirhash. */
    127        1.1  dholland 	if (ip->i_dirhash == NULL) {
    128        1.3  dholland 		if (ip->i_size < (ulfs_dirhashminblks * dirblksiz) || OFSFMT(ip))
    129        1.1  dholland 			return (-1);
    130        1.1  dholland 	} else {
    131        1.1  dholland 		/* Hash exists, but sysctls could have changed. */
    132        1.3  dholland 		if (ip->i_size < (ulfs_dirhashminblks * dirblksiz) ||
    133        1.3  dholland 		    ulfs_dirhashmem > ulfs_dirhashmaxmem) {
    134        1.3  dholland 			ulfsdirhash_free(ip);
    135        1.1  dholland 			return (-1);
    136        1.1  dholland 		}
    137        1.1  dholland 		/* Check if hash exists and is intact (note: unlocked read). */
    138        1.1  dholland 		if (ip->i_dirhash->dh_hash != NULL)
    139        1.1  dholland 			return (0);
    140        1.1  dholland 		/* Free the old, recycled hash and build a new one. */
    141        1.3  dholland 		ulfsdirhash_free(ip);
    142        1.1  dholland 	}
    143        1.1  dholland 
    144        1.1  dholland 	/* Don't hash removed directories. */
    145        1.1  dholland 	if (ip->i_nlink == 0)
    146        1.1  dholland 		return (-1);
    147        1.1  dholland 
    148        1.1  dholland 	vp = ip->i_vnode;
    149        1.1  dholland 	/* Allocate 50% more entries than this dir size could ever need. */
    150        1.1  dholland 	KASSERT(ip->i_size >= dirblksiz);
    151       1.13  dholland 	nslots = ip->i_size / LFS_DIRECTSIZ(fs, 1);
    152        1.1  dholland 	nslots = (nslots * 3 + 1) / 2;
    153        1.1  dholland 	narrays = howmany(nslots, DH_NBLKOFF);
    154        1.1  dholland 	nslots = narrays * DH_NBLKOFF;
    155        1.1  dholland 	dirblocks = howmany(ip->i_size, dirblksiz);
    156        1.1  dholland 	nblocks = (dirblocks * 3 + 1) / 2;
    157        1.1  dholland 
    158        1.1  dholland 	memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
    159        1.1  dholland 	    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
    160        1.1  dholland 	    nblocks * sizeof(*dh->dh_blkfree);
    161        1.1  dholland 
    162        1.3  dholland 	while (atomic_add_int_nv(&ulfs_dirhashmem, memreqd) >
    163        1.3  dholland 	    ulfs_dirhashmaxmem) {
    164        1.3  dholland 		atomic_add_int(&ulfs_dirhashmem, -memreqd);
    165        1.3  dholland 		if (memreqd > ulfs_dirhashmaxmem / 2)
    166        1.1  dholland 			return (-1);
    167        1.1  dholland 		/* Try to free some space. */
    168        1.3  dholland 		if (ulfsdirhash_recycle(memreqd) != 0)
    169        1.1  dholland 			return (-1);
    170        1.1  dholland 	        else
    171        1.1  dholland 		    	DIRHASHLIST_UNLOCK();
    172        1.1  dholland 	}
    173        1.1  dholland 
    174        1.1  dholland 	/*
    175        1.1  dholland 	 * Use non-blocking mallocs so that we will revert to a linear
    176        1.1  dholland 	 * lookup on failure rather than potentially blocking forever.
    177        1.1  dholland 	 */
    178        1.3  dholland 	dh = pool_cache_get(ulfsdirhash_cache, PR_NOWAIT);
    179        1.1  dholland 	if (dh == NULL) {
    180        1.3  dholland 		atomic_add_int(&ulfs_dirhashmem, -memreqd);
    181        1.1  dholland 		return (-1);
    182        1.1  dholland 	}
    183        1.1  dholland 	memset(dh, 0, sizeof(*dh));
    184        1.1  dholland 	mutex_init(&dh->dh_lock, MUTEX_DEFAULT, IPL_NONE);
    185        1.1  dholland 	DIRHASH_LOCK(dh);
    186        1.1  dholland 	dh->dh_hashsz = narrays * sizeof(dh->dh_hash[0]);
    187        1.1  dholland 	dh->dh_hash = kmem_zalloc(dh->dh_hashsz, KM_NOSLEEP);
    188        1.1  dholland 	dh->dh_blkfreesz = nblocks * sizeof(dh->dh_blkfree[0]);
    189        1.1  dholland 	dh->dh_blkfree = kmem_zalloc(dh->dh_blkfreesz, KM_NOSLEEP);
    190        1.1  dholland 	if (dh->dh_hash == NULL || dh->dh_blkfree == NULL)
    191        1.1  dholland 		goto fail;
    192        1.1  dholland 	for (i = 0; i < narrays; i++) {
    193        1.1  dholland 		if ((dh->dh_hash[i] = DIRHASH_BLKALLOC()) == NULL)
    194        1.1  dholland 			goto fail;
    195        1.1  dholland 		for (j = 0; j < DH_NBLKOFF; j++)
    196        1.1  dholland 			dh->dh_hash[i][j] = DIRHASH_EMPTY;
    197        1.1  dholland 	}
    198        1.1  dholland 
    199        1.1  dholland 	/* Initialise the hash table and block statistics. */
    200        1.1  dholland 	dh->dh_narrays = narrays;
    201        1.1  dholland 	dh->dh_hlen = nslots;
    202        1.1  dholland 	dh->dh_nblk = nblocks;
    203        1.1  dholland 	dh->dh_dirblks = dirblocks;
    204        1.1  dholland 	for (i = 0; i < dirblocks; i++)
    205        1.1  dholland 		dh->dh_blkfree[i] = dirblksiz / DIRALIGN;
    206        1.1  dholland 	for (i = 0; i < DH_NFSTATS; i++)
    207        1.1  dholland 		dh->dh_firstfree[i] = -1;
    208        1.1  dholland 	dh->dh_firstfree[DH_NFSTATS] = 0;
    209        1.1  dholland 	dh->dh_seqopt = 0;
    210        1.1  dholland 	dh->dh_seqoff = 0;
    211        1.1  dholland 	dh->dh_score = DH_SCOREINIT;
    212        1.1  dholland 	ip->i_dirhash = dh;
    213        1.1  dholland 
    214        1.3  dholland 	bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
    215        1.1  dholland 	pos = 0;
    216        1.1  dholland 	while (pos < ip->i_size) {
    217  1.17.18.1    martin 		preempt_point();
    218  1.17.18.1    martin 
    219        1.1  dholland 		/* If necessary, get the next directory block. */
    220        1.1  dholland 		if ((pos & bmask) == 0) {
    221        1.1  dholland 			if (bp != NULL)
    222        1.1  dholland 				brelse(bp, 0);
    223        1.3  dholland 			if (ulfs_blkatoff(vp, (off_t)pos, NULL, &bp, false) != 0)
    224        1.1  dholland 				goto fail;
    225        1.1  dholland 		}
    226        1.1  dholland 
    227        1.1  dholland 		/* Add this entry to the hash. */
    228       1.14  dholland 		ep = (LFS_DIRHEADER *)((char *)bp->b_data + (pos & bmask));
    229       1.10  dholland 		if (lfs_dir_getreclen(fs, ep) == 0 || lfs_dir_getreclen(fs, ep) >
    230        1.1  dholland 		    dirblksiz - (pos & (dirblksiz - 1))) {
    231        1.1  dholland 			/* Corrupted directory. */
    232        1.1  dholland 			brelse(bp, 0);
    233        1.1  dholland 			goto fail;
    234        1.1  dholland 		}
    235       1.10  dholland 		if (lfs_dir_getino(fs, ep) != 0) {
    236        1.3  dholland 			/* Add the entry (simplified ulfsdirhash_add). */
    237       1.11  dholland 			slot = ulfsdirhash_hash(dh, lfs_dir_nameptr(fs, ep),
    238        1.9  dholland 						lfs_dir_getnamlen(fs, ep));
    239        1.1  dholland 			while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
    240        1.1  dholland 				slot = WRAPINCR(slot, dh->dh_hlen);
    241        1.1  dholland 			dh->dh_hused++;
    242        1.1  dholland 			DH_ENTRY(dh, slot) = pos;
    243        1.9  dholland 			ulfsdirhash_adjfree(dh, pos, -LFS_DIRSIZ(fs, ep),
    244        1.1  dholland 			    dirblksiz);
    245        1.1  dholland 		}
    246       1.10  dholland 		pos += lfs_dir_getreclen(fs, ep);
    247        1.1  dholland 	}
    248        1.1  dholland 
    249        1.1  dholland 	if (bp != NULL)
    250        1.1  dholland 		brelse(bp, 0);
    251        1.1  dholland 	DIRHASHLIST_LOCK();
    252        1.3  dholland 	TAILQ_INSERT_TAIL(&ulfsdirhash_list, dh, dh_list);
    253        1.1  dholland 	dh->dh_onlist = 1;
    254        1.1  dholland 	DIRHASH_UNLOCK(dh);
    255        1.1  dholland 	DIRHASHLIST_UNLOCK();
    256        1.1  dholland 	return (0);
    257        1.1  dholland 
    258        1.1  dholland fail:
    259       1.17  dholland 	ip->i_dirhash = NULL;
    260        1.1  dholland 	DIRHASH_UNLOCK(dh);
    261        1.1  dholland 	if (dh->dh_hash != NULL) {
    262        1.1  dholland 		for (i = 0; i < narrays; i++)
    263        1.1  dholland 			if (dh->dh_hash[i] != NULL)
    264        1.1  dholland 				DIRHASH_BLKFREE(dh->dh_hash[i]);
    265        1.1  dholland 		kmem_free(dh->dh_hash, dh->dh_hashsz);
    266        1.1  dholland 	}
    267        1.1  dholland 	if (dh->dh_blkfree != NULL)
    268        1.1  dholland 		kmem_free(dh->dh_blkfree, dh->dh_blkfreesz);
    269        1.1  dholland 	mutex_destroy(&dh->dh_lock);
    270        1.3  dholland 	pool_cache_put(ulfsdirhash_cache, dh);
    271        1.3  dholland 	atomic_add_int(&ulfs_dirhashmem, -memreqd);
    272        1.1  dholland 	return (-1);
    273        1.1  dholland }
    274        1.1  dholland 
    275        1.1  dholland /*
    276        1.1  dholland  * Free any hash table associated with inode 'ip'.
    277        1.1  dholland  */
    278        1.1  dholland void
    279        1.3  dholland ulfsdirhash_free(struct inode *ip)
    280        1.1  dholland {
    281        1.1  dholland 	struct dirhash *dh;
    282        1.1  dholland 	int i, mem;
    283        1.1  dholland 
    284        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    285        1.1  dholland 		return;
    286        1.1  dholland 
    287       1.17  dholland 	ip->i_dirhash = NULL;
    288       1.17  dholland 
    289        1.1  dholland 	if (dh->dh_onlist) {
    290        1.1  dholland 		DIRHASHLIST_LOCK();
    291        1.1  dholland 		if (dh->dh_onlist)
    292        1.3  dholland 			TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
    293        1.1  dholland 		DIRHASHLIST_UNLOCK();
    294        1.1  dholland 	}
    295        1.1  dholland 
    296        1.1  dholland 	/* The dirhash pointed to by 'dh' is exclusively ours now. */
    297        1.1  dholland 	mem = sizeof(*dh);
    298        1.1  dholland 	if (dh->dh_hash != NULL) {
    299        1.1  dholland 		for (i = 0; i < dh->dh_narrays; i++)
    300        1.1  dholland 			DIRHASH_BLKFREE(dh->dh_hash[i]);
    301        1.1  dholland 		kmem_free(dh->dh_hash, dh->dh_hashsz);
    302        1.1  dholland 		kmem_free(dh->dh_blkfree, dh->dh_blkfreesz);
    303        1.1  dholland 		mem += dh->dh_hashsz;
    304        1.1  dholland 		mem += dh->dh_narrays * DH_NBLKOFF * sizeof(**dh->dh_hash);
    305        1.1  dholland 		mem += dh->dh_nblk * sizeof(*dh->dh_blkfree);
    306        1.1  dholland 	}
    307        1.1  dholland 	mutex_destroy(&dh->dh_lock);
    308        1.3  dholland 	pool_cache_put(ulfsdirhash_cache, dh);
    309        1.1  dholland 
    310        1.3  dholland 	atomic_add_int(&ulfs_dirhashmem, -mem);
    311        1.1  dholland }
    312        1.1  dholland 
    313        1.1  dholland /*
    314        1.1  dholland  * Find the offset of the specified name within the given inode.
    315        1.1  dholland  * Returns 0 on success, ENOENT if the entry does not exist, or
    316        1.1  dholland  * EJUSTRETURN if the caller should revert to a linear search.
    317        1.1  dholland  *
    318        1.1  dholland  * If successful, the directory offset is stored in *offp, and a
    319        1.1  dholland  * pointer to a struct buf containing the entry is stored in *bpp. If
    320        1.1  dholland  * prevoffp is non-NULL, the offset of the previous entry within
    321        1.1  dholland  * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
    322        1.1  dholland  * is the first in a block, the start of the block is used).
    323        1.1  dholland  */
    324        1.1  dholland int
    325        1.3  dholland ulfsdirhash_lookup(struct inode *ip, const char *name, int namelen, doff_t *offp,
    326        1.1  dholland     struct buf **bpp, doff_t *prevoffp)
    327        1.1  dholland {
    328        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    329        1.1  dholland 	struct dirhash *dh, *dh_next;
    330       1.14  dholland 	LFS_DIRHEADER *dp;
    331        1.1  dholland 	struct vnode *vp;
    332        1.1  dholland 	struct buf *bp;
    333        1.1  dholland 	doff_t blkoff, bmask, offset, prevoff;
    334        1.1  dholland 	int i, slot;
    335        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    336        1.1  dholland 
    337        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    338        1.1  dholland 		return (EJUSTRETURN);
    339        1.1  dholland 
    340        1.1  dholland 	/*
    341        1.1  dholland 	 * Move this dirhash towards the end of the list if it has a
    342        1.1  dholland 	 * score higher than the next entry, and acquire the dh_lock.
    343        1.1  dholland 	 * Optimise the case where it's already the last by performing
    344        1.1  dholland 	 * an unlocked read of the TAILQ_NEXT pointer.
    345        1.1  dholland 	 *
    346        1.1  dholland 	 * In both cases, end up holding just dh_lock.
    347        1.1  dholland 	 */
    348        1.1  dholland 	if (TAILQ_NEXT(dh, dh_list) != NULL) {
    349        1.1  dholland 		DIRHASHLIST_LOCK();
    350        1.1  dholland 		DIRHASH_LOCK(dh);
    351        1.1  dholland 		/*
    352        1.1  dholland 		 * If the new score will be greater than that of the next
    353        1.1  dholland 		 * entry, then move this entry past it. With both mutexes
    354        1.1  dholland 		 * held, dh_next won't go away, but its dh_score could
    355        1.1  dholland 		 * change; that's not important since it is just a hint.
    356        1.1  dholland 		 */
    357        1.1  dholland 		if (dh->dh_hash != NULL &&
    358        1.1  dholland 		    (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
    359        1.1  dholland 		    dh->dh_score >= dh_next->dh_score) {
    360        1.1  dholland 			KASSERT(dh->dh_onlist);
    361        1.3  dholland 			TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
    362        1.3  dholland 			TAILQ_INSERT_AFTER(&ulfsdirhash_list, dh_next, dh,
    363        1.1  dholland 			    dh_list);
    364        1.1  dholland 		}
    365        1.1  dholland 		DIRHASHLIST_UNLOCK();
    366        1.1  dholland 	} else {
    367        1.1  dholland 		/* Already the last, though that could change as we wait. */
    368        1.1  dholland 		DIRHASH_LOCK(dh);
    369        1.1  dholland 	}
    370        1.1  dholland 	if (dh->dh_hash == NULL) {
    371        1.1  dholland 		DIRHASH_UNLOCK(dh);
    372        1.3  dholland 		ulfsdirhash_free(ip);
    373        1.1  dholland 		return (EJUSTRETURN);
    374        1.1  dholland 	}
    375        1.1  dholland 
    376        1.1  dholland 	/* Update the score. */
    377        1.1  dholland 	if (dh->dh_score < DH_SCOREMAX)
    378        1.1  dholland 		dh->dh_score++;
    379        1.1  dholland 
    380        1.1  dholland 	vp = ip->i_vnode;
    381        1.3  dholland 	bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
    382        1.1  dholland 	blkoff = -1;
    383        1.1  dholland 	bp = NULL;
    384        1.1  dholland restart:
    385        1.3  dholland 	slot = ulfsdirhash_hash(dh, name, namelen);
    386        1.1  dholland 
    387        1.1  dholland 	if (dh->dh_seqopt) {
    388        1.1  dholland 		/*
    389        1.1  dholland 		 * Sequential access optimisation. dh_seqoff contains the
    390        1.1  dholland 		 * offset of the directory entry immediately following
    391        1.1  dholland 		 * the last entry that was looked up. Check if this offset
    392        1.1  dholland 		 * appears in the hash chain for the name we are looking for.
    393        1.1  dholland 		 */
    394        1.1  dholland 		for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
    395        1.1  dholland 		    i = WRAPINCR(i, dh->dh_hlen))
    396        1.1  dholland 			if (offset == dh->dh_seqoff)
    397        1.1  dholland 				break;
    398        1.1  dholland 		if (offset == dh->dh_seqoff) {
    399        1.1  dholland 			/*
    400        1.1  dholland 			 * We found an entry with the expected offset. This
    401        1.1  dholland 			 * is probably the entry we want, but if not, the
    402        1.1  dholland 			 * code below will turn off seqoff and retry.
    403        1.1  dholland 			 */
    404        1.1  dholland 			slot = i;
    405        1.1  dholland 		} else
    406        1.1  dholland 			dh->dh_seqopt = 0;
    407        1.1  dholland 	}
    408        1.1  dholland 
    409        1.1  dholland 	for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
    410        1.1  dholland 	    slot = WRAPINCR(slot, dh->dh_hlen)) {
    411        1.1  dholland 		if (offset == DIRHASH_DEL)
    412        1.1  dholland 			continue;
    413        1.1  dholland 
    414        1.1  dholland 		if (offset < 0 || offset >= ip->i_size)
    415        1.3  dholland 			panic("ulfsdirhash_lookup: bad offset in hash array");
    416        1.1  dholland 		if ((offset & ~bmask) != blkoff) {
    417        1.1  dholland 			if (bp != NULL)
    418        1.1  dholland 				brelse(bp, 0);
    419        1.1  dholland 			blkoff = offset & ~bmask;
    420        1.3  dholland 			if (ulfs_blkatoff(vp, (off_t)blkoff,
    421        1.1  dholland 			    NULL, &bp, false) != 0) {
    422        1.1  dholland 				DIRHASH_UNLOCK(dh);
    423        1.1  dholland 				return (EJUSTRETURN);
    424        1.1  dholland 			}
    425        1.1  dholland 		}
    426       1.14  dholland 		dp = (LFS_DIRHEADER *)((char *)bp->b_data + (offset & bmask));
    427       1.10  dholland 		if (lfs_dir_getreclen(fs, dp) == 0 || lfs_dir_getreclen(fs, dp) >
    428        1.1  dholland 		    dirblksiz - (offset & (dirblksiz - 1))) {
    429        1.1  dholland 			/* Corrupted directory. */
    430        1.1  dholland 			DIRHASH_UNLOCK(dh);
    431        1.1  dholland 			brelse(bp, 0);
    432        1.1  dholland 			return (EJUSTRETURN);
    433        1.1  dholland 		}
    434        1.9  dholland 		if (lfs_dir_getnamlen(fs, dp) == namelen &&
    435       1.11  dholland 		    memcmp(lfs_dir_nameptr(fs, dp), name, namelen) == 0) {
    436        1.1  dholland 			/* Found. Get the prev offset if needed. */
    437        1.1  dholland 			if (prevoffp != NULL) {
    438        1.1  dholland 				if (offset & (dirblksiz - 1)) {
    439       1.10  dholland 					prevoff = ulfsdirhash_getprev(fs, dp,
    440        1.1  dholland 					    offset, dirblksiz);
    441        1.1  dholland 					if (prevoff == -1) {
    442        1.1  dholland 						brelse(bp, 0);
    443        1.1  dholland 						return (EJUSTRETURN);
    444        1.1  dholland 					}
    445        1.1  dholland 				} else
    446        1.1  dholland 					prevoff = offset;
    447        1.1  dholland 				*prevoffp = prevoff;
    448        1.1  dholland 			}
    449        1.1  dholland 
    450        1.1  dholland 			/* Check for sequential access, and update offset. */
    451        1.1  dholland 			if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
    452        1.1  dholland 				dh->dh_seqopt = 1;
    453        1.9  dholland 			dh->dh_seqoff = offset + LFS_DIRSIZ(fs, dp);
    454        1.1  dholland 			DIRHASH_UNLOCK(dh);
    455        1.1  dholland 
    456        1.1  dholland 			*bpp = bp;
    457        1.1  dholland 			*offp = offset;
    458        1.1  dholland 			return (0);
    459        1.1  dholland 		}
    460        1.1  dholland 
    461        1.1  dholland 		if (dh->dh_hash == NULL) {
    462        1.1  dholland 			DIRHASH_UNLOCK(dh);
    463        1.1  dholland 			if (bp != NULL)
    464        1.1  dholland 				brelse(bp, 0);
    465        1.3  dholland 			ulfsdirhash_free(ip);
    466        1.1  dholland 			return (EJUSTRETURN);
    467        1.1  dholland 		}
    468        1.1  dholland 		/*
    469        1.1  dholland 		 * When the name doesn't match in the seqopt case, go back
    470        1.1  dholland 		 * and search normally.
    471        1.1  dholland 		 */
    472        1.1  dholland 		if (dh->dh_seqopt) {
    473        1.1  dholland 			dh->dh_seqopt = 0;
    474        1.1  dholland 			goto restart;
    475        1.1  dholland 		}
    476        1.1  dholland 	}
    477        1.1  dholland 	DIRHASH_UNLOCK(dh);
    478        1.1  dholland 	if (bp != NULL)
    479        1.1  dholland 		brelse(bp, 0);
    480        1.1  dholland 	return (ENOENT);
    481        1.1  dholland }
    482        1.1  dholland 
    483        1.1  dholland /*
    484        1.1  dholland  * Find a directory block with room for 'slotneeded' bytes. Returns
    485        1.1  dholland  * the offset of the directory entry that begins the free space.
    486        1.1  dholland  * This will either be the offset of an existing entry that has free
    487        1.1  dholland  * space at the end, or the offset of an entry with d_ino == 0 at
    488        1.1  dholland  * the start of a DIRBLKSIZ block.
    489        1.1  dholland  *
    490        1.1  dholland  * To use the space, the caller may need to compact existing entries in
    491        1.1  dholland  * the directory. The total number of bytes in all of the entries involved
    492        1.1  dholland  * in the compaction is stored in *slotsize. In other words, all of
    493        1.1  dholland  * the entries that must be compacted are exactly contained in the
    494        1.1  dholland  * region beginning at the returned offset and spanning *slotsize bytes.
    495        1.1  dholland  *
    496        1.1  dholland  * Returns -1 if no space was found, indicating that the directory
    497        1.1  dholland  * must be extended.
    498        1.1  dholland  */
    499        1.1  dholland doff_t
    500        1.3  dholland ulfsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
    501        1.1  dholland {
    502        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    503       1.14  dholland 	LFS_DIRHEADER *dp;
    504        1.1  dholland 	struct dirhash *dh;
    505        1.1  dholland 	struct buf *bp;
    506        1.1  dholland 	doff_t pos, slotstart;
    507        1.1  dholland 	int dirblock, error, freebytes, i;
    508        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    509        1.1  dholland 
    510        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    511        1.1  dholland 		return (-1);
    512        1.1  dholland 
    513        1.1  dholland 	DIRHASH_LOCK(dh);
    514        1.1  dholland 	if (dh->dh_hash == NULL) {
    515        1.1  dholland 		DIRHASH_UNLOCK(dh);
    516        1.3  dholland 		ulfsdirhash_free(ip);
    517        1.1  dholland 		return (-1);
    518        1.1  dholland 	}
    519        1.1  dholland 
    520        1.1  dholland 	/* Find a directory block with the desired free space. */
    521        1.1  dholland 	dirblock = -1;
    522        1.1  dholland 	for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
    523        1.1  dholland 		if ((dirblock = dh->dh_firstfree[i]) != -1)
    524        1.1  dholland 			break;
    525        1.1  dholland 	if (dirblock == -1) {
    526        1.1  dholland 		DIRHASH_UNLOCK(dh);
    527        1.1  dholland 		return (-1);
    528        1.1  dholland 	}
    529        1.1  dholland 
    530        1.1  dholland 	KASSERT(dirblock < dh->dh_nblk &&
    531        1.1  dholland 	    dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN));
    532        1.1  dholland 	pos = dirblock * dirblksiz;
    533        1.3  dholland 	error = ulfs_blkatoff(ip->i_vnode, (off_t)pos, (void *)&dp, &bp, false);
    534        1.1  dholland 	if (error) {
    535        1.1  dholland 		DIRHASH_UNLOCK(dh);
    536        1.1  dholland 		return (-1);
    537        1.1  dholland 	}
    538        1.1  dholland 	/* Find the first entry with free space. */
    539        1.1  dholland 	for (i = 0; i < dirblksiz; ) {
    540       1.10  dholland 		if (lfs_dir_getreclen(fs, dp) == 0) {
    541        1.1  dholland 			DIRHASH_UNLOCK(dh);
    542        1.1  dholland 			brelse(bp, 0);
    543        1.1  dholland 			return (-1);
    544        1.1  dholland 		}
    545       1.10  dholland 		if (lfs_dir_getino(fs, dp) == 0 || lfs_dir_getreclen(fs, dp) > LFS_DIRSIZ(fs, dp))
    546        1.1  dholland 			break;
    547       1.10  dholland 		i += lfs_dir_getreclen(fs, dp);
    548       1.10  dholland 		dp = LFS_NEXTDIR(fs, dp);
    549        1.1  dholland 	}
    550        1.1  dholland 	if (i > dirblksiz) {
    551        1.1  dholland 		DIRHASH_UNLOCK(dh);
    552        1.1  dholland 		brelse(bp, 0);
    553        1.1  dholland 		return (-1);
    554        1.1  dholland 	}
    555        1.1  dholland 	slotstart = pos + i;
    556        1.1  dholland 
    557        1.1  dholland 	/* Find the range of entries needed to get enough space */
    558        1.1  dholland 	freebytes = 0;
    559        1.1  dholland 	while (i < dirblksiz && freebytes < slotneeded) {
    560       1.10  dholland 		freebytes += lfs_dir_getreclen(fs, dp);
    561       1.10  dholland 		if (lfs_dir_getino(fs, dp) != 0)
    562        1.9  dholland 			freebytes -= LFS_DIRSIZ(fs, dp);
    563       1.10  dholland 		if (lfs_dir_getreclen(fs, dp) == 0) {
    564        1.1  dholland 			DIRHASH_UNLOCK(dh);
    565        1.1  dholland 			brelse(bp, 0);
    566        1.1  dholland 			return (-1);
    567        1.1  dholland 		}
    568       1.10  dholland 		i += lfs_dir_getreclen(fs, dp);
    569       1.10  dholland 		dp = LFS_NEXTDIR(fs, dp);
    570        1.1  dholland 	}
    571        1.1  dholland 	if (i > dirblksiz) {
    572        1.1  dholland 		DIRHASH_UNLOCK(dh);
    573        1.1  dholland 		brelse(bp, 0);
    574        1.1  dholland 		return (-1);
    575        1.1  dholland 	}
    576        1.1  dholland 	if (freebytes < slotneeded)
    577        1.3  dholland 		panic("ulfsdirhash_findfree: free mismatch");
    578        1.1  dholland 	DIRHASH_UNLOCK(dh);
    579        1.1  dholland 	brelse(bp, 0);
    580        1.1  dholland 	*slotsize = pos + i - slotstart;
    581        1.1  dholland 	return (slotstart);
    582        1.1  dholland }
    583        1.1  dholland 
    584        1.1  dholland /*
    585        1.1  dholland  * Return the start of the unused space at the end of a directory, or
    586        1.1  dholland  * -1 if there are no trailing unused blocks.
    587        1.1  dholland  */
    588        1.1  dholland doff_t
    589        1.3  dholland ulfsdirhash_enduseful(struct inode *ip)
    590        1.1  dholland {
    591        1.1  dholland 	struct dirhash *dh;
    592        1.1  dholland 	int i;
    593        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    594        1.1  dholland 
    595        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    596        1.1  dholland 		return (-1);
    597        1.1  dholland 
    598        1.1  dholland 	DIRHASH_LOCK(dh);
    599        1.1  dholland 	if (dh->dh_hash == NULL) {
    600        1.1  dholland 		DIRHASH_UNLOCK(dh);
    601        1.3  dholland 		ulfsdirhash_free(ip);
    602        1.1  dholland 		return (-1);
    603        1.1  dholland 	}
    604        1.1  dholland 
    605        1.1  dholland 	if (dh->dh_blkfree[dh->dh_dirblks - 1] != dirblksiz / DIRALIGN) {
    606        1.1  dholland 		DIRHASH_UNLOCK(dh);
    607        1.1  dholland 		return (-1);
    608        1.1  dholland 	}
    609        1.1  dholland 
    610        1.1  dholland 	for (i = dh->dh_dirblks - 1; i >= 0; i--)
    611        1.1  dholland 		if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
    612        1.1  dholland 			break;
    613        1.1  dholland 	DIRHASH_UNLOCK(dh);
    614        1.1  dholland 	return ((doff_t)(i + 1) * dirblksiz);
    615        1.1  dholland }
    616        1.1  dholland 
    617        1.1  dholland /*
    618        1.1  dholland  * Insert information into the hash about a new directory entry. dirp
    619        1.4  dholland  * points to a struct lfs_direct containing the entry, and offset specifies
    620        1.1  dholland  * the offset of this entry.
    621        1.1  dholland  */
    622        1.1  dholland void
    623       1.14  dholland ulfsdirhash_add(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
    624        1.1  dholland {
    625        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    626        1.1  dholland 	struct dirhash *dh;
    627        1.1  dholland 	int slot;
    628        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    629        1.1  dholland 
    630        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    631        1.1  dholland 		return;
    632        1.1  dholland 
    633        1.1  dholland 	DIRHASH_LOCK(dh);
    634        1.1  dholland 	if (dh->dh_hash == NULL) {
    635        1.1  dholland 		DIRHASH_UNLOCK(dh);
    636        1.3  dholland 		ulfsdirhash_free(ip);
    637        1.1  dholland 		return;
    638        1.1  dholland 	}
    639        1.1  dholland 
    640        1.1  dholland 	KASSERT(offset < dh->dh_dirblks * dirblksiz);
    641        1.1  dholland 	/*
    642        1.1  dholland 	 * Normal hash usage is < 66%. If the usage gets too high then
    643        1.1  dholland 	 * remove the hash entirely and let it be rebuilt later.
    644        1.1  dholland 	 */
    645        1.1  dholland 	if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
    646        1.1  dholland 		DIRHASH_UNLOCK(dh);
    647        1.3  dholland 		ulfsdirhash_free(ip);
    648        1.1  dholland 		return;
    649        1.1  dholland 	}
    650        1.1  dholland 
    651        1.1  dholland 	/* Find a free hash slot (empty or deleted), and add the entry. */
    652       1.11  dholland 	slot = ulfsdirhash_hash(dh, lfs_dir_nameptr(fs, dirp),
    653       1.11  dholland 				lfs_dir_getnamlen(fs, dirp));
    654        1.1  dholland 	while (DH_ENTRY(dh, slot) >= 0)
    655        1.1  dholland 		slot = WRAPINCR(slot, dh->dh_hlen);
    656        1.1  dholland 	if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
    657        1.1  dholland 		dh->dh_hused++;
    658        1.1  dholland 	DH_ENTRY(dh, slot) = offset;
    659        1.1  dholland 
    660        1.1  dholland 	/* Update the per-block summary info. */
    661        1.9  dholland 	ulfsdirhash_adjfree(dh, offset, -LFS_DIRSIZ(fs, dirp), dirblksiz);
    662        1.1  dholland 	DIRHASH_UNLOCK(dh);
    663        1.1  dholland }
    664        1.1  dholland 
    665        1.1  dholland /*
    666        1.1  dholland  * Remove the specified directory entry from the hash. The entry to remove
    667        1.1  dholland  * is defined by the name in `dirp', which must exist at the specified
    668        1.1  dholland  * `offset' within the directory.
    669        1.1  dholland  */
    670        1.1  dholland void
    671       1.14  dholland ulfsdirhash_remove(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
    672        1.1  dholland {
    673        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    674        1.1  dholland 	struct dirhash *dh;
    675        1.1  dholland 	int slot;
    676        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    677        1.1  dholland 
    678        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    679        1.1  dholland 		return;
    680        1.1  dholland 
    681        1.1  dholland 	DIRHASH_LOCK(dh);
    682        1.1  dholland 	if (dh->dh_hash == NULL) {
    683        1.1  dholland 		DIRHASH_UNLOCK(dh);
    684        1.3  dholland 		ulfsdirhash_free(ip);
    685        1.1  dholland 		return;
    686        1.1  dholland 	}
    687        1.1  dholland 
    688        1.1  dholland 	KASSERT(offset < dh->dh_dirblks * dirblksiz);
    689        1.1  dholland 	/* Find the entry */
    690       1.11  dholland 	slot = ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dirp),
    691        1.9  dholland 				    lfs_dir_getnamlen(fs, dirp), offset);
    692        1.1  dholland 
    693        1.1  dholland 	/* Remove the hash entry. */
    694        1.3  dholland 	ulfsdirhash_delslot(dh, slot);
    695        1.1  dholland 
    696        1.1  dholland 	/* Update the per-block summary info. */
    697        1.9  dholland 	ulfsdirhash_adjfree(dh, offset, LFS_DIRSIZ(fs, dirp), dirblksiz);
    698        1.1  dholland 	DIRHASH_UNLOCK(dh);
    699        1.1  dholland }
    700        1.1  dholland 
    701        1.1  dholland /*
    702        1.1  dholland  * Change the offset associated with a directory entry in the hash. Used
    703        1.1  dholland  * when compacting directory blocks.
    704        1.1  dholland  */
    705        1.1  dholland void
    706       1.14  dholland ulfsdirhash_move(struct inode *ip, LFS_DIRHEADER *dirp, doff_t oldoff,
    707        1.1  dholland     doff_t newoff)
    708        1.1  dholland {
    709        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    710        1.1  dholland 	struct dirhash *dh;
    711        1.1  dholland 	int slot;
    712        1.1  dholland 
    713        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    714        1.1  dholland 		return;
    715        1.1  dholland 	DIRHASH_LOCK(dh);
    716        1.1  dholland 	if (dh->dh_hash == NULL) {
    717        1.1  dholland 		DIRHASH_UNLOCK(dh);
    718        1.3  dholland 		ulfsdirhash_free(ip);
    719        1.1  dholland 		return;
    720        1.1  dholland 	}
    721        1.1  dholland 
    722        1.7  dholland 	KASSERT(oldoff < dh->dh_dirblks * ip->i_lfs->um_dirblksiz &&
    723        1.7  dholland 	    newoff < dh->dh_dirblks * ip->i_lfs->um_dirblksiz);
    724        1.1  dholland 	/* Find the entry, and update the offset. */
    725       1.11  dholland 	slot = ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dirp),
    726        1.9  dholland 				    lfs_dir_getnamlen(fs, dirp), oldoff);
    727        1.1  dholland 	DH_ENTRY(dh, slot) = newoff;
    728        1.1  dholland 	DIRHASH_UNLOCK(dh);
    729        1.1  dholland }
    730        1.1  dholland 
    731        1.1  dholland /*
    732        1.1  dholland  * Inform dirhash that the directory has grown by one block that
    733        1.1  dholland  * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
    734        1.1  dholland  */
    735        1.1  dholland void
    736        1.3  dholland ulfsdirhash_newblk(struct inode *ip, doff_t offset)
    737        1.1  dholland {
    738        1.1  dholland 	struct dirhash *dh;
    739        1.1  dholland 	int block;
    740        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    741        1.1  dholland 
    742        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    743        1.1  dholland 		return;
    744        1.1  dholland 	DIRHASH_LOCK(dh);
    745        1.1  dholland 	if (dh->dh_hash == NULL) {
    746        1.1  dholland 		DIRHASH_UNLOCK(dh);
    747        1.3  dholland 		ulfsdirhash_free(ip);
    748        1.1  dholland 		return;
    749        1.1  dholland 	}
    750        1.1  dholland 
    751        1.1  dholland 	KASSERT(offset == dh->dh_dirblks * dirblksiz);
    752        1.1  dholland 	block = offset / dirblksiz;
    753        1.1  dholland 	if (block >= dh->dh_nblk) {
    754        1.1  dholland 		/* Out of space; must rebuild. */
    755        1.1  dholland 		DIRHASH_UNLOCK(dh);
    756        1.3  dholland 		ulfsdirhash_free(ip);
    757        1.1  dholland 		return;
    758        1.1  dholland 	}
    759        1.1  dholland 	dh->dh_dirblks = block + 1;
    760        1.1  dholland 
    761        1.1  dholland 	/* Account for the new free block. */
    762        1.1  dholland 	dh->dh_blkfree[block] = dirblksiz / DIRALIGN;
    763        1.1  dholland 	if (dh->dh_firstfree[DH_NFSTATS] == -1)
    764        1.1  dholland 		dh->dh_firstfree[DH_NFSTATS] = block;
    765        1.1  dholland 	DIRHASH_UNLOCK(dh);
    766        1.1  dholland }
    767        1.1  dholland 
    768        1.1  dholland /*
    769        1.1  dholland  * Inform dirhash that the directory is being truncated.
    770        1.1  dholland  */
    771        1.1  dholland void
    772        1.3  dholland ulfsdirhash_dirtrunc(struct inode *ip, doff_t offset)
    773        1.1  dholland {
    774        1.1  dholland 	struct dirhash *dh;
    775        1.1  dholland 	int block, i;
    776        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    777        1.1  dholland 
    778        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    779        1.1  dholland 		return;
    780        1.1  dholland 
    781        1.1  dholland 	DIRHASH_LOCK(dh);
    782        1.1  dholland 	if (dh->dh_hash == NULL) {
    783        1.1  dholland 		DIRHASH_UNLOCK(dh);
    784        1.3  dholland 		ulfsdirhash_free(ip);
    785        1.1  dholland 		return;
    786        1.1  dholland 	}
    787        1.1  dholland 
    788        1.1  dholland 	KASSERT(offset <= dh->dh_dirblks * dirblksiz);
    789        1.1  dholland 	block = howmany(offset, dirblksiz);
    790        1.1  dholland 	/*
    791        1.1  dholland 	 * If the directory shrinks to less than 1/8 of dh_nblk blocks
    792        1.1  dholland 	 * (about 20% of its original size due to the 50% extra added in
    793        1.3  dholland 	 * ulfsdirhash_build) then free it, and let the caller rebuild
    794        1.1  dholland 	 * if necessary.
    795        1.1  dholland 	 */
    796        1.1  dholland 	if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
    797        1.1  dholland 		DIRHASH_UNLOCK(dh);
    798        1.3  dholland 		ulfsdirhash_free(ip);
    799        1.1  dholland 		return;
    800        1.1  dholland 	}
    801        1.1  dholland 
    802        1.1  dholland 	/*
    803        1.1  dholland 	 * Remove any `first free' information pertaining to the
    804        1.1  dholland 	 * truncated blocks. All blocks we're removing should be
    805        1.1  dholland 	 * completely unused.
    806        1.1  dholland 	 */
    807        1.1  dholland 	if (dh->dh_firstfree[DH_NFSTATS] >= block)
    808        1.1  dholland 		dh->dh_firstfree[DH_NFSTATS] = -1;
    809        1.1  dholland 	for (i = block; i < dh->dh_dirblks; i++)
    810        1.1  dholland 		if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
    811        1.3  dholland 			panic("ulfsdirhash_dirtrunc: blocks in use");
    812        1.1  dholland 	for (i = 0; i < DH_NFSTATS; i++)
    813        1.1  dholland 		if (dh->dh_firstfree[i] >= block)
    814        1.3  dholland 			panic("ulfsdirhash_dirtrunc: first free corrupt");
    815        1.1  dholland 	dh->dh_dirblks = block;
    816        1.1  dholland 	DIRHASH_UNLOCK(dh);
    817        1.1  dholland }
    818        1.1  dholland 
    819        1.1  dholland /*
    820        1.1  dholland  * Debugging function to check that the dirhash information about
    821        1.1  dholland  * a directory block matches its actual contents. Panics if a mismatch
    822        1.1  dholland  * is detected.
    823        1.1  dholland  *
    824        1.1  dholland  * On entry, `sbuf' should point to the start of an in-core
    825        1.1  dholland  * DIRBLKSIZ-sized directory block, and `offset' should contain the
    826        1.1  dholland  * offset from the start of the directory of that block.
    827        1.1  dholland  */
    828        1.1  dholland void
    829        1.3  dholland ulfsdirhash_checkblock(struct inode *ip, char *sbuf, doff_t offset)
    830        1.1  dholland {
    831        1.9  dholland 	struct lfs *fs = ip->i_lfs;
    832        1.1  dholland 	struct dirhash *dh;
    833       1.14  dholland 	LFS_DIRHEADER *dp;
    834        1.1  dholland 	int block, ffslot, i, nfree;
    835        1.7  dholland 	int dirblksiz = ip->i_lfs->um_dirblksiz;
    836        1.1  dholland 
    837        1.3  dholland 	if (!ulfs_dirhashcheck)
    838        1.1  dholland 		return;
    839        1.1  dholland 	if ((dh = ip->i_dirhash) == NULL)
    840        1.1  dholland 		return;
    841        1.1  dholland 
    842        1.1  dholland 	DIRHASH_LOCK(dh);
    843        1.1  dholland 	if (dh->dh_hash == NULL) {
    844        1.1  dholland 		DIRHASH_UNLOCK(dh);
    845        1.3  dholland 		ulfsdirhash_free(ip);
    846        1.1  dholland 		return;
    847        1.1  dholland 	}
    848        1.1  dholland 
    849        1.1  dholland 	block = offset / dirblksiz;
    850        1.1  dholland 	if ((offset & (dirblksiz - 1)) != 0 || block >= dh->dh_dirblks)
    851        1.3  dholland 		panic("ulfsdirhash_checkblock: bad offset");
    852        1.1  dholland 
    853        1.1  dholland 	nfree = 0;
    854       1.10  dholland 	for (i = 0; i < dirblksiz; i += lfs_dir_getreclen(fs, dp)) {
    855       1.14  dholland 		dp = (LFS_DIRHEADER *)(sbuf + i);
    856       1.10  dholland 		if (lfs_dir_getreclen(fs, dp) == 0 || i + lfs_dir_getreclen(fs, dp) > dirblksiz)
    857        1.3  dholland 			panic("ulfsdirhash_checkblock: bad dir");
    858        1.1  dholland 
    859       1.10  dholland 		if (lfs_dir_getino(fs, dp) == 0) {
    860        1.1  dholland #if 0
    861        1.1  dholland 			/*
    862        1.1  dholland 			 * XXX entries with d_ino == 0 should only occur
    863        1.1  dholland 			 * at the start of a DIRBLKSIZ block. However the
    864        1.3  dholland 			 * ulfs code is tolerant of such entries at other
    865        1.1  dholland 			 * offsets, and fsck does not fix them.
    866        1.1  dholland 			 */
    867        1.1  dholland 			if (i != 0)
    868        1.3  dholland 				panic("ulfsdirhash_checkblock: bad dir inode");
    869        1.1  dholland #endif
    870       1.10  dholland 			nfree += lfs_dir_getreclen(fs, dp);
    871        1.1  dholland 			continue;
    872        1.1  dholland 		}
    873        1.1  dholland 
    874        1.1  dholland 		/* Check that the entry	exists (will panic if it doesn't). */
    875       1.11  dholland 		ulfsdirhash_findslot(dh, lfs_dir_nameptr(fs, dp),
    876       1.11  dholland 				     lfs_dir_getnamlen(fs, dp),
    877        1.9  dholland 				     offset + i);
    878        1.1  dholland 
    879       1.10  dholland 		nfree += lfs_dir_getreclen(fs, dp) - LFS_DIRSIZ(fs, dp);
    880        1.1  dholland 	}
    881        1.1  dholland 	if (i != dirblksiz)
    882        1.3  dholland 		panic("ulfsdirhash_checkblock: bad dir end");
    883        1.1  dholland 
    884        1.1  dholland 	if (dh->dh_blkfree[block] * DIRALIGN != nfree)
    885        1.3  dholland 		panic("ulfsdirhash_checkblock: bad free count");
    886        1.1  dholland 
    887        1.1  dholland 	ffslot = BLKFREE2IDX(nfree / DIRALIGN);
    888        1.1  dholland 	for (i = 0; i <= DH_NFSTATS; i++)
    889        1.1  dholland 		if (dh->dh_firstfree[i] == block && i != ffslot)
    890        1.3  dholland 			panic("ulfsdirhash_checkblock: bad first-free");
    891        1.1  dholland 	if (dh->dh_firstfree[ffslot] == -1)
    892        1.3  dholland 		panic("ulfsdirhash_checkblock: missing first-free entry");
    893        1.1  dholland 	DIRHASH_UNLOCK(dh);
    894        1.1  dholland }
    895        1.1  dholland 
    896        1.1  dholland /*
    897        1.1  dholland  * Hash the specified filename into a dirhash slot.
    898        1.1  dholland  */
    899        1.1  dholland static int
    900        1.3  dholland ulfsdirhash_hash(struct dirhash *dh, const char *name, int namelen)
    901        1.1  dholland {
    902        1.1  dholland 	u_int32_t hash;
    903        1.1  dholland 
    904        1.1  dholland 	/*
    905        1.1  dholland 	 * We hash the name and then some other bit of data that is
    906        1.1  dholland 	 * invariant over the dirhash's lifetime. Otherwise names
    907        1.1  dholland 	 * differing only in the last byte are placed close to one
    908        1.1  dholland 	 * another in the table, which is bad for linear probing.
    909        1.1  dholland 	 */
    910        1.1  dholland 	hash = hash32_buf(name, namelen, HASH32_BUF_INIT);
    911        1.1  dholland 	hash = hash32_buf(&dh, sizeof(dh), hash);
    912        1.1  dholland 	return (hash % dh->dh_hlen);
    913        1.1  dholland }
    914        1.1  dholland 
    915        1.1  dholland /*
    916        1.1  dholland  * Adjust the number of free bytes in the block containing `offset'
    917        1.1  dholland  * by the value specified by `diff'.
    918        1.1  dholland  *
    919        1.1  dholland  * The caller must ensure we have exclusive access to `dh'; normally
    920        1.1  dholland  * that means that dh_lock should be held, but this is also called
    921        1.3  dholland  * from ulfsdirhash_build() where exclusive access can be assumed.
    922        1.1  dholland  */
    923        1.1  dholland static void
    924        1.3  dholland ulfsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff, int dirblksiz)
    925        1.1  dholland {
    926        1.1  dholland 	int block, i, nfidx, ofidx;
    927        1.1  dholland 
    928        1.1  dholland 	KASSERT(mutex_owned(&dh->dh_lock));
    929        1.1  dholland 
    930        1.1  dholland 	/* Update the per-block summary info. */
    931        1.1  dholland 	block = offset / dirblksiz;
    932        1.1  dholland 	KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks);
    933        1.1  dholland 	ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
    934        1.1  dholland 	dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
    935        1.1  dholland 	nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
    936        1.1  dholland 
    937        1.1  dholland 	/* Update the `first free' list if necessary. */
    938        1.1  dholland 	if (ofidx != nfidx) {
    939        1.1  dholland 		/* If removing, scan forward for the next block. */
    940        1.1  dholland 		if (dh->dh_firstfree[ofidx] == block) {
    941        1.1  dholland 			for (i = block + 1; i < dh->dh_dirblks; i++)
    942        1.1  dholland 				if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
    943        1.1  dholland 					break;
    944        1.1  dholland 			dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
    945        1.1  dholland 		}
    946        1.1  dholland 
    947        1.1  dholland 		/* Make this the new `first free' if necessary */
    948        1.1  dholland 		if (dh->dh_firstfree[nfidx] > block ||
    949        1.1  dholland 		    dh->dh_firstfree[nfidx] == -1)
    950        1.1  dholland 			dh->dh_firstfree[nfidx] = block;
    951        1.1  dholland 	}
    952        1.1  dholland }
    953        1.1  dholland 
    954        1.1  dholland /*
    955        1.1  dholland  * Find the specified name which should have the specified offset.
    956        1.1  dholland  * Returns a slot number, and panics on failure.
    957        1.1  dholland  *
    958        1.1  dholland  * `dh' must be locked on entry and remains so on return.
    959        1.1  dholland  */
    960        1.1  dholland static int
    961        1.3  dholland ulfsdirhash_findslot(struct dirhash *dh, const char *name, int namelen,
    962        1.1  dholland     doff_t offset)
    963        1.1  dholland {
    964        1.1  dholland 	int slot;
    965        1.1  dholland 
    966        1.1  dholland 	KASSERT(mutex_owned(&dh->dh_lock));
    967        1.1  dholland 
    968        1.1  dholland 	/* Find the entry. */
    969        1.1  dholland 	KASSERT(dh->dh_hused < dh->dh_hlen);
    970        1.3  dholland 	slot = ulfsdirhash_hash(dh, name, namelen);
    971        1.1  dholland 	while (DH_ENTRY(dh, slot) != offset &&
    972        1.1  dholland 	    DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
    973        1.1  dholland 		slot = WRAPINCR(slot, dh->dh_hlen);
    974        1.1  dholland 	if (DH_ENTRY(dh, slot) != offset)
    975        1.3  dholland 		panic("ulfsdirhash_findslot: '%.*s' not found", namelen, name);
    976        1.1  dholland 
    977        1.1  dholland 	return (slot);
    978        1.1  dholland }
    979        1.1  dholland 
    980        1.1  dholland /*
    981        1.1  dholland  * Remove the entry corresponding to the specified slot from the hash array.
    982        1.1  dholland  *
    983        1.1  dholland  * `dh' must be locked on entry and remains so on return.
    984        1.1  dholland  */
    985        1.1  dholland static void
    986        1.3  dholland ulfsdirhash_delslot(struct dirhash *dh, int slot)
    987        1.1  dholland {
    988        1.1  dholland 	int i;
    989        1.1  dholland 
    990        1.1  dholland 	KASSERT(mutex_owned(&dh->dh_lock));
    991        1.1  dholland 
    992        1.1  dholland 	/* Mark the entry as deleted. */
    993        1.1  dholland 	DH_ENTRY(dh, slot) = DIRHASH_DEL;
    994        1.1  dholland 
    995        1.1  dholland 	/* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
    996        1.1  dholland 	for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
    997        1.1  dholland 		i = WRAPINCR(i, dh->dh_hlen);
    998        1.1  dholland 	if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
    999        1.1  dholland 		i = WRAPDECR(i, dh->dh_hlen);
   1000        1.1  dholland 		while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
   1001        1.1  dholland 			DH_ENTRY(dh, i) = DIRHASH_EMPTY;
   1002        1.1  dholland 			dh->dh_hused--;
   1003        1.1  dholland 			i = WRAPDECR(i, dh->dh_hlen);
   1004        1.1  dholland 		}
   1005        1.1  dholland 		KASSERT(dh->dh_hused >= 0);
   1006        1.1  dholland 	}
   1007        1.1  dholland }
   1008        1.1  dholland 
   1009        1.1  dholland /*
   1010        1.1  dholland  * Given a directory entry and its offset, find the offset of the
   1011        1.1  dholland  * previous entry in the same DIRBLKSIZ-sized block. Returns an
   1012        1.1  dholland  * offset, or -1 if there is no previous entry in the block or some
   1013        1.1  dholland  * other problem occurred.
   1014        1.1  dholland  */
   1015        1.1  dholland static doff_t
   1016       1.14  dholland ulfsdirhash_getprev(struct lfs *fs, LFS_DIRHEADER *dirp,
   1017       1.10  dholland 		doff_t offset, int dirblksiz)
   1018        1.1  dholland {
   1019       1.14  dholland 	LFS_DIRHEADER *dp;
   1020        1.1  dholland 	char *blkbuf;
   1021        1.1  dholland 	doff_t blkoff, prevoff;
   1022        1.1  dholland 	int entrypos, i;
   1023       1.10  dholland 	unsigned reclen;
   1024        1.1  dholland 
   1025        1.1  dholland 	blkoff = offset & ~(dirblksiz - 1);	/* offset of start of block */
   1026        1.1  dholland 	entrypos = offset & (dirblksiz - 1);	/* entry relative to block */
   1027        1.1  dholland 	blkbuf = (char *)dirp - entrypos;
   1028        1.1  dholland 	prevoff = blkoff;
   1029        1.1  dholland 
   1030        1.1  dholland 	/* If `offset' is the start of a block, there is no previous entry. */
   1031        1.1  dholland 	if (entrypos == 0)
   1032        1.1  dholland 		return (-1);
   1033        1.1  dholland 
   1034        1.1  dholland 	/* Scan from the start of the block until we get to the entry. */
   1035       1.10  dholland 	for (i = 0; i < entrypos; i += reclen) {
   1036       1.14  dholland 		dp = (LFS_DIRHEADER *)(blkbuf + i);
   1037       1.10  dholland 		reclen = lfs_dir_getreclen(fs, dp);
   1038       1.10  dholland 		if (reclen == 0 || i + reclen > entrypos)
   1039        1.1  dholland 			return (-1);	/* Corrupted directory. */
   1040        1.1  dholland 		prevoff = blkoff + i;
   1041        1.1  dholland 	}
   1042        1.1  dholland 	return (prevoff);
   1043        1.1  dholland }
   1044        1.1  dholland 
   1045        1.1  dholland /*
   1046        1.1  dholland  * Try to free up `wanted' bytes by stealing memory from existing
   1047        1.1  dholland  * dirhashes. Returns zero with list locked if successful.
   1048        1.1  dholland  */
   1049        1.1  dholland static int
   1050        1.3  dholland ulfsdirhash_recycle(int wanted)
   1051        1.1  dholland {
   1052        1.1  dholland 	struct dirhash *dh;
   1053        1.1  dholland 	doff_t **hash;
   1054        1.1  dholland 	u_int8_t *blkfree;
   1055        1.1  dholland 	int i, mem, narrays;
   1056        1.1  dholland 	size_t hashsz, blkfreesz;
   1057        1.1  dholland 
   1058        1.1  dholland 	DIRHASHLIST_LOCK();
   1059        1.3  dholland 	while (wanted + ulfs_dirhashmem > ulfs_dirhashmaxmem) {
   1060        1.1  dholland 		/* Find a dirhash, and lock it. */
   1061        1.3  dholland 		if ((dh = TAILQ_FIRST(&ulfsdirhash_list)) == NULL) {
   1062        1.1  dholland 			DIRHASHLIST_UNLOCK();
   1063        1.1  dholland 			return (-1);
   1064        1.1  dholland 		}
   1065        1.1  dholland 		DIRHASH_LOCK(dh);
   1066        1.1  dholland 		KASSERT(dh->dh_hash != NULL);
   1067        1.1  dholland 
   1068        1.1  dholland 		/* Decrement the score; only recycle if it becomes zero. */
   1069        1.1  dholland 		if (--dh->dh_score > 0) {
   1070        1.1  dholland 			DIRHASH_UNLOCK(dh);
   1071        1.1  dholland 			DIRHASHLIST_UNLOCK();
   1072        1.1  dholland 			return (-1);
   1073        1.1  dholland 		}
   1074        1.1  dholland 
   1075        1.1  dholland 		/* Remove it from the list and detach its memory. */
   1076        1.3  dholland 		TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
   1077        1.1  dholland 		dh->dh_onlist = 0;
   1078        1.1  dholland 		hash = dh->dh_hash;
   1079        1.1  dholland 		hashsz = dh->dh_hashsz;
   1080        1.1  dholland 		dh->dh_hash = NULL;
   1081        1.1  dholland 		blkfree = dh->dh_blkfree;
   1082        1.1  dholland 		blkfreesz = dh->dh_blkfreesz;
   1083        1.1  dholland 		dh->dh_blkfree = NULL;
   1084        1.1  dholland 		narrays = dh->dh_narrays;
   1085        1.1  dholland 		mem = narrays * sizeof(*dh->dh_hash) +
   1086        1.1  dholland 		    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
   1087        1.1  dholland 		    dh->dh_nblk * sizeof(*dh->dh_blkfree);
   1088        1.1  dholland 
   1089        1.1  dholland 		/* Unlock everything, free the detached memory. */
   1090        1.1  dholland 		DIRHASH_UNLOCK(dh);
   1091        1.1  dholland 		DIRHASHLIST_UNLOCK();
   1092        1.1  dholland 
   1093        1.1  dholland 		for (i = 0; i < narrays; i++)
   1094        1.1  dholland 			DIRHASH_BLKFREE(hash[i]);
   1095        1.1  dholland 		kmem_free(hash, hashsz);
   1096        1.1  dholland 		kmem_free(blkfree, blkfreesz);
   1097        1.1  dholland 
   1098        1.1  dholland 		/* Account for the returned memory, and repeat if necessary. */
   1099        1.1  dholland 		DIRHASHLIST_LOCK();
   1100        1.3  dholland 		atomic_add_int(&ulfs_dirhashmem, -mem);
   1101        1.1  dholland 	}
   1102        1.1  dholland 	/* Success. */
   1103        1.1  dholland 	return (0);
   1104        1.1  dholland }
   1105        1.1  dholland 
   1106        1.1  dholland static void
   1107        1.3  dholland ulfsdirhash_sysctl_init(void)
   1108        1.1  dholland {
   1109        1.1  dholland 	const struct sysctlnode *rnode, *cnode;
   1110        1.1  dholland 
   1111        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, NULL, &rnode,
   1112        1.1  dholland 		       CTLFLAG_PERMANENT,
   1113        1.3  dholland 		       CTLTYPE_NODE, "ulfs",
   1114        1.3  dholland 		       SYSCTL_DESCR("ulfs"),
   1115        1.1  dholland 		       NULL, 0, NULL, 0,
   1116        1.8     pooka 		       CTL_VFS, CTL_CREATE, CTL_EOL);
   1117        1.1  dholland 
   1118        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &rnode,
   1119        1.1  dholland 		       CTLFLAG_PERMANENT,
   1120        1.1  dholland 		       CTLTYPE_NODE, "dirhash",
   1121        1.1  dholland 		       SYSCTL_DESCR("dirhash"),
   1122        1.1  dholland 		       NULL, 0, NULL, 0,
   1123        1.1  dholland 		       CTL_CREATE, CTL_EOL);
   1124        1.1  dholland 
   1125        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1126        1.1  dholland 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1127        1.1  dholland 		       CTLTYPE_INT, "minblocks",
   1128        1.1  dholland 		       SYSCTL_DESCR("minimum hashed directory size in blocks"),
   1129        1.3  dholland 		       NULL, 0, &ulfs_dirhashminblks, 0,
   1130        1.1  dholland 		       CTL_CREATE, CTL_EOL);
   1131        1.1  dholland 
   1132        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1133        1.1  dholland 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1134        1.1  dholland 		       CTLTYPE_INT, "maxmem",
   1135        1.1  dholland 		       SYSCTL_DESCR("maximum dirhash memory usage"),
   1136        1.3  dholland 		       NULL, 0, &ulfs_dirhashmaxmem, 0,
   1137        1.1  dholland 		       CTL_CREATE, CTL_EOL);
   1138        1.1  dholland 
   1139        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1140        1.1  dholland 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1141        1.1  dholland 		       CTLTYPE_INT, "memused",
   1142        1.1  dholland 		       SYSCTL_DESCR("current dirhash memory usage"),
   1143        1.3  dholland 		       NULL, 0, &ulfs_dirhashmem, 0,
   1144        1.1  dholland 		       CTL_CREATE, CTL_EOL);
   1145        1.1  dholland 
   1146        1.3  dholland 	sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &cnode,
   1147        1.1  dholland 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1148        1.1  dholland 		       CTLTYPE_INT, "docheck",
   1149        1.1  dholland 		       SYSCTL_DESCR("enable extra sanity checks"),
   1150        1.3  dholland 		       NULL, 0, &ulfs_dirhashcheck, 0,
   1151        1.1  dholland 		       CTL_CREATE, CTL_EOL);
   1152        1.1  dholland }
   1153        1.1  dholland 
   1154        1.1  dholland void
   1155        1.3  dholland ulfsdirhash_init(void)
   1156        1.1  dholland {
   1157        1.1  dholland 
   1158        1.3  dholland 	mutex_init(&ulfsdirhash_lock, MUTEX_DEFAULT, IPL_NONE);
   1159        1.3  dholland 	ulfsdirhashblk_cache = pool_cache_init(DH_NBLKOFF * sizeof(daddr_t), 0,
   1160        1.1  dholland 	    0, 0, "dirhashblk", NULL, IPL_NONE, NULL, NULL, NULL);
   1161        1.3  dholland 	ulfsdirhash_cache = pool_cache_init(sizeof(struct dirhash), 0,
   1162        1.1  dholland 	    0, 0, "dirhash", NULL, IPL_NONE, NULL, NULL, NULL);
   1163        1.3  dholland 	TAILQ_INIT(&ulfsdirhash_list);
   1164        1.3  dholland 	ulfsdirhash_sysctl_init();
   1165        1.1  dholland }
   1166        1.1  dholland 
   1167        1.1  dholland void
   1168        1.3  dholland ulfsdirhash_done(void)
   1169        1.1  dholland {
   1170        1.1  dholland 
   1171        1.3  dholland 	KASSERT(TAILQ_EMPTY(&ulfsdirhash_list));
   1172        1.3  dholland 	pool_cache_destroy(ulfsdirhashblk_cache);
   1173        1.3  dholland 	pool_cache_destroy(ulfsdirhash_cache);
   1174        1.3  dholland 	mutex_destroy(&ulfsdirhash_lock);
   1175        1.3  dholland 	sysctl_teardown(&ulfsdirhash_sysctl_log);
   1176        1.1  dholland }
   1177