]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zap_micro.c
Provide more flexible object allocation interface
[mirror_zfs.git] / module / zfs / zap_micro.c
index 131c33e29baf0eede462e53bbe35a52aa6eb330b..fa369f7975484a13372c6fc776663efa5f4dd873 100644 (file)
  *
  * CDDL HEADER END
  */
+
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
+ * Copyright 2017 Nexenta Systems, Inc.
  */
 
 #include <sys/zio.h>
@@ -86,22 +88,20 @@ zap_hash(zap_name_t *zn)
                ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
 
                if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
-                       int i;
                        const uint64_t *wp = zn->zn_key_norm;
 
                        ASSERT(zn->zn_key_intlen == 8);
-                       for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
-                               int j;
+                       for (int i = 0; i < zn->zn_key_norm_numints;
+                           wp++, i++) {
                                uint64_t word = *wp;
 
-                               for (j = 0; j < zn->zn_key_intlen; j++) {
+                               for (int j = 0; j < zn->zn_key_intlen; j++) {
                                        h = (h >> 8) ^
                                            zfs_crc64_table[(h ^ word) & 0xFF];
                                        word >>= NBBY;
                                }
                        }
                } else {
-                       int i, len;
                        const uint8_t *cp = zn->zn_key_norm;
 
                        /*
@@ -111,10 +111,10 @@ zap_hash(zap_name_t *zn)
                         * zn_key_*_numints includes the terminating
                         * null for non-binary keys.)
                         */
-                       len = zn->zn_key_norm_numints - 1;
+                       int len = zn->zn_key_norm_numints - 1;
 
                        ASSERT(zn->zn_key_intlen == 1);
-                       for (i = 0; i < len; cp++, i++) {
+                       for (int i = 0; i < len; cp++, i++) {
                                h = (h >> 8) ^
                                    zfs_crc64_table[(h ^ *cp) & 0xFF];
                        }
@@ -124,7 +124,7 @@ zap_hash(zap_name_t *zn)
         * Don't use all 64 bits, since we need some in the cookie for
         * the collision differentiator.  We MUST use the high bits,
         * since those are the ones that we first pay attention to when
-        * chosing the bucket.
+        * choosing the bucket.
         */
        h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
 
@@ -132,20 +132,17 @@ zap_hash(zap_name_t *zn)
 }
 
 static int
-zap_normalize(zap_t *zap, const char *name, char *namenorm)
+zap_normalize(zap_t *zap, const char *name, char *namenorm, int normflags)
 {
-       size_t inlen, outlen;
-       int err;
-
        ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
 
-       inlen = strlen(name) + 1;
-       outlen = ZAP_MAXNAMELEN;
+       size_t inlen = strlen(name) + 1;
+       size_t outlen = ZAP_MAXNAMELEN;
 
-       err = 0;
+       int err = 0;
        (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
-           zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
-           U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
+           normflags | U8_TEXTPREP_IGNORE_NULL | U8_TEXTPREP_IGNORE_INVALID,
+           U8_UNICODE_LATEST, &err);
 
        return (err);
 }
@@ -155,15 +152,15 @@ zap_match(zap_name_t *zn, const char *matchname)
 {
        ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
 
-       if (zn->zn_matchtype == MT_FIRST) {
+       if (zn->zn_matchtype & MT_NORMALIZE) {
                char norm[ZAP_MAXNAMELEN];
 
-               if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
+               if (zap_normalize(zn->zn_zap, matchname, norm,
+                   zn->zn_normflags) != 0)
                        return (B_FALSE);
 
                return (strcmp(zn->zn_key_norm, norm) == 0);
        } else {
-               /* MT_BEST or MT_EXACT */
                return (strcmp(zn->zn_key_orig, matchname) == 0);
        }
 }
@@ -184,15 +181,30 @@ zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
        zn->zn_key_orig = key;
        zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
        zn->zn_matchtype = mt;
+       zn->zn_normflags = zap->zap_normflags;
+
+       /*
+        * If we're dealing with a case sensitive lookup on a mixed or
+        * insensitive fs, remove U8_TEXTPREP_TOUPPER or the lookup
+        * will fold case to all caps overriding the lookup request.
+        */
+       if (mt & MT_MATCH_CASE)
+               zn->zn_normflags &= ~U8_TEXTPREP_TOUPPER;
+
        if (zap->zap_normflags) {
-               if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
+               /*
+                * We *must* use zap_normflags because this normalization is
+                * what the hash is computed from.
+                */
+               if (zap_normalize(zap, key, zn->zn_normbuf,
+                   zap->zap_normflags) != 0) {
                        zap_name_free(zn);
                        return (NULL);
                }
                zn->zn_key_norm = zn->zn_normbuf;
                zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
        } else {
-               if (mt != MT_EXACT) {
+               if (mt != 0) {
                        zap_name_free(zn);
                        return (NULL);
                }
@@ -201,6 +213,20 @@ zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
        }
 
        zn->zn_hash = zap_hash(zn);
+
+       if (zap->zap_normflags != zn->zn_normflags) {
+               /*
+                * We *must* use zn_normflags because this normalization is
+                * what the matching is based on.  (Not the hash!)
+                */
+               if (zap_normalize(zap, key, zn->zn_normbuf,
+                   zn->zn_normflags) != 0) {
+                       zap_name_free(zn);
+                       return (NULL);
+               }
+               zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
+       }
+
        return (zn);
 }
 
@@ -214,7 +240,7 @@ zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
        zn->zn_key_intlen = sizeof (*key);
        zn->zn_key_orig = zn->zn_key_norm = key;
        zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
-       zn->zn_matchtype = MT_EXACT;
+       zn->zn_matchtype = 0;
 
        zn->zn_hash = zap_hash(zn);
        return (zn);
@@ -223,12 +249,11 @@ zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
 static void
 mzap_byteswap(mzap_phys_t *buf, size_t size)
 {
-       int i, max;
        buf->mz_block_type = BSWAP_64(buf->mz_block_type);
        buf->mz_salt = BSWAP_64(buf->mz_salt);
        buf->mz_normflags = BSWAP_64(buf->mz_normflags);
-       max = (size / MZAP_ENT_LEN) - 1;
-       for (i = 0; i < max; i++) {
+       int max = (size / MZAP_ENT_LEN) - 1;
+       for (int i = 0; i < max; i++) {
                buf->mz_chunk[i].mze_value =
                    BSWAP_64(buf->mz_chunk[i].mze_value);
                buf->mz_chunk[i].mze_cd =
@@ -239,9 +264,7 @@ mzap_byteswap(mzap_phys_t *buf, size_t size)
 void
 zap_byteswap(void *buf, size_t size)
 {
-       uint64_t block_type;
-
-       block_type = *(uint64_t *)buf;
+       uint64_t block_type = *(uint64_t *)buf;
 
        if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
                /* ASSERT(magic == ZAP_LEAF_MAGIC); */
@@ -267,12 +290,10 @@ mze_compare(const void *arg1, const void *arg2)
 static void
 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
 {
-       mzap_ent_t *mze;
-
        ASSERT(zap->zap_ismicro);
        ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 
-       mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
+       mzap_ent_t *mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
        mze->mze_chunkid = chunkid;
        mze->mze_hash = hash;
        mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
@@ -294,7 +315,6 @@ mze_find(zap_name_t *zn)
        mze_tofind.mze_hash = zn->zn_hash;
        mze_tofind.mze_cd = 0;
 
-again:
        mze = avl_find(avl, &mze_tofind, &idx);
        if (mze == NULL)
                mze = avl_nearest(avl, idx, AVL_AFTER);
@@ -303,10 +323,7 @@ again:
                if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
                        return (mze);
        }
-       if (zn->zn_matchtype == MT_BEST) {
-               zn->zn_matchtype = MT_FIRST;
-               goto again;
-       }
+
        return (NULL);
 }
 
@@ -314,10 +331,8 @@ static uint32_t
 mze_find_unused_cd(zap_t *zap, uint64_t hash)
 {
        mzap_ent_t mze_tofind;
-       mzap_ent_t *mze;
        avl_index_t idx;
        avl_tree_t *avl = &zap->zap_m.zap_avl;
-       uint32_t cd;
 
        ASSERT(zap->zap_ismicro);
        ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
@@ -325,8 +340,8 @@ mze_find_unused_cd(zap_t *zap, uint64_t hash)
        mze_tofind.mze_hash = hash;
        mze_tofind.mze_cd = 0;
 
-       cd = 0;
-       for (mze = avl_find(avl, &mze_tofind, &idx);
+       uint32_t cd = 0;
+       for (mzap_ent_t *mze = avl_find(avl, &mze_tofind, &idx);
            mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
                if (mze->mze_cd != cd)
                        break;
@@ -336,6 +351,41 @@ mze_find_unused_cd(zap_t *zap, uint64_t hash)
        return (cd);
 }
 
+/*
+ * Each mzap entry requires at max : 4 chunks
+ * 3 chunks for names + 1 chunk for value.
+ */
+#define        MZAP_ENT_CHUNKS (1 + ZAP_LEAF_ARRAY_NCHUNKS(MZAP_NAME_LEN) + \
+       ZAP_LEAF_ARRAY_NCHUNKS(sizeof (uint64_t)))
+
+/*
+ * Check if the current entry keeps the colliding entries under the fatzap leaf
+ * size.
+ */
+static boolean_t
+mze_canfit_fzap_leaf(zap_name_t *zn, uint64_t hash)
+{
+       zap_t *zap = zn->zn_zap;
+       mzap_ent_t mze_tofind;
+       mzap_ent_t *mze;
+       avl_index_t idx;
+       avl_tree_t *avl = &zap->zap_m.zap_avl;
+       uint32_t mzap_ents = 0;
+
+       mze_tofind.mze_hash = hash;
+       mze_tofind.mze_cd = 0;
+
+       for (mze = avl_find(avl, &mze_tofind, &idx);
+           mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
+               mzap_ents++;
+       }
+
+       /* Include the new entry being added */
+       mzap_ents++;
+
+       return (ZAP_LEAF_NUMCHUNKS_DEF > (mzap_ents * MZAP_ENT_CHUNKS));
+}
+
 static void
 mze_remove(zap_t *zap, mzap_ent_t *mze)
 {
@@ -361,15 +411,13 @@ static zap_t *
 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
 {
        zap_t *winner;
-       zap_t *zap;
-       int i;
        uint64_t *zap_hdr = (uint64_t *)db->db_data;
        uint64_t zap_block_type = zap_hdr[0];
        uint64_t zap_magic = zap_hdr[1];
 
        ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
 
-       zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
+       zap_t *zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
        rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
        rw_enter(&zap->zap_rwlock, RW_WRITER);
        zap->zap_objset = os;
@@ -377,7 +425,8 @@ mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
        zap->zap_dbuf = db;
 
        if (zap_block_type != ZBT_MICRO) {
-               mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
+               mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT,
+                   0);
                zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
                if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
                        winner = NULL;  /* No actual winner here... */
@@ -392,7 +441,7 @@ mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
         * it, because zap_lockdir() checks zap_ismicro without the lock
         * held.
         */
-       dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
+       dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf);
        winner = dmu_buf_set_user(db, &zap->zap_dbu);
 
        if (winner != NULL)
@@ -405,15 +454,14 @@ mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
                avl_create(&zap->zap_m.zap_avl, mze_compare,
                    sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
 
-               for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
+               for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) {
                        mzap_ent_phys_t *mze =
                            &zap_m_phys(zap)->mz_chunk[i];
                        if (mze->mze_name[0]) {
                                zap_name_t *zn;
 
                                zap->zap_m.zap_num_entries++;
-                               zn = zap_name_alloc(zap, mze->mze_name,
-                                   MT_EXACT);
+                               zn = zap_name_alloc(zap, mze->mze_name, 0);
                                mze_insert(zap, i, zn->zn_hash);
                                zap_name_free(zn);
                        }
@@ -453,25 +501,26 @@ handle_winner:
        return (winner);
 }
 
+/*
+ * This routine "consumes" the caller's hold on the dbuf, which must
+ * have the specified tag.
+ */
 static int
 zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
 {
-       dmu_object_info_t doi;
-       zap_t *zap;
-       krw_t lt;
-
+       ASSERT0(db->db_offset);
        objset_t *os = dmu_buf_get_objset(db);
        uint64_t obj = db->db_object;
+       dmu_object_info_t doi;
 
-       ASSERT0(db->db_offset);
        *zapp = NULL;
 
        dmu_object_info_from_db(db, &doi);
        if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
                return (SET_ERROR(EINVAL));
 
-       zap = dmu_buf_get_user(db);
+       zap_t *zap = dmu_buf_get_user(db);
        if (zap == NULL) {
                zap = mzap_open(os, obj, db);
                if (zap == NULL) {
@@ -490,7 +539,7 @@ zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
         * can only be different if it was upgraded from micro to fat,
         * and micro wanted WRITER but fat only needs READER.
         */
-       lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
+       krw_t lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
        rw_enter(&zap->zap_rwlock, lt);
        if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
                /* it was upgraded, now we only need reader */
@@ -514,11 +563,10 @@ zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
            zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
                uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
                if (newsz > MZAP_MAX_BLKSZ) {
-                       int err;
                        dprintf("upgrading obj %llu: num_entries=%u\n",
                            obj, zap->zap_m.zap_num_entries);
                        *zapp = zap;
-                       err = mzap_upgrade(zapp, tag, tx, 0);
+                       int err = mzap_upgrade(zapp, tag, tx, 0);
                        if (err != 0)
                                rw_exit(&zap->zap_rwlock);
                        return (err);
@@ -537,12 +585,19 @@ zap_lockdir_by_dnode(dnode_t *dn, dmu_tx_t *tx,
     krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
 {
        dmu_buf_t *db;
-       int err;
 
-       err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH);
+       int err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH);
        if (err != 0) {
                return (err);
        }
+#ifdef ZFS_DEBUG
+       {
+               dmu_object_info_t doi;
+               dmu_object_info_from_db(db, &doi);
+               ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
+       }
+#endif
+
        err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
        if (err != 0) {
                dmu_buf_rele(db, tag);
@@ -555,11 +610,17 @@ zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
     krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
 {
        dmu_buf_t *db;
-       int err;
 
-       err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH);
+       int err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH);
        if (err != 0)
                return (err);
+#ifdef ZFS_DEBUG
+       {
+               dmu_object_info_t doi;
+               dmu_object_info_from_db(db, &doi);
+               ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
+       }
+#endif
        err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
        if (err != 0)
                dmu_buf_rele(db, tag);
@@ -576,22 +637,20 @@ zap_unlockdir(zap_t *zap, void *tag)
 static int
 mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
 {
-       mzap_phys_t *mzp;
-       int i, sz, nchunks;
        int err = 0;
        zap_t *zap = *zapp;
 
        ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 
-       sz = zap->zap_dbuf->db_size;
-       mzp = vmem_alloc(sz, KM_SLEEP);
+       int sz = zap->zap_dbuf->db_size;
+       mzap_phys_t *mzp = vmem_alloc(sz, KM_SLEEP);
        bcopy(zap->zap_dbuf->db_data, mzp, sz);
-       nchunks = zap->zap_m.zap_num_chunks;
+       int nchunks = zap->zap_m.zap_num_chunks;
 
        if (!flags) {
                err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
                    1ULL << fzap_default_block_shift, 0, tx);
-               if (err) {
+               if (err != 0) {
                        vmem_free(mzp, sz);
                        return (err);
                }
@@ -604,58 +663,92 @@ mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
 
        fzap_upgrade(zap, tx, flags);
 
-       for (i = 0; i < nchunks; i++) {
+       for (int i = 0; i < nchunks; i++) {
                mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
-               zap_name_t *zn;
                if (mze->mze_name[0] == 0)
                        continue;
                dprintf("adding %s=%llu\n",
                    mze->mze_name, mze->mze_value);
-               zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
-               err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
-                   tag, tx);
+               zap_name_t *zn = zap_name_alloc(zap, mze->mze_name, 0);
+               /* If we fail here, we would end up losing entries */
+               VERIFY0(fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
+                   tag, tx));
                zap = zn->zn_zap;       /* fzap_add_cd() may change zap */
                zap_name_free(zn);
-               if (err)
-                       break;
        }
        vmem_free(mzp, sz);
        *zapp = zap;
-       return (err);
+       return (0);
 }
 
+/*
+ * The "normflags" determine the behavior of the matchtype_t which is
+ * passed to zap_lookup_norm().  Names which have the same normalized
+ * version will be stored with the same hash value, and therefore we can
+ * perform normalization-insensitive lookups.  We can be Unicode form-
+ * insensitive and/or case-insensitive.  The following flags are valid for
+ * "normflags":
+ *
+ * U8_TEXTPREP_NFC
+ * U8_TEXTPREP_NFD
+ * U8_TEXTPREP_NFKC
+ * U8_TEXTPREP_NFKD
+ * U8_TEXTPREP_TOUPPER
+ *
+ * The *_NF* (Normalization Form) flags are mutually exclusive; at most one
+ * of them may be supplied.
+ */
 void
-mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
-    dmu_tx_t *tx)
+mzap_create_impl(dnode_t *dn, int normflags, zap_flags_t flags, dmu_tx_t *tx)
 {
        dmu_buf_t *db;
-       mzap_phys_t *zp;
 
-       VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
-
-#ifdef ZFS_DEBUG
-       {
-               dmu_object_info_t doi;
-               dmu_object_info_from_db(db, &doi);
-               ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
-       }
-#endif
+       VERIFY0(dmu_buf_hold_by_dnode(dn, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
 
        dmu_buf_will_dirty(db, tx);
-       zp = db->db_data;
+       mzap_phys_t *zp = db->db_data;
        zp->mz_block_type = ZBT_MICRO;
-       zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
+       zp->mz_salt =
+           ((uintptr_t)db ^ (uintptr_t)tx ^ (dn->dn_object << 1)) | 1ULL;
        zp->mz_normflags = normflags;
-       dmu_buf_rele(db, FTAG);
 
        if (flags != 0) {
                zap_t *zap;
                /* Only fat zap supports flags; upgrade immediately. */
-               VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
-                   B_FALSE, B_FALSE, FTAG, &zap));
-               VERIFY3U(0, ==, mzap_upgrade(&zap, FTAG, tx, flags));
+               VERIFY0(zap_lockdir_impl(db, FTAG, tx, RW_WRITER,
+                   B_FALSE, B_FALSE, &zap));
+               VERIFY0(mzap_upgrade(&zap, FTAG, tx, flags));
                zap_unlockdir(zap, FTAG);
+       } else {
+               dmu_buf_rele(db, FTAG);
+       }
+}
+
+static uint64_t
+zap_create_impl(objset_t *os, int normflags, zap_flags_t flags,
+    dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
+    dmu_object_type_t bonustype, int bonuslen, int dnodesize,
+    dnode_t **allocated_dnode, void *tag, dmu_tx_t *tx)
+{
+       uint64_t obj;
+
+       ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP);
+
+       if (allocated_dnode == NULL) {
+               dnode_t *dn;
+               obj = dmu_object_alloc_hold(os, ot, 1ULL << leaf_blockshift,
+                   indirect_blockshift, bonustype, bonuslen, dnodesize,
+                   &dn, FTAG, tx);
+               mzap_create_impl(dn, normflags, flags, tx);
+               dnode_rele(dn, FTAG);
+       } else {
+               obj = dmu_object_alloc_hold(os, ot, 1ULL << leaf_blockshift,
+                   indirect_blockshift, bonustype, bonuslen, dnodesize,
+                   allocated_dnode, tag, tx);
+               mzap_create_impl(*allocated_dnode, normflags, flags, tx);
        }
+
+       return (obj);
 }
 
 int
@@ -688,13 +781,23 @@ zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
     dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
     int dnodesize, dmu_tx_t *tx)
 {
-       int err;
+       dnode_t *dn;
+       int error;
 
-       err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
+       ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP);
+       error = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
            dnodesize, tx);
-       if (err != 0)
-               return (err);
-       mzap_create_impl(os, obj, normflags, 0, tx);
+       if (error != 0)
+               return (error);
+
+       error = dnode_hold(os, obj, FTAG, &dn);
+       if (error != 0)
+               return (error);
+
+       mzap_create_impl(dn, normflags, 0, tx);
+
+       dnode_rele(dn, FTAG);
+
        return (0);
 }
 
@@ -725,11 +828,8 @@ uint64_t
 zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
 {
-       uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
-           dnodesize, tx);
-
-       mzap_create_impl(os, obj, normflags, 0, tx);
-       return (obj);
+       return (zap_create_impl(os, normflags, 0, ot, 0, 0,
+           bonustype, bonuslen, dnodesize, NULL, NULL, tx));
 }
 
 uint64_t
@@ -746,19 +846,25 @@ zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
 {
-       uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
-           dnodesize, tx);
-
-       ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
-           leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
-           indirect_blockshift >= SPA_MINBLOCKSHIFT &&
-           indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
-
-       VERIFY(dmu_object_set_blocksize(os, obj,
-           1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
+       return (zap_create_impl(os, normflags, flags, ot, leaf_blockshift,
+           indirect_blockshift, bonustype, bonuslen, dnodesize, NULL, NULL,
+           tx));
+}
 
-       mzap_create_impl(os, obj, normflags, flags, tx);
-       return (obj);
+/*
+ * Create a zap object and return a pointer to the newly allocated dnode via
+ * the allocated_dnode argument.  The returned dnode will be held and the
+ * caller is responsible for releasing the hold by calling dnode_rele().
+ */
+uint64_t
+zap_create_hold(objset_t *os, int normflags, zap_flags_t flags,
+    dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
+    dmu_object_type_t bonustype, int bonuslen, int dnodesize,
+    dnode_t **allocated_dnode, void *tag, dmu_tx_t *tx)
+{
+       return (zap_create_impl(os, normflags, flags, ot, leaf_blockshift,
+           indirect_blockshift, bonustype, bonuslen, dnodesize,
+           allocated_dnode, tag, tx));
 }
 
 int
@@ -774,7 +880,7 @@ zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
 }
 
 void
-zap_evict(void *dbu)
+zap_evict_sync(void *dbu)
 {
        zap_t *zap = dbu;
 
@@ -792,10 +898,10 @@ int
 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
 {
        zap_t *zap;
-       int err;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
        if (!zap->zap_ismicro) {
                err = fzap_count(zap, count);
@@ -813,7 +919,6 @@ zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
 static boolean_t
 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
 {
-       mzap_ent_t *other;
        int direction = AVL_BEFORE;
        boolean_t allocdzn = B_FALSE;
 
@@ -821,13 +926,13 @@ mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
                return (B_FALSE);
 
 again:
-       for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
+       for (mzap_ent_t *other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
            other && other->mze_hash == mze->mze_hash;
            other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
 
                if (zn == NULL) {
                        zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
-                           MT_FIRST);
+                           MT_NORMALIZE);
                        allocdzn = B_TRUE;
                }
                if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
@@ -856,7 +961,7 @@ zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
     uint64_t integer_size, uint64_t num_integers, void *buf)
 {
        return (zap_lookup_norm(os, zapobj, name, integer_size,
-           num_integers, buf, MT_EXACT, NULL, 0, NULL));
+           num_integers, buf, 0, NULL, 0, NULL));
 }
 
 static int
@@ -866,10 +971,8 @@ zap_lookup_impl(zap_t *zap, const char *name,
     boolean_t *ncp)
 {
        int err = 0;
-       mzap_ent_t *mze;
-       zap_name_t *zn;
 
-       zn = zap_name_alloc(zap, name, mt);
+       zap_name_t *zn = zap_name_alloc(zap, name, mt);
        if (zn == NULL)
                return (SET_ERROR(ENOTSUP));
 
@@ -877,7 +980,7 @@ zap_lookup_impl(zap_t *zap, const char *name,
                err = fzap_lookup(zn, integer_size, num_integers, buf,
                    realname, rn_len, ncp);
        } else {
-               mze = mze_find(zn);
+               mzap_ent_t *mze = mze_find(zn);
                if (mze == NULL) {
                        err = SET_ERROR(ENOENT);
                } else {
@@ -908,9 +1011,9 @@ zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
     boolean_t *ncp)
 {
        zap_t *zap;
-       int err;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
        if (err != 0)
                return (err);
        err = zap_lookup_impl(zap, name, integer_size,
@@ -929,7 +1032,7 @@ zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
        err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
        if (err)
                return (err);
-       zn = zap_name_alloc(zap, name, MT_EXACT);
+       zn = zap_name_alloc(zap, name, 0);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -946,7 +1049,7 @@ zap_lookup_by_dnode(dnode_t *dn, const char *name,
     uint64_t integer_size, uint64_t num_integers, void *buf)
 {
        return (zap_lookup_norm_by_dnode(dn, name, integer_size,
-           num_integers, buf, MT_EXACT, NULL, 0, NULL));
+           num_integers, buf, 0, NULL, 0, NULL));
 }
 
 int
@@ -956,9 +1059,8 @@ zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
     boolean_t *ncp)
 {
        zap_t *zap;
-       int err;
 
-       err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
+       int err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
            FTAG, &zap);
        if (err != 0)
                return (err);
@@ -973,13 +1075,12 @@ zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
     int key_numints)
 {
        zap_t *zap;
-       int err;
-       zap_name_t *zn;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -996,13 +1097,12 @@ zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
 {
        zap_t *zap;
-       int err;
-       zap_name_t *zn;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1019,7 +1119,7 @@ int
 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
 {
        int err = zap_lookup_norm(os, zapobj, name, 0,
-           0, NULL, MT_EXACT, NULL, 0, NULL);
+           0, NULL, 0, NULL, 0, NULL);
        if (err == EOVERFLOW || err == EINVAL)
                err = 0; /* found, but skipped reading the value */
        return (err);
@@ -1030,14 +1130,12 @@ zap_length(objset_t *os, uint64_t zapobj, const char *name,
     uint64_t *integer_size, uint64_t *num_integers)
 {
        zap_t *zap;
-       int err;
-       mzap_ent_t *mze;
-       zap_name_t *zn;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc(zap, name, MT_EXACT);
+       zap_name_t *zn = zap_name_alloc(zap, name, 0);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1045,7 +1143,7 @@ zap_length(objset_t *os, uint64_t zapobj, const char *name,
        if (!zap->zap_ismicro) {
                err = fzap_length(zn, integer_size, num_integers);
        } else {
-               mze = mze_find(zn);
+               mzap_ent_t *mze = mze_find(zn);
                if (mze == NULL) {
                        err = SET_ERROR(ENOENT);
                } else {
@@ -1065,13 +1163,12 @@ zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
 {
        zap_t *zap;
-       int err;
-       zap_name_t *zn;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1085,27 +1182,24 @@ zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
 static void
 mzap_addent(zap_name_t *zn, uint64_t value)
 {
-       int i;
        zap_t *zap = zn->zn_zap;
        int start = zap->zap_m.zap_alloc_next;
-       uint32_t cd;
 
        ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 
 #ifdef ZFS_DEBUG
-       for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
-               ASSERTV(mzap_ent_phys_t *mze);
-               ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
+       for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) {
+               mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
                ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
        }
 #endif
 
-       cd = mze_find_unused_cd(zap, zn->zn_hash);
+       uint32_t cd = mze_find_unused_cd(zap, zn->zn_hash);
        /* given the limited size of the microzap, this can't happen */
        ASSERT(cd < zap_maxcd(zap));
 
 again:
-       for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
+       for (int i = start; i < zap->zap_m.zap_num_chunks; i++) {
                mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
                if (mze->mze_name[0] == 0) {
                        mze->mze_value = value;
@@ -1128,39 +1222,33 @@ again:
        cmn_err(CE_PANIC, "out of entries!");
 }
 
-int
-zap_add(objset_t *os, uint64_t zapobj, const char *key,
+static int
+zap_add_impl(zap_t *zap, const char *key,
     int integer_size, uint64_t num_integers,
-    const void *val, dmu_tx_t *tx)
+    const void *val, dmu_tx_t *tx, void *tag)
 {
-       zap_t *zap;
-       int err;
-       mzap_ent_t *mze;
        const uint64_t *intval = val;
-       zap_name_t *zn;
+       int err = 0;
 
-       err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
-       if (err)
-               return (err);
-       zn = zap_name_alloc(zap, key, MT_EXACT);
+       zap_name_t *zn = zap_name_alloc(zap, key, 0);
        if (zn == NULL) {
-               zap_unlockdir(zap, FTAG);
+               zap_unlockdir(zap, tag);
                return (SET_ERROR(ENOTSUP));
        }
        if (!zap->zap_ismicro) {
-               err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
+               err = fzap_add(zn, integer_size, num_integers, val, tag, tx);
                zap = zn->zn_zap;       /* fzap_add() may change zap */
        } else if (integer_size != 8 || num_integers != 1 ||
-           strlen(key) >= MZAP_NAME_LEN) {
-               err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
+           strlen(key) >= MZAP_NAME_LEN ||
+           !mze_canfit_fzap_leaf(zn, zn->zn_hash)) {
+               err = mzap_upgrade(&zn->zn_zap, tag, tx, 0);
                if (err == 0) {
                        err = fzap_add(zn, integer_size, num_integers, val,
-                           FTAG, tx);
+                           tag, tx);
                }
                zap = zn->zn_zap;       /* fzap_add() may change zap */
        } else {
-               mze = mze_find(zn);
-               if (mze != NULL) {
+               if (mze_find(zn) != NULL) {
                        err = SET_ERROR(EEXIST);
                } else {
                        mzap_addent(zn, *intval);
@@ -1169,23 +1257,54 @@ zap_add(objset_t *os, uint64_t zapobj, const char *key,
        ASSERT(zap == zn->zn_zap);
        zap_name_free(zn);
        if (zap != NULL)        /* may be NULL if fzap_add() failed */
-               zap_unlockdir(zap, FTAG);
+               zap_unlockdir(zap, tag);
        return (err);
 }
 
 int
-zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
-    int key_numints, int integer_size, uint64_t num_integers,
+zap_add(objset_t *os, uint64_t zapobj, const char *key,
+    int integer_size, uint64_t num_integers,
     const void *val, dmu_tx_t *tx)
 {
        zap_t *zap;
        int err;
-       zap_name_t *zn;
 
        err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
-       if (err)
+       if (err != 0)
+               return (err);
+       err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
+       /* zap_add_impl() calls zap_unlockdir() */
+       return (err);
+}
+
+int
+zap_add_by_dnode(dnode_t *dn, const char *key,
+    int integer_size, uint64_t num_integers,
+    const void *val, dmu_tx_t *tx)
+{
+       zap_t *zap;
+       int err;
+
+       err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
+       if (err != 0)
+               return (err);
+       err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
+       /* zap_add_impl() calls zap_unlockdir() */
+       return (err);
+}
+
+int
+zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
+    int key_numints, int integer_size, uint64_t num_integers,
+    const void *val, dmu_tx_t *tx)
+{
+       zap_t *zap;
+
+       int err =
+           zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1203,26 +1322,13 @@ zap_update(objset_t *os, uint64_t zapobj, const char *name,
     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
 {
        zap_t *zap;
-       mzap_ent_t *mze;
        const uint64_t *intval = val;
-       zap_name_t *zn;
-       int err;
-
-#ifdef ZFS_DEBUG
-       uint64_t oldval;
-
-       /*
-        * If there is an old value, it shouldn't change across the
-        * lockdir (eg, due to bprewrite's xlation).
-        */
-       if (integer_size == 8 && num_integers == 1)
-               (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
-#endif
 
-       err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc(zap, name, MT_EXACT);
+       zap_name_t *zn = zap_name_alloc(zap, name, 0);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1242,9 +1348,8 @@ zap_update(objset_t *os, uint64_t zapobj, const char *name,
                }
                zap = zn->zn_zap;       /* fzap_update() may change zap */
        } else {
-               mze = mze_find(zn);
+               mzap_ent_t *mze = mze_find(zn);
                if (mze != NULL) {
-                       ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
                        MZE_PHYS(zap, mze)->mze_value = *intval;
                } else {
                        mzap_addent(zn, *intval);
@@ -1263,13 +1368,12 @@ zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
 {
        zap_t *zap;
-       zap_name_t *zn;
-       int err;
 
-       err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
+       if (err != 0)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1285,30 +1389,22 @@ zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
 int
 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
 {
-       return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
+       return (zap_remove_norm(os, zapobj, name, 0, tx));
 }
 
-int
-zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
+static int
+zap_remove_impl(zap_t *zap, const char *name,
     matchtype_t mt, dmu_tx_t *tx)
 {
-       zap_t *zap;
-       int err;
-       mzap_ent_t *mze;
-       zap_name_t *zn;
+       int err = 0;
 
-       err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
-       if (err)
-               return (err);
-       zn = zap_name_alloc(zap, name, mt);
-       if (zn == NULL) {
-               zap_unlockdir(zap, FTAG);
+       zap_name_t *zn = zap_name_alloc(zap, name, mt);
+       if (zn == NULL)
                return (SET_ERROR(ENOTSUP));
-       }
        if (!zap->zap_ismicro) {
                err = fzap_remove(zn, tx);
        } else {
-               mze = mze_find(zn);
+               mzap_ent_t *mze = mze_find(zn);
                if (mze == NULL) {
                        err = SET_ERROR(ENOENT);
                } else {
@@ -1319,22 +1415,49 @@ zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
                }
        }
        zap_name_free(zn);
-       zap_unlockdir(zap, FTAG);
        return (err);
 }
 
 int
-zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
-    int key_numints, dmu_tx_t *tx)
+zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
+    matchtype_t mt, dmu_tx_t *tx)
 {
        zap_t *zap;
        int err;
-       zap_name_t *zn;
 
        err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
        if (err)
                return (err);
-       zn = zap_name_alloc_uint64(zap, key, key_numints);
+       err = zap_remove_impl(zap, name, mt, tx);
+       zap_unlockdir(zap, FTAG);
+       return (err);
+}
+
+int
+zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx)
+{
+       zap_t *zap;
+       int err;
+
+       err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
+       if (err)
+               return (err);
+       err = zap_remove_impl(zap, name, 0, tx);
+       zap_unlockdir(zap, FTAG);
+       return (err);
+}
+
+int
+zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
+    int key_numints, dmu_tx_t *tx)
+{
+       zap_t *zap;
+
+       int err =
+           zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
+               return (err);
+       zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
        if (zn == NULL) {
                zap_unlockdir(zap, FTAG);
                return (SET_ERROR(ENOTSUP));
@@ -1410,9 +1533,6 @@ int
 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
 {
        int err;
-       avl_index_t idx;
-       mzap_ent_t mze_tofind;
-       mzap_ent_t *mze;
 
        if (zc->zc_hash == -1ULL)
                return (SET_ERROR(ENOENT));
@@ -1421,7 +1541,7 @@ zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
                int hb;
                err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
                    RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
-               if (err)
+               if (err != 0)
                        return (err);
 
                /*
@@ -1441,10 +1561,14 @@ zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
        if (!zc->zc_zap->zap_ismicro) {
                err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
        } else {
+               avl_index_t idx;
+               mzap_ent_t mze_tofind;
+
                mze_tofind.mze_hash = zc->zc_hash;
                mze_tofind.mze_cd = zc->zc_cd;
 
-               mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
+               mzap_ent_t *mze =
+                   avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
                if (mze == NULL) {
                        mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
                            idx, AVL_AFTER);
@@ -1481,11 +1605,11 @@ zap_cursor_advance(zap_cursor_t *zc)
 int
 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
 {
-       int err;
        zap_t *zap;
 
-       err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
-       if (err)
+       int err =
+           zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
+       if (err != 0)
                return (err);
 
        bzero(zs, sizeof (zap_stats_t));
@@ -1501,83 +1625,7 @@ zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
        return (0);
 }
 
-int
-zap_count_write_by_dnode(dnode_t *dn, const char *name, int add,
-    uint64_t *towrite, uint64_t *tooverwrite)
-{
-       zap_t *zap;
-       int err = 0;
-
-       /*
-        * Since, we don't have a name, we cannot figure out which blocks will
-        * be affected in this operation. So, account for the worst case :
-        * - 3 blocks overwritten: target leaf, ptrtbl block, header block
-        * - 4 new blocks written if adding:
-        *      - 2 blocks for possibly split leaves,
-        *      - 2 grown ptrtbl blocks
-        *
-        * This also accommodates the case where an add operation to a fairly
-        * large microzap results in a promotion to fatzap.
-        */
-       if (name == NULL) {
-               *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
-               return (err);
-       }
-
-       /*
-        * We lock the zap with adding == FALSE. Because, if we pass
-        * the actual value of add, it could trigger a mzap_upgrade().
-        * At present we are just evaluating the possibility of this operation
-        * and hence we do not want to trigger an upgrade.
-        */
-       err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
-           FTAG, &zap);
-       if (err != 0)
-               return (err);
-
-       if (!zap->zap_ismicro) {
-               zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
-               if (zn) {
-                       err = fzap_count_write(zn, add, towrite,
-                           tooverwrite);
-                       zap_name_free(zn);
-               } else {
-                       /*
-                        * We treat this case as similar to (name == NULL)
-                        */
-                       *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
-               }
-       } else {
-               /*
-                * We are here if (name != NULL) and this is a micro-zap.
-                * We account for the header block depending on whether it
-                * is freeable.
-                *
-                * Incase of an add-operation it is hard to find out
-                * if this add will promote this microzap to fatzap.
-                * Hence, we consider the worst case and account for the
-                * blocks assuming this microzap would be promoted to a
-                * fatzap.
-                *
-                * 1 block overwritten  : header block
-                * 4 new blocks written : 2 new split leaf, 2 grown
-                *                      ptrtbl blocks
-                */
-               if (dmu_buf_freeable(zap->zap_dbuf))
-                       *tooverwrite += MZAP_MAX_BLKSZ;
-               else
-                       *towrite += MZAP_MAX_BLKSZ;
-
-               if (add) {
-                       *towrite += 4 * MZAP_MAX_BLKSZ;
-               }
-       }
-
-       zap_unlockdir(zap, FTAG);
-       return (err);
-}
-
-#if defined(_KERNEL) && defined(HAVE_SPL)
+#if defined(_KERNEL)
 EXPORT_SYMBOL(zap_create);
 EXPORT_SYMBOL(zap_create_dnsize);
 EXPORT_SYMBOL(zap_create_norm);
@@ -1587,21 +1635,24 @@ EXPORT_SYMBOL(zap_create_flags_dnsize);
 EXPORT_SYMBOL(zap_create_claim);
 EXPORT_SYMBOL(zap_create_claim_norm);
 EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
+EXPORT_SYMBOL(zap_create_hold);
 EXPORT_SYMBOL(zap_destroy);
 EXPORT_SYMBOL(zap_lookup);
+EXPORT_SYMBOL(zap_lookup_by_dnode);
 EXPORT_SYMBOL(zap_lookup_norm);
 EXPORT_SYMBOL(zap_lookup_uint64);
 EXPORT_SYMBOL(zap_contains);
 EXPORT_SYMBOL(zap_prefetch);
 EXPORT_SYMBOL(zap_prefetch_uint64);
-EXPORT_SYMBOL(zap_count_write_by_dnode);
 EXPORT_SYMBOL(zap_add);
+EXPORT_SYMBOL(zap_add_by_dnode);
 EXPORT_SYMBOL(zap_add_uint64);
 EXPORT_SYMBOL(zap_update);
 EXPORT_SYMBOL(zap_update_uint64);
 EXPORT_SYMBOL(zap_length);
 EXPORT_SYMBOL(zap_length_uint64);
 EXPORT_SYMBOL(zap_remove);
+EXPORT_SYMBOL(zap_remove_by_dnode);
 EXPORT_SYMBOL(zap_remove_norm);
 EXPORT_SYMBOL(zap_remove_uint64);
 EXPORT_SYMBOL(zap_count);