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.
26 * This file contains the top half of the zfs directory structure
27 * implementation. The bottom half is in zap_leaf.c.
29 * The zdir is an extendable hash data structure. There is a table of
30 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
31 * each a constant size and hold a variable number of directory entries.
32 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
34 * The pointer table holds a power of 2 number of pointers.
35 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
36 * by the pointer at index i in the table holds entries whose hash value
37 * has a zd_prefix_len - bit prefix
42 #include <sys/zfs_context.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/fs/zfs.h>
46 #include <sys/refcount.h>
47 #include <sys/zap_impl.h>
48 #include <sys/zap_leaf.h>
50 int fzap_default_block_shift = 14; /* 16k blocksize */
52 static void zap_leaf_pageout(dmu_buf_t *db, void *vl);
53 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
57 fzap_byteswap(void *vbuf, size_t size)
61 block_type = *(uint64_t *)vbuf;
63 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
64 zap_leaf_byteswap(vbuf, size);
66 /* it's a ptrtbl block */
67 byteswap_uint64_array(vbuf, size);
72 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
79 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
80 zap->zap_ismicro = FALSE;
82 (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap,
83 &zap->zap_f.zap_phys, zap_evict);
85 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
86 zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1;
88 zp = zap->zap_f.zap_phys;
90 * explicitly zero it since it might be coming from an
91 * initialized microzap
93 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
94 zp->zap_block_type = ZBT_HEADER;
95 zp->zap_magic = ZAP_MAGIC;
97 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
99 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
100 zp->zap_num_leafs = 1;
101 zp->zap_num_entries = 0;
102 zp->zap_salt = zap->zap_salt;
103 zp->zap_normflags = zap->zap_normflags;
104 zp->zap_flags = flags;
106 /* block 1 will be the first leaf */
107 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
108 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
111 * set up block 1 - the first leaf
113 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
114 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
115 dmu_buf_will_dirty(db, tx);
117 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
119 l->l_phys = db->db_data;
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 ASSERT3U(tbl->zt_blks_copied, ==, 0);
165 dmu_prefetch(zap->zap_objset, zap->zap_object,
166 tbl->zt_blk << bs, tbl->zt_numblks << bs);
170 * Copy the ptrtbl from the old to new location.
173 b = tbl->zt_blks_copied;
174 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
175 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
179 /* first half of entries in old[b] go to new[2*b+0] */
180 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
181 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
182 dmu_buf_will_dirty(db_new, tx);
183 transfer_func(db_old->db_data, db_new->db_data, hepb);
184 dmu_buf_rele(db_new, FTAG);
186 /* second half of entries in old[b] go to new[2*b+1] */
187 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
188 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
189 dmu_buf_will_dirty(db_new, tx);
190 transfer_func((uint64_t *)db_old->db_data + hepb,
191 db_new->db_data, hepb);
192 dmu_buf_rele(db_new, FTAG);
194 dmu_buf_rele(db_old, FTAG);
196 tbl->zt_blks_copied++;
198 dprintf("copied block %llu of %llu\n",
199 tbl->zt_blks_copied, tbl->zt_numblks);
201 if (tbl->zt_blks_copied == tbl->zt_numblks) {
202 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
203 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
205 tbl->zt_blk = newblk;
206 tbl->zt_numblks *= 2;
209 tbl->zt_blks_copied = 0;
211 dprintf("finished; numblocks now %llu (%lluk entries)\n",
212 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
219 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
224 int bs = FZAP_BLOCK_SHIFT(zap);
227 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
228 ASSERT(tbl->zt_blk != 0);
230 dprintf("storing %llx at index %llx\n", val, idx);
233 off = idx & ((1<<(bs-3))-1);
235 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
236 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
239 dmu_buf_will_dirty(db, tx);
241 if (tbl->zt_nextblk != 0) {
242 uint64_t idx2 = idx * 2;
243 uint64_t blk2 = idx2 >> (bs-3);
244 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
247 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
248 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
249 DMU_READ_NO_PREFETCH);
251 dmu_buf_rele(db, FTAG);
254 dmu_buf_will_dirty(db2, tx);
255 ((uint64_t *)db2->db_data)[off2] = val;
256 ((uint64_t *)db2->db_data)[off2+1] = val;
257 dmu_buf_rele(db2, FTAG);
260 ((uint64_t *)db->db_data)[off] = val;
261 dmu_buf_rele(db, FTAG);
267 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
272 int bs = FZAP_BLOCK_SHIFT(zap);
274 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
277 off = idx & ((1<<(bs-3))-1);
279 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
280 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
283 *valp = ((uint64_t *)db->db_data)[off];
284 dmu_buf_rele(db, FTAG);
286 if (tbl->zt_nextblk != 0) {
288 * read the nextblk for the sake of i/o error checking,
289 * so that zap_table_load() will catch errors for
292 blk = (idx*2) >> (bs-3);
294 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
295 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
296 DMU_READ_NO_PREFETCH);
297 dmu_buf_rele(db, FTAG);
303 * Routines for growing the ptrtbl.
307 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
310 for (i = 0; i < n; i++) {
311 uint64_t lb = src[i];
318 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
321 * The pointer table should never use more hash bits than we
322 * have (otherwise we'd be using useless zero bits to index it).
323 * If we are within 2 bits of running out, stop growing, since
324 * this is already an aberrant condition.
326 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
329 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
331 * We are outgrowing the "embedded" ptrtbl (the one
332 * stored in the header block). Give it its own entire
333 * block, which will double the size of the ptrtbl.
339 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
340 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
341 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_blk, ==, 0);
343 newblk = zap_allocate_blocks(zap, 1);
344 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
345 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
346 DMU_READ_NO_PREFETCH);
349 dmu_buf_will_dirty(db_new, tx);
350 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
351 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
352 dmu_buf_rele(db_new, FTAG);
354 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk = newblk;
355 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks = 1;
356 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift++;
358 ASSERT3U(1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
359 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks <<
360 (FZAP_BLOCK_SHIFT(zap)-3));
364 return (zap_table_grow(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
365 zap_ptrtbl_transfer, tx));
370 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
372 dmu_buf_will_dirty(zap->zap_dbuf, tx);
373 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
374 ASSERT(delta > 0 || zap->zap_f.zap_phys->zap_num_entries >= -delta);
375 zap->zap_f.zap_phys->zap_num_entries += delta;
376 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
380 zap_allocate_blocks(zap_t *zap, int nblocks)
383 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
384 newblk = zap->zap_f.zap_phys->zap_freeblk;
385 zap->zap_f.zap_phys->zap_freeblk += nblocks;
390 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
393 zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
395 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
397 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, NULL);
398 rw_enter(&l->l_rwlock, RW_WRITER);
399 l->l_blkid = zap_allocate_blocks(zap, 1);
403 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
404 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
405 DMU_READ_NO_PREFETCH));
406 winner = dmu_buf_set_user(l->l_dbuf, l, &l->l_phys, zap_leaf_pageout);
407 ASSERT(winner == NULL);
408 dmu_buf_will_dirty(l->l_dbuf, tx);
410 zap_leaf_init(l, zap->zap_normflags != 0);
412 zap->zap_f.zap_phys->zap_num_leafs++;
418 fzap_count(zap_t *zap, uint64_t *count)
420 ASSERT(!zap->zap_ismicro);
421 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
422 *count = zap->zap_f.zap_phys->zap_num_entries;
423 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
428 * Routines for obtaining zap_leaf_t's
432 zap_put_leaf(zap_leaf_t *l)
434 rw_exit(&l->l_rwlock);
435 dmu_buf_rele(l->l_dbuf, NULL);
440 zap_leaf_pageout(dmu_buf_t *db, void *vl)
444 rw_destroy(&l->l_rwlock);
445 kmem_free(l, sizeof (zap_leaf_t));
449 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
451 zap_leaf_t *l, *winner;
455 l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
456 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, NULL);
457 rw_enter(&l->l_rwlock, RW_WRITER);
459 l->l_bs = highbit(db->db_size)-1;
463 winner = dmu_buf_set_user(db, l, &l->l_phys, zap_leaf_pageout);
465 rw_exit(&l->l_rwlock);
466 if (winner != NULL) {
467 /* someone else set it first */
468 zap_leaf_pageout(NULL, l);
473 * lhr_pad was previously used for the next leaf in the leaf
474 * chain. There should be no chained leafs (as we have removed
477 ASSERT3U(l->l_phys->l_hdr.lh_pad1, ==, 0);
480 * There should be more hash entries than there can be
481 * chunks to put in the hash table
483 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
485 /* The chunks should begin at the end of the hash table */
486 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==, (zap_leaf_chunk_t *)
487 &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
489 /* The chunks should end at the end of the block */
490 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
491 (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size);
497 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
502 int bs = FZAP_BLOCK_SHIFT(zap);
505 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
507 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
508 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
512 ASSERT3U(db->db_object, ==, zap->zap_object);
513 ASSERT3U(db->db_offset, ==, blkid << bs);
514 ASSERT3U(db->db_size, ==, 1 << bs);
517 l = dmu_buf_get_user(db);
520 l = zap_open_leaf(blkid, db);
522 rw_enter(&l->l_rwlock, lt);
524 * Must lock before dirtying, otherwise l->l_phys could change,
525 * causing ASSERT below to fail.
528 dmu_buf_will_dirty(db, tx);
529 ASSERT3U(l->l_blkid, ==, blkid);
530 ASSERT3P(l->l_dbuf, ==, db);
531 ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data);
532 ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF);
533 ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
540 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
542 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
544 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
546 (1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift));
547 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
550 return (zap_table_load(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
556 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
559 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
561 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0) {
562 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
565 return (zap_table_store(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
571 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
576 ASSERT(zap->zap_dbuf == NULL ||
577 zap->zap_f.zap_phys == zap->zap_dbuf->db_data);
578 ASSERT3U(zap->zap_f.zap_phys->zap_magic, ==, ZAP_MAGIC);
579 idx = ZAP_HASH_IDX(h, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
580 err = zap_idx_to_blk(zap, idx, &blk);
583 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
585 ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) ==
586 (*lp)->l_phys->l_hdr.lh_prefix);
591 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp)
593 zap_t *zap = zn->zn_zap;
594 uint64_t hash = zn->zn_hash;
596 int prefix_diff, i, err;
598 int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
600 ASSERT3U(old_prefix_len, <=, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
601 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
603 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
604 l->l_phys->l_hdr.lh_prefix);
606 if (zap_tryupgradedir(zap, tx) == 0 ||
607 old_prefix_len == zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
608 /* We failed to upgrade, or need to grow the pointer table */
609 objset_t *os = zap->zap_objset;
610 uint64_t object = zap->zap_object;
614 err = zap_lockdir(os, object, tx, RW_WRITER,
615 FALSE, FALSE, &zn->zn_zap);
619 ASSERT(!zap->zap_ismicro);
621 while (old_prefix_len ==
622 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
623 err = zap_grow_ptrtbl(zap, tx);
628 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
632 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) {
633 /* it split while our locks were down */
638 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
639 ASSERT3U(old_prefix_len, <, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
640 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
641 l->l_phys->l_hdr.lh_prefix);
643 prefix_diff = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift -
644 (old_prefix_len + 1);
645 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
647 /* check for i/o errors before doing zap_leaf_split */
648 for (i = 0; i < (1ULL<<prefix_diff); i++) {
650 err = zap_idx_to_blk(zap, sibling+i, &blk);
653 ASSERT3U(blk, ==, l->l_blkid);
656 nl = zap_create_leaf(zap, tx);
657 zap_leaf_split(l, nl, zap->zap_normflags != 0);
659 /* set sibling pointers */
660 for (i = 0; i < (1ULL<<prefix_diff); i++) {
661 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
662 ASSERT3U(err, ==, 0); /* we checked for i/o errors above */
665 if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) {
666 /* we want the sibling */
678 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx)
680 zap_t *zap = zn->zn_zap;
681 int shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
682 int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift &&
683 l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
687 if (leaffull || zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk) {
691 * We are in the middle of growing the pointer table, or
692 * this leaf will soon make us grow it.
694 if (zap_tryupgradedir(zap, tx) == 0) {
695 objset_t *os = zap->zap_objset;
696 uint64_t zapobj = zap->zap_object;
699 err = zap_lockdir(os, zapobj, tx,
700 RW_WRITER, FALSE, FALSE, &zn->zn_zap);
706 /* could have finished growing while our locks were down */
707 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift == shift)
708 (void) zap_grow_ptrtbl(zap, tx);
713 fzap_checkname(zap_name_t *zn)
715 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
716 return (ENAMETOOLONG);
721 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
723 /* Only integer sizes supported by C */
724 switch (integer_size) {
734 if (integer_size * num_integers > ZAP_MAXVALUELEN)
741 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
745 if ((err = fzap_checkname(zn)) != 0)
747 return (fzap_checksize(integer_size, num_integers));
751 * Routines for manipulating attributes.
754 fzap_lookup(zap_name_t *zn,
755 uint64_t integer_size, uint64_t num_integers, void *buf,
756 char *realname, int rn_len, boolean_t *ncp)
760 zap_entry_handle_t zeh;
762 if ((err = fzap_checkname(zn)) != 0)
765 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
768 err = zap_leaf_lookup(l, zn, &zeh);
770 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
775 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
776 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
778 *ncp = zap_entry_normalization_conflict(&zeh,
779 zn, NULL, zn->zn_zap);
788 fzap_add_cd(zap_name_t *zn,
789 uint64_t integer_size, uint64_t num_integers,
790 const void *val, uint32_t cd, dmu_tx_t *tx)
794 zap_entry_handle_t zeh;
795 zap_t *zap = zn->zn_zap;
797 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
798 ASSERT(!zap->zap_ismicro);
799 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
801 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
805 err = zap_leaf_lookup(l, zn, &zeh);
813 err = zap_entry_create(l, zn, cd,
814 integer_size, num_integers, val, &zeh);
817 zap_increment_num_entries(zap, 1, tx);
818 } else if (err == EAGAIN) {
819 err = zap_expand_leaf(zn, l, tx, &l);
820 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
827 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
832 fzap_add(zap_name_t *zn,
833 uint64_t integer_size, uint64_t num_integers,
834 const void *val, dmu_tx_t *tx)
836 int err = fzap_check(zn, integer_size, num_integers);
840 return (fzap_add_cd(zn, integer_size, num_integers,
841 val, ZAP_NEED_CD, tx));
845 fzap_update(zap_name_t *zn,
846 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
850 zap_entry_handle_t zeh;
851 zap_t *zap = zn->zn_zap;
853 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
854 err = fzap_check(zn, integer_size, num_integers);
858 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
862 err = zap_leaf_lookup(l, zn, &zeh);
863 create = (err == ENOENT);
864 ASSERT(err == 0 || err == ENOENT);
867 err = zap_entry_create(l, zn, ZAP_NEED_CD,
868 integer_size, num_integers, val, &zeh);
870 zap_increment_num_entries(zap, 1, tx);
872 err = zap_entry_update(&zeh, integer_size, num_integers, val);
876 err = zap_expand_leaf(zn, l, tx, &l);
877 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
883 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
888 fzap_length(zap_name_t *zn,
889 uint64_t *integer_size, uint64_t *num_integers)
893 zap_entry_handle_t zeh;
895 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
898 err = zap_leaf_lookup(l, zn, &zeh);
903 *integer_size = zeh.zeh_integer_size;
905 *num_integers = zeh.zeh_num_integers;
912 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
916 zap_entry_handle_t zeh;
918 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
921 err = zap_leaf_lookup(l, zn, &zeh);
923 zap_entry_remove(&zeh);
924 zap_increment_num_entries(zn->zn_zap, -1, tx);
931 fzap_prefetch(zap_name_t *zn)
934 zap_t *zap = zn->zn_zap;
937 idx = ZAP_HASH_IDX(zn->zn_hash,
938 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
939 if (zap_idx_to_blk(zap, idx, &blk) != 0)
941 bs = FZAP_BLOCK_SHIFT(zap);
942 dmu_prefetch(zap->zap_objset, zap->zap_object, blk << bs, 1 << bs);
946 * Helper functions for consumers.
950 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
960 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
961 for (zap_cursor_init(&zc, os, zapobj);
962 (err = zap_cursor_retrieve(&zc, za)) == 0;
963 zap_cursor_advance(&zc)) {
964 if ((za->za_first_integer & mask) == (value & mask)) {
965 (void) strcpy(name, za->za_name);
969 zap_cursor_fini(&zc);
970 kmem_free(za, sizeof (zap_attribute_t));
975 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
981 for (zap_cursor_init(&zc, os, fromobj);
982 zap_cursor_retrieve(&zc, &za) == 0;
983 (void) zap_cursor_advance(&zc)) {
984 if (za.za_integer_length != 8 || za.za_num_integers != 1)
986 err = zap_add(os, intoobj, za.za_name,
987 8, 1, &za.za_first_integer, tx);
991 zap_cursor_fini(&zc);
996 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
997 uint64_t value, dmu_tx_t *tx)
1003 for (zap_cursor_init(&zc, os, fromobj);
1004 zap_cursor_retrieve(&zc, &za) == 0;
1005 (void) zap_cursor_advance(&zc)) {
1006 if (za.za_integer_length != 8 || za.za_num_integers != 1)
1008 err = zap_add(os, intoobj, za.za_name,
1013 zap_cursor_fini(&zc);
1018 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1025 for (zap_cursor_init(&zc, os, fromobj);
1026 zap_cursor_retrieve(&zc, &za) == 0;
1027 (void) zap_cursor_advance(&zc)) {
1030 if (za.za_integer_length != 8 || za.za_num_integers != 1)
1033 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1034 if (err != 0 && err != ENOENT)
1036 delta += za.za_first_integer;
1037 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1041 zap_cursor_fini(&zc);
1046 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1050 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1051 return (zap_add(os, obj, name, 8, 1, &value, tx));
1055 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1059 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1060 return (zap_remove(os, obj, name, tx));
1064 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1068 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1069 return (zap_lookup(os, obj, name, 8, 1, &value));
1073 zap_add_int_key(objset_t *os, uint64_t obj,
1074 uint64_t key, uint64_t value, dmu_tx_t *tx)
1078 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1079 return (zap_add(os, obj, name, 8, 1, &value, tx));
1083 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1087 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1088 return (zap_lookup(os, obj, name, 8, 1, valuep));
1092 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1101 err = zap_lookup(os, obj, name, 8, 1, &value);
1102 if (err != 0 && err != ENOENT)
1106 err = zap_remove(os, obj, name, tx);
1108 err = zap_update(os, obj, name, 8, 1, &value, tx);
1113 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1118 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1119 return (zap_increment(os, obj, name, delta, tx));
1123 * Routines for iterating over the attributes.
1127 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1130 zap_entry_handle_t zeh;
1133 /* retrieve the next entry at or after zc_hash/zc_cd */
1134 /* if no entry, return ENOENT */
1137 (ZAP_HASH_IDX(zc->zc_hash,
1138 zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) !=
1139 zc->zc_leaf->l_phys->l_hdr.lh_prefix)) {
1140 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1141 zap_put_leaf(zc->zc_leaf);
1146 if (zc->zc_leaf == NULL) {
1147 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1152 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1156 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1158 if (err == ENOENT) {
1160 (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1;
1161 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1163 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) {
1164 zc->zc_hash = -1ULL;
1166 zap_put_leaf(zc->zc_leaf);
1173 zc->zc_hash = zeh.zeh_hash;
1174 zc->zc_cd = zeh.zeh_cd;
1175 za->za_integer_length = zeh.zeh_integer_size;
1176 za->za_num_integers = zeh.zeh_num_integers;
1177 if (zeh.zeh_num_integers == 0) {
1178 za->za_first_integer = 0;
1180 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1181 ASSERT(err == 0 || err == EOVERFLOW);
1183 err = zap_entry_read_name(zap, &zeh,
1184 sizeof (za->za_name), za->za_name);
1187 za->za_normalization_conflict =
1188 zap_entry_normalization_conflict(&zeh,
1189 NULL, za->za_name, zap);
1191 rw_exit(&zc->zc_leaf->l_rwlock);
1196 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1199 uint64_t lastblk = 0;
1202 * NB: if a leaf has more pointers than an entire ptrtbl block
1203 * can hold, then it'll be accounted for more than once, since
1204 * we won't have lastblk.
1206 for (i = 0; i < len; i++) {
1209 if (tbl[i] == lastblk)
1213 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1215 zap_leaf_stats(zap, l, zs);
1222 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1226 zap_entry_handle_t zeh;
1228 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1229 return (ENAMETOOLONG);
1231 err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1235 err = zap_leaf_lookup(l, zn, &zeh);
1240 zc->zc_hash = zeh.zeh_hash;
1241 zc->zc_cd = zeh.zeh_cd;
1247 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1249 int bs = FZAP_BLOCK_SHIFT(zap);
1250 zs->zs_blocksize = 1ULL << bs;
1253 * Set zap_phys_t fields
1255 zs->zs_num_leafs = zap->zap_f.zap_phys->zap_num_leafs;
1256 zs->zs_num_entries = zap->zap_f.zap_phys->zap_num_entries;
1257 zs->zs_num_blocks = zap->zap_f.zap_phys->zap_freeblk;
1258 zs->zs_block_type = zap->zap_f.zap_phys->zap_block_type;
1259 zs->zs_magic = zap->zap_f.zap_phys->zap_magic;
1260 zs->zs_salt = zap->zap_f.zap_phys->zap_salt;
1263 * Set zap_ptrtbl fields
1265 zs->zs_ptrtbl_len = 1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1266 zs->zs_ptrtbl_nextblk = zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk;
1267 zs->zs_ptrtbl_blks_copied =
1268 zap->zap_f.zap_phys->zap_ptrtbl.zt_blks_copied;
1269 zs->zs_ptrtbl_zt_blk = zap->zap_f.zap_phys->zap_ptrtbl.zt_blk;
1270 zs->zs_ptrtbl_zt_numblks = zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1271 zs->zs_ptrtbl_zt_shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
1273 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
1274 /* the ptrtbl is entirely in the header block. */
1275 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1276 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1280 dmu_prefetch(zap->zap_objset, zap->zap_object,
1281 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk << bs,
1282 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << bs);
1284 for (b = 0; b < zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
1289 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1290 (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk + b) << bs,
1291 FTAG, &db, DMU_READ_NO_PREFETCH);
1293 zap_stats_ptrtbl(zap, db->db_data,
1295 dmu_buf_rele(db, FTAG);
1302 fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
1303 uint64_t *tooverwrite)
1305 zap_t *zap = zn->zn_zap;
1310 * Account for the header block of the fatzap.
1312 if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
1313 *tooverwrite += zap->zap_dbuf->db_size;
1315 *towrite += zap->zap_dbuf->db_size;
1319 * Account for the pointer table blocks.
1320 * If we are adding we need to account for the following cases :
1321 * - If the pointer table is embedded, this operation could force an
1322 * external pointer table.
1323 * - If this already has an external pointer table this operation
1324 * could extend the table.
1327 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0)
1328 *towrite += zap->zap_dbuf->db_size;
1330 *towrite += (zap->zap_dbuf->db_size * 3);
1334 * Now, check if the block containing leaf is freeable
1335 * and account accordingly.
1337 err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1342 if (!add && dmu_buf_freeable(l->l_dbuf)) {
1343 *tooverwrite += l->l_dbuf->db_size;
1346 * If this an add operation, the leaf block could split.
1347 * Hence, we need to account for an additional leaf block.
1349 *towrite += (add ? 2 : 1) * l->l_dbuf->db_size;