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