xref: /titanic_51/usr/src/uts/common/fs/zfs/zap_micro.c (revision 074bb90d80fdbeb2d04a8450a55ecbc96de28785)
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_length(objset_t *os, uint64_t zapobj, const char *name,
820     uint64_t *integer_size, uint64_t *num_integers)
821 {
822 	zap_t *zap;
823 	int err;
824 	mzap_ent_t *mze;
825 	zap_name_t *zn;
826 
827 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
828 	if (err)
829 		return (err);
830 	zn = zap_name_alloc(zap, name, MT_EXACT);
831 	if (zn == NULL) {
832 		zap_unlockdir(zap);
833 		return (ENOTSUP);
834 	}
835 	if (!zap->zap_ismicro) {
836 		err = fzap_length(zn, integer_size, num_integers);
837 	} else {
838 		mze = mze_find(zn);
839 		if (mze == NULL) {
840 			err = ENOENT;
841 		} else {
842 			if (integer_size)
843 				*integer_size = 8;
844 			if (num_integers)
845 				*num_integers = 1;
846 		}
847 	}
848 	zap_name_free(zn);
849 	zap_unlockdir(zap);
850 	return (err);
851 }
852 
853 int
854 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
855     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
856 {
857 	zap_t *zap;
858 	int err;
859 	zap_name_t *zn;
860 
861 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
862 	if (err)
863 		return (err);
864 	zn = zap_name_alloc_uint64(zap, key, key_numints);
865 	if (zn == NULL) {
866 		zap_unlockdir(zap);
867 		return (ENOTSUP);
868 	}
869 	err = fzap_length(zn, integer_size, num_integers);
870 	zap_name_free(zn);
871 	zap_unlockdir(zap);
872 	return (err);
873 }
874 
875 static void
876 mzap_addent(zap_name_t *zn, uint64_t value)
877 {
878 	int i;
879 	zap_t *zap = zn->zn_zap;
880 	int start = zap->zap_m.zap_alloc_next;
881 	uint32_t cd;
882 
883 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
884 
885 #ifdef ZFS_DEBUG
886 	for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
887 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
888 		ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
889 	}
890 #endif
891 
892 	cd = mze_find_unused_cd(zap, zn->zn_hash);
893 	/* given the limited size of the microzap, this can't happen */
894 	ASSERT(cd < zap_maxcd(zap));
895 
896 again:
897 	for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
898 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
899 		if (mze->mze_name[0] == 0) {
900 			mze->mze_value = value;
901 			mze->mze_cd = cd;
902 			(void) strcpy(mze->mze_name, zn->zn_key_orig);
903 			zap->zap_m.zap_num_entries++;
904 			zap->zap_m.zap_alloc_next = i+1;
905 			if (zap->zap_m.zap_alloc_next ==
906 			    zap->zap_m.zap_num_chunks)
907 				zap->zap_m.zap_alloc_next = 0;
908 			mze_insert(zap, i, zn->zn_hash, mze);
909 			return;
910 		}
911 	}
912 	if (start != 0) {
913 		start = 0;
914 		goto again;
915 	}
916 	ASSERT(!"out of entries!");
917 }
918 
919 int
920 zap_add(objset_t *os, uint64_t zapobj, const char *key,
921     int integer_size, uint64_t num_integers,
922     const void *val, dmu_tx_t *tx)
923 {
924 	zap_t *zap;
925 	int err;
926 	mzap_ent_t *mze;
927 	const uint64_t *intval = val;
928 	zap_name_t *zn;
929 
930 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
931 	if (err)
932 		return (err);
933 	zn = zap_name_alloc(zap, key, MT_EXACT);
934 	if (zn == NULL) {
935 		zap_unlockdir(zap);
936 		return (ENOTSUP);
937 	}
938 	if (!zap->zap_ismicro) {
939 		err = fzap_add(zn, integer_size, num_integers, val, tx);
940 		zap = zn->zn_zap;	/* fzap_add() may change zap */
941 	} else if (integer_size != 8 || num_integers != 1 ||
942 	    strlen(key) >= MZAP_NAME_LEN) {
943 		err = mzap_upgrade(&zn->zn_zap, tx, 0);
944 		if (err == 0)
945 			err = fzap_add(zn, integer_size, num_integers, val, tx);
946 		zap = zn->zn_zap;	/* fzap_add() may change zap */
947 	} else {
948 		mze = mze_find(zn);
949 		if (mze != NULL) {
950 			err = EEXIST;
951 		} else {
952 			mzap_addent(zn, *intval);
953 		}
954 	}
955 	ASSERT(zap == zn->zn_zap);
956 	zap_name_free(zn);
957 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
958 		zap_unlockdir(zap);
959 	return (err);
960 }
961 
962 int
963 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
964     int key_numints, int integer_size, uint64_t num_integers,
965     const void *val, dmu_tx_t *tx)
966 {
967 	zap_t *zap;
968 	int err;
969 	zap_name_t *zn;
970 
971 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
972 	if (err)
973 		return (err);
974 	zn = zap_name_alloc_uint64(zap, key, key_numints);
975 	if (zn == NULL) {
976 		zap_unlockdir(zap);
977 		return (ENOTSUP);
978 	}
979 	err = fzap_add(zn, integer_size, num_integers, val, tx);
980 	zap = zn->zn_zap;	/* fzap_add() may change zap */
981 	zap_name_free(zn);
982 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
983 		zap_unlockdir(zap);
984 	return (err);
985 }
986 
987 int
988 zap_update(objset_t *os, uint64_t zapobj, const char *name,
989     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
990 {
991 	zap_t *zap;
992 	mzap_ent_t *mze;
993 	const uint64_t *intval = val;
994 	zap_name_t *zn;
995 	int err;
996 
997 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
998 	if (err)
999 		return (err);
1000 	zn = zap_name_alloc(zap, name, MT_EXACT);
1001 	if (zn == NULL) {
1002 		zap_unlockdir(zap);
1003 		return (ENOTSUP);
1004 	}
1005 	if (!zap->zap_ismicro) {
1006 		err = fzap_update(zn, integer_size, num_integers, val, tx);
1007 		zap = zn->zn_zap;	/* fzap_update() may change zap */
1008 	} else if (integer_size != 8 || num_integers != 1 ||
1009 	    strlen(name) >= MZAP_NAME_LEN) {
1010 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1011 		    zapobj, integer_size, num_integers, name);
1012 		err = mzap_upgrade(&zn->zn_zap, tx, 0);
1013 		if (err == 0)
1014 			err = fzap_update(zn, integer_size, num_integers,
1015 			    val, tx);
1016 		zap = zn->zn_zap;	/* fzap_update() may change zap */
1017 	} else {
1018 		mze = mze_find(zn);
1019 		if (mze != NULL) {
1020 			mze->mze_phys.mze_value = *intval;
1021 			zap->zap_m.zap_phys->mz_chunk
1022 			    [mze->mze_chunkid].mze_value = *intval;
1023 		} else {
1024 			mzap_addent(zn, *intval);
1025 		}
1026 	}
1027 	ASSERT(zap == zn->zn_zap);
1028 	zap_name_free(zn);
1029 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
1030 		zap_unlockdir(zap);
1031 	return (err);
1032 }
1033 
1034 int
1035 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1036     int key_numints,
1037     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1038 {
1039 	zap_t *zap;
1040 	zap_name_t *zn;
1041 	int err;
1042 
1043 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1044 	if (err)
1045 		return (err);
1046 	zn = zap_name_alloc_uint64(zap, key, key_numints);
1047 	if (zn == NULL) {
1048 		zap_unlockdir(zap);
1049 		return (ENOTSUP);
1050 	}
1051 	err = fzap_update(zn, integer_size, num_integers, val, tx);
1052 	zap = zn->zn_zap;	/* fzap_update() may change zap */
1053 	zap_name_free(zn);
1054 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
1055 		zap_unlockdir(zap);
1056 	return (err);
1057 }
1058 
1059 int
1060 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1061 {
1062 	return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1063 }
1064 
1065 int
1066 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1067     matchtype_t mt, dmu_tx_t *tx)
1068 {
1069 	zap_t *zap;
1070 	int err;
1071 	mzap_ent_t *mze;
1072 	zap_name_t *zn;
1073 
1074 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1075 	if (err)
1076 		return (err);
1077 	zn = zap_name_alloc(zap, name, mt);
1078 	if (zn == NULL) {
1079 		zap_unlockdir(zap);
1080 		return (ENOTSUP);
1081 	}
1082 	if (!zap->zap_ismicro) {
1083 		err = fzap_remove(zn, tx);
1084 	} else {
1085 		mze = mze_find(zn);
1086 		if (mze == NULL) {
1087 			err = ENOENT;
1088 		} else {
1089 			zap->zap_m.zap_num_entries--;
1090 			bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
1091 			    sizeof (mzap_ent_phys_t));
1092 			mze_remove(zap, mze);
1093 		}
1094 	}
1095 	zap_name_free(zn);
1096 	zap_unlockdir(zap);
1097 	return (err);
1098 }
1099 
1100 int
1101 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1102     int key_numints, dmu_tx_t *tx)
1103 {
1104 	zap_t *zap;
1105 	int err;
1106 	zap_name_t *zn;
1107 
1108 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1109 	if (err)
1110 		return (err);
1111 	zn = zap_name_alloc_uint64(zap, key, key_numints);
1112 	if (zn == NULL) {
1113 		zap_unlockdir(zap);
1114 		return (ENOTSUP);
1115 	}
1116 	err = fzap_remove(zn, tx);
1117 	zap_name_free(zn);
1118 	zap_unlockdir(zap);
1119 	return (err);
1120 }
1121 
1122 /*
1123  * Routines for iterating over the attributes.
1124  */
1125 
1126 void
1127 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1128     uint64_t serialized)
1129 {
1130 	zc->zc_objset = os;
1131 	zc->zc_zap = NULL;
1132 	zc->zc_leaf = NULL;
1133 	zc->zc_zapobj = zapobj;
1134 	zc->zc_serialized = serialized;
1135 	zc->zc_hash = 0;
1136 	zc->zc_cd = 0;
1137 }
1138 
1139 void
1140 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1141 {
1142 	zap_cursor_init_serialized(zc, os, zapobj, 0);
1143 }
1144 
1145 void
1146 zap_cursor_fini(zap_cursor_t *zc)
1147 {
1148 	if (zc->zc_zap) {
1149 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1150 		zap_unlockdir(zc->zc_zap);
1151 		zc->zc_zap = NULL;
1152 	}
1153 	if (zc->zc_leaf) {
1154 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1155 		zap_put_leaf(zc->zc_leaf);
1156 		zc->zc_leaf = NULL;
1157 	}
1158 	zc->zc_objset = NULL;
1159 }
1160 
1161 uint64_t
1162 zap_cursor_serialize(zap_cursor_t *zc)
1163 {
1164 	if (zc->zc_hash == -1ULL)
1165 		return (-1ULL);
1166 	if (zc->zc_zap == NULL)
1167 		return (zc->zc_serialized);
1168 	ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1169 	ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1170 
1171 	/*
1172 	 * We want to keep the high 32 bits of the cursor zero if we can, so
1173 	 * that 32-bit programs can access this.  So usually use a small
1174 	 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1175 	 * of the cursor.
1176 	 *
1177 	 * [ collision differentiator | zap_hashbits()-bit hash value ]
1178 	 */
1179 	return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1180 	    ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1181 }
1182 
1183 int
1184 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1185 {
1186 	int err;
1187 	avl_index_t idx;
1188 	mzap_ent_t mze_tofind;
1189 	mzap_ent_t *mze;
1190 
1191 	if (zc->zc_hash == -1ULL)
1192 		return (ENOENT);
1193 
1194 	if (zc->zc_zap == NULL) {
1195 		int hb;
1196 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1197 		    RW_READER, TRUE, FALSE, &zc->zc_zap);
1198 		if (err)
1199 			return (err);
1200 
1201 		/*
1202 		 * To support zap_cursor_init_serialized, advance, retrieve,
1203 		 * we must add to the existing zc_cd, which may already
1204 		 * be 1 due to the zap_cursor_advance.
1205 		 */
1206 		ASSERT(zc->zc_hash == 0);
1207 		hb = zap_hashbits(zc->zc_zap);
1208 		zc->zc_hash = zc->zc_serialized << (64 - hb);
1209 		zc->zc_cd += zc->zc_serialized >> hb;
1210 		if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1211 			zc->zc_cd = 0;
1212 	} else {
1213 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1214 	}
1215 	if (!zc->zc_zap->zap_ismicro) {
1216 		err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1217 	} else {
1218 		err = ENOENT;
1219 
1220 		mze_tofind.mze_hash = zc->zc_hash;
1221 		mze_tofind.mze_phys.mze_cd = zc->zc_cd;
1222 
1223 		mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1224 		if (mze == NULL) {
1225 			mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1226 			    idx, AVL_AFTER);
1227 		}
1228 		if (mze) {
1229 			ASSERT(0 == bcmp(&mze->mze_phys,
1230 			    &zc->zc_zap->zap_m.zap_phys->mz_chunk
1231 			    [mze->mze_chunkid], sizeof (mze->mze_phys)));
1232 
1233 			za->za_normalization_conflict =
1234 			    mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1235 			za->za_integer_length = 8;
1236 			za->za_num_integers = 1;
1237 			za->za_first_integer = mze->mze_phys.mze_value;
1238 			(void) strcpy(za->za_name, mze->mze_phys.mze_name);
1239 			zc->zc_hash = mze->mze_hash;
1240 			zc->zc_cd = mze->mze_phys.mze_cd;
1241 			err = 0;
1242 		} else {
1243 			zc->zc_hash = -1ULL;
1244 		}
1245 	}
1246 	rw_exit(&zc->zc_zap->zap_rwlock);
1247 	return (err);
1248 }
1249 
1250 void
1251 zap_cursor_advance(zap_cursor_t *zc)
1252 {
1253 	if (zc->zc_hash == -1ULL)
1254 		return;
1255 	zc->zc_cd++;
1256 }
1257 
1258 int
1259 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1260 {
1261 	int err = 0;
1262 	mzap_ent_t *mze;
1263 	zap_name_t *zn;
1264 
1265 	if (zc->zc_zap == NULL) {
1266 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1267 		    RW_READER, TRUE, FALSE, &zc->zc_zap);
1268 		if (err)
1269 			return (err);
1270 	} else {
1271 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1272 	}
1273 
1274 	zn = zap_name_alloc(zc->zc_zap, name, mt);
1275 	if (zn == NULL) {
1276 		rw_exit(&zc->zc_zap->zap_rwlock);
1277 		return (ENOTSUP);
1278 	}
1279 
1280 	if (!zc->zc_zap->zap_ismicro) {
1281 		err = fzap_cursor_move_to_key(zc, zn);
1282 	} else {
1283 		mze = mze_find(zn);
1284 		if (mze == NULL) {
1285 			err = ENOENT;
1286 			goto out;
1287 		}
1288 		zc->zc_hash = mze->mze_hash;
1289 		zc->zc_cd = mze->mze_phys.mze_cd;
1290 	}
1291 
1292 out:
1293 	zap_name_free(zn);
1294 	rw_exit(&zc->zc_zap->zap_rwlock);
1295 	return (err);
1296 }
1297 
1298 int
1299 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1300 {
1301 	int err;
1302 	zap_t *zap;
1303 
1304 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1305 	if (err)
1306 		return (err);
1307 
1308 	bzero(zs, sizeof (zap_stats_t));
1309 
1310 	if (zap->zap_ismicro) {
1311 		zs->zs_blocksize = zap->zap_dbuf->db_size;
1312 		zs->zs_num_entries = zap->zap_m.zap_num_entries;
1313 		zs->zs_num_blocks = 1;
1314 	} else {
1315 		fzap_get_stats(zap, zs);
1316 	}
1317 	zap_unlockdir(zap);
1318 	return (0);
1319 }
1320 
1321 int
1322 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1323     uint64_t *towrite, uint64_t *tooverwrite)
1324 {
1325 	zap_t *zap;
1326 	int err = 0;
1327 
1328 
1329 	/*
1330 	 * Since, we don't have a name, we cannot figure out which blocks will
1331 	 * be affected in this operation. So, account for the worst case :
1332 	 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1333 	 * - 4 new blocks written if adding:
1334 	 * 	- 2 blocks for possibly split leaves,
1335 	 * 	- 2 grown ptrtbl blocks
1336 	 *
1337 	 * This also accomodates the case where an add operation to a fairly
1338 	 * large microzap results in a promotion to fatzap.
1339 	 */
1340 	if (name == NULL) {
1341 		*towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
1342 		return (err);
1343 	}
1344 
1345 	/*
1346 	 * We lock the zap with adding ==  FALSE. Because, if we pass
1347 	 * the actual value of add, it could trigger a mzap_upgrade().
1348 	 * At present we are just evaluating the possibility of this operation
1349 	 * and hence we donot want to trigger an upgrade.
1350 	 */
1351 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1352 	if (err)
1353 		return (err);
1354 
1355 	if (!zap->zap_ismicro) {
1356 		zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1357 		if (zn) {
1358 			err = fzap_count_write(zn, add, towrite,
1359 			    tooverwrite);
1360 			zap_name_free(zn);
1361 		} else {
1362 			/*
1363 			 * We treat this case as similar to (name == NULL)
1364 			 */
1365 			*towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
1366 		}
1367 	} else {
1368 		/*
1369 		 * We are here if (name != NULL) and this is a micro-zap.
1370 		 * We account for the header block depending on whether it
1371 		 * is freeable.
1372 		 *
1373 		 * Incase of an add-operation it is hard to find out
1374 		 * if this add will promote this microzap to fatzap.
1375 		 * Hence, we consider the worst case and account for the
1376 		 * blocks assuming this microzap would be promoted to a
1377 		 * fatzap.
1378 		 *
1379 		 * 1 block overwritten  : header block
1380 		 * 4 new blocks written : 2 new split leaf, 2 grown
1381 		 *			ptrtbl blocks
1382 		 */
1383 		if (dmu_buf_freeable(zap->zap_dbuf))
1384 			*tooverwrite += SPA_MAXBLOCKSIZE;
1385 		else
1386 			*towrite += SPA_MAXBLOCKSIZE;
1387 
1388 		if (add) {
1389 			*towrite += 4 * SPA_MAXBLOCKSIZE;
1390 		}
1391 	}
1392 
1393 	zap_unlockdir(zap);
1394 	return (err);
1395 }
1396