Home | History | Annotate | Download | only in zfs
      1 /*
      2  * CDDL HEADER START
      3  *
      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.
      7  *
      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.
     12  *
     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]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #include <sys/zio.h>
     27 #include <sys/spa.h>
     28 #include <sys/dmu.h>
     29 #include <sys/zfs_context.h>
     30 #include <sys/zap.h>
     31 #include <sys/refcount.h>
     32 #include <sys/zap_impl.h>
     33 #include <sys/zap_leaf.h>
     34 #include <sys/avl.h>
     35 
     36 #ifdef _KERNEL
     37 #include <sys/sunddi.h>
     38 #endif
     39 
     40 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags);
     41 
     42 uint64_t
     43 zap_getflags(zap_t *zap)
     44 {
     45 	if (zap->zap_ismicro)
     46 		return (0);
     47 	return (zap->zap_u.zap_fat.zap_phys->zap_flags);
     48 }
     49 
     50 int
     51 zap_hashbits(zap_t *zap)
     52 {
     53 	if (zap_getflags(zap) & ZAP_FLAG_HASH64)
     54 		return (48);
     55 	else
     56 		return (28);
     57 }
     58 
     59 uint32_t
     60 zap_maxcd(zap_t *zap)
     61 {
     62 	if (zap_getflags(zap) & ZAP_FLAG_HASH64)
     63 		return ((1<<16)-1);
     64 	else
     65 		return (-1U);
     66 }
     67 
     68 static uint64_t
     69 zap_hash(zap_name_t *zn)
     70 {
     71 	zap_t *zap = zn->zn_zap;
     72 	uint64_t h = 0;
     73 
     74 	if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
     75 		ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
     76 		h = *(uint64_t *)zn->zn_key_orig;
     77 	} else {
     78 		const uint8_t *cp = (const uint8_t *)zn->zn_key_norm;
     79 		int i, len;
     80 
     81 		h = zap->zap_salt;
     82 		ASSERT(h != 0);
     83 		ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
     84 		len = zn->zn_key_norm_len;
     85 
     86 		/*
     87 		 * Because we previously stored the terminating null on
     88 		 * disk, but didn't hash it, we need to continue to not
     89 		 * hash it.  (The zn_key_*_len includes the terminating
     90 		 * null for non-binary keys.)
     91 		 */
     92 		if (!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY))
     93 			len--;
     94 
     95 		for (i = 0; i < len; cp++, i++)
     96 			h = (h >> 8) ^ zfs_crc64_table[(h ^ *cp) & 0xFF];
     97 
     98 	}
     99 	/*
    100 	 * Don't use all 64 bits, since we need some in the cookie for
    101 	 * the collision differentiator.  We MUST use the high bits,
    102 	 * since those are the ones that we first pay attention to when
    103 	 * chosing the bucket.
    104 	 */
    105 	h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
    106 
    107 	return (h);
    108 }
    109 
    110 static int
    111 zap_normalize(zap_t *zap, const char *name, char *namenorm)
    112 {
    113 	size_t inlen, outlen;
    114 	int err;
    115 
    116 	ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
    117 
    118 	inlen = strlen(name) + 1;
    119 	outlen = ZAP_MAXNAMELEN;
    120 
    121 	err = 0;
    122 	(void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
    123 	    zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
    124 	    U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
    125 
    126 	return (err);
    127 }
    128 
    129 boolean_t
    130 zap_match(zap_name_t *zn, const char *matchname)
    131 {
    132 	ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
    133 
    134 	if (zn->zn_matchtype == MT_FIRST) {
    135 		char norm[ZAP_MAXNAMELEN];
    136 
    137 		if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
    138 			return (B_FALSE);
    139 
    140 		return (strcmp(zn->zn_key_norm, norm) == 0);
    141 	} else {
    142 		/* MT_BEST or MT_EXACT */
    143 		return (strcmp(zn->zn_key_orig, matchname) == 0);
    144 	}
    145 }
    146 
    147 void
    148 zap_name_free(zap_name_t *zn)
    149 {
    150 	kmem_free(zn, sizeof (zap_name_t));
    151 }
    152 
    153 zap_name_t *
    154 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
    155 {
    156 	zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
    157 
    158 	zn->zn_zap = zap;
    159 	zn->zn_key_intlen = sizeof (*key);
    160 	zn->zn_key_orig = key;
    161 	zn->zn_key_orig_len = strlen(zn->zn_key_orig) + 1;
    162 	zn->zn_matchtype = mt;
    163 	if (zap->zap_normflags) {
    164 		if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
    165 			zap_name_free(zn);
    166 			return (NULL);
    167 		}
    168 		zn->zn_key_norm = zn->zn_normbuf;
    169 		zn->zn_key_norm_len = strlen(zn->zn_key_norm) + 1;
    170 	} else {
    171 		if (mt != MT_EXACT) {
    172 			zap_name_free(zn);
    173 			return (NULL);
    174 		}
    175 		zn->zn_key_norm = zn->zn_key_orig;
    176 		zn->zn_key_norm_len = zn->zn_key_orig_len;
    177 	}
    178 
    179 	zn->zn_hash = zap_hash(zn);
    180 	return (zn);
    181 }
    182 
    183 zap_name_t *
    184 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
    185 {
    186 	zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
    187 
    188 	ASSERT(zap->zap_normflags == 0);
    189 	zn->zn_zap = zap;
    190 	zn->zn_key_intlen = sizeof (*key);
    191 	zn->zn_key_orig = zn->zn_key_norm = key;
    192 	zn->zn_key_orig_len = zn->zn_key_norm_len = numints;
    193 	zn->zn_matchtype = MT_EXACT;
    194 
    195 	zn->zn_hash = zap_hash(zn);
    196 	return (zn);
    197 }
    198 
    199 static void
    200 mzap_byteswap(mzap_phys_t *buf, size_t size)
    201 {
    202 	int i, max;
    203 	buf->mz_block_type = BSWAP_64(buf->mz_block_type);
    204 	buf->mz_salt = BSWAP_64(buf->mz_salt);
    205 	buf->mz_normflags = BSWAP_64(buf->mz_normflags);
    206 	max = (size / MZAP_ENT_LEN) - 1;
    207 	for (i = 0; i < max; i++) {
    208 		buf->mz_chunk[i].mze_value =
    209 		    BSWAP_64(buf->mz_chunk[i].mze_value);
    210 		buf->mz_chunk[i].mze_cd =
    211 		    BSWAP_32(buf->mz_chunk[i].mze_cd);
    212 	}
    213 }
    214 
    215 void
    216 zap_byteswap(void *buf, size_t size)
    217 {
    218 	uint64_t block_type;
    219 
    220 	block_type = *(uint64_t *)buf;
    221 
    222 	if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
    223 		/* ASSERT(magic == ZAP_LEAF_MAGIC); */
    224 		mzap_byteswap(buf, size);
    225 	} else {
    226 		fzap_byteswap(buf, size);
    227 	}
    228 }
    229 
    230 static int
    231 mze_compare(const void *arg1, const void *arg2)
    232 {
    233 	const mzap_ent_t *mze1 = arg1;
    234 	const mzap_ent_t *mze2 = arg2;
    235 
    236 	if (mze1->mze_hash > mze2->mze_hash)
    237 		return (+1);
    238 	if (mze1->mze_hash < mze2->mze_hash)
    239 		return (-1);
    240 	if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd)
    241 		return (+1);
    242 	if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd)
    243 		return (-1);
    244 	return (0);
    245 }
    246 
    247 static void
    248 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep)
    249 {
    250 	mzap_ent_t *mze;
    251 
    252 	ASSERT(zap->zap_ismicro);
    253 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
    254 	ASSERT(mzep->mze_cd < zap_maxcd(zap));
    255 
    256 	mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
    257 	mze->mze_chunkid = chunkid;
    258 	mze->mze_hash = hash;
    259 	mze->mze_phys = *mzep;
    260 	avl_add(&zap->zap_m.zap_avl, mze);
    261 }
    262 
    263 static mzap_ent_t *
    264 mze_find(zap_name_t *zn)
    265 {
    266 	mzap_ent_t mze_tofind;
    267 	mzap_ent_t *mze;
    268 	avl_index_t idx;
    269 	avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
    270 
    271 	ASSERT(zn->zn_zap->zap_ismicro);
    272 	ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
    273 
    274 	mze_tofind.mze_hash = zn->zn_hash;
    275 	mze_tofind.mze_phys.mze_cd = 0;
    276 
    277 again:
    278 	mze = avl_find(avl, &mze_tofind, &idx);
    279 	if (mze == NULL)
    280 		mze = avl_nearest(avl, idx, AVL_AFTER);
    281 	for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
    282 		if (zap_match(zn, mze->mze_phys.mze_name))
    283 			return (mze);
    284 	}
    285 	if (zn->zn_matchtype == MT_BEST) {
    286 		zn->zn_matchtype = MT_FIRST;
    287 		goto again;
    288 	}
    289 	return (NULL);
    290 }
    291 
    292 static uint32_t
    293 mze_find_unused_cd(zap_t *zap, uint64_t hash)
    294 {
    295 	mzap_ent_t mze_tofind;
    296 	mzap_ent_t *mze;
    297 	avl_index_t idx;
    298 	avl_tree_t *avl = &zap->zap_m.zap_avl;
    299 	uint32_t cd;
    300 
    301 	ASSERT(zap->zap_ismicro);
    302 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
    303 
    304 	mze_tofind.mze_hash = hash;
    305 	mze_tofind.mze_phys.mze_cd = 0;
    306 
    307 	cd = 0;
    308 	for (mze = avl_find(avl, &mze_tofind, &idx);
    309 	    mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
    310 		if (mze->mze_phys.mze_cd != cd)
    311 			break;
    312 		cd++;
    313 	}
    314 
    315 	return (cd);
    316 }
    317 
    318 static void
    319 mze_remove(zap_t *zap, mzap_ent_t *mze)
    320 {
    321 	ASSERT(zap->zap_ismicro);
    322 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
    323 
    324 	avl_remove(&zap->zap_m.zap_avl, mze);
    325 	kmem_free(mze, sizeof (mzap_ent_t));
    326 }
    327 
    328 static void
    329 mze_destroy(zap_t *zap)
    330 {
    331 	mzap_ent_t *mze;
    332 	void *avlcookie = NULL;
    333 
    334 	while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
    335 		kmem_free(mze, sizeof (mzap_ent_t));
    336 	avl_destroy(&zap->zap_m.zap_avl);
    337 }
    338 
    339 static zap_t *
    340 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
    341 {
    342 	zap_t *winner;
    343 	zap_t *zap;
    344 	int i;
    345 
    346 	ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
    347 
    348 	zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
    349 	rw_init(&zap->zap_rwlock, 0, 0, 0);
    350 	rw_enter(&zap->zap_rwlock, RW_WRITER);
    351 	zap->zap_objset = os;
    352 	zap->zap_object = obj;
    353 	zap->zap_dbuf = db;
    354 
    355 	if (*(uint64_t *)db->db_data != ZBT_MICRO) {
    356 		mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
    357 		zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
    358 	} else {
    359 		zap->zap_ismicro = TRUE;
    360 	}
    361 
    362 	/*
    363 	 * Make sure that zap_ismicro is set before we let others see
    364 	 * it, because zap_lockdir() checks zap_ismicro without the lock
    365 	 * held.
    366 	 */
    367 	winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
    368 
    369 	if (winner != NULL) {
    370 		rw_exit(&zap->zap_rwlock);
    371 		rw_destroy(&zap->zap_rwlock);
    372 		if (!zap->zap_ismicro)
    373 			mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
    374 		kmem_free(zap, sizeof (zap_t));
    375 		return (winner);
    376 	}
    377 
    378 	if (zap->zap_ismicro) {
    379 		zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
    380 		zap->zap_normflags = zap->zap_m.zap_phys->mz_normflags;
    381 		zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
    382 		avl_create(&zap->zap_m.zap_avl, mze_compare,
    383 		    sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
    384 
    385 		for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
    386 			mzap_ent_phys_t *mze =
    387 			    &zap->zap_m.zap_phys->mz_chunk[i];
    388 			if (mze->mze_name[0]) {
    389 				zap_name_t *zn;
    390 
    391 				zap->zap_m.zap_num_entries++;
    392 				zn = zap_name_alloc(zap, mze->mze_name,
    393 				    MT_EXACT);
    394 				mze_insert(zap, i, zn->zn_hash, mze);
    395 				zap_name_free(zn);
    396 			}
    397 		}
    398 	} else {
    399 		zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
    400 		zap->zap_normflags = zap->zap_f.zap_phys->zap_normflags;
    401 
    402 		ASSERT3U(sizeof (struct zap_leaf_header), ==,
    403 		    2*ZAP_LEAF_CHUNKSIZE);
    404 
    405 		/*
    406 		 * The embedded pointer table should not overlap the
    407 		 * other members.
    408 		 */
    409 		ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
    410 		    &zap->zap_f.zap_phys->zap_salt);
    411 
    412 		/*
    413 		 * The embedded pointer table should end at the end of
    414 		 * the block
    415 		 */
    416 		ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
    417 		    1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
    418 		    (uintptr_t)zap->zap_f.zap_phys, ==,
    419 		    zap->zap_dbuf->db_size);
    420 	}
    421 	rw_exit(&zap->zap_rwlock);
    422 	return (zap);
    423 }
    424 
    425 int
    426 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
    427     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
    428 {
    429 	zap_t *zap;
    430 	dmu_buf_t *db;
    431 	krw_t lt;
    432 	int err;
    433 
    434 	*zapp = NULL;
    435 
    436 	err = dmu_buf_hold(os, obj, 0, NULL, &db);
    437 	if (err)
    438 		return (err);
    439 
    440 #ifdef ZFS_DEBUG
    441 	{
    442 		dmu_object_info_t doi;
    443 		dmu_object_info_from_db(db, &doi);
    444 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
    445 	}
    446 #endif
    447 
    448 	zap = dmu_buf_get_user(db);
    449 	if (zap == NULL)
    450 		zap = mzap_open(os, obj, db);
    451 
    452 	/*
    453 	 * We're checking zap_ismicro without the lock held, in order to
    454 	 * tell what type of lock we want.  Once we have some sort of
    455 	 * lock, see if it really is the right type.  In practice this
    456 	 * can only be different if it was upgraded from micro to fat,
    457 	 * and micro wanted WRITER but fat only needs READER.
    458 	 */
    459 	lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
    460 	rw_enter(&zap->zap_rwlock, lt);
    461 	if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
    462 		/* it was upgraded, now we only need reader */
    463 		ASSERT(lt == RW_WRITER);
    464 		ASSERT(RW_READER ==
    465 		    (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
    466 		rw_downgrade(&zap->zap_rwlock);
    467 		lt = RW_READER;
    468 	}
    469 
    470 	zap->zap_objset = os;
    471 
    472 	if (lt == RW_WRITER)
    473 		dmu_buf_will_dirty(db, tx);
    474 
    475 	ASSERT3P(zap->zap_dbuf, ==, db);
    476 
    477 	ASSERT(!zap->zap_ismicro ||
    478 	    zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
    479 	if (zap->zap_ismicro && tx && adding &&
    480 	    zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
    481 		uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
    482 		if (newsz > MZAP_MAX_BLKSZ) {
    483 			dprintf("upgrading obj %llu: num_entries=%u\n",
    484 			    obj, zap->zap_m.zap_num_entries);
    485 			*zapp = zap;
    486 			return (mzap_upgrade(zapp, tx, 0));
    487 		}
    488 		err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
    489 		ASSERT3U(err, ==, 0);
    490 		zap->zap_m.zap_num_chunks =
    491 		    db->db_size / MZAP_ENT_LEN - 1;
    492 	}
    493 
    494 	*zapp = zap;
    495 	return (0);
    496 }
    497 
    498 void
    499 zap_unlockdir(zap_t *zap)
    500 {
    501 	rw_exit(&zap->zap_rwlock);
    502 	dmu_buf_rele(zap->zap_dbuf, NULL);
    503 }
    504 
    505 static int
    506 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
    507 {
    508 	mzap_phys_t *mzp;
    509 	int i, sz, nchunks;
    510 	int err = 0;
    511 	zap_t *zap = *zapp;
    512 
    513 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
    514 
    515 	sz = zap->zap_dbuf->db_size;
    516 	mzp = kmem_alloc(sz, KM_SLEEP);
    517 	bcopy(zap->zap_dbuf->db_data, mzp, sz);
    518 	nchunks = zap->zap_m.zap_num_chunks;
    519 
    520 	if (!flags) {
    521 		err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
    522 		    1ULL << fzap_default_block_shift, 0, tx);
    523 		if (err) {
    524 			kmem_free(mzp, sz);
    525 			return (err);
    526 		}
    527 	}
    528 
    529 	dprintf("upgrading obj=%llu with %u chunks\n",
    530 	    zap->zap_object, nchunks);
    531 	/* XXX destroy the avl later, so we can use the stored hash value */
    532 	mze_destroy(zap);
    533 
    534 	fzap_upgrade(zap, tx, flags);
    535 
    536 	for (i = 0; i < nchunks; i++) {
    537 		mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
    538 		zap_name_t *zn;
    539 		if (mze->mze_name[0] == 0)
    540 			continue;
    541 		dprintf("adding %s=%llu\n",
    542 		    mze->mze_name, mze->mze_value);
    543 		zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
    544 		err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
    545 		zap = zn->zn_zap;	/* fzap_add_cd() may change zap */
    546 		zap_name_free(zn);
    547 		if (err)
    548 			break;
    549 	}
    550 	kmem_free(mzp, sz);
    551 	*zapp = zap;
    552 	return (err);
    553 }
    554 
    555 static void
    556 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
    557     dmu_tx_t *tx)
    558 {
    559 	dmu_buf_t *db;
    560 	mzap_phys_t *zp;
    561 
    562 	VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
    563 
    564 #ifdef ZFS_DEBUG
    565 	{
    566 		dmu_object_info_t doi;
    567 		dmu_object_info_from_db(db, &doi);
    568 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
    569 	}
    570 #endif
    571 
    572 	dmu_buf_will_dirty(db, tx);
    573 	zp = db->db_data;
    574 	zp->mz_block_type = ZBT_MICRO;
    575 	zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
    576 	zp->mz_normflags = normflags;
    577 	dmu_buf_rele(db, FTAG);
    578 
    579 	if (flags != 0) {
    580 		zap_t *zap;
    581 		/* Only fat zap supports flags; upgrade immediately. */
    582 		VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
    583 		    B_FALSE, B_FALSE, &zap));
    584 		VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
    585 		zap_unlockdir(zap);
    586 	}
    587 }
    588 
    589 int
    590 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
    591     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    592 {
    593 	return (zap_create_claim_norm(os, obj,
    594 	    0, ot, bonustype, bonuslen, tx));
    595 }
    596 
    597 int
    598 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
    599     dmu_object_type_t ot,
    600     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    601 {
    602 	int err;
    603 
    604 	err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
    605 	if (err != 0)
    606 		return (err);
    607 	mzap_create_impl(os, obj, normflags, 0, tx);
    608 	return (0);
    609 }
    610 
    611 uint64_t
    612 zap_create(objset_t *os, dmu_object_type_t ot,
    613     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    614 {
    615 	return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
    616 }
    617 
    618 uint64_t
    619 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
    620     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    621 {
    622 	uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
    623 
    624 	mzap_create_impl(os, obj, normflags, 0, tx);
    625 	return (obj);
    626 }
    627 
    628 uint64_t
    629 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
    630     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
    631     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
    632 {
    633 	uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
    634 
    635 	ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
    636 	    leaf_blockshift <= SPA_MAXBLOCKSHIFT &&
    637 	    indirect_blockshift >= SPA_MINBLOCKSHIFT &&
    638 	    indirect_blockshift <= SPA_MAXBLOCKSHIFT);
    639 
    640 	VERIFY(dmu_object_set_blocksize(os, obj,
    641 	    1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
    642 
    643 	mzap_create_impl(os, obj, normflags, flags, tx);
    644 	return (obj);
    645 }
    646 
    647 int
    648 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
    649 {
    650 	/*
    651 	 * dmu_object_free will free the object number and free the
    652 	 * data.  Freeing the data will cause our pageout function to be
    653 	 * called, which will destroy our data (zap_leaf_t's and zap_t).
    654 	 */
    655 
    656 	return (dmu_object_free(os, zapobj, tx));
    657 }
    658 
    659 _NOTE(ARGSUSED(0))
    660 void
    661 zap_evict(dmu_buf_t *db, void *vzap)
    662 {
    663 	zap_t *zap = vzap;
    664 
    665 	rw_destroy(&zap->zap_rwlock);
    666 
    667 	if (zap->zap_ismicro)
    668 		mze_destroy(zap);
    669 	else
    670 		mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
    671 
    672 	kmem_free(zap, sizeof (zap_t));
    673 }
    674 
    675 int
    676 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
    677 {
    678 	zap_t *zap;
    679 	int err;
    680 
    681 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
    682 	if (err)
    683 		return (err);
    684 	if (!zap->zap_ismicro) {
    685 		err = fzap_count(zap, count);
    686 	} else {
    687 		*count = zap->zap_m.zap_num_entries;
    688 	}
    689 	zap_unlockdir(zap);
    690 	return (err);
    691 }
    692 
    693 /*
    694  * zn may be NULL; if not specified, it will be computed if needed.
    695  * See also the comment above zap_entry_normalization_conflict().
    696  */
    697 static boolean_t
    698 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
    699 {
    700 	mzap_ent_t *other;
    701 	int direction = AVL_BEFORE;
    702 	boolean_t allocdzn = B_FALSE;
    703 
    704 	if (zap->zap_normflags == 0)
    705 		return (B_FALSE);
    706 
    707 again:
    708 	for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
    709 	    other && other->mze_hash == mze->mze_hash;
    710 	    other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
    711 
    712 		if (zn == NULL) {
    713 			zn = zap_name_alloc(zap, mze->mze_phys.mze_name,
    714 			    MT_FIRST);
    715 			allocdzn = B_TRUE;
    716 		}
    717 		if (zap_match(zn, other->mze_phys.mze_name)) {
    718 			if (allocdzn)
    719 				zap_name_free(zn);
    720 			return (B_TRUE);
    721 		}
    722 	}
    723 
    724 	if (direction == AVL_BEFORE) {
    725 		direction = AVL_AFTER;
    726 		goto again;
    727 	}
    728 
    729 	if (allocdzn)
    730 		zap_name_free(zn);
    731 	return (B_FALSE);
    732 }
    733 
    734 /*
    735  * Routines for manipulating attributes.
    736  */
    737 
    738 int
    739 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
    740     uint64_t integer_size, uint64_t num_integers, void *buf)
    741 {
    742 	return (zap_lookup_norm(os, zapobj, name, integer_size,
    743 	    num_integers, buf, MT_EXACT, NULL, 0, NULL));
    744 }
    745 
    746 int
    747 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
    748     uint64_t integer_size, uint64_t num_integers, void *buf,
    749     matchtype_t mt, char *realname, int rn_len,
    750     boolean_t *ncp)
    751 {
    752 	zap_t *zap;
    753 	int err;
    754 	mzap_ent_t *mze;
    755 	zap_name_t *zn;
    756 
    757 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
    758 	if (err)
    759 		return (err);
    760 	zn = zap_name_alloc(zap, name, mt);
    761 	if (zn == NULL) {
    762 		zap_unlockdir(zap);
    763 		return (ENOTSUP);
    764 	}
    765 
    766 	if (!zap->zap_ismicro) {
    767 		err = fzap_lookup(zn, integer_size, num_integers, buf,
    768 		    realname, rn_len, ncp);
    769 	} else {
    770 		mze = mze_find(zn);
    771 		if (mze == NULL) {
    772 			err = ENOENT;
    773 		} else {
    774 			if (num_integers < 1) {
    775 				err = EOVERFLOW;
    776 			} else if (integer_size != 8) {
    777 				err = EINVAL;
    778 			} else {
    779 				*(uint64_t *)buf = mze->mze_phys.mze_value;
    780 				(void) strlcpy(realname,
    781 				    mze->mze_phys.mze_name, rn_len);
    782 				if (ncp) {
    783 					*ncp = mzap_normalization_conflict(zap,
    784 					    zn, mze);
    785 				}
    786 			}
    787 		}
    788 	}
    789 	zap_name_free(zn);
    790 	zap_unlockdir(zap);
    791 	return (err);
    792 }
    793 
    794 int
    795 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
    796     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
    797 {
    798 	zap_t *zap;
    799 	int err;
    800 	zap_name_t *zn;
    801 
    802 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
    803 	if (err)
    804 		return (err);
    805 	zn = zap_name_alloc_uint64(zap, key, key_numints);
    806 	if (zn == NULL) {
    807 		zap_unlockdir(zap);
    808 		return (ENOTSUP);
    809 	}
    810 
    811 	err = fzap_lookup(zn, integer_size, num_integers, buf,
    812 	    NULL, 0, NULL);
    813 	zap_name_free(zn);
    814 	zap_unlockdir(zap);
    815 	return (err);
    816 }
    817 
    818 int
    819 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
    820 {
    821 	int err = (zap_lookup_norm(os, zapobj, name, 0,
    822 	    0, NULL, MT_EXACT, NULL, 0, NULL));
    823 	if (err == EOVERFLOW || err == EINVAL)
    824 		err = 0; /* found, but skipped reading the value */
    825 	return (err);
    826 }
    827 
    828 int
    829 zap_length(objset_t *os, uint64_t zapobj, const char *name,
    830     uint64_t *integer_size, uint64_t *num_integers)
    831 {
    832 	zap_t *zap;
    833 	int err;
    834 	mzap_ent_t *mze;
    835 	zap_name_t *zn;
    836 
    837 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
    838 	if (err)
    839 		return (err);
    840 	zn = zap_name_alloc(zap, name, MT_EXACT);
    841 	if (zn == NULL) {
    842 		zap_unlockdir(zap);
    843 		return (ENOTSUP);
    844 	}
    845 	if (!zap->zap_ismicro) {
    846 		err = fzap_length(zn, integer_size, num_integers);
    847 	} else {
    848 		mze = mze_find(zn);
    849 		if (mze == NULL) {
    850 			err = ENOENT;
    851 		} else {
    852 			if (integer_size)
    853 				*integer_size = 8;
    854 			if (num_integers)
    855 				*num_integers = 1;
    856 		}
    857 	}
    858 	zap_name_free(zn);
    859 	zap_unlockdir(zap);
    860 	return (err);
    861 }
    862 
    863 int
    864 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
    865     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
    866 {
    867 	zap_t *zap;
    868 	int err;
    869 	zap_name_t *zn;
    870 
    871 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
    872 	if (err)
    873 		return (err);
    874 	zn = zap_name_alloc_uint64(zap, key, key_numints);
    875 	if (zn == NULL) {
    876 		zap_unlockdir(zap);
    877 		return (ENOTSUP);
    878 	}
    879 	err = fzap_length(zn, integer_size, num_integers);
    880 	zap_name_free(zn);
    881 	zap_unlockdir(zap);
    882 	return (err);
    883 }
    884 
    885 static void
    886 mzap_addent(zap_name_t *zn, uint64_t value)
    887 {
    888 	int i;
    889 	zap_t *zap = zn->zn_zap;
    890 	int start = zap->zap_m.zap_alloc_next;
    891 	uint32_t cd;
    892 
    893 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
    894 
    895 #ifdef ZFS_DEBUG
    896 	for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
    897 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
    898 		ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
    899 	}
    900 #endif
    901 
    902 	cd = mze_find_unused_cd(zap, zn->zn_hash);
    903 	/* given the limited size of the microzap, this can't happen */
    904 	ASSERT(cd < zap_maxcd(zap));
    905 
    906 again:
    907 	for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
    908 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
    909 		if (mze->mze_name[0] == 0) {
    910 			mze->mze_value = value;
    911 			mze->mze_cd = cd;
    912 			(void) strcpy(mze->mze_name, zn->zn_key_orig);
    913 			zap->zap_m.zap_num_entries++;
    914 			zap->zap_m.zap_alloc_next = i+1;
    915 			if (zap->zap_m.zap_alloc_next ==
    916 			    zap->zap_m.zap_num_chunks)
    917 				zap->zap_m.zap_alloc_next = 0;
    918 			mze_insert(zap, i, zn->zn_hash, mze);
    919 			return;
    920 		}
    921 	}
    922 	if (start != 0) {
    923 		start = 0;
    924 		goto again;
    925 	}
    926 	ASSERT(!"out of entries!");
    927 }
    928 
    929 int
    930 zap_add(objset_t *os, uint64_t zapobj, const char *key,
    931     int integer_size, uint64_t num_integers,
    932     const void *val, dmu_tx_t *tx)
    933 {
    934 	zap_t *zap;
    935 	int err;
    936 	mzap_ent_t *mze;
    937 	const uint64_t *intval = val;
    938 	zap_name_t *zn;
    939 
    940 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
    941 	if (err)
    942 		return (err);
    943 	zn = zap_name_alloc(zap, key, MT_EXACT);
    944 	if (zn == NULL) {
    945 		zap_unlockdir(zap);
    946 		return (ENOTSUP);
    947 	}
    948 	if (!zap->zap_ismicro) {
    949 		err = fzap_add(zn, integer_size, num_integers, val, tx);
    950 		zap = zn->zn_zap;	/* fzap_add() may change zap */
    951 	} else if (integer_size != 8 || num_integers != 1 ||
    952 	    strlen(key) >= MZAP_NAME_LEN) {
    953 		err = mzap_upgrade(&zn->zn_zap, tx, 0);
    954 		if (err == 0)
    955 			err = fzap_add(zn, integer_size, num_integers, val, tx);
    956 		zap = zn->zn_zap;	/* fzap_add() may change zap */
    957 	} else {
    958 		mze = mze_find(zn);
    959 		if (mze != NULL) {
    960 			err = EEXIST;
    961 		} else {
    962 			mzap_addent(zn, *intval);
    963 		}
    964 	}
    965 	ASSERT(zap == zn->zn_zap);
    966 	zap_name_free(zn);
    967 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
    968 		zap_unlockdir(zap);
    969 	return (err);
    970 }
    971 
    972 int
    973 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
    974     int key_numints, int integer_size, uint64_t num_integers,
    975     const void *val, dmu_tx_t *tx)
    976 {
    977 	zap_t *zap;
    978 	int err;
    979 	zap_name_t *zn;
    980 
    981 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
    982 	if (err)
    983 		return (err);
    984 	zn = zap_name_alloc_uint64(zap, key, key_numints);
    985 	if (zn == NULL) {
    986 		zap_unlockdir(zap);
    987 		return (ENOTSUP);
    988 	}
    989 	err = fzap_add(zn, integer_size, num_integers, val, tx);
    990 	zap = zn->zn_zap;	/* fzap_add() may change zap */
    991 	zap_name_free(zn);
    992 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
    993 		zap_unlockdir(zap);
    994 	return (err);
    995 }
    996 
    997 int
    998 zap_update(objset_t *os, uint64_t zapobj, const char *name,
    999     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
   1000 {
   1001 	zap_t *zap;
   1002 	mzap_ent_t *mze;
   1003 	const uint64_t *intval = val;
   1004 	zap_name_t *zn;
   1005 	int err;
   1006 
   1007 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
   1008 	if (err)
   1009 		return (err);
   1010 	zn = zap_name_alloc(zap, name, MT_EXACT);
   1011 	if (zn == NULL) {
   1012 		zap_unlockdir(zap);
   1013 		return (ENOTSUP);
   1014 	}
   1015 	if (!zap->zap_ismicro) {
   1016 		err = fzap_update(zn, integer_size, num_integers, val, tx);
   1017 		zap = zn->zn_zap;	/* fzap_update() may change zap */
   1018 	} else if (integer_size != 8 || num_integers != 1 ||
   1019 	    strlen(name) >= MZAP_NAME_LEN) {
   1020 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
   1021 		    zapobj, integer_size, num_integers, name);
   1022 		err = mzap_upgrade(&zn->zn_zap, tx, 0);
   1023 		if (err == 0)
   1024 			err = fzap_update(zn, integer_size, num_integers,
   1025 			    val, tx);
   1026 		zap = zn->zn_zap;	/* fzap_update() may change zap */
   1027 	} else {
   1028 		mze = mze_find(zn);
   1029 		if (mze != NULL) {
   1030 			mze->mze_phys.mze_value = *intval;
   1031 			zap->zap_m.zap_phys->mz_chunk
   1032 			    [mze->mze_chunkid].mze_value = *intval;
   1033 		} else {
   1034 			mzap_addent(zn, *intval);
   1035 		}
   1036 	}
   1037 	ASSERT(zap == zn->zn_zap);
   1038 	zap_name_free(zn);
   1039 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
   1040 		zap_unlockdir(zap);
   1041 	return (err);
   1042 }
   1043 
   1044 int
   1045 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
   1046     int key_numints,
   1047     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
   1048 {
   1049 	zap_t *zap;
   1050 	zap_name_t *zn;
   1051 	int err;
   1052 
   1053 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
   1054 	if (err)
   1055 		return (err);
   1056 	zn = zap_name_alloc_uint64(zap, key, key_numints);
   1057 	if (zn == NULL) {
   1058 		zap_unlockdir(zap);
   1059 		return (ENOTSUP);
   1060 	}
   1061 	err = fzap_update(zn, integer_size, num_integers, val, tx);
   1062 	zap = zn->zn_zap;	/* fzap_update() may change zap */
   1063 	zap_name_free(zn);
   1064 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
   1065 		zap_unlockdir(zap);
   1066 	return (err);
   1067 }
   1068 
   1069 int
   1070 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
   1071 {
   1072 	return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
   1073 }
   1074 
   1075 int
   1076 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
   1077     matchtype_t mt, dmu_tx_t *tx)
   1078 {
   1079 	zap_t *zap;
   1080 	int err;
   1081 	mzap_ent_t *mze;
   1082 	zap_name_t *zn;
   1083 
   1084 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
   1085 	if (err)
   1086 		return (err);
   1087 	zn = zap_name_alloc(zap, name, mt);
   1088 	if (zn == NULL) {
   1089 		zap_unlockdir(zap);
   1090 		return (ENOTSUP);
   1091 	}
   1092 	if (!zap->zap_ismicro) {
   1093 		err = fzap_remove(zn, tx);
   1094 	} else {
   1095 		mze = mze_find(zn);
   1096 		if (mze == NULL) {
   1097 			err = ENOENT;
   1098 		} else {
   1099 			zap->zap_m.zap_num_entries--;
   1100 			bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
   1101 			    sizeof (mzap_ent_phys_t));
   1102 			mze_remove(zap, mze);
   1103 		}
   1104 	}
   1105 	zap_name_free(zn);
   1106 	zap_unlockdir(zap);
   1107 	return (err);
   1108 }
   1109 
   1110 int
   1111 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
   1112     int key_numints, dmu_tx_t *tx)
   1113 {
   1114 	zap_t *zap;
   1115 	int err;
   1116 	zap_name_t *zn;
   1117 
   1118 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
   1119 	if (err)
   1120 		return (err);
   1121 	zn = zap_name_alloc_uint64(zap, key, key_numints);
   1122 	if (zn == NULL) {
   1123 		zap_unlockdir(zap);
   1124 		return (ENOTSUP);
   1125 	}
   1126 	err = fzap_remove(zn, tx);
   1127 	zap_name_free(zn);
   1128 	zap_unlockdir(zap);
   1129 	return (err);
   1130 }
   1131 
   1132 /*
   1133  * Routines for iterating over the attributes.
   1134  */
   1135 
   1136 void
   1137 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
   1138     uint64_t serialized)
   1139 {
   1140 	zc->zc_objset = os;
   1141 	zc->zc_zap = NULL;
   1142 	zc->zc_leaf = NULL;
   1143 	zc->zc_zapobj = zapobj;
   1144 	zc->zc_serialized = serialized;
   1145 	zc->zc_hash = 0;
   1146 	zc->zc_cd = 0;
   1147 }
   1148 
   1149 void
   1150 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
   1151 {
   1152 	zap_cursor_init_serialized(zc, os, zapobj, 0);
   1153 }
   1154 
   1155 void
   1156 zap_cursor_fini(zap_cursor_t *zc)
   1157 {
   1158 	if (zc->zc_zap) {
   1159 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
   1160 		zap_unlockdir(zc->zc_zap);
   1161 		zc->zc_zap = NULL;
   1162 	}
   1163 	if (zc->zc_leaf) {
   1164 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
   1165 		zap_put_leaf(zc->zc_leaf);
   1166 		zc->zc_leaf = NULL;
   1167 	}
   1168 	zc->zc_objset = NULL;
   1169 }
   1170 
   1171 uint64_t
   1172 zap_cursor_serialize(zap_cursor_t *zc)
   1173 {
   1174 	if (zc->zc_hash == -1ULL)
   1175 		return (-1ULL);
   1176 	if (zc->zc_zap == NULL)
   1177 		return (zc->zc_serialized);
   1178 	ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
   1179 	ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
   1180 
   1181 	/*
   1182 	 * We want to keep the high 32 bits of the cursor zero if we can, so
   1183 	 * that 32-bit programs can access this.  So usually use a small
   1184 	 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
   1185 	 * of the cursor.
   1186 	 *
   1187 	 * [ collision differentiator | zap_hashbits()-bit hash value ]
   1188 	 */
   1189 	return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
   1190 	    ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
   1191 }
   1192 
   1193 int
   1194 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
   1195 {
   1196 	int err;
   1197 	avl_index_t idx;
   1198 	mzap_ent_t mze_tofind;
   1199 	mzap_ent_t *mze;
   1200 
   1201 	if (zc->zc_hash == -1ULL)
   1202 		return (ENOENT);
   1203 
   1204 	if (zc->zc_zap == NULL) {
   1205 		int hb;
   1206 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
   1207 		    RW_READER, TRUE, FALSE, &zc->zc_zap);
   1208 		if (err)
   1209 			return (err);
   1210 
   1211 		/*
   1212 		 * To support zap_cursor_init_serialized, advance, retrieve,
   1213 		 * we must add to the existing zc_cd, which may already
   1214 		 * be 1 due to the zap_cursor_advance.
   1215 		 */
   1216 		ASSERT(zc->zc_hash == 0);
   1217 		hb = zap_hashbits(zc->zc_zap);
   1218 		zc->zc_hash = zc->zc_serialized << (64 - hb);
   1219 		zc->zc_cd += zc->zc_serialized >> hb;
   1220 		if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
   1221 			zc->zc_cd = 0;
   1222 	} else {
   1223 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
   1224 	}
   1225 	if (!zc->zc_zap->zap_ismicro) {
   1226 		err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
   1227 	} else {
   1228 		err = ENOENT;
   1229 
   1230 		mze_tofind.mze_hash = zc->zc_hash;
   1231 		mze_tofind.mze_phys.mze_cd = zc->zc_cd;
   1232 
   1233 		mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
   1234 		if (mze == NULL) {
   1235 			mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
   1236 			    idx, AVL_AFTER);
   1237 		}
   1238 		if (mze) {
   1239 			ASSERT(0 == bcmp(&mze->mze_phys,
   1240 			    &zc->zc_zap->zap_m.zap_phys->mz_chunk
   1241 			    [mze->mze_chunkid], sizeof (mze->mze_phys)));
   1242 
   1243 			za->za_normalization_conflict =
   1244 			    mzap_normalization_conflict(zc->zc_zap, NULL, mze);
   1245 			za->za_integer_length = 8;
   1246 			za->za_num_integers = 1;
   1247 			za->za_first_integer = mze->mze_phys.mze_value;
   1248 			(void) strcpy(za->za_name, mze->mze_phys.mze_name);
   1249 			zc->zc_hash = mze->mze_hash;
   1250 			zc->zc_cd = mze->mze_phys.mze_cd;
   1251 			err = 0;
   1252 		} else {
   1253 			zc->zc_hash = -1ULL;
   1254 		}
   1255 	}
   1256 	rw_exit(&zc->zc_zap->zap_rwlock);
   1257 	return (err);
   1258 }
   1259 
   1260 void
   1261 zap_cursor_advance(zap_cursor_t *zc)
   1262 {
   1263 	if (zc->zc_hash == -1ULL)
   1264 		return;
   1265 	zc->zc_cd++;
   1266 }
   1267 
   1268 int
   1269 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
   1270 {
   1271 	int err = 0;
   1272 	mzap_ent_t *mze;
   1273 	zap_name_t *zn;
   1274 
   1275 	if (zc->zc_zap == NULL) {
   1276 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
   1277 		    RW_READER, TRUE, FALSE, &zc->zc_zap);
   1278 		if (err)
   1279 			return (err);
   1280 	} else {
   1281 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
   1282 	}
   1283 
   1284 	zn = zap_name_alloc(zc->zc_zap, name, mt);
   1285 	if (zn == NULL) {
   1286 		rw_exit(&zc->zc_zap->zap_rwlock);
   1287 		return (ENOTSUP);
   1288 	}
   1289 
   1290 	if (!zc->zc_zap->zap_ismicro) {
   1291 		err = fzap_cursor_move_to_key(zc, zn);
   1292 	} else {
   1293 		mze = mze_find(zn);
   1294 		if (mze == NULL) {
   1295 			err = ENOENT;
   1296 			goto out;
   1297 		}
   1298 		zc->zc_hash = mze->mze_hash;
   1299 		zc->zc_cd = mze->mze_phys.mze_cd;
   1300 	}
   1301 
   1302 out:
   1303 	zap_name_free(zn);
   1304 	rw_exit(&zc->zc_zap->zap_rwlock);
   1305 	return (err);
   1306 }
   1307 
   1308 int
   1309 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
   1310 {
   1311 	int err;
   1312 	zap_t *zap;
   1313 
   1314 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
   1315 	if (err)
   1316 		return (err);
   1317 
   1318 	bzero(zs, sizeof (zap_stats_t));
   1319 
   1320 	if (zap->zap_ismicro) {
   1321 		zs->zs_blocksize = zap->zap_dbuf->db_size;
   1322 		zs->zs_num_entries = zap->zap_m.zap_num_entries;
   1323 		zs->zs_num_blocks = 1;
   1324 	} else {
   1325 		fzap_get_stats(zap, zs);
   1326 	}
   1327 	zap_unlockdir(zap);
   1328 	return (0);
   1329 }
   1330 
   1331 int
   1332 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
   1333     uint64_t *towrite, uint64_t *tooverwrite)
   1334 {
   1335 	zap_t *zap;
   1336 	int err = 0;
   1337 
   1338 
   1339 	/*
   1340 	 * Since, we don't have a name, we cannot figure out which blocks will
   1341 	 * be affected in this operation. So, account for the worst case :
   1342 	 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
   1343 	 * - 4 new blocks written if adding:
   1344 	 * 	- 2 blocks for possibly split leaves,
   1345 	 * 	- 2 grown ptrtbl blocks
   1346 	 *
   1347 	 * This also accomodates the case where an add operation to a fairly
   1348 	 * large microzap results in a promotion to fatzap.
   1349 	 */
   1350 	if (name == NULL) {
   1351 		*towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
   1352 		return (err);
   1353 	}
   1354 
   1355 	/*
   1356 	 * We lock the zap with adding ==  FALSE. Because, if we pass
   1357 	 * the actual value of add, it could trigger a mzap_upgrade().
   1358 	 * At present we are just evaluating the possibility of this operation
   1359 	 * and hence we donot want to trigger an upgrade.
   1360 	 */
   1361 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
   1362 	if (err)
   1363 		return (err);
   1364 
   1365 	if (!zap->zap_ismicro) {
   1366 		zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
   1367 		if (zn) {
   1368 			err = fzap_count_write(zn, add, towrite,
   1369 			    tooverwrite);
   1370 			zap_name_free(zn);
   1371 		} else {
   1372 			/*
   1373 			 * We treat this case as similar to (name == NULL)
   1374 			 */
   1375 			*towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
   1376 		}
   1377 	} else {
   1378 		/*
   1379 		 * We are here if (name != NULL) and this is a micro-zap.
   1380 		 * We account for the header block depending on whether it
   1381 		 * is freeable.
   1382 		 *
   1383 		 * Incase of an add-operation it is hard to find out
   1384 		 * if this add will promote this microzap to fatzap.
   1385 		 * Hence, we consider the worst case and account for the
   1386 		 * blocks assuming this microzap would be promoted to a
   1387 		 * fatzap.
   1388 		 *
   1389 		 * 1 block overwritten  : header block
   1390 		 * 4 new blocks written : 2 new split leaf, 2 grown
   1391 		 *			ptrtbl blocks
   1392 		 */
   1393 		if (dmu_buf_freeable(zap->zap_dbuf))
   1394 			*tooverwrite += SPA_MAXBLOCKSIZE;
   1395 		else
   1396 			*towrite += SPA_MAXBLOCKSIZE;
   1397 
   1398 		if (add) {
   1399 			*towrite += 4 * SPA_MAXBLOCKSIZE;
   1400 		}
   1401 	}
   1402 
   1403 	zap_unlockdir(zap);
   1404 	return (err);
   1405 }
   1406