xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zap.h (revision 0c779ad424a92a84d1e07d47cab7f8009189202b)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
566328dd3Sahrens  * Common Development and Distribution License (the "License").
666328dd3Sahrens  * 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.
23*0c779ad4SMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #ifndef	_SYS_ZAP_H
27fa9e4066Sahrens #define	_SYS_ZAP_H
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * ZAP - ZFS Attribute Processor
31fa9e4066Sahrens  *
32da6c28aaSamw  * The ZAP is a module which sits on top of the DMU (Data Management
33fa9e4066Sahrens  * Unit) and implements a higher-level storage primitive using DMU
34fa9e4066Sahrens  * objects.  Its primary consumer is the ZPL (ZFS Posix Layer).
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * A "zapobj" is a DMU object which the ZAP uses to stores attributes.
37fa9e4066Sahrens  * Users should use only zap routines to access a zapobj - they should
38fa9e4066Sahrens  * not access the DMU object directly using DMU routines.
39fa9e4066Sahrens  *
40fa9e4066Sahrens  * The attributes stored in a zapobj are name-value pairs.  The name is
4166328dd3Sahrens  * a zero-terminated string of up to ZAP_MAXNAMELEN bytes (including
4266328dd3Sahrens  * terminating NULL).  The value is an array of integers, which may be
4366328dd3Sahrens  * 1, 2, 4, or 8 bytes long.  The total space used by the array (number
4466328dd3Sahrens  * of integers * integer length) can be up to ZAP_MAXVALUELEN bytes.
4566328dd3Sahrens  * Note that an 8-byte integer value can be used to store the location
4666328dd3Sahrens  * (object number) of another dmu object (which may be itself a zapobj).
4766328dd3Sahrens  * Note that you can use a zero-length attribute to store a single bit
4866328dd3Sahrens  * of information - the attribute is present or not.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * The ZAP routines are thread-safe.  However, you must observe the
51fa9e4066Sahrens  * DMU's restriction that a transaction may not be operated on
52fa9e4066Sahrens  * concurrently.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * Any of the routines that return an int may return an I/O error (EIO
55fa9e4066Sahrens  * or ECHECKSUM).
56fa9e4066Sahrens  *
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * Implementation / Performance Notes:
59fa9e4066Sahrens  *
60fa9e4066Sahrens  * The ZAP is intended to operate most efficiently on attributes with
6166328dd3Sahrens  * short (49 bytes or less) names and single 8-byte values, for which
6266328dd3Sahrens  * the microzap will be used.  The ZAP should be efficient enough so
6366328dd3Sahrens  * that the user does not need to cache these attributes.
64fa9e4066Sahrens  *
65fa9e4066Sahrens  * The ZAP's locking scheme makes its routines thread-safe.  Operations
66fa9e4066Sahrens  * on different zapobjs will be processed concurrently.  Operations on
67fa9e4066Sahrens  * the same zapobj which only read data will be processed concurrently.
68fa9e4066Sahrens  * Operations on the same zapobj which modify data will be processed
69fa9e4066Sahrens  * concurrently when there are many attributes in the zapobj (because
7066328dd3Sahrens  * the ZAP uses per-block locking - more than 128 * (number of cpus)
71fa9e4066Sahrens  * small attributes will suffice).
72fa9e4066Sahrens  */
73fa9e4066Sahrens 
74fa9e4066Sahrens /*
75fa9e4066Sahrens  * We're using zero-terminated byte strings (ie. ASCII or UTF-8 C
76fa9e4066Sahrens  * strings) for the names of attributes, rather than a byte string
77fa9e4066Sahrens  * bounded by an explicit length.  If some day we want to support names
78fa9e4066Sahrens  * in character sets which have embedded zeros (eg. UTF-16, UTF-32),
79fa9e4066Sahrens  * we'll have to add routines for using length-bounded strings.
80fa9e4066Sahrens  */
81fa9e4066Sahrens 
82fa9e4066Sahrens #include <sys/dmu.h>
83*0c779ad4SMatthew Ahrens #include <sys/refcount.h>
84fa9e4066Sahrens 
85fa9e4066Sahrens #ifdef	__cplusplus
86fa9e4066Sahrens extern "C" {
87fa9e4066Sahrens #endif
88fa9e4066Sahrens 
89da6c28aaSamw /*
90f7170741SWill Andrews  * Specifies matching criteria for ZAP lookups.
91da6c28aaSamw  */
92da6c28aaSamw typedef enum matchtype
93da6c28aaSamw {
94f7170741SWill Andrews 	/* Only find an exact match (non-normalized) */
95da6c28aaSamw 	MT_EXACT,
96f7170741SWill Andrews 	/*
97f7170741SWill Andrews 	 * If there is an exact match, find that, otherwise find the
98f7170741SWill Andrews 	 * first normalized match.
99f7170741SWill Andrews 	 */
100da6c28aaSamw 	MT_BEST,
101f7170741SWill Andrews 	/*
102f7170741SWill Andrews 	 * Find the "first" normalized (case and Unicode form) match;
103f7170741SWill Andrews 	 * the designated "first" match will not change as long as the
104f7170741SWill Andrews 	 * set of entries with this normalization doesn't change.
105f7170741SWill Andrews 	 */
106da6c28aaSamw 	MT_FIRST
107da6c28aaSamw } matchtype_t;
108da6c28aaSamw 
109b24ab676SJeff Bonwick typedef enum zap_flags {
110b24ab676SJeff Bonwick 	/* Use 64-bit hash value (serialized cursors will always use 64-bits) */
111b24ab676SJeff Bonwick 	ZAP_FLAG_HASH64 = 1 << 0,
112b24ab676SJeff Bonwick 	/* Key is binary, not string (zap_add_uint64() can be used) */
113b24ab676SJeff Bonwick 	ZAP_FLAG_UINT64_KEY = 1 << 1,
114b24ab676SJeff Bonwick 	/*
115b24ab676SJeff Bonwick 	 * First word of key (which must be an array of uint64) is
116b24ab676SJeff Bonwick 	 * already randomly distributed.
117b24ab676SJeff Bonwick 	 */
118b24ab676SJeff Bonwick 	ZAP_FLAG_PRE_HASHED_KEY = 1 << 2,
119b24ab676SJeff Bonwick } zap_flags_t;
120b24ab676SJeff Bonwick 
121fa9e4066Sahrens /*
122fa9e4066Sahrens  * Create a new zapobj with no attributes and return its object number.
123da6c28aaSamw  * MT_EXACT will cause the zap object to only support MT_EXACT lookups,
124da6c28aaSamw  * otherwise any matchtype can be used for lookups.
125da6c28aaSamw  *
126da6c28aaSamw  * normflags specifies what normalization will be done.  values are:
127da6c28aaSamw  * 0: no normalization (legacy on-disk format, supports MT_EXACT matching
128da6c28aaSamw  *     only)
129da6c28aaSamw  * U8_TEXTPREP_TOLOWER: case normalization will be performed.
130da6c28aaSamw  *     MT_FIRST/MT_BEST matching will find entries that match without
131da6c28aaSamw  *     regard to case (eg. looking for "foo" can find an entry "Foo").
132da6c28aaSamw  * Eventually, other flags will permit unicode normalization as well.
133fa9e4066Sahrens  */
134fa9e4066Sahrens uint64_t zap_create(objset_t *ds, dmu_object_type_t ot,
135fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
136da6c28aaSamw uint64_t zap_create_norm(objset_t *ds, int normflags, dmu_object_type_t ot,
137da6c28aaSamw     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
138b24ab676SJeff Bonwick uint64_t zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
139b24ab676SJeff Bonwick     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
140b24ab676SJeff Bonwick     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
141ad135b5dSChristopher Siden uint64_t zap_create_link(objset_t *os, dmu_object_type_t ot,
142ad135b5dSChristopher Siden     uint64_t parent_obj, const char *name, dmu_tx_t *tx);
143fa9e4066Sahrens 
1442acef22dSMatthew Ahrens /*
1452acef22dSMatthew Ahrens  * Initialize an already-allocated object.
1462acef22dSMatthew Ahrens  */
1472acef22dSMatthew Ahrens void mzap_create_impl(objset_t *os, uint64_t obj, int normflags,
1482acef22dSMatthew Ahrens     zap_flags_t flags, dmu_tx_t *tx);
1492acef22dSMatthew Ahrens 
150fa9e4066Sahrens /*
151fa9e4066Sahrens  * Create a new zapobj with no attributes from the given (unallocated)
152fa9e4066Sahrens  * object number.
153fa9e4066Sahrens  */
154fa9e4066Sahrens int zap_create_claim(objset_t *ds, uint64_t obj, dmu_object_type_t ot,
155fa9e4066Sahrens     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
156da6c28aaSamw int zap_create_claim_norm(objset_t *ds, uint64_t obj,
157da6c28aaSamw     int normflags, dmu_object_type_t ot,
158da6c28aaSamw     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
159fa9e4066Sahrens 
160fa9e4066Sahrens /*
161fa9e4066Sahrens  * The zapobj passed in must be a valid ZAP object for all of the
162fa9e4066Sahrens  * following routines.
163fa9e4066Sahrens  */
164fa9e4066Sahrens 
165fa9e4066Sahrens /*
166fa9e4066Sahrens  * Destroy this zapobj and all its attributes.
167fa9e4066Sahrens  *
168fa9e4066Sahrens  * Frees the object number using dmu_object_free.
169fa9e4066Sahrens  */
170fa9e4066Sahrens int zap_destroy(objset_t *ds, uint64_t zapobj, dmu_tx_t *tx);
171fa9e4066Sahrens 
172fa9e4066Sahrens /*
173fa9e4066Sahrens  * Manipulate attributes.
174fa9e4066Sahrens  *
175fa9e4066Sahrens  * 'integer_size' is in bytes, and must be 1, 2, 4, or 8.
176fa9e4066Sahrens  */
177fa9e4066Sahrens 
178fa9e4066Sahrens /*
179fa9e4066Sahrens  * Retrieve the contents of the attribute with the given name.
180fa9e4066Sahrens  *
181fa9e4066Sahrens  * If the requested attribute does not exist, the call will fail and
182fa9e4066Sahrens  * return ENOENT.
183fa9e4066Sahrens  *
184fa9e4066Sahrens  * If 'integer_size' is smaller than the attribute's integer size, the
185fa9e4066Sahrens  * call will fail and return EINVAL.
186fa9e4066Sahrens  *
187fa9e4066Sahrens  * If 'integer_size' is equal to or larger than the attribute's integer
188f7170741SWill Andrews  * size, the call will succeed and return 0.
189f7170741SWill Andrews  *
190f7170741SWill Andrews  * When converting to a larger integer size, the integers will be treated as
191f7170741SWill Andrews  * unsigned (ie. no sign-extension will be performed).
192fa9e4066Sahrens  *
193fa9e4066Sahrens  * 'num_integers' is the length (in integers) of 'buf'.
194fa9e4066Sahrens  *
195fa9e4066Sahrens  * If the attribute is longer than the buffer, as many integers as will
196fa9e4066Sahrens  * fit will be transferred to 'buf'.  If the entire attribute was not
197fa9e4066Sahrens  * transferred, the call will return EOVERFLOW.
198f7170741SWill Andrews  */
199f7170741SWill Andrews int zap_lookup(objset_t *ds, uint64_t zapobj, const char *name,
200f7170741SWill Andrews     uint64_t integer_size, uint64_t num_integers, void *buf);
201f7170741SWill Andrews 
202f7170741SWill Andrews /*
203da6c28aaSamw  * If rn_len is nonzero, realname will be set to the name of the found
204da6c28aaSamw  * entry (which may be different from the requested name if matchtype is
205da6c28aaSamw  * not MT_EXACT).
206da6c28aaSamw  *
207da6c28aaSamw  * If normalization_conflictp is not NULL, it will be set if there is
208da6c28aaSamw  * another name with the same case/unicode normalized form.
209fa9e4066Sahrens  */
210da6c28aaSamw int zap_lookup_norm(objset_t *ds, uint64_t zapobj, const char *name,
211da6c28aaSamw     uint64_t integer_size, uint64_t num_integers, void *buf,
212da6c28aaSamw     matchtype_t mt, char *realname, int rn_len,
213da6c28aaSamw     boolean_t *normalization_conflictp);
214b24ab676SJeff Bonwick int zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
215b24ab676SJeff Bonwick     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf);
21692241e0bSTom Erickson int zap_contains(objset_t *ds, uint64_t zapobj, const char *name);
217c7cd2421SGeorge Wilson int zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
218c7cd2421SGeorge Wilson     int key_numints);
219fa9e4066Sahrens 
2203d692628SSanjeev Bagewadi int zap_count_write(objset_t *os, uint64_t zapobj, const char *name,
221*0c779ad4SMatthew Ahrens     int add, refcount_t *towrite, refcount_t *tooverwrite);
2223d692628SSanjeev Bagewadi 
223fa9e4066Sahrens /*
224fa9e4066Sahrens  * Create an attribute with the given name and value.
225fa9e4066Sahrens  *
226fa9e4066Sahrens  * If an attribute with the given name already exists, the call will
227fa9e4066Sahrens  * fail and return EEXIST.
228fa9e4066Sahrens  */
229b24ab676SJeff Bonwick int zap_add(objset_t *ds, uint64_t zapobj, const char *key,
230fa9e4066Sahrens     int integer_size, uint64_t num_integers,
231fa9e4066Sahrens     const void *val, dmu_tx_t *tx);
232b24ab676SJeff Bonwick int zap_add_uint64(objset_t *ds, uint64_t zapobj, const uint64_t *key,
233b24ab676SJeff Bonwick     int key_numints, int integer_size, uint64_t num_integers,
234b24ab676SJeff Bonwick     const void *val, dmu_tx_t *tx);
235fa9e4066Sahrens 
236fa9e4066Sahrens /*
237fa9e4066Sahrens  * Set the attribute with the given name to the given value.  If an
238fa9e4066Sahrens  * attribute with the given name does not exist, it will be created.  If
239fa9e4066Sahrens  * an attribute with the given name already exists, the previous value
240fa9e4066Sahrens  * will be overwritten.  The integer_size may be different from the
241fa9e4066Sahrens  * existing attribute's integer size, in which case the attribute's
242fa9e4066Sahrens  * integer size will be updated to the new value.
243fa9e4066Sahrens  */
244fa9e4066Sahrens int zap_update(objset_t *ds, uint64_t zapobj, const char *name,
245fa9e4066Sahrens     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
246b24ab676SJeff Bonwick int zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
247b24ab676SJeff Bonwick     int key_numints,
248b24ab676SJeff Bonwick     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
249fa9e4066Sahrens 
250fa9e4066Sahrens /*
251fa9e4066Sahrens  * Get the length (in integers) and the integer size of the specified
252fa9e4066Sahrens  * attribute.
253fa9e4066Sahrens  *
254fa9e4066Sahrens  * If the requested attribute does not exist, the call will fail and
255fa9e4066Sahrens  * return ENOENT.
256fa9e4066Sahrens  */
257fa9e4066Sahrens int zap_length(objset_t *ds, uint64_t zapobj, const char *name,
258fa9e4066Sahrens     uint64_t *integer_size, uint64_t *num_integers);
259b24ab676SJeff Bonwick int zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
260b24ab676SJeff Bonwick     int key_numints, uint64_t *integer_size, uint64_t *num_integers);
261fa9e4066Sahrens 
262fa9e4066Sahrens /*
263fa9e4066Sahrens  * Remove the specified attribute.
264fa9e4066Sahrens  *
265fa9e4066Sahrens  * If the specified attribute does not exist, the call will fail and
266fa9e4066Sahrens  * return ENOENT.
267fa9e4066Sahrens  */
268fa9e4066Sahrens int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx);
269da6c28aaSamw int zap_remove_norm(objset_t *ds, uint64_t zapobj, const char *name,
270da6c28aaSamw     matchtype_t mt, dmu_tx_t *tx);
271b24ab676SJeff Bonwick int zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
272b24ab676SJeff Bonwick     int key_numints, dmu_tx_t *tx);
273fa9e4066Sahrens 
274fa9e4066Sahrens /*
275fa9e4066Sahrens  * Returns (in *count) the number of attributes in the specified zap
276fa9e4066Sahrens  * object.
277fa9e4066Sahrens  */
278fa9e4066Sahrens int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count);
279fa9e4066Sahrens 
280fa9e4066Sahrens /*
281e7437265Sahrens  * Returns (in name) the name of the entry whose (value & mask)
282fa9e4066Sahrens  * (za_first_integer) is value, or ENOENT if not found.  The string
283e7437265Sahrens  * pointed to by name must be at least 256 bytes long.  If mask==0, the
284e7437265Sahrens  * match must be exact (ie, same as mask=-1ULL).
285fa9e4066Sahrens  */
286e7437265Sahrens int zap_value_search(objset_t *os, uint64_t zapobj,
287e7437265Sahrens     uint64_t value, uint64_t mask, char *name);
288fa9e4066Sahrens 
289088f3894Sahrens /*
290088f3894Sahrens  * Transfer all the entries from fromobj into intoobj.  Only works on
291088f3894Sahrens  * int_size=8 num_integers=1 values.  Fails if there are any duplicated
292088f3894Sahrens  * entries.
293088f3894Sahrens  */
294088f3894Sahrens int zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx);
295088f3894Sahrens 
2963f9d6ad7SLin Ling /* Same as zap_join, but set the values to 'value'. */
2973f9d6ad7SLin Ling int zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
2983f9d6ad7SLin Ling     uint64_t value, dmu_tx_t *tx);
2993f9d6ad7SLin Ling 
3003f9d6ad7SLin Ling /* Same as zap_join, but add together any duplicated entries. */
3013f9d6ad7SLin Ling int zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
3023f9d6ad7SLin Ling     dmu_tx_t *tx);
3033f9d6ad7SLin Ling 
304088f3894Sahrens /*
305088f3894Sahrens  * Manipulate entries where the name + value are the "same" (the name is
306088f3894Sahrens  * a stringified version of the value).
307088f3894Sahrens  */
308088f3894Sahrens int zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
309088f3894Sahrens int zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
310088f3894Sahrens int zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value);
3119966ca11SMatthew Ahrens int zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
3129966ca11SMatthew Ahrens     dmu_tx_t *tx);
313088f3894Sahrens 
3143f9d6ad7SLin Ling /* Here the key is an int and the value is a different int. */
3153f9d6ad7SLin Ling int zap_add_int_key(objset_t *os, uint64_t obj,
3163f9d6ad7SLin Ling     uint64_t key, uint64_t value, dmu_tx_t *tx);
317f1745736SMatthew Ahrens int zap_update_int_key(objset_t *os, uint64_t obj,
318f1745736SMatthew Ahrens     uint64_t key, uint64_t value, dmu_tx_t *tx);
3193f9d6ad7SLin Ling int zap_lookup_int_key(objset_t *os, uint64_t obj,
3203f9d6ad7SLin Ling     uint64_t key, uint64_t *valuep);
3213f9d6ad7SLin Ling 
3223f9d6ad7SLin Ling int zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
3233f9d6ad7SLin Ling     dmu_tx_t *tx);
3243f9d6ad7SLin Ling 
32587e5029aSahrens struct zap;
32687e5029aSahrens struct zap_leaf;
327fa9e4066Sahrens typedef struct zap_cursor {
328fa9e4066Sahrens 	/* This structure is opaque! */
329fa9e4066Sahrens 	objset_t *zc_objset;
33087e5029aSahrens 	struct zap *zc_zap;
33187e5029aSahrens 	struct zap_leaf *zc_leaf;
332fa9e4066Sahrens 	uint64_t zc_zapobj;
333b24ab676SJeff Bonwick 	uint64_t zc_serialized;
334fa9e4066Sahrens 	uint64_t zc_hash;
335fa9e4066Sahrens 	uint32_t zc_cd;
336fa9e4066Sahrens } zap_cursor_t;
337fa9e4066Sahrens 
338fa9e4066Sahrens typedef struct {
339fa9e4066Sahrens 	int za_integer_length;
340da6c28aaSamw 	/*
341da6c28aaSamw 	 * za_normalization_conflict will be set if there are additional
342da6c28aaSamw 	 * entries with this normalized form (eg, "foo" and "Foo").
343da6c28aaSamw 	 */
344da6c28aaSamw 	boolean_t za_normalization_conflict;
345fa9e4066Sahrens 	uint64_t za_num_integers;
346fa9e4066Sahrens 	uint64_t za_first_integer;	/* no sign extension for <8byte ints */
347fa9e4066Sahrens 	char za_name[MAXNAMELEN];
348fa9e4066Sahrens } zap_attribute_t;
349fa9e4066Sahrens 
350fa9e4066Sahrens /*
351fa9e4066Sahrens  * The interface for listing all the attributes of a zapobj can be
352fa9e4066Sahrens  * thought of as cursor moving down a list of the attributes one by
353fa9e4066Sahrens  * one.  The cookie returned by the zap_cursor_serialize routine is
354fa9e4066Sahrens  * persistent across system calls (and across reboot, even).
355fa9e4066Sahrens  */
356fa9e4066Sahrens 
357fa9e4066Sahrens /*
358fa9e4066Sahrens  * Initialize a zap cursor, pointing to the "first" attribute of the
35987e5029aSahrens  * zapobj.  You must _fini the cursor when you are done with it.
360fa9e4066Sahrens  */
361fa9e4066Sahrens void zap_cursor_init(zap_cursor_t *zc, objset_t *ds, uint64_t zapobj);
36287e5029aSahrens void zap_cursor_fini(zap_cursor_t *zc);
363fa9e4066Sahrens 
364fa9e4066Sahrens /*
365fa9e4066Sahrens  * Get the attribute currently pointed to by the cursor.  Returns
366fa9e4066Sahrens  * ENOENT if at the end of the attributes.
367fa9e4066Sahrens  */
368fa9e4066Sahrens int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za);
369fa9e4066Sahrens 
370fa9e4066Sahrens /*
371fa9e4066Sahrens  * Advance the cursor to the next attribute.
372fa9e4066Sahrens  */
373fa9e4066Sahrens void zap_cursor_advance(zap_cursor_t *zc);
374fa9e4066Sahrens 
375fa9e4066Sahrens /*
376fa9e4066Sahrens  * Get a persistent cookie pointing to the current position of the zap
377fa9e4066Sahrens  * cursor.  The low 4 bits in the cookie are always zero, and thus can
378fa9e4066Sahrens  * be used as to differentiate a serialized cookie from a different type
379fa9e4066Sahrens  * of value.  The cookie will be less than 2^32 as long as there are
380fa9e4066Sahrens  * fewer than 2^22 (4.2 million) entries in the zap object.
381fa9e4066Sahrens  */
382fa9e4066Sahrens uint64_t zap_cursor_serialize(zap_cursor_t *zc);
383fa9e4066Sahrens 
384fa9e4066Sahrens /*
385fa9e4066Sahrens  * Initialize a zap cursor pointing to the position recorded by
386fa9e4066Sahrens  * zap_cursor_serialize (in the "serialized" argument).  You can also
387fa9e4066Sahrens  * use a "serialized" argument of 0 to start at the beginning of the
388fa9e4066Sahrens  * zapobj (ie.  zap_cursor_init_serialized(..., 0) is equivalent to
389fa9e4066Sahrens  * zap_cursor_init(...).)
390fa9e4066Sahrens  */
391fa9e4066Sahrens void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds,
392fa9e4066Sahrens     uint64_t zapobj, uint64_t serialized);
393fa9e4066Sahrens 
394fa9e4066Sahrens 
395fa9e4066Sahrens #define	ZAP_HISTOGRAM_SIZE 10
396fa9e4066Sahrens 
397fa9e4066Sahrens typedef struct zap_stats {
398fa9e4066Sahrens 	/*
399fa9e4066Sahrens 	 * Size of the pointer table (in number of entries).
400fa9e4066Sahrens 	 * This is always a power of 2, or zero if it's a microzap.
401fa9e4066Sahrens 	 * In general, it should be considerably greater than zs_num_leafs.
402fa9e4066Sahrens 	 */
403fa9e4066Sahrens 	uint64_t zs_ptrtbl_len;
404fa9e4066Sahrens 
405fa9e4066Sahrens 	uint64_t zs_blocksize;		/* size of zap blocks */
406fa9e4066Sahrens 
407fa9e4066Sahrens 	/*
408fa9e4066Sahrens 	 * The number of blocks used.  Note that some blocks may be
409fa9e4066Sahrens 	 * wasted because old ptrtbl's and large name/value blocks are
410fa9e4066Sahrens 	 * not reused.  (Although their space is reclaimed, we don't
411fa9e4066Sahrens 	 * reuse those offsets in the object.)
412fa9e4066Sahrens 	 */
413fa9e4066Sahrens 	uint64_t zs_num_blocks;
414fa9e4066Sahrens 
4158248818dSnd 	/*
4168248818dSnd 	 * Pointer table values from zap_ptrtbl in the zap_phys_t
4178248818dSnd 	 */
4188248818dSnd 	uint64_t zs_ptrtbl_nextblk;	  /* next (larger) copy start block */
4198248818dSnd 	uint64_t zs_ptrtbl_blks_copied;   /* number source blocks copied */
4208248818dSnd 	uint64_t zs_ptrtbl_zt_blk;	  /* starting block number */
4218248818dSnd 	uint64_t zs_ptrtbl_zt_numblks;    /* number of blocks */
4228248818dSnd 	uint64_t zs_ptrtbl_zt_shift;	  /* bits to index it */
4238248818dSnd 
4248248818dSnd 	/*
4258248818dSnd 	 * Values of the other members of the zap_phys_t
4268248818dSnd 	 */
4278248818dSnd 	uint64_t zs_block_type;		/* ZBT_HEADER */
4288248818dSnd 	uint64_t zs_magic;		/* ZAP_MAGIC */
4298248818dSnd 	uint64_t zs_num_leafs;		/* The number of leaf blocks */
4308248818dSnd 	uint64_t zs_num_entries;	/* The number of zap entries */
4318248818dSnd 	uint64_t zs_salt;		/* salt to stir into hash function */
4328248818dSnd 
433fa9e4066Sahrens 	/*
434fa9e4066Sahrens 	 * Histograms.  For all histograms, the last index
435fa9e4066Sahrens 	 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater
436fa9e4066Sahrens 	 * than what can be represented.  For example
437fa9e4066Sahrens 	 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number
438fa9e4066Sahrens 	 * of leafs with more than 45 entries.
439fa9e4066Sahrens 	 */
440fa9e4066Sahrens 
441fa9e4066Sahrens 	/*
442fa9e4066Sahrens 	 * zs_leafs_with_n_pointers[n] is the number of leafs with
443fa9e4066Sahrens 	 * 2^n pointers to it.
444fa9e4066Sahrens 	 */
445fa9e4066Sahrens 	uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE];
446fa9e4066Sahrens 
447fa9e4066Sahrens 	/*
448fa9e4066Sahrens 	 * zs_leafs_with_n_entries[n] is the number of leafs with
449fa9e4066Sahrens 	 * [n*5, (n+1)*5) entries.  In the current implementation, there
450fa9e4066Sahrens 	 * can be at most 55 entries in any block, but there may be
451fa9e4066Sahrens 	 * fewer if the name or value is large, or the block is not
452fa9e4066Sahrens 	 * completely full.
453fa9e4066Sahrens 	 */
454fa9e4066Sahrens 	uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE];
455fa9e4066Sahrens 
456fa9e4066Sahrens 	/*
457fa9e4066Sahrens 	 * zs_leafs_n_tenths_full[n] is the number of leafs whose
458fa9e4066Sahrens 	 * fullness is in the range [n/10, (n+1)/10).
459fa9e4066Sahrens 	 */
460fa9e4066Sahrens 	uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE];
461fa9e4066Sahrens 
462fa9e4066Sahrens 	/*
463fa9e4066Sahrens 	 * zs_entries_using_n_chunks[n] is the number of entries which
464fa9e4066Sahrens 	 * consume n 24-byte chunks.  (Note, large names/values only use
465fa9e4066Sahrens 	 * one chunk, but contribute to zs_num_blocks_large.)
466fa9e4066Sahrens 	 */
467fa9e4066Sahrens 	uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE];
468fa9e4066Sahrens 
469fa9e4066Sahrens 	/*
470fa9e4066Sahrens 	 * zs_buckets_with_n_entries[n] is the number of buckets (each
471fa9e4066Sahrens 	 * leaf has 64 buckets) with n entries.
472fa9e4066Sahrens 	 * zs_buckets_with_n_entries[1] should be very close to
473fa9e4066Sahrens 	 * zs_num_entries.
474fa9e4066Sahrens 	 */
475fa9e4066Sahrens 	uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE];
476fa9e4066Sahrens } zap_stats_t;
477fa9e4066Sahrens 
478fa9e4066Sahrens /*
479fa9e4066Sahrens  * Get statistics about a ZAP object.  Note: you need to be aware of the
480fa9e4066Sahrens  * internal implementation of the ZAP to correctly interpret some of the
481fa9e4066Sahrens  * statistics.  This interface shouldn't be relied on unless you really
482fa9e4066Sahrens  * know what you're doing.
483fa9e4066Sahrens  */
484fa9e4066Sahrens int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs);
485fa9e4066Sahrens 
486fa9e4066Sahrens #ifdef	__cplusplus
487fa9e4066Sahrens }
488fa9e4066Sahrens #endif
489fa9e4066Sahrens 
4908248818dSnd #endif	/* _SYS_ZAP_H */
491