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.
23 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 #include <sys/zfs_context.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
37 #include <sys/dmu_objset.h>
40 #include <sys/sunddi.h>
43 extern inline mzap_phys_t
*zap_m_phys(zap_t
*zap
);
45 static int mzap_upgrade(zap_t
**zapp
, dmu_tx_t
*tx
, zap_flags_t flags
);
48 zap_getflags(zap_t
*zap
)
52 return (zap_f_phys(zap
)->zap_flags
);
56 zap_hashbits(zap_t
*zap
)
58 if (zap_getflags(zap
) & ZAP_FLAG_HASH64
)
67 if (zap_getflags(zap
) & ZAP_FLAG_HASH64
)
74 zap_hash(zap_name_t
*zn
)
76 zap_t
*zap
= zn
->zn_zap
;
79 if (zap_getflags(zap
) & ZAP_FLAG_PRE_HASHED_KEY
) {
80 ASSERT(zap_getflags(zap
) & ZAP_FLAG_UINT64_KEY
);
81 h
= *(uint64_t *)zn
->zn_key_orig
;
85 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
87 if (zap_getflags(zap
) & ZAP_FLAG_UINT64_KEY
) {
89 const uint64_t *wp
= zn
->zn_key_norm
;
91 ASSERT(zn
->zn_key_intlen
== 8);
92 for (i
= 0; i
< zn
->zn_key_norm_numints
; wp
++, i
++) {
96 for (j
= 0; j
< zn
->zn_key_intlen
; j
++) {
98 zfs_crc64_table
[(h
^ word
) & 0xFF];
104 const uint8_t *cp
= zn
->zn_key_norm
;
107 * We previously stored the terminating null on
108 * disk, but didn't hash it, so we need to
109 * continue to not hash it. (The
110 * zn_key_*_numints includes the terminating
111 * null for non-binary keys.)
113 len
= zn
->zn_key_norm_numints
- 1;
115 ASSERT(zn
->zn_key_intlen
== 1);
116 for (i
= 0; i
< len
; cp
++, i
++) {
118 zfs_crc64_table
[(h
^ *cp
) & 0xFF];
123 * Don't use all 64 bits, since we need some in the cookie for
124 * the collision differentiator. We MUST use the high bits,
125 * since those are the ones that we first pay attention to when
126 * chosing the bucket.
128 h
&= ~((1ULL << (64 - zap_hashbits(zap
))) - 1);
134 zap_normalize(zap_t
*zap
, const char *name
, char *namenorm
)
136 size_t inlen
, outlen
;
139 ASSERT(!(zap_getflags(zap
) & ZAP_FLAG_UINT64_KEY
));
141 inlen
= strlen(name
) + 1;
142 outlen
= ZAP_MAXNAMELEN
;
145 (void) u8_textprep_str((char *)name
, &inlen
, namenorm
, &outlen
,
146 zap
->zap_normflags
| U8_TEXTPREP_IGNORE_NULL
|
147 U8_TEXTPREP_IGNORE_INVALID
, U8_UNICODE_LATEST
, &err
);
153 zap_match(zap_name_t
*zn
, const char *matchname
)
155 ASSERT(!(zap_getflags(zn
->zn_zap
) & ZAP_FLAG_UINT64_KEY
));
157 if (zn
->zn_matchtype
== MT_FIRST
) {
158 char norm
[ZAP_MAXNAMELEN
];
160 if (zap_normalize(zn
->zn_zap
, matchname
, norm
) != 0)
163 return (strcmp(zn
->zn_key_norm
, norm
) == 0);
165 /* MT_BEST or MT_EXACT */
166 return (strcmp(zn
->zn_key_orig
, matchname
) == 0);
171 zap_name_free(zap_name_t
*zn
)
173 kmem_free(zn
, sizeof (zap_name_t
));
177 zap_name_alloc(zap_t
*zap
, const char *key
, matchtype_t mt
)
179 zap_name_t
*zn
= kmem_alloc(sizeof (zap_name_t
), KM_SLEEP
);
182 zn
->zn_key_intlen
= sizeof (*key
);
183 zn
->zn_key_orig
= key
;
184 zn
->zn_key_orig_numints
= strlen(zn
->zn_key_orig
) + 1;
185 zn
->zn_matchtype
= mt
;
186 if (zap
->zap_normflags
) {
187 if (zap_normalize(zap
, key
, zn
->zn_normbuf
) != 0) {
191 zn
->zn_key_norm
= zn
->zn_normbuf
;
192 zn
->zn_key_norm_numints
= strlen(zn
->zn_key_norm
) + 1;
194 if (mt
!= MT_EXACT
) {
198 zn
->zn_key_norm
= zn
->zn_key_orig
;
199 zn
->zn_key_norm_numints
= zn
->zn_key_orig_numints
;
202 zn
->zn_hash
= zap_hash(zn
);
207 zap_name_alloc_uint64(zap_t
*zap
, const uint64_t *key
, int numints
)
209 zap_name_t
*zn
= kmem_alloc(sizeof (zap_name_t
), KM_SLEEP
);
211 ASSERT(zap
->zap_normflags
== 0);
213 zn
->zn_key_intlen
= sizeof (*key
);
214 zn
->zn_key_orig
= zn
->zn_key_norm
= key
;
215 zn
->zn_key_orig_numints
= zn
->zn_key_norm_numints
= numints
;
216 zn
->zn_matchtype
= MT_EXACT
;
218 zn
->zn_hash
= zap_hash(zn
);
223 mzap_byteswap(mzap_phys_t
*buf
, size_t size
)
226 buf
->mz_block_type
= BSWAP_64(buf
->mz_block_type
);
227 buf
->mz_salt
= BSWAP_64(buf
->mz_salt
);
228 buf
->mz_normflags
= BSWAP_64(buf
->mz_normflags
);
229 max
= (size
/ MZAP_ENT_LEN
) - 1;
230 for (i
= 0; i
< max
; i
++) {
231 buf
->mz_chunk
[i
].mze_value
=
232 BSWAP_64(buf
->mz_chunk
[i
].mze_value
);
233 buf
->mz_chunk
[i
].mze_cd
=
234 BSWAP_32(buf
->mz_chunk
[i
].mze_cd
);
239 zap_byteswap(void *buf
, size_t size
)
243 block_type
= *(uint64_t *)buf
;
245 if (block_type
== ZBT_MICRO
|| block_type
== BSWAP_64(ZBT_MICRO
)) {
246 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
247 mzap_byteswap(buf
, size
);
249 fzap_byteswap(buf
, size
);
254 mze_compare(const void *arg1
, const void *arg2
)
256 const mzap_ent_t
*mze1
= arg1
;
257 const mzap_ent_t
*mze2
= arg2
;
259 if (mze1
->mze_hash
> mze2
->mze_hash
)
261 if (mze1
->mze_hash
< mze2
->mze_hash
)
263 if (mze1
->mze_cd
> mze2
->mze_cd
)
265 if (mze1
->mze_cd
< mze2
->mze_cd
)
271 mze_insert(zap_t
*zap
, int chunkid
, uint64_t hash
)
275 ASSERT(zap
->zap_ismicro
);
276 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
278 mze
= kmem_alloc(sizeof (mzap_ent_t
), KM_SLEEP
);
279 mze
->mze_chunkid
= chunkid
;
280 mze
->mze_hash
= hash
;
281 mze
->mze_cd
= MZE_PHYS(zap
, mze
)->mze_cd
;
282 ASSERT(MZE_PHYS(zap
, mze
)->mze_name
[0] != 0);
283 avl_add(&zap
->zap_m
.zap_avl
, mze
);
287 mze_find(zap_name_t
*zn
)
289 mzap_ent_t mze_tofind
;
292 avl_tree_t
*avl
= &zn
->zn_zap
->zap_m
.zap_avl
;
294 ASSERT(zn
->zn_zap
->zap_ismicro
);
295 ASSERT(RW_LOCK_HELD(&zn
->zn_zap
->zap_rwlock
));
297 mze_tofind
.mze_hash
= zn
->zn_hash
;
298 mze_tofind
.mze_cd
= 0;
301 mze
= avl_find(avl
, &mze_tofind
, &idx
);
303 mze
= avl_nearest(avl
, idx
, AVL_AFTER
);
304 for (; mze
&& mze
->mze_hash
== zn
->zn_hash
; mze
= AVL_NEXT(avl
, mze
)) {
305 ASSERT3U(mze
->mze_cd
, ==, MZE_PHYS(zn
->zn_zap
, mze
)->mze_cd
);
306 if (zap_match(zn
, MZE_PHYS(zn
->zn_zap
, mze
)->mze_name
))
309 if (zn
->zn_matchtype
== MT_BEST
) {
310 zn
->zn_matchtype
= MT_FIRST
;
317 mze_find_unused_cd(zap_t
*zap
, uint64_t hash
)
319 mzap_ent_t mze_tofind
;
322 avl_tree_t
*avl
= &zap
->zap_m
.zap_avl
;
325 ASSERT(zap
->zap_ismicro
);
326 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
328 mze_tofind
.mze_hash
= hash
;
329 mze_tofind
.mze_cd
= 0;
332 for (mze
= avl_find(avl
, &mze_tofind
, &idx
);
333 mze
&& mze
->mze_hash
== hash
; mze
= AVL_NEXT(avl
, mze
)) {
334 if (mze
->mze_cd
!= cd
)
343 mze_remove(zap_t
*zap
, mzap_ent_t
*mze
)
345 ASSERT(zap
->zap_ismicro
);
346 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
348 avl_remove(&zap
->zap_m
.zap_avl
, mze
);
349 kmem_free(mze
, sizeof (mzap_ent_t
));
353 mze_destroy(zap_t
*zap
)
356 void *avlcookie
= NULL
;
358 while (mze
= avl_destroy_nodes(&zap
->zap_m
.zap_avl
, &avlcookie
))
359 kmem_free(mze
, sizeof (mzap_ent_t
));
360 avl_destroy(&zap
->zap_m
.zap_avl
);
364 mzap_open(objset_t
*os
, uint64_t obj
, dmu_buf_t
*db
)
370 ASSERT3U(MZAP_ENT_LEN
, ==, sizeof (mzap_ent_phys_t
));
372 zap
= kmem_zalloc(sizeof (zap_t
), KM_SLEEP
);
373 rw_init(&zap
->zap_rwlock
, 0, 0, 0);
374 rw_enter(&zap
->zap_rwlock
, RW_WRITER
);
375 zap
->zap_objset
= os
;
376 zap
->zap_object
= obj
;
379 if (*(uint64_t *)db
->db_data
!= ZBT_MICRO
) {
380 mutex_init(&zap
->zap_f
.zap_num_entries_mtx
, 0, 0, 0);
381 zap
->zap_f
.zap_block_shift
= highbit64(db
->db_size
) - 1;
383 zap
->zap_ismicro
= TRUE
;
387 * Make sure that zap_ismicro is set before we let others see
388 * it, because zap_lockdir() checks zap_ismicro without the lock
391 dmu_buf_init_user(&zap
->zap_dbu
, zap_evict
, &zap
->zap_dbuf
);
392 winner
= dmu_buf_set_user(db
, &zap
->zap_dbu
);
394 if (winner
!= NULL
) {
395 rw_exit(&zap
->zap_rwlock
);
396 rw_destroy(&zap
->zap_rwlock
);
397 if (!zap
->zap_ismicro
)
398 mutex_destroy(&zap
->zap_f
.zap_num_entries_mtx
);
399 kmem_free(zap
, sizeof (zap_t
));
403 if (zap
->zap_ismicro
) {
404 zap
->zap_salt
= zap_m_phys(zap
)->mz_salt
;
405 zap
->zap_normflags
= zap_m_phys(zap
)->mz_normflags
;
406 zap
->zap_m
.zap_num_chunks
= db
->db_size
/ MZAP_ENT_LEN
- 1;
407 avl_create(&zap
->zap_m
.zap_avl
, mze_compare
,
408 sizeof (mzap_ent_t
), offsetof(mzap_ent_t
, mze_node
));
410 for (i
= 0; i
< zap
->zap_m
.zap_num_chunks
; i
++) {
411 mzap_ent_phys_t
*mze
=
412 &zap_m_phys(zap
)->mz_chunk
[i
];
413 if (mze
->mze_name
[0]) {
416 zap
->zap_m
.zap_num_entries
++;
417 zn
= zap_name_alloc(zap
, mze
->mze_name
,
419 mze_insert(zap
, i
, zn
->zn_hash
);
424 zap
->zap_salt
= zap_f_phys(zap
)->zap_salt
;
425 zap
->zap_normflags
= zap_f_phys(zap
)->zap_normflags
;
427 ASSERT3U(sizeof (struct zap_leaf_header
), ==,
428 2*ZAP_LEAF_CHUNKSIZE
);
431 * The embedded pointer table should not overlap the
434 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0), >,
435 &zap_f_phys(zap
)->zap_salt
);
438 * The embedded pointer table should end at the end of
441 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap
,
442 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap
)) -
443 (uintptr_t)zap_f_phys(zap
), ==,
444 zap
->zap_dbuf
->db_size
);
446 rw_exit(&zap
->zap_rwlock
);
451 zap_lockdir(objset_t
*os
, uint64_t obj
, dmu_tx_t
*tx
,
452 krw_t lti
, boolean_t fatreader
, boolean_t adding
, zap_t
**zapp
)
461 err
= dmu_buf_hold(os
, obj
, 0, NULL
, &db
, DMU_READ_NO_PREFETCH
);
467 dmu_object_info_t doi
;
468 dmu_object_info_from_db(db
, &doi
);
469 ASSERT3U(DMU_OT_BYTESWAP(doi
.doi_type
), ==, DMU_BSWAP_ZAP
);
473 zap
= dmu_buf_get_user(db
);
475 zap
= mzap_open(os
, obj
, db
);
478 * We're checking zap_ismicro without the lock held, in order to
479 * tell what type of lock we want. Once we have some sort of
480 * lock, see if it really is the right type. In practice this
481 * can only be different if it was upgraded from micro to fat,
482 * and micro wanted WRITER but fat only needs READER.
484 lt
= (!zap
->zap_ismicro
&& fatreader
) ? RW_READER
: lti
;
485 rw_enter(&zap
->zap_rwlock
, lt
);
486 if (lt
!= ((!zap
->zap_ismicro
&& fatreader
) ? RW_READER
: lti
)) {
487 /* it was upgraded, now we only need reader */
488 ASSERT(lt
== RW_WRITER
);
490 (!zap
->zap_ismicro
&& fatreader
) ? RW_READER
: lti
);
491 rw_downgrade(&zap
->zap_rwlock
);
495 zap
->zap_objset
= os
;
498 dmu_buf_will_dirty(db
, tx
);
500 ASSERT3P(zap
->zap_dbuf
, ==, db
);
502 ASSERT(!zap
->zap_ismicro
||
503 zap
->zap_m
.zap_num_entries
<= zap
->zap_m
.zap_num_chunks
);
504 if (zap
->zap_ismicro
&& tx
&& adding
&&
505 zap
->zap_m
.zap_num_entries
== zap
->zap_m
.zap_num_chunks
) {
506 uint64_t newsz
= db
->db_size
+ SPA_MINBLOCKSIZE
;
507 if (newsz
> MZAP_MAX_BLKSZ
) {
508 dprintf("upgrading obj %llu: num_entries=%u\n",
509 obj
, zap
->zap_m
.zap_num_entries
);
511 return (mzap_upgrade(zapp
, tx
, 0));
513 err
= dmu_object_set_blocksize(os
, obj
, newsz
, 0, tx
);
515 zap
->zap_m
.zap_num_chunks
=
516 db
->db_size
/ MZAP_ENT_LEN
- 1;
524 zap_unlockdir(zap_t
*zap
)
526 rw_exit(&zap
->zap_rwlock
);
527 dmu_buf_rele(zap
->zap_dbuf
, NULL
);
531 mzap_upgrade(zap_t
**zapp
, dmu_tx_t
*tx
, zap_flags_t flags
)
538 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
540 sz
= zap
->zap_dbuf
->db_size
;
541 mzp
= kmem_alloc(sz
, KM_SLEEP
);
542 bcopy(zap
->zap_dbuf
->db_data
, mzp
, sz
);
543 nchunks
= zap
->zap_m
.zap_num_chunks
;
546 err
= dmu_object_set_blocksize(zap
->zap_objset
, zap
->zap_object
,
547 1ULL << fzap_default_block_shift
, 0, tx
);
554 dprintf("upgrading obj=%llu with %u chunks\n",
555 zap
->zap_object
, nchunks
);
556 /* XXX destroy the avl later, so we can use the stored hash value */
559 fzap_upgrade(zap
, tx
, flags
);
561 for (i
= 0; i
< nchunks
; i
++) {
562 mzap_ent_phys_t
*mze
= &mzp
->mz_chunk
[i
];
564 if (mze
->mze_name
[0] == 0)
566 dprintf("adding %s=%llu\n",
567 mze
->mze_name
, mze
->mze_value
);
568 zn
= zap_name_alloc(zap
, mze
->mze_name
, MT_EXACT
);
569 err
= fzap_add_cd(zn
, 8, 1, &mze
->mze_value
, mze
->mze_cd
, tx
);
570 zap
= zn
->zn_zap
; /* fzap_add_cd() may change zap */
581 mzap_create_impl(objset_t
*os
, uint64_t obj
, int normflags
, zap_flags_t flags
,
587 VERIFY(0 == dmu_buf_hold(os
, obj
, 0, FTAG
, &db
, DMU_READ_NO_PREFETCH
));
591 dmu_object_info_t doi
;
592 dmu_object_info_from_db(db
, &doi
);
593 ASSERT3U(DMU_OT_BYTESWAP(doi
.doi_type
), ==, DMU_BSWAP_ZAP
);
597 dmu_buf_will_dirty(db
, tx
);
599 zp
->mz_block_type
= ZBT_MICRO
;
600 zp
->mz_salt
= ((uintptr_t)db
^ (uintptr_t)tx
^ (obj
<< 1)) | 1ULL;
601 zp
->mz_normflags
= normflags
;
602 dmu_buf_rele(db
, FTAG
);
606 /* Only fat zap supports flags; upgrade immediately. */
607 VERIFY(0 == zap_lockdir(os
, obj
, tx
, RW_WRITER
,
608 B_FALSE
, B_FALSE
, &zap
));
609 VERIFY3U(0, ==, mzap_upgrade(&zap
, tx
, flags
));
615 zap_create_claim(objset_t
*os
, uint64_t obj
, dmu_object_type_t ot
,
616 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
618 return (zap_create_claim_norm(os
, obj
,
619 0, ot
, bonustype
, bonuslen
, tx
));
623 zap_create_claim_norm(objset_t
*os
, uint64_t obj
, int normflags
,
624 dmu_object_type_t ot
,
625 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
629 err
= dmu_object_claim(os
, obj
, ot
, 0, bonustype
, bonuslen
, tx
);
632 mzap_create_impl(os
, obj
, normflags
, 0, tx
);
637 zap_create(objset_t
*os
, dmu_object_type_t ot
,
638 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
640 return (zap_create_norm(os
, 0, ot
, bonustype
, bonuslen
, tx
));
644 zap_create_norm(objset_t
*os
, int normflags
, dmu_object_type_t ot
,
645 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
647 uint64_t obj
= dmu_object_alloc(os
, ot
, 0, bonustype
, bonuslen
, tx
);
649 mzap_create_impl(os
, obj
, normflags
, 0, tx
);
654 zap_create_flags(objset_t
*os
, int normflags
, zap_flags_t flags
,
655 dmu_object_type_t ot
, int leaf_blockshift
, int indirect_blockshift
,
656 dmu_object_type_t bonustype
, int bonuslen
, dmu_tx_t
*tx
)
658 uint64_t obj
= dmu_object_alloc(os
, ot
, 0, bonustype
, bonuslen
, tx
);
660 ASSERT(leaf_blockshift
>= SPA_MINBLOCKSHIFT
&&
661 leaf_blockshift
<= SPA_OLD_MAXBLOCKSHIFT
&&
662 indirect_blockshift
>= SPA_MINBLOCKSHIFT
&&
663 indirect_blockshift
<= SPA_OLD_MAXBLOCKSHIFT
);
665 VERIFY(dmu_object_set_blocksize(os
, obj
,
666 1ULL << leaf_blockshift
, indirect_blockshift
, tx
) == 0);
668 mzap_create_impl(os
, obj
, normflags
, flags
, tx
);
673 zap_destroy(objset_t
*os
, uint64_t zapobj
, dmu_tx_t
*tx
)
676 * dmu_object_free will free the object number and free the
677 * data. Freeing the data will cause our pageout function to be
678 * called, which will destroy our data (zap_leaf_t's and zap_t).
681 return (dmu_object_free(os
, zapobj
, tx
));
689 rw_destroy(&zap
->zap_rwlock
);
691 if (zap
->zap_ismicro
)
694 mutex_destroy(&zap
->zap_f
.zap_num_entries_mtx
);
696 kmem_free(zap
, sizeof (zap_t
));
700 zap_count(objset_t
*os
, uint64_t zapobj
, uint64_t *count
)
705 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
708 if (!zap
->zap_ismicro
) {
709 err
= fzap_count(zap
, count
);
711 *count
= zap
->zap_m
.zap_num_entries
;
718 * zn may be NULL; if not specified, it will be computed if needed.
719 * See also the comment above zap_entry_normalization_conflict().
722 mzap_normalization_conflict(zap_t
*zap
, zap_name_t
*zn
, mzap_ent_t
*mze
)
725 int direction
= AVL_BEFORE
;
726 boolean_t allocdzn
= B_FALSE
;
728 if (zap
->zap_normflags
== 0)
732 for (other
= avl_walk(&zap
->zap_m
.zap_avl
, mze
, direction
);
733 other
&& other
->mze_hash
== mze
->mze_hash
;
734 other
= avl_walk(&zap
->zap_m
.zap_avl
, other
, direction
)) {
737 zn
= zap_name_alloc(zap
, MZE_PHYS(zap
, mze
)->mze_name
,
741 if (zap_match(zn
, MZE_PHYS(zap
, other
)->mze_name
)) {
748 if (direction
== AVL_BEFORE
) {
749 direction
= AVL_AFTER
;
759 * Routines for manipulating attributes.
763 zap_lookup(objset_t
*os
, uint64_t zapobj
, const char *name
,
764 uint64_t integer_size
, uint64_t num_integers
, void *buf
)
766 return (zap_lookup_norm(os
, zapobj
, name
, integer_size
,
767 num_integers
, buf
, MT_EXACT
, NULL
, 0, NULL
));
771 zap_lookup_norm(objset_t
*os
, uint64_t zapobj
, const char *name
,
772 uint64_t integer_size
, uint64_t num_integers
, void *buf
,
773 matchtype_t mt
, char *realname
, int rn_len
,
781 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
784 zn
= zap_name_alloc(zap
, name
, mt
);
787 return (SET_ERROR(ENOTSUP
));
790 if (!zap
->zap_ismicro
) {
791 err
= fzap_lookup(zn
, integer_size
, num_integers
, buf
,
792 realname
, rn_len
, ncp
);
796 err
= SET_ERROR(ENOENT
);
798 if (num_integers
< 1) {
799 err
= SET_ERROR(EOVERFLOW
);
800 } else if (integer_size
!= 8) {
801 err
= SET_ERROR(EINVAL
);
804 MZE_PHYS(zap
, mze
)->mze_value
;
805 (void) strlcpy(realname
,
806 MZE_PHYS(zap
, mze
)->mze_name
, rn_len
);
808 *ncp
= mzap_normalization_conflict(zap
,
820 zap_prefetch_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
827 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
830 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
833 return (SET_ERROR(ENOTSUP
));
843 zap_lookup_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
844 int key_numints
, uint64_t integer_size
, uint64_t num_integers
, void *buf
)
850 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
853 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
856 return (SET_ERROR(ENOTSUP
));
859 err
= fzap_lookup(zn
, integer_size
, num_integers
, buf
,
867 zap_contains(objset_t
*os
, uint64_t zapobj
, const char *name
)
869 int err
= zap_lookup_norm(os
, zapobj
, name
, 0,
870 0, NULL
, MT_EXACT
, NULL
, 0, NULL
);
871 if (err
== EOVERFLOW
|| err
== EINVAL
)
872 err
= 0; /* found, but skipped reading the value */
877 zap_length(objset_t
*os
, uint64_t zapobj
, const char *name
,
878 uint64_t *integer_size
, uint64_t *num_integers
)
885 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
888 zn
= zap_name_alloc(zap
, name
, MT_EXACT
);
891 return (SET_ERROR(ENOTSUP
));
893 if (!zap
->zap_ismicro
) {
894 err
= fzap_length(zn
, integer_size
, num_integers
);
898 err
= SET_ERROR(ENOENT
);
912 zap_length_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
913 int key_numints
, uint64_t *integer_size
, uint64_t *num_integers
)
919 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
922 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
925 return (SET_ERROR(ENOTSUP
));
927 err
= fzap_length(zn
, integer_size
, num_integers
);
934 mzap_addent(zap_name_t
*zn
, uint64_t value
)
937 zap_t
*zap
= zn
->zn_zap
;
938 int start
= zap
->zap_m
.zap_alloc_next
;
941 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
944 for (i
= 0; i
< zap
->zap_m
.zap_num_chunks
; i
++) {
945 mzap_ent_phys_t
*mze
= &zap_m_phys(zap
)->mz_chunk
[i
];
946 ASSERT(strcmp(zn
->zn_key_orig
, mze
->mze_name
) != 0);
950 cd
= mze_find_unused_cd(zap
, zn
->zn_hash
);
951 /* given the limited size of the microzap, this can't happen */
952 ASSERT(cd
< zap_maxcd(zap
));
955 for (i
= start
; i
< zap
->zap_m
.zap_num_chunks
; i
++) {
956 mzap_ent_phys_t
*mze
= &zap_m_phys(zap
)->mz_chunk
[i
];
957 if (mze
->mze_name
[0] == 0) {
958 mze
->mze_value
= value
;
960 (void) strcpy(mze
->mze_name
, zn
->zn_key_orig
);
961 zap
->zap_m
.zap_num_entries
++;
962 zap
->zap_m
.zap_alloc_next
= i
+1;
963 if (zap
->zap_m
.zap_alloc_next
==
964 zap
->zap_m
.zap_num_chunks
)
965 zap
->zap_m
.zap_alloc_next
= 0;
966 mze_insert(zap
, i
, zn
->zn_hash
);
974 ASSERT(!"out of entries!");
978 zap_add(objset_t
*os
, uint64_t zapobj
, const char *key
,
979 int integer_size
, uint64_t num_integers
,
980 const void *val
, dmu_tx_t
*tx
)
985 const uint64_t *intval
= val
;
988 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, TRUE
, &zap
);
991 zn
= zap_name_alloc(zap
, key
, MT_EXACT
);
994 return (SET_ERROR(ENOTSUP
));
996 if (!zap
->zap_ismicro
) {
997 err
= fzap_add(zn
, integer_size
, num_integers
, val
, tx
);
998 zap
= zn
->zn_zap
; /* fzap_add() may change zap */
999 } else if (integer_size
!= 8 || num_integers
!= 1 ||
1000 strlen(key
) >= MZAP_NAME_LEN
) {
1001 err
= mzap_upgrade(&zn
->zn_zap
, tx
, 0);
1003 err
= fzap_add(zn
, integer_size
, num_integers
, val
, tx
);
1004 zap
= zn
->zn_zap
; /* fzap_add() may change zap */
1008 err
= SET_ERROR(EEXIST
);
1010 mzap_addent(zn
, *intval
);
1013 ASSERT(zap
== zn
->zn_zap
);
1015 if (zap
!= NULL
) /* may be NULL if fzap_add() failed */
1021 zap_add_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
1022 int key_numints
, int integer_size
, uint64_t num_integers
,
1023 const void *val
, dmu_tx_t
*tx
)
1029 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, TRUE
, &zap
);
1032 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
1035 return (SET_ERROR(ENOTSUP
));
1037 err
= fzap_add(zn
, integer_size
, num_integers
, val
, tx
);
1038 zap
= zn
->zn_zap
; /* fzap_add() may change zap */
1040 if (zap
!= NULL
) /* may be NULL if fzap_add() failed */
1046 zap_update(objset_t
*os
, uint64_t zapobj
, const char *name
,
1047 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
1052 const uint64_t *intval
= val
;
1058 * If there is an old value, it shouldn't change across the
1059 * lockdir (eg, due to bprewrite's xlation).
1061 if (integer_size
== 8 && num_integers
== 1)
1062 (void) zap_lookup(os
, zapobj
, name
, 8, 1, &oldval
);
1065 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, TRUE
, &zap
);
1068 zn
= zap_name_alloc(zap
, name
, MT_EXACT
);
1071 return (SET_ERROR(ENOTSUP
));
1073 if (!zap
->zap_ismicro
) {
1074 err
= fzap_update(zn
, integer_size
, num_integers
, val
, tx
);
1075 zap
= zn
->zn_zap
; /* fzap_update() may change zap */
1076 } else if (integer_size
!= 8 || num_integers
!= 1 ||
1077 strlen(name
) >= MZAP_NAME_LEN
) {
1078 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1079 zapobj
, integer_size
, num_integers
, name
);
1080 err
= mzap_upgrade(&zn
->zn_zap
, tx
, 0);
1082 err
= fzap_update(zn
, integer_size
, num_integers
,
1084 zap
= zn
->zn_zap
; /* fzap_update() may change zap */
1088 ASSERT3U(MZE_PHYS(zap
, mze
)->mze_value
, ==, oldval
);
1089 MZE_PHYS(zap
, mze
)->mze_value
= *intval
;
1091 mzap_addent(zn
, *intval
);
1094 ASSERT(zap
== zn
->zn_zap
);
1096 if (zap
!= NULL
) /* may be NULL if fzap_upgrade() failed */
1102 zap_update_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
1104 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
1110 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, TRUE
, &zap
);
1113 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
1116 return (SET_ERROR(ENOTSUP
));
1118 err
= fzap_update(zn
, integer_size
, num_integers
, val
, tx
);
1119 zap
= zn
->zn_zap
; /* fzap_update() may change zap */
1121 if (zap
!= NULL
) /* may be NULL if fzap_upgrade() failed */
1127 zap_remove(objset_t
*os
, uint64_t zapobj
, const char *name
, dmu_tx_t
*tx
)
1129 return (zap_remove_norm(os
, zapobj
, name
, MT_EXACT
, tx
));
1133 zap_remove_norm(objset_t
*os
, uint64_t zapobj
, const char *name
,
1134 matchtype_t mt
, dmu_tx_t
*tx
)
1141 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, FALSE
, &zap
);
1144 zn
= zap_name_alloc(zap
, name
, mt
);
1147 return (SET_ERROR(ENOTSUP
));
1149 if (!zap
->zap_ismicro
) {
1150 err
= fzap_remove(zn
, tx
);
1154 err
= SET_ERROR(ENOENT
);
1156 zap
->zap_m
.zap_num_entries
--;
1157 bzero(&zap_m_phys(zap
)->mz_chunk
[mze
->mze_chunkid
],
1158 sizeof (mzap_ent_phys_t
));
1159 mze_remove(zap
, mze
);
1168 zap_remove_uint64(objset_t
*os
, uint64_t zapobj
, const uint64_t *key
,
1169 int key_numints
, dmu_tx_t
*tx
)
1175 err
= zap_lockdir(os
, zapobj
, tx
, RW_WRITER
, TRUE
, FALSE
, &zap
);
1178 zn
= zap_name_alloc_uint64(zap
, key
, key_numints
);
1181 return (SET_ERROR(ENOTSUP
));
1183 err
= fzap_remove(zn
, tx
);
1190 * Routines for iterating over the attributes.
1194 zap_cursor_init_serialized(zap_cursor_t
*zc
, objset_t
*os
, uint64_t zapobj
,
1195 uint64_t serialized
)
1200 zc
->zc_zapobj
= zapobj
;
1201 zc
->zc_serialized
= serialized
;
1207 zap_cursor_init(zap_cursor_t
*zc
, objset_t
*os
, uint64_t zapobj
)
1209 zap_cursor_init_serialized(zc
, os
, zapobj
, 0);
1213 zap_cursor_fini(zap_cursor_t
*zc
)
1216 rw_enter(&zc
->zc_zap
->zap_rwlock
, RW_READER
);
1217 zap_unlockdir(zc
->zc_zap
);
1221 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1222 zap_put_leaf(zc
->zc_leaf
);
1225 zc
->zc_objset
= NULL
;
1229 zap_cursor_serialize(zap_cursor_t
*zc
)
1231 if (zc
->zc_hash
== -1ULL)
1233 if (zc
->zc_zap
== NULL
)
1234 return (zc
->zc_serialized
);
1235 ASSERT((zc
->zc_hash
& zap_maxcd(zc
->zc_zap
)) == 0);
1236 ASSERT(zc
->zc_cd
< zap_maxcd(zc
->zc_zap
));
1239 * We want to keep the high 32 bits of the cursor zero if we can, so
1240 * that 32-bit programs can access this. So usually use a small
1241 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1244 * [ collision differentiator | zap_hashbits()-bit hash value ]
1246 return ((zc
->zc_hash
>> (64 - zap_hashbits(zc
->zc_zap
))) |
1247 ((uint64_t)zc
->zc_cd
<< zap_hashbits(zc
->zc_zap
)));
1251 zap_cursor_retrieve(zap_cursor_t
*zc
, zap_attribute_t
*za
)
1255 mzap_ent_t mze_tofind
;
1258 if (zc
->zc_hash
== -1ULL)
1259 return (SET_ERROR(ENOENT
));
1261 if (zc
->zc_zap
== NULL
) {
1263 err
= zap_lockdir(zc
->zc_objset
, zc
->zc_zapobj
, NULL
,
1264 RW_READER
, TRUE
, FALSE
, &zc
->zc_zap
);
1269 * To support zap_cursor_init_serialized, advance, retrieve,
1270 * we must add to the existing zc_cd, which may already
1271 * be 1 due to the zap_cursor_advance.
1273 ASSERT(zc
->zc_hash
== 0);
1274 hb
= zap_hashbits(zc
->zc_zap
);
1275 zc
->zc_hash
= zc
->zc_serialized
<< (64 - hb
);
1276 zc
->zc_cd
+= zc
->zc_serialized
>> hb
;
1277 if (zc
->zc_cd
>= zap_maxcd(zc
->zc_zap
)) /* corrupt serialized */
1280 rw_enter(&zc
->zc_zap
->zap_rwlock
, RW_READER
);
1282 if (!zc
->zc_zap
->zap_ismicro
) {
1283 err
= fzap_cursor_retrieve(zc
->zc_zap
, zc
, za
);
1285 mze_tofind
.mze_hash
= zc
->zc_hash
;
1286 mze_tofind
.mze_cd
= zc
->zc_cd
;
1288 mze
= avl_find(&zc
->zc_zap
->zap_m
.zap_avl
, &mze_tofind
, &idx
);
1290 mze
= avl_nearest(&zc
->zc_zap
->zap_m
.zap_avl
,
1294 mzap_ent_phys_t
*mzep
= MZE_PHYS(zc
->zc_zap
, mze
);
1295 ASSERT3U(mze
->mze_cd
, ==, mzep
->mze_cd
);
1296 za
->za_normalization_conflict
=
1297 mzap_normalization_conflict(zc
->zc_zap
, NULL
, mze
);
1298 za
->za_integer_length
= 8;
1299 za
->za_num_integers
= 1;
1300 za
->za_first_integer
= mzep
->mze_value
;
1301 (void) strcpy(za
->za_name
, mzep
->mze_name
);
1302 zc
->zc_hash
= mze
->mze_hash
;
1303 zc
->zc_cd
= mze
->mze_cd
;
1306 zc
->zc_hash
= -1ULL;
1307 err
= SET_ERROR(ENOENT
);
1310 rw_exit(&zc
->zc_zap
->zap_rwlock
);
1315 zap_cursor_advance(zap_cursor_t
*zc
)
1317 if (zc
->zc_hash
== -1ULL)
1323 zap_get_stats(objset_t
*os
, uint64_t zapobj
, zap_stats_t
*zs
)
1328 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
1332 bzero(zs
, sizeof (zap_stats_t
));
1334 if (zap
->zap_ismicro
) {
1335 zs
->zs_blocksize
= zap
->zap_dbuf
->db_size
;
1336 zs
->zs_num_entries
= zap
->zap_m
.zap_num_entries
;
1337 zs
->zs_num_blocks
= 1;
1339 fzap_get_stats(zap
, zs
);
1346 zap_count_write(objset_t
*os
, uint64_t zapobj
, const char *name
, int add
,
1347 uint64_t *towrite
, uint64_t *tooverwrite
)
1353 * Since, we don't have a name, we cannot figure out which blocks will
1354 * be affected in this operation. So, account for the worst case :
1355 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1356 * - 4 new blocks written if adding:
1357 * - 2 blocks for possibly split leaves,
1358 * - 2 grown ptrtbl blocks
1360 * This also accomodates the case where an add operation to a fairly
1361 * large microzap results in a promotion to fatzap.
1364 *towrite
+= (3 + (add
? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE
;
1369 * We lock the zap with adding == FALSE. Because, if we pass
1370 * the actual value of add, it could trigger a mzap_upgrade().
1371 * At present we are just evaluating the possibility of this operation
1372 * and hence we donot want to trigger an upgrade.
1374 err
= zap_lockdir(os
, zapobj
, NULL
, RW_READER
, TRUE
, FALSE
, &zap
);
1378 if (!zap
->zap_ismicro
) {
1379 zap_name_t
*zn
= zap_name_alloc(zap
, name
, MT_EXACT
);
1381 err
= fzap_count_write(zn
, add
, towrite
,
1386 * We treat this case as similar to (name == NULL)
1388 *towrite
+= (3 + (add
? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE
;
1392 * We are here if (name != NULL) and this is a micro-zap.
1393 * We account for the header block depending on whether it
1396 * Incase of an add-operation it is hard to find out
1397 * if this add will promote this microzap to fatzap.
1398 * Hence, we consider the worst case and account for the
1399 * blocks assuming this microzap would be promoted to a
1402 * 1 block overwritten : header block
1403 * 4 new blocks written : 2 new split leaf, 2 grown
1406 if (dmu_buf_freeable(zap
->zap_dbuf
))
1407 *tooverwrite
+= MZAP_MAX_BLKSZ
;
1409 *towrite
+= MZAP_MAX_BLKSZ
;
1412 *towrite
+= 4 * MZAP_MAX_BLKSZ
;