1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5f65e61cahrens * Common Development and Distribution License (the "License").
6f65e61cahrens * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
211c17160Kevin Crowe
22fa9e406ahrens/*
233f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24bf26014Matthew Ahrens * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
251c17160Kevin Crowe * Copyright 2017 Nexenta Systems, Inc.
26fa9e406ahrens */
27fa9e406ahrens
28fa9e406ahrens/*
29fa9e406ahrens * The 512-byte leaf is broken into 32 16-byte chunks.
30fa9e406ahrens * chunk number n means l_chunk[n], even though the header precedes it.
31fa9e406ahrens * the names are stored null-terminated.
32fa9e406ahrens */
33fa9e406ahrens
34b24ab67Jeff Bonwick#include <sys/zio.h>
35478ed9aEric Taylor#include <sys/spa.h>
36478ed9aEric Taylor#include <sys/dmu.h>
37fa9e406ahrens#include <sys/zfs_context.h>
38478ed9aEric Taylor#include <sys/fs/zfs.h>
39fa9e406ahrens#include <sys/zap.h>
40fa9e406ahrens#include <sys/zap_impl.h>
41fa9e406ahrens#include <sys/zap_leaf.h>
423f9d6adLin Ling#include <sys/arc.h>
43fa9e406ahrens
44da6c28aamwstatic uint16_t *zap_leaf_rehash_entry(zap_leaf_t *l, uint16_t entry);
45da6c28aamw
46fa9e406ahrens#define	CHAIN_END 0xffff /* end of the chunk chain */
47fa9e406ahrens
48f65e61cahrens/* half the (current) minimum block size */
49fa9e406ahrens#define	MAX_ARRAY_BYTES (8<<10)
50fa9e406ahrens
51fa9e406ahrens#define	LEAF_HASH(l, h) \
52f65e61cahrens	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
53c137962Justin T. Gibbs	((h) >> \
54c137962Justin T. Gibbs	(64 - ZAP_LEAF_HASH_SHIFT(l) - zap_leaf_phys(l)->l_hdr.lh_prefix_len)))
55fa9e406ahrens
56c137962Justin T. Gibbs#define	LEAF_HASH_ENTPTR(l, h) (&zap_leaf_phys(l)->l_hash[LEAF_HASH(l, h)])
57fa9e406ahrens
58c137962Justin T. Gibbsextern inline zap_leaf_phys_t *zap_leaf_phys(zap_leaf_t *l);
59fa9e406ahrens
60fa9e406ahrensstatic void
61fa9e406ahrenszap_memset(void *a, int c, size_t n)
62fa9e406ahrens{
63fa9e406ahrens	char *cp = a;
64fa9e406ahrens	char *cpend = cp + n;
65fa9e406ahrens
66fa9e406ahrens	while (cp < cpend)
67fa9e406ahrens		*cp++ = c;
68fa9e406ahrens}
69fa9e406ahrens
70fa9e406ahrensstatic void
71fa9e406ahrensstv(int len, void *addr, uint64_t value)
72fa9e406ahrens{
73fa9e406ahrens	switch (len) {
74fa9e406ahrens	case 1:
75fa9e406ahrens		*(uint8_t *)addr = value;
76fa9e406ahrens		return;
77fa9e406ahrens	case 2:
78fa9e406ahrens		*(uint16_t *)addr = value;
79fa9e406ahrens		return;
80fa9e406ahrens	case 4:
81fa9e406ahrens		*(uint32_t *)addr = value;
82fa9e406ahrens		return;
83fa9e406ahrens	case 8:
84fa9e406ahrens		*(uint64_t *)addr = value;
85fa9e406ahrens		return;
86fa9e406ahrens	}
87fa9e406ahrens	ASSERT(!"bad int len");
88fa9e406ahrens}
89fa9e406ahrens
90fa9e406ahrensstatic uint64_t
91fa9e406ahrensldv(int len, const void *addr)
92fa9e406ahrens{
93fa9e406ahrens	switch (len) {
94fa9e406ahrens	case 1:
95fa9e406ahrens		return (*(uint8_t *)addr);
96fa9e406ahrens	case 2:
97fa9e406ahrens		return (*(uint16_t *)addr);
98fa9e406ahrens	case 4:
99fa9e406ahrens		return (*(uint32_t *)addr);
100fa9e406ahrens	case 8:
101fa9e406ahrens		return (*(uint64_t *)addr);
102fa9e406ahrens	}
103fa9e406ahrens	ASSERT(!"bad int len");
1045ad8204nd	return (0xFEEDFACEDEADBEEFULL);
105fa9e406ahrens}
106fa9e406ahrens
107fa9e406ahrensvoid
108f65e61cahrenszap_leaf_byteswap(zap_leaf_phys_t *buf, int size)
109fa9e406ahrens{
110f65e61cahrens	zap_leaf_t l;
111c137962Justin T. Gibbs	dmu_buf_t l_dbuf;
112c137962Justin T. Gibbs
113c137962Justin T. Gibbs	l_dbuf.db_data = buf;
114bf16b11Matthew Ahrens	l.l_bs = highbit64(size) - 1;
115c137962Justin T. Gibbs	l.l_dbuf = &l_dbuf;
116fa9e406ahrens
117bf16b11Matthew Ahrens	buf->l_hdr.lh_block_type =	BSWAP_64(buf->l_hdr.lh_block_type);
118bf16b11Matthew Ahrens	buf->l_hdr.lh_prefix =		BSWAP_64(buf->l_hdr.lh_prefix);
119bf16b11Matthew Ahrens	buf->l_hdr.lh_magic =		BSWAP_32(buf->l_hdr.lh_magic);
120bf16b11Matthew Ahrens	buf->l_hdr.lh_nfree =		BSWAP_16(buf->l_hdr.lh_nfree);
121bf16b11Matthew Ahrens	buf->l_hdr.lh_nentries =	BSWAP_16(buf->l_hdr.lh_nentries);
122bf16b11Matthew Ahrens	buf->l_hdr.lh_prefix_len =	BSWAP_16(buf->l_hdr.lh_prefix_len);
123bf16b11Matthew Ahrens	buf->l_hdr.lh_freelist =	BSWAP_16(buf->l_hdr.lh_freelist);
124fa9e406ahrens
125bf26014Matthew Ahrens	for (int i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++)
126fa9e406ahrens		buf->l_hash[i] = BSWAP_16(buf->l_hash[i]);
127fa9e406ahrens
128bf26014Matthew Ahrens	for (int i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
129f65e61cahrens		zap_leaf_chunk_t *lc = &ZAP_LEAF_CHUNK(&l, i);
130fa9e406ahrens		struct zap_leaf_entry *le;
131fa9e406ahrens
132f65e61cahrens		switch (lc->l_free.lf_type) {
133f65e61cahrens		case ZAP_CHUNK_ENTRY:
134f65e61cahrens			le = &lc->l_entry;
135fa9e406ahrens
13666328ddahrens			le->le_type =		BSWAP_8(le->le_type);
137486ae71Matthew Ahrens			le->le_value_intlen =	BSWAP_8(le->le_value_intlen);
13866328ddahrens			le->le_next =		BSWAP_16(le->le_next);
13966328ddahrens			le->le_name_chunk =	BSWAP_16(le->le_name_chunk);
140486ae71Matthew Ahrens			le->le_name_numints =	BSWAP_16(le->le_name_numints);
14166328ddahrens			le->le_value_chunk =	BSWAP_16(le->le_value_chunk);
142486ae71Matthew Ahrens			le->le_value_numints =	BSWAP_16(le->le_value_numints);
14366328ddahrens			le->le_cd =		BSWAP_32(le->le_cd);
14466328ddahrens			le->le_hash =		BSWAP_64(le->le_hash);
145fa9e406ahrens			break;
146f65e61cahrens		case ZAP_CHUNK_FREE:
14766328ddahrens			lc->l_free.lf_type =	BSWAP_8(lc->l_free.lf_type);
14866328ddahrens			lc->l_free.lf_next =	BSWAP_16(lc->l_free.lf_next);
149fa9e406ahrens			break;
150f65e61cahrens		case ZAP_CHUNK_ARRAY:
15166328ddahrens			lc->l_array.la_type =	BSWAP_8(lc->l_array.la_type);
15266328ddahrens			lc->l_array.la_next =	BSWAP_16(lc->l_array.la_next);
153fa9e406ahrens			/* la_array doesn't need swapping */
154fa9e406ahrens			break;
155fa9e406ahrens		default:
156fa9e406ahrens			ASSERT(!"bad leaf type");
157fa9e406ahrens		}
158fa9e406ahrens	}
159fa9e406ahrens}
160fa9e406ahrens
161fa9e406ahrensvoid
162de8267etimhzap_leaf_init(zap_leaf_t *l, boolean_t sort)
163fa9e406ahrens{
164bf16b11Matthew Ahrens	l->l_bs = highbit64(l->l_dbuf->db_size) - 1;
165c137962Justin T. Gibbs	zap_memset(&zap_leaf_phys(l)->l_hdr, 0,
166c137962Justin T. Gibbs	    sizeof (struct zap_leaf_header));
167c137962Justin T. Gibbs	zap_memset(zap_leaf_phys(l)->l_hash, CHAIN_END,
168c137962Justin T. Gibbs	    2*ZAP_LEAF_HASH_NUMENTRIES(l));
169bf26014Matthew Ahrens	for (int i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
170f65e61cahrens		ZAP_LEAF_CHUNK(l, i).l_free.lf_type = ZAP_CHUNK_FREE;
171f65e61cahrens		ZAP_LEAF_CHUNK(l, i).l_free.lf_next = i+1;
172fa9e406ahrens	}
173f65e61cahrens	ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)-1).l_free.lf_next = CHAIN_END;
174c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_block_type = ZBT_LEAF;
175c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_magic = ZAP_LEAF_MAGIC;
176c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_nfree = ZAP_LEAF_NUMCHUNKS(l);
177de8267etimh	if (sort)
178c137962Justin T. Gibbs		zap_leaf_phys(l)->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
179fa9e406ahrens}
180fa9e406ahrens
181fa9e406ahrens/*
182fa9e406ahrens * Routines which manipulate leaf chunks (l_chunk[]).
183fa9e406ahrens */
184fa9e406ahrens
185fa9e406ahrensstatic uint16_t
186fa9e406ahrenszap_leaf_chunk_alloc(zap_leaf_t *l)
187fa9e406ahrens{
188c137962Justin T. Gibbs	ASSERT(zap_leaf_phys(l)->l_hdr.lh_nfree > 0);
189fa9e406ahrens
190bf26014Matthew Ahrens	int chunk = zap_leaf_phys(l)->l_hdr.lh_freelist;
191f65e61cahrens	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
192f65e61cahrens	ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_free.lf_type, ==, ZAP_CHUNK_FREE);
193fa9e406ahrens
194c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_freelist =
195c137962Justin T. Gibbs	    ZAP_LEAF_CHUNK(l, chunk).l_free.lf_next;
196fa9e406ahrens
197c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_nfree--;
198fa9e406ahrens
199fa9e406ahrens	return (chunk);
200fa9e406ahrens}
201fa9e406ahrens
202fa9e406ahrensstatic void
203fa9e406ahrenszap_leaf_chunk_free(zap_leaf_t *l, uint16_t chunk)
204fa9e406ahrens{
205f65e61cahrens	struct zap_leaf_free *zlf = &ZAP_LEAF_CHUNK(l, chunk).l_free;
206c137962Justin T. Gibbs	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l));
207f65e61cahrens	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
208f65e61cahrens	ASSERT(zlf->lf_type != ZAP_CHUNK_FREE);
209fa9e406ahrens
210f65e61cahrens	zlf->lf_type = ZAP_CHUNK_FREE;
211c137962Justin T. Gibbs	zlf->lf_next = zap_leaf_phys(l)->l_hdr.lh_freelist;
212fa9e406ahrens	bzero(zlf->lf_pad, sizeof (zlf->lf_pad)); /* help it to compress */
213c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_freelist = chunk;
214fa9e406ahrens
215c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_nfree++;
216fa9e406ahrens}
217fa9e406ahrens
218fa9e406ahrens/*
219fa9e406ahrens * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
220fa9e406ahrens */
221fa9e406ahrens
222fa9e406ahrensstatic uint16_t
22366328ddahrenszap_leaf_array_create(zap_leaf_t *l, const char *buf,
224486ae71Matthew Ahrens    int integer_size, int num_integers)
225fa9e406ahrens{
226fa9e406ahrens	uint16_t chunk_head;
227fa9e406ahrens	uint16_t *chunkp = &chunk_head;
228fa9e406ahrens	int byten = 0;
229d5285caGeorge Wilson	uint64_t value = 0;
230bf26014Matthew Ahrens	int shift = (integer_size - 1) * 8;
231fa9e406ahrens	int len = num_integers;
232fa9e406ahrens
233fa9e406ahrens	ASSERT3U(num_integers * integer_size, <, MAX_ARRAY_BYTES);
234fa9e406ahrens
235fa9e406ahrens	while (len > 0) {
236fa9e406ahrens		uint16_t chunk = zap_leaf_chunk_alloc(l);
237f65e61cahrens		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
238fa9e406ahrens
239f65e61cahrens		la->la_type = ZAP_CHUNK_ARRAY;
240bf26014Matthew Ahrens		for (int i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) {
241fa9e406ahrens			if (byten == 0)
242fa9e406ahrens				value = ldv(integer_size, buf);
243c5608ceahrens			la->la_array[i] = value >> shift;
244fa9e406ahrens			value <<= 8;
245fa9e406ahrens			if (++byten == integer_size) {
246fa9e406ahrens				byten = 0;
247fa9e406ahrens				buf += integer_size;
248fa9e406ahrens				if (--len == 0)
249fa9e406ahrens					break;
250fa9e406ahrens			}
251fa9e406ahrens		}
252fa9e406ahrens
253fa9e406ahrens		*chunkp = chunk;
254fa9e406ahrens		chunkp = &la->la_next;
255fa9e406ahrens	}
256fa9e406ahrens	*chunkp = CHAIN_END;
257fa9e406ahrens
258fa9e406ahrens	return (chunk_head);
259fa9e406ahrens}
260fa9e406ahrens
261fa9e406ahrensstatic void
26266328ddahrenszap_leaf_array_free(zap_leaf_t *l, uint16_t *chunkp)
263fa9e406ahrens{
264fa9e406ahrens	uint16_t chunk = *chunkp;
265fa9e406ahrens
266fa9e406ahrens	*chunkp = CHAIN_END;
267fa9e406ahrens
268fa9e406ahrens	while (chunk != CHAIN_END) {
269f65e61cahrens		int nextchunk = ZAP_LEAF_CHUNK(l, chunk).l_array.la_next;
270f65e61cahrens		ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_array.la_type, ==,
271f65e61cahrens		    ZAP_CHUNK_ARRAY);
272fa9e406ahrens		zap_leaf_chunk_free(l, chunk);
273fa9e406ahrens		chunk = nextchunk;
274fa9e406ahrens	}
275fa9e406ahrens}
276fa9e406ahrens
277fa9e406ahrens/* array_len and buf_len are in integers, not bytes */
278fa9e406ahrensstatic void
27966328ddahrenszap_leaf_array_read(zap_leaf_t *l, uint16_t chunk,
280fa9e406ahrens    int array_int_len, int array_len, int buf_int_len, uint64_t buf_len,
281b24ab67Jeff Bonwick    void *buf)
282fa9e406ahrens{
283fa9e406ahrens	int len = MIN(array_len, buf_len);
284fa9e406ahrens	int byten = 0;
285fa9e406ahrens	uint64_t value = 0;
286b24ab67Jeff Bonwick	char *p = buf;
287fa9e406ahrens
288fa9e406ahrens	ASSERT3U(array_int_len, <=, buf_int_len);
289fa9e406ahrens
29087e5029ahrens	/* Fast path for one 8-byte integer */
29187e5029ahrens	if (array_int_len == 8 && buf_int_len == 8 && len == 1) {
292f65e61cahrens		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
2939621b9bbonwick		uint8_t *ip = la->la_array;
294b24ab67Jeff Bonwick		uint64_t *buf64 = buf;
2959621b9bbonwick
2969621b9bbonwick		*buf64 = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
2979621b9bbonwick		    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
2989621b9bbonwick		    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
2999621b9bbonwick		    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
30087e5029ahrens		return;
30187e5029ahrens	}
30287e5029ahrens
30387e5029ahrens	/* Fast path for an array of 1-byte integers (eg. the entry name) */
30487e5029ahrens	if (array_int_len == 1 && buf_int_len == 1 &&
30587e5029ahrens	    buf_len > array_len + ZAP_LEAF_ARRAY_BYTES) {
30687e5029ahrens		while (chunk != CHAIN_END) {
30787e5029ahrens			struct zap_leaf_array *la =
308f65e61cahrens			    &ZAP_LEAF_CHUNK(l, chunk).l_array;
309b24ab67Jeff Bonwick			bcopy(la->la_array, p, ZAP_LEAF_ARRAY_BYTES);
310b24ab67Jeff Bonwick			p += ZAP_LEAF_ARRAY_BYTES;
31187e5029ahrens			chunk = la->la_next;
31287e5029ahrens		}
31387e5029ahrens		return;
31487e5029ahrens	}
31587e5029ahrens
316fa9e406ahrens	while (len > 0) {
317f65e61cahrens		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
318fa9e406ahrens
319f65e61cahrens		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
320bf26014Matthew Ahrens		for (int i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) {
321fa9e406ahrens			value = (value << 8) | la->la_array[i];
322fa9e406ahrens			byten++;
323fa9e406ahrens			if (byten == array_int_len) {
324b24ab67Jeff Bonwick				stv(buf_int_len, p, value);
325fa9e406ahrens				byten = 0;
326fa9e406ahrens				len--;
327fa9e406ahrens				if (len == 0)
328fa9e406ahrens					return;
329b24ab67Jeff Bonwick				p += buf_int_len;
330fa9e406ahrens			}
331fa9e406ahrens		}
332fa9e406ahrens		chunk = la->la_next;
333fa9e406ahrens	}
334fa9e406ahrens}
335fa9e406ahrens
336da6c28aamwstatic boolean_t
337486ae71Matthew Ahrenszap_leaf_array_match(zap_leaf_t *l, zap_name_t *zn,
338486ae71Matthew Ahrens    int chunk, int array_numints)
339fa9e406ahrens{
340fa9e406ahrens	int bseen = 0;
341fa9e406ahrens
342b24ab67Jeff Bonwick	if (zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY) {
343bf26014Matthew Ahrens		uint64_t *thiskey =
344bf26014Matthew Ahrens		    kmem_alloc(array_numints * sizeof (*thiskey), KM_SLEEP);
345b24ab67Jeff Bonwick		ASSERT(zn->zn_key_intlen == sizeof (*thiskey));
346b24ab67Jeff Bonwick
347486ae71Matthew Ahrens		zap_leaf_array_read(l, chunk, sizeof (*thiskey), array_numints,
348486ae71Matthew Ahrens		    sizeof (*thiskey), array_numints, thiskey);
349bf26014Matthew Ahrens		boolean_t match = bcmp(thiskey, zn->zn_key_orig,
350486ae71Matthew Ahrens		    array_numints * sizeof (*thiskey)) == 0;
351486ae71Matthew Ahrens		kmem_free(thiskey, array_numints * sizeof (*thiskey));
352b24ab67Jeff Bonwick		return (match);
353b24ab67Jeff Bonwick	}
354b24ab67Jeff Bonwick
355486ae71Matthew Ahrens	ASSERT(zn->zn_key_intlen == 1);
3561c17160Kevin Crowe	if (zn->zn_matchtype & MT_NORMALIZE) {
357486ae71Matthew Ahrens		char *thisname = kmem_alloc(array_numints, KM_SLEEP);
358da6c28aamw
359486ae71Matthew Ahrens		zap_leaf_array_read(l, chunk, sizeof (char), array_numints,
360486ae71Matthew Ahrens		    sizeof (char), array_numints, thisname);
361bf26014Matthew Ahrens		boolean_t match = zap_match(zn, thisname);
362486ae71Matthew Ahrens		kmem_free(thisname, array_numints);
363da6c28aamw		return (match);
364da6c28aamw	}
365da6c28aamw
366b24ab67Jeff Bonwick	/*
367b24ab67Jeff Bonwick	 * Fast path for exact matching.
368b24ab67Jeff Bonwick	 * First check that the lengths match, so that we don't read
369b24ab67Jeff Bonwick	 * past the end of the zn_key_orig array.
370b24ab67Jeff Bonwick	 */
371486ae71Matthew Ahrens	if (array_numints != zn->zn_key_orig_numints)
372b24ab67Jeff Bonwick		return (B_FALSE);
373486ae71Matthew Ahrens	while (bseen < array_numints) {
374f65e61cahrens		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
375486ae71Matthew Ahrens		int toread = MIN(array_numints - bseen, ZAP_LEAF_ARRAY_BYTES);
376f65e61cahrens		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
377b24ab67Jeff Bonwick		if (bcmp(la->la_array, (char *)zn->zn_key_orig + bseen, toread))
378fa9e406ahrens			break;
379fa9e406ahrens		chunk = la->la_next;
380fa9e406ahrens		bseen += toread;
381fa9e406ahrens	}
382486ae71Matthew Ahrens	return (bseen == array_numints);
383fa9e406ahrens}
384fa9e406ahrens
385fa9e406ahrens/*
386fa9e406ahrens * Routines which manipulate leaf entries.
387fa9e406ahrens */
388fa9e406ahrens
389fa9e406ahrensint
390da6c28aamwzap_leaf_lookup(zap_leaf_t *l, zap_name_t *zn, zap_entry_handle_t *zeh)
391fa9e406ahrens{
392fa9e406ahrens	struct zap_leaf_entry *le;
393fa9e406ahrens
394c137962Justin T. Gibbs	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
395fa9e406ahrens
396bf26014Matthew Ahrens	for (uint16_t *chunkp = LEAF_HASH_ENTPTR(l, zn->zn_hash);
397fa9e406ahrens	    *chunkp != CHAIN_END; chunkp = &le->le_next) {
398fa9e406ahrens		uint16_t chunk = *chunkp;
399f65e61cahrens		le = ZAP_LEAF_ENTRY(l, chunk);
400fa9e406ahrens
401f65e61cahrens		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
402f65e61cahrens		ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
403fa9e406ahrens
404da6c28aamw		if (le->le_hash != zn->zn_hash)
405fa9e406ahrens			continue;
406fa9e406ahrens
407da6c28aamw		/*
408da6c28aamw		 * NB: the entry chain is always sorted by cd on
409da6c28aamw		 * normalized zap objects, so this will find the
4101c17160Kevin Crowe		 * lowest-cd match for MT_NORMALIZE.
411da6c28aamw		 */
4121c17160Kevin Crowe		ASSERT((zn->zn_matchtype == 0) ||
413c137962Justin T. Gibbs		    (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED));
414da6c28aamw		if (zap_leaf_array_match(l, zn, le->le_name_chunk,
415486ae71Matthew Ahrens		    le->le_name_numints)) {
416486ae71Matthew Ahrens			zeh->zeh_num_integers = le->le_value_numints;
417486ae71Matthew Ahrens			zeh->zeh_integer_size = le->le_value_intlen;
418fa9e406ahrens			zeh->zeh_cd = le->le_cd;
419fa9e406ahrens			zeh->zeh_hash = le->le_hash;
420fa9e406ahrens			zeh->zeh_chunkp = chunkp;
42166328ddahrens			zeh->zeh_leaf = l;
422fa9e406ahrens			return (0);
423fa9e406ahrens		}
424fa9e406ahrens	}
425fa9e406ahrens
426be6fd75Matthew Ahrens	return (SET_ERROR(ENOENT));
427fa9e406ahrens}
428fa9e406ahrens
429fa9e406ahrens/* Return (h1,cd1 >= h2,cd2) */
43087e5029ahrens#define	HCD_GTEQ(h1, cd1, h2, cd2) \
43187e5029ahrens	((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
432fa9e406ahrens
433fa9e406ahrensint
434fa9e406ahrenszap_leaf_lookup_closest(zap_leaf_t *l,
435fa9e406ahrens    uint64_t h, uint32_t cd, zap_entry_handle_t *zeh)
436fa9e406ahrens{
437fa9e406ahrens	uint64_t besth = -1ULL;
438b24ab67Jeff Bonwick	uint32_t bestcd = -1U;
439f65e61cahrens	uint16_t bestlh = ZAP_LEAF_HASH_NUMENTRIES(l)-1;
440fa9e406ahrens	struct zap_leaf_entry *le;
441fa9e406ahrens
442c137962Justin T. Gibbs	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
443fa9e406ahrens
444bf26014Matthew Ahrens	for (uint16_t lh = LEAF_HASH(l, h); lh <= bestlh; lh++) {
445bf26014Matthew Ahrens		for (uint16_t chunk = zap_leaf_phys(l)->l_hash[lh];
446fa9e406ahrens		    chunk != CHAIN_END; chunk = le->le_next) {
447f65e61cahrens			le = ZAP_LEAF_ENTRY(l, chunk);
448fa9e406ahrens
449f65e61cahrens			ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
450f65e61cahrens			ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
451fa9e406ahrens
45287e5029ahrens			if (HCD_GTEQ(le->le_hash, le->le_cd, h, cd) &&
45387e5029ahrens			    HCD_GTEQ(besth, bestcd, le->le_hash, le->le_cd)) {
454fa9e406ahrens				ASSERT3U(bestlh, >=, lh);
455fa9e406ahrens				bestlh = lh;
456fa9e406ahrens				besth = le->le_hash;
457fa9e406ahrens				bestcd = le->le_cd;
458fa9e406ahrens
459486ae71Matthew Ahrens				zeh->zeh_num_integers = le->le_value_numints;
460486ae71Matthew Ahrens				zeh->zeh_integer_size = le->le_value_intlen;
461fa9e406ahrens				zeh->zeh_cd = le->le_cd;
462fa9e406ahrens				zeh->zeh_hash = le->le_hash;
463fa9e406ahrens				zeh->zeh_fakechunk = chunk;
464fa9e406ahrens				zeh->zeh_chunkp = &zeh->zeh_fakechunk;
46566328ddahrens				zeh->zeh_leaf = l;
466fa9e406ahrens			}
467fa9e406ahrens		}
468fa9e406ahrens	}
469fa9e406ahrens
470b24ab67Jeff Bonwick	return (bestcd == -1U ? ENOENT : 0);
471fa9e406ahrens}
472fa9e406ahrens
473fa9e406ahrensint
474fa9e406ahrenszap_entry_read(const zap_entry_handle_t *zeh,
475fa9e406ahrens    uint8_t integer_size, uint64_t num_integers, void *buf)
476fa9e406ahrens{
477f65e61cahrens	struct zap_leaf_entry *le =
47866328ddahrens	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
479f65e61cahrens	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
480fa9e406ahrens
481486ae71Matthew Ahrens	if (le->le_value_intlen > integer_size)
482be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
483fa9e406ahrens
484486ae71Matthew Ahrens	zap_leaf_array_read(zeh->zeh_leaf, le->le_value_chunk,
485486ae71Matthew Ahrens	    le->le_value_intlen, le->le_value_numints,
486486ae71Matthew Ahrens	    integer_size, num_integers, buf);
487fa9e406ahrens
488fa9e406ahrens	if (zeh->zeh_num_integers > num_integers)
489be6fd75Matthew Ahrens		return (SET_ERROR(EOVERFLOW));
490fa9e406ahrens	return (0);
491fa9e406ahrens
492fa9e406ahrens}
493fa9e406ahrens
494fa9e406ahrensint
495b24ab67Jeff Bonwickzap_entry_read_name(zap_t *zap, const zap_entry_handle_t *zeh, uint16_t buflen,
496b24ab67Jeff Bonwick    char *buf)
497fa9e406ahrens{
498f65e61cahrens	struct zap_leaf_entry *le =
49966328ddahrens	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
500f65e61cahrens	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
501fa9e406ahrens
502b24ab67Jeff Bonwick	if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
503b24ab67Jeff Bonwick		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 8,
504486ae71Matthew Ahrens		    le->le_name_numints, 8, buflen / 8, buf);
505b24ab67Jeff Bonwick	} else {
506b24ab67Jeff Bonwick		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 1,
507486ae71Matthew Ahrens		    le->le_name_numints, 1, buflen, buf);
508b24ab67Jeff Bonwick	}
509486ae71Matthew Ahrens	if (le->le_name_numints > buflen)
510be6fd75Matthew Ahrens		return (SET_ERROR(EOVERFLOW));
511fa9e406ahrens	return (0);
512fa9e406ahrens}
513fa9e406ahrens
514fa9e406ahrensint
515fa9e406ahrenszap_entry_update(zap_entry_handle_t *zeh,
5169a686fbPaul Dagnelie    uint8_t integer_size, uint64_t num_integers, const void *buf)
517fa9e406ahrens{
51866328ddahrens	zap_leaf_t *l = zeh->zeh_leaf;
51966328ddahrens	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, *zeh->zeh_chunkp);
520fa9e406ahrens
521bf26014Matthew Ahrens	int delta_chunks = ZAP_LEAF_ARRAY_NCHUNKS(num_integers * integer_size) -
522486ae71Matthew Ahrens	    ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_numints * le->le_value_intlen);
523fa9e406ahrens
524c137962Justin T. Gibbs	if ((int)zap_leaf_phys(l)->l_hdr.lh_nfree < delta_chunks)
525be6fd75Matthew Ahrens		return (SET_ERROR(EAGAIN));
526fa9e406ahrens
52766328ddahrens	zap_leaf_array_free(l, &le->le_value_chunk);
528fa9e406ahrens	le->le_value_chunk =
52966328ddahrens	    zap_leaf_array_create(l, buf, integer_size, num_integers);
530486ae71Matthew Ahrens	le->le_value_numints = num_integers;
531486ae71Matthew Ahrens	le->le_value_intlen = integer_size;
532fa9e406ahrens	return (0);
533fa9e406ahrens}
534fa9e406ahrens
535fa9e406ahrensvoid
536fa9e406ahrenszap_entry_remove(zap_entry_handle_t *zeh)
537fa9e406ahrens{
53866328ddahrens	zap_leaf_t *l = zeh->zeh_leaf;
539fa9e406ahrens
540fa9e406ahrens	ASSERT3P(zeh->zeh_chunkp, !=, &zeh->zeh_fakechunk);
541fa9e406ahrens
542bf26014Matthew Ahrens	uint16_t entry_chunk = *zeh->zeh_chunkp;
543bf26014Matthew Ahrens	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry_chunk);
544f65e61cahrens	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
545fa9e406ahrens
54666328ddahrens	zap_leaf_array_free(l, &le->le_name_chunk);
54766328ddahrens	zap_leaf_array_free(l, &le->le_value_chunk);
548fa9e406ahrens
549fa9e406ahrens	*zeh->zeh_chunkp = le->le_next;
550fa9e406ahrens	zap_leaf_chunk_free(l, entry_chunk);
551fa9e406ahrens
552c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_nentries--;
553fa9e406ahrens}
554fa9e406ahrens
555fa9e406ahrensint
556b24ab67Jeff Bonwickzap_entry_create(zap_leaf_t *l, zap_name_t *zn, uint32_t cd,
557fa9e406ahrens    uint8_t integer_size, uint64_t num_integers, const void *buf,
558fa9e406ahrens    zap_entry_handle_t *zeh)
559fa9e406ahrens{
560fa9e406ahrens	uint16_t chunk;
561fa9e406ahrens	struct zap_leaf_entry *le;
562b24ab67Jeff Bonwick	uint64_t h = zn->zn_hash;
563fa9e406ahrens
564bf26014Matthew Ahrens	uint64_t valuelen = integer_size * num_integers;
565fa9e406ahrens
566bf26014Matthew Ahrens	int numchunks = 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn->zn_key_orig_numints *
567486ae71Matthew Ahrens	    zn->zn_key_intlen) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen);
568f65e61cahrens	if (numchunks > ZAP_LEAF_NUMCHUNKS(l))
569fa9e406ahrens		return (E2BIG);
570fa9e406ahrens
571b24ab67Jeff Bonwick	if (cd == ZAP_NEED_CD) {
572da6c28aamw		/* find the lowest unused cd */
573c137962Justin T. Gibbs		if (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED) {
574da6c28aamw			cd = 0;
575da6c28aamw
57666328ddahrens			for (chunk = *LEAF_HASH_ENTPTR(l, h);
57766328ddahrens			    chunk != CHAIN_END; chunk = le->le_next) {
57866328ddahrens				le = ZAP_LEAF_ENTRY(l, chunk);
579da6c28aamw				if (le->le_cd > cd)
580fa9e406ahrens					break;
581da6c28aamw				if (le->le_hash == h) {
582da6c28aamw					ASSERT3U(cd, ==, le->le_cd);
583da6c28aamw					cd++;
58466328ddahrens				}
585fa9e406ahrens			}
586da6c28aamw		} else {
587da6c28aamw			/* old unsorted format; do it the O(n^2) way */
588b24ab67Jeff Bonwick			for (cd = 0; ; cd++) {
589da6c28aamw				for (chunk = *LEAF_HASH_ENTPTR(l, h);
590da6c28aamw				    chunk != CHAIN_END; chunk = le->le_next) {
591da6c28aamw					le = ZAP_LEAF_ENTRY(l, chunk);
592da6c28aamw					if (le->le_hash == h &&
593da6c28aamw					    le->le_cd == cd) {
594da6c28aamw						break;
595da6c28aamw					}
596da6c28aamw				}
597da6c28aamw				/* If this cd is not in use, we are good. */
598da6c28aamw				if (chunk == CHAIN_END)
599da6c28aamw					break;
600da6c28aamw			}
601fa9e406ahrens		}
602da6c28aamw		/*
603b24ab67Jeff Bonwick		 * We would run out of space in a block before we could
604b24ab67Jeff Bonwick		 * store enough entries to run out of CD values.
605da6c28aamw		 */
606b24ab67Jeff Bonwick		ASSERT3U(cd, <, zap_maxcd(zn->zn_zap));
607fa9e406ahrens	}
608fa9e406ahrens
609c137962Justin T. Gibbs	if (zap_leaf_phys(l)->l_hdr.lh_nfree < numchunks)
610be6fd75Matthew Ahrens		return (SET_ERROR(EAGAIN));
611fa9e406ahrens
612fa9e406ahrens	/* make the entry */
613fa9e406ahrens	chunk = zap_leaf_chunk_alloc(l);
614f65e61cahrens	le = ZAP_LEAF_ENTRY(l, chunk);
615f65e61cahrens	le->le_type = ZAP_CHUNK_ENTRY;
616b24ab67Jeff Bonwick	le->le_name_chunk = zap_leaf_array_create(l, zn->zn_key_orig,
617486ae71Matthew Ahrens	    zn->zn_key_intlen, zn->zn_key_orig_numints);
618486ae71Matthew Ahrens	le->le_name_numints = zn->zn_key_orig_numints;
619fa9e406ahrens	le->le_value_chunk =
62066328ddahrens	    zap_leaf_array_create(l, buf, integer_size, num_integers);
621486ae71Matthew Ahrens	le->le_value_numints = num_integers;
622486ae71Matthew Ahrens	le->le_value_intlen = integer_size;
623fa9e406ahrens	le->le_hash = h;
624fa9e406ahrens	le->le_cd = cd;
625fa9e406ahrens
626fa9e406ahrens	/* link it into the hash chain */
627da6c28aamw	/* XXX if we did the search above, we could just use that */
628bf26014Matthew Ahrens	uint16_t *chunkp = zap_leaf_rehash_entry(l, chunk);
629fa9e406ahrens
630c137962Justin T. Gibbs	zap_leaf_phys(l)->l_hdr.lh_nentries++;
631fa9e406ahrens
63266328ddahrens	zeh->zeh_leaf = l;
633fa9e406ahrens	zeh->zeh_num_integers = num_integers;
634486ae71Matthew Ahrens	zeh->zeh_integer_size = le->le_value_intlen;
635fa9e406ahrens	zeh->zeh_cd = le->le_cd;
636fa9e406ahrens	zeh->zeh_hash = le->le_hash;
637fa9e406ahrens	zeh->zeh_chunkp = chunkp;
638fa9e406ahrens
639fa9e406ahrens	return (0);
640fa9e406ahrens}
641fa9e406ahrens
642fa9e406ahrens/*
643da6c28aamw * Determine if there is another entry with the same normalized form.
644da6c28aamw * For performance purposes, either zn or name must be provided (the
645da6c28aamw * other can be NULL).  Note, there usually won't be any hash
646da6c28aamw * conflicts, in which case we don't need the concatenated/normalized
647da6c28aamw * form of the name.  But all callers have one of these on hand anyway,
648da6c28aamw * so might as well take advantage.  A cleaner but slower interface
649da6c28aamw * would accept neither argument, and compute the normalized name as
650da6c28aamw * needed (using zap_name_alloc(zap_entry_read_name(zeh))).
651da6c28aamw */
652da6c28aamwboolean_t
653da6c28aamwzap_entry_normalization_conflict(zap_entry_handle_t *zeh, zap_name_t *zn,
654da6c28aamw    const char *name, zap_t *zap)
655da6c28aamw{
656da6c28aamw	struct zap_leaf_entry *le;
657da6c28aamw	boolean_t allocdzn = B_FALSE;
658da6c28aamw
659da6c28aamw	if (zap->zap_normflags == 0)
660da6c28aamw		return (B_FALSE);
661da6c28aamw
662bf26014Matthew Ahrens	for (uint16_t chunk = *LEAF_HASH_ENTPTR(zeh->zeh_leaf, zeh->zeh_hash);
663da6c28aamw	    chunk != CHAIN_END; chunk = le->le_next) {
664da6c28aamw		le = ZAP_LEAF_ENTRY(zeh->zeh_leaf, chunk);
665da6c28aamw		if (le->le_hash != zeh->zeh_hash)
666da6c28aamw			continue;
667da6c28aamw		if (le->le_cd == zeh->zeh_cd)
668da6c28aamw			continue;
669da6c28aamw
670da6c28aamw		if (zn == NULL) {
6711c17160Kevin Crowe			zn = zap_name_alloc(zap, name, MT_NORMALIZE);
672da6c28aamw			allocdzn = B_TRUE;
673da6c28aamw		}
674da6c28aamw		if (zap_leaf_array_match(zeh->zeh_leaf, zn,
675486ae71Matthew Ahrens		    le->le_name_chunk, le->le_name_numints)) {
676da6c28aamw			if (allocdzn)
677da6c28aamw				zap_name_free(zn);
678