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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30 * ZAP - ZFS Attribute Processor
32 * The ZAP is a module which sits on top of the DMU (Data Management
33 * Unit) and implements a higher-level storage primitive using DMU
34 * objects. Its primary consumer is the ZPL (ZFS Posix Layer).
36 * A "zapobj" is a DMU object which the ZAP uses to stores attributes.
37 * Users should use only zap routines to access a zapobj - they should
38 * not access the DMU object directly using DMU routines.
40 * The attributes stored in a zapobj are name-value pairs. The name is
41 * a zero-terminated string of up to ZAP_MAXNAMELEN bytes (including
42 * terminating NULL). The value is an array of integers, which may be
43 * 1, 2, 4, or 8 bytes long. The total space used by the array (number
44 * of integers * integer length) can be up to ZAP_MAXVALUELEN bytes.
45 * Note that an 8-byte integer value can be used to store the location
46 * (object number) of another dmu object (which may be itself a zapobj).
47 * Note that you can use a zero-length attribute to store a single bit
48 * of information - the attribute is present or not.
50 * The ZAP routines are thread-safe. However, you must observe the
51 * DMU's restriction that a transaction may not be operated on
54 * Any of the routines that return an int may return an I/O error (EIO
58 * Implementation / Performance Notes:
60 * The ZAP is intended to operate most efficiently on attributes with
61 * short (49 bytes or less) names and single 8-byte values, for which
62 * the microzap will be used. The ZAP should be efficient enough so
63 * that the user does not need to cache these attributes.
65 * The ZAP's locking scheme makes its routines thread-safe. Operations
66 * on different zapobjs will be processed concurrently. Operations on
67 * the same zapobj which only read data will be processed concurrently.
68 * Operations on the same zapobj which modify data will be processed
69 * concurrently when there are many attributes in the zapobj (because
70 * the ZAP uses per-block locking - more than 128 * (number of cpus)
71 * small attributes will suffice).
75 * We're using zero-terminated byte strings (ie. ASCII or UTF-8 C
76 * strings) for the names of attributes, rather than a byte string
77 * bounded by an explicit length. If some day we want to support names
78 * in character sets which have embedded zeros (eg. UTF-16, UTF-32),
79 * we'll have to add routines for using length-bounded strings.
83 #include <sys/refcount.h>
90 * Specifies matching criteria for ZAP lookups.
92 typedef enum matchtype
94 /* Only find an exact match (non-normalized) */
97 * If there is an exact match, find that, otherwise find the
98 * first normalized match.
102 * Find the "first" normalized (case and Unicode form) match;
103 * the designated "first" match will not change as long as the
104 * set of entries with this normalization doesn't change.
109 typedef enum zap_flags {
110 /* Use 64-bit hash value (serialized cursors will always use 64-bits) */
111 ZAP_FLAG_HASH64 = 1 << 0,
112 /* Key is binary, not string (zap_add_uint64() can be used) */
113 ZAP_FLAG_UINT64_KEY = 1 << 1,
115 * First word of key (which must be an array of uint64) is
116 * already randomly distributed.
118 ZAP_FLAG_PRE_HASHED_KEY = 1 << 2,
122 * Create a new zapobj with no attributes and return its object number.
123 * MT_EXACT will cause the zap object to only support MT_EXACT lookups,
124 * otherwise any matchtype can be used for lookups.
126 * normflags specifies what normalization will be done. values are:
127 * 0: no normalization (legacy on-disk format, supports MT_EXACT matching
129 * U8_TEXTPREP_TOLOWER: case normalization will be performed.
130 * MT_FIRST/MT_BEST matching will find entries that match without
131 * regard to case (eg. looking for "foo" can find an entry "Foo").
132 * Eventually, other flags will permit unicode normalization as well.
134 uint64_t zap_create(objset_t *ds, dmu_object_type_t ot,
135 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
136 uint64_t zap_create_norm(objset_t *ds, int normflags, dmu_object_type_t ot,
137 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
138 uint64_t zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
139 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
140 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
141 uint64_t zap_create_link(objset_t *os, dmu_object_type_t ot,
142 uint64_t parent_obj, const char *name, dmu_tx_t *tx);
145 * Initialize an already-allocated object.
147 void mzap_create_impl(objset_t *os, uint64_t obj, int normflags,
148 zap_flags_t flags, dmu_tx_t *tx);
151 * Create a new zapobj with no attributes from the given (unallocated)
154 int zap_create_claim(objset_t *ds, uint64_t obj, dmu_object_type_t ot,
155 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
156 int zap_create_claim_norm(objset_t *ds, uint64_t obj,
157 int normflags, dmu_object_type_t ot,
158 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
161 * The zapobj passed in must be a valid ZAP object for all of the
162 * following routines.
166 * Destroy this zapobj and all its attributes.
168 * Frees the object number using dmu_object_free.
170 int zap_destroy(objset_t *ds, uint64_t zapobj, dmu_tx_t *tx);
173 * Manipulate attributes.
175 * 'integer_size' is in bytes, and must be 1, 2, 4, or 8.
179 * Retrieve the contents of the attribute with the given name.
181 * If the requested attribute does not exist, the call will fail and
184 * If 'integer_size' is smaller than the attribute's integer size, the
185 * call will fail and return EINVAL.
187 * If 'integer_size' is equal to or larger than the attribute's integer
188 * size, the call will succeed and return 0.
190 * When converting to a larger integer size, the integers will be treated as
191 * unsigned (ie. no sign-extension will be performed).
193 * 'num_integers' is the length (in integers) of 'buf'.
195 * If the attribute is longer than the buffer, as many integers as will
196 * fit will be transferred to 'buf'. If the entire attribute was not
197 * transferred, the call will return EOVERFLOW.
199 int zap_lookup(objset_t *ds, uint64_t zapobj, const char *name,
200 uint64_t integer_size, uint64_t num_integers, void *buf);
203 * If rn_len is nonzero, realname will be set to the name of the found
204 * entry (which may be different from the requested name if matchtype is
207 * If normalization_conflictp is not NULL, it will be set if there is
208 * another name with the same case/unicode normalized form.
210 int zap_lookup_norm(objset_t *ds, uint64_t zapobj, const char *name,
211 uint64_t integer_size, uint64_t num_integers, void *buf,
212 matchtype_t mt, char *realname, int rn_len,
213 boolean_t *normalization_conflictp);
214 int zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
215 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf);
216 int zap_contains(objset_t *ds, uint64_t zapobj, const char *name);
217 int zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
219 int zap_lookup_by_dnode(dnode_t *dn, const char *name,
220 uint64_t integer_size, uint64_t num_integers, void *buf);
221 int zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
222 uint64_t integer_size, uint64_t num_integers, void *buf,
223 matchtype_t mt, char *realname, int rn_len,
226 int zap_count_write_by_dnode(dnode_t *dn, const char *name,
227 int add, refcount_t *towrite, refcount_t *tooverwrite);
230 * Create an attribute with the given name and value.
232 * If an attribute with the given name already exists, the call will
233 * fail and return EEXIST.
235 int zap_add(objset_t *ds, uint64_t zapobj, const char *key,
236 int integer_size, uint64_t num_integers,
237 const void *val, dmu_tx_t *tx);
238 int zap_add_uint64(objset_t *ds, uint64_t zapobj, const uint64_t *key,
239 int key_numints, int integer_size, uint64_t num_integers,
240 const void *val, dmu_tx_t *tx);
243 * Set the attribute with the given name to the given value. If an
244 * attribute with the given name does not exist, it will be created. If
245 * an attribute with the given name already exists, the previous value
246 * will be overwritten. The integer_size may be different from the
247 * existing attribute's integer size, in which case the attribute's
248 * integer size will be updated to the new value.
250 int zap_update(objset_t *ds, uint64_t zapobj, const char *name,
251 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
252 int zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
254 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
257 * Get the length (in integers) and the integer size of the specified
260 * If the requested attribute does not exist, the call will fail and
263 int zap_length(objset_t *ds, uint64_t zapobj, const char *name,
264 uint64_t *integer_size, uint64_t *num_integers);
265 int zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
266 int key_numints, uint64_t *integer_size, uint64_t *num_integers);
269 * Remove the specified attribute.
271 * If the specified attribute does not exist, the call will fail and
274 int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx);
275 int zap_remove_norm(objset_t *ds, uint64_t zapobj, const char *name,
276 matchtype_t mt, dmu_tx_t *tx);
277 int zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
278 int key_numints, dmu_tx_t *tx);
281 * Returns (in *count) the number of attributes in the specified zap
284 int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count);
287 * Returns (in name) the name of the entry whose (value & mask)
288 * (za_first_integer) is value, or ENOENT if not found. The string
289 * pointed to by name must be at least 256 bytes long. If mask==0, the
290 * match must be exact (ie, same as mask=-1ULL).
292 int zap_value_search(objset_t *os, uint64_t zapobj,
293 uint64_t value, uint64_t mask, char *name);
296 * Transfer all the entries from fromobj into intoobj. Only works on
297 * int_size=8 num_integers=1 values. Fails if there are any duplicated
300 int zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx);
302 /* Same as zap_join, but set the values to 'value'. */
303 int zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
304 uint64_t value, dmu_tx_t *tx);
306 /* Same as zap_join, but add together any duplicated entries. */
307 int zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
311 * Manipulate entries where the name + value are the "same" (the name is
312 * a stringified version of the value).
314 int zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
315 int zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
316 int zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value);
317 int zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
320 /* Here the key is an int and the value is a different int. */
321 int zap_add_int_key(objset_t *os, uint64_t obj,
322 uint64_t key, uint64_t value, dmu_tx_t *tx);
323 int zap_update_int_key(objset_t *os, uint64_t obj,
324 uint64_t key, uint64_t value, dmu_tx_t *tx);
325 int zap_lookup_int_key(objset_t *os, uint64_t obj,
326 uint64_t key, uint64_t *valuep);
328 int zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
333 typedef struct zap_cursor {
334 /* This structure is opaque! */
337 struct zap_leaf *zc_leaf;
339 uint64_t zc_serialized;
345 int za_integer_length;
347 * za_normalization_conflict will be set if there are additional
348 * entries with this normalized form (eg, "foo" and "Foo").
350 boolean_t za_normalization_conflict;
351 uint64_t za_num_integers;
352 uint64_t za_first_integer; /* no sign extension for <8byte ints */
353 char za_name[ZAP_MAXNAMELEN];
357 * The interface for listing all the attributes of a zapobj can be
358 * thought of as cursor moving down a list of the attributes one by
359 * one. The cookie returned by the zap_cursor_serialize routine is
360 * persistent across system calls (and across reboot, even).
364 * Initialize a zap cursor, pointing to the "first" attribute of the
365 * zapobj. You must _fini the cursor when you are done with it.
367 void zap_cursor_init(zap_cursor_t *zc, objset_t *ds, uint64_t zapobj);
368 void zap_cursor_fini(zap_cursor_t *zc);
371 * Get the attribute currently pointed to by the cursor. Returns
372 * ENOENT if at the end of the attributes.
374 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za);
377 * Advance the cursor to the next attribute.
379 void zap_cursor_advance(zap_cursor_t *zc);
382 * Get a persistent cookie pointing to the current position of the zap
383 * cursor. The low 4 bits in the cookie are always zero, and thus can
384 * be used as to differentiate a serialized cookie from a different type
385 * of value. The cookie will be less than 2^32 as long as there are
386 * fewer than 2^22 (4.2 million) entries in the zap object.
388 uint64_t zap_cursor_serialize(zap_cursor_t *zc);
391 * Advance the cursor to the attribute having the given key.
393 int zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt);
396 * Initialize a zap cursor pointing to the position recorded by
397 * zap_cursor_serialize (in the "serialized" argument). You can also
398 * use a "serialized" argument of 0 to start at the beginning of the
399 * zapobj (ie. zap_cursor_init_serialized(..., 0) is equivalent to
400 * zap_cursor_init(...).)
402 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds,
403 uint64_t zapobj, uint64_t serialized);
406 #define ZAP_HISTOGRAM_SIZE 10
408 typedef struct zap_stats {
410 * Size of the pointer table (in number of entries).
411 * This is always a power of 2, or zero if it's a microzap.
412 * In general, it should be considerably greater than zs_num_leafs.
414 uint64_t zs_ptrtbl_len;
416 uint64_t zs_blocksize; /* size of zap blocks */
419 * The number of blocks used. Note that some blocks may be
420 * wasted because old ptrtbl's and large name/value blocks are
421 * not reused. (Although their space is reclaimed, we don't
422 * reuse those offsets in the object.)
424 uint64_t zs_num_blocks;
427 * Pointer table values from zap_ptrtbl in the zap_phys_t
429 uint64_t zs_ptrtbl_nextblk; /* next (larger) copy start block */
430 uint64_t zs_ptrtbl_blks_copied; /* number source blocks copied */
431 uint64_t zs_ptrtbl_zt_blk; /* starting block number */
432 uint64_t zs_ptrtbl_zt_numblks; /* number of blocks */
433 uint64_t zs_ptrtbl_zt_shift; /* bits to index it */
436 * Values of the other members of the zap_phys_t
438 uint64_t zs_block_type; /* ZBT_HEADER */
439 uint64_t zs_magic; /* ZAP_MAGIC */
440 uint64_t zs_num_leafs; /* The number of leaf blocks */
441 uint64_t zs_num_entries; /* The number of zap entries */
442 uint64_t zs_salt; /* salt to stir into hash function */
445 * Histograms. For all histograms, the last index
446 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater
447 * than what can be represented. For example
448 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number
449 * of leafs with more than 45 entries.
453 * zs_leafs_with_n_pointers[n] is the number of leafs with
454 * 2^n pointers to it.
456 uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE];
459 * zs_leafs_with_n_entries[n] is the number of leafs with
460 * [n*5, (n+1)*5) entries. In the current implementation, there
461 * can be at most 55 entries in any block, but there may be
462 * fewer if the name or value is large, or the block is not
465 uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE];
468 * zs_leafs_n_tenths_full[n] is the number of leafs whose
469 * fullness is in the range [n/10, (n+1)/10).
471 uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE];
474 * zs_entries_using_n_chunks[n] is the number of entries which
475 * consume n 24-byte chunks. (Note, large names/values only use
476 * one chunk, but contribute to zs_num_blocks_large.)
478 uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE];
481 * zs_buckets_with_n_entries[n] is the number of buckets (each
482 * leaf has 64 buckets) with n entries.
483 * zs_buckets_with_n_entries[1] should be very close to
486 uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE];
490 * Get statistics about a ZAP object. Note: you need to be aware of the
491 * internal implementation of the ZAP to correctly interpret some of the
492 * statistics. This interface shouldn't be relied on unless you really
493 * know what you're doing.
495 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs);
501 #endif /* _SYS_ZAP_H */