4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28 #include <sys/zfs_context.h>
30 #include <sys/refcount.h>
31 #include <sys/zap_impl.h>
32 #include <sys/zap_leaf.h>
37 #include <sys/sunddi.h>
40 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags);
43 zap_getflags(zap_t *zap)
47 return (zap->zap_u.zap_fat.zap_phys->zap_flags);
51 zap_hashbits(zap_t *zap)
53 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
62 if (zap_getflags(zap) & ZAP_FLAG_HASH64)
69 zap_hash(zap_name_t *zn)
71 zap_t *zap = zn->zn_zap;
74 if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
75 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
76 h = *(uint64_t *)zn->zn_key_orig;
80 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
82 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
84 const uint64_t *wp = zn->zn_key_norm;
86 ASSERT(zn->zn_key_intlen == 8);
87 for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
91 for (j = 0; j < zn->zn_key_intlen; j++) {
93 zfs_crc64_table[(h ^ word) & 0xFF];
99 const uint8_t *cp = zn->zn_key_norm;
102 * We previously stored the terminating null on
103 * disk, but didn't hash it, so we need to
104 * continue to not hash it. (The
105 * zn_key_*_numints includes the terminating
106 * null for non-binary keys.)
108 len = zn->zn_key_norm_numints - 1;
110 ASSERT(zn->zn_key_intlen == 1);
111 for (i = 0; i < len; cp++, i++) {
113 zfs_crc64_table[(h ^ *cp) & 0xFF];
118 * Don't use all 64 bits, since we need some in the cookie for
119 * the collision differentiator. We MUST use the high bits,
120 * since those are the ones that we first pay attention to when
121 * chosing the bucket.
123 h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
129 zap_normalize(zap_t *zap, const char *name, char *namenorm)
131 size_t inlen, outlen;
134 ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
136 inlen = strlen(name) + 1;
137 outlen = ZAP_MAXNAMELEN;
140 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
141 zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
142 U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
148 zap_match(zap_name_t *zn, const char *matchname)
150 ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
152 if (zn->zn_matchtype == MT_FIRST) {
153 char norm[ZAP_MAXNAMELEN];
155 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
158 return (strcmp(zn->zn_key_norm, norm) == 0);
160 /* MT_BEST or MT_EXACT */
161 return (strcmp(zn->zn_key_orig, matchname) == 0);
166 zap_name_free(zap_name_t *zn)
168 kmem_free(zn, sizeof (zap_name_t));
172 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
174 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
177 zn->zn_key_intlen = sizeof (*key);
178 zn->zn_key_orig = key;
179 zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
180 zn->zn_matchtype = mt;
181 if (zap->zap_normflags) {
182 if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
186 zn->zn_key_norm = zn->zn_normbuf;
187 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
189 if (mt != MT_EXACT) {
193 zn->zn_key_norm = zn->zn_key_orig;
194 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
197 zn->zn_hash = zap_hash(zn);
202 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
204 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
206 ASSERT(zap->zap_normflags == 0);
208 zn->zn_key_intlen = sizeof (*key);
209 zn->zn_key_orig = zn->zn_key_norm = key;
210 zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
211 zn->zn_matchtype = MT_EXACT;
213 zn->zn_hash = zap_hash(zn);
218 mzap_byteswap(mzap_phys_t *buf, size_t size)
221 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
222 buf->mz_salt = BSWAP_64(buf->mz_salt);
223 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
224 max = (size / MZAP_ENT_LEN) - 1;
225 for (i = 0; i < max; i++) {
226 buf->mz_chunk[i].mze_value =
227 BSWAP_64(buf->mz_chunk[i].mze_value);
228 buf->mz_chunk[i].mze_cd =
229 BSWAP_32(buf->mz_chunk[i].mze_cd);
234 zap_byteswap(void *buf, size_t size)
238 block_type = *(uint64_t *)buf;
240 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
241 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
242 mzap_byteswap(buf, size);
244 fzap_byteswap(buf, size);
249 mze_compare(const void *arg1, const void *arg2)
251 const mzap_ent_t *mze1 = arg1;
252 const mzap_ent_t *mze2 = arg2;
254 if (mze1->mze_hash > mze2->mze_hash)
256 if (mze1->mze_hash < mze2->mze_hash)
258 if (mze1->mze_cd > mze2->mze_cd)
260 if (mze1->mze_cd < mze2->mze_cd)
266 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
270 ASSERT(zap->zap_ismicro);
271 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
273 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
274 mze->mze_chunkid = chunkid;
275 mze->mze_hash = hash;
276 mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
277 ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
278 avl_add(&zap->zap_m.zap_avl, mze);
282 mze_find(zap_name_t *zn)
284 mzap_ent_t mze_tofind;
287 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
289 ASSERT(zn->zn_zap->zap_ismicro);
290 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
292 mze_tofind.mze_hash = zn->zn_hash;
293 mze_tofind.mze_cd = 0;
296 mze = avl_find(avl, &mze_tofind, &idx);
298 mze = avl_nearest(avl, idx, AVL_AFTER);
299 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
300 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
301 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
304 if (zn->zn_matchtype == MT_BEST) {
305 zn->zn_matchtype = MT_FIRST;
312 mze_find_unused_cd(zap_t *zap, uint64_t hash)
314 mzap_ent_t mze_tofind;
317 avl_tree_t *avl = &zap->zap_m.zap_avl;
320 ASSERT(zap->zap_ismicro);
321 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
323 mze_tofind.mze_hash = hash;
324 mze_tofind.mze_cd = 0;
327 for (mze = avl_find(avl, &mze_tofind, &idx);
328 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
329 if (mze->mze_cd != cd)
338 mze_remove(zap_t *zap, mzap_ent_t *mze)
340 ASSERT(zap->zap_ismicro);
341 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
343 avl_remove(&zap->zap_m.zap_avl, mze);
344 kmem_free(mze, sizeof (mzap_ent_t));
348 mze_destroy(zap_t *zap)
351 void *avlcookie = NULL;
353 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
354 kmem_free(mze, sizeof (mzap_ent_t));
355 avl_destroy(&zap->zap_m.zap_avl);
359 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
365 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
367 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
368 rw_init(&zap->zap_rwlock, 0, 0, 0);
369 rw_enter(&zap->zap_rwlock, RW_WRITER);
370 zap->zap_objset = os;
371 zap->zap_object = obj;
374 if (*(uint64_t *)db->db_data != ZBT_MICRO) {
375 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
376 zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
378 zap->zap_ismicro = TRUE;
382 * Make sure that zap_ismicro is set before we let others see
383 * it, because zap_lockdir() checks zap_ismicro without the lock
386 winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
388 if (winner != NULL) {
389 rw_exit(&zap->zap_rwlock);
390 rw_destroy(&zap->zap_rwlock);
391 if (!zap->zap_ismicro)
392 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
393 kmem_free(zap, sizeof (zap_t));
397 if (zap->zap_ismicro) {
398 zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
399 zap->zap_normflags = zap->zap_m.zap_phys->mz_normflags;
400 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
401 avl_create(&zap->zap_m.zap_avl, mze_compare,
402 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
404 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
405 mzap_ent_phys_t *mze =
406 &zap->zap_m.zap_phys->mz_chunk[i];
407 if (mze->mze_name[0]) {
410 zap->zap_m.zap_num_entries++;
411 zn = zap_name_alloc(zap, mze->mze_name,
413 mze_insert(zap, i, zn->zn_hash);
418 zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
419 zap->zap_normflags = zap->zap_f.zap_phys->zap_normflags;
421 ASSERT3U(sizeof (struct zap_leaf_header), ==,
422 2*ZAP_LEAF_CHUNKSIZE);
425 * The embedded pointer table should not overlap the
428 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
429 &zap->zap_f.zap_phys->zap_salt);
432 * The embedded pointer table should end at the end of
435 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
436 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
437 (uintptr_t)zap->zap_f.zap_phys, ==,
438 zap->zap_dbuf->db_size);
440 rw_exit(&zap->zap_rwlock);
445 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
446 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
455 err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
461 dmu_object_info_t doi;
462 dmu_object_info_from_db(db, &doi);
463 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
467 zap = dmu_buf_get_user(db);
469 zap = mzap_open(os, obj, db);
472 * We're checking zap_ismicro without the lock held, in order to
473 * tell what type of lock we want. Once we have some sort of
474 * lock, see if it really is the right type. In practice this
475 * can only be different if it was upgraded from micro to fat,
476 * and micro wanted WRITER but fat only needs READER.
478 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
479 rw_enter(&zap->zap_rwlock, lt);
480 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
481 /* it was upgraded, now we only need reader */
482 ASSERT(lt == RW_WRITER);
484 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
485 rw_downgrade(&zap->zap_rwlock);
489 zap->zap_objset = os;
492 dmu_buf_will_dirty(db, tx);
494 ASSERT3P(zap->zap_dbuf, ==, db);
496 ASSERT(!zap->zap_ismicro ||
497 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
498 if (zap->zap_ismicro && tx && adding &&
499 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
500 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
501 if (newsz > MZAP_MAX_BLKSZ) {
502 dprintf("upgrading obj %llu: num_entries=%u\n",
503 obj, zap->zap_m.zap_num_entries);
505 return (mzap_upgrade(zapp, tx, 0));
507 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
508 ASSERT3U(err, ==, 0);
509 zap->zap_m.zap_num_chunks =
510 db->db_size / MZAP_ENT_LEN - 1;
518 zap_unlockdir(zap_t *zap)
520 rw_exit(&zap->zap_rwlock);
521 dmu_buf_rele(zap->zap_dbuf, NULL);
525 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
532 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
534 sz = zap->zap_dbuf->db_size;
535 mzp = kmem_alloc(sz, KM_SLEEP);
536 bcopy(zap->zap_dbuf->db_data, mzp, sz);
537 nchunks = zap->zap_m.zap_num_chunks;
540 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
541 1ULL << fzap_default_block_shift, 0, tx);
548 dprintf("upgrading obj=%llu with %u chunks\n",
549 zap->zap_object, nchunks);
550 /* XXX destroy the avl later, so we can use the stored hash value */
553 fzap_upgrade(zap, tx, flags);
555 for (i = 0; i < nchunks; i++) {
556 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
558 if (mze->mze_name[0] == 0)
560 dprintf("adding %s=%llu\n",
561 mze->mze_name, mze->mze_value);
562 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
563 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
564 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
575 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
581 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
585 dmu_object_info_t doi;
586 dmu_object_info_from_db(db, &doi);
587 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
591 dmu_buf_will_dirty(db, tx);
593 zp->mz_block_type = ZBT_MICRO;
594 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
595 zp->mz_normflags = normflags;
596 dmu_buf_rele(db, FTAG);
600 /* Only fat zap supports flags; upgrade immediately. */
601 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
602 B_FALSE, B_FALSE, &zap));
603 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
609 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
610 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
612 return (zap_create_claim_norm(os, obj,
613 0, ot, bonustype, bonuslen, tx));
617 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
618 dmu_object_type_t ot,
619 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
623 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
626 mzap_create_impl(os, obj, normflags, 0, tx);
631 zap_create(objset_t *os, dmu_object_type_t ot,
632 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
634 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
638 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
639 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
641 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
643 mzap_create_impl(os, obj, normflags, 0, tx);
648 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
649 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
650 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
652 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
654 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
655 leaf_blockshift <= SPA_MAXBLOCKSHIFT &&
656 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
657 indirect_blockshift <= SPA_MAXBLOCKSHIFT);
659 VERIFY(dmu_object_set_blocksize(os, obj,
660 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
662 mzap_create_impl(os, obj, normflags, flags, tx);
667 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
670 * dmu_object_free will free the object number and free the
671 * data. Freeing the data will cause our pageout function to be
672 * called, which will destroy our data (zap_leaf_t's and zap_t).
675 return (dmu_object_free(os, zapobj, tx));
680 zap_evict(dmu_buf_t *db, void *vzap)
684 rw_destroy(&zap->zap_rwlock);
686 if (zap->zap_ismicro)
689 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
691 kmem_free(zap, sizeof (zap_t));
695 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
700 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
703 if (!zap->zap_ismicro) {
704 err = fzap_count(zap, count);
706 *count = zap->zap_m.zap_num_entries;
713 * zn may be NULL; if not specified, it will be computed if needed.
714 * See also the comment above zap_entry_normalization_conflict().
717 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
720 int direction = AVL_BEFORE;
721 boolean_t allocdzn = B_FALSE;
723 if (zap->zap_normflags == 0)
727 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
728 other && other->mze_hash == mze->mze_hash;
729 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
732 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
736 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
743 if (direction == AVL_BEFORE) {
744 direction = AVL_AFTER;
754 * Routines for manipulating attributes.
758 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
759 uint64_t integer_size, uint64_t num_integers, void *buf)
761 return (zap_lookup_norm(os, zapobj, name, integer_size,
762 num_integers, buf, MT_EXACT, NULL, 0, NULL));
766 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
767 uint64_t integer_size, uint64_t num_integers, void *buf,
768 matchtype_t mt, char *realname, int rn_len,
776 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
779 zn = zap_name_alloc(zap, name, mt);
785 if (!zap->zap_ismicro) {
786 err = fzap_lookup(zn, integer_size, num_integers, buf,
787 realname, rn_len, ncp);
793 if (num_integers < 1) {
795 } else if (integer_size != 8) {
799 MZE_PHYS(zap, mze)->mze_value;
800 (void) strlcpy(realname,
801 MZE_PHYS(zap, mze)->mze_name, rn_len);
803 *ncp = mzap_normalization_conflict(zap,
815 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
822 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
825 zn = zap_name_alloc_uint64(zap, key, key_numints);
838 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
839 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
845 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
848 zn = zap_name_alloc_uint64(zap, key, key_numints);
854 err = fzap_lookup(zn, integer_size, num_integers, buf,
862 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
864 int err = (zap_lookup_norm(os, zapobj, name, 0,
865 0, NULL, MT_EXACT, NULL, 0, NULL));
866 if (err == EOVERFLOW || err == EINVAL)
867 err = 0; /* found, but skipped reading the value */
872 zap_length(objset_t *os, uint64_t zapobj, const char *name,
873 uint64_t *integer_size, uint64_t *num_integers)
880 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
883 zn = zap_name_alloc(zap, name, MT_EXACT);
888 if (!zap->zap_ismicro) {
889 err = fzap_length(zn, integer_size, num_integers);
907 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
908 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
914 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
917 zn = zap_name_alloc_uint64(zap, key, key_numints);
922 err = fzap_length(zn, integer_size, num_integers);
929 mzap_addent(zap_name_t *zn, uint64_t value)
932 zap_t *zap = zn->zn_zap;
933 int start = zap->zap_m.zap_alloc_next;
936 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
939 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
940 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
941 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
945 cd = mze_find_unused_cd(zap, zn->zn_hash);
946 /* given the limited size of the microzap, this can't happen */
947 ASSERT(cd < zap_maxcd(zap));
950 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
951 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
952 if (mze->mze_name[0] == 0) {
953 mze->mze_value = value;
955 (void) strcpy(mze->mze_name, zn->zn_key_orig);
956 zap->zap_m.zap_num_entries++;
957 zap->zap_m.zap_alloc_next = i+1;
958 if (zap->zap_m.zap_alloc_next ==
959 zap->zap_m.zap_num_chunks)
960 zap->zap_m.zap_alloc_next = 0;
961 mze_insert(zap, i, zn->zn_hash);
969 ASSERT(!"out of entries!");
973 zap_add(objset_t *os, uint64_t zapobj, const char *key,
974 int integer_size, uint64_t num_integers,
975 const void *val, dmu_tx_t *tx)
980 const uint64_t *intval = val;
983 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
986 zn = zap_name_alloc(zap, key, MT_EXACT);
991 if (!zap->zap_ismicro) {
992 err = fzap_add(zn, integer_size, num_integers, val, tx);
993 zap = zn->zn_zap; /* fzap_add() may change zap */
994 } else if (integer_size != 8 || num_integers != 1 ||
995 strlen(key) >= MZAP_NAME_LEN) {
996 err = mzap_upgrade(&zn->zn_zap, tx, 0);
998 err = fzap_add(zn, integer_size, num_integers, val, tx);
999 zap = zn->zn_zap; /* fzap_add() may change zap */
1005 mzap_addent(zn, *intval);
1008 ASSERT(zap == zn->zn_zap);
1010 if (zap != NULL) /* may be NULL if fzap_add() failed */
1016 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1017 int key_numints, int integer_size, uint64_t num_integers,
1018 const void *val, dmu_tx_t *tx)
1024 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1027 zn = zap_name_alloc_uint64(zap, key, key_numints);
1032 err = fzap_add(zn, integer_size, num_integers, val, tx);
1033 zap = zn->zn_zap; /* fzap_add() may change zap */
1035 if (zap != NULL) /* may be NULL if fzap_add() failed */
1041 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1042 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1047 const uint64_t *intval = val;
1053 * If there is an old value, it shouldn't change across the
1054 * lockdir (eg, due to bprewrite's xlation).
1056 if (integer_size == 8 && num_integers == 1)
1057 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1060 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1063 zn = zap_name_alloc(zap, name, MT_EXACT);
1068 if (!zap->zap_ismicro) {
1069 err = fzap_update(zn, integer_size, num_integers, val, tx);
1070 zap = zn->zn_zap; /* fzap_update() may change zap */
1071 } else if (integer_size != 8 || num_integers != 1 ||
1072 strlen(name) >= MZAP_NAME_LEN) {
1073 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1074 zapobj, integer_size, num_integers, name);
1075 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1077 err = fzap_update(zn, integer_size, num_integers,
1079 zap = zn->zn_zap; /* fzap_update() may change zap */
1083 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1084 MZE_PHYS(zap, mze)->mze_value = *intval;
1086 mzap_addent(zn, *intval);
1089 ASSERT(zap == zn->zn_zap);
1091 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1097 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1099 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1105 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1108 zn = zap_name_alloc_uint64(zap, key, key_numints);
1113 err = fzap_update(zn, integer_size, num_integers, val, tx);
1114 zap = zn->zn_zap; /* fzap_update() may change zap */
1116 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1122 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1124 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1128 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1129 matchtype_t mt, dmu_tx_t *tx)
1136 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1139 zn = zap_name_alloc(zap, name, mt);
1144 if (!zap->zap_ismicro) {
1145 err = fzap_remove(zn, tx);
1151 zap->zap_m.zap_num_entries--;
1152 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
1153 sizeof (mzap_ent_phys_t));
1154 mze_remove(zap, mze);
1163 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1164 int key_numints, dmu_tx_t *tx)
1170 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1173 zn = zap_name_alloc_uint64(zap, key, key_numints);
1178 err = fzap_remove(zn, tx);
1185 * Routines for iterating over the attributes.
1189 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1190 uint64_t serialized)
1195 zc->zc_zapobj = zapobj;
1196 zc->zc_serialized = serialized;
1202 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1204 zap_cursor_init_serialized(zc, os, zapobj, 0);
1208 zap_cursor_fini(zap_cursor_t *zc)
1211 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1212 zap_unlockdir(zc->zc_zap);
1216 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1217 zap_put_leaf(zc->zc_leaf);
1220 zc->zc_objset = NULL;
1224 zap_cursor_serialize(zap_cursor_t *zc)
1226 if (zc->zc_hash == -1ULL)
1228 if (zc->zc_zap == NULL)
1229 return (zc->zc_serialized);
1230 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1231 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1234 * We want to keep the high 32 bits of the cursor zero if we can, so
1235 * that 32-bit programs can access this. So usually use a small
1236 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1239 * [ collision differentiator | zap_hashbits()-bit hash value ]
1241 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1242 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1246 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1250 mzap_ent_t mze_tofind;
1253 if (zc->zc_hash == -1ULL)
1256 if (zc->zc_zap == NULL) {
1258 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1259 RW_READER, TRUE, FALSE, &zc->zc_zap);
1264 * To support zap_cursor_init_serialized, advance, retrieve,
1265 * we must add to the existing zc_cd, which may already
1266 * be 1 due to the zap_cursor_advance.
1268 ASSERT(zc->zc_hash == 0);
1269 hb = zap_hashbits(zc->zc_zap);
1270 zc->zc_hash = zc->zc_serialized << (64 - hb);
1271 zc->zc_cd += zc->zc_serialized >> hb;
1272 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1275 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1277 if (!zc->zc_zap->zap_ismicro) {
1278 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1282 mze_tofind.mze_hash = zc->zc_hash;
1283 mze_tofind.mze_cd = zc->zc_cd;
1285 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1287 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1291 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1292 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1293 za->za_normalization_conflict =
1294 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1295 za->za_integer_length = 8;
1296 za->za_num_integers = 1;
1297 za->za_first_integer = mzep->mze_value;
1298 (void) strcpy(za->za_name, mzep->mze_name);
1299 zc->zc_hash = mze->mze_hash;
1300 zc->zc_cd = mze->mze_cd;
1303 zc->zc_hash = -1ULL;
1306 rw_exit(&zc->zc_zap->zap_rwlock);
1311 zap_cursor_advance(zap_cursor_t *zc)
1313 if (zc->zc_hash == -1ULL)
1319 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1325 if (zc->zc_zap == NULL) {
1326 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1327 RW_READER, TRUE, FALSE, &zc->zc_zap);
1331 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1334 zn = zap_name_alloc(zc->zc_zap, name, mt);
1336 rw_exit(&zc->zc_zap->zap_rwlock);
1340 if (!zc->zc_zap->zap_ismicro) {
1341 err = fzap_cursor_move_to_key(zc, zn);
1348 zc->zc_hash = mze->mze_hash;
1349 zc->zc_cd = mze->mze_cd;
1354 rw_exit(&zc->zc_zap->zap_rwlock);
1359 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1364 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1368 bzero(zs, sizeof (zap_stats_t));
1370 if (zap->zap_ismicro) {
1371 zs->zs_blocksize = zap->zap_dbuf->db_size;
1372 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1373 zs->zs_num_blocks = 1;
1375 fzap_get_stats(zap, zs);
1382 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1383 uint64_t *towrite, uint64_t *tooverwrite)
1390 * Since, we don't have a name, we cannot figure out which blocks will
1391 * be affected in this operation. So, account for the worst case :
1392 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1393 * - 4 new blocks written if adding:
1394 * - 2 blocks for possibly split leaves,
1395 * - 2 grown ptrtbl blocks
1397 * This also accomodates the case where an add operation to a fairly
1398 * large microzap results in a promotion to fatzap.
1401 *towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
1406 * We lock the zap with adding == FALSE. Because, if we pass
1407 * the actual value of add, it could trigger a mzap_upgrade().
1408 * At present we are just evaluating the possibility of this operation
1409 * and hence we donot want to trigger an upgrade.
1411 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1415 if (!zap->zap_ismicro) {
1416 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1418 err = fzap_count_write(zn, add, towrite,
1423 * We treat this case as similar to (name == NULL)
1425 *towrite += (3 + (add ? 4 : 0)) * SPA_MAXBLOCKSIZE;
1429 * We are here if (name != NULL) and this is a micro-zap.
1430 * We account for the header block depending on whether it
1433 * Incase of an add-operation it is hard to find out
1434 * if this add will promote this microzap to fatzap.
1435 * Hence, we consider the worst case and account for the
1436 * blocks assuming this microzap would be promoted to a
1439 * 1 block overwritten : header block
1440 * 4 new blocks written : 2 new split leaf, 2 grown
1443 if (dmu_buf_freeable(zap->zap_dbuf))
1444 *tooverwrite += SPA_MAXBLOCKSIZE;
1446 *towrite += SPA_MAXBLOCKSIZE;
1449 *towrite += 4 * SPA_MAXBLOCKSIZE;