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 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/zfs_context.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
38 static void mzap_upgrade(zap_t *zap, dmu_tx_t *tx);
42 mzap_byteswap(mzap_phys_t *buf, size_t size)
45 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
46 buf->mz_salt = BSWAP_64(buf->mz_salt);
47 max = (size / MZAP_ENT_LEN) - 1;
48 for (i = 0; i < max; i++) {
49 buf->mz_chunk[i].mze_value =
50 BSWAP_64(buf->mz_chunk[i].mze_value);
51 buf->mz_chunk[i].mze_cd =
52 BSWAP_32(buf->mz_chunk[i].mze_cd);
57 zap_byteswap(void *buf, size_t size)
61 block_type = *(uint64_t *)buf;
63 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
64 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
65 mzap_byteswap(buf, size);
67 fzap_byteswap(buf, size);
72 mze_compare(const void *arg1, const void *arg2)
74 const mzap_ent_t *mze1 = arg1;
75 const mzap_ent_t *mze2 = arg2;
77 if (mze1->mze_hash > mze2->mze_hash)
79 if (mze1->mze_hash < mze2->mze_hash)
81 if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd)
83 if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd)
89 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep)
93 ASSERT(zap->zap_ismicro);
94 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
95 ASSERT(mzep->mze_cd < ZAP_MAXCD);
96 ASSERT3U(zap_hash(zap, mzep->mze_name), ==, hash);
98 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
99 mze->mze_chunkid = chunkid;
100 mze->mze_hash = hash;
101 mze->mze_phys = *mzep;
102 avl_add(&zap->zap_m.zap_avl, mze);
106 mze_find(zap_t *zap, const char *name, uint64_t hash)
108 mzap_ent_t mze_tofind;
111 avl_tree_t *avl = &zap->zap_m.zap_avl;
113 ASSERT(zap->zap_ismicro);
114 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
115 ASSERT3U(zap_hash(zap, name), ==, hash);
117 if (strlen(name) >= sizeof (mze_tofind.mze_phys.mze_name))
120 mze_tofind.mze_hash = hash;
121 mze_tofind.mze_phys.mze_cd = 0;
123 mze = avl_find(avl, &mze_tofind, &idx);
125 mze = avl_nearest(avl, idx, AVL_AFTER);
126 for (; mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
127 if (strcmp(name, mze->mze_phys.mze_name) == 0)
134 mze_find_unused_cd(zap_t *zap, uint64_t hash)
136 mzap_ent_t mze_tofind;
139 avl_tree_t *avl = &zap->zap_m.zap_avl;
142 ASSERT(zap->zap_ismicro);
143 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
145 mze_tofind.mze_hash = hash;
146 mze_tofind.mze_phys.mze_cd = 0;
149 for (mze = avl_find(avl, &mze_tofind, &idx);
150 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
151 if (mze->mze_phys.mze_cd != cd)
160 mze_remove(zap_t *zap, mzap_ent_t *mze)
162 ASSERT(zap->zap_ismicro);
163 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
165 avl_remove(&zap->zap_m.zap_avl, mze);
166 kmem_free(mze, sizeof (mzap_ent_t));
170 mze_destroy(zap_t *zap)
173 void *avlcookie = NULL;
175 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
176 kmem_free(mze, sizeof (mzap_ent_t));
177 avl_destroy(&zap->zap_m.zap_avl);
181 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
187 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
189 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
190 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, 0);
191 rw_enter(&zap->zap_rwlock, RW_WRITER);
192 zap->zap_objset = os;
193 zap->zap_object = obj;
196 if (((uint64_t *)db->db_data)[0] != ZBT_MICRO) {
197 mutex_init(&zap->zap_f.zap_num_entries_mtx, NULL,
199 zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
201 zap->zap_ismicro = TRUE;
205 * Make sure that zap_ismicro is set before we let others see
206 * it, because zap_lockdir() checks zap_ismicro without the lock
209 winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
211 if (winner != NULL) {
212 rw_exit(&zap->zap_rwlock);
213 rw_destroy(&zap->zap_rwlock);
214 if (!zap->zap_ismicro)
215 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
216 kmem_free(zap, sizeof (zap_t));
220 if (zap->zap_ismicro) {
221 zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
222 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
223 avl_create(&zap->zap_m.zap_avl, mze_compare,
224 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
226 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
227 mzap_ent_phys_t *mze =
228 &zap->zap_m.zap_phys->mz_chunk[i];
229 if (mze->mze_name[0]) {
230 zap->zap_m.zap_num_entries++;
232 zap_hash(zap, mze->mze_name), mze);
236 zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
238 ASSERT3U(sizeof (struct zap_leaf_header), ==,
239 2*ZAP_LEAF_CHUNKSIZE);
242 * The embedded pointer table should not overlap the
245 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
246 &zap->zap_f.zap_phys->zap_salt);
249 * The embedded pointer table should end at the end of
252 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
253 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
254 (uintptr_t)zap->zap_f.zap_phys, ==,
255 zap->zap_dbuf->db_size);
257 rw_exit(&zap->zap_rwlock);
262 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
263 krw_t lti, int fatreader, zap_t **zapp)
272 err = dmu_buf_hold(os, obj, 0, NULL, &db);
278 dmu_object_info_t doi;
279 dmu_object_info_from_db(db, &doi);
280 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
284 zap = dmu_buf_get_user(db);
286 zap = mzap_open(os, obj, db);
289 * We're checking zap_ismicro without the lock held, in order to
290 * tell what type of lock we want. Once we have some sort of
291 * lock, see if it really is the right type. In practice this
292 * can only be different if it was upgraded from micro to fat,
293 * and micro wanted WRITER but fat only needs READER.
295 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
296 rw_enter(&zap->zap_rwlock, lt);
297 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
298 /* it was upgraded, now we only need reader */
299 ASSERT(lt == RW_WRITER);
301 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
302 rw_downgrade(&zap->zap_rwlock);
306 zap->zap_objset = os;
309 dmu_buf_will_dirty(db, tx);
311 ASSERT3P(zap->zap_dbuf, ==, db);
313 ASSERT(!zap->zap_ismicro ||
314 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
315 if (zap->zap_ismicro && tx &&
316 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
317 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
318 if (newsz > MZAP_MAX_BLKSZ) {
319 dprintf("upgrading obj %llu: num_entries=%u\n",
320 obj, zap->zap_m.zap_num_entries);
321 mzap_upgrade(zap, tx);
325 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
326 ASSERT3U(err, ==, 0);
327 zap->zap_m.zap_num_chunks =
328 db->db_size / MZAP_ENT_LEN - 1;
336 zap_unlockdir(zap_t *zap)
338 rw_exit(&zap->zap_rwlock);
339 dmu_buf_rele(zap->zap_dbuf, NULL);
343 mzap_upgrade(zap_t *zap, dmu_tx_t *tx)
346 int i, sz, nchunks, err;
348 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
350 sz = zap->zap_dbuf->db_size;
351 mzp = kmem_alloc(sz, KM_SLEEP);
352 bcopy(zap->zap_dbuf->db_data, mzp, sz);
353 nchunks = zap->zap_m.zap_num_chunks;
355 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
356 1ULL << fzap_default_block_shift, 0, tx);
359 dprintf("upgrading obj=%llu with %u chunks\n",
360 zap->zap_object, nchunks);
363 fzap_upgrade(zap, tx);
365 for (i = 0; i < nchunks; i++) {
367 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
368 if (mze->mze_name[0] == 0)
370 dprintf("adding %s=%llu\n",
371 mze->mze_name, mze->mze_value);
372 err = fzap_add_cd(zap,
373 mze->mze_name, 8, 1, &mze->mze_value,
375 ASSERT3U(err, ==, 0);
381 zap_hash(zap_t *zap, const char *name)
385 uint64_t crc = zap->zap_salt;
388 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
389 for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
390 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF];
393 * Only use 28 bits, since we need 4 bits in the cookie for the
394 * collision differentiator. We MUST use the high bits, since
395 * those are the onces that we first pay attention to when
396 * chosing the bucket.
398 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
405 mzap_create_impl(objset_t *os, uint64_t obj, dmu_tx_t *tx)
410 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
414 dmu_object_info_t doi;
415 dmu_object_info_from_db(db, &doi);
416 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
420 dmu_buf_will_dirty(db, tx);
422 zp->mz_block_type = ZBT_MICRO;
423 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
424 ASSERT(zp->mz_salt != 0);
425 dmu_buf_rele(db, FTAG);
429 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
430 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
434 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
437 mzap_create_impl(os, obj, tx);
442 zap_create(objset_t *os, dmu_object_type_t ot,
443 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
445 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
447 mzap_create_impl(os, obj, tx);
452 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
455 * dmu_object_free will free the object number and free the
456 * data. Freeing the data will cause our pageout function to be
457 * called, which will destroy our data (zap_leaf_t's and zap_t).
460 return (dmu_object_free(os, zapobj, tx));
465 zap_evict(dmu_buf_t *db, void *vzap)
469 rw_destroy(&zap->zap_rwlock);
471 if (zap->zap_ismicro)
474 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
476 kmem_free(zap, sizeof (zap_t));
480 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
485 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
488 if (!zap->zap_ismicro) {
489 err = fzap_count(zap, count);
491 *count = zap->zap_m.zap_num_entries;
498 * Routines for maniplulating attributes.
502 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
503 uint64_t integer_size, uint64_t num_integers, void *buf)
509 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
512 if (!zap->zap_ismicro) {
513 err = fzap_lookup(zap, name,
514 integer_size, num_integers, buf);
516 mze = mze_find(zap, name, zap_hash(zap, name));
520 if (num_integers < 1)
522 else if (integer_size != 8)
525 *(uint64_t *)buf = mze->mze_phys.mze_value;
533 zap_length(objset_t *os, uint64_t zapobj, const char *name,
534 uint64_t *integer_size, uint64_t *num_integers)
540 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
543 if (!zap->zap_ismicro) {
544 err = fzap_length(zap, name, integer_size, num_integers);
546 mze = mze_find(zap, name, zap_hash(zap, name));
561 mzap_addent(zap_t *zap, const char *name, uint64_t hash, uint64_t value)
564 int start = zap->zap_m.zap_alloc_next;
567 dprintf("obj=%llu %s=%llu\n", zap->zap_object, name, value);
568 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
571 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
572 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
573 ASSERT(strcmp(name, mze->mze_name) != 0);
577 cd = mze_find_unused_cd(zap, hash);
578 /* given the limited size of the microzap, this can't happen */
579 ASSERT(cd != ZAP_MAXCD);
582 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
583 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
584 if (mze->mze_name[0] == 0) {
585 mze->mze_value = value;
587 (void) strcpy(mze->mze_name, name);
588 zap->zap_m.zap_num_entries++;
589 zap->zap_m.zap_alloc_next = i+1;
590 if (zap->zap_m.zap_alloc_next ==
591 zap->zap_m.zap_num_chunks)
592 zap->zap_m.zap_alloc_next = 0;
593 mze_insert(zap, i, hash, mze);
601 ASSERT(!"out of entries!");
605 zap_add(objset_t *os, uint64_t zapobj, const char *name,
606 int integer_size, uint64_t num_integers,
607 const void *val, dmu_tx_t *tx)
612 const uint64_t *intval = val;
615 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
618 if (!zap->zap_ismicro) {
619 err = fzap_add(zap, name, integer_size, num_integers, val, tx);
620 } else if (integer_size != 8 || num_integers != 1 ||
621 strlen(name) >= MZAP_NAME_LEN) {
622 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
623 zapobj, integer_size, num_integers, name);
624 mzap_upgrade(zap, tx);
625 err = fzap_add(zap, name, integer_size, num_integers, val, tx);
627 hash = zap_hash(zap, name);
628 mze = mze_find(zap, name, hash);
632 mzap_addent(zap, name, hash, *intval);
640 zap_update(objset_t *os, uint64_t zapobj, const char *name,
641 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
645 const uint64_t *intval = val;
649 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
652 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
653 if (!zap->zap_ismicro) {
654 err = fzap_update(zap, name,
655 integer_size, num_integers, val, tx);
656 } else if (integer_size != 8 || num_integers != 1 ||
657 strlen(name) >= MZAP_NAME_LEN) {
658 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
659 zapobj, integer_size, num_integers, name);
660 mzap_upgrade(zap, tx);
661 err = fzap_update(zap, name,
662 integer_size, num_integers, val, tx);
664 hash = zap_hash(zap, name);
665 mze = mze_find(zap, name, hash);
667 mze->mze_phys.mze_value = *intval;
668 zap->zap_m.zap_phys->mz_chunk
669 [mze->mze_chunkid].mze_value = *intval;
671 mzap_addent(zap, name, hash, *intval);
679 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
685 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, &zap);
688 if (!zap->zap_ismicro) {
689 err = fzap_remove(zap, name, tx);
691 mze = mze_find(zap, name, zap_hash(zap, name));
693 dprintf("fail: %s\n", name);
696 dprintf("success: %s\n", name);
697 zap->zap_m.zap_num_entries--;
698 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
699 sizeof (mzap_ent_phys_t));
700 mze_remove(zap, mze);
709 * Routines for iterating over the attributes.
713 * We want to keep the high 32 bits of the cursor zero if we can, so
714 * that 32-bit programs can access this. So use a small hash value so
715 * we can fit 4 bits of cd into the 32-bit cursor.
717 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ]
720 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
726 zc->zc_zapobj = zapobj;
727 if (serialized == -1ULL) {
731 zc->zc_hash = serialized << (64-ZAP_HASHBITS);
732 zc->zc_cd = serialized >> ZAP_HASHBITS;
733 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */
739 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
741 zap_cursor_init_serialized(zc, os, zapobj, 0);
745 zap_cursor_fini(zap_cursor_t *zc)
748 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
749 zap_unlockdir(zc->zc_zap);
753 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
754 zap_put_leaf(zc->zc_leaf);
757 zc->zc_objset = NULL;
761 zap_cursor_serialize(zap_cursor_t *zc)
763 if (zc->zc_hash == -1ULL)
765 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0);
766 ASSERT(zc->zc_cd < ZAP_MAXCD);
767 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) |
768 ((uint64_t)zc->zc_cd << ZAP_HASHBITS));
772 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
776 mzap_ent_t mze_tofind;
779 if (zc->zc_hash == -1ULL)
782 if (zc->zc_zap == NULL) {
783 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
784 RW_READER, TRUE, &zc->zc_zap);
788 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
790 if (!zc->zc_zap->zap_ismicro) {
791 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
795 mze_tofind.mze_hash = zc->zc_hash;
796 mze_tofind.mze_phys.mze_cd = zc->zc_cd;
798 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
799 ASSERT(mze == NULL || 0 == bcmp(&mze->mze_phys,
800 &zc->zc_zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
801 sizeof (mze->mze_phys)));
803 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
807 za->za_integer_length = 8;
808 za->za_num_integers = 1;
809 za->za_first_integer = mze->mze_phys.mze_value;
810 (void) strcpy(za->za_name, mze->mze_phys.mze_name);
811 zc->zc_hash = mze->mze_hash;
812 zc->zc_cd = mze->mze_phys.mze_cd;
818 rw_exit(&zc->zc_zap->zap_rwlock);
823 zap_cursor_advance(zap_cursor_t *zc)
825 if (zc->zc_hash == -1ULL)
828 if (zc->zc_cd >= ZAP_MAXCD) {
830 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS);
831 if (zc->zc_hash == 0) /* EOF */
837 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
842 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, &zap);
846 bzero(zs, sizeof (zap_stats_t));
848 if (zap->zap_ismicro) {
849 zs->zs_blocksize = zap->zap_dbuf->db_size;
850 zs->zs_num_entries = zap->zap_m.zap_num_entries;
851 zs->zs_num_blocks = 1;
853 fzap_get_stats(zap, zs);