xref: /titanic_50/usr/src/uts/common/fs/zfs/zap_micro.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 2007 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 		rw_exit(&zap->zap_rwlock);
212 		rw_destroy(&zap->zap_rwlock);
213 		if (!zap->zap_ismicro)
214 			mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
215 		kmem_free(zap, sizeof (zap_t));
216 		return (winner);
217 	}
218 
219 	if (zap->zap_ismicro) {
220 		zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
221 		zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
222 		avl_create(&zap->zap_m.zap_avl, mze_compare,
223 		    sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
224 
225 		for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
226 			mzap_ent_phys_t *mze =
227 			    &zap->zap_m.zap_phys->mz_chunk[i];
228 			if (mze->mze_name[0]) {
229 				zap->zap_m.zap_num_entries++;
230 				mze_insert(zap, i,
231 				    zap_hash(zap, mze->mze_name), mze);
232 			}
233 		}
234 	} else {
235 		zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
236 
237 		ASSERT3U(sizeof (struct zap_leaf_header), ==,
238 		    2*ZAP_LEAF_CHUNKSIZE);
239 
240 		/*
241 		 * The embedded pointer table should not overlap the
242 		 * other members.
243 		 */
244 		ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
245 		    &zap->zap_f.zap_phys->zap_salt);
246 
247 		/*
248 		 * The embedded pointer table should end at the end of
249 		 * the block
250 		 */
251 		ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
252 		    1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
253 		    (uintptr_t)zap->zap_f.zap_phys, ==,
254 		    zap->zap_dbuf->db_size);
255 	}
256 	rw_exit(&zap->zap_rwlock);
257 	return (zap);
258 }
259 
260 int
261 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
262     krw_t lti, int fatreader, zap_t **zapp)
263 {
264 	zap_t *zap;
265 	dmu_buf_t *db;
266 	krw_t lt;
267 	int err;
268 
269 	*zapp = NULL;
270 
271 	err = dmu_buf_hold(os, obj, 0, NULL, &db);
272 	if (err)
273 		return (err);
274 
275 #ifdef ZFS_DEBUG
276 	{
277 		dmu_object_info_t doi;
278 		dmu_object_info_from_db(db, &doi);
279 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
280 	}
281 #endif
282 
283 	zap = dmu_buf_get_user(db);
284 	if (zap == NULL)
285 		zap = mzap_open(os, obj, db);
286 
287 	/*
288 	 * We're checking zap_ismicro without the lock held, in order to
289 	 * tell what type of lock we want.  Once we have some sort of
290 	 * lock, see if it really is the right type.  In practice this
291 	 * can only be different if it was upgraded from micro to fat,
292 	 * and micro wanted WRITER but fat only needs READER.
293 	 */
294 	lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
295 	rw_enter(&zap->zap_rwlock, lt);
296 	if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
297 		/* it was upgraded, now we only need reader */
298 		ASSERT(lt == RW_WRITER);
299 		ASSERT(RW_READER ==
300 		    (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
301 		rw_downgrade(&zap->zap_rwlock);
302 		lt = RW_READER;
303 	}
304 
305 	zap->zap_objset = os;
306 
307 	if (lt == RW_WRITER)
308 		dmu_buf_will_dirty(db, tx);
309 
310 	ASSERT3P(zap->zap_dbuf, ==, db);
311 
312 	ASSERT(!zap->zap_ismicro ||
313 	    zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
314 	if (zap->zap_ismicro && tx &&
315 	    zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
316 		uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
317 		if (newsz > MZAP_MAX_BLKSZ) {
318 			dprintf("upgrading obj %llu: num_entries=%u\n",
319 			    obj, zap->zap_m.zap_num_entries);
320 			mzap_upgrade(zap, tx);
321 			*zapp = zap;
322 			return (0);
323 		}
324 		err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
325 		ASSERT3U(err, ==, 0);
326 		zap->zap_m.zap_num_chunks =
327 		    db->db_size / MZAP_ENT_LEN - 1;
328 	}
329 
330 	*zapp = zap;
331 	return (0);
332 }
333 
334 void
335 zap_unlockdir(zap_t *zap)
336 {
337 	rw_exit(&zap->zap_rwlock);
338 	dmu_buf_rele(zap->zap_dbuf, NULL);
339 }
340 
341 static void
342 mzap_upgrade(zap_t *zap, dmu_tx_t *tx)
343 {
344 	mzap_phys_t *mzp;
345 	int i, sz, nchunks, err;
346 
347 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
348 
349 	sz = zap->zap_dbuf->db_size;
350 	mzp = kmem_alloc(sz, KM_SLEEP);
351 	bcopy(zap->zap_dbuf->db_data, mzp, sz);
352 	nchunks = zap->zap_m.zap_num_chunks;
353 
354 	err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
355 	    1ULL << fzap_default_block_shift, 0, tx);
356 	ASSERT(err == 0);
357 
358 	dprintf("upgrading obj=%llu with %u chunks\n",
359 	    zap->zap_object, nchunks);
360 	mze_destroy(zap);
361 
362 	fzap_upgrade(zap, tx);
363 
364 	for (i = 0; i < nchunks; i++) {
365 		int err;
366 		mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
367 		if (mze->mze_name[0] == 0)
368 			continue;
369 		dprintf("adding %s=%llu\n",
370 		    mze->mze_name, mze->mze_value);
371 		err = fzap_add_cd(zap,
372 		    mze->mze_name, 8, 1, &mze->mze_value,
373 		    mze->mze_cd, tx);
374 		ASSERT3U(err, ==, 0);
375 	}
376 	kmem_free(mzp, sz);
377 }
378 
379 uint64_t
380 zap_hash(zap_t *zap, const char *name)
381 {
382 	const uint8_t *cp;
383 	uint8_t c;
384 	uint64_t crc = zap->zap_salt;
385 
386 	ASSERT(crc != 0);
387 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
388 	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
389 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF];
390 
391 	/*
392 	 * Only use 28 bits, since we need 4 bits in the cookie for the
393 	 * collision differentiator.  We MUST use the high bits, since
394 	 * those are the onces that we first pay attention to when
395 	 * chosing the bucket.
396 	 */
397 	crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
398 
399 	return (crc);
400 }
401 
402 
403 static void
404 mzap_create_impl(objset_t *os, uint64_t obj, dmu_tx_t *tx)
405 {
406 	dmu_buf_t *db;
407 	mzap_phys_t *zp;
408 
409 	VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
410 
411 #ifdef ZFS_DEBUG
412 	{
413 		dmu_object_info_t doi;
414 		dmu_object_info_from_db(db, &doi);
415 		ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
416 	}
417 #endif
418 
419 	dmu_buf_will_dirty(db, tx);
420 	zp = db->db_data;
421 	zp->mz_block_type = ZBT_MICRO;
422 	zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
423 	ASSERT(zp->mz_salt != 0);
424 	dmu_buf_rele(db, FTAG);
425 }
426 
427 int
428 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
429     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
430 {
431 	int err;
432 
433 	err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
434 	if (err != 0)
435 		return (err);
436 	mzap_create_impl(os, obj, tx);
437 	return (0);
438 }
439 
440 uint64_t
441 zap_create(objset_t *os, dmu_object_type_t ot,
442     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
443 {
444 	uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
445 
446 	mzap_create_impl(os, obj, tx);
447 	return (obj);
448 }
449 
450 int
451 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
452 {
453 	/*
454 	 * dmu_object_free will free the object number and free the
455 	 * data.  Freeing the data will cause our pageout function to be
456 	 * called, which will destroy our data (zap_leaf_t's and zap_t).
457 	 */
458 
459 	return (dmu_object_free(os, zapobj, tx));
460 }
461 
462 _NOTE(ARGSUSED(0))
463 void
464 zap_evict(dmu_buf_t *db, void *vzap)
465 {
466 	zap_t *zap = vzap;
467 
468 	rw_destroy(&zap->zap_rwlock);
469 
470 	if (zap->zap_ismicro)
471 		mze_destroy(zap);
472 	else
473 		mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
474 
475 	kmem_free(zap, sizeof (zap_t));
476 }
477 
478 int
479 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
480 {
481 	zap_t *zap;
482 	int err;
483 
484 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
485 	if (err)
486 		return (err);
487 	if (!zap->zap_ismicro) {
488 		err = fzap_count(zap, count);
489 	} else {
490 		*count = zap->zap_m.zap_num_entries;
491 	}
492 	zap_unlockdir(zap);
493 	return (err);
494 }
495 
496 /*
497  * Routines for maniplulating attributes.
498  */
499 
500 int
501 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
502     uint64_t integer_size, uint64_t num_integers, void *buf)
503 {
504 	zap_t *zap;
505 	int err;
506 	mzap_ent_t *mze;
507 
508 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
509 	if (err)
510 		return (err);
511 	if (!zap->zap_ismicro) {
512 		err = fzap_lookup(zap, name,
513 		    integer_size, num_integers, buf);
514 	} else {
515 		mze = mze_find(zap, name, zap_hash(zap, name));
516 		if (mze == NULL) {
517 			err = ENOENT;
518 		} else {
519 			if (num_integers < 1)
520 				err = EOVERFLOW;
521 			else if (integer_size != 8)
522 				err = EINVAL;
523 			else
524 				*(uint64_t *)buf = mze->mze_phys.mze_value;
525 		}
526 	}
527 	zap_unlockdir(zap);
528 	return (err);
529 }
530 
531 int
532 zap_length(objset_t *os, uint64_t zapobj, const char *name,
533     uint64_t *integer_size, uint64_t *num_integers)
534 {
535 	zap_t *zap;
536 	int err;
537 	mzap_ent_t *mze;
538 
539 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
540 	if (err)
541 		return (err);
542 	if (!zap->zap_ismicro) {
543 		err = fzap_length(zap, name, integer_size, num_integers);
544 	} else {
545 		mze = mze_find(zap, name, zap_hash(zap, name));
546 		if (mze == NULL) {
547 			err = ENOENT;
548 		} else {
549 			if (integer_size)
550 				*integer_size = 8;
551 			if (num_integers)
552 				*num_integers = 1;
553 		}
554 	}
555 	zap_unlockdir(zap);
556 	return (err);
557 }
558 
559 static void
560 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value)
561 {
562 	int i;
563 	int start = zap->zap_m.zap_alloc_next;
564 	uint32_t cd;
565 
566 	dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value);
567 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
568 
569 #ifdef ZFS_DEBUG
570 	for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
571 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
572 		ASSERT(strcmp(name, mze->mze_name) != 0);
573 	}
574 #endif
575 
576 	cd = mze_find_unused_cd(zap, hash);
577 	/* given the limited size of the microzap, this can't happen */
578 	ASSERT(cd != ZAP_MAXCD);
579 
580 again:
581 	for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
582 		mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
583 		if (mze->mze_name[0] == 0) {
584 			mze->mze_value = value;
585 			mze->mze_cd = cd;
586 			(void) strcpy(mze->mze_name, name);
587 			zap->zap_m.zap_num_entries++;
588 			zap->zap_m.zap_alloc_next = i+1;
589 			if (zap->zap_m.zap_alloc_next ==
590 			    zap->zap_m.zap_num_chunks)
591 				zap->zap_m.zap_alloc_next = 0;
592 			mze_insert(zap, i, hash, mze);
593 			return;
594 		}
595 	}
596 	if (start != 0) {
597 		start = 0;
598 		goto again;
599 	}
600 	ASSERT(!"out of entries!");
601 }
602 
603 int
604 zap_add(objset_t *os, uint64_t zapobj, const char *name,
605     int integer_size, uint64_t num_integers,
606     const void *val, dmu_tx_t *tx)
607 {
608 	zap_t *zap;
609 	int err;
610 	mzap_ent_t *mze;
611 	const uint64_t *intval = val;
612 	uint64_t hash;
613 
614 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
615 	if (err)
616 		return (err);
617 	if (!zap->zap_ismicro) {
618 		err = fzap_add(zap, name, integer_size, num_integers, val, tx);
619 	} else if (integer_size != 8 || num_integers != 1 ||
620 	    strlen(name) >= MZAP_NAME_LEN) {
621 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
622 		    zapobj, integer_size, num_integers, name);
623 		mzap_upgrade(zap, tx);
624 		err = fzap_add(zap, name, integer_size, num_integers, val, tx);
625 	} else {
626 		hash = zap_hash(zap, name);
627 		mze = mze_find(zap, name, hash);
628 		if (mze != NULL) {
629 			err = EEXIST;
630 		} else {
631 			mzap_addent(zap, name, hash, *intval);
632 		}
633 	}
634 	zap_unlockdir(zap);
635 	return (err);
636 }
637 
638 int
639 zap_update(objset_t *os, uint64_t zapobj, const char *name,
640     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
641 {
642 	zap_t *zap;
643 	mzap_ent_t *mze;
644 	const uint64_t *intval = val;
645 	uint64_t hash;
646 	int err;
647 
648 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
649 	if (err)
650 		return (err);
651 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
652 	if (!zap->zap_ismicro) {
653 		err = fzap_update(zap, name,
654 		    integer_size, num_integers, val, tx);
655 	} else if (integer_size != 8 || num_integers != 1 ||
656 	    strlen(name) >= MZAP_NAME_LEN) {
657 		dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
658 		    zapobj, integer_size, num_integers, name);
659 		mzap_upgrade(zap, tx);
660 		err = fzap_update(zap, name,
661 		    integer_size, num_integers, val, tx);
662 	} else {
663 		hash = zap_hash(zap, name);
664 		mze = mze_find(zap, name, hash);
665 		if (mze != NULL) {
666 			mze->mze_phys.mze_value = *intval;
667 			zap->zap_m.zap_phys->mz_chunk
668 			    [mze->mze_chunkid].mze_value = *intval;
669 		} else {
670 			mzap_addent(zap, name, hash, *intval);
671 		}
672 	}
673 	zap_unlockdir(zap);
674 	return (err);
675 }
676 
677 int
678 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
679 {
680 	zap_t *zap;
681 	int err;
682 	mzap_ent_t *mze;
683 
684 	err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
685 	if (err)
686 		return (err);
687 	if (!zap->zap_ismicro) {
688 		err = fzap_remove(zap, name, tx);
689 	} else {
690 		mze = mze_find(zap, name, zap_hash(zap, name));
691 		if (mze == NULL) {
692 			dprintf("fail: %s\n", name);
693 			err = ENOENT;
694 		} else {
695 			dprintf("success: %s\n", name);
696 			zap->zap_m.zap_num_entries--;
697 			bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
698 			    sizeof (mzap_ent_phys_t));
699 			mze_remove(zap, mze);
700 		}
701 	}
702 	zap_unlockdir(zap);
703 	return (err);
704 }
705 
706 
707 /*
708  * Routines for iterating over the attributes.
709  */
710 
711 /*
712  * We want to keep the high 32 bits of the cursor zero if we can, so
713  * that 32-bit programs can access this.  So use a small hash value so
714  * we can fit 4 bits of cd into the 32-bit cursor.
715  *
716  * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ]
717  */
718 void
719 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
720     uint64_t serialized)
721 {
722 	zc->zc_objset = os;
723 	zc->zc_zap = NULL;
724 	zc->zc_leaf = NULL;
725 	zc->zc_zapobj = zapobj;
726 	if (serialized == -1ULL) {
727 		zc->zc_hash = -1ULL;
728 		zc->zc_cd = 0;
729 	} else {
730 		zc->zc_hash = serialized << (64-ZAP_HASHBITS);
731 		zc->zc_cd = serialized >> ZAP_HASHBITS;
732 		if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */
733 			zc->zc_cd = 0;
734 	}
735 }
736 
737 void
738 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
739 {
740 	zap_cursor_init_serialized(zc, os, zapobj, 0);
741 }
742 
743 void
744 zap_cursor_fini(zap_cursor_t *zc)
745 {
746 	if (zc->zc_zap) {
747 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
748 		zap_unlockdir(zc->zc_zap);
749 		zc->zc_zap = NULL;
750 	}
751 	if (zc->zc_leaf) {
752 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
753 		zap_put_leaf(zc->zc_leaf);
754 		zc->zc_leaf = NULL;
755 	}
756 	zc->zc_objset = NULL;
757 }
758 
759 uint64_t
760 zap_cursor_serialize(zap_cursor_t *zc)
761 {
762 	if (zc->zc_hash == -1ULL)
763 		return (-1ULL);
764 	ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0);
765 	ASSERT(zc->zc_cd < ZAP_MAXCD);
766 	return ((zc->zc_hash >> (64-ZAP_HASHBITS)) |
767 	    ((uint64_t)zc->zc_cd << ZAP_HASHBITS));
768 }
769 
770 int
771 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
772 {
773 	int err;
774 	avl_index_t idx;
775 	mzap_ent_t mze_tofind;
776 	mzap_ent_t *mze;
777 
778 	if (zc->zc_hash == -1ULL)
779 		return (ENOENT);
780 
781 	if (zc->zc_zap == NULL) {
782 		err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
783 		    RW_READER, TRUE, &zc->zc_zap);
784 		if (err)
785 			return (err);
786 	} else {
787 		rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
788 	}
789 	if (!zc->zc_zap->zap_ismicro) {
790 		err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
791 	} else {
792 		err = ENOENT;
793 
794 		mze_tofind.mze_hash = zc->zc_hash;
795 		mze_tofind.mze_phys.mze_cd = zc->zc_cd;
796 
797 		mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
798 		ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys,
799 		    &zc->zc_zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
800 		    sizeof (mze->mze_phys)));
801 		if (mze == NULL) {
802 			mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
803 			    idx, AVL_AFTER);
804 		}
805 		if (mze) {
806 			za->za_integer_length = 8;
807 			za->za_num_integers = 1;
808 			za->za_first_integer = mze->mze_phys.mze_value;
809 			(void) strcpy(za->za_name, mze->mze_phys.mze_name);
810 			zc->zc_hash = mze->mze_hash;
811 			zc->zc_cd = mze->mze_phys.mze_cd;
812 			err = 0;
813 		} else {
814 			zc->zc_hash = -1ULL;
815 		}
816 	}
817 	rw_exit(&zc->zc_zap->zap_rwlock);
818 	return (err);
819 }
820 
821 void
822 zap_cursor_advance(zap_cursor_t *zc)
823 {
824 	if (zc->zc_hash == -1ULL)
825 		return;
826 	zc->zc_cd++;
827 	if (zc->zc_cd >= ZAP_MAXCD) {
828 		zc->zc_cd = 0;
829 		zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS);
830 		if (zc->zc_hash == 0) /* EOF */
831 			zc->zc_hash = -1ULL;
832 	}
833 }
834 
835 int
836 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
837 {
838 	int err;
839 	zap_t *zap;
840 
841 	err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
842 	if (err)
843 		return (err);
844 
845 	bzero(zs, sizeof (zap_stats_t));
846 
847 	if (zap->zap_ismicro) {
848 		zs->zs_blocksize = zap->zap_dbuf->db_size;
849 		zs->zs_num_entries = zap->zap_m.zap_num_entries;
850 		zs->zs_num_blocks = 1;
851 	} else {
852 		fzap_get_stats(zap, zs);
853 	}
854 	zap_unlockdir(zap);
855 	return (0);
856 }
857