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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/refcount.h>
34 #include <sys/zap_impl.h>
35 #include <sys/zap_leaf.h>
38 #include <sys/dmu_objset.h>
41 #include <sys/sunddi.h>
44 extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
46 static int mzap_upgrade(zap_t **zapp,
47 void *tag, dmu_tx_t *tx, zap_flags_t flags);
50 zap_getflags(zap_t *zap)
54 return (zap_f_phys(zap)->zap_flags);
58 zap_hashbits(zap_t *zap)
60 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
69 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
76 zap_hash(zap_name_t *zn)
78 zap_t *zap = zn->zn_zap;
81 if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
82 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
83 h = *(uint64_t *)zn->zn_key_orig;
87 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
89 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
91 const uint64_t *wp = zn->zn_key_norm;
93 ASSERT(zn->zn_key_intlen == 8);
94 for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
98 for (j = 0; j < zn->zn_key_intlen; j++) {
100 zfs_crc64_table[(h ^ word) & 0xFF];
106 const uint8_t *cp = zn->zn_key_norm;
109 * We previously stored the terminating null on
110 * disk, but didn't hash it, so we need to
111 * continue to not hash it. (The
112 * zn_key_*_numints includes the terminating
113 * null for non-binary keys.)
115 len = zn->zn_key_norm_numints - 1;
117 ASSERT(zn->zn_key_intlen == 1);
118 for (i = 0; i < len; cp++, i++) {
120 zfs_crc64_table[(h ^ *cp) & 0xFF];
125 * Don't use all 64 bits, since we need some in the cookie for
126 * the collision differentiator. We MUST use the high bits,
127 * since those are the ones that we first pay attention to when
128 * chosing the bucket.
130 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
136 zap_normalize(zap_t *zap, const char *name, char *namenorm)
138 size_t inlen, outlen;
141 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
143 inlen = strlen(name) + 1;
144 outlen = ZAP_MAXNAMELEN;
147 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
148 zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
149 U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
155 zap_match(zap_name_t *zn, const char *matchname)
157 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
159 if (zn->zn_matchtype == MT_FIRST) {
160 char norm[ZAP_MAXNAMELEN];
162 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
165 return (strcmp(zn->zn_key_norm, norm) == 0);
167 /* MT_BEST or MT_EXACT */
168 return (strcmp(zn->zn_key_orig, matchname) == 0);
173 zap_name_free(zap_name_t *zn)
175 kmem_free(zn, sizeof (zap_name_t));
179 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
181 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
184 zn->zn_key_intlen = sizeof (*key);
185 zn->zn_key_orig = key;
186 zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
187 zn->zn_matchtype = mt;
188 if (zap->zap_normflags) {
189 if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
193 zn->zn_key_norm = zn->zn_normbuf;
194 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
196 if (mt != MT_EXACT) {
200 zn->zn_key_norm = zn->zn_key_orig;
201 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
204 zn->zn_hash = zap_hash(zn);
209 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
211 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
213 ASSERT(zap->zap_normflags == 0);
215 zn->zn_key_intlen = sizeof (*key);
216 zn->zn_key_orig = zn->zn_key_norm = key;
217 zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
218 zn->zn_matchtype = MT_EXACT;
220 zn->zn_hash = zap_hash(zn);
225 mzap_byteswap(mzap_phys_t *buf, size_t size)
228 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
229 buf->mz_salt = BSWAP_64(buf->mz_salt);
230 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
231 max = (size / MZAP_ENT_LEN) - 1;
232 for (i = 0; i < max; i++) {
233 buf->mz_chunk[i].mze_value =
234 BSWAP_64(buf->mz_chunk[i].mze_value);
235 buf->mz_chunk[i].mze_cd =
236 BSWAP_32(buf->mz_chunk[i].mze_cd);
241 zap_byteswap(void *buf, size_t size)
245 block_type = *(uint64_t *)buf;
247 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
248 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
249 mzap_byteswap(buf, size);
251 fzap_byteswap(buf, size);
256 mze_compare(const void *arg1, const void *arg2)
258 const mzap_ent_t *mze1 = arg1;
259 const mzap_ent_t *mze2 = arg2;
261 if (mze1->mze_hash > mze2->mze_hash)
263 if (mze1->mze_hash < mze2->mze_hash)
265 if (mze1->mze_cd > mze2->mze_cd)
267 if (mze1->mze_cd < mze2->mze_cd)
273 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
278 ASSERT(zap->zap_ismicro);
279 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
281 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
282 mze->mze_chunkid = chunkid;
283 mze->mze_hash = hash;
284 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
285 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
286 if (avl_find(&zap->zap_m.zap_avl, mze, &idx) != NULL) {
287 kmem_free(mze, sizeof (mzap_ent_t));
290 avl_insert(&zap->zap_m.zap_avl, mze, idx);
295 mze_find(zap_name_t *zn)
297 mzap_ent_t mze_tofind;
300 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
302 ASSERT(zn->zn_zap->zap_ismicro);
303 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
305 mze_tofind.mze_hash = zn->zn_hash;
306 mze_tofind.mze_cd = 0;
309 mze = avl_find(avl, &mze_tofind, &idx);
311 mze = avl_nearest(avl, idx, AVL_AFTER);
312 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
313 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
314 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
317 if (zn->zn_matchtype == MT_BEST) {
318 zn->zn_matchtype = MT_FIRST;
325 mze_find_unused_cd(zap_t *zap, uint64_t hash)
327 mzap_ent_t mze_tofind;
330 avl_tree_t *avl = &zap->zap_m.zap_avl;
333 ASSERT(zap->zap_ismicro);
334 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
336 mze_tofind.mze_hash = hash;
337 mze_tofind.mze_cd = 0;
340 for (mze = avl_find(avl, &mze_tofind, &idx);
341 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
342 if (mze->mze_cd != cd)
351 mze_remove(zap_t *zap, mzap_ent_t *mze)
353 ASSERT(zap->zap_ismicro);
354 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
356 avl_remove(&zap->zap_m.zap_avl, mze);
357 kmem_free(mze, sizeof (mzap_ent_t));
361 mze_destroy(zap_t *zap)
364 void *avlcookie = NULL;
366 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
367 kmem_free(mze, sizeof (mzap_ent_t));
368 avl_destroy(&zap->zap_m.zap_avl);
372 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
377 uint64_t *zap_hdr = (uint64_t *)db->db_data;
378 uint64_t zap_block_type = zap_hdr[0];
379 uint64_t zap_magic = zap_hdr[1];
381 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
383 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
384 rw_init(&zap->zap_rwlock, 0, 0, 0);
385 rw_enter(&zap->zap_rwlock, RW_WRITER);
386 zap->zap_objset = os;
387 zap->zap_object = obj;
390 if (zap_block_type != ZBT_MICRO) {
391 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
392 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
393 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
394 winner = NULL; /* No actual winner here... */
398 zap->zap_ismicro = TRUE;
402 * Make sure that zap_ismicro is set before we let others see
403 * it, because zap_lockdir() checks zap_ismicro without the lock
406 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
407 winner = dmu_buf_set_user(db, &zap->zap_dbu);
412 if (zap->zap_ismicro) {
413 zap->zap_salt = zap_m_phys(zap)->mz_salt;
414 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
415 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
416 avl_create(&zap->zap_m.zap_avl, mze_compare,
417 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
419 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
420 mzap_ent_phys_t *mze =
421 &zap_m_phys(zap)->mz_chunk[i];
422 if (mze->mze_name[0]) {
425 zn = zap_name_alloc(zap, mze->mze_name,
427 if (mze_insert(zap, i, zn->zn_hash) == 0)
428 zap->zap_m.zap_num_entries++;
430 printf("ZFS WARNING: Duplicated ZAP "
431 "entry detected (%s).\n",
438 zap->zap_salt = zap_f_phys(zap)->zap_salt;
439 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
441 ASSERT3U(sizeof (struct zap_leaf_header), ==,
442 2*ZAP_LEAF_CHUNKSIZE);
445 * The embedded pointer table should not overlap the
448 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
449 &zap_f_phys(zap)->zap_salt);
452 * The embedded pointer table should end at the end of
455 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
456 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
457 (uintptr_t)zap_f_phys(zap), ==,
458 zap->zap_dbuf->db_size);
460 rw_exit(&zap->zap_rwlock);
464 rw_exit(&zap->zap_rwlock);
465 rw_destroy(&zap->zap_rwlock);
466 if (!zap->zap_ismicro)
467 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
468 kmem_free(zap, sizeof (zap_t));
473 zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
474 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
479 ASSERT0(db->db_offset);
480 objset_t *os = dmu_buf_get_objset(db);
481 uint64_t obj = db->db_object;
487 dmu_object_info_t doi;
488 dmu_object_info_from_db(db, &doi);
489 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
493 zap = dmu_buf_get_user(db);
495 zap = mzap_open(os, obj, db);
498 * mzap_open() didn't like what it saw on-disk.
499 * Check for corruption!
501 return (SET_ERROR(EIO));
506 * We're checking zap_ismicro without the lock held, in order to
507 * tell what type of lock we want. Once we have some sort of
508 * lock, see if it really is the right type. In practice this
509 * can only be different if it was upgraded from micro to fat,
510 * and micro wanted WRITER but fat only needs READER.
512 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
513 rw_enter(&zap->zap_rwlock, lt);
514 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
515 /* it was upgraded, now we only need reader */
516 ASSERT(lt == RW_WRITER);
518 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
519 rw_downgrade(&zap->zap_rwlock);
523 zap->zap_objset = os;
526 dmu_buf_will_dirty(db, tx);
528 ASSERT3P(zap->zap_dbuf, ==, db);
530 ASSERT(!zap->zap_ismicro ||
531 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
532 if (zap->zap_ismicro && tx && adding &&
533 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
534 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
535 if (newsz > MZAP_MAX_BLKSZ) {
536 dprintf("upgrading obj %llu: num_entries=%u\n",
537 obj, zap->zap_m.zap_num_entries);
539 int err = mzap_upgrade(zapp, tag, tx, 0);
541 rw_exit(&zap->zap_rwlock);
544 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
545 zap->zap_m.zap_num_chunks =
546 db->db_size / MZAP_ENT_LEN - 1;
554 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
555 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
560 err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH);
563 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
565 dmu_buf_rele(db, tag);
570 zap_unlockdir(zap_t *zap, void *tag)
572 rw_exit(&zap->zap_rwlock);
573 dmu_buf_rele(zap->zap_dbuf, tag);
577 mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
584 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
586 sz = zap->zap_dbuf->db_size;
587 mzp = zio_buf_alloc(sz);
588 bcopy(zap->zap_dbuf->db_data, mzp, sz);
589 nchunks = zap->zap_m.zap_num_chunks;
592 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
593 1ULL << fzap_default_block_shift, 0, tx);
595 zio_buf_free(mzp, sz);
600 dprintf("upgrading obj=%llu with %u chunks\n",
601 zap->zap_object, nchunks);
602 /* XXX destroy the avl later, so we can use the stored hash value */
605 fzap_upgrade(zap, tx, flags);
607 for (i = 0; i < nchunks; i++) {
608 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
610 if (mze->mze_name[0] == 0)
612 dprintf("adding %s=%llu\n",
613 mze->mze_name, mze->mze_value);
614 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
615 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
617 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
622 zio_buf_free(mzp, sz);
628 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
634 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
638 dmu_object_info_t doi;
639 dmu_object_info_from_db(db, &doi);
640 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
644 dmu_buf_will_dirty(db, tx);
646 zp->mz_block_type = ZBT_MICRO;
647 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
648 zp->mz_normflags = normflags;
649 dmu_buf_rele(db, FTAG);
653 /* Only fat zap supports flags; upgrade immediately. */
654 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
655 B_FALSE, B_FALSE, FTAG, &zap));
656 VERIFY3U(0, ==, mzap_upgrade(&zap, FTAG, tx, flags));
657 zap_unlockdir(zap, FTAG);
662 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
663 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
665 return (zap_create_claim_norm(os, obj,
666 0, ot, bonustype, bonuslen, tx));
670 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
671 dmu_object_type_t ot,
672 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
676 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
679 mzap_create_impl(os, obj, normflags, 0, tx);
684 zap_create(objset_t *os, dmu_object_type_t ot,
685 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
687 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
691 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
692 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
694 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
696 mzap_create_impl(os, obj, normflags, 0, tx);
701 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
702 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
703 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
705 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
707 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
708 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
709 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
710 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
712 VERIFY(dmu_object_set_blocksize(os, obj,
713 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
715 mzap_create_impl(os, obj, normflags, flags, tx);
720 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
723 * dmu_object_free will free the object number and free the
724 * data. Freeing the data will cause our pageout function to be
725 * called, which will destroy our data (zap_leaf_t's and zap_t).
728 return (dmu_object_free(os, zapobj, tx));
736 rw_destroy(&zap->zap_rwlock);
738 if (zap->zap_ismicro)
741 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
743 kmem_free(zap, sizeof (zap_t));
747 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
752 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
755 if (!zap->zap_ismicro) {
756 err = fzap_count(zap, count);
758 *count = zap->zap_m.zap_num_entries;
760 zap_unlockdir(zap, FTAG);
765 * zn may be NULL; if not specified, it will be computed if needed.
766 * See also the comment above zap_entry_normalization_conflict().
769 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
772 int direction = AVL_BEFORE;
773 boolean_t allocdzn = B_FALSE;
775 if (zap->zap_normflags == 0)
779 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
780 other && other->mze_hash == mze->mze_hash;
781 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
784 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
788 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
795 if (direction == AVL_BEFORE) {
796 direction = AVL_AFTER;
806 * Routines for manipulating attributes.
810 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
811 uint64_t integer_size, uint64_t num_integers, void *buf)
813 return (zap_lookup_norm(os, zapobj, name, integer_size,
814 num_integers, buf, MT_EXACT, NULL, 0, NULL));
818 zap_lookup_impl(zap_t *zap, const char *name,
819 uint64_t integer_size, uint64_t num_integers, void *buf,
820 matchtype_t mt, char *realname, int rn_len,
827 zn = zap_name_alloc(zap, name, mt);
829 return (SET_ERROR(ENOTSUP));
831 if (!zap->zap_ismicro) {
832 err = fzap_lookup(zn, integer_size, num_integers, buf,
833 realname, rn_len, ncp);
837 err = SET_ERROR(ENOENT);
839 if (num_integers < 1) {
840 err = SET_ERROR(EOVERFLOW);
841 } else if (integer_size != 8) {
842 err = SET_ERROR(EINVAL);
845 MZE_PHYS(zap, mze)->mze_value;
846 (void) strlcpy(realname,
847 MZE_PHYS(zap, mze)->mze_name, rn_len);
849 *ncp = mzap_normalization_conflict(zap,
860 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
861 uint64_t integer_size, uint64_t num_integers, void *buf,
862 matchtype_t mt, char *realname, int rn_len,
868 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
871 err = zap_lookup_impl(zap, name, integer_size,
872 num_integers, buf, mt, realname, rn_len, ncp);
873 zap_unlockdir(zap, FTAG);
878 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
885 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
888 zn = zap_name_alloc_uint64(zap, key, key_numints);
890 zap_unlockdir(zap, FTAG);
891 return (SET_ERROR(ENOTSUP));
896 zap_unlockdir(zap, FTAG);
901 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
902 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
908 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
911 zn = zap_name_alloc_uint64(zap, key, key_numints);
913 zap_unlockdir(zap, FTAG);
914 return (SET_ERROR(ENOTSUP));
917 err = fzap_lookup(zn, integer_size, num_integers, buf,
920 zap_unlockdir(zap, FTAG);
925 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
927 int err = zap_lookup_norm(os, zapobj, name, 0,
928 0, NULL, MT_EXACT, NULL, 0, NULL);
929 if (err == EOVERFLOW || err == EINVAL)
930 err = 0; /* found, but skipped reading the value */
935 zap_length(objset_t *os, uint64_t zapobj, const char *name,
936 uint64_t *integer_size, uint64_t *num_integers)
943 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
946 zn = zap_name_alloc(zap, name, MT_EXACT);
948 zap_unlockdir(zap, FTAG);
949 return (SET_ERROR(ENOTSUP));
951 if (!zap->zap_ismicro) {
952 err = fzap_length(zn, integer_size, num_integers);
956 err = SET_ERROR(ENOENT);
965 zap_unlockdir(zap, FTAG);
970 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
971 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
977 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
980 zn = zap_name_alloc_uint64(zap, key, key_numints);
982 zap_unlockdir(zap, FTAG);
983 return (SET_ERROR(ENOTSUP));
985 err = fzap_length(zn, integer_size, num_integers);
987 zap_unlockdir(zap, FTAG);
992 mzap_addent(zap_name_t *zn, uint64_t value)
995 zap_t *zap = zn->zn_zap;
996 int start = zap->zap_m.zap_alloc_next;
999 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1002 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
1003 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1004 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
1008 cd = mze_find_unused_cd(zap, zn->zn_hash);
1009 /* given the limited size of the microzap, this can't happen */
1010 ASSERT(cd < zap_maxcd(zap));
1013 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
1014 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1015 if (mze->mze_name[0] == 0) {
1016 mze->mze_value = value;
1018 (void) strcpy(mze->mze_name, zn->zn_key_orig);
1019 zap->zap_m.zap_num_entries++;
1020 zap->zap_m.zap_alloc_next = i+1;
1021 if (zap->zap_m.zap_alloc_next ==
1022 zap->zap_m.zap_num_chunks)
1023 zap->zap_m.zap_alloc_next = 0;
1024 VERIFY(0 == mze_insert(zap, i, zn->zn_hash));
1032 ASSERT(!"out of entries!");
1036 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1037 int integer_size, uint64_t num_integers,
1038 const void *val, dmu_tx_t *tx)
1043 const uint64_t *intval = val;
1046 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1049 zn = zap_name_alloc(zap, key, MT_EXACT);
1051 zap_unlockdir(zap, FTAG);
1052 return (SET_ERROR(ENOTSUP));
1054 if (!zap->zap_ismicro) {
1055 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1056 zap = zn->zn_zap; /* fzap_add() may change zap */
1057 } else if (integer_size != 8 || num_integers != 1 ||
1058 strlen(key) >= MZAP_NAME_LEN) {
1059 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1061 err = fzap_add(zn, integer_size, num_integers, val,
1064 zap = zn->zn_zap; /* fzap_add() may change zap */
1068 err = SET_ERROR(EEXIST);
1070 mzap_addent(zn, *intval);
1073 ASSERT(zap == zn->zn_zap);
1075 if (zap != NULL) /* may be NULL if fzap_add() failed */
1076 zap_unlockdir(zap, FTAG);
1081 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1082 int key_numints, int integer_size, uint64_t num_integers,
1083 const void *val, dmu_tx_t *tx)
1089 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1092 zn = zap_name_alloc_uint64(zap, key, key_numints);
1094 zap_unlockdir(zap, FTAG);
1095 return (SET_ERROR(ENOTSUP));
1097 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1098 zap = zn->zn_zap; /* fzap_add() may change zap */
1100 if (zap != NULL) /* may be NULL if fzap_add() failed */
1101 zap_unlockdir(zap, FTAG);
1106 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1107 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1112 const uint64_t *intval = val;
1118 * If there is an old value, it shouldn't change across the
1119 * lockdir (eg, due to bprewrite's xlation).
1121 if (integer_size == 8 && num_integers == 1)
1122 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1125 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1128 zn = zap_name_alloc(zap, name, MT_EXACT);
1130 zap_unlockdir(zap, FTAG);
1131 return (SET_ERROR(ENOTSUP));
1133 if (!zap->zap_ismicro) {
1134 err = fzap_update(zn, integer_size, num_integers, val,
1136 zap = zn->zn_zap; /* fzap_update() may change zap */
1137 } else if (integer_size != 8 || num_integers != 1 ||
1138 strlen(name) >= MZAP_NAME_LEN) {
1139 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1140 zapobj, integer_size, num_integers, name);
1141 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1143 err = fzap_update(zn, integer_size, num_integers,
1146 zap = zn->zn_zap; /* fzap_update() may change zap */
1150 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1151 MZE_PHYS(zap, mze)->mze_value = *intval;
1153 mzap_addent(zn, *intval);
1156 ASSERT(zap == zn->zn_zap);
1158 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1159 zap_unlockdir(zap, FTAG);
1164 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1166 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1172 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1175 zn = zap_name_alloc_uint64(zap, key, key_numints);
1177 zap_unlockdir(zap, FTAG);
1178 return (SET_ERROR(ENOTSUP));
1180 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
1181 zap = zn->zn_zap; /* fzap_update() may change zap */
1183 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1184 zap_unlockdir(zap, FTAG);
1189 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1191 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1195 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1196 matchtype_t mt, dmu_tx_t *tx)
1203 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1206 zn = zap_name_alloc(zap, name, mt);
1208 zap_unlockdir(zap, FTAG);
1209 return (SET_ERROR(ENOTSUP));
1211 if (!zap->zap_ismicro) {
1212 err = fzap_remove(zn, tx);
1216 err = SET_ERROR(ENOENT);
1218 zap->zap_m.zap_num_entries--;
1219 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1220 sizeof (mzap_ent_phys_t));
1221 mze_remove(zap, mze);
1225 zap_unlockdir(zap, FTAG);
1230 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1231 int key_numints, dmu_tx_t *tx)
1237 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1240 zn = zap_name_alloc_uint64(zap, key, key_numints);
1242 zap_unlockdir(zap, FTAG);
1243 return (SET_ERROR(ENOTSUP));
1245 err = fzap_remove(zn, tx);
1247 zap_unlockdir(zap, FTAG);
1252 * Routines for iterating over the attributes.
1256 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1257 uint64_t serialized)
1262 zc->zc_zapobj = zapobj;
1263 zc->zc_serialized = serialized;
1269 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1271 zap_cursor_init_serialized(zc, os, zapobj, 0);
1275 zap_cursor_fini(zap_cursor_t *zc)
1278 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1279 zap_unlockdir(zc->zc_zap, NULL);
1283 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1284 zap_put_leaf(zc->zc_leaf);
1287 zc->zc_objset = NULL;
1291 zap_cursor_serialize(zap_cursor_t *zc)
1293 if (zc->zc_hash == -1ULL)
1295 if (zc->zc_zap == NULL)
1296 return (zc->zc_serialized);
1297 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1298 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1301 * We want to keep the high 32 bits of the cursor zero if we can, so
1302 * that 32-bit programs can access this. So usually use a small
1303 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1306 * [ collision differentiator | zap_hashbits()-bit hash value ]
1308 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1309 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1313 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1317 mzap_ent_t mze_tofind;
1320 if (zc->zc_hash == -1ULL)
1321 return (SET_ERROR(ENOENT));
1323 if (zc->zc_zap == NULL) {
1325 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1326 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
1331 * To support zap_cursor_init_serialized, advance, retrieve,
1332 * we must add to the existing zc_cd, which may already
1333 * be 1 due to the zap_cursor_advance.
1335 ASSERT(zc->zc_hash == 0);
1336 hb = zap_hashbits(zc->zc_zap);
1337 zc->zc_hash = zc->zc_serialized << (64 - hb);
1338 zc->zc_cd += zc->zc_serialized >> hb;
1339 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1342 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1344 if (!zc->zc_zap->zap_ismicro) {
1345 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1347 mze_tofind.mze_hash = zc->zc_hash;
1348 mze_tofind.mze_cd = zc->zc_cd;
1350 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1352 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1356 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1357 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1358 za->za_normalization_conflict =
1359 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1360 za->za_integer_length = 8;
1361 za->za_num_integers = 1;
1362 za->za_first_integer = mzep->mze_value;
1363 (void) strcpy(za->za_name, mzep->mze_name);
1364 zc->zc_hash = mze->mze_hash;
1365 zc->zc_cd = mze->mze_cd;
1368 zc->zc_hash = -1ULL;
1369 err = SET_ERROR(ENOENT);
1372 rw_exit(&zc->zc_zap->zap_rwlock);
1377 zap_cursor_advance(zap_cursor_t *zc)
1379 if (zc->zc_hash == -1ULL)
1385 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1391 if (zc->zc_zap == NULL) {
1392 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1393 RW_READER, TRUE, FALSE, &zc->zc_zap);
1397 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1400 zn = zap_name_alloc(zc->zc_zap, name, mt);
1402 rw_exit(&zc->zc_zap->zap_rwlock);
1403 return (SET_ERROR(ENOTSUP));
1406 if (!zc->zc_zap->zap_ismicro) {
1407 err = fzap_cursor_move_to_key(zc, zn);
1411 err = SET_ERROR(ENOENT);
1414 zc->zc_hash = mze->mze_hash;
1415 zc->zc_cd = mze->mze_cd;
1420 rw_exit(&zc->zc_zap->zap_rwlock);
1425 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1430 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1434 bzero(zs, sizeof (zap_stats_t));
1436 if (zap->zap_ismicro) {
1437 zs->zs_blocksize = zap->zap_dbuf->db_size;
1438 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1439 zs->zs_num_blocks = 1;
1441 fzap_get_stats(zap, zs);
1443 zap_unlockdir(zap, FTAG);
1448 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1449 refcount_t *towrite, refcount_t *tooverwrite)
1455 * Since, we don't have a name, we cannot figure out which blocks will
1456 * be affected in this operation. So, account for the worst case :
1457 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1458 * - 4 new blocks written if adding:
1459 * - 2 blocks for possibly split leaves,
1460 * - 2 grown ptrtbl blocks
1462 * This also accommodates the case where an add operation to a fairly
1463 * large microzap results in a promotion to fatzap.
1466 (void) refcount_add_many(towrite,
1467 (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE, FTAG);
1472 * We lock the zap with adding == FALSE. Because, if we pass
1473 * the actual value of add, it could trigger a mzap_upgrade().
1474 * At present we are just evaluating the possibility of this operation
1475 * and hence we do not want to trigger an upgrade.
1477 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE,
1482 if (!zap->zap_ismicro) {
1483 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1485 err = fzap_count_write(zn, add, towrite,
1490 * We treat this case as similar to (name == NULL)
1492 (void) refcount_add_many(towrite,
1493 (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE, FTAG);
1497 * We are here if (name != NULL) and this is a micro-zap.
1498 * We account for the header block depending on whether it
1501 * Incase of an add-operation it is hard to find out
1502 * if this add will promote this microzap to fatzap.
1503 * Hence, we consider the worst case and account for the
1504 * blocks assuming this microzap would be promoted to a
1507 * 1 block overwritten : header block
1508 * 4 new blocks written : 2 new split leaf, 2 grown
1511 if (dmu_buf_freeable(zap->zap_dbuf)) {
1512 (void) refcount_add_many(tooverwrite,
1513 MZAP_MAX_BLKSZ, FTAG);
1515 (void) refcount_add_many(towrite,
1516 MZAP_MAX_BLKSZ, FTAG);
1520 (void) refcount_add_many(towrite,
1521 4 * MZAP_MAX_BLKSZ, FTAG);
1525 zap_unlockdir(zap, FTAG);