1/* $NetBSD: dirhash.h,v 1.6 2013/07/07 19:31:26 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#ifndef DIRHASH_SIZE
35#define DIRHASH_SIZE (1024*1024)
36#endif
37
38#define DIRHASH_HASHBITS 5
39#define DIRHASH_HASHSIZE (1 << DIRHASH_HASHBITS)
40#define DIRHASH_HASHMASK (DIRHASH_HASHSIZE - 1)
41
42/* dirent's d_namlen is to avoid useless costly fid->dirent translations */
43struct dirhash_entry {
44 uint32_t hashvalue;
45 uint64_t offset;
46 uint32_t d_namlen;
47 uint32_t entry_size;
48 LIST_ENTRY(dirhash_entry) next;
49};
50
51struct dirhash {
52 uint32_t flags;
53 uint32_t size; /* in bytes */
54 uint32_t refcnt;
55 uint32_t num_files;
56 LIST_HEAD(, dirhash_entry) entries[DIRHASH_HASHSIZE];
57 LIST_HEAD(, dirhash_entry) free_entries;
58 TAILQ_ENTRY(dirhash) next;
59};
60
61#define DIRH_PURGED 0x0001 /* dirhash has been purged */
62#define DIRH_COMPLETE 0x0002 /* dirhash is complete */
63#define DIRH_BROKEN 0x0004 /* dirhash is broken on readin */
64#define DIRH_COMPACTABLE 0x0008 /* free space can be compacted */
65#define DIRH_FLAGBITS "\10\1PURGED\2COMPLETE\3BROKEN\4COMPACTABLE"
66
67void dirhash_init(void);
68/* void dirhash_finish(void); */
69
70void dirhash_purge(struct dirhash **);
71void dirhash_purge_entries(struct dirhash *);
72void dirhash_get(struct dirhash **);
73void dirhash_put(struct dirhash *);
74void dirhash_enter(struct dirhash *, struct dirent *, uint64_t,
75 uint32_t, int);
76void dirhash_enter_freed(struct dirhash *, uint64_t, uint32_t);
77void dirhash_remove(struct dirhash *, struct dirent *dirent,
78 uint64_t, uint32_t);
79int dirhash_lookup(struct dirhash *, const char *, int,
80 struct dirhash_entry **);
81int dirhash_lookup_freed(struct dirhash *, uint32_t,
82 struct dirhash_entry **);
83bool dirhash_dir_isempty(struct dirhash *dirh);
84
85#endif /* _SYS_DIRHASH_H_ */
86