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 http://www.opensolaris.org/os/licensing.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
27 * The 512-byte leaf is broken into 32 16-byte chunks.
28 * chunk number n means l_chunk[n], even though the header precedes it.
29 * the names are stored null-terminated.
35 #include <sys/zfs_context.h>
36 #include <sys/fs/zfs.h>
38 #include <sys/zap_impl.h>
39 #include <sys/zap_leaf.h>
42 static uint16_t *zap_leaf_rehash_entry(zap_leaf_t
*l
, uint16_t entry
);
44 #define CHAIN_END 0xffff /* end of the chunk chain */
46 /* half the (current) minimum block size */
47 #define MAX_ARRAY_BYTES (8<<10)
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)])
56 extern inline zap_leaf_phys_t
*zap_leaf_phys(zap_leaf_t
*l
);
59 zap_memset(void *a
, int c
, size_t n
)
69 stv(int len
, void *addr
, uint64_t value
)
73 *(uint8_t *)addr
= value
;
76 *(uint16_t *)addr
= value
;
79 *(uint32_t *)addr
= value
;
82 *(uint64_t *)addr
= value
;
85 ASSERT(!"bad int len");
89 ldv(int len
, const void *addr
)
93 return (*(uint8_t *)addr
);
95 return (*(uint16_t *)addr
);
97 return (*(uint32_t *)addr
);
99 return (*(uint64_t *)addr
);
101 ASSERT(!"bad int len");
102 return (0xFEEDFACEDEADBEEFULL
);
106 zap_leaf_byteswap(zap_leaf_phys_t
*buf
, int size
)
112 l_dbuf
.db_data
= buf
;
113 l
.l_bs
= highbit64(size
) - 1;
116 buf
->l_hdr
.lh_block_type
= BSWAP_64(buf
->l_hdr
.lh_block_type
);
117 buf
->l_hdr
.lh_prefix
= BSWAP_64(buf
->l_hdr
.lh_prefix
);
118 buf
->l_hdr
.lh_magic
= BSWAP_32(buf
->l_hdr
.lh_magic
);
119 buf
->l_hdr
.lh_nfree
= BSWAP_16(buf
->l_hdr
.lh_nfree
);
120 buf
->l_hdr
.lh_nentries
= BSWAP_16(buf
->l_hdr
.lh_nentries
);
121 buf
->l_hdr
.lh_prefix_len
= BSWAP_16(buf
->l_hdr
.lh_prefix_len
);
122 buf
->l_hdr
.lh_freelist
= BSWAP_16(buf
->l_hdr
.lh_freelist
);
124 for (i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(&l
); i
++)
125 buf
->l_hash
[i
] = BSWAP_16(buf
->l_hash
[i
]);
127 for (i
= 0; i
< ZAP_LEAF_NUMCHUNKS(&l
); i
++) {
128 zap_leaf_chunk_t
*lc
= &ZAP_LEAF_CHUNK(&l
, i
);
129 struct zap_leaf_entry
*le
;
131 switch (lc
->l_free
.lf_type
) {
132 case ZAP_CHUNK_ENTRY
:
135 le
->le_type
= BSWAP_8(le
->le_type
);
136 le
->le_value_intlen
= BSWAP_8(le
->le_value_intlen
);
137 le
->le_next
= BSWAP_16(le
->le_next
);
138 le
->le_name_chunk
= BSWAP_16(le
->le_name_chunk
);
139 le
->le_name_numints
= BSWAP_16(le
->le_name_numints
);
140 le
->le_value_chunk
= BSWAP_16(le
->le_value_chunk
);
141 le
->le_value_numints
= BSWAP_16(le
->le_value_numints
);
142 le
->le_cd
= BSWAP_32(le
->le_cd
);
143 le
->le_hash
= BSWAP_64(le
->le_hash
);
146 lc
->l_free
.lf_type
= BSWAP_8(lc
->l_free
.lf_type
);
147 lc
->l_free
.lf_next
= BSWAP_16(lc
->l_free
.lf_next
);
149 case ZAP_CHUNK_ARRAY
:
150 lc
->l_array
.la_type
= BSWAP_8(lc
->l_array
.la_type
);
151 lc
->l_array
.la_next
= BSWAP_16(lc
->l_array
.la_next
);
152 /* la_array doesn't need swapping */
155 ASSERT(!"bad leaf type");
161 zap_leaf_init(zap_leaf_t
*l
, boolean_t sort
)
165 l
->l_bs
= highbit64(l
->l_dbuf
->db_size
) - 1;
166 zap_memset(&zap_leaf_phys(l
)->l_hdr
, 0,
167 sizeof (struct zap_leaf_header
));
168 zap_memset(zap_leaf_phys(l
)->l_hash
, CHAIN_END
,
169 2*ZAP_LEAF_HASH_NUMENTRIES(l
));
170 for (i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
171 ZAP_LEAF_CHUNK(l
, i
).l_free
.lf_type
= ZAP_CHUNK_FREE
;
172 ZAP_LEAF_CHUNK(l
, i
).l_free
.lf_next
= i
+1;
174 ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)-1).l_free
.lf_next
= CHAIN_END
;
175 zap_leaf_phys(l
)->l_hdr
.lh_block_type
= ZBT_LEAF
;
176 zap_leaf_phys(l
)->l_hdr
.lh_magic
= ZAP_LEAF_MAGIC
;
177 zap_leaf_phys(l
)->l_hdr
.lh_nfree
= ZAP_LEAF_NUMCHUNKS(l
);
179 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
183 * Routines which manipulate leaf chunks (l_chunk[]).
187 zap_leaf_chunk_alloc(zap_leaf_t
*l
)
191 ASSERT(zap_leaf_phys(l
)->l_hdr
.lh_nfree
> 0);
193 chunk
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
194 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
195 ASSERT3U(ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_type
, ==, ZAP_CHUNK_FREE
);
197 zap_leaf_phys(l
)->l_hdr
.lh_freelist
=
198 ZAP_LEAF_CHUNK(l
, chunk
).l_free
.lf_next
;
200 zap_leaf_phys(l
)->l_hdr
.lh_nfree
--;
206 zap_leaf_chunk_free(zap_leaf_t
*l
, uint16_t chunk
)
208 struct zap_leaf_free
*zlf
= &ZAP_LEAF_CHUNK(l
, chunk
).l_free
;
209 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_nfree
, <, ZAP_LEAF_NUMCHUNKS(l
));
210 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
211 ASSERT(zlf
->lf_type
!= ZAP_CHUNK_FREE
);
213 zlf
->lf_type
= ZAP_CHUNK_FREE
;
214 zlf
->lf_next
= zap_leaf_phys(l
)->l_hdr
.lh_freelist
;
215 bzero(zlf
->lf_pad
, sizeof (zlf
->lf_pad
)); /* help it to compress */
216 zap_leaf_phys(l
)->l_hdr
.lh_freelist
= chunk
;
218 zap_leaf_phys(l
)->l_hdr
.lh_nfree
++;
222 * Routines which manipulate leaf arrays (zap_leaf_array type chunks).
226 zap_leaf_array_create(zap_leaf_t
*l
, const char *buf
,
227 int integer_size
, int num_integers
)
230 uint16_t *chunkp
= &chunk_head
;
233 int shift
= (integer_size
-1)*8;
234 int len
= num_integers
;
236 ASSERT3U(num_integers
* integer_size
, <, MAX_ARRAY_BYTES
);
239 uint16_t chunk
= zap_leaf_chunk_alloc(l
);
240 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
243 la
->la_type
= ZAP_CHUNK_ARRAY
;
244 for (i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
; i
++) {
246 value
= ldv(integer_size
, buf
);
247 la
->la_array
[i
] = value
>> shift
;
249 if (++byten
== integer_size
) {
258 chunkp
= &la
->la_next
;
266 zap_leaf_array_free(zap_leaf_t
*l
, uint16_t *chunkp
)
268 uint16_t chunk
= *chunkp
;
272 while (chunk
!= CHAIN_END
) {
273 int nextchunk
= ZAP_LEAF_CHUNK(l
, chunk
).l_array
.la_next
;
274 ASSERT3U(ZAP_LEAF_CHUNK(l
, chunk
).l_array
.la_type
, ==,
276 zap_leaf_chunk_free(l
, chunk
);
281 /* array_len and buf_len are in integers, not bytes */
283 zap_leaf_array_read(zap_leaf_t
*l
, uint16_t chunk
,
284 int array_int_len
, int array_len
, int buf_int_len
, uint64_t buf_len
,
287 int len
= MIN(array_len
, buf_len
);
292 ASSERT3U(array_int_len
, <=, buf_int_len
);
294 /* Fast path for one 8-byte integer */
295 if (array_int_len
== 8 && buf_int_len
== 8 && len
== 1) {
296 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
297 uint8_t *ip
= la
->la_array
;
298 uint64_t *buf64
= buf
;
300 *buf64
= (uint64_t)ip
[0] << 56 | (uint64_t)ip
[1] << 48 |
301 (uint64_t)ip
[2] << 40 | (uint64_t)ip
[3] << 32 |
302 (uint64_t)ip
[4] << 24 | (uint64_t)ip
[5] << 16 |
303 (uint64_t)ip
[6] << 8 | (uint64_t)ip
[7];
307 /* Fast path for an array of 1-byte integers (eg. the entry name) */
308 if (array_int_len
== 1 && buf_int_len
== 1 &&
309 buf_len
> array_len
+ ZAP_LEAF_ARRAY_BYTES
) {
310 while (chunk
!= CHAIN_END
) {
311 struct zap_leaf_array
*la
=
312 &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
313 bcopy(la
->la_array
, p
, ZAP_LEAF_ARRAY_BYTES
);
314 p
+= ZAP_LEAF_ARRAY_BYTES
;
321 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
324 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
325 for (i
= 0; i
< ZAP_LEAF_ARRAY_BYTES
&& len
> 0; i
++) {
326 value
= (value
<< 8) | la
->la_array
[i
];
328 if (byten
== array_int_len
) {
329 stv(buf_int_len
, p
, value
);
342 zap_leaf_array_match(zap_leaf_t
*l
, zap_name_t
*zn
,
343 int chunk
, int array_numints
)
347 if (zap_getflags(zn
->zn_zap
) & ZAP_FLAG_UINT64_KEY
) {
351 ASSERT(zn
->zn_key_intlen
== sizeof (*thiskey
));
352 thiskey
= kmem_alloc(array_numints
* sizeof (*thiskey
),
355 zap_leaf_array_read(l
, chunk
, sizeof (*thiskey
), array_numints
,
356 sizeof (*thiskey
), array_numints
, thiskey
);
357 match
= bcmp(thiskey
, zn
->zn_key_orig
,
358 array_numints
* sizeof (*thiskey
)) == 0;
359 kmem_free(thiskey
, array_numints
* sizeof (*thiskey
));
363 ASSERT(zn
->zn_key_intlen
== 1);
364 if (zn
->zn_matchtype
== MT_FIRST
) {
365 char *thisname
= kmem_alloc(array_numints
, KM_SLEEP
);
368 zap_leaf_array_read(l
, chunk
, sizeof (char), array_numints
,
369 sizeof (char), array_numints
, thisname
);
370 match
= zap_match(zn
, thisname
);
371 kmem_free(thisname
, array_numints
);
376 * Fast path for exact matching.
377 * First check that the lengths match, so that we don't read
378 * past the end of the zn_key_orig array.
380 if (array_numints
!= zn
->zn_key_orig_numints
)
382 while (bseen
< array_numints
) {
383 struct zap_leaf_array
*la
= &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
384 int toread
= MIN(array_numints
- bseen
, ZAP_LEAF_ARRAY_BYTES
);
385 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
386 if (bcmp(la
->la_array
, (char *)zn
->zn_key_orig
+ bseen
, toread
))
391 return (bseen
== array_numints
);
395 * Routines which manipulate leaf entries.
399 zap_leaf_lookup(zap_leaf_t
*l
, zap_name_t
*zn
, zap_entry_handle_t
*zeh
)
402 struct zap_leaf_entry
*le
;
404 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
407 for (chunkp
= LEAF_HASH_ENTPTR(l
, zn
->zn_hash
);
408 *chunkp
!= CHAIN_END
; chunkp
= &le
->le_next
) {
409 uint16_t chunk
= *chunkp
;
410 le
= ZAP_LEAF_ENTRY(l
, chunk
);
412 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
413 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
415 if (le
->le_hash
!= zn
->zn_hash
)
419 * NB: the entry chain is always sorted by cd on
420 * normalized zap objects, so this will find the
421 * lowest-cd match for MT_FIRST.
423 ASSERT(zn
->zn_matchtype
== MT_EXACT
||
424 (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
));
425 if (zap_leaf_array_match(l
, zn
, le
->le_name_chunk
,
426 le
->le_name_numints
)) {
427 zeh
->zeh_num_integers
= le
->le_value_numints
;
428 zeh
->zeh_integer_size
= le
->le_value_intlen
;
429 zeh
->zeh_cd
= le
->le_cd
;
430 zeh
->zeh_hash
= le
->le_hash
;
431 zeh
->zeh_chunkp
= chunkp
;
438 * NB: we could of course do this in one pass, but that would be
439 * a pain. We'll see if MT_BEST is even used much.
441 if (zn
->zn_matchtype
== MT_BEST
) {
442 zn
->zn_matchtype
= MT_FIRST
;
446 return (SET_ERROR(ENOENT
));
449 /* Return (h1,cd1 >= h2,cd2) */
450 #define HCD_GTEQ(h1, cd1, h2, cd2) \
451 ((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
454 zap_leaf_lookup_closest(zap_leaf_t
*l
,
455 uint64_t h
, uint32_t cd
, zap_entry_handle_t
*zeh
)
458 uint64_t besth
= -1ULL;
459 uint32_t bestcd
= -1U;
460 uint16_t bestlh
= ZAP_LEAF_HASH_NUMENTRIES(l
)-1;
462 struct zap_leaf_entry
*le
;
464 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
466 for (lh
= LEAF_HASH(l
, h
); lh
<= bestlh
; lh
++) {
467 for (chunk
= zap_leaf_phys(l
)->l_hash
[lh
];
468 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
469 le
= ZAP_LEAF_ENTRY(l
, chunk
);
471 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
472 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
474 if (HCD_GTEQ(le
->le_hash
, le
->le_cd
, h
, cd
) &&
475 HCD_GTEQ(besth
, bestcd
, le
->le_hash
, le
->le_cd
)) {
476 ASSERT3U(bestlh
, >=, lh
);
481 zeh
->zeh_num_integers
= le
->le_value_numints
;
482 zeh
->zeh_integer_size
= le
->le_value_intlen
;
483 zeh
->zeh_cd
= le
->le_cd
;
484 zeh
->zeh_hash
= le
->le_hash
;
485 zeh
->zeh_fakechunk
= chunk
;
486 zeh
->zeh_chunkp
= &zeh
->zeh_fakechunk
;
492 return (bestcd
== -1U ? ENOENT
: 0);
496 zap_entry_read(const zap_entry_handle_t
*zeh
,
497 uint8_t integer_size
, uint64_t num_integers
, void *buf
)
499 struct zap_leaf_entry
*le
=
500 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
501 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
503 if (le
->le_value_intlen
> integer_size
)
504 return (SET_ERROR(EINVAL
));
506 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_value_chunk
,
507 le
->le_value_intlen
, le
->le_value_numints
,
508 integer_size
, num_integers
, buf
);
510 if (zeh
->zeh_num_integers
> num_integers
)
511 return (SET_ERROR(EOVERFLOW
));
517 zap_entry_read_name(zap_t
*zap
, const zap_entry_handle_t
*zeh
, uint16_t buflen
,
520 struct zap_leaf_entry
*le
=
521 ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, *zeh
->zeh_chunkp
);
522 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
524 if (zap_getflags(zap
) & ZAP_FLAG_UINT64_KEY
) {
525 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_name_chunk
, 8,
526 le
->le_name_numints
, 8, buflen
/ 8, buf
);
528 zap_leaf_array_read(zeh
->zeh_leaf
, le
->le_name_chunk
, 1,
529 le
->le_name_numints
, 1, buflen
, buf
);
531 if (le
->le_name_numints
> buflen
)
532 return (SET_ERROR(EOVERFLOW
));
537 zap_entry_update(zap_entry_handle_t
*zeh
,
538 uint8_t integer_size
, uint64_t num_integers
, const void *buf
)
541 zap_leaf_t
*l
= zeh
->zeh_leaf
;
542 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, *zeh
->zeh_chunkp
);
544 delta_chunks
= ZAP_LEAF_ARRAY_NCHUNKS(num_integers
* integer_size
) -
545 ZAP_LEAF_ARRAY_NCHUNKS(le
->le_value_numints
* le
->le_value_intlen
);
547 if ((int)zap_leaf_phys(l
)->l_hdr
.lh_nfree
< delta_chunks
)
548 return (SET_ERROR(EAGAIN
));
550 zap_leaf_array_free(l
, &le
->le_value_chunk
);
552 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
553 le
->le_value_numints
= num_integers
;
554 le
->le_value_intlen
= integer_size
;
559 zap_entry_remove(zap_entry_handle_t
*zeh
)
561 uint16_t entry_chunk
;
562 struct zap_leaf_entry
*le
;
563 zap_leaf_t
*l
= zeh
->zeh_leaf
;
565 ASSERT3P(zeh
->zeh_chunkp
, !=, &zeh
->zeh_fakechunk
);
567 entry_chunk
= *zeh
->zeh_chunkp
;
568 le
= ZAP_LEAF_ENTRY(l
, entry_chunk
);
569 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
571 zap_leaf_array_free(l
, &le
->le_name_chunk
);
572 zap_leaf_array_free(l
, &le
->le_value_chunk
);
574 *zeh
->zeh_chunkp
= le
->le_next
;
575 zap_leaf_chunk_free(l
, entry_chunk
);
577 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
581 zap_entry_create(zap_leaf_t
*l
, zap_name_t
*zn
, uint32_t cd
,
582 uint8_t integer_size
, uint64_t num_integers
, const void *buf
,
583 zap_entry_handle_t
*zeh
)
587 struct zap_leaf_entry
*le
;
590 uint64_t h
= zn
->zn_hash
;
592 valuelen
= integer_size
* num_integers
;
594 numchunks
= 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn
->zn_key_orig_numints
*
595 zn
->zn_key_intlen
) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen
);
596 if (numchunks
> ZAP_LEAF_NUMCHUNKS(l
))
599 if (cd
== ZAP_NEED_CD
) {
600 /* find the lowest unused cd */
601 if (zap_leaf_phys(l
)->l_hdr
.lh_flags
& ZLF_ENTRIES_CDSORTED
) {
604 for (chunk
= *LEAF_HASH_ENTPTR(l
, h
);
605 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
606 le
= ZAP_LEAF_ENTRY(l
, chunk
);
609 if (le
->le_hash
== h
) {
610 ASSERT3U(cd
, ==, le
->le_cd
);
615 /* old unsorted format; do it the O(n^2) way */
616 for (cd
= 0; ; cd
++) {
617 for (chunk
= *LEAF_HASH_ENTPTR(l
, h
);
618 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
619 le
= ZAP_LEAF_ENTRY(l
, chunk
);
620 if (le
->le_hash
== h
&&
625 /* If this cd is not in use, we are good. */
626 if (chunk
== CHAIN_END
)
631 * We would run out of space in a block before we could
632 * store enough entries to run out of CD values.
634 ASSERT3U(cd
, <, zap_maxcd(zn
->zn_zap
));
637 if (zap_leaf_phys(l
)->l_hdr
.lh_nfree
< numchunks
)
638 return (SET_ERROR(EAGAIN
));
641 chunk
= zap_leaf_chunk_alloc(l
);
642 le
= ZAP_LEAF_ENTRY(l
, chunk
);
643 le
->le_type
= ZAP_CHUNK_ENTRY
;
644 le
->le_name_chunk
= zap_leaf_array_create(l
, zn
->zn_key_orig
,
645 zn
->zn_key_intlen
, zn
->zn_key_orig_numints
);
646 le
->le_name_numints
= zn
->zn_key_orig_numints
;
648 zap_leaf_array_create(l
, buf
, integer_size
, num_integers
);
649 le
->le_value_numints
= num_integers
;
650 le
->le_value_intlen
= integer_size
;
654 /* link it into the hash chain */
655 /* XXX if we did the search above, we could just use that */
656 chunkp
= zap_leaf_rehash_entry(l
, chunk
);
658 zap_leaf_phys(l
)->l_hdr
.lh_nentries
++;
661 zeh
->zeh_num_integers
= num_integers
;
662 zeh
->zeh_integer_size
= le
->le_value_intlen
;
663 zeh
->zeh_cd
= le
->le_cd
;
664 zeh
->zeh_hash
= le
->le_hash
;
665 zeh
->zeh_chunkp
= chunkp
;
671 * Determine if there is another entry with the same normalized form.
672 * For performance purposes, either zn or name must be provided (the
673 * other can be NULL). Note, there usually won't be any hash
674 * conflicts, in which case we don't need the concatenated/normalized
675 * form of the name. But all callers have one of these on hand anyway,
676 * so might as well take advantage. A cleaner but slower interface
677 * would accept neither argument, and compute the normalized name as
678 * needed (using zap_name_alloc(zap_entry_read_name(zeh))).
681 zap_entry_normalization_conflict(zap_entry_handle_t
*zeh
, zap_name_t
*zn
,
682 const char *name
, zap_t
*zap
)
685 struct zap_leaf_entry
*le
;
686 boolean_t allocdzn
= B_FALSE
;
688 if (zap
->zap_normflags
== 0)
691 for (chunk
= *LEAF_HASH_ENTPTR(zeh
->zeh_leaf
, zeh
->zeh_hash
);
692 chunk
!= CHAIN_END
; chunk
= le
->le_next
) {
693 le
= ZAP_LEAF_ENTRY(zeh
->zeh_leaf
, chunk
);
694 if (le
->le_hash
!= zeh
->zeh_hash
)
696 if (le
->le_cd
== zeh
->zeh_cd
)
700 zn
= zap_name_alloc(zap
, name
, MT_FIRST
);
703 if (zap_leaf_array_match(zeh
->zeh_leaf
, zn
,
704 le
->le_name_chunk
, le
->le_name_numints
)) {
716 * Routines for transferring entries between leafs.
720 zap_leaf_rehash_entry(zap_leaf_t
*l
, uint16_t entry
)
722 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, entry
);
723 struct zap_leaf_entry
*le2
;
727 * keep the entry chain sorted by cd
728 * NB: this will not cause problems for unsorted leafs, though
729 * it is unnecessary there.
731 for (chunkp
= LEAF_HASH_ENTPTR(l
, le
->le_hash
);
732 *chunkp
!= CHAIN_END
; chunkp
= &le2
->le_next
) {
733 le2
= ZAP_LEAF_ENTRY(l
, *chunkp
);
734 if (le2
->le_cd
> le
->le_cd
)
738 le
->le_next
= *chunkp
;
744 zap_leaf_transfer_array(zap_leaf_t
*l
, uint16_t chunk
, zap_leaf_t
*nl
)
747 uint16_t *nchunkp
= &new_chunk
;
749 while (chunk
!= CHAIN_END
) {
750 uint16_t nchunk
= zap_leaf_chunk_alloc(nl
);
751 struct zap_leaf_array
*nla
=
752 &ZAP_LEAF_CHUNK(nl
, nchunk
).l_array
;
753 struct zap_leaf_array
*la
=
754 &ZAP_LEAF_CHUNK(l
, chunk
).l_array
;
755 int nextchunk
= la
->la_next
;
757 ASSERT3U(chunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
758 ASSERT3U(nchunk
, <, ZAP_LEAF_NUMCHUNKS(l
));
760 *nla
= *la
; /* structure assignment */
762 zap_leaf_chunk_free(l
, chunk
);
765 nchunkp
= &nla
->la_next
;
767 *nchunkp
= CHAIN_END
;
772 zap_leaf_transfer_entry(zap_leaf_t
*l
, int entry
, zap_leaf_t
*nl
)
774 struct zap_leaf_entry
*le
, *nle
;
777 le
= ZAP_LEAF_ENTRY(l
, entry
);
778 ASSERT3U(le
->le_type
, ==, ZAP_CHUNK_ENTRY
);
780 chunk
= zap_leaf_chunk_alloc(nl
);
781 nle
= ZAP_LEAF_ENTRY(nl
, chunk
);
782 *nle
= *le
; /* structure assignment */
784 (void) zap_leaf_rehash_entry(nl
, chunk
);
786 nle
->le_name_chunk
= zap_leaf_transfer_array(l
, le
->le_name_chunk
, nl
);
787 nle
->le_value_chunk
=
788 zap_leaf_transfer_array(l
, le
->le_value_chunk
, nl
);
790 zap_leaf_chunk_free(l
, entry
);
792 zap_leaf_phys(l
)->l_hdr
.lh_nentries
--;
793 zap_leaf_phys(nl
)->l_hdr
.lh_nentries
++;
797 * Transfer the entries whose hash prefix ends in 1 to the new leaf.
800 zap_leaf_split(zap_leaf_t
*l
, zap_leaf_t
*nl
, boolean_t sort
)
803 int bit
= 64 - 1 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
805 /* set new prefix and prefix_len */
806 zap_leaf_phys(l
)->l_hdr
.lh_prefix
<<= 1;
807 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
++;
808 zap_leaf_phys(nl
)->l_hdr
.lh_prefix
=
809 zap_leaf_phys(l
)->l_hdr
.lh_prefix
| 1;
810 zap_leaf_phys(nl
)->l_hdr
.lh_prefix_len
=
811 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
813 /* break existing hash chains */
814 zap_memset(zap_leaf_phys(l
)->l_hash
, CHAIN_END
,
815 2*ZAP_LEAF_HASH_NUMENTRIES(l
));
818 zap_leaf_phys(l
)->l_hdr
.lh_flags
|= ZLF_ENTRIES_CDSORTED
;
821 * Transfer entries whose hash bit 'bit' is set to nl; rehash
822 * the remaining entries
824 * NB: We could find entries via the hashtable instead. That
825 * would be O(hashents+numents) rather than O(numblks+numents),
826 * but this accesses memory more sequentially, and when we're
827 * called, the block is usually pretty full.
829 for (i
= 0; i
< ZAP_LEAF_NUMCHUNKS(l
); i
++) {
830 struct zap_leaf_entry
*le
= ZAP_LEAF_ENTRY(l
, i
);
831 if (le
->le_type
!= ZAP_CHUNK_ENTRY
)
834 if (le
->le_hash
& (1ULL << bit
))
835 zap_leaf_transfer_entry(l
, i
, nl
);
837 (void) zap_leaf_rehash_entry(l
, i
);
842 zap_leaf_stats(zap_t
*zap
, zap_leaf_t
*l
, zap_stats_t
*zs
)
846 n
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
-
847 zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
848 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
849 zs
->zs_leafs_with_2n_pointers
[n
]++;
852 n
= zap_leaf_phys(l
)->l_hdr
.lh_nentries
/5;
853 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
854 zs
->zs_blocks_with_n5_entries
[n
]++;
856 n
= ((1<<FZAP_BLOCK_SHIFT(zap
)) -
857 zap_leaf_phys(l
)->l_hdr
.lh_nfree
* (ZAP_LEAF_ARRAY_BYTES
+1))*10 /
858 (1<<FZAP_BLOCK_SHIFT(zap
));
859 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
860 zs
->zs_blocks_n_tenths_full
[n
]++;
862 for (i
= 0; i
< ZAP_LEAF_HASH_NUMENTRIES(l
); i
++) {
864 int chunk
= zap_leaf_phys(l
)->l_hash
[i
];
866 while (chunk
!= CHAIN_END
) {
867 struct zap_leaf_entry
*le
=
868 ZAP_LEAF_ENTRY(l
, chunk
);
870 n
= 1 + ZAP_LEAF_ARRAY_NCHUNKS(le
->le_name_numints
) +
871 ZAP_LEAF_ARRAY_NCHUNKS(le
->le_value_numints
*
872 le
->le_value_intlen
);
873 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
874 zs
->zs_entries_using_n_chunks
[n
]++;
881 n
= MIN(n
, ZAP_HISTOGRAM_SIZE
-1);
882 zs
->zs_buckets_with_n_entries
[n
]++;