]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zap_micro.c
Handle zap_add() failures in mixed case mode
[mirror_zfs.git] / module / zfs / zap_micro.c
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 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
27 */
28
29 #include <sys/zio.h>
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>
38 #include <sys/arc.h>
39 #include <sys/dmu_objset.h>
40
41 #ifdef _KERNEL
42 #include <sys/sunddi.h>
43 #endif
44
45 extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
46
47 static int mzap_upgrade(zap_t **zapp,
48 void *tag, dmu_tx_t *tx, zap_flags_t flags);
49
50 uint64_t
51 zap_getflags(zap_t *zap)
52 {
53 if (zap->zap_ismicro)
54 return (0);
55 return (zap_f_phys(zap)->zap_flags);
56 }
57
58 int
59 zap_hashbits(zap_t *zap)
60 {
61 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
62 return (48);
63 else
64 return (28);
65 }
66
67 uint32_t
68 zap_maxcd(zap_t *zap)
69 {
70 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
71 return ((1<<16)-1);
72 else
73 return (-1U);
74 }
75
76 static uint64_t
77 zap_hash(zap_name_t *zn)
78 {
79 zap_t *zap = zn->zn_zap;
80 uint64_t h = 0;
81
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 }
125 /*
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
129 * choosing the bucket.
130 */
131 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
132
133 return (h);
134 }
135
136 static int
137 zap_normalize(zap_t *zap, const char *name, char *namenorm, int normflags)
138 {
139 size_t inlen, outlen;
140 int err;
141
142 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
143
144 inlen = strlen(name) + 1;
145 outlen = ZAP_MAXNAMELEN;
146
147 err = 0;
148 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
149 normflags | U8_TEXTPREP_IGNORE_NULL | U8_TEXTPREP_IGNORE_INVALID,
150 U8_UNICODE_LATEST, &err);
151
152 return (err);
153 }
154
155 boolean_t
156 zap_match(zap_name_t *zn, const char *matchname)
157 {
158 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
159
160 if (zn->zn_matchtype & MT_NORMALIZE) {
161 char norm[ZAP_MAXNAMELEN];
162
163 if (zap_normalize(zn->zn_zap, matchname, norm,
164 zn->zn_normflags) != 0)
165 return (B_FALSE);
166
167 return (strcmp(zn->zn_key_norm, norm) == 0);
168 } else {
169 return (strcmp(zn->zn_key_orig, matchname) == 0);
170 }
171 }
172
173 void
174 zap_name_free(zap_name_t *zn)
175 {
176 kmem_free(zn, sizeof (zap_name_t));
177 }
178
179 zap_name_t *
180 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
181 {
182 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
183
184 zn->zn_zap = zap;
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;
188 zn->zn_matchtype = mt;
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
199 if (zap->zap_normflags) {
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) {
206 zap_name_free(zn);
207 return (NULL);
208 }
209 zn->zn_key_norm = zn->zn_normbuf;
210 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
211 } else {
212 if (mt != 0) {
213 zap_name_free(zn);
214 return (NULL);
215 }
216 zn->zn_key_norm = zn->zn_key_orig;
217 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
218 }
219
220 zn->zn_hash = zap_hash(zn);
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
235 return (zn);
236 }
237
238 zap_name_t *
239 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
240 {
241 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
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;
248 zn->zn_matchtype = 0;
249
250 zn->zn_hash = zap_hash(zn);
251 return (zn);
252 }
253
254 static void
255 mzap_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
270 void
271 zap_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
285 static int
286 mze_compare(const void *arg1, const void *arg2)
287 {
288 const mzap_ent_t *mze1 = arg1;
289 const mzap_ent_t *mze2 = arg2;
290
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));
296 }
297
298 static void
299 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
300 {
301 mzap_ent_t *mze;
302
303 ASSERT(zap->zap_ismicro);
304 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
305
306 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
307 mze->mze_chunkid = chunkid;
308 mze->mze_hash = hash;
309 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
310 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
311 avl_add(&zap->zap_m.zap_avl, mze);
312 }
313
314 static mzap_ent_t *
315 mze_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
325 mze_tofind.mze_hash = zn->zn_hash;
326 mze_tofind.mze_cd = 0;
327
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)) {
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))
334 return (mze);
335 }
336
337 return (NULL);
338 }
339
340 static uint32_t
341 mze_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;
353 mze_tofind.mze_cd = 0;
354
355 cd = 0;
356 for (mze = avl_find(avl, &mze_tofind, &idx);
357 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
358 if (mze->mze_cd != cd)
359 break;
360 cd++;
361 }
362
363 return (cd);
364 }
365
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 */
377 static boolean_t
378 mze_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
401 static void
402 mze_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
411 static void
412 mze_destroy(zap_t *zap)
413 {
414 mzap_ent_t *mze;
415 void *avlcookie = NULL;
416
417 while ((mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie)))
418 kmem_free(mze, sizeof (mzap_ent_t));
419 avl_destroy(&zap->zap_m.zap_avl);
420 }
421
422 static zap_t *
423 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
424 {
425 zap_t *winner;
426 zap_t *zap;
427 int i;
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];
431
432 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
433
434 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
435 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
436 rw_enter(&zap->zap_rwlock, RW_WRITER);
437 zap->zap_objset = os;
438 zap->zap_object = obj;
439 zap->zap_dbuf = db;
440
441 if (zap_block_type != ZBT_MICRO) {
442 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT,
443 0);
444 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
445 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
446 winner = NULL; /* No actual winner here... */
447 goto handle_winner;
448 }
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 */
458 dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf);
459 winner = dmu_buf_set_user(db, &zap->zap_dbu);
460
461 if (winner != NULL)
462 goto handle_winner;
463
464 if (zap->zap_ismicro) {
465 zap->zap_salt = zap_m_phys(zap)->mz_salt;
466 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
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 =
473 &zap_m_phys(zap)->mz_chunk[i];
474 if (mze->mze_name[0]) {
475 zap_name_t *zn;
476
477 zap->zap_m.zap_num_entries++;
478 zn = zap_name_alloc(zap, mze->mze_name, 0);
479 mze_insert(zap, i, zn->zn_hash);
480 zap_name_free(zn);
481 }
482 }
483 } else {
484 zap->zap_salt = zap_f_phys(zap)->zap_salt;
485 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
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), >,
495 &zap_f_phys(zap)->zap_salt);
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)) -
503 (uintptr_t)zap_f_phys(zap), ==,
504 zap->zap_dbuf->db_size);
505 }
506 rw_exit(&zap->zap_rwlock);
507 return (zap);
508
509 handle_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);
516 }
517
518 static int
519 zap_lockdir_impl(dmu_buf_t *db, void *tag, dmu_tx_t *tx,
520 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
521 {
522 dmu_object_info_t doi;
523 zap_t *zap;
524 krw_t lt;
525
526 objset_t *os = dmu_buf_get_objset(db);
527 uint64_t obj = db->db_object;
528
529 ASSERT0(db->db_offset);
530 *zapp = NULL;
531
532 dmu_object_info_from_db(db, &doi);
533 if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
534 return (SET_ERROR(EINVAL));
535
536 zap = dmu_buf_get_user(db);
537 if (zap == NULL) {
538 zap = mzap_open(os, obj, db);
539 if (zap == NULL) {
540 /*
541 * mzap_open() didn't like what it saw on-disk.
542 * Check for corruption!
543 */
544 return (SET_ERROR(EIO));
545 }
546 }
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 ==
561 ((!zap->zap_ismicro && fatreader) ? RW_READER : lti));
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;
582 int err = mzap_upgrade(zapp, tag, tx, 0);
583 if (err != 0)
584 rw_exit(&zap->zap_rwlock);
585 return (err);
586 }
587 VERIFY0(dmu_object_set_blocksize(os, obj, newsz, 0, tx));
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
596 static int
597 zap_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
614 int
615 zap_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
630 void
631 zap_unlockdir(zap_t *zap, void *tag)
632 {
633 rw_exit(&zap->zap_rwlock);
634 dmu_buf_rele(zap->zap_dbuf, tag);
635 }
636
637 static int
638 mzap_upgrade(zap_t **zapp, void *tag, dmu_tx_t *tx, zap_flags_t flags)
639 {
640 mzap_phys_t *mzp;
641 int i, sz, nchunks;
642 int err = 0;
643 zap_t *zap = *zapp;
644
645 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
646
647 sz = zap->zap_dbuf->db_size;
648 mzp = vmem_alloc(sz, KM_SLEEP);
649 bcopy(zap->zap_dbuf->db_data, mzp, sz);
650 nchunks = zap->zap_m.zap_num_chunks;
651
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) {
656 vmem_free(mzp, sz);
657 return (err);
658 }
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
666 fzap_upgrade(zap, tx, flags);
667
668 for (i = 0; i < nchunks; i++) {
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);
675 zn = zap_name_alloc(zap, mze->mze_name, 0);
676 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
677 tag, tx);
678 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
679 zap_name_free(zn);
680 if (err)
681 break;
682 }
683 vmem_free(mzp, sz);
684 *zapp = zap;
685 return (err);
686 }
687
688 /*
689 * The "normflags" determine the behavior of the matchtype_t which is
690 * passed to zap_lookup_norm(). Names which have the same normalized
691 * version will be stored with the same hash value, and therefore we can
692 * perform normalization-insensitive lookups. We can be Unicode form-
693 * insensitive and/or case-insensitive. The following flags are valid for
694 * "normflags":
695 *
696 * U8_TEXTPREP_NFC
697 * U8_TEXTPREP_NFD
698 * U8_TEXTPREP_NFKC
699 * U8_TEXTPREP_NFKD
700 * U8_TEXTPREP_TOUPPER
701 *
702 * The *_NF* (Normalization Form) flags are mutually exclusive; at most one
703 * of them may be supplied.
704 */
705 void
706 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
707 dmu_tx_t *tx)
708 {
709 dmu_buf_t *db;
710 mzap_phys_t *zp;
711
712 VERIFY0(dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
713
714 #ifdef ZFS_DEBUG
715 {
716 dmu_object_info_t doi;
717 dmu_object_info_from_db(db, &doi);
718 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
719 }
720 #endif
721
722 dmu_buf_will_dirty(db, tx);
723 zp = db->db_data;
724 zp->mz_block_type = ZBT_MICRO;
725 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
726 zp->mz_normflags = normflags;
727 dmu_buf_rele(db, FTAG);
728
729 if (flags != 0) {
730 zap_t *zap;
731 /* Only fat zap supports flags; upgrade immediately. */
732 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
733 B_FALSE, B_FALSE, FTAG, &zap));
734 VERIFY3U(0, ==, mzap_upgrade(&zap, FTAG, tx, flags));
735 zap_unlockdir(zap, FTAG);
736 }
737 }
738
739 int
740 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
741 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
742 {
743 return (zap_create_claim_dnsize(os, obj, ot, bonustype, bonuslen,
744 0, tx));
745 }
746
747 int
748 zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot,
749 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
750 {
751 return (zap_create_claim_norm_dnsize(os, obj,
752 0, ot, bonustype, bonuslen, dnodesize, tx));
753 }
754
755 int
756 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
757 dmu_object_type_t ot,
758 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
759 {
760 return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype,
761 bonuslen, 0, tx));
762 }
763
764 int
765 zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
766 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
767 int dnodesize, dmu_tx_t *tx)
768 {
769 int err;
770
771 err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
772 dnodesize, tx);
773 if (err != 0)
774 return (err);
775 mzap_create_impl(os, obj, normflags, 0, tx);
776 return (0);
777 }
778
779 uint64_t
780 zap_create(objset_t *os, dmu_object_type_t ot,
781 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
782 {
783 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
784 }
785
786 uint64_t
787 zap_create_dnsize(objset_t *os, dmu_object_type_t ot,
788 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
789 {
790 return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen,
791 dnodesize, tx));
792 }
793
794 uint64_t
795 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
796 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
797 {
798 return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen,
799 0, tx));
800 }
801
802 uint64_t
803 zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
804 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
805 {
806 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
807 dnodesize, tx);
808
809 mzap_create_impl(os, obj, normflags, 0, tx);
810 return (obj);
811 }
812
813 uint64_t
814 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
815 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
816 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
817 {
818 return (zap_create_flags_dnsize(os, normflags, flags, ot,
819 leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx));
820 }
821
822 uint64_t
823 zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
824 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
825 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
826 {
827 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
828 dnodesize, tx);
829
830 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
831 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
832 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
833 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
834
835 VERIFY(dmu_object_set_blocksize(os, obj,
836 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
837
838 mzap_create_impl(os, obj, normflags, flags, tx);
839 return (obj);
840 }
841
842 int
843 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
844 {
845 /*
846 * dmu_object_free will free the object number and free the
847 * data. Freeing the data will cause our pageout function to be
848 * called, which will destroy our data (zap_leaf_t's and zap_t).
849 */
850
851 return (dmu_object_free(os, zapobj, tx));
852 }
853
854 void
855 zap_evict_sync(void *dbu)
856 {
857 zap_t *zap = dbu;
858
859 rw_destroy(&zap->zap_rwlock);
860
861 if (zap->zap_ismicro)
862 mze_destroy(zap);
863 else
864 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
865
866 kmem_free(zap, sizeof (zap_t));
867 }
868
869 int
870 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
871 {
872 zap_t *zap;
873 int err;
874
875 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
876 if (err)
877 return (err);
878 if (!zap->zap_ismicro) {
879 err = fzap_count(zap, count);
880 } else {
881 *count = zap->zap_m.zap_num_entries;
882 }
883 zap_unlockdir(zap, FTAG);
884 return (err);
885 }
886
887 /*
888 * zn may be NULL; if not specified, it will be computed if needed.
889 * See also the comment above zap_entry_normalization_conflict().
890 */
891 static boolean_t
892 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
893 {
894 mzap_ent_t *other;
895 int direction = AVL_BEFORE;
896 boolean_t allocdzn = B_FALSE;
897
898 if (zap->zap_normflags == 0)
899 return (B_FALSE);
900
901 again:
902 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
903 other && other->mze_hash == mze->mze_hash;
904 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
905
906 if (zn == NULL) {
907 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
908 MT_NORMALIZE);
909 allocdzn = B_TRUE;
910 }
911 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
912 if (allocdzn)
913 zap_name_free(zn);
914 return (B_TRUE);
915 }
916 }
917
918 if (direction == AVL_BEFORE) {
919 direction = AVL_AFTER;
920 goto again;
921 }
922
923 if (allocdzn)
924 zap_name_free(zn);
925 return (B_FALSE);
926 }
927
928 /*
929 * Routines for manipulating attributes.
930 */
931
932 int
933 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
934 uint64_t integer_size, uint64_t num_integers, void *buf)
935 {
936 return (zap_lookup_norm(os, zapobj, name, integer_size,
937 num_integers, buf, 0, NULL, 0, NULL));
938 }
939
940 static int
941 zap_lookup_impl(zap_t *zap, const char *name,
942 uint64_t integer_size, uint64_t num_integers, void *buf,
943 matchtype_t mt, char *realname, int rn_len,
944 boolean_t *ncp)
945 {
946 int err = 0;
947 mzap_ent_t *mze;
948 zap_name_t *zn;
949
950 zn = zap_name_alloc(zap, name, mt);
951 if (zn == NULL)
952 return (SET_ERROR(ENOTSUP));
953
954 if (!zap->zap_ismicro) {
955 err = fzap_lookup(zn, integer_size, num_integers, buf,
956 realname, rn_len, ncp);
957 } else {
958 mze = mze_find(zn);
959 if (mze == NULL) {
960 err = SET_ERROR(ENOENT);
961 } else {
962 if (num_integers < 1) {
963 err = SET_ERROR(EOVERFLOW);
964 } else if (integer_size != 8) {
965 err = SET_ERROR(EINVAL);
966 } else {
967 *(uint64_t *)buf =
968 MZE_PHYS(zap, mze)->mze_value;
969 (void) strlcpy(realname,
970 MZE_PHYS(zap, mze)->mze_name, rn_len);
971 if (ncp) {
972 *ncp = mzap_normalization_conflict(zap,
973 zn, mze);
974 }
975 }
976 }
977 }
978 zap_name_free(zn);
979 return (err);
980 }
981
982 int
983 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
984 uint64_t integer_size, uint64_t num_integers, void *buf,
985 matchtype_t mt, char *realname, int rn_len,
986 boolean_t *ncp)
987 {
988 zap_t *zap;
989 int err;
990
991 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
992 if (err != 0)
993 return (err);
994 err = zap_lookup_impl(zap, name, integer_size,
995 num_integers, buf, mt, realname, rn_len, ncp);
996 zap_unlockdir(zap, FTAG);
997 return (err);
998 }
999
1000 int
1001 zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
1002 {
1003 zap_t *zap;
1004 int err;
1005 zap_name_t *zn;
1006
1007 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1008 if (err)
1009 return (err);
1010 zn = zap_name_alloc(zap, name, 0);
1011 if (zn == NULL) {
1012 zap_unlockdir(zap, FTAG);
1013 return (SET_ERROR(ENOTSUP));
1014 }
1015
1016 fzap_prefetch(zn);
1017 zap_name_free(zn);
1018 zap_unlockdir(zap, FTAG);
1019 return (err);
1020 }
1021
1022 int
1023 zap_lookup_by_dnode(dnode_t *dn, const char *name,
1024 uint64_t integer_size, uint64_t num_integers, void *buf)
1025 {
1026 return (zap_lookup_norm_by_dnode(dn, name, integer_size,
1027 num_integers, buf, 0, NULL, 0, NULL));
1028 }
1029
1030 int
1031 zap_lookup_norm_by_dnode(dnode_t *dn, const char *name,
1032 uint64_t integer_size, uint64_t num_integers, void *buf,
1033 matchtype_t mt, char *realname, int rn_len,
1034 boolean_t *ncp)
1035 {
1036 zap_t *zap;
1037 int err;
1038
1039 err = zap_lockdir_by_dnode(dn, NULL, RW_READER, TRUE, FALSE,
1040 FTAG, &zap);
1041 if (err != 0)
1042 return (err);
1043 err = zap_lookup_impl(zap, name, integer_size,
1044 num_integers, buf, mt, realname, rn_len, ncp);
1045 zap_unlockdir(zap, FTAG);
1046 return (err);
1047 }
1048
1049 int
1050 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1051 int key_numints)
1052 {
1053 zap_t *zap;
1054 int err;
1055 zap_name_t *zn;
1056
1057 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1058 if (err)
1059 return (err);
1060 zn = zap_name_alloc_uint64(zap, key, key_numints);
1061 if (zn == NULL) {
1062 zap_unlockdir(zap, FTAG);
1063 return (SET_ERROR(ENOTSUP));
1064 }
1065
1066 fzap_prefetch(zn);
1067 zap_name_free(zn);
1068 zap_unlockdir(zap, FTAG);
1069 return (err);
1070 }
1071
1072 int
1073 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1074 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
1075 {
1076 zap_t *zap;
1077 int err;
1078 zap_name_t *zn;
1079
1080 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1081 if (err)
1082 return (err);
1083 zn = zap_name_alloc_uint64(zap, key, key_numints);
1084 if (zn == NULL) {
1085 zap_unlockdir(zap, FTAG);
1086 return (SET_ERROR(ENOTSUP));
1087 }
1088
1089 err = fzap_lookup(zn, integer_size, num_integers, buf,
1090 NULL, 0, NULL);
1091 zap_name_free(zn);
1092 zap_unlockdir(zap, FTAG);
1093 return (err);
1094 }
1095
1096 int
1097 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
1098 {
1099 int err = zap_lookup_norm(os, zapobj, name, 0,
1100 0, NULL, 0, NULL, 0, NULL);
1101 if (err == EOVERFLOW || err == EINVAL)
1102 err = 0; /* found, but skipped reading the value */
1103 return (err);
1104 }
1105
1106 int
1107 zap_length(objset_t *os, uint64_t zapobj, const char *name,
1108 uint64_t *integer_size, uint64_t *num_integers)
1109 {
1110 zap_t *zap;
1111 int err;
1112 mzap_ent_t *mze;
1113 zap_name_t *zn;
1114
1115 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1116 if (err)
1117 return (err);
1118 zn = zap_name_alloc(zap, name, 0);
1119 if (zn == NULL) {
1120 zap_unlockdir(zap, FTAG);
1121 return (SET_ERROR(ENOTSUP));
1122 }
1123 if (!zap->zap_ismicro) {
1124 err = fzap_length(zn, integer_size, num_integers);
1125 } else {
1126 mze = mze_find(zn);
1127 if (mze == NULL) {
1128 err = SET_ERROR(ENOENT);
1129 } else {
1130 if (integer_size)
1131 *integer_size = 8;
1132 if (num_integers)
1133 *num_integers = 1;
1134 }
1135 }
1136 zap_name_free(zn);
1137 zap_unlockdir(zap, FTAG);
1138 return (err);
1139 }
1140
1141 int
1142 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1143 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
1144 {
1145 zap_t *zap;
1146 int err;
1147 zap_name_t *zn;
1148
1149 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1150 if (err)
1151 return (err);
1152 zn = zap_name_alloc_uint64(zap, key, key_numints);
1153 if (zn == NULL) {
1154 zap_unlockdir(zap, FTAG);
1155 return (SET_ERROR(ENOTSUP));
1156 }
1157 err = fzap_length(zn, integer_size, num_integers);
1158 zap_name_free(zn);
1159 zap_unlockdir(zap, FTAG);
1160 return (err);
1161 }
1162
1163 static void
1164 mzap_addent(zap_name_t *zn, uint64_t value)
1165 {
1166 int i;
1167 zap_t *zap = zn->zn_zap;
1168 int start = zap->zap_m.zap_alloc_next;
1169 uint32_t cd;
1170
1171 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1172
1173 #ifdef ZFS_DEBUG
1174 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
1175 ASSERTV(mzap_ent_phys_t *mze);
1176 ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
1177 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
1178 }
1179 #endif
1180
1181 cd = mze_find_unused_cd(zap, zn->zn_hash);
1182 /* given the limited size of the microzap, this can't happen */
1183 ASSERT(cd < zap_maxcd(zap));
1184
1185 again:
1186 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
1187 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1188 if (mze->mze_name[0] == 0) {
1189 mze->mze_value = value;
1190 mze->mze_cd = cd;
1191 (void) strlcpy(mze->mze_name, zn->zn_key_orig,
1192 sizeof (mze->mze_name));
1193 zap->zap_m.zap_num_entries++;
1194 zap->zap_m.zap_alloc_next = i+1;
1195 if (zap->zap_m.zap_alloc_next ==
1196 zap->zap_m.zap_num_chunks)
1197 zap->zap_m.zap_alloc_next = 0;
1198 mze_insert(zap, i, zn->zn_hash);
1199 return;
1200 }
1201 }
1202 if (start != 0) {
1203 start = 0;
1204 goto again;
1205 }
1206 cmn_err(CE_PANIC, "out of entries!");
1207 }
1208
1209 static int
1210 zap_add_impl(zap_t *zap, const char *key,
1211 int integer_size, uint64_t num_integers,
1212 const void *val, dmu_tx_t *tx, void *tag)
1213 {
1214 int err = 0;
1215 mzap_ent_t *mze;
1216 const uint64_t *intval = val;
1217 zap_name_t *zn;
1218
1219 zn = zap_name_alloc(zap, key, 0);
1220 if (zn == NULL) {
1221 zap_unlockdir(zap, tag);
1222 return (SET_ERROR(ENOTSUP));
1223 }
1224 if (!zap->zap_ismicro) {
1225 err = fzap_add(zn, integer_size, num_integers, val, tag, tx);
1226 zap = zn->zn_zap; /* fzap_add() may change zap */
1227 } else if (integer_size != 8 || num_integers != 1 ||
1228 strlen(key) >= MZAP_NAME_LEN ||
1229 !mze_canfit_fzap_leaf(zn, zn->zn_hash)) {
1230 err = mzap_upgrade(&zn->zn_zap, tag, tx, 0);
1231 if (err == 0) {
1232 err = fzap_add(zn, integer_size, num_integers, val,
1233 tag, tx);
1234 }
1235 zap = zn->zn_zap; /* fzap_add() may change zap */
1236 } else {
1237 mze = mze_find(zn);
1238 if (mze != NULL) {
1239 err = SET_ERROR(EEXIST);
1240 } else {
1241 mzap_addent(zn, *intval);
1242 }
1243 }
1244 ASSERT(zap == zn->zn_zap);
1245 zap_name_free(zn);
1246 if (zap != NULL) /* may be NULL if fzap_add() failed */
1247 zap_unlockdir(zap, tag);
1248 return (err);
1249 }
1250
1251 int
1252 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1253 int integer_size, uint64_t num_integers,
1254 const void *val, dmu_tx_t *tx)
1255 {
1256 zap_t *zap;
1257 int err;
1258
1259 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1260 if (err != 0)
1261 return (err);
1262 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1263 /* zap_add_impl() calls zap_unlockdir() */
1264 return (err);
1265 }
1266
1267 int
1268 zap_add_by_dnode(dnode_t *dn, const char *key,
1269 int integer_size, uint64_t num_integers,
1270 const void *val, dmu_tx_t *tx)
1271 {
1272 zap_t *zap;
1273 int err;
1274
1275 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1276 if (err != 0)
1277 return (err);
1278 err = zap_add_impl(zap, key, integer_size, num_integers, val, tx, FTAG);
1279 /* zap_add_impl() calls zap_unlockdir() */
1280 return (err);
1281 }
1282
1283 int
1284 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1285 int key_numints, int integer_size, uint64_t num_integers,
1286 const void *val, dmu_tx_t *tx)
1287 {
1288 zap_t *zap;
1289 int err;
1290 zap_name_t *zn;
1291
1292 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1293 if (err)
1294 return (err);
1295 zn = zap_name_alloc_uint64(zap, key, key_numints);
1296 if (zn == NULL) {
1297 zap_unlockdir(zap, FTAG);
1298 return (SET_ERROR(ENOTSUP));
1299 }
1300 err = fzap_add(zn, integer_size, num_integers, val, FTAG, tx);
1301 zap = zn->zn_zap; /* fzap_add() may change zap */
1302 zap_name_free(zn);
1303 if (zap != NULL) /* may be NULL if fzap_add() failed */
1304 zap_unlockdir(zap, FTAG);
1305 return (err);
1306 }
1307
1308 int
1309 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1310 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1311 {
1312 zap_t *zap;
1313 mzap_ent_t *mze;
1314 const uint64_t *intval = val;
1315 zap_name_t *zn;
1316 int err;
1317
1318 #ifdef ZFS_DEBUG
1319 uint64_t oldval;
1320
1321 /*
1322 * If there is an old value, it shouldn't change across the
1323 * lockdir (eg, due to bprewrite's xlation).
1324 */
1325 if (integer_size == 8 && num_integers == 1)
1326 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1327 #endif
1328
1329 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1330 if (err)
1331 return (err);
1332 zn = zap_name_alloc(zap, name, 0);
1333 if (zn == NULL) {
1334 zap_unlockdir(zap, FTAG);
1335 return (SET_ERROR(ENOTSUP));
1336 }
1337 if (!zap->zap_ismicro) {
1338 err = fzap_update(zn, integer_size, num_integers, val,
1339 FTAG, tx);
1340 zap = zn->zn_zap; /* fzap_update() may change zap */
1341 } else if (integer_size != 8 || num_integers != 1 ||
1342 strlen(name) >= MZAP_NAME_LEN) {
1343 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1344 zapobj, integer_size, num_integers, name);
1345 err = mzap_upgrade(&zn->zn_zap, FTAG, tx, 0);
1346 if (err == 0) {
1347 err = fzap_update(zn, integer_size, num_integers,
1348 val, FTAG, tx);
1349 }
1350 zap = zn->zn_zap; /* fzap_update() may change zap */
1351 } else {
1352 mze = mze_find(zn);
1353 if (mze != NULL) {
1354 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1355 MZE_PHYS(zap, mze)->mze_value = *intval;
1356 } else {
1357 mzap_addent(zn, *intval);
1358 }
1359 }
1360 ASSERT(zap == zn->zn_zap);
1361 zap_name_free(zn);
1362 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1363 zap_unlockdir(zap, FTAG);
1364 return (err);
1365 }
1366
1367 int
1368 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1369 int key_numints,
1370 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1371 {
1372 zap_t *zap;
1373 zap_name_t *zn;
1374 int err;
1375
1376 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, FTAG, &zap);
1377 if (err)
1378 return (err);
1379 zn = zap_name_alloc_uint64(zap, key, key_numints);
1380 if (zn == NULL) {
1381 zap_unlockdir(zap, FTAG);
1382 return (SET_ERROR(ENOTSUP));
1383 }
1384 err = fzap_update(zn, integer_size, num_integers, val, FTAG, tx);
1385 zap = zn->zn_zap; /* fzap_update() may change zap */
1386 zap_name_free(zn);
1387 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1388 zap_unlockdir(zap, FTAG);
1389 return (err);
1390 }
1391
1392 int
1393 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1394 {
1395 return (zap_remove_norm(os, zapobj, name, 0, tx));
1396 }
1397
1398 static int
1399 zap_remove_impl(zap_t *zap, const char *name,
1400 matchtype_t mt, dmu_tx_t *tx)
1401 {
1402 mzap_ent_t *mze;
1403 zap_name_t *zn;
1404 int err = 0;
1405
1406 zn = zap_name_alloc(zap, name, mt);
1407 if (zn == NULL)
1408 return (SET_ERROR(ENOTSUP));
1409 if (!zap->zap_ismicro) {
1410 err = fzap_remove(zn, tx);
1411 } else {
1412 mze = mze_find(zn);
1413 if (mze == NULL) {
1414 err = SET_ERROR(ENOENT);
1415 } else {
1416 zap->zap_m.zap_num_entries--;
1417 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1418 sizeof (mzap_ent_phys_t));
1419 mze_remove(zap, mze);
1420 }
1421 }
1422 zap_name_free(zn);
1423 return (err);
1424 }
1425
1426 int
1427 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1428 matchtype_t mt, dmu_tx_t *tx)
1429 {
1430 zap_t *zap;
1431 int err;
1432
1433 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1434 if (err)
1435 return (err);
1436 err = zap_remove_impl(zap, name, mt, tx);
1437 zap_unlockdir(zap, FTAG);
1438 return (err);
1439 }
1440
1441 int
1442 zap_remove_by_dnode(dnode_t *dn, const char *name, dmu_tx_t *tx)
1443 {
1444 zap_t *zap;
1445 int err;
1446
1447 err = zap_lockdir_by_dnode(dn, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1448 if (err)
1449 return (err);
1450 err = zap_remove_impl(zap, name, 0, tx);
1451 zap_unlockdir(zap, FTAG);
1452 return (err);
1453 }
1454
1455 int
1456 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1457 int key_numints, dmu_tx_t *tx)
1458 {
1459 zap_t *zap;
1460 int err;
1461 zap_name_t *zn;
1462
1463 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, FTAG, &zap);
1464 if (err)
1465 return (err);
1466 zn = zap_name_alloc_uint64(zap, key, key_numints);
1467 if (zn == NULL) {
1468 zap_unlockdir(zap, FTAG);
1469 return (SET_ERROR(ENOTSUP));
1470 }
1471 err = fzap_remove(zn, tx);
1472 zap_name_free(zn);
1473 zap_unlockdir(zap, FTAG);
1474 return (err);
1475 }
1476
1477 /*
1478 * Routines for iterating over the attributes.
1479 */
1480
1481 void
1482 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1483 uint64_t serialized)
1484 {
1485 zc->zc_objset = os;
1486 zc->zc_zap = NULL;
1487 zc->zc_leaf = NULL;
1488 zc->zc_zapobj = zapobj;
1489 zc->zc_serialized = serialized;
1490 zc->zc_hash = 0;
1491 zc->zc_cd = 0;
1492 }
1493
1494 void
1495 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1496 {
1497 zap_cursor_init_serialized(zc, os, zapobj, 0);
1498 }
1499
1500 void
1501 zap_cursor_fini(zap_cursor_t *zc)
1502 {
1503 if (zc->zc_zap) {
1504 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1505 zap_unlockdir(zc->zc_zap, NULL);
1506 zc->zc_zap = NULL;
1507 }
1508 if (zc->zc_leaf) {
1509 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1510 zap_put_leaf(zc->zc_leaf);
1511 zc->zc_leaf = NULL;
1512 }
1513 zc->zc_objset = NULL;
1514 }
1515
1516 uint64_t
1517 zap_cursor_serialize(zap_cursor_t *zc)
1518 {
1519 if (zc->zc_hash == -1ULL)
1520 return (-1ULL);
1521 if (zc->zc_zap == NULL)
1522 return (zc->zc_serialized);
1523 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1524 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1525
1526 /*
1527 * We want to keep the high 32 bits of the cursor zero if we can, so
1528 * that 32-bit programs can access this. So usually use a small
1529 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1530 * of the cursor.
1531 *
1532 * [ collision differentiator | zap_hashbits()-bit hash value ]
1533 */
1534 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1535 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1536 }
1537
1538 int
1539 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1540 {
1541 int err;
1542 avl_index_t idx;
1543 mzap_ent_t mze_tofind;
1544 mzap_ent_t *mze;
1545
1546 if (zc->zc_hash == -1ULL)
1547 return (SET_ERROR(ENOENT));
1548
1549 if (zc->zc_zap == NULL) {
1550 int hb;
1551 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1552 RW_READER, TRUE, FALSE, NULL, &zc->zc_zap);
1553 if (err)
1554 return (err);
1555
1556 /*
1557 * To support zap_cursor_init_serialized, advance, retrieve,
1558 * we must add to the existing zc_cd, which may already
1559 * be 1 due to the zap_cursor_advance.
1560 */
1561 ASSERT(zc->zc_hash == 0);
1562 hb = zap_hashbits(zc->zc_zap);
1563 zc->zc_hash = zc->zc_serialized << (64 - hb);
1564 zc->zc_cd += zc->zc_serialized >> hb;
1565 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1566 zc->zc_cd = 0;
1567 } else {
1568 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1569 }
1570 if (!zc->zc_zap->zap_ismicro) {
1571 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1572 } else {
1573 mze_tofind.mze_hash = zc->zc_hash;
1574 mze_tofind.mze_cd = zc->zc_cd;
1575
1576 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1577 if (mze == NULL) {
1578 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1579 idx, AVL_AFTER);
1580 }
1581 if (mze) {
1582 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1583 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1584 za->za_normalization_conflict =
1585 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1586 za->za_integer_length = 8;
1587 za->za_num_integers = 1;
1588 za->za_first_integer = mzep->mze_value;
1589 (void) strcpy(za->za_name, mzep->mze_name);
1590 zc->zc_hash = mze->mze_hash;
1591 zc->zc_cd = mze->mze_cd;
1592 err = 0;
1593 } else {
1594 zc->zc_hash = -1ULL;
1595 err = SET_ERROR(ENOENT);
1596 }
1597 }
1598 rw_exit(&zc->zc_zap->zap_rwlock);
1599 return (err);
1600 }
1601
1602 void
1603 zap_cursor_advance(zap_cursor_t *zc)
1604 {
1605 if (zc->zc_hash == -1ULL)
1606 return;
1607 zc->zc_cd++;
1608 }
1609
1610 int
1611 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1612 {
1613 int err;
1614 zap_t *zap;
1615
1616 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, FTAG, &zap);
1617 if (err)
1618 return (err);
1619
1620 bzero(zs, sizeof (zap_stats_t));
1621
1622 if (zap->zap_ismicro) {
1623 zs->zs_blocksize = zap->zap_dbuf->db_size;
1624 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1625 zs->zs_num_blocks = 1;
1626 } else {
1627 fzap_get_stats(zap, zs);
1628 }
1629 zap_unlockdir(zap, FTAG);
1630 return (0);
1631 }
1632
1633 #if defined(_KERNEL) && defined(HAVE_SPL)
1634 EXPORT_SYMBOL(zap_create);
1635 EXPORT_SYMBOL(zap_create_dnsize);
1636 EXPORT_SYMBOL(zap_create_norm);
1637 EXPORT_SYMBOL(zap_create_norm_dnsize);
1638 EXPORT_SYMBOL(zap_create_flags);
1639 EXPORT_SYMBOL(zap_create_flags_dnsize);
1640 EXPORT_SYMBOL(zap_create_claim);
1641 EXPORT_SYMBOL(zap_create_claim_norm);
1642 EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
1643 EXPORT_SYMBOL(zap_destroy);
1644 EXPORT_SYMBOL(zap_lookup);
1645 EXPORT_SYMBOL(zap_lookup_by_dnode);
1646 EXPORT_SYMBOL(zap_lookup_norm);
1647 EXPORT_SYMBOL(zap_lookup_uint64);
1648 EXPORT_SYMBOL(zap_contains);
1649 EXPORT_SYMBOL(zap_prefetch);
1650 EXPORT_SYMBOL(zap_prefetch_uint64);
1651 EXPORT_SYMBOL(zap_add);
1652 EXPORT_SYMBOL(zap_add_by_dnode);
1653 EXPORT_SYMBOL(zap_add_uint64);
1654 EXPORT_SYMBOL(zap_update);
1655 EXPORT_SYMBOL(zap_update_uint64);
1656 EXPORT_SYMBOL(zap_length);
1657 EXPORT_SYMBOL(zap_length_uint64);
1658 EXPORT_SYMBOL(zap_remove);
1659 EXPORT_SYMBOL(zap_remove_by_dnode);
1660 EXPORT_SYMBOL(zap_remove_norm);
1661 EXPORT_SYMBOL(zap_remove_uint64);
1662 EXPORT_SYMBOL(zap_count);
1663 EXPORT_SYMBOL(zap_value_search);
1664 EXPORT_SYMBOL(zap_join);
1665 EXPORT_SYMBOL(zap_join_increment);
1666 EXPORT_SYMBOL(zap_add_int);
1667 EXPORT_SYMBOL(zap_remove_int);
1668 EXPORT_SYMBOL(zap_lookup_int);
1669 EXPORT_SYMBOL(zap_increment_int);
1670 EXPORT_SYMBOL(zap_add_int_key);
1671 EXPORT_SYMBOL(zap_lookup_int_key);
1672 EXPORT_SYMBOL(zap_increment);
1673 EXPORT_SYMBOL(zap_cursor_init);
1674 EXPORT_SYMBOL(zap_cursor_fini);
1675 EXPORT_SYMBOL(zap_cursor_retrieve);
1676 EXPORT_SYMBOL(zap_cursor_advance);
1677 EXPORT_SYMBOL(zap_cursor_serialize);
1678 EXPORT_SYMBOL(zap_cursor_init_serialized);
1679 EXPORT_SYMBOL(zap_get_stats);
1680 #endif