]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zap_micro.c
Unify behavior of deadman parameters
[mirror_zfs.git] / module / zfs / zap_micro.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.
8bea9815 24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
0c66c32d 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
9b7b9cd3 26 * Copyright 2017 Nexenta Systems, Inc.
34dc7c2f
BB
27 */
28
428870ff 29#include <sys/zio.h>
34dc7c2f
BB
30#include <sys/spa.h>
31#include <sys/dmu.h>
32#include <sys/zfs_context.h>
33#include <sys/zap.h>
34#include <sys/refcount.h>
35#include <sys/zap_impl.h>
36#include <sys/zap_leaf.h>
37#include <sys/avl.h>
428870ff 38#include <sys/arc.h>
f1512ee6 39#include <sys/dmu_objset.h>
34dc7c2f
BB
40
41#ifdef _KERNEL
42#include <sys/sunddi.h>
43#endif
44
d683ddbb
JG
45extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
46
8bea9815
MA
47static int mzap_upgrade(zap_t **zapp,
48 void *tag, dmu_tx_t *tx, zap_flags_t flags);
34dc7c2f 49
428870ff
BB
50uint64_t
51zap_getflags(zap_t *zap)
52{
53 if (zap->zap_ismicro)
54 return (0);
d683ddbb 55 return (zap_f_phys(zap)->zap_flags);
428870ff 56}
34dc7c2f 57
428870ff
BB
58int
59zap_hashbits(zap_t *zap)
34dc7c2f 60{
428870ff
BB
61 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
62 return (48);
63 else
64 return (28);
65}
34dc7c2f 66
428870ff
BB
67uint32_t
68zap_maxcd(zap_t *zap)
69{
70 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
71 return ((1<<16)-1);
72 else
73 return (-1U);
74}
34dc7c2f 75
428870ff
BB
76static uint64_t
77zap_hash(zap_name_t *zn)
78{
79 zap_t *zap = zn->zn_zap;
80 uint64_t h = 0;
34dc7c2f 81
428870ff
BB
82 if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
83 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
84 h = *(uint64_t *)zn->zn_key_orig;
85 } else {
86 h = zap->zap_salt;
87 ASSERT(h != 0);
88 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
89
90 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
91 int i;
92 const uint64_t *wp = zn->zn_key_norm;
93
94 ASSERT(zn->zn_key_intlen == 8);
95 for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
96 int j;
97 uint64_t word = *wp;
98
99 for (j = 0; j < zn->zn_key_intlen; j++) {
100 h = (h >> 8) ^
101 zfs_crc64_table[(h ^ word) & 0xFF];
102 word >>= NBBY;
103 }
104 }
105 } else {
106 int i, len;
107 const uint8_t *cp = zn->zn_key_norm;
108
109 /*
110 * We previously stored the terminating null on
111 * disk, but didn't hash it, so we need to
112 * continue to not hash it. (The
113 * zn_key_*_numints includes the terminating
114 * null for non-binary keys.)
115 */
116 len = zn->zn_key_norm_numints - 1;
117
118 ASSERT(zn->zn_key_intlen == 1);
119 for (i = 0; i < len; cp++, i++) {
120 h = (h >> 8) ^
121 zfs_crc64_table[(h ^ *cp) & 0xFF];
122 }
123 }
124 }
34dc7c2f 125 /*
428870ff
BB
126 * Don't use all 64 bits, since we need some in the cookie for
127 * the collision differentiator. We MUST use the high bits,
128 * since those are the ones that we first pay attention to when
4e33ba4c 129 * choosing the bucket.
34dc7c2f 130 */
428870ff 131 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
34dc7c2f 132
428870ff 133 return (h);
34dc7c2f
BB
134}
135
136static int
9b7b9cd3 137zap_normalize(zap_t *zap, const char *name, char *namenorm, int normflags)
34dc7c2f
BB
138{
139 size_t inlen, outlen;
140 int err;
141
428870ff
BB
142 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
143
34dc7c2f
BB
144 inlen = strlen(name) + 1;
145 outlen = ZAP_MAXNAMELEN;
146
147 err = 0;
148 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
9b7b9cd3
GM
149 normflags | U8_TEXTPREP_IGNORE_NULL | U8_TEXTPREP_IGNORE_INVALID,
150 U8_UNICODE_LATEST, &err);
34dc7c2f
BB
151
152 return (err);
153}
154
155boolean_t
156zap_match(zap_name_t *zn, const char *matchname)
157{
428870ff
BB
158 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
159
9b7b9cd3 160 if (zn->zn_matchtype & MT_NORMALIZE) {
34dc7c2f
BB
161 char norm[ZAP_MAXNAMELEN];
162
9b7b9cd3
GM
163 if (zap_normalize(zn->zn_zap, matchname, norm,
164 zn->zn_normflags) != 0)
34dc7c2f
BB
165 return (B_FALSE);
166
428870ff 167 return (strcmp(zn->zn_key_norm, norm) == 0);
34dc7c2f 168 } else {
428870ff 169 return (strcmp(zn->zn_key_orig, matchname) == 0);
34dc7c2f
BB
170 }
171}
172
173void
174zap_name_free(zap_name_t *zn)
175{
176 kmem_free(zn, sizeof (zap_name_t));
177}
178
34dc7c2f 179zap_name_t *
428870ff 180zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
34dc7c2f 181{
79c76d5b 182 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
34dc7c2f
BB
183
184 zn->zn_zap = zap;
428870ff
BB
185 zn->zn_key_intlen = sizeof (*key);
186 zn->zn_key_orig = key;
187 zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
34dc7c2f 188 zn->zn_matchtype = mt;
9b7b9cd3
GM
189 zn->zn_normflags = zap->zap_normflags;
190
191 /*
192 * If we're dealing with a case sensitive lookup on a mixed or
193 * insensitive fs, remove U8_TEXTPREP_TOUPPER or the lookup
194 * will fold case to all caps overriding the lookup request.
195 */
196 if (mt & MT_MATCH_CASE)
197 zn->zn_normflags &= ~U8_TEXTPREP_TOUPPER;
198
34dc7c2f 199 if (zap->zap_normflags) {
9b7b9cd3
GM
200 /*
201 * We *must* use zap_normflags because this normalization is
202 * what the hash is computed from.
203 */
204 if (zap_normalize(zap, key, zn->zn_normbuf,
205 zap->zap_normflags) != 0) {
34dc7c2f
BB
206 zap_name_free(zn);
207 return (NULL);
208 }
428870ff
BB
209 zn->zn_key_norm = zn->zn_normbuf;
210 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
34dc7c2f 211 } else {
9b7b9cd3 212 if (mt != 0) {
34dc7c2f
BB
213 zap_name_free(zn);
214 return (NULL);
215 }
428870ff
BB
216 zn->zn_key_norm = zn->zn_key_orig;
217 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
34dc7c2f
BB
218 }
219
428870ff 220 zn->zn_hash = zap_hash(zn);
9b7b9cd3
GM
221
222 if (zap->zap_normflags != zn->zn_normflags) {
223 /*
224 * We *must* use zn_normflags because this normalization is
225 * what the matching is based on. (Not the hash!)
226 */
227 if (zap_normalize(zap, key, zn->zn_normbuf,
228 zn->zn_normflags) != 0) {
229 zap_name_free(zn);
230 return (NULL);
231 }
232 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
233 }
234
428870ff
BB
235 return (zn);
236}
237
238zap_name_t *
239zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
240{
79c76d5b 241 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
428870ff
BB
242
243 ASSERT(zap->zap_normflags == 0);
244 zn->zn_zap = zap;
245 zn->zn_key_intlen = sizeof (*key);
246 zn->zn_key_orig = zn->zn_key_norm = key;
247 zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
9b7b9cd3 248 zn->zn_matchtype = 0;
428870ff
BB
249
250 zn->zn_hash = zap_hash(zn);
34dc7c2f
BB
251 return (zn);
252}
253
254static void
255mzap_byteswap(mzap_phys_t *buf, size_t size)
256{
257 int i, max;
258 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
259 buf->mz_salt = BSWAP_64(buf->mz_salt);
260 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
261 max = (size / MZAP_ENT_LEN) - 1;
262 for (i = 0; i < max; i++) {
263 buf->mz_chunk[i].mze_value =
264 BSWAP_64(buf->mz_chunk[i].mze_value);
265 buf->mz_chunk[i].mze_cd =
266 BSWAP_32(buf->mz_chunk[i].mze_cd);
267 }
268}
269
270void
271zap_byteswap(void *buf, size_t size)
272{
273 uint64_t block_type;
274
275 block_type = *(uint64_t *)buf;
276
277 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
278 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
279 mzap_byteswap(buf, size);
280 } else {
281 fzap_byteswap(buf, size);
282 }
283}
284
285static int
286mze_compare(const void *arg1, const void *arg2)
287{
288 const mzap_ent_t *mze1 = arg1;
289 const mzap_ent_t *mze2 = arg2;
290
ee36c709
GN
291 int cmp = AVL_CMP(mze1->mze_hash, mze2->mze_hash);
292 if (likely(cmp))
293 return (cmp);
294
295 return (AVL_CMP(mze1->mze_cd, mze2->mze_cd));
34dc7c2f
BB
296}
297
298static void
428870ff 299mze_insert(zap_t *zap, int chunkid, uint64_t hash)
34dc7c2f
BB
300{
301 mzap_ent_t *mze;
302
303 ASSERT(zap->zap_ismicro);
304 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
34dc7c2f 305
79c76d5b 306 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
34dc7c2f
BB
307 mze->mze_chunkid = chunkid;
308 mze->mze_hash = hash;
428870ff
BB
309 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
310 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
34dc7c2f
BB
311 avl_add(&zap->zap_m.zap_avl, mze);
312}
313
314static mzap_ent_t *
315mze_find(zap_name_t *zn)
316{
317 mzap_ent_t mze_tofind;
318 mzap_ent_t *mze;
319 avl_index_t idx;
320 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
321
322 ASSERT(zn->zn_zap->zap_ismicro);
323 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
324
34dc7c2f 325 mze_tofind.mze_hash = zn->zn_hash;
428870ff 326 mze_tofind.mze_cd = 0;
34dc7c2f 327
34dc7c2f
BB
328 mze = avl_find(avl, &mze_tofind, &idx);
329 if (mze == NULL)
330 mze = avl_nearest(avl, idx, AVL_AFTER);
331 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
428870ff
BB
332 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
333 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
34dc7c2f
BB
334 return (mze);
335 }
9b7b9cd3 336
34dc7c2f
BB
337 return (NULL);
338}
339
340static uint32_t
341mze_find_unused_cd(zap_t *zap, uint64_t hash)
342{
343 mzap_ent_t mze_tofind;
344 mzap_ent_t *mze;
345 avl_index_t idx;
346 avl_tree_t *avl = &zap->zap_m.zap_avl;
347 uint32_t cd;
348
349 ASSERT(zap->zap_ismicro);
350 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
351
352 mze_tofind.mze_hash = hash;
428870ff 353 mze_tofind.mze_cd = 0;
34dc7c2f
BB
354
355 cd = 0;
356 for (mze = avl_find(avl, &mze_tofind, &idx);
357 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
428870ff 358 if (mze->mze_cd != cd)
34dc7c2f
BB
359 break;
360 cd++;
361 }
362
363 return (cd);
364}
365
599b8648
CC
366/*
367 * Each mzap entry requires at max : 4 chunks
368 * 3 chunks for names + 1 chunk for value.
369 */
370#define MZAP_ENT_CHUNKS (1 + ZAP_LEAF_ARRAY_NCHUNKS(MZAP_NAME_LEN) + \
371 ZAP_LEAF_ARRAY_NCHUNKS(sizeof (uint64_t)))
372
373/*
374 * Check if the current entry keeps the colliding entries under the fatzap leaf
375 * size.
376 */
377static boolean_t
378mze_canfit_fzap_leaf(zap_name_t *zn, uint64_t hash)
379{
380 zap_t *zap = zn->zn_zap;
381 mzap_ent_t mze_tofind;
382 mzap_ent_t *mze;
383 avl_index_t idx;
384 avl_tree_t *avl = &zap->zap_m.zap_avl;
385 uint32_t mzap_ents = 0;
386
387 mze_tofind.mze_hash = hash;
388 mze_tofind.mze_cd = 0;
389
390 for (mze = avl_find(avl, &mze_tofind, &idx);
391 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
392 mzap_ents++;
393 }
394
395 /* Include the new entry being added */
396 mzap_ents++;
397
398 return (ZAP_LEAF_NUMCHUNKS_DEF > (mzap_ents * MZAP_ENT_CHUNKS));
399}
400
34dc7c2f
BB
401static void
402mze_remove(zap_t *zap, mzap_ent_t *mze)
403{
404 ASSERT(zap->zap_ismicro);
405 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
406
407 avl_remove(&zap->zap_m.zap_avl, mze);
408 kmem_free(mze, sizeof (mzap_ent_t));
409}
410
411static void
412mze_destroy(zap_t *zap)
413{
414 mzap_ent_t *mze;
415 void *avlcookie = NULL;
416
c65aa5b2 417 while ((mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie)))
34dc7c2f
BB
418 kmem_free(mze, sizeof (mzap_ent_t));
419 avl_destroy(&zap->zap_m.zap_avl);
420}
421
422static zap_t *
423mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
424{
425 zap_t *winner;
426 zap_t *zap;
427 int i;
32c8c946
CC
428 uint64_t *zap_hdr = (uint64_t *)db->db_data;
429 uint64_t zap_block_type = zap_hdr[0];
430 uint64_t zap_magic = zap_hdr[1];
34dc7c2f
BB
431
432 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
433
79c76d5b 434 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
ef5319df 435 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
34dc7c2f
BB
436 rw_enter(&zap->zap_rwlock, RW_WRITER);
437 zap->zap_objset = os;
438 zap->zap_object = obj;
439 zap->zap_dbuf = db;
440
32c8c946 441 if (zap_block_type != ZBT_MICRO) {
c17486b2
GN
442 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT,
443 0);
9bd274dd 444 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
32c8c946
CC
445 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
446 winner = NULL; /* No actual winner here... */
447 goto handle_winner;
448 }
34dc7c2f
BB
449 } else {
450 zap->zap_ismicro = TRUE;
451 }
452
453 /*
454 * Make sure that zap_ismicro is set before we let others see
455 * it, because zap_lockdir() checks zap_ismicro without the lock
456 * held.
457 */
39efbde7 458 dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf);
0c66c32d 459 winner = dmu_buf_set_user(db, &zap->zap_dbu);
34dc7c2f 460
32c8c946
CC
461 if (winner != NULL)
462 goto handle_winner;
34dc7c2f
BB
463
464 if (zap->zap_ismicro) {
d683ddbb
JG
465 zap->zap_salt = zap_m_phys(zap)->mz_salt;
466 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
34dc7c2f
BB
467 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
468 avl_create(&zap->zap_m.zap_avl, mze_compare,
469 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
470
471 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
472 mzap_ent_phys_t *mze =
d683ddbb 473 &zap_m_phys(zap)->mz_chunk[i];
34dc7c2f
BB
474 if (mze->mze_name[0]) {
475 zap_name_t *zn;
476
477 zap->zap_m.zap_num_entries++;
9b7b9cd3 478 zn = zap_name_alloc(zap, mze->mze_name, 0);
428870ff 479 mze_insert(zap, i, zn->zn_hash);
34dc7c2f
BB
480 zap_name_free(zn);
481 }
482 }
483 } else {
d683ddbb
JG
484 zap->zap_salt = zap_f_phys(zap)->zap_salt;
485 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
34dc7c2f
BB
486
487 ASSERT3U(sizeof (struct zap_leaf_header), ==,
488 2*ZAP_LEAF_CHUNKSIZE);
489
490 /*
491 * The embedded pointer table should not overlap the
492 * other members.
493 */
494 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
d683ddbb 495 &zap_f_phys(zap)->zap_salt);
34dc7c2f
BB
496
497 /*
498 * The embedded pointer table should end at the end of
499 * the block
500 */
501 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
502 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
d683ddbb 503 (uintptr_t)zap_f_phys(zap), ==,
34dc7c2f
BB
504 zap->zap_dbuf->db_size);
505 }
506 rw_exit(&zap->zap_rwlock);
507 return (zap);
32c8c946
CC
508
509handle_winner:
510 rw_exit(&zap->zap_rwlock);
511 rw_destroy(&zap->zap_rwlock);
512 if (!zap->zap_ismicro)
513 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
514 kmem_free(zap, sizeof (zap_t));
515 return (winner);
34dc7c2f
BB
516}
517
8bea9815
MA
518static int
519zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
34dc7c2f
BB
520 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
521{
ceb49b0a 522 dmu_object_info_t doi;
34dc7c2f 523 zap_t *zap;
34dc7c2f 524 krw_t lt;
34dc7c2f 525
8bea9815
MA
526 objset_t *os = dmu_buf_get_objset(db);
527 uint64_t obj = db->db_object;
34dc7c2f 528
8bea9815
MA
529 ASSERT0(db->db_offset);
530 *zapp = NULL;
34dc7c2f 531
ceb49b0a
BB
532 dmu_object_info_from_db(db, &doi);
533 if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
534 return (SET_ERROR(EINVAL));
34dc7c2f
BB
535
536 zap = dmu_buf_get_user(db);
32c8c946 537 if (zap == NULL) {
34dc7c2f 538 zap = mzap_open(os, obj, db);
32c8c946
CC
539 if (zap == NULL) {
540 /*
541 * mzap_open() didn't like what it saw on-disk.
542 * Check for corruption!
543 */
32c8c946
CC
544 return (SET_ERROR(EIO));
545 }
546 }
34dc7c2f
BB
547
548 /*
549 * We're checking zap_ismicro without the lock held, in order to
550 * tell what type of lock we want. Once we have some sort of
551 * lock, see if it really is the right type. In practice this
552 * can only be different if it was upgraded from micro to fat,
553 * and micro wanted WRITER but fat only needs READER.
554 */
555 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
556 rw_enter(&zap->zap_rwlock, lt);
557 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
558 /* it was upgraded, now we only need reader */
559 ASSERT(lt == RW_WRITER);
560 ASSERT(RW_READER ==
6d79eabf 561 ((!zap->zap_ismicro && fatreader) ? RW_READER : lti));
34dc7c2f
BB
562 rw_downgrade(&zap->zap_rwlock);
563 lt = RW_READER;
564 }
565
566 zap->zap_objset = os;
567
568 if (lt == RW_WRITER)
569 dmu_buf_will_dirty(db, tx);
570
571 ASSERT3P(zap->zap_dbuf, ==, db);
572
573 ASSERT(!zap->zap_ismicro ||
574 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
575 if (zap->zap_ismicro && tx && adding &&
576 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
577 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
578 if (newsz > MZAP_MAX_BLKSZ) {
579 dprintf("upgrading obj %llu: num_entries=%u\n",
580 obj, zap->zap_m.zap_num_entries);
581 *zapp = zap;
1c27024e 582 int err = mzap_upgrade(zapp, tag, tx, 0);
8bea9815
MA
583 if (err != 0)
584 rw_exit(&zap->zap_rwlock);
585 return (err);
34dc7c2f 586 }
8bea9815 587 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
34dc7c2f
BB
588 zap->zap_m.zap_num_chunks =
589 db->db_size / MZAP_ENT_LEN - 1;
590 }
591
592 *zapp = zap;
593 return (0);
594}
595
2bce8049
MA
596static int
597zap_lockdir_by_dnode(dnode_t *dn, dmu_tx_t *tx,
598 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
599{
600 dmu_buf_t *db;
601 int err;
602
603 err = dmu_buf_hold_by_dnode(dn, 0, tag, &db, DMU_READ_NO_PREFETCH);
604 if (err != 0) {
605 return (err);
606 }
607 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
608 if (err != 0) {
609 dmu_buf_rele(db, tag);
610 }
611 return (err);
612}
613
8bea9815
MA
614int
615zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
616 krw_t lti, boolean_t fatreader, boolean_t adding, void *tag, zap_t **zapp)
617{
618 dmu_buf_t *db;
619 int err;
620
621 err = dmu_buf_hold(os, obj, 0, tag, &db, DMU_READ_NO_PREFETCH);
622 if (err != 0)
623 return (err);
624 err = zap_lockdir_impl(db, tag, tx, lti, fatreader, adding, zapp);
625 if (err != 0)
626 dmu_buf_rele(db, tag);
627 return (err);
628}
629
34dc7c2f 630void
8bea9815 631zap_unlockdir(zap_t *zap, void *tag)
34dc7c2f
BB
632{
633 rw_exit(&zap->zap_rwlock);
8bea9815 634 dmu_buf_rele(zap->zap_dbuf, tag);
34dc7c2f
BB
635}
636
637static int
8bea9815 638mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
34dc7c2f
BB
639{
640 mzap_phys_t *mzp;
428870ff
BB
641 int i, sz, nchunks;
642 int err = 0;
34dc7c2f
BB
643 zap_t *zap = *zapp;
644
645 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
646
647 sz = zap->zap_dbuf->db_size;
a3fd9d9e 648 mzp = vmem_alloc(sz, KM_SLEEP);
34dc7c2f
BB
649 bcopy(zap->zap_dbuf->db_data, mzp, sz);
650 nchunks = zap->zap_m.zap_num_chunks;
651
428870ff
BB
652 if (!flags) {
653 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
654 1ULL << fzap_default_block_shift, 0, tx);
655 if (err) {
a3fd9d9e 656 vmem_free(mzp, sz);
428870ff
BB
657 return (err);
658 }
34dc7c2f
BB
659 }
660
661 dprintf("upgrading obj=%llu with %u chunks\n",
662 zap->zap_object, nchunks);
663 /* XXX destroy the avl later, so we can use the stored hash value */
664 mze_destroy(zap);
665
428870ff 666 fzap_upgrade(zap, tx, flags);
34dc7c2f
BB
667
668 for (i = 0; i < nchunks; i++) {
34dc7c2f
BB
669 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
670 zap_name_t *zn;
671 if (mze->mze_name[0] == 0)
672 continue;
673 dprintf("adding %s=%llu\n",
674 mze->mze_name, mze->mze_value);
9b7b9cd3 675 zn = zap_name_alloc(zap, mze->mze_name, 0);
599b8648
CC
676 /* If we fail here, we would end up losing entries */
677 VERIFY0(fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
678 tag, tx));
34dc7c2f
BB
679 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
680 zap_name_free(zn);
34dc7c2f 681 }
a3fd9d9e 682 vmem_free(mzp, sz);
34dc7c2f 683 *zapp = zap;
599b8648 684 return (0);
34dc7c2f
BB
685}
686
9b7b9cd3
GM
687/*
688 * The "normflags" determine the behavior of the matchtype_t which is
689 * passed to zap_lookup_norm(). Names which have the same normalized
690 * version will be stored with the same hash value, and therefore we can
691 * perform normalization-insensitive lookups. We can be Unicode form-
692 * insensitive and/or case-insensitive. The following flags are valid for
693 * "normflags":
694 *
695 * U8_TEXTPREP_NFC
696 * U8_TEXTPREP_NFD
697 * U8_TEXTPREP_NFKC
698 * U8_TEXTPREP_NFKD
699 * U8_TEXTPREP_TOUPPER
700 *
701 * The *_NF* (Normalization Form) flags are mutually exclusive; at most one
702 * of them may be supplied.
703 */
fa86b5db 704void
428870ff
BB
705mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
706 dmu_tx_t *tx)
34dc7c2f
BB
707{
708 dmu_buf_t *db;
709 mzap_phys_t *zp;
710
9631681b 711 VERIFY0(dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
34dc7c2f
BB
712
713#ifdef ZFS_DEBUG
714 {
715 dmu_object_info_t doi;
716 dmu_object_info_from_db(db, &doi);
9ae529ec 717 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
34dc7c2f
BB
718 }
719#endif
720
721 dmu_buf_will_dirty(db, tx);
722 zp = db->db_data;
723 zp->mz_block_type = ZBT_MICRO;
724 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
725 zp->mz_normflags = normflags;
726 dmu_buf_rele(db, FTAG);
428870ff
BB
727
728 if (flags != 0) {
729 zap_t *zap;
730 /* Only fat zap supports flags; upgrade immediately. */
731 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
8bea9815
MA
732 B_FALSE, B_FALSE, FTAG, &zap));
733 VERIFY3U(0, ==, mzap_upgrade(&zap, FTAG, tx, flags));
734 zap_unlockdir(zap, FTAG);
428870ff 735 }
34dc7c2f
BB
736}
737
738int
739zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
740 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
741{
50c957f7
NB
742 return (zap_create_claim_dnsize(os, obj, ot, bonustype, bonuslen,
743 0, tx));
744}
745
746int
747zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot,
748 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
749{
750 return (zap_create_claim_norm_dnsize(os, obj,
751 0, ot, bonustype, bonuslen, dnodesize, tx));
34dc7c2f
BB
752}
753
754int
755zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
756 dmu_object_type_t ot,
757 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
50c957f7
NB
758{
759 return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype,
760 bonuslen, 0, tx));
761}
762
763int
764zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
765 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
766 int dnodesize, dmu_tx_t *tx)
34dc7c2f
BB
767{
768 int err;
769
50c957f7
NB
770 err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
771 dnodesize, tx);
34dc7c2f
BB
772 if (err != 0)
773 return (err);
428870ff 774 mzap_create_impl(os, obj, normflags, 0, tx);
34dc7c2f
BB
775 return (0);
776}
777
778uint64_t
779zap_create(objset_t *os, dmu_object_type_t ot,
780 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
781{
782 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
783}
784
50c957f7
NB
785uint64_t
786zap_create_dnsize(objset_t *os, dmu_object_type_t ot,
787 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
788{
789 return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen,
790 dnodesize, tx));
791}
792
34dc7c2f
BB
793uint64_t
794zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
795 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
796{
50c957f7
NB
797 return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen,
798 0, tx));
799}
800
801uint64_t
802zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
803 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
804{
805 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
806 dnodesize, tx);
34dc7c2f 807
428870ff
BB
808 mzap_create_impl(os, obj, normflags, 0, tx);
809 return (obj);
810}
811
812uint64_t
813zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
814 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
815 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
816{
50c957f7
NB
817 return (zap_create_flags_dnsize(os, normflags, flags, ot,
818 leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx));
819}
820
821uint64_t
822zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
823 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
824 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
825{
826 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
827 dnodesize, tx);
428870ff
BB
828
829 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
f1512ee6 830 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
428870ff 831 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
f1512ee6 832 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
428870ff
BB
833
834 VERIFY(dmu_object_set_blocksize(os, obj,
835 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
836
837 mzap_create_impl(os, obj, normflags, flags, tx);
34dc7c2f
BB
838 return (obj);
839}
840
841int
842zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
843{
844 /*
845 * dmu_object_free will free the object number and free the
846 * data. Freeing the data will cause our pageout function to be
847 * called, which will destroy our data (zap_leaf_t's and zap_t).
848 */
849
850 return (dmu_object_free(os, zapobj, tx));
851}
852
34dc7c2f 853void
39efbde7 854zap_evict_sync(void *dbu)
34dc7c2f 855{
0c66c32d 856 zap_t *zap = dbu;
34dc7c2f
BB
857
858 rw_destroy(&zap->zap_rwlock);
859
860 if (zap->zap_ismicro)
861 mze_destroy(zap);
862 else
863 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
864
865 kmem_free(zap, sizeof (zap_t));
866}
867
868int
869zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
870{
871 zap_t *zap;
872 int err;
873
8bea9815 874 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
34dc7c2f
BB
875 if (err)
876 return (err);
877 if (!zap->zap_ismicro) {
878 err = fzap_count(zap, count);
879 } else {
880 *count = zap->zap_m.zap_num_entries;
881 }
8bea9815 882 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
883 return (err);
884}
885
886/*
887 * zn may be NULL; if not specified, it will be computed if needed.
888 * See also the comment above zap_entry_normalization_conflict().
889 */
890static boolean_t
891mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
892{
893 mzap_ent_t *other;
894 int direction = AVL_BEFORE;
895 boolean_t allocdzn = B_FALSE;
896
897 if (zap->zap_normflags == 0)
898 return (B_FALSE);
899
900again:
901 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
902 other && other->mze_hash == mze->mze_hash;
903 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
904
905 if (zn == NULL) {
428870ff 906 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
9b7b9cd3 907 MT_NORMALIZE);
34dc7c2f
BB
908 allocdzn = B_TRUE;
909 }
428870ff 910 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
34dc7c2f
BB
911 if (allocdzn)
912 zap_name_free(zn);
913 return (B_TRUE);
914 }
915 }
916
917 if (direction == AVL_BEFORE) {
918 direction = AVL_AFTER;
919 goto again;
920 }
921
922 if (allocdzn)
923 zap_name_free(zn);
924 return (B_FALSE);
925}
926
927/*
928 * Routines for manipulating attributes.
929 */
930
931int
932zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
933 uint64_t integer_size, uint64_t num_integers, void *buf)
934{
935 return (zap_lookup_norm(os, zapobj, name, integer_size,
9b7b9cd3 936 num_integers, buf, 0, NULL, 0, NULL));
34dc7c2f
BB
937}
938
8bea9815
MA
939static int
940zap_lookup_impl(zap_t *zap, const char *name,
34dc7c2f
BB
941 uint64_t integer_size, uint64_t num_integers, void *buf,
942 matchtype_t mt, char *realname, int rn_len,
943 boolean_t *ncp)
944{
8bea9815 945 int err = 0;
34dc7c2f
BB
946 mzap_ent_t *mze;
947 zap_name_t *zn;
948
34dc7c2f 949 zn = zap_name_alloc(zap, name, mt);
8bea9815 950 if (zn == NULL)
2e528b49 951 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
952
953 if (!zap->zap_ismicro) {
954 err = fzap_lookup(zn, integer_size, num_integers, buf,
955 realname, rn_len, ncp);
956 } else {
957 mze = mze_find(zn);
958 if (mze == NULL) {
2e528b49 959 err = SET_ERROR(ENOENT);
34dc7c2f
BB
960 } else {
961 if (num_integers < 1) {
2e528b49 962 err = SET_ERROR(EOVERFLOW);
34dc7c2f 963 } else if (integer_size != 8) {
2e528b49 964 err = SET_ERROR(EINVAL);
34dc7c2f 965 } else {
428870ff
BB
966 *(uint64_t *)buf =
967 MZE_PHYS(zap, mze)->mze_value;
34dc7c2f 968 (void) strlcpy(realname,
428870ff 969 MZE_PHYS(zap, mze)->mze_name, rn_len);
34dc7c2f
BB
970 if (ncp) {
971 *ncp = mzap_normalization_conflict(zap,
972 zn, mze);
973 }
974 }
975 }
976 }
977 zap_name_free(zn);
8bea9815
MA
978 return (err);
979}
980
981int
982zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
983 uint64_t integer_size, uint64_t num_integers, void *buf,
984 matchtype_t mt, char *realname, int rn_len,
985 boolean_t *ncp)
986{
987 zap_t *zap;
988 int err;
989
990 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
991 if (err != 0)
992 return (err);
993 err = zap_lookup_impl(zap, name, integer_size,
994 num_integers, buf, mt, realname, rn_len, ncp);
995 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
996 return (err);
997}
998
07248450
BB
999int
1000zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
1001{
1002 zap_t *zap;
1003 int err;
1004 zap_name_t *zn;
1005
8bea9815 1006 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
07248450
BB
1007 if (err)
1008 return (err);
9b7b9cd3 1009 zn = zap_name_alloc(zap, name, 0);
07248450 1010 if (zn == NULL) {
8bea9815 1011 zap_unlockdir(zap, FTAG);
07248450
BB
1012 return (SET_ERROR(ENOTSUP));
1013 }
1014
1015 fzap_prefetch(zn);
1016 zap_name_free(zn);
8bea9815 1017 zap_unlockdir(zap, FTAG);
07248450
BB
1018 return (err);
1019}
1020
2bce8049
MA
1021int
1022zap_lookup_by_dnode(dnode_t *dn, const char *name,
1023 uint64_t integer_size, uint64_t num_integers, void *buf)
1024{
1025 return (zap_lookup_norm_by_dnode(dn, name, integer_size,
9b7b9cd3 1026 num_integers, buf, 0, NULL, 0, NULL));
2bce8049
MA
1027}
1028
1029int
1030zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
1031 uint64_t integer_size, uint64_t num_integers, void *buf,
1032 matchtype_t mt, char *realname, int rn_len,
1033 boolean_t *ncp)
1034{
1035 zap_t *zap;
1036 int err;
1037
1038 err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
1039 FTAG, &zap);
1040 if (err != 0)
1041 return (err);
1042 err = zap_lookup_impl(zap, name, integer_size,
1043 num_integers, buf, mt, realname, rn_len, ncp);
1044 zap_unlockdir(zap, FTAG);
1045 return (err);
1046}
1047
428870ff
BB
1048int
1049zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1050 int key_numints)
1051{
1052 zap_t *zap;
1053 int err;
1054 zap_name_t *zn;
1055
8bea9815 1056 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
428870ff
BB
1057 if (err)
1058 return (err);
1059 zn = zap_name_alloc_uint64(zap, key, key_numints);
1060 if (zn == NULL) {
8bea9815 1061 zap_unlockdir(zap, FTAG);
2e528b49 1062 return (SET_ERROR(ENOTSUP));
428870ff
BB
1063 }
1064
1065 fzap_prefetch(zn);
1066 zap_name_free(zn);
8bea9815 1067 zap_unlockdir(zap, FTAG);
428870ff
BB
1068 return (err);
1069}
1070
1071int
1072zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1073 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
1074{
1075 zap_t *zap;
1076 int err;
1077 zap_name_t *zn;
1078
8bea9815 1079 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
428870ff
BB
1080 if (err)
1081 return (err);
1082 zn = zap_name_alloc_uint64(zap, key, key_numints);
1083 if (zn == NULL) {
8bea9815 1084 zap_unlockdir(zap, FTAG);
2e528b49 1085 return (SET_ERROR(ENOTSUP));
428870ff
BB
1086 }
1087
1088 err = fzap_lookup(zn, integer_size, num_integers, buf,
1089 NULL, 0, NULL);
1090 zap_name_free(zn);
8bea9815 1091 zap_unlockdir(zap, FTAG);
428870ff
BB
1092 return (err);
1093}
1094
1095int
1096zap_contains(objset_t *os, uint64_t zapobj, const char *name)
1097{
fa86b5db 1098 int err = zap_lookup_norm(os, zapobj, name, 0,
9b7b9cd3 1099 0, NULL, 0, NULL, 0, NULL);
428870ff
BB
1100 if (err == EOVERFLOW || err == EINVAL)
1101 err = 0; /* found, but skipped reading the value */
1102 return (err);
1103}
1104
34dc7c2f
BB
1105int
1106zap_length(objset_t *os, uint64_t zapobj, const char *name,
1107 uint64_t *integer_size, uint64_t *num_integers)
1108{
1109 zap_t *zap;
1110 int err;
1111 mzap_ent_t *mze;
1112 zap_name_t *zn;
1113
8bea9815 1114 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
34dc7c2f
BB
1115 if (err)
1116 return (err);
9b7b9cd3 1117 zn = zap_name_alloc(zap, name, 0);
34dc7c2f 1118 if (zn == NULL) {
8bea9815 1119 zap_unlockdir(zap, FTAG);
2e528b49 1120 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1121 }
1122 if (!zap->zap_ismicro) {
1123 err = fzap_length(zn, integer_size, num_integers);
1124 } else {
1125 mze = mze_find(zn);
1126 if (mze == NULL) {
2e528b49 1127 err = SET_ERROR(ENOENT);
34dc7c2f
BB
1128 } else {
1129 if (integer_size)
1130 *integer_size = 8;
1131 if (num_integers)
1132 *num_integers = 1;
1133 }
1134 }
1135 zap_name_free(zn);
8bea9815 1136 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
1137 return (err);
1138}
1139
428870ff
BB
1140int
1141zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1142 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
1143{
1144 zap_t *zap;
1145 int err;
1146 zap_name_t *zn;
1147
8bea9815 1148 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
428870ff
BB
1149 if (err)
1150 return (err);
1151 zn = zap_name_alloc_uint64(zap, key, key_numints);
1152 if (zn == NULL) {
8bea9815 1153 zap_unlockdir(zap, FTAG);
2e528b49 1154 return (SET_ERROR(ENOTSUP));
428870ff
BB
1155 }
1156 err = fzap_length(zn, integer_size, num_integers);
1157 zap_name_free(zn);
8bea9815 1158 zap_unlockdir(zap, FTAG);
428870ff
BB
1159 return (err);
1160}
1161
34dc7c2f
BB
1162static void
1163mzap_addent(zap_name_t *zn, uint64_t value)
1164{
1165 int i;
1166 zap_t *zap = zn->zn_zap;
1167 int start = zap->zap_m.zap_alloc_next;
1168 uint32_t cd;
1169
34dc7c2f
BB
1170 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1171
1172#ifdef ZFS_DEBUG
1173 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
d1d7e268 1174 ASSERTV(mzap_ent_phys_t *mze);
d683ddbb 1175 ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
428870ff 1176 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
34dc7c2f
BB
1177 }
1178#endif
1179
1180 cd = mze_find_unused_cd(zap, zn->zn_hash);
1181 /* given the limited size of the microzap, this can't happen */
428870ff 1182 ASSERT(cd < zap_maxcd(zap));
34dc7c2f
BB
1183
1184again:
1185 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
d683ddbb 1186 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
34dc7c2f
BB
1187 if (mze->mze_name[0] == 0) {
1188 mze->mze_value = value;
1189 mze->mze_cd = cd;
680eada9 1190 (void) strlcpy(mze->mze_name, zn->zn_key_orig,
1191 sizeof (mze->mze_name));
34dc7c2f
BB
1192 zap->zap_m.zap_num_entries++;
1193 zap->zap_m.zap_alloc_next = i+1;
1194 if (zap->zap_m.zap_alloc_next ==
1195 zap->zap_m.zap_num_chunks)
1196 zap->zap_m.zap_alloc_next = 0;
428870ff 1197 mze_insert(zap, i, zn->zn_hash);
34dc7c2f
BB
1198 return;
1199 }
1200 }
1201 if (start != 0) {
1202 start = 0;
1203 goto again;
1204 }
989fd514 1205 cmn_err(CE_PANIC, "out of entries!");
34dc7c2f
BB
1206}
1207
0eef1bde 1208static int
1209zap_add_impl(zap_t *zap, const char *key,
34dc7c2f 1210 int integer_size, uint64_t num_integers,
0eef1bde 1211 const void *val, dmu_tx_t *tx, void *tag)
34dc7c2f 1212{
0eef1bde 1213 int err = 0;
34dc7c2f
BB
1214 mzap_ent_t *mze;
1215 const uint64_t *intval = val;
1216 zap_name_t *zn;
1217
9b7b9cd3 1218 zn = zap_name_alloc(zap, key, 0);
34dc7c2f 1219 if (zn == NULL) {
0eef1bde 1220 zap_unlockdir(zap, tag);
2e528b49 1221 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1222 }
1223 if (!zap->zap_ismicro) {
0eef1bde 1224 err = fzap_add(zn, integer_size, num_integers, val, tag, tx);
34dc7c2f
BB
1225 zap = zn->zn_zap; /* fzap_add() may change zap */
1226 } else if (integer_size != 8 || num_integers != 1 ||
599b8648
CC
1227 strlen(key) >= MZAP_NAME_LEN ||
1228 !mze_canfit_fzap_leaf(zn, zn->zn_hash)) {
0eef1bde 1229 err = mzap_upgrade(&zn->zn_zap, tag, tx, 0);
8bea9815
MA
1230 if (err == 0) {
1231 err = fzap_add(zn, integer_size, num_integers, val,
0eef1bde 1232 tag, tx);
8bea9815 1233 }
34dc7c2f
BB
1234 zap = zn->zn_zap; /* fzap_add() may change zap */
1235 } else {
1236 mze = mze_find(zn);
1237 if (mze != NULL) {
2e528b49 1238 err = SET_ERROR(EEXIST);
34dc7c2f
BB
1239 } else {
1240 mzap_addent(zn, *intval);
1241 }
1242 }
1243 ASSERT(zap == zn->zn_zap);
1244 zap_name_free(zn);
66eead53
MA
1245 if (zap != NULL) /* may be NULL if fzap_add() failed */
1246 zap_unlockdir(zap, tag);
0eef1bde 1247 return (err);
1248}
1249
1250int
1251zap_add(objset_t *os, uint64_t zapobj, const char *key,
1252 int integer_size, uint64_t num_integers,
1253 const void *val, dmu_tx_t *tx)
1254{
1255 zap_t *zap;
1256 int err;
1257
1258 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1259 if (err != 0)
1260 return (err);
1261 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1262 /* zap_add_impl() calls zap_unlockdir() */
1263 return (err);
1264}
1265
1266int
1267zap_add_by_dnode(dnode_t *dn, const char *key,
1268 int integer_size, uint64_t num_integers,
1269 const void *val, dmu_tx_t *tx)
1270{
1271 zap_t *zap;
1272 int err;
1273
1274 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1275 if (err != 0)
1276 return (err);
1277 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1278 /* zap_add_impl() calls zap_unlockdir() */
34dc7c2f
BB
1279 return (err);
1280}
1281
428870ff
BB
1282int
1283zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1284 int key_numints, int integer_size, uint64_t num_integers,
1285 const void *val, dmu_tx_t *tx)
1286{
1287 zap_t *zap;
1288 int err;
1289 zap_name_t *zn;
1290
8bea9815 1291 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
428870ff
BB
1292 if (err)
1293 return (err);
1294 zn = zap_name_alloc_uint64(zap, key, key_numints);
1295 if (zn == NULL) {
8bea9815 1296 zap_unlockdir(zap, FTAG);
2e528b49 1297 return (SET_ERROR(ENOTSUP));
428870ff 1298 }
8bea9815 1299 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
428870ff
BB
1300 zap = zn->zn_zap; /* fzap_add() may change zap */
1301 zap_name_free(zn);
1302 if (zap != NULL) /* may be NULL if fzap_add() failed */
8bea9815 1303 zap_unlockdir(zap, FTAG);
428870ff
BB
1304 return (err);
1305}
1306
34dc7c2f
BB
1307int
1308zap_update(objset_t *os, uint64_t zapobj, const char *name,
1309 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1310{
1311 zap_t *zap;
1312 mzap_ent_t *mze;
1313 const uint64_t *intval = val;
1314 zap_name_t *zn;
1315 int err;
1316
428870ff 1317#ifdef ZFS_DEBUG
1fde1e37
BB
1318 uint64_t oldval;
1319
428870ff
BB
1320 /*
1321 * If there is an old value, it shouldn't change across the
1322 * lockdir (eg, due to bprewrite's xlation).
1323 */
1324 if (integer_size == 8 && num_integers == 1)
1325 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1326#endif
1327
8bea9815 1328 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
34dc7c2f
BB
1329 if (err)
1330 return (err);
9b7b9cd3 1331 zn = zap_name_alloc(zap, name, 0);
34dc7c2f 1332 if (zn == NULL) {
8bea9815 1333 zap_unlockdir(zap, FTAG);
2e528b49 1334 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1335 }
1336 if (!zap->zap_ismicro) {
8bea9815
MA
1337 err = fzap_update(zn, integer_size, num_integers, val,
1338 FTAG, tx);
34dc7c2f
BB
1339 zap = zn->zn_zap; /* fzap_update() may change zap */
1340 } else if (integer_size != 8 || num_integers != 1 ||
1341 strlen(name) >= MZAP_NAME_LEN) {
1342 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1343 zapobj, integer_size, num_integers, name);
8bea9815
MA
1344 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1345 if (err == 0) {
34dc7c2f 1346 err = fzap_update(zn, integer_size, num_integers,
8bea9815
MA
1347 val, FTAG, tx);
1348 }
34dc7c2f
BB
1349 zap = zn->zn_zap; /* fzap_update() may change zap */
1350 } else {
1351 mze = mze_find(zn);
1352 if (mze != NULL) {
428870ff
BB
1353 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1354 MZE_PHYS(zap, mze)->mze_value = *intval;
34dc7c2f
BB
1355 } else {
1356 mzap_addent(zn, *intval);
1357 }
1358 }
1359 ASSERT(zap == zn->zn_zap);
1360 zap_name_free(zn);
1361 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
8bea9815 1362 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
1363 return (err);
1364}
1365
428870ff
BB
1366int
1367zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1368 int key_numints,
1369 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1370{
1371 zap_t *zap;
1372 zap_name_t *zn;
1373 int err;
1374
8bea9815 1375 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
428870ff
BB
1376 if (err)
1377 return (err);
1378 zn = zap_name_alloc_uint64(zap, key, key_numints);
1379 if (zn == NULL) {
8bea9815 1380 zap_unlockdir(zap, FTAG);
2e528b49 1381 return (SET_ERROR(ENOTSUP));
428870ff 1382 }
8bea9815 1383 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
428870ff
BB
1384 zap = zn->zn_zap; /* fzap_update() may change zap */
1385 zap_name_free(zn);
1386 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
8bea9815 1387 zap_unlockdir(zap, FTAG);
428870ff
BB
1388 return (err);
1389}
1390
34dc7c2f
BB
1391int
1392zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1393{
9b7b9cd3 1394 return (zap_remove_norm(os, zapobj, name, 0, tx));
34dc7c2f
BB
1395}
1396
0eef1bde 1397static int
1398zap_remove_impl(zap_t *zap, const char *name,
34dc7c2f
BB
1399 matchtype_t mt, dmu_tx_t *tx)
1400{
34dc7c2f
BB
1401 mzap_ent_t *mze;
1402 zap_name_t *zn;
0eef1bde 1403 int err = 0;
34dc7c2f 1404
34dc7c2f 1405 zn = zap_name_alloc(zap, name, mt);
0eef1bde 1406 if (zn == NULL)
2e528b49 1407 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1408 if (!zap->zap_ismicro) {
1409 err = fzap_remove(zn, tx);
1410 } else {
1411 mze = mze_find(zn);
1412 if (mze == NULL) {
2e528b49 1413 err = SET_ERROR(ENOENT);
34dc7c2f
BB
1414 } else {
1415 zap->zap_m.zap_num_entries--;
d683ddbb 1416 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
34dc7c2f
BB
1417 sizeof (mzap_ent_phys_t));
1418 mze_remove(zap, mze);
1419 }
1420 }
1421 zap_name_free(zn);
0eef1bde 1422 return (err);
1423}
1424
1425int
1426zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1427 matchtype_t mt, dmu_tx_t *tx)
1428{
1429 zap_t *zap;
1430 int err;
1431
1432 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1433 if (err)
1434 return (err);
1435 err = zap_remove_impl(zap, name, mt, tx);
1436 zap_unlockdir(zap, FTAG);
1437 return (err);
1438}
1439
1440int
1441zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx)
1442{
1443 zap_t *zap;
1444 int err;
1445
1446 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1447 if (err)
1448 return (err);
9b7b9cd3 1449 err = zap_remove_impl(zap, name, 0, tx);
8bea9815 1450 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
1451 return (err);
1452}
1453
428870ff
BB
1454int
1455zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1456 int key_numints, dmu_tx_t *tx)
1457{
1458 zap_t *zap;
1459 int err;
1460 zap_name_t *zn;
1461
8bea9815 1462 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
428870ff
BB
1463 if (err)
1464 return (err);
1465 zn = zap_name_alloc_uint64(zap, key, key_numints);
1466 if (zn == NULL) {
8bea9815 1467 zap_unlockdir(zap, FTAG);
2e528b49 1468 return (SET_ERROR(ENOTSUP));
428870ff
BB
1469 }
1470 err = fzap_remove(zn, tx);
1471 zap_name_free(zn);
8bea9815 1472 zap_unlockdir(zap, FTAG);
428870ff
BB
1473 return (err);
1474}
1475
34dc7c2f
BB
1476/*
1477 * Routines for iterating over the attributes.
1478 */
1479
34dc7c2f
BB
1480void
1481zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1482 uint64_t serialized)
1483{
1484 zc->zc_objset = os;
1485 zc->zc_zap = NULL;
1486 zc->zc_leaf = NULL;
1487 zc->zc_zapobj = zapobj;
428870ff
BB
1488 zc->zc_serialized = serialized;
1489 zc->zc_hash = 0;
1490 zc->zc_cd = 0;
34dc7c2f
BB
1491}
1492
1493void
1494zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1495{
1496 zap_cursor_init_serialized(zc, os, zapobj, 0);
1497}
1498
1499void
1500zap_cursor_fini(zap_cursor_t *zc)
1501{
1502 if (zc->zc_zap) {
1503 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
8bea9815 1504 zap_unlockdir(zc->zc_zap, NULL);
34dc7c2f
BB
1505 zc->zc_zap = NULL;
1506 }
1507 if (zc->zc_leaf) {
1508 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1509 zap_put_leaf(zc->zc_leaf);
1510 zc->zc_leaf = NULL;
1511 }
1512 zc->zc_objset = NULL;
1513}
1514
1515uint64_t
1516zap_cursor_serialize(zap_cursor_t *zc)
1517{
1518 if (zc->zc_hash == -1ULL)
1519 return (-1ULL);
428870ff
BB
1520 if (zc->zc_zap == NULL)
1521 return (zc->zc_serialized);
1522 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1523 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1524
1525 /*
1526 * We want to keep the high 32 bits of the cursor zero if we can, so
1527 * that 32-bit programs can access this. So usually use a small
1528 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1529 * of the cursor.
1530 *
1531 * [ collision differentiator | zap_hashbits()-bit hash value ]
1532 */
1533 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1534 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
34dc7c2f
BB
1535}
1536
1537int
1538zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1539{
1540 int err;
1541 avl_index_t idx;
1542 mzap_ent_t mze_tofind;
1543 mzap_ent_t *mze;
1544
1545 if (zc->zc_hash == -1ULL)
2e528b49 1546 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1547
1548 if (zc->zc_zap == NULL) {
428870ff 1549 int hb;
34dc7c2f 1550 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
8bea9815 1551 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
34dc7c2f
BB
1552 if (err)
1553 return (err);
428870ff
BB
1554
1555 /*
1556 * To support zap_cursor_init_serialized, advance, retrieve,
1557 * we must add to the existing zc_cd, which may already
1558 * be 1 due to the zap_cursor_advance.
1559 */
1560 ASSERT(zc->zc_hash == 0);
1561 hb = zap_hashbits(zc->zc_zap);
1562 zc->zc_hash = zc->zc_serialized << (64 - hb);
1563 zc->zc_cd += zc->zc_serialized >> hb;
1564 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1565 zc->zc_cd = 0;
34dc7c2f
BB
1566 } else {
1567 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1568 }
1569 if (!zc->zc_zap->zap_ismicro) {
1570 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1571 } else {
34dc7c2f 1572 mze_tofind.mze_hash = zc->zc_hash;
428870ff 1573 mze_tofind.mze_cd = zc->zc_cd;
34dc7c2f
BB
1574
1575 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1576 if (mze == NULL) {
1577 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1578 idx, AVL_AFTER);
1579 }
1580 if (mze) {
428870ff
BB
1581 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1582 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
34dc7c2f
BB
1583 za->za_normalization_conflict =
1584 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1585 za->za_integer_length = 8;
1586 za->za_num_integers = 1;
428870ff
BB
1587 za->za_first_integer = mzep->mze_value;
1588 (void) strcpy(za->za_name, mzep->mze_name);
34dc7c2f 1589 zc->zc_hash = mze->mze_hash;
428870ff 1590 zc->zc_cd = mze->mze_cd;
34dc7c2f
BB
1591 err = 0;
1592 } else {
1593 zc->zc_hash = -1ULL;
2e528b49 1594 err = SET_ERROR(ENOENT);
34dc7c2f
BB
1595 }
1596 }
1597 rw_exit(&zc->zc_zap->zap_rwlock);
1598 return (err);
1599}
1600
1601void
1602zap_cursor_advance(zap_cursor_t *zc)
1603{
1604 if (zc->zc_hash == -1ULL)
1605 return;
1606 zc->zc_cd++;
428870ff
BB
1607}
1608
34dc7c2f
BB
1609int
1610zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1611{
1612 int err;
1613 zap_t *zap;
1614
8bea9815 1615 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
34dc7c2f
BB
1616 if (err)
1617 return (err);
1618
1619 bzero(zs, sizeof (zap_stats_t));
1620
1621 if (zap->zap_ismicro) {
1622 zs->zs_blocksize = zap->zap_dbuf->db_size;
1623 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1624 zs->zs_num_blocks = 1;
1625 } else {
1626 fzap_get_stats(zap, zs);
1627 }
8bea9815 1628 zap_unlockdir(zap, FTAG);
34dc7c2f
BB
1629 return (0);
1630}
9babb374 1631
c28b2279 1632#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 1633EXPORT_SYMBOL(zap_create);
50c957f7 1634EXPORT_SYMBOL(zap_create_dnsize);
dee28b07 1635EXPORT_SYMBOL(zap_create_norm);
50c957f7 1636EXPORT_SYMBOL(zap_create_norm_dnsize);
dee28b07 1637EXPORT_SYMBOL(zap_create_flags);
50c957f7 1638EXPORT_SYMBOL(zap_create_flags_dnsize);
dee28b07
BB
1639EXPORT_SYMBOL(zap_create_claim);
1640EXPORT_SYMBOL(zap_create_claim_norm);
50c957f7 1641EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
dee28b07 1642EXPORT_SYMBOL(zap_destroy);
c28b2279 1643EXPORT_SYMBOL(zap_lookup);
0eef1bde 1644EXPORT_SYMBOL(zap_lookup_by_dnode);
c28b2279 1645EXPORT_SYMBOL(zap_lookup_norm);
dee28b07
BB
1646EXPORT_SYMBOL(zap_lookup_uint64);
1647EXPORT_SYMBOL(zap_contains);
07248450 1648EXPORT_SYMBOL(zap_prefetch);
dee28b07 1649EXPORT_SYMBOL(zap_prefetch_uint64);
dee28b07 1650EXPORT_SYMBOL(zap_add);
0eef1bde 1651EXPORT_SYMBOL(zap_add_by_dnode);
dee28b07 1652EXPORT_SYMBOL(zap_add_uint64);
c28b2279 1653EXPORT_SYMBOL(zap_update);
dee28b07
BB
1654EXPORT_SYMBOL(zap_update_uint64);
1655EXPORT_SYMBOL(zap_length);
1656EXPORT_SYMBOL(zap_length_uint64);
1657EXPORT_SYMBOL(zap_remove);
0eef1bde 1658EXPORT_SYMBOL(zap_remove_by_dnode);
dee28b07
BB
1659EXPORT_SYMBOL(zap_remove_norm);
1660EXPORT_SYMBOL(zap_remove_uint64);
1661EXPORT_SYMBOL(zap_count);
1662EXPORT_SYMBOL(zap_value_search);
1663EXPORT_SYMBOL(zap_join);
1664EXPORT_SYMBOL(zap_join_increment);
1665EXPORT_SYMBOL(zap_add_int);
1666EXPORT_SYMBOL(zap_remove_int);
1667EXPORT_SYMBOL(zap_lookup_int);
1668EXPORT_SYMBOL(zap_increment_int);
1669EXPORT_SYMBOL(zap_add_int_key);
1670EXPORT_SYMBOL(zap_lookup_int_key);
1671EXPORT_SYMBOL(zap_increment);
1672EXPORT_SYMBOL(zap_cursor_init);
1673EXPORT_SYMBOL(zap_cursor_fini);
1674EXPORT_SYMBOL(zap_cursor_retrieve);
1675EXPORT_SYMBOL(zap_cursor_advance);
1676EXPORT_SYMBOL(zap_cursor_serialize);
dee28b07
BB
1677EXPORT_SYMBOL(zap_cursor_init_serialized);
1678EXPORT_SYMBOL(zap_get_stats);
c28b2279 1679#endif