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 https://opensource.org/licenses/CDDL-1.0.
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26  * Copyright 2017 Nexenta Systems, Inc.
27  */
28 
29 #include <sys/zio.h>
30 #include <sys/spa.h>
31 #include <sys/dmu.h>
32 #include <sys/zfs_context.h>
33 #include <sys/zap.h>
34 #include <sys/zap_impl.h>
35 #include <sys/zap_leaf.h>
36 #include <sys/btree.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 int zap_micro_max_size = MZAP_MAX_BLKSZ;
45 
46 static int mzap_upgrade(zap_t **zapp,
47     const 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 			const uint64_t *wp = zn->zn_key_norm;
91 
92 			ASSERT(zn->zn_key_intlen == 8);
93 			for (int i = 0; i < zn->zn_key_norm_numints;
94 			    wp++, i++) {
95 				uint64_t word = *wp;
96 
97 				for (int j = 0; j < 8; j++) {
98 					h = (h >> 8) ^
99 					    zfs_crc64_table[(h ^ word) & 0xFF];
100 					word >>= NBBY;
101 				}
102 			}
103 		} else {
104 			const uint8_t *cp = zn->zn_key_norm;
105 
106 			/*
107 			 * We previously stored the terminating null on
108 			 * disk, but didn't hash it, so we need to
109 			 * continue to not hash it.  (The
110 			 * zn_key_*_numints includes the terminating
111 			 * null for non-binary keys.)
112 			 */
113 			int len = zn->zn_key_norm_numints - 1;
114 
115 			ASSERT(zn->zn_key_intlen == 1);
116 			for (int i = 0; i < len; cp++, i++) {
117 				h = (h >> 8) ^
118 				    zfs_crc64_table[(h ^ *cp) & 0xFF];
119 			}
120 		}
121 	}
122 	/*
123 	 * Don't use all 64 bits, since we need some in the cookie for
124 	 * the collision differentiator.  We MUST use the high bits,
125 	 * since those are the ones that we first pay attention to when
126 	 * choosing the bucket.
127 	 */
128 	h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
129 
130 	return (h);
131 }
132 
133 static int
134 zap_normalize(zap_t *zap, const char *name, char *namenorm, int normflags)
135 {
136 	ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
137 
138 	size_t inlen = strlen(name) + 1;
139 	size_t outlen = ZAP_MAXNAMELEN;
140 
141 	int err = 0;
142 	(void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
143 	    normflags | U8_TEXTPREP_IGNORE_NULL | U8_TEXTPREP_IGNORE_INVALID,
144 	    U8_UNICODE_LATEST, &err);
145 
146 	return (err);
147 }
148 
149 boolean_t
150 zap_match(zap_name_t *zn, const char *matchname)
151 {
152 	ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
153 
154 	if (zn->zn_matchtype & MT_NORMALIZE) {
155 		char norm[ZAP_MAXNAMELEN];
156 
157 		if (zap_normalize(zn->zn_zap, matchname, norm,
158 		    zn->zn_normflags) != 0)
159 			return (B_FALSE);
160 
161 		return (strcmp(zn->zn_key_norm, norm) == 0);
162 	} else {
163 		return (strcmp(zn->zn_key_orig, matchname) == 0);
164 	}
165 }
166 
167 static zap_name_t *
168 zap_name_alloc(zap_t *zap)
169 {
170 	zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
171 	zn->zn_zap = zap;
172 	return (zn);
173 }
174 
175 void
176 zap_name_free(zap_name_t *zn)
177 {
178 	kmem_free(zn, sizeof (zap_name_t));
179 }
180 
181 static int
182 zap_name_init_str(zap_name_t *zn, const char *key, matchtype_t mt)
183 {
184 	zap_t *zap = zn->zn_zap;
185 
186 	zn->zn_key_intlen = sizeof (*key);
187 	zn->zn_key_orig = key;
188 	zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
189 	zn->zn_matchtype = mt;
190 	zn->zn_normflags = zap->zap_normflags;
191 
192 	/*
193 	 * If we're dealing with a case sensitive lookup on a mixed or
194 	 * insensitive fs, remove U8_TEXTPREP_TOUPPER or the lookup
195 	 * will fold case to all caps overriding the lookup request.
196 	 */
197 	if (mt & MT_MATCH_CASE)
198 		zn->zn_normflags &= ~U8_TEXTPREP_TOUPPER;
199 
200 	if (zap->zap_normflags) {
201 		/*
202 		 * We *must* use zap_normflags because this normalization is
203 		 * what the hash is computed from.
204 		 */
205 		if (zap_normalize(zap, key, zn->zn_normbuf,
206 		    zap->zap_normflags) != 0)
207 			return (SET_ERROR(ENOTSUP));
208 		zn->zn_key_norm = zn->zn_normbuf;
209 		zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
210 	} else {
211 		if (mt != 0)
212 			return (SET_ERROR(ENOTSUP));
213 		zn->zn_key_norm = zn->zn_key_orig;
214 		zn->zn_key_norm_numints = zn->zn_key_orig_numints;
215 	}
216 
217 	zn->zn_hash = zap_hash(zn);
218 
219 	if (zap->zap_normflags != zn->zn_normflags) {
220 		/*
221 		 * We *must* use zn_normflags because this normalization is
222 		 * what the matching is based on.  (Not the hash!)
223 		 */
224 		if (zap_normalize(zap, key, zn->zn_normbuf,
225 		    zn->zn_normflags) != 0)
226 			return (SET_ERROR(ENOTSUP));
227 		zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
228 	}
229 
230 	return (0);
231 }
232 
233 zap_name_t *
234 zap_name_alloc_str(zap_t *zap, const char *key, matchtype_t mt)
235 {
236 	zap_name_t *zn = zap_name_alloc(zap);
237 	if (zap_name_init_str(zn, key, mt) != 0) {
238 		zap_name_free(zn);
239 		return (NULL);
240 	}
241 	return (zn);
242 }
243 
244 static zap_name_t *
245 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
246 {
247 	zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
248 
249 	ASSERT(zap->zap_normflags == 0);
250 	zn->zn_zap = zap;
251 	zn->zn_key_intlen = sizeof (*key);
252 	zn->zn_key_orig = zn->zn_key_norm = key;
253 	zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
254 	zn->zn_matchtype = 0;
255 
256 	zn->zn_hash = zap_hash(zn);
257 	return (zn);
258 }
259 
260 static void
261 mzap_byteswap(mzap_phys_t *buf, size_t size)
262 {
263 	buf->mz_block_type = BSWAP_64(buf->mz_block_type);
264 	buf->mz_salt = BSWAP_64(buf->mz_salt);
265 	buf->mz_normflags = BSWAP_64(buf->mz_normflags);
266 	int max = (size / MZAP_ENT_LEN) - 1;
267 	for (int i = 0; i < max; i++) {
268 		buf->mz_chunk[i].mze_value =
269 		    BSWAP_64(buf->mz_chunk[i].mze_value);
270 		buf->mz_chunk[i].mze_cd =
271 		    BSWAP_32(buf->mz_chunk[i].mze_cd);
272 	}
273 }
274 
275 void
276 zap_byteswap(void *buf, size_t size)
277 {
278 	uint64_t block_type = *(uint64_t *)buf;
279 
280 	if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
281 		/* ASSERT(magic == ZAP_LEAF_MAGIC); */
282 		mzap_byteswap(buf, size);
283 	} else {
284 		fzap_byteswap(buf, size);
285 	}
286 }
287 
288 static int
289 mze_compare(const void *arg1, const void *arg2)
290 {
291 	const mzap_ent_t *mze1 = arg1;
292 	const mzap_ent_t *mze2 = arg2;
293 
294 	return (TREE_CMP((uint64_t)(mze1->mze_hash) << 32 | mze1->mze_cd,
295 	    (uint64_t)(mze2->mze_hash) << 32 | mze2->mze_cd));
296 }
297 
298 static void
299 mze_insert(zap_t *zap, uint16_t chunkid, uint64_t hash)
300 {
301 	mzap_ent_t mze;
302 
303 	ASSERT(zap->zap_ismicro);
304 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
305 
306 	mze.mze_chunkid = chunkid;
307 	ASSERT0(hash & 0xffffffff);
308 	mze.mze_hash = hash >> 32;
309 	ASSERT3U(MZE_PHYS(zap, &mze)->mze_cd, <=, 0xffff);
310 	mze.mze_cd = (uint16_t)MZE_PHYS(zap, &mze)->mze_cd;
311 	ASSERT(MZE_PHYS(zap, &mze)->mze_name[0] != 0);
312 	zfs_btree_add(&zap->zap_m.zap_tree, &mze);
313 }
314 
315 static mzap_ent_t *
316 mze_find(zap_name_t *zn, zfs_btree_index_t *idx)
317 {
318 	mzap_ent_t mze_tofind;
319 	mzap_ent_t *mze;
320 	zfs_btree_t *tree = &zn->zn_zap->zap_m.zap_tree;
321 
322 	ASSERT(zn->zn_zap->zap_ismicro);
323 	ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
324 
325 	ASSERT0(zn->zn_hash & 0xffffffff);
326 	mze_tofind.mze_hash = zn->zn_hash >> 32;
327 	mze_tofind.mze_cd = 0;
328 
329 	mze = zfs_btree_find(tree, &mze_tofind, idx);
330 	if (mze == NULL)
331 		mze = zfs_btree_next(tree, idx, idx);
332 	for (; mze && mze->mze_hash == mze_tofind.mze_hash;
333 	    mze = zfs_btree_next(tree, idx, idx)) {
334 		ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
335 		if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
336 			return (mze);
337 	}
338 
339 	return (NULL);
340 }
341 
342 static uint32_t
343 mze_find_unused_cd(zap_t *zap, uint64_t hash)
344 {
345 	mzap_ent_t mze_tofind;
346 	zfs_btree_index_t idx;
347 	zfs_btree_t *tree = &zap->zap_m.zap_tree;
348 
349 	ASSERT(zap->zap_ismicro);
350 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
351 
352 	ASSERT0(hash & 0xffffffff);
353 	hash >>= 32;
354 	mze_tofind.mze_hash = hash;
355 	mze_tofind.mze_cd = 0;
356 
357 	uint32_t cd = 0;
358 	for (mzap_ent_t *mze = zfs_btree_find(tree, &mze_tofind, &idx);
359 	    mze && mze->mze_hash == hash;
360 	    mze = zfs_btree_next(tree, &idx, &idx)) {
361 		if (mze->mze_cd != cd)
362 			break;
363 		cd++;
364 	}
365 
366 	return (cd);
367 }
368 
369 /*
370  * Each mzap entry requires at max : 4 chunks
371  * 3 chunks for names + 1 chunk for value.
372  */
373 #define	MZAP_ENT_CHUNKS	(1 + ZAP_LEAF_ARRAY_NCHUNKS(MZAP_NAME_LEN) + \
374 	ZAP_LEAF_ARRAY_NCHUNKS(sizeof (uint64_t)))
375 
376 /*
377  * Check if the current entry keeps the colliding entries under the fatzap leaf
378  * size.
379  */
380 static boolean_t
381 mze_canfit_fzap_leaf(zap_name_t *zn, uint64_t hash)
382 {
383 	zap_t *zap = zn->zn_zap;
384 	mzap_ent_t mze_tofind;
385 	zfs_btree_index_t idx;
386 	zfs_btree_t *tree = &zap->zap_m.zap_tree;
387 	uint32_t mzap_ents = 0;
388 
389 	ASSERT0(hash & 0xffffffff);
390 	hash >>= 32;
391 	mze_tofind.mze_hash = hash;
392 	mze_tofind.mze_cd = 0;
393 
394 	for (mzap_ent_t *mze = zfs_btree_find(tree, &mze_tofind, &idx);
395 	    mze && mze->mze_hash == hash;
396 	    mze = zfs_btree_next(tree, &idx, &idx)) {
397 		mzap_ents++;
398 	}
399 
400 	/* Include the new entry being added */
401 	mzap_ents++;
402 
403 	return (ZAP_LEAF_NUMCHUNKS_DEF > (mzap_ents * MZAP_ENT_CHUNKS));
404 }
405 
406 static void
407 mze_destroy(zap_t *zap)
408 {
409 	zfs_btree_clear(&zap->zap_m.zap_tree);
410 	zfs_btree_destroy(&zap->zap_m.zap_tree);
411 }
412 
413 static zap_t *
414 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
415 {
416 	zap_t *winner;
417 	uint64_t *zap_hdr = (uint64_t *)db->db_data;
418 	uint64_t zap_block_type = zap_hdr[0];
419 	uint64_t zap_magic = zap_hdr[1];
420 
421 	ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
422 
423 	zap_t *zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
424 	rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
425 	rw_enter(&zap->zap_rwlock, RW_WRITER);
426 	zap->zap_objset = os;
427 	zap->zap_object = obj;
428 	zap->zap_dbuf = db;
429 
430 	if (zap_block_type != ZBT_MICRO) {
431 		mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT,
432 		    0);
433 		zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
434 		if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
435 			winner = NULL;	/* No actual winner here... */
436 			goto handle_winner;
437 		}
438 	} else {
439 		zap->zap_ismicro = TRUE;
440 	}
441 
442 	/*
443 	 * Make sure that zap_ismicro is set before we let others see
444 	 * it, because zap_lockdir() checks zap_ismicro without the lock
445 	 * held.
446 	 */
447 	dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf);
448 	winner = dmu_buf_set_user(db, &zap->zap_dbu);
449 
450 	if (winner != NULL)
451 		goto handle_winner;
452 
453 	if (zap->zap_ismicro) {
454 		zap->zap_salt = zap_m_phys(zap)->mz_salt;
455 		zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
456 		zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
457 
458 		/*
459 		 * Reduce B-tree leaf from 4KB to 512 bytes to reduce memmove()
460 		 * overhead on massive inserts below.  It still allows to store
461 		 * 62 entries before we have to add 2KB B-tree core node.
462 		 */
463 		zfs_btree_create_custom(&zap->zap_m.zap_tree, mze_compare,
464 		    sizeof (mzap_ent_t), 512);
465 
466 		zap_name_t *zn = zap_name_alloc(zap);
467 		for (uint16_t i = 0; i < zap->zap_m.zap_num_chunks; i++) {
468 			mzap_ent_phys_t *mze =
469 			    &zap_m_phys(zap)->mz_chunk[i];
470 			if (mze->mze_name[0]) {
471 				zap->zap_m.zap_num_entries++;
472 				zap_name_init_str(zn, mze->mze_name, 0);
473 				mze_insert(zap, i, zn->zn_hash);
474 			}
475 		}
476 		zap_name_free(zn);
477 	} else {
478 		zap->zap_salt = zap_f_phys(zap)->zap_salt;
479 		zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
480 
481 		ASSERT3U(sizeof (struct zap_leaf_header), ==,
482 		    2*ZAP_LEAF_CHUNKSIZE);
483 
484 		/*
485 		 * The embedded pointer table should not overlap the
486 		 * other members.
487 		 */
488 		ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
489 		    &zap_f_phys(zap)->zap_salt);
490 
491 		/*
492 		 * The embedded pointer table should end at the end of
493 		 * the block
494 		 */
495 		ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
496 		    1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
497 		    (uintptr_t)zap_f_phys(zap), ==,
498 		    zap->zap_dbuf->db_size);
499 	}
500 	rw_exit(&zap->zap_rwlock);
501 	return (zap);
502 
503 handle_winner:
504 	rw_exit(&zap->zap_rwlock);
505 	rw_destroy(&zap->zap_rwlock);
506 	if (!zap->zap_ismicro)
507 		mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
508 	kmem_free(zap, sizeof (zap_t));
509 	return (winner);
510 }
511 
512 /*
513  * This routine "consumes" the caller's hold on the dbuf, which must
514  * have the specified tag.
515  */
516 static int
517 zap_lockdir_impl(dmu_buf_t *db, const void *tag, dmu_tx_t *tx,
518     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
519 {
520 	ASSERT0(db->db_offset);
521 	objset_t *os = dmu_buf_get_objset(db);
522 	uint64_t obj = db->db_object;
523 	dmu_object_info_t doi;
524 
525 	*zapp = NULL;
526 
527 	dmu_object_info_from_db(db, &doi);
528 	if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
529 		return (SET_ERROR(EINVAL));
530 
531 	zap_t *zap = dmu_buf_get_user(db);
532 	if (zap == NULL) {
533 		zap = mzap_open(os, obj, db);
534 		if (zap == NULL) {
535 			/*
536 			 * mzap_open() didn't like what it saw on-disk.
537 			 * Check for corruption!
538 			 */
539 			return (SET_ERROR(EIO));
540 		}
541 	}
542 
543 	/*
544 	 * We're checking zap_ismicro without the lock held, in order to
545 	 * tell what type of lock we want.  Once we have some sort of
546 	 * lock, see if it really is the right type.  In practice this
547 	 * can only be different if it was upgraded from micro to fat,
548 	 * and micro wanted WRITER but fat only needs READER.
549 	 */
550 	krw_t lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
551 	rw_enter(&zap->zap_rwlock, lt);
552 	if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
553 		/* it was upgraded, now we only need reader */
554 		ASSERT(lt == RW_WRITER);
555 		ASSERT(RW_READER ==
556 		    ((!zap->zap_ismicro && fatreader) ? RW_READER : lti));
557 		rw_downgrade(&zap->zap_rwlock);
558 		lt = RW_READER;
559 	}
560 
561 	zap->zap_objset = os;
562 
563 	if (lt == RW_WRITER)
564 		dmu_buf_will_dirty(db, tx);
565 
566 	ASSERT3P(zap->zap_dbuf, ==, db);
567 
568 	ASSERT(!zap->zap_ismicro ||
569 	    zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
570 	if (zap->zap_ismicro && tx && adding &&
571 	    zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
572 		uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
573 		if (newsz > zap_micro_max_size) {
574 			dprintf("upgrading obj %llu: num_entries=%u\n",
575 			    (u_longlong_t)obj, zap->zap_m.zap_num_entries);
576 			*zapp = zap;
577 			int err = mzap_upgrade(zapp, tag, tx, 0);
578 			if (err != 0)
579 				rw_exit(&zap->zap_rwlock);
580 			return (err);
581 		}
582 		VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
583 		zap->zap_m.zap_num_chunks =
584 		    db->db_size / MZAP_ENT_LEN - 1;
585 	}
586 
587 	*zapp = zap;
588 	return (0);
589 }
590 
591 static int
592 zap_lockdir_by_dnode(dnode_t *dn, dmu_tx_t *tx,
593     krw_t lti, boolean_t fatreader, boolean_t adding, const void *tag,
594     zap_t **zapp)
595 {
596 	dmu_buf_t *db;
597 
598 	int err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH);
599 	if (err != 0) {
600 		return (err);
601 	}
602 #ifdef ZFS_DEBUG
603 	{
604 		dmu_object_info_t doi;
605 		dmu_object_info_from_db(db, &doi);
606 		ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
607 	}
608 #endif
609 
610 	err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
611 	if (err != 0) {
612 		dmu_buf_rele(db, tag);
613 	}
614 	return (err);
615 }
616 
617 int
618 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
619     krw_t lti, boolean_t fatreader, boolean_t adding, const void *tag,
620     zap_t **zapp)
621 {
622 	dmu_buf_t *db;
623 
624 	int err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH);
625 	if (err != 0)
626 		return (err);
627 #ifdef ZFS_DEBUG
628 	{
629 		dmu_object_info_t doi;
630 		dmu_object_info_from_db(db, &doi);
631 		ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
632 	}
633 #endif
634 	err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
635 	if (err != 0)
636 		dmu_buf_rele(db, tag);
637 	return (err);
638 }
639 
640 void
641 zap_unlockdir(zap_t *zap, const void *tag)
642 {
643 	rw_exit(&zap->zap_rwlock);
644 	dmu_buf_rele(zap->zap_dbuf, tag);
645 }
646 
647 static int
648 mzap_upgrade(zap_t **zapp, const void *tag, dmu_tx_t *tx, zap_flags_t flags)
649 {
650 	int err = 0;
651 	zap_t *zap = *zapp;
652 
653 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
654 
655 	int sz = zap->zap_dbuf->db_size;
656 	mzap_phys_t *mzp = vmem_alloc(sz, KM_SLEEP);
657 	memcpy(mzp, zap->zap_dbuf->db_data, sz);
658 	int nchunks = zap->zap_m.zap_num_chunks;
659 
660 	if (!flags) {
661 		err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
662 		    1ULL << fzap_default_block_shift, 0, tx);
663 		if (err != 0) {
664 			vmem_free(mzp, sz);
665 			return (err);
666 		}
667 	}
668 
669 	dprintf("upgrading obj=%llu with %u chunks\n",
670 	    (u_longlong_t)zap->zap_object, nchunks);
671 	/* XXX destroy the tree later, so we can use the stored hash value */
672 	mze_destroy(zap);
673 
674 	fzap_upgrade(zap, tx, flags);
675 
676 	zap_name_t *zn = zap_name_alloc(zap);
677 	for (int i = 0; i < nchunks; i++) {
678 		mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
679 		if (mze->mze_name[0] == 0)
680 			continue;
681 		dprintf("adding %s=%llu\n",
682 		    mze->mze_name, (u_longlong_t)mze->mze_value);
683 		zap_name_init_str(zn, mze->mze_name, 0);
684 		/* If we fail here, we would end up losing entries */
685 		VERIFY0(fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
686 		    tag, tx));
687 		zap = zn->zn_zap;	/* fzap_add_cd() may change zap */
688 	}
689 	zap_name_free(zn);
690 	vmem_free(mzp, sz);
691 	*zapp = zap;
692 	return (0);
693 }
694 
695 /*
696  * The "normflags" determine the behavior of the matchtype_t which is
697  * passed to zap_lookup_norm().  Names which have the same normalized
698  * version will be stored with the same hash value, and therefore we can
699  * perform normalization-insensitive lookups.  We can be Unicode form-
700  * insensitive and/or case-insensitive.  The following flags are valid for
701  * "normflags":
702  *
703  * U8_TEXTPREP_NFC
704  * U8_TEXTPREP_NFD
705  * U8_TEXTPREP_NFKC
706  * U8_TEXTPREP_NFKD
707  * U8_TEXTPREP_TOUPPER
708  *
709  * The *_NF* (Normalization Form) flags are mutually exclusive; at most one
710  * of them may be supplied.
711  */
712 void
713 mzap_create_impl(dnode_t *dn, int normflags, zap_flags_t flags, dmu_tx_t *tx)
714 {
715 	dmu_buf_t *db;
716 
717 	VERIFY0(dmu_buf_hold_by_dnode(dn, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
718 
719 	dmu_buf_will_dirty(db, tx);
720 	mzap_phys_t *zp = db->db_data;
721 	zp->mz_block_type = ZBT_MICRO;
722 	zp->mz_salt =
723 	    ((uintptr_t)db ^ (uintptr_t)tx ^ (dn->dn_object << 1)) | 1ULL;
724 	zp->mz_normflags = normflags;
725 
726 	if (flags != 0) {
727 		zap_t *zap;
728 		/* Only fat zap supports flags; upgrade immediately. */
729 		VERIFY0(zap_lockdir_impl(db, FTAG, tx, RW_WRITER,
730 		    B_FALSE, B_FALSE, &zap));
731 		VERIFY0(mzap_upgrade(&zap, FTAG, tx, flags));
732 		zap_unlockdir(zap, FTAG);
733 	} else {
734 		dmu_buf_rele(db, FTAG);
735 	}
736 }
737 
738 static uint64_t
739 zap_create_impl(objset_t *os, int normflags, zap_flags_t flags,
740     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
741     dmu_object_type_t bonustype, int bonuslen, int dnodesize,
742     dnode_t **allocated_dnode, const void *tag, dmu_tx_t *tx)
743 {
744 	uint64_t obj;
745 
746 	ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP);
747 
748 	if (allocated_dnode == NULL) {
749 		dnode_t *dn;
750 		obj = dmu_object_alloc_hold(os, ot, 1ULL << leaf_blockshift,
751 		    indirect_blockshift, bonustype, bonuslen, dnodesize,
752 		    &dn, FTAG, tx);
753 		mzap_create_impl(dn, normflags, flags, tx);
754 		dnode_rele(dn, FTAG);
755 	} else {
756 		obj = dmu_object_alloc_hold(os, ot, 1ULL << leaf_blockshift,
757 		    indirect_blockshift, bonustype, bonuslen, dnodesize,
758 		    allocated_dnode, tag, tx);
759 		mzap_create_impl(*allocated_dnode, normflags, flags, tx);
760 	}
761 
762 	return (obj);
763 }
764 
765 int
766 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
767     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
768 {
769 	return (zap_create_claim_dnsize(os, obj, ot, bonustype, bonuslen,
770 	    0, tx));
771 }
772 
773 int
774 zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot,
775     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
776 {
777 	return (zap_create_claim_norm_dnsize(os, obj,
778 	    0, ot, bonustype, bonuslen, dnodesize, tx));
779 }
780 
781 int
782 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
783     dmu_object_type_t ot,
784     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
785 {
786 	return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype,
787 	    bonuslen, 0, tx));
788 }
789 
790 int
791 zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
792     dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
793     int dnodesize, dmu_tx_t *tx)
794 {
795 	dnode_t *dn;
796 	int error;
797 
798 	ASSERT3U(DMU_OT_BYTESWAP(ot), ==, DMU_BSWAP_ZAP);
799 	error = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
800 	    dnodesize, tx);
801 	if (error != 0)
802 		return (error);
803 
804 	error = dnode_hold(os, obj, FTAG, &dn);
805 	if (error != 0)
806 		return (error);
807 
808 	mzap_create_impl(dn, normflags, 0, tx);
809 
810 	dnode_rele(dn, FTAG);
811 
812 	return (0);
813 }
814 
815 uint64_t
816 zap_create(objset_t *os, dmu_object_type_t ot,
817     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
818 {
819 	return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
820 }
821 
822 uint64_t
823 zap_create_dnsize(objset_t *os, dmu_object_type_t ot,
824     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
825 {
826 	return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen,
827 	    dnodesize, tx));
828 }
829 
830 uint64_t
831 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
832     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
833 {
834 	return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen,
835 	    0, tx));
836 }
837 
838 uint64_t
839 zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
840     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
841 {
842 	return (zap_create_impl(os, normflags, 0, ot, 0, 0,
843 	    bonustype, bonuslen, dnodesize, NULL, NULL, tx));
844 }
845 
846 uint64_t
847 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
848     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
849     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
850 {
851 	return (zap_create_flags_dnsize(os, normflags, flags, ot,
852 	    leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx));
853 }
854 
855 uint64_t
856 zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
857     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
858     dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
859 {
860 	return (zap_create_impl(os, normflags, flags, ot, leaf_blockshift,
861 	    indirect_blockshift, bonustype, bonuslen, dnodesize, NULL, NULL,
862 	    tx));
863 }
864 
865 /*
866  * Create a zap object and return a pointer to the newly allocated dnode via
867  * the allocated_dnode argument.  The returned dnode will be held and the
868  * caller is responsible for releasing the hold by calling dnode_rele().
869  */
870 uint64_t
871 zap_create_hold(objset_t *os, int normflags, zap_flags_t flags,
872     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
873     dmu_object_type_t bonustype, int bonuslen, int dnodesize,
874     dnode_t **allocated_dnode, const void *tag, dmu_tx_t *tx)
875 {
876 	return (zap_create_impl(os, normflags, flags, ot, leaf_blockshift,
877 	    indirect_blockshift, bonustype, bonuslen, dnodesize,
878 	    allocated_dnode, tag, tx));
879 }
880 
881 int
882 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
883 {
884 	/*
885 	 * dmu_object_free will free the object number and free the
886 	 * data.  Freeing the data will cause our pageout function to be
887 	 * called, which will destroy our data (zap_leaf_t's and zap_t).
888 	 */
889 
890 	return (dmu_object_free(os, zapobj, tx));
891 }
892 
893 void
894 zap_evict_sync(void *dbu)
895 {
896 	zap_t *zap = dbu;
897 
898 	rw_destroy(&zap->zap_rwlock);
899 
900 	if (zap->zap_ismicro)
901 		mze_destroy(zap);
902 	else
903 		mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
904 
905 	kmem_free(zap, sizeof (zap_t));
906 }
907 
908 int
909 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
910 {
911 	zap_t *zap;
912 
913 	int err =
914 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
915 	if (err != 0)
916 		return (err);
917 	if (!zap->zap_ismicro) {
918 		err = fzap_count(zap, count);
919 	} else {
920 		*count = zap->zap_m.zap_num_entries;
921 	}
922 	zap_unlockdir(zap, FTAG);
923 	return (err);
924 }
925 
926 /*
927  * zn may be NULL; if not specified, it will be computed if needed.
928  * See also the comment above zap_entry_normalization_conflict().
929  */
930 static boolean_t
931 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze,
932     zfs_btree_index_t *idx)
933 {
934 	boolean_t allocdzn = B_FALSE;
935 	mzap_ent_t *other;
936 	zfs_btree_index_t oidx;
937 
938 	if (zap->zap_normflags == 0)
939 		return (B_FALSE);
940 
941 	for (other = zfs_btree_prev(&zap->zap_m.zap_tree, idx, &oidx);
942 	    other && other->mze_hash == mze->mze_hash;
943 	    other = zfs_btree_prev(&zap->zap_m.zap_tree, &oidx, &oidx)) {
944 
945 		if (zn == NULL) {
946 			zn = zap_name_alloc_str(zap,
947 			    MZE_PHYS(zap, mze)->mze_name, MT_NORMALIZE);
948 			allocdzn = B_TRUE;
949 		}
950 		if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
951 			if (allocdzn)
952 				zap_name_free(zn);
953 			return (B_TRUE);
954 		}
955 	}
956 
957 	for (other = zfs_btree_next(&zap->zap_m.zap_tree, idx, &oidx);
958 	    other && other->mze_hash == mze->mze_hash;
959 	    other = zfs_btree_next(&zap->zap_m.zap_tree, &oidx, &oidx)) {
960 
961 		if (zn == NULL) {
962 			zn = zap_name_alloc_str(zap,
963 			    MZE_PHYS(zap, mze)->mze_name, MT_NORMALIZE);
964 			allocdzn = B_TRUE;
965 		}
966 		if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
967 			if (allocdzn)
968 				zap_name_free(zn);
969 			return (B_TRUE);
970 		}
971 	}
972 
973 	if (allocdzn)
974 		zap_name_free(zn);
975 	return (B_FALSE);
976 }
977 
978 /*
979  * Routines for manipulating attributes.
980  */
981 
982 int
983 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
984     uint64_t integer_size, uint64_t num_integers, void *buf)
985 {
986 	return (zap_lookup_norm(os, zapobj, name, integer_size,
987 	    num_integers, buf, 0, NULL, 0, NULL));
988 }
989 
990 static int
991 zap_lookup_impl(zap_t *zap, const char *name,
992     uint64_t integer_size, uint64_t num_integers, void *buf,
993     matchtype_t mt, char *realname, int rn_len,
994     boolean_t *ncp)
995 {
996 	int err = 0;
997 
998 	zap_name_t *zn = zap_name_alloc_str(zap, name, mt);
999 	if (zn == NULL)
1000 		return (SET_ERROR(ENOTSUP));
1001 
1002 	if (!zap->zap_ismicro) {
1003 		err = fzap_lookup(zn, integer_size, num_integers, buf,
1004 		    realname, rn_len, ncp);
1005 	} else {
1006 		zfs_btree_index_t idx;
1007 		mzap_ent_t *mze = mze_find(zn, &idx);
1008 		if (mze == NULL) {
1009 			err = SET_ERROR(ENOENT);
1010 		} else {
1011 			if (num_integers < 1) {
1012 				err = SET_ERROR(EOVERFLOW);
1013 			} else if (integer_size != 8) {
1014 				err = SET_ERROR(EINVAL);
1015 			} else {
1016 				*(uint64_t *)buf =
1017 				    MZE_PHYS(zap, mze)->mze_value;
1018 				if (realname != NULL)
1019 					(void) strlcpy(realname,
1020 					    MZE_PHYS(zap, mze)->mze_name,
1021 					    rn_len);
1022 				if (ncp) {
1023 					*ncp = mzap_normalization_conflict(zap,
1024 					    zn, mze, &idx);
1025 				}
1026 			}
1027 		}
1028 	}
1029 	zap_name_free(zn);
1030 	return (err);
1031 }
1032 
1033 int
1034 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
1035     uint64_t integer_size, uint64_t num_integers, void *buf,
1036     matchtype_t mt, char *realname, int rn_len,
1037     boolean_t *ncp)
1038 {
1039 	zap_t *zap;
1040 
1041 	int err =
1042 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1043 	if (err != 0)
1044 		return (err);
1045 	err = zap_lookup_impl(zap, name, integer_size,
1046 	    num_integers, buf, mt, realname, rn_len, ncp);
1047 	zap_unlockdir(zap, FTAG);
1048 	return (err);
1049 }
1050 
1051 int
1052 zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
1053 {
1054 	zap_t *zap;
1055 	int err;
1056 	zap_name_t *zn;
1057 
1058 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1059 	if (err)
1060 		return (err);
1061 	zn = zap_name_alloc_str(zap, name, 0);
1062 	if (zn == NULL) {
1063 		zap_unlockdir(zap, FTAG);
1064 		return (SET_ERROR(ENOTSUP));
1065 	}
1066 
1067 	fzap_prefetch(zn);
1068 	zap_name_free(zn);
1069 	zap_unlockdir(zap, FTAG);
1070 	return (err);
1071 }
1072 
1073 int
1074 zap_lookup_by_dnode(dnode_t *dn, const char *name,
1075     uint64_t integer_size, uint64_t num_integers, void *buf)
1076 {
1077 	return (zap_lookup_norm_by_dnode(dn, name, integer_size,
1078 	    num_integers, buf, 0, NULL, 0, NULL));
1079 }
1080 
1081 int
1082 zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
1083     uint64_t integer_size, uint64_t num_integers, void *buf,
1084     matchtype_t mt, char *realname, int rn_len,
1085     boolean_t *ncp)
1086 {
1087 	zap_t *zap;
1088 
1089 	int err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
1090 	    FTAG, &zap);
1091 	if (err != 0)
1092 		return (err);
1093 	err = zap_lookup_impl(zap, name, integer_size,
1094 	    num_integers, buf, mt, realname, rn_len, ncp);
1095 	zap_unlockdir(zap, FTAG);
1096 	return (err);
1097 }
1098 
1099 int
1100 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1101     int key_numints)
1102 {
1103 	zap_t *zap;
1104 
1105 	int err =
1106 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1107 	if (err != 0)
1108 		return (err);
1109 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1110 	if (zn == NULL) {
1111 		zap_unlockdir(zap, FTAG);
1112 		return (SET_ERROR(ENOTSUP));
1113 	}
1114 
1115 	fzap_prefetch(zn);
1116 	zap_name_free(zn);
1117 	zap_unlockdir(zap, FTAG);
1118 	return (err);
1119 }
1120 
1121 int
1122 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1123     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
1124 {
1125 	zap_t *zap;
1126 
1127 	int err =
1128 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1129 	if (err != 0)
1130 		return (err);
1131 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1132 	if (zn == NULL) {
1133 		zap_unlockdir(zap, FTAG);
1134 		return (SET_ERROR(ENOTSUP));
1135 	}
1136 
1137 	err = fzap_lookup(zn, integer_size, num_integers, buf,
1138 	    NULL, 0, NULL);
1139 	zap_name_free(zn);
1140 	zap_unlockdir(zap, FTAG);
1141 	return (err);
1142 }
1143 
1144 int
1145 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
1146 {
1147 	int err = zap_lookup_norm(os, zapobj, name, 0,
1148 	    0, NULL, 0, NULL, 0, NULL);
1149 	if (err == EOVERFLOW || err == EINVAL)
1150 		err = 0; /* found, but skipped reading the value */
1151 	return (err);
1152 }
1153 
1154 int
1155 zap_length(objset_t *os, uint64_t zapobj, const char *name,
1156     uint64_t *integer_size, uint64_t *num_integers)
1157 {
1158 	zap_t *zap;
1159 
1160 	int err =
1161 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1162 	if (err != 0)
1163 		return (err);
1164 	zap_name_t *zn = zap_name_alloc_str(zap, name, 0);
1165 	if (zn == NULL) {
1166 		zap_unlockdir(zap, FTAG);
1167 		return (SET_ERROR(ENOTSUP));
1168 	}
1169 	if (!zap->zap_ismicro) {
1170 		err = fzap_length(zn, integer_size, num_integers);
1171 	} else {
1172 		zfs_btree_index_t idx;
1173 		mzap_ent_t *mze = mze_find(zn, &idx);
1174 		if (mze == NULL) {
1175 			err = SET_ERROR(ENOENT);
1176 		} else {
1177 			if (integer_size)
1178 				*integer_size = 8;
1179 			if (num_integers)
1180 				*num_integers = 1;
1181 		}
1182 	}
1183 	zap_name_free(zn);
1184 	zap_unlockdir(zap, FTAG);
1185 	return (err);
1186 }
1187 
1188 int
1189 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1190     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
1191 {
1192 	zap_t *zap;
1193 
1194 	int err =
1195 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1196 	if (err != 0)
1197 		return (err);
1198 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1199 	if (zn == NULL) {
1200 		zap_unlockdir(zap, FTAG);
1201 		return (SET_ERROR(ENOTSUP));
1202 	}
1203 	err = fzap_length(zn, integer_size, num_integers);
1204 	zap_name_free(zn);
1205 	zap_unlockdir(zap, FTAG);
1206 	return (err);
1207 }
1208 
1209 static void
1210 mzap_addent(zap_name_t *zn, uint64_t value)
1211 {
1212 	zap_t *zap = zn->zn_zap;
1213 	uint16_t start = zap->zap_m.zap_alloc_next;
1214 
1215 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1216 
1217 #ifdef ZFS_DEBUG
1218 	for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) {
1219 		mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1220 		ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
1221 	}
1222 #endif
1223 
1224 	uint32_t cd = mze_find_unused_cd(zap, zn->zn_hash);
1225 	/* given the limited size of the microzap, this can't happen */
1226 	ASSERT(cd < zap_maxcd(zap));
1227 
1228 again:
1229 	for (uint16_t i = start; i < zap->zap_m.zap_num_chunks; i++) {
1230 		mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1231 		if (mze->mze_name[0] == 0) {
1232 			mze->mze_value = value;
1233 			mze->mze_cd = cd;
1234 			(void) strlcpy(mze->mze_name, zn->zn_key_orig,
1235 			    sizeof (mze->mze_name));
1236 			zap->zap_m.zap_num_entries++;
1237 			zap->zap_m.zap_alloc_next = i+1;
1238 			if (zap->zap_m.zap_alloc_next ==
1239 			    zap->zap_m.zap_num_chunks)
1240 				zap->zap_m.zap_alloc_next = 0;
1241 			mze_insert(zap, i, zn->zn_hash);
1242 			return;
1243 		}
1244 	}
1245 	if (start != 0) {
1246 		start = 0;
1247 		goto again;
1248 	}
1249 	cmn_err(CE_PANIC, "out of entries!");
1250 }
1251 
1252 static int
1253 zap_add_impl(zap_t *zap, const char *key,
1254     int integer_size, uint64_t num_integers,
1255     const void *val, dmu_tx_t *tx, const void *tag)
1256 {
1257 	const uint64_t *intval = val;
1258 	int err = 0;
1259 
1260 	zap_name_t *zn = zap_name_alloc_str(zap, key, 0);
1261 	if (zn == NULL) {
1262 		zap_unlockdir(zap, tag);
1263 		return (SET_ERROR(ENOTSUP));
1264 	}
1265 	if (!zap->zap_ismicro) {
1266 		err = fzap_add(zn, integer_size, num_integers, val, tag, tx);
1267 		zap = zn->zn_zap;	/* fzap_add() may change zap */
1268 	} else if (integer_size != 8 || num_integers != 1 ||
1269 	    strlen(key) >= MZAP_NAME_LEN ||
1270 	    !mze_canfit_fzap_leaf(zn, zn->zn_hash)) {
1271 		err = mzap_upgrade(&zn->zn_zap, tag, tx, 0);
1272 		if (err == 0) {
1273 			err = fzap_add(zn, integer_size, num_integers, val,
1274 			    tag, tx);
1275 		}
1276 		zap = zn->zn_zap;	/* fzap_add() may change zap */
1277 	} else {
1278 		zfs_btree_index_t idx;
1279 		if (mze_find(zn, &idx) != NULL) {
1280 			err = SET_ERROR(EEXIST);
1281 		} else {
1282 			mzap_addent(zn, *intval);
1283 		}
1284 	}
1285 	ASSERT(zap == zn->zn_zap);
1286 	zap_name_free(zn);
1287 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
1288 		zap_unlockdir(zap, tag);
1289 	return (err);
1290 }
1291 
1292 int
1293 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1294     int integer_size, uint64_t num_integers,
1295     const void *val, dmu_tx_t *tx)
1296 {
1297 	zap_t *zap;
1298 	int err;
1299 
1300 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1301 	if (err != 0)
1302 		return (err);
1303 	err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1304 	/* zap_add_impl() calls zap_unlockdir() */
1305 	return (err);
1306 }
1307 
1308 int
1309 zap_add_by_dnode(dnode_t *dn, const char *key,
1310     int integer_size, uint64_t num_integers,
1311     const void *val, dmu_tx_t *tx)
1312 {
1313 	zap_t *zap;
1314 	int err;
1315 
1316 	err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1317 	if (err != 0)
1318 		return (err);
1319 	err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1320 	/* zap_add_impl() calls zap_unlockdir() */
1321 	return (err);
1322 }
1323 
1324 int
1325 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1326     int key_numints, int integer_size, uint64_t num_integers,
1327     const void *val, dmu_tx_t *tx)
1328 {
1329 	zap_t *zap;
1330 
1331 	int err =
1332 	    zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1333 	if (err != 0)
1334 		return (err);
1335 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1336 	if (zn == NULL) {
1337 		zap_unlockdir(zap, FTAG);
1338 		return (SET_ERROR(ENOTSUP));
1339 	}
1340 	err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1341 	zap = zn->zn_zap;	/* fzap_add() may change zap */
1342 	zap_name_free(zn);
1343 	if (zap != NULL)	/* may be NULL if fzap_add() failed */
1344 		zap_unlockdir(zap, FTAG);
1345 	return (err);
1346 }
1347 
1348 int
1349 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1350     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1351 {
1352 	zap_t *zap;
1353 	const uint64_t *intval = val;
1354 
1355 	int err =
1356 	    zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1357 	if (err != 0)
1358 		return (err);
1359 	zap_name_t *zn = zap_name_alloc_str(zap, name, 0);
1360 	if (zn == NULL) {
1361 		zap_unlockdir(zap, FTAG);
1362 		return (SET_ERROR(ENOTSUP));
1363 	}
1364 	if (!zap->zap_ismicro) {
1365 		err = fzap_update(zn, integer_size, num_integers, val,
1366 		    FTAG, tx);
1367 		zap = zn->zn_zap;	/* fzap_update() may change zap */
1368 	} else if (integer_size != 8 || num_integers != 1 ||
1369 	    strlen(name) >= MZAP_NAME_LEN) {
1370 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1371 		    (u_longlong_t)zapobj, integer_size,
1372 		    (u_longlong_t)num_integers, name);
1373 		err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1374 		if (err == 0) {
1375 			err = fzap_update(zn, integer_size, num_integers,
1376 			    val, FTAG, tx);
1377 		}
1378 		zap = zn->zn_zap;	/* fzap_update() may change zap */
1379 	} else {
1380 		zfs_btree_index_t idx;
1381 		mzap_ent_t *mze = mze_find(zn, &idx);
1382 		if (mze != NULL) {
1383 			MZE_PHYS(zap, mze)->mze_value = *intval;
1384 		} else {
1385 			mzap_addent(zn, *intval);
1386 		}
1387 	}
1388 	ASSERT(zap == zn->zn_zap);
1389 	zap_name_free(zn);
1390 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
1391 		zap_unlockdir(zap, FTAG);
1392 	return (err);
1393 }
1394 
1395 int
1396 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1397     int key_numints,
1398     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1399 {
1400 	zap_t *zap;
1401 
1402 	int err =
1403 	    zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1404 	if (err != 0)
1405 		return (err);
1406 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1407 	if (zn == NULL) {
1408 		zap_unlockdir(zap, FTAG);
1409 		return (SET_ERROR(ENOTSUP));
1410 	}
1411 	err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
1412 	zap = zn->zn_zap;	/* fzap_update() may change zap */
1413 	zap_name_free(zn);
1414 	if (zap != NULL)	/* may be NULL if fzap_upgrade() failed */
1415 		zap_unlockdir(zap, FTAG);
1416 	return (err);
1417 }
1418 
1419 int
1420 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1421 {
1422 	return (zap_remove_norm(os, zapobj, name, 0, tx));
1423 }
1424 
1425 static int
1426 zap_remove_impl(zap_t *zap, const char *name,
1427     matchtype_t mt, dmu_tx_t *tx)
1428 {
1429 	int err = 0;
1430 
1431 	zap_name_t *zn = zap_name_alloc_str(zap, name, mt);
1432 	if (zn == NULL)
1433 		return (SET_ERROR(ENOTSUP));
1434 	if (!zap->zap_ismicro) {
1435 		err = fzap_remove(zn, tx);
1436 	} else {
1437 		zfs_btree_index_t idx;
1438 		mzap_ent_t *mze = mze_find(zn, &idx);
1439 		if (mze == NULL) {
1440 			err = SET_ERROR(ENOENT);
1441 		} else {
1442 			zap->zap_m.zap_num_entries--;
1443 			memset(MZE_PHYS(zap, mze), 0, sizeof (mzap_ent_phys_t));
1444 			zfs_btree_remove_idx(&zap->zap_m.zap_tree, &idx);
1445 		}
1446 	}
1447 	zap_name_free(zn);
1448 	return (err);
1449 }
1450 
1451 int
1452 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1453     matchtype_t mt, dmu_tx_t *tx)
1454 {
1455 	zap_t *zap;
1456 	int err;
1457 
1458 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1459 	if (err)
1460 		return (err);
1461 	err = zap_remove_impl(zap, name, mt, tx);
1462 	zap_unlockdir(zap, FTAG);
1463 	return (err);
1464 }
1465 
1466 int
1467 zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx)
1468 {
1469 	zap_t *zap;
1470 	int err;
1471 
1472 	err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1473 	if (err)
1474 		return (err);
1475 	err = zap_remove_impl(zap, name, 0, tx);
1476 	zap_unlockdir(zap, FTAG);
1477 	return (err);
1478 }
1479 
1480 int
1481 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1482     int key_numints, dmu_tx_t *tx)
1483 {
1484 	zap_t *zap;
1485 
1486 	int err =
1487 	    zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1488 	if (err != 0)
1489 		return (err);
1490 	zap_name_t *zn = zap_name_alloc_uint64(zap, key, key_numints);
1491 	if (zn == NULL) {
1492 		zap_unlockdir(zap, FTAG);
1493 		return (SET_ERROR(ENOTSUP));
1494 	}
1495 	err = fzap_remove(zn, tx);
1496 	zap_name_free(zn);
1497 	zap_unlockdir(zap, FTAG);
1498 	return (err);
1499 }
1500 
1501 /*
1502  * Routines for iterating over the attributes.
1503  */
1504 
1505 static void
1506 zap_cursor_init_impl(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1507     uint64_t serialized, boolean_t prefetch)
1508 {
1509 	zc->zc_objset = os;
1510 	zc->zc_zap = NULL;
1511 	zc->zc_leaf = NULL;
1512 	zc->zc_zapobj = zapobj;
1513 	zc->zc_serialized = serialized;
1514 	zc->zc_hash = 0;
1515 	zc->zc_cd = 0;
1516 	zc->zc_prefetch = prefetch;
1517 }
1518 void
1519 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1520     uint64_t serialized)
1521 {
1522 	zap_cursor_init_impl(zc, os, zapobj, serialized, B_TRUE);
1523 }
1524 
1525 /*
1526  * Initialize a cursor at the beginning of the ZAP object.  The entire
1527  * ZAP object will be prefetched.
1528  */
1529 void
1530 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1531 {
1532 	zap_cursor_init_impl(zc, os, zapobj, 0, B_TRUE);
1533 }
1534 
1535 /*
1536  * Initialize a cursor at the beginning, but request that we not prefetch
1537  * the entire ZAP object.
1538  */
1539 void
1540 zap_cursor_init_noprefetch(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1541 {
1542 	zap_cursor_init_impl(zc, os, zapobj, 0, B_FALSE);
1543 }
1544 
1545 void
1546 zap_cursor_fini(zap_cursor_t *zc)
1547 {
1548 	if (zc->zc_zap) {
1549 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1550 		zap_unlockdir(zc->zc_zap, NULL);
1551 		zc->zc_zap = NULL;
1552 	}
1553 	if (zc->zc_leaf) {
1554 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1555 		zap_put_leaf(zc->zc_leaf);
1556 		zc->zc_leaf = NULL;
1557 	}
1558 	zc->zc_objset = NULL;
1559 }
1560 
1561 uint64_t
1562 zap_cursor_serialize(zap_cursor_t *zc)
1563 {
1564 	if (zc->zc_hash == -1ULL)
1565 		return (-1ULL);
1566 	if (zc->zc_zap == NULL)
1567 		return (zc->zc_serialized);
1568 	ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1569 	ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1570 
1571 	/*
1572 	 * We want to keep the high 32 bits of the cursor zero if we can, so
1573 	 * that 32-bit programs can access this.  So usually use a small
1574 	 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1575 	 * of the cursor.
1576 	 *
1577 	 * [ collision differentiator | zap_hashbits()-bit hash value ]
1578 	 */
1579 	return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1580 	    ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1581 }
1582 
1583 int
1584 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1585 {
1586 	int err;
1587 
1588 	if (zc->zc_hash == -1ULL)
1589 		return (SET_ERROR(ENOENT));
1590 
1591 	if (zc->zc_zap == NULL) {
1592 		int hb;
1593 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1594 		    RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
1595 		if (err != 0)
1596 			return (err);
1597 
1598 		/*
1599 		 * To support zap_cursor_init_serialized, advance, retrieve,
1600 		 * we must add to the existing zc_cd, which may already
1601 		 * be 1 due to the zap_cursor_advance.
1602 		 */
1603 		ASSERT(zc->zc_hash == 0);
1604 		hb = zap_hashbits(zc->zc_zap);
1605 		zc->zc_hash = zc->zc_serialized << (64 - hb);
1606 		zc->zc_cd += zc->zc_serialized >> hb;
1607 		if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1608 			zc->zc_cd = 0;
1609 	} else {
1610 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1611 	}
1612 	if (!zc->zc_zap->zap_ismicro) {
1613 		err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1614 	} else {
1615 		zfs_btree_index_t idx;
1616 		mzap_ent_t mze_tofind;
1617 
1618 		mze_tofind.mze_hash = zc->zc_hash >> 32;
1619 		mze_tofind.mze_cd = zc->zc_cd;
1620 
1621 		mzap_ent_t *mze = zfs_btree_find(&zc->zc_zap->zap_m.zap_tree,
1622 		    &mze_tofind, &idx);
1623 		if (mze == NULL) {
1624 			mze = zfs_btree_next(&zc->zc_zap->zap_m.zap_tree,
1625 			    &idx, &idx);
1626 		}
1627 		if (mze) {
1628 			mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1629 			ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1630 			za->za_normalization_conflict =
1631 			    mzap_normalization_conflict(zc->zc_zap, NULL,
1632 			    mze, &idx);
1633 			za->za_integer_length = 8;
1634 			za->za_num_integers = 1;
1635 			za->za_first_integer = mzep->mze_value;
1636 			(void) strlcpy(za->za_name, mzep->mze_name,
1637 			    sizeof (za->za_name));
1638 			zc->zc_hash = (uint64_t)mze->mze_hash << 32;
1639 			zc->zc_cd = mze->mze_cd;
1640 			err = 0;
1641 		} else {
1642 			zc->zc_hash = -1ULL;
1643 			err = SET_ERROR(ENOENT);
1644 		}
1645 	}
1646 	rw_exit(&zc->zc_zap->zap_rwlock);
1647 	return (err);
1648 }
1649 
1650 void
1651 zap_cursor_advance(zap_cursor_t *zc)
1652 {
1653 	if (zc->zc_hash == -1ULL)
1654 		return;
1655 	zc->zc_cd++;
1656 }
1657 
1658 int
1659 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1660 {
1661 	zap_t *zap;
1662 
1663 	int err =
1664 	    zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1665 	if (err != 0)
1666 		return (err);
1667 
1668 	memset(zs, 0, sizeof (zap_stats_t));
1669 
1670 	if (zap->zap_ismicro) {
1671 		zs->zs_blocksize = zap->zap_dbuf->db_size;
1672 		zs->zs_num_entries = zap->zap_m.zap_num_entries;
1673 		zs->zs_num_blocks = 1;
1674 	} else {
1675 		fzap_get_stats(zap, zs);
1676 	}
1677 	zap_unlockdir(zap, FTAG);
1678 	return (0);
1679 }
1680 
1681 #if defined(_KERNEL)
1682 EXPORT_SYMBOL(zap_create);
1683 EXPORT_SYMBOL(zap_create_dnsize);
1684 EXPORT_SYMBOL(zap_create_norm);
1685 EXPORT_SYMBOL(zap_create_norm_dnsize);
1686 EXPORT_SYMBOL(zap_create_flags);
1687 EXPORT_SYMBOL(zap_create_flags_dnsize);
1688 EXPORT_SYMBOL(zap_create_claim);
1689 EXPORT_SYMBOL(zap_create_claim_norm);
1690 EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
1691 EXPORT_SYMBOL(zap_create_hold);
1692 EXPORT_SYMBOL(zap_destroy);
1693 EXPORT_SYMBOL(zap_lookup);
1694 EXPORT_SYMBOL(zap_lookup_by_dnode);
1695 EXPORT_SYMBOL(zap_lookup_norm);
1696 EXPORT_SYMBOL(zap_lookup_uint64);
1697 EXPORT_SYMBOL(zap_contains);
1698 EXPORT_SYMBOL(zap_prefetch);
1699 EXPORT_SYMBOL(zap_prefetch_uint64);
1700 EXPORT_SYMBOL(zap_add);
1701 EXPORT_SYMBOL(zap_add_by_dnode);
1702 EXPORT_SYMBOL(zap_add_uint64);
1703 EXPORT_SYMBOL(zap_update);
1704 EXPORT_SYMBOL(zap_update_uint64);
1705 EXPORT_SYMBOL(zap_length);
1706 EXPORT_SYMBOL(zap_length_uint64);
1707 EXPORT_SYMBOL(zap_remove);
1708 EXPORT_SYMBOL(zap_remove_by_dnode);
1709 EXPORT_SYMBOL(zap_remove_norm);
1710 EXPORT_SYMBOL(zap_remove_uint64);
1711 EXPORT_SYMBOL(zap_count);
1712 EXPORT_SYMBOL(zap_value_search);
1713 EXPORT_SYMBOL(zap_join);
1714 EXPORT_SYMBOL(zap_join_increment);
1715 EXPORT_SYMBOL(zap_add_int);
1716 EXPORT_SYMBOL(zap_remove_int);
1717 EXPORT_SYMBOL(zap_lookup_int);
1718 EXPORT_SYMBOL(zap_increment_int);
1719 EXPORT_SYMBOL(zap_add_int_key);
1720 EXPORT_SYMBOL(zap_lookup_int_key);
1721 EXPORT_SYMBOL(zap_increment);
1722 EXPORT_SYMBOL(zap_cursor_init);
1723 EXPORT_SYMBOL(zap_cursor_fini);
1724 EXPORT_SYMBOL(zap_cursor_retrieve);
1725 EXPORT_SYMBOL(zap_cursor_advance);
1726 EXPORT_SYMBOL(zap_cursor_serialize);
1727 EXPORT_SYMBOL(zap_cursor_init_serialized);
1728 EXPORT_SYMBOL(zap_get_stats);
1729 
1730 /* CSTYLED */
1731 ZFS_MODULE_PARAM(zfs, , zap_micro_max_size, INT, ZMOD_RW,
1732 	"Maximum micro ZAP size, before converting to a fat ZAP, in bytes");
1733 #endif
1734