Optimize RAIDZ expansion
[zfs.git] / module / zfs / zap_leaf.c
blobe396523a94b267383f1e10d5b43f2b145ae9d7e8
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
34 #include <sys/zio.h>
35 #include <sys/spa.h>
36 #include <sys/dmu.h>
37 #include <sys/zfs_context.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zap.h>
40 #include <sys/zap_impl.h>
41 #include <sys/zap_leaf.h>
42 #include <sys/arc.h>
44 static uint16_t *zap_leaf_rehash_entry(zap_leaf_t *l, struct zap_leaf_entry *le,
45 uint16_t entry);
47 #define CHAIN_END 0xffff /* end of the chunk chain */
49 #define LEAF_HASH(l, h) \
50 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \
51 ((h) >> \
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 static void
57 stv(int len, void *addr, uint64_t value)
59 switch (len) {
60 case 1:
61 *(uint8_t *)addr = value;
62 return;
63 case 2:
64 *(uint16_t *)addr = value;
65 return;
66 case 4:
67 *(uint32_t *)addr = value;
68 return;
69 case 8:
70 *(uint64_t *)addr = value;
71 return;
72 default:
73 PANIC("bad int len %d", len);
77 static uint64_t
78 ldv(int len, const void *addr)
80 switch (len) {
81 case 1:
82 return (*(uint8_t *)addr);
83 case 2:
84 return (*(uint16_t *)addr);
85 case 4:
86 return (*(uint32_t *)addr);
87 case 8:
88 return (*(uint64_t *)addr);
89 default:
90 PANIC("bad int len %d", len);
92 return (0xFEEDFACEDEADBEEFULL);
95 void
96 zap_leaf_byteswap(zap_leaf_phys_t *buf, size_t size)
98 zap_leaf_t l;
99 dmu_buf_t l_dbuf;
101 l_dbuf.db_data = buf;
102 l.l_bs = highbit64(size) - 1;
103 l.l_dbuf = &l_dbuf;
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:
122 le = &lc->l_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);
133 break;
134 case ZAP_CHUNK_FREE:
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);
137 break;
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 */
142 break;
143 default:
144 cmn_err(CE_PANIC, "bad leaf type %d",
145 lc->l_free.lf_type);
150 void
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);
166 if (sort)
167 zap_leaf_phys(l)->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
171 * Routines which manipulate leaf chunks (l_chunk[]).
174 static uint16_t
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--;
188 return (chunk);
191 static void
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).
211 static uint16_t
212 zap_leaf_array_create(zap_leaf_t *l, const char *buf,
213 int integer_size, int num_integers)
215 uint16_t chunk_head;
216 uint16_t *chunkp = &chunk_head;
217 int byten = integer_size;
218 uint64_t value = 0;
219 int shift = (integer_size - 1) * 8;
220 int len = num_integers;
222 ASSERT3U(num_integers * integer_size, <=, ZAP_MAXVALUELEN);
224 if (len > 0)
225 value = ldv(integer_size, buf);
226 while (len > 0) {
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;
233 value <<= 8;
234 if (--byten == 0) {
235 if (--len == 0)
236 break;
237 byten = integer_size;
238 buf += integer_size;
239 value = ldv(integer_size, buf);
243 *chunkp = chunk;
244 chunkp = &la->la_next;
246 *chunkp = CHAIN_END;
248 return (chunk_head);
252 * Non-destructively copy array between leaves.
254 static uint16_t
255 zap_leaf_array_copy(zap_leaf_t *l, uint16_t chunk, zap_leaf_t *nl)
257 uint16_t new_chunk;
258 uint16_t *nchunkp = &new_chunk;
260 while (chunk != CHAIN_END) {
261 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
262 uint16_t nchunk = zap_leaf_chunk_alloc(nl);
264 struct zap_leaf_array *la =
265 &ZAP_LEAF_CHUNK(l, chunk).l_array;
266 struct zap_leaf_array *nla =
267 &ZAP_LEAF_CHUNK(nl, nchunk).l_array;
268 ASSERT3U(la->la_type, ==, ZAP_CHUNK_ARRAY);
270 *nla = *la; /* structure assignment */
272 chunk = la->la_next;
273 *nchunkp = nchunk;
274 nchunkp = &nla->la_next;
276 *nchunkp = CHAIN_END;
277 return (new_chunk);
281 * Free array. Unlike trivial loop of zap_leaf_chunk_free() this does
282 * not reverse order of chunks in the free list, reducing fragmentation.
284 static void
285 zap_leaf_array_free(zap_leaf_t *l, uint16_t chunk)
287 struct zap_leaf_header *hdr = &zap_leaf_phys(l)->l_hdr;
288 uint16_t *tailp = &hdr->lh_freelist;
289 uint16_t oldfree = *tailp;
291 while (chunk != CHAIN_END) {
292 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
293 zap_leaf_chunk_t *c = &ZAP_LEAF_CHUNK(l, chunk);
294 ASSERT3U(c->l_array.la_type, ==, ZAP_CHUNK_ARRAY);
296 *tailp = chunk;
297 chunk = c->l_array.la_next;
299 c->l_free.lf_type = ZAP_CHUNK_FREE;
300 memset(c->l_free.lf_pad, 0, sizeof (c->l_free.lf_pad));
301 tailp = &c->l_free.lf_next;
303 ASSERT3U(hdr->lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l));
304 hdr->lh_nfree++;
307 *tailp = oldfree;
310 /* array_len and buf_len are in integers, not bytes */
311 static void
312 zap_leaf_array_read(zap_leaf_t *l, uint16_t chunk,
313 int array_int_len, int array_len, int buf_int_len, uint64_t buf_len,
314 void *buf)
316 int len = MIN(array_len, buf_len);
317 int byten = 0;
318 uint64_t value = 0;
319 char *p = buf;
321 ASSERT3U(array_int_len, <=, buf_int_len);
323 /* Fast path for one 8-byte integer */
324 if (array_int_len == 8 && buf_int_len == 8 && len == 1) {
325 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
326 uint8_t *ip = la->la_array;
327 uint64_t *buf64 = buf;
329 *buf64 = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
330 (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
331 (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
332 (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
333 return;
336 /* Fast path for an array of 1-byte integers (eg. the entry name) */
337 if (array_int_len == 1 && buf_int_len == 1 &&
338 buf_len > array_len + ZAP_LEAF_ARRAY_BYTES) {
339 while (chunk != CHAIN_END) {
340 struct zap_leaf_array *la =
341 &ZAP_LEAF_CHUNK(l, chunk).l_array;
342 memcpy(p, la->la_array, ZAP_LEAF_ARRAY_BYTES);
343 p += ZAP_LEAF_ARRAY_BYTES;
344 chunk = la->la_next;
346 return;
349 while (len > 0) {
350 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
352 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
353 for (int i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) {
354 value = (value << 8) | la->la_array[i];
355 byten++;
356 if (byten == array_int_len) {
357 stv(buf_int_len, p, value);
358 byten = 0;
359 len--;
360 if (len == 0)
361 return;
362 p += buf_int_len;
365 chunk = la->la_next;
369 static boolean_t
370 zap_leaf_array_match(zap_leaf_t *l, zap_name_t *zn,
371 uint_t chunk, int array_numints)
373 int bseen = 0;
375 if (zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY) {
376 uint64_t *thiskey =
377 kmem_alloc(array_numints * sizeof (*thiskey), KM_SLEEP);
378 ASSERT(zn->zn_key_intlen == sizeof (*thiskey));
380 zap_leaf_array_read(l, chunk, sizeof (*thiskey), array_numints,
381 sizeof (*thiskey), array_numints, thiskey);
382 boolean_t match = memcmp(thiskey, zn->zn_key_orig,
383 array_numints * sizeof (*thiskey)) == 0;
384 kmem_free(thiskey, array_numints * sizeof (*thiskey));
385 return (match);
388 ASSERT(zn->zn_key_intlen == 1);
389 if (zn->zn_matchtype & MT_NORMALIZE) {
390 char *thisname = kmem_alloc(array_numints, KM_SLEEP);
392 zap_leaf_array_read(l, chunk, sizeof (char), array_numints,
393 sizeof (char), array_numints, thisname);
394 boolean_t match = zap_match(zn, thisname);
395 kmem_free(thisname, array_numints);
396 return (match);
400 * Fast path for exact matching.
401 * First check that the lengths match, so that we don't read
402 * past the end of the zn_key_orig array.
404 if (array_numints != zn->zn_key_orig_numints)
405 return (B_FALSE);
406 while (bseen < array_numints) {
407 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array;
408 int toread = MIN(array_numints - bseen, ZAP_LEAF_ARRAY_BYTES);
409 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
410 if (memcmp(la->la_array, (char *)zn->zn_key_orig + bseen,
411 toread))
412 break;
413 chunk = la->la_next;
414 bseen += toread;
416 return (bseen == array_numints);
420 * Routines which manipulate leaf entries.
424 zap_leaf_lookup(zap_leaf_t *l, zap_name_t *zn, zap_entry_handle_t *zeh)
426 struct zap_leaf_entry *le;
428 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
430 for (uint16_t *chunkp = LEAF_HASH_ENTPTR(l, zn->zn_hash);
431 *chunkp != CHAIN_END; chunkp = &le->le_next) {
432 uint16_t chunk = *chunkp;
433 le = ZAP_LEAF_ENTRY(l, chunk);
435 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
436 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
438 if (le->le_hash != zn->zn_hash)
439 continue;
442 * NB: the entry chain is always sorted by cd on
443 * normalized zap objects, so this will find the
444 * lowest-cd match for MT_NORMALIZE.
446 ASSERT((zn->zn_matchtype == 0) ||
447 (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED));
448 if (zap_leaf_array_match(l, zn, le->le_name_chunk,
449 le->le_name_numints)) {
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_chunkp = chunkp;
455 zeh->zeh_leaf = l;
456 return (0);
460 return (SET_ERROR(ENOENT));
463 /* Return (h1,cd1 >= h2,cd2) */
464 #define HCD_GTEQ(h1, cd1, h2, cd2) \
465 ((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE))
468 zap_leaf_lookup_closest(zap_leaf_t *l,
469 uint64_t h, uint32_t cd, zap_entry_handle_t *zeh)
471 uint64_t besth = -1ULL;
472 uint32_t bestcd = -1U;
473 uint16_t bestlh = ZAP_LEAF_HASH_NUMENTRIES(l)-1;
474 struct zap_leaf_entry *le;
476 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
478 for (uint16_t lh = LEAF_HASH(l, h); lh <= bestlh; lh++) {
479 for (uint16_t chunk = zap_leaf_phys(l)->l_hash[lh];
480 chunk != CHAIN_END; chunk = le->le_next) {
481 le = ZAP_LEAF_ENTRY(l, chunk);
483 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l));
484 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
486 if (HCD_GTEQ(le->le_hash, le->le_cd, h, cd) &&
487 HCD_GTEQ(besth, bestcd, le->le_hash, le->le_cd)) {
488 ASSERT3U(bestlh, >=, lh);
489 bestlh = lh;
490 besth = le->le_hash;
491 bestcd = le->le_cd;
493 zeh->zeh_num_integers = le->le_value_numints;
494 zeh->zeh_integer_size = le->le_value_intlen;
495 zeh->zeh_cd = le->le_cd;
496 zeh->zeh_hash = le->le_hash;
497 zeh->zeh_fakechunk = chunk;
498 zeh->zeh_chunkp = &zeh->zeh_fakechunk;
499 zeh->zeh_leaf = l;
504 return (bestcd == -1U ? SET_ERROR(ENOENT) : 0);
508 zap_entry_read(const zap_entry_handle_t *zeh,
509 uint8_t integer_size, uint64_t num_integers, void *buf)
511 struct zap_leaf_entry *le =
512 ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
513 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
515 if (le->le_value_intlen > integer_size)
516 return (SET_ERROR(EINVAL));
518 zap_leaf_array_read(zeh->zeh_leaf, le->le_value_chunk,
519 le->le_value_intlen, le->le_value_numints,
520 integer_size, num_integers, buf);
522 if (zeh->zeh_num_integers > num_integers)
523 return (SET_ERROR(EOVERFLOW));
524 return (0);
529 zap_entry_read_name(zap_t *zap, const zap_entry_handle_t *zeh, uint16_t buflen,
530 char *buf)
532 struct zap_leaf_entry *le =
533 ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp);
534 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
536 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
537 zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 8,
538 le->le_name_numints, 8, buflen / 8, buf);
539 } else {
540 zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 1,
541 le->le_name_numints, 1, buflen, buf);
543 if (le->le_name_numints > buflen)
544 return (SET_ERROR(EOVERFLOW));
545 return (0);
549 zap_entry_update(zap_entry_handle_t *zeh,
550 uint8_t integer_size, uint64_t num_integers, const void *buf)
552 zap_leaf_t *l = zeh->zeh_leaf;
553 struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, *zeh->zeh_chunkp);
555 int delta_chunks = ZAP_LEAF_ARRAY_NCHUNKS(num_integers * integer_size) -
556 ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_numints * le->le_value_intlen);
558 if ((int)zap_leaf_phys(l)->l_hdr.lh_nfree < delta_chunks)
559 return (SET_ERROR(EAGAIN));
561 zap_leaf_array_free(l, le->le_value_chunk);
562 le->le_value_chunk =
563 zap_leaf_array_create(l, buf, integer_size, num_integers);
564 le->le_value_numints = num_integers;
565 le->le_value_intlen = integer_size;
566 return (0);
569 void
570 zap_entry_remove(zap_entry_handle_t *zeh)
572 zap_leaf_t *l = zeh->zeh_leaf;
574 ASSERT3P(zeh->zeh_chunkp, !=, &zeh->zeh_fakechunk);
576 uint16_t entry_chunk = *zeh->zeh_chunkp;
577 struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry_chunk);
578 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
580 *zeh->zeh_chunkp = le->le_next;
582 /* Free in opposite order to reduce fragmentation. */
583 zap_leaf_array_free(l, le->le_value_chunk);
584 zap_leaf_array_free(l, le->le_name_chunk);
585 zap_leaf_chunk_free(l, entry_chunk);
587 zap_leaf_phys(l)->l_hdr.lh_nentries--;
591 zap_entry_create(zap_leaf_t *l, zap_name_t *zn, uint32_t cd,
592 uint8_t integer_size, uint64_t num_integers, const void *buf,
593 zap_entry_handle_t *zeh)
595 uint16_t chunk;
596 struct zap_leaf_entry *le;
597 uint64_t h = zn->zn_hash;
599 uint64_t valuelen = integer_size * num_integers;
601 uint_t numchunks = 1 + ZAP_LEAF_ARRAY_NCHUNKS(zn->zn_key_orig_numints *
602 zn->zn_key_intlen) + ZAP_LEAF_ARRAY_NCHUNKS(valuelen);
603 if (numchunks > ZAP_LEAF_NUMCHUNKS(l))
604 return (SET_ERROR(E2BIG));
606 if (cd == ZAP_NEED_CD) {
607 /* find the lowest unused cd */
608 if (zap_leaf_phys(l)->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED) {
609 cd = 0;
611 for (chunk = *LEAF_HASH_ENTPTR(l, h);
612 chunk != CHAIN_END; chunk = le->le_next) {
613 le = ZAP_LEAF_ENTRY(l, chunk);
614 if (le->le_cd > cd)
615 break;
616 if (le->le_hash == h) {
617 ASSERT3U(cd, ==, le->le_cd);
618 cd++;
621 } else {
622 /* old unsorted format; do it the O(n^2) way */
623 for (cd = 0; ; cd++) {
624 for (chunk = *LEAF_HASH_ENTPTR(l, h);
625 chunk != CHAIN_END; chunk = le->le_next) {
626 le = ZAP_LEAF_ENTRY(l, chunk);
627 if (le->le_hash == h &&
628 le->le_cd == cd) {
629 break;
632 /* If this cd is not in use, we are good. */
633 if (chunk == CHAIN_END)
634 break;
638 * We would run out of space in a block before we could
639 * store enough entries to run out of CD values.
641 ASSERT3U(cd, <, zap_maxcd(zn->zn_zap));
644 if (zap_leaf_phys(l)->l_hdr.lh_nfree < numchunks)
645 return (SET_ERROR(EAGAIN));
647 /* make the entry */
648 chunk = zap_leaf_chunk_alloc(l);
649 le = ZAP_LEAF_ENTRY(l, chunk);
650 le->le_type = ZAP_CHUNK_ENTRY;
651 le->le_name_chunk = zap_leaf_array_create(l, zn->zn_key_orig,
652 zn->zn_key_intlen, zn->zn_key_orig_numints);
653 le->le_name_numints = zn->zn_key_orig_numints;
654 le->le_value_chunk =
655 zap_leaf_array_create(l, buf, integer_size, num_integers);
656 le->le_value_numints = num_integers;
657 le->le_value_intlen = integer_size;
658 le->le_hash = h;
659 le->le_cd = cd;
661 /* link it into the hash chain */
662 /* XXX if we did the search above, we could just use that */
663 uint16_t *chunkp = zap_leaf_rehash_entry(l, le, chunk);
665 zap_leaf_phys(l)->l_hdr.lh_nentries++;
667 zeh->zeh_leaf = l;
668 zeh->zeh_num_integers = num_integers;
669 zeh->zeh_integer_size = le->le_value_intlen;
670 zeh->zeh_cd = le->le_cd;
671 zeh->zeh_hash = le->le_hash;
672 zeh->zeh_chunkp = chunkp;
674 return (0);
678 * Determine if there is another entry with the same normalized form.
679 * For performance purposes, either zn or name must be provided (the
680 * other can be NULL). Note, there usually won't be any hash
681 * conflicts, in which case we don't need the concatenated/normalized
682 * form of the name. But all callers have one of these on hand anyway,
683 * so might as well take advantage. A cleaner but slower interface
684 * would accept neither argument, and compute the normalized name as
685 * needed (using zap_name_alloc_str(zap_entry_read_name(zeh))).
687 boolean_t
688 zap_entry_normalization_conflict(zap_entry_handle_t *zeh, zap_name_t *zn,
689 const char *name, zap_t *zap)
691 struct zap_leaf_entry *le;
692 boolean_t allocdzn = B_FALSE;
694 if (zap->zap_normflags == 0)
695 return (B_FALSE);
697 for (uint16_t chunk = *LEAF_HASH_ENTPTR(zeh->zeh_leaf, zeh->zeh_hash);
698 chunk != CHAIN_END; chunk = le->le_next) {
699 le = ZAP_LEAF_ENTRY(zeh->zeh_leaf, chunk);
700 if (le->le_hash != zeh->zeh_hash)
701 continue;
702 if (le->le_cd == zeh->zeh_cd)
703 continue;
705 if (zn == NULL) {
706 zn = zap_name_alloc_str(zap, name, MT_NORMALIZE);
707 allocdzn = B_TRUE;
709 if (zap_leaf_array_match(zeh->zeh_leaf, zn,
710 le->le_name_chunk, le->le_name_numints)) {
711 if (allocdzn)
712 zap_name_free(zn);
713 return (B_TRUE);
716 if (allocdzn)
717 zap_name_free(zn);
718 return (B_FALSE);
722 * Routines for transferring entries between leafs.
725 static uint16_t *
726 zap_leaf_rehash_entry(zap_leaf_t *l, struct zap_leaf_entry *le, uint16_t entry)
728 struct zap_leaf_entry *le2;
729 uint16_t *chunkp;
732 * keep the entry chain sorted by cd
733 * NB: this will not cause problems for unsorted leafs, though
734 * it is unnecessary there.
736 for (chunkp = LEAF_HASH_ENTPTR(l, le->le_hash);
737 *chunkp != CHAIN_END; chunkp = &le2->le_next) {
738 le2 = ZAP_LEAF_ENTRY(l, *chunkp);
739 if (le2->le_cd > le->le_cd)
740 break;
743 le->le_next = *chunkp;
744 *chunkp = entry;
745 return (chunkp);
748 static void
749 zap_leaf_transfer_entry(zap_leaf_t *l, uint_t entry, zap_leaf_t *nl)
751 struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry);
752 ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY);
754 uint16_t chunk = zap_leaf_chunk_alloc(nl);
755 struct zap_leaf_entry *nle = ZAP_LEAF_ENTRY(nl, chunk);
756 *nle = *le; /* structure assignment */
758 (void) zap_leaf_rehash_entry(nl, nle, chunk);
760 nle->le_name_chunk = zap_leaf_array_copy(l, le->le_name_chunk, nl);
761 nle->le_value_chunk = zap_leaf_array_copy(l, le->le_value_chunk, nl);
763 /* Free in opposite order to reduce fragmentation. */
764 zap_leaf_array_free(l, le->le_value_chunk);
765 zap_leaf_array_free(l, le->le_name_chunk);
766 zap_leaf_chunk_free(l, entry);
768 zap_leaf_phys(l)->l_hdr.lh_nentries--;
769 zap_leaf_phys(nl)->l_hdr.lh_nentries++;
773 * Transfer the entries whose hash prefix ends in 1 to the new leaf.
775 void
776 zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort)
778 uint_t bit = 64 - 1 - zap_leaf_phys(l)->l_hdr.lh_prefix_len;
780 /* set new prefix and prefix_len */
781 zap_leaf_phys(l)->l_hdr.lh_prefix <<= 1;
782 zap_leaf_phys(l)->l_hdr.lh_prefix_len++;
783 zap_leaf_phys(nl)->l_hdr.lh_prefix =
784 zap_leaf_phys(l)->l_hdr.lh_prefix | 1;
785 zap_leaf_phys(nl)->l_hdr.lh_prefix_len =
786 zap_leaf_phys(l)->l_hdr.lh_prefix_len;
788 /* break existing hash chains */
789 memset(zap_leaf_phys(l)->l_hash, CHAIN_END,
790 2*ZAP_LEAF_HASH_NUMENTRIES(l));
792 if (sort)
793 zap_leaf_phys(l)->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED;
796 * Transfer entries whose hash bit 'bit' is set to nl; rehash
797 * the remaining entries
799 * NB: We could find entries via the hashtable instead. That
800 * would be O(hashents+numents) rather than O(numblks+numents),
801 * but this accesses memory more sequentially, and when we're
802 * called, the block is usually pretty full.
804 for (uint_t i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) {
805 struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, i);
806 if (le->le_type != ZAP_CHUNK_ENTRY)
807 continue;
809 if (le->le_hash & (1ULL << bit))
810 zap_leaf_transfer_entry(l, i, nl);
811 else
812 (void) zap_leaf_rehash_entry(l, le, i);
816 void
817 zap_leaf_stats(zap_t *zap, zap_leaf_t *l, zap_stats_t *zs)
819 uint_t n = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
820 zap_leaf_phys(l)->l_hdr.lh_prefix_len;
821 n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
822 zs->zs_leafs_with_2n_pointers[n]++;
825 n = zap_leaf_phys(l)->l_hdr.lh_nentries/5;
826 n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
827 zs->zs_blocks_with_n5_entries[n]++;
829 n = ((1<<FZAP_BLOCK_SHIFT(zap)) -
830 zap_leaf_phys(l)->l_hdr.lh_nfree * (ZAP_LEAF_ARRAY_BYTES+1))*10 /
831 (1<<FZAP_BLOCK_SHIFT(zap));
832 n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
833 zs->zs_blocks_n_tenths_full[n]++;
835 for (uint_t i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(l); i++) {
836 uint_t nentries = 0;
837 uint_t chunk = zap_leaf_phys(l)->l_hash[i];
839 while (chunk != CHAIN_END) {
840 struct zap_leaf_entry *le =
841 ZAP_LEAF_ENTRY(l, chunk);
843 n = 1 + ZAP_LEAF_ARRAY_NCHUNKS(le->le_name_numints) +
844 ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_numints *
845 le->le_value_intlen);
846 n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
847 zs->zs_entries_using_n_chunks[n]++;
849 chunk = le->le_next;
850 nentries++;
853 n = nentries;
854 n = MIN(n, ZAP_HISTOGRAM_SIZE-1);
855 zs->zs_buckets_with_n_entries[n]++;