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