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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/spa.h> 30 #include <sys/dmu.h> 31 #include <sys/zfs_context.h> 32 #include <sys/zap.h> 33 #include <sys/zap_impl.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 ASSERT(size == (1<<ZAP_BLOCK_SHIFT)); 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 } 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 rw_exit(&zap->zap_rwlock); 239 return (zap); 240 } 241 242 int 243 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx, 244 krw_t lti, int fatreader, zap_t **zapp) 245 { 246 zap_t *zap; 247 dmu_buf_t *db; 248 krw_t lt; 249 int err; 250 251 *zapp = NULL; 252 253 db = dmu_buf_hold(os, obj, 0); 254 255 #ifdef ZFS_DEBUG 256 { 257 dmu_object_info_t doi; 258 dmu_object_info_from_db(db, &doi); 259 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap); 260 } 261 #endif 262 263 /* 264 * The zap can deal with EIO here, but its callers don't yet, so 265 * spare them by doing a mustsucceed read. 266 */ 267 dmu_buf_read(db); 268 269 zap = dmu_buf_get_user(db); 270 if (zap == NULL) 271 zap = mzap_open(os, obj, db); 272 273 /* 274 * We're checking zap_ismicro without the lock held, in order to 275 * tell what type of lock we want. Once we have some sort of 276 * lock, see if it really is the right type. In practice this 277 * can only be different if it was upgraded from micro to fat, 278 * and micro wanted WRITER but fat only needs READER. 279 */ 280 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti; 281 rw_enter(&zap->zap_rwlock, lt); 282 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) { 283 /* it was upgraded, now we only need reader */ 284 ASSERT(lt == RW_WRITER); 285 ASSERT(RW_READER == 286 (!zap->zap_ismicro && fatreader) ? RW_READER : lti); 287 rw_downgrade(&zap->zap_rwlock); 288 lt = RW_READER; 289 } 290 291 zap->zap_objset = os; 292 293 if (lt == RW_WRITER) 294 dmu_buf_will_dirty(db, tx); 295 296 ASSERT3P(zap->zap_dbuf, ==, db); 297 298 ASSERT(!zap->zap_ismicro || 299 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks); 300 if (zap->zap_ismicro && tx && 301 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) { 302 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE; 303 if (newsz > MZAP_MAX_BLKSZ) { 304 dprintf("upgrading obj %llu: num_entries=%u\n", 305 obj, zap->zap_m.zap_num_entries); 306 mzap_upgrade(zap, tx); 307 *zapp = zap; 308 return (0); 309 } 310 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx); 311 ASSERT3U(err, ==, 0); 312 zap->zap_m.zap_num_chunks = 313 db->db_size / MZAP_ENT_LEN - 1; 314 } 315 316 *zapp = zap; 317 return (0); 318 } 319 320 void 321 zap_unlockdir(zap_t *zap) 322 { 323 rw_exit(&zap->zap_rwlock); 324 dmu_buf_rele(zap->zap_dbuf); 325 } 326 327 static void 328 mzap_upgrade(zap_t *zap, dmu_tx_t *tx) 329 { 330 mzap_phys_t *mzp; 331 int i, sz, nchunks, err; 332 333 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 334 335 sz = zap->zap_dbuf->db_size; 336 mzp = kmem_alloc(sz, KM_SLEEP); 337 bcopy(zap->zap_dbuf->db_data, mzp, sz); 338 nchunks = zap->zap_m.zap_num_chunks; 339 340 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object, 341 1ULL << ZAP_BLOCK_SHIFT, 0, tx); 342 ASSERT(err == 0); 343 344 dprintf("upgrading obj=%llu with %u chunks\n", 345 zap->zap_object, nchunks); 346 mze_destroy(zap); 347 348 fzap_upgrade(zap, tx); 349 350 for (i = 0; i < nchunks; i++) { 351 int err; 352 mzap_ent_phys_t *mze = &mzp->mz_chunk[i]; 353 if (mze->mze_name[0] == 0) 354 continue; 355 dprintf("adding %s=%llu\n", 356 mze->mze_name, mze->mze_value); 357 err = fzap_add_cd(zap, 358 mze->mze_name, 8, 1, &mze->mze_value, 359 mze->mze_cd, tx, NULL); 360 ASSERT3U(err, ==, 0); 361 } 362 kmem_free(mzp, sz); 363 } 364 365 uint64_t 366 zap_hash(zap_t *zap, const char *name) 367 { 368 const uint8_t *cp; 369 uint8_t c; 370 uint64_t crc = zap->zap_salt; 371 372 ASSERT(crc != 0); 373 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 374 for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++) 375 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF]; 376 377 /* 378 * Only use 28 bits, since we need 4 bits in the cookie for the 379 * collision differentiator. We MUST use the high bits, since 380 * those are the onces that we first pay attention to when 381 * chosing the bucket. 382 */ 383 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1); 384 385 return (crc); 386 } 387 388 389 static void 390 mzap_create_impl(objset_t *os, uint64_t obj, dmu_tx_t *tx) 391 { 392 dmu_buf_t *db; 393 mzap_phys_t *zp; 394 395 db = dmu_buf_hold(os, obj, 0); 396 397 #ifdef ZFS_DEBUG 398 { 399 dmu_object_info_t doi; 400 dmu_object_info_from_db(db, &doi); 401 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap); 402 } 403 #endif 404 405 dmu_buf_will_dirty(db, tx); 406 zp = db->db_data; 407 zp->mz_block_type = ZBT_MICRO; 408 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL; 409 ASSERT(zp->mz_salt != 0); 410 dmu_buf_rele(db); 411 } 412 413 int 414 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot, 415 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 416 { 417 int err; 418 419 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx); 420 if (err != 0) 421 return (err); 422 mzap_create_impl(os, obj, tx); 423 return (0); 424 } 425 426 uint64_t 427 zap_create(objset_t *os, dmu_object_type_t ot, 428 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 429 { 430 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 431 432 mzap_create_impl(os, obj, tx); 433 return (obj); 434 } 435 436 int 437 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx) 438 { 439 /* 440 * dmu_object_free will free the object number and free the 441 * data. Freeing the data will cause our pageout function to be 442 * called, which will destroy our data (zap_leaf_t's and zap_t). 443 */ 444 445 return (dmu_object_free(os, zapobj, tx)); 446 } 447 448 _NOTE(ARGSUSED(0)) 449 void 450 zap_pageout(dmu_buf_t *db, void *vmzap) 451 { 452 zap_t *zap = vmzap; 453 454 rw_destroy(&zap->zap_rwlock); 455 456 if (zap->zap_ismicro) { 457 mze_destroy(zap); 458 } 459 460 kmem_free(zap, sizeof (zap_t)); 461 } 462 463 464 int 465 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 466 { 467 zap_t *zap; 468 int err; 469 470 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 471 if (err) 472 return (err); 473 if (!zap->zap_ismicro) { 474 err = fzap_count(zap, count); 475 } else { 476 *count = zap->zap_m.zap_num_entries; 477 } 478 zap_unlockdir(zap); 479 return (err); 480 } 481 482 /* 483 * Routines for maniplulating attributes. 484 */ 485 486 int 487 zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 488 uint64_t integer_size, uint64_t num_integers, void *buf) 489 { 490 zap_t *zap; 491 int err; 492 mzap_ent_t *mze; 493 494 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 495 if (err) 496 return (err); 497 if (!zap->zap_ismicro) { 498 err = fzap_lookup(zap, name, 499 integer_size, num_integers, buf); 500 } else { 501 mze = mze_find(zap, name, zap_hash(zap, name)); 502 if (mze == NULL) { 503 err = ENOENT; 504 } else { 505 if (num_integers < 1) 506 err = EOVERFLOW; 507 else if (integer_size != 8) 508 err = EINVAL; 509 else 510 *(uint64_t *)buf = mze->mze_phys.mze_value; 511 } 512 } 513 zap_unlockdir(zap); 514 return (err); 515 } 516 517 int 518 zap_length(objset_t *os, uint64_t zapobj, const char *name, 519 uint64_t *integer_size, uint64_t *num_integers) 520 { 521 zap_t *zap; 522 int err; 523 mzap_ent_t *mze; 524 525 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 526 if (err) 527 return (err); 528 if (!zap->zap_ismicro) { 529 err = fzap_length(zap, name, integer_size, num_integers); 530 } else { 531 mze = mze_find(zap, name, zap_hash(zap, name)); 532 if (mze == NULL) { 533 err = ENOENT; 534 } else { 535 if (integer_size) 536 *integer_size = 8; 537 if (num_integers) 538 *num_integers = 1; 539 } 540 } 541 zap_unlockdir(zap); 542 return (err); 543 } 544 545 static void 546 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value) 547 { 548 int i; 549 int start = zap->zap_m.zap_alloc_next; 550 uint32_t cd; 551 552 dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value); 553 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 554 555 #ifdef ZFS_DEBUG 556 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 557 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 558 ASSERT(strcmp(name, mze->mze_name) != 0); 559 } 560 #endif 561 562 cd = mze_find_unused_cd(zap, hash); 563 /* given the limited size of the microzap, this can't happen */ 564 ASSERT(cd != ZAP_MAXCD); 565 566 again: 567 for (i = start; i < zap->zap_m.zap_num_chunks; i++) { 568 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i]; 569 if (mze->mze_name[0] == 0) { 570 mze->mze_value = value; 571 mze->mze_cd = cd; 572 (void) strcpy(mze->mze_name, name); 573 zap->zap_m.zap_num_entries++; 574 zap->zap_m.zap_alloc_next = i+1; 575 if (zap->zap_m.zap_alloc_next == 576 zap->zap_m.zap_num_chunks) 577 zap->zap_m.zap_alloc_next = 0; 578 mze_insert(zap, i, hash, mze); 579 return; 580 } 581 } 582 if (start != 0) { 583 start = 0; 584 goto again; 585 } 586 ASSERT(!"out of entries!"); 587 } 588 589 int 590 zap_add(objset_t *os, uint64_t zapobj, const char *name, 591 int integer_size, uint64_t num_integers, 592 const void *val, dmu_tx_t *tx) 593 { 594 zap_t *zap; 595 int err; 596 mzap_ent_t *mze; 597 const uint64_t *intval = val; 598 uint64_t hash; 599 600 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 601 if (err) 602 return (err); 603 if (!zap->zap_ismicro) { 604 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 605 } else if (integer_size != 8 || num_integers != 1 || 606 strlen(name) >= MZAP_NAME_LEN) { 607 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 608 zapobj, integer_size, num_integers, name); 609 mzap_upgrade(zap, tx); 610 err = fzap_add(zap, name, integer_size, num_integers, val, tx); 611 } else { 612 hash = zap_hash(zap, name); 613 mze = mze_find(zap, name, hash); 614 if (mze != NULL) { 615 err = EEXIST; 616 } else { 617 mzap_addent(zap, name, hash, *intval); 618 } 619 } 620 zap_unlockdir(zap); 621 return (err); 622 } 623 624 int 625 zap_update(objset_t *os, uint64_t zapobj, const char *name, 626 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 627 { 628 zap_t *zap; 629 mzap_ent_t *mze; 630 const uint64_t *intval = val; 631 uint64_t hash; 632 int err; 633 634 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 635 if (err) 636 return (err); 637 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 638 if (!zap->zap_ismicro) { 639 err = fzap_update(zap, name, 640 integer_size, num_integers, val, tx); 641 } else if (integer_size != 8 || num_integers != 1 || 642 strlen(name) >= MZAP_NAME_LEN) { 643 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 644 zapobj, integer_size, num_integers, name); 645 mzap_upgrade(zap, tx); 646 err = fzap_update(zap, name, 647 integer_size, num_integers, val, tx); 648 } else { 649 hash = zap_hash(zap, name); 650 mze = mze_find(zap, name, hash); 651 if (mze != NULL) { 652 mze->mze_phys.mze_value = *intval; 653 zap->zap_m.zap_phys->mz_chunk 654 [mze->mze_chunkid].mze_value = *intval; 655 } else { 656 mzap_addent(zap, name, hash, *intval); 657 } 658 } 659 zap_unlockdir(zap); 660 return (0); 661 } 662 663 int 664 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 665 { 666 zap_t *zap; 667 int err; 668 mzap_ent_t *mze; 669 670 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap); 671 if (err) 672 return (err); 673 if (!zap->zap_ismicro) { 674 err = fzap_remove(zap, name, tx); 675 } else { 676 mze = mze_find(zap, name, zap_hash(zap, name)); 677 if (mze == NULL) { 678 dprintf("fail: %s\n", name); 679 err = ENOENT; 680 } else { 681 dprintf("success: %s\n", name); 682 zap->zap_m.zap_num_entries--; 683 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 684 sizeof (mzap_ent_phys_t)); 685 mze_remove(zap, mze); 686 } 687 } 688 zap_unlockdir(zap); 689 return (err); 690 } 691 692 693 /* 694 * Routines for iterating over the attributes. 695 */ 696 697 void 698 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 699 { 700 zc->zc_objset = os; 701 zc->zc_zapobj = zapobj; 702 zc->zc_hash = 0; 703 zc->zc_cd = 0; 704 } 705 706 /* 707 * We want to keep the high 32 bits of the cursor zero if we can, so 708 * that 32-bit programs can access this. So use a small hash value so 709 * we can fit 4 bits of cd into the 32-bit cursor. 710 * 711 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ] 712 */ 713 void 714 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 715 uint64_t serialized) 716 { 717 zc->zc_objset = os; 718 zc->zc_zapobj = zapobj; 719 if (serialized == -1ULL) { 720 zc->zc_hash = -1ULL; 721 zc->zc_cd = 0; 722 } else { 723 zc->zc_hash = serialized << (64-ZAP_HASHBITS); 724 zc->zc_cd = serialized >> ZAP_HASHBITS; 725 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */ 726 zc->zc_cd = 0; 727 } 728 } 729 730 uint64_t 731 zap_cursor_serialize(zap_cursor_t *zc) 732 { 733 if (zc->zc_hash == -1ULL) 734 return (-1ULL); 735 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0); 736 ASSERT(zc->zc_cd < ZAP_MAXCD); 737 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) | 738 ((uint64_t)zc->zc_cd << ZAP_HASHBITS)); 739 } 740 741 int 742 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 743 { 744 zap_t *zap; 745 int err; 746 avl_index_t idx; 747 mzap_ent_t mze_tofind; 748 mzap_ent_t *mze; 749 750 if (zc->zc_hash == -1ULL) 751 return (ENOENT); 752 753 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 754 RW_READER, TRUE, &zap); 755 if (err) 756 return (err); 757 if (!zap->zap_ismicro) { 758 err = fzap_cursor_retrieve(zap, zc, za); 759 } else { 760 err = ENOENT; 761 762 mze_tofind.mze_hash = zc->zc_hash; 763 mze_tofind.mze_phys.mze_cd = zc->zc_cd; 764 765 mze = avl_find(&zap->zap_m.zap_avl, &mze_tofind, &idx); 766 ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys, 767 &zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid], 768 sizeof (mze->mze_phys))); 769 if (mze == NULL) 770 mze = avl_nearest(&zap->zap_m.zap_avl, idx, AVL_AFTER); 771 772 if (mze) { 773 za->za_integer_length = 8; 774 za->za_num_integers = 1; 775 za->za_first_integer = mze->mze_phys.mze_value; 776 (void) strcpy(za->za_name, mze->mze_phys.mze_name); 777 zc->zc_hash = mze->mze_hash; 778 zc->zc_cd = mze->mze_phys.mze_cd; 779 err = 0; 780 } else { 781 zc->zc_hash = -1ULL; 782 } 783 } 784 zap_unlockdir(zap); 785 return (err); 786 } 787 788 void 789 zap_cursor_advance(zap_cursor_t *zc) 790 { 791 if (zc->zc_hash == -1ULL) 792 return; 793 zc->zc_cd++; 794 if (zc->zc_cd >= ZAP_MAXCD) { 795 zc->zc_cd = 0; 796 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS); 797 if (zc->zc_hash == 0) /* EOF */ 798 zc->zc_hash = -1ULL; 799 } 800 } 801 802 int 803 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 804 { 805 int err; 806 zap_t *zap; 807 808 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap); 809 if (err) 810 return (err); 811 812 bzero(zs, sizeof (zap_stats_t)); 813 814 if (zap->zap_ismicro) { 815 zs->zs_blocksize = zap->zap_dbuf->db_size; 816 zs->zs_num_entries = zap->zap_m.zap_num_entries; 817 zs->zs_num_blocks = 1; 818 } else { 819 fzap_get_stats(zap, zs); 820 } 821 zap_unlockdir(zap); 822 return (0); 823 } 824