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 * 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>
48 #include <sys/refcount.h>
49 #include <sys/zap_impl.h>
50 #include <sys/zap_leaf.h>
51 #include <sys/zfs_znode.h>
53 int fzap_default_block_shift = 14; /* 16k blocksize */
55 static void zap_leaf_pageout(dmu_buf_t *db, void *vl);
56 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
60 fzap_byteswap(void *vbuf, size_t size)
64 block_type = *(uint64_t *)vbuf;
66 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
67 zap_leaf_byteswap(vbuf, size);
69 /* it's a ptrtbl block */
70 byteswap_uint64_array(vbuf, size);
75 fzap_upgrade(zap_t *zap, dmu_tx_t *tx)
82 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
83 zap->zap_ismicro = FALSE;
85 (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap,
86 &zap->zap_f.zap_phys, zap_evict);
88 mutex_init(&zap->zap_f.zap_num_entries_mtx, NULL, MUTEX_DEFAULT, 0);
89 zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1;
91 zp = zap->zap_f.zap_phys;
93 * explicitly zero it since it might be coming from an
94 * initialized microzap
96 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
97 zp->zap_block_type = ZBT_HEADER;
98 zp->zap_magic = ZAP_MAGIC;
100 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
102 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
103 zp->zap_num_leafs = 1;
104 zp->zap_num_entries = 0;
105 zp->zap_salt = zap->zap_salt;
107 /* block 1 will be the first leaf */
108 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
109 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
112 * set up block 1 - the first leaf
114 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
115 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db));
116 dmu_buf_will_dirty(db, tx);
118 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
120 l->l_phys = db->db_data;
124 kmem_free(l, sizeof (zap_leaf_t));
125 dmu_buf_rele(db, FTAG);
129 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
131 if (RW_WRITE_HELD(&zap->zap_rwlock))
133 if (rw_tryupgrade(&zap->zap_rwlock)) {
134 dmu_buf_will_dirty(zap->zap_dbuf, tx);
141 * Generic routines for dealing with the pointer & cookie tables.
145 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
146 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
150 dmu_buf_t *db_old, *db_new;
152 int bs = FZAP_BLOCK_SHIFT(zap);
153 int hepb = 1<<(bs-4);
154 /* hepb = half the number of entries in a block */
156 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
157 ASSERT(tbl->zt_blk != 0);
158 ASSERT(tbl->zt_numblks > 0);
160 if (tbl->zt_nextblk != 0) {
161 newblk = tbl->zt_nextblk;
163 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
164 tbl->zt_nextblk = newblk;
165 ASSERT3U(tbl->zt_blks_copied, ==, 0);
166 dmu_prefetch(zap->zap_objset, zap->zap_object,
167 tbl->zt_blk << bs, tbl->zt_numblks << bs);
171 * Copy the ptrtbl from the old to new location.
174 b = tbl->zt_blks_copied;
175 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
176 (tbl->zt_blk + b) << bs, FTAG, &db_old);
180 /* first half of entries in old[b] go to new[2*b+0] */
181 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
182 (newblk + 2*b+0) << bs, FTAG, &db_new));
183 dmu_buf_will_dirty(db_new, tx);
184 transfer_func(db_old->db_data, db_new->db_data, hepb);
185 dmu_buf_rele(db_new, FTAG);
187 /* second half of entries in old[b] go to new[2*b+1] */
188 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
189 (newblk + 2*b+1) << bs, FTAG, &db_new));
190 dmu_buf_will_dirty(db_new, tx);
191 transfer_func((uint64_t *)db_old->db_data + hepb,
192 db_new->db_data, hepb);
193 dmu_buf_rele(db_new, FTAG);
195 dmu_buf_rele(db_old, FTAG);
197 tbl->zt_blks_copied++;
199 dprintf("copied block %llu of %llu\n",
200 tbl->zt_blks_copied, tbl->zt_numblks);
202 if (tbl->zt_blks_copied == tbl->zt_numblks) {
203 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
204 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
206 tbl->zt_blk = newblk;
207 tbl->zt_numblks *= 2;
210 tbl->zt_blks_copied = 0;
212 dprintf("finished; numblocks now %llu (%lluk entries)\n",
213 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
220 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
225 int bs = FZAP_BLOCK_SHIFT(zap);
228 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
229 ASSERT(tbl->zt_blk != 0);
231 dprintf("storing %llx at index %llx\n", val, idx);
234 off = idx & ((1<<(bs-3))-1);
236 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
237 (tbl->zt_blk + blk) << bs, FTAG, &db);
240 dmu_buf_will_dirty(db, tx);
242 if (tbl->zt_nextblk != 0) {
243 uint64_t idx2 = idx * 2;
244 uint64_t blk2 = idx2 >> (bs-3);
245 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
248 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
249 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2);
251 dmu_buf_rele(db, FTAG);
254 dmu_buf_will_dirty(db2, tx);
255 ((uint64_t *)db2->db_data)[off2] = val;
256 ((uint64_t *)db2->db_data)[off2+1] = val;
257 dmu_buf_rele(db2, FTAG);
260 ((uint64_t *)db->db_data)[off] = val;
261 dmu_buf_rele(db, FTAG);
267 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
272 int bs = FZAP_BLOCK_SHIFT(zap);
274 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
277 off = idx & ((1<<(bs-3))-1);
279 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
280 (tbl->zt_blk + blk) << bs, FTAG, &db);
283 *valp = ((uint64_t *)db->db_data)[off];
284 dmu_buf_rele(db, FTAG);
286 if (tbl->zt_nextblk != 0) {
288 * read the nextblk for the sake of i/o error checking,
289 * so that zap_table_load() will catch errors for
292 blk = (idx*2) >> (bs-3);
294 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
295 (tbl->zt_nextblk + blk) << bs, FTAG, &db);
296 dmu_buf_rele(db, FTAG);
302 * Routines for growing the ptrtbl.
306 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
309 for (i = 0; i < n; i++) {
310 uint64_t lb = src[i];
317 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
319 /* In case things go horribly wrong. */
320 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift >= ZAP_HASHBITS-2)
323 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
325 * We are outgrowing the "embedded" ptrtbl (the one
326 * stored in the header block). Give it its own entire
327 * block, which will double the size of the ptrtbl.
333 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
334 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
335 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_blk, ==, 0);
337 newblk = zap_allocate_blocks(zap, 1);
338 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
339 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new);
342 dmu_buf_will_dirty(db_new, tx);
343 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
344 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
345 dmu_buf_rele(db_new, FTAG);
347 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk = newblk;
348 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks = 1;
349 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift++;
351 ASSERT3U(1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
352 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks <<
353 (FZAP_BLOCK_SHIFT(zap)-3));
357 return (zap_table_grow(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
358 zap_ptrtbl_transfer, tx));
363 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
365 dmu_buf_will_dirty(zap->zap_dbuf, tx);
366 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
367 ASSERT(delta > 0 || zap->zap_f.zap_phys->zap_num_entries >= -delta);
368 zap->zap_f.zap_phys->zap_num_entries += delta;
369 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
373 zap_allocate_blocks(zap_t *zap, int nblocks)
376 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
377 newblk = zap->zap_f.zap_phys->zap_freeblk;
378 zap->zap_f.zap_phys->zap_freeblk += nblocks;
383 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
386 zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
388 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
390 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0);
391 rw_enter(&l->l_rwlock, RW_WRITER);
392 l->l_blkid = zap_allocate_blocks(zap, 1);
396 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
397 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf));
398 winner = dmu_buf_set_user(l->l_dbuf, l, &l->l_phys, zap_leaf_pageout);
399 ASSERT(winner == NULL);
400 dmu_buf_will_dirty(l->l_dbuf, tx);
404 zap->zap_f.zap_phys->zap_num_leafs++;
410 fzap_count(zap_t *zap, uint64_t *count)
412 ASSERT(!zap->zap_ismicro);
413 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
414 *count = zap->zap_f.zap_phys->zap_num_entries;
415 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
420 * Routines for obtaining zap_leaf_t's
424 zap_put_leaf(zap_leaf_t *l)
426 rw_exit(&l->l_rwlock);
427 dmu_buf_rele(l->l_dbuf, NULL);
432 zap_leaf_pageout(dmu_buf_t *db, void *vl)
436 rw_destroy(&l->l_rwlock);
437 kmem_free(l, sizeof (zap_leaf_t));
441 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
443 zap_leaf_t *l, *winner;
447 l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
448 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0);
449 rw_enter(&l->l_rwlock, RW_WRITER);
451 l->l_bs = highbit(db->db_size)-1;
455 winner = dmu_buf_set_user(db, l, &l->l_phys, zap_leaf_pageout);
457 rw_exit(&l->l_rwlock);
458 if (winner != NULL) {
459 /* someone else set it first */
460 zap_leaf_pageout(NULL, l);
465 * lhr_pad was previously used for the next leaf in the leaf
466 * chain. There should be no chained leafs (as we have removed
469 ASSERT3U(l->l_phys->l_hdr.lh_pad1, ==, 0);
472 * There should be more hash entries than there can be
473 * chunks to put in the hash table
475 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
477 /* The chunks should begin at the end of the hash table */
478 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
479 &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
481 /* The chunks should end at the end of the block */
482 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
483 (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size);
489 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
494 int bs = FZAP_BLOCK_SHIFT(zap);
497 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
499 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
500 blkid << bs, NULL, &db);
504 ASSERT3U(db->db_object, ==, zap->zap_object);
505 ASSERT3U(db->db_offset, ==, blkid << bs);
506 ASSERT3U(db->db_size, ==, 1 << bs);
509 l = dmu_buf_get_user(db);
512 l = zap_open_leaf(blkid, db);
514 rw_enter(&l->l_rwlock, lt);
516 * Must lock before dirtying, otherwise l->l_phys could change,
517 * causing ASSERT below to fail.
520 dmu_buf_will_dirty(db, tx);
521 ASSERT3U(l->l_blkid, ==, blkid);
522 ASSERT3P(l->l_dbuf, ==, db);
523 ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data);
524 ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF);
525 ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
532 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
534 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
536 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
538 (1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift));
539 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
542 return (zap_table_load(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
548 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
551 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
553 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0) {
554 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
557 return (zap_table_store(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
563 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
568 ASSERT(zap->zap_dbuf == NULL ||
569 zap->zap_f.zap_phys == zap->zap_dbuf->db_data);
570 ASSERT3U(zap->zap_f.zap_phys->zap_magic, ==, ZAP_MAGIC);
571 idx = ZAP_HASH_IDX(h, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
572 err = zap_idx_to_blk(zap, idx, &blk);
575 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
577 ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) ==
578 (*lp)->l_phys->l_hdr.lh_prefix);
583 zap_expand_leaf(zap_t *zap, zap_leaf_t *l, uint64_t hash, dmu_tx_t *tx,
587 int prefix_diff, i, err;
589 int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
591 ASSERT3U(old_prefix_len, <=, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
592 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
594 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
595 l->l_phys->l_hdr.lh_prefix);
597 if (zap_tryupgradedir(zap, tx) == 0 ||
598 old_prefix_len == zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
599 /* We failed to upgrade, or need to grow the pointer table */
600 objset_t *os = zap->zap_objset;
601 uint64_t object = zap->zap_object;
605 err = zap_lockdir(os, object, tx, RW_WRITER, FALSE, &zap);
608 ASSERT(!zap->zap_ismicro);
610 while (old_prefix_len ==
611 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
612 err = zap_grow_ptrtbl(zap, tx);
617 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
621 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) {
622 /* it split while our locks were down */
627 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
628 ASSERT3U(old_prefix_len, <, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
629 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
630 l->l_phys->l_hdr.lh_prefix);
632 prefix_diff = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift -
633 (old_prefix_len + 1);
634 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
636 /* check for i/o errors before doing zap_leaf_split */
637 for (i = 0; i < (1ULL<<prefix_diff); i++) {
639 err = zap_idx_to_blk(zap, sibling+i, &blk);
642 ASSERT3U(blk, ==, l->l_blkid);
645 nl = zap_create_leaf(zap, tx);
646 zap_leaf_split(l, nl);
648 /* set sibling pointers */
649 for (i = 0; i < (1ULL<<prefix_diff); i++) {
650 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
651 ASSERT3U(err, ==, 0); /* we checked for i/o errors above */
654 if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) {
655 /* we want the sibling */
667 zap_put_leaf_maybe_grow_ptrtbl(zap_t *zap, zap_leaf_t *l, dmu_tx_t *tx)
669 int shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
670 int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift &&
671 l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
675 if (leaffull || zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk) {
679 * We are in the middle of growing the pointer table, or
680 * this leaf will soon make us grow it.
682 if (zap_tryupgradedir(zap, tx) == 0) {
683 objset_t *os = zap->zap_objset;
684 uint64_t zapobj = zap->zap_object;
687 err = zap_lockdir(os, zapobj, tx,
688 RW_WRITER, FALSE, &zap);
693 /* could have finished growing while our locks were down */
694 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift == shift)
695 (void) zap_grow_ptrtbl(zap, tx);
701 fzap_checksize(const char *name, uint64_t integer_size, uint64_t num_integers)
703 if (name && strlen(name) > ZAP_MAXNAMELEN)
706 /* Only integer sizes supported by C */
707 switch (integer_size) {
717 if (integer_size * num_integers > ZAP_MAXVALUELEN)
724 * Routines for maniplulating attributes.
727 fzap_lookup(zap_t *zap, const char *name,
728 uint64_t integer_size, uint64_t num_integers, void *buf)
733 zap_entry_handle_t zeh;
735 err = fzap_checksize(name, integer_size, num_integers);
739 hash = zap_hash(zap, name);
740 err = zap_deref_leaf(zap, hash, NULL, RW_READER, &l);
743 err = zap_leaf_lookup(l, name, hash, &zeh);
745 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
752 fzap_add_cd(zap_t *zap, const char *name,
753 uint64_t integer_size, uint64_t num_integers,
754 const void *val, uint32_t cd, dmu_tx_t *tx)
759 zap_entry_handle_t zeh;
761 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
762 ASSERT(!zap->zap_ismicro);
763 ASSERT(fzap_checksize(name, integer_size, num_integers) == 0);
765 hash = zap_hash(zap, name);
766 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
770 err = zap_leaf_lookup(l, name, hash, &zeh);
778 err = zap_entry_create(l, name, hash, cd,
779 integer_size, num_integers, val, &zeh);
782 zap_increment_num_entries(zap, 1, tx);
783 } else if (err == EAGAIN) {
784 err = zap_expand_leaf(zap, l, hash, tx, &l);
790 zap_put_leaf_maybe_grow_ptrtbl(zap, l, tx);
795 fzap_add(zap_t *zap, const char *name,
796 uint64_t integer_size, uint64_t num_integers,
797 const void *val, dmu_tx_t *tx)
799 int err = fzap_checksize(name, integer_size, num_integers);
803 return (fzap_add_cd(zap, name, integer_size, num_integers,
804 val, ZAP_MAXCD, tx));
808 fzap_update(zap_t *zap, const char *name,
809 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
814 zap_entry_handle_t zeh;
816 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
817 err = fzap_checksize(name, integer_size, num_integers);
821 hash = zap_hash(zap, name);
822 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
826 err = zap_leaf_lookup(l, name, hash, &zeh);
827 create = (err == ENOENT);
828 ASSERT(err == 0 || err == ENOENT);
830 /* XXX If this leaf is chained, split it if we can. */
833 err = zap_entry_create(l, name, hash, ZAP_MAXCD,
834 integer_size, num_integers, val, &zeh);
836 zap_increment_num_entries(zap, 1, tx);
838 err = zap_entry_update(&zeh, integer_size, num_integers, val);
842 err = zap_expand_leaf(zap, l, hash, tx, &l);
847 zap_put_leaf_maybe_grow_ptrtbl(zap, l, tx);
852 fzap_length(zap_t *zap, const char *name,
853 uint64_t *integer_size, uint64_t *num_integers)
858 zap_entry_handle_t zeh;
860 hash = zap_hash(zap, name);
861 err = zap_deref_leaf(zap, hash, NULL, RW_READER, &l);
864 err = zap_leaf_lookup(l, name, hash, &zeh);
869 *integer_size = zeh.zeh_integer_size;
871 *num_integers = zeh.zeh_num_integers;
878 fzap_remove(zap_t *zap, const char *name, dmu_tx_t *tx)
883 zap_entry_handle_t zeh;
885 hash = zap_hash(zap, name);
886 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
889 err = zap_leaf_lookup(l, name, hash, &zeh);
891 zap_entry_remove(&zeh);
892 zap_increment_num_entries(zap, -1, tx);
895 dprintf("fzap_remove: ds=%p obj=%llu name=%s err=%d\n",
896 zap->zap_objset, zap->zap_object, name, err);
901 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, char *name)
907 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
908 for (zap_cursor_init(&zc, os, zapobj);
909 (err = zap_cursor_retrieve(&zc, za)) == 0;
910 zap_cursor_advance(&zc)) {
911 if (ZFS_DIRENT_OBJ(za->za_first_integer) == value) {
912 (void) strcpy(name, za->za_name);
916 zap_cursor_fini(&zc);
917 kmem_free(za, sizeof (zap_attribute_t));
923 * Routines for iterating over the attributes.
927 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
930 zap_entry_handle_t zeh;
933 /* retrieve the next entry at or after zc_hash/zc_cd */
934 /* if no entry, return ENOENT */
937 (ZAP_HASH_IDX(zc->zc_hash,
938 zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) !=
939 zc->zc_leaf->l_phys->l_hdr.lh_prefix)) {
940 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
941 zap_put_leaf(zc->zc_leaf);
946 if (zc->zc_leaf == NULL) {
947 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
952 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
956 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
960 (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1;
961 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
963 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) {
966 zap_put_leaf(zc->zc_leaf);
973 zc->zc_hash = zeh.zeh_hash;
974 zc->zc_cd = zeh.zeh_cd;
975 za->za_integer_length = zeh.zeh_integer_size;
976 za->za_num_integers = zeh.zeh_num_integers;
977 if (zeh.zeh_num_integers == 0) {
978 za->za_first_integer = 0;
980 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
981 ASSERT(err == 0 || err == EOVERFLOW);
983 err = zap_entry_read_name(&zeh,
984 sizeof (za->za_name), za->za_name);
987 rw_exit(&zc->zc_leaf->l_rwlock);
993 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
996 uint64_t lastblk = 0;
999 * NB: if a leaf has more pointers than an entire ptrtbl block
1000 * can hold, then it'll be accounted for more than once, since
1001 * we won't have lastblk.
1003 for (i = 0; i < len; i++) {
1006 if (tbl[i] == lastblk)
1010 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1012 zap_leaf_stats(zap, l, zs);
1019 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1021 int bs = FZAP_BLOCK_SHIFT(zap);
1022 zs->zs_blocksize = 1ULL << bs;
1025 * Set zap_phys_t fields
1027 zs->zs_num_leafs = zap->zap_f.zap_phys->zap_num_leafs;
1028 zs->zs_num_entries = zap->zap_f.zap_phys->zap_num_entries;
1029 zs->zs_num_blocks = zap->zap_f.zap_phys->zap_freeblk;
1030 zs->zs_block_type = zap->zap_f.zap_phys->zap_block_type;
1031 zs->zs_magic = zap->zap_f.zap_phys->zap_magic;
1032 zs->zs_salt = zap->zap_f.zap_phys->zap_salt;
1035 * Set zap_ptrtbl fields
1037 zs->zs_ptrtbl_len = 1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1038 zs->zs_ptrtbl_nextblk = zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk;
1039 zs->zs_ptrtbl_blks_copied =
1040 zap->zap_f.zap_phys->zap_ptrtbl.zt_blks_copied;
1041 zs->zs_ptrtbl_zt_blk = zap->zap_f.zap_phys->zap_ptrtbl.zt_blk;
1042 zs->zs_ptrtbl_zt_numblks = zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1043 zs->zs_ptrtbl_zt_shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1045 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
1046 /* the ptrtbl is entirely in the header block. */
1047 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1048 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1052 dmu_prefetch(zap->zap_objset, zap->zap_object,
1053 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk << bs,
1054 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << bs);
1056 for (b = 0; b < zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1061 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1062 (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk + b) << bs,
1065 zap_stats_ptrtbl(zap, db->db_data,
1067 dmu_buf_rele(db, FTAG);