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