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) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * This file contains the top half of the zfs directory structure
29 * implementation. The bottom half is in zap_leaf.c.
31 * The zdir is an extendable hash data structure. There is a table of
32 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
33 * each a constant size and hold a variable number of directory entries.
34 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
36 * The pointer table holds a power of 2 number of pointers.
37 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
38 * by the pointer at index i in the table holds entries whose hash value
39 * has a zd_prefix_len - bit prefix
44 #include <sys/zfs_context.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/fs/zfs.h>
48 #include <sys/refcount.h>
49 #include <sys/zap_impl.h>
50 #include <sys/zap_leaf.h>
52 int fzap_default_block_shift = 14; /* 16k blocksize */
54 extern inline zap_phys_t *zap_f_phys(zap_t *zap);
56 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
59 fzap_byteswap(void *vbuf, size_t size)
63 block_type = *(uint64_t *)vbuf;
65 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
66 zap_leaf_byteswap(vbuf, size);
68 /* it's a ptrtbl block */
69 byteswap_uint64_array(vbuf, size);
74 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
81 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
82 zap->zap_ismicro = FALSE;
84 zap->zap_dbu.dbu_evict_func_sync = zap_evict_sync;
85 zap->zap_dbu.dbu_evict_func_async = NULL;
87 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
88 zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
92 * explicitly zero it since it might be coming from an
93 * initialized microzap
95 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
96 zp->zap_block_type = ZBT_HEADER;
97 zp->zap_magic = ZAP_MAGIC;
99 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
101 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
102 zp->zap_num_leafs = 1;
103 zp->zap_num_entries = 0;
104 zp->zap_salt = zap->zap_salt;
105 zp->zap_normflags = zap->zap_normflags;
106 zp->zap_flags = flags;
108 /* block 1 will be the first leaf */
109 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
110 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
113 * set up block 1 - the first leaf
115 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
116 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
117 dmu_buf_will_dirty(db, tx);
119 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
122 zap_leaf_init(l, zp->zap_normflags != 0);
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 ASSERT0(tbl->zt_blks_copied);
166 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
167 tbl->zt_blk << bs, tbl->zt_numblks << bs,
168 ZIO_PRIORITY_SYNC_READ);
172 * Copy the ptrtbl from the old to new location.
175 b = tbl->zt_blks_copied;
176 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
177 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
181 /* first half of entries in old[b] go to new[2*b+0] */
182 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
183 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
184 dmu_buf_will_dirty(db_new, tx);
185 transfer_func(db_old->db_data, db_new->db_data, hepb);
186 dmu_buf_rele(db_new, FTAG);
188 /* second half of entries in old[b] go to new[2*b+1] */
189 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
190 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
191 dmu_buf_will_dirty(db_new, tx);
192 transfer_func((uint64_t *)db_old->db_data + hepb,
193 db_new->db_data, hepb);
194 dmu_buf_rele(db_new, FTAG);
196 dmu_buf_rele(db_old, FTAG);
198 tbl->zt_blks_copied++;
200 dprintf("copied block %llu of %llu\n",
201 tbl->zt_blks_copied, tbl->zt_numblks);
203 if (tbl->zt_blks_copied == tbl->zt_numblks) {
204 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
205 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
207 tbl->zt_blk = newblk;
208 tbl->zt_numblks *= 2;
211 tbl->zt_blks_copied = 0;
213 dprintf("finished; numblocks now %llu (%lluk entries)\n",
214 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
221 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
226 int bs = FZAP_BLOCK_SHIFT(zap);
229 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
230 ASSERT(tbl->zt_blk != 0);
232 dprintf("storing %llx at index %llx\n", val, idx);
235 off = idx & ((1<<(bs-3))-1);
237 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
238 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
241 dmu_buf_will_dirty(db, tx);
243 if (tbl->zt_nextblk != 0) {
244 uint64_t idx2 = idx * 2;
245 uint64_t blk2 = idx2 >> (bs-3);
246 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
249 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
250 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
251 DMU_READ_NO_PREFETCH);
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)
275 int bs = FZAP_BLOCK_SHIFT(zap);
277 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
280 off = idx & ((1<<(bs-3))-1);
283 * Note: this is equivalent to dmu_buf_hold(), but we use
284 * _dnode_enter / _by_dnode because it's faster because we don't
285 * have to hold the dnode.
287 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
288 err = dmu_buf_hold_by_dnode(dn,
289 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
290 dmu_buf_dnode_exit(zap->zap_dbuf);
293 *valp = ((uint64_t *)db->db_data)[off];
294 dmu_buf_rele(db, FTAG);
296 if (tbl->zt_nextblk != 0) {
298 * read the nextblk for the sake of i/o error checking,
299 * so that zap_table_load() will catch errors for
302 blk = (idx*2) >> (bs-3);
304 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
305 err = dmu_buf_hold_by_dnode(dn,
306 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
307 DMU_READ_NO_PREFETCH);
308 dmu_buf_dnode_exit(zap->zap_dbuf);
310 dmu_buf_rele(db, FTAG);
316 * Routines for growing the ptrtbl.
320 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
323 for (i = 0; i < n; i++) {
324 uint64_t lb = src[i];
331 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
334 * The pointer table should never use more hash bits than we
335 * have (otherwise we'd be using useless zero bits to index it).
336 * If we are within 2 bits of running out, stop growing, since
337 * this is already an aberrant condition.
339 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
340 return (SET_ERROR(ENOSPC));
342 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
344 * We are outgrowing the "embedded" ptrtbl (the one
345 * stored in the header block). Give it its own entire
346 * block, which will double the size of the ptrtbl.
352 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
353 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
354 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
356 newblk = zap_allocate_blocks(zap, 1);
357 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
358 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
359 DMU_READ_NO_PREFETCH);
362 dmu_buf_will_dirty(db_new, tx);
363 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
364 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
365 dmu_buf_rele(db_new, FTAG);
367 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
368 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
369 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
371 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
372 zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
373 (FZAP_BLOCK_SHIFT(zap)-3));
377 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
378 zap_ptrtbl_transfer, tx));
383 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
385 dmu_buf_will_dirty(zap->zap_dbuf, tx);
386 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
387 ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
388 zap_f_phys(zap)->zap_num_entries += delta;
389 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
393 zap_allocate_blocks(zap_t *zap, int nblocks)
396 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
397 newblk = zap_f_phys(zap)->zap_freeblk;
398 zap_f_phys(zap)->zap_freeblk += nblocks;
403 zap_leaf_evict_sync(void *dbu)
407 rw_destroy(&l->l_rwlock);
408 kmem_free(l, sizeof (zap_leaf_t));
412 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
415 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
417 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
419 rw_init(&l->l_rwlock, 0, 0, 0);
420 rw_enter(&l->l_rwlock, RW_WRITER);
421 l->l_blkid = zap_allocate_blocks(zap, 1);
424 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
425 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
426 DMU_READ_NO_PREFETCH));
427 dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
428 winner = dmu_buf_set_user(l->l_dbuf, &l->l_dbu);
429 ASSERT(winner == NULL);
430 dmu_buf_will_dirty(l->l_dbuf, tx);
432 zap_leaf_init(l, zap->zap_normflags != 0);
434 zap_f_phys(zap)->zap_num_leafs++;
440 fzap_count(zap_t *zap, uint64_t *count)
442 ASSERT(!zap->zap_ismicro);
443 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
444 *count = zap_f_phys(zap)->zap_num_entries;
445 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
450 * Routines for obtaining zap_leaf_t's
454 zap_put_leaf(zap_leaf_t *l)
456 rw_exit(&l->l_rwlock);
457 dmu_buf_rele(l->l_dbuf, NULL);
461 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
463 zap_leaf_t *l, *winner;
467 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
468 rw_init(&l->l_rwlock, 0, 0, 0);
469 rw_enter(&l->l_rwlock, RW_WRITER);
471 l->l_bs = highbit64(db->db_size) - 1;
474 dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
475 winner = dmu_buf_set_user(db, &l->l_dbu);
477 rw_exit(&l->l_rwlock);
478 if (winner != NULL) {
479 /* someone else set it first */
480 zap_leaf_evict_sync(&l->l_dbu);
485 * lhr_pad was previously used for the next leaf in the leaf
486 * chain. There should be no chained leafs (as we have removed
489 ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
492 * There should be more hash entries than there can be
493 * chunks to put in the hash table
495 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
497 /* The chunks should begin at the end of the hash table */
498 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
499 &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
501 /* The chunks should end at the end of the block */
502 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
503 (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
509 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
514 int bs = FZAP_BLOCK_SHIFT(zap);
517 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
519 dnode_t *dn = dmu_buf_dnode_enter(zap->zap_dbuf);
520 err = dmu_buf_hold_by_dnode(dn,
521 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
522 dmu_buf_dnode_exit(zap->zap_dbuf);
526 ASSERT3U(db->db_object, ==, zap->zap_object);
527 ASSERT3U(db->db_offset, ==, blkid << bs);
528 ASSERT3U(db->db_size, ==, 1 << bs);
531 l = dmu_buf_get_user(db);
534 l = zap_open_leaf(blkid, db);
536 rw_enter(&l->l_rwlock, lt);
538 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
539 * causing ASSERT below to fail.
542 dmu_buf_will_dirty(db, tx);
543 ASSERT3U(l->l_blkid, ==, blkid);
544 ASSERT3P(l->l_dbuf, ==, db);
545 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
546 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
553 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
555 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
557 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
559 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
560 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
563 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
569 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
572 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
574 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
575 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
578 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
584 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
589 ASSERT(zap->zap_dbuf == NULL ||
590 zap_f_phys(zap) == zap->zap_dbuf->db_data);
592 /* Reality check for corrupt zap objects (leaf or header). */
593 if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
594 zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
595 zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
596 return (SET_ERROR(EIO));
599 idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
600 err = zap_idx_to_blk(zap, idx, &blk);
603 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
606 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
607 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
612 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
613 void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
615 zap_t *zap = zn->zn_zap;
616 uint64_t hash = zn->zn_hash;
618 int prefix_diff, i, err;
620 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
622 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
623 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
625 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
626 zap_leaf_phys(l)->l_hdr.lh_prefix);
628 if (zap_tryupgradedir(zap, tx) == 0 ||
629 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
630 /* We failed to upgrade, or need to grow the pointer table */
631 objset_t *os = zap->zap_objset;
632 uint64_t object = zap->zap_object;
635 zap_unlockdir(zap, tag);
636 err = zap_lockdir(os, object, tx, RW_WRITER,
637 FALSE, FALSE, tag, &zn->zn_zap);
641 ASSERT(!zap->zap_ismicro);
643 while (old_prefix_len ==
644 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
645 err = zap_grow_ptrtbl(zap, tx);
650 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
654 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
655 /* it split while our locks were down */
660 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
661 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
662 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
663 zap_leaf_phys(l)->l_hdr.lh_prefix);
665 prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
666 (old_prefix_len + 1);
667 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
669 /* check for i/o errors before doing zap_leaf_split */
670 for (i = 0; i < (1ULL<<prefix_diff); i++) {
672 err = zap_idx_to_blk(zap, sibling+i, &blk);
675 ASSERT3U(blk, ==, l->l_blkid);
678 nl = zap_create_leaf(zap, tx);
679 zap_leaf_split(l, nl, zap->zap_normflags != 0);
681 /* set sibling pointers */
682 for (i = 0; i < (1ULL << prefix_diff); i++) {
683 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
684 ASSERT0(err); /* we checked for i/o errors above */
687 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
688 /* we want the sibling */
700 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
701 void *tag, dmu_tx_t *tx)
703 zap_t *zap = zn->zn_zap;
704 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
705 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
706 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
710 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
714 * We are in the middle of growing the pointer table, or
715 * this leaf will soon make us grow it.
717 if (zap_tryupgradedir(zap, tx) == 0) {
718 objset_t *os = zap->zap_objset;
719 uint64_t zapobj = zap->zap_object;
721 zap_unlockdir(zap, tag);
722 err = zap_lockdir(os, zapobj, tx,
723 RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
729 /* could have finished growing while our locks were down */
730 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
731 (void) zap_grow_ptrtbl(zap, tx);
736 fzap_checkname(zap_name_t *zn)
738 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
739 return (SET_ERROR(ENAMETOOLONG));
744 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
746 /* Only integer sizes supported by C */
747 switch (integer_size) {
754 return (SET_ERROR(EINVAL));
757 if (integer_size * num_integers > ZAP_MAXVALUELEN)
764 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
768 if ((err = fzap_checkname(zn)) != 0)
770 return (fzap_checksize(integer_size, num_integers));
774 * Routines for manipulating attributes.
777 fzap_lookup(zap_name_t *zn,
778 uint64_t integer_size, uint64_t num_integers, void *buf,
779 char *realname, int rn_len, boolean_t *ncp)
783 zap_entry_handle_t zeh;
785 if ((err = fzap_checkname(zn)) != 0)
788 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
791 err = zap_leaf_lookup(l, zn, &zeh);
793 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
798 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
799 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
801 *ncp = zap_entry_normalization_conflict(&zeh,
802 zn, NULL, zn->zn_zap);
811 fzap_add_cd(zap_name_t *zn,
812 uint64_t integer_size, uint64_t num_integers,
813 const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
817 zap_entry_handle_t zeh;
818 zap_t *zap = zn->zn_zap;
820 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
821 ASSERT(!zap->zap_ismicro);
822 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
824 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
828 err = zap_leaf_lookup(l, zn, &zeh);
830 err = SET_ERROR(EEXIST);
836 err = zap_entry_create(l, zn, cd,
837 integer_size, num_integers, val, &zeh);
840 zap_increment_num_entries(zap, 1, tx);
841 } else if (err == EAGAIN) {
842 err = zap_expand_leaf(zn, l, tag, tx, &l);
843 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
850 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
855 fzap_add(zap_name_t *zn,
856 uint64_t integer_size, uint64_t num_integers,
857 const void *val, void *tag, dmu_tx_t *tx)
859 int err = fzap_check(zn, integer_size, num_integers);
863 return (fzap_add_cd(zn, integer_size, num_integers,
864 val, ZAP_NEED_CD, tag, tx));
868 fzap_update(zap_name_t *zn,
869 int integer_size, uint64_t num_integers, const void *val,
870 void *tag, dmu_tx_t *tx)
874 zap_entry_handle_t zeh;
875 zap_t *zap = zn->zn_zap;
877 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
878 err = fzap_check(zn, integer_size, num_integers);
882 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
886 err = zap_leaf_lookup(l, zn, &zeh);
887 create = (err == ENOENT);
888 ASSERT(err == 0 || err == ENOENT);
891 err = zap_entry_create(l, zn, ZAP_NEED_CD,
892 integer_size, num_integers, val, &zeh);
894 zap_increment_num_entries(zap, 1, tx);
896 err = zap_entry_update(&zeh, integer_size, num_integers, val);
900 err = zap_expand_leaf(zn, l, tag, tx, &l);
901 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
907 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
912 fzap_length(zap_name_t *zn,
913 uint64_t *integer_size, uint64_t *num_integers)
917 zap_entry_handle_t zeh;
919 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
922 err = zap_leaf_lookup(l, zn, &zeh);
927 *integer_size = zeh.zeh_integer_size;
929 *num_integers = zeh.zeh_num_integers;
936 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
940 zap_entry_handle_t zeh;
942 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
945 err = zap_leaf_lookup(l, zn, &zeh);
947 zap_entry_remove(&zeh);
948 zap_increment_num_entries(zn->zn_zap, -1, tx);
955 fzap_prefetch(zap_name_t *zn)
958 zap_t *zap = zn->zn_zap;
961 idx = ZAP_HASH_IDX(zn->zn_hash,
962 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
963 if (zap_idx_to_blk(zap, idx, &blk) != 0)
965 bs = FZAP_BLOCK_SHIFT(zap);
966 dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
967 ZIO_PRIORITY_SYNC_READ);
971 * Helper functions for consumers.
975 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
976 const char *name, dmu_tx_t *tx)
980 VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
981 VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
988 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
998 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
999 for (zap_cursor_init(&zc, os, zapobj);
1000 (err = zap_cursor_retrieve(&zc, za)) == 0;
1001 zap_cursor_advance(&zc)) {
1002 if ((za->za_first_integer & mask) == (value & mask)) {
1003 (void) strcpy(name, za->za_name);
1007 zap_cursor_fini(&zc);
1008 kmem_free(za, sizeof (zap_attribute_t));
1013 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1020 for (zap_cursor_init(&zc, os, fromobj);
1021 zap_cursor_retrieve(&zc, &za) == 0;
1022 (void) zap_cursor_advance(&zc)) {
1023 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1024 err = SET_ERROR(EINVAL);
1027 err = zap_add(os, intoobj, za.za_name,
1028 8, 1, &za.za_first_integer, tx);
1032 zap_cursor_fini(&zc);
1037 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1038 uint64_t value, dmu_tx_t *tx)
1045 for (zap_cursor_init(&zc, os, fromobj);
1046 zap_cursor_retrieve(&zc, &za) == 0;
1047 (void) zap_cursor_advance(&zc)) {
1048 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1049 err = SET_ERROR(EINVAL);
1052 err = zap_add(os, intoobj, za.za_name,
1057 zap_cursor_fini(&zc);
1062 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1070 for (zap_cursor_init(&zc, os, fromobj);
1071 zap_cursor_retrieve(&zc, &za) == 0;
1072 (void) zap_cursor_advance(&zc)) {
1075 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1076 err = SET_ERROR(EINVAL);
1080 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1081 if (err != 0 && err != ENOENT)
1083 delta += za.za_first_integer;
1084 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1088 zap_cursor_fini(&zc);
1093 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1097 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1098 return (zap_add(os, obj, name, 8, 1, &value, tx));
1102 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1106 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1107 return (zap_remove(os, obj, name, tx));
1111 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1115 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1116 return (zap_lookup(os, obj, name, 8, 1, &value));
1120 zap_add_int_key(objset_t *os, uint64_t obj,
1121 uint64_t key, uint64_t value, dmu_tx_t *tx)
1125 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1126 return (zap_add(os, obj, name, 8, 1, &value, tx));
1130 zap_update_int_key(objset_t *os, uint64_t obj,
1131 uint64_t key, uint64_t value, dmu_tx_t *tx)
1135 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1136 return (zap_update(os, obj, name, 8, 1, &value, tx));
1140 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1144 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1145 return (zap_lookup(os, obj, name, 8, 1, valuep));
1149 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1158 err = zap_lookup(os, obj, name, 8, 1, &value);
1159 if (err != 0 && err != ENOENT)
1163 err = zap_remove(os, obj, name, tx);
1165 err = zap_update(os, obj, name, 8, 1, &value, tx);
1170 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1175 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1176 return (zap_increment(os, obj, name, delta, tx));
1180 * Routines for iterating over the attributes.
1184 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1187 zap_entry_handle_t zeh;
1190 /* retrieve the next entry at or after zc_hash/zc_cd */
1191 /* if no entry, return ENOENT */
1194 (ZAP_HASH_IDX(zc->zc_hash,
1195 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1196 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1197 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1198 zap_put_leaf(zc->zc_leaf);
1203 if (zc->zc_leaf == NULL) {
1204 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1209 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1213 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1215 if (err == ENOENT) {
1217 (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1218 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1220 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1222 zc->zc_hash = -1ULL;
1224 zap_put_leaf(zc->zc_leaf);
1231 zc->zc_hash = zeh.zeh_hash;
1232 zc->zc_cd = zeh.zeh_cd;
1233 za->za_integer_length = zeh.zeh_integer_size;
1234 za->za_num_integers = zeh.zeh_num_integers;
1235 if (zeh.zeh_num_integers == 0) {
1236 za->za_first_integer = 0;
1238 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1239 ASSERT(err == 0 || err == EOVERFLOW);
1241 err = zap_entry_read_name(zap, &zeh,
1242 sizeof (za->za_name), za->za_name);
1245 za->za_normalization_conflict =
1246 zap_entry_normalization_conflict(&zeh,
1247 NULL, za->za_name, zap);
1249 rw_exit(&zc->zc_leaf->l_rwlock);
1254 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1257 uint64_t lastblk = 0;
1260 * NB: if a leaf has more pointers than an entire ptrtbl block
1261 * can hold, then it'll be accounted for more than once, since
1262 * we won't have lastblk.
1264 for (i = 0; i < len; i++) {
1267 if (tbl[i] == lastblk)
1271 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1273 zap_leaf_stats(zap, l, zs);
1280 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1284 zap_entry_handle_t zeh;
1286 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1287 return (SET_ERROR(ENAMETOOLONG));
1289 err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1293 err = zap_leaf_lookup(l, zn, &zeh);
1298 zc->zc_hash = zeh.zeh_hash;
1299 zc->zc_cd = zeh.zeh_cd;
1305 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1307 int bs = FZAP_BLOCK_SHIFT(zap);
1308 zs->zs_blocksize = 1ULL << bs;
1311 * Set zap_phys_t fields
1313 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1314 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1315 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1316 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1317 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1318 zs->zs_salt = zap_f_phys(zap)->zap_salt;
1321 * Set zap_ptrtbl fields
1323 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1324 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1325 zs->zs_ptrtbl_blks_copied =
1326 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1327 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1328 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1329 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1331 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1332 /* the ptrtbl is entirely in the header block. */
1333 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1334 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1338 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
1339 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1340 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1341 ZIO_PRIORITY_SYNC_READ);
1343 for (b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1348 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1349 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1350 FTAG, &db, DMU_READ_NO_PREFETCH);
1352 zap_stats_ptrtbl(zap, db->db_data,
1354 dmu_buf_rele(db, FTAG);