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 https://opensource.org/licenses/CDDL-1.0.
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 #ifndef _SYS_ZAP_LEAF_H
27 #define _SYS_ZAP_LEAF_H
39 #define ZAP_LEAF_MAGIC 0x2AB1EAF
41 /* chunk size = 24 bytes */
42 #define ZAP_LEAF_CHUNKSIZE 24
45 * The amount of space available for chunks is:
46 * block size (1<<l->l_bs) - hash entry size (2) * number of hash
47 * entries - header space (2*chunksize)
49 #define ZAP_LEAF_NUMCHUNKS_BS(bs) \
50 (((1<<(bs)) - 2*ZAP_LEAF_HASH_NUMENTRIES_BS(bs)) / \
51 ZAP_LEAF_CHUNKSIZE - 2)
53 #define ZAP_LEAF_NUMCHUNKS(l) (ZAP_LEAF_NUMCHUNKS_BS(((l)->l_bs)))
55 #define ZAP_LEAF_NUMCHUNKS_DEF \
56 (ZAP_LEAF_NUMCHUNKS_BS(fzap_default_block_shift))
59 * The amount of space within the chunk available for the array is:
60 * chunk size - space for type (1) - space for next pointer (2)
62 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
64 #define ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
65 (((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
68 * Low water mark: when there are only this many chunks free, start
69 * growing the ptrtbl. Ideally, this should be larger than a
70 * "reasonably-sized" entry. 20 chunks is more than enough for the
71 * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
72 * while still being only around 3% for 16k blocks.
74 #define ZAP_LEAF_LOW_WATER (20)
77 * The leaf hash table has block size / 2^5 (32) number of entries,
78 * which should be more than enough for the maximum number of entries,
79 * which is less than block size / CHUNKSIZE (24) / minimum number of
80 * chunks per entry (3).
82 #define ZAP_LEAF_HASH_SHIFT_BS(bs) ((bs) - 5)
83 #define ZAP_LEAF_HASH_NUMENTRIES_BS(bs) (1 << ZAP_LEAF_HASH_SHIFT_BS(bs))
84 #define ZAP_LEAF_HASH_SHIFT(l) (ZAP_LEAF_HASH_SHIFT_BS(((l)->l_bs)))
85 #define ZAP_LEAF_HASH_NUMENTRIES(l) (ZAP_LEAF_HASH_NUMENTRIES_BS(((l)->l_bs)))
88 * The chunks start immediately after the hash table. The end of the
89 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
92 #define ZAP_LEAF_CHUNK(l, idx) \
93 ((zap_leaf_chunk_t *) \
94 (zap_leaf_phys(l)->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
95 #define ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
97 typedef enum zap_chunk_type
{
99 ZAP_CHUNK_ENTRY
= 252,
100 ZAP_CHUNK_ARRAY
= 251,
101 ZAP_CHUNK_TYPE_MAX
= 250
104 #define ZLF_ENTRIES_CDSORTED (1<<0)
108 * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
110 typedef struct zap_leaf_phys
{
111 struct zap_leaf_header
{
113 uint64_t lh_block_type
; /* ZBT_LEAF */
115 uint64_t lh_prefix
; /* hash prefix of this leaf */
116 uint32_t lh_magic
; /* ZAP_LEAF_MAGIC */
117 uint16_t lh_nfree
; /* number free chunks */
118 uint16_t lh_nentries
; /* number of entries */
119 uint16_t lh_prefix_len
; /* num bits used to id this */
121 /* Private to zap_leaf */
122 uint16_t lh_freelist
; /* chunk head of free list */
123 uint8_t lh_flags
; /* ZLF_* flags */
125 } l_hdr
; /* 2 24-byte chunks */
128 * The header is followed by a hash table with
129 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries. The hash table is
130 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
131 * zap_leaf_chunk structures. These structures are accessed
132 * with the ZAP_LEAF_CHUNK() macro.
138 typedef union zap_leaf_chunk
{
139 struct zap_leaf_entry
{
140 uint8_t le_type
; /* always ZAP_CHUNK_ENTRY */
141 uint8_t le_value_intlen
; /* size of value's ints */
142 uint16_t le_next
; /* next entry in hash chain */
143 uint16_t le_name_chunk
; /* first chunk of the name */
144 uint16_t le_name_numints
; /* ints in name (incl null) */
145 uint16_t le_value_chunk
; /* first chunk of the value */
146 uint16_t le_value_numints
; /* value length in ints */
147 uint32_t le_cd
; /* collision differentiator */
148 uint64_t le_hash
; /* hash value of the name */
150 struct zap_leaf_array
{
151 uint8_t la_type
; /* always ZAP_CHUNK_ARRAY */
152 uint8_t la_array
[ZAP_LEAF_ARRAY_BYTES
];
153 uint16_t la_next
; /* next blk or CHAIN_END */
155 struct zap_leaf_free
{
156 uint8_t lf_type
; /* always ZAP_CHUNK_FREE */
157 uint8_t lf_pad
[ZAP_LEAF_ARRAY_BYTES
];
158 uint16_t lf_next
; /* next in free list, or CHAIN_END */
162 typedef struct zap_leaf
{
163 dmu_buf_user_t l_dbu
;
165 uint64_t l_blkid
; /* 1<<ZAP_BLOCK_SHIFT byte block off */
166 int l_bs
; /* block size shift */
170 static inline zap_leaf_phys_t
*
171 zap_leaf_phys(zap_leaf_t
*l
)
173 return (l
->l_dbuf
->db_data
);
176 typedef struct zap_entry_handle
{
177 /* Set by zap_leaf and public to ZAP */
178 uint64_t zeh_num_integers
;
181 uint8_t zeh_integer_size
;
183 /* Private to zap_leaf */
184 uint16_t zeh_fakechunk
;
185 uint16_t *zeh_chunkp
;
186 zap_leaf_t
*zeh_leaf
;
187 } zap_entry_handle_t
;
190 * Return a handle to the named entry, or ENOENT if not found. The hash
191 * value must equal zap_hash(name).
193 extern int zap_leaf_lookup(zap_leaf_t
*l
,
194 struct zap_name
*zn
, zap_entry_handle_t
*zeh
);
197 * Return a handle to the entry with this hash+cd, or the entry with the
198 * next closest hash+cd.
200 extern int zap_leaf_lookup_closest(zap_leaf_t
*l
,
201 uint64_t hash
, uint32_t cd
, zap_entry_handle_t
*zeh
);
204 * Read the first num_integers in the attribute. Integer size
205 * conversion will be done without sign extension. Return EINVAL if
206 * integer_size is too small. Return EOVERFLOW if there are more than
207 * num_integers in the attribute.
209 extern int zap_entry_read(const zap_entry_handle_t
*zeh
,
210 uint8_t integer_size
, uint64_t num_integers
, void *buf
);
212 extern int zap_entry_read_name(struct zap
*zap
, const zap_entry_handle_t
*zeh
,
213 uint16_t buflen
, char *buf
);
216 * Replace the value of an existing entry.
218 * May fail if it runs out of space (ENOSPC).
220 extern int zap_entry_update(zap_entry_handle_t
*zeh
,
221 uint8_t integer_size
, uint64_t num_integers
, const void *buf
);
226 extern void zap_entry_remove(zap_entry_handle_t
*zeh
);
229 * Create an entry. An equal entry must not exist, and this entry must
230 * belong in this leaf (according to its hash value). Fills in the
231 * entry handle on success. Returns 0 on success or ENOSPC on failure.
233 extern int zap_entry_create(zap_leaf_t
*l
, struct zap_name
*zn
, uint32_t cd
,
234 uint8_t integer_size
, uint64_t num_integers
, const void *buf
,
235 zap_entry_handle_t
*zeh
);
237 /* Determine whether there is another entry with the same normalized form. */
238 extern boolean_t
zap_entry_normalization_conflict(zap_entry_handle_t
*zeh
,
239 struct zap_name
*zn
, const char *name
, struct zap
*zap
);
245 extern void zap_leaf_init(zap_leaf_t
*l
, boolean_t sort
);
246 extern void zap_leaf_byteswap(zap_leaf_phys_t
*buf
, int len
);
247 extern void zap_leaf_split(zap_leaf_t
*l
, zap_leaf_t
*nl
, boolean_t sort
);
248 extern void zap_leaf_stats(struct zap
*zap
, zap_leaf_t
*l
,
249 struct zap_stats
*zs
);
255 #endif /* _SYS_ZAP_LEAF_H */