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