ulfs_dirhash.h revision 1.8 1 1.8 dholland /* $NetBSD: ulfs_dirhash.h,v 1.8 2015/09/21 01:24:23 dholland Exp $ */
2 1.1 dholland /* from NetBSD: dirhash.h,v 1.6 2008/06/04 11:33:19 ad Exp */
3 1.1 dholland
4 1.1 dholland /*
5 1.1 dholland * Copyright (c) 2001 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/dirhash.h,v 1.2.2.2 2004/12/08 11:54:13 dwmalone Exp $
29 1.1 dholland */
30 1.1 dholland
31 1.2 dholland #ifndef _UFS_LFS_ULFS_DIRHASH_H_
32 1.2 dholland #define _UFS_LFS_ULFS_DIRHASH_H_
33 1.1 dholland
34 1.1 dholland /*
35 1.1 dholland * For fast operations on large directories, we maintain a hash
36 1.1 dholland * that maps the file name to the offset of the directory entry within
37 1.1 dholland * the directory file.
38 1.1 dholland *
39 1.1 dholland * The hashing uses a dumb spillover to the next free slot on
40 1.1 dholland * collisions, so we must keep the utilisation low to avoid
41 1.1 dholland * long linear searches. Deleted entries that are not the last
42 1.1 dholland * in a chain must be marked DIRHASH_DEL.
43 1.1 dholland *
44 1.1 dholland * We also maintain information about free space in each block
45 1.1 dholland * to speed up creations.
46 1.1 dholland */
47 1.1 dholland #define DIRHASH_EMPTY (-1) /* entry unused */
48 1.1 dholland #define DIRHASH_DEL (-2) /* deleted entry; may be part of chain */
49 1.1 dholland
50 1.1 dholland #define DIRALIGN 4
51 1.7 dholland #define DH_NFSTATS (LFS_MAXDIRENTRYSIZE / DIRALIGN)
52 1.7 dholland /* max DIRALIGN words in a directory entry */
53 1.1 dholland
54 1.1 dholland /*
55 1.1 dholland * Dirhash uses a score mechanism to achieve a hybrid between a
56 1.1 dholland * least-recently-used and a least-often-used algorithm for entry
57 1.1 dholland * recycling. The score is incremented when a directory is used, and
58 1.1 dholland * decremented when the directory is a candidate for recycling. When
59 1.1 dholland * the score reaches zero, the hash is recycled. Hashes are linked
60 1.1 dholland * together on a TAILQ list, and hashes with higher scores filter
61 1.1 dholland * towards the tail (most recently used) end of the list.
62 1.1 dholland *
63 1.1 dholland * New hash entries are given an inital score of DH_SCOREINIT and are
64 1.1 dholland * placed at the most-recently-used end of the list. This helps a lot
65 1.1 dholland * in the worst-case case scenario where every directory access is
66 1.1 dholland * to a directory that is not hashed (i.e. the working set of hash
67 1.1 dholland * candidates is much larger than the configured memry limit). In this
68 1.1 dholland * case it limits the number of hash builds to 1/DH_SCOREINIT of the
69 1.1 dholland * number of accesses.
70 1.1 dholland */
71 1.1 dholland #define DH_SCOREINIT 8 /* initial dh_score when dirhash built */
72 1.1 dholland #define DH_SCOREMAX 64 /* max dh_score value */
73 1.1 dholland
74 1.1 dholland /*
75 1.1 dholland * The main hash table has 2 levels. It is an array of pointers to
76 1.1 dholland * blocks of DH_NBLKOFF offsets.
77 1.1 dholland */
78 1.1 dholland #define DH_BLKOFFSHIFT 8
79 1.1 dholland #define DH_NBLKOFF (1 << DH_BLKOFFSHIFT)
80 1.1 dholland #define DH_BLKOFFMASK (DH_NBLKOFF - 1)
81 1.1 dholland
82 1.1 dholland #define DH_ENTRY(dh, slot) \
83 1.1 dholland ((dh)->dh_hash[(slot) >> DH_BLKOFFSHIFT][(slot) & DH_BLKOFFMASK])
84 1.1 dholland
85 1.1 dholland struct dirhash {
86 1.1 dholland kmutex_t dh_lock; /* protects all fields except dh_list */
87 1.1 dholland
88 1.1 dholland doff_t **dh_hash; /* the hash array (2-level) */
89 1.1 dholland size_t dh_hashsz;
90 1.1 dholland int dh_narrays; /* number of entries in dh_hash */
91 1.1 dholland int dh_hlen; /* total slots in the 2-level hash array */
92 1.1 dholland int dh_hused; /* entries in use */
93 1.1 dholland
94 1.1 dholland u_int8_t *dh_blkfree; /* free DIRALIGN words in each dir block */
95 1.1 dholland size_t dh_blkfreesz;
96 1.1 dholland int dh_nblk; /* size of dh_blkfree array */
97 1.1 dholland int dh_dirblks; /* number of DIRBLKSIZ blocks in dir */
98 1.1 dholland int dh_firstfree[DH_NFSTATS + 1]; /* first blk with N words free */
99 1.1 dholland
100 1.1 dholland int dh_seqopt; /* sequential access optimisation enabled */
101 1.1 dholland doff_t dh_seqoff; /* sequential access optimisation offset */
102 1.1 dholland
103 1.1 dholland int dh_score; /* access count for this dirhash */
104 1.1 dholland
105 1.2 dholland int dh_onlist; /* true if on the ulfsdirhash_list chain */
106 1.1 dholland
107 1.2 dholland /* Protected by ulfsdirhash_lock. */
108 1.1 dholland TAILQ_ENTRY(dirhash) dh_list; /* chain of all dirhashes */
109 1.1 dholland };
110 1.1 dholland
111 1.1 dholland
112 1.1 dholland /*
113 1.1 dholland * Dirhash functions.
114 1.1 dholland */
115 1.2 dholland int ulfsdirhash_build(struct inode *);
116 1.2 dholland doff_t ulfsdirhash_findfree(struct inode *, int, int *);
117 1.2 dholland doff_t ulfsdirhash_enduseful(struct inode *);
118 1.2 dholland int ulfsdirhash_lookup(struct inode *, const char *, int, doff_t *,
119 1.1 dholland struct buf **, doff_t *);
120 1.2 dholland void ulfsdirhash_newblk(struct inode *, doff_t);
121 1.8 dholland void ulfsdirhash_add(struct inode *, LFS_DIRHEADER *, doff_t);
122 1.8 dholland void ulfsdirhash_remove(struct inode *, LFS_DIRHEADER *, doff_t);
123 1.8 dholland void ulfsdirhash_move(struct inode *, LFS_DIRHEADER *, doff_t, doff_t);
124 1.2 dholland void ulfsdirhash_dirtrunc(struct inode *, doff_t);
125 1.2 dholland void ulfsdirhash_free(struct inode *);
126 1.2 dholland void ulfsdirhash_checkblock(struct inode *, char *, doff_t);
127 1.2 dholland void ulfsdirhash_init(void);
128 1.2 dholland void ulfsdirhash_done(void);
129 1.1 dholland
130 1.2 dholland #endif /* !_UFS_LFS_ULFS_DIRHASH_H_ */
131