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