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, 2014 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, 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 if (mze1->mze_hash > mze2->mze_hash)
262 if (mze1->mze_hash < mze2->mze_hash)
264 if (mze1->mze_cd > mze2->mze_cd)
266 if (mze1->mze_cd < mze2->mze_cd)
272 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
277 ASSERT(zap->zap_ismicro);
278 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
280 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
281 mze->mze_chunkid = chunkid;
282 mze->mze_hash = hash;
283 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
284 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
285 if (avl_find(&zap->zap_m.zap_avl, mze, &idx) != NULL) {
286 kmem_free(mze, sizeof (mzap_ent_t));
289 avl_insert(&zap->zap_m.zap_avl, mze, idx);
294 mze_find(zap_name_t *zn)
296 mzap_ent_t mze_tofind;
299 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
301 ASSERT(zn->zn_zap->zap_ismicro);
302 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
304 mze_tofind.mze_hash = zn->zn_hash;
305 mze_tofind.mze_cd = 0;
308 mze = avl_find(avl, &mze_tofind, &idx);
310 mze = avl_nearest(avl, idx, AVL_AFTER);
311 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
312 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
313 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
316 if (zn->zn_matchtype == MT_BEST) {
317 zn->zn_matchtype = MT_FIRST;
324 mze_find_unused_cd(zap_t *zap, uint64_t hash)
326 mzap_ent_t mze_tofind;
329 avl_tree_t *avl = &zap->zap_m.zap_avl;
332 ASSERT(zap->zap_ismicro);
333 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
335 mze_tofind.mze_hash = hash;
336 mze_tofind.mze_cd = 0;
339 for (mze = avl_find(avl, &mze_tofind, &idx);
340 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
341 if (mze->mze_cd != cd)
350 mze_remove(zap_t *zap, mzap_ent_t *mze)
352 ASSERT(zap->zap_ismicro);
353 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
355 avl_remove(&zap->zap_m.zap_avl, mze);
356 kmem_free(mze, sizeof (mzap_ent_t));
360 mze_destroy(zap_t *zap)
363 void *avlcookie = NULL;
365 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
366 kmem_free(mze, sizeof (mzap_ent_t));
367 avl_destroy(&zap->zap_m.zap_avl);
371 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
377 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
379 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
380 rw_init(&zap->zap_rwlock, 0, 0, 0);
381 rw_enter(&zap->zap_rwlock, RW_WRITER);
382 zap->zap_objset = os;
383 zap->zap_object = obj;
386 if (*(uint64_t *)db->db_data != ZBT_MICRO) {
387 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
388 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
390 zap->zap_ismicro = TRUE;
394 * Make sure that zap_ismicro is set before we let others see
395 * it, because zap_lockdir() checks zap_ismicro without the lock
398 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
399 winner = dmu_buf_set_user(db, &zap->zap_dbu);
401 if (winner != NULL) {
402 rw_exit(&zap->zap_rwlock);
403 rw_destroy(&zap->zap_rwlock);
404 if (!zap->zap_ismicro)
405 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
406 kmem_free(zap, sizeof (zap_t));
410 if (zap->zap_ismicro) {
411 zap->zap_salt = zap_m_phys(zap)->mz_salt;
412 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
413 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
414 avl_create(&zap->zap_m.zap_avl, mze_compare,
415 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
417 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
418 mzap_ent_phys_t *mze =
419 &zap_m_phys(zap)->mz_chunk[i];
420 if (mze->mze_name[0]) {
423 zn = zap_name_alloc(zap, mze->mze_name,
425 if (mze_insert(zap, i, zn->zn_hash) == 0)
426 zap->zap_m.zap_num_entries++;
428 printf("ZFS WARNING: Duplicated ZAP "
429 "entry detected (%s).\n",
436 zap->zap_salt = zap_f_phys(zap)->zap_salt;
437 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
439 ASSERT3U(sizeof (struct zap_leaf_header), ==,
440 2*ZAP_LEAF_CHUNKSIZE);
443 * The embedded pointer table should not overlap the
446 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
447 &zap_f_phys(zap)->zap_salt);
450 * The embedded pointer table should end at the end of
453 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
454 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
455 (uintptr_t)zap_f_phys(zap), ==,
456 zap->zap_dbuf->db_size);
458 rw_exit(&zap->zap_rwlock);
463 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
464 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
473 err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
479 dmu_object_info_t doi;
480 dmu_object_info_from_db(db, &doi);
481 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
485 zap = dmu_buf_get_user(db);
487 zap = mzap_open(os, obj, db);
490 * We're checking zap_ismicro without the lock held, in order to
491 * tell what type of lock we want. Once we have some sort of
492 * lock, see if it really is the right type. In practice this
493 * can only be different if it was upgraded from micro to fat,
494 * and micro wanted WRITER but fat only needs READER.
496 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
497 rw_enter(&zap->zap_rwlock, lt);
498 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
499 /* it was upgraded, now we only need reader */
500 ASSERT(lt == RW_WRITER);
502 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
503 rw_downgrade(&zap->zap_rwlock);
507 zap->zap_objset = os;
510 dmu_buf_will_dirty(db, tx);
512 ASSERT3P(zap->zap_dbuf, ==, db);
514 ASSERT(!zap->zap_ismicro ||
515 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
516 if (zap->zap_ismicro && tx && adding &&
517 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
518 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
519 if (newsz > MZAP_MAX_BLKSZ) {
520 dprintf("upgrading obj %llu: num_entries=%u\n",
521 obj, zap->zap_m.zap_num_entries);
523 return (mzap_upgrade(zapp, tx, 0));
525 err = 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_unlockdir(zap_t *zap)
538 rw_exit(&zap->zap_rwlock);
539 dmu_buf_rele(zap->zap_dbuf, NULL);
543 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
550 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
552 sz = zap->zap_dbuf->db_size;
553 mzp = zio_buf_alloc(sz);
554 bcopy(zap->zap_dbuf->db_data, mzp, sz);
555 nchunks = zap->zap_m.zap_num_chunks;
558 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
559 1ULL << fzap_default_block_shift, 0, tx);
561 zio_buf_free(mzp, sz);
566 dprintf("upgrading obj=%llu with %u chunks\n",
567 zap->zap_object, nchunks);
568 /* XXX destroy the avl later, so we can use the stored hash value */
571 fzap_upgrade(zap, tx, flags);
573 for (i = 0; i < nchunks; i++) {
574 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
576 if (mze->mze_name[0] == 0)
578 dprintf("adding %s=%llu\n",
579 mze->mze_name, mze->mze_value);
580 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
581 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
582 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
587 zio_buf_free(mzp, sz);
593 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
599 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
603 dmu_object_info_t doi;
604 dmu_object_info_from_db(db, &doi);
605 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
609 dmu_buf_will_dirty(db, tx);
611 zp->mz_block_type = ZBT_MICRO;
612 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
613 zp->mz_normflags = normflags;
614 dmu_buf_rele(db, FTAG);
618 /* Only fat zap supports flags; upgrade immediately. */
619 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
620 B_FALSE, B_FALSE, &zap));
621 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
627 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
628 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
630 return (zap_create_claim_norm(os, obj,
631 0, ot, bonustype, bonuslen, tx));
635 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
636 dmu_object_type_t ot,
637 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
641 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
644 mzap_create_impl(os, obj, normflags, 0, tx);
649 zap_create(objset_t *os, dmu_object_type_t ot,
650 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
652 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
656 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
657 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
659 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
661 mzap_create_impl(os, obj, normflags, 0, tx);
666 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
667 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
668 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
670 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
672 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
673 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
674 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
675 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
677 VERIFY(dmu_object_set_blocksize(os, obj,
678 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
680 mzap_create_impl(os, obj, normflags, flags, tx);
685 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
688 * dmu_object_free will free the object number and free the
689 * data. Freeing the data will cause our pageout function to be
690 * called, which will destroy our data (zap_leaf_t's and zap_t).
693 return (dmu_object_free(os, zapobj, tx));
701 rw_destroy(&zap->zap_rwlock);
703 if (zap->zap_ismicro)
706 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
708 kmem_free(zap, sizeof (zap_t));
712 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
717 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
720 if (!zap->zap_ismicro) {
721 err = fzap_count(zap, count);
723 *count = zap->zap_m.zap_num_entries;
730 * zn may be NULL; if not specified, it will be computed if needed.
731 * See also the comment above zap_entry_normalization_conflict().
734 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
737 int direction = AVL_BEFORE;
738 boolean_t allocdzn = B_FALSE;
740 if (zap->zap_normflags == 0)
744 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
745 other && other->mze_hash == mze->mze_hash;
746 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
749 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
753 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
760 if (direction == AVL_BEFORE) {
761 direction = AVL_AFTER;
771 * Routines for manipulating attributes.
775 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
776 uint64_t integer_size, uint64_t num_integers, void *buf)
778 return (zap_lookup_norm(os, zapobj, name, integer_size,
779 num_integers, buf, MT_EXACT, NULL, 0, NULL));
783 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
784 uint64_t integer_size, uint64_t num_integers, void *buf,
785 matchtype_t mt, char *realname, int rn_len,
793 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
796 zn = zap_name_alloc(zap, name, mt);
799 return (SET_ERROR(ENOTSUP));
802 if (!zap->zap_ismicro) {
803 err = fzap_lookup(zn, integer_size, num_integers, buf,
804 realname, rn_len, ncp);
808 err = SET_ERROR(ENOENT);
810 if (num_integers < 1) {
811 err = SET_ERROR(EOVERFLOW);
812 } else if (integer_size != 8) {
813 err = SET_ERROR(EINVAL);
816 MZE_PHYS(zap, mze)->mze_value;
817 (void) strlcpy(realname,
818 MZE_PHYS(zap, mze)->mze_name, rn_len);
820 *ncp = mzap_normalization_conflict(zap,
832 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
839 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
842 zn = zap_name_alloc_uint64(zap, key, key_numints);
845 return (SET_ERROR(ENOTSUP));
855 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
856 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
862 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
865 zn = zap_name_alloc_uint64(zap, key, key_numints);
868 return (SET_ERROR(ENOTSUP));
871 err = fzap_lookup(zn, integer_size, num_integers, buf,
879 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
881 int err = zap_lookup_norm(os, zapobj, name, 0,
882 0, NULL, MT_EXACT, NULL, 0, NULL);
883 if (err == EOVERFLOW || err == EINVAL)
884 err = 0; /* found, but skipped reading the value */
889 zap_length(objset_t *os, uint64_t zapobj, const char *name,
890 uint64_t *integer_size, uint64_t *num_integers)
897 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
900 zn = zap_name_alloc(zap, name, MT_EXACT);
903 return (SET_ERROR(ENOTSUP));
905 if (!zap->zap_ismicro) {
906 err = fzap_length(zn, integer_size, num_integers);
910 err = SET_ERROR(ENOENT);
924 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
925 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
931 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
934 zn = zap_name_alloc_uint64(zap, key, key_numints);
937 return (SET_ERROR(ENOTSUP));
939 err = fzap_length(zn, integer_size, num_integers);
946 mzap_addent(zap_name_t *zn, uint64_t value)
949 zap_t *zap = zn->zn_zap;
950 int start = zap->zap_m.zap_alloc_next;
953 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
956 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
957 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
958 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
962 cd = mze_find_unused_cd(zap, zn->zn_hash);
963 /* given the limited size of the microzap, this can't happen */
964 ASSERT(cd < zap_maxcd(zap));
967 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
968 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
969 if (mze->mze_name[0] == 0) {
970 mze->mze_value = value;
972 (void) strcpy(mze->mze_name, zn->zn_key_orig);
973 zap->zap_m.zap_num_entries++;
974 zap->zap_m.zap_alloc_next = i+1;
975 if (zap->zap_m.zap_alloc_next ==
976 zap->zap_m.zap_num_chunks)
977 zap->zap_m.zap_alloc_next = 0;
978 VERIFY(0 == mze_insert(zap, i, zn->zn_hash));
986 ASSERT(!"out of entries!");
990 zap_add(objset_t *os, uint64_t zapobj, const char *key,
991 int integer_size, uint64_t num_integers,
992 const void *val, dmu_tx_t *tx)
997 const uint64_t *intval = val;
1000 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1003 zn = zap_name_alloc(zap, key, MT_EXACT);
1006 return (SET_ERROR(ENOTSUP));
1008 if (!zap->zap_ismicro) {
1009 err = fzap_add(zn, integer_size, num_integers, val, tx);
1010 zap = zn->zn_zap; /* fzap_add() may change zap */
1011 } else if (integer_size != 8 || num_integers != 1 ||
1012 strlen(key) >= MZAP_NAME_LEN) {
1013 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1015 err = fzap_add(zn, integer_size, num_integers, val, tx);
1016 zap = zn->zn_zap; /* fzap_add() may change zap */
1020 err = SET_ERROR(EEXIST);
1022 mzap_addent(zn, *intval);
1025 ASSERT(zap == zn->zn_zap);
1027 if (zap != NULL) /* may be NULL if fzap_add() failed */
1033 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1034 int key_numints, int integer_size, uint64_t num_integers,
1035 const void *val, dmu_tx_t *tx)
1041 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1044 zn = zap_name_alloc_uint64(zap, key, key_numints);
1047 return (SET_ERROR(ENOTSUP));
1049 err = fzap_add(zn, integer_size, num_integers, val, tx);
1050 zap = zn->zn_zap; /* fzap_add() may change zap */
1052 if (zap != NULL) /* may be NULL if fzap_add() failed */
1058 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1059 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1064 const uint64_t *intval = val;
1070 * If there is an old value, it shouldn't change across the
1071 * lockdir (eg, due to bprewrite's xlation).
1073 if (integer_size == 8 && num_integers == 1)
1074 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1077 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1080 zn = zap_name_alloc(zap, name, MT_EXACT);
1083 return (SET_ERROR(ENOTSUP));
1085 if (!zap->zap_ismicro) {
1086 err = fzap_update(zn, integer_size, num_integers, val, tx);
1087 zap = zn->zn_zap; /* fzap_update() may change zap */
1088 } else if (integer_size != 8 || num_integers != 1 ||
1089 strlen(name) >= MZAP_NAME_LEN) {
1090 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1091 zapobj, integer_size, num_integers, name);
1092 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1094 err = fzap_update(zn, integer_size, num_integers,
1096 zap = zn->zn_zap; /* fzap_update() may change zap */
1100 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1101 MZE_PHYS(zap, mze)->mze_value = *intval;
1103 mzap_addent(zn, *intval);
1106 ASSERT(zap == zn->zn_zap);
1108 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1114 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1116 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1122 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1125 zn = zap_name_alloc_uint64(zap, key, key_numints);
1128 return (SET_ERROR(ENOTSUP));
1130 err = fzap_update(zn, integer_size, num_integers, val, tx);
1131 zap = zn->zn_zap; /* fzap_update() may change zap */
1133 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1139 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1141 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1145 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1146 matchtype_t mt, dmu_tx_t *tx)
1153 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1156 zn = zap_name_alloc(zap, name, mt);
1159 return (SET_ERROR(ENOTSUP));
1161 if (!zap->zap_ismicro) {
1162 err = fzap_remove(zn, tx);
1166 err = SET_ERROR(ENOENT);
1168 zap->zap_m.zap_num_entries--;
1169 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1170 sizeof (mzap_ent_phys_t));
1171 mze_remove(zap, mze);
1180 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1181 int key_numints, dmu_tx_t *tx)
1187 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1190 zn = zap_name_alloc_uint64(zap, key, key_numints);
1193 return (SET_ERROR(ENOTSUP));
1195 err = fzap_remove(zn, tx);
1202 * Routines for iterating over the attributes.
1206 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1207 uint64_t serialized)
1212 zc->zc_zapobj = zapobj;
1213 zc->zc_serialized = serialized;
1219 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1221 zap_cursor_init_serialized(zc, os, zapobj, 0);
1225 zap_cursor_fini(zap_cursor_t *zc)
1228 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1229 zap_unlockdir(zc->zc_zap);
1233 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1234 zap_put_leaf(zc->zc_leaf);
1237 zc->zc_objset = NULL;
1241 zap_cursor_serialize(zap_cursor_t *zc)
1243 if (zc->zc_hash == -1ULL)
1245 if (zc->zc_zap == NULL)
1246 return (zc->zc_serialized);
1247 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1248 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1251 * We want to keep the high 32 bits of the cursor zero if we can, so
1252 * that 32-bit programs can access this. So usually use a small
1253 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1256 * [ collision differentiator | zap_hashbits()-bit hash value ]
1258 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1259 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1263 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1267 mzap_ent_t mze_tofind;
1270 if (zc->zc_hash == -1ULL)
1271 return (SET_ERROR(ENOENT));
1273 if (zc->zc_zap == NULL) {
1275 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1276 RW_READER, TRUE, FALSE, &zc->zc_zap);
1281 * To support zap_cursor_init_serialized, advance, retrieve,
1282 * we must add to the existing zc_cd, which may already
1283 * be 1 due to the zap_cursor_advance.
1285 ASSERT(zc->zc_hash == 0);
1286 hb = zap_hashbits(zc->zc_zap);
1287 zc->zc_hash = zc->zc_serialized << (64 - hb);
1288 zc->zc_cd += zc->zc_serialized >> hb;
1289 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1292 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1294 if (!zc->zc_zap->zap_ismicro) {
1295 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1297 mze_tofind.mze_hash = zc->zc_hash;
1298 mze_tofind.mze_cd = zc->zc_cd;
1300 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1302 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1306 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1307 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1308 za->za_normalization_conflict =
1309 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1310 za->za_integer_length = 8;
1311 za->za_num_integers = 1;
1312 za->za_first_integer = mzep->mze_value;
1313 (void) strcpy(za->za_name, mzep->mze_name);
1314 zc->zc_hash = mze->mze_hash;
1315 zc->zc_cd = mze->mze_cd;
1318 zc->zc_hash = -1ULL;
1319 err = SET_ERROR(ENOENT);
1322 rw_exit(&zc->zc_zap->zap_rwlock);
1327 zap_cursor_advance(zap_cursor_t *zc)
1329 if (zc->zc_hash == -1ULL)
1335 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1341 if (zc->zc_zap == NULL) {
1342 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1343 RW_READER, TRUE, FALSE, &zc->zc_zap);
1347 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1350 zn = zap_name_alloc(zc->zc_zap, name, mt);
1352 rw_exit(&zc->zc_zap->zap_rwlock);
1353 return (SET_ERROR(ENOTSUP));
1356 if (!zc->zc_zap->zap_ismicro) {
1357 err = fzap_cursor_move_to_key(zc, zn);
1361 err = SET_ERROR(ENOENT);
1364 zc->zc_hash = mze->mze_hash;
1365 zc->zc_cd = mze->mze_cd;
1370 rw_exit(&zc->zc_zap->zap_rwlock);
1375 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1380 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1384 bzero(zs, sizeof (zap_stats_t));
1386 if (zap->zap_ismicro) {
1387 zs->zs_blocksize = zap->zap_dbuf->db_size;
1388 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1389 zs->zs_num_blocks = 1;
1391 fzap_get_stats(zap, zs);
1398 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1399 uint64_t *towrite, uint64_t *tooverwrite)
1405 * Since, we don't have a name, we cannot figure out which blocks will
1406 * be affected in this operation. So, account for the worst case :
1407 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1408 * - 4 new blocks written if adding:
1409 * - 2 blocks for possibly split leaves,
1410 * - 2 grown ptrtbl blocks
1412 * This also accomodates the case where an add operation to a fairly
1413 * large microzap results in a promotion to fatzap.
1416 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1421 * We lock the zap with adding == FALSE. Because, if we pass
1422 * the actual value of add, it could trigger a mzap_upgrade().
1423 * At present we are just evaluating the possibility of this operation
1424 * and hence we donot want to trigger an upgrade.
1426 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1430 if (!zap->zap_ismicro) {
1431 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1433 err = fzap_count_write(zn, add, towrite,
1438 * We treat this case as similar to (name == NULL)
1440 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1444 * We are here if (name != NULL) and this is a micro-zap.
1445 * We account for the header block depending on whether it
1448 * Incase of an add-operation it is hard to find out
1449 * if this add will promote this microzap to fatzap.
1450 * Hence, we consider the worst case and account for the
1451 * blocks assuming this microzap would be promoted to a
1454 * 1 block overwritten : header block
1455 * 4 new blocks written : 2 new split leaf, 2 grown
1458 if (dmu_buf_freeable(zap->zap_dbuf))
1459 *tooverwrite += MZAP_MAX_BLKSZ;
1461 *towrite += MZAP_MAX_BLKSZ;
1464 *towrite += 4 * MZAP_MAX_BLKSZ;