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