xref: /titanic_51/usr/src/uts/common/fs/zfs/zap_micro.c (revision a192e900f6d2b0e1a822e3252c0dfd795ed49d76)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/spa.h>
29 #include <sys/dmu.h>
30 #include <sys/zfs_context.h>
31 #include <sys/zap.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
35 #include <sys/avl.h>
36 
37 
38 static void mzap_upgrade(zap_t *zap, dmu_tx_t *tx);
39 
40 
41 static void
42 mzap_byteswap(mzap_phys_t *buf, size_t size)
43 {
44 	int i, max;
45 	buf->mz_block_type = BSWAP_64(buf->mz_block_type);
46 	buf->mz_salt = BSWAP_64(buf->mz_salt);
47 	max = (size / MZAP_ENT_LEN) - 1;
48 	for (i = 0; i < max; i++) {
49 		buf->mz_chunk[i].mze_value =
50 		    BSWAP_64(buf->mz_chunk[i].mze_value);
51 		buf->mz_chunk[i].mze_cd =
52 		    BSWAP_32(buf->mz_chunk[i].mze_cd);
53 	}
54 }
55 
56 void
57 zap_byteswap(void *buf, size_t size)
58 {
59 	uint64_t block_type;
60 
61 	block_type = *(uint64_t *)buf;
62 
63 	if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
64 		/* ASSERT(magic == ZAP_LEAF_MAGIC); */
65 		mzap_byteswap(buf, size);
66 	} else {
67 		fzap_byteswap(buf, size);
68 	}
69 }
70 
71 static int
72 mze_compare(const void *arg1, const void *arg2)
73 {
74 	const mzap_ent_t *mze1 = arg1;
75 	const mzap_ent_t *mze2 = arg2;
76 
77 	if (mze1->mze_hash > mze2->mze_hash)
78 		return (+1);
79 	if (mze1->mze_hash < mze2->mze_hash)
80 		return (-1);
81 	if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd)
82 		return (+1);
83 	if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd)
84 		return (-1);
85 	return (0);
86 }
87 
88 static void
89 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep)
90 {
91 	mzap_ent_t *mze;
92 
93 	ASSERT(zap->zap_ismicro);
94 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
95 	ASSERT(mzep->mze_cd < ZAP_MAXCD);
96 	ASSERT3U(zap_hash(zap, mzep->mze_name), ==, hash);
97 
98 	mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
99 	mze->mze_chunkid = chunkid;
100 	mze->mze_hash = hash;
101 	mze->mze_phys = *mzep;
102 	avl_add(&zap->zap_m.zap_avl, mze);
103 }
104 
105 static mzap_ent_t *
106 mze_find(zap_t *zap, const char *name, uint64_t hash)
107 {
108 	mzap_ent_t mze_tofind;
109 	mzap_ent_t *mze;
110 	avl_index_t idx;
111 	avl_tree_t *avl = &zap->zap_m.zap_avl;
112 
113 	ASSERT(zap->zap_ismicro);
114 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
115 	ASSERT3U(zap_hash(zap, name), ==, hash);
116 
117 	if (strlen(name) >= sizeof (mze_tofind.mze_phys.mze_name))
118 		return (NULL);
119 
120 	mze_tofind.mze_hash = hash;
121 	mze_tofind.mze_phys.mze_cd = 0;
122 
123 	mze = avl_find(avl, &mze_tofind, &idx);
124 	if (mze == NULL)
125 		mze = avl_nearest(avl, idx, AVL_AFTER);
126 	for (; mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
127 		if (strcmp(name, mze->mze_phys.mze_name) == 0)
128 			return (mze);
129 	}
130 	return (NULL);
131 }
132 
133 static uint32_t
134 mze_find_unused_cd(zap_t *zap, uint64_t hash)
135 {
136 	mzap_ent_t mze_tofind;
137 	mzap_ent_t *mze;
138 	avl_index_t idx;
139 	avl_tree_t *avl = &zap->zap_m.zap_avl;
140 	uint32_t cd;
141 
142 	ASSERT(zap->zap_ismicro);
143 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
144 
145 	mze_tofind.mze_hash = hash;
146 	mze_tofind.mze_phys.mze_cd = 0;
147 
148 	cd = 0;
149 	for (mze = avl_find(avl, &mze_tofind, &idx);
150 	    mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
151 		if (mze->mze_phys.mze_cd != cd)
152 			break;
153 		cd++;
154 	}
155 
156 	return (cd);
157 }
158 
159 static void
160 mze_remove(zap_t *zap, mzap_ent_t *mze)
161 {
162 	ASSERT(zap->zap_ismicro);
163 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
164 
165 	avl_remove(&zap->zap_m.zap_avl, mze);
166 	kmem_free(mze, sizeof (mzap_ent_t));
167 }
168 
169 static void
170 mze_destroy(zap_t *zap)
171 {
172 	mzap_ent_t *mze;
173 	void *avlcookie = NULL;
174 
175 	while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
176 		kmem_free(mze, sizeof (mzap_ent_t));
177 	avl_destroy(&zap->zap_m.zap_avl);
178 }
179 
180 static zap_t *
181 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
182 {
183 	zap_t *winner;
184 	zap_t *zap;
185 	int i;
186 
187 	ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
188 
189 	zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
190 	rw_init(&zap->zap_rwlock, 0, 0, 0);
191 	rw_enter(&zap->zap_rwlock, RW_WRITER);
192 	zap->zap_objset = os;
193 	zap->zap_object = obj;
194 	zap->zap_dbuf = db;
195 
196 	if (((uint64_t *)db->db_data)[0] != ZBT_MICRO) {
197 		mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
198 		zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
199 	} else {
200 		zap->zap_ismicro = TRUE;
201 	}
202 
203 	/*
204 	 * Make sure that zap_ismicro is set before we let others see
205 	 * it, because zap_lockdir() checks zap_ismicro without the lock
206 	 * held.
207 	 */
208 	winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
209 
210 	if (winner != NULL) {
211 		kmem_free(zap, sizeof (zap_t));
212 		return (winner);
213 	}
214 
215 	if (zap->zap_ismicro) {
216 		zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
217 		zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
218 		avl_create(&zap->zap_m.zap_avl, mze_compare,
219 		    sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
220 
221 		for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
222 			mzap_ent_phys_t *mze =
223 			    &zap->zap_m.zap_phys->mz_chunk[i];
224 			if (mze->mze_name[0]) {
225 				zap->zap_m.zap_num_entries++;
226 				mze_insert(zap, i,
227 				    zap_hash(zap, mze->mze_name), mze);
228 			}
229 		}
230 	} else {
231 		zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
232 
233 		ASSERT3U(sizeof (struct zap_leaf_header), ==,
234 		    2*ZAP_LEAF_CHUNKSIZE);
235 
236 		/*
237 		 * The embedded pointer table should not overlap the
238 		 * other members.
239 		 */
240 		ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
241 		    &zap->zap_f.zap_phys->zap_salt);
242 
243 		/*
244 		 * The embedded pointer table should end at the end of
245 		 * the block
246 		 */
247 		ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
248 		    1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
249 		    (uintptr_t)zap->zap_f.zap_phys, ==,
250 		    zap->zap_dbuf->db_size);
251 	}
252 	rw_exit(&zap->zap_rwlock);
253 	return (zap);
254 }
255 
256 int
257 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
258     krw_t lti, int fatreader, zap_t **zapp)
259 {
260 	zap_t *zap;
261 	dmu_buf_t *db;
262 	krw_t lt;
263 	int err;
264 
265 	*zapp = NULL;
266 
267 	err = dmu_buf_hold(os, obj, 0, NULL, &db);
268 	if (err)
269 		return (err);
270 
271 #ifdef ZFS_DEBUG
272 	{
273 		dmu_object_info_t doi;
274 		dmu_object_info_from_db(db, &doi);
275 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
276 	}
277 #endif
278 
279 	zap = dmu_buf_get_user(db);
280 	if (zap == NULL)
281 		zap = mzap_open(os, obj, db);
282 
283 	/*
284 	 * We're checking zap_ismicro without the lock held, in order to
285 	 * tell what type of lock we want.  Once we have some sort of
286 	 * lock, see if it really is the right type.  In practice this
287 	 * can only be different if it was upgraded from micro to fat,
288 	 * and micro wanted WRITER but fat only needs READER.
289 	 */
290 	lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
291 	rw_enter(&zap->zap_rwlock, lt);
292 	if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
293 		/* it was upgraded, now we only need reader */
294 		ASSERT(lt == RW_WRITER);
295 		ASSERT(RW_READER ==
296 		    (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
297 		rw_downgrade(&zap->zap_rwlock);
298 		lt = RW_READER;
299 	}
300 
301 	zap->zap_objset = os;
302 
303 	if (lt == RW_WRITER)
304 		dmu_buf_will_dirty(db, tx);
305 
306 	ASSERT3P(zap->zap_dbuf, ==, db);
307 
308 	ASSERT(!zap->zap_ismicro ||
309 	    zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
310 	if (zap->zap_ismicro && tx &&
311 	    zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
312 		uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
313 		if (newsz > MZAP_MAX_BLKSZ) {
314 			dprintf("upgrading obj %llu: num_entries=%u\n",
315 			    obj, zap->zap_m.zap_num_entries);
316 			mzap_upgrade(zap, tx);
317 			*zapp = zap;
318 			return (0);
319 		}
320 		err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
321 		ASSERT3U(err, ==, 0);
322 		zap->zap_m.zap_num_chunks =
323 		    db->db_size / MZAP_ENT_LEN - 1;
324 	}
325 
326 	*zapp = zap;
327 	return (0);
328 }
329 
330 void
331 zap_unlockdir(zap_t *zap)
332 {
333 	rw_exit(&zap->zap_rwlock);
334 	dmu_buf_rele(zap->zap_dbuf, NULL);
335 }
336 
337 static void
338 mzap_upgrade(zap_t *zap, dmu_tx_t *tx)
339 {
340 	mzap_phys_t *mzp;
341 	int i, sz, nchunks, err;
342 
343 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
344 
345 	sz = zap->zap_dbuf->db_size;
346 	mzp = kmem_alloc(sz, KM_SLEEP);
347 	bcopy(zap->zap_dbuf->db_data, mzp, sz);
348 	nchunks = zap->zap_m.zap_num_chunks;
349 
350 	err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
351 	    1ULL << fzap_default_block_shift, 0, tx);
352 	ASSERT(err == 0);
353 
354 	dprintf("upgrading obj=%llu with %u chunks\n",
355 	    zap->zap_object, nchunks);
356 	mze_destroy(zap);
357 
358 	fzap_upgrade(zap, tx);
359 
360 	for (i = 0; i < nchunks; i++) {
361 		int err;
362 		mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
363 		if (mze->mze_name[0] == 0)
364 			continue;
365 		dprintf("adding %s=%llu\n",
366 		    mze->mze_name, mze->mze_value);
367 		err = fzap_add_cd(zap,
368 		    mze->mze_name, 8, 1, &mze->mze_value,
369 		    mze->mze_cd, tx);
370 		ASSERT3U(err, ==, 0);
371 	}
372 	kmem_free(mzp, sz);
373 }
374 
375 uint64_t
376 zap_hash(zap_t *zap, const char *name)
377 {
378 	const uint8_t *cp;
379 	uint8_t c;
380 	uint64_t crc = zap->zap_salt;
381 
382 	ASSERT(crc != 0);
383 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
384 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
385 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF];
386 
387 	/*
388 	 * Only use 28 bits, since we need 4 bits in the cookie for the
389 	 * collision differentiator.  We MUST use the high bits, since
390 	 * those are the onces that we first pay attention to when
391 	 * chosing the bucket.
392 	 */
393 	crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
394 
395 	return (crc);
396 }
397 
398 
399 static void
400 mzap_create_impl(objset_t *os, uint64_t obj, dmu_tx_t *tx)
401 {
402 	dmu_buf_t *db;
403 	mzap_phys_t *zp;
404 
405 	VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
406 
407 #ifdef ZFS_DEBUG
408 	{
409 		dmu_object_info_t doi;
410 		dmu_object_info_from_db(db, &doi);
411 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
412 	}
413 #endif
414 
415 	dmu_buf_will_dirty(db, tx);
416 	zp = db->db_data;
417 	zp->mz_block_type = ZBT_MICRO;
418 	zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
419 	ASSERT(zp->mz_salt != 0);
420 	dmu_buf_rele(db, FTAG);
421 }
422 
423 int
424 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
425     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
426 {
427 	int err;
428 
429 	err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
430 	if (err != 0)
431 		return (err);
432 	mzap_create_impl(os, obj, tx);
433 	return (0);
434 }
435 
436 uint64_t
437 zap_create(objset_t *os, dmu_object_type_t ot,
438     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
439 {
440 	uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
441 
442 	mzap_create_impl(os, obj, tx);
443 	return (obj);
444 }
445 
446 int
447 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
448 {
449 	/*
450 	 * dmu_object_free will free the object number and free the
451 	 * data.  Freeing the data will cause our pageout function to be
452 	 * called, which will destroy our data (zap_leaf_t's and zap_t).
453 	 */
454 
455 	return (dmu_object_free(os, zapobj, tx));
456 }
457 
458 _NOTE(ARGSUSED(0))
459 void
460 zap_evict(dmu_buf_t *db, void *vzap)
461 {
462 	zap_t *zap = vzap;
463 
464 	rw_destroy(&zap->zap_rwlock);
465 
466 	if (zap->zap_ismicro)
467 		mze_destroy(zap);
468 
469 	kmem_free(zap, sizeof (zap_t));
470 }
471 
472 int
473 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
474 {
475 	zap_t *zap;
476 	int err;
477 
478 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
479 	if (err)
480 		return (err);
481 	if (!zap->zap_ismicro) {
482 		err = fzap_count(zap, count);
483 	} else {
484 		*count = zap->zap_m.zap_num_entries;
485 	}
486 	zap_unlockdir(zap);
487 	return (err);
488 }
489 
490 /*
491  * Routines for maniplulating attributes.
492  */
493 
494 int
495 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
496     uint64_t integer_size, uint64_t num_integers, void *buf)
497 {
498 	zap_t *zap;
499 	int err;
500 	mzap_ent_t *mze;
501 
502 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
503 	if (err)
504 		return (err);
505 	if (!zap->zap_ismicro) {
506 		err = fzap_lookup(zap, name,
507 		    integer_size, num_integers, buf);
508 	} else {
509 		mze = mze_find(zap, name, zap_hash(zap, name));
510 		if (mze == NULL) {
511 			err = ENOENT;
512 		} else {
513 			if (num_integers < 1)
514 				err = EOVERFLOW;
515 			else if (integer_size != 8)
516 				err = EINVAL;
517 			else
518 				*(uint64_t *)buf = mze->mze_phys.mze_value;
519 		}
520 	}
521 	zap_unlockdir(zap);
522 	return (err);
523 }
524 
525 int
526 zap_length(objset_t *os, uint64_t zapobj, const char *name,
527     uint64_t *integer_size, uint64_t *num_integers)
528 {
529 	zap_t *zap;
530 	int err;
531 	mzap_ent_t *mze;
532 
533 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
534 	if (err)
535 		return (err);
536 	if (!zap->zap_ismicro) {
537 		err = fzap_length(zap, name, integer_size, num_integers);
538 	} else {
539 		mze = mze_find(zap, name, zap_hash(zap, name));
540 		if (mze == NULL) {
541 			err = ENOENT;
542 		} else {
543 			if (integer_size)
544 				*integer_size = 8;
545 			if (num_integers)
546 				*num_integers = 1;
547 		}
548 	}
549 	zap_unlockdir(zap);
550 	return (err);
551 }
552 
553 static void
554 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value)
555 {
556 	int i;
557 	int start = zap->zap_m.zap_alloc_next;
558 	uint32_t cd;
559 
560 	dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value);
561 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
562 
563 #ifdef ZFS_DEBUG
564 	for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
565 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
566 		ASSERT(strcmp(name, mze->mze_name) != 0);
567 	}
568 #endif
569 
570 	cd = mze_find_unused_cd(zap, hash);
571 	/* given the limited size of the microzap, this can't happen */
572 	ASSERT(cd != ZAP_MAXCD);
573 
574 again:
575 	for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
576 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
577 		if (mze->mze_name[0] == 0) {
578 			mze->mze_value = value;
579 			mze->mze_cd = cd;
580 			(void) strcpy(mze->mze_name, name);
581 			zap->zap_m.zap_num_entries++;
582 			zap->zap_m.zap_alloc_next = i+1;
583 			if (zap->zap_m.zap_alloc_next ==
584 			    zap->zap_m.zap_num_chunks)
585 				zap->zap_m.zap_alloc_next = 0;
586 			mze_insert(zap, i, hash, mze);
587 			return;
588 		}
589 	}
590 	if (start != 0) {
591 		start = 0;
592 		goto again;
593 	}
594 	ASSERT(!"out of entries!");
595 }
596 
597 int
598 zap_add(objset_t *os, uint64_t zapobj, const char *name,
599     int integer_size, uint64_t num_integers,
600     const void *val, dmu_tx_t *tx)
601 {
602 	zap_t *zap;
603 	int err;
604 	mzap_ent_t *mze;
605 	const uint64_t *intval = val;
606 	uint64_t hash;
607 
608 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
609 	if (err)
610 		return (err);
611 	if (!zap->zap_ismicro) {
612 		err = fzap_add(zap, name, integer_size, num_integers, val, tx);
613 	} else if (integer_size != 8 || num_integers != 1 ||
614 	    strlen(name) >= MZAP_NAME_LEN) {
615 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
616 		    zapobj, integer_size, num_integers, name);
617 		mzap_upgrade(zap, tx);
618 		err = fzap_add(zap, name, integer_size, num_integers, val, tx);
619 	} else {
620 		hash = zap_hash(zap, name);
621 		mze = mze_find(zap, name, hash);
622 		if (mze != NULL) {
623 			err = EEXIST;
624 		} else {
625 			mzap_addent(zap, name, hash, *intval);
626 		}
627 	}
628 	zap_unlockdir(zap);
629 	return (err);
630 }
631 
632 int
633 zap_update(objset_t *os, uint64_t zapobj, const char *name,
634     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
635 {
636 	zap_t *zap;
637 	mzap_ent_t *mze;
638 	const uint64_t *intval = val;
639 	uint64_t hash;
640 	int err;
641 
642 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
643 	if (err)
644 		return (err);
645 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
646 	if (!zap->zap_ismicro) {
647 		err = fzap_update(zap, name,
648 		    integer_size, num_integers, val, tx);
649 	} else if (integer_size != 8 || num_integers != 1 ||
650 	    strlen(name) >= MZAP_NAME_LEN) {
651 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
652 		    zapobj, integer_size, num_integers, name);
653 		mzap_upgrade(zap, tx);
654 		err = fzap_update(zap, name,
655 		    integer_size, num_integers, val, tx);
656 	} else {
657 		hash = zap_hash(zap, name);
658 		mze = mze_find(zap, name, hash);
659 		if (mze != NULL) {
660 			mze->mze_phys.mze_value = *intval;
661 			zap->zap_m.zap_phys->mz_chunk
662 			    [mze->mze_chunkid].mze_value = *intval;
663 		} else {
664 			mzap_addent(zap, name, hash, *intval);
665 		}
666 	}
667 	zap_unlockdir(zap);
668 	return (err);
669 }
670 
671 int
672 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
673 {
674 	zap_t *zap;
675 	int err;
676 	mzap_ent_t *mze;
677 
678 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
679 	if (err)
680 		return (err);
681 	if (!zap->zap_ismicro) {
682 		err = fzap_remove(zap, name, tx);
683 	} else {
684 		mze = mze_find(zap, name, zap_hash(zap, name));
685 		if (mze == NULL) {
686 			dprintf("fail: %s\n", name);
687 			err = ENOENT;
688 		} else {
689 			dprintf("success: %s\n", name);
690 			zap->zap_m.zap_num_entries--;
691 			bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
692 			    sizeof (mzap_ent_phys_t));
693 			mze_remove(zap, mze);
694 		}
695 	}
696 	zap_unlockdir(zap);
697 	return (err);
698 }
699 
700 
701 /*
702  * Routines for iterating over the attributes.
703  */
704 
705 /*
706  * We want to keep the high 32 bits of the cursor zero if we can, so
707  * that 32-bit programs can access this.  So use a small hash value so
708  * we can fit 4 bits of cd into the 32-bit cursor.
709  *
710  * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ]
711  */
712 void
713 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
714     uint64_t serialized)
715 {
716 	zc->zc_objset = os;
717 	zc->zc_zap = NULL;
718 	zc->zc_leaf = NULL;
719 	zc->zc_zapobj = zapobj;
720 	if (serialized == -1ULL) {
721 		zc->zc_hash = -1ULL;
722 		zc->zc_cd = 0;
723 	} else {
724 		zc->zc_hash = serialized << (64-ZAP_HASHBITS);
725 		zc->zc_cd = serialized >> ZAP_HASHBITS;
726 		if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */
727 			zc->zc_cd = 0;
728 	}
729 }
730 
731 void
732 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
733 {
734 	zap_cursor_init_serialized(zc, os, zapobj, 0);
735 }
736 
737 void
738 zap_cursor_fini(zap_cursor_t *zc)
739 {
740 	if (zc->zc_zap) {
741 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
742 		zap_unlockdir(zc->zc_zap);
743 		zc->zc_zap = NULL;
744 	}
745 	if (zc->zc_leaf) {
746 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
747 		zap_put_leaf(zc->zc_leaf);
748 		zc->zc_leaf = NULL;
749 	}
750 	zc->zc_objset = NULL;
751 }
752 
753 uint64_t
754 zap_cursor_serialize(zap_cursor_t *zc)
755 {
756 	if (zc->zc_hash == -1ULL)
757 		return (-1ULL);
758 	ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0);
759 	ASSERT(zc->zc_cd < ZAP_MAXCD);
760 	return ((zc->zc_hash >> (64-ZAP_HASHBITS)) |
761 	    ((uint64_t)zc->zc_cd << ZAP_HASHBITS));
762 }
763 
764 int
765 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
766 {
767 	int err;
768 	avl_index_t idx;
769 	mzap_ent_t mze_tofind;
770 	mzap_ent_t *mze;
771 
772 	if (zc->zc_hash == -1ULL)
773 		return (ENOENT);
774 
775 	if (zc->zc_zap == NULL) {
776 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
777 		    RW_READER, TRUE, &zc->zc_zap);
778 		if (err)
779 			return (err);
780 	} else {
781 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
782 	}
783 	if (!zc->zc_zap->zap_ismicro) {
784 		err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
785 	} else {
786 		err = ENOENT;
787 
788 		mze_tofind.mze_hash = zc->zc_hash;
789 		mze_tofind.mze_phys.mze_cd = zc->zc_cd;
790 
791 		mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
792 		ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys,
793 		    &zc->zc_zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
794 		    sizeof (mze->mze_phys)));
795 		if (mze == NULL) {
796 			mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
797 			    idx, AVL_AFTER);
798 		}
799 		if (mze) {
800 			za->za_integer_length = 8;
801 			za->za_num_integers = 1;
802 			za->za_first_integer = mze->mze_phys.mze_value;
803 			(void) strcpy(za->za_name, mze->mze_phys.mze_name);
804 			zc->zc_hash = mze->mze_hash;
805 			zc->zc_cd = mze->mze_phys.mze_cd;
806 			err = 0;
807 		} else {
808 			zc->zc_hash = -1ULL;
809 		}
810 	}
811 	rw_exit(&zc->zc_zap->zap_rwlock);
812 	return (err);
813 }
814 
815 void
816 zap_cursor_advance(zap_cursor_t *zc)
817 {
818 	if (zc->zc_hash == -1ULL)
819 		return;
820 	zc->zc_cd++;
821 	if (zc->zc_cd >= ZAP_MAXCD) {
822 		zc->zc_cd = 0;
823 		zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS);
824 		if (zc->zc_hash == 0) /* EOF */
825 			zc->zc_hash = -1ULL;
826 	}
827 }
828 
829 int
830 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
831 {
832 	int err;
833 	zap_t *zap;
834 
835 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
836 	if (err)
837 		return (err);
838 
839 	bzero(zs, sizeof (zap_stats_t));
840 
841 	if (zap->zap_ismicro) {
842 		zs->zs_blocksize = zap->zap_dbuf->db_size;
843 		zs->zs_num_entries = zap->zap_m.zap_num_entries;
844 		zs->zs_num_blocks = 1;
845 	} else {
846 		fzap_get_stats(zap, zs);
847 	}
848 	zap_unlockdir(zap);
849 	return (0);
850 }
851