1 /* $NetBSD: dirhash.h,v 1.7 2025/10/31 10:35:52 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef _SYS_DIRHASH_H_ 29 #define _SYS_DIRHASH_H_ 30 31 #include <sys/queue.h> 32 #include <sys/dirent.h> 33 34 #define DIRHASH_HASHBITS 5 35 #define DIRHASH_HASHSIZE (1 << DIRHASH_HASHBITS) 36 #define DIRHASH_HASHMASK (DIRHASH_HASHSIZE - 1) 37 38 /* dirent's d_namlen is to avoid useless costly fid->dirent translations */ 39 struct dirhash_entry { 40 uint32_t hashvalue; 41 uint64_t offset; 42 uint32_t d_namlen; 43 uint32_t entry_size; 44 LIST_ENTRY(dirhash_entry) next; 45 }; 46 47 struct dirhash { 48 uint32_t flags; 49 uint32_t size; /* in bytes */ 50 uint32_t refcnt; 51 uint32_t num_files; 52 LIST_HEAD(, dirhash_entry) entries[DIRHASH_HASHSIZE]; 53 LIST_HEAD(, dirhash_entry) free_entries; 54 TAILQ_ENTRY(dirhash) next; 55 }; 56 57 #define DIRH_PURGED 0x0001 /* dirhash has been purged */ 58 #define DIRH_COMPLETE 0x0002 /* dirhash is complete */ 59 #define DIRH_BROKEN 0x0004 /* dirhash is broken on readin */ 60 #define DIRH_COMPACTABLE 0x0008 /* free space can be compacted */ 61 #define DIRH_FLAGBITS "\10\1PURGED\2COMPLETE\3BROKEN\4COMPACTABLE" 62 63 void dirhash_init(void); 64 /* void dirhash_finish(void); */ 65 66 void dirhash_purge(struct dirhash **); 67 void dirhash_purge_entries(struct dirhash *); 68 void dirhash_get(struct dirhash **); 69 void dirhash_put(struct dirhash *); 70 void dirhash_enter(struct dirhash *, struct dirent *, uint64_t, 71 uint32_t, int); 72 void dirhash_enter_freed(struct dirhash *, uint64_t, uint32_t); 73 void dirhash_remove(struct dirhash *, struct dirent *dirent, 74 uint64_t, uint32_t); 75 int dirhash_lookup(struct dirhash *, const char *, int, 76 struct dirhash_entry **); 77 int dirhash_lookup_freed(struct dirhash *, uint32_t, 78 struct dirhash_entry **); 79 bool dirhash_dir_isempty(struct dirhash *dirh); 80 81 #endif /* _SYS_DIRHASH_H_ */ 82