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 = zap_evict;
86 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
87 zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
91 * explicitly zero it since it might be coming from an
92 * initialized microzap
94 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
95 zp->zap_block_type = ZBT_HEADER;
96 zp->zap_magic = ZAP_MAGIC;
98 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
100 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
101 zp->zap_num_leafs = 1;
102 zp->zap_num_entries = 0;
103 zp->zap_salt = zap->zap_salt;
104 zp->zap_normflags = zap->zap_normflags;
105 zp->zap_flags = flags;
107 /* block 1 will be the first leaf */
108 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
109 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
112 * set up block 1 - the first leaf
114 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
115 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
116 dmu_buf_will_dirty(db, tx);
118 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
121 zap_leaf_init(l, zp->zap_normflags != 0);
123 kmem_free(l, sizeof (zap_leaf_t));
124 dmu_buf_rele(db, FTAG);
128 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
130 if (RW_WRITE_HELD(&zap->zap_rwlock))
132 if (rw_tryupgrade(&zap->zap_rwlock)) {
133 dmu_buf_will_dirty(zap->zap_dbuf, tx);
140 * Generic routines for dealing with the pointer & cookie tables.
144 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
145 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
149 dmu_buf_t *db_old, *db_new;
151 int bs = FZAP_BLOCK_SHIFT(zap);
152 int hepb = 1<<(bs-4);
153 /* hepb = half the number of entries in a block */
155 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
156 ASSERT(tbl->zt_blk != 0);
157 ASSERT(tbl->zt_numblks > 0);
159 if (tbl->zt_nextblk != 0) {
160 newblk = tbl->zt_nextblk;
162 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
163 tbl->zt_nextblk = newblk;
164 ASSERT0(tbl->zt_blks_copied);
165 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
166 tbl->zt_blk << bs, tbl->zt_numblks << bs,
167 ZIO_PRIORITY_SYNC_READ);
171 * Copy the ptrtbl from the old to new location.
174 b = tbl->zt_blks_copied;
175 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
176 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
180 /* first half of entries in old[b] go to new[2*b+0] */
181 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
182 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
183 dmu_buf_will_dirty(db_new, tx);
184 transfer_func(db_old->db_data, db_new->db_data, hepb);
185 dmu_buf_rele(db_new, FTAG);
187 /* second half of entries in old[b] go to new[2*b+1] */
188 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
189 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
190 dmu_buf_will_dirty(db_new, tx);
191 transfer_func((uint64_t *)db_old->db_data + hepb,
192 db_new->db_data, hepb);
193 dmu_buf_rele(db_new, FTAG);
195 dmu_buf_rele(db_old, FTAG);
197 tbl->zt_blks_copied++;
199 dprintf("copied block %llu of %llu\n",
200 tbl->zt_blks_copied, tbl->zt_numblks);
202 if (tbl->zt_blks_copied == tbl->zt_numblks) {
203 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
204 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
206 tbl->zt_blk = newblk;
207 tbl->zt_numblks *= 2;
210 tbl->zt_blks_copied = 0;
212 dprintf("finished; numblocks now %llu (%lluk entries)\n",
213 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
220 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
225 int bs = FZAP_BLOCK_SHIFT(zap);
228 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
229 ASSERT(tbl->zt_blk != 0);
231 dprintf("storing %llx at index %llx\n", val, idx);
234 off = idx & ((1<<(bs-3))-1);
236 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
237 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
240 dmu_buf_will_dirty(db, tx);
242 if (tbl->zt_nextblk != 0) {
243 uint64_t idx2 = idx * 2;
244 uint64_t blk2 = idx2 >> (bs-3);
245 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
248 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
249 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
250 DMU_READ_NO_PREFETCH);
252 dmu_buf_rele(db, FTAG);
255 dmu_buf_will_dirty(db2, tx);
256 ((uint64_t *)db2->db_data)[off2] = val;
257 ((uint64_t *)db2->db_data)[off2+1] = val;
258 dmu_buf_rele(db2, FTAG);
261 ((uint64_t *)db->db_data)[off] = val;
262 dmu_buf_rele(db, FTAG);
268 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
274 int bs = FZAP_BLOCK_SHIFT(zap);
276 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
279 off = idx & ((1<<(bs-3))-1);
282 * Note: this is equivalent to dmu_buf_hold(), but we use
283 * _dnode_enter / _by_dnode because it's faster because we don't
284 * have to hold the dnode.
286 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
287 err = dmu_buf_hold_by_dnode(dn,
288 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
289 dmu_buf_dnode_exit(zap->zap_dbuf);
292 *valp = ((uint64_t *)db->db_data)[off];
293 dmu_buf_rele(db, FTAG);
295 if (tbl->zt_nextblk != 0) {
297 * read the nextblk for the sake of i/o error checking,
298 * so that zap_table_load() will catch errors for
301 blk = (idx*2) >> (bs-3);
303 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
304 err = dmu_buf_hold_by_dnode(dn,
305 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
306 DMU_READ_NO_PREFETCH);
307 dmu_buf_dnode_exit(zap->zap_dbuf);
309 dmu_buf_rele(db, FTAG);
315 * Routines for growing the ptrtbl.
319 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
322 for (i = 0; i < n; i++) {
323 uint64_t lb = src[i];
330 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
333 * The pointer table should never use more hash bits than we
334 * have (otherwise we'd be using useless zero bits to index it).
335 * If we are within 2 bits of running out, stop growing, since
336 * this is already an aberrant condition.
338 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
339 return (SET_ERROR(ENOSPC));
341 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
343 * We are outgrowing the "embedded" ptrtbl (the one
344 * stored in the header block). Give it its own entire
345 * block, which will double the size of the ptrtbl.
351 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
352 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
353 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
355 newblk = zap_allocate_blocks(zap, 1);
356 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
357 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
358 DMU_READ_NO_PREFETCH);
361 dmu_buf_will_dirty(db_new, tx);
362 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
363 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
364 dmu_buf_rele(db_new, FTAG);
366 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
367 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
368 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
370 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
371 zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
372 (FZAP_BLOCK_SHIFT(zap)-3));
376 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
377 zap_ptrtbl_transfer, tx));
382 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
384 dmu_buf_will_dirty(zap->zap_dbuf, tx);
385 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
386 ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
387 zap_f_phys(zap)->zap_num_entries += delta;
388 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
392 zap_allocate_blocks(zap_t *zap, int nblocks)
395 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
396 newblk = zap_f_phys(zap)->zap_freeblk;
397 zap_f_phys(zap)->zap_freeblk += nblocks;
402 zap_leaf_pageout(void *dbu)
406 rw_destroy(&l->l_rwlock);
407 kmem_free(l, sizeof (zap_leaf_t));
411 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
414 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
416 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
418 rw_init(&l->l_rwlock, 0, 0, 0);
419 rw_enter(&l->l_rwlock, RW_WRITER);
420 l->l_blkid = zap_allocate_blocks(zap, 1);
423 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
424 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
425 DMU_READ_NO_PREFETCH));
426 dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
427 winner = dmu_buf_set_user(l->l_dbuf, &l->l_dbu);
428 ASSERT(winner == NULL);
429 dmu_buf_will_dirty(l->l_dbuf, tx);
431 zap_leaf_init(l, zap->zap_normflags != 0);
433 zap_f_phys(zap)->zap_num_leafs++;
439 fzap_count(zap_t *zap, uint64_t *count)
441 ASSERT(!zap->zap_ismicro);
442 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
443 *count = zap_f_phys(zap)->zap_num_entries;
444 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
449 * Routines for obtaining zap_leaf_t's
453 zap_put_leaf(zap_leaf_t *l)
455 rw_exit(&l->l_rwlock);
456 dmu_buf_rele(l->l_dbuf, NULL);
460 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
462 zap_leaf_t *l, *winner;
466 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
467 rw_init(&l->l_rwlock, 0, 0, 0);
468 rw_enter(&l->l_rwlock, RW_WRITER);
470 l->l_bs = highbit64(db->db_size) - 1;
473 dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
474 winner = dmu_buf_set_user(db, &l->l_dbu);
476 rw_exit(&l->l_rwlock);
477 if (winner != NULL) {
478 /* someone else set it first */
479 zap_leaf_pageout(&l->l_dbu);
484 * lhr_pad was previously used for the next leaf in the leaf
485 * chain. There should be no chained leafs (as we have removed
488 ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
491 * There should be more hash entries than there can be
492 * chunks to put in the hash table
494 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
496 /* The chunks should begin at the end of the hash table */
497 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
498 &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
500 /* The chunks should end at the end of the block */
501 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
502 (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
508 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
513 int bs = FZAP_BLOCK_SHIFT(zap);
516 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
518 dnode_t *dn = dmu_buf_dnode_enter(zap->zap_dbuf);
519 err = dmu_buf_hold_by_dnode(dn,
520 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
521 dmu_buf_dnode_exit(zap->zap_dbuf);
525 ASSERT3U(db->db_object, ==, zap->zap_object);
526 ASSERT3U(db->db_offset, ==, blkid << bs);
527 ASSERT3U(db->db_size, ==, 1 << bs);
530 l = dmu_buf_get_user(db);
533 l = zap_open_leaf(blkid, db);
535 rw_enter(&l->l_rwlock, lt);
537 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
538 * causing ASSERT below to fail.
541 dmu_buf_will_dirty(db, tx);
542 ASSERT3U(l->l_blkid, ==, blkid);
543 ASSERT3P(l->l_dbuf, ==, db);
544 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
545 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
552 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
554 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
556 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
558 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
559 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
562 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
568 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
571 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
573 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
574 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
577 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
583 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
588 ASSERT(zap->zap_dbuf == NULL ||
589 zap_f_phys(zap) == zap->zap_dbuf->db_data);
591 /* Reality check for corrupt zap objects (leaf or header). */
592 if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
593 zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
594 zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
595 return (SET_ERROR(EIO));
598 idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
599 err = zap_idx_to_blk(zap, idx, &blk);
602 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
605 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
606 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
611 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
612 void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
614 zap_t *zap = zn->zn_zap;
615 uint64_t hash = zn->zn_hash;
617 int prefix_diff, i, err;
619 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
621 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
622 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
624 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
625 zap_leaf_phys(l)->l_hdr.lh_prefix);
627 if (zap_tryupgradedir(zap, tx) == 0 ||
628 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
629 /* We failed to upgrade, or need to grow the pointer table */
630 objset_t *os = zap->zap_objset;
631 uint64_t object = zap->zap_object;
634 zap_unlockdir(zap, tag);
635 err = zap_lockdir(os, object, tx, RW_WRITER,
636 FALSE, FALSE, tag, &zn->zn_zap);
640 ASSERT(!zap->zap_ismicro);
642 while (old_prefix_len ==
643 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
644 err = zap_grow_ptrtbl(zap, tx);
649 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
653 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
654 /* it split while our locks were down */
659 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
660 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
661 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
662 zap_leaf_phys(l)->l_hdr.lh_prefix);
664 prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
665 (old_prefix_len + 1);
666 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
668 /* check for i/o errors before doing zap_leaf_split */
669 for (i = 0; i < (1ULL<<prefix_diff); i++) {
671 err = zap_idx_to_blk(zap, sibling+i, &blk);
674 ASSERT3U(blk, ==, l->l_blkid);
677 nl = zap_create_leaf(zap, tx);
678 zap_leaf_split(l, nl, zap->zap_normflags != 0);
680 /* set sibling pointers */
681 for (i = 0; i < (1ULL << prefix_diff); i++) {
682 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
683 ASSERT0(err); /* we checked for i/o errors above */
686 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
687 /* we want the sibling */
699 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
700 void *tag, dmu_tx_t *tx)
702 zap_t *zap = zn->zn_zap;
703 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
704 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
705 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
709 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
713 * We are in the middle of growing the pointer table, or
714 * this leaf will soon make us grow it.
716 if (zap_tryupgradedir(zap, tx) == 0) {
717 objset_t *os = zap->zap_objset;
718 uint64_t zapobj = zap->zap_object;
720 zap_unlockdir(zap, tag);
721 err = zap_lockdir(os, zapobj, tx,
722 RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
728 /* could have finished growing while our locks were down */
729 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
730 (void) zap_grow_ptrtbl(zap, tx);
735 fzap_checkname(zap_name_t *zn)
737 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
738 return (SET_ERROR(ENAMETOOLONG));
743 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
745 /* Only integer sizes supported by C */
746 switch (integer_size) {
753 return (SET_ERROR(EINVAL));
756 if (integer_size * num_integers > ZAP_MAXVALUELEN)
763 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
767 if ((err = fzap_checkname(zn)) != 0)
769 return (fzap_checksize(integer_size, num_integers));
773 * Routines for manipulating attributes.
776 fzap_lookup(zap_name_t *zn,
777 uint64_t integer_size, uint64_t num_integers, void *buf,
778 char *realname, int rn_len, boolean_t *ncp)
782 zap_entry_handle_t zeh;
784 if ((err = fzap_checkname(zn)) != 0)
787 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
790 err = zap_leaf_lookup(l, zn, &zeh);
792 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
797 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
798 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
800 *ncp = zap_entry_normalization_conflict(&zeh,
801 zn, NULL, zn->zn_zap);
810 fzap_add_cd(zap_name_t *zn,
811 uint64_t integer_size, uint64_t num_integers,
812 const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
816 zap_entry_handle_t zeh;
817 zap_t *zap = zn->zn_zap;
819 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
820 ASSERT(!zap->zap_ismicro);
821 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
823 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
827 err = zap_leaf_lookup(l, zn, &zeh);
829 err = SET_ERROR(EEXIST);
835 err = zap_entry_create(l, zn, cd,
836 integer_size, num_integers, val, &zeh);
839 zap_increment_num_entries(zap, 1, tx);
840 } else if (err == EAGAIN) {
841 err = zap_expand_leaf(zn, l, tag, tx, &l);
842 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
849 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
854 fzap_add(zap_name_t *zn,
855 uint64_t integer_size, uint64_t num_integers,
856 const void *val, void *tag, dmu_tx_t *tx)
858 int err = fzap_check(zn, integer_size, num_integers);
862 return (fzap_add_cd(zn, integer_size, num_integers,
863 val, ZAP_NEED_CD, tag, tx));
867 fzap_update(zap_name_t *zn,
868 int integer_size, uint64_t num_integers, const void *val,
869 void *tag, dmu_tx_t *tx)
873 zap_entry_handle_t zeh;
874 zap_t *zap = zn->zn_zap;
876 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
877 err = fzap_check(zn, integer_size, num_integers);
881 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
885 err = zap_leaf_lookup(l, zn, &zeh);
886 create = (err == ENOENT);
887 ASSERT(err == 0 || err == ENOENT);
890 err = zap_entry_create(l, zn, ZAP_NEED_CD,
891 integer_size, num_integers, val, &zeh);
893 zap_increment_num_entries(zap, 1, tx);
895 err = zap_entry_update(&zeh, integer_size, num_integers, val);
899 err = zap_expand_leaf(zn, l, tag, tx, &l);
900 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
906 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
911 fzap_length(zap_name_t *zn,
912 uint64_t *integer_size, uint64_t *num_integers)
916 zap_entry_handle_t zeh;
918 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
921 err = zap_leaf_lookup(l, zn, &zeh);
926 *integer_size = zeh.zeh_integer_size;
928 *num_integers = zeh.zeh_num_integers;
935 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
939 zap_entry_handle_t zeh;
941 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
944 err = zap_leaf_lookup(l, zn, &zeh);
946 zap_entry_remove(&zeh);
947 zap_increment_num_entries(zn->zn_zap, -1, tx);
954 fzap_prefetch(zap_name_t *zn)
957 zap_t *zap = zn->zn_zap;
960 idx = ZAP_HASH_IDX(zn->zn_hash,
961 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
962 if (zap_idx_to_blk(zap, idx, &blk) != 0)
964 bs = FZAP_BLOCK_SHIFT(zap);
965 dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
966 ZIO_PRIORITY_SYNC_READ);
970 * Helper functions for consumers.
974 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
975 const char *name, dmu_tx_t *tx)
979 VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
980 VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
987 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
997 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
998 for (zap_cursor_init(&zc, os, zapobj);
999 (err = zap_cursor_retrieve(&zc, za)) == 0;
1000 zap_cursor_advance(&zc)) {
1001 if ((za->za_first_integer & mask) == (value & mask)) {
1002 (void) strcpy(name, za->za_name);
1006 zap_cursor_fini(&zc);
1007 kmem_free(za, sizeof (zap_attribute_t));
1012 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1019 for (zap_cursor_init(&zc, os, fromobj);
1020 zap_cursor_retrieve(&zc, &za) == 0;
1021 (void) zap_cursor_advance(&zc)) {
1022 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1023 err = SET_ERROR(EINVAL);
1026 err = zap_add(os, intoobj, za.za_name,
1027 8, 1, &za.za_first_integer, tx);
1031 zap_cursor_fini(&zc);
1036 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1037 uint64_t value, dmu_tx_t *tx)
1044 for (zap_cursor_init(&zc, os, fromobj);
1045 zap_cursor_retrieve(&zc, &za) == 0;
1046 (void) zap_cursor_advance(&zc)) {
1047 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1048 err = SET_ERROR(EINVAL);
1051 err = zap_add(os, intoobj, za.za_name,
1056 zap_cursor_fini(&zc);
1061 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1069 for (zap_cursor_init(&zc, os, fromobj);
1070 zap_cursor_retrieve(&zc, &za) == 0;
1071 (void) zap_cursor_advance(&zc)) {
1074 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1075 err = SET_ERROR(EINVAL);
1079 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1080 if (err != 0 && err != ENOENT)
1082 delta += za.za_first_integer;
1083 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1087 zap_cursor_fini(&zc);
1092 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1096 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1097 return (zap_add(os, obj, name, 8, 1, &value, tx));
1101 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1105 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1106 return (zap_remove(os, obj, name, tx));
1110 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1114 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1115 return (zap_lookup(os, obj, name, 8, 1, &value));
1119 zap_add_int_key(objset_t *os, uint64_t obj,
1120 uint64_t key, uint64_t value, dmu_tx_t *tx)
1124 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1125 return (zap_add(os, obj, name, 8, 1, &value, tx));
1129 zap_update_int_key(objset_t *os, uint64_t obj,
1130 uint64_t key, uint64_t value, dmu_tx_t *tx)
1134 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1135 return (zap_update(os, obj, name, 8, 1, &value, tx));
1139 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1143 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1144 return (zap_lookup(os, obj, name, 8, 1, valuep));
1148 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1157 err = zap_lookup(os, obj, name, 8, 1, &value);
1158 if (err != 0 && err != ENOENT)
1162 err = zap_remove(os, obj, name, tx);
1164 err = zap_update(os, obj, name, 8, 1, &value, tx);
1169 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1174 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1175 return (zap_increment(os, obj, name, delta, tx));
1179 * Routines for iterating over the attributes.
1183 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1186 zap_entry_handle_t zeh;
1189 /* retrieve the next entry at or after zc_hash/zc_cd */
1190 /* if no entry, return ENOENT */
1193 (ZAP_HASH_IDX(zc->zc_hash,
1194 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1195 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1196 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1197 zap_put_leaf(zc->zc_leaf);
1202 if (zc->zc_leaf == NULL) {
1203 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1208 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1212 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1214 if (err == ENOENT) {
1216 (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1217 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1219 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1221 zc->zc_hash = -1ULL;
1223 zap_put_leaf(zc->zc_leaf);
1230 zc->zc_hash = zeh.zeh_hash;
1231 zc->zc_cd = zeh.zeh_cd;
1232 za->za_integer_length = zeh.zeh_integer_size;
1233 za->za_num_integers = zeh.zeh_num_integers;
1234 if (zeh.zeh_num_integers == 0) {
1235 za->za_first_integer = 0;
1237 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1238 ASSERT(err == 0 || err == EOVERFLOW);
1240 err = zap_entry_read_name(zap, &zeh,
1241 sizeof (za->za_name), za->za_name);
1244 za->za_normalization_conflict =
1245 zap_entry_normalization_conflict(&zeh,
1246 NULL, za->za_name, zap);
1248 rw_exit(&zc->zc_leaf->l_rwlock);
1253 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1256 uint64_t lastblk = 0;
1259 * NB: if a leaf has more pointers than an entire ptrtbl block
1260 * can hold, then it'll be accounted for more than once, since
1261 * we won't have lastblk.
1263 for (i = 0; i < len; i++) {
1266 if (tbl[i] == lastblk)
1270 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1272 zap_leaf_stats(zap, l, zs);
1279 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1283 zap_entry_handle_t zeh;
1285 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1286 return (SET_ERROR(ENAMETOOLONG));
1288 err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1292 err = zap_leaf_lookup(l, zn, &zeh);
1297 zc->zc_hash = zeh.zeh_hash;
1298 zc->zc_cd = zeh.zeh_cd;
1304 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1306 int bs = FZAP_BLOCK_SHIFT(zap);
1307 zs->zs_blocksize = 1ULL << bs;
1310 * Set zap_phys_t fields
1312 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1313 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1314 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1315 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1316 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1317 zs->zs_salt = zap_f_phys(zap)->zap_salt;
1320 * Set zap_ptrtbl fields
1322 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1323 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1324 zs->zs_ptrtbl_blks_copied =
1325 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1326 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1327 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1328 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1330 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1331 /* the ptrtbl is entirely in the header block. */
1332 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1333 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1337 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
1338 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1339 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1340 ZIO_PRIORITY_SYNC_READ);
1342 for (b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1347 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1348 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1349 FTAG, &db, DMU_READ_NO_PREFETCH);
1351 zap_stats_ptrtbl(zap, db->db_data,
1353 dmu_buf_rele(db, FTAG);
1360 fzap_count_write(zap_name_t *zn, int add, refcount_t *towrite,
1361 refcount_t *tooverwrite)
1363 zap_t *zap = zn->zn_zap;
1368 * Account for the header block of the fatzap.
1370 if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
1371 (void) refcount_add_many(tooverwrite,
1372 zap->zap_dbuf->db_size, FTAG);
1374 (void) refcount_add_many(towrite,
1375 zap->zap_dbuf->db_size, FTAG);
1379 * Account for the pointer table blocks.
1380 * If we are adding we need to account for the following cases :
1381 * - If the pointer table is embedded, this operation could force an
1382 * external pointer table.
1383 * - If this already has an external pointer table this operation
1384 * could extend the table.
1387 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
1388 (void) refcount_add_many(towrite,
1389 zap->zap_dbuf->db_size, FTAG);
1391 (void) refcount_add_many(towrite,
1392 zap->zap_dbuf->db_size * 3, FTAG);
1397 * Now, check if the block containing leaf is freeable
1398 * and account accordingly.
1400 err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1405 if (!add && dmu_buf_freeable(l->l_dbuf)) {
1406 (void) refcount_add_many(tooverwrite, l->l_dbuf->db_size, FTAG);
1409 * If this an add operation, the leaf block could split.
1410 * Hence, we need to account for an additional leaf block.
1412 (void) refcount_add_many(towrite,
1413 (add ? 2 : 1) * l->l_dbuf->db_size, FTAG);