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