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