Home | History | Annotate | Line # | Download | only in kern
      1  1.16  riastrad /*	$NetBSD: vfs_dirhash.c,v 1.16 2024/12/07 02:11:42 riastradh 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.16  riastrad  *
      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.16  riastrad  *
     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.16  riastrad  *
     27   1.1   reinoud  */
     28   1.1   reinoud 
     29   1.1   reinoud #include <sys/cdefs.h>
     30  1.16  riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_dirhash.c,v 1.16 2024/12/07 02:11:42 riastradh Exp $");
     31   1.1   reinoud 
     32   1.1   reinoud /* CLEAN UP! */
     33   1.1   reinoud #include <sys/param.h>
     34  1.16  riastrad #include <sys/types.h>
     35  1.16  riastrad 
     36   1.1   reinoud #include <sys/buf.h>
     37   1.1   reinoud #include <sys/dirent.h>
     38  1.16  riastrad #include <sys/dirhash.h>
     39   1.5   reinoud #include <sys/hash.h>
     40  1.16  riastrad #include <sys/kernel.h>
     41   1.5   reinoud #include <sys/mutex.h>
     42   1.5   reinoud #include <sys/pool.h>
     43   1.5   reinoud #include <sys/queue.h>
     44  1.16  riastrad #include <sys/sysctl.h>
     45   1.5   reinoud #include <sys/vnode.h>
     46   1.1   reinoud 
     47   1.5   reinoud #if 1
     48  1.16  riastrad #	define DPRINTF(a) __nothing
     49   1.5   reinoud #else
     50  1.16  riastrad #	define DPRINTF(a) printf a
     51   1.5   reinoud #endif
     52   1.5   reinoud 
     53   1.8   reinoud /*
     54   1.8   reinoud  * The locking protocol of the dirhash structures is fairly simple:
     55   1.8   reinoud  *
     56   1.8   reinoud  * The global dirhash_queue is protected by the dirhashmutex. This lock is
     57   1.8   reinoud  * internal only and is FS/mountpoint/vnode independent. On exit of the
     58  1.14    andvar  * exported functions this mutex is not held.
     59   1.8   reinoud  *
     60  1.15   reinoud  * The dirhash structure is considered part of the vnode/inode structure and
     61  1.15   reinoud  * will thus use the lock that protects that vnode/inode.
     62   1.8   reinoud  *
     63   1.8   reinoud  * The dirhash entries are considered part of the dirhash structure and thus
     64   1.8   reinoud  * are on the same lock.
     65   1.8   reinoud  */
     66   1.1   reinoud 
     67   1.1   reinoud static struct sysctllog *sysctl_log;
     68   1.5   reinoud static struct pool dirhash_pool;
     69   1.5   reinoud static struct pool dirhash_entry_pool;
     70   1.1   reinoud 
     71   1.3   reinoud static kmutex_t dirhashmutex;
     72   1.3   reinoud static uint32_t maxdirhashsize = DIRHASH_SIZE;
     73   1.3   reinoud static uint32_t dirhashsize    = 0;
     74   1.3   reinoud static TAILQ_HEAD(_dirhash, dirhash) dirhash_queue;
     75   1.1   reinoud 
     76   1.1   reinoud 
     77   1.1   reinoud void
     78   1.1   reinoud dirhash_init(void)
     79   1.1   reinoud {
     80   1.2   reinoud 	const struct sysctlnode *rnode, *cnode;
     81   1.5   reinoud 	size_t sz;
     82   1.1   reinoud 	uint32_t max_entries;
     83   1.1   reinoud 
     84   1.1   reinoud 	/* initialise dirhash queue */
     85   1.1   reinoud 	TAILQ_INIT(&dirhash_queue);
     86   1.1   reinoud 
     87   1.1   reinoud 	/* init dirhash pools */
     88   1.5   reinoud 	sz = sizeof(struct dirhash);
     89   1.5   reinoud 	pool_init(&dirhash_pool, sz, 0, 0, 0,
     90  1.16  riastrad 	    "dirhpl", NULL, IPL_NONE);
     91   1.5   reinoud 
     92   1.5   reinoud 	sz = sizeof(struct dirhash_entry);
     93   1.5   reinoud 	pool_init(&dirhash_entry_pool, sz, 0, 0, 0,
     94  1.16  riastrad 	    "dirhepl", NULL, IPL_NONE);
     95   1.1   reinoud 
     96   1.1   reinoud 	mutex_init(&dirhashmutex, MUTEX_DEFAULT, IPL_NONE);
     97   1.5   reinoud 	max_entries = maxdirhashsize / sz;
     98   1.1   reinoud 	pool_sethiwat(&dirhash_entry_pool, max_entries);
     99   1.1   reinoud 	dirhashsize = 0;
    100   1.1   reinoud 
    101   1.1   reinoud 	/* create sysctl knobs and dials */
    102   1.1   reinoud 	sysctl_log = NULL;
    103   1.2   reinoud 	sysctl_createv(&sysctl_log, 0, NULL, &rnode,
    104  1.16  riastrad 	    CTLFLAG_PERMANENT,
    105  1.16  riastrad 	    CTLTYPE_NODE, "dirhash", NULL,
    106  1.16  riastrad 	    NULL, 0, NULL, 0,
    107  1.16  riastrad 	    CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
    108   1.2   reinoud 	sysctl_createv(&sysctl_log, 0, &rnode, &cnode,
    109  1.16  riastrad 	    CTLFLAG_PERMANENT,
    110  1.16  riastrad 	    CTLTYPE_INT, "memused",
    111  1.16  riastrad 	    SYSCTL_DESCR("current dirhash memory usage"),
    112  1.16  riastrad 	    NULL, 0, &dirhashsize, 0,
    113  1.16  riastrad 	    CTL_CREATE, CTL_EOL);
    114   1.2   reinoud 	sysctl_createv(&sysctl_log, 0, &rnode, &cnode,
    115  1.16  riastrad 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    116  1.16  riastrad 	    CTLTYPE_INT, "maxmem",
    117  1.16  riastrad 	    SYSCTL_DESCR("maximum dirhash memory usage"),
    118  1.16  riastrad 	    NULL, 0, &maxdirhashsize, 0,
    119  1.16  riastrad 	    CTL_CREATE, CTL_EOL);
    120   1.1   reinoud }
    121   1.1   reinoud 
    122   1.1   reinoud 
    123   1.1   reinoud #if 0
    124   1.1   reinoud void
    125   1.1   reinoud dirhash_finish(void)
    126   1.1   reinoud {
    127   1.1   reinoud 	pool_destroy(&dirhash_pool);
    128   1.1   reinoud 	pool_destroy(&dirhash_entry_pool);
    129   1.1   reinoud 
    130   1.1   reinoud 	mutex_destroy(&dirhashmutex);
    131   1.1   reinoud 
    132   1.1   reinoud 	/* sysctl_teardown(&sysctl_log); */
    133   1.1   reinoud }
    134   1.1   reinoud #endif
    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 void
    141   1.1   reinoud dirhash_purge_entries(struct dirhash *dirh)
    142   1.1   reinoud {
    143   1.1   reinoud 	struct dirhash_entry *dirh_e;
    144   1.1   reinoud 	uint32_t hashline;
    145   1.1   reinoud 
    146   1.1   reinoud 	if (dirh == NULL)
    147   1.1   reinoud 		return;
    148   1.1   reinoud 
    149   1.1   reinoud 	if (dirh->size == 0)
    150   1.1   reinoud 		return;
    151   1.1   reinoud 
    152   1.1   reinoud 	for (hashline = 0; hashline < DIRHASH_HASHSIZE; hashline++) {
    153  1.16  riastrad 		while ((dirh_e = LIST_FIRST(&dirh->entries[hashline]))
    154  1.16  riastrad 		    != NULL) {
    155   1.1   reinoud 			LIST_REMOVE(dirh_e, next);
    156   1.1   reinoud 			pool_put(&dirhash_entry_pool, dirh_e);
    157   1.1   reinoud 		}
    158   1.1   reinoud 	}
    159   1.1   reinoud 
    160  1.10     enami 	while ((dirh_e = LIST_FIRST(&dirh->free_entries)) != NULL) {
    161   1.1   reinoud 		LIST_REMOVE(dirh_e, next);
    162   1.1   reinoud 		pool_put(&dirhash_entry_pool, dirh_e);
    163   1.1   reinoud 	}
    164   1.1   reinoud 
    165   1.1   reinoud 	dirh->flags &= ~DIRH_COMPLETE;
    166   1.1   reinoud 	dirh->flags |=  DIRH_PURGED;
    167  1.11   reinoud 	dirh->num_files = 0;
    168   1.1   reinoud 
    169   1.1   reinoud 	dirhashsize -= dirh->size;
    170   1.1   reinoud 	dirh->size = 0;
    171   1.1   reinoud }
    172   1.1   reinoud 
    173   1.1   reinoud void
    174   1.1   reinoud dirhash_purge(struct dirhash **dirhp)
    175   1.1   reinoud {
    176   1.1   reinoud 	struct dirhash *dirh = *dirhp;
    177   1.1   reinoud 
    178   1.1   reinoud 	if (dirh == NULL)
    179   1.1   reinoud 		return;
    180   1.1   reinoud 
    181   1.7   reinoud 	/* purge its entries */
    182   1.7   reinoud 	dirhash_purge_entries(dirh);
    183   1.7   reinoud 
    184   1.7   reinoud 	/* recycle */
    185   1.1   reinoud 	mutex_enter(&dirhashmutex);
    186   1.1   reinoud 	TAILQ_REMOVE(&dirhash_queue, dirh, next);
    187   1.5   reinoud 	mutex_exit(&dirhashmutex);
    188   1.5   reinoud 
    189   1.1   reinoud 	pool_put(&dirhash_pool, dirh);
    190   1.1   reinoud 	*dirhp = NULL;
    191   1.1   reinoud }
    192   1.1   reinoud 
    193   1.1   reinoud void
    194   1.1   reinoud dirhash_get(struct dirhash **dirhp)
    195   1.1   reinoud {
    196   1.1   reinoud 	struct dirhash *dirh;
    197   1.1   reinoud 	uint32_t hashline;
    198   1.1   reinoud 
    199   1.5   reinoud 	/* if no dirhash was given, allocate one */
    200   1.1   reinoud 	dirh = *dirhp;
    201   1.5   reinoud 	if (dirh == NULL) {
    202  1.13  christos 		dirh = pool_get(&dirhash_pool, PR_WAITOK | PR_ZERO);
    203   1.5   reinoud 		for (hashline = 0; hashline < DIRHASH_HASHSIZE; hashline++) {
    204   1.1   reinoud 			LIST_INIT(&dirh->entries[hashline]);
    205   1.5   reinoud 		}
    206   1.5   reinoud 	}
    207   1.5   reinoud 
    208   1.5   reinoud 	/* implement LRU on the dirhash queue */
    209   1.5   reinoud 	mutex_enter(&dirhashmutex);
    210   1.5   reinoud 	if (*dirhp) {
    211   1.5   reinoud 		/* remove from queue to be requeued */
    212   1.1   reinoud 		TAILQ_REMOVE(&dirhash_queue, dirh, next);
    213   1.1   reinoud 	}
    214   1.1   reinoud 	dirh->refcnt++;
    215   1.1   reinoud 	TAILQ_INSERT_HEAD(&dirhash_queue, dirh, next);
    216   1.5   reinoud 	mutex_exit(&dirhashmutex);
    217   1.1   reinoud 
    218   1.5   reinoud 	*dirhp = dirh;
    219   1.1   reinoud }
    220   1.1   reinoud 
    221   1.1   reinoud void
    222   1.1   reinoud dirhash_put(struct dirhash *dirh)
    223   1.1   reinoud {
    224   1.1   reinoud 
    225   1.1   reinoud 	mutex_enter(&dirhashmutex);
    226   1.1   reinoud 	dirh->refcnt--;
    227   1.1   reinoud 	mutex_exit(&dirhashmutex);
    228   1.1   reinoud }
    229   1.1   reinoud 
    230   1.1   reinoud void
    231   1.1   reinoud dirhash_enter(struct dirhash *dirh,
    232  1.16  riastrad     struct dirent *dirent, uint64_t offset, uint32_t entry_size, int new_p)
    233   1.1   reinoud {
    234   1.1   reinoud 	struct dirhash *del_dirh, *prev_dirh;
    235   1.1   reinoud 	struct dirhash_entry *dirh_e;
    236   1.1   reinoud 	uint32_t hashvalue, hashline;
    237   1.1   reinoud 	int entrysize;
    238   1.1   reinoud 
    239   1.1   reinoud 	/* make sure we have a dirhash to work on */
    240   1.1   reinoud 	KASSERT(dirh);
    241   1.1   reinoud 	KASSERT(dirh->refcnt > 0);
    242   1.1   reinoud 
    243   1.1   reinoud 	/* are we trying to re-enter an entry? */
    244  1.12      matt 	if (!new_p && (dirh->flags & DIRH_COMPLETE))
    245   1.1   reinoud 		return;
    246   1.1   reinoud 
    247   1.1   reinoud 	/* calculate our hash */
    248  1.16  riastrad 	hashvalue = hash32_strn(dirent->d_name, dirent->d_namlen,
    249  1.16  riastrad 	    HASH32_STR_INIT);
    250   1.1   reinoud 	hashline  = hashvalue & DIRHASH_HASHMASK;
    251   1.1   reinoud 
    252   1.1   reinoud 	/* lookup and insert entry if not there yet */
    253   1.1   reinoud 	LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
    254   1.1   reinoud 		/* check for hash collision */
    255   1.1   reinoud 		if (dirh_e->hashvalue != hashvalue)
    256   1.1   reinoud 			continue;
    257   1.1   reinoud 		if (dirh_e->offset != offset)
    258   1.1   reinoud 			continue;
    259   1.1   reinoud 		/* got it already */
    260   1.1   reinoud 		KASSERT(dirh_e->d_namlen == dirent->d_namlen);
    261   1.1   reinoud 		KASSERT(dirh_e->entry_size == entry_size);
    262   1.1   reinoud 		return;
    263   1.1   reinoud 	}
    264   1.1   reinoud 
    265   1.1   reinoud 	DPRINTF(("dirhash enter %"PRIu64", %d, %d for `%*.*s`\n",
    266   1.1   reinoud 		offset, entry_size, dirent->d_namlen,
    267   1.1   reinoud 		dirent->d_namlen, dirent->d_namlen, dirent->d_name));
    268   1.1   reinoud 
    269   1.1   reinoud 	/* check if entry is in free space list */
    270   1.1   reinoud 	LIST_FOREACH(dirh_e, &dirh->free_entries, next) {
    271   1.1   reinoud 		if (dirh_e->offset == offset) {
    272   1.1   reinoud 			DPRINTF(("\tremoving free entry\n"));
    273   1.1   reinoud 			LIST_REMOVE(dirh_e, next);
    274   1.9   reinoud 			pool_put(&dirhash_entry_pool, dirh_e);
    275   1.1   reinoud 			break;
    276   1.1   reinoud 		}
    277   1.1   reinoud 	}
    278   1.1   reinoud 
    279   1.1   reinoud 	/* ensure we are not passing the dirhash limit */
    280   1.1   reinoud 	entrysize = sizeof(struct dirhash_entry);
    281   1.1   reinoud 	if (dirhashsize + entrysize > maxdirhashsize) {
    282   1.7   reinoud 		/* we walk the dirhash_queue, so need to lock it */
    283   1.7   reinoud 		mutex_enter(&dirhashmutex);
    284   1.1   reinoud 		del_dirh = TAILQ_LAST(&dirhash_queue, _dirhash);
    285   1.1   reinoud 		KASSERT(del_dirh);
    286   1.1   reinoud 		while (dirhashsize + entrysize > maxdirhashsize) {
    287   1.1   reinoud 			/* no use trying to delete myself */
    288   1.1   reinoud 			if (del_dirh == dirh)
    289   1.1   reinoud 				break;
    290   1.1   reinoud 			prev_dirh = TAILQ_PREV(del_dirh, _dirhash, next);
    291   1.1   reinoud 			if (del_dirh->refcnt == 0)
    292   1.1   reinoud 				dirhash_purge_entries(del_dirh);
    293   1.1   reinoud 			del_dirh = prev_dirh;
    294   1.1   reinoud 		}
    295   1.7   reinoud 		mutex_exit(&dirhashmutex);
    296   1.1   reinoud 	}
    297   1.1   reinoud 
    298   1.1   reinoud 	/* add to the hashline */
    299  1.13  christos 	dirh_e = pool_get(&dirhash_entry_pool, PR_WAITOK | PR_ZERO);
    300   1.1   reinoud 
    301   1.1   reinoud 	dirh_e->hashvalue = hashvalue;
    302   1.1   reinoud 	dirh_e->offset    = offset;
    303   1.1   reinoud 	dirh_e->d_namlen  = dirent->d_namlen;
    304   1.1   reinoud 	dirh_e->entry_size  = entry_size;
    305   1.1   reinoud 
    306   1.1   reinoud 	dirh->size  += sizeof(struct dirhash_entry);
    307  1.11   reinoud 	dirh->num_files++;
    308   1.1   reinoud 	dirhashsize += sizeof(struct dirhash_entry);
    309   1.1   reinoud 	LIST_INSERT_HEAD(&dirh->entries[hashline], dirh_e, next);
    310   1.1   reinoud }
    311   1.1   reinoud 
    312   1.1   reinoud void
    313  1.16  riastrad dirhash_enter_freed(struct dirhash *dirh, uint64_t offset, uint32_t entry_size)
    314   1.1   reinoud {
    315   1.1   reinoud 	struct dirhash_entry *dirh_e;
    316   1.1   reinoud 
    317   1.1   reinoud 	/* make sure we have a dirhash to work on */
    318   1.1   reinoud 	KASSERT(dirh);
    319   1.1   reinoud 	KASSERT(dirh->refcnt > 0);
    320   1.1   reinoud 
    321   1.1   reinoud 	/* check for double entry of free space */
    322   1.5   reinoud 	LIST_FOREACH(dirh_e, &dirh->free_entries, next) {
    323   1.1   reinoud 		KASSERT(dirh_e->offset != offset);
    324   1.5   reinoud 	}
    325   1.1   reinoud 
    326   1.1   reinoud 	DPRINTF(("dirhash enter FREED %"PRIu64", %d\n",
    327   1.1   reinoud 		offset, entry_size));
    328  1.13  christos 	dirh_e = pool_get(&dirhash_entry_pool, PR_WAITOK | PR_ZERO);
    329   1.1   reinoud 
    330   1.1   reinoud 	dirh_e->hashvalue = 0;		/* not relevant */
    331   1.1   reinoud 	dirh_e->offset    = offset;
    332   1.1   reinoud 	dirh_e->d_namlen  = 0;		/* not relevant */
    333   1.1   reinoud 	dirh_e->entry_size  = entry_size;
    334   1.1   reinoud 
    335   1.1   reinoud 	/* XXX it might be preferable to append them at the tail */
    336   1.1   reinoud 	LIST_INSERT_HEAD(&dirh->free_entries, dirh_e, next);
    337   1.1   reinoud 	dirh->size  += sizeof(struct dirhash_entry);
    338   1.1   reinoud 	dirhashsize += sizeof(struct dirhash_entry);
    339   1.1   reinoud }
    340   1.1   reinoud 
    341   1.1   reinoud void
    342   1.1   reinoud dirhash_remove(struct dirhash *dirh, struct dirent *dirent,
    343  1.16  riastrad     uint64_t offset, uint32_t entry_size)
    344   1.1   reinoud {
    345   1.1   reinoud 	struct dirhash_entry *dirh_e;
    346   1.1   reinoud 	uint32_t hashvalue, hashline;
    347   1.1   reinoud 
    348   1.1   reinoud 	DPRINTF(("dirhash remove %"PRIu64", %d for `%*.*s`\n",
    349  1.16  riastrad 		offset, entry_size,
    350   1.1   reinoud 		dirent->d_namlen, dirent->d_namlen, dirent->d_name));
    351   1.1   reinoud 
    352   1.1   reinoud 	/* make sure we have a dirhash to work on */
    353   1.1   reinoud 	KASSERT(dirh);
    354   1.1   reinoud 	KASSERT(dirh->refcnt > 0);
    355   1.1   reinoud 
    356   1.1   reinoud 	/* calculate our hash */
    357  1.16  riastrad 	hashvalue = hash32_strn(dirent->d_name, dirent->d_namlen,
    358  1.16  riastrad 	    HASH32_STR_INIT);
    359   1.1   reinoud 	hashline  = hashvalue & DIRHASH_HASHMASK;
    360   1.1   reinoud 
    361   1.1   reinoud 	/* lookup entry */
    362   1.1   reinoud 	LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
    363   1.1   reinoud 		/* check for hash collision */
    364   1.1   reinoud 		if (dirh_e->hashvalue != hashvalue)
    365   1.1   reinoud 			continue;
    366   1.1   reinoud 		if (dirh_e->offset != offset)
    367   1.1   reinoud 			continue;
    368   1.1   reinoud 
    369   1.1   reinoud 		/* got it! */
    370   1.1   reinoud 		KASSERT(dirh_e->d_namlen == dirent->d_namlen);
    371   1.1   reinoud 		KASSERT(dirh_e->entry_size == entry_size);
    372   1.1   reinoud 		LIST_REMOVE(dirh_e, next);
    373   1.5   reinoud 		dirh->size -= sizeof(struct dirhash_entry);
    374  1.11   reinoud 		KASSERT(dirh->num_files > 0);
    375  1.11   reinoud 		dirh->num_files--;
    376   1.1   reinoud 		dirhashsize -= sizeof(struct dirhash_entry);
    377   1.1   reinoud 
    378   1.1   reinoud 		dirhash_enter_freed(dirh, offset, entry_size);
    379   1.1   reinoud 		return;
    380   1.1   reinoud 	}
    381   1.1   reinoud 
    382   1.1   reinoud 	/* not found! */
    383   1.1   reinoud 	panic("dirhash_remove couldn't find entry in hash table\n");
    384   1.1   reinoud }
    385   1.1   reinoud 
    386   1.5   reinoud /*
    387   1.5   reinoud  * BUGALERT: don't use result longer than needed, never past the node lock.
    388   1.5   reinoud  * Call with NULL *result initially and it will return nonzero if again.
    389   1.5   reinoud  */
    390   1.1   reinoud int
    391   1.1   reinoud dirhash_lookup(struct dirhash *dirh, const char *d_name, int d_namlen,
    392  1.16  riastrad     struct dirhash_entry **result)
    393   1.1   reinoud {
    394   1.1   reinoud 	struct dirhash_entry *dirh_e;
    395   1.1   reinoud 	uint32_t hashvalue, hashline;
    396   1.1   reinoud 
    397   1.1   reinoud 	/* make sure we have a dirhash to work on */
    398   1.1   reinoud 	KASSERT(dirh);
    399   1.1   reinoud 	KASSERT(dirh->refcnt > 0);
    400   1.1   reinoud 
    401   1.1   reinoud 	/* start where we were */
    402   1.1   reinoud 	if (*result) {
    403   1.1   reinoud 		dirh_e = *result;
    404   1.1   reinoud 
    405   1.1   reinoud 		/* retrieve information to avoid recalculation and advance */
    406   1.1   reinoud 		hashvalue = dirh_e->hashvalue;
    407   1.1   reinoud 		dirh_e = LIST_NEXT(*result, next);
    408   1.1   reinoud 	} else {
    409   1.1   reinoud 		/* calculate our hash and lookup all entries in hashline */
    410   1.5   reinoud 		hashvalue = hash32_strn(d_name, d_namlen, HASH32_STR_INIT);
    411   1.1   reinoud 		hashline  = hashvalue & DIRHASH_HASHMASK;
    412   1.1   reinoud 		dirh_e = LIST_FIRST(&dirh->entries[hashline]);
    413   1.1   reinoud 	}
    414   1.1   reinoud 
    415   1.1   reinoud 	for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
    416   1.1   reinoud 		/* check for hash collision */
    417   1.1   reinoud 		if (dirh_e->hashvalue != hashvalue)
    418   1.1   reinoud 			continue;
    419   1.1   reinoud 		if (dirh_e->d_namlen != d_namlen)
    420   1.1   reinoud 			continue;
    421   1.1   reinoud 		/* might have an entry in the cache */
    422   1.1   reinoud 		*result = dirh_e;
    423   1.1   reinoud 		return 1;
    424   1.1   reinoud 	}
    425   1.1   reinoud 
    426   1.1   reinoud 	*result = NULL;
    427   1.1   reinoud 	return 0;
    428   1.1   reinoud }
    429   1.1   reinoud 
    430   1.5   reinoud /*
    431   1.5   reinoud  * BUGALERT: don't use result longer than needed, never past the node lock.
    432   1.5   reinoud  * Call with NULL *result initially and it will return nonzero if again.
    433   1.5   reinoud  */
    434   1.1   reinoud int
    435   1.1   reinoud dirhash_lookup_freed(struct dirhash *dirh, uint32_t min_entrysize,
    436  1.16  riastrad     struct dirhash_entry **result)
    437   1.1   reinoud {
    438   1.1   reinoud 	struct dirhash_entry *dirh_e;
    439   1.1   reinoud 
    440   1.1   reinoud 	/* make sure we have a dirhash to work on */
    441   1.1   reinoud 	KASSERT(dirh);
    442   1.1   reinoud 	KASSERT(dirh->refcnt > 0);
    443   1.1   reinoud 
    444   1.1   reinoud 	/* start where we were */
    445   1.1   reinoud 	if (*result) {
    446   1.1   reinoud 		dirh_e = LIST_NEXT(*result, next);
    447   1.1   reinoud 	} else {
    448   1.1   reinoud 		/* lookup all entries that match */
    449   1.1   reinoud 		dirh_e = LIST_FIRST(&dirh->free_entries);
    450   1.1   reinoud 	}
    451   1.1   reinoud 
    452   1.1   reinoud 	for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
    453   1.1   reinoud 		/* check for minimum size */
    454   1.1   reinoud 		if (dirh_e->entry_size < min_entrysize)
    455   1.1   reinoud 			continue;
    456   1.1   reinoud 		/* might be a candidate */
    457   1.1   reinoud 		*result = dirh_e;
    458   1.1   reinoud 		return 1;
    459   1.1   reinoud 	}
    460   1.1   reinoud 
    461   1.1   reinoud 	*result = NULL;
    462   1.1   reinoud 	return 0;
    463   1.1   reinoud }
    464  1.11   reinoud 
    465  1.11   reinoud bool
    466  1.11   reinoud dirhash_dir_isempty(struct dirhash *dirh)
    467  1.11   reinoud {
    468  1.11   reinoud #ifdef DEBUG
    469  1.11   reinoud 	struct dirhash_entry *dirh_e;
    470  1.11   reinoud 	int hashline, num;
    471  1.11   reinoud 
    472  1.11   reinoud 	num = 0;
    473  1.11   reinoud 	for (hashline = 0; hashline < DIRHASH_HASHSIZE; hashline++) {
    474  1.11   reinoud 		LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
    475  1.11   reinoud 			num++;
    476  1.11   reinoud 		}
    477  1.11   reinoud 	}
    478  1.11   reinoud 
    479  1.11   reinoud 	if (dirh->num_files != num) {
    480  1.11   reinoud 		printf("dirhash_dir_isempy: dirhash_counter failed: "
    481  1.16  riastrad 		    "dirh->num_files = %d, counted %d\n",
    482  1.16  riastrad 		    dirh->num_files, num);
    483  1.11   reinoud 		assert(dirh->num_files == num);
    484  1.11   reinoud 	}
    485  1.11   reinoud #endif
    486  1.11   reinoud 	/* assert the directory hash info is valid */
    487  1.11   reinoud 	KASSERT(dirh->flags & DIRH_COMPLETE);
    488  1.11   reinoud 
    489  1.11   reinoud 	/* the directory is empty when only '..' lifes in it or is absent */
    490  1.11   reinoud 	return (dirh->num_files <= 1);
    491  1.11   reinoud }
    492