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