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