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 int 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, MUTEX_DEFAULT, 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 (%uk 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, NULL, RW_NOLOCKDEP, NULL);
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, NULL, RW_DEFAULT, NULL);
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), ==, (zap_leaf_chunk_t *)
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));
527 * If system crashed just after dmu_free_long_range in zfs_rmnode, we
528 * would be left with an empty xattr dir in delete queue. blkid=0
529 * would be passed in when doing zfs_purgedir. If that's the case we
530 * should just return immediately. The underlying objects should
531 * already be freed, so this should be perfectly fine.
534 return (SET_ERROR(ENOENT));
536 int bs = FZAP_BLOCK_SHIFT(zap);
537 dnode_t *dn = dmu_buf_dnode_enter(zap->zap_dbuf);
538 int err = dmu_buf_hold_by_dnode(dn,
539 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
540 dmu_buf_dnode_exit(zap->zap_dbuf);
544 ASSERT3U(db->db_object, ==, zap->zap_object);
545 ASSERT3U(db->db_offset, ==, blkid << bs);
546 ASSERT3U(db->db_size, ==, 1 << bs);
549 zap_leaf_t *l = dmu_buf_get_user(db);
552 l = zap_open_leaf(blkid, db);
554 rw_enter(&l->l_rwlock, lt);
556 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
557 * causing ASSERT below to fail.
560 dmu_buf_will_dirty(db, tx);
561 ASSERT3U(l->l_blkid, ==, blkid);
562 ASSERT3P(l->l_dbuf, ==, db);
563 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
564 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
571 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
573 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
575 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
577 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
578 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
581 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
587 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
590 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
592 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
593 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
596 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
602 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
606 ASSERT(zap->zap_dbuf == NULL ||
607 zap_f_phys(zap) == zap->zap_dbuf->db_data);
609 /* Reality check for corrupt zap objects (leaf or header). */
610 if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
611 zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
612 zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
613 return (SET_ERROR(EIO));
616 uint64_t idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
617 int err = zap_idx_to_blk(zap, idx, &blk);
620 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
623 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
624 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
629 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
630 void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
632 zap_t *zap = zn->zn_zap;
633 uint64_t hash = zn->zn_hash;
635 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
637 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
638 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
640 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
641 zap_leaf_phys(l)->l_hdr.lh_prefix);
643 if (zap_tryupgradedir(zap, tx) == 0 ||
644 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
645 /* We failed to upgrade, or need to grow the pointer table */
646 objset_t *os = zap->zap_objset;
647 uint64_t object = zap->zap_object;
650 zap_unlockdir(zap, tag);
651 err = zap_lockdir(os, object, tx, RW_WRITER,
652 FALSE, FALSE, tag, &zn->zn_zap);
656 ASSERT(!zap->zap_ismicro);
658 while (old_prefix_len ==
659 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
660 err = zap_grow_ptrtbl(zap, tx);
665 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
669 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
670 /* it split while our locks were down */
675 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
676 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
677 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
678 zap_leaf_phys(l)->l_hdr.lh_prefix);
680 int prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
681 (old_prefix_len + 1);
683 (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
685 /* check for i/o errors before doing zap_leaf_split */
686 for (int i = 0; i < (1ULL << prefix_diff); i++) {
688 err = zap_idx_to_blk(zap, sibling + i, &blk);
691 ASSERT3U(blk, ==, l->l_blkid);
694 zap_leaf_t *nl = zap_create_leaf(zap, tx);
695 zap_leaf_split(l, nl, zap->zap_normflags != 0);
697 /* set sibling pointers */
698 for (int i = 0; i < (1ULL << prefix_diff); i++) {
699 err = zap_set_idx_to_blk(zap, sibling + i, nl->l_blkid, tx);
700 ASSERT0(err); /* we checked for i/o errors above */
703 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_prefix_len, >, 0);
705 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
706 /* we want the sibling */
718 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
719 void *tag, dmu_tx_t *tx)
721 zap_t *zap = zn->zn_zap;
722 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
723 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
724 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
728 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
730 * We are in the middle of growing the pointer table, or
731 * this leaf will soon make us grow it.
733 if (zap_tryupgradedir(zap, tx) == 0) {
734 objset_t *os = zap->zap_objset;
735 uint64_t zapobj = zap->zap_object;
737 zap_unlockdir(zap, tag);
738 int err = zap_lockdir(os, zapobj, tx,
739 RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
745 /* could have finished growing while our locks were down */
746 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
747 (void) zap_grow_ptrtbl(zap, tx);
752 fzap_checkname(zap_name_t *zn)
754 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
755 return (SET_ERROR(ENAMETOOLONG));
760 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
762 /* Only integer sizes supported by C */
763 switch (integer_size) {
770 return (SET_ERROR(EINVAL));
773 if (integer_size * num_integers > ZAP_MAXVALUELEN)
774 return (SET_ERROR(E2BIG));
780 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
782 int err = fzap_checkname(zn);
785 return (fzap_checksize(integer_size, num_integers));
789 * Routines for manipulating attributes.
792 fzap_lookup(zap_name_t *zn,
793 uint64_t integer_size, uint64_t num_integers, void *buf,
794 char *realname, int rn_len, boolean_t *ncp)
797 zap_entry_handle_t zeh;
799 int err = fzap_checkname(zn);
803 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
806 err = zap_leaf_lookup(l, zn, &zeh);
808 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
813 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
814 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
816 *ncp = zap_entry_normalization_conflict(&zeh,
817 zn, NULL, zn->zn_zap);
826 fzap_add_cd(zap_name_t *zn,
827 uint64_t integer_size, uint64_t num_integers,
828 const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
832 zap_entry_handle_t zeh;
833 zap_t *zap = zn->zn_zap;
835 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
836 ASSERT(!zap->zap_ismicro);
837 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
839 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
843 err = zap_leaf_lookup(l, zn, &zeh);
845 err = SET_ERROR(EEXIST);
851 err = zap_entry_create(l, zn, cd,
852 integer_size, num_integers, val, &zeh);
855 zap_increment_num_entries(zap, 1, tx);
856 } else if (err == EAGAIN) {
857 err = zap_expand_leaf(zn, l, tag, tx, &l);
858 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
861 } else if (err == ENOSPC) {
863 * If we failed to expand the leaf, then bailout
864 * as there is no point trying
865 * zap_put_leaf_maybe_grow_ptrtbl().
873 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
878 fzap_add(zap_name_t *zn,
879 uint64_t integer_size, uint64_t num_integers,
880 const void *val, void *tag, dmu_tx_t *tx)
882 int err = fzap_check(zn, integer_size, num_integers);
886 return (fzap_add_cd(zn, integer_size, num_integers,
887 val, ZAP_NEED_CD, tag, tx));
891 fzap_update(zap_name_t *zn,
892 int integer_size, uint64_t num_integers, const void *val,
893 void *tag, dmu_tx_t *tx)
898 zap_entry_handle_t zeh;
899 zap_t *zap = zn->zn_zap;
901 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
902 err = fzap_check(zn, integer_size, num_integers);
906 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
910 err = zap_leaf_lookup(l, zn, &zeh);
911 create = (err == ENOENT);
912 ASSERT(err == 0 || err == ENOENT);
915 err = zap_entry_create(l, zn, ZAP_NEED_CD,
916 integer_size, num_integers, val, &zeh);
918 zap_increment_num_entries(zap, 1, tx);
920 err = zap_entry_update(&zeh, integer_size, num_integers, val);
924 err = zap_expand_leaf(zn, l, tag, tx, &l);
925 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
931 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
936 fzap_length(zap_name_t *zn,
937 uint64_t *integer_size, uint64_t *num_integers)
941 zap_entry_handle_t zeh;
943 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
946 err = zap_leaf_lookup(l, zn, &zeh);
950 if (integer_size != 0)
951 *integer_size = zeh.zeh_integer_size;
952 if (num_integers != 0)
953 *num_integers = zeh.zeh_num_integers;
960 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
964 zap_entry_handle_t zeh;
966 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
969 err = zap_leaf_lookup(l, zn, &zeh);
971 zap_entry_remove(&zeh);
972 zap_increment_num_entries(zn->zn_zap, -1, tx);
979 fzap_prefetch(zap_name_t *zn)
982 zap_t *zap = zn->zn_zap;
984 uint64_t idx = ZAP_HASH_IDX(zn->zn_hash,
985 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
986 if (zap_idx_to_blk(zap, idx, &blk) != 0)
988 int bs = FZAP_BLOCK_SHIFT(zap);
989 dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
990 ZIO_PRIORITY_SYNC_READ);
994 * Helper functions for consumers.
998 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
999 const char *name, dmu_tx_t *tx)
1001 return (zap_create_link_dnsize(os, ot, parent_obj, name, 0, tx));
1005 zap_create_link_dnsize(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
1006 const char *name, int dnodesize, dmu_tx_t *tx)
1010 new_obj = zap_create_dnsize(os, ot, DMU_OT_NONE, 0, dnodesize, tx);
1011 VERIFY(new_obj != 0);
1012 VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
1019 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
1028 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1029 for (zap_cursor_init(&zc, os, zapobj);
1030 (err = zap_cursor_retrieve(&zc, za)) == 0;
1031 zap_cursor_advance(&zc)) {
1032 if ((za->za_first_integer & mask) == (value & mask)) {
1033 (void) strcpy(name, za->za_name);
1037 zap_cursor_fini(&zc);
1038 kmem_free(za, sizeof (*za));
1043 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1048 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1049 for (zap_cursor_init(&zc, os, fromobj);
1050 zap_cursor_retrieve(&zc, za) == 0;
1051 (void) zap_cursor_advance(&zc)) {
1052 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1053 err = SET_ERROR(EINVAL);
1056 err = zap_add(os, intoobj, za->za_name,
1057 8, 1, &za->za_first_integer, tx);
1061 zap_cursor_fini(&zc);
1062 kmem_free(za, sizeof (*za));
1067 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1068 uint64_t value, dmu_tx_t *tx)
1073 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1074 for (zap_cursor_init(&zc, os, fromobj);
1075 zap_cursor_retrieve(&zc, za) == 0;
1076 (void) zap_cursor_advance(&zc)) {
1077 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1078 err = SET_ERROR(EINVAL);
1081 err = zap_add(os, intoobj, za->za_name,
1086 zap_cursor_fini(&zc);
1087 kmem_free(za, sizeof (*za));
1092 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1098 zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1099 for (zap_cursor_init(&zc, os, fromobj);
1100 zap_cursor_retrieve(&zc, za) == 0;
1101 (void) zap_cursor_advance(&zc)) {
1104 if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1105 err = SET_ERROR(EINVAL);
1109 err = zap_lookup(os, intoobj, za->za_name, 8, 1, &delta);
1110 if (err != 0 && err != ENOENT)
1112 delta += za->za_first_integer;
1113 err = zap_update(os, intoobj, za->za_name, 8, 1, &delta, tx);
1117 zap_cursor_fini(&zc);
1118 kmem_free(za, sizeof (*za));
1123 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1127 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1128 return (zap_add(os, obj, name, 8, 1, &value, tx));
1132 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1136 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1137 return (zap_remove(os, obj, name, tx));
1141 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1145 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1146 return (zap_lookup(os, obj, name, 8, 1, &value));
1150 zap_add_int_key(objset_t *os, uint64_t obj,
1151 uint64_t key, uint64_t value, dmu_tx_t *tx)
1155 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1156 return (zap_add(os, obj, name, 8, 1, &value, tx));
1160 zap_update_int_key(objset_t *os, uint64_t obj,
1161 uint64_t key, uint64_t value, dmu_tx_t *tx)
1165 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1166 return (zap_update(os, obj, name, 8, 1, &value, tx));
1170 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1174 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1175 return (zap_lookup(os, obj, name, 8, 1, valuep));
1179 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1187 int err = zap_lookup(os, obj, name, 8, 1, &value);
1188 if (err != 0 && err != ENOENT)
1192 err = zap_remove(os, obj, name, tx);
1194 err = zap_update(os, obj, name, 8, 1, &value, tx);
1199 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1204 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1205 return (zap_increment(os, obj, name, delta, tx));
1209 * Routines for iterating over the attributes.
1213 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1216 zap_entry_handle_t zeh;
1219 /* retrieve the next entry at or after zc_hash/zc_cd */
1220 /* if no entry, return ENOENT */
1223 * If we are reading from the beginning, we're almost certain to
1224 * iterate over the entire ZAP object. If there are multiple leaf
1225 * blocks (freeblk > 2), prefetch the whole object (up to
1226 * dmu_prefetch_max bytes), so that we read the leaf blocks
1227 * concurrently. (Unless noprefetch was requested via
1228 * zap_cursor_init_noprefetch()).
1230 if (zc->zc_hash == 0 && zap_iterate_prefetch &&
1231 zc->zc_prefetch && zap_f_phys(zap)->zap_freeblk > 2) {
1232 dmu_prefetch(zc->zc_objset, zc->zc_zapobj, 0, 0,
1233 zap_f_phys(zap)->zap_freeblk << FZAP_BLOCK_SHIFT(zap),
1234 ZIO_PRIORITY_ASYNC_READ);
1238 (ZAP_HASH_IDX(zc->zc_hash,
1239 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1240 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1241 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1242 zap_put_leaf(zc->zc_leaf);
1247 if (zc->zc_leaf == NULL) {
1248 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1253 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1257 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1259 if (err == ENOENT) {
1260 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0) {
1261 zc->zc_hash = -1ULL;
1264 uint64_t nocare = (1ULL <<
1265 (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1267 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1270 if (zc->zc_hash == 0) {
1271 zc->zc_hash = -1ULL;
1273 zap_put_leaf(zc->zc_leaf);
1281 zc->zc_hash = zeh.zeh_hash;
1282 zc->zc_cd = zeh.zeh_cd;
1283 za->za_integer_length = zeh.zeh_integer_size;
1284 za->za_num_integers = zeh.zeh_num_integers;
1285 if (zeh.zeh_num_integers == 0) {
1286 za->za_first_integer = 0;
1288 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1289 ASSERT(err == 0 || err == EOVERFLOW);
1291 err = zap_entry_read_name(zap, &zeh,
1292 sizeof (za->za_name), za->za_name);
1295 za->za_normalization_conflict =
1296 zap_entry_normalization_conflict(&zeh,
1297 NULL, za->za_name, zap);
1299 rw_exit(&zc->zc_leaf->l_rwlock);
1304 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1306 uint64_t lastblk = 0;
1309 * NB: if a leaf has more pointers than an entire ptrtbl block
1310 * can hold, then it'll be accounted for more than once, since
1311 * we won't have lastblk.
1313 for (int i = 0; i < len; i++) {
1316 if (tbl[i] == lastblk)
1320 int err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1322 zap_leaf_stats(zap, l, zs);
1329 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1331 int bs = FZAP_BLOCK_SHIFT(zap);
1332 zs->zs_blocksize = 1ULL << bs;
1335 * Set zap_phys_t fields
1337 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1338 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1339 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1340 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1341 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1342 zs->zs_salt = zap_f_phys(zap)->zap_salt;
1345 * Set zap_ptrtbl fields
1347 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1348 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1349 zs->zs_ptrtbl_blks_copied =
1350 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1351 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1352 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1353 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1355 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1356 /* the ptrtbl is entirely in the header block. */
1357 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1358 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1360 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
1361 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1362 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1363 ZIO_PRIORITY_SYNC_READ);
1365 for (int b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1370 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1371 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1372 FTAG, &db, DMU_READ_NO_PREFETCH);
1374 zap_stats_ptrtbl(zap, db->db_data,
1376 dmu_buf_rele(db, FTAG);
1383 ZFS_MODULE_PARAM(zfs, , zap_iterate_prefetch, INT, ZMOD_RW,
1384 "When iterating ZAP object, prefetch it");