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