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.
30 #include <sys/zfs_context.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
37 #include <sys/dmu_objset.h>
40 #include <sys/sunddi.h>
43 extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
45 static int mzap_upgrade(zap_t **zapp,
46 void *tag, dmu_tx_t *tx, zap_flags_t flags);
49 zap_getflags(zap_t *zap)
53 return (zap_f_phys(zap)->zap_flags);
57 zap_hashbits(zap_t *zap)
59 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
68 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
75 zap_hash(zap_name_t *zn)
77 zap_t *zap = zn->zn_zap;
80 if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
81 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
82 h = *(uint64_t *)zn->zn_key_orig;
86 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
88 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
90 const uint64_t *wp = zn->zn_key_norm;
92 ASSERT(zn->zn_key_intlen == 8);
93 for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
97 for (j = 0; j < zn->zn_key_intlen; j++) {
99 zfs_crc64_table[(h ^ word) & 0xFF];
105 const uint8_t *cp = zn->zn_key_norm;
108 * We previously stored the terminating null on
109 * disk, but didn't hash it, so we need to
110 * continue to not hash it. (The
111 * zn_key_*_numints includes the terminating
112 * null for non-binary keys.)
114 len = zn->zn_key_norm_numints - 1;
116 ASSERT(zn->zn_key_intlen == 1);
117 for (i = 0; i < len; cp++, i++) {
119 zfs_crc64_table[(h ^ *cp) & 0xFF];
124 * Don't use all 64 bits, since we need some in the cookie for
125 * the collision differentiator. We MUST use the high bits,
126 * since those are the ones that we first pay attention to when
127 * chosing the bucket.
129 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
135 zap_normalize(zap_t *zap, const char *name, char *namenorm)
137 size_t inlen, outlen;
140 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
142 inlen = strlen(name) + 1;
143 outlen = ZAP_MAXNAMELEN;
146 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
147 zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
148 U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
154 zap_match(zap_name_t *zn, const char *matchname)
156 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
158 if (zn->zn_matchtype == MT_FIRST) {
159 char norm[ZAP_MAXNAMELEN];
161 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
164 return (strcmp(zn->zn_key_norm, norm) == 0);
166 /* MT_BEST or MT_EXACT */
167 return (strcmp(zn->zn_key_orig, matchname) == 0);
172 zap_name_free(zap_name_t *zn)
174 kmem_free(zn, sizeof (zap_name_t));
178 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
180 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
183 zn->zn_key_intlen = sizeof (*key);
184 zn->zn_key_orig = key;
185 zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
186 zn->zn_matchtype = mt;
187 if (zap->zap_normflags) {
188 if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
192 zn->zn_key_norm = zn->zn_normbuf;
193 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
195 if (mt != MT_EXACT) {
199 zn->zn_key_norm = zn->zn_key_orig;
200 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
203 zn->zn_hash = zap_hash(zn);
208 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
210 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
212 ASSERT(zap->zap_normflags == 0);
214 zn->zn_key_intlen = sizeof (*key);
215 zn->zn_key_orig = zn->zn_key_norm = key;
216 zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
217 zn->zn_matchtype = MT_EXACT;
219 zn->zn_hash = zap_hash(zn);
224 mzap_byteswap(mzap_phys_t *buf, size_t size)
227 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
228 buf->mz_salt = BSWAP_64(buf->mz_salt);
229 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
230 max = (size / MZAP_ENT_LEN) - 1;
231 for (i = 0; i < max; i++) {
232 buf->mz_chunk[i].mze_value =
233 BSWAP_64(buf->mz_chunk[i].mze_value);
234 buf->mz_chunk[i].mze_cd =
235 BSWAP_32(buf->mz_chunk[i].mze_cd);
240 zap_byteswap(void *buf, size_t size)
244 block_type = *(uint64_t *)buf;
246 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
247 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
248 mzap_byteswap(buf, size);
250 fzap_byteswap(buf, size);
255 mze_compare(const void *arg1, const void *arg2)
257 const mzap_ent_t *mze1 = arg1;
258 const mzap_ent_t *mze2 = arg2;
260 int cmp = AVL_CMP(mze1->mze_hash, mze2->mze_hash);
264 return (AVL_CMP(mze1->mze_cd, mze2->mze_cd));
268 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
272 ASSERT(zap->zap_ismicro);
273 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
275 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
276 mze->mze_chunkid = chunkid;
277 mze->mze_hash = hash;
278 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
279 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
280 avl_add(&zap->zap_m.zap_avl, mze);
284 mze_find(zap_name_t *zn)
286 mzap_ent_t mze_tofind;
289 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
291 ASSERT(zn->zn_zap->zap_ismicro);
292 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
294 mze_tofind.mze_hash = zn->zn_hash;
295 mze_tofind.mze_cd = 0;
298 mze = avl_find(avl, &mze_tofind, &idx);
300 mze = avl_nearest(avl, idx, AVL_AFTER);
301 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
302 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
303 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
306 if (zn->zn_matchtype == MT_BEST) {
307 zn->zn_matchtype = MT_FIRST;
314 mze_find_unused_cd(zap_t *zap, uint64_t hash)
316 mzap_ent_t mze_tofind;
319 avl_tree_t *avl = &zap->zap_m.zap_avl;
322 ASSERT(zap->zap_ismicro);
323 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
325 mze_tofind.mze_hash = hash;
326 mze_tofind.mze_cd = 0;
329 for (mze = avl_find(avl, &mze_tofind, &idx);
330 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
331 if (mze->mze_cd != cd)
340 mze_remove(zap_t *zap, mzap_ent_t *mze)
342 ASSERT(zap->zap_ismicro);
343 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
345 avl_remove(&zap->zap_m.zap_avl, mze);
346 kmem_free(mze, sizeof (mzap_ent_t));
350 mze_destroy(zap_t *zap)
353 void *avlcookie = NULL;
355 while ((mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie)))
356 kmem_free(mze, sizeof (mzap_ent_t));
357 avl_destroy(&zap->zap_m.zap_avl);
361 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
366 uint64_t *zap_hdr = (uint64_t *)db->db_data;
367 uint64_t zap_block_type = zap_hdr[0];
368 uint64_t zap_magic = zap_hdr[1];
370 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
372 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
373 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
374 rw_enter(&zap->zap_rwlock, RW_WRITER);
375 zap->zap_objset = os;
376 zap->zap_object = obj;
379 if (zap_block_type != ZBT_MICRO) {
380 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
381 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
382 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
383 winner = NULL; /* No actual winner here... */
387 zap->zap_ismicro = TRUE;
391 * Make sure that zap_ismicro is set before we let others see
392 * it, because zap_lockdir() checks zap_ismicro without the lock
395 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
396 winner = dmu_buf_set_user(db, &zap->zap_dbu);
401 if (zap->zap_ismicro) {
402 zap->zap_salt = zap_m_phys(zap)->mz_salt;
403 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
404 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
405 avl_create(&zap->zap_m.zap_avl, mze_compare,
406 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
408 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
409 mzap_ent_phys_t *mze =
410 &zap_m_phys(zap)->mz_chunk[i];
411 if (mze->mze_name[0]) {
414 zap->zap_m.zap_num_entries++;
415 zn = zap_name_alloc(zap, mze->mze_name,
417 mze_insert(zap, i, zn->zn_hash);
422 zap->zap_salt = zap_f_phys(zap)->zap_salt;
423 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
425 ASSERT3U(sizeof (struct zap_leaf_header), ==,
426 2*ZAP_LEAF_CHUNKSIZE);
429 * The embedded pointer table should not overlap the
432 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
433 &zap_f_phys(zap)->zap_salt);
436 * The embedded pointer table should end at the end of
439 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
440 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
441 (uintptr_t)zap_f_phys(zap), ==,
442 zap->zap_dbuf->db_size);
444 rw_exit(&zap->zap_rwlock);
448 rw_exit(&zap->zap_rwlock);
449 rw_destroy(&zap->zap_rwlock);
450 if (!zap->zap_ismicro)
451 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
452 kmem_free(zap, sizeof (zap_t));
457 zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
458 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
460 dmu_object_info_t doi;
464 objset_t *os = dmu_buf_get_objset(db);
465 uint64_t obj = db->db_object;
467 ASSERT0(db->db_offset);
470 dmu_object_info_from_db(db, &doi);
471 if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
472 return (SET_ERROR(EINVAL));
474 zap = dmu_buf_get_user(db);
476 zap = mzap_open(os, obj, db);
479 * mzap_open() didn't like what it saw on-disk.
480 * Check for corruption!
482 return (SET_ERROR(EIO));
487 * We're checking zap_ismicro without the lock held, in order to
488 * tell what type of lock we want. Once we have some sort of
489 * lock, see if it really is the right type. In practice this
490 * can only be different if it was upgraded from micro to fat,
491 * and micro wanted WRITER but fat only needs READER.
493 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
494 rw_enter(&zap->zap_rwlock, lt);
495 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
496 /* it was upgraded, now we only need reader */
497 ASSERT(lt == RW_WRITER);
499 ((!zap->zap_ismicro && fatreader) ? RW_READER : lti));
500 rw_downgrade(&zap->zap_rwlock);
504 zap->zap_objset = os;
507 dmu_buf_will_dirty(db, tx);
509 ASSERT3P(zap->zap_dbuf, ==, db);
511 ASSERT(!zap->zap_ismicro ||
512 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
513 if (zap->zap_ismicro && tx && adding &&
514 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
515 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
516 if (newsz > MZAP_MAX_BLKSZ) {
518 dprintf("upgrading obj %llu: num_entries=%u\n",
519 obj, zap->zap_m.zap_num_entries);
521 err = mzap_upgrade(zapp, tag, tx, 0);
523 rw_exit(&zap->zap_rwlock);
526 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
527 zap->zap_m.zap_num_chunks =
528 db->db_size / MZAP_ENT_LEN - 1;
536 zap_lockdir_by_dnode(dnode_t *dn, dmu_tx_t *tx,
537 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
542 err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH);
546 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
548 dmu_buf_rele(db, tag);
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_dnsize(os, obj, ot, bonustype, bonuslen,
670 zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot,
671 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
673 return (zap_create_claim_norm_dnsize(os, obj,
674 0, ot, bonustype, bonuslen, dnodesize, tx));
678 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
679 dmu_object_type_t ot,
680 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
682 return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype,
687 zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
688 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
689 int dnodesize, dmu_tx_t *tx)
693 err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
697 mzap_create_impl(os, obj, normflags, 0, tx);
702 zap_create(objset_t *os, dmu_object_type_t ot,
703 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
705 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
709 zap_create_dnsize(objset_t *os, dmu_object_type_t ot,
710 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
712 return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen,
717 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
718 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
720 return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen,
725 zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
726 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
728 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
731 mzap_create_impl(os, obj, normflags, 0, tx);
736 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
737 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
738 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
740 return (zap_create_flags_dnsize(os, normflags, flags, ot,
741 leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx));
745 zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
746 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
747 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
749 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
752 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
753 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
754 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
755 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
757 VERIFY(dmu_object_set_blocksize(os, obj,
758 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
760 mzap_create_impl(os, obj, normflags, flags, tx);
765 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
768 * dmu_object_free will free the object number and free the
769 * data. Freeing the data will cause our pageout function to be
770 * called, which will destroy our data (zap_leaf_t's and zap_t).
773 return (dmu_object_free(os, zapobj, tx));
781 rw_destroy(&zap->zap_rwlock);
783 if (zap->zap_ismicro)
786 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
788 kmem_free(zap, sizeof (zap_t));
792 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
797 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
800 if (!zap->zap_ismicro) {
801 err = fzap_count(zap, count);
803 *count = zap->zap_m.zap_num_entries;
805 zap_unlockdir(zap, FTAG);
810 * zn may be NULL; if not specified, it will be computed if needed.
811 * See also the comment above zap_entry_normalization_conflict().
814 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
817 int direction = AVL_BEFORE;
818 boolean_t allocdzn = B_FALSE;
820 if (zap->zap_normflags == 0)
824 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
825 other && other->mze_hash == mze->mze_hash;
826 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
829 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
833 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
840 if (direction == AVL_BEFORE) {
841 direction = AVL_AFTER;
851 * Routines for manipulating attributes.
855 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
856 uint64_t integer_size, uint64_t num_integers, void *buf)
858 return (zap_lookup_norm(os, zapobj, name, integer_size,
859 num_integers, buf, MT_EXACT, NULL, 0, NULL));
863 zap_lookup_impl(zap_t *zap, const char *name,
864 uint64_t integer_size, uint64_t num_integers, void *buf,
865 matchtype_t mt, char *realname, int rn_len,
872 zn = zap_name_alloc(zap, name, mt);
874 return (SET_ERROR(ENOTSUP));
876 if (!zap->zap_ismicro) {
877 err = fzap_lookup(zn, integer_size, num_integers, buf,
878 realname, rn_len, ncp);
882 err = SET_ERROR(ENOENT);
884 if (num_integers < 1) {
885 err = SET_ERROR(EOVERFLOW);
886 } else if (integer_size != 8) {
887 err = SET_ERROR(EINVAL);
890 MZE_PHYS(zap, mze)->mze_value;
891 (void) strlcpy(realname,
892 MZE_PHYS(zap, mze)->mze_name, rn_len);
894 *ncp = mzap_normalization_conflict(zap,
905 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
906 uint64_t integer_size, uint64_t num_integers, void *buf,
907 matchtype_t mt, char *realname, int rn_len,
913 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
916 err = zap_lookup_impl(zap, name, integer_size,
917 num_integers, buf, mt, realname, rn_len, ncp);
918 zap_unlockdir(zap, FTAG);
923 zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
929 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
932 zn = zap_name_alloc(zap, name, MT_EXACT);
934 zap_unlockdir(zap, FTAG);
935 return (SET_ERROR(ENOTSUP));
940 zap_unlockdir(zap, FTAG);
945 zap_lookup_by_dnode(dnode_t *dn, const char *name,
946 uint64_t integer_size, uint64_t num_integers, void *buf)
948 return (zap_lookup_norm_by_dnode(dn, name, integer_size,
949 num_integers, buf, MT_EXACT, NULL, 0, NULL));
953 zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
954 uint64_t integer_size, uint64_t num_integers, void *buf,
955 matchtype_t mt, char *realname, int rn_len,
961 err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
965 err = zap_lookup_impl(zap, name, integer_size,
966 num_integers, buf, mt, realname, rn_len, ncp);
967 zap_unlockdir(zap, FTAG);
972 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
979 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
982 zn = zap_name_alloc_uint64(zap, key, key_numints);
984 zap_unlockdir(zap, FTAG);
985 return (SET_ERROR(ENOTSUP));
990 zap_unlockdir(zap, FTAG);
995 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
996 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
1002 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1005 zn = zap_name_alloc_uint64(zap, key, key_numints);
1007 zap_unlockdir(zap, FTAG);
1008 return (SET_ERROR(ENOTSUP));
1011 err = fzap_lookup(zn, integer_size, num_integers, buf,
1014 zap_unlockdir(zap, FTAG);
1019 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
1021 int err = zap_lookup_norm(os, zapobj, name, 0,
1022 0, NULL, MT_EXACT, NULL, 0, NULL);
1023 if (err == EOVERFLOW || err == EINVAL)
1024 err = 0; /* found, but skipped reading the value */
1029 zap_length(objset_t *os, uint64_t zapobj, const char *name,
1030 uint64_t *integer_size, uint64_t *num_integers)
1037 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1040 zn = zap_name_alloc(zap, name, MT_EXACT);
1042 zap_unlockdir(zap, FTAG);
1043 return (SET_ERROR(ENOTSUP));
1045 if (!zap->zap_ismicro) {
1046 err = fzap_length(zn, integer_size, num_integers);
1050 err = SET_ERROR(ENOENT);
1059 zap_unlockdir(zap, FTAG);
1064 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1065 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
1071 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1074 zn = zap_name_alloc_uint64(zap, key, key_numints);
1076 zap_unlockdir(zap, FTAG);
1077 return (SET_ERROR(ENOTSUP));
1079 err = fzap_length(zn, integer_size, num_integers);
1081 zap_unlockdir(zap, FTAG);
1086 mzap_addent(zap_name_t *zn, uint64_t value)
1089 zap_t *zap = zn->zn_zap;
1090 int start = zap->zap_m.zap_alloc_next;
1093 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1096 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
1097 ASSERTV(mzap_ent_phys_t *mze);
1098 ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
1099 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
1103 cd = mze_find_unused_cd(zap, zn->zn_hash);
1104 /* given the limited size of the microzap, this can't happen */
1105 ASSERT(cd < zap_maxcd(zap));
1108 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
1109 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1110 if (mze->mze_name[0] == 0) {
1111 mze->mze_value = value;
1113 (void) strcpy(mze->mze_name, zn->zn_key_orig);
1114 zap->zap_m.zap_num_entries++;
1115 zap->zap_m.zap_alloc_next = i+1;
1116 if (zap->zap_m.zap_alloc_next ==
1117 zap->zap_m.zap_num_chunks)
1118 zap->zap_m.zap_alloc_next = 0;
1119 mze_insert(zap, i, zn->zn_hash);
1127 cmn_err(CE_PANIC, "out of entries!");
1131 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1132 int integer_size, uint64_t num_integers,
1133 const void *val, dmu_tx_t *tx)
1138 const uint64_t *intval = val;
1141 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1144 zn = zap_name_alloc(zap, key, MT_EXACT);
1146 zap_unlockdir(zap, FTAG);
1147 return (SET_ERROR(ENOTSUP));
1149 if (!zap->zap_ismicro) {
1150 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1151 zap = zn->zn_zap; /* fzap_add() may change zap */
1152 } else if (integer_size != 8 || num_integers != 1 ||
1153 strlen(key) >= MZAP_NAME_LEN) {
1154 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1156 err = fzap_add(zn, integer_size, num_integers, val,
1159 zap = zn->zn_zap; /* fzap_add() may change zap */
1163 err = SET_ERROR(EEXIST);
1165 mzap_addent(zn, *intval);
1168 ASSERT(zap == zn->zn_zap);
1170 if (zap != NULL) /* may be NULL if fzap_add() failed */
1171 zap_unlockdir(zap, FTAG);
1176 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1177 int key_numints, int integer_size, uint64_t num_integers,
1178 const void *val, dmu_tx_t *tx)
1184 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1187 zn = zap_name_alloc_uint64(zap, key, key_numints);
1189 zap_unlockdir(zap, FTAG);
1190 return (SET_ERROR(ENOTSUP));
1192 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1193 zap = zn->zn_zap; /* fzap_add() may change zap */
1195 if (zap != NULL) /* may be NULL if fzap_add() failed */
1196 zap_unlockdir(zap, FTAG);
1201 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1202 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1206 const uint64_t *intval = val;
1214 * If there is an old value, it shouldn't change across the
1215 * lockdir (eg, due to bprewrite's xlation).
1217 if (integer_size == 8 && num_integers == 1)
1218 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1221 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1224 zn = zap_name_alloc(zap, name, MT_EXACT);
1226 zap_unlockdir(zap, FTAG);
1227 return (SET_ERROR(ENOTSUP));
1229 if (!zap->zap_ismicro) {
1230 err = fzap_update(zn, integer_size, num_integers, val,
1232 zap = zn->zn_zap; /* fzap_update() may change zap */
1233 } else if (integer_size != 8 || num_integers != 1 ||
1234 strlen(name) >= MZAP_NAME_LEN) {
1235 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1236 zapobj, integer_size, num_integers, name);
1237 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1239 err = fzap_update(zn, integer_size, num_integers,
1242 zap = zn->zn_zap; /* fzap_update() may change zap */
1246 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1247 MZE_PHYS(zap, mze)->mze_value = *intval;
1249 mzap_addent(zn, *intval);
1252 ASSERT(zap == zn->zn_zap);
1254 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1255 zap_unlockdir(zap, FTAG);
1260 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1262 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1268 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1271 zn = zap_name_alloc_uint64(zap, key, key_numints);
1273 zap_unlockdir(zap, FTAG);
1274 return (SET_ERROR(ENOTSUP));
1276 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
1277 zap = zn->zn_zap; /* fzap_update() may change zap */
1279 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1280 zap_unlockdir(zap, FTAG);
1285 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1287 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1291 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1292 matchtype_t mt, dmu_tx_t *tx)
1299 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1302 zn = zap_name_alloc(zap, name, mt);
1304 zap_unlockdir(zap, FTAG);
1305 return (SET_ERROR(ENOTSUP));
1307 if (!zap->zap_ismicro) {
1308 err = fzap_remove(zn, tx);
1312 err = SET_ERROR(ENOENT);
1314 zap->zap_m.zap_num_entries--;
1315 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1316 sizeof (mzap_ent_phys_t));
1317 mze_remove(zap, mze);
1321 zap_unlockdir(zap, FTAG);
1326 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1327 int key_numints, dmu_tx_t *tx)
1333 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1336 zn = zap_name_alloc_uint64(zap, key, key_numints);
1338 zap_unlockdir(zap, FTAG);
1339 return (SET_ERROR(ENOTSUP));
1341 err = fzap_remove(zn, tx);
1343 zap_unlockdir(zap, FTAG);
1348 * Routines for iterating over the attributes.
1352 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1353 uint64_t serialized)
1358 zc->zc_zapobj = zapobj;
1359 zc->zc_serialized = serialized;
1365 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1367 zap_cursor_init_serialized(zc, os, zapobj, 0);
1371 zap_cursor_fini(zap_cursor_t *zc)
1374 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1375 zap_unlockdir(zc->zc_zap, NULL);
1379 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1380 zap_put_leaf(zc->zc_leaf);
1383 zc->zc_objset = NULL;
1387 zap_cursor_serialize(zap_cursor_t *zc)
1389 if (zc->zc_hash == -1ULL)
1391 if (zc->zc_zap == NULL)
1392 return (zc->zc_serialized);
1393 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1394 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1397 * We want to keep the high 32 bits of the cursor zero if we can, so
1398 * that 32-bit programs can access this. So usually use a small
1399 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1402 * [ collision differentiator | zap_hashbits()-bit hash value ]
1404 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1405 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1409 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1413 mzap_ent_t mze_tofind;
1416 if (zc->zc_hash == -1ULL)
1417 return (SET_ERROR(ENOENT));
1419 if (zc->zc_zap == NULL) {
1421 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1422 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
1427 * To support zap_cursor_init_serialized, advance, retrieve,
1428 * we must add to the existing zc_cd, which may already
1429 * be 1 due to the zap_cursor_advance.
1431 ASSERT(zc->zc_hash == 0);
1432 hb = zap_hashbits(zc->zc_zap);
1433 zc->zc_hash = zc->zc_serialized << (64 - hb);
1434 zc->zc_cd += zc->zc_serialized >> hb;
1435 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1438 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1440 if (!zc->zc_zap->zap_ismicro) {
1441 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1443 mze_tofind.mze_hash = zc->zc_hash;
1444 mze_tofind.mze_cd = zc->zc_cd;
1446 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1448 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1452 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1453 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1454 za->za_normalization_conflict =
1455 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1456 za->za_integer_length = 8;
1457 za->za_num_integers = 1;
1458 za->za_first_integer = mzep->mze_value;
1459 (void) strcpy(za->za_name, mzep->mze_name);
1460 zc->zc_hash = mze->mze_hash;
1461 zc->zc_cd = mze->mze_cd;
1464 zc->zc_hash = -1ULL;
1465 err = SET_ERROR(ENOENT);
1468 rw_exit(&zc->zc_zap->zap_rwlock);
1473 zap_cursor_advance(zap_cursor_t *zc)
1475 if (zc->zc_hash == -1ULL)
1481 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1486 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1490 bzero(zs, sizeof (zap_stats_t));
1492 if (zap->zap_ismicro) {
1493 zs->zs_blocksize = zap->zap_dbuf->db_size;
1494 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1495 zs->zs_num_blocks = 1;
1497 fzap_get_stats(zap, zs);
1499 zap_unlockdir(zap, FTAG);
1504 zap_count_write_by_dnode(dnode_t *dn, const char *name, int add,
1505 uint64_t *towrite, uint64_t *tooverwrite)
1511 * Since, we don't have a name, we cannot figure out which blocks will
1512 * be affected in this operation. So, account for the worst case :
1513 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1514 * - 4 new blocks written if adding:
1515 * - 2 blocks for possibly split leaves,
1516 * - 2 grown ptrtbl blocks
1518 * This also accommodates the case where an add operation to a fairly
1519 * large microzap results in a promotion to fatzap.
1522 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1527 * We lock the zap with adding == FALSE. Because, if we pass
1528 * the actual value of add, it could trigger a mzap_upgrade().
1529 * At present we are just evaluating the possibility of this operation
1530 * and hence we do not want to trigger an upgrade.
1532 err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
1537 if (!zap->zap_ismicro) {
1538 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1540 err = fzap_count_write(zn, add, towrite,
1545 * We treat this case as similar to (name == NULL)
1547 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1551 * We are here if (name != NULL) and this is a micro-zap.
1552 * We account for the header block depending on whether it
1555 * Incase of an add-operation it is hard to find out
1556 * if this add will promote this microzap to fatzap.
1557 * Hence, we consider the worst case and account for the
1558 * blocks assuming this microzap would be promoted to a
1561 * 1 block overwritten : header block
1562 * 4 new blocks written : 2 new split leaf, 2 grown
1565 if (dmu_buf_freeable(zap->zap_dbuf))
1566 *tooverwrite += MZAP_MAX_BLKSZ;
1568 *towrite += MZAP_MAX_BLKSZ;
1571 *towrite += 4 * MZAP_MAX_BLKSZ;
1575 zap_unlockdir(zap, FTAG);
1579 #if defined(_KERNEL) && defined(HAVE_SPL)
1580 EXPORT_SYMBOL(zap_create);
1581 EXPORT_SYMBOL(zap_create_dnsize);
1582 EXPORT_SYMBOL(zap_create_norm);
1583 EXPORT_SYMBOL(zap_create_norm_dnsize);
1584 EXPORT_SYMBOL(zap_create_flags);
1585 EXPORT_SYMBOL(zap_create_flags_dnsize);
1586 EXPORT_SYMBOL(zap_create_claim);
1587 EXPORT_SYMBOL(zap_create_claim_norm);
1588 EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
1589 EXPORT_SYMBOL(zap_destroy);
1590 EXPORT_SYMBOL(zap_lookup);
1591 EXPORT_SYMBOL(zap_lookup_norm);
1592 EXPORT_SYMBOL(zap_lookup_uint64);
1593 EXPORT_SYMBOL(zap_contains);
1594 EXPORT_SYMBOL(zap_prefetch);
1595 EXPORT_SYMBOL(zap_prefetch_uint64);
1596 EXPORT_SYMBOL(zap_count_write_by_dnode);
1597 EXPORT_SYMBOL(zap_add);
1598 EXPORT_SYMBOL(zap_add_uint64);
1599 EXPORT_SYMBOL(zap_update);
1600 EXPORT_SYMBOL(zap_update_uint64);
1601 EXPORT_SYMBOL(zap_length);
1602 EXPORT_SYMBOL(zap_length_uint64);
1603 EXPORT_SYMBOL(zap_remove);
1604 EXPORT_SYMBOL(zap_remove_norm);
1605 EXPORT_SYMBOL(zap_remove_uint64);
1606 EXPORT_SYMBOL(zap_count);
1607 EXPORT_SYMBOL(zap_value_search);
1608 EXPORT_SYMBOL(zap_join);
1609 EXPORT_SYMBOL(zap_join_increment);
1610 EXPORT_SYMBOL(zap_add_int);
1611 EXPORT_SYMBOL(zap_remove_int);
1612 EXPORT_SYMBOL(zap_lookup_int);
1613 EXPORT_SYMBOL(zap_increment_int);
1614 EXPORT_SYMBOL(zap_add_int_key);
1615 EXPORT_SYMBOL(zap_lookup_int_key);
1616 EXPORT_SYMBOL(zap_increment);
1617 EXPORT_SYMBOL(zap_cursor_init);
1618 EXPORT_SYMBOL(zap_cursor_fini);
1619 EXPORT_SYMBOL(zap_cursor_retrieve);
1620 EXPORT_SYMBOL(zap_cursor_advance);
1621 EXPORT_SYMBOL(zap_cursor_serialize);
1622 EXPORT_SYMBOL(zap_cursor_init_serialized);
1623 EXPORT_SYMBOL(zap_get_stats);