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