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)
378 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
380 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
381 rw_init(&zap->zap_rwlock, 0, 0, 0);
382 rw_enter(&zap->zap_rwlock, RW_WRITER);
383 zap->zap_objset = os;
384 zap->zap_object = obj;
387 if (*(uint64_t *)db->db_data != ZBT_MICRO) {
388 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
389 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
391 zap->zap_ismicro = TRUE;
395 * Make sure that zap_ismicro is set before we let others see
396 * it, because zap_lockdir() checks zap_ismicro without the lock
399 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
400 winner = dmu_buf_set_user(db, &zap->zap_dbu);
402 if (winner != NULL) {
403 rw_exit(&zap->zap_rwlock);
404 rw_destroy(&zap->zap_rwlock);
405 if (!zap->zap_ismicro)
406 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
407 kmem_free(zap, sizeof (zap_t));
411 if (zap->zap_ismicro) {
412 zap->zap_salt = zap_m_phys(zap)->mz_salt;
413 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
414 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
415 avl_create(&zap->zap_m.zap_avl, mze_compare,
416 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
418 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
419 mzap_ent_phys_t *mze =
420 &zap_m_phys(zap)->mz_chunk[i];
421 if (mze->mze_name[0]) {
424 zn = zap_name_alloc(zap, mze->mze_name,
426 if (mze_insert(zap, i, zn->zn_hash) == 0)
427 zap->zap_m.zap_num_entries++;
429 printf("ZFS WARNING: Duplicated ZAP "
430 "entry detected (%s).\n",
437 zap->zap_salt = zap_f_phys(zap)->zap_salt;
438 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
440 ASSERT3U(sizeof (struct zap_leaf_header), ==,
441 2*ZAP_LEAF_CHUNKSIZE);
444 * The embedded pointer table should not overlap the
447 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
448 &zap_f_phys(zap)->zap_salt);
451 * The embedded pointer table should end at the end of
454 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
455 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
456 (uintptr_t)zap_f_phys(zap), ==,
457 zap->zap_dbuf->db_size);
459 rw_exit(&zap->zap_rwlock);
464 zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
465 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
470 ASSERT0(db->db_offset);
471 objset_t *os = dmu_buf_get_objset(db);
472 uint64_t obj = db->db_object;
478 dmu_object_info_t doi;
479 dmu_object_info_from_db(db, &doi);
480 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
484 zap = dmu_buf_get_user(db);
486 zap = mzap_open(os, obj, db);
489 * We're checking zap_ismicro without the lock held, in order to
490 * tell what type of lock we want. Once we have some sort of
491 * lock, see if it really is the right type. In practice this
492 * can only be different if it was upgraded from micro to fat,
493 * and micro wanted WRITER but fat only needs READER.
495 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
496 rw_enter(&zap->zap_rwlock, lt);
497 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
498 /* it was upgraded, now we only need reader */
499 ASSERT(lt == RW_WRITER);
501 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
502 rw_downgrade(&zap->zap_rwlock);
506 zap->zap_objset = os;
509 dmu_buf_will_dirty(db, tx);
511 ASSERT3P(zap->zap_dbuf, ==, db);
513 ASSERT(!zap->zap_ismicro ||
514 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
515 if (zap->zap_ismicro && tx && adding &&
516 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
517 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
518 if (newsz > MZAP_MAX_BLKSZ) {
519 dprintf("upgrading obj %llu: num_entries=%u\n",
520 obj, zap->zap_m.zap_num_entries);
522 int err = mzap_upgrade(zapp, tag, tx, 0);
524 rw_exit(&zap->zap_rwlock);
527 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
528 zap->zap_m.zap_num_chunks =
529 db->db_size / MZAP_ENT_LEN - 1;
537 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
538 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
543 err = dmu_buf_hold(os, obj, 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);
553 zap_unlockdir(zap_t *zap, void *tag)
555 rw_exit(&zap->zap_rwlock);
556 dmu_buf_rele(zap->zap_dbuf, tag);
560 mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
567 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
569 sz = zap->zap_dbuf->db_size;
570 mzp = zio_buf_alloc(sz);
571 bcopy(zap->zap_dbuf->db_data, mzp, sz);
572 nchunks = zap->zap_m.zap_num_chunks;
575 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
576 1ULL << fzap_default_block_shift, 0, tx);
578 zio_buf_free(mzp, sz);
583 dprintf("upgrading obj=%llu with %u chunks\n",
584 zap->zap_object, nchunks);
585 /* XXX destroy the avl later, so we can use the stored hash value */
588 fzap_upgrade(zap, tx, flags);
590 for (i = 0; i < nchunks; i++) {
591 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
593 if (mze->mze_name[0] == 0)
595 dprintf("adding %s=%llu\n",
596 mze->mze_name, mze->mze_value);
597 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
598 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
600 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
605 zio_buf_free(mzp, sz);
611 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
617 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
621 dmu_object_info_t doi;
622 dmu_object_info_from_db(db, &doi);
623 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
627 dmu_buf_will_dirty(db, tx);
629 zp->mz_block_type = ZBT_MICRO;
630 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
631 zp->mz_normflags = normflags;
632 dmu_buf_rele(db, FTAG);
636 /* Only fat zap supports flags; upgrade immediately. */
637 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
638 B_FALSE, B_FALSE, FTAG, &zap));
639 VERIFY3U(0, ==, mzap_upgrade(&zap, FTAG, tx, flags));
640 zap_unlockdir(zap, FTAG);
645 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
646 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
648 return (zap_create_claim_norm(os, obj,
649 0, ot, bonustype, bonuslen, tx));
653 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
654 dmu_object_type_t ot,
655 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
659 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
662 mzap_create_impl(os, obj, normflags, 0, tx);
667 zap_create(objset_t *os, dmu_object_type_t ot,
668 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
670 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
674 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
675 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
677 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
679 mzap_create_impl(os, obj, normflags, 0, tx);
684 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
685 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
686 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
688 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
690 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
691 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
692 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
693 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
695 VERIFY(dmu_object_set_blocksize(os, obj,
696 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
698 mzap_create_impl(os, obj, normflags, flags, tx);
703 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
706 * dmu_object_free will free the object number and free the
707 * data. Freeing the data will cause our pageout function to be
708 * called, which will destroy our data (zap_leaf_t's and zap_t).
711 return (dmu_object_free(os, zapobj, tx));
719 rw_destroy(&zap->zap_rwlock);
721 if (zap->zap_ismicro)
724 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
726 kmem_free(zap, sizeof (zap_t));
730 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
735 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
738 if (!zap->zap_ismicro) {
739 err = fzap_count(zap, count);
741 *count = zap->zap_m.zap_num_entries;
743 zap_unlockdir(zap, FTAG);
748 * zn may be NULL; if not specified, it will be computed if needed.
749 * See also the comment above zap_entry_normalization_conflict().
752 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
755 int direction = AVL_BEFORE;
756 boolean_t allocdzn = B_FALSE;
758 if (zap->zap_normflags == 0)
762 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
763 other && other->mze_hash == mze->mze_hash;
764 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
767 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
771 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
778 if (direction == AVL_BEFORE) {
779 direction = AVL_AFTER;
789 * Routines for manipulating attributes.
793 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
794 uint64_t integer_size, uint64_t num_integers, void *buf)
796 return (zap_lookup_norm(os, zapobj, name, integer_size,
797 num_integers, buf, MT_EXACT, NULL, 0, NULL));
801 zap_lookup_impl(zap_t *zap, const char *name,
802 uint64_t integer_size, uint64_t num_integers, void *buf,
803 matchtype_t mt, char *realname, int rn_len,
810 zn = zap_name_alloc(zap, name, mt);
812 return (SET_ERROR(ENOTSUP));
814 if (!zap->zap_ismicro) {
815 err = fzap_lookup(zn, integer_size, num_integers, buf,
816 realname, rn_len, ncp);
820 err = SET_ERROR(ENOENT);
822 if (num_integers < 1) {
823 err = SET_ERROR(EOVERFLOW);
824 } else if (integer_size != 8) {
825 err = SET_ERROR(EINVAL);
828 MZE_PHYS(zap, mze)->mze_value;
829 (void) strlcpy(realname,
830 MZE_PHYS(zap, mze)->mze_name, rn_len);
832 *ncp = mzap_normalization_conflict(zap,
843 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
844 uint64_t integer_size, uint64_t num_integers, void *buf,
845 matchtype_t mt, char *realname, int rn_len,
851 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
854 err = zap_lookup_impl(zap, name, integer_size,
855 num_integers, buf, mt, realname, rn_len, ncp);
856 zap_unlockdir(zap, FTAG);
861 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
868 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
871 zn = zap_name_alloc_uint64(zap, key, key_numints);
873 zap_unlockdir(zap, FTAG);
874 return (SET_ERROR(ENOTSUP));
879 zap_unlockdir(zap, FTAG);
884 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
885 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
891 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
894 zn = zap_name_alloc_uint64(zap, key, key_numints);
896 zap_unlockdir(zap, FTAG);
897 return (SET_ERROR(ENOTSUP));
900 err = fzap_lookup(zn, integer_size, num_integers, buf,
903 zap_unlockdir(zap, FTAG);
908 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
910 int err = zap_lookup_norm(os, zapobj, name, 0,
911 0, NULL, MT_EXACT, NULL, 0, NULL);
912 if (err == EOVERFLOW || err == EINVAL)
913 err = 0; /* found, but skipped reading the value */
918 zap_length(objset_t *os, uint64_t zapobj, const char *name,
919 uint64_t *integer_size, uint64_t *num_integers)
926 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
929 zn = zap_name_alloc(zap, name, MT_EXACT);
931 zap_unlockdir(zap, FTAG);
932 return (SET_ERROR(ENOTSUP));
934 if (!zap->zap_ismicro) {
935 err = fzap_length(zn, integer_size, num_integers);
939 err = SET_ERROR(ENOENT);
948 zap_unlockdir(zap, FTAG);
953 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
954 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
960 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
963 zn = zap_name_alloc_uint64(zap, key, key_numints);
965 zap_unlockdir(zap, FTAG);
966 return (SET_ERROR(ENOTSUP));
968 err = fzap_length(zn, integer_size, num_integers);
970 zap_unlockdir(zap, FTAG);
975 mzap_addent(zap_name_t *zn, uint64_t value)
978 zap_t *zap = zn->zn_zap;
979 int start = zap->zap_m.zap_alloc_next;
982 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
985 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
986 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
987 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
991 cd = mze_find_unused_cd(zap, zn->zn_hash);
992 /* given the limited size of the microzap, this can't happen */
993 ASSERT(cd < zap_maxcd(zap));
996 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
997 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
998 if (mze->mze_name[0] == 0) {
999 mze->mze_value = value;
1001 (void) strcpy(mze->mze_name, zn->zn_key_orig);
1002 zap->zap_m.zap_num_entries++;
1003 zap->zap_m.zap_alloc_next = i+1;
1004 if (zap->zap_m.zap_alloc_next ==
1005 zap->zap_m.zap_num_chunks)
1006 zap->zap_m.zap_alloc_next = 0;
1007 VERIFY(0 == mze_insert(zap, i, zn->zn_hash));
1015 ASSERT(!"out of entries!");
1019 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1020 int integer_size, uint64_t num_integers,
1021 const void *val, dmu_tx_t *tx)
1026 const uint64_t *intval = val;
1029 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1032 zn = zap_name_alloc(zap, key, MT_EXACT);
1034 zap_unlockdir(zap, FTAG);
1035 return (SET_ERROR(ENOTSUP));
1037 if (!zap->zap_ismicro) {
1038 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1039 zap = zn->zn_zap; /* fzap_add() may change zap */
1040 } else if (integer_size != 8 || num_integers != 1 ||
1041 strlen(key) >= MZAP_NAME_LEN) {
1042 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1044 err = fzap_add(zn, integer_size, num_integers, val,
1047 zap = zn->zn_zap; /* fzap_add() may change zap */
1051 err = SET_ERROR(EEXIST);
1053 mzap_addent(zn, *intval);
1056 ASSERT(zap == zn->zn_zap);
1058 if (zap != NULL) /* may be NULL if fzap_add() failed */
1059 zap_unlockdir(zap, FTAG);
1064 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1065 int key_numints, int integer_size, uint64_t num_integers,
1066 const void *val, dmu_tx_t *tx)
1072 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1075 zn = zap_name_alloc_uint64(zap, key, key_numints);
1077 zap_unlockdir(zap, FTAG);
1078 return (SET_ERROR(ENOTSUP));
1080 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1081 zap = zn->zn_zap; /* fzap_add() may change zap */
1083 if (zap != NULL) /* may be NULL if fzap_add() failed */
1084 zap_unlockdir(zap, FTAG);
1089 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1090 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1095 const uint64_t *intval = val;
1101 * If there is an old value, it shouldn't change across the
1102 * lockdir (eg, due to bprewrite's xlation).
1104 if (integer_size == 8 && num_integers == 1)
1105 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1108 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1111 zn = zap_name_alloc(zap, name, MT_EXACT);
1113 zap_unlockdir(zap, FTAG);
1114 return (SET_ERROR(ENOTSUP));
1116 if (!zap->zap_ismicro) {
1117 err = fzap_update(zn, integer_size, num_integers, val,
1119 zap = zn->zn_zap; /* fzap_update() may change zap */
1120 } else if (integer_size != 8 || num_integers != 1 ||
1121 strlen(name) >= MZAP_NAME_LEN) {
1122 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1123 zapobj, integer_size, num_integers, name);
1124 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1126 err = fzap_update(zn, integer_size, num_integers,
1129 zap = zn->zn_zap; /* fzap_update() may change zap */
1133 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1134 MZE_PHYS(zap, mze)->mze_value = *intval;
1136 mzap_addent(zn, *intval);
1139 ASSERT(zap == zn->zn_zap);
1141 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1142 zap_unlockdir(zap, FTAG);
1147 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1149 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1155 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1158 zn = zap_name_alloc_uint64(zap, key, key_numints);
1160 zap_unlockdir(zap, FTAG);
1161 return (SET_ERROR(ENOTSUP));
1163 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
1164 zap = zn->zn_zap; /* fzap_update() may change zap */
1166 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1167 zap_unlockdir(zap, FTAG);
1172 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1174 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1178 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1179 matchtype_t mt, dmu_tx_t *tx)
1186 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1189 zn = zap_name_alloc(zap, name, mt);
1191 zap_unlockdir(zap, FTAG);
1192 return (SET_ERROR(ENOTSUP));
1194 if (!zap->zap_ismicro) {
1195 err = fzap_remove(zn, tx);
1199 err = SET_ERROR(ENOENT);
1201 zap->zap_m.zap_num_entries--;
1202 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1203 sizeof (mzap_ent_phys_t));
1204 mze_remove(zap, mze);
1208 zap_unlockdir(zap, FTAG);
1213 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1214 int key_numints, dmu_tx_t *tx)
1220 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1223 zn = zap_name_alloc_uint64(zap, key, key_numints);
1225 zap_unlockdir(zap, FTAG);
1226 return (SET_ERROR(ENOTSUP));
1228 err = fzap_remove(zn, tx);
1230 zap_unlockdir(zap, FTAG);
1235 * Routines for iterating over the attributes.
1239 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1240 uint64_t serialized)
1245 zc->zc_zapobj = zapobj;
1246 zc->zc_serialized = serialized;
1252 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1254 zap_cursor_init_serialized(zc, os, zapobj, 0);
1258 zap_cursor_fini(zap_cursor_t *zc)
1261 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1262 zap_unlockdir(zc->zc_zap, NULL);
1266 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1267 zap_put_leaf(zc->zc_leaf);
1270 zc->zc_objset = NULL;
1274 zap_cursor_serialize(zap_cursor_t *zc)
1276 if (zc->zc_hash == -1ULL)
1278 if (zc->zc_zap == NULL)
1279 return (zc->zc_serialized);
1280 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1281 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1284 * We want to keep the high 32 bits of the cursor zero if we can, so
1285 * that 32-bit programs can access this. So usually use a small
1286 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1289 * [ collision differentiator | zap_hashbits()-bit hash value ]
1291 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1292 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1296 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1300 mzap_ent_t mze_tofind;
1303 if (zc->zc_hash == -1ULL)
1304 return (SET_ERROR(ENOENT));
1306 if (zc->zc_zap == NULL) {
1308 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1309 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
1314 * To support zap_cursor_init_serialized, advance, retrieve,
1315 * we must add to the existing zc_cd, which may already
1316 * be 1 due to the zap_cursor_advance.
1318 ASSERT(zc->zc_hash == 0);
1319 hb = zap_hashbits(zc->zc_zap);
1320 zc->zc_hash = zc->zc_serialized << (64 - hb);
1321 zc->zc_cd += zc->zc_serialized >> hb;
1322 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1325 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1327 if (!zc->zc_zap->zap_ismicro) {
1328 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1330 mze_tofind.mze_hash = zc->zc_hash;
1331 mze_tofind.mze_cd = zc->zc_cd;
1333 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1335 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1339 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1340 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1341 za->za_normalization_conflict =
1342 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1343 za->za_integer_length = 8;
1344 za->za_num_integers = 1;
1345 za->za_first_integer = mzep->mze_value;
1346 (void) strcpy(za->za_name, mzep->mze_name);
1347 zc->zc_hash = mze->mze_hash;
1348 zc->zc_cd = mze->mze_cd;
1351 zc->zc_hash = -1ULL;
1352 err = SET_ERROR(ENOENT);
1355 rw_exit(&zc->zc_zap->zap_rwlock);
1360 zap_cursor_advance(zap_cursor_t *zc)
1362 if (zc->zc_hash == -1ULL)
1368 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1374 if (zc->zc_zap == NULL) {
1375 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1376 RW_READER, TRUE, FALSE, &zc->zc_zap);
1380 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1383 zn = zap_name_alloc(zc->zc_zap, name, mt);
1385 rw_exit(&zc->zc_zap->zap_rwlock);
1386 return (SET_ERROR(ENOTSUP));
1389 if (!zc->zc_zap->zap_ismicro) {
1390 err = fzap_cursor_move_to_key(zc, zn);
1394 err = SET_ERROR(ENOENT);
1397 zc->zc_hash = mze->mze_hash;
1398 zc->zc_cd = mze->mze_cd;
1403 rw_exit(&zc->zc_zap->zap_rwlock);
1408 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1413 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1417 bzero(zs, sizeof (zap_stats_t));
1419 if (zap->zap_ismicro) {
1420 zs->zs_blocksize = zap->zap_dbuf->db_size;
1421 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1422 zs->zs_num_blocks = 1;
1424 fzap_get_stats(zap, zs);
1426 zap_unlockdir(zap, FTAG);
1431 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1432 refcount_t *towrite, refcount_t *tooverwrite)
1438 * Since, we don't have a name, we cannot figure out which blocks will
1439 * be affected in this operation. So, account for the worst case :
1440 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1441 * - 4 new blocks written if adding:
1442 * - 2 blocks for possibly split leaves,
1443 * - 2 grown ptrtbl blocks
1445 * This also accommodates the case where an add operation to a fairly
1446 * large microzap results in a promotion to fatzap.
1449 (void) refcount_add_many(towrite,
1450 (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE, FTAG);
1455 * We lock the zap with adding == FALSE. Because, if we pass
1456 * the actual value of add, it could trigger a mzap_upgrade().
1457 * At present we are just evaluating the possibility of this operation
1458 * and hence we do not want to trigger an upgrade.
1460 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE,
1465 if (!zap->zap_ismicro) {
1466 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1468 err = fzap_count_write(zn, add, towrite,
1473 * We treat this case as similar to (name == NULL)
1475 (void) refcount_add_many(towrite,
1476 (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE, FTAG);
1480 * We are here if (name != NULL) and this is a micro-zap.
1481 * We account for the header block depending on whether it
1484 * Incase of an add-operation it is hard to find out
1485 * if this add will promote this microzap to fatzap.
1486 * Hence, we consider the worst case and account for the
1487 * blocks assuming this microzap would be promoted to a
1490 * 1 block overwritten : header block
1491 * 4 new blocks written : 2 new split leaf, 2 grown
1494 if (dmu_buf_freeable(zap->zap_dbuf)) {
1495 (void) refcount_add_many(tooverwrite,
1496 MZAP_MAX_BLKSZ, FTAG);
1498 (void) refcount_add_many(towrite,
1499 MZAP_MAX_BLKSZ, FTAG);
1503 (void) refcount_add_many(towrite,
1504 4 * MZAP_MAX_BLKSZ, FTAG);
1508 zap_unlockdir(zap, FTAG);