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) 2011, 2018 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26 * Copyright (c) 2014 Integros [integros.com] 27 * Copyright 2017 Nexenta Systems, Inc. 28 */ 29 30#include <sys/zio.h> 31#include <sys/spa.h> 32#include <sys/dmu.h> 33#include <sys/zfs_context.h> 34#include <sys/zap.h> 35#include <sys/refcount.h> 36#include <sys/zap_impl.h> 37#include <sys/zap_leaf.h> 38#include <sys/avl.h> 39#include <sys/arc.h> 40#include <sys/dmu_objset.h> 41 42#ifdef _KERNEL 43#include <sys/sunddi.h> 44#endif 45 46extern inline mzap_phys_t *zap_m_phys(zap_t *zap); 47 48static int mzap_upgrade(zap_t **zapp, 49 void *tag, dmu_tx_t *tx, zap_flags_t flags); 50 51uint64_t 52zap_getflags(zap_t *zap) 53{ 54 if (zap->zap_ismicro) 55 return (0); 56 return (zap_f_phys(zap)->zap_flags); 57} 58 59int 60zap_hashbits(zap_t *zap) 61{ 62 if (zap_getflags(zap) & ZAP_FLAG_HASH64) 63 return (48); 64 else 65 return (28); 66} 67 68uint32_t 69zap_maxcd(zap_t *zap) 70{ 71 if (zap_getflags(zap) & ZAP_FLAG_HASH64) 72 return ((1<<16)-1); 73 else 74 return (-1U); 75} 76 77static uint64_t 78zap_hash(zap_name_t *zn) 79{ 80 zap_t *zap = zn->zn_zap; 81 uint64_t h = 0; 82 83 if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) { 84 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY); 85 h = *(uint64_t *)zn->zn_key_orig; 86 } else { 87 h = zap->zap_salt; 88 ASSERT(h != 0); 89 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 90 91 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) { 92 const uint64_t *wp = zn->zn_key_norm; 93 94 ASSERT(zn->zn_key_intlen == 8); 95 for (int i = 0; i < zn->zn_key_norm_numints; 96 wp++, i++) { 97 uint64_t word = *wp; 98 99 for (int j = 0; j < zn->zn_key_intlen; j++) { 100 h = (h >> 8) ^ 101 zfs_crc64_table[(h ^ word) & 0xFF]; 102 word >>= NBBY; 103 } 104 } 105 } else { 106 const uint8_t *cp = zn->zn_key_norm; 107 108 /* 109 * We previously stored the terminating null on 110 * disk, but didn't hash it, so we need to 111 * continue to not hash it. (The 112 * zn_key_*_numints includes the terminating 113 * null for non-binary keys.) 114 */ 115 int len = zn->zn_key_norm_numints - 1; 116 117 ASSERT(zn->zn_key_intlen == 1); 118 for (int i = 0; i < len; cp++, i++) { 119 h = (h >> 8) ^ 120 zfs_crc64_table[(h ^ *cp) & 0xFF]; 121 } 122 } 123 } 124 /* 125 * Don't use all 64 bits, since we need some in the cookie for 126 * the collision differentiator. We MUST use the high bits, 127 * since those are the ones that we first pay attention to when 128 * chosing the bucket. 129 */ 130 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1); 131 132 return (h); 133} 134 135static int 136zap_normalize(zap_t *zap, const char *name, char *namenorm, int normflags) 137{ 138 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY)); 139 140 size_t inlen = strlen(name) + 1; 141 size_t outlen = ZAP_MAXNAMELEN; 142 143 int err = 0; 144 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen, 145 normflags | U8_TEXTPREP_IGNORE_NULL | U8_TEXTPREP_IGNORE_INVALID, 146 U8_UNICODE_LATEST, &err); 147 148 return (err); 149} 150 151boolean_t 152zap_match(zap_name_t *zn, const char *matchname) 153{ 154 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY)); 155 156 if (zn->zn_matchtype & MT_NORMALIZE) { 157 char norm[ZAP_MAXNAMELEN]; 158 159 if (zap_normalize(zn->zn_zap, matchname, norm, 160 zn->zn_normflags) != 0) 161 return (B_FALSE); 162 163 return (strcmp(zn->zn_key_norm, norm) == 0); 164 } else { 165 return (strcmp(zn->zn_key_orig, matchname) == 0); 166 } 167} 168 169void 170zap_name_free(zap_name_t *zn) 171{ 172 kmem_free(zn, sizeof (zap_name_t)); 173} 174 175zap_name_t * 176zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt) 177{ 178 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP); 179 180 zn->zn_zap = zap; 181 zn->zn_key_intlen = sizeof (*key); 182 zn->zn_key_orig = key; 183 zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1; 184 zn->zn_matchtype = mt; 185 zn->zn_normflags = zap->zap_normflags; 186 187 /* 188 * If we're dealing with a case sensitive lookup on a mixed or 189 * insensitive fs, remove U8_TEXTPREP_TOUPPER or the lookup 190 * will fold case to all caps overriding the lookup request. 191 */ 192 if (mt & MT_MATCH_CASE) 193 zn->zn_normflags &= ~U8_TEXTPREP_TOUPPER; 194 195 if (zap->zap_normflags) { 196 /* 197 * We *must* use zap_normflags because this normalization is 198 * what the hash is computed from. 199 */ 200 if (zap_normalize(zap, key, zn->zn_normbuf, 201 zap->zap_normflags) != 0) { 202 zap_name_free(zn); 203 return (NULL); 204 } 205 zn->zn_key_norm = zn->zn_normbuf; 206 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1; 207 } else { 208 if (mt != 0) { 209 zap_name_free(zn); 210 return (NULL); 211 } 212 zn->zn_key_norm = zn->zn_key_orig; 213 zn->zn_key_norm_numints = zn->zn_key_orig_numints; 214 } 215 216 zn->zn_hash = zap_hash(zn); 217 218 if (zap->zap_normflags != zn->zn_normflags) { 219 /* 220 * We *must* use zn_normflags because this normalization is 221 * what the matching is based on. (Not the hash!) 222 */ 223 if (zap_normalize(zap, key, zn->zn_normbuf, 224 zn->zn_normflags) != 0) { 225 zap_name_free(zn); 226 return (NULL); 227 } 228 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1; 229 } 230 231 return (zn); 232} 233 234zap_name_t * 235zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints) 236{ 237 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP); 238 239 ASSERT(zap->zap_normflags == 0); 240 zn->zn_zap = zap; 241 zn->zn_key_intlen = sizeof (*key); 242 zn->zn_key_orig = zn->zn_key_norm = key; 243 zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints; 244 zn->zn_matchtype = 0; 245 246 zn->zn_hash = zap_hash(zn); 247 return (zn); 248} 249 250static void 251mzap_byteswap(mzap_phys_t *buf, size_t size) 252{ 253 buf->mz_block_type = BSWAP_64(buf->mz_block_type); 254 buf->mz_salt = BSWAP_64(buf->mz_salt); 255 buf->mz_normflags = BSWAP_64(buf->mz_normflags); 256 int max = (size / MZAP_ENT_LEN) - 1; 257 for (int i = 0; i < max; i++) { 258 buf->mz_chunk[i].mze_value = 259 BSWAP_64(buf->mz_chunk[i].mze_value); 260 buf->mz_chunk[i].mze_cd = 261 BSWAP_32(buf->mz_chunk[i].mze_cd); 262 } 263} 264 265void 266zap_byteswap(void *buf, size_t size) 267{ 268 uint64_t block_type = *(uint64_t *)buf; 269 270 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) { 271 /* ASSERT(magic == ZAP_LEAF_MAGIC); */ 272 mzap_byteswap(buf, size); 273 } else { 274 fzap_byteswap(buf, size); 275 } 276} 277 278static int 279mze_compare(const void *arg1, const void *arg2) 280{ 281 const mzap_ent_t *mze1 = arg1; 282 const mzap_ent_t *mze2 = arg2; 283 284 int cmp = AVL_CMP(mze1->mze_hash, mze2->mze_hash); 285 if (likely(cmp)) 286 return (cmp); 287 288 return (AVL_CMP(mze1->mze_cd, mze2->mze_cd)); 289} 290 291static int 292mze_insert(zap_t *zap, int chunkid, uint64_t hash) 293{ 294 avl_index_t idx; 295 296 ASSERT(zap->zap_ismicro); 297 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 298 299 mzap_ent_t *mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP); 300 mze->mze_chunkid = chunkid; 301 mze->mze_hash = hash; 302 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd; 303 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0); 304 if (avl_find(&zap->zap_m.zap_avl, mze, &idx) != NULL) { 305 kmem_free(mze, sizeof (mzap_ent_t)); 306 return (EEXIST); 307 } 308 avl_insert(&zap->zap_m.zap_avl, mze, idx); 309 return (0); 310} 311 312static mzap_ent_t * 313mze_find(zap_name_t *zn) 314{ 315 mzap_ent_t mze_tofind; 316 mzap_ent_t *mze; 317 avl_index_t idx; 318 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl; 319 320 ASSERT(zn->zn_zap->zap_ismicro); 321 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock)); 322 323 mze_tofind.mze_hash = zn->zn_hash; 324 mze_tofind.mze_cd = 0; 325 326 mze = avl_find(avl, &mze_tofind, &idx); 327 if (mze == NULL) 328 mze = avl_nearest(avl, idx, AVL_AFTER); 329 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) { 330 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd); 331 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name)) 332 return (mze); 333 } 334 335 return (NULL); 336} 337 338static uint32_t 339mze_find_unused_cd(zap_t *zap, uint64_t hash) 340{ 341 mzap_ent_t mze_tofind; 342 avl_index_t idx; 343 avl_tree_t *avl = &zap->zap_m.zap_avl; 344 345 ASSERT(zap->zap_ismicro); 346 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 347 348 mze_tofind.mze_hash = hash; 349 mze_tofind.mze_cd = 0; 350 351 uint32_t cd = 0; 352 for (mzap_ent_t *mze = avl_find(avl, &mze_tofind, &idx); 353 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) { 354 if (mze->mze_cd != cd) 355 break; 356 cd++; 357 } 358 359 return (cd); 360} 361 362static void 363mze_remove(zap_t *zap, mzap_ent_t *mze) 364{ 365 ASSERT(zap->zap_ismicro); 366 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 367 368 avl_remove(&zap->zap_m.zap_avl, mze); 369 kmem_free(mze, sizeof (mzap_ent_t)); 370} 371 372static void 373mze_destroy(zap_t *zap) 374{ 375 mzap_ent_t *mze; 376 void *avlcookie = NULL; 377 378 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie)) 379 kmem_free(mze, sizeof (mzap_ent_t)); 380 avl_destroy(&zap->zap_m.zap_avl); 381} 382 383static zap_t * 384mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db) 385{ 386 zap_t *winner; 387 uint64_t *zap_hdr = (uint64_t *)db->db_data; 388 uint64_t zap_block_type = zap_hdr[0]; 389 uint64_t zap_magic = zap_hdr[1]; 390 391 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t)); 392 393 zap_t *zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP); 394 rw_init(&zap->zap_rwlock, 0, 0, 0); 395 rw_enter(&zap->zap_rwlock, RW_WRITER); 396 zap->zap_objset = os; 397 zap->zap_object = obj; 398 zap->zap_dbuf = db; 399 400 if (zap_block_type != ZBT_MICRO) { 401 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0); 402 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1; 403 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) { 404 winner = NULL; /* No actual winner here... */ 405 goto handle_winner; 406 } 407 } else { 408 zap->zap_ismicro = TRUE; 409 } 410 411 /* 412 * Make sure that zap_ismicro is set before we let others see 413 * it, because zap_lockdir() checks zap_ismicro without the lock 414 * held. 415 */ 416 dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf); 417 winner = dmu_buf_set_user(db, &zap->zap_dbu); 418 419 if (winner != NULL) 420 goto handle_winner; 421 422 if (zap->zap_ismicro) { 423 zap->zap_salt = zap_m_phys(zap)->mz_salt; 424 zap->zap_normflags = zap_m_phys(zap)->mz_normflags; 425 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1; 426 avl_create(&zap->zap_m.zap_avl, mze_compare, 427 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node)); 428 429 for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) { 430 mzap_ent_phys_t *mze = 431 &zap_m_phys(zap)->mz_chunk[i]; 432 if (mze->mze_name[0]) { 433 zap_name_t *zn; 434 435 zn = zap_name_alloc(zap, mze->mze_name, 0); 436 if (mze_insert(zap, i, zn->zn_hash) == 0) 437 zap->zap_m.zap_num_entries++; 438 else { 439 printf("ZFS WARNING: Duplicated ZAP " 440 "entry detected (%s).\n", 441 mze->mze_name); 442 } 443 zap_name_free(zn); 444 } 445 } 446 } else { 447 zap->zap_salt = zap_f_phys(zap)->zap_salt; 448 zap->zap_normflags = zap_f_phys(zap)->zap_normflags; 449 450 ASSERT3U(sizeof (struct zap_leaf_header), ==, 451 2*ZAP_LEAF_CHUNKSIZE); 452 453 /* 454 * The embedded pointer table should not overlap the 455 * other members. 456 */ 457 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >, 458 &zap_f_phys(zap)->zap_salt); 459 460 /* 461 * The embedded pointer table should end at the end of 462 * the block 463 */ 464 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap, 465 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) - 466 (uintptr_t)zap_f_phys(zap), ==, 467 zap->zap_dbuf->db_size); 468 } 469 rw_exit(&zap->zap_rwlock); 470 return (zap); 471 472handle_winner: 473 rw_exit(&zap->zap_rwlock); 474 rw_destroy(&zap->zap_rwlock); 475 if (!zap->zap_ismicro) 476 mutex_destroy(&zap->zap_f.zap_num_entries_mtx); 477 kmem_free(zap, sizeof (zap_t)); 478 return (winner); 479} 480 481/* 482 * This routine "consumes" the caller's hold on the dbuf, which must 483 * have the specified tag. 484 */ 485static int 486zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx, 487 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp) 488{ 489 ASSERT0(db->db_offset); 490 objset_t *os = dmu_buf_get_objset(db); 491 uint64_t obj = db->db_object; 492 493 *zapp = NULL; 494 495 zap_t *zap = dmu_buf_get_user(db); 496 if (zap == NULL) { 497 zap = mzap_open(os, obj, db); 498 if (zap == NULL) { 499 /* 500 * mzap_open() didn't like what it saw on-disk. 501 * Check for corruption! 502 */ 503 return (SET_ERROR(EIO)); 504 } 505 } 506 507 /* 508 * We're checking zap_ismicro without the lock held, in order to 509 * tell what type of lock we want. Once we have some sort of 510 * lock, see if it really is the right type. In practice this 511 * can only be different if it was upgraded from micro to fat, 512 * and micro wanted WRITER but fat only needs READER. 513 */ 514 krw_t lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti; 515 rw_enter(&zap->zap_rwlock, lt); 516 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) { 517 /* it was upgraded, now we only need reader */ 518 ASSERT(lt == RW_WRITER); 519 ASSERT(RW_READER == 520 (!zap->zap_ismicro && fatreader) ? RW_READER : lti); 521 rw_downgrade(&zap->zap_rwlock); 522 lt = RW_READER; 523 } 524 525 zap->zap_objset = os; 526 527 if (lt == RW_WRITER) 528 dmu_buf_will_dirty(db, tx); 529 530 ASSERT3P(zap->zap_dbuf, ==, db); 531 532 ASSERT(!zap->zap_ismicro || 533 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks); 534 if (zap->zap_ismicro && tx && adding && 535 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) { 536 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE; 537 if (newsz > MZAP_MAX_BLKSZ) { 538 dprintf("upgrading obj %llu: num_entries=%u\n", 539 obj, zap->zap_m.zap_num_entries); 540 *zapp = zap; 541 int err = mzap_upgrade(zapp, tag, tx, 0); 542 if (err != 0) 543 rw_exit(&zap->zap_rwlock); 544 return (err); 545 } 546 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx)); 547 zap->zap_m.zap_num_chunks = 548 db->db_size / MZAP_ENT_LEN - 1; 549 } 550 551 *zapp = zap; 552 return (0); 553} 554 555static int 556zap_lockdir_by_dnode(dnode_t *dn, dmu_tx_t *tx, 557 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp) 558{ 559 dmu_buf_t *db; 560 561 int err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH); 562 if (err != 0) { 563 return (err); 564 } 565#ifdef ZFS_DEBUG 566 { 567 dmu_object_info_t doi; 568 dmu_object_info_from_db(db, &doi); 569 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP); 570 } 571#endif 572 573 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp); 574 if (err != 0) { 575 dmu_buf_rele(db, tag); 576 } 577 return (err); 578} 579 580int 581zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx, 582 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp) 583{ 584 dmu_buf_t *db; 585 586 int err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH); 587 if (err != 0) 588 return (err); 589#ifdef ZFS_DEBUG 590 { 591 dmu_object_info_t doi; 592 dmu_object_info_from_db(db, &doi); 593 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP); 594 } 595#endif 596 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp); 597 if (err != 0) 598 dmu_buf_rele(db, tag); 599 return (err); 600} 601 602void 603zap_unlockdir(zap_t *zap, void *tag) 604{ 605 rw_exit(&zap->zap_rwlock); 606 dmu_buf_rele(zap->zap_dbuf, tag); 607} 608 609static int 610mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags) 611{ 612 int err = 0; 613 zap_t *zap = *zapp; 614 615 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 616 617 int sz = zap->zap_dbuf->db_size; 618 mzap_phys_t *mzp = zio_buf_alloc(sz); 619 bcopy(zap->zap_dbuf->db_data, mzp, sz); 620 int nchunks = zap->zap_m.zap_num_chunks; 621 622 if (!flags) { 623 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object, 624 1ULL << fzap_default_block_shift, 0, tx); 625 if (err != 0) { 626 zio_buf_free(mzp, sz); 627 return (err); 628 } 629 } 630 631 dprintf("upgrading obj=%llu with %u chunks\n", 632 zap->zap_object, nchunks); 633 /* XXX destroy the avl later, so we can use the stored hash value */ 634 mze_destroy(zap); 635 636 fzap_upgrade(zap, tx, flags); 637 638 for (int i = 0; i < nchunks; i++) { 639 mzap_ent_phys_t *mze = &mzp->mz_chunk[i]; 640 if (mze->mze_name[0] == 0) 641 continue; 642 dprintf("adding %s=%llu\n", 643 mze->mze_name, mze->mze_value); 644 zap_name_t *zn = zap_name_alloc(zap, mze->mze_name, 0); 645 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, 646 tag, tx); 647 zap = zn->zn_zap; /* fzap_add_cd() may change zap */ 648 zap_name_free(zn); 649 if (err != 0) 650 break; 651 } 652 zio_buf_free(mzp, sz); 653 *zapp = zap; 654 return (err); 655} 656 657/* 658 * The "normflags" determine the behavior of the matchtype_t which is 659 * passed to zap_lookup_norm(). Names which have the same normalized 660 * version will be stored with the same hash value, and therefore we can 661 * perform normalization-insensitive lookups. We can be Unicode form- 662 * insensitive and/or case-insensitive. The following flags are valid for 663 * "normflags": 664 * 665 * U8_TEXTPREP_NFC 666 * U8_TEXTPREP_NFD 667 * U8_TEXTPREP_NFKC 668 * U8_TEXTPREP_NFKD 669 * U8_TEXTPREP_TOUPPER 670 * 671 * The *_NF* (Normalization Form) flags are mutually exclusive; at most one 672 * of them may be supplied. 673 */ 674void 675mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags, 676 dmu_tx_t *tx) 677{ 678 dmu_buf_t *db; 679 680 VERIFY0(dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH)); 681 682 dmu_buf_will_dirty(db, tx); 683 mzap_phys_t *zp = db->db_data; 684 zp->mz_block_type = ZBT_MICRO; 685 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL; 686 zp->mz_normflags = normflags; 687 688 if (flags != 0) { 689 zap_t *zap; 690 /* Only fat zap supports flags; upgrade immediately. */ 691 VERIFY0(zap_lockdir_impl(db, FTAG, tx, RW_WRITER, 692 B_FALSE, B_FALSE, &zap)); 693 VERIFY0(mzap_upgrade(&zap, FTAG, tx, flags)); 694 zap_unlockdir(zap, FTAG); 695 } else { 696 dmu_buf_rele(db, FTAG); 697 } 698} 699 700int 701zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot, 702 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 703{ 704 return (zap_create_claim_dnsize(os, obj, ot, bonustype, bonuslen, 705 0, tx)); 706} 707 708int 709zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot, 710 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx) 711{ 712 return (zap_create_claim_norm_dnsize(os, obj, 713 0, ot, bonustype, bonuslen, dnodesize, tx)); 714} 715 716int 717zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags, 718 dmu_object_type_t ot, 719 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 720{ 721 return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype, 722 bonuslen, 0, tx)); 723} 724 725int 726zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags, 727 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen, 728 int dnodesize, dmu_tx_t *tx) 729{ 730 int err; 731 732 err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen, 733 dnodesize, tx); 734 if (err != 0) 735 return (err); 736 mzap_create_impl(os, obj, normflags, 0, tx); 737 return (0); 738} 739 740uint64_t 741zap_create(objset_t *os, dmu_object_type_t ot, 742 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 743{ 744 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx)); 745} 746 747uint64_t 748zap_create_dnsize(objset_t *os, dmu_object_type_t ot, 749 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx) 750{ 751 return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen, 752 dnodesize, tx)); 753} 754 755uint64_t 756zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot, 757 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 758{ 759 ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP); 760 return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen, 761 0, tx)); 762} 763 764uint64_t 765zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot, 766 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx) 767{ 768 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen, 769 dnodesize, tx); 770 771 mzap_create_impl(os, obj, normflags, 0, tx); 772 return (obj); 773} 774 775uint64_t 776zap_create_flags(objset_t *os, int normflags, zap_flags_t flags, 777 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift, 778 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 779{ 780 ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP); 781 return (zap_create_flags_dnsize(os, normflags, flags, ot, 782 leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx)); 783} 784 785uint64_t 786zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags, 787 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift, 788 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx) 789{ 790 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen, 791 dnodesize, tx); 792 793 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT && 794 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT && 795 indirect_blockshift >= SPA_MINBLOCKSHIFT && 796 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT); 797 798 VERIFY(dmu_object_set_blocksize(os, obj, 799 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0); 800 801 mzap_create_impl(os, obj, normflags, flags, tx); 802 return (obj); 803} 804 805int 806zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx) 807{ 808 /* 809 * dmu_object_free will free the object number and free the 810 * data. Freeing the data will cause our pageout function to be 811 * called, which will destroy our data (zap_leaf_t's and zap_t). 812 */ 813 814 return (dmu_object_free(os, zapobj, tx)); 815} 816 817void 818zap_evict_sync(void *dbu) 819{ 820 zap_t *zap = dbu; 821 822 rw_destroy(&zap->zap_rwlock); 823 824 if (zap->zap_ismicro) 825 mze_destroy(zap); 826 else 827 mutex_destroy(&zap->zap_f.zap_num_entries_mtx); 828 829 kmem_free(zap, sizeof (zap_t)); 830} 831 832int 833zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 834{ 835 zap_t *zap; 836 837 int err = 838 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 839 if (err != 0) 840 return (err); 841 if (!zap->zap_ismicro) { 842 err = fzap_count(zap, count); 843 } else { 844 *count = zap->zap_m.zap_num_entries; 845 } 846 zap_unlockdir(zap, FTAG); 847 return (err); 848} 849 850/* 851 * zn may be NULL; if not specified, it will be computed if needed. 852 * See also the comment above zap_entry_normalization_conflict(). 853 */ 854static boolean_t 855mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze) 856{ 857 int direction = AVL_BEFORE; 858 boolean_t allocdzn = B_FALSE; 859 860 if (zap->zap_normflags == 0) 861 return (B_FALSE); 862 863again: 864 for (mzap_ent_t *other = avl_walk(&zap->zap_m.zap_avl, mze, direction); 865 other && other->mze_hash == mze->mze_hash; 866 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) { 867 868 if (zn == NULL) { 869 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name, 870 MT_NORMALIZE); 871 allocdzn = B_TRUE; 872 } 873 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) { 874 if (allocdzn) 875 zap_name_free(zn); 876 return (B_TRUE); 877 } 878 } 879 880 if (direction == AVL_BEFORE) { 881 direction = AVL_AFTER; 882 goto again; 883 } 884 885 if (allocdzn) 886 zap_name_free(zn); 887 return (B_FALSE); 888} 889 890/* 891 * Routines for manipulating attributes. 892 */ 893 894int 895zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 896 uint64_t integer_size, uint64_t num_integers, void *buf) 897{ 898 return (zap_lookup_norm(os, zapobj, name, integer_size, 899 num_integers, buf, 0, NULL, 0, NULL)); 900} 901 902static int 903zap_lookup_impl(zap_t *zap, const char *name, 904 uint64_t integer_size, uint64_t num_integers, void *buf, 905 matchtype_t mt, char *realname, int rn_len, 906 boolean_t *ncp) 907{ 908 int err = 0; 909 910 zap_name_t *zn = zap_name_alloc(zap, name, mt); 911 if (zn == NULL) 912 return (SET_ERROR(ENOTSUP)); 913 914 if (!zap->zap_ismicro) { 915 err = fzap_lookup(zn, integer_size, num_integers, buf, 916 realname, rn_len, ncp); 917 } else { 918 mzap_ent_t *mze = mze_find(zn); 919 if (mze == NULL) { 920 err = SET_ERROR(ENOENT); 921 } else { 922 if (num_integers < 1) { 923 err = SET_ERROR(EOVERFLOW); 924 } else if (integer_size != 8) { 925 err = SET_ERROR(EINVAL); 926 } else { 927 *(uint64_t *)buf = 928 MZE_PHYS(zap, mze)->mze_value; 929 (void) strlcpy(realname, 930 MZE_PHYS(zap, mze)->mze_name, rn_len); 931 if (ncp) { 932 *ncp = mzap_normalization_conflict(zap, 933 zn, mze); 934 } 935 } 936 } 937 } 938 zap_name_free(zn); 939 return (err); 940} 941 942int 943zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name, 944 uint64_t integer_size, uint64_t num_integers, void *buf, 945 matchtype_t mt, char *realname, int rn_len, 946 boolean_t *ncp) 947{ 948 zap_t *zap; 949 950 int err = 951 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 952 if (err != 0) 953 return (err); 954 err = zap_lookup_impl(zap, name, integer_size, 955 num_integers, buf, mt, realname, rn_len, ncp); 956 zap_unlockdir(zap, FTAG); 957 return (err); 958} 959 960int 961zap_lookup_by_dnode(dnode_t *dn, const char *name, 962 uint64_t integer_size, uint64_t num_integers, void *buf) 963{ 964 return (zap_lookup_norm_by_dnode(dn, name, integer_size, 965 num_integers, buf, 0, NULL, 0, NULL)); 966} 967 968int 969zap_lookup_norm_by_dnode(dnode_t *dn, const char *name, 970 uint64_t integer_size, uint64_t num_integers, void *buf, 971 matchtype_t mt, char *realname, int rn_len, 972 boolean_t *ncp) 973{ 974 zap_t *zap; 975 976 int err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE, 977 FTAG, &zap); 978 if (err != 0) 979 return (err); 980 err = zap_lookup_impl(zap, name, integer_size, 981 num_integers, buf, mt, realname, rn_len, ncp); 982 zap_unlockdir(zap, FTAG); 983 return (err); 984} 985 986int 987zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 988 int key_numints) 989{ 990 zap_t *zap; 991 992 int err = 993 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 994 if (err != 0) 995 return (err); 996 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 997 if (zn == NULL) { 998 zap_unlockdir(zap, FTAG); 999 return (SET_ERROR(ENOTSUP)); 1000 } 1001 1002 fzap_prefetch(zn); 1003 zap_name_free(zn); 1004 zap_unlockdir(zap, FTAG); 1005 return (err); 1006} 1007 1008int 1009zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1010 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf) 1011{ 1012 zap_t *zap; 1013 1014 int err = 1015 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 1016 if (err != 0) 1017 return (err); 1018 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 1019 if (zn == NULL) { 1020 zap_unlockdir(zap, FTAG); 1021 return (SET_ERROR(ENOTSUP)); 1022 } 1023 1024 err = fzap_lookup(zn, integer_size, num_integers, buf, 1025 NULL, 0, NULL); 1026 zap_name_free(zn); 1027 zap_unlockdir(zap, FTAG); 1028 return (err); 1029} 1030 1031int 1032zap_contains(objset_t *os, uint64_t zapobj, const char *name) 1033{ 1034 int err = zap_lookup_norm(os, zapobj, name, 0, 1035 0, NULL, 0, NULL, 0, NULL); 1036 if (err == EOVERFLOW || err == EINVAL) 1037 err = 0; /* found, but skipped reading the value */ 1038 return (err); 1039} 1040 1041int 1042zap_length(objset_t *os, uint64_t zapobj, const char *name, 1043 uint64_t *integer_size, uint64_t *num_integers) 1044{ 1045 zap_t *zap; 1046 1047 int err = 1048 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 1049 if (err != 0) 1050 return (err); 1051 zap_name_t *zn = zap_name_alloc(zap, name, 0); 1052 if (zn == NULL) { 1053 zap_unlockdir(zap, FTAG); 1054 return (SET_ERROR(ENOTSUP)); 1055 } 1056 if (!zap->zap_ismicro) { 1057 err = fzap_length(zn, integer_size, num_integers); 1058 } else { 1059 mzap_ent_t *mze = mze_find(zn); 1060 if (mze == NULL) { 1061 err = SET_ERROR(ENOENT); 1062 } else { 1063 if (integer_size) 1064 *integer_size = 8; 1065 if (num_integers) 1066 *num_integers = 1; 1067 } 1068 } 1069 zap_name_free(zn); 1070 zap_unlockdir(zap, FTAG); 1071 return (err); 1072} 1073 1074int 1075zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1076 int key_numints, uint64_t *integer_size, uint64_t *num_integers) 1077{ 1078 zap_t *zap; 1079 1080 int err = 1081 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 1082 if (err != 0) 1083 return (err); 1084 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 1085 if (zn == NULL) { 1086 zap_unlockdir(zap, FTAG); 1087 return (SET_ERROR(ENOTSUP)); 1088 } 1089 err = fzap_length(zn, integer_size, num_integers); 1090 zap_name_free(zn); 1091 zap_unlockdir(zap, FTAG); 1092 return (err); 1093} 1094 1095static void 1096mzap_addent(zap_name_t *zn, uint64_t value) 1097{ 1098 zap_t *zap = zn->zn_zap; 1099 int start = zap->zap_m.zap_alloc_next; 1100 1101 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 1102 1103#ifdef ZFS_DEBUG 1104 for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) { 1105 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 1106 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0); 1107 } 1108#endif 1109 1110 uint32_t cd = mze_find_unused_cd(zap, zn->zn_hash); 1111 /* given the limited size of the microzap, this can't happen */ 1112 ASSERT(cd < zap_maxcd(zap)); 1113 1114again: 1115 for (int i = start; i < zap->zap_m.zap_num_chunks; i++) { 1116 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 1117 if (mze->mze_name[0] == 0) { 1118 mze->mze_value = value; 1119 mze->mze_cd = cd; 1120 (void) strcpy(mze->mze_name, zn->zn_key_orig); 1121 zap->zap_m.zap_num_entries++; 1122 zap->zap_m.zap_alloc_next = i+1; 1123 if (zap->zap_m.zap_alloc_next == 1124 zap->zap_m.zap_num_chunks) 1125 zap->zap_m.zap_alloc_next = 0; 1126 VERIFY(0 == mze_insert(zap, i, zn->zn_hash)); 1127 return; 1128 } 1129 } 1130 if (start != 0) { 1131 start = 0; 1132 goto again; 1133 } 1134 ASSERT(!"out of entries!"); 1135} 1136 1137static int 1138zap_add_impl(zap_t *zap, const char *key, 1139 int integer_size, uint64_t num_integers, 1140 const void *val, dmu_tx_t *tx, void *tag) 1141{ 1142 const uint64_t *intval = val; 1143 int err = 0; 1144 1145 zap_name_t *zn = zap_name_alloc(zap, key, 0); 1146 if (zn == NULL) { 1147 zap_unlockdir(zap, tag); 1148 return (SET_ERROR(ENOTSUP)); 1149 } 1150 if (!zap->zap_ismicro) { 1151 err = fzap_add(zn, integer_size, num_integers, val, tag, tx); 1152 zap = zn->zn_zap; /* fzap_add() may change zap */ 1153 } else if (integer_size != 8 || num_integers != 1 || 1154 strlen(key) >= MZAP_NAME_LEN) { 1155 err = mzap_upgrade(&zn->zn_zap, tag, tx, 0); 1156 if (err == 0) { 1157 err = fzap_add(zn, integer_size, num_integers, val, 1158 tag, tx); 1159 } 1160 zap = zn->zn_zap; /* fzap_add() may change zap */ 1161 } else { 1162 if (mze_find(zn) != NULL) { 1163 err = SET_ERROR(EEXIST); 1164 } else { 1165 mzap_addent(zn, *intval); 1166 } 1167 } 1168 ASSERT(zap == zn->zn_zap); 1169 zap_name_free(zn); 1170 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1171 zap_unlockdir(zap, tag); 1172 return (err); 1173} 1174 1175int 1176zap_add(objset_t *os, uint64_t zapobj, const char *key, 1177 int integer_size, uint64_t num_integers, 1178 const void *val, dmu_tx_t *tx) 1179{ 1180 zap_t *zap; 1181 int err; 1182 1183 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap); 1184 if (err != 0) 1185 return (err); 1186 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG); 1187 /* zap_add_impl() calls zap_unlockdir() */ 1188 return (err); 1189} 1190 1191int 1192zap_add_by_dnode(dnode_t *dn, const char *key, 1193 int integer_size, uint64_t num_integers, 1194 const void *val, dmu_tx_t *tx) 1195{ 1196 zap_t *zap; 1197 int err; 1198 1199 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap); 1200 if (err != 0) 1201 return (err); 1202 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG); 1203 /* zap_add_impl() calls zap_unlockdir() */ 1204 return (err); 1205} 1206 1207int 1208zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1209 int key_numints, int integer_size, uint64_t num_integers, 1210 const void *val, dmu_tx_t *tx) 1211{ 1212 zap_t *zap; 1213 1214 int err = 1215 zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap); 1216 if (err != 0) 1217 return (err); 1218 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 1219 if (zn == NULL) { 1220 zap_unlockdir(zap, FTAG); 1221 return (SET_ERROR(ENOTSUP)); 1222 } 1223 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx); 1224 zap = zn->zn_zap; /* fzap_add() may change zap */ 1225 zap_name_free(zn); 1226 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1227 zap_unlockdir(zap, FTAG); 1228 return (err); 1229} 1230 1231int 1232zap_update(objset_t *os, uint64_t zapobj, const char *name, 1233 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1234{ 1235 zap_t *zap; 1236 uint64_t oldval; 1237 const uint64_t *intval = val; 1238 1239#ifdef ZFS_DEBUG 1240 /* 1241 * If there is an old value, it shouldn't change across the 1242 * lockdir (eg, due to bprewrite's xlation). 1243 */ 1244 if (integer_size == 8 && num_integers == 1) 1245 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval); 1246#endif 1247 1248 int err = 1249 zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap); 1250 if (err != 0) 1251 return (err); 1252 zap_name_t *zn = zap_name_alloc(zap, name, 0); 1253 if (zn == NULL) { 1254 zap_unlockdir(zap, FTAG); 1255 return (SET_ERROR(ENOTSUP)); 1256 } 1257 if (!zap->zap_ismicro) { 1258 err = fzap_update(zn, integer_size, num_integers, val, 1259 FTAG, tx); 1260 zap = zn->zn_zap; /* fzap_update() may change zap */ 1261 } else if (integer_size != 8 || num_integers != 1 || 1262 strlen(name) >= MZAP_NAME_LEN) { 1263 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 1264 zapobj, integer_size, num_integers, name); 1265 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0); 1266 if (err == 0) { 1267 err = fzap_update(zn, integer_size, num_integers, 1268 val, FTAG, tx); 1269 } 1270 zap = zn->zn_zap; /* fzap_update() may change zap */ 1271 } else { 1272 mzap_ent_t *mze = mze_find(zn); 1273 if (mze != NULL) { 1274 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval); 1275 MZE_PHYS(zap, mze)->mze_value = *intval; 1276 } else { 1277 mzap_addent(zn, *intval); 1278 } 1279 } 1280 ASSERT(zap == zn->zn_zap); 1281 zap_name_free(zn); 1282 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1283 zap_unlockdir(zap, FTAG); 1284 return (err); 1285} 1286 1287int 1288zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1289 int key_numints, 1290 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1291{ 1292 zap_t *zap; 1293 1294 int err = 1295 zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap); 1296 if (err != 0) 1297 return (err); 1298 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 1299 if (zn == NULL) { 1300 zap_unlockdir(zap, FTAG); 1301 return (SET_ERROR(ENOTSUP)); 1302 } 1303 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx); 1304 zap = zn->zn_zap; /* fzap_update() may change zap */ 1305 zap_name_free(zn); 1306 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1307 zap_unlockdir(zap, FTAG); 1308 return (err); 1309} 1310 1311int 1312zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 1313{ 1314 return (zap_remove_norm(os, zapobj, name, 0, tx)); 1315} 1316 1317static int 1318zap_remove_impl(zap_t *zap, const char *name, 1319 matchtype_t mt, dmu_tx_t *tx) 1320{ 1321 int err = 0; 1322 1323 zap_name_t *zn = zap_name_alloc(zap, name, mt); 1324 if (zn == NULL) 1325 return (SET_ERROR(ENOTSUP)); 1326 if (!zap->zap_ismicro) { 1327 err = fzap_remove(zn, tx); 1328 } else { 1329 mzap_ent_t *mze = mze_find(zn); 1330 if (mze == NULL) { 1331 err = SET_ERROR(ENOENT); 1332 } else { 1333 zap->zap_m.zap_num_entries--; 1334 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid], 1335 sizeof (mzap_ent_phys_t)); 1336 mze_remove(zap, mze); 1337 } 1338 } 1339 zap_name_free(zn); 1340 return (err); 1341} 1342 1343int 1344zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name, 1345 matchtype_t mt, dmu_tx_t *tx) 1346{ 1347 zap_t *zap; 1348 int err; 1349 1350 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap); 1351 if (err) 1352 return (err); 1353 err = zap_remove_impl(zap, name, mt, tx); 1354 zap_unlockdir(zap, FTAG); 1355 return (err); 1356} 1357 1358int 1359zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx) 1360{ 1361 zap_t *zap; 1362 int err; 1363 1364 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap); 1365 if (err) 1366 return (err); 1367 err = zap_remove_impl(zap, name, 0, tx); 1368 zap_unlockdir(zap, FTAG); 1369 return (err); 1370} 1371 1372int 1373zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1374 int key_numints, dmu_tx_t *tx) 1375{ 1376 zap_t *zap; 1377 1378 int err = 1379 zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap); 1380 if (err != 0) 1381 return (err); 1382 zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints); 1383 if (zn == NULL) { 1384 zap_unlockdir(zap, FTAG); 1385 return (SET_ERROR(ENOTSUP)); 1386 } 1387 err = fzap_remove(zn, tx); 1388 zap_name_free(zn); 1389 zap_unlockdir(zap, FTAG); 1390 return (err); 1391} 1392 1393/* 1394 * Routines for iterating over the attributes. 1395 */ 1396 1397static void 1398zap_cursor_init_impl(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 1399 uint64_t serialized, boolean_t prefetch) 1400{ 1401 zc->zc_objset = os; 1402 zc->zc_zap = NULL; 1403 zc->zc_leaf = NULL; 1404 zc->zc_zapobj = zapobj; 1405 zc->zc_serialized = serialized; 1406 zc->zc_hash = 0; 1407 zc->zc_cd = 0; 1408 zc->zc_prefetch = prefetch; 1409} 1410void 1411zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 1412 uint64_t serialized) 1413{ 1414 zap_cursor_init_impl(zc, os, zapobj, serialized, B_TRUE); 1415} 1416 1417/* 1418 * Initialize a cursor at the beginning of the ZAP object. The entire 1419 * ZAP object will be prefetched. 1420 */ 1421void 1422zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 1423{ 1424 zap_cursor_init_impl(zc, os, zapobj, 0, B_TRUE); 1425} 1426 1427/* 1428 * Initialize a cursor at the beginning, but request that we not prefetch 1429 * the entire ZAP object. 1430 */ 1431void 1432zap_cursor_init_noprefetch(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 1433{ 1434 zap_cursor_init_impl(zc, os, zapobj, 0, B_FALSE); 1435} 1436 1437void 1438zap_cursor_fini(zap_cursor_t *zc) 1439{ 1440 if (zc->zc_zap) { 1441 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1442 zap_unlockdir(zc->zc_zap, NULL); 1443 zc->zc_zap = NULL; 1444 } 1445 if (zc->zc_leaf) { 1446 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 1447 zap_put_leaf(zc->zc_leaf); 1448 zc->zc_leaf = NULL; 1449 } 1450 zc->zc_objset = NULL; 1451} 1452 1453uint64_t 1454zap_cursor_serialize(zap_cursor_t *zc) 1455{ 1456 if (zc->zc_hash == -1ULL) 1457 return (-1ULL); 1458 if (zc->zc_zap == NULL) 1459 return (zc->zc_serialized); 1460 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0); 1461 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap)); 1462 1463 /* 1464 * We want to keep the high 32 bits of the cursor zero if we can, so 1465 * that 32-bit programs can access this. So usually use a small 1466 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits 1467 * of the cursor. 1468 * 1469 * [ collision differentiator | zap_hashbits()-bit hash value ] 1470 */ 1471 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) | 1472 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap))); 1473} 1474 1475int 1476zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 1477{ 1478 int err; 1479 1480 if (zc->zc_hash == -1ULL) 1481 return (SET_ERROR(ENOENT)); 1482 1483 if (zc->zc_zap == NULL) { 1484 int hb; 1485 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1486 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap); 1487 if (err != 0) 1488 return (err); 1489 1490 /* 1491 * To support zap_cursor_init_serialized, advance, retrieve, 1492 * we must add to the existing zc_cd, which may already 1493 * be 1 due to the zap_cursor_advance. 1494 */ 1495 ASSERT(zc->zc_hash == 0); 1496 hb = zap_hashbits(zc->zc_zap); 1497 zc->zc_hash = zc->zc_serialized << (64 - hb); 1498 zc->zc_cd += zc->zc_serialized >> hb; 1499 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */ 1500 zc->zc_cd = 0; 1501 } else { 1502 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1503 } 1504 if (!zc->zc_zap->zap_ismicro) { 1505 err = fzap_cursor_retrieve(zc->zc_zap, zc, za); 1506 } else { 1507 avl_index_t idx; 1508 mzap_ent_t mze_tofind; 1509 1510 mze_tofind.mze_hash = zc->zc_hash; 1511 mze_tofind.mze_cd = zc->zc_cd; 1512 1513 mzap_ent_t *mze = 1514 avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx); 1515 if (mze == NULL) { 1516 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl, 1517 idx, AVL_AFTER); 1518 } 1519 if (mze) { 1520 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze); 1521 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd); 1522 za->za_normalization_conflict = 1523 mzap_normalization_conflict(zc->zc_zap, NULL, mze); 1524 za->za_integer_length = 8; 1525 za->za_num_integers = 1; 1526 za->za_first_integer = mzep->mze_value; 1527 (void) strcpy(za->za_name, mzep->mze_name); 1528 zc->zc_hash = mze->mze_hash; 1529 zc->zc_cd = mze->mze_cd; 1530 err = 0; 1531 } else { 1532 zc->zc_hash = -1ULL; 1533 err = SET_ERROR(ENOENT); 1534 } 1535 } 1536 rw_exit(&zc->zc_zap->zap_rwlock); 1537 return (err); 1538} 1539 1540void 1541zap_cursor_advance(zap_cursor_t *zc) 1542{ 1543 if (zc->zc_hash == -1ULL) 1544 return; 1545 zc->zc_cd++; 1546} 1547 1548int 1549zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt) 1550{ 1551 int err = 0; 1552 mzap_ent_t *mze; 1553 zap_name_t *zn; 1554 1555 if (zc->zc_zap == NULL) { 1556 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1557 RW_READER, TRUE, FALSE, FTAG, &zc->zc_zap); 1558 if (err) 1559 return (err); 1560 } else { 1561 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1562 } 1563 1564 zn = zap_name_alloc(zc->zc_zap, name, mt); 1565 if (zn == NULL) { 1566 rw_exit(&zc->zc_zap->zap_rwlock); 1567 return (SET_ERROR(ENOTSUP)); 1568 } 1569 1570 if (!zc->zc_zap->zap_ismicro) { 1571 err = fzap_cursor_move_to_key(zc, zn); 1572 } else { 1573 mze = mze_find(zn); 1574 if (mze == NULL) { 1575 err = SET_ERROR(ENOENT); 1576 goto out; 1577 } 1578 zc->zc_hash = mze->mze_hash; 1579 zc->zc_cd = mze->mze_cd; 1580 } 1581 1582out: 1583 zap_name_free(zn); 1584 rw_exit(&zc->zc_zap->zap_rwlock); 1585 return (err); 1586} 1587 1588int 1589zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 1590{ 1591 zap_t *zap; 1592 1593 int err = 1594 zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap); 1595 if (err != 0) 1596 return (err); 1597 1598 bzero(zs, sizeof (zap_stats_t)); 1599 1600 if (zap->zap_ismicro) { 1601 zs->zs_blocksize = zap->zap_dbuf->db_size; 1602 zs->zs_num_entries = zap->zap_m.zap_num_entries; 1603 zs->zs_num_blocks = 1; 1604 } else { 1605 fzap_get_stats(zap, zs); 1606 } 1607 zap_unlockdir(zap, FTAG); 1608 return (0); 1609} 1610