1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5f65e61c0Sahrens * Common Development and Distribution License (the "License").
6f65e61c0Sahrens * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
24fa9e4066Sahrens */
25fa9e4066Sahrens
26fa9e4066Sahrens #ifndef _SYS_ZAP_LEAF_H
27fa9e4066Sahrens #define _SYS_ZAP_LEAF_H
28fa9e4066Sahrens
293f9d6ad7SLin Ling #include <sys/zap.h>
303f9d6ad7SLin Ling
31fa9e4066Sahrens #ifdef __cplusplus
32fa9e4066Sahrens extern "C" {
33fa9e4066Sahrens #endif
34fa9e4066Sahrens
35fa9e4066Sahrens struct zap;
363f1f8012SMatthew Ahrens struct zap_name;
373f1f8012SMatthew Ahrens struct zap_stats;
38fa9e4066Sahrens
39fa9e4066Sahrens #define ZAP_LEAF_MAGIC 0x2AB1EAF
40fa9e4066Sahrens
41fa9e4066Sahrens /* chunk size = 24 bytes */
42f65e61c0Sahrens #define ZAP_LEAF_CHUNKSIZE 24
43fa9e4066Sahrens
44f65e61c0Sahrens /*
45f65e61c0Sahrens * The amount of space available for chunks is:
46f65e61c0Sahrens * block size (1<<l->l_bs) - hash entry size (2) * number of hash
47f65e61c0Sahrens * entries - header space (2*chunksize)
48f65e61c0Sahrens */
49f65e61c0Sahrens #define ZAP_LEAF_NUMCHUNKS(l) \
50f65e61c0Sahrens (((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
51f65e61c0Sahrens ZAP_LEAF_CHUNKSIZE - 2)
52f65e61c0Sahrens
53f65e61c0Sahrens /*
54f65e61c0Sahrens * The amount of space within the chunk available for the array is:
55f65e61c0Sahrens * chunk size - space for type (1) - space for next pointer (2)
56f65e61c0Sahrens */
57f65e61c0Sahrens #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
58f65e61c0Sahrens
5966328dd3Sahrens #define ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
6066328dd3Sahrens (((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
6166328dd3Sahrens
6266328dd3Sahrens /*
6366328dd3Sahrens * Low water mark: when there are only this many chunks free, start
6466328dd3Sahrens * growing the ptrtbl. Ideally, this should be larger than a
6566328dd3Sahrens * "reasonably-sized" entry. 20 chunks is more than enough for the
6666328dd3Sahrens * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
6766328dd3Sahrens * while still being only around 3% for 16k blocks.
6866328dd3Sahrens */
6966328dd3Sahrens #define ZAP_LEAF_LOW_WATER (20)
7066328dd3Sahrens
71f65e61c0Sahrens /*
72f65e61c0Sahrens * The leaf hash table has block size / 2^5 (32) number of entries,
73f65e61c0Sahrens * which should be more than enough for the maximum number of entries,
74f65e61c0Sahrens * which is less than block size / CHUNKSIZE (24) / minimum number of
75f65e61c0Sahrens * chunks per entry (3).
76f65e61c0Sahrens */
77f65e61c0Sahrens #define ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
78f65e61c0Sahrens #define ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
79fa9e4066Sahrens
80f65e61c0Sahrens /*
81f65e61c0Sahrens * The chunks start immediately after the hash table. The end of the
82f65e61c0Sahrens * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
83f65e61c0Sahrens * chunk_t.
84f65e61c0Sahrens */
85f65e61c0Sahrens #define ZAP_LEAF_CHUNK(l, idx) \
86f65e61c0Sahrens ((zap_leaf_chunk_t *) \
87c1379625SJustin T. Gibbs (zap_leaf_phys(l)->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
88f65e61c0Sahrens #define ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
89f65e61c0Sahrens
90f65e61c0Sahrens typedef enum zap_chunk_type {
91f65e61c0Sahrens ZAP_CHUNK_FREE = 253,
92f65e61c0Sahrens ZAP_CHUNK_ENTRY = 252,
93f65e61c0Sahrens ZAP_CHUNK_ARRAY = 251,
94f65e61c0Sahrens ZAP_CHUNK_TYPE_MAX = 250
95f65e61c0Sahrens } zap_chunk_type_t;
96fa9e4066Sahrens
97da6c28aaSamw #define ZLF_ENTRIES_CDSORTED (1<<0)
98da6c28aaSamw
99fa9e4066Sahrens /*
100fa9e4066Sahrens * TAKE NOTE:
101fa9e4066Sahrens * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
102fa9e4066Sahrens */
103fa9e4066Sahrens typedef struct zap_leaf_phys {
104fa9e4066Sahrens struct zap_leaf_header {
105f7170741SWill Andrews /* Public to ZAP */
10666328dd3Sahrens uint64_t lh_block_type; /* ZBT_LEAF */
10766328dd3Sahrens uint64_t lh_pad1;
10866328dd3Sahrens uint64_t lh_prefix; /* hash prefix of this leaf */
10966328dd3Sahrens uint32_t lh_magic; /* ZAP_LEAF_MAGIC */
11066328dd3Sahrens uint16_t lh_nfree; /* number free chunks */
11166328dd3Sahrens uint16_t lh_nentries; /* number of entries */
11266328dd3Sahrens uint16_t lh_prefix_len; /* num bits used to id this */
113fa9e4066Sahrens
114f7170741SWill Andrews /* Private to zap_leaf */
115fa9e4066Sahrens uint16_t lh_freelist; /* chunk head of free list */
116da6c28aaSamw uint8_t lh_flags; /* ZLF_* flags */
117da6c28aaSamw uint8_t lh_pad2[11];
118fa9e4066Sahrens } l_hdr; /* 2 24-byte chunks */
119fa9e4066Sahrens
120f65e61c0Sahrens /*
121f65e61c0Sahrens * The header is followed by a hash table with
122f65e61c0Sahrens * ZAP_LEAF_HASH_NUMENTRIES(zap) entries. The hash table is
123f65e61c0Sahrens * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
124f65e61c0Sahrens * zap_leaf_chunk structures. These structures are accessed
125f65e61c0Sahrens * with the ZAP_LEAF_CHUNK() macro.
126f65e61c0Sahrens */
127f65e61c0Sahrens
128f65e61c0Sahrens uint16_t l_hash[1];
129fa9e4066Sahrens } zap_leaf_phys_t;
130fa9e4066Sahrens
131f65e61c0Sahrens typedef union zap_leaf_chunk {
132f65e61c0Sahrens struct zap_leaf_entry {
133f65e61c0Sahrens uint8_t le_type; /* always ZAP_CHUNK_ENTRY */
134486ae710SMatthew Ahrens uint8_t le_value_intlen; /* size of value's ints */
135f65e61c0Sahrens uint16_t le_next; /* next entry in hash chain */
136f65e61c0Sahrens uint16_t le_name_chunk; /* first chunk of the name */
137486ae710SMatthew Ahrens uint16_t le_name_numints; /* ints in name (incl null) */
138f65e61c0Sahrens uint16_t le_value_chunk; /* first chunk of the value */
139486ae710SMatthew Ahrens uint16_t le_value_numints; /* value length in ints */
140f65e61c0Sahrens uint32_t le_cd; /* collision differentiator */
141f65e61c0Sahrens uint64_t le_hash; /* hash value of the name */
142f65e61c0Sahrens } l_entry;
143f65e61c0Sahrens struct zap_leaf_array {
144f65e61c0Sahrens uint8_t la_type; /* always ZAP_CHUNK_ARRAY */
145f65e61c0Sahrens uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
146f65e61c0Sahrens uint16_t la_next; /* next blk or CHAIN_END */
147f65e61c0Sahrens } l_array;
148f65e61c0Sahrens struct zap_leaf_free {
149f65e61c0Sahrens uint8_t lf_type; /* always ZAP_CHUNK_FREE */
150f65e61c0Sahrens uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
151f65e61c0Sahrens uint16_t lf_next; /* next in free list, or CHAIN_END */
152f65e61c0Sahrens } l_free;
153f65e61c0Sahrens } zap_leaf_chunk_t;
154f65e61c0Sahrens
155fa9e4066Sahrens typedef struct zap_leaf {
156*bc9014e6SJustin Gibbs dmu_buf_user_t l_dbu;
157088f3894Sahrens krwlock_t l_rwlock;
158fa9e4066Sahrens uint64_t l_blkid; /* 1<<ZAP_BLOCK_SHIFT byte block off */
159f65e61c0Sahrens int l_bs; /* block size shift */
160fa9e4066Sahrens dmu_buf_t *l_dbuf;
161fa9e4066Sahrens } zap_leaf_t;
162fa9e4066Sahrens
163c1379625SJustin T. Gibbs inline zap_leaf_phys_t *
zap_leaf_phys(zap_leaf_t * l)164c1379625SJustin T. Gibbs zap_leaf_phys(zap_leaf_t *l)
165c1379625SJustin T. Gibbs {
166c1379625SJustin T. Gibbs return (l->l_dbuf->db_data);
167c1379625SJustin T. Gibbs }
168fa9e4066Sahrens
169fa9e4066Sahrens typedef struct zap_entry_handle {
170f7170741SWill Andrews /* Set by zap_leaf and public to ZAP */
171fa9e4066Sahrens uint64_t zeh_num_integers;
172fa9e4066Sahrens uint64_t zeh_hash;
173fa9e4066Sahrens uint32_t zeh_cd;
174fa9e4066Sahrens uint8_t zeh_integer_size;
175fa9e4066Sahrens
176f7170741SWill Andrews /* Private to zap_leaf */
177fa9e4066Sahrens uint16_t zeh_fakechunk;
178fa9e4066Sahrens uint16_t *zeh_chunkp;
17966328dd3Sahrens zap_leaf_t *zeh_leaf;
180fa9e4066Sahrens } zap_entry_handle_t;
181fa9e4066Sahrens
182fa9e4066Sahrens /*
183fa9e4066Sahrens * Return a handle to the named entry, or ENOENT if not found. The hash
184fa9e4066Sahrens * value must equal zap_hash(name).
185fa9e4066Sahrens */
186fa9e4066Sahrens extern int zap_leaf_lookup(zap_leaf_t *l,
1873f1f8012SMatthew Ahrens struct zap_name *zn, zap_entry_handle_t *zeh);
188fa9e4066Sahrens
189fa9e4066Sahrens /*
190fa9e4066Sahrens * Return a handle to the entry with this hash+cd, or the entry with the
191fa9e4066Sahrens * next closest hash+cd.
192fa9e4066Sahrens */
193fa9e4066Sahrens extern int zap_leaf_lookup_closest(zap_leaf_t *l,
194fa9e4066Sahrens uint64_t hash, uint32_t cd, zap_entry_handle_t *zeh);
195fa9e4066Sahrens
196fa9e4066Sahrens /*
197fa9e4066Sahrens * Read the first num_integers in the attribute. Integer size
198fa9e4066Sahrens * conversion will be done without sign extension. Return EINVAL if
199fa9e4066Sahrens * integer_size is too small. Return EOVERFLOW if there are more than
200fa9e4066Sahrens * num_integers in the attribute.
201fa9e4066Sahrens */
202fa9e4066Sahrens extern int zap_entry_read(const zap_entry_handle_t *zeh,
203b24ab676SJeff Bonwick uint8_t integer_size, uint64_t num_integers, void *buf);
204fa9e4066Sahrens
2053f1f8012SMatthew Ahrens extern int zap_entry_read_name(struct zap *zap, const zap_entry_handle_t *zeh,
206b24ab676SJeff Bonwick uint16_t buflen, char *buf);
207fa9e4066Sahrens
208fa9e4066Sahrens /*
209fa9e4066Sahrens * Replace the value of an existing entry.
210fa9e4066Sahrens *
211f7170741SWill Andrews * May fail if it runs out of space (ENOSPC).
212fa9e4066Sahrens */
213fa9e4066Sahrens extern int zap_entry_update(zap_entry_handle_t *zeh,
214b24ab676SJeff Bonwick uint8_t integer_size, uint64_t num_integers, const void *buf);
215fa9e4066Sahrens
216fa9e4066Sahrens /*
217fa9e4066Sahrens * Remove an entry.
218fa9e4066Sahrens */
219fa9e4066Sahrens extern void zap_entry_remove(zap_entry_handle_t *zeh);
220fa9e4066Sahrens
221fa9e4066Sahrens /*
222fa9e4066Sahrens * Create an entry. An equal entry must not exist, and this entry must
223fa9e4066Sahrens * belong in this leaf (according to its hash value). Fills in the
224fa9e4066Sahrens * entry handle on success. Returns 0 on success or ENOSPC on failure.
225fa9e4066Sahrens */
2263f1f8012SMatthew Ahrens extern int zap_entry_create(zap_leaf_t *l, struct zap_name *zn, uint32_t cd,
227b24ab676SJeff Bonwick uint8_t integer_size, uint64_t num_integers, const void *buf,
228b24ab676SJeff Bonwick zap_entry_handle_t *zeh);
229fa9e4066Sahrens
230f7170741SWill Andrews /* Determine whether there is another entry with the same normalized form. */
231da6c28aaSamw extern boolean_t zap_entry_normalization_conflict(zap_entry_handle_t *zeh,
2323f1f8012SMatthew Ahrens struct zap_name *zn, const char *name, struct zap *zap);
233da6c28aaSamw
234fa9e4066Sahrens /*
235fa9e4066Sahrens * Other stuff.
236fa9e4066Sahrens */
237fa9e4066Sahrens
238de8267e0Stimh extern void zap_leaf_init(zap_leaf_t *l, boolean_t sort);
239f65e61c0Sahrens extern void zap_leaf_byteswap(zap_leaf_phys_t *buf, int len);
240de8267e0Stimh extern void zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort);
2413f1f8012SMatthew Ahrens extern void zap_leaf_stats(struct zap *zap, zap_leaf_t *l,
2423f1f8012SMatthew Ahrens struct zap_stats *zs);
243fa9e4066Sahrens
244fa9e4066Sahrens #ifdef __cplusplus
245fa9e4066Sahrens }
246fa9e4066Sahrens #endif
247fa9e4066Sahrens
248fa9e4066Sahrens #endif /* _SYS_ZAP_LEAF_H */
249