xref: /freebsd/sys/contrib/openzfs/module/zfs/zap_leaf.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
26  * Copyright 2017 Nexenta Systems, Inc.
27  */
28 
29 /*
30  * The 512-byte leaf is broken into 32 16-byte chunks.
31  * chunk number n means l_chunk[n], even though the header precedes it.
32  * the names are stored null-terminated.
33  */
34 
35 #include <sys/zio.h>
36 #include <sys/spa.h>
37 #include <sys/dmu.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zap.h>
41 #include <sys/zap_impl.h>
42 #include <sys/zap_leaf.h>
43 #include <sys/arc.h>
44 
45 static uint16_t *zap_leaf_rehash_entry(zap_leaf_t *l, struct zap_leaf_entry *le,
46     uint16_t entry);
47 
48 #define	CHAIN_END 0xffff /* end of the chunk chain */
49 
50 #define	LEAF_HASH(l, h) \
51 	((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
52 	((h) >> \
53 	(64 - ZAP_LEAF_HASH_SHIFT(l) - zap_leaf_phys(l)->l_hdr.lh_prefix_len)))
54 
55 #define	LEAF_HASH_ENTPTR(l, h)	(&zap_leaf_phys(l)->l_hash[LEAF_HASH(l, h)])
56 
57 static void
stv(int len,void * addr,uint64_t value)58 stv(int len, void *addr, uint64_t value)
59 {
60 	switch (len) {
61 	case 1:
62 		*(uint8_t *)addr = value;
63 		return;
64 	case 2:
65 		*(uint16_t *)addr = value;
66 		return;
67 	case 4:
68 		*(uint32_t *)addr = value;
69 		return;
70 	case 8:
71 		*(uint64_t *)addr = value;
72 		return;
73 	default:
74 		PANIC("bad int len %d", len);
75 	}
76 }
77 
78 static uint64_t
ldv(int len,const void * addr)79 ldv(int len, const void *addr)
80 {
81 	switch (len) {
82 	case 1:
83 		return (*(uint8_t *)addr);
84 	case 2:
85 		return (*(uint16_t *)addr);
86 	case 4:
87 		return (*(uint32_t *)addr);
88 	case 8:
89 		return (*(uint64_t *)addr);
90 	default:
91 		PANIC("bad int len %d", len);
92 	}
93 	return (0xFEEDFACEDEADBEEFULL);
94 }
95 
96 void
zap_leaf_byteswap(zap_leaf_phys_t * buf,size_t size)97 zap_leaf_byteswap(zap_leaf_phys_t *buf, size_t size)
98 {
99 	zap_leaf_t l;
100 	dmu_buf_t l_dbuf;
101 
102 	l_dbuf.db_data = buf;
103 	l.l_bs = highbit64(size) - 1;
104 	l.l_dbuf = &l_dbuf;
105 
106 	buf->l_hdr.lh_block_type =	BSWAP_64(buf->l_hdr.lh_block_type);
107 	buf->l_hdr.lh_prefix =		BSWAP_64(buf->l_hdr.lh_prefix);
108 	buf->l_hdr.lh_magic =		BSWAP_32(buf->l_hdr.lh_magic);
109 	buf->l_hdr.lh_nfree =		BSWAP_16(buf->l_hdr.lh_nfree);
110 	buf->l_hdr.lh_nentries =	BSWAP_16(buf->l_hdr.lh_nentries);
111 	buf->l_hdr.lh_prefix_len =	BSWAP_16(buf->l_hdr.lh_prefix_len);
112 	buf->l_hdr.lh_freelist =	BSWAP_16(buf->l_hdr.lh_freelist);
113 
114 	for (uint_t i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++)
115 		buf->l_hash[i] = BSWAP_16(buf->l_hash[i]);
116 
117 	for (uint_t i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
118 		zap_leaf_chunk_t *lc = &ZAP_LEAF_CHUNK(&l, i);
119 		struct zap_leaf_entry *le;
120 
121 		switch (lc->l_free.lf_type) {
122 		case ZAP_CHUNK_ENTRY:
123 			le = &lc->l_entry;
124 
125 			le->le_type =		BSWAP_8(le->le_type);
126 			le->le_value_intlen =	BSWAP_8(le->le_value_intlen);
127 			le->le_next =		BSWAP_16(le->le_next);
128 			le->le_name_chunk =	BSWAP_16(le->le_name_chunk);
129 			le->le_name_numints =	BSWAP_16(le->le_name_numints);
130 			le->le_value_chunk =	BSWAP_16(le->le_value_chunk);
131 			le->le_value_numints =	BSWAP_16(le->le_value_numints);
132 			le->le_cd =		BSWAP_32(le->le_cd);
133 			le->le_hash =		BSWAP_64(le->le_hash);
134 			break;
135 		case ZAP_CHUNK_FREE:
136 			lc->l_free.lf_type =	BSWAP_8(lc->l_free.lf_type);
137 			lc->l_free.lf_next =	BSWAP_16(lc->l_free.lf_next);
138 			break;
139 		case ZAP_CHUNK_ARRAY:
140 			lc->l_array.la_type =	BSWAP_8(lc->l_array.la_type);
141 			lc->l_array.la_next =	BSWAP_16(lc->l_array.la_next);
142 			/* la_array doesn't need swapping */
143 			break;
144 		default:
145 			cmn_err(CE_PANIC, "bad leaf type %d",
146 			    lc->l_free.lf_type);
147 		}
148 	}
149 }
150 
151 void
zap_leaf_init(zap_leaf_t * l,boolean_t sort)152 zap_leaf_init(zap_leaf_t *l, boolean_t sort)
153 {
154 	l->l_bs = highbit64(l->l_dbuf->db_size) - 1;
155 	memset(&zap_leaf_phys(l)->l_hdr, 0,
156 	    sizeof (struct zap_leaf_header));
157 	memset(zap_leaf_phys(l)->l_hash, CHAIN_END,
158 	    2*ZAP_LEAF_HASH_NUMENTRIES(l));
159 	for (uint_t i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
160 		ZAP_LEAF_CHUNK(l, i).l_free.lf_type = ZAP_CHUNK_FREE;
161 		ZAP_LEAF_CHUNK(l, i).l_free.lf_next = i+1;
162 	}
163 	ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)-1).l_free.lf_next = CHAIN_END;
164 	zap_leaf_phys(l)->l_hdr.lh_block_type = ZBT_LEAF;
165 	zap_leaf_phys(l)->l_hdr.lh_magic = ZAP_LEAF_MAGIC;
166 	zap_leaf_phys(l)->l_hdr.lh_nfree = ZAP_LEAF_NUMCHUNKS(l);
167 	if (sort)
168 		zap_leaf_phys(l)->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
169 }
170 
171 /*
172  * Routines which manipulate leaf chunks (l_chunk[]).
173  */
174 
175 static uint16_t
zap_leaf_chunk_alloc(zap_leaf_t * l)176 zap_leaf_chunk_alloc(zap_leaf_t *l)
177 {
178 	ASSERT(zap_leaf_phys(l)->l_hdr.lh_nfree > 0);
179 
180 	uint_t chunk = zap_leaf_phys(l)->l_hdr.lh_freelist;
181 	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
182 	ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_free.lf_type, ==, ZAP_CHUNK_FREE);
183 
184 	zap_leaf_phys(l)->l_hdr.lh_freelist =
185 	    ZAP_LEAF_CHUNK(l, chunk).l_free.lf_next;
186 
187 	zap_leaf_phys(l)->l_hdr.lh_nfree--;
188 
189 	return (chunk);
190 }
191 
192 static void
zap_leaf_chunk_free(zap_leaf_t * l,uint16_t chunk)193 zap_leaf_chunk_free(zap_leaf_t *l, uint16_t chunk)
194 {
195 	struct zap_leaf_free *zlf = &ZAP_LEAF_CHUNK(l, chunk).l_free;
196 	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l));
197 	ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
198 	ASSERT(zlf->lf_type != ZAP_CHUNK_FREE);
199 
200 	zlf->lf_type = ZAP_CHUNK_FREE;
201 	zlf->lf_next = zap_leaf_phys(l)->l_hdr.lh_freelist;
202 	memset(zlf->lf_pad, 0, sizeof (zlf->lf_pad)); /* help it to compress */
203 	zap_leaf_phys(l)->l_hdr.lh_freelist = chunk;
204 
205 	zap_leaf_phys(l)->l_hdr.lh_nfree++;
206 }
207 
208 /*
209  * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
210  */
211 
212 static uint16_t
zap_leaf_array_create(zap_leaf_t * l,const char * buf,int integer_size,int num_integers)213 zap_leaf_array_create(zap_leaf_t *l, const char *buf,
214     int integer_size, int num_integers)
215 {
216 	uint16_t chunk_head;
217 	uint16_t *chunkp = &chunk_head;
218 	int byten = integer_size;
219 	uint64_t value = 0;
220 	int shift = (integer_size - 1) * 8;
221 	int len = num_integers;
222 
223 	ASSERT3U(num_integers * integer_size, <=, ZAP_MAXVALUELEN);
224 
225 	if (len > 0)
226 		value = ldv(integer_size, buf);
227 	while (len > 0) {
228 		uint16_t chunk = zap_leaf_chunk_alloc(l);
229 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
230 
231 		la->la_type = ZAP_CHUNK_ARRAY;
232 		for (int i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) {
233 			la->la_array[i] = value >> shift;
234 			value <<= 8;
235 			if (--byten == 0) {
236 				if (--len == 0)
237 					break;
238 				byten = integer_size;
239 				buf += integer_size;
240 				value = ldv(integer_size, buf);
241 			}
242 		}
243 
244 		*chunkp = chunk;
245 		chunkp = &la->la_next;
246 	}
247 	*chunkp = CHAIN_END;
248 
249 	return (chunk_head);
250 }
251 
252 /*
253  * Non-destructively copy array between leaves.
254  */
255 static uint16_t
zap_leaf_array_copy(zap_leaf_t * l,uint16_t chunk,zap_leaf_t * nl)256 zap_leaf_array_copy(zap_leaf_t *l, uint16_t chunk, zap_leaf_t *nl)
257 {
258 	uint16_t new_chunk;
259 	uint16_t *nchunkp = &new_chunk;
260 
261 	while (chunk != CHAIN_END) {
262 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
263 		uint16_t nchunk = zap_leaf_chunk_alloc(nl);
264 
265 		struct zap_leaf_array *la =
266 		    &ZAP_LEAF_CHUNK(l, chunk).l_array;
267 		struct zap_leaf_array *nla =
268 		    &ZAP_LEAF_CHUNK(nl, nchunk).l_array;
269 		ASSERT3U(la->la_type, ==, ZAP_CHUNK_ARRAY);
270 
271 		*nla = *la; /* structure assignment */
272 
273 		chunk = la->la_next;
274 		*nchunkp = nchunk;
275 		nchunkp = &nla->la_next;
276 	}
277 	*nchunkp = CHAIN_END;
278 	return (new_chunk);
279 }
280 
281 /*
282  * Free array.  Unlike trivial loop of zap_leaf_chunk_free() this does
283  * not reverse order of chunks in the free list, reducing fragmentation.
284  */
285 static void
zap_leaf_array_free(zap_leaf_t * l,uint16_t chunk)286 zap_leaf_array_free(zap_leaf_t *l, uint16_t chunk)
287 {
288 	struct zap_leaf_header *hdr = &zap_leaf_phys(l)->l_hdr;
289 	uint16_t *tailp = &hdr->lh_freelist;
290 	uint16_t oldfree = *tailp;
291 
292 	while (chunk != CHAIN_END) {
293 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
294 		zap_leaf_chunk_t *c = &ZAP_LEAF_CHUNK(l, chunk);
295 		ASSERT3U(c->l_array.la_type, ==, ZAP_CHUNK_ARRAY);
296 
297 		*tailp = chunk;
298 		chunk = c->l_array.la_next;
299 
300 		c->l_free.lf_type = ZAP_CHUNK_FREE;
301 		memset(c->l_free.lf_pad, 0, sizeof (c->l_free.lf_pad));
302 		tailp = &c->l_free.lf_next;
303 
304 		ASSERT3U(hdr->lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l));
305 		hdr->lh_nfree++;
306 	}
307 
308 	*tailp = oldfree;
309 }
310 
311 /* array_len and buf_len are in integers, not bytes */
312 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)313 zap_leaf_array_read(zap_leaf_t *l, uint16_t chunk,
314     int array_int_len, int array_len, int buf_int_len, uint64_t buf_len,
315     void *buf)
316 {
317 	int len = MIN(array_len, buf_len);
318 	int byten = 0;
319 	uint64_t value = 0;
320 	char *p = buf;
321 
322 	ASSERT3U(array_int_len, <=, buf_int_len);
323 
324 	/* Fast path for one 8-byte integer */
325 	if (array_int_len == 8 && buf_int_len == 8 && len == 1) {
326 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
327 		uint8_t *ip = la->la_array;
328 		uint64_t *buf64 = buf;
329 
330 		*buf64 = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
331 		    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
332 		    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
333 		    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
334 		return;
335 	}
336 
337 	/* Fast path for an array of 1-byte integers (eg. the entry name) */
338 	if (array_int_len == 1 && buf_int_len == 1 &&
339 	    buf_len > array_len + ZAP_LEAF_ARRAY_BYTES) {
340 		while (chunk != CHAIN_END) {
341 			struct zap_leaf_array *la =
342 			    &ZAP_LEAF_CHUNK(l, chunk).l_array;
343 			memcpy(p, la->la_array, ZAP_LEAF_ARRAY_BYTES);
344 			p += ZAP_LEAF_ARRAY_BYTES;
345 			chunk = la->la_next;
346 		}
347 		return;
348 	}
349 
350 	while (len > 0) {
351 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
352 
353 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
354 		for (int i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) {
355 			value = (value << 8) | la->la_array[i];
356 			byten++;
357 			if (byten == array_int_len) {
358 				stv(buf_int_len, p, value);
359 				byten = 0;
360 				len--;
361 				if (len == 0)
362 					return;
363 				p += buf_int_len;
364 			}
365 		}
366 		chunk = la->la_next;
367 	}
368 }
369 
370 static boolean_t
zap_leaf_array_match(zap_leaf_t * l,zap_name_t * zn,uint_t chunk,int array_numints)371 zap_leaf_array_match(zap_leaf_t *l, zap_name_t *zn,
372     uint_t chunk, int array_numints)
373 {
374 	int bseen = 0;
375 
376 	if (zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY) {
377 		uint64_t *thiskey =
378 		    kmem_alloc(array_numints * sizeof (*thiskey), KM_SLEEP);
379 		ASSERT(zn->zn_key_intlen == sizeof (*thiskey));
380 
381 		zap_leaf_array_read(l, chunk, sizeof (*thiskey), array_numints,
382 		    sizeof (*thiskey), array_numints, thiskey);
383 		boolean_t match = memcmp(thiskey, zn->zn_key_orig,
384 		    array_numints * sizeof (*thiskey)) == 0;
385 		kmem_free(thiskey, array_numints * sizeof (*thiskey));
386 		return (match);
387 	}
388 
389 	ASSERT(zn->zn_key_intlen == 1);
390 	if (zn->zn_matchtype & MT_NORMALIZE) {
391 		char *thisname = kmem_alloc(array_numints, KM_SLEEP);
392 
393 		zap_leaf_array_read(l, chunk, sizeof (char), array_numints,
394 		    sizeof (char), array_numints, thisname);
395 		boolean_t match = zap_match(zn, thisname);
396 		kmem_free(thisname, array_numints);
397 		return (match);
398 	}
399 
400 	/*
401 	 * Fast path for exact matching.
402 	 * First check that the lengths match, so that we don't read
403 	 * past the end of the zn_key_orig array.
404 	 */
405 	if (array_numints != zn->zn_key_orig_numints)
406 		return (B_FALSE);
407 	while (bseen < array_numints) {
408 		struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
409 		int toread = MIN(array_numints - bseen, ZAP_LEAF_ARRAY_BYTES);
410 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
411 		if (memcmp(la->la_array, (char *)zn->zn_key_orig + bseen,
412 		    toread))
413 			break;
414 		chunk = la->la_next;
415 		bseen += toread;
416 	}
417 	return (bseen == array_numints);
418 }
419 
420 /*
421  * Routines which manipulate leaf entries.
422  */
423 
424 int
zap_leaf_lookup(zap_leaf_t * l,zap_name_t * zn,zap_entry_handle_t * zeh)425 zap_leaf_lookup(zap_leaf_t *l, zap_name_t *zn, zap_entry_handle_t *zeh)
426 {
427 	struct zap_leaf_entry *le;
428 
429 	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
430 
431 	for (uint16_t *chunkp = LEAF_HASH_ENTPTR(l, zn->zn_hash);
432 	    *chunkp != CHAIN_END; chunkp = &le->le_next) {
433 		uint16_t chunk = *chunkp;
434 		le = ZAP_LEAF_ENTRY(l, chunk);
435 
436 		ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
437 		ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
438 
439 		if (le->le_hash != zn->zn_hash)
440 			continue;
441 
442 		/*
443 		 * NB: the entry chain is always sorted by cd on
444 		 * normalized zap objects, so this will find the
445 		 * lowest-cd match for MT_NORMALIZE.
446 		 */
447 		ASSERT((zn->zn_matchtype == 0) ||
448 		    (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED));
449 		if (zap_leaf_array_match(l, zn, le->le_name_chunk,
450 		    le->le_name_numints)) {
451 			zeh->zeh_num_integers = le->le_value_numints;
452 			zeh->zeh_integer_size = le->le_value_intlen;
453 			zeh->zeh_cd = le->le_cd;
454 			zeh->zeh_hash = le->le_hash;
455 			zeh->zeh_chunkp = chunkp;
456 			zeh->zeh_leaf = l;
457 			return (0);
458 		}
459 	}
460 
461 	return (SET_ERROR(ENOENT));
462 }
463 
464 /* Return (h1,cd1 >= h2,cd2) */
465 #define	HCD_GTEQ(h1, cd1, h2, cd2) \
466 	((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
467 
468 int
zap_leaf_lookup_closest(zap_leaf_t * l,uint64_t h,uint32_t cd,zap_entry_handle_t * zeh)469 zap_leaf_lookup_closest(zap_leaf_t *l,
470     uint64_t h, uint32_t cd, zap_entry_handle_t *zeh)
471 {
472 	uint64_t besth = -1ULL;
473 	uint32_t bestcd = -1U;
474 	uint16_t bestlh = ZAP_LEAF_HASH_NUMENTRIES(l)-1;
475 	struct zap_leaf_entry *le;
476 
477 	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
478 
479 	for (uint16_t lh = LEAF_HASH(l, h); lh <= bestlh; lh++) {
480 		for (uint16_t chunk = zap_leaf_phys(l)->l_hash[lh];
481 		    chunk != CHAIN_END; chunk = le->le_next) {
482 			le = ZAP_LEAF_ENTRY(l, chunk);
483 
484 			ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
485 			ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
486 
487 			if (HCD_GTEQ(le->le_hash, le->le_cd, h, cd) &&
488 			    HCD_GTEQ(besth, bestcd, le->le_hash, le->le_cd)) {
489 				ASSERT3U(bestlh, >=, lh);
490 				bestlh = lh;
491 				besth = le->le_hash;
492 				bestcd = le->le_cd;
493 
494 				zeh->zeh_num_integers = le->le_value_numints;
495 				zeh->zeh_integer_size = le->le_value_intlen;
496 				zeh->zeh_cd = le->le_cd;
497 				zeh->zeh_hash = le->le_hash;
498 				zeh->zeh_fakechunk = chunk;
499 				zeh->zeh_chunkp = &zeh->zeh_fakechunk;
500 				zeh->zeh_leaf = l;
501 			}
502 		}
503 	}
504 
505 	return (bestcd == -1U ? SET_ERROR(ENOENT) : 0);
506 }
507 
508 int
zap_entry_read(const zap_entry_handle_t * zeh,uint8_t integer_size,uint64_t num_integers,void * buf)509 zap_entry_read(const zap_entry_handle_t *zeh,
510     uint8_t integer_size, uint64_t num_integers, void *buf)
511 {
512 	struct zap_leaf_entry *le =
513 	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
514 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
515 
516 	if (le->le_value_intlen > integer_size)
517 		return (SET_ERROR(EINVAL));
518 
519 	zap_leaf_array_read(zeh->zeh_leaf, le->le_value_chunk,
520 	    le->le_value_intlen, le->le_value_numints,
521 	    integer_size, num_integers, buf);
522 
523 	if (zeh->zeh_num_integers > num_integers)
524 		return (SET_ERROR(EOVERFLOW));
525 	return (0);
526 
527 }
528 
529 int
zap_entry_read_name(zap_t * zap,const zap_entry_handle_t * zeh,uint16_t buflen,char * buf)530 zap_entry_read_name(zap_t *zap, const zap_entry_handle_t *zeh, uint16_t buflen,
531     char *buf)
532 {
533 	struct zap_leaf_entry *le =
534 	    ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
535 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
536 
537 	if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
538 		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 8,
539 		    le->le_name_numints, 8, buflen / 8, buf);
540 	} else {
541 		zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 1,
542 		    le->le_name_numints, 1, buflen, buf);
543 	}
544 	if (le->le_name_numints > buflen)
545 		return (SET_ERROR(EOVERFLOW));
546 	return (0);
547 }
548 
549 int
zap_entry_update(zap_entry_handle_t * zeh,uint8_t integer_size,uint64_t num_integers,const void * buf)550 zap_entry_update(zap_entry_handle_t *zeh,
551     uint8_t integer_size, uint64_t num_integers, const void *buf)
552 {
553 	zap_leaf_t *l = zeh->zeh_leaf;
554 	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, *zeh->zeh_chunkp);
555 
556 	int delta_chunks = ZAP_LEAF_ARRAY_NCHUNKS(num_integers * integer_size) -
557 	    ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_numints * le->le_value_intlen);
558 
559 	if ((int)zap_leaf_phys(l)->l_hdr.lh_nfree < delta_chunks)
560 		return (SET_ERROR(EAGAIN));
561 
562 	zap_leaf_array_free(l, le->le_value_chunk);
563 	le->le_value_chunk =
564 	    zap_leaf_array_create(l, buf, integer_size, num_integers);
565 	le->le_value_numints = num_integers;
566 	le->le_value_intlen = integer_size;
567 	return (0);
568 }
569 
570 void
zap_entry_remove(zap_entry_handle_t * zeh)571 zap_entry_remove(zap_entry_handle_t *zeh)
572 {
573 	zap_leaf_t *l = zeh->zeh_leaf;
574 
575 	ASSERT3P(zeh->zeh_chunkp, !=, &zeh->zeh_fakechunk);
576 
577 	uint16_t entry_chunk = *zeh->zeh_chunkp;
578 	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry_chunk);
579 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
580 
581 	*zeh->zeh_chunkp = le->le_next;
582 
583 	/* Free in opposite order to reduce fragmentation. */
584 	zap_leaf_array_free(l, le->le_value_chunk);
585 	zap_leaf_array_free(l, le->le_name_chunk);
586 	zap_leaf_chunk_free(l, entry_chunk);
587 
588 	zap_leaf_phys(l)->l_hdr.lh_nentries--;
589 }
590 
591 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)592 zap_entry_create(zap_leaf_t *l, zap_name_t *zn, uint32_t cd,
593     uint8_t integer_size, uint64_t num_integers, const void *buf,
594     zap_entry_handle_t *zeh)
595 {
596 	uint16_t chunk;
597 	struct zap_leaf_entry *le;
598 	uint64_t h = zn->zn_hash;
599 
600 	uint64_t valuelen = integer_size * num_integers;
601 
602 	uint_t numchunks = 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn->zn_key_orig_numints *
603 	    zn->zn_key_intlen) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen);
604 	if (numchunks > ZAP_LEAF_NUMCHUNKS(l))
605 		return (SET_ERROR(E2BIG));
606 
607 	if (cd == ZAP_NEED_CD) {
608 		/* find the lowest unused cd */
609 		if (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED) {
610 			cd = 0;
611 
612 			for (chunk = *LEAF_HASH_ENTPTR(l, h);
613 			    chunk != CHAIN_END; chunk = le->le_next) {
614 				le = ZAP_LEAF_ENTRY(l, chunk);
615 				if (le->le_cd > cd)
616 					break;
617 				if (le->le_hash == h) {
618 					ASSERT3U(cd, ==, le->le_cd);
619 					cd++;
620 				}
621 			}
622 		} else {
623 			/* old unsorted format; do it the O(n^2) way */
624 			for (cd = 0; ; cd++) {
625 				for (chunk = *LEAF_HASH_ENTPTR(l, h);
626 				    chunk != CHAIN_END; chunk = le->le_next) {
627 					le = ZAP_LEAF_ENTRY(l, chunk);
628 					if (le->le_hash == h &&
629 					    le->le_cd == cd) {
630 						break;
631 					}
632 				}
633 				/* If this cd is not in use, we are good. */
634 				if (chunk == CHAIN_END)
635 					break;
636 			}
637 		}
638 		/*
639 		 * We would run out of space in a block before we could
640 		 * store enough entries to run out of CD values.
641 		 */
642 		ASSERT3U(cd, <, zap_maxcd(zn->zn_zap));
643 	}
644 
645 	if (zap_leaf_phys(l)->l_hdr.lh_nfree < numchunks)
646 		return (SET_ERROR(EAGAIN));
647 
648 	/* make the entry */
649 	chunk = zap_leaf_chunk_alloc(l);
650 	le = ZAP_LEAF_ENTRY(l, chunk);
651 	le->le_type = ZAP_CHUNK_ENTRY;
652 	le->le_name_chunk = zap_leaf_array_create(l, zn->zn_key_orig,
653 	    zn->zn_key_intlen, zn->zn_key_orig_numints);
654 	le->le_name_numints = zn->zn_key_orig_numints;
655 	le->le_value_chunk =
656 	    zap_leaf_array_create(l, buf, integer_size, num_integers);
657 	le->le_value_numints = num_integers;
658 	le->le_value_intlen = integer_size;
659 	le->le_hash = h;
660 	le->le_cd = cd;
661 
662 	/* link it into the hash chain */
663 	/* XXX if we did the search above, we could just use that */
664 	uint16_t *chunkp = zap_leaf_rehash_entry(l, le, chunk);
665 
666 	zap_leaf_phys(l)->l_hdr.lh_nentries++;
667 
668 	zeh->zeh_leaf = l;
669 	zeh->zeh_num_integers = num_integers;
670 	zeh->zeh_integer_size = le->le_value_intlen;
671 	zeh->zeh_cd = le->le_cd;
672 	zeh->zeh_hash = le->le_hash;
673 	zeh->zeh_chunkp = chunkp;
674 
675 	return (0);
676 }
677 
678 /*
679  * Determine if there is another entry with the same normalized form.
680  * For performance purposes, either zn or name must be provided (the
681  * other can be NULL).  Note, there usually won't be any hash
682  * conflicts, in which case we don't need the concatenated/normalized
683  * form of the name.  But all callers have one of these on hand anyway,
684  * so might as well take advantage.  A cleaner but slower interface
685  * would accept neither argument, and compute the normalized name as
686  * needed (using zap_name_alloc_str(zap_entry_read_name(zeh))).
687  */
688 boolean_t
zap_entry_normalization_conflict(zap_entry_handle_t * zeh,zap_name_t * zn,const char * name,zap_t * zap)689 zap_entry_normalization_conflict(zap_entry_handle_t *zeh, zap_name_t *zn,
690     const char *name, zap_t *zap)
691 {
692 	struct zap_leaf_entry *le;
693 	boolean_t allocdzn = B_FALSE;
694 
695 	if (zap->zap_normflags == 0)
696 		return (B_FALSE);
697 
698 	for (uint16_t chunk = *LEAF_HASH_ENTPTR(zeh->zeh_leaf, zeh->zeh_hash);
699 	    chunk != CHAIN_END; chunk = le->le_next) {
700 		le = ZAP_LEAF_ENTRY(zeh->zeh_leaf, chunk);
701 		if (le->le_hash != zeh->zeh_hash)
702 			continue;
703 		if (le->le_cd == zeh->zeh_cd)
704 			continue;
705 
706 		if (zn == NULL) {
707 			zn = zap_name_alloc_str(zap, name, MT_NORMALIZE);
708 			allocdzn = B_TRUE;
709 		}
710 		if (zap_leaf_array_match(zeh->zeh_leaf, zn,
711 		    le->le_name_chunk, le->le_name_numints)) {
712 			if (allocdzn)
713 				zap_name_free(zn);
714 			return (B_TRUE);
715 		}
716 	}
717 	if (allocdzn)
718 		zap_name_free(zn);
719 	return (B_FALSE);
720 }
721 
722 /*
723  * Routines for transferring entries between leafs.
724  */
725 
726 static uint16_t *
zap_leaf_rehash_entry(zap_leaf_t * l,struct zap_leaf_entry * le,uint16_t entry)727 zap_leaf_rehash_entry(zap_leaf_t *l, struct zap_leaf_entry *le, uint16_t entry)
728 {
729 	struct zap_leaf_entry *le2;
730 	uint16_t *chunkp;
731 
732 	/*
733 	 * keep the entry chain sorted by cd
734 	 * NB: this will not cause problems for unsorted leafs, though
735 	 * it is unnecessary there.
736 	 */
737 	for (chunkp = LEAF_HASH_ENTPTR(l, le->le_hash);
738 	    *chunkp != CHAIN_END; chunkp = &le2->le_next) {
739 		le2 = ZAP_LEAF_ENTRY(l, *chunkp);
740 		if (le2->le_cd > le->le_cd)
741 			break;
742 	}
743 
744 	le->le_next = *chunkp;
745 	*chunkp = entry;
746 	return (chunkp);
747 }
748 
749 static void
zap_leaf_transfer_entry(zap_leaf_t * l,uint_t entry,zap_leaf_t * nl)750 zap_leaf_transfer_entry(zap_leaf_t *l, uint_t entry, zap_leaf_t *nl)
751 {
752 	struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry);
753 	ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
754 
755 	uint16_t chunk = zap_leaf_chunk_alloc(nl);
756 	struct zap_leaf_entry *nle = ZAP_LEAF_ENTRY(nl, chunk);
757 	*nle = *le; /* structure assignment */
758 
759 	(void) zap_leaf_rehash_entry(nl, nle, chunk);
760 
761 	nle->le_name_chunk = zap_leaf_array_copy(l, le->le_name_chunk, nl);
762 	nle->le_value_chunk = zap_leaf_array_copy(l, le->le_value_chunk, nl);
763 
764 	/* Free in opposite order to reduce fragmentation. */
765 	zap_leaf_array_free(l, le->le_value_chunk);
766 	zap_leaf_array_free(l, le->le_name_chunk);
767 	zap_leaf_chunk_free(l, entry);
768 
769 	zap_leaf_phys(l)->l_hdr.lh_nentries--;
770 	zap_leaf_phys(nl)->l_hdr.lh_nentries++;
771 }
772 
773 /*
774  * Transfer the entries whose hash prefix ends in 1 to the new leaf.
775  */
776 void
zap_leaf_split(zap_leaf_t * l,zap_leaf_t * nl,boolean_t sort)777 zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort)
778 {
779 	uint_t bit = 64 - 1 - zap_leaf_phys(l)->l_hdr.lh_prefix_len;
780 
781 	/* set new prefix and prefix_len */
782 	zap_leaf_phys(l)->l_hdr.lh_prefix <<= 1;
783 	zap_leaf_phys(l)->l_hdr.lh_prefix_len++;
784 	zap_leaf_phys(nl)->l_hdr.lh_prefix =
785 	    zap_leaf_phys(l)->l_hdr.lh_prefix | 1;
786 	zap_leaf_phys(nl)->l_hdr.lh_prefix_len =
787 	    zap_leaf_phys(l)->l_hdr.lh_prefix_len;
788 
789 	/* break existing hash chains */
790 	memset(zap_leaf_phys(l)->l_hash, CHAIN_END,
791 	    2*ZAP_LEAF_HASH_NUMENTRIES(l));
792 
793 	if (sort)
794 		zap_leaf_phys(l)->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
795 
796 	/*
797 	 * Transfer entries whose hash bit 'bit' is set to nl; rehash
798 	 * the remaining entries
799 	 *
800 	 * NB: We could find entries via the hashtable instead. That
801 	 * would be O(hashents+numents) rather than O(numblks+numents),
802 	 * but this accesses memory more sequentially, and when we're
803 	 * called, the block is usually pretty full.
804 	 */
805 	for (uint_t i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
806 		struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, i);
807 		if (le->le_type != ZAP_CHUNK_ENTRY)
808 			continue;
809 
810 		if (le->le_hash & (1ULL << bit))
811 			zap_leaf_transfer_entry(l, i, nl);
812 		else
813 			(void) zap_leaf_rehash_entry(l, le, i);
814 	}
815 }
816 
817 void
zap_leaf_stats(zap_t * zap,zap_leaf_t * l,zap_stats_t * zs)818 zap_leaf_stats(zap_t *zap, zap_leaf_t *l, zap_stats_t *zs)
819 {
820 	uint_t n = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
821 	    zap_leaf_phys(l)->l_hdr.lh_prefix_len;
822 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
823 	zs->zs_leafs_with_2n_pointers[n]++;
824 
825 
826 	n = zap_leaf_phys(l)->l_hdr.lh_nentries/5;
827 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
828 	zs->zs_blocks_with_n5_entries[n]++;
829 
830 	n = ((1<<FZAP_BLOCK_SHIFT(zap)) -
831 	    zap_leaf_phys(l)->l_hdr.lh_nfree * (ZAP_LEAF_ARRAY_BYTES+1))*10 /
832 	    (1<<FZAP_BLOCK_SHIFT(zap));
833 	n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
834 	zs->zs_blocks_n_tenths_full[n]++;
835 
836 	for (uint_t i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(l); i++) {
837 		uint_t nentries = 0;
838 		uint_t chunk = zap_leaf_phys(l)->l_hash[i];
839 
840 		while (chunk != CHAIN_END) {
841 			struct zap_leaf_entry *le =
842 			    ZAP_LEAF_ENTRY(l, chunk);
843 
844 			n = 1 + ZAP_LEAF_ARRAY_NCHUNKS(le->le_name_numints) +
845 			    ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_numints *
846 			    le->le_value_intlen);
847 			n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
848 			zs->zs_entries_using_n_chunks[n]++;
849 
850 			chunk = le->le_next;
851 			nentries++;
852 		}
853 
854 		n = nentries;
855 		n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
856 		zs->zs_buckets_with_n_entries[n]++;
857 	}
858 }
859