xref: /onnv-gate/usr/src/uts/common/fs/zfs/zap_leaf.c (revision 10922:e2081f502306)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51491Sahrens  * Common Development and Distribution License (the "License").
61491Sahrens  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
229643SEric.Taylor@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * The 512-byte leaf is broken into 32 16-byte chunks.
28789Sahrens  * chunk number n means l_chunk[n], even though the header precedes it.
29789Sahrens  * the names are stored null-terminated.
30789Sahrens  */
31789Sahrens 
32*10922SJeff.Bonwick@Sun.COM #include <sys/zio.h>
339643SEric.Taylor@Sun.COM #include <sys/spa.h>
349643SEric.Taylor@Sun.COM #include <sys/dmu.h>
35789Sahrens #include <sys/zfs_context.h>
369643SEric.Taylor@Sun.COM #include <sys/fs/zfs.h>
37789Sahrens #include <sys/zap.h>
38789Sahrens #include <sys/zap_impl.h>
39789Sahrens #include <sys/zap_leaf.h>
40789Sahrens 
415331Samw static uint16_t *zap_leaf_rehash_entry(zap_leaf_t *l, uint16_t entry);
425331Samw 
43789Sahrens #define	CHAIN_END 0xffff /* end of the chunk chain */
44789Sahrens 
451491Sahrens /* half the (current) minimum block size */
46789Sahrens #define	MAX_ARRAY_BYTES (8<<10)
47789Sahrens 
48789Sahrens #define	LEAF_HASH(l, h) \
491491Sahrens 	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
501578Sahrens 	((h) >> (64 - ZAP_LEAF_HASH_SHIFT(l)-(l)->l_phys->l_hdr.lh_prefix_len)))
51789Sahrens 
52789Sahrens #define	LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)])
53789Sahrens 
54789Sahrens 
55789Sahrens static void
56789Sahrens zap_memset(void *a, int c, size_t n)
57789Sahrens {
58789Sahrens 	char *cp = a;
59789Sahrens 	char *cpend = cp + n;
60789Sahrens 
61789Sahrens 	while (cp < cpend)
62789Sahrens 		*cp++ = c;
63789Sahrens }
64789Sahrens 
65789Sahrens static void
66789Sahrens stv(int len, void *addr, uint64_t value)
67789Sahrens {
68789Sahrens 	switch (len) {
69789Sahrens 	case 1:
70789Sahrens 		*(uint8_t *)addr = value;
71789Sahrens 		return;
72789Sahrens 	case 2:
73789Sahrens 		*(uint16_t *)addr = value;
74789Sahrens 		return;
75789Sahrens 	case 4:
76789Sahrens 		*(uint32_t *)addr = value;
77789Sahrens 		return;
78789Sahrens 	case 8:
79789Sahrens 		*(uint64_t *)addr = value;
80789Sahrens 		return;
81789Sahrens 	}
82789Sahrens 	ASSERT(!"bad int len");
83789Sahrens }
84789Sahrens 
85789Sahrens static uint64_t
86789Sahrens ldv(int len, const void *addr)
87789Sahrens {
88789Sahrens 	switch (len) {
89789Sahrens 	case 1:
90789Sahrens 		return (*(uint8_t *)addr);
91789Sahrens 	case 2:
92789Sahrens 		return (*(uint16_t *)addr);
93789Sahrens 	case 4:
94789Sahrens 		return (*(uint32_t *)addr);
95789Sahrens 	case 8:
96789Sahrens 		return (*(uint64_t *)addr);
97789Sahrens 	}
98789Sahrens 	ASSERT(!"bad int len");
992856Snd150628 	return (0xFEEDFACEDEADBEEFULL);
100789Sahrens }
101789Sahrens 
102789Sahrens void
1031491Sahrens zap_leaf_byteswap(zap_leaf_phys_t *buf, int size)
104789Sahrens {
105789Sahrens 	int i;
1061491Sahrens 	zap_leaf_t l;
1071491Sahrens 	l.l_bs = highbit(size)-1;
1081491Sahrens 	l.l_phys = buf;
109789Sahrens 
1101578Sahrens 	buf->l_hdr.lh_block_type = 	BSWAP_64(buf->l_hdr.lh_block_type);
1111578Sahrens 	buf->l_hdr.lh_prefix = 		BSWAP_64(buf->l_hdr.lh_prefix);
1121578Sahrens 	buf->l_hdr.lh_magic = 		BSWAP_32(buf->l_hdr.lh_magic);
1131578Sahrens 	buf->l_hdr.lh_nfree = 		BSWAP_16(buf->l_hdr.lh_nfree);
1141578Sahrens 	buf->l_hdr.lh_nentries = 	BSWAP_16(buf->l_hdr.lh_nentries);
1151578Sahrens 	buf->l_hdr.lh_prefix_len = 	BSWAP_16(buf->l_hdr.lh_prefix_len);
116789Sahrens 	buf->l_hdr.lh_freelist = 	BSWAP_16(buf->l_hdr.lh_freelist);
117789Sahrens 
1181491Sahrens 	for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++)
119789Sahrens 		buf->l_hash[i] = BSWAP_16(buf->l_hash[i]);
120789Sahrens 
1211491Sahrens 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
1221491Sahrens 		zap_leaf_chunk_t *lc = &ZAP_LEAF_CHUNK(&l, i);
123789Sahrens 		struct zap_leaf_entry *le;
124789Sahrens 
1251491Sahrens 		switch (lc->l_free.lf_type) {
1261491Sahrens 		case ZAP_CHUNK_ENTRY:
1271491Sahrens 			le = &lc->l_entry;
128789Sahrens 
1291578Sahrens 			le->le_type =		BSWAP_8(le->le_type);
1301578Sahrens 			le->le_int_size =	BSWAP_8(le->le_int_size);
1311578Sahrens 			le->le_next =		BSWAP_16(le->le_next);
1321578Sahrens 			le->le_name_chunk =	BSWAP_16(le->le_name_chunk);
1331578Sahrens 			le->le_name_length =	BSWAP_16(le->le_name_length);
1341578Sahrens 			le->le_value_chunk =	BSWAP_16(le->le_value_chunk);
1351578Sahrens 			le->le_value_length =	BSWAP_16(le->le_value_length);
1361578Sahrens 			le->le_cd =		BSWAP_32(le->le_cd);
1371578Sahrens 			le->le_hash =		BSWAP_64(le->le_hash);
138789Sahrens 			break;
1391491Sahrens 		case ZAP_CHUNK_FREE:
1401578Sahrens 			lc->l_free.lf_type =	BSWAP_8(lc->l_free.lf_type);
1411578Sahrens 			lc->l_free.lf_next =	BSWAP_16(lc->l_free.lf_next);
142789Sahrens 			break;
1431491Sahrens 		case ZAP_CHUNK_ARRAY:
1441578Sahrens 			lc->l_array.la_type =	BSWAP_8(lc->l_array.la_type);
1451578Sahrens 			lc->l_array.la_next =	BSWAP_16(lc->l_array.la_next);
146789Sahrens 			/* la_array doesn't need swapping */
147789Sahrens 			break;
148789Sahrens 		default:
149789Sahrens 			ASSERT(!"bad leaf type");
150789Sahrens 		}
151789Sahrens 	}
152789Sahrens }
153789Sahrens 
154789Sahrens void
1555498Stimh zap_leaf_init(zap_leaf_t *l, boolean_t sort)
156789Sahrens {
157789Sahrens 	int i;
158789Sahrens 
1591491Sahrens 	l->l_bs = highbit(l->l_dbuf->db_size)-1;
160789Sahrens 	zap_memset(&l->l_phys->l_hdr, 0, sizeof (struct zap_leaf_header));
1611491Sahrens 	zap_memset(l->l_phys->l_hash, CHAIN_END, 2*ZAP_LEAF_HASH_NUMENTRIES(l));
1621491Sahrens 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
1631491Sahrens 		ZAP_LEAF_CHUNK(l, i).l_free.lf_type = ZAP_CHUNK_FREE;
1641491Sahrens 		ZAP_LEAF_CHUNK(l, i).l_free.lf_next = i+1;
165789Sahrens 	}
1661491Sahrens 	ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)-1).l_free.lf_next = CHAIN_END;
1671578Sahrens 	l->l_phys->l_hdr.lh_block_type = ZBT_LEAF;
1681578Sahrens 	l->l_phys->l_hdr.lh_magic = ZAP_LEAF_MAGIC;
1691578Sahrens 	l->l_phys->l_hdr.lh_nfree = ZAP_LEAF_NUMCHUNKS(l);
1705498Stimh 	if (sort)
1715331Samw 		l->l_phys->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
172789Sahrens }
173789Sahrens 
174789Sahrens /*
175789Sahrens  * Routines which manipulate leaf chunks (l_chunk[]).
176789Sahrens  */
177789Sahrens 
178789Sahrens static uint16_t
179789Sahrens zap_leaf_chunk_alloc(zap_leaf_t *l)
180789Sahrens {
181789Sahrens 	int chunk;
182789Sahrens 
1831578Sahrens 	ASSERT(l->l_phys->l_hdr.lh_nfree > 0);
184789Sahrens 
185789Sahrens 	chunk = l->l_phys->l_hdr.lh_freelist;
1861491Sahrens 	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
1871491Sahrens 	ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_free.lf_type, ==, ZAP_CHUNK_FREE);
188789Sahrens 
1891491Sahrens 	l->l_phys->l_hdr.lh_freelist = ZAP_LEAF_CHUNK(l, chunk).l_free.lf_next;
190789Sahrens 
1911578Sahrens 	l->l_phys->l_hdr.lh_nfree--;
192789Sahrens 
193789Sahrens 	return (chunk);
194789Sahrens }
195789Sahrens 
196789Sahrens static void
197789Sahrens zap_leaf_chunk_free(zap_leaf_t *l, uint16_t chunk)
198789Sahrens {
1991491Sahrens 	struct zap_leaf_free *zlf = &ZAP_LEAF_CHUNK(l, chunk).l_free;
2001578Sahrens 	ASSERT3U(l->l_phys->l_hdr.lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l));
2011491Sahrens 	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
2021491Sahrens 	ASSERT(zlf->lf_type != ZAP_CHUNK_FREE);
203789Sahrens 
2041491Sahrens 	zlf->lf_type = ZAP_CHUNK_FREE;
205789Sahrens 	zlf->lf_next = l->l_phys->l_hdr.lh_freelist;
206789Sahrens 	bzero(zlf->lf_pad, sizeof (zlf->lf_pad)); /* help it to compress */
207789Sahrens 	l->l_phys->l_hdr.lh_freelist = chunk;
208789Sahrens 
2091578Sahrens 	l->l_phys->l_hdr.lh_nfree++;
210789Sahrens }
211789Sahrens 
212789Sahrens /*
213789Sahrens  * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
214789Sahrens  */
215789Sahrens 
216789Sahrens static uint16_t
2171578Sahrens zap_leaf_array_create(zap_leaf_t *l, const char *buf,
218789Sahrens 	int integer_size, int num_integers)
219789Sahrens {
220789Sahrens 	uint16_t chunk_head;
221789Sahrens 	uint16_t *chunkp = &chunk_head;
222789Sahrens 	int byten = 0;
223789Sahrens 	uint64_t value;
224789Sahrens 	int shift = (integer_size-1)*8;
225789Sahrens 	int len = num_integers;
226789Sahrens 
227789Sahrens 	ASSERT3U(num_integers * integer_size, <, MAX_ARRAY_BYTES);
228789Sahrens 
229789Sahrens 	while (len > 0) {
230789Sahrens 		uint16_t chunk = zap_leaf_chunk_alloc(l);
2311491Sahrens 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
232789Sahrens 		int i;
233789Sahrens 
2341491Sahrens 		la->la_type = ZAP_CHUNK_ARRAY;
235789Sahrens 		for (i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) {
236789Sahrens 			if (byten == 0)
237789Sahrens 				value = ldv(integer_size, buf);
2383052Sahrens 			la->la_array[i] = value >> shift;
239789Sahrens 			value <<= 8;
240789Sahrens 			if (++byten == integer_size) {
241789Sahrens 				byten = 0;
242789Sahrens 				buf += integer_size;
243789Sahrens 				if (--len == 0)
244789Sahrens 					break;
245789Sahrens 			}
246789Sahrens 		}
247789Sahrens 
248789Sahrens 		*chunkp = chunk;
249789Sahrens 		chunkp = &la->la_next;
250789Sahrens 	}
251789Sahrens 	*chunkp = CHAIN_END;
252789Sahrens 
253789Sahrens 	return (chunk_head);
254789Sahrens }
255789Sahrens 
256789Sahrens static void
2571578Sahrens zap_leaf_array_free(zap_leaf_t *l, uint16_t *chunkp)
258789Sahrens {
259789Sahrens 	uint16_t chunk = *chunkp;
260789Sahrens 
261789Sahrens 	*chunkp = CHAIN_END;
262789Sahrens 
263789Sahrens 	while (chunk != CHAIN_END) {
2641491Sahrens 		int nextchunk = ZAP_LEAF_CHUNK(l, chunk).l_array.la_next;
2651491Sahrens 		ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_array.la_type, ==,
2661491Sahrens 		    ZAP_CHUNK_ARRAY);
267789Sahrens 		zap_leaf_chunk_free(l, chunk);
268789Sahrens 		chunk = nextchunk;
269789Sahrens 	}
270789Sahrens }
271789Sahrens 
272789Sahrens /* array_len and buf_len are in integers, not bytes */
273789Sahrens static void
2741578Sahrens zap_leaf_array_read(zap_leaf_t *l, uint16_t chunk,
275789Sahrens     int array_int_len, int array_len, int buf_int_len, uint64_t buf_len,
276*10922SJeff.Bonwick@Sun.COM     void *buf)
277789Sahrens {
278789Sahrens 	int len = MIN(array_len, buf_len);
279789Sahrens 	int byten = 0;
280789Sahrens 	uint64_t value = 0;
281*10922SJeff.Bonwick@Sun.COM 	char *p = buf;
282789Sahrens 
283789Sahrens 	ASSERT3U(array_int_len, <=, buf_int_len);
284789Sahrens 
285885Sahrens 	/* Fast path for one 8-byte integer */
286885Sahrens 	if (array_int_len == 8 && buf_int_len == 8 && len == 1) {
2871491Sahrens 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
288899Sbonwick 		uint8_t *ip = la->la_array;
289*10922SJeff.Bonwick@Sun.COM 		uint64_t *buf64 = buf;
290899Sbonwick 
291899Sbonwick 		*buf64 = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
292899Sbonwick 		    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
293899Sbonwick 		    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
294899Sbonwick 		    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
295885Sahrens 		return;
296885Sahrens 	}
297885Sahrens 
298885Sahrens 	/* Fast path for an array of 1-byte integers (eg. the entry name) */
299885Sahrens 	if (array_int_len == 1 && buf_int_len == 1 &&
300885Sahrens 	    buf_len > array_len + ZAP_LEAF_ARRAY_BYTES) {
301885Sahrens 		while (chunk != CHAIN_END) {
302885Sahrens 			struct zap_leaf_array *la =
3031491Sahrens 			    &ZAP_LEAF_CHUNK(l, chunk).l_array;
304*10922SJeff.Bonwick@Sun.COM 			bcopy(la->la_array, p, ZAP_LEAF_ARRAY_BYTES);
305*10922SJeff.Bonwick@Sun.COM 			p += ZAP_LEAF_ARRAY_BYTES;
306885Sahrens 			chunk = la->la_next;
307885Sahrens 		}
308885Sahrens 		return;
309885Sahrens 	}
310885Sahrens 
311789Sahrens 	while (len > 0) {
3121491Sahrens 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
313789Sahrens 		int i;
314789Sahrens 
3151491Sahrens 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
316789Sahrens 		for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
317789Sahrens 			value = (value << 8) | la->la_array[i];
318789Sahrens 			byten++;
319789Sahrens 			if (byten == array_int_len) {
320*10922SJeff.Bonwick@Sun.COM 				stv(buf_int_len, p, value);
321789Sahrens 				byten = 0;
322789Sahrens 				len--;
323789Sahrens 				if (len == 0)
324789Sahrens 					return;
325*10922SJeff.Bonwick@Sun.COM 				p += buf_int_len;
326789Sahrens 			}
327789Sahrens 		}
328789Sahrens 		chunk = la->la_next;
329789Sahrens 	}
330789Sahrens }
331789Sahrens 
332789Sahrens /*
333789Sahrens  * array_len is actual len in bytes (not encoded le_value_length).
3345331Samw  * namenorm is null-terminated.
335789Sahrens  */
3365331Samw static boolean_t
3375331Samw zap_leaf_array_match(zap_leaf_t *l, zap_name_t *zn, int chunk, int array_len)
338789Sahrens {
339789Sahrens 	int bseen = 0;
340789Sahrens 
341*10922SJeff.Bonwick@Sun.COM 	if (zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY) {
342*10922SJeff.Bonwick@Sun.COM 		uint64_t *thiskey;
343*10922SJeff.Bonwick@Sun.COM 		boolean_t match;
344*10922SJeff.Bonwick@Sun.COM 
345*10922SJeff.Bonwick@Sun.COM 		ASSERT(zn->zn_key_intlen == sizeof (*thiskey));
346*10922SJeff.Bonwick@Sun.COM 		thiskey = kmem_alloc(array_len * sizeof (*thiskey), KM_SLEEP);
347*10922SJeff.Bonwick@Sun.COM 
348*10922SJeff.Bonwick@Sun.COM 		zap_leaf_array_read(l, chunk, sizeof (*thiskey), array_len,
349*10922SJeff.Bonwick@Sun.COM 		    sizeof (*thiskey), array_len, thiskey);
350*10922SJeff.Bonwick@Sun.COM 		match = bcmp(thiskey, zn->zn_key_orig,
351*10922SJeff.Bonwick@Sun.COM 		    array_len * sizeof (*thiskey)) == 0;
352*10922SJeff.Bonwick@Sun.COM 		kmem_free(thiskey, array_len * sizeof (*thiskey));
353*10922SJeff.Bonwick@Sun.COM 		return (match);
354*10922SJeff.Bonwick@Sun.COM 	}
355*10922SJeff.Bonwick@Sun.COM 
3565331Samw 	if (zn->zn_matchtype == MT_FIRST) {
3575331Samw 		char *thisname = kmem_alloc(array_len, KM_SLEEP);
3585331Samw 		boolean_t match;
3595331Samw 
360*10922SJeff.Bonwick@Sun.COM 		zap_leaf_array_read(l, chunk, sizeof (char), array_len,
361*10922SJeff.Bonwick@Sun.COM 		    sizeof (char), array_len, thisname);
3625331Samw 		match = zap_match(zn, thisname);
3635331Samw 		kmem_free(thisname, array_len);
3645331Samw 		return (match);
3655331Samw 	}
3665331Samw 
367*10922SJeff.Bonwick@Sun.COM 	/*
368*10922SJeff.Bonwick@Sun.COM 	 * Fast path for exact matching.
369*10922SJeff.Bonwick@Sun.COM 	 * First check that the lengths match, so that we don't read
370*10922SJeff.Bonwick@Sun.COM 	 * past the end of the zn_key_orig array.
371*10922SJeff.Bonwick@Sun.COM 	 */
372*10922SJeff.Bonwick@Sun.COM 	if (array_len != zn->zn_key_orig_len)
373*10922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
374789Sahrens 	while (bseen < array_len) {
3751491Sahrens 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
376789Sahrens 		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
3771491Sahrens 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
378*10922SJeff.Bonwick@Sun.COM 		if (bcmp(la->la_array, (char *)zn->zn_key_orig + bseen, toread))
379789Sahrens 			break;
380789Sahrens 		chunk = la->la_next;
381789Sahrens 		bseen += toread;
382789Sahrens 	}
383789Sahrens 	return (bseen == array_len);
384789Sahrens }
385789Sahrens 
386789Sahrens /*
387789Sahrens  * Routines which manipulate leaf entries.
388789Sahrens  */
389789Sahrens 
390789Sahrens int
3915331Samw zap_leaf_lookup(zap_leaf_t *l, zap_name_t *zn, zap_entry_handle_t *zeh)
392789Sahrens {
393789Sahrens 	uint16_t *chunkp;
394789Sahrens 	struct zap_leaf_entry *le;
395789Sahrens 
3961578Sahrens 	ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
397789Sahrens 
3985331Samw again:
3995331Samw 	for (chunkp = LEAF_HASH_ENTPTR(l, zn->zn_hash);
400789Sahrens 	    *chunkp != CHAIN_END; chunkp = &le->le_next) {
401789Sahrens 		uint16_t chunk = *chunkp;
4021491Sahrens 		le = ZAP_LEAF_ENTRY(l, chunk);
403789Sahrens 
4041491Sahrens 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
4051491Sahrens 		ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
406789Sahrens 
4075331Samw 		if (le->le_hash != zn->zn_hash)
408789Sahrens 			continue;
409789Sahrens 
4105331Samw 		/*
4115331Samw 		 * NB: the entry chain is always sorted by cd on
4125331Samw 		 * normalized zap objects, so this will find the
4135331Samw 		 * lowest-cd match for MT_FIRST.
4145331Samw 		 */
4155331Samw 		ASSERT(zn->zn_matchtype == MT_EXACT ||
4165331Samw 		    (l->l_phys->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED));
4175331Samw 		if (zap_leaf_array_match(l, zn, le->le_name_chunk,
4185331Samw 		    le->le_name_length)) {
419789Sahrens 			zeh->zeh_num_integers = le->le_value_length;
420789Sahrens 			zeh->zeh_integer_size = le->le_int_size;
421789Sahrens 			zeh->zeh_cd = le->le_cd;
422789Sahrens 			zeh->zeh_hash = le->le_hash;
423789Sahrens 			zeh->zeh_chunkp = chunkp;
4241578Sahrens 			zeh->zeh_leaf = l;
425789Sahrens 			return (0);
426789Sahrens 		}
427789Sahrens 	}
428789Sahrens 
4295331Samw 	/*
4305331Samw 	 * NB: we could of course do this in one pass, but that would be
4315331Samw 	 * a pain.  We'll see if MT_BEST is even used much.
4325331Samw 	 */
4335331Samw 	if (zn->zn_matchtype == MT_BEST) {
4345331Samw 		zn->zn_matchtype = MT_FIRST;
4355331Samw 		goto again;
4365331Samw 	}
4375331Samw 
438789Sahrens 	return (ENOENT);
439789Sahrens }
440789Sahrens 
441789Sahrens /* Return (h1,cd1 >= h2,cd2) */
442885Sahrens #define	HCD_GTEQ(h1, cd1, h2, cd2) \
443885Sahrens 	((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
444789Sahrens 
445789Sahrens int
446789Sahrens zap_leaf_lookup_closest(zap_leaf_t *l,
447789Sahrens     uint64_t h, uint32_t cd, zap_entry_handle_t *zeh)
448789Sahrens {
449789Sahrens 	uint16_t chunk;
450789Sahrens 	uint64_t besth = -1ULL;
451*10922SJeff.Bonwick@Sun.COM 	uint32_t bestcd = -1U;
4521491Sahrens 	uint16_t bestlh = ZAP_LEAF_HASH_NUMENTRIES(l)-1;
453789Sahrens 	uint16_t lh;
454789Sahrens 	struct zap_leaf_entry *le;
455789Sahrens 
4561578Sahrens 	ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
457789Sahrens 
458789Sahrens 	for (lh = LEAF_HASH(l, h); lh <= bestlh; lh++) {
459789Sahrens 		for (chunk = l->l_phys->l_hash[lh];
460789Sahrens 		    chunk != CHAIN_END; chunk = le->le_next) {
4611491Sahrens 			le = ZAP_LEAF_ENTRY(l, chunk);
462789Sahrens 
4631491Sahrens 			ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
4641491Sahrens 			ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
465789Sahrens 
466885Sahrens 			if (HCD_GTEQ(le->le_hash, le->le_cd, h, cd) &&
467885Sahrens 			    HCD_GTEQ(besth, bestcd, le->le_hash, le->le_cd)) {
468789Sahrens 				ASSERT3U(bestlh, >=, lh);
469789Sahrens 				bestlh = lh;
470789Sahrens 				besth = le->le_hash;
471789Sahrens 				bestcd = le->le_cd;
472789Sahrens 
473789Sahrens 				zeh->zeh_num_integers = le->le_value_length;
474789Sahrens 				zeh->zeh_integer_size = le->le_int_size;
475789Sahrens 				zeh->zeh_cd = le->le_cd;
476789Sahrens 				zeh->zeh_hash = le->le_hash;
477789Sahrens 				zeh->zeh_fakechunk = chunk;
478789Sahrens 				zeh->zeh_chunkp = &zeh->zeh_fakechunk;
4791578Sahrens 				zeh->zeh_leaf = l;
480789Sahrens 			}
481789Sahrens 		}
482789Sahrens 	}
483789Sahrens 
484*10922SJeff.Bonwick@Sun.COM 	return (bestcd == -1U ? ENOENT : 0);
485789Sahrens }
486789Sahrens 
487789Sahrens int
488789Sahrens zap_entry_read(const zap_entry_handle_t *zeh,
489789Sahrens     uint8_t integer_size, uint64_t num_integers, void *buf)
490789Sahrens {
4911491Sahrens 	struct zap_leaf_entry *le =
4921578Sahrens 	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
4931491Sahrens 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
494789Sahrens 
495789Sahrens 	if (le->le_int_size > integer_size)
496789Sahrens 		return (EINVAL);
497789Sahrens 
4981578Sahrens 	zap_leaf_array_read(zeh->zeh_leaf, le->le_value_chunk, le->le_int_size,
499789Sahrens 	    le->le_value_length, integer_size, num_integers, buf);
500789Sahrens 
501789Sahrens 	if (zeh->zeh_num_integers > num_integers)
502789Sahrens 		return (EOVERFLOW);
503789Sahrens 	return (0);
504789Sahrens 
505789Sahrens }
506789Sahrens 
507789Sahrens int
508*10922SJeff.Bonwick@Sun.COM zap_entry_read_name(zap_t *zap, const zap_entry_handle_t *zeh, uint16_t buflen,
509*10922SJeff.Bonwick@Sun.COM     char *buf)
510789Sahrens {
5111491Sahrens 	struct zap_leaf_entry *le =
5121578Sahrens 	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
5131491Sahrens 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
514789Sahrens 
515*10922SJeff.Bonwick@Sun.COM 	if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
516*10922SJeff.Bonwick@Sun.COM 		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 8,
517*10922SJeff.Bonwick@Sun.COM 		    le->le_name_length, 8, buflen / 8, buf);
518*10922SJeff.Bonwick@Sun.COM 	} else {
519*10922SJeff.Bonwick@Sun.COM 		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 1,
520*10922SJeff.Bonwick@Sun.COM 		    le->le_name_length, 1, buflen, buf);
521*10922SJeff.Bonwick@Sun.COM 	}
522789Sahrens 	if (le->le_name_length > buflen)
523789Sahrens 		return (EOVERFLOW);
524789Sahrens 	return (0);
525789Sahrens }
526789Sahrens 
527789Sahrens int
528789Sahrens zap_entry_update(zap_entry_handle_t *zeh,
529789Sahrens 	uint8_t integer_size, uint64_t num_integers, const void *buf)
530789Sahrens {
531789Sahrens 	int delta_chunks;
5321578Sahrens 	zap_leaf_t *l = zeh->zeh_leaf;
5331578Sahrens 	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, *zeh->zeh_chunkp);
534789Sahrens 
5351578Sahrens 	delta_chunks = ZAP_LEAF_ARRAY_NCHUNKS(num_integers * integer_size) -
5361578Sahrens 	    ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_length * le->le_int_size);
537789Sahrens 
5381578Sahrens 	if ((int)l->l_phys->l_hdr.lh_nfree < delta_chunks)
539789Sahrens 		return (EAGAIN);
540789Sahrens 
541789Sahrens 	/*
542789Sahrens 	 * We should search other chained leaves (via
543789Sahrens 	 * zap_entry_remove,create?) otherwise returning EAGAIN will
544789Sahrens 	 * just send us into an infinite loop if we have to chain
545789Sahrens 	 * another leaf block, rather than being able to split this
546789Sahrens 	 * block.
547789Sahrens 	 */
548789Sahrens 
5491578Sahrens 	zap_leaf_array_free(l, &le->le_value_chunk);
550789Sahrens 	le->le_value_chunk =
5511578Sahrens 	    zap_leaf_array_create(l, buf, integer_size, num_integers);
5521491Sahrens 	le->le_value_length = num_integers;
553789Sahrens 	le->le_int_size = integer_size;
554789Sahrens 	return (0);
555789Sahrens }
556789Sahrens 
557789Sahrens void
558789Sahrens zap_entry_remove(zap_entry_handle_t *zeh)
559789Sahrens {
560789Sahrens 	uint16_t entry_chunk;
561789Sahrens 	struct zap_leaf_entry *le;
5621578Sahrens 	zap_leaf_t *l = zeh->zeh_leaf;
563789Sahrens 
564789Sahrens 	ASSERT3P(zeh->zeh_chunkp, !=, &zeh->zeh_fakechunk);
565789Sahrens 
566789Sahrens 	entry_chunk = *zeh->zeh_chunkp;
5671491Sahrens 	le = ZAP_LEAF_ENTRY(l, entry_chunk);
5681491Sahrens 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
569789Sahrens 
5701578Sahrens 	zap_leaf_array_free(l, &le->le_name_chunk);
5711578Sahrens 	zap_leaf_array_free(l, &le->le_value_chunk);
572789Sahrens 
573789Sahrens 	*zeh->zeh_chunkp = le->le_next;
574789Sahrens 	zap_leaf_chunk_free(l, entry_chunk);
575789Sahrens 
5761578Sahrens 	l->l_phys->l_hdr.lh_nentries--;
577789Sahrens }
578789Sahrens 
579789Sahrens int
580*10922SJeff.Bonwick@Sun.COM zap_entry_create(zap_leaf_t *l, zap_name_t *zn, uint32_t cd,
581789Sahrens     uint8_t integer_size, uint64_t num_integers, const void *buf,
582789Sahrens     zap_entry_handle_t *zeh)
583789Sahrens {
584789Sahrens 	uint16_t chunk;
585789Sahrens 	uint16_t *chunkp;
586789Sahrens 	struct zap_leaf_entry *le;
587*10922SJeff.Bonwick@Sun.COM 	uint64_t valuelen;
588789Sahrens 	int numchunks;
589*10922SJeff.Bonwick@Sun.COM 	uint64_t h = zn->zn_hash;
590789Sahrens 
591789Sahrens 	valuelen = integer_size * num_integers;
592789Sahrens 
593*10922SJeff.Bonwick@Sun.COM 	numchunks = 1 +
594*10922SJeff.Bonwick@Sun.COM 	    ZAP_LEAF_ARRAY_NCHUNKS(zn->zn_key_orig_len * zn->zn_key_intlen) +
5951578Sahrens 	    ZAP_LEAF_ARRAY_NCHUNKS(valuelen);
5961491Sahrens 	if (numchunks > ZAP_LEAF_NUMCHUNKS(l))
597789Sahrens 		return (E2BIG);
598789Sahrens 
599*10922SJeff.Bonwick@Sun.COM 	if (cd == ZAP_NEED_CD) {
6005331Samw 		/* find the lowest unused cd */
6015331Samw 		if (l->l_phys->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED) {
6025331Samw 			cd = 0;
6035331Samw 
6041578Sahrens 			for (chunk = *LEAF_HASH_ENTPTR(l, h);
6051578Sahrens 			    chunk != CHAIN_END; chunk = le->le_next) {
6061578Sahrens 				le = ZAP_LEAF_ENTRY(l, chunk);
6075331Samw 				if (le->le_cd > cd)
6081578Sahrens 					break;
6095331Samw 				if (le->le_hash == h) {
6105331Samw 					ASSERT3U(cd, ==, le->le_cd);
6115331Samw 					cd++;
612789Sahrens 				}
613789Sahrens 			}
6145331Samw 		} else {
6155331Samw 			/* old unsorted format; do it the O(n^2) way */
616*10922SJeff.Bonwick@Sun.COM 			for (cd = 0; ; cd++) {
6175331Samw 				for (chunk = *LEAF_HASH_ENTPTR(l, h);
6185331Samw 				    chunk != CHAIN_END; chunk = le->le_next) {
6195331Samw 					le = ZAP_LEAF_ENTRY(l, chunk);
6205331Samw 					if (le->le_hash == h &&
6215331Samw 					    le->le_cd == cd) {
6225331Samw 						break;
6235331Samw 					}
6245331Samw 				}
6255331Samw 				/* If this cd is not in use, we are good. */
6265331Samw 				if (chunk == CHAIN_END)
6275331Samw 					break;
6285331Samw 			}
629789Sahrens 		}
6305331Samw 		/*
631*10922SJeff.Bonwick@Sun.COM 		 * We would run out of space in a block before we could
632*10922SJeff.Bonwick@Sun.COM 		 * store enough entries to run out of CD values.
6335331Samw 		 */
634*10922SJeff.Bonwick@Sun.COM 		ASSERT3U(cd, <, zap_maxcd(zn->zn_zap));
635789Sahrens 	}
636789Sahrens 
6371578Sahrens 	if (l->l_phys->l_hdr.lh_nfree < numchunks)
638789Sahrens 		return (EAGAIN);
639789Sahrens 
640789Sahrens 	/* make the entry */
641789Sahrens 	chunk = zap_leaf_chunk_alloc(l);
6421491Sahrens 	le = ZAP_LEAF_ENTRY(l, chunk);
6431491Sahrens 	le->le_type = ZAP_CHUNK_ENTRY;
644*10922SJeff.Bonwick@Sun.COM 	le->le_name_chunk = zap_leaf_array_create(l, zn->zn_key_orig,
645*10922SJeff.Bonwick@Sun.COM 	    zn->zn_key_intlen, zn->zn_key_orig_len);
646*10922SJeff.Bonwick@Sun.COM 	le->le_name_length = zn->zn_key_orig_len;
647789Sahrens 	le->le_value_chunk =
6481578Sahrens 	    zap_leaf_array_create(l, buf, integer_size, num_integers);
6491491Sahrens 	le->le_value_length = num_integers;
650789Sahrens 	le->le_int_size = integer_size;
651789Sahrens 	le->le_hash = h;
652789Sahrens 	le->le_cd = cd;
653789Sahrens 
654789Sahrens 	/* link it into the hash chain */
6555331Samw 	/* XXX if we did the search above, we could just use that */
6565331Samw 	chunkp = zap_leaf_rehash_entry(l, chunk);
657789Sahrens 
6581578Sahrens 	l->l_phys->l_hdr.lh_nentries++;
659789Sahrens 
6601578Sahrens 	zeh->zeh_leaf = l;
661789Sahrens 	zeh->zeh_num_integers = num_integers;
662789Sahrens 	zeh->zeh_integer_size = le->le_int_size;
663789Sahrens 	zeh->zeh_cd = le->le_cd;
664789Sahrens 	zeh->zeh_hash = le->le_hash;
665789Sahrens 	zeh->zeh_chunkp = chunkp;
666789Sahrens 
667789Sahrens 	return (0);
668789Sahrens }
669789Sahrens 
670789Sahrens /*
6715331Samw  * Determine if there is another entry with the same normalized form.
6725331Samw  * For performance purposes, either zn or name must be provided (the
6735331Samw  * other can be NULL).  Note, there usually won't be any hash
6745331Samw  * conflicts, in which case we don't need the concatenated/normalized
6755331Samw  * form of the name.  But all callers have one of these on hand anyway,
6765331Samw  * so might as well take advantage.  A cleaner but slower interface
6775331Samw  * would accept neither argument, and compute the normalized name as
6785331Samw  * needed (using zap_name_alloc(zap_entry_read_name(zeh))).
6795331Samw  */
6805331Samw boolean_t
6815331Samw zap_entry_normalization_conflict(zap_entry_handle_t *zeh, zap_name_t *zn,
6825331Samw     const char *name, zap_t *zap)
6835331Samw {
6845331Samw 	uint64_t chunk;
6855331Samw 	struct zap_leaf_entry *le;
6865331Samw 	boolean_t allocdzn = B_FALSE;
6875331Samw 
6885331Samw 	if (zap->zap_normflags == 0)
6895331Samw 		return (B_FALSE);
6905331Samw 
6915331Samw 	for (chunk = *LEAF_HASH_ENTPTR(zeh->zeh_leaf, zeh->zeh_hash);
6925331Samw 	    chunk != CHAIN_END; chunk = le->le_next) {
6935331Samw 		le = ZAP_LEAF_ENTRY(zeh->zeh_leaf, chunk);
6945331Samw 		if (le->le_hash != zeh->zeh_hash)
6955331Samw 			continue;
6965331Samw 		if (le->le_cd == zeh->zeh_cd)
6975331Samw 			continue;
6985331Samw 
6995331Samw 		if (zn == NULL) {
7005331Samw 			zn = zap_name_alloc(zap, name, MT_FIRST);
7015331Samw 			allocdzn = B_TRUE;
7025331Samw 		}
7035331Samw 		if (zap_leaf_array_match(zeh->zeh_leaf, zn,
7045331Samw 		    le->le_name_chunk, le->le_name_length)) {
7055331Samw 			if (allocdzn)
7065331Samw 				zap_name_free(zn);
7075331Samw 			return (B_TRUE);
7085331Samw 		}
7095331Samw 	}
7105331Samw 	if (allocdzn)
7115331Samw 		zap_name_free(zn);
7125331Samw 	return (B_FALSE);
7135331Samw }
7145331Samw 
7155331Samw /*
716789Sahrens  * Routines for transferring entries between leafs.
717789Sahrens  */
718789Sahrens 
7195331Samw static uint16_t *
720789Sahrens zap_leaf_rehash_entry(zap_leaf_t *l, uint16_t entry)
721789Sahrens {
7221491Sahrens 	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry);
7235331Samw 	struct zap_leaf_entry *le2;
7245331Samw 	uint16_t *chunkp;
7255331Samw 
7265331Samw 	/*
7275331Samw 	 * keep the entry chain sorted by cd
7285331Samw 	 * NB: this will not cause problems for unsorted leafs, though
7295331Samw 	 * it is unnecessary there.
7305331Samw 	 */
7315331Samw 	for (chunkp = LEAF_HASH_ENTPTR(l, le->le_hash);
7325331Samw 	    *chunkp != CHAIN_END; chunkp = &le2->le_next) {
7335331Samw 		le2 = ZAP_LEAF_ENTRY(l, *chunkp);
7345331Samw 		if (le2->le_cd > le->le_cd)
7355331Samw 			break;
7365331Samw 	}
7375331Samw 
7385331Samw 	le->le_next = *chunkp;
7395331Samw 	*chunkp = entry;
7405331Samw 	return (chunkp);
741789Sahrens }
742789Sahrens 
743789Sahrens static uint16_t
744789Sahrens zap_leaf_transfer_array(zap_leaf_t *l, uint16_t chunk, zap_leaf_t *nl)
745789Sahrens {
746789Sahrens 	uint16_t new_chunk;
747789Sahrens 	uint16_t *nchunkp = &new_chunk;
748789Sahrens 
749789Sahrens 	while (chunk != CHAIN_END) {
750789Sahrens 		uint16_t nchunk = zap_leaf_chunk_alloc(nl);
751789Sahrens 		struct zap_leaf_array *nla =
7521491Sahrens 		    &ZAP_LEAF_CHUNK(nl, nchunk).l_array;
753789Sahrens 		struct zap_leaf_array *la =
7541491Sahrens 		    &ZAP_LEAF_CHUNK(l, chunk).l_array;
755789Sahrens 		int nextchunk = la->la_next;
756789Sahrens 
7571491Sahrens 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
7581491Sahrens 		ASSERT3U(nchunk, <, ZAP_LEAF_NUMCHUNKS(l));
759789Sahrens 
7601578Sahrens 		*nla = *la; /* structure assignment */
761789Sahrens 
762789Sahrens 		zap_leaf_chunk_free(l, chunk);
763789Sahrens 		chunk = nextchunk;
764789Sahrens 		*nchunkp = nchunk;
765789Sahrens 		nchunkp = &nla->la_next;
766789Sahrens 	}
767789Sahrens 	*nchunkp = CHAIN_END;
768789Sahrens 	return (new_chunk);
769789Sahrens }
770789Sahrens 
771789Sahrens static void
7721578Sahrens zap_leaf_transfer_entry(zap_leaf_t *l, int entry, zap_leaf_t *nl)
773789Sahrens {
774789Sahrens 	struct zap_leaf_entry *le, *nle;
7751578Sahrens 	uint16_t chunk;
776789Sahrens 
7771491Sahrens 	le = ZAP_LEAF_ENTRY(l, entry);
7781491Sahrens 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
779789Sahrens 
780789Sahrens 	chunk = zap_leaf_chunk_alloc(nl);
7811491Sahrens 	nle = ZAP_LEAF_ENTRY(nl, chunk);
7821578Sahrens 	*nle = *le; /* structure assignment */
783789Sahrens 
7845331Samw 	(void) zap_leaf_rehash_entry(nl, chunk);
785789Sahrens 
786789Sahrens 	nle->le_name_chunk = zap_leaf_transfer_array(l, le->le_name_chunk, nl);
787789Sahrens 	nle->le_value_chunk =
788789Sahrens 	    zap_leaf_transfer_array(l, le->le_value_chunk, nl);
789789Sahrens 
790789Sahrens 	zap_leaf_chunk_free(l, entry);
791789Sahrens 
7921578Sahrens 	l->l_phys->l_hdr.lh_nentries--;
7931578Sahrens 	nl->l_phys->l_hdr.lh_nentries++;
794789Sahrens }
795789Sahrens 
796789Sahrens /*
7971578Sahrens  * Transfer the entries whose hash prefix ends in 1 to the new leaf.
798789Sahrens  */
7991578Sahrens void
8005498Stimh zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort)
801789Sahrens {
802789Sahrens 	int i;
8031578Sahrens 	int bit = 64 - 1 - l->l_phys->l_hdr.lh_prefix_len;
804789Sahrens 
8051578Sahrens 	/* set new prefix and prefix_len */
8061578Sahrens 	l->l_phys->l_hdr.lh_prefix <<= 1;
8071578Sahrens 	l->l_phys->l_hdr.lh_prefix_len++;
8081578Sahrens 	nl->l_phys->l_hdr.lh_prefix = l->l_phys->l_hdr.lh_prefix | 1;
8091578Sahrens 	nl->l_phys->l_hdr.lh_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
8101578Sahrens 
811789Sahrens 	/* break existing hash chains */
8121491Sahrens 	zap_memset(l->l_phys->l_hash, CHAIN_END, 2*ZAP_LEAF_HASH_NUMENTRIES(l));
813789Sahrens 
8145498Stimh 	if (sort)
8155331Samw 		l->l_phys->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
8165331Samw 
8171578Sahrens 	/*
8181578Sahrens 	 * Transfer entries whose hash bit 'bit' is set to nl; rehash
8191578Sahrens 	 * the remaining entries
8201578Sahrens 	 *
8211578Sahrens 	 * NB: We could find entries via the hashtable instead. That
8221578Sahrens 	 * would be O(hashents+numents) rather than O(numblks+numents),
8231578Sahrens 	 * but this accesses memory more sequentially, and when we're
8241578Sahrens 	 * called, the block is usually pretty full.
8251578Sahrens 	 */
8261491Sahrens 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
8271491Sahrens 		struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, i);
8281491Sahrens 		if (le->le_type != ZAP_CHUNK_ENTRY)
829789Sahrens 			continue;
830789Sahrens 
8311578Sahrens 		if (le->le_hash & (1ULL << bit))
8321578Sahrens 			zap_leaf_transfer_entry(l, i, nl);
8331578Sahrens 		else
8345331Samw 			(void) zap_leaf_rehash_entry(l, i);
835789Sahrens 	}
836789Sahrens }
837789Sahrens 
838789Sahrens void
8391578Sahrens zap_leaf_stats(zap_t *zap, zap_leaf_t *l, zap_stats_t *zs)
840789Sahrens {
8411578Sahrens 	int i, n;
842789Sahrens 
8431578Sahrens 	n = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift -
8441578Sahrens 	    l->l_phys->l_hdr.lh_prefix_len;
845789Sahrens 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
846789Sahrens 	zs->zs_leafs_with_2n_pointers[n]++;
847789Sahrens 
848789Sahrens 
8491578Sahrens 	n = l->l_phys->l_hdr.lh_nentries/5;
8501578Sahrens 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
8511578Sahrens 	zs->zs_blocks_with_n5_entries[n]++;
852789Sahrens 
8531578Sahrens 	n = ((1<<FZAP_BLOCK_SHIFT(zap)) -
8541578Sahrens 	    l->l_phys->l_hdr.lh_nfree * (ZAP_LEAF_ARRAY_BYTES+1))*10 /
8551578Sahrens 	    (1<<FZAP_BLOCK_SHIFT(zap));
8561578Sahrens 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
8571578Sahrens 	zs->zs_blocks_n_tenths_full[n]++;
858789Sahrens 
8591578Sahrens 	for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(l); i++) {
8601578Sahrens 		int nentries = 0;
8611578Sahrens 		int chunk = l->l_phys->l_hash[i];
8621578Sahrens 
8631578Sahrens 		while (chunk != CHAIN_END) {
8641578Sahrens 			struct zap_leaf_entry *le =
8651578Sahrens 			    ZAP_LEAF_ENTRY(l, chunk);
866789Sahrens 
8671578Sahrens 			n = 1 + ZAP_LEAF_ARRAY_NCHUNKS(le->le_name_length) +
8681578Sahrens 			    ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_length *
8695498Stimh 			    le->le_int_size);
8701578Sahrens 			n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
8711578Sahrens 			zs->zs_entries_using_n_chunks[n]++;
872789Sahrens 
8731578Sahrens 			chunk = le->le_next;
8741578Sahrens 			nentries++;
875789Sahrens 		}
876789Sahrens 
8771578Sahrens 		n = nentries;
8781578Sahrens 		n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
8791578Sahrens 		zs->zs_buckets_with_n_entries[n]++;
8801578Sahrens 	}
881789Sahrens }
882