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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains the top half of the zfs directory structure
31 * implementation. The bottom half is in zap_leaf.c.
33 * The zdir is an extendable hash data structure. There is a table of
34 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
35 * each a constant size and hold a variable number of directory entries.
36 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
38 * The pointer table holds a power of 2 number of pointers.
39 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
40 * by the pointer at index i in the table holds entries whose hash value
41 * has a zd_prefix_len - bit prefix
46 #include <sys/zfs_context.h>
47 #include <sys/zfs_znode.h>
49 #include <sys/refcount.h>
50 #include <sys/zap_impl.h>
51 #include <sys/zap_leaf.h>
52 #include <sys/zfs_znode.h>
54 int fzap_default_block_shift = 14; /* 16k blocksize */
56 static void zap_leaf_pageout(dmu_buf_t *db, void *vl);
57 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
61 fzap_byteswap(void *vbuf, size_t size)
65 block_type = *(uint64_t *)vbuf;
67 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
68 zap_leaf_byteswap(vbuf, size);
70 /* it's a ptrtbl block */
71 byteswap_uint64_array(vbuf, size);
76 fzap_upgrade(zap_t *zap, dmu_tx_t *tx)
83 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
84 zap->zap_ismicro = FALSE;
86 (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap,
87 &zap->zap_f.zap_phys, zap_evict);
89 mutex_init(&zap->zap_f.zap_num_entries_mtx, NULL, MUTEX_DEFAULT, 0);
90 zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1;
92 zp = zap->zap_f.zap_phys;
94 * explicitly zero it since it might be coming from an
95 * initialized microzap
97 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
98 zp->zap_block_type = ZBT_HEADER;
99 zp->zap_magic = ZAP_MAGIC;
101 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
103 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
104 zp->zap_num_leafs = 1;
105 zp->zap_num_entries = 0;
106 zp->zap_salt = zap->zap_salt;
107 zp->zap_normflags = zap->zap_normflags;
109 /* block 1 will be the first leaf */
110 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
111 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
114 * set up block 1 - the first leaf
116 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
117 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db));
118 dmu_buf_will_dirty(db, tx);
120 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
122 l->l_phys = db->db_data;
124 zap_leaf_init(l, zp->zap_normflags != 0);
126 kmem_free(l, sizeof (zap_leaf_t));
127 dmu_buf_rele(db, FTAG);
131 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
133 if (RW_WRITE_HELD(&zap->zap_rwlock))
135 if (rw_tryupgrade(&zap->zap_rwlock)) {
136 dmu_buf_will_dirty(zap->zap_dbuf, tx);
143 * Generic routines for dealing with the pointer & cookie tables.
147 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
148 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
152 dmu_buf_t *db_old, *db_new;
154 int bs = FZAP_BLOCK_SHIFT(zap);
155 int hepb = 1<<(bs-4);
156 /* hepb = half the number of entries in a block */
158 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
159 ASSERT(tbl->zt_blk != 0);
160 ASSERT(tbl->zt_numblks > 0);
162 if (tbl->zt_nextblk != 0) {
163 newblk = tbl->zt_nextblk;
165 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
166 tbl->zt_nextblk = newblk;
167 ASSERT3U(tbl->zt_blks_copied, ==, 0);
168 dmu_prefetch(zap->zap_objset, zap->zap_object,
169 tbl->zt_blk << bs, tbl->zt_numblks << bs);
173 * Copy the ptrtbl from the old to new location.
176 b = tbl->zt_blks_copied;
177 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
178 (tbl->zt_blk + b) << bs, FTAG, &db_old);
182 /* first half of entries in old[b] go to new[2*b+0] */
183 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
184 (newblk + 2*b+0) << bs, FTAG, &db_new));
185 dmu_buf_will_dirty(db_new, tx);
186 transfer_func(db_old->db_data, db_new->db_data, hepb);
187 dmu_buf_rele(db_new, FTAG);
189 /* second half of entries in old[b] go to new[2*b+1] */
190 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
191 (newblk + 2*b+1) << bs, FTAG, &db_new));
192 dmu_buf_will_dirty(db_new, tx);
193 transfer_func((uint64_t *)db_old->db_data + hepb,
194 db_new->db_data, hepb);
195 dmu_buf_rele(db_new, FTAG);
197 dmu_buf_rele(db_old, FTAG);
199 tbl->zt_blks_copied++;
201 dprintf("copied block %llu of %llu\n",
202 tbl->zt_blks_copied, tbl->zt_numblks);
204 if (tbl->zt_blks_copied == tbl->zt_numblks) {
205 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
206 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
208 tbl->zt_blk = newblk;
209 tbl->zt_numblks *= 2;
212 tbl->zt_blks_copied = 0;
214 dprintf("finished; numblocks now %llu (%lluk entries)\n",
215 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
222 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
227 int bs = FZAP_BLOCK_SHIFT(zap);
230 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
231 ASSERT(tbl->zt_blk != 0);
233 dprintf("storing %llx at index %llx\n", val, idx);
236 off = idx & ((1<<(bs-3))-1);
238 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
239 (tbl->zt_blk + blk) << bs, FTAG, &db);
242 dmu_buf_will_dirty(db, tx);
244 if (tbl->zt_nextblk != 0) {
245 uint64_t idx2 = idx * 2;
246 uint64_t blk2 = idx2 >> (bs-3);
247 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
250 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
251 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2);
253 dmu_buf_rele(db, FTAG);
256 dmu_buf_will_dirty(db2, tx);
257 ((uint64_t *)db2->db_data)[off2] = val;
258 ((uint64_t *)db2->db_data)[off2+1] = val;
259 dmu_buf_rele(db2, FTAG);
262 ((uint64_t *)db->db_data)[off] = val;
263 dmu_buf_rele(db, FTAG);
269 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
274 int bs = FZAP_BLOCK_SHIFT(zap);
276 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
279 off = idx & ((1<<(bs-3))-1);
281 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
282 (tbl->zt_blk + blk) << bs, FTAG, &db);
285 *valp = ((uint64_t *)db->db_data)[off];
286 dmu_buf_rele(db, FTAG);
288 if (tbl->zt_nextblk != 0) {
290 * read the nextblk for the sake of i/o error checking,
291 * so that zap_table_load() will catch errors for
294 blk = (idx*2) >> (bs-3);
296 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
297 (tbl->zt_nextblk + blk) << bs, FTAG, &db);
298 dmu_buf_rele(db, FTAG);
304 * Routines for growing the ptrtbl.
308 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
311 for (i = 0; i < n; i++) {
312 uint64_t lb = src[i];
319 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
321 /* In case things go horribly wrong. */
322 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift >= ZAP_HASHBITS-2)
325 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
327 * We are outgrowing the "embedded" ptrtbl (the one
328 * stored in the header block). Give it its own entire
329 * block, which will double the size of the ptrtbl.
335 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
336 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
337 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_blk, ==, 0);
339 newblk = zap_allocate_blocks(zap, 1);
340 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
341 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new);
344 dmu_buf_will_dirty(db_new, tx);
345 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
346 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
347 dmu_buf_rele(db_new, FTAG);
349 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk = newblk;
350 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks = 1;
351 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift++;
353 ASSERT3U(1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
354 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks <<
355 (FZAP_BLOCK_SHIFT(zap)-3));
359 return (zap_table_grow(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
360 zap_ptrtbl_transfer, tx));
365 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
367 dmu_buf_will_dirty(zap->zap_dbuf, tx);
368 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
369 ASSERT(delta > 0 || zap->zap_f.zap_phys->zap_num_entries >= -delta);
370 zap->zap_f.zap_phys->zap_num_entries += delta;
371 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
375 zap_allocate_blocks(zap_t *zap, int nblocks)
378 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
379 newblk = zap->zap_f.zap_phys->zap_freeblk;
380 zap->zap_f.zap_phys->zap_freeblk += nblocks;
385 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
388 zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
390 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
392 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0);
393 rw_enter(&l->l_rwlock, RW_WRITER);
394 l->l_blkid = zap_allocate_blocks(zap, 1);
398 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
399 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf));
400 winner = dmu_buf_set_user(l->l_dbuf, l, &l->l_phys, zap_leaf_pageout);
401 ASSERT(winner == NULL);
402 dmu_buf_will_dirty(l->l_dbuf, tx);
404 zap_leaf_init(l, zap->zap_normflags != 0);
406 zap->zap_f.zap_phys->zap_num_leafs++;
412 fzap_count(zap_t *zap, uint64_t *count)
414 ASSERT(!zap->zap_ismicro);
415 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
416 *count = zap->zap_f.zap_phys->zap_num_entries;
417 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
422 * Routines for obtaining zap_leaf_t's
426 zap_put_leaf(zap_leaf_t *l)
428 rw_exit(&l->l_rwlock);
429 dmu_buf_rele(l->l_dbuf, NULL);
434 zap_leaf_pageout(dmu_buf_t *db, void *vl)
438 rw_destroy(&l->l_rwlock);
439 kmem_free(l, sizeof (zap_leaf_t));
443 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
445 zap_leaf_t *l, *winner;
449 l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
450 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0);
451 rw_enter(&l->l_rwlock, RW_WRITER);
453 l->l_bs = highbit(db->db_size)-1;
457 winner = dmu_buf_set_user(db, l, &l->l_phys, zap_leaf_pageout);
459 rw_exit(&l->l_rwlock);
460 if (winner != NULL) {
461 /* someone else set it first */
462 zap_leaf_pageout(NULL, l);
467 * lhr_pad was previously used for the next leaf in the leaf
468 * chain. There should be no chained leafs (as we have removed
471 ASSERT3U(l->l_phys->l_hdr.lh_pad1, ==, 0);
474 * There should be more hash entries than there can be
475 * chunks to put in the hash table
477 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
479 /* The chunks should begin at the end of the hash table */
480 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
481 &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
483 /* The chunks should end at the end of the block */
484 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
485 (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size);
491 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
496 int bs = FZAP_BLOCK_SHIFT(zap);
499 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
501 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
502 blkid << bs, NULL, &db);
506 ASSERT3U(db->db_object, ==, zap->zap_object);
507 ASSERT3U(db->db_offset, ==, blkid << bs);
508 ASSERT3U(db->db_size, ==, 1 << bs);
511 l = dmu_buf_get_user(db);
514 l = zap_open_leaf(blkid, db);
516 rw_enter(&l->l_rwlock, lt);
518 * Must lock before dirtying, otherwise l->l_phys could change,
519 * causing ASSERT below to fail.
522 dmu_buf_will_dirty(db, tx);
523 ASSERT3U(l->l_blkid, ==, blkid);
524 ASSERT3P(l->l_dbuf, ==, db);
525 ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data);
526 ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF);
527 ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
534 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
536 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
538 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
540 (1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift));
541 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
544 return (zap_table_load(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
550 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
553 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
555 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0) {
556 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
559 return (zap_table_store(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
565 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
570 ASSERT(zap->zap_dbuf == NULL ||
571 zap->zap_f.zap_phys == zap->zap_dbuf->db_data);
572 ASSERT3U(zap->zap_f.zap_phys->zap_magic, ==, ZAP_MAGIC);
573 idx = ZAP_HASH_IDX(h, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
574 err = zap_idx_to_blk(zap, idx, &blk);
577 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
579 ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) ==
580 (*lp)->l_phys->l_hdr.lh_prefix);
585 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp)
587 zap_t *zap = zn->zn_zap;
588 uint64_t hash = zn->zn_hash;
590 int prefix_diff, i, err;
592 int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
594 ASSERT3U(old_prefix_len, <=, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
595 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
597 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
598 l->l_phys->l_hdr.lh_prefix);
600 if (zap_tryupgradedir(zap, tx) == 0 ||
601 old_prefix_len == zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
602 /* We failed to upgrade, or need to grow the pointer table */
603 objset_t *os = zap->zap_objset;
604 uint64_t object = zap->zap_object;
608 err = zap_lockdir(os, object, tx, RW_WRITER,
609 FALSE, FALSE, &zn->zn_zap);
613 ASSERT(!zap->zap_ismicro);
615 while (old_prefix_len ==
616 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
617 err = zap_grow_ptrtbl(zap, tx);
622 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
626 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) {
627 /* it split while our locks were down */
632 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
633 ASSERT3U(old_prefix_len, <, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
634 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
635 l->l_phys->l_hdr.lh_prefix);
637 prefix_diff = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift -
638 (old_prefix_len + 1);
639 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
641 /* check for i/o errors before doing zap_leaf_split */
642 for (i = 0; i < (1ULL<<prefix_diff); i++) {
644 err = zap_idx_to_blk(zap, sibling+i, &blk);
647 ASSERT3U(blk, ==, l->l_blkid);
650 nl = zap_create_leaf(zap, tx);
651 zap_leaf_split(l, nl, zap->zap_normflags != 0);
653 /* set sibling pointers */
654 for (i = 0; i < (1ULL<<prefix_diff); i++) {
655 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
656 ASSERT3U(err, ==, 0); /* we checked for i/o errors above */
659 if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) {
660 /* we want the sibling */
672 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx)
674 zap_t *zap = zn->zn_zap;
675 int shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
676 int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift &&
677 l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
681 if (leaffull || zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk) {
685 * We are in the middle of growing the pointer table, or
686 * this leaf will soon make us grow it.
688 if (zap_tryupgradedir(zap, tx) == 0) {
689 objset_t *os = zap->zap_objset;
690 uint64_t zapobj = zap->zap_object;
693 err = zap_lockdir(os, zapobj, tx,
694 RW_WRITER, FALSE, FALSE, &zn->zn_zap);
700 /* could have finished growing while our locks were down */
701 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift == shift)
702 (void) zap_grow_ptrtbl(zap, tx);
708 fzap_checksize(const char *name, uint64_t integer_size, uint64_t num_integers)
710 if (name && strlen(name) > ZAP_MAXNAMELEN)
713 /* Only integer sizes supported by C */
714 switch (integer_size) {
724 if (integer_size * num_integers > ZAP_MAXVALUELEN)
731 * Routines for manipulating attributes.
734 fzap_lookup(zap_name_t *zn,
735 uint64_t integer_size, uint64_t num_integers, void *buf,
736 char *realname, int rn_len, boolean_t *ncp)
740 zap_entry_handle_t zeh;
742 err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers);
746 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
749 err = zap_leaf_lookup(l, zn, &zeh);
751 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
752 (void) zap_entry_read_name(&zeh, rn_len, realname);
754 *ncp = zap_entry_normalization_conflict(&zeh,
755 zn, NULL, zn->zn_zap);
764 fzap_add_cd(zap_name_t *zn,
765 uint64_t integer_size, uint64_t num_integers,
766 const void *val, uint32_t cd, dmu_tx_t *tx)
770 zap_entry_handle_t zeh;
771 zap_t *zap = zn->zn_zap;
773 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
774 ASSERT(!zap->zap_ismicro);
775 ASSERT(fzap_checksize(zn->zn_name_orij,
776 integer_size, num_integers) == 0);
778 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
782 err = zap_leaf_lookup(l, zn, &zeh);
790 err = zap_entry_create(l, zn->zn_name_orij, zn->zn_hash, cd,
791 integer_size, num_integers, val, &zeh);
794 zap_increment_num_entries(zap, 1, tx);
795 } else if (err == EAGAIN) {
796 err = zap_expand_leaf(zn, l, tx, &l);
797 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
804 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
809 fzap_add(zap_name_t *zn,
810 uint64_t integer_size, uint64_t num_integers,
811 const void *val, dmu_tx_t *tx)
813 int err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers);
817 return (fzap_add_cd(zn, integer_size, num_integers,
818 val, ZAP_MAXCD, tx));
822 fzap_update(zap_name_t *zn,
823 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
827 zap_entry_handle_t zeh;
828 zap_t *zap = zn->zn_zap;
830 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
831 err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers);
835 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
839 err = zap_leaf_lookup(l, zn, &zeh);
840 create = (err == ENOENT);
841 ASSERT(err == 0 || err == ENOENT);
844 err = zap_entry_create(l, zn->zn_name_orij, zn->zn_hash,
845 ZAP_MAXCD, integer_size, num_integers, val, &zeh);
847 zap_increment_num_entries(zap, 1, tx);
849 err = zap_entry_update(&zeh, integer_size, num_integers, val);
853 err = zap_expand_leaf(zn, l, tx, &l);
854 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
860 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
865 fzap_length(zap_name_t *zn,
866 uint64_t *integer_size, uint64_t *num_integers)
870 zap_entry_handle_t zeh;
872 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
875 err = zap_leaf_lookup(l, zn, &zeh);
880 *integer_size = zeh.zeh_integer_size;
882 *num_integers = zeh.zeh_num_integers;
889 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
893 zap_entry_handle_t zeh;
895 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
898 err = zap_leaf_lookup(l, zn, &zeh);
900 zap_entry_remove(&zeh);
901 zap_increment_num_entries(zn->zn_zap, -1, tx);
908 * Helper functions for consumers.
912 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
922 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
923 for (zap_cursor_init(&zc, os, zapobj);
924 (err = zap_cursor_retrieve(&zc, za)) == 0;
925 zap_cursor_advance(&zc)) {
926 if ((za->za_first_integer & mask) == (value & mask)) {
927 (void) strcpy(name, za->za_name);
931 zap_cursor_fini(&zc);
932 kmem_free(za, sizeof (zap_attribute_t));
937 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
943 for (zap_cursor_init(&zc, os, fromobj);
944 zap_cursor_retrieve(&zc, &za) == 0;
945 (void) zap_cursor_advance(&zc)) {
946 if (za.za_integer_length != 8 || za.za_num_integers != 1)
948 err = zap_add(os, intoobj, za.za_name,
949 8, 1, &za.za_first_integer, tx);
953 zap_cursor_fini(&zc);
958 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
962 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
963 return (zap_add(os, obj, name, 8, 1, &value, tx));
967 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
971 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
972 return (zap_remove(os, obj, name, tx));
976 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
980 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
981 return (zap_lookup(os, obj, name, 8, 1, &value));
985 * Routines for iterating over the attributes.
989 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
992 zap_entry_handle_t zeh;
995 /* retrieve the next entry at or after zc_hash/zc_cd */
996 /* if no entry, return ENOENT */
999 (ZAP_HASH_IDX(zc->zc_hash,
1000 zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) !=
1001 zc->zc_leaf->l_phys->l_hdr.lh_prefix)) {
1002 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1003 zap_put_leaf(zc->zc_leaf);
1008 if (zc->zc_leaf == NULL) {
1009 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1014 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1018 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1020 if (err == ENOENT) {
1022 (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1;
1023 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1025 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) {
1026 zc->zc_hash = -1ULL;
1028 zap_put_leaf(zc->zc_leaf);
1035 zc->zc_hash = zeh.zeh_hash;
1036 zc->zc_cd = zeh.zeh_cd;
1037 za->za_integer_length = zeh.zeh_integer_size;
1038 za->za_num_integers = zeh.zeh_num_integers;
1039 if (zeh.zeh_num_integers == 0) {
1040 za->za_first_integer = 0;
1042 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1043 ASSERT(err == 0 || err == EOVERFLOW);
1045 err = zap_entry_read_name(&zeh,
1046 sizeof (za->za_name), za->za_name);
1049 za->za_normalization_conflict =
1050 zap_entry_normalization_conflict(&zeh,
1051 NULL, za->za_name, zap);
1053 rw_exit(&zc->zc_leaf->l_rwlock);
1059 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1062 uint64_t lastblk = 0;
1065 * NB: if a leaf has more pointers than an entire ptrtbl block
1066 * can hold, then it'll be accounted for more than once, since
1067 * we won't have lastblk.
1069 for (i = 0; i < len; i++) {
1072 if (tbl[i] == lastblk)
1076 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1078 zap_leaf_stats(zap, l, zs);
1085 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1087 int bs = FZAP_BLOCK_SHIFT(zap);
1088 zs->zs_blocksize = 1ULL << bs;
1091 * Set zap_phys_t fields
1093 zs->zs_num_leafs = zap->zap_f.zap_phys->zap_num_leafs;
1094 zs->zs_num_entries = zap->zap_f.zap_phys->zap_num_entries;
1095 zs->zs_num_blocks = zap->zap_f.zap_phys->zap_freeblk;
1096 zs->zs_block_type = zap->zap_f.zap_phys->zap_block_type;
1097 zs->zs_magic = zap->zap_f.zap_phys->zap_magic;
1098 zs->zs_salt = zap->zap_f.zap_phys->zap_salt;
1101 * Set zap_ptrtbl fields
1103 zs->zs_ptrtbl_len = 1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1104 zs->zs_ptrtbl_nextblk = zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk;
1105 zs->zs_ptrtbl_blks_copied =
1106 zap->zap_f.zap_phys->zap_ptrtbl.zt_blks_copied;
1107 zs->zs_ptrtbl_zt_blk = zap->zap_f.zap_phys->zap_ptrtbl.zt_blk;
1108 zs->zs_ptrtbl_zt_numblks = zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1109 zs->zs_ptrtbl_zt_shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1111 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
1112 /* the ptrtbl is entirely in the header block. */
1113 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1114 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1118 dmu_prefetch(zap->zap_objset, zap->zap_object,
1119 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk << bs,
1120 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << bs);
1122 for (b = 0; b < zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1127 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1128 (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk + b) << bs,
1131 zap_stats_ptrtbl(zap, db->db_data,
1133 dmu_buf_rele(db, FTAG);