xref: /titanic_50/usr/src/uts/common/fs/zfs/sys/zap.h (revision 39b934fce0e8cf6fbca646adbc101efba60943f0)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_ZAP_H
27 #define	_SYS_ZAP_H
28 
29 /*
30  * ZAP - ZFS Attribute Processor
31  *
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).
35  *
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.
39  *
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.
49  *
50  * The ZAP routines are thread-safe.  However, you must observe the
51  * DMU's restriction that a transaction may not be operated on
52  * concurrently.
53  *
54  * Any of the routines that return an int may return an I/O error (EIO
55  * or ECHECKSUM).
56  *
57  *
58  * Implementation / Performance Notes:
59  *
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.
64  *
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).
72  */
73 
74 /*
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.
80  */
81 
82 #include <sys/dmu.h>
83 
84 #ifdef	__cplusplus
85 extern "C" {
86 #endif
87 
88 /*
89  * The matchtype specifies which entry will be accessed.
90  * MT_EXACT: only find an exact match (non-normalized)
91  * MT_FIRST: find the "first" normalized (case and Unicode
92  *     form) match; the designated "first" match will not change as long
93  *     as the set of entries with this normalization doesn't change
94  * MT_BEST: if there is an exact match, find that, otherwise find the
95  *     first normalized match
96  */
97 typedef enum matchtype
98 {
99 	MT_EXACT,
100 	MT_BEST,
101 	MT_FIRST
102 } matchtype_t;
103 
104 /*
105  * Create a new zapobj with no attributes and return its object number.
106  * MT_EXACT will cause the zap object to only support MT_EXACT lookups,
107  * otherwise any matchtype can be used for lookups.
108  *
109  * normflags specifies what normalization will be done.  values are:
110  * 0: no normalization (legacy on-disk format, supports MT_EXACT matching
111  *     only)
112  * U8_TEXTPREP_TOLOWER: case normalization will be performed.
113  *     MT_FIRST/MT_BEST matching will find entries that match without
114  *     regard to case (eg. looking for "foo" can find an entry "Foo").
115  * Eventually, other flags will permit unicode normalization as well.
116  */
117 uint64_t zap_create(objset_t *ds, dmu_object_type_t ot,
118     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
119 uint64_t zap_create_norm(objset_t *ds, int normflags, dmu_object_type_t ot,
120     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
121 
122 /*
123  * Create a new zapobj with no attributes from the given (unallocated)
124  * object number.
125  */
126 int zap_create_claim(objset_t *ds, uint64_t obj, dmu_object_type_t ot,
127     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
128 int zap_create_claim_norm(objset_t *ds, uint64_t obj,
129     int normflags, dmu_object_type_t ot,
130     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
131 
132 /*
133  * The zapobj passed in must be a valid ZAP object for all of the
134  * following routines.
135  */
136 
137 /*
138  * Destroy this zapobj and all its attributes.
139  *
140  * Frees the object number using dmu_object_free.
141  */
142 int zap_destroy(objset_t *ds, uint64_t zapobj, dmu_tx_t *tx);
143 
144 /*
145  * Manipulate attributes.
146  *
147  * 'integer_size' is in bytes, and must be 1, 2, 4, or 8.
148  */
149 
150 /*
151  * Retrieve the contents of the attribute with the given name.
152  *
153  * If the requested attribute does not exist, the call will fail and
154  * return ENOENT.
155  *
156  * If 'integer_size' is smaller than the attribute's integer size, the
157  * call will fail and return EINVAL.
158  *
159  * If 'integer_size' is equal to or larger than the attribute's integer
160  * size, the call will succeed and return 0.  * When converting to a
161  * larger integer size, the integers will be treated as unsigned (ie. no
162  * sign-extension will be performed).
163  *
164  * 'num_integers' is the length (in integers) of 'buf'.
165  *
166  * If the attribute is longer than the buffer, as many integers as will
167  * fit will be transferred to 'buf'.  If the entire attribute was not
168  * transferred, the call will return EOVERFLOW.
169  *
170  * If rn_len is nonzero, realname will be set to the name of the found
171  * entry (which may be different from the requested name if matchtype is
172  * not MT_EXACT).
173  *
174  * If normalization_conflictp is not NULL, it will be set if there is
175  * another name with the same case/unicode normalized form.
176  */
177 int zap_lookup(objset_t *ds, uint64_t zapobj, const char *name,
178     uint64_t integer_size, uint64_t num_integers, void *buf);
179 int zap_lookup_norm(objset_t *ds, uint64_t zapobj, const char *name,
180     uint64_t integer_size, uint64_t num_integers, void *buf,
181     matchtype_t mt, char *realname, int rn_len,
182     boolean_t *normalization_conflictp);
183 
184 int zap_count_write(objset_t *os, uint64_t zapobj, const char *name,
185     int add, uint64_t *towrite, uint64_t *tooverwrite,
186     uint64_t dn_datablkshift);
187 
188 /*
189  * Create an attribute with the given name and value.
190  *
191  * If an attribute with the given name already exists, the call will
192  * fail and return EEXIST.
193  */
194 int zap_add(objset_t *ds, uint64_t zapobj, const char *name,
195     int integer_size, uint64_t num_integers,
196     const void *val, dmu_tx_t *tx);
197 
198 /*
199  * Set the attribute with the given name to the given value.  If an
200  * attribute with the given name does not exist, it will be created.  If
201  * an attribute with the given name already exists, the previous value
202  * will be overwritten.  The integer_size may be different from the
203  * existing attribute's integer size, in which case the attribute's
204  * integer size will be updated to the new value.
205  */
206 int zap_update(objset_t *ds, uint64_t zapobj, const char *name,
207     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
208 
209 /*
210  * Get the length (in integers) and the integer size of the specified
211  * attribute.
212  *
213  * If the requested attribute does not exist, the call will fail and
214  * return ENOENT.
215  */
216 int zap_length(objset_t *ds, uint64_t zapobj, const char *name,
217     uint64_t *integer_size, uint64_t *num_integers);
218 
219 /*
220  * Remove the specified attribute.
221  *
222  * If the specified attribute does not exist, the call will fail and
223  * return ENOENT.
224  */
225 int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx);
226 int zap_remove_norm(objset_t *ds, uint64_t zapobj, const char *name,
227     matchtype_t mt, dmu_tx_t *tx);
228 
229 /*
230  * Returns (in *count) the number of attributes in the specified zap
231  * object.
232  */
233 int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count);
234 
235 
236 /*
237  * Returns (in name) the name of the entry whose (value & mask)
238  * (za_first_integer) is value, or ENOENT if not found.  The string
239  * pointed to by name must be at least 256 bytes long.  If mask==0, the
240  * match must be exact (ie, same as mask=-1ULL).
241  */
242 int zap_value_search(objset_t *os, uint64_t zapobj,
243     uint64_t value, uint64_t mask, char *name);
244 
245 /*
246  * Transfer all the entries from fromobj into intoobj.  Only works on
247  * int_size=8 num_integers=1 values.  Fails if there are any duplicated
248  * entries.
249  */
250 int zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx);
251 
252 /*
253  * Manipulate entries where the name + value are the "same" (the name is
254  * a stringified version of the value).
255  */
256 int zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
257 int zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx);
258 int zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value);
259 
260 struct zap;
261 struct zap_leaf;
262 typedef struct zap_cursor {
263 	/* This structure is opaque! */
264 	objset_t *zc_objset;
265 	struct zap *zc_zap;
266 	struct zap_leaf *zc_leaf;
267 	uint64_t zc_zapobj;
268 	uint64_t zc_hash;
269 	uint32_t zc_cd;
270 } zap_cursor_t;
271 
272 typedef struct {
273 	int za_integer_length;
274 	/*
275 	 * za_normalization_conflict will be set if there are additional
276 	 * entries with this normalized form (eg, "foo" and "Foo").
277 	 */
278 	boolean_t za_normalization_conflict;
279 	uint64_t za_num_integers;
280 	uint64_t za_first_integer;	/* no sign extension for <8byte ints */
281 	char za_name[MAXNAMELEN];
282 } zap_attribute_t;
283 
284 /*
285  * The interface for listing all the attributes of a zapobj can be
286  * thought of as cursor moving down a list of the attributes one by
287  * one.  The cookie returned by the zap_cursor_serialize routine is
288  * persistent across system calls (and across reboot, even).
289  */
290 
291 /*
292  * Initialize a zap cursor, pointing to the "first" attribute of the
293  * zapobj.  You must _fini the cursor when you are done with it.
294  */
295 void zap_cursor_init(zap_cursor_t *zc, objset_t *ds, uint64_t zapobj);
296 void zap_cursor_fini(zap_cursor_t *zc);
297 
298 /*
299  * Get the attribute currently pointed to by the cursor.  Returns
300  * ENOENT if at the end of the attributes.
301  */
302 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za);
303 
304 /*
305  * Advance the cursor to the next attribute.
306  */
307 void zap_cursor_advance(zap_cursor_t *zc);
308 
309 /*
310  * Get a persistent cookie pointing to the current position of the zap
311  * cursor.  The low 4 bits in the cookie are always zero, and thus can
312  * be used as to differentiate a serialized cookie from a different type
313  * of value.  The cookie will be less than 2^32 as long as there are
314  * fewer than 2^22 (4.2 million) entries in the zap object.
315  */
316 uint64_t zap_cursor_serialize(zap_cursor_t *zc);
317 
318 /*
319  * Initialize a zap cursor pointing to the position recorded by
320  * zap_cursor_serialize (in the "serialized" argument).  You can also
321  * use a "serialized" argument of 0 to start at the beginning of the
322  * zapobj (ie.  zap_cursor_init_serialized(..., 0) is equivalent to
323  * zap_cursor_init(...).)
324  */
325 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds,
326     uint64_t zapobj, uint64_t serialized);
327 
328 
329 #define	ZAP_HISTOGRAM_SIZE 10
330 
331 typedef struct zap_stats {
332 	/*
333 	 * Size of the pointer table (in number of entries).
334 	 * This is always a power of 2, or zero if it's a microzap.
335 	 * In general, it should be considerably greater than zs_num_leafs.
336 	 */
337 	uint64_t zs_ptrtbl_len;
338 
339 	uint64_t zs_blocksize;		/* size of zap blocks */
340 
341 	/*
342 	 * The number of blocks used.  Note that some blocks may be
343 	 * wasted because old ptrtbl's and large name/value blocks are
344 	 * not reused.  (Although their space is reclaimed, we don't
345 	 * reuse those offsets in the object.)
346 	 */
347 	uint64_t zs_num_blocks;
348 
349 	/*
350 	 * Pointer table values from zap_ptrtbl in the zap_phys_t
351 	 */
352 	uint64_t zs_ptrtbl_nextblk;	  /* next (larger) copy start block */
353 	uint64_t zs_ptrtbl_blks_copied;   /* number source blocks copied */
354 	uint64_t zs_ptrtbl_zt_blk;	  /* starting block number */
355 	uint64_t zs_ptrtbl_zt_numblks;    /* number of blocks */
356 	uint64_t zs_ptrtbl_zt_shift;	  /* bits to index it */
357 
358 	/*
359 	 * Values of the other members of the zap_phys_t
360 	 */
361 	uint64_t zs_block_type;		/* ZBT_HEADER */
362 	uint64_t zs_magic;		/* ZAP_MAGIC */
363 	uint64_t zs_num_leafs;		/* The number of leaf blocks */
364 	uint64_t zs_num_entries;	/* The number of zap entries */
365 	uint64_t zs_salt;		/* salt to stir into hash function */
366 
367 	/*
368 	 * Histograms.  For all histograms, the last index
369 	 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater
370 	 * than what can be represented.  For example
371 	 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number
372 	 * of leafs with more than 45 entries.
373 	 */
374 
375 	/*
376 	 * zs_leafs_with_n_pointers[n] is the number of leafs with
377 	 * 2^n pointers to it.
378 	 */
379 	uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE];
380 
381 	/*
382 	 * zs_leafs_with_n_entries[n] is the number of leafs with
383 	 * [n*5, (n+1)*5) entries.  In the current implementation, there
384 	 * can be at most 55 entries in any block, but there may be
385 	 * fewer if the name or value is large, or the block is not
386 	 * completely full.
387 	 */
388 	uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE];
389 
390 	/*
391 	 * zs_leafs_n_tenths_full[n] is the number of leafs whose
392 	 * fullness is in the range [n/10, (n+1)/10).
393 	 */
394 	uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE];
395 
396 	/*
397 	 * zs_entries_using_n_chunks[n] is the number of entries which
398 	 * consume n 24-byte chunks.  (Note, large names/values only use
399 	 * one chunk, but contribute to zs_num_blocks_large.)
400 	 */
401 	uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE];
402 
403 	/*
404 	 * zs_buckets_with_n_entries[n] is the number of buckets (each
405 	 * leaf has 64 buckets) with n entries.
406 	 * zs_buckets_with_n_entries[1] should be very close to
407 	 * zs_num_entries.
408 	 */
409 	uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE];
410 } zap_stats_t;
411 
412 /*
413  * Get statistics about a ZAP object.  Note: you need to be aware of the
414  * internal implementation of the ZAP to correctly interpret some of the
415  * statistics.  This interface shouldn't be relied on unless you really
416  * know what you're doing.
417  */
418 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs);
419 
420 #ifdef	__cplusplus
421 }
422 #endif
423 
424 #endif	/* _SYS_ZAP_H */
425