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