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