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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/zfs_context.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
38 #include <sys/sunddi.h>
41 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx);
45 zap_hash(zap_t *zap, const char *normname)
49 uint64_t crc = zap->zap_salt;
51 /* NB: name must already be normalized, if necessary */
54 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
55 for (cp = (const uint8_t *)normname; (c = *cp) != '\0'; cp++) {
56 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF];
60 * Only use 28 bits, since we need 4 bits in the cookie for the
61 * collision differentiator. We MUST use the high bits, since
62 * those are the ones that we first pay attention to when
65 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
71 zap_normalize(zap_t *zap, const char *name, char *namenorm)
76 inlen = strlen(name) + 1;
77 outlen = ZAP_MAXNAMELEN;
80 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
81 zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL, U8_UNICODE_LATEST,
88 zap_match(zap_name_t *zn, const char *matchname)
90 if (zn->zn_matchtype == MT_FIRST) {
91 char norm[ZAP_MAXNAMELEN];
93 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
96 return (strcmp(zn->zn_name_norm, norm) == 0);
98 /* MT_BEST or MT_EXACT */
99 return (strcmp(zn->zn_name_orij, matchname) == 0);
104 zap_name_free(zap_name_t *zn)
106 kmem_free(zn, sizeof (zap_name_t));
109 /* XXX combine this with zap_lockdir()? */
111 zap_name_alloc(zap_t *zap, const char *name, matchtype_t mt)
113 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
116 zn->zn_name_orij = name;
117 zn->zn_matchtype = mt;
118 if (zap->zap_normflags) {
119 if (zap_normalize(zap, name, zn->zn_normbuf) != 0) {
123 zn->zn_name_norm = zn->zn_normbuf;
125 if (mt != MT_EXACT) {
129 zn->zn_name_norm = zn->zn_name_orij;
132 zn->zn_hash = zap_hash(zap, zn->zn_name_norm);
137 mzap_byteswap(mzap_phys_t *buf, size_t size)
140 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
141 buf->mz_salt = BSWAP_64(buf->mz_salt);
142 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
143 max = (size / MZAP_ENT_LEN) - 1;
144 for (i = 0; i < max; i++) {
145 buf->mz_chunk[i].mze_value =
146 BSWAP_64(buf->mz_chunk[i].mze_value);
147 buf->mz_chunk[i].mze_cd =
148 BSWAP_32(buf->mz_chunk[i].mze_cd);
153 zap_byteswap(void *buf, size_t size)
157 block_type = *(uint64_t *)buf;
159 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
160 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
161 mzap_byteswap(buf, size);
163 fzap_byteswap(buf, size);
168 mze_compare(const void *arg1, const void *arg2)
170 const mzap_ent_t *mze1 = arg1;
171 const mzap_ent_t *mze2 = arg2;
173 if (mze1->mze_hash > mze2->mze_hash)
175 if (mze1->mze_hash < mze2->mze_hash)
177 if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd)
179 if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd)
185 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep)
190 ASSERT(zap->zap_ismicro);
191 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
192 ASSERT(mzep->mze_cd < ZAP_MAXCD);
194 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
195 mze->mze_chunkid = chunkid;
196 mze->mze_hash = hash;
197 mze->mze_phys = *mzep;
198 if (avl_find(&zap->zap_m.zap_avl, mze, &idx) != NULL) {
199 kmem_free(mze, sizeof (mzap_ent_t));
202 avl_insert(&zap->zap_m.zap_avl, mze, idx);
207 mze_find(zap_name_t *zn)
209 mzap_ent_t mze_tofind;
212 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
214 ASSERT(zn->zn_zap->zap_ismicro);
215 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
217 if (strlen(zn->zn_name_norm) >= sizeof (mze_tofind.mze_phys.mze_name))
220 mze_tofind.mze_hash = zn->zn_hash;
221 mze_tofind.mze_phys.mze_cd = 0;
224 mze = avl_find(avl, &mze_tofind, &idx);
226 mze = avl_nearest(avl, idx, AVL_AFTER);
227 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
228 if (zap_match(zn, mze->mze_phys.mze_name))
231 if (zn->zn_matchtype == MT_BEST) {
232 zn->zn_matchtype = MT_FIRST;
239 mze_find_unused_cd(zap_t *zap, uint64_t hash)
241 mzap_ent_t mze_tofind;
244 avl_tree_t *avl = &zap->zap_m.zap_avl;
247 ASSERT(zap->zap_ismicro);
248 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
250 mze_tofind.mze_hash = hash;
251 mze_tofind.mze_phys.mze_cd = 0;
254 for (mze = avl_find(avl, &mze_tofind, &idx);
255 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
256 if (mze->mze_phys.mze_cd != cd)
265 mze_remove(zap_t *zap, mzap_ent_t *mze)
267 ASSERT(zap->zap_ismicro);
268 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
270 avl_remove(&zap->zap_m.zap_avl, mze);
271 kmem_free(mze, sizeof (mzap_ent_t));
275 mze_destroy(zap_t *zap)
278 void *avlcookie = NULL;
280 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
281 kmem_free(mze, sizeof (mzap_ent_t));
282 avl_destroy(&zap->zap_m.zap_avl);
286 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
292 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
294 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
295 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, 0);
296 rw_enter(&zap->zap_rwlock, RW_WRITER);
297 zap->zap_objset = os;
298 zap->zap_object = obj;
301 if (*(uint64_t *)db->db_data != ZBT_MICRO) {
302 mutex_init(&zap->zap_f.zap_num_entries_mtx, NULL,
304 zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
306 zap->zap_ismicro = TRUE;
310 * Make sure that zap_ismicro is set before we let others see
311 * it, because zap_lockdir() checks zap_ismicro without the lock
314 winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
316 if (winner != NULL) {
317 rw_exit(&zap->zap_rwlock);
318 rw_destroy(&zap->zap_rwlock);
319 if (!zap->zap_ismicro)
320 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
321 kmem_free(zap, sizeof (zap_t));
325 if (zap->zap_ismicro) {
326 zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
327 zap->zap_normflags = zap->zap_m.zap_phys->mz_normflags;
328 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
329 avl_create(&zap->zap_m.zap_avl, mze_compare,
330 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
332 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
333 mzap_ent_phys_t *mze =
334 &zap->zap_m.zap_phys->mz_chunk[i];
335 if (mze->mze_name[0]) {
338 zn = zap_name_alloc(zap, mze->mze_name,
340 if (mze_insert(zap, i, zn->zn_hash, mze) == 0)
341 zap->zap_m.zap_num_entries++;
343 printf("ZFS WARNING: Duplicated ZAP "
344 "entry detected (%s).\n",
351 zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
352 zap->zap_normflags = zap->zap_f.zap_phys->zap_normflags;
354 ASSERT3U(sizeof (struct zap_leaf_header), ==,
355 2*ZAP_LEAF_CHUNKSIZE);
358 * The embedded pointer table should not overlap the
361 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
362 &zap->zap_f.zap_phys->zap_salt);
365 * The embedded pointer table should end at the end of
368 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
369 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
370 (uintptr_t)zap->zap_f.zap_phys, ==,
371 zap->zap_dbuf->db_size);
373 rw_exit(&zap->zap_rwlock);
378 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
379 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
388 err = dmu_buf_hold(os, obj, 0, NULL, &db);
394 dmu_object_info_t doi;
395 dmu_object_info_from_db(db, &doi);
396 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
400 zap = dmu_buf_get_user(db);
402 zap = mzap_open(os, obj, db);
405 * We're checking zap_ismicro without the lock held, in order to
406 * tell what type of lock we want. Once we have some sort of
407 * lock, see if it really is the right type. In practice this
408 * can only be different if it was upgraded from micro to fat,
409 * and micro wanted WRITER but fat only needs READER.
411 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
412 rw_enter(&zap->zap_rwlock, lt);
413 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
414 /* it was upgraded, now we only need reader */
415 ASSERT(lt == RW_WRITER);
417 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
418 rw_downgrade(&zap->zap_rwlock);
422 zap->zap_objset = os;
425 dmu_buf_will_dirty(db, tx);
427 ASSERT3P(zap->zap_dbuf, ==, db);
429 ASSERT(!zap->zap_ismicro ||
430 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
431 if (zap->zap_ismicro && tx && adding &&
432 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
433 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
434 if (newsz > MZAP_MAX_BLKSZ) {
435 dprintf("upgrading obj %llu: num_entries=%u\n",
436 obj, zap->zap_m.zap_num_entries);
438 return (mzap_upgrade(zapp, tx));
440 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
441 ASSERT3U(err, ==, 0);
442 zap->zap_m.zap_num_chunks =
443 db->db_size / MZAP_ENT_LEN - 1;
451 zap_unlockdir(zap_t *zap)
453 rw_exit(&zap->zap_rwlock);
454 dmu_buf_rele(zap->zap_dbuf, NULL);
458 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx)
461 int i, sz, nchunks, err;
464 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
466 sz = zap->zap_dbuf->db_size;
467 mzp = kmem_alloc(sz, KM_SLEEP);
468 bcopy(zap->zap_dbuf->db_data, mzp, sz);
469 nchunks = zap->zap_m.zap_num_chunks;
471 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
472 1ULL << fzap_default_block_shift, 0, tx);
478 dprintf("upgrading obj=%llu with %u chunks\n",
479 zap->zap_object, nchunks);
480 /* XXX destroy the avl later, so we can use the stored hash value */
483 fzap_upgrade(zap, tx);
485 for (i = 0; i < nchunks; i++) {
487 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
489 if (mze->mze_name[0] == 0)
491 dprintf("adding %s=%llu\n",
492 mze->mze_name, mze->mze_value);
493 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
494 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
495 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
506 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, dmu_tx_t *tx)
511 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
515 dmu_object_info_t doi;
516 dmu_object_info_from_db(db, &doi);
517 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
521 dmu_buf_will_dirty(db, tx);
523 zp->mz_block_type = ZBT_MICRO;
524 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
525 zp->mz_normflags = normflags;
526 dmu_buf_rele(db, FTAG);
530 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
531 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
533 return (zap_create_claim_norm(os, obj,
534 0, ot, bonustype, bonuslen, tx));
538 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
539 dmu_object_type_t ot,
540 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
544 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
547 mzap_create_impl(os, obj, normflags, tx);
552 zap_create(objset_t *os, dmu_object_type_t ot,
553 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
555 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
559 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
560 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
562 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
564 mzap_create_impl(os, obj, normflags, tx);
569 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
572 * dmu_object_free will free the object number and free the
573 * data. Freeing the data will cause our pageout function to be
574 * called, which will destroy our data (zap_leaf_t's and zap_t).
577 return (dmu_object_free(os, zapobj, tx));
582 zap_evict(dmu_buf_t *db, void *vzap)
586 rw_destroy(&zap->zap_rwlock);
588 if (zap->zap_ismicro)
591 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
593 kmem_free(zap, sizeof (zap_t));
597 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
602 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
605 if (!zap->zap_ismicro) {
606 err = fzap_count(zap, count);
608 *count = zap->zap_m.zap_num_entries;
615 * zn may be NULL; if not specified, it will be computed if needed.
616 * See also the comment above zap_entry_normalization_conflict().
619 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
622 int direction = AVL_BEFORE;
623 boolean_t allocdzn = B_FALSE;
625 if (zap->zap_normflags == 0)
629 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
630 other && other->mze_hash == mze->mze_hash;
631 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
634 zn = zap_name_alloc(zap, mze->mze_phys.mze_name,
638 if (zap_match(zn, other->mze_phys.mze_name)) {
645 if (direction == AVL_BEFORE) {
646 direction = AVL_AFTER;
656 * Routines for manipulating attributes.
660 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
661 uint64_t integer_size, uint64_t num_integers, void *buf)
663 return (zap_lookup_norm(os, zapobj, name, integer_size,
664 num_integers, buf, MT_EXACT, NULL, 0, NULL));
668 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
669 uint64_t integer_size, uint64_t num_integers, void *buf,
670 matchtype_t mt, char *realname, int rn_len,
678 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
681 zn = zap_name_alloc(zap, name, mt);
687 if (!zap->zap_ismicro) {
688 err = fzap_lookup(zn, integer_size, num_integers, buf,
689 realname, rn_len, ncp);
695 if (num_integers < 1) {
697 } else if (integer_size != 8) {
700 *(uint64_t *)buf = mze->mze_phys.mze_value;
701 (void) strlcpy(realname,
702 mze->mze_phys.mze_name, rn_len);
704 *ncp = mzap_normalization_conflict(zap,
716 zap_length(objset_t *os, uint64_t zapobj, const char *name,
717 uint64_t *integer_size, uint64_t *num_integers)
724 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
727 zn = zap_name_alloc(zap, name, MT_EXACT);
732 if (!zap->zap_ismicro) {
733 err = fzap_length(zn, integer_size, num_integers);
751 mzap_addent(zap_name_t *zn, uint64_t value)
754 zap_t *zap = zn->zn_zap;
755 int start = zap->zap_m.zap_alloc_next;
758 dprintf("obj=%llu %s=%llu\n", zap->zap_object,
759 zn->zn_name_orij, value);
760 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
763 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
764 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
765 ASSERT(strcmp(zn->zn_name_orij, mze->mze_name) != 0);
769 cd = mze_find_unused_cd(zap, zn->zn_hash);
770 /* given the limited size of the microzap, this can't happen */
771 ASSERT(cd != ZAP_MAXCD);
774 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
775 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
776 if (mze->mze_name[0] == 0) {
777 mze->mze_value = value;
779 (void) strcpy(mze->mze_name, zn->zn_name_orij);
780 zap->zap_m.zap_num_entries++;
781 zap->zap_m.zap_alloc_next = i+1;
782 if (zap->zap_m.zap_alloc_next ==
783 zap->zap_m.zap_num_chunks)
784 zap->zap_m.zap_alloc_next = 0;
785 VERIFY(0 == mze_insert(zap, i, zn->zn_hash, mze));
793 ASSERT(!"out of entries!");
797 zap_add(objset_t *os, uint64_t zapobj, const char *name,
798 int integer_size, uint64_t num_integers,
799 const void *val, dmu_tx_t *tx)
804 const uint64_t *intval = val;
807 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
810 zn = zap_name_alloc(zap, name, MT_EXACT);
815 if (!zap->zap_ismicro) {
816 err = fzap_add(zn, integer_size, num_integers, val, tx);
817 zap = zn->zn_zap; /* fzap_add() may change zap */
818 } else if (integer_size != 8 || num_integers != 1 ||
819 strlen(name) >= MZAP_NAME_LEN) {
820 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
821 zapobj, integer_size, num_integers, name);
822 err = mzap_upgrade(&zn->zn_zap, tx);
824 err = fzap_add(zn, integer_size, num_integers, val, tx);
825 zap = zn->zn_zap; /* fzap_add() may change zap */
831 mzap_addent(zn, *intval);
834 ASSERT(zap == zn->zn_zap);
836 if (zap != NULL) /* may be NULL if fzap_add() failed */
842 zap_update(objset_t *os, uint64_t zapobj, const char *name,
843 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
847 const uint64_t *intval = val;
851 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
854 zn = zap_name_alloc(zap, name, MT_EXACT);
859 if (!zap->zap_ismicro) {
860 err = fzap_update(zn, integer_size, num_integers, val, tx);
861 zap = zn->zn_zap; /* fzap_update() may change zap */
862 } else if (integer_size != 8 || num_integers != 1 ||
863 strlen(name) >= MZAP_NAME_LEN) {
864 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
865 zapobj, integer_size, num_integers, name);
866 err = mzap_upgrade(&zn->zn_zap, tx);
868 err = fzap_update(zn, integer_size, num_integers,
870 zap = zn->zn_zap; /* fzap_update() may change zap */
874 mze->mze_phys.mze_value = *intval;
875 zap->zap_m.zap_phys->mz_chunk
876 [mze->mze_chunkid].mze_value = *intval;
878 mzap_addent(zn, *intval);
881 ASSERT(zap == zn->zn_zap);
883 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
889 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
891 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
895 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
896 matchtype_t mt, dmu_tx_t *tx)
903 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
906 zn = zap_name_alloc(zap, name, mt);
911 if (!zap->zap_ismicro) {
912 err = fzap_remove(zn, tx);
918 zap->zap_m.zap_num_entries--;
919 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
920 sizeof (mzap_ent_phys_t));
921 mze_remove(zap, mze);
930 * Routines for iterating over the attributes.
934 * We want to keep the high 32 bits of the cursor zero if we can, so
935 * that 32-bit programs can access this. So use a small hash value so
936 * we can fit 4 bits of cd into the 32-bit cursor.
938 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ]
941 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
947 zc->zc_zapobj = zapobj;
948 if (serialized == -1ULL) {
952 zc->zc_hash = serialized << (64-ZAP_HASHBITS);
953 zc->zc_cd = serialized >> ZAP_HASHBITS;
954 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */
960 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
962 zap_cursor_init_serialized(zc, os, zapobj, 0);
966 zap_cursor_fini(zap_cursor_t *zc)
969 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
970 zap_unlockdir(zc->zc_zap);
974 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
975 zap_put_leaf(zc->zc_leaf);
978 zc->zc_objset = NULL;
982 zap_cursor_serialize(zap_cursor_t *zc)
984 if (zc->zc_hash == -1ULL)
986 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0);
987 ASSERT(zc->zc_cd < ZAP_MAXCD);
988 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) |
989 ((uint64_t)zc->zc_cd << ZAP_HASHBITS));
993 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
997 mzap_ent_t mze_tofind;
1000 if (zc->zc_hash == -1ULL)
1003 if (zc->zc_zap == NULL) {
1004 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1005 RW_READER, TRUE, FALSE, &zc->zc_zap);
1009 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1011 if (!zc->zc_zap->zap_ismicro) {
1012 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1016 mze_tofind.mze_hash = zc->zc_hash;
1017 mze_tofind.mze_phys.mze_cd = zc->zc_cd;
1019 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1021 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1025 ASSERT(0 == bcmp(&mze->mze_phys,
1026 &zc->zc_zap->zap_m.zap_phys->mz_chunk
1027 [mze->mze_chunkid], sizeof (mze->mze_phys)));
1029 za->za_normalization_conflict =
1030 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1031 za->za_integer_length = 8;
1032 za->za_num_integers = 1;
1033 za->za_first_integer = mze->mze_phys.mze_value;
1034 (void) strcpy(za->za_name, mze->mze_phys.mze_name);
1035 zc->zc_hash = mze->mze_hash;
1036 zc->zc_cd = mze->mze_phys.mze_cd;
1039 zc->zc_hash = -1ULL;
1042 rw_exit(&zc->zc_zap->zap_rwlock);
1047 zap_cursor_advance(zap_cursor_t *zc)
1049 if (zc->zc_hash == -1ULL)
1052 if (zc->zc_cd >= ZAP_MAXCD) {
1054 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS);
1055 if (zc->zc_hash == 0) /* EOF */
1056 zc->zc_hash = -1ULL;
1061 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1066 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1070 bzero(zs, sizeof (zap_stats_t));
1072 if (zap->zap_ismicro) {
1073 zs->zs_blocksize = zap->zap_dbuf->db_size;
1074 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1075 zs->zs_num_blocks = 1;
1077 fzap_get_stats(zap, zs);