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