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)
376 uint64_t *zap_hdr = (uint64_t *)db->db_data;
377 uint64_t zap_block_type = zap_hdr[0];
378 uint64_t zap_magic = zap_hdr[1];
380 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
382 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
383 rw_init(&zap->zap_rwlock, 0, 0, 0);
384 rw_enter(&zap->zap_rwlock, RW_WRITER);
385 zap->zap_objset = os;
386 zap->zap_object = obj;
389 if (zap_block_type != ZBT_MICRO) {
390 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
391 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
392 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
393 winner = NULL; /* No actual winner here... */
397 zap->zap_ismicro = TRUE;
401 * Make sure that zap_ismicro is set before we let others see
402 * it, because zap_lockdir() checks zap_ismicro without the lock
405 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
406 winner = dmu_buf_set_user(db, &zap->zap_dbu);
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);
463 rw_exit(&zap->zap_rwlock);
464 rw_destroy(&zap->zap_rwlock);
465 if (!zap->zap_ismicro)
466 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
467 kmem_free(zap, sizeof (zap_t));
472 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
473 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
482 err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
488 dmu_object_info_t doi;
489 dmu_object_info_from_db(db, &doi);
490 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
494 zap = dmu_buf_get_user(db);
496 zap = mzap_open(os, obj, db);
499 * mzap_open() didn't like what it saw on-disk.
500 * Check for corruption!
502 dmu_buf_rele(db, NULL);
503 return (SET_ERROR(EIO));
508 * We're checking zap_ismicro without the lock held, in order to
509 * tell what type of lock we want. Once we have some sort of
510 * lock, see if it really is the right type. In practice this
511 * can only be different if it was upgraded from micro to fat,
512 * and micro wanted WRITER but fat only needs READER.
514 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
515 rw_enter(&zap->zap_rwlock, lt);
516 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
517 /* it was upgraded, now we only need reader */
518 ASSERT(lt == RW_WRITER);
520 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
521 rw_downgrade(&zap->zap_rwlock);
525 zap->zap_objset = os;
528 dmu_buf_will_dirty(db, tx);
530 ASSERT3P(zap->zap_dbuf, ==, db);
532 ASSERT(!zap->zap_ismicro ||
533 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
534 if (zap->zap_ismicro && tx && adding &&
535 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
536 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
537 if (newsz > MZAP_MAX_BLKSZ) {
538 dprintf("upgrading obj %llu: num_entries=%u\n",
539 obj, zap->zap_m.zap_num_entries);
541 return (mzap_upgrade(zapp, tx, 0));
543 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
545 zap->zap_m.zap_num_chunks =
546 db->db_size / MZAP_ENT_LEN - 1;
554 zap_unlockdir(zap_t *zap)
556 rw_exit(&zap->zap_rwlock);
557 dmu_buf_rele(zap->zap_dbuf, NULL);
561 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
568 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
570 sz = zap->zap_dbuf->db_size;
571 mzp = zio_buf_alloc(sz);
572 bcopy(zap->zap_dbuf->db_data, mzp, sz);
573 nchunks = zap->zap_m.zap_num_chunks;
576 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
577 1ULL << fzap_default_block_shift, 0, tx);
579 zio_buf_free(mzp, sz);
584 dprintf("upgrading obj=%llu with %u chunks\n",
585 zap->zap_object, nchunks);
586 /* XXX destroy the avl later, so we can use the stored hash value */
589 fzap_upgrade(zap, tx, flags);
591 for (i = 0; i < nchunks; i++) {
592 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
594 if (mze->mze_name[0] == 0)
596 dprintf("adding %s=%llu\n",
597 mze->mze_name, mze->mze_value);
598 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
599 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
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, &zap));
639 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
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, &zap);
738 if (!zap->zap_ismicro) {
739 err = fzap_count(zap, count);
741 *count = zap->zap_m.zap_num_entries;
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_norm(objset_t *os, uint64_t zapobj, const char *name,
802 uint64_t integer_size, uint64_t num_integers, void *buf,
803 matchtype_t mt, char *realname, int rn_len,
811 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
814 zn = zap_name_alloc(zap, name, mt);
817 return (SET_ERROR(ENOTSUP));
820 if (!zap->zap_ismicro) {
821 err = fzap_lookup(zn, integer_size, num_integers, buf,
822 realname, rn_len, ncp);
826 err = SET_ERROR(ENOENT);
828 if (num_integers < 1) {
829 err = SET_ERROR(EOVERFLOW);
830 } else if (integer_size != 8) {
831 err = SET_ERROR(EINVAL);
834 MZE_PHYS(zap, mze)->mze_value;
835 (void) strlcpy(realname,
836 MZE_PHYS(zap, mze)->mze_name, rn_len);
838 *ncp = mzap_normalization_conflict(zap,
850 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
857 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
860 zn = zap_name_alloc_uint64(zap, key, key_numints);
863 return (SET_ERROR(ENOTSUP));
873 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
874 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
880 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
883 zn = zap_name_alloc_uint64(zap, key, key_numints);
886 return (SET_ERROR(ENOTSUP));
889 err = fzap_lookup(zn, integer_size, num_integers, buf,
897 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
899 int err = zap_lookup_norm(os, zapobj, name, 0,
900 0, NULL, MT_EXACT, NULL, 0, NULL);
901 if (err == EOVERFLOW || err == EINVAL)
902 err = 0; /* found, but skipped reading the value */
907 zap_length(objset_t *os, uint64_t zapobj, const char *name,
908 uint64_t *integer_size, uint64_t *num_integers)
915 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
918 zn = zap_name_alloc(zap, name, MT_EXACT);
921 return (SET_ERROR(ENOTSUP));
923 if (!zap->zap_ismicro) {
924 err = fzap_length(zn, integer_size, num_integers);
928 err = SET_ERROR(ENOENT);
942 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
943 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
949 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
952 zn = zap_name_alloc_uint64(zap, key, key_numints);
955 return (SET_ERROR(ENOTSUP));
957 err = fzap_length(zn, integer_size, num_integers);
964 mzap_addent(zap_name_t *zn, uint64_t value)
967 zap_t *zap = zn->zn_zap;
968 int start = zap->zap_m.zap_alloc_next;
971 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
974 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
975 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
976 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
980 cd = mze_find_unused_cd(zap, zn->zn_hash);
981 /* given the limited size of the microzap, this can't happen */
982 ASSERT(cd < zap_maxcd(zap));
985 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
986 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
987 if (mze->mze_name[0] == 0) {
988 mze->mze_value = value;
990 (void) strcpy(mze->mze_name, zn->zn_key_orig);
991 zap->zap_m.zap_num_entries++;
992 zap->zap_m.zap_alloc_next = i+1;
993 if (zap->zap_m.zap_alloc_next ==
994 zap->zap_m.zap_num_chunks)
995 zap->zap_m.zap_alloc_next = 0;
996 VERIFY(0 == mze_insert(zap, i, zn->zn_hash));
1004 ASSERT(!"out of entries!");
1008 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1009 int integer_size, uint64_t num_integers,
1010 const void *val, dmu_tx_t *tx)
1015 const uint64_t *intval = val;
1018 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1021 zn = zap_name_alloc(zap, key, MT_EXACT);
1024 return (SET_ERROR(ENOTSUP));
1026 if (!zap->zap_ismicro) {
1027 err = fzap_add(zn, integer_size, num_integers, val, tx);
1028 zap = zn->zn_zap; /* fzap_add() may change zap */
1029 } else if (integer_size != 8 || num_integers != 1 ||
1030 strlen(key) >= MZAP_NAME_LEN) {
1031 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1033 err = fzap_add(zn, integer_size, num_integers, val, tx);
1034 zap = zn->zn_zap; /* fzap_add() may change zap */
1038 err = SET_ERROR(EEXIST);
1040 mzap_addent(zn, *intval);
1043 ASSERT(zap == zn->zn_zap);
1045 if (zap != NULL) /* may be NULL if fzap_add() failed */
1051 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1052 int key_numints, int integer_size, uint64_t num_integers,
1053 const void *val, dmu_tx_t *tx)
1059 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1062 zn = zap_name_alloc_uint64(zap, key, key_numints);
1065 return (SET_ERROR(ENOTSUP));
1067 err = fzap_add(zn, integer_size, num_integers, val, tx);
1068 zap = zn->zn_zap; /* fzap_add() may change zap */
1070 if (zap != NULL) /* may be NULL if fzap_add() failed */
1076 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1077 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1082 const uint64_t *intval = val;
1088 * If there is an old value, it shouldn't change across the
1089 * lockdir (eg, due to bprewrite's xlation).
1091 if (integer_size == 8 && num_integers == 1)
1092 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1095 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1098 zn = zap_name_alloc(zap, name, MT_EXACT);
1101 return (SET_ERROR(ENOTSUP));
1103 if (!zap->zap_ismicro) {
1104 err = fzap_update(zn, integer_size, num_integers, val, tx);
1105 zap = zn->zn_zap; /* fzap_update() may change zap */
1106 } else if (integer_size != 8 || num_integers != 1 ||
1107 strlen(name) >= MZAP_NAME_LEN) {
1108 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1109 zapobj, integer_size, num_integers, name);
1110 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1112 err = fzap_update(zn, integer_size, num_integers,
1114 zap = zn->zn_zap; /* fzap_update() may change zap */
1118 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1119 MZE_PHYS(zap, mze)->mze_value = *intval;
1121 mzap_addent(zn, *intval);
1124 ASSERT(zap == zn->zn_zap);
1126 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1132 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1134 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1140 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1143 zn = zap_name_alloc_uint64(zap, key, key_numints);
1146 return (SET_ERROR(ENOTSUP));
1148 err = fzap_update(zn, integer_size, num_integers, val, tx);
1149 zap = zn->zn_zap; /* fzap_update() may change zap */
1151 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1157 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1159 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1163 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1164 matchtype_t mt, dmu_tx_t *tx)
1171 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1174 zn = zap_name_alloc(zap, name, mt);
1177 return (SET_ERROR(ENOTSUP));
1179 if (!zap->zap_ismicro) {
1180 err = fzap_remove(zn, tx);
1184 err = SET_ERROR(ENOENT);
1186 zap->zap_m.zap_num_entries--;
1187 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1188 sizeof (mzap_ent_phys_t));
1189 mze_remove(zap, mze);
1198 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1199 int key_numints, dmu_tx_t *tx)
1205 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1208 zn = zap_name_alloc_uint64(zap, key, key_numints);
1211 return (SET_ERROR(ENOTSUP));
1213 err = fzap_remove(zn, tx);
1220 * Routines for iterating over the attributes.
1224 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1225 uint64_t serialized)
1230 zc->zc_zapobj = zapobj;
1231 zc->zc_serialized = serialized;
1237 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1239 zap_cursor_init_serialized(zc, os, zapobj, 0);
1243 zap_cursor_fini(zap_cursor_t *zc)
1246 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1247 zap_unlockdir(zc->zc_zap);
1251 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1252 zap_put_leaf(zc->zc_leaf);
1255 zc->zc_objset = NULL;
1259 zap_cursor_serialize(zap_cursor_t *zc)
1261 if (zc->zc_hash == -1ULL)
1263 if (zc->zc_zap == NULL)
1264 return (zc->zc_serialized);
1265 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1266 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1269 * We want to keep the high 32 bits of the cursor zero if we can, so
1270 * that 32-bit programs can access this. So usually use a small
1271 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1274 * [ collision differentiator | zap_hashbits()-bit hash value ]
1276 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1277 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1281 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1285 mzap_ent_t mze_tofind;
1288 if (zc->zc_hash == -1ULL)
1289 return (SET_ERROR(ENOENT));
1291 if (zc->zc_zap == NULL) {
1293 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1294 RW_READER, TRUE, FALSE, &zc->zc_zap);
1299 * To support zap_cursor_init_serialized, advance, retrieve,
1300 * we must add to the existing zc_cd, which may already
1301 * be 1 due to the zap_cursor_advance.
1303 ASSERT(zc->zc_hash == 0);
1304 hb = zap_hashbits(zc->zc_zap);
1305 zc->zc_hash = zc->zc_serialized << (64 - hb);
1306 zc->zc_cd += zc->zc_serialized >> hb;
1307 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1310 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1312 if (!zc->zc_zap->zap_ismicro) {
1313 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1315 mze_tofind.mze_hash = zc->zc_hash;
1316 mze_tofind.mze_cd = zc->zc_cd;
1318 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1320 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1324 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1325 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1326 za->za_normalization_conflict =
1327 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1328 za->za_integer_length = 8;
1329 za->za_num_integers = 1;
1330 za->za_first_integer = mzep->mze_value;
1331 (void) strcpy(za->za_name, mzep->mze_name);
1332 zc->zc_hash = mze->mze_hash;
1333 zc->zc_cd = mze->mze_cd;
1336 zc->zc_hash = -1ULL;
1337 err = SET_ERROR(ENOENT);
1340 rw_exit(&zc->zc_zap->zap_rwlock);
1345 zap_cursor_advance(zap_cursor_t *zc)
1347 if (zc->zc_hash == -1ULL)
1353 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1359 if (zc->zc_zap == NULL) {
1360 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1361 RW_READER, TRUE, FALSE, &zc->zc_zap);
1365 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1368 zn = zap_name_alloc(zc->zc_zap, name, mt);
1370 rw_exit(&zc->zc_zap->zap_rwlock);
1371 return (SET_ERROR(ENOTSUP));
1374 if (!zc->zc_zap->zap_ismicro) {
1375 err = fzap_cursor_move_to_key(zc, zn);
1379 err = SET_ERROR(ENOENT);
1382 zc->zc_hash = mze->mze_hash;
1383 zc->zc_cd = mze->mze_cd;
1388 rw_exit(&zc->zc_zap->zap_rwlock);
1393 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1398 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1402 bzero(zs, sizeof (zap_stats_t));
1404 if (zap->zap_ismicro) {
1405 zs->zs_blocksize = zap->zap_dbuf->db_size;
1406 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1407 zs->zs_num_blocks = 1;
1409 fzap_get_stats(zap, zs);
1416 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1417 uint64_t *towrite, uint64_t *tooverwrite)
1423 * Since, we don't have a name, we cannot figure out which blocks will
1424 * be affected in this operation. So, account for the worst case :
1425 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1426 * - 4 new blocks written if adding:
1427 * - 2 blocks for possibly split leaves,
1428 * - 2 grown ptrtbl blocks
1430 * This also accomodates the case where an add operation to a fairly
1431 * large microzap results in a promotion to fatzap.
1434 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1439 * We lock the zap with adding == FALSE. Because, if we pass
1440 * the actual value of add, it could trigger a mzap_upgrade().
1441 * At present we are just evaluating the possibility of this operation
1442 * and hence we donot want to trigger an upgrade.
1444 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1448 if (!zap->zap_ismicro) {
1449 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1451 err = fzap_count_write(zn, add, towrite,
1456 * We treat this case as similar to (name == NULL)
1458 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1462 * We are here if (name != NULL) and this is a micro-zap.
1463 * We account for the header block depending on whether it
1466 * Incase of an add-operation it is hard to find out
1467 * if this add will promote this microzap to fatzap.
1468 * Hence, we consider the worst case and account for the
1469 * blocks assuming this microzap would be promoted to a
1472 * 1 block overwritten : header block
1473 * 4 new blocks written : 2 new split leaf, 2 grown
1476 if (dmu_buf_freeable(zap->zap_dbuf))
1477 *tooverwrite += MZAP_MAX_BLKSZ;
1479 *towrite += MZAP_MAX_BLKSZ;
1482 *towrite += 4 * MZAP_MAX_BLKSZ;