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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/spa.h> 29 #include <sys/dmu.h> 30 #include <sys/zfs_context.h> 31 #include <sys/zap.h> 32 #include <sys/refcount.h> 33 #include <sys/zap_impl.h> 34 #include <sys/zap_leaf.h> 35 #include <sys/avl.h> 36 37 38 static uint64_t mzap_write_cookie(zap_t *zap, uint64_t cookie, 39 uint64_t entptr); 40 static void mzap_upgrade(zap_t *zap, dmu_tx_t *tx); 41 42 43 static void 44 mzap_byteswap(mzap_phys_t *buf, size_t size) 45 { 46 int i, max; 47 buf->mz_block_type = BSWAP_64(buf->mz_block_type); 48 buf->mz_salt = BSWAP_64(buf->mz_salt); 49 max = (size / MZAP_ENT_LEN) - 1; 50 for (i = 0; i < max; i++) { 51 buf->mz_chunk[i].mze_value = 52 BSWAP_64(buf->mz_chunk[i].mze_value); 53 buf->mz_chunk[i].mze_cd = 54 BSWAP_32(buf->mz_chunk[i].mze_cd); 55 } 56 } 57 58 void 59 zap_byteswap(void *buf, size_t size) 60 { 61 uint64_t block_type; 62 63 block_type = *(uint64_t *)buf; 64 65 switch (block_type) { 66 case ZBT_MICRO: 67 case BSWAP_64(ZBT_MICRO): 68 /* ASSERT(magic == ZAP_LEAF_MAGIC); */ 69 mzap_byteswap(buf, size); 70 return; 71 default: 72 fzap_byteswap(buf, size); 73 return; 74 } 75 } 76 77 static int 78 mze_compare(const void *arg1, const void *arg2) 79 { 80 const mzap_ent_t *mze1 = arg1; 81 const mzap_ent_t *mze2 = arg2; 82 83 if (mze1->mze_hash > mze2->mze_hash) 84 return (+1); 85 if (mze1->mze_hash < mze2->mze_hash) 86 return (-1); 87 if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd) 88 return (+1); 89 if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd) 90 return (-1); 91 return (0); 92 } 93 94 static void 95 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep) 96 { 97 mzap_ent_t *mze; 98 99 ASSERT(zap->zap_ismicro); 100 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 101 ASSERT(mzep->mze_cd < ZAP_MAXCD); 102 ASSERT3U(zap_hash(zap, mzep->mze_name), ==, hash); 103 104 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP); 105 mze->mze_chunkid = chunkid; 106 mze->mze_hash = hash; 107 mze->mze_phys = *mzep; 108 avl_add(&zap->zap_m.zap_avl, mze); 109 } 110 111 static mzap_ent_t * 112 mze_find(zap_t *zap, const char *name, uint64_t hash) 113 { 114 mzap_ent_t mze_tofind; 115 mzap_ent_t *mze; 116 avl_index_t idx; 117 avl_tree_t *avl = &zap->zap_m.zap_avl; 118 119 ASSERT(zap->zap_ismicro); 120 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 121 ASSERT3U(zap_hash(zap, name), ==, hash); 122 123 if (strlen(name) >= sizeof (mze_tofind.mze_phys.mze_name)) 124 return (NULL); 125 126 mze_tofind.mze_hash = hash; 127 mze_tofind.mze_phys.mze_cd = 0; 128 129 mze = avl_find(avl, &mze_tofind, &idx); 130 if (mze == NULL) 131 mze = avl_nearest(avl, idx, AVL_AFTER); 132 for (; mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) { 133 if (strcmp(name, mze->mze_phys.mze_name) == 0) 134 return (mze); 135 } 136 return (NULL); 137 } 138 139 static uint32_t 140 mze_find_unused_cd(zap_t *zap, uint64_t hash) 141 { 142 mzap_ent_t mze_tofind; 143 mzap_ent_t *mze; 144 avl_index_t idx; 145 avl_tree_t *avl = &zap->zap_m.zap_avl; 146 uint32_t cd; 147 148 ASSERT(zap->zap_ismicro); 149 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 150 151 mze_tofind.mze_hash = hash; 152 mze_tofind.mze_phys.mze_cd = 0; 153 154 cd = 0; 155 for (mze = avl_find(avl, &mze_tofind, &idx); 156 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) { 157 if (mze->mze_phys.mze_cd != cd) 158 break; 159 cd++; 160 } 161 162 return (cd); 163 } 164 165 static void 166 mze_remove(zap_t *zap, mzap_ent_t *mze) 167 { 168 ASSERT(zap->zap_ismicro); 169 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 170 171 avl_remove(&zap->zap_m.zap_avl, mze); 172 kmem_free(mze, sizeof (mzap_ent_t)); 173 } 174 175 static void 176 mze_destroy(zap_t *zap) 177 { 178 mzap_ent_t *mze; 179 void *avlcookie = NULL; 180 181 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie)) 182 kmem_free(mze, sizeof (mzap_ent_t)); 183 avl_destroy(&zap->zap_m.zap_avl); 184 } 185 186 static zap_t * 187 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db) 188 { 189 zap_t *winner; 190 zap_t *zap; 191 int i; 192 193 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t)); 194 195 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP); 196 rw_init(&zap->zap_rwlock, 0, 0, 0); 197 rw_enter(&zap->zap_rwlock, RW_WRITER); 198 zap->zap_objset = os; 199 zap->zap_object = obj; 200 zap->zap_dbuf = db; 201 202 if (((uint64_t *)db->db_data)[0] != ZBT_MICRO) { 203 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0); 204 zap->zap_f.zap_block_shift = highbit(db->db_size) - 1; 205 } else { 206 zap->zap_ismicro = TRUE; 207 } 208 209 /* 210 * Make sure that zap_ismicro is set before we let others see 211 * it, because zap_lockdir() checks zap_ismicro without the lock 212 * held. 213 */ 214 winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict); 215 216 if (winner != NULL) { 217 kmem_free(zap, sizeof (zap_t)); 218 return (winner); 219 } 220 221 if (zap->zap_ismicro) { 222 zap->zap_salt = zap->zap_m.zap_phys->mz_salt; 223 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1; 224 avl_create(&zap->zap_m.zap_avl, mze_compare, 225 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node)); 226 227 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 228 mzap_ent_phys_t *mze = 229 &zap->zap_m.zap_phys->mz_chunk[i]; 230 if (mze->mze_name[0]) { 231 zap->zap_m.zap_num_entries++; 232 mze_insert(zap, i, 233 zap_hash(zap, mze->mze_name), mze); 234 } 235 } 236 } else { 237 zap->zap_salt = zap->zap_f.zap_phys->zap_salt; 238 239 ASSERT3U(sizeof (struct zap_leaf_header), ==, 240 2*ZAP_LEAF_CHUNKSIZE); 241 242 /* 243 * The embedded pointer table should not overlap the 244 * other members. 245 */ 246 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >, 247 &zap->zap_f.zap_phys->zap_salt); 248 249 /* 250 * The embedded pointer table should end at the end of 251 * the block 252 */ 253 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap, 254 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) - 255 (uintptr_t)zap->zap_f.zap_phys, ==, 256 zap->zap_dbuf->db_size); 257 } 258 rw_exit(&zap->zap_rwlock); 259 return (zap); 260 } 261 262 int 263 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx, 264 krw_t lti, int fatreader, zap_t **zapp) 265 { 266 zap_t *zap; 267 dmu_buf_t *db; 268 krw_t lt; 269 int err; 270 271 *zapp = NULL; 272 273 err = dmu_buf_hold(os, obj, 0, NULL, &db); 274 if (err) 275 return (err); 276 277 #ifdef ZFS_DEBUG 278 { 279 dmu_object_info_t doi; 280 dmu_object_info_from_db(db, &doi); 281 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap); 282 } 283 #endif 284 285 zap = dmu_buf_get_user(db); 286 if (zap == NULL) 287 zap = mzap_open(os, obj, db); 288 289 /* 290 * We're checking zap_ismicro without the lock held, in order to 291 * tell what type of lock we want. Once we have some sort of 292 * lock, see if it really is the right type. In practice this 293 * can only be different if it was upgraded from micro to fat, 294 * and micro wanted WRITER but fat only needs READER. 295 */ 296 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti; 297 rw_enter(&zap->zap_rwlock, lt); 298 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) { 299 /* it was upgraded, now we only need reader */ 300 ASSERT(lt == RW_WRITER); 301 ASSERT(RW_READER == 302 (!zap->zap_ismicro && fatreader) ? RW_READER : lti); 303 rw_downgrade(&zap->zap_rwlock); 304 lt = RW_READER; 305 } 306 307 zap->zap_objset = os; 308 309 if (lt == RW_WRITER) 310 dmu_buf_will_dirty(db, tx); 311 312 ASSERT3P(zap->zap_dbuf, ==, db); 313 314 ASSERT(!zap->zap_ismicro || 315 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks); 316 if (zap->zap_ismicro && tx && 317 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) { 318 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE; 319 if (newsz > MZAP_MAX_BLKSZ) { 320 dprintf("upgrading obj %llu: num_entries=%u\n", 321 obj, zap->zap_m.zap_num_entries); 322 mzap_upgrade(zap, tx); 323 *zapp = zap; 324 return (0); 325 } 326 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx); 327 ASSERT3U(err, ==, 0); 328 zap->zap_m.zap_num_chunks = 329 db->db_size / MZAP_ENT_LEN - 1; 330 } 331 332 *zapp = zap; 333 return (0); 334 } 335 336 void 337 zap_unlockdir(zap_t *zap) 338 { 339 rw_exit(&zap->zap_rwlock); 340 dmu_buf_rele(zap->zap_dbuf, NULL); 341 } 342 343 static void 344 mzap_upgrade(zap_t *zap, dmu_tx_t *tx) 345 { 346 mzap_phys_t *mzp; 347 int i, sz, nchunks, err; 348 349 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 350 351 sz = zap->zap_dbuf->db_size; 352 mzp = kmem_alloc(sz, KM_SLEEP); 353 bcopy(zap->zap_dbuf->db_data, mzp, sz); 354 nchunks = zap->zap_m.zap_num_chunks; 355 356 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object, 357 1ULL << fzap_default_block_shift, 0, tx); 358 ASSERT(err == 0); 359 360 dprintf("upgrading obj=%llu with %u chunks\n", 361 zap->zap_object, nchunks); 362 mze_destroy(zap); 363 364 fzap_upgrade(zap, tx); 365 366 for (i = 0; i < nchunks; i++) { 367 int err; 368 mzap_ent_phys_t *mze = &mzp->mz_chunk[i]; 369 if (mze->mze_name[0] == 0) 370 continue; 371 dprintf("adding %s=%llu\n", 372 mze->mze_name, mze->mze_value); 373 err = fzap_add_cd(zap, 374 mze->mze_name, 8, 1, &mze->mze_value, 375 mze->mze_cd, tx); 376 ASSERT3U(err, ==, 0); 377 } 378 kmem_free(mzp, sz); 379 } 380 381 uint64_t 382 zap_hash(zap_t *zap, const char *name) 383 { 384 const uint8_t *cp; 385 uint8_t c; 386 uint64_t crc = zap->zap_salt; 387 388 ASSERT(crc != 0); 389 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 390 for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++) 391 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF]; 392 393 /* 394 * Only use 28 bits, since we need 4 bits in the cookie for the 395 * collision differentiator. We MUST use the high bits, since 396 * those are the onces that we first pay attention to when 397 * chosing the bucket. 398 */ 399 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1); 400 401 return (crc); 402 } 403 404 405 static void 406 mzap_create_impl(objset_t *os, uint64_t obj, dmu_tx_t *tx) 407 { 408 dmu_buf_t *db; 409 mzap_phys_t *zp; 410 411 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db)); 412 413 #ifdef ZFS_DEBUG 414 { 415 dmu_object_info_t doi; 416 dmu_object_info_from_db(db, &doi); 417 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap); 418 } 419 #endif 420 421 dmu_buf_will_dirty(db, tx); 422 zp = db->db_data; 423 zp->mz_block_type = ZBT_MICRO; 424 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL; 425 ASSERT(zp->mz_salt != 0); 426 dmu_buf_rele(db, FTAG); 427 } 428 429 int 430 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot, 431 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 432 { 433 int err; 434 435 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx); 436 if (err != 0) 437 return (err); 438 mzap_create_impl(os, obj, tx); 439 return (0); 440 } 441 442 uint64_t 443 zap_create(objset_t *os, dmu_object_type_t ot, 444 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 445 { 446 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 447 448 mzap_create_impl(os, obj, tx); 449 return (obj); 450 } 451 452 int 453 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx) 454 { 455 /* 456 * dmu_object_free will free the object number and free the 457 * data. Freeing the data will cause our pageout function to be 458 * called, which will destroy our data (zap_leaf_t's and zap_t). 459 */ 460 461 return (dmu_object_free(os, zapobj, tx)); 462 } 463 464 _NOTE(ARGSUSED(0)) 465 void 466 zap_evict(dmu_buf_t *db, void *vzap) 467 { 468 zap_t *zap = vzap; 469 470 rw_destroy(&zap->zap_rwlock); 471 472 if (zap->zap_ismicro) 473 mze_destroy(zap); 474 475 kmem_free(zap, sizeof (zap_t)); 476 } 477 478 int 479 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 480 { 481 zap_t *zap; 482 int err; 483 484 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 485 if (err) 486 return (err); 487 if (!zap->zap_ismicro) { 488 err = fzap_count(zap, count); 489 } else { 490 *count = zap->zap_m.zap_num_entries; 491 } 492 zap_unlockdir(zap); 493 return (err); 494 } 495 496 /* 497 * Routines for maniplulating attributes. 498 */ 499 500 int 501 zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 502 uint64_t integer_size, uint64_t num_integers, void *buf) 503 { 504 zap_t *zap; 505 int err; 506 mzap_ent_t *mze; 507 508 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 509 if (err) 510 return (err); 511 if (!zap->zap_ismicro) { 512 err = fzap_lookup(zap, name, 513 integer_size, num_integers, buf); 514 } else { 515 mze = mze_find(zap, name, zap_hash(zap, name)); 516 if (mze == NULL) { 517 err = ENOENT; 518 } else { 519 if (num_integers < 1) 520 err = EOVERFLOW; 521 else if (integer_size != 8) 522 err = EINVAL; 523 else 524 *(uint64_t *)buf = mze->mze_phys.mze_value; 525 } 526 } 527 zap_unlockdir(zap); 528 return (err); 529 } 530 531 int 532 zap_length(objset_t *os, uint64_t zapobj, const char *name, 533 uint64_t *integer_size, uint64_t *num_integers) 534 { 535 zap_t *zap; 536 int err; 537 mzap_ent_t *mze; 538 539 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 540 if (err) 541 return (err); 542 if (!zap->zap_ismicro) { 543 err = fzap_length(zap, name, integer_size, num_integers); 544 } else { 545 mze = mze_find(zap, name, zap_hash(zap, name)); 546 if (mze == NULL) { 547 err = ENOENT; 548 } else { 549 if (integer_size) 550 *integer_size = 8; 551 if (num_integers) 552 *num_integers = 1; 553 } 554 } 555 zap_unlockdir(zap); 556 return (err); 557 } 558 559 static void 560 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value) 561 { 562 int i; 563 int start = zap->zap_m.zap_alloc_next; 564 uint32_t cd; 565 566 dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value); 567 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 568 569 #ifdef ZFS_DEBUG 570 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 571 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 572 ASSERT(strcmp(name, mze->mze_name) != 0); 573 } 574 #endif 575 576 cd = mze_find_unused_cd(zap, hash); 577 /* given the limited size of the microzap, this can't happen */ 578 ASSERT(cd != ZAP_MAXCD); 579 580 again: 581 for (i = start; i < zap->zap_m.zap_num_chunks; i++) { 582 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 583 if (mze->mze_name[0] == 0) { 584 mze->mze_value = value; 585 mze->mze_cd = cd; 586 (void) strcpy(mze->mze_name, name); 587 zap->zap_m.zap_num_entries++; 588 zap->zap_m.zap_alloc_next = i+1; 589 if (zap->zap_m.zap_alloc_next == 590 zap->zap_m.zap_num_chunks) 591 zap->zap_m.zap_alloc_next = 0; 592 mze_insert(zap, i, hash, mze); 593 return; 594 } 595 } 596 if (start != 0) { 597 start = 0; 598 goto again; 599 } 600 ASSERT(!"out of entries!"); 601 } 602 603 int 604 zap_add(objset_t *os, uint64_t zapobj, const char *name, 605 int integer_size, uint64_t num_integers, 606 const void *val, dmu_tx_t *tx) 607 { 608 zap_t *zap; 609 int err; 610 mzap_ent_t *mze; 611 const uint64_t *intval = val; 612 uint64_t hash; 613 614 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 615 if (err) 616 return (err); 617 if (!zap->zap_ismicro) { 618 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 619 } else if (integer_size != 8 || num_integers != 1 || 620 strlen(name) >= MZAP_NAME_LEN) { 621 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 622 zapobj, integer_size, num_integers, name); 623 mzap_upgrade(zap, tx); 624 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 625 } else { 626 hash = zap_hash(zap, name); 627 mze = mze_find(zap, name, hash); 628 if (mze != NULL) { 629 err = EEXIST; 630 } else { 631 mzap_addent(zap, name, hash, *intval); 632 } 633 } 634 zap_unlockdir(zap); 635 return (err); 636 } 637 638 int 639 zap_update(objset_t *os, uint64_t zapobj, const char *name, 640 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 641 { 642 zap_t *zap; 643 mzap_ent_t *mze; 644 const uint64_t *intval = val; 645 uint64_t hash; 646 int err; 647 648 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 649 if (err) 650 return (err); 651 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 652 if (!zap->zap_ismicro) { 653 err = fzap_update(zap, name, 654 integer_size, num_integers, val, tx); 655 } else if (integer_size != 8 || num_integers != 1 || 656 strlen(name) >= MZAP_NAME_LEN) { 657 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 658 zapobj, integer_size, num_integers, name); 659 mzap_upgrade(zap, tx); 660 err = fzap_update(zap, name, 661 integer_size, num_integers, val, tx); 662 } else { 663 hash = zap_hash(zap, name); 664 mze = mze_find(zap, name, hash); 665 if (mze != NULL) { 666 mze->mze_phys.mze_value = *intval; 667 zap->zap_m.zap_phys->mz_chunk 668 [mze->mze_chunkid].mze_value = *intval; 669 } else { 670 mzap_addent(zap, name, hash, *intval); 671 } 672 } 673 zap_unlockdir(zap); 674 return (err); 675 } 676 677 int 678 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 679 { 680 zap_t *zap; 681 int err; 682 mzap_ent_t *mze; 683 684 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 685 if (err) 686 return (err); 687 if (!zap->zap_ismicro) { 688 err = fzap_remove(zap, name, tx); 689 } else { 690 mze = mze_find(zap, name, zap_hash(zap, name)); 691 if (mze == NULL) { 692 dprintf("fail: %s\n", name); 693 err = ENOENT; 694 } else { 695 dprintf("success: %s\n", name); 696 zap->zap_m.zap_num_entries--; 697 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 698 sizeof (mzap_ent_phys_t)); 699 mze_remove(zap, mze); 700 } 701 } 702 zap_unlockdir(zap); 703 return (err); 704 } 705 706 707 /* 708 * Routines for iterating over the attributes. 709 */ 710 711 /* 712 * We want to keep the high 32 bits of the cursor zero if we can, so 713 * that 32-bit programs can access this. So use a small hash value so 714 * we can fit 4 bits of cd into the 32-bit cursor. 715 * 716 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ] 717 */ 718 void 719 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 720 uint64_t serialized) 721 { 722 zc->zc_objset = os; 723 zc->zc_zap = NULL; 724 zc->zc_leaf = NULL; 725 zc->zc_zapobj = zapobj; 726 if (serialized == -1ULL) { 727 zc->zc_hash = -1ULL; 728 zc->zc_cd = 0; 729 } else { 730 zc->zc_hash = serialized << (64-ZAP_HASHBITS); 731 zc->zc_cd = serialized >> ZAP_HASHBITS; 732 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */ 733 zc->zc_cd = 0; 734 } 735 } 736 737 void 738 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 739 { 740 zap_cursor_init_serialized(zc, os, zapobj, 0); 741 } 742 743 void 744 zap_cursor_fini(zap_cursor_t *zc) 745 { 746 if (zc->zc_zap) { 747 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 748 zap_unlockdir(zc->zc_zap); 749 zc->zc_zap = NULL; 750 } 751 if (zc->zc_leaf) { 752 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 753 zap_put_leaf(zc->zc_leaf); 754 zc->zc_leaf = NULL; 755 } 756 zc->zc_objset = NULL; 757 } 758 759 uint64_t 760 zap_cursor_serialize(zap_cursor_t *zc) 761 { 762 if (zc->zc_hash == -1ULL) 763 return (-1ULL); 764 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0); 765 ASSERT(zc->zc_cd < ZAP_MAXCD); 766 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) | 767 ((uint64_t)zc->zc_cd << ZAP_HASHBITS)); 768 } 769 770 int 771 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 772 { 773 int err; 774 avl_index_t idx; 775 mzap_ent_t mze_tofind; 776 mzap_ent_t *mze; 777 778 if (zc->zc_hash == -1ULL) 779 return (ENOENT); 780 781 if (zc->zc_zap == NULL) { 782 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 783 RW_READER, TRUE, &zc->zc_zap); 784 if (err) 785 return (err); 786 } else { 787 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 788 } 789 if (!zc->zc_zap->zap_ismicro) { 790 err = fzap_cursor_retrieve(zc->zc_zap, zc, za); 791 } else { 792 err = ENOENT; 793 794 mze_tofind.mze_hash = zc->zc_hash; 795 mze_tofind.mze_phys.mze_cd = zc->zc_cd; 796 797 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx); 798 ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys, 799 &zc->zc_zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 800 sizeof (mze->mze_phys))); 801 if (mze == NULL) { 802 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl, 803 idx, AVL_AFTER); 804 } 805 if (mze) { 806 za->za_integer_length = 8; 807 za->za_num_integers = 1; 808 za->za_first_integer = mze->mze_phys.mze_value; 809 (void) strcpy(za->za_name, mze->mze_phys.mze_name); 810 zc->zc_hash = mze->mze_hash; 811 zc->zc_cd = mze->mze_phys.mze_cd; 812 err = 0; 813 } else { 814 zc->zc_hash = -1ULL; 815 } 816 } 817 rw_exit(&zc->zc_zap->zap_rwlock); 818 return (err); 819 } 820 821 void 822 zap_cursor_advance(zap_cursor_t *zc) 823 { 824 if (zc->zc_hash == -1ULL) 825 return; 826 zc->zc_cd++; 827 if (zc->zc_cd >= ZAP_MAXCD) { 828 zc->zc_cd = 0; 829 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS); 830 if (zc->zc_hash == 0) /* EOF */ 831 zc->zc_hash = -1ULL; 832 } 833 } 834 835 int 836 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 837 { 838 int err; 839 zap_t *zap; 840 841 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 842 if (err) 843 return (err); 844 845 bzero(zs, sizeof (zap_stats_t)); 846 847 if (zap->zap_ismicro) { 848 zs->zs_blocksize = zap->zap_dbuf->db_size; 849 zs->zs_num_entries = zap->zap_m.zap_num_entries; 850 zs->zs_num_blocks = 1; 851 } else { 852 fzap_get_stats(zap, zs); 853 } 854 zap_unlockdir(zap); 855 return (0); 856 } 857