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