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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
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.
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
40 #include <sys/zap_impl.h>
41 #include <sys/zap_leaf.h>
44 static uint16_t *zap_leaf_rehash_entry(zap_leaf_t
*l
, struct zap_leaf_entry
*le
,
47 #define CHAIN_END 0xffff /* end of the chunk chain */
49 #define LEAF_HASH(l, h) \
50 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
52 (64 - ZAP_LEAF_HASH_SHIFT(l) - zap_leaf_phys(l)->l_hdr.lh_prefix_len)))
54 #define LEAF_HASH_ENTPTR(l, h) (&zap_leaf_phys(l)->l_hash[LEAF_HASH(l, h)])
57 stv(int len
, void *addr
, uint64_t value
)
61 *(uint8_t *)addr
= value
;
64 *(uint16_t *)addr
= value
;
67 *(uint32_t *)addr
= value
;
70 *(uint64_t *)addr
= value
;
73 PANIC("bad int len %d", len
);
78 ldv(int len
, const void *addr
)
82 return (*(uint8_t *)addr
);
84 return (*(uint16_t *)addr
);
86 return (*(uint32_t *)addr
);
88 return (*(uint64_t *)addr
);
90 PANIC("bad int len %d", len
);
92 return (0xFEEDFACEDEADBEEFULL
);
96 zap_leaf_byteswap(zap_leaf_phys_t
*buf
, size_t size
)
101 l_dbuf
.db_data
= buf
;
102 l
.l_bs
= highbit64(size
) - 1;
105 buf
->l_hdr
.lh_block_type
= BSWAP_64(buf
->l_hdr
.lh_block_type
);
106 buf
->l_hdr
.lh_prefix
= BSWAP_64(buf
->l_hdr
.lh_prefix
);
107 buf
->l_hdr
.lh_magic
= BSWAP_32(buf
->l_hdr
.lh_magic
);
108 buf
->l_hdr
.lh_nfree
= BSWAP_16(buf
->l_hdr
.lh_nfree
);
109 buf
->l_hdr
.lh_nentries
= BSWAP_16(buf
->l_hdr
.lh_nentries
);
110 buf
->l_hdr
.lh_prefix_len
= BSWAP_16(buf
->l_hdr
.lh_prefix_len
);
111 buf
->l_hdr
.lh_freelist
= BSWAP_16(buf
->l_hdr
.lh_freelist
);
113 for (uint_t i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(&l
); i
++)
114 buf
->l_hash
[i
] = BSWAP_16(buf
->l_hash
[i
]);
116 for (uint_t i
= 0; i
< ZAP_LEAF_NUMCHUNKS(&l
); i
++) {
117 zap_leaf_chunk_t
*lc
= &ZAP_LEAF_CHUNK(&l
, i
);
118 struct zap_leaf_entry
*le
;
120 switch (lc
->l_free
.lf_type
) {
121 case ZAP_CHUNK_ENTRY
:
124 le
->le_type
= BSWAP_8(le
->le_type
);
125 le
->le_value_intlen
= BSWAP_8(le
->le_value_intlen
);
126 le
->le_next
= BSWAP_16(le
->le_next
);
127 le
->le_name_chunk
= BSWAP_16(le
->le_name_chunk
);
128 le
->le_name_numints
= BSWAP_16(le
->le_name_numints
);
129 le
->le_value_chunk
= BSWAP_16(le
->le_value_chunk
);
130 le
->le_value_numints
= BSWAP_16(le
->le_value_numints
);
131 le
->le_cd
= BSWAP_32(le
->le_cd
);
132 le
->le_hash
= BSWAP_64(le
->le_hash
);
135 lc
->l_free
.lf_type
= BSWAP_8(lc
->l_free
.lf_type
);
136 lc
->l_free
.lf_next
= BSWAP_16(lc
->l_free
.lf_next
);
138 case ZAP_CHUNK_ARRAY
:
139 lc
->l_array
.la_type
= BSWAP_8(lc
->l_array
.la_type
);
140 lc
->l_array
.la_next
= BSWAP_16(lc
->l_array
.la_next
);
141 /* la_array doesn't need swapping */
144 cmn_err(CE_PANIC
, "bad leaf type %d",
151 zap_leaf_init(zap_leaf_t
*l
, boolean_t sort
)
153 l
->l_bs
= highbit64(l
->l_dbuf
->db_size
) - 1;
154 memset(&zap_leaf_phys(l
)->l_hdr
, 0,
155 sizeof (struct zap_leaf_header
));
156 memset(zap_leaf_phys(l
)->l_hash
, CHAIN_END
,
157 2*ZAP_LEAF_HASH_NUMENTRIES(l
));
158 for (uint_t i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
159 ZAP_LEAF_CHUNK(l
, i
).l_free
.lf_type
= ZAP_CHUNK_FREE
;
160 ZAP_LEAF_CHUNK(l
, i
).l_free
.lf_next
= i
+1;
162 ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)-1).l_free
.lf_next
= CHAIN_END
;
163 zap_leaf_phys(l
)->l_hdr
.lh_block_type
= ZBT_LEAF
;
164 zap_leaf_phys(l
)->l_hdr
.lh_magic
= ZAP_LEAF_MAGIC
;
165 zap_leaf_phys(l
)->l_hdr
.lh_nfree
= ZAP_LEAF_NUMCHUNKS(l
);
167 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
171 * Routines which manipulate leaf chunks (l_chunk[]).
175 zap_leaf_chunk_alloc(zap_leaf_t
*l
)
177 ASSERT(zap_leaf_phys(l
)->l_hdr
.lh_nfree
> 0);
179 uint_t chunk
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
180 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
181 ASSERT3U(ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_type
, ==, ZAP_CHUNK_FREE
);
183 zap_leaf_phys(l
)->l_hdr
.lh_freelist
=
184 ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_next
;
186 zap_leaf_phys(l
)->l_hdr
.lh_nfree
--;
192 zap_leaf_chunk_free(zap_leaf_t
*l
, uint16_t chunk
)
194 struct zap_leaf_free
*zlf
= &ZAP_LEAF_CHUNK(l
, chunk
).l_free
;
195 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_nfree
, <, ZAP_LEAF_NUMCHUNKS(l
));
196 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
197 ASSERT(zlf
->lf_type
!= ZAP_CHUNK_FREE
);
199 zlf
->lf_type
= ZAP_CHUNK_FREE
;
200 zlf
->lf_next
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
201 memset(zlf
->lf_pad
, 0, sizeof (zlf
->lf_pad
)); /* help it to compress */
202 zap_leaf_phys(l
)->l_hdr
.lh_freelist
= chunk
;
204 zap_leaf_phys(l
)->l_hdr
.lh_nfree
++;
208 * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
212 zap_leaf_array_create(zap_leaf_t
*l
, const char *buf
,
213 int integer_size
, int num_integers
)
216 uint16_t *chunkp
= &chunk_head
;
217 int byten
= integer_size
;
219 int shift
= (integer_size
- 1) * 8;
220 int len
= num_integers
;
222 ASSERT3U(num_integers
* integer_size
, <=, ZAP_MAXVALUELEN
);
225 value
= ldv(integer_size
, buf
);
227 uint16_t chunk
= zap_leaf_chunk_alloc(l
);
228 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
230 la
->la_type
= ZAP_CHUNK_ARRAY
;
231 for (int i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
; i
++) {
232 la
->la_array
[i
] = value
>> shift
;
237 byten
= integer_size
;
239 value
= ldv(integer_size
, buf
);
244 chunkp
= &la
->la_next
;
252 zap_leaf_array_free(zap_leaf_t
*l
, uint16_t *chunkp
)
254 uint16_t chunk
= *chunkp
;
258 while (chunk
!= CHAIN_END
) {
259 uint_t nextchunk
= ZAP_LEAF_CHUNK(l
, chunk
).l_array
.la_next
;
260 ASSERT3U(ZAP_LEAF_CHUNK(l
, chunk
).l_array
.la_type
, ==,
262 zap_leaf_chunk_free(l
, chunk
);
267 /* array_len and buf_len are in integers, not bytes */
269 zap_leaf_array_read(zap_leaf_t
*l
, uint16_t chunk
,
270 int array_int_len
, int array_len
, int buf_int_len
, uint64_t buf_len
,
273 int len
= MIN(array_len
, buf_len
);
278 ASSERT3U(array_int_len
, <=, buf_int_len
);
280 /* Fast path for one 8-byte integer */
281 if (array_int_len
== 8 && buf_int_len
== 8 && len
== 1) {
282 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
283 uint8_t *ip
= la
->la_array
;
284 uint64_t *buf64
= buf
;
286 *buf64
= (uint64_t)ip
[0] << 56 | (uint64_t)ip
[1] << 48 |
287 (uint64_t)ip
[2] << 40 | (uint64_t)ip
[3] << 32 |
288 (uint64_t)ip
[4] << 24 | (uint64_t)ip
[5] << 16 |
289 (uint64_t)ip
[6] << 8 | (uint64_t)ip
[7];
293 /* Fast path for an array of 1-byte integers (eg. the entry name) */
294 if (array_int_len
== 1 && buf_int_len
== 1 &&
295 buf_len
> array_len
+ ZAP_LEAF_ARRAY_BYTES
) {
296 while (chunk
!= CHAIN_END
) {
297 struct zap_leaf_array
*la
=
298 &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
299 memcpy(p
, la
->la_array
, ZAP_LEAF_ARRAY_BYTES
);
300 p
+= ZAP_LEAF_ARRAY_BYTES
;
307 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
309 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
310 for (int i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
; i
++) {
311 value
= (value
<< 8) | la
->la_array
[i
];
313 if (byten
== array_int_len
) {
314 stv(buf_int_len
, p
, value
);
327 zap_leaf_array_match(zap_leaf_t
*l
, zap_name_t
*zn
,
328 uint_t chunk
, int array_numints
)
332 if (zap_getflags(zn
->zn_zap
) & ZAP_FLAG_UINT64_KEY
) {
334 kmem_alloc(array_numints
* sizeof (*thiskey
), KM_SLEEP
);
335 ASSERT(zn
->zn_key_intlen
== sizeof (*thiskey
));
337 zap_leaf_array_read(l
, chunk
, sizeof (*thiskey
), array_numints
,
338 sizeof (*thiskey
), array_numints
, thiskey
);
339 boolean_t match
= memcmp(thiskey
, zn
->zn_key_orig
,
340 array_numints
* sizeof (*thiskey
)) == 0;
341 kmem_free(thiskey
, array_numints
* sizeof (*thiskey
));
345 ASSERT(zn
->zn_key_intlen
== 1);
346 if (zn
->zn_matchtype
& MT_NORMALIZE
) {
347 char *thisname
= kmem_alloc(array_numints
, KM_SLEEP
);
349 zap_leaf_array_read(l
, chunk
, sizeof (char), array_numints
,
350 sizeof (char), array_numints
, thisname
);
351 boolean_t match
= zap_match(zn
, thisname
);
352 kmem_free(thisname
, array_numints
);
357 * Fast path for exact matching.
358 * First check that the lengths match, so that we don't read
359 * past the end of the zn_key_orig array.
361 if (array_numints
!= zn
->zn_key_orig_numints
)
363 while (bseen
< array_numints
) {
364 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
365 int toread
= MIN(array_numints
- bseen
, ZAP_LEAF_ARRAY_BYTES
);
366 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
367 if (memcmp(la
->la_array
, (char *)zn
->zn_key_orig
+ bseen
,
373 return (bseen
== array_numints
);
377 * Routines which manipulate leaf entries.
381 zap_leaf_lookup(zap_leaf_t
*l
, zap_name_t
*zn
, zap_entry_handle_t
*zeh
)
383 struct zap_leaf_entry
*le
;
385 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
387 for (uint16_t *chunkp
= LEAF_HASH_ENTPTR(l
, zn
->zn_hash
);
388 *chunkp
!= CHAIN_END
; chunkp
= &le
->le_next
) {
389 uint16_t chunk
= *chunkp
;
390 le
= ZAP_LEAF_ENTRY(l
, chunk
);
392 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
393 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
395 if (le
->le_hash
!= zn
->zn_hash
)
399 * NB: the entry chain is always sorted by cd on
400 * normalized zap objects, so this will find the
401 * lowest-cd match for MT_NORMALIZE.
403 ASSERT((zn
->zn_matchtype
== 0) ||
404 (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
));
405 if (zap_leaf_array_match(l
, zn
, le
->le_name_chunk
,
406 le
->le_name_numints
)) {
407 zeh
->zeh_num_integers
= le
->le_value_numints
;
408 zeh
->zeh_integer_size
= le
->le_value_intlen
;
409 zeh
->zeh_cd
= le
->le_cd
;
410 zeh
->zeh_hash
= le
->le_hash
;
411 zeh
->zeh_chunkp
= chunkp
;
417 return (SET_ERROR(ENOENT
));
420 /* Return (h1,cd1 >= h2,cd2) */
421 #define HCD_GTEQ(h1, cd1, h2, cd2) \
422 ((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
425 zap_leaf_lookup_closest(zap_leaf_t
*l
,
426 uint64_t h
, uint32_t cd
, zap_entry_handle_t
*zeh
)
428 uint64_t besth
= -1ULL;
429 uint32_t bestcd
= -1U;
430 uint16_t bestlh
= ZAP_LEAF_HASH_NUMENTRIES(l
)-1;
431 struct zap_leaf_entry
*le
;
433 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
435 for (uint16_t lh
= LEAF_HASH(l
, h
); lh
<= bestlh
; lh
++) {
436 for (uint16_t chunk
= zap_leaf_phys(l
)->l_hash
[lh
];
437 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
438 le
= ZAP_LEAF_ENTRY(l
, chunk
);
440 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
441 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
443 if (HCD_GTEQ(le
->le_hash
, le
->le_cd
, h
, cd
) &&
444 HCD_GTEQ(besth
, bestcd
, le
->le_hash
, le
->le_cd
)) {
445 ASSERT3U(bestlh
, >=, lh
);
450 zeh
->zeh_num_integers
= le
->le_value_numints
;
451 zeh
->zeh_integer_size
= le
->le_value_intlen
;
452 zeh
->zeh_cd
= le
->le_cd
;
453 zeh
->zeh_hash
= le
->le_hash
;
454 zeh
->zeh_fakechunk
= chunk
;
455 zeh
->zeh_chunkp
= &zeh
->zeh_fakechunk
;
461 return (bestcd
== -1U ? SET_ERROR(ENOENT
) : 0);
465 zap_entry_read(const zap_entry_handle_t
*zeh
,
466 uint8_t integer_size
, uint64_t num_integers
, void *buf
)
468 struct zap_leaf_entry
*le
=
469 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
470 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
472 if (le
->le_value_intlen
> integer_size
)
473 return (SET_ERROR(EINVAL
));
475 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_value_chunk
,
476 le
->le_value_intlen
, le
->le_value_numints
,
477 integer_size
, num_integers
, buf
);
479 if (zeh
->zeh_num_integers
> num_integers
)
480 return (SET_ERROR(EOVERFLOW
));
486 zap_entry_read_name(zap_t
*zap
, const zap_entry_handle_t
*zeh
, uint16_t buflen
,
489 struct zap_leaf_entry
*le
=
490 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
491 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
493 if (zap_getflags(zap
) & ZAP_FLAG_UINT64_KEY
) {
494 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_name_chunk
, 8,
495 le
->le_name_numints
, 8, buflen
/ 8, buf
);
497 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_name_chunk
, 1,
498 le
->le_name_numints
, 1, buflen
, buf
);
500 if (le
->le_name_numints
> buflen
)
501 return (SET_ERROR(EOVERFLOW
));
506 zap_entry_update(zap_entry_handle_t
*zeh
,
507 uint8_t integer_size
, uint64_t num_integers
, const void *buf
)
509 zap_leaf_t
*l
= zeh
->zeh_leaf
;
510 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, *zeh
->zeh_chunkp
);
512 int delta_chunks
= ZAP_LEAF_ARRAY_NCHUNKS(num_integers
* integer_size
) -
513 ZAP_LEAF_ARRAY_NCHUNKS(le
->le_value_numints
* le
->le_value_intlen
);
515 if ((int)zap_leaf_phys(l
)->l_hdr
.lh_nfree
< delta_chunks
)
516 return (SET_ERROR(EAGAIN
));
518 zap_leaf_array_free(l
, &le
->le_value_chunk
);
520 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
521 le
->le_value_numints
= num_integers
;
522 le
->le_value_intlen
= integer_size
;
527 zap_entry_remove(zap_entry_handle_t
*zeh
)
529 zap_leaf_t
*l
= zeh
->zeh_leaf
;
531 ASSERT3P(zeh
->zeh_chunkp
, !=, &zeh
->zeh_fakechunk
);
533 uint16_t entry_chunk
= *zeh
->zeh_chunkp
;
534 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry_chunk
);
535 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
537 zap_leaf_array_free(l
, &le
->le_name_chunk
);
538 zap_leaf_array_free(l
, &le
->le_value_chunk
);
540 *zeh
->zeh_chunkp
= le
->le_next
;
541 zap_leaf_chunk_free(l
, entry_chunk
);
543 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
547 zap_entry_create(zap_leaf_t
*l
, zap_name_t
*zn
, uint32_t cd
,
548 uint8_t integer_size
, uint64_t num_integers
, const void *buf
,
549 zap_entry_handle_t
*zeh
)
552 struct zap_leaf_entry
*le
;
553 uint64_t h
= zn
->zn_hash
;
555 uint64_t valuelen
= integer_size
* num_integers
;
557 uint_t numchunks
= 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn
->zn_key_orig_numints
*
558 zn
->zn_key_intlen
) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen
);
559 if (numchunks
> ZAP_LEAF_NUMCHUNKS(l
))
560 return (SET_ERROR(E2BIG
));
562 if (cd
== ZAP_NEED_CD
) {
563 /* find the lowest unused cd */
564 if (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
) {
567 for (chunk
= *LEAF_HASH_ENTPTR(l
, h
);
568 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
569 le
= ZAP_LEAF_ENTRY(l
, chunk
);
572 if (le
->le_hash
== h
) {
573 ASSERT3U(cd
, ==, le
->le_cd
);
578 /* old unsorted format; do it the O(n^2) way */
579 for (cd
= 0; ; cd
++) {
580 for (chunk
= *LEAF_HASH_ENTPTR(l
, h
);
581 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
582 le
= ZAP_LEAF_ENTRY(l
, chunk
);
583 if (le
->le_hash
== h
&&
588 /* If this cd is not in use, we are good. */
589 if (chunk
== CHAIN_END
)
594 * We would run out of space in a block before we could
595 * store enough entries to run out of CD values.
597 ASSERT3U(cd
, <, zap_maxcd(zn
->zn_zap
));
600 if (zap_leaf_phys(l
)->l_hdr
.lh_nfree
< numchunks
)
601 return (SET_ERROR(EAGAIN
));
604 chunk
= zap_leaf_chunk_alloc(l
);
605 le
= ZAP_LEAF_ENTRY(l
, chunk
);
606 le
->le_type
= ZAP_CHUNK_ENTRY
;
607 le
->le_name_chunk
= zap_leaf_array_create(l
, zn
->zn_key_orig
,
608 zn
->zn_key_intlen
, zn
->zn_key_orig_numints
);
609 le
->le_name_numints
= zn
->zn_key_orig_numints
;
611 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
612 le
->le_value_numints
= num_integers
;
613 le
->le_value_intlen
= integer_size
;
617 /* link it into the hash chain */
618 /* XXX if we did the search above, we could just use that */
619 uint16_t *chunkp
= zap_leaf_rehash_entry(l
, le
, chunk
);
621 zap_leaf_phys(l
)->l_hdr
.lh_nentries
++;
624 zeh
->zeh_num_integers
= num_integers
;
625 zeh
->zeh_integer_size
= le
->le_value_intlen
;
626 zeh
->zeh_cd
= le
->le_cd
;
627 zeh
->zeh_hash
= le
->le_hash
;
628 zeh
->zeh_chunkp
= chunkp
;
634 * Determine if there is another entry with the same normalized form.
635 * For performance purposes, either zn or name must be provided (the
636 * other can be NULL). Note, there usually won't be any hash
637 * conflicts, in which case we don't need the concatenated/normalized
638 * form of the name. But all callers have one of these on hand anyway,
639 * so might as well take advantage. A cleaner but slower interface
640 * would accept neither argument, and compute the normalized name as
641 * needed (using zap_name_alloc_str(zap_entry_read_name(zeh))).
644 zap_entry_normalization_conflict(zap_entry_handle_t
*zeh
, zap_name_t
*zn
,
645 const char *name
, zap_t
*zap
)
647 struct zap_leaf_entry
*le
;
648 boolean_t allocdzn
= B_FALSE
;
650 if (zap
->zap_normflags
== 0)
653 for (uint16_t chunk
= *LEAF_HASH_ENTPTR(zeh
->zeh_leaf
, zeh
->zeh_hash
);
654 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
655 le
= ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, chunk
);
656 if (le
->le_hash
!= zeh
->zeh_hash
)
658 if (le
->le_cd
== zeh
->zeh_cd
)
662 zn
= zap_name_alloc_str(zap
, name
, MT_NORMALIZE
);
665 if (zap_leaf_array_match(zeh
->zeh_leaf
, zn
,
666 le
->le_name_chunk
, le
->le_name_numints
)) {
678 * Routines for transferring entries between leafs.
682 zap_leaf_rehash_entry(zap_leaf_t
*l
, struct zap_leaf_entry
*le
, uint16_t entry
)
684 struct zap_leaf_entry
*le2
;
688 * keep the entry chain sorted by cd
689 * NB: this will not cause problems for unsorted leafs, though
690 * it is unnecessary there.
692 for (chunkp
= LEAF_HASH_ENTPTR(l
, le
->le_hash
);
693 *chunkp
!= CHAIN_END
; chunkp
= &le2
->le_next
) {
694 le2
= ZAP_LEAF_ENTRY(l
, *chunkp
);
695 if (le2
->le_cd
> le
->le_cd
)
699 le
->le_next
= *chunkp
;
705 zap_leaf_transfer_array(zap_leaf_t
*l
, uint16_t chunk
, zap_leaf_t
*nl
)
708 uint16_t *nchunkp
= &new_chunk
;
710 while (chunk
!= CHAIN_END
) {
711 uint16_t nchunk
= zap_leaf_chunk_alloc(nl
);
712 struct zap_leaf_array
*nla
=
713 &ZAP_LEAF_CHUNK(nl
, nchunk
).l_array
;
714 struct zap_leaf_array
*la
=
715 &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
716 uint_t nextchunk
= la
->la_next
;
718 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
719 ASSERT3U(nchunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
721 *nla
= *la
; /* structure assignment */
723 zap_leaf_chunk_free(l
, chunk
);
726 nchunkp
= &nla
->la_next
;
728 *nchunkp
= CHAIN_END
;
733 zap_leaf_transfer_entry(zap_leaf_t
*l
, uint_t entry
, zap_leaf_t
*nl
)
735 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry
);
736 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
738 uint16_t chunk
= zap_leaf_chunk_alloc(nl
);
739 struct zap_leaf_entry
*nle
= ZAP_LEAF_ENTRY(nl
, chunk
);
740 *nle
= *le
; /* structure assignment */
742 (void) zap_leaf_rehash_entry(nl
, nle
, chunk
);
744 nle
->le_name_chunk
= zap_leaf_transfer_array(l
, le
->le_name_chunk
, nl
);
745 nle
->le_value_chunk
=
746 zap_leaf_transfer_array(l
, le
->le_value_chunk
, nl
);
748 zap_leaf_chunk_free(l
, entry
);
750 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
751 zap_leaf_phys(nl
)->l_hdr
.lh_nentries
++;
755 * Transfer the entries whose hash prefix ends in 1 to the new leaf.
758 zap_leaf_split(zap_leaf_t
*l
, zap_leaf_t
*nl
, boolean_t sort
)
760 uint_t bit
= 64 - 1 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
762 /* set new prefix and prefix_len */
763 zap_leaf_phys(l
)->l_hdr
.lh_prefix
<<= 1;
764 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
++;
765 zap_leaf_phys(nl
)->l_hdr
.lh_prefix
=
766 zap_leaf_phys(l
)->l_hdr
.lh_prefix
| 1;
767 zap_leaf_phys(nl
)->l_hdr
.lh_prefix_len
=
768 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
770 /* break existing hash chains */
771 memset(zap_leaf_phys(l
)->l_hash
, CHAIN_END
,
772 2*ZAP_LEAF_HASH_NUMENTRIES(l
));
775 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
778 * Transfer entries whose hash bit 'bit' is set to nl; rehash
779 * the remaining entries
781 * NB: We could find entries via the hashtable instead. That
782 * would be O(hashents+numents) rather than O(numblks+numents),
783 * but this accesses memory more sequentially, and when we're
784 * called, the block is usually pretty full.
786 for (uint_t i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
787 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, i
);
788 if (le
->le_type
!= ZAP_CHUNK_ENTRY
)
791 if (le
->le_hash
& (1ULL << bit
))
792 zap_leaf_transfer_entry(l
, i
, nl
);
794 (void) zap_leaf_rehash_entry(l
, le
, i
);
799 zap_leaf_stats(zap_t
*zap
, zap_leaf_t
*l
, zap_stats_t
*zs
)
801 uint_t n
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
-
802 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
803 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
804 zs
->zs_leafs_with_2n_pointers
[n
]++;
807 n
= zap_leaf_phys(l
)->l_hdr
.lh_nentries
/5;
808 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
809 zs
->zs_blocks_with_n5_entries
[n
]++;
811 n
= ((1<<FZAP_BLOCK_SHIFT(zap
)) -
812 zap_leaf_phys(l
)->l_hdr
.lh_nfree
* (ZAP_LEAF_ARRAY_BYTES
+1))*10 /
813 (1<<FZAP_BLOCK_SHIFT(zap
));
814 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
815 zs
->zs_blocks_n_tenths_full
[n
]++;
817 for (uint_t i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(l
); i
++) {
819 uint_t chunk
= zap_leaf_phys(l
)->l_hash
[i
];
821 while (chunk
!= CHAIN_END
) {
822 struct zap_leaf_entry
*le
=
823 ZAP_LEAF_ENTRY(l
, chunk
);
825 n
= 1 + ZAP_LEAF_ARRAY_NCHUNKS(le
->le_name_numints
) +
826 ZAP_LEAF_ARRAY_NCHUNKS(le
->le_value_numints
*
827 le
->le_value_intlen
);
828 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
829 zs
->zs_entries_using_n_chunks
[n
]++;
836 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
837 zs
->zs_buckets_with_n_entries
[n
]++;