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