4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #ifndef _SYS_ZAP_LEAF_H
27 #define _SYS_ZAP_LEAF_H
29 #pragma ident "%Z%%M% %I% %E% SMI"
37 #define ZAP_LEAF_MAGIC 0x2AB1EAF
39 /* chunk size = 24 bytes */
40 #define ZAP_LEAF_CHUNKSIZE 24
43 * The amount of space available for chunks is:
44 * block size (1<<l->l_bs) - hash entry size (2) * number of hash
45 * entries - header space (2*chunksize)
47 #define ZAP_LEAF_NUMCHUNKS(l) \
48 (((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
49 ZAP_LEAF_CHUNKSIZE - 2)
52 * The amount of space within the chunk available for the array is:
53 * chunk size - space for type (1) - space for next pointer (2)
55 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
57 #define ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
58 (((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
61 * Low water mark: when there are only this many chunks free, start
62 * growing the ptrtbl. Ideally, this should be larger than a
63 * "reasonably-sized" entry. 20 chunks is more than enough for the
64 * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
65 * while still being only around 3% for 16k blocks.
67 #define ZAP_LEAF_LOW_WATER (20)
70 * The leaf hash table has block size / 2^5 (32) number of entries,
71 * which should be more than enough for the maximum number of entries,
72 * which is less than block size / CHUNKSIZE (24) / minimum number of
73 * chunks per entry (3).
75 #define ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
76 #define ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
79 * The chunks start immediately after the hash table. The end of the
80 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
83 #define ZAP_LEAF_CHUNK(l, idx) \
84 ((zap_leaf_chunk_t *) \
85 ((l)->l_phys->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
86 #define ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
88 typedef enum zap_chunk_type {
90 ZAP_CHUNK_ENTRY = 252,
91 ZAP_CHUNK_ARRAY = 251,
92 ZAP_CHUNK_TYPE_MAX = 250
97 * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
99 typedef struct zap_leaf_phys {
100 struct zap_leaf_header {
101 uint64_t lh_block_type; /* ZBT_LEAF */
103 uint64_t lh_prefix; /* hash prefix of this leaf */
104 uint32_t lh_magic; /* ZAP_LEAF_MAGIC */
105 uint16_t lh_nfree; /* number free chunks */
106 uint16_t lh_nentries; /* number of entries */
107 uint16_t lh_prefix_len; /* num bits used to id this */
109 /* above is accessable to zap, below is zap_leaf private */
111 uint16_t lh_freelist; /* chunk head of free list */
113 } l_hdr; /* 2 24-byte chunks */
116 * The header is followed by a hash table with
117 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries. The hash table is
118 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
119 * zap_leaf_chunk structures. These structures are accessed
120 * with the ZAP_LEAF_CHUNK() macro.
126 typedef union zap_leaf_chunk {
127 struct zap_leaf_entry {
128 uint8_t le_type; /* always ZAP_CHUNK_ENTRY */
129 uint8_t le_int_size; /* size of ints */
130 uint16_t le_next; /* next entry in hash chain */
131 uint16_t le_name_chunk; /* first chunk of the name */
132 uint16_t le_name_length; /* bytes in name, incl null */
133 uint16_t le_value_chunk; /* first chunk of the value */
134 uint16_t le_value_length; /* value length in ints */
135 uint32_t le_cd; /* collision differentiator */
136 uint64_t le_hash; /* hash value of the name */
138 struct zap_leaf_array {
139 uint8_t la_type; /* always ZAP_CHUNK_ARRAY */
140 uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
141 uint16_t la_next; /* next blk or CHAIN_END */
143 struct zap_leaf_free {
144 uint8_t lf_type; /* always ZAP_CHUNK_FREE */
145 uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
146 uint16_t lf_next; /* next in free list, or CHAIN_END */
150 typedef struct zap_leaf {
151 krwlock_t l_rwlock; /* only used on head of chain */
152 uint64_t l_blkid; /* 1<<ZAP_BLOCK_SHIFT byte block off */
153 int l_bs; /* block size shift */
155 zap_leaf_phys_t *l_phys;
159 typedef struct zap_entry_handle {
160 /* below is set by zap_leaf.c and is public to zap.c */
161 uint64_t zeh_num_integers;
164 uint8_t zeh_integer_size;
166 /* below is private to zap_leaf.c */
167 uint16_t zeh_fakechunk;
168 uint16_t *zeh_chunkp;
169 zap_leaf_t *zeh_leaf;
170 } zap_entry_handle_t;
173 * Return a handle to the named entry, or ENOENT if not found. The hash
174 * value must equal zap_hash(name).
176 extern int zap_leaf_lookup(zap_leaf_t *l,
177 const char *name, uint64_t h, zap_entry_handle_t *zeh);
180 * Return a handle to the entry with this hash+cd, or the entry with the
181 * next closest hash+cd.
183 extern int zap_leaf_lookup_closest(zap_leaf_t *l,
184 uint64_t hash, uint32_t cd, zap_entry_handle_t *zeh);
187 * Read the first num_integers in the attribute. Integer size
188 * conversion will be done without sign extension. Return EINVAL if
189 * integer_size is too small. Return EOVERFLOW if there are more than
190 * num_integers in the attribute.
192 extern int zap_entry_read(const zap_entry_handle_t *zeh,
193 uint8_t integer_size, uint64_t num_integers, void *buf);
195 extern int zap_entry_read_name(const zap_entry_handle_t *zeh,
196 uint16_t buflen, char *buf);
199 * Replace the value of an existing entry.
201 * zap_entry_update may fail if it runs out of space (ENOSPC).
203 extern int zap_entry_update(zap_entry_handle_t *zeh,
204 uint8_t integer_size, uint64_t num_integers, const void *buf);
209 extern void zap_entry_remove(zap_entry_handle_t *zeh);
212 * Create an entry. An equal entry must not exist, and this entry must
213 * belong in this leaf (according to its hash value). Fills in the
214 * entry handle on success. Returns 0 on success or ENOSPC on failure.
216 extern int zap_entry_create(zap_leaf_t *l,
217 const char *name, uint64_t h, uint32_t cd,
218 uint8_t integer_size, uint64_t num_integers, const void *buf,
219 zap_entry_handle_t *zeh);
225 extern void zap_leaf_init(zap_leaf_t *l);
226 extern void zap_leaf_byteswap(zap_leaf_phys_t *buf, int len);
227 extern void zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl);
228 extern void zap_leaf_stats(zap_t *zap, zap_leaf_t *l, zap_stats_t *zs);
234 #endif /* _SYS_ZAP_LEAF_H */