xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zap.h (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_ZAP_H
28 #define	_SYS_ZAP_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * ZAP - ZFS Attribute Processor
34  *
35  * The ZAP is a module which sits on top of the DMU (Data Managemnt
36  * Unit) and implements a higher-level storage primitive using DMU
37  * objects.  Its primary consumer is the ZPL (ZFS Posix Layer).
38  *
39  * A "zapobj" is a DMU object which the ZAP uses to stores attributes.
40  * Users should use only zap routines to access a zapobj - they should
41  * not access the DMU object directly using DMU routines.
42  *
43  * The attributes stored in a zapobj are name-value pairs.  The name is
44  * a zero-terminated string of up to 256 bytes (including terminating
45  * NULL).  The value is an array of integers (whose length is limited
46  * only by the size of the zapobj).  The integers may be 1, 2, 4, or 8
47  * bytes long.  Note that an 8-byte integer value can be used to store
48  * the location (object number) of another dmu object (which may be
49  * itself a zapobj).  Note that you can use a zero-length attribute to
50  * store a single bit of information - the attribute is present or not.
51  *
52  * The ZAP routines are thread-safe.  However, you must observe the
53  * DMU's restriction that a transaction may not be operated on
54  * concurrently.
55  *
56  * Any of the routines that return an int may return an I/O error (EIO
57  * or ECHECKSUM).
58  *
59  *
60  * Implementation / Performance Notes:
61  *
62  * The ZAP is intended to operate most efficiently on attributes with
63  * short (23 bytes or less) names and short (23 bytes or less) values.
64  * The ZAP should be efficient enough so that the user does not need to
65  * cache these attributes.
66  *
67  * Using extremely long (~256 bytes or more) attribute names or values
68  * values will result in poor performance, due to the memcpy from the
69  * user's buffer into the ZAP object.  This penalty can be avoided by
70  * creating an integer-type attribute to store an object number, and
71  * accessing that object using the DMU directly.
72  *
73  * The ZAP's locking scheme makes its routines thread-safe.  Operations
74  * on different zapobjs will be processed concurrently.  Operations on
75  * the same zapobj which only read data will be processed concurrently.
76  * Operations on the same zapobj which modify data will be processed
77  * concurrently when there are many attributes in the zapobj (because
78  * the ZAP uses per-block locking - more than 32 * (number of cpus)
79  * small attributes will suffice).
80  */
81 
82 /*
83  * We're using zero-terminated byte strings (ie. ASCII or UTF-8 C
84  * strings) for the names of attributes, rather than a byte string
85  * bounded by an explicit length.  If some day we want to support names
86  * in character sets which have embedded zeros (eg. UTF-16, UTF-32),
87  * we'll have to add routines for using length-bounded strings.
88  */
89 
90 #include <sys/dmu.h>
91 
92 #ifdef	__cplusplus
93 extern "C" {
94 #endif
95 
96 /*
97  * Create a new zapobj with no attributes and return its object number.
98  */
99 uint64_t zap_create(objset_t *ds, dmu_object_type_t ot,
100     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
101 
102 /*
103  * Create a new zapobj with no attributes from the given (unallocated)
104  * object number.
105  */
106 int zap_create_claim(objset_t *ds, uint64_t obj, dmu_object_type_t ot,
107     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx);
108 
109 /*
110  * The zapobj passed in must be a valid ZAP object for all of the
111  * following routines.
112  */
113 
114 /*
115  * Destroy this zapobj and all its attributes.
116  *
117  * Frees the object number using dmu_object_free.
118  */
119 int zap_destroy(objset_t *ds, uint64_t zapobj, dmu_tx_t *tx);
120 
121 /*
122  * Manipulate attributes.
123  *
124  * 'integer_size' is in bytes, and must be 1, 2, 4, or 8.
125  */
126 
127 /*
128  * Retrieve the contents of the attribute with the given name.
129  *
130  * If the requested attribute does not exist, the call will fail and
131  * return ENOENT.
132  *
133  * If 'integer_size' is smaller than the attribute's integer size, the
134  * call will fail and return EINVAL.
135  *
136  * If 'integer_size' is equal to or larger than the attribute's integer
137  * size, the call will succeed and return 0.  * When converting to a
138  * larger integer size, the integers will be treated as unsigned (ie. no
139  * sign-extension will be performed).
140  *
141  * 'num_integers' is the length (in integers) of 'buf'.
142  *
143  * If the attribute is longer than the buffer, as many integers as will
144  * fit will be transferred to 'buf'.  If the entire attribute was not
145  * transferred, the call will return EOVERFLOW.
146  */
147 int zap_lookup(objset_t *ds, uint64_t zapobj, const char *name,
148     uint64_t integer_size, uint64_t num_integers, void *buf);
149 
150 /*
151  * Create an attribute with the given name and value.
152  *
153  * If an attribute with the given name already exists, the call will
154  * fail and return EEXIST.
155  */
156 int zap_add(objset_t *ds, uint64_t zapobj, const char *name,
157     int integer_size, uint64_t num_integers,
158     const void *val, dmu_tx_t *tx);
159 
160 /*
161  * Set the attribute with the given name to the given value.  If an
162  * attribute with the given name does not exist, it will be created.  If
163  * an attribute with the given name already exists, the previous value
164  * will be overwritten.  The integer_size may be different from the
165  * existing attribute's integer size, in which case the attribute's
166  * integer size will be updated to the new value.
167  */
168 int zap_update(objset_t *ds, uint64_t zapobj, const char *name,
169     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
170 
171 /*
172  * Get the length (in integers) and the integer size of the specified
173  * attribute.
174  *
175  * If the requested attribute does not exist, the call will fail and
176  * return ENOENT.
177  */
178 int zap_length(objset_t *ds, uint64_t zapobj, const char *name,
179     uint64_t *integer_size, uint64_t *num_integers);
180 
181 /*
182  * Remove the specified attribute.
183  *
184  * If the specified attribute does not exist, the call will fail and
185  * return ENOENT.
186  */
187 int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx);
188 
189 /*
190  * Returns (in *count) the number of attributes in the specified zap
191  * object.
192  */
193 int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count);
194 
195 
196 /*
197  * Returns (in name) the name of the entry whose value
198  * (za_first_integer) is value, or ENOENT if not found.  The string
199  * pointed to by name must be at least 256 bytes long.
200  */
201 int zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, char *name);
202 
203 struct zap;
204 struct zap_leaf;
205 typedef struct zap_cursor {
206 	/* This structure is opaque! */
207 	objset_t *zc_objset;
208 	struct zap *zc_zap;
209 	struct zap_leaf *zc_leaf;
210 	uint64_t zc_zapobj;
211 	uint64_t zc_hash;
212 	uint32_t zc_cd;
213 } zap_cursor_t;
214 
215 typedef struct {
216 	int za_integer_length;
217 	uint64_t za_num_integers;
218 	uint64_t za_first_integer;	/* no sign extension for <8byte ints */
219 	char za_name[MAXNAMELEN];
220 } zap_attribute_t;
221 
222 /*
223  * The interface for listing all the attributes of a zapobj can be
224  * thought of as cursor moving down a list of the attributes one by
225  * one.  The cookie returned by the zap_cursor_serialize routine is
226  * persistent across system calls (and across reboot, even).
227  */
228 
229 /*
230  * Initialize a zap cursor, pointing to the "first" attribute of the
231  * zapobj.  You must _fini the cursor when you are done with it.
232  */
233 void zap_cursor_init(zap_cursor_t *zc, objset_t *ds, uint64_t zapobj);
234 void zap_cursor_fini(zap_cursor_t *zc);
235 
236 /*
237  * Get the attribute currently pointed to by the cursor.  Returns
238  * ENOENT if at the end of the attributes.
239  */
240 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za);
241 
242 /*
243  * Advance the cursor to the next attribute.
244  */
245 void zap_cursor_advance(zap_cursor_t *zc);
246 
247 /*
248  * Get a persistent cookie pointing to the current position of the zap
249  * cursor.  The low 4 bits in the cookie are always zero, and thus can
250  * be used as to differentiate a serialized cookie from a different type
251  * of value.  The cookie will be less than 2^32 as long as there are
252  * fewer than 2^22 (4.2 million) entries in the zap object.
253  */
254 uint64_t zap_cursor_serialize(zap_cursor_t *zc);
255 
256 /*
257  * Initialize a zap cursor pointing to the position recorded by
258  * zap_cursor_serialize (in the "serialized" argument).  You can also
259  * use a "serialized" argument of 0 to start at the beginning of the
260  * zapobj (ie.  zap_cursor_init_serialized(..., 0) is equivalent to
261  * zap_cursor_init(...).)
262  */
263 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds,
264     uint64_t zapobj, uint64_t serialized);
265 
266 
267 #define	ZAP_HISTOGRAM_SIZE 10
268 
269 typedef struct zap_stats {
270 	/*
271 	 * Size of the pointer table (in number of entries).
272 	 * This is always a power of 2, or zero if it's a microzap.
273 	 * In general, it should be considerably greater than zs_num_leafs.
274 	 */
275 	uint64_t zs_ptrtbl_len;
276 
277 	uint64_t zs_blocksize;		/* size of zap blocks */
278 
279 	uint64_t zs_num_leafs;		/* The number of leaf blocks */
280 
281 	uint64_t zs_num_entries;	/* The number of zap entries */
282 
283 	/*
284 	 * The number of blocks used.  Note that some blocks may be
285 	 * wasted because old ptrtbl's and large name/value blocks are
286 	 * not reused.  (Although their space is reclaimed, we don't
287 	 * reuse those offsets in the object.)
288 	 */
289 	uint64_t zs_num_blocks;
290 
291 	/* The number of blocks used for large names or values */
292 	uint64_t zs_num_blocks_large;
293 
294 	/*
295 	 * Histograms.  For all histograms, the last index
296 	 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater
297 	 * than what can be represented.  For example
298 	 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number
299 	 * of leafs with more than 45 entries.
300 	 */
301 
302 	/*
303 	 * zs_leafs_with_n_pointers[n] is the number of leafs with
304 	 * 2^n pointers to it.
305 	 */
306 	uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE];
307 
308 	/*
309 	 * zs_leafs_with_n_chained[n] is the number of leafs with n
310 	 * chained blocks.  zs_leafs_with_n_chained[0] (leafs with no
311 	 * chained blocks) should be very close to zs_num_leafs.
312 	 */
313 	uint64_t zs_leafs_with_n_chained[ZAP_HISTOGRAM_SIZE];
314 
315 	/*
316 	 * zs_leafs_with_n_entries[n] is the number of leafs with
317 	 * [n*5, (n+1)*5) entries.  In the current implementation, there
318 	 * can be at most 55 entries in any block, but there may be
319 	 * fewer if the name or value is large, or the block is not
320 	 * completely full.
321 	 */
322 	uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE];
323 
324 	/*
325 	 * zs_leafs_n_tenths_full[n] is the number of leafs whose
326 	 * fullness is in the range [n/10, (n+1)/10).
327 	 */
328 	uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE];
329 
330 	/*
331 	 * zs_entries_using_n_chunks[n] is the number of entries which
332 	 * consume n 24-byte chunks.  (Note, large names/values only use
333 	 * one chunk, but contribute to zs_num_blocks_large.)
334 	 */
335 	uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE];
336 
337 	/*
338 	 * zs_buckets_with_n_entries[n] is the number of buckets (each
339 	 * leaf has 64 buckets) with n entries.
340 	 * zs_buckets_with_n_entries[1] should be very close to
341 	 * zs_num_entries.
342 	 */
343 	uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE];
344 } zap_stats_t;
345 
346 /*
347  * Get statistics about a ZAP object.  Note: you need to be aware of the
348  * internal implementation of the ZAP to correctly interpret some of the
349  * statistics.  This interface shouldn't be relied on unless you really
350  * know what you're doing.
351  */
352 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs);
353 
354 #ifdef	__cplusplus
355 }
356 #endif
357 
358 #endif /* _SYS_ZAP_H */
359