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_pageout); 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_pageout(dmu_buf_t *db, void *vmzap) 467 { 468 zap_t *zap = vmzap; 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 479 int 480 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 481 { 482 zap_t *zap; 483 int err; 484 485 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 486 if (err) 487 return (err); 488 if (!zap->zap_ismicro) { 489 err = fzap_count(zap, count); 490 } else { 491 *count = zap->zap_m.zap_num_entries; 492 } 493 zap_unlockdir(zap); 494 return (err); 495 } 496 497 /* 498 * Routines for maniplulating attributes. 499 */ 500 501 int 502 zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 503 uint64_t integer_size, uint64_t num_integers, void *buf) 504 { 505 zap_t *zap; 506 int err; 507 mzap_ent_t *mze; 508 509 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 510 if (err) 511 return (err); 512 if (!zap->zap_ismicro) { 513 err = fzap_lookup(zap, name, 514 integer_size, num_integers, buf); 515 } else { 516 mze = mze_find(zap, name, zap_hash(zap, name)); 517 if (mze == NULL) { 518 err = ENOENT; 519 } else { 520 if (num_integers < 1) 521 err = EOVERFLOW; 522 else if (integer_size != 8) 523 err = EINVAL; 524 else 525 *(uint64_t *)buf = mze->mze_phys.mze_value; 526 } 527 } 528 zap_unlockdir(zap); 529 return (err); 530 } 531 532 int 533 zap_length(objset_t *os, uint64_t zapobj, const char *name, 534 uint64_t *integer_size, uint64_t *num_integers) 535 { 536 zap_t *zap; 537 int err; 538 mzap_ent_t *mze; 539 540 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 541 if (err) 542 return (err); 543 if (!zap->zap_ismicro) { 544 err = fzap_length(zap, name, integer_size, num_integers); 545 } else { 546 mze = mze_find(zap, name, zap_hash(zap, name)); 547 if (mze == NULL) { 548 err = ENOENT; 549 } else { 550 if (integer_size) 551 *integer_size = 8; 552 if (num_integers) 553 *num_integers = 1; 554 } 555 } 556 zap_unlockdir(zap); 557 return (err); 558 } 559 560 static void 561 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value) 562 { 563 int i; 564 int start = zap->zap_m.zap_alloc_next; 565 uint32_t cd; 566 567 dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value); 568 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 569 570 #ifdef ZFS_DEBUG 571 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 572 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 573 ASSERT(strcmp(name, mze->mze_name) != 0); 574 } 575 #endif 576 577 cd = mze_find_unused_cd(zap, hash); 578 /* given the limited size of the microzap, this can't happen */ 579 ASSERT(cd != ZAP_MAXCD); 580 581 again: 582 for (i = start; i < zap->zap_m.zap_num_chunks; i++) { 583 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 584 if (mze->mze_name[0] == 0) { 585 mze->mze_value = value; 586 mze->mze_cd = cd; 587 (void) strcpy(mze->mze_name, name); 588 zap->zap_m.zap_num_entries++; 589 zap->zap_m.zap_alloc_next = i+1; 590 if (zap->zap_m.zap_alloc_next == 591 zap->zap_m.zap_num_chunks) 592 zap->zap_m.zap_alloc_next = 0; 593 mze_insert(zap, i, hash, mze); 594 return; 595 } 596 } 597 if (start != 0) { 598 start = 0; 599 goto again; 600 } 601 ASSERT(!"out of entries!"); 602 } 603 604 int 605 zap_add(objset_t *os, uint64_t zapobj, const char *name, 606 int integer_size, uint64_t num_integers, 607 const void *val, dmu_tx_t *tx) 608 { 609 zap_t *zap; 610 int err; 611 mzap_ent_t *mze; 612 const uint64_t *intval = val; 613 uint64_t hash; 614 615 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 616 if (err) 617 return (err); 618 if (!zap->zap_ismicro) { 619 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 620 } else if (integer_size != 8 || num_integers != 1 || 621 strlen(name) >= MZAP_NAME_LEN) { 622 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 623 zapobj, integer_size, num_integers, name); 624 mzap_upgrade(zap, tx); 625 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 626 } else { 627 hash = zap_hash(zap, name); 628 mze = mze_find(zap, name, hash); 629 if (mze != NULL) { 630 err = EEXIST; 631 } else { 632 mzap_addent(zap, name, hash, *intval); 633 } 634 } 635 zap_unlockdir(zap); 636 return (err); 637 } 638 639 int 640 zap_update(objset_t *os, uint64_t zapobj, const char *name, 641 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 642 { 643 zap_t *zap; 644 mzap_ent_t *mze; 645 const uint64_t *intval = val; 646 uint64_t hash; 647 int err; 648 649 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 650 if (err) 651 return (err); 652 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 653 if (!zap->zap_ismicro) { 654 err = fzap_update(zap, name, 655 integer_size, num_integers, val, tx); 656 } else if (integer_size != 8 || num_integers != 1 || 657 strlen(name) >= MZAP_NAME_LEN) { 658 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 659 zapobj, integer_size, num_integers, name); 660 mzap_upgrade(zap, tx); 661 err = fzap_update(zap, name, 662 integer_size, num_integers, val, tx); 663 } else { 664 hash = zap_hash(zap, name); 665 mze = mze_find(zap, name, hash); 666 if (mze != NULL) { 667 mze->mze_phys.mze_value = *intval; 668 zap->zap_m.zap_phys->mz_chunk 669 [mze->mze_chunkid].mze_value = *intval; 670 } else { 671 mzap_addent(zap, name, hash, *intval); 672 } 673 } 674 zap_unlockdir(zap); 675 return (0); 676 } 677 678 int 679 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 680 { 681 zap_t *zap; 682 int err; 683 mzap_ent_t *mze; 684 685 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 686 if (err) 687 return (err); 688 if (!zap->zap_ismicro) { 689 err = fzap_remove(zap, name, tx); 690 } else { 691 mze = mze_find(zap, name, zap_hash(zap, name)); 692 if (mze == NULL) { 693 dprintf("fail: %s\n", name); 694 err = ENOENT; 695 } else { 696 dprintf("success: %s\n", name); 697 zap->zap_m.zap_num_entries--; 698 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 699 sizeof (mzap_ent_phys_t)); 700 mze_remove(zap, mze); 701 } 702 } 703 zap_unlockdir(zap); 704 return (err); 705 } 706 707 708 /* 709 * Routines for iterating over the attributes. 710 */ 711 712 /* 713 * We want to keep the high 32 bits of the cursor zero if we can, so 714 * that 32-bit programs can access this. So use a small hash value so 715 * we can fit 4 bits of cd into the 32-bit cursor. 716 * 717 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ] 718 */ 719 void 720 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 721 uint64_t serialized) 722 { 723 zc->zc_objset = os; 724 zc->zc_zap = NULL; 725 zc->zc_leaf = NULL; 726 zc->zc_zapobj = zapobj; 727 if (serialized == -1ULL) { 728 zc->zc_hash = -1ULL; 729 zc->zc_cd = 0; 730 } else { 731 zc->zc_hash = serialized << (64-ZAP_HASHBITS); 732 zc->zc_cd = serialized >> ZAP_HASHBITS; 733 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */ 734 zc->zc_cd = 0; 735 } 736 } 737 738 void 739 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 740 { 741 zap_cursor_init_serialized(zc, os, zapobj, 0); 742 } 743 744 void 745 zap_cursor_fini(zap_cursor_t *zc) 746 { 747 if (zc->zc_zap) { 748 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 749 zap_unlockdir(zc->zc_zap); 750 zc->zc_zap = NULL; 751 } 752 if (zc->zc_leaf) { 753 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 754 zap_put_leaf(zc->zc_leaf); 755 zc->zc_leaf = NULL; 756 } 757 zc->zc_objset = NULL; 758 } 759 760 uint64_t 761 zap_cursor_serialize(zap_cursor_t *zc) 762 { 763 if (zc->zc_hash == -1ULL) 764 return (-1ULL); 765 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0); 766 ASSERT(zc->zc_cd < ZAP_MAXCD); 767 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) | 768 ((uint64_t)zc->zc_cd << ZAP_HASHBITS)); 769 } 770 771 int 772 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 773 { 774 int err; 775 avl_index_t idx; 776 mzap_ent_t mze_tofind; 777 mzap_ent_t *mze; 778 779 if (zc->zc_hash == -1ULL) 780 return (ENOENT); 781 782 if (zc->zc_zap == NULL) { 783 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 784 RW_READER, TRUE, &zc->zc_zap); 785 if (err) 786 return (err); 787 } else { 788 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 789 } 790 if (!zc->zc_zap->zap_ismicro) { 791 err = fzap_cursor_retrieve(zc->zc_zap, zc, za); 792 } else { 793 err = ENOENT; 794 795 mze_tofind.mze_hash = zc->zc_hash; 796 mze_tofind.mze_phys.mze_cd = zc->zc_cd; 797 798 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx); 799 ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys, 800 &zc->zc_zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 801 sizeof (mze->mze_phys))); 802 if (mze == NULL) { 803 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl, 804 idx, AVL_AFTER); 805 } 806 if (mze) { 807 za->za_integer_length = 8; 808 za->za_num_integers = 1; 809 za->za_first_integer = mze->mze_phys.mze_value; 810 (void) strcpy(za->za_name, mze->mze_phys.mze_name); 811 zc->zc_hash = mze->mze_hash; 812 zc->zc_cd = mze->mze_phys.mze_cd; 813 err = 0; 814 } else { 815 zc->zc_hash = -1ULL; 816 } 817 } 818 rw_exit(&zc->zc_zap->zap_rwlock); 819 return (err); 820 } 821 822 void 823 zap_cursor_advance(zap_cursor_t *zc) 824 { 825 if (zc->zc_hash == -1ULL) 826 return; 827 zc->zc_cd++; 828 if (zc->zc_cd >= ZAP_MAXCD) { 829 zc->zc_cd = 0; 830 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS); 831 if (zc->zc_hash == 0) /* EOF */ 832 zc->zc_hash = -1ULL; 833 } 834 } 835 836 int 837 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 838 { 839 int err; 840 zap_t *zap; 841 842 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 843 if (err) 844 return (err); 845 846 bzero(zs, sizeof (zap_stats_t)); 847 848 if (zap->zap_ismicro) { 849 zs->zs_blocksize = zap->zap_dbuf->db_size; 850 zs->zs_num_entries = zap->zap_m.zap_num_entries; 851 zs->zs_num_blocks = 1; 852 } else { 853 fzap_get_stats(zap, zs); 854 } 855 zap_unlockdir(zap); 856 return (0); 857 } 858