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