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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains the top half of the zfs directory structure
31 * implementation. The bottom half is in zap_leaf.c.
33 * The zdir is an extendable hash data structure. There is a table of
34 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
35 * each a constant size and hold a variable number of directory entries.
36 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
38 * The pointer table holds a power of 2 number of pointers.
39 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
40 * by the pointer at index i in the table holds entries whose hash value
41 * has a zd_prefix_len - bit prefix
46 #include <sys/zfs_context.h>
47 #include <sys/zfs_znode.h>
49 #include <sys/refcount.h>
50 #include <sys/zap_impl.h>
51 #include <sys/zap_leaf.h>
53 int fzap_default_block_shift
= 14; /* 16k blocksize */
55 static void zap_leaf_pageout(dmu_buf_t
*db
, void *vl
);
56 static uint64_t zap_allocate_blocks(zap_t
*zap
, int nblocks
);
60 fzap_byteswap(void *vbuf
, size_t size
)
64 block_type
= *(uint64_t *)vbuf
;
66 if (block_type
== ZBT_LEAF
|| block_type
== BSWAP_64(ZBT_LEAF
))
67 zap_leaf_byteswap(vbuf
, size
);
69 /* it's a ptrtbl block */
70 byteswap_uint64_array(vbuf
, size
);
75 fzap_upgrade(zap_t
*zap
, dmu_tx_t
*tx
)
82 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
83 zap
->zap_ismicro
= FALSE
;
85 (void) dmu_buf_update_user(zap
->zap_dbuf
, zap
, zap
,
86 &zap
->zap_f
.zap_phys
, zap_evict
);
88 mutex_init(&zap
->zap_f
.zap_num_entries_mtx
, 0, 0, 0);
89 zap
->zap_f
.zap_block_shift
= highbit(zap
->zap_dbuf
->db_size
) - 1;
91 zp
= zap
->zap_f
.zap_phys
;
93 * explicitly zero it since it might be coming from an
94 * initialized microzap
96 bzero(zap
->zap_dbuf
->db_data
, zap
->zap_dbuf
->db_size
);
97 zp
->zap_block_type
= ZBT_HEADER
;
98 zp
->zap_magic
= ZAP_MAGIC
;
100 zp
->zap_ptrtbl
.zt_shift
= ZAP_EMBEDDED_PTRTBL_SHIFT(zap
);
102 zp
->zap_freeblk
= 2; /* block 1 will be the first leaf */
103 zp
->zap_num_leafs
= 1;
104 zp
->zap_num_entries
= 0;
105 zp
->zap_salt
= zap
->zap_salt
;
106 zp
->zap_normflags
= zap
->zap_normflags
;
108 /* block 1 will be the first leaf */
109 for (i
= 0; i
< (1<<zp
->zap_ptrtbl
.zt_shift
); i
++)
110 ZAP_EMBEDDED_PTRTBL_ENT(zap
, i
) = 1;
113 * set up block 1 - the first leaf
115 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
116 1<<FZAP_BLOCK_SHIFT(zap
), FTAG
, &db
));
117 dmu_buf_will_dirty(db
, tx
);
119 l
= kmem_zalloc(sizeof (zap_leaf_t
), KM_SLEEP
);
121 l
->l_phys
= db
->db_data
;
123 zap_leaf_init(l
, zp
->zap_normflags
!= 0);
125 kmem_free(l
, sizeof (zap_leaf_t
));
126 dmu_buf_rele(db
, FTAG
);
130 zap_tryupgradedir(zap_t
*zap
, dmu_tx_t
*tx
)
132 if (RW_WRITE_HELD(&zap
->zap_rwlock
))
134 if (rw_tryupgrade(&zap
->zap_rwlock
)) {
135 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
142 * Generic routines for dealing with the pointer & cookie tables.
146 zap_table_grow(zap_t
*zap
, zap_table_phys_t
*tbl
,
147 void (*transfer_func
)(const uint64_t *src
, uint64_t *dst
, int n
),
151 dmu_buf_t
*db_old
, *db_new
;
153 int bs
= FZAP_BLOCK_SHIFT(zap
);
154 int hepb
= 1<<(bs
-4);
155 /* hepb = half the number of entries in a block */
157 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
158 ASSERT(tbl
->zt_blk
!= 0);
159 ASSERT(tbl
->zt_numblks
> 0);
161 if (tbl
->zt_nextblk
!= 0) {
162 newblk
= tbl
->zt_nextblk
;
164 newblk
= zap_allocate_blocks(zap
, tbl
->zt_numblks
* 2);
165 tbl
->zt_nextblk
= newblk
;
166 ASSERT3U(tbl
->zt_blks_copied
, ==, 0);
167 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
,
168 tbl
->zt_blk
<< bs
, tbl
->zt_numblks
<< bs
);
172 * Copy the ptrtbl from the old to new location.
175 b
= tbl
->zt_blks_copied
;
176 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
177 (tbl
->zt_blk
+ b
) << bs
, FTAG
, &db_old
);
181 /* first half of entries in old[b] go to new[2*b+0] */
182 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
183 (newblk
+ 2*b
+0) << bs
, FTAG
, &db_new
));
184 dmu_buf_will_dirty(db_new
, tx
);
185 transfer_func(db_old
->db_data
, db_new
->db_data
, hepb
);
186 dmu_buf_rele(db_new
, FTAG
);
188 /* second half of entries in old[b] go to new[2*b+1] */
189 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
190 (newblk
+ 2*b
+1) << bs
, FTAG
, &db_new
));
191 dmu_buf_will_dirty(db_new
, tx
);
192 transfer_func((uint64_t *)db_old
->db_data
+ hepb
,
193 db_new
->db_data
, hepb
);
194 dmu_buf_rele(db_new
, FTAG
);
196 dmu_buf_rele(db_old
, FTAG
);
198 tbl
->zt_blks_copied
++;
200 dprintf("copied block %llu of %llu\n",
201 tbl
->zt_blks_copied
, tbl
->zt_numblks
);
203 if (tbl
->zt_blks_copied
== tbl
->zt_numblks
) {
204 (void) dmu_free_range(zap
->zap_objset
, zap
->zap_object
,
205 tbl
->zt_blk
<< bs
, tbl
->zt_numblks
<< bs
, tx
);
207 tbl
->zt_blk
= newblk
;
208 tbl
->zt_numblks
*= 2;
211 tbl
->zt_blks_copied
= 0;
213 dprintf("finished; numblocks now %llu (%lluk entries)\n",
214 tbl
->zt_numblks
, 1<<(tbl
->zt_shift
-10));
221 zap_table_store(zap_t
*zap
, zap_table_phys_t
*tbl
, uint64_t idx
, uint64_t val
,
226 int bs
= FZAP_BLOCK_SHIFT(zap
);
229 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
230 ASSERT(tbl
->zt_blk
!= 0);
232 dprintf("storing %llx at index %llx\n", val
, idx
);
235 off
= idx
& ((1<<(bs
-3))-1);
237 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
238 (tbl
->zt_blk
+ blk
) << bs
, FTAG
, &db
);
241 dmu_buf_will_dirty(db
, tx
);
243 if (tbl
->zt_nextblk
!= 0) {
244 uint64_t idx2
= idx
* 2;
245 uint64_t blk2
= idx2
>> (bs
-3);
246 uint64_t off2
= idx2
& ((1<<(bs
-3))-1);
249 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
250 (tbl
->zt_nextblk
+ blk2
) << bs
, FTAG
, &db2
);
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
)
273 int bs
= FZAP_BLOCK_SHIFT(zap
);
275 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
278 off
= idx
& ((1<<(bs
-3))-1);
280 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
281 (tbl
->zt_blk
+ blk
) << bs
, FTAG
, &db
);
284 *valp
= ((uint64_t *)db
->db_data
)[off
];
285 dmu_buf_rele(db
, FTAG
);
287 if (tbl
->zt_nextblk
!= 0) {
289 * read the nextblk for the sake of i/o error checking,
290 * so that zap_table_load() will catch errors for
293 blk
= (idx
*2) >> (bs
-3);
295 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
296 (tbl
->zt_nextblk
+ blk
) << bs
, FTAG
, &db
);
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
)
320 /* In case things go horribly wrong. */
321 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
>= ZAP_HASHBITS
-2)
324 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
== 0) {
326 * We are outgrowing the "embedded" ptrtbl (the one
327 * stored in the header block). Give it its own entire
328 * block, which will double the size of the ptrtbl.
334 ASSERT3U(zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
, ==,
335 ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
336 ASSERT3U(zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
, ==, 0);
338 newblk
= zap_allocate_blocks(zap
, 1);
339 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
340 newblk
<< FZAP_BLOCK_SHIFT(zap
), FTAG
, &db_new
);
343 dmu_buf_will_dirty(db_new
, tx
);
344 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
345 db_new
->db_data
, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
346 dmu_buf_rele(db_new
, FTAG
);
348 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
= newblk
;
349 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
= 1;
350 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
++;
352 ASSERT3U(1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
, ==,
353 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
<<
354 (FZAP_BLOCK_SHIFT(zap
)-3));
358 return (zap_table_grow(zap
, &zap
->zap_f
.zap_phys
->zap_ptrtbl
,
359 zap_ptrtbl_transfer
, tx
));
364 zap_increment_num_entries(zap_t
*zap
, int delta
, dmu_tx_t
*tx
)
366 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
367 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
);
368 ASSERT(delta
> 0 || zap
->zap_f
.zap_phys
->zap_num_entries
>= -delta
);
369 zap
->zap_f
.zap_phys
->zap_num_entries
+= delta
;
370 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
374 zap_allocate_blocks(zap_t
*zap
, int nblocks
)
377 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
378 newblk
= zap
->zap_f
.zap_phys
->zap_freeblk
;
379 zap
->zap_f
.zap_phys
->zap_freeblk
+= nblocks
;
384 zap_create_leaf(zap_t
*zap
, dmu_tx_t
*tx
)
387 zap_leaf_t
*l
= kmem_alloc(sizeof (zap_leaf_t
), KM_SLEEP
);
389 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
391 rw_init(&l
->l_rwlock
, 0, 0, 0);
392 rw_enter(&l
->l_rwlock
, RW_WRITER
);
393 l
->l_blkid
= zap_allocate_blocks(zap
, 1);
397 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
398 l
->l_blkid
<< FZAP_BLOCK_SHIFT(zap
), NULL
, &l
->l_dbuf
));
399 winner
= dmu_buf_set_user(l
->l_dbuf
, l
, &l
->l_phys
, zap_leaf_pageout
);
400 ASSERT(winner
== NULL
);
401 dmu_buf_will_dirty(l
->l_dbuf
, tx
);
403 zap_leaf_init(l
, zap
->zap_normflags
!= 0);
405 zap
->zap_f
.zap_phys
->zap_num_leafs
++;
411 fzap_count(zap_t
*zap
, uint64_t *count
)
413 ASSERT(!zap
->zap_ismicro
);
414 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
); /* unnecessary */
415 *count
= zap
->zap_f
.zap_phys
->zap_num_entries
;
416 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
421 * Routines for obtaining zap_leaf_t's
425 zap_put_leaf(zap_leaf_t
*l
)
427 rw_exit(&l
->l_rwlock
);
428 dmu_buf_rele(l
->l_dbuf
, NULL
);
433 zap_leaf_pageout(dmu_buf_t
*db
, void *vl
)
437 rw_destroy(&l
->l_rwlock
);
438 kmem_free(l
, sizeof (zap_leaf_t
));
442 zap_open_leaf(uint64_t blkid
, dmu_buf_t
*db
)
444 zap_leaf_t
*l
, *winner
;
448 l
= kmem_alloc(sizeof (zap_leaf_t
), KM_SLEEP
);
449 rw_init(&l
->l_rwlock
, 0, 0, 0);
450 rw_enter(&l
->l_rwlock
, RW_WRITER
);
452 l
->l_bs
= highbit(db
->db_size
)-1;
456 winner
= dmu_buf_set_user(db
, l
, &l
->l_phys
, zap_leaf_pageout
);
458 rw_exit(&l
->l_rwlock
);
459 if (winner
!= NULL
) {
460 /* someone else set it first */
461 zap_leaf_pageout(NULL
, l
);
466 * lhr_pad was previously used for the next leaf in the leaf
467 * chain. There should be no chained leafs (as we have removed
470 ASSERT3U(l
->l_phys
->l_hdr
.lh_pad1
, ==, 0);
473 * There should be more hash entries than there can be
474 * chunks to put in the hash table
476 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l
), >, ZAP_LEAF_NUMCHUNKS(l
) / 3);
478 /* The chunks should begin at the end of the hash table */
479 ASSERT3P(&ZAP_LEAF_CHUNK(l
, 0), ==,
480 &l
->l_phys
->l_hash
[ZAP_LEAF_HASH_NUMENTRIES(l
)]);
482 /* The chunks should end at the end of the block */
483 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)) -
484 (uintptr_t)l
->l_phys
, ==, l
->l_dbuf
->db_size
);
490 zap_get_leaf_byblk(zap_t
*zap
, uint64_t blkid
, dmu_tx_t
*tx
, krw_t lt
,
495 int bs
= FZAP_BLOCK_SHIFT(zap
);
498 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
500 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
501 blkid
<< bs
, NULL
, &db
);
505 ASSERT3U(db
->db_object
, ==, zap
->zap_object
);
506 ASSERT3U(db
->db_offset
, ==, blkid
<< bs
);
507 ASSERT3U(db
->db_size
, ==, 1 << bs
);
510 l
= dmu_buf_get_user(db
);
513 l
= zap_open_leaf(blkid
, db
);
515 rw_enter(&l
->l_rwlock
, lt
);
517 * Must lock before dirtying, otherwise l->l_phys could change,
518 * causing ASSERT below to fail.
521 dmu_buf_will_dirty(db
, tx
);
522 ASSERT3U(l
->l_blkid
, ==, blkid
);
523 ASSERT3P(l
->l_dbuf
, ==, db
);
524 ASSERT3P(l
->l_phys
, ==, l
->l_dbuf
->db_data
);
525 ASSERT3U(l
->l_phys
->l_hdr
.lh_block_type
, ==, ZBT_LEAF
);
526 ASSERT3U(l
->l_phys
->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
533 zap_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t *valp
)
535 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
537 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
== 0) {
539 (1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
));
540 *valp
= ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
);
543 return (zap_table_load(zap
, &zap
->zap_f
.zap_phys
->zap_ptrtbl
,
549 zap_set_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t blk
, dmu_tx_t
*tx
)
552 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
554 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
== 0) {
555 ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
) = blk
;
558 return (zap_table_store(zap
, &zap
->zap_f
.zap_phys
->zap_ptrtbl
,
564 zap_deref_leaf(zap_t
*zap
, uint64_t h
, dmu_tx_t
*tx
, krw_t lt
, zap_leaf_t
**lp
)
569 ASSERT(zap
->zap_dbuf
== NULL
||
570 zap
->zap_f
.zap_phys
== zap
->zap_dbuf
->db_data
);
571 ASSERT3U(zap
->zap_f
.zap_phys
->zap_magic
, ==, ZAP_MAGIC
);
572 idx
= ZAP_HASH_IDX(h
, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
573 err
= zap_idx_to_blk(zap
, idx
, &blk
);
576 err
= zap_get_leaf_byblk(zap
, blk
, tx
, lt
, lp
);
578 ASSERT(err
|| ZAP_HASH_IDX(h
, (*lp
)->l_phys
->l_hdr
.lh_prefix_len
) ==
579 (*lp
)->l_phys
->l_hdr
.lh_prefix
);
584 zap_expand_leaf(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
, zap_leaf_t
**lp
)
586 zap_t
*zap
= zn
->zn_zap
;
587 uint64_t hash
= zn
->zn_hash
;
589 int prefix_diff
, i
, err
;
591 int old_prefix_len
= l
->l_phys
->l_hdr
.lh_prefix_len
;
593 ASSERT3U(old_prefix_len
, <=, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
594 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
596 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
597 l
->l_phys
->l_hdr
.lh_prefix
);
599 if (zap_tryupgradedir(zap
, tx
) == 0 ||
600 old_prefix_len
== zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
) {
601 /* We failed to upgrade, or need to grow the pointer table */
602 objset_t
*os
= zap
->zap_objset
;
603 uint64_t object
= zap
->zap_object
;
607 err
= zap_lockdir(os
, object
, tx
, RW_WRITER
,
608 FALSE
, FALSE
, &zn
->zn_zap
);
612 ASSERT(!zap
->zap_ismicro
);
614 while (old_prefix_len
==
615 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
) {
616 err
= zap_grow_ptrtbl(zap
, tx
);
621 err
= zap_deref_leaf(zap
, hash
, tx
, RW_WRITER
, &l
);
625 if (l
->l_phys
->l_hdr
.lh_prefix_len
!= old_prefix_len
) {
626 /* it split while our locks were down */
631 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
632 ASSERT3U(old_prefix_len
, <, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
633 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
634 l
->l_phys
->l_hdr
.lh_prefix
);
636 prefix_diff
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
-
637 (old_prefix_len
+ 1);
638 sibling
= (ZAP_HASH_IDX(hash
, old_prefix_len
+ 1) | 1) << prefix_diff
;
640 /* check for i/o errors before doing zap_leaf_split */
641 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
643 err
= zap_idx_to_blk(zap
, sibling
+i
, &blk
);
646 ASSERT3U(blk
, ==, l
->l_blkid
);
649 nl
= zap_create_leaf(zap
, tx
);
650 zap_leaf_split(l
, nl
, zap
->zap_normflags
!= 0);
652 /* set sibling pointers */
653 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
654 err
= zap_set_idx_to_blk(zap
, sibling
+i
, nl
->l_blkid
, tx
);
655 ASSERT3U(err
, ==, 0); /* we checked for i/o errors above */
658 if (hash
& (1ULL << (64 - l
->l_phys
->l_hdr
.lh_prefix_len
))) {
659 /* we want the sibling */
671 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
)
673 zap_t
*zap
= zn
->zn_zap
;
674 int shift
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
675 int leaffull
= (l
->l_phys
->l_hdr
.lh_prefix_len
== shift
&&
676 l
->l_phys
->l_hdr
.lh_nfree
< ZAP_LEAF_LOW_WATER
);
680 if (leaffull
|| zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_nextblk
) {
684 * We are in the middle of growing the pointer table, or
685 * this leaf will soon make us grow it.
687 if (zap_tryupgradedir(zap
, tx
) == 0) {
688 objset_t
*os
= zap
->zap_objset
;
689 uint64_t zapobj
= zap
->zap_object
;
692 err
= zap_lockdir(os
, zapobj
, tx
,
693 RW_WRITER
, FALSE
, FALSE
, &zn
->zn_zap
);
699 /* could have finished growing while our locks were down */
700 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
== shift
)
701 (void) zap_grow_ptrtbl(zap
, tx
);
707 fzap_checksize(const char *name
, uint64_t integer_size
, uint64_t num_integers
)
709 if (name
&& strlen(name
) > ZAP_MAXNAMELEN
)
712 /* Only integer sizes supported by C */
713 switch (integer_size
) {
723 if (integer_size
* num_integers
> ZAP_MAXVALUELEN
)
730 * Routines for manipulating attributes.
733 fzap_lookup(zap_name_t
*zn
,
734 uint64_t integer_size
, uint64_t num_integers
, void *buf
,
735 char *realname
, int rn_len
, boolean_t
*ncp
)
739 zap_entry_handle_t zeh
;
741 err
= fzap_checksize(zn
->zn_name_orij
, integer_size
, num_integers
);
745 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
748 err
= zap_leaf_lookup(l
, zn
, &zeh
);
750 err
= zap_entry_read(&zeh
, integer_size
, num_integers
, buf
);
751 (void) zap_entry_read_name(&zeh
, rn_len
, realname
);
753 *ncp
= zap_entry_normalization_conflict(&zeh
,
754 zn
, NULL
, zn
->zn_zap
);
763 fzap_add_cd(zap_name_t
*zn
,
764 uint64_t integer_size
, uint64_t num_integers
,
765 const void *val
, uint32_t cd
, dmu_tx_t
*tx
)
769 zap_entry_handle_t zeh
;
770 zap_t
*zap
= zn
->zn_zap
;
772 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
773 ASSERT(!zap
->zap_ismicro
);
774 ASSERT(fzap_checksize(zn
->zn_name_orij
,
775 integer_size
, num_integers
) == 0);
777 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
781 err
= zap_leaf_lookup(l
, zn
, &zeh
);
789 err
= zap_entry_create(l
, zn
->zn_name_orij
, zn
->zn_hash
, cd
,
790 integer_size
, num_integers
, val
, &zeh
);
793 zap_increment_num_entries(zap
, 1, tx
);
794 } else if (err
== EAGAIN
) {
795 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
796 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
803 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
808 fzap_add(zap_name_t
*zn
,
809 uint64_t integer_size
, uint64_t num_integers
,
810 const void *val
, dmu_tx_t
*tx
)
812 int err
= fzap_checksize(zn
->zn_name_orij
, integer_size
, num_integers
);
816 return (fzap_add_cd(zn
, integer_size
, num_integers
,
817 val
, ZAP_MAXCD
, tx
));
821 fzap_update(zap_name_t
*zn
,
822 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
826 zap_entry_handle_t zeh
;
827 zap_t
*zap
= zn
->zn_zap
;
829 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
830 err
= fzap_checksize(zn
->zn_name_orij
, integer_size
, num_integers
);
834 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
838 err
= zap_leaf_lookup(l
, zn
, &zeh
);
839 create
= (err
== ENOENT
);
840 ASSERT(err
== 0 || err
== ENOENT
);
843 err
= zap_entry_create(l
, zn
->zn_name_orij
, zn
->zn_hash
,
844 ZAP_MAXCD
, integer_size
, num_integers
, val
, &zeh
);
846 zap_increment_num_entries(zap
, 1, tx
);
848 err
= zap_entry_update(&zeh
, integer_size
, num_integers
, val
);
852 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
853 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
859 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
864 fzap_length(zap_name_t
*zn
,
865 uint64_t *integer_size
, uint64_t *num_integers
)
869 zap_entry_handle_t zeh
;
871 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
874 err
= zap_leaf_lookup(l
, zn
, &zeh
);
879 *integer_size
= zeh
.zeh_integer_size
;
881 *num_integers
= zeh
.zeh_num_integers
;
888 fzap_remove(zap_name_t
*zn
, dmu_tx_t
*tx
)
892 zap_entry_handle_t zeh
;
894 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
897 err
= zap_leaf_lookup(l
, zn
, &zeh
);
899 zap_entry_remove(&zeh
);
900 zap_increment_num_entries(zn
->zn_zap
, -1, tx
);
907 * Helper functions for consumers.
911 zap_value_search(objset_t
*os
, uint64_t zapobj
, uint64_t value
, uint64_t mask
,
921 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
922 for (zap_cursor_init(&zc
, os
, zapobj
);
923 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
924 zap_cursor_advance(&zc
)) {
925 if ((za
->za_first_integer
& mask
) == (value
& mask
)) {
926 (void) strcpy(name
, za
->za_name
);
930 zap_cursor_fini(&zc
);
931 kmem_free(za
, sizeof (zap_attribute_t
));
936 zap_join(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
, dmu_tx_t
*tx
)
942 for (zap_cursor_init(&zc
, os
, fromobj
);
943 zap_cursor_retrieve(&zc
, &za
) == 0;
944 (void) zap_cursor_advance(&zc
)) {
945 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1)
947 err
= zap_add(os
, intoobj
, za
.za_name
,
948 8, 1, &za
.za_first_integer
, tx
);
952 zap_cursor_fini(&zc
);
957 zap_add_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
961 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
962 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
966 zap_remove_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
970 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
971 return (zap_remove(os
, obj
, name
, tx
));
975 zap_lookup_int(objset_t
*os
, uint64_t obj
, uint64_t value
)
979 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
980 return (zap_lookup(os
, obj
, name
, 8, 1, &value
));
984 * Routines for iterating over the attributes.
988 fzap_cursor_retrieve(zap_t
*zap
, zap_cursor_t
*zc
, zap_attribute_t
*za
)
991 zap_entry_handle_t zeh
;
994 /* retrieve the next entry at or after zc_hash/zc_cd */
995 /* if no entry, return ENOENT */
998 (ZAP_HASH_IDX(zc
->zc_hash
,
999 zc
->zc_leaf
->l_phys
->l_hdr
.lh_prefix_len
) !=
1000 zc
->zc_leaf
->l_phys
->l_hdr
.lh_prefix
)) {
1001 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1002 zap_put_leaf(zc
->zc_leaf
);
1007 if (zc
->zc_leaf
== NULL
) {
1008 err
= zap_deref_leaf(zap
, zc
->zc_hash
, NULL
, RW_READER
,
1013 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1017 err
= zap_leaf_lookup_closest(l
, zc
->zc_hash
, zc
->zc_cd
, &zeh
);
1019 if (err
== ENOENT
) {
1021 (1ULL << (64 - l
->l_phys
->l_hdr
.lh_prefix_len
)) - 1;
1022 zc
->zc_hash
= (zc
->zc_hash
& ~nocare
) + nocare
+ 1;
1024 if (l
->l_phys
->l_hdr
.lh_prefix_len
== 0 || zc
->zc_hash
== 0) {
1025 zc
->zc_hash
= -1ULL;
1027 zap_put_leaf(zc
->zc_leaf
);
1034 zc
->zc_hash
= zeh
.zeh_hash
;
1035 zc
->zc_cd
= zeh
.zeh_cd
;
1036 za
->za_integer_length
= zeh
.zeh_integer_size
;
1037 za
->za_num_integers
= zeh
.zeh_num_integers
;
1038 if (zeh
.zeh_num_integers
== 0) {
1039 za
->za_first_integer
= 0;
1041 err
= zap_entry_read(&zeh
, 8, 1, &za
->za_first_integer
);
1042 ASSERT(err
== 0 || err
== EOVERFLOW
);
1044 err
= zap_entry_read_name(&zeh
,
1045 sizeof (za
->za_name
), za
->za_name
);
1048 za
->za_normalization_conflict
=
1049 zap_entry_normalization_conflict(&zeh
,
1050 NULL
, za
->za_name
, zap
);
1052 rw_exit(&zc
->zc_leaf
->l_rwlock
);
1058 zap_stats_ptrtbl(zap_t
*zap
, uint64_t *tbl
, int len
, zap_stats_t
*zs
)
1061 uint64_t lastblk
= 0;
1064 * NB: if a leaf has more pointers than an entire ptrtbl block
1065 * can hold, then it'll be accounted for more than once, since
1066 * we won't have lastblk.
1068 for (i
= 0; i
< len
; i
++) {
1071 if (tbl
[i
] == lastblk
)
1075 err
= zap_get_leaf_byblk(zap
, tbl
[i
], NULL
, RW_READER
, &l
);
1077 zap_leaf_stats(zap
, l
, zs
);
1084 fzap_get_stats(zap_t
*zap
, zap_stats_t
*zs
)
1086 int bs
= FZAP_BLOCK_SHIFT(zap
);
1087 zs
->zs_blocksize
= 1ULL << bs
;
1090 * Set zap_phys_t fields
1092 zs
->zs_num_leafs
= zap
->zap_f
.zap_phys
->zap_num_leafs
;
1093 zs
->zs_num_entries
= zap
->zap_f
.zap_phys
->zap_num_entries
;
1094 zs
->zs_num_blocks
= zap
->zap_f
.zap_phys
->zap_freeblk
;
1095 zs
->zs_block_type
= zap
->zap_f
.zap_phys
->zap_block_type
;
1096 zs
->zs_magic
= zap
->zap_f
.zap_phys
->zap_magic
;
1097 zs
->zs_salt
= zap
->zap_f
.zap_phys
->zap_salt
;
1100 * Set zap_ptrtbl fields
1102 zs
->zs_ptrtbl_len
= 1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
1103 zs
->zs_ptrtbl_nextblk
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_nextblk
;
1104 zs
->zs_ptrtbl_blks_copied
=
1105 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blks_copied
;
1106 zs
->zs_ptrtbl_zt_blk
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
;
1107 zs
->zs_ptrtbl_zt_numblks
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
;
1108 zs
->zs_ptrtbl_zt_shift
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
1110 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
== 0) {
1111 /* the ptrtbl is entirely in the header block. */
1112 zap_stats_ptrtbl(zap
, &ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
1113 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
), zs
);
1117 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
,
1118 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
<< bs
,
1119 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
<< bs
);
1121 for (b
= 0; b
< zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
;
1126 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
1127 (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
+ b
) << bs
,
1130 zap_stats_ptrtbl(zap
, db
->db_data
,
1132 dmu_buf_rele(db
, FTAG
);