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 https://opensource.org/licenses/CDDL-1.0. 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 87 extern "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 */ 98 typedef enum matchtype { 99 MT_NORMALIZE = 1 << 0, 100 MT_MATCH_CASE = 1 << 1, 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 #if defined(__linux__) && defined(_KERNEL) 114 } zfs_zap_flags_t; 115 #define zap_flags_t zfs_zap_flags_t 116 #else 117 } zap_flags_t; 118 #endif 119 120 /* 121 * Create a new zapobj with no attributes and return its object number. 122 */ 123 uint64_t zap_create(objset_t *ds, dmu_object_type_t ot, 124 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 125 uint64_t zap_create_dnsize(objset_t *ds, dmu_object_type_t ot, 126 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx); 127 uint64_t zap_create_norm(objset_t *ds, int normflags, dmu_object_type_t ot, 128 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 129 uint64_t zap_create_norm_dnsize(objset_t *ds, int normflags, 130 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen, 131 int dnodesize, 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 uint64_t zap_create_flags_dnsize(objset_t *os, int normflags, 136 zap_flags_t flags, dmu_object_type_t ot, int leaf_blockshift, 137 int indirect_blockshift, dmu_object_type_t bonustype, int bonuslen, 138 int dnodesize, dmu_tx_t *tx); 139 uint64_t zap_create_hold(objset_t *os, int normflags, zap_flags_t flags, 140 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift, 141 dmu_object_type_t bonustype, int bonuslen, int dnodesize, 142 dnode_t **allocated_dnode, const void *tag, dmu_tx_t *tx); 143 144 uint64_t zap_create_link(objset_t *os, dmu_object_type_t ot, 145 uint64_t parent_obj, const char *name, dmu_tx_t *tx); 146 uint64_t zap_create_link_dnsize(objset_t *os, dmu_object_type_t ot, 147 uint64_t parent_obj, const char *name, int dnodesize, dmu_tx_t *tx); 148 149 /* 150 * Initialize an already-allocated object. 151 */ 152 void mzap_create_impl(dnode_t *dn, int normflags, zap_flags_t flags, 153 dmu_tx_t *tx); 154 155 /* 156 * Create a new zapobj with no attributes from the given (unallocated) 157 * object number. 158 */ 159 int zap_create_claim(objset_t *ds, uint64_t obj, dmu_object_type_t ot, 160 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 161 int zap_create_claim_dnsize(objset_t *ds, uint64_t obj, dmu_object_type_t ot, 162 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx); 163 int zap_create_claim_norm(objset_t *ds, uint64_t obj, 164 int normflags, dmu_object_type_t ot, 165 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx); 166 int zap_create_claim_norm_dnsize(objset_t *ds, uint64_t obj, 167 int normflags, dmu_object_type_t ot, 168 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx); 169 170 /* 171 * The zapobj passed in must be a valid ZAP object for all of the 172 * following routines. 173 */ 174 175 /* 176 * Destroy this zapobj and all its attributes. 177 * 178 * Frees the object number using dmu_object_free. 179 */ 180 int zap_destroy(objset_t *ds, uint64_t zapobj, dmu_tx_t *tx); 181 182 /* 183 * Manipulate attributes. 184 * 185 * 'integer_size' is in bytes, and must be 1, 2, 4, or 8. 186 */ 187 188 /* 189 * Retrieve the contents of the attribute with the given name. 190 * 191 * If the requested attribute does not exist, the call will fail and 192 * return ENOENT. 193 * 194 * If 'integer_size' is smaller than the attribute's integer size, the 195 * call will fail and return EINVAL. 196 * 197 * If 'integer_size' is equal to or larger than the attribute's integer 198 * size, the call will succeed and return 0. 199 * 200 * When converting to a larger integer size, the integers will be treated as 201 * unsigned (ie. no sign-extension will be performed). 202 * 203 * 'num_integers' is the length (in integers) of 'buf'. 204 * 205 * If the attribute is longer than the buffer, as many integers as will 206 * fit will be transferred to 'buf'. If the entire attribute was not 207 * transferred, the call will return EOVERFLOW. 208 */ 209 int zap_lookup(objset_t *ds, uint64_t zapobj, const char *name, 210 uint64_t integer_size, uint64_t num_integers, void *buf); 211 212 /* 213 * If rn_len is nonzero, realname will be set to the name of the found 214 * entry (which may be different from the requested name if matchtype is 215 * not MT_EXACT). 216 * 217 * If normalization_conflictp is not NULL, it will be set if there is 218 * another name with the same case/unicode normalized form. 219 */ 220 int zap_lookup_norm(objset_t *ds, uint64_t zapobj, const char *name, 221 uint64_t integer_size, uint64_t num_integers, void *buf, 222 matchtype_t mt, char *realname, int rn_len, 223 boolean_t *normalization_conflictp); 224 int zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 225 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf); 226 int zap_lookup_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 227 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf); 228 int zap_contains(objset_t *ds, uint64_t zapobj, const char *name); 229 int zap_prefetch(objset_t *os, uint64_t zapobj, const char *name); 230 int zap_prefetch_object(objset_t *os, uint64_t zapobj); 231 int zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 232 int key_numints); 233 int zap_prefetch_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 234 int key_numints); 235 236 int zap_lookup_by_dnode(dnode_t *dn, const char *name, 237 uint64_t integer_size, uint64_t num_integers, void *buf); 238 int zap_lookup_norm_by_dnode(dnode_t *dn, const char *name, 239 uint64_t integer_size, uint64_t num_integers, void *buf, 240 matchtype_t mt, char *realname, int rn_len, 241 boolean_t *ncp); 242 243 /* 244 * Create an attribute with the given name and value. 245 * 246 * If an attribute with the given name already exists, the call will 247 * fail and return EEXIST. 248 */ 249 int zap_add(objset_t *ds, uint64_t zapobj, const char *key, 250 int integer_size, uint64_t num_integers, 251 const void *val, dmu_tx_t *tx); 252 int zap_add_by_dnode(dnode_t *dn, const char *key, 253 int integer_size, uint64_t num_integers, 254 const void *val, dmu_tx_t *tx); 255 int zap_add_uint64(objset_t *ds, uint64_t zapobj, const uint64_t *key, 256 int key_numints, int integer_size, uint64_t num_integers, 257 const void *val, dmu_tx_t *tx); 258 int zap_add_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 259 int key_numints, int integer_size, uint64_t num_integers, 260 const void *val, dmu_tx_t *tx); 261 262 /* 263 * Set the attribute with the given name to the given value. If an 264 * attribute with the given name does not exist, it will be created. If 265 * an attribute with the given name already exists, the previous value 266 * will be overwritten. The integer_size may be different from the 267 * existing attribute's integer size, in which case the attribute's 268 * integer size will be updated to the new value. 269 */ 270 int zap_update(objset_t *ds, uint64_t zapobj, const char *name, 271 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 272 int zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 273 int key_numints, 274 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 275 int zap_update_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 276 int key_numints, 277 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 278 279 /* 280 * Get the length (in integers) and the integer size of the specified 281 * attribute. 282 * 283 * If the requested attribute does not exist, the call will fail and 284 * return ENOENT. 285 */ 286 int zap_length(objset_t *ds, uint64_t zapobj, const char *name, 287 uint64_t *integer_size, uint64_t *num_integers); 288 int zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 289 int key_numints, uint64_t *integer_size, uint64_t *num_integers); 290 291 /* 292 * Remove the specified attribute. 293 * 294 * If the specified attribute does not exist, the call will fail and 295 * return ENOENT. 296 */ 297 int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx); 298 int zap_remove_norm(objset_t *ds, uint64_t zapobj, const char *name, 299 matchtype_t mt, dmu_tx_t *tx); 300 int zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx); 301 int zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 302 int key_numints, dmu_tx_t *tx); 303 int zap_remove_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 304 int key_numints, dmu_tx_t *tx); 305 306 /* 307 * Returns (in *count) the number of attributes in the specified zap 308 * object. 309 */ 310 int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count); 311 312 /* 313 * Returns (in name) the name of the entry whose (value & mask) 314 * (za_first_integer) is value, or ENOENT if not found. The string 315 * pointed to by name must be at least 256 bytes long. If mask==0, the 316 * match must be exact (ie, same as mask=-1ULL). 317 */ 318 int zap_value_search(objset_t *os, uint64_t zapobj, 319 uint64_t value, uint64_t mask, char *name, uint64_t namelen); 320 321 /* 322 * Transfer all the entries from fromobj into intoobj. Only works on 323 * int_size=8 num_integers=1 values. Fails if there are any duplicated 324 * entries. 325 */ 326 int zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx); 327 328 /* Same as zap_join, but set the values to 'value'. */ 329 int zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj, 330 uint64_t value, dmu_tx_t *tx); 331 332 /* Same as zap_join, but add together any duplicated entries. */ 333 int zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj, 334 dmu_tx_t *tx); 335 336 /* 337 * Manipulate entries where the name + value are the "same" (the name is 338 * a stringified version of the value). 339 */ 340 int zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx); 341 int zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx); 342 int zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value); 343 int zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta, 344 dmu_tx_t *tx); 345 346 /* Here the key is an int and the value is a different int. */ 347 int zap_add_int_key(objset_t *os, uint64_t obj, 348 uint64_t key, uint64_t value, dmu_tx_t *tx); 349 int zap_update_int_key(objset_t *os, uint64_t obj, 350 uint64_t key, uint64_t value, dmu_tx_t *tx); 351 int zap_lookup_int_key(objset_t *os, uint64_t obj, 352 uint64_t key, uint64_t *valuep); 353 354 int zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta, 355 dmu_tx_t *tx); 356 357 struct zap; 358 struct zap_leaf; 359 typedef struct zap_cursor { 360 /* This structure is opaque! */ 361 objset_t *zc_objset; 362 struct zap *zc_zap; 363 struct zap_leaf *zc_leaf; 364 uint64_t zc_zapobj; 365 uint64_t zc_serialized; 366 uint64_t zc_hash; 367 uint32_t zc_cd; 368 boolean_t zc_prefetch; 369 } zap_cursor_t; 370 371 typedef struct { 372 int za_integer_length; 373 /* 374 * za_normalization_conflict will be set if there are additional 375 * entries with this normalized form (eg, "foo" and "Foo"). 376 */ 377 boolean_t za_normalization_conflict; 378 uint64_t za_num_integers; 379 uint64_t za_first_integer; /* no sign extension for <8byte ints */ 380 uint32_t za_name_len; 381 uint32_t za_pad; /* We want za_name aligned to uint64_t. */ 382 char za_name[]; 383 } zap_attribute_t; 384 385 void zap_init(void); 386 void zap_fini(void); 387 388 /* 389 * Alloc and free zap_attribute_t. 390 */ 391 zap_attribute_t *zap_attribute_alloc(void); 392 zap_attribute_t *zap_attribute_long_alloc(void); 393 void zap_attribute_free(zap_attribute_t *attrp); 394 395 /* 396 * The interface for listing all the attributes of a zapobj can be 397 * thought of as cursor moving down a list of the attributes one by 398 * one. The cookie returned by the zap_cursor_serialize routine is 399 * persistent across system calls (and across reboot, even). 400 */ 401 402 /* 403 * Initialize a zap cursor, pointing to the "first" attribute of the 404 * zapobj. You must _fini the cursor when you are done with it. 405 */ 406 void zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj); 407 void zap_cursor_init_noprefetch(zap_cursor_t *zc, objset_t *os, 408 uint64_t zapobj); 409 void zap_cursor_fini(zap_cursor_t *zc); 410 411 /* 412 * Get the attribute currently pointed to by the cursor. Returns 413 * ENOENT if at the end of the attributes. 414 */ 415 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za); 416 417 /* 418 * Advance the cursor to the next attribute. 419 */ 420 void zap_cursor_advance(zap_cursor_t *zc); 421 422 /* 423 * Get a persistent cookie pointing to the current position of the zap 424 * cursor. The low 4 bits in the cookie are always zero, and thus can 425 * be used as to differentiate a serialized cookie from a different type 426 * of value. The cookie will be less than 2^32 as long as there are 427 * fewer than 2^22 (4.2 million) entries in the zap object. 428 */ 429 uint64_t zap_cursor_serialize(zap_cursor_t *zc); 430 431 /* 432 * Initialize a zap cursor pointing to the position recorded by 433 * zap_cursor_serialize (in the "serialized" argument). You can also 434 * use a "serialized" argument of 0 to start at the beginning of the 435 * zapobj (ie. zap_cursor_init_serialized(..., 0) is equivalent to 436 * zap_cursor_init(...).) 437 */ 438 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds, 439 uint64_t zapobj, uint64_t serialized); 440 441 442 #define ZAP_HISTOGRAM_SIZE 10 443 444 typedef struct zap_stats { 445 /* 446 * Size of the pointer table (in number of entries). 447 * This is always a power of 2, or zero if it's a microzap. 448 * In general, it should be considerably greater than zs_num_leafs. 449 */ 450 uint64_t zs_ptrtbl_len; 451 452 uint64_t zs_blocksize; /* size of zap blocks */ 453 454 /* 455 * The number of blocks used. Note that some blocks may be 456 * wasted because old ptrtbl's and large name/value blocks are 457 * not reused. (Although their space is reclaimed, we don't 458 * reuse those offsets in the object.) 459 */ 460 uint64_t zs_num_blocks; 461 462 /* 463 * Pointer table values from zap_ptrtbl in the zap_phys_t 464 */ 465 uint64_t zs_ptrtbl_nextblk; /* next (larger) copy start block */ 466 uint64_t zs_ptrtbl_blks_copied; /* number source blocks copied */ 467 uint64_t zs_ptrtbl_zt_blk; /* starting block number */ 468 uint64_t zs_ptrtbl_zt_numblks; /* number of blocks */ 469 uint64_t zs_ptrtbl_zt_shift; /* bits to index it */ 470 471 /* 472 * Values of the other members of the zap_phys_t 473 */ 474 uint64_t zs_block_type; /* ZBT_HEADER */ 475 uint64_t zs_magic; /* ZAP_MAGIC */ 476 uint64_t zs_num_leafs; /* The number of leaf blocks */ 477 uint64_t zs_num_entries; /* The number of zap entries */ 478 uint64_t zs_salt; /* salt to stir into hash function */ 479 480 /* 481 * Histograms. For all histograms, the last index 482 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater 483 * than what can be represented. For example 484 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number 485 * of leafs with more than 45 entries. 486 */ 487 488 /* 489 * zs_leafs_with_n_pointers[n] is the number of leafs with 490 * 2^n pointers to it. 491 */ 492 uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE]; 493 494 /* 495 * zs_leafs_with_n_entries[n] is the number of leafs with 496 * [n*5, (n+1)*5) entries. In the current implementation, there 497 * can be at most 55 entries in any block, but there may be 498 * fewer if the name or value is large, or the block is not 499 * completely full. 500 */ 501 uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE]; 502 503 /* 504 * zs_leafs_n_tenths_full[n] is the number of leafs whose 505 * fullness is in the range [n/10, (n+1)/10). 506 */ 507 uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE]; 508 509 /* 510 * zs_entries_using_n_chunks[n] is the number of entries which 511 * consume n 24-byte chunks. (Note, large names/values only use 512 * one chunk, but contribute to zs_num_blocks_large.) 513 */ 514 uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE]; 515 516 /* 517 * zs_buckets_with_n_entries[n] is the number of buckets (each 518 * leaf has 64 buckets) with n entries. 519 * zs_buckets_with_n_entries[1] should be very close to 520 * zs_num_entries. 521 */ 522 uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE]; 523 } zap_stats_t; 524 525 /* 526 * Get statistics about a ZAP object. Note: you need to be aware of the 527 * internal implementation of the ZAP to correctly interpret some of the 528 * statistics. This interface shouldn't be relied on unless you really 529 * know what you're doing. 530 */ 531 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs); 532 533 #ifdef __cplusplus 534 } 535 #endif 536 537 #endif /* _SYS_ZAP_H */ 538