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