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