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, 2014 by Delphix. All rights reserved.
27 * This file contains the top half of the zfs directory structure
28 * implementation. The bottom half is in zap_leaf.c.
30 * The zdir is an extendable hash data structure. There is a table of
31 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
32 * each a constant size and hold a variable number of directory entries.
33 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
35 * The pointer table holds a power of 2 number of pointers.
36 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
37 * by the pointer at index i in the table holds entries whose hash value
38 * has a zd_prefix_len - bit prefix
43 #include <sys/zfs_context.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/fs/zfs.h>
47 #include <sys/refcount.h>
48 #include <sys/zap_impl.h>
49 #include <sys/zap_leaf.h>
51 int fzap_default_block_shift
= 14; /* 16k blocksize */
53 extern inline zap_phys_t
*zap_f_phys(zap_t
*zap
);
55 static void zap_leaf_pageout(dmu_buf_t
*db
, void *vl
);
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 (void) dmu_buf_update_user(zap
->zap_dbuf
, zap
, zap
, 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
,
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
);
298 dmu_buf_rele(db
, FTAG
);
304 * Routines for growing the ptrtbl.
308 zap_ptrtbl_transfer(const uint64_t *src
, uint64_t *dst
, int n
)
311 for (i
= 0; i
< n
; i
++) {
312 uint64_t lb
= src
[i
];
319 zap_grow_ptrtbl(zap_t
*zap
, dmu_tx_t
*tx
)
322 * The pointer table should never use more hash bits than we
323 * have (otherwise we'd be using useless zero bits to index it).
324 * If we are within 2 bits of running out, stop growing, since
325 * this is already an aberrant condition.
327 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
>= zap_hashbits(zap
) - 2)
328 return (SET_ERROR(ENOSPC
));
330 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
332 * We are outgrowing the "embedded" ptrtbl (the one
333 * stored in the header block). Give it its own entire
334 * block, which will double the size of the ptrtbl.
340 ASSERT3U(zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
, ==,
341 ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
342 ASSERT0(zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
);
344 newblk
= zap_allocate_blocks(zap
, 1);
345 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
346 newblk
<< FZAP_BLOCK_SHIFT(zap
), FTAG
, &db_new
,
347 DMU_READ_NO_PREFETCH
);
350 dmu_buf_will_dirty(db_new
, tx
);
351 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
352 db_new
->db_data
, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
353 dmu_buf_rele(db_new
, FTAG
);
355 zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
= newblk
;
356 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
= 1;
357 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
++;
359 ASSERT3U(1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
, ==,
360 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
<<
361 (FZAP_BLOCK_SHIFT(zap
)-3));
365 return (zap_table_grow(zap
, &zap_f_phys(zap
)->zap_ptrtbl
,
366 zap_ptrtbl_transfer
, tx
));
371 zap_increment_num_entries(zap_t
*zap
, int delta
, dmu_tx_t
*tx
)
373 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
374 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
);
375 ASSERT(delta
> 0 || zap_f_phys(zap
)->zap_num_entries
>= -delta
);
376 zap_f_phys(zap
)->zap_num_entries
+= delta
;
377 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
381 zap_allocate_blocks(zap_t
*zap
, int nblocks
)
384 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
385 newblk
= zap_f_phys(zap
)->zap_freeblk
;
386 zap_f_phys(zap
)->zap_freeblk
+= nblocks
;
391 zap_create_leaf(zap_t
*zap
, dmu_tx_t
*tx
)
394 zap_leaf_t
*l
= kmem_alloc(sizeof (zap_leaf_t
), KM_SLEEP
);
396 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
398 rw_init(&l
->l_rwlock
, 0, 0, 0);
399 rw_enter(&l
->l_rwlock
, RW_WRITER
);
400 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
, 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_f_phys(zap
)->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_f_phys(zap
)->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
, 0, 0, 0);
457 rw_enter(&l
->l_rwlock
, RW_WRITER
);
459 l
->l_bs
= highbit64(db
->db_size
) - 1;
462 winner
= dmu_buf_set_user(db
, l
, zap_leaf_pageout
);
464 rw_exit(&l
->l_rwlock
);
465 if (winner
!= NULL
) {
466 /* someone else set it first */
467 zap_leaf_pageout(NULL
, l
);
472 * lhr_pad was previously used for the next leaf in the leaf
473 * chain. There should be no chained leafs (as we have removed
476 ASSERT0(zap_leaf_phys(l
)->l_hdr
.lh_pad1
);
479 * There should be more hash entries than there can be
480 * chunks to put in the hash table
482 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l
), >, ZAP_LEAF_NUMCHUNKS(l
) / 3);
484 /* The chunks should begin at the end of the hash table */
485 ASSERT3P(&ZAP_LEAF_CHUNK(l
, 0), ==,
486 &zap_leaf_phys(l
)->l_hash
[ZAP_LEAF_HASH_NUMENTRIES(l
)]);
488 /* The chunks should end at the end of the block */
489 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)) -
490 (uintptr_t)zap_leaf_phys(l
), ==, l
->l_dbuf
->db_size
);
496 zap_get_leaf_byblk(zap_t
*zap
, uint64_t blkid
, dmu_tx_t
*tx
, krw_t lt
,
501 int bs
= FZAP_BLOCK_SHIFT(zap
);
504 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
506 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
507 blkid
<< bs
, NULL
, &db
, DMU_READ_NO_PREFETCH
);
511 ASSERT3U(db
->db_object
, ==, zap
->zap_object
);
512 ASSERT3U(db
->db_offset
, ==, blkid
<< bs
);
513 ASSERT3U(db
->db_size
, ==, 1 << bs
);
516 l
= dmu_buf_get_user(db
);
519 l
= zap_open_leaf(blkid
, db
);
521 rw_enter(&l
->l_rwlock
, lt
);
523 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
524 * causing ASSERT below to fail.
527 dmu_buf_will_dirty(db
, tx
);
528 ASSERT3U(l
->l_blkid
, ==, blkid
);
529 ASSERT3P(l
->l_dbuf
, ==, db
);
530 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_block_type
, ==, ZBT_LEAF
);
531 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
538 zap_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t *valp
)
540 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
542 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
544 (1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
));
545 *valp
= ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
);
548 return (zap_table_load(zap
, &zap_f_phys(zap
)->zap_ptrtbl
,
554 zap_set_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t blk
, dmu_tx_t
*tx
)
557 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
559 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
== 0) {
560 ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
) = blk
;
563 return (zap_table_store(zap
, &zap_f_phys(zap
)->zap_ptrtbl
,
569 zap_deref_leaf(zap_t
*zap
, uint64_t h
, dmu_tx_t
*tx
, krw_t lt
, zap_leaf_t
**lp
)
574 ASSERT(zap
->zap_dbuf
== NULL
||
575 zap_f_phys(zap
) == zap
->zap_dbuf
->db_data
);
576 ASSERT3U(zap_f_phys(zap
)->zap_magic
, ==, ZAP_MAGIC
);
577 idx
= ZAP_HASH_IDX(h
, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
578 err
= zap_idx_to_blk(zap
, idx
, &blk
);
581 err
= zap_get_leaf_byblk(zap
, blk
, tx
, lt
, lp
);
584 ZAP_HASH_IDX(h
, zap_leaf_phys(*lp
)->l_hdr
.lh_prefix_len
) ==
585 zap_leaf_phys(*lp
)->l_hdr
.lh_prefix
);
590 zap_expand_leaf(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
, zap_leaf_t
**lp
)
592 zap_t
*zap
= zn
->zn_zap
;
593 uint64_t hash
= zn
->zn_hash
;
595 int prefix_diff
, i
, err
;
597 int old_prefix_len
= zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
599 ASSERT3U(old_prefix_len
, <=, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
600 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
602 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
603 zap_leaf_phys(l
)->l_hdr
.lh_prefix
);
605 if (zap_tryupgradedir(zap
, tx
) == 0 ||
606 old_prefix_len
== zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
) {
607 /* We failed to upgrade, or need to grow the pointer table */
608 objset_t
*os
= zap
->zap_objset
;
609 uint64_t object
= zap
->zap_object
;
613 err
= zap_lockdir(os
, object
, tx
, RW_WRITER
,
614 FALSE
, FALSE
, &zn
->zn_zap
);
618 ASSERT(!zap
->zap_ismicro
);
620 while (old_prefix_len
==
621 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
) {
622 err
= zap_grow_ptrtbl(zap
, tx
);
627 err
= zap_deref_leaf(zap
, hash
, tx
, RW_WRITER
, &l
);
631 if (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
!= old_prefix_len
) {
632 /* it split while our locks were down */
637 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
638 ASSERT3U(old_prefix_len
, <, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
639 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
640 zap_leaf_phys(l
)->l_hdr
.lh_prefix
);
642 prefix_diff
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
-
643 (old_prefix_len
+ 1);
644 sibling
= (ZAP_HASH_IDX(hash
, old_prefix_len
+ 1) | 1) << prefix_diff
;
646 /* check for i/o errors before doing zap_leaf_split */
647 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
649 err
= zap_idx_to_blk(zap
, sibling
+i
, &blk
);
652 ASSERT3U(blk
, ==, l
->l_blkid
);
655 nl
= zap_create_leaf(zap
, tx
);
656 zap_leaf_split(l
, nl
, zap
->zap_normflags
!= 0);
658 /* set sibling pointers */
659 for (i
= 0; i
< (1ULL << prefix_diff
); i
++) {
660 err
= zap_set_idx_to_blk(zap
, sibling
+i
, nl
->l_blkid
, tx
);
661 ASSERT0(err
); /* we checked for i/o errors above */
664 if (hash
& (1ULL << (64 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
))) {
665 /* we want the sibling */
677 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
)
679 zap_t
*zap
= zn
->zn_zap
;
680 int shift
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
681 int leaffull
= (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
== shift
&&
682 zap_leaf_phys(l
)->l_hdr
.lh_nfree
< ZAP_LEAF_LOW_WATER
);
686 if (leaffull
|| zap_f_phys(zap
)->zap_ptrtbl
.zt_nextblk
) {
690 * We are in the middle of growing the pointer table, or
691 * this leaf will soon make us grow it.
693 if (zap_tryupgradedir(zap
, tx
) == 0) {
694 objset_t
*os
= zap
->zap_objset
;
695 uint64_t zapobj
= zap
->zap_object
;
698 err
= zap_lockdir(os
, zapobj
, tx
,
699 RW_WRITER
, FALSE
, FALSE
, &zn
->zn_zap
);
705 /* could have finished growing while our locks were down */
706 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
== shift
)
707 (void) zap_grow_ptrtbl(zap
, tx
);
712 fzap_checkname(zap_name_t
*zn
)
714 if (zn
->zn_key_orig_numints
* zn
->zn_key_intlen
> ZAP_MAXNAMELEN
)
715 return (SET_ERROR(ENAMETOOLONG
));
720 fzap_checksize(uint64_t integer_size
, uint64_t num_integers
)
722 /* Only integer sizes supported by C */
723 switch (integer_size
) {
730 return (SET_ERROR(EINVAL
));
733 if (integer_size
* num_integers
> ZAP_MAXVALUELEN
)
740 fzap_check(zap_name_t
*zn
, uint64_t integer_size
, uint64_t num_integers
)
744 if ((err
= fzap_checkname(zn
)) != 0)
746 return (fzap_checksize(integer_size
, num_integers
));
750 * Routines for manipulating attributes.
753 fzap_lookup(zap_name_t
*zn
,
754 uint64_t integer_size
, uint64_t num_integers
, void *buf
,
755 char *realname
, int rn_len
, boolean_t
*ncp
)
759 zap_entry_handle_t zeh
;
761 if ((err
= fzap_checkname(zn
)) != 0)
764 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
767 err
= zap_leaf_lookup(l
, zn
, &zeh
);
769 if ((err
= fzap_checksize(integer_size
, num_integers
)) != 0) {
774 err
= zap_entry_read(&zeh
, integer_size
, num_integers
, buf
);
775 (void) zap_entry_read_name(zn
->zn_zap
, &zeh
, rn_len
, realname
);
777 *ncp
= zap_entry_normalization_conflict(&zeh
,
778 zn
, NULL
, zn
->zn_zap
);
787 fzap_add_cd(zap_name_t
*zn
,
788 uint64_t integer_size
, uint64_t num_integers
,
789 const void *val
, uint32_t cd
, dmu_tx_t
*tx
)
793 zap_entry_handle_t zeh
;
794 zap_t
*zap
= zn
->zn_zap
;
796 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
797 ASSERT(!zap
->zap_ismicro
);
798 ASSERT(fzap_check(zn
, integer_size
, num_integers
) == 0);
800 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
804 err
= zap_leaf_lookup(l
, zn
, &zeh
);
806 err
= SET_ERROR(EEXIST
);
812 err
= zap_entry_create(l
, zn
, cd
,
813 integer_size
, num_integers
, val
, &zeh
);
816 zap_increment_num_entries(zap
, 1, tx
);
817 } else if (err
== EAGAIN
) {
818 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
819 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
826 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
831 fzap_add(zap_name_t
*zn
,
832 uint64_t integer_size
, uint64_t num_integers
,
833 const void *val
, dmu_tx_t
*tx
)
835 int err
= fzap_check(zn
, integer_size
, num_integers
);
839 return (fzap_add_cd(zn
, integer_size
, num_integers
,
840 val
, ZAP_NEED_CD
, tx
));
844 fzap_update(zap_name_t
*zn
,
845 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
849 zap_entry_handle_t zeh
;
850 zap_t
*zap
= zn
->zn_zap
;
852 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
853 err
= fzap_check(zn
, integer_size
, num_integers
);
857 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
861 err
= zap_leaf_lookup(l
, zn
, &zeh
);
862 create
= (err
== ENOENT
);
863 ASSERT(err
== 0 || err
== ENOENT
);
866 err
= zap_entry_create(l
, zn
, ZAP_NEED_CD
,
867 integer_size
, num_integers
, val
, &zeh
);
869 zap_increment_num_entries(zap
, 1, tx
);
871 err
= zap_entry_update(&zeh
, integer_size
, num_integers
, val
);
875 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
876 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
882 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
887 fzap_length(zap_name_t
*zn
,
888 uint64_t *integer_size
, uint64_t *num_integers
)
892 zap_entry_handle_t zeh
;
894 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
897 err
= zap_leaf_lookup(l
, zn
, &zeh
);
902 *integer_size
= zeh
.zeh_integer_size
;
904 *num_integers
= zeh
.zeh_num_integers
;
911 fzap_remove(zap_name_t
*zn
, dmu_tx_t
*tx
)
915 zap_entry_handle_t zeh
;
917 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
920 err
= zap_leaf_lookup(l
, zn
, &zeh
);
922 zap_entry_remove(&zeh
);
923 zap_increment_num_entries(zn
->zn_zap
, -1, tx
);
930 fzap_prefetch(zap_name_t
*zn
)
933 zap_t
*zap
= zn
->zn_zap
;
936 idx
= ZAP_HASH_IDX(zn
->zn_hash
,
937 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
938 if (zap_idx_to_blk(zap
, idx
, &blk
) != 0)
940 bs
= FZAP_BLOCK_SHIFT(zap
);
941 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
, blk
<< bs
, 1 << bs
);
945 * Helper functions for consumers.
949 zap_create_link(objset_t
*os
, dmu_object_type_t ot
, uint64_t parent_obj
,
950 const char *name
, dmu_tx_t
*tx
)
954 VERIFY((new_obj
= zap_create(os
, ot
, DMU_OT_NONE
, 0, tx
)) > 0);
955 VERIFY(zap_add(os
, parent_obj
, name
, sizeof (uint64_t), 1, &new_obj
,
962 zap_value_search(objset_t
*os
, uint64_t zapobj
, uint64_t value
, uint64_t mask
,
972 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
973 for (zap_cursor_init(&zc
, os
, zapobj
);
974 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
975 zap_cursor_advance(&zc
)) {
976 if ((za
->za_first_integer
& mask
) == (value
& mask
)) {
977 (void) strcpy(name
, za
->za_name
);
981 zap_cursor_fini(&zc
);
982 kmem_free(za
, sizeof (zap_attribute_t
));
987 zap_join(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
, dmu_tx_t
*tx
)
994 for (zap_cursor_init(&zc
, os
, fromobj
);
995 zap_cursor_retrieve(&zc
, &za
) == 0;
996 (void) zap_cursor_advance(&zc
)) {
997 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1) {
998 err
= SET_ERROR(EINVAL
);
1001 err
= zap_add(os
, intoobj
, za
.za_name
,
1002 8, 1, &za
.za_first_integer
, tx
);
1006 zap_cursor_fini(&zc
);
1011 zap_join_key(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1012 uint64_t value
, 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
,
1031 zap_cursor_fini(&zc
);
1036 zap_join_increment(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1044 for (zap_cursor_init(&zc
, os
, fromobj
);
1045 zap_cursor_retrieve(&zc
, &za
) == 0;
1046 (void) zap_cursor_advance(&zc
)) {
1049 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1) {
1050 err
= SET_ERROR(EINVAL
);
1054 err
= zap_lookup(os
, intoobj
, za
.za_name
, 8, 1, &delta
);
1055 if (err
!= 0 && err
!= ENOENT
)
1057 delta
+= za
.za_first_integer
;
1058 err
= zap_update(os
, intoobj
, za
.za_name
, 8, 1, &delta
, tx
);
1062 zap_cursor_fini(&zc
);
1067 zap_add_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1071 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1072 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
1076 zap_remove_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1080 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1081 return (zap_remove(os
, obj
, name
, tx
));
1085 zap_lookup_int(objset_t
*os
, uint64_t obj
, uint64_t value
)
1089 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1090 return (zap_lookup(os
, obj
, name
, 8, 1, &value
));
1094 zap_add_int_key(objset_t
*os
, uint64_t obj
,
1095 uint64_t key
, uint64_t value
, dmu_tx_t
*tx
)
1099 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1100 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
1104 zap_update_int_key(objset_t
*os
, uint64_t obj
,
1105 uint64_t key
, uint64_t value
, dmu_tx_t
*tx
)
1109 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1110 return (zap_update(os
, obj
, name
, 8, 1, &value
, tx
));
1114 zap_lookup_int_key(objset_t
*os
, uint64_t obj
, uint64_t key
, uint64_t *valuep
)
1118 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1119 return (zap_lookup(os
, obj
, name
, 8, 1, valuep
));
1123 zap_increment(objset_t
*os
, uint64_t obj
, const char *name
, int64_t delta
,
1132 err
= zap_lookup(os
, obj
, name
, 8, 1, &value
);
1133 if (err
!= 0 && err
!= ENOENT
)
1137 err
= zap_remove(os
, obj
, name
, tx
);
1139 err
= zap_update(os
, obj
, name
, 8, 1, &value
, tx
);
1144 zap_increment_int(objset_t
*os
, uint64_t obj
, uint64_t key
, int64_t delta
,
1149 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1150 return (zap_increment(os
, obj
, name
, delta
, tx
));
1154 * Routines for iterating over the attributes.
1158 fzap_cursor_retrieve(zap_t
*zap
, zap_cursor_t
*zc
, zap_attribute_t
*za
)
1161 zap_entry_handle_t zeh
;
1164 /* retrieve the next entry at or after zc_hash/zc_cd */
1165 /* if no entry, return ENOENT */
1168 (ZAP_HASH_IDX(zc
->zc_hash
,
1169 zap_leaf_phys(zc
->zc_leaf
)->l_hdr
.lh_prefix_len
) !=
1170 zap_leaf_phys(zc
->zc_leaf
)->l_hdr
.lh_prefix
)) {
1171 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1172 zap_put_leaf(zc
->zc_leaf
);
1177 if (zc
->zc_leaf
== NULL
) {
1178 err
= zap_deref_leaf(zap
, zc
->zc_hash
, NULL
, RW_READER
,
1183 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1187 err
= zap_leaf_lookup_closest(l
, zc
->zc_hash
, zc
->zc_cd
, &zeh
);
1189 if (err
== ENOENT
) {
1191 (1ULL << (64 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
)) - 1;
1192 zc
->zc_hash
= (zc
->zc_hash
& ~nocare
) + nocare
+ 1;
1194 if (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
== 0 ||
1196 zc
->zc_hash
= -1ULL;
1198 zap_put_leaf(zc
->zc_leaf
);
1205 zc
->zc_hash
= zeh
.zeh_hash
;
1206 zc
->zc_cd
= zeh
.zeh_cd
;
1207 za
->za_integer_length
= zeh
.zeh_integer_size
;
1208 za
->za_num_integers
= zeh
.zeh_num_integers
;
1209 if (zeh
.zeh_num_integers
== 0) {
1210 za
->za_first_integer
= 0;
1212 err
= zap_entry_read(&zeh
, 8, 1, &za
->za_first_integer
);
1213 ASSERT(err
== 0 || err
== EOVERFLOW
);
1215 err
= zap_entry_read_name(zap
, &zeh
,
1216 sizeof (za
->za_name
), za
->za_name
);
1219 za
->za_normalization_conflict
=
1220 zap_entry_normalization_conflict(&zeh
,
1221 NULL
, za
->za_name
, zap
);
1223 rw_exit(&zc
->zc_leaf
->l_rwlock
);
1228 zap_stats_ptrtbl(zap_t
*zap
, uint64_t *tbl
, int len
, zap_stats_t
*zs
)
1231 uint64_t lastblk
= 0;
1234 * NB: if a leaf has more pointers than an entire ptrtbl block
1235 * can hold, then it'll be accounted for more than once, since
1236 * we won't have lastblk.
1238 for (i
= 0; i
< len
; i
++) {
1241 if (tbl
[i
] == lastblk
)
1245 err
= zap_get_leaf_byblk(zap
, tbl
[i
], NULL
, RW_READER
, &l
);
1247 zap_leaf_stats(zap
, l
, zs
);
1254 fzap_get_stats(zap_t
*zap
, zap_stats_t
*zs
)
1256 int bs
= FZAP_BLOCK_SHIFT(zap
);
1257 zs
->zs_blocksize
= 1ULL << bs
;
1260 * Set zap_phys_t fields
1262 zs
->zs_num_leafs
= zap_f_phys(zap
)->zap_num_leafs
;
1263 zs
->zs_num_entries
= zap_f_phys(zap
)->zap_num_entries
;
1264 zs
->zs_num_blocks
= zap_f_phys(zap
)->zap_freeblk
;
1265 zs
->zs_block_type
= zap_f_phys(zap
)->zap_block_type
;
1266 zs
->zs_magic
= zap_f_phys(zap
)->zap_magic
;
1267 zs
->zs_salt
= zap_f_phys(zap
)->zap_salt
;
1270 * Set zap_ptrtbl fields
1272 zs
->zs_ptrtbl_len
= 1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
1273 zs
->zs_ptrtbl_nextblk
= zap_f_phys(zap
)->zap_ptrtbl
.zt_nextblk
;
1274 zs
->zs_ptrtbl_blks_copied
=
1275 zap_f_phys(zap
)->zap_ptrtbl
.zt_blks_copied
;
1276 zs
->zs_ptrtbl_zt_blk
= zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
;
1277 zs
->zs_ptrtbl_zt_numblks
= zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
;
1278 zs
->zs_ptrtbl_zt_shift
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
1280 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
1281 /* the ptrtbl is entirely in the header block. */
1282 zap_stats_ptrtbl(zap
, &ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
1283 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
), zs
);
1287 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
,
1288 zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
<< bs
,
1289 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
<< bs
);
1291 for (b
= 0; b
< zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
;
1296 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
1297 (zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
+ b
) << bs
,
1298 FTAG
, &db
, DMU_READ_NO_PREFETCH
);
1300 zap_stats_ptrtbl(zap
, db
->db_data
,
1302 dmu_buf_rele(db
, FTAG
);
1309 fzap_count_write(zap_name_t
*zn
, int add
, uint64_t *towrite
,
1310 uint64_t *tooverwrite
)
1312 zap_t
*zap
= zn
->zn_zap
;
1317 * Account for the header block of the fatzap.
1319 if (!add
&& dmu_buf_freeable(zap
->zap_dbuf
)) {
1320 *tooverwrite
+= zap
->zap_dbuf
->db_size
;
1322 *towrite
+= zap
->zap_dbuf
->db_size
;
1326 * Account for the pointer table blocks.
1327 * If we are adding we need to account for the following cases :
1328 * - If the pointer table is embedded, this operation could force an
1329 * external pointer table.
1330 * - If this already has an external pointer table this operation
1331 * could extend the table.
1334 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
== 0)
1335 *towrite
+= zap
->zap_dbuf
->db_size
;
1337 *towrite
+= (zap
->zap_dbuf
->db_size
* 3);
1341 * Now, check if the block containing leaf is freeable
1342 * and account accordingly.
1344 err
= zap_deref_leaf(zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
1349 if (!add
&& dmu_buf_freeable(l
->l_dbuf
)) {
1350 *tooverwrite
+= l
->l_dbuf
->db_size
;
1353 * If this an add operation, the leaf block could split.
1354 * Hence, we need to account for an additional leaf block.
1356 *towrite
+= (add
? 2 : 1) * l
->l_dbuf
->db_size
;