zap_leaf.h revision 288549
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
24 */
25
26#ifndef	_SYS_ZAP_LEAF_H
27#define	_SYS_ZAP_LEAF_H
28
29#include <sys/zap.h>
30
31#ifdef	__cplusplus
32extern "C" {
33#endif
34
35struct zap;
36struct zap_name;
37struct zap_stats;
38
39#define	ZAP_LEAF_MAGIC 0x2AB1EAF
40
41/* chunk size = 24 bytes */
42#define	ZAP_LEAF_CHUNKSIZE 24
43
44/*
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)
48 */
49#define	ZAP_LEAF_NUMCHUNKS(l) \
50	(((1<<(l)->l_bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(l)) / \
51	ZAP_LEAF_CHUNKSIZE - 2)
52
53/*
54 * The amount of space within the chunk available for the array is:
55 * chunk size - space for type (1) - space for next pointer (2)
56 */
57#define	ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
58
59#define	ZAP_LEAF_ARRAY_NCHUNKS(bytes) \
60	(((bytes)+ZAP_LEAF_ARRAY_BYTES-1)/ZAP_LEAF_ARRAY_BYTES)
61
62/*
63 * Low water mark:  when there are only this many chunks free, start
64 * growing the ptrtbl.  Ideally, this should be larger than a
65 * "reasonably-sized" entry.  20 chunks is more than enough for the
66 * largest directory entry (MAXNAMELEN (256) byte name, 8-byte value),
67 * while still being only around 3% for 16k blocks.
68 */
69#define	ZAP_LEAF_LOW_WATER (20)
70
71/*
72 * The leaf hash table has block size / 2^5 (32) number of entries,
73 * which should be more than enough for the maximum number of entries,
74 * which is less than block size / CHUNKSIZE (24) / minimum number of
75 * chunks per entry (3).
76 */
77#define	ZAP_LEAF_HASH_SHIFT(l) ((l)->l_bs - 5)
78#define	ZAP_LEAF_HASH_NUMENTRIES(l) (1 << ZAP_LEAF_HASH_SHIFT(l))
79
80/*
81 * The chunks start immediately after the hash table.  The end of the
82 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
83 * chunk_t.
84 */
85#define	ZAP_LEAF_CHUNK(l, idx) \
86	((zap_leaf_chunk_t *) \
87	(zap_leaf_phys(l)->l_hash + ZAP_LEAF_HASH_NUMENTRIES(l)))[idx]
88#define	ZAP_LEAF_ENTRY(l, idx) (&ZAP_LEAF_CHUNK(l, idx).l_entry)
89
90typedef enum zap_chunk_type {
91	ZAP_CHUNK_FREE = 253,
92	ZAP_CHUNK_ENTRY = 252,
93	ZAP_CHUNK_ARRAY = 251,
94	ZAP_CHUNK_TYPE_MAX = 250
95} zap_chunk_type_t;
96
97#define	ZLF_ENTRIES_CDSORTED (1<<0)
98
99/*
100 * TAKE NOTE:
101 * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified.
102 */
103typedef struct zap_leaf_phys {
104	struct zap_leaf_header {
105		/* Public to ZAP */
106		uint64_t lh_block_type;		/* ZBT_LEAF */
107		uint64_t lh_pad1;
108		uint64_t lh_prefix;		/* hash prefix of this leaf */
109		uint32_t lh_magic;		/* ZAP_LEAF_MAGIC */
110		uint16_t lh_nfree;		/* number free chunks */
111		uint16_t lh_nentries;		/* number of entries */
112		uint16_t lh_prefix_len;		/* num bits used to id this */
113
114		/* Private to zap_leaf */
115		uint16_t lh_freelist;		/* chunk head of free list */
116		uint8_t lh_flags;		/* ZLF_* flags */
117		uint8_t lh_pad2[11];
118	} l_hdr; /* 2 24-byte chunks */
119
120	/*
121	 * The header is followed by a hash table with
122	 * ZAP_LEAF_HASH_NUMENTRIES(zap) entries.  The hash table is
123	 * followed by an array of ZAP_LEAF_NUMCHUNKS(zap)
124	 * zap_leaf_chunk structures.  These structures are accessed
125	 * with the ZAP_LEAF_CHUNK() macro.
126	 */
127
128	uint16_t l_hash[1];
129} zap_leaf_phys_t;
130
131typedef union zap_leaf_chunk {
132	struct zap_leaf_entry {
133		uint8_t le_type; 		/* always ZAP_CHUNK_ENTRY */
134		uint8_t le_value_intlen;	/* size of value's ints */
135		uint16_t le_next;		/* next entry in hash chain */
136		uint16_t le_name_chunk;		/* first chunk of the name */
137		uint16_t le_name_numints;	/* ints in name (incl null) */
138		uint16_t le_value_chunk;	/* first chunk of the value */
139		uint16_t le_value_numints;	/* value length in ints */
140		uint32_t le_cd;			/* collision differentiator */
141		uint64_t le_hash;		/* hash value of the name */
142	} l_entry;
143	struct zap_leaf_array {
144		uint8_t la_type;		/* always ZAP_CHUNK_ARRAY */
145		uint8_t la_array[ZAP_LEAF_ARRAY_BYTES];
146		uint16_t la_next;		/* next blk or CHAIN_END */
147	} l_array;
148	struct zap_leaf_free {
149		uint8_t lf_type;		/* always ZAP_CHUNK_FREE */
150		uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES];
151		uint16_t lf_next;	/* next in free list, or CHAIN_END */
152	} l_free;
153} zap_leaf_chunk_t;
154
155typedef struct zap_leaf {
156	dmu_buf_user_t l_dbu;
157	krwlock_t l_rwlock;
158	uint64_t l_blkid;		/* 1<<ZAP_BLOCK_SHIFT byte block off */
159	int l_bs;			/* block size shift */
160	dmu_buf_t *l_dbuf;
161} zap_leaf_t;
162
163inline zap_leaf_phys_t *
164zap_leaf_phys(zap_leaf_t *l)
165{
166	return (l->l_dbuf->db_data);
167}
168
169typedef struct zap_entry_handle {
170	/* Set by zap_leaf and public to ZAP */
171	uint64_t zeh_num_integers;
172	uint64_t zeh_hash;
173	uint32_t zeh_cd;
174	uint8_t zeh_integer_size;
175
176	/* Private to zap_leaf */
177	uint16_t zeh_fakechunk;
178	uint16_t *zeh_chunkp;
179	zap_leaf_t *zeh_leaf;
180} zap_entry_handle_t;
181
182/*
183 * Return a handle to the named entry, or ENOENT if not found.  The hash
184 * value must equal zap_hash(name).
185 */
186extern int zap_leaf_lookup(zap_leaf_t *l,
187    struct zap_name *zn, zap_entry_handle_t *zeh);
188
189/*
190 * Return a handle to the entry with this hash+cd, or the entry with the
191 * next closest hash+cd.
192 */
193extern int zap_leaf_lookup_closest(zap_leaf_t *l,
194    uint64_t hash, uint32_t cd, zap_entry_handle_t *zeh);
195
196/*
197 * Read the first num_integers in the attribute.  Integer size
198 * conversion will be done without sign extension.  Return EINVAL if
199 * integer_size is too small.  Return EOVERFLOW if there are more than
200 * num_integers in the attribute.
201 */
202extern int zap_entry_read(const zap_entry_handle_t *zeh,
203    uint8_t integer_size, uint64_t num_integers, void *buf);
204
205extern int zap_entry_read_name(struct zap *zap, const zap_entry_handle_t *zeh,
206    uint16_t buflen, char *buf);
207
208/*
209 * Replace the value of an existing entry.
210 *
211 * May fail if it runs out of space (ENOSPC).
212 */
213extern int zap_entry_update(zap_entry_handle_t *zeh,
214    uint8_t integer_size, uint64_t num_integers, const void *buf);
215
216/*
217 * Remove an entry.
218 */
219extern void zap_entry_remove(zap_entry_handle_t *zeh);
220
221/*
222 * Create an entry. An equal entry must not exist, and this entry must
223 * belong in this leaf (according to its hash value).  Fills in the
224 * entry handle on success.  Returns 0 on success or ENOSPC on failure.
225 */
226extern int zap_entry_create(zap_leaf_t *l, struct zap_name *zn, uint32_t cd,
227    uint8_t integer_size, uint64_t num_integers, const void *buf,
228    zap_entry_handle_t *zeh);
229
230/* Determine whether there is another entry with the same normalized form. */
231extern boolean_t zap_entry_normalization_conflict(zap_entry_handle_t *zeh,
232    struct zap_name *zn, const char *name, struct zap *zap);
233
234/*
235 * Other stuff.
236 */
237
238extern void zap_leaf_init(zap_leaf_t *l, boolean_t sort);
239extern void zap_leaf_byteswap(zap_leaf_phys_t *buf, int len);
240extern void zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort);
241extern void zap_leaf_stats(struct zap *zap, zap_leaf_t *l,
242    struct zap_stats *zs);
243
244#ifdef	__cplusplus
245}
246#endif
247
248#endif /* _SYS_ZAP_LEAF_H */
249