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