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