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