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
, uint16_t entry
);
46 #define CHAIN_END 0xffff /* end of the chunk chain */
48 #define LEAF_HASH(l, h) \
49 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
51 (64 - ZAP_LEAF_HASH_SHIFT(l) - zap_leaf_phys(l)->l_hdr.lh_prefix_len)))
53 #define LEAF_HASH_ENTPTR(l, h) (&zap_leaf_phys(l)->l_hash[LEAF_HASH(l, h)])
56 zap_memset(void *a
, int c
, size_t n
)
66 stv(int len
, void *addr
, uint64_t value
)
70 *(uint8_t *)addr
= value
;
73 *(uint16_t *)addr
= value
;
76 *(uint32_t *)addr
= value
;
79 *(uint64_t *)addr
= value
;
82 cmn_err(CE_PANIC
, "bad int len %d", len
);
87 ldv(int len
, const void *addr
)
91 return (*(uint8_t *)addr
);
93 return (*(uint16_t *)addr
);
95 return (*(uint32_t *)addr
);
97 return (*(uint64_t *)addr
);
99 cmn_err(CE_PANIC
, "bad int len %d", len
);
101 return (0xFEEDFACEDEADBEEFULL
);
105 zap_leaf_byteswap(zap_leaf_phys_t
*buf
, int size
)
110 l_dbuf
.db_data
= buf
;
111 l
.l_bs
= highbit64(size
) - 1;
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
);
122 for (int i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(&l
); i
++)
123 buf
->l_hash
[i
] = BSWAP_16(buf
->l_hash
[i
]);
125 for (int i
= 0; i
< ZAP_LEAF_NUMCHUNKS(&l
); i
++) {
126 zap_leaf_chunk_t
*lc
= &ZAP_LEAF_CHUNK(&l
, i
);
127 struct zap_leaf_entry
*le
;
129 switch (lc
->l_free
.lf_type
) {
130 case ZAP_CHUNK_ENTRY
:
133 le
->le_type
= BSWAP_8(le
->le_type
);
134 le
->le_value_intlen
= BSWAP_8(le
->le_value_intlen
);
135 le
->le_next
= BSWAP_16(le
->le_next
);
136 le
->le_name_chunk
= BSWAP_16(le
->le_name_chunk
);
137 le
->le_name_numints
= BSWAP_16(le
->le_name_numints
);
138 le
->le_value_chunk
= BSWAP_16(le
->le_value_chunk
);
139 le
->le_value_numints
= BSWAP_16(le
->le_value_numints
);
140 le
->le_cd
= BSWAP_32(le
->le_cd
);
141 le
->le_hash
= BSWAP_64(le
->le_hash
);
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
);
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 */
153 cmn_err(CE_PANIC
, "bad leaf type %d",
160 zap_leaf_init(zap_leaf_t
*l
, boolean_t sort
)
162 l
->l_bs
= highbit64(l
->l_dbuf
->db_size
) - 1;
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
));
167 for (int i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
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;
171 ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)-1).l_free
.lf_next
= CHAIN_END
;
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
);
176 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
180 * Routines which manipulate leaf chunks (l_chunk[]).
184 zap_leaf_chunk_alloc(zap_leaf_t
*l
)
186 ASSERT(zap_leaf_phys(l
)->l_hdr
.lh_nfree
> 0);
188 int chunk
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
189 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
190 ASSERT3U(ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_type
, ==, ZAP_CHUNK_FREE
);
192 zap_leaf_phys(l
)->l_hdr
.lh_freelist
=
193 ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_next
;
195 zap_leaf_phys(l
)->l_hdr
.lh_nfree
--;
201 zap_leaf_chunk_free(zap_leaf_t
*l
, uint16_t chunk
)
203 struct zap_leaf_free
*zlf
= &ZAP_LEAF_CHUNK(l
, chunk
).l_free
;
204 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_nfree
, <, ZAP_LEAF_NUMCHUNKS(l
));
205 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
206 ASSERT(zlf
->lf_type
!= ZAP_CHUNK_FREE
);
208 zlf
->lf_type
= ZAP_CHUNK_FREE
;
209 zlf
->lf_next
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
210 memset(zlf
->lf_pad
, 0, sizeof (zlf
->lf_pad
)); /* help it to compress */
211 zap_leaf_phys(l
)->l_hdr
.lh_freelist
= chunk
;
213 zap_leaf_phys(l
)->l_hdr
.lh_nfree
++;
217 * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
221 zap_leaf_array_create(zap_leaf_t
*l
, const char *buf
,
222 int integer_size
, int num_integers
)
225 uint16_t *chunkp
= &chunk_head
;
228 int shift
= (integer_size
- 1) * 8;
229 int len
= num_integers
;
231 ASSERT3U(num_integers
* integer_size
, <=, ZAP_MAXVALUELEN
);
234 uint16_t chunk
= zap_leaf_chunk_alloc(l
);
235 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
237 la
->la_type
= ZAP_CHUNK_ARRAY
;
238 for (int i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
; i
++) {
240 value
= ldv(integer_size
, buf
);
241 la
->la_array
[i
] = value
>> shift
;
243 if (++byten
== integer_size
) {
252 chunkp
= &la
->la_next
;
260 zap_leaf_array_free(zap_leaf_t
*l
, uint16_t *chunkp
)
262 uint16_t chunk
= *chunkp
;
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
, ==,
270 zap_leaf_chunk_free(l
, chunk
);
275 /* array_len and buf_len are in integers, not bytes */
277 zap_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
,
281 int len
= MIN(array_len
, buf_len
);
286 ASSERT3U(array_int_len
, <=, buf_int_len
);
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
;
292 uint64_t *buf64
= buf
;
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];
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
;
307 memcpy(p
, la
->la_array
, ZAP_LEAF_ARRAY_BYTES
);
308 p
+= ZAP_LEAF_ARRAY_BYTES
;
315 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
317 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
318 for (int i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
; i
++) {
319 value
= (value
<< 8) | la
->la_array
[i
];
321 if (byten
== array_int_len
) {
322 stv(buf_int_len
, p
, value
);
335 zap_leaf_array_match(zap_leaf_t
*l
, zap_name_t
*zn
,
336 int chunk
, int array_numints
)
340 if (zap_getflags(zn
->zn_zap
) & ZAP_FLAG_UINT64_KEY
) {
342 kmem_alloc(array_numints
* sizeof (*thiskey
), KM_SLEEP
);
343 ASSERT(zn
->zn_key_intlen
== sizeof (*thiskey
));
345 zap_leaf_array_read(l
, chunk
, sizeof (*thiskey
), array_numints
,
346 sizeof (*thiskey
), array_numints
, thiskey
);
347 boolean_t match
= memcmp(thiskey
, zn
->zn_key_orig
,
348 array_numints
* sizeof (*thiskey
)) == 0;
349 kmem_free(thiskey
, array_numints
* sizeof (*thiskey
));
353 ASSERT(zn
->zn_key_intlen
== 1);
354 if (zn
->zn_matchtype
& MT_NORMALIZE
) {
355 char *thisname
= kmem_alloc(array_numints
, KM_SLEEP
);
357 zap_leaf_array_read(l
, chunk
, sizeof (char), array_numints
,
358 sizeof (char), array_numints
, thisname
);
359 boolean_t match
= zap_match(zn
, thisname
);
360 kmem_free(thisname
, array_numints
);
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.
369 if (array_numints
!= zn
->zn_key_orig_numints
)
371 while (bseen
< array_numints
) {
372 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
373 int toread
= MIN(array_numints
- bseen
, ZAP_LEAF_ARRAY_BYTES
);
374 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
375 if (memcmp(la
->la_array
, (char *)zn
->zn_key_orig
+ bseen
,
381 return (bseen
== array_numints
);
385 * Routines which manipulate leaf entries.
389 zap_leaf_lookup(zap_leaf_t
*l
, zap_name_t
*zn
, zap_entry_handle_t
*zeh
)
391 struct zap_leaf_entry
*le
;
393 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
395 for (uint16_t *chunkp
= LEAF_HASH_ENTPTR(l
, zn
->zn_hash
);
396 *chunkp
!= CHAIN_END
; chunkp
= &le
->le_next
) {
397 uint16_t chunk
= *chunkp
;
398 le
= ZAP_LEAF_ENTRY(l
, chunk
);
400 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
401 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
403 if (le
->le_hash
!= zn
->zn_hash
)
407 * NB: the entry chain is always sorted by cd on
408 * normalized zap objects, so this will find the
409 * lowest-cd match for MT_NORMALIZE.
411 ASSERT((zn
->zn_matchtype
== 0) ||
412 (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
));
413 if (zap_leaf_array_match(l
, zn
, le
->le_name_chunk
,
414 le
->le_name_numints
)) {
415 zeh
->zeh_num_integers
= le
->le_value_numints
;
416 zeh
->zeh_integer_size
= le
->le_value_intlen
;
417 zeh
->zeh_cd
= le
->le_cd
;
418 zeh
->zeh_hash
= le
->le_hash
;
419 zeh
->zeh_chunkp
= chunkp
;
425 return (SET_ERROR(ENOENT
));
428 /* Return (h1,cd1 >= h2,cd2) */
429 #define HCD_GTEQ(h1, cd1, h2, cd2) \
430 ((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
433 zap_leaf_lookup_closest(zap_leaf_t
*l
,
434 uint64_t h
, uint32_t cd
, zap_entry_handle_t
*zeh
)
436 uint64_t besth
= -1ULL;
437 uint32_t bestcd
= -1U;
438 uint16_t bestlh
= ZAP_LEAF_HASH_NUMENTRIES(l
)-1;
439 struct zap_leaf_entry
*le
;
441 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
443 for (uint16_t lh
= LEAF_HASH(l
, h
); lh
<= bestlh
; lh
++) {
444 for (uint16_t chunk
= zap_leaf_phys(l
)->l_hash
[lh
];
445 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
446 le
= ZAP_LEAF_ENTRY(l
, chunk
);
448 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
449 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
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
);
458 zeh
->zeh_num_integers
= le
->le_value_numints
;
459 zeh
->zeh_integer_size
= le
->le_value_intlen
;
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
;
469 return (bestcd
== -1U ? SET_ERROR(ENOENT
) : 0);
473 zap_entry_read(const zap_entry_handle_t
*zeh
,
474 uint8_t integer_size
, uint64_t num_integers
, void *buf
)
476 struct zap_leaf_entry
*le
=
477 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
478 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
480 if (le
->le_value_intlen
> integer_size
)
481 return (SET_ERROR(EINVAL
));
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
);
487 if (zeh
->zeh_num_integers
> num_integers
)
488 return (SET_ERROR(EOVERFLOW
));
494 zap_entry_read_name(zap_t
*zap
, const zap_entry_handle_t
*zeh
, uint16_t buflen
,
497 struct zap_leaf_entry
*le
=
498 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
499 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
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
);
505 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_name_chunk
, 1,
506 le
->le_name_numints
, 1, buflen
, buf
);
508 if (le
->le_name_numints
> buflen
)
509 return (SET_ERROR(EOVERFLOW
));
514 zap_entry_update(zap_entry_handle_t
*zeh
,
515 uint8_t integer_size
, uint64_t num_integers
, const void *buf
)
517 zap_leaf_t
*l
= zeh
->zeh_leaf
;
518 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, *zeh
->zeh_chunkp
);
520 int delta_chunks
= ZAP_LEAF_ARRAY_NCHUNKS(num_integers
* integer_size
) -
521 ZAP_LEAF_ARRAY_NCHUNKS(le
->le_value_numints
* le
->le_value_intlen
);
523 if ((int)zap_leaf_phys(l
)->l_hdr
.lh_nfree
< delta_chunks
)
524 return (SET_ERROR(EAGAIN
));
526 zap_leaf_array_free(l
, &le
->le_value_chunk
);
528 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
529 le
->le_value_numints
= num_integers
;
530 le
->le_value_intlen
= integer_size
;
535 zap_entry_remove(zap_entry_handle_t
*zeh
)
537 zap_leaf_t
*l
= zeh
->zeh_leaf
;
539 ASSERT3P(zeh
->zeh_chunkp
, !=, &zeh
->zeh_fakechunk
);
541 uint16_t entry_chunk
= *zeh
->zeh_chunkp
;
542 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry_chunk
);
543 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
545 zap_leaf_array_free(l
, &le
->le_name_chunk
);
546 zap_leaf_array_free(l
, &le
->le_value_chunk
);
548 *zeh
->zeh_chunkp
= le
->le_next
;
549 zap_leaf_chunk_free(l
, entry_chunk
);
551 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
555 zap_entry_create(zap_leaf_t
*l
, zap_name_t
*zn
, uint32_t cd
,
556 uint8_t integer_size
, uint64_t num_integers
, const void *buf
,
557 zap_entry_handle_t
*zeh
)
560 struct zap_leaf_entry
*le
;
561 uint64_t h
= zn
->zn_hash
;
563 uint64_t valuelen
= integer_size
* num_integers
;
565 int numchunks
= 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn
->zn_key_orig_numints
*
566 zn
->zn_key_intlen
) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen
);
567 if (numchunks
> ZAP_LEAF_NUMCHUNKS(l
))
568 return (SET_ERROR(E2BIG
));
570 if (cd
== ZAP_NEED_CD
) {
571 /* find the lowest unused cd */
572 if (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
) {
575 for (chunk
= *LEAF_HASH_ENTPTR(l
, h
);
576 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
577 le
= ZAP_LEAF_ENTRY(l
, chunk
);
580 if (le
->le_hash
== h
) {
581 ASSERT3U(cd
, ==, le
->le_cd
);
586 /* old unsorted format; do it the O(n^2) way */
587 for (cd
= 0; ; cd
++) {
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
&&
596 /* If this cd is not in use, we are good. */
597 if (chunk
== CHAIN_END
)
602 * We would run out of space in a block before we could
603 * store enough entries to run out of CD values.
605 ASSERT3U(cd
, <, zap_maxcd(zn
->zn_zap
));
608 if (zap_leaf_phys(l
)->l_hdr
.lh_nfree
< numchunks
)
609 return (SET_ERROR(EAGAIN
));
612 chunk
= zap_leaf_chunk_alloc(l
);
613 le
= ZAP_LEAF_ENTRY(l
, chunk
);
614 le
->le_type
= ZAP_CHUNK_ENTRY
;
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
;
619 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
620 le
->le_value_numints
= num_integers
;
621 le
->le_value_intlen
= integer_size
;
625 /* link it into the hash chain */
626 /* XXX if we did the search above, we could just use that */
627 uint16_t *chunkp
= zap_leaf_rehash_entry(l
, chunk
);
629 zap_leaf_phys(l
)->l_hdr
.lh_nentries
++;
632 zeh
->zeh_num_integers
= num_integers
;
633 zeh
->zeh_integer_size
= le
->le_value_intlen
;
634 zeh
->zeh_cd
= le
->le_cd
;
635 zeh
->zeh_hash
= le
->le_hash
;
636 zeh
->zeh_chunkp
= chunkp
;
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_str(zap_entry_read_name(zeh))).
652 zap_entry_normalization_conflict(zap_entry_handle_t
*zeh
, zap_name_t
*zn
,
653 const char *name
, zap_t
*zap
)
655 struct zap_leaf_entry
*le
;
656 boolean_t allocdzn
= B_FALSE
;
658 if (zap
->zap_normflags
== 0)
661 for (uint16_t chunk
= *LEAF_HASH_ENTPTR(zeh
->zeh_leaf
, zeh
->zeh_hash
);
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
)
666 if (le
->le_cd
== zeh
->zeh_cd
)
670 zn
= zap_name_alloc_str(zap
, name
, MT_NORMALIZE
);
673 if (zap_leaf_array_match(zeh
->zeh_leaf
, zn
,
674 le
->le_name_chunk
, le
->le_name_numints
)) {
686 * Routines for transferring entries between leafs.
690 zap_leaf_rehash_entry(zap_leaf_t
*l
, uint16_t entry
)
692 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry
);
693 struct zap_leaf_entry
*le2
;
697 * keep the entry chain sorted by cd
698 * NB: this will not cause problems for unsorted leafs, though
699 * it is unnecessary there.
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
)
708 le
->le_next
= *chunkp
;
714 zap_leaf_transfer_array(zap_leaf_t
*l
, uint16_t chunk
, zap_leaf_t
*nl
)
717 uint16_t *nchunkp
= &new_chunk
;
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
;
727 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
728 ASSERT3U(nchunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
730 *nla
= *la
; /* structure assignment */
732 zap_leaf_chunk_free(l
, chunk
);
735 nchunkp
= &nla
->la_next
;
737 *nchunkp
= CHAIN_END
;
742 zap_leaf_transfer_entry(zap_leaf_t
*l
, int entry
, zap_leaf_t
*nl
)
744 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry
);
745 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
747 uint16_t chunk
= zap_leaf_chunk_alloc(nl
);
748 struct zap_leaf_entry
*nle
= ZAP_LEAF_ENTRY(nl
, chunk
);
749 *nle
= *le
; /* structure assignment */
751 (void) zap_leaf_rehash_entry(nl
, chunk
);
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
);
757 zap_leaf_chunk_free(l
, entry
);
759 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
760 zap_leaf_phys(nl
)->l_hdr
.lh_nentries
++;
764 * Transfer the entries whose hash prefix ends in 1 to the new leaf.
767 zap_leaf_split(zap_leaf_t
*l
, zap_leaf_t
*nl
, boolean_t sort
)
769 int bit
= 64 - 1 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
771 /* set new prefix and prefix_len */
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
;
779 /* break existing hash chains */
780 zap_memset(zap_leaf_phys(l
)->l_hash
, CHAIN_END
,
781 2*ZAP_LEAF_HASH_NUMENTRIES(l
));
784 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
787 * Transfer entries whose hash bit 'bit' is set to nl; rehash
788 * the remaining entries
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.
795 for (int i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
796 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, i
);
797 if (le
->le_type
!= ZAP_CHUNK_ENTRY
)
800 if (le
->le_hash
& (1ULL << bit
))
801 zap_leaf_transfer_entry(l
, i
, nl
);
803 (void) zap_leaf_rehash_entry(l
, i
);
808 zap_leaf_stats(zap_t
*zap
, zap_leaf_t
*l
, zap_stats_t
*zs
)
810 int n
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
-
811 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
812 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
813 zs
->zs_leafs_with_2n_pointers
[n
]++;
816 n
= zap_leaf_phys(l
)->l_hdr
.lh_nentries
/5;
817 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
818 zs
->zs_blocks_with_n5_entries
[n
]++;
820 n
= ((1<<FZAP_BLOCK_SHIFT(zap
)) -
821 zap_leaf_phys(l
)->l_hdr
.lh_nfree
* (ZAP_LEAF_ARRAY_BYTES
+1))*10 /
822 (1<<FZAP_BLOCK_SHIFT(zap
));
823 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
824 zs
->zs_blocks_n_tenths_full
[n
]++;
826 for (int i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(l
); i
++) {
828 int chunk
= zap_leaf_phys(l
)->l_hash
[i
];
830 while (chunk
!= CHAIN_END
) {
831 struct zap_leaf_entry
*le
=
832 ZAP_LEAF_ENTRY(l
, chunk
);
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
);
837 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
838 zs
->zs_entries_using_n_chunks
[n
]++;
845 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
846 zs
->zs_buckets_with_n_entries
[n
]++;