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