xref: /titanic_41/usr/src/uts/common/fs/zfs/sys/zap.h (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 typedef struct zap_cursor {
204 	/* This structure is opaque! */
205 	objset_t *zc_objset;
206 	uint64_t zc_zapobj;
207 	uint64_t zc_hash;
208 	uint32_t zc_cd;
209 } zap_cursor_t;
210 
211 typedef struct {
212 	int za_integer_length;
213 	uint64_t za_num_integers;
214 	uint64_t za_first_integer;	/* no sign extension for <8byte ints */
215 	char za_name[MAXNAMELEN];
216 } zap_attribute_t;
217 
218 /*
219  * The interface for listing all the attributes of a zapobj can be
220  * thought of as cursor moving down a list of the attributes one by
221  * one.  The cookie returned by the zap_cursor_serialize routine is
222  * persistent across system calls (and across reboot, even).
223  */
224 
225 /*
226  * Initialize a zap cursor, pointing to the "first" attribute of the
227  * zapobj.
228  */
229 void zap_cursor_init(zap_cursor_t *zc, objset_t *ds, uint64_t zapobj);
230 
231 /*
232  * Get the attribute currently pointed to by the cursor.  Returns
233  * ENOENT if at the end of the attributes.
234  */
235 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za);
236 
237 /*
238  * Advance the cursor to the next attribute.
239  */
240 void zap_cursor_advance(zap_cursor_t *zc);
241 
242 /*
243  * Get a persistent cookie pointing to the current position of the zap
244  * cursor.  The low 4 bits in the cookie are always zero, and thus can
245  * be used as to differentiate a serialized cookie from a different type
246  * of value.  The cookie will be less than 2^32 as long as there are
247  * fewer than 2^22 (4.2 million) entries in the zap object.
248  */
249 uint64_t zap_cursor_serialize(zap_cursor_t *zc);
250 
251 /*
252  * Initialize a zap cursor pointing to the position recorded by
253  * zap_cursor_serialize (in the "serialized" argument).  You can also
254  * use a "serialized" argument of 0 to start at the beginning of the
255  * zapobj (ie.  zap_cursor_init_serialized(..., 0) is equivalent to
256  * zap_cursor_init(...).)
257  */
258 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds,
259     uint64_t zapobj, uint64_t serialized);
260 
261 
262 #define	ZAP_HISTOGRAM_SIZE 10
263 
264 typedef struct zap_stats {
265 	/*
266 	 * Size of the pointer table (in number of entries).
267 	 * This is always a power of 2, or zero if it's a microzap.
268 	 * In general, it should be considerably greater than zs_num_leafs.
269 	 */
270 	uint64_t zs_ptrtbl_len;
271 
272 	uint64_t zs_blocksize;		/* size of zap blocks */
273 
274 	uint64_t zs_num_leafs;		/* The number of leaf blocks */
275 
276 	uint64_t zs_num_entries;	/* The number of zap entries */
277 
278 	/*
279 	 * The number of blocks used.  Note that some blocks may be
280 	 * wasted because old ptrtbl's and large name/value blocks are
281 	 * not reused.  (Although their space is reclaimed, we don't
282 	 * reuse those offsets in the object.)
283 	 */
284 	uint64_t zs_num_blocks;
285 
286 	/* The number of blocks used for large names or values */
287 	uint64_t zs_num_blocks_large;
288 
289 	/*
290 	 * Histograms.  For all histograms, the last index
291 	 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater
292 	 * than what can be represented.  For example
293 	 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number
294 	 * of leafs with more than 45 entries.
295 	 */
296 
297 	/*
298 	 * zs_leafs_with_n_pointers[n] is the number of leafs with
299 	 * 2^n pointers to it.
300 	 */
301 	uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE];
302 
303 	/*
304 	 * zs_leafs_with_n_chained[n] is the number of leafs with n
305 	 * chained blocks.  zs_leafs_with_n_chained[0] (leafs with no
306 	 * chained blocks) should be very close to zs_num_leafs.
307 	 */
308 	uint64_t zs_leafs_with_n_chained[ZAP_HISTOGRAM_SIZE];
309 
310 	/*
311 	 * zs_leafs_with_n_entries[n] is the number of leafs with
312 	 * [n*5, (n+1)*5) entries.  In the current implementation, there
313 	 * can be at most 55 entries in any block, but there may be
314 	 * fewer if the name or value is large, or the block is not
315 	 * completely full.
316 	 */
317 	uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE];
318 
319 	/*
320 	 * zs_leafs_n_tenths_full[n] is the number of leafs whose
321 	 * fullness is in the range [n/10, (n+1)/10).
322 	 */
323 	uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE];
324 
325 	/*
326 	 * zs_entries_using_n_chunks[n] is the number of entries which
327 	 * consume n 24-byte chunks.  (Note, large names/values only use
328 	 * one chunk, but contribute to zs_num_blocks_large.)
329 	 */
330 	uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE];
331 
332 	/*
333 	 * zs_buckets_with_n_entries[n] is the number of buckets (each
334 	 * leaf has 64 buckets) with n entries.
335 	 * zs_buckets_with_n_entries[1] should be very close to
336 	 * zs_num_entries.
337 	 */
338 	uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE];
339 } zap_stats_t;
340 
341 /*
342  * Get statistics about a ZAP object.  Note: you need to be aware of the
343  * internal implementation of the ZAP to correctly interpret some of the
344  * statistics.  This interface shouldn't be relied on unless you really
345  * know what you're doing.
346  */
347 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs);
348 
349 #ifdef	__cplusplus
350 }
351 #endif
352 
353 #endif /* _SYS_ZAP_H */
354