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_lookup_length_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 230 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf, 231 uint64_t *actual_num_integers); 232 int zap_contains(objset_t *ds, uint64_t zapobj, const char *name); 233 int zap_prefetch(objset_t *os, uint64_t zapobj, const char *name); 234 int zap_prefetch_object(objset_t *os, uint64_t zapobj); 235 int zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 236 int key_numints); 237 int zap_prefetch_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 238 int key_numints); 239 240 int zap_lookup_by_dnode(dnode_t *dn, const char *name, 241 uint64_t integer_size, uint64_t num_integers, void *buf); 242 int zap_lookup_norm_by_dnode(dnode_t *dn, const char *name, 243 uint64_t integer_size, uint64_t num_integers, void *buf, 244 matchtype_t mt, char *realname, int rn_len, 245 boolean_t *ncp); 246 247 /* 248 * Create an attribute with the given name and value. 249 * 250 * If an attribute with the given name already exists, the call will 251 * fail and return EEXIST. 252 */ 253 int zap_add(objset_t *ds, uint64_t zapobj, const char *key, 254 int integer_size, uint64_t num_integers, 255 const void *val, dmu_tx_t *tx); 256 int zap_add_by_dnode(dnode_t *dn, const char *key, 257 int integer_size, uint64_t num_integers, 258 const void *val, dmu_tx_t *tx); 259 int zap_add_uint64(objset_t *ds, uint64_t zapobj, const uint64_t *key, 260 int key_numints, int integer_size, uint64_t num_integers, 261 const void *val, dmu_tx_t *tx); 262 int zap_add_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 263 int key_numints, int integer_size, uint64_t num_integers, 264 const void *val, dmu_tx_t *tx); 265 266 /* 267 * Set the attribute with the given name to the given value. If an 268 * attribute with the given name does not exist, it will be created. If 269 * an attribute with the given name already exists, the previous value 270 * will be overwritten. The integer_size may be different from the 271 * existing attribute's integer size, in which case the attribute's 272 * integer size will be updated to the new value. 273 */ 274 int zap_update(objset_t *ds, uint64_t zapobj, const char *name, 275 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 276 int zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 277 int key_numints, 278 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 279 int zap_update_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 280 int key_numints, 281 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx); 282 283 /* 284 * Get the length (in integers) and the integer size of the specified 285 * attribute. 286 * 287 * If the requested attribute does not exist, the call will fail and 288 * return ENOENT. 289 */ 290 int zap_length(objset_t *ds, uint64_t zapobj, const char *name, 291 uint64_t *integer_size, uint64_t *num_integers); 292 int zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 293 int key_numints, uint64_t *integer_size, uint64_t *num_integers); 294 int zap_length_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 295 int key_numints, uint64_t *integer_size, uint64_t *num_integers); 296 297 /* 298 * Remove the specified attribute. 299 * 300 * If the specified attribute does not exist, the call will fail and 301 * return ENOENT. 302 */ 303 int zap_remove(objset_t *ds, uint64_t zapobj, const char *name, dmu_tx_t *tx); 304 int zap_remove_norm(objset_t *ds, uint64_t zapobj, const char *name, 305 matchtype_t mt, dmu_tx_t *tx); 306 int zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx); 307 int zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 308 int key_numints, dmu_tx_t *tx); 309 int zap_remove_uint64_by_dnode(dnode_t *dn, const uint64_t *key, 310 int key_numints, dmu_tx_t *tx); 311 312 /* 313 * Returns (in *count) the number of attributes in the specified zap 314 * object. 315 */ 316 int zap_count(objset_t *ds, uint64_t zapobj, uint64_t *count); 317 int zap_count_by_dnode(dnode_t *dn, uint64_t *count); 318 319 /* 320 * Returns (in name) the name of the entry whose (value & mask) 321 * (za_first_integer) is value, or ENOENT if not found. The string 322 * pointed to by name must be at least 256 bytes long. If mask==0, the 323 * match must be exact (ie, same as mask=-1ULL). 324 */ 325 int zap_value_search(objset_t *os, uint64_t zapobj, 326 uint64_t value, uint64_t mask, char *name, uint64_t namelen); 327 328 /* 329 * Transfer all the entries from fromobj into intoobj. Only works on 330 * int_size=8 num_integers=1 values. Fails if there are any duplicated 331 * entries. 332 */ 333 int zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx); 334 335 /* Same as zap_join, but set the values to 'value'. */ 336 int zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj, 337 uint64_t value, dmu_tx_t *tx); 338 339 /* Same as zap_join, but add together any duplicated entries. */ 340 int zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj, 341 dmu_tx_t *tx); 342 343 /* 344 * Manipulate entries where the name + value are the "same" (the name is 345 * a stringified version of the value). 346 */ 347 int zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx); 348 int zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx); 349 int zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value); 350 int zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta, 351 dmu_tx_t *tx); 352 353 /* Here the key is an int and the value is a different int. */ 354 int zap_add_int_key(objset_t *os, uint64_t obj, 355 uint64_t key, uint64_t value, dmu_tx_t *tx); 356 int zap_update_int_key(objset_t *os, uint64_t obj, 357 uint64_t key, uint64_t value, dmu_tx_t *tx); 358 int zap_lookup_int_key(objset_t *os, uint64_t obj, 359 uint64_t key, uint64_t *valuep); 360 361 int zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta, 362 dmu_tx_t *tx); 363 364 struct zap; 365 struct zap_leaf; 366 typedef struct zap_cursor { 367 /* This structure is opaque! */ 368 objset_t *zc_objset; 369 struct zap *zc_zap; 370 struct zap_leaf *zc_leaf; 371 uint64_t zc_zapobj; 372 uint64_t zc_serialized; 373 uint64_t zc_hash; 374 uint32_t zc_cd; 375 boolean_t zc_prefetch; 376 } zap_cursor_t; 377 378 typedef struct { 379 int za_integer_length; 380 /* 381 * za_normalization_conflict will be set if there are additional 382 * entries with this normalized form (eg, "foo" and "Foo"). 383 */ 384 boolean_t za_normalization_conflict; 385 uint64_t za_num_integers; 386 uint64_t za_first_integer; /* no sign extension for <8byte ints */ 387 uint32_t za_name_len; 388 uint32_t za_pad; /* We want za_name aligned to uint64_t. */ 389 char za_name[]; 390 } zap_attribute_t; 391 392 void zap_init(void); 393 void zap_fini(void); 394 395 /* 396 * Alloc and free zap_attribute_t. 397 */ 398 zap_attribute_t *zap_attribute_alloc(void); 399 zap_attribute_t *zap_attribute_long_alloc(void); 400 void zap_attribute_free(zap_attribute_t *attrp); 401 402 /* 403 * The interface for listing all the attributes of a zapobj can be 404 * thought of as cursor moving down a list of the attributes one by 405 * one. The cookie returned by the zap_cursor_serialize routine is 406 * persistent across system calls (and across reboot, even). 407 */ 408 409 /* 410 * Initialize a zap cursor, pointing to the "first" attribute of the 411 * zapobj. You must _fini the cursor when you are done with it. 412 */ 413 void zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj); 414 void zap_cursor_init_noprefetch(zap_cursor_t *zc, objset_t *os, 415 uint64_t zapobj); 416 void zap_cursor_fini(zap_cursor_t *zc); 417 418 /* 419 * Get the attribute currently pointed to by the cursor. Returns 420 * ENOENT if at the end of the attributes. 421 */ 422 int zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za); 423 424 /* 425 * Advance the cursor to the next attribute. 426 */ 427 void zap_cursor_advance(zap_cursor_t *zc); 428 429 /* 430 * Get a persistent cookie pointing to the current position of the zap 431 * cursor. The low 4 bits in the cookie are always zero, and thus can 432 * be used as to differentiate a serialized cookie from a different type 433 * of value. The cookie will be less than 2^32 as long as there are 434 * fewer than 2^22 (4.2 million) entries in the zap object. 435 */ 436 uint64_t zap_cursor_serialize(zap_cursor_t *zc); 437 438 /* 439 * Initialize a zap cursor pointing to the position recorded by 440 * zap_cursor_serialize (in the "serialized" argument). You can also 441 * use a "serialized" argument of 0 to start at the beginning of the 442 * zapobj (ie. zap_cursor_init_serialized(..., 0) is equivalent to 443 * zap_cursor_init(...).) 444 */ 445 void zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *ds, 446 uint64_t zapobj, uint64_t serialized); 447 448 449 #define ZAP_HISTOGRAM_SIZE 10 450 451 typedef struct zap_stats { 452 /* 453 * Size of the pointer table (in number of entries). 454 * This is always a power of 2, or zero if it's a microzap. 455 * In general, it should be considerably greater than zs_num_leafs. 456 */ 457 uint64_t zs_ptrtbl_len; 458 459 uint64_t zs_blocksize; /* size of zap blocks */ 460 461 /* 462 * The number of blocks used. Note that some blocks may be 463 * wasted because old ptrtbl's and large name/value blocks are 464 * not reused. (Although their space is reclaimed, we don't 465 * reuse those offsets in the object.) 466 */ 467 uint64_t zs_num_blocks; 468 469 /* 470 * Pointer table values from zap_ptrtbl in the zap_phys_t 471 */ 472 uint64_t zs_ptrtbl_nextblk; /* next (larger) copy start block */ 473 uint64_t zs_ptrtbl_blks_copied; /* number source blocks copied */ 474 uint64_t zs_ptrtbl_zt_blk; /* starting block number */ 475 uint64_t zs_ptrtbl_zt_numblks; /* number of blocks */ 476 uint64_t zs_ptrtbl_zt_shift; /* bits to index it */ 477 478 /* 479 * Values of the other members of the zap_phys_t 480 */ 481 uint64_t zs_block_type; /* ZBT_HEADER */ 482 uint64_t zs_magic; /* ZAP_MAGIC */ 483 uint64_t zs_num_leafs; /* The number of leaf blocks */ 484 uint64_t zs_num_entries; /* The number of zap entries */ 485 uint64_t zs_salt; /* salt to stir into hash function */ 486 487 /* 488 * Histograms. For all histograms, the last index 489 * (ZAP_HISTOGRAM_SIZE-1) includes any values which are greater 490 * than what can be represented. For example 491 * zs_leafs_with_n5_entries[ZAP_HISTOGRAM_SIZE-1] is the number 492 * of leafs with more than 45 entries. 493 */ 494 495 /* 496 * zs_leafs_with_n_pointers[n] is the number of leafs with 497 * 2^n pointers to it. 498 */ 499 uint64_t zs_leafs_with_2n_pointers[ZAP_HISTOGRAM_SIZE]; 500 501 /* 502 * zs_leafs_with_n_entries[n] is the number of leafs with 503 * [n*5, (n+1)*5) entries. In the current implementation, there 504 * can be at most 55 entries in any block, but there may be 505 * fewer if the name or value is large, or the block is not 506 * completely full. 507 */ 508 uint64_t zs_blocks_with_n5_entries[ZAP_HISTOGRAM_SIZE]; 509 510 /* 511 * zs_leafs_n_tenths_full[n] is the number of leafs whose 512 * fullness is in the range [n/10, (n+1)/10). 513 */ 514 uint64_t zs_blocks_n_tenths_full[ZAP_HISTOGRAM_SIZE]; 515 516 /* 517 * zs_entries_using_n_chunks[n] is the number of entries which 518 * consume n 24-byte chunks. (Note, large names/values only use 519 * one chunk, but contribute to zs_num_blocks_large.) 520 */ 521 uint64_t zs_entries_using_n_chunks[ZAP_HISTOGRAM_SIZE]; 522 523 /* 524 * zs_buckets_with_n_entries[n] is the number of buckets (each 525 * leaf has 64 buckets) with n entries. 526 * zs_buckets_with_n_entries[1] should be very close to 527 * zs_num_entries. 528 */ 529 uint64_t zs_buckets_with_n_entries[ZAP_HISTOGRAM_SIZE]; 530 } zap_stats_t; 531 532 /* 533 * Get statistics about a ZAP object. Note: you need to be aware of the 534 * internal implementation of the ZAP to correctly interpret some of the 535 * statistics. This interface shouldn't be relied on unless you really 536 * know what you're doing. 537 */ 538 int zap_get_stats(objset_t *ds, uint64_t zapobj, zap_stats_t *zs); 539 540 #ifdef __cplusplus 541 } 542 #endif 543 544 #endif /* _SYS_ZAP_H */ 545