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