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, 2018 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>
53 * If zap_iterate_prefetch is set, we will prefetch the entire ZAP object
54 * (all leaf blocks) when we start iterating over it.
56 * For zap_cursor_init(), the callers all intend to iterate through all the
57 * entries. There are a few cases where an error (typically i/o error) could
58 * cause it to bail out early.
60 * For zap_cursor_init_serialized(), there are callers that do the iteration
61 * outside of ZFS. Typically they would iterate over everything, but we
62 * don't have control of that. E.g. zfs_ioc_snapshot_list_next(),
63 * zcp_snapshots_iter(), and other iterators over things in the MOS - these
64 * are called by /sbin/zfs and channel programs. The other example is
65 * zfs_readdir() which iterates over directory entries for the getdents()
66 * syscall. /sbin/ls iterates to the end (unless it receives a signal), but
67 * userland doesn't have to.
69 * Given that the ZAP entries aren't returned in a specific order, the only
70 * legitimate use cases for partial iteration would be:
72 * 1. Pagination: e.g. you only want to display 100 entries at a time, so you
73 * get the first 100 and then wait for the user to hit "next page", which
76 * 2. You want to know if there are more than X entries, without relying on
77 * the zfs-specific implementation of the directory's st_size (which is
78 * the number of entries).
80 boolean_t zap_iterate_prefetch = B_TRUE;
82 int fzap_default_block_shift = 14; /* 16k blocksize */
84 extern inline zap_phys_t *zap_f_phys(zap_t *zap);
86 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
89 fzap_byteswap(void *vbuf, size_t size)
91 uint64_t block_type = *(uint64_t *)vbuf;
93 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
94 zap_leaf_byteswap(vbuf, size);
96 /* it's a ptrtbl block */
97 byteswap_uint64_array(vbuf, size);
102 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
104 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
105 zap->zap_ismicro = FALSE;
107 zap->zap_dbu.dbu_evict_func_sync = zap_evict_sync;
108 zap->zap_dbu.dbu_evict_func_async = NULL;
110 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
111 zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
113 zap_phys_t *zp = zap_f_phys(zap);
115 * explicitly zero it since it might be coming from an
116 * initialized microzap
118 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
119 zp->zap_block_type = ZBT_HEADER;
120 zp->zap_magic = ZAP_MAGIC;
122 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
124 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
125 zp->zap_num_leafs = 1;
126 zp->zap_num_entries = 0;
127 zp->zap_salt = zap->zap_salt;
128 zp->zap_normflags = zap->zap_normflags;
129 zp->zap_flags = flags;
131 /* block 1 will be the first leaf */
132 for (int i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
133 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
136 * set up block 1 - the first leaf
139 VERIFY0(dmu_buf_hold(zap->zap_objset, zap->zap_object,
140 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
141 dmu_buf_will_dirty(db, tx);
143 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
146 zap_leaf_init(l, zp->zap_normflags != 0);
148 kmem_free(l, sizeof (zap_leaf_t));
149 dmu_buf_rele(db, FTAG);
153 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
155 if (RW_WRITE_HELD(&zap->zap_rwlock))
157 if (rw_tryupgrade(&zap->zap_rwlock)) {
158 dmu_buf_will_dirty(zap->zap_dbuf, tx);
165 * Generic routines for dealing with the pointer & cookie tables.
169 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
170 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
174 int bs = FZAP_BLOCK_SHIFT(zap);
175 int hepb = 1<<(bs-4);
176 /* hepb = half the number of entries in a block */
178 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
179 ASSERT(tbl->zt_blk != 0);
180 ASSERT(tbl->zt_numblks > 0);
182 if (tbl->zt_nextblk != 0) {
183 newblk = tbl->zt_nextblk;
185 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
186 tbl->zt_nextblk = newblk;
187 ASSERT0(tbl->zt_blks_copied);
188 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
189 tbl->zt_blk << bs, tbl->zt_numblks << bs,
190 ZIO_PRIORITY_SYNC_READ);
194 * Copy the ptrtbl from the old to new location.
197 uint64_t b = tbl->zt_blks_copied;
199 int err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
200 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
204 /* first half of entries in old[b] go to new[2*b+0] */
206 VERIFY0(dmu_buf_hold(zap->zap_objset, zap->zap_object,
207 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
208 dmu_buf_will_dirty(db_new, tx);
209 transfer_func(db_old->db_data, db_new->db_data, hepb);
210 dmu_buf_rele(db_new, FTAG);
212 /* second half of entries in old[b] go to new[2*b+1] */
213 VERIFY0(dmu_buf_hold(zap->zap_objset, zap->zap_object,
214 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
215 dmu_buf_will_dirty(db_new, tx);
216 transfer_func((uint64_t *)db_old->db_data + hepb,
217 db_new->db_data, hepb);
218 dmu_buf_rele(db_new, FTAG);
220 dmu_buf_rele(db_old, FTAG);
222 tbl->zt_blks_copied++;
224 dprintf("copied block %llu of %llu\n",
225 tbl->zt_blks_copied, tbl->zt_numblks);
227 if (tbl->zt_blks_copied == tbl->zt_numblks) {
228 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
229 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
231 tbl->zt_blk = newblk;
232 tbl->zt_numblks *= 2;
235 tbl->zt_blks_copied = 0;
237 dprintf("finished; numblocks now %llu (%lluk entries)\n",
238 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
245 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
248 int bs = FZAP_BLOCK_SHIFT(zap);
250 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
251 ASSERT(tbl->zt_blk != 0);
253 dprintf("storing %llx at index %llx\n", val, idx);
255 uint64_t blk = idx >> (bs-3);
256 uint64_t off = idx & ((1<<(bs-3))-1);
259 int err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
260 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
263 dmu_buf_will_dirty(db, tx);
265 if (tbl->zt_nextblk != 0) {
266 uint64_t idx2 = idx * 2;
267 uint64_t blk2 = idx2 >> (bs-3);
268 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
271 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
272 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
273 DMU_READ_NO_PREFETCH);
275 dmu_buf_rele(db, FTAG);
278 dmu_buf_will_dirty(db2, tx);
279 ((uint64_t *)db2->db_data)[off2] = val;
280 ((uint64_t *)db2->db_data)[off2+1] = val;
281 dmu_buf_rele(db2, FTAG);
284 ((uint64_t *)db->db_data)[off] = val;
285 dmu_buf_rele(db, FTAG);
291 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
293 int bs = FZAP_BLOCK_SHIFT(zap);
295 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
297 uint64_t blk = idx >> (bs-3);
298 uint64_t off = idx & ((1<<(bs-3))-1);
301 * Note: this is equivalent to dmu_buf_hold(), but we use
302 * _dnode_enter / _by_dnode because it's faster because we don't
303 * have to hold the dnode.
305 dnode_t *dn = dmu_buf_dnode_enter(zap->zap_dbuf);
307 int err = dmu_buf_hold_by_dnode(dn,
308 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
309 dmu_buf_dnode_exit(zap->zap_dbuf);
312 *valp = ((uint64_t *)db->db_data)[off];
313 dmu_buf_rele(db, FTAG);
315 if (tbl->zt_nextblk != 0) {
317 * read the nextblk for the sake of i/o error checking,
318 * so that zap_table_load() will catch errors for
321 blk = (idx*2) >> (bs-3);
323 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
324 err = dmu_buf_hold_by_dnode(dn,
325 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
326 DMU_READ_NO_PREFETCH);
327 dmu_buf_dnode_exit(zap->zap_dbuf);
329 dmu_buf_rele(db, FTAG);
335 * Routines for growing the ptrtbl.
339 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
341 for (int i = 0; i < n; i++) {
342 uint64_t lb = src[i];
349 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
352 * The pointer table should never use more hash bits than we
353 * have (otherwise we'd be using useless zero bits to index it).
354 * If we are within 2 bits of running out, stop growing, since
355 * this is already an aberrant condition.
357 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
358 return (SET_ERROR(ENOSPC));
360 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
362 * We are outgrowing the "embedded" ptrtbl (the one
363 * stored in the header block). Give it its own entire
364 * block, which will double the size of the ptrtbl.
366 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
367 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
368 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
370 uint64_t newblk = zap_allocate_blocks(zap, 1);
372 int err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
373 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
374 DMU_READ_NO_PREFETCH);
377 dmu_buf_will_dirty(db_new, tx);
378 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
379 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
380 dmu_buf_rele(db_new, FTAG);
382 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
383 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
384 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
386 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
387 zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
388 (FZAP_BLOCK_SHIFT(zap)-3));
392 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
393 zap_ptrtbl_transfer, tx));
398 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
400 dmu_buf_will_dirty(zap->zap_dbuf, tx);
401 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
402 ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
403 zap_f_phys(zap)->zap_num_entries += delta;
404 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
408 zap_allocate_blocks(zap_t *zap, int nblocks)
410 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
411 uint64_t newblk = zap_f_phys(zap)->zap_freeblk;
412 zap_f_phys(zap)->zap_freeblk += nblocks;
417 zap_leaf_evict_sync(void *dbu)
421 rw_destroy(&l->l_rwlock);
422 kmem_free(l, sizeof (zap_leaf_t));
426 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
428 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
430 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
432 rw_init(&l->l_rwlock, 0, 0, 0);
433 rw_enter(&l->l_rwlock, RW_WRITER);
434 l->l_blkid = zap_allocate_blocks(zap, 1);
437 VERIFY0(dmu_buf_hold(zap->zap_objset, zap->zap_object,
438 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
439 DMU_READ_NO_PREFETCH));
440 dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
441 VERIFY3P(NULL, ==, dmu_buf_set_user(l->l_dbuf, &l->l_dbu));
442 dmu_buf_will_dirty(l->l_dbuf, tx);
444 zap_leaf_init(l, zap->zap_normflags != 0);
446 zap_f_phys(zap)->zap_num_leafs++;
452 fzap_count(zap_t *zap, uint64_t *count)
454 ASSERT(!zap->zap_ismicro);
455 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
456 *count = zap_f_phys(zap)->zap_num_entries;
457 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
462 * Routines for obtaining zap_leaf_t's
466 zap_put_leaf(zap_leaf_t *l)
468 rw_exit(&l->l_rwlock);
469 dmu_buf_rele(l->l_dbuf, NULL);
473 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
477 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
478 rw_init(&l->l_rwlock, 0, 0, 0);
479 rw_enter(&l->l_rwlock, RW_WRITER);
481 l->l_bs = highbit64(db->db_size) - 1;
484 dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
485 zap_leaf_t *winner = dmu_buf_set_user(db, &l->l_dbu);
487 rw_exit(&l->l_rwlock);
488 if (winner != NULL) {
489 /* someone else set it first */
490 zap_leaf_evict_sync(&l->l_dbu);
495 * lhr_pad was previously used for the next leaf in the leaf
496 * chain. There should be no chained leafs (as we have removed
499 ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
502 * There should be more hash entries than there can be
503 * chunks to put in the hash table
505 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
507 /* The chunks should begin at the end of the hash table */
508 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
509 &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
511 /* The chunks should end at the end of the block */
512 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
513 (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
519 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
524 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
526 int bs = FZAP_BLOCK_SHIFT(zap);
527 dnode_t *dn = dmu_buf_dnode_enter(zap->zap_dbuf);
528 int err = dmu_buf_hold_by_dnode(dn,
529 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
530 dmu_buf_dnode_exit(zap->zap_dbuf);
534 ASSERT3U(db->db_object, ==, zap->zap_object);
535 ASSERT3U(db->db_offset, ==, blkid << bs);
536 ASSERT3U(db->db_size, ==, 1 << bs);
539 zap_leaf_t *l = dmu_buf_get_user(db);
542 l = zap_open_leaf(blkid, db);
544 rw_enter(&l->l_rwlock, lt);
546 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
547 * causing ASSERT below to fail.
550 dmu_buf_will_dirty(db, tx);
551 ASSERT3U(l->l_blkid, ==, blkid);
552 ASSERT3P(l->l_dbuf, ==, db);
553 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
554 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
561 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
563 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
565 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
567 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
568 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
571 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
577 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
580 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
582 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
583 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
586 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
592 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
596 ASSERT(zap->zap_dbuf == NULL ||
597 zap_f_phys(zap) == zap->zap_dbuf->db_data);
599 /* Reality check for corrupt zap objects (leaf or header). */
600 if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
601 zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
602 zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
603 return (SET_ERROR(EIO));
606 uint64_t idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
607 int err = zap_idx_to_blk(zap, idx, &blk);
610 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
613 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
614 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
619 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
620 void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
622 zap_t *zap = zn->zn_zap;
623 uint64_t hash = zn->zn_hash;
625 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
627 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
628 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
630 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
631 zap_leaf_phys(l)->l_hdr.lh_prefix);
633 if (zap_tryupgradedir(zap, tx) == 0 ||
634 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
635 /* We failed to upgrade, or need to grow the pointer table */
636 objset_t *os = zap->zap_objset;
637 uint64_t object = zap->zap_object;
640 zap_unlockdir(zap, tag);
641 err = zap_lockdir(os, object, tx, RW_WRITER,
642 FALSE, FALSE, tag, &zn->zn_zap);
646 ASSERT(!zap->zap_ismicro);
648 while (old_prefix_len ==
649 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
650 err = zap_grow_ptrtbl(zap, tx);
655 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
659 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
660 /* it split while our locks were down */
665 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
666 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
667 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
668 zap_leaf_phys(l)->l_hdr.lh_prefix);
670 int prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
671 (old_prefix_len + 1);
673 (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
675 /* check for i/o errors before doing zap_leaf_split */
676 for (int i = 0; i < (1ULL << prefix_diff); i++) {
678 err = zap_idx_to_blk(zap, sibling + i, &blk);
681 ASSERT3U(blk, ==, l->l_blkid);
684 zap_leaf_t *nl = zap_create_leaf(zap, tx);
685 zap_leaf_split(l, nl, zap->zap_normflags != 0);
687 /* set sibling pointers */
688 for (int i = 0; i < (1ULL << prefix_diff); i++) {
689 err = zap_set_idx_to_blk(zap, sibling + i, nl->l_blkid, tx);
690 ASSERT0(err); /* we checked for i/o errors above */
693 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
694 /* we want the sibling */
706 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
707 void *tag, dmu_tx_t *tx)
709 zap_t *zap = zn->zn_zap;
710 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
711 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
712 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
716 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
718 * We are in the middle of growing the pointer table, or
719 * this leaf will soon make us grow it.
721 if (zap_tryupgradedir(zap, tx) == 0) {
722 objset_t *os = zap->zap_objset;
723 uint64_t zapobj = zap->zap_object;
725 zap_unlockdir(zap, tag);
726 int err = zap_lockdir(os, zapobj, tx,
727 RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
733 /* could have finished growing while our locks were down */
734 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
735 (void) zap_grow_ptrtbl(zap, tx);
740 fzap_checkname(zap_name_t *zn)
742 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
743 return (SET_ERROR(ENAMETOOLONG));
748 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
750 /* Only integer sizes supported by C */
751 switch (integer_size) {
758 return (SET_ERROR(EINVAL));
761 if (integer_size * num_integers > ZAP_MAXVALUELEN)
768 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
770 int err = fzap_checkname(zn);
773 return (fzap_checksize(integer_size, num_integers));
777 * Routines for manipulating attributes.
780 fzap_lookup(zap_name_t *zn,
781 uint64_t integer_size, uint64_t num_integers, void *buf,
782 char *realname, int rn_len, boolean_t *ncp)
785 zap_entry_handle_t zeh;
787 int err = fzap_checkname(zn);
791 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
794 err = zap_leaf_lookup(l, zn, &zeh);
796 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
801 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
802 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
804 *ncp = zap_entry_normalization_conflict(&zeh,
805 zn, NULL, zn->zn_zap);
814 fzap_add_cd(zap_name_t *zn,
815 uint64_t integer_size, uint64_t num_integers,
816 const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
820 zap_entry_handle_t zeh;
821 zap_t *zap = zn->zn_zap;
823 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
824 ASSERT(!zap->zap_ismicro);
825 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
827 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
831 err = zap_leaf_lookup(l, zn, &zeh);
833 err = SET_ERROR(EEXIST);
839 err = zap_entry_create(l, zn, cd,
840 integer_size, num_integers, val, &zeh);
843 zap_increment_num_entries(zap, 1, tx);
844 } else if (err == EAGAIN) {
845 err = zap_expand_leaf(zn, l, tag, tx, &l);
846 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
853 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
858 fzap_add(zap_name_t *zn,
859 uint64_t integer_size, uint64_t num_integers,
860 const void *val, void *tag, dmu_tx_t *tx)
862 int err = fzap_check(zn, integer_size, num_integers);
866 return (fzap_add_cd(zn, integer_size, num_integers,
867 val, ZAP_NEED_CD, tag, tx));
871 fzap_update(zap_name_t *zn,
872 int integer_size, uint64_t num_integers, const void *val,
873 void *tag, dmu_tx_t *tx)
878 zap_entry_handle_t zeh;
879 zap_t *zap = zn->zn_zap;
881 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
882 err = fzap_check(zn, integer_size, num_integers);
886 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
890 err = zap_leaf_lookup(l, zn, &zeh);
891 create = (err == ENOENT);
892 ASSERT(err == 0 || err == ENOENT);
895 err = zap_entry_create(l, zn, ZAP_NEED_CD,
896 integer_size, num_integers, val, &zeh);
898 zap_increment_num_entries(zap, 1, tx);
900 err = zap_entry_update(&zeh, integer_size, num_integers, val);
904 err = zap_expand_leaf(zn, l, tag, tx, &l);
905 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
911 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
916 fzap_length(zap_name_t *zn,
917 uint64_t *integer_size, uint64_t *num_integers)
921 zap_entry_handle_t zeh;
923 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
926 err = zap_leaf_lookup(l, zn, &zeh);
930 if (integer_size != 0)
931 *integer_size = zeh.zeh_integer_size;
932 if (num_integers != 0)
933 *num_integers = zeh.zeh_num_integers;
940 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
944 zap_entry_handle_t zeh;
946 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
949 err = zap_leaf_lookup(l, zn, &zeh);
951 zap_entry_remove(&zeh);
952 zap_increment_num_entries(zn->zn_zap, -1, tx);
959 fzap_prefetch(zap_name_t *zn)
962 zap_t *zap = zn->zn_zap;
964 uint64_t idx = ZAP_HASH_IDX(zn->zn_hash,
965 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
966 if (zap_idx_to_blk(zap, idx, &blk) != 0)
968 int bs = FZAP_BLOCK_SHIFT(zap);
969 dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
970 ZIO_PRIORITY_SYNC_READ);
974 * Helper functions for consumers.
978 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
979 const char *name, dmu_tx_t *tx)
981 return (zap_create_link_dnsize(os, ot, parent_obj, name, 0, tx));
985 zap_create_link_dnsize(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
986 const char *name, int dnodesize, dmu_tx_t *tx)
990 VERIFY((new_obj = zap_create_dnsize(os, ot, DMU_OT_NONE, 0,
991 dnodesize, tx)) > 0);
992 VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
999 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
1008 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1009 for (zap_cursor_init(&zc, os, zapobj);
1010 (err = zap_cursor_retrieve(&zc, za)) == 0;
1011 zap_cursor_advance(&zc)) {
1012 if ((za->za_first_integer & mask) == (value & mask)) {
1013 (void) strcpy(name, za->za_name);
1017 zap_cursor_fini(&zc);
1018 kmem_free(za, sizeof (*za));
1023 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1028 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1029 for (zap_cursor_init(&zc, os, fromobj);
1030 zap_cursor_retrieve(&zc, za) == 0;
1031 (void) zap_cursor_advance(&zc)) {
1032 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1033 err = SET_ERROR(EINVAL);
1036 err = zap_add(os, intoobj, za->za_name,
1037 8, 1, &za->za_first_integer, tx);
1041 zap_cursor_fini(&zc);
1042 kmem_free(za, sizeof (*za));
1047 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1048 uint64_t value, dmu_tx_t *tx)
1053 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1054 for (zap_cursor_init(&zc, os, fromobj);
1055 zap_cursor_retrieve(&zc, za) == 0;
1056 (void) zap_cursor_advance(&zc)) {
1057 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1058 err = SET_ERROR(EINVAL);
1061 err = zap_add(os, intoobj, za->za_name,
1066 zap_cursor_fini(&zc);
1067 kmem_free(za, sizeof (*za));
1072 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1078 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1079 for (zap_cursor_init(&zc, os, fromobj);
1080 zap_cursor_retrieve(&zc, za) == 0;
1081 (void) zap_cursor_advance(&zc)) {
1084 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1085 err = SET_ERROR(EINVAL);
1089 err = zap_lookup(os, intoobj, za->za_name, 8, 1, &delta);
1090 if (err != 0 && err != ENOENT)
1092 delta += za->za_first_integer;
1093 err = zap_update(os, intoobj, za->za_name, 8, 1, &delta, tx);
1097 zap_cursor_fini(&zc);
1098 kmem_free(za, sizeof (*za));
1103 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1107 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1108 return (zap_add(os, obj, name, 8, 1, &value, tx));
1112 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1116 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1117 return (zap_remove(os, obj, name, tx));
1121 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1125 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1126 return (zap_lookup(os, obj, name, 8, 1, &value));
1130 zap_add_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_add(os, obj, name, 8, 1, &value, tx));
1140 zap_update_int_key(objset_t *os, uint64_t obj,
1141 uint64_t key, uint64_t value, dmu_tx_t *tx)
1145 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1146 return (zap_update(os, obj, name, 8, 1, &value, tx));
1150 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1154 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1155 return (zap_lookup(os, obj, name, 8, 1, valuep));
1159 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1167 int err = zap_lookup(os, obj, name, 8, 1, &value);
1168 if (err != 0 && err != ENOENT)
1172 err = zap_remove(os, obj, name, tx);
1174 err = zap_update(os, obj, name, 8, 1, &value, tx);
1179 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1184 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1185 return (zap_increment(os, obj, name, delta, tx));
1189 * Routines for iterating over the attributes.
1193 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1196 zap_entry_handle_t zeh;
1199 /* retrieve the next entry at or after zc_hash/zc_cd */
1200 /* if no entry, return ENOENT */
1203 * If we are reading from the beginning, we're almost
1204 * certain to iterate over the entire ZAP object. If there are
1205 * multiple leaf blocks (freeblk > 2), prefetch the whole
1206 * object, so that we read the leaf blocks concurrently.
1207 * (Unless noprefetch was requested via zap_cursor_init_noprefetch()).
1209 if (zc->zc_hash == 0 && zap_iterate_prefetch &&
1210 zc->zc_prefetch && zap_f_phys(zap)->zap_freeblk > 2) {
1211 dmu_prefetch(zc->zc_objset, zc->zc_zapobj, 0, 0,
1212 zap_f_phys(zap)->zap_freeblk << FZAP_BLOCK_SHIFT(zap),
1213 ZIO_PRIORITY_ASYNC_READ);
1217 (ZAP_HASH_IDX(zc->zc_hash,
1218 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1219 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1220 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1221 zap_put_leaf(zc->zc_leaf);
1226 if (zc->zc_leaf == NULL) {
1227 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1232 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1236 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1238 if (err == ENOENT) {
1240 (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1241 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1243 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1245 zc->zc_hash = -1ULL;
1247 zap_put_leaf(zc->zc_leaf);
1254 zc->zc_hash = zeh.zeh_hash;
1255 zc->zc_cd = zeh.zeh_cd;
1256 za->za_integer_length = zeh.zeh_integer_size;
1257 za->za_num_integers = zeh.zeh_num_integers;
1258 if (zeh.zeh_num_integers == 0) {
1259 za->za_first_integer = 0;
1261 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1262 ASSERT(err == 0 || err == EOVERFLOW);
1264 err = zap_entry_read_name(zap, &zeh,
1265 sizeof (za->za_name), za->za_name);
1268 za->za_normalization_conflict =
1269 zap_entry_normalization_conflict(&zeh,
1270 NULL, za->za_name, zap);
1272 rw_exit(&zc->zc_leaf->l_rwlock);
1277 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1279 uint64_t lastblk = 0;
1282 * NB: if a leaf has more pointers than an entire ptrtbl block
1283 * can hold, then it'll be accounted for more than once, since
1284 * we won't have lastblk.
1286 for (int i = 0; i < len; i++) {
1289 if (tbl[i] == lastblk)
1293 int err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1295 zap_leaf_stats(zap, l, zs);
1302 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1306 zap_entry_handle_t zeh;
1308 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1309 return (SET_ERROR(ENAMETOOLONG));
1311 err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1315 err = zap_leaf_lookup(l, zn, &zeh);
1320 zc->zc_hash = zeh.zeh_hash;
1321 zc->zc_cd = zeh.zeh_cd;
1327 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1329 int bs = FZAP_BLOCK_SHIFT(zap);
1330 zs->zs_blocksize = 1ULL << bs;
1333 * Set zap_phys_t fields
1335 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1336 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1337 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1338 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1339 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1340 zs->zs_salt = zap_f_phys(zap)->zap_salt;
1343 * Set zap_ptrtbl fields
1345 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1346 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1347 zs->zs_ptrtbl_blks_copied =
1348 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1349 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1350 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1351 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1353 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1354 /* the ptrtbl is entirely in the header block. */
1355 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1356 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1358 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
1359 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1360 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1361 ZIO_PRIORITY_SYNC_READ);
1363 for (int b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1368 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1369 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1370 FTAG, &db, DMU_READ_NO_PREFETCH);
1372 zap_stats_ptrtbl(zap, db->db_data,
1374 dmu_buf_rele(db, FTAG);