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 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 static void zap_leaf_pageout(dmu_buf_t
*db
, void *vl
);
54 static uint64_t zap_allocate_blocks(zap_t
*zap
, int nblocks
);
58 fzap_byteswap(void *vbuf
, size_t size
)
62 block_type
= *(uint64_t *)vbuf
;
64 if (block_type
== ZBT_LEAF
|| block_type
== BSWAP_64(ZBT_LEAF
))
65 zap_leaf_byteswap(vbuf
, size
);
67 /* it's a ptrtbl block */
68 byteswap_uint64_array(vbuf
, size
);
73 fzap_upgrade(zap_t
*zap
, dmu_tx_t
*tx
, zap_flags_t flags
)
80 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
81 zap
->zap_ismicro
= FALSE
;
83 (void) dmu_buf_update_user(zap
->zap_dbuf
, zap
, zap
,
84 &zap
->zap_f
.zap_phys
, zap_evict
);
86 mutex_init(&zap
->zap_f
.zap_num_entries_mtx
, 0, 0, 0);
87 zap
->zap_f
.zap_block_shift
= highbit(zap
->zap_dbuf
->db_size
) - 1;
89 zp
= zap
->zap_f
.zap_phys
;
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_PUSHPAGE
);
120 l
->l_phys
= db
->db_data
;
122 zap_leaf_init(l
, zp
->zap_normflags
!= 0);
124 kmem_free(l
, sizeof (zap_leaf_t
));
125 dmu_buf_rele(db
, FTAG
);
129 zap_tryupgradedir(zap_t
*zap
, dmu_tx_t
*tx
)
131 if (RW_WRITE_HELD(&zap
->zap_rwlock
))
133 if (rw_tryupgrade(&zap
->zap_rwlock
)) {
134 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
141 * Generic routines for dealing with the pointer & cookie tables.
145 zap_table_grow(zap_t
*zap
, zap_table_phys_t
*tbl
,
146 void (*transfer_func
)(const uint64_t *src
, uint64_t *dst
, int n
),
150 dmu_buf_t
*db_old
, *db_new
;
152 int bs
= FZAP_BLOCK_SHIFT(zap
);
153 int hepb
= 1<<(bs
-4);
154 /* hepb = half the number of entries in a block */
156 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
157 ASSERT(tbl
->zt_blk
!= 0);
158 ASSERT(tbl
->zt_numblks
> 0);
160 if (tbl
->zt_nextblk
!= 0) {
161 newblk
= tbl
->zt_nextblk
;
163 newblk
= zap_allocate_blocks(zap
, tbl
->zt_numblks
* 2);
164 tbl
->zt_nextblk
= newblk
;
165 ASSERT3U(tbl
->zt_blks_copied
, ==, 0);
166 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
,
167 tbl
->zt_blk
<< bs
, tbl
->zt_numblks
<< bs
);
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
)
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
, DMU_READ_NO_PREFETCH
);
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_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
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
>= zap_hashbits(zap
) - 2)
330 if (zap
->zap_f
.zap_phys
->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
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
, ==,
341 ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
342 ASSERT3U(zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
, ==, 0);
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
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
= newblk
;
356 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
= 1;
357 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
++;
359 ASSERT3U(1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
, ==,
360 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
<<
361 (FZAP_BLOCK_SHIFT(zap
)-3));
365 return (zap_table_grow(zap
, &zap
->zap_f
.zap_phys
->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
->zap_f
.zap_phys
->zap_num_entries
>= -delta
);
376 zap
->zap_f
.zap_phys
->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
->zap_f
.zap_phys
->zap_freeblk
;
386 zap
->zap_f
.zap_phys
->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_PUSHPAGE
);
396 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
398 rw_init(&l
->l_rwlock
, NULL
, RW_DEFAULT
, NULL
);
399 rw_enter(&l
->l_rwlock
, RW_WRITER
);
400 l
->l_blkid
= zap_allocate_blocks(zap
, 1);
404 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
405 l
->l_blkid
<< FZAP_BLOCK_SHIFT(zap
), NULL
, &l
->l_dbuf
,
406 DMU_READ_NO_PREFETCH
));
407 winner
= dmu_buf_set_user(l
->l_dbuf
, l
, &l
->l_phys
, zap_leaf_pageout
);
408 ASSERT(winner
== NULL
);
409 dmu_buf_will_dirty(l
->l_dbuf
, tx
);
411 zap_leaf_init(l
, zap
->zap_normflags
!= 0);
413 zap
->zap_f
.zap_phys
->zap_num_leafs
++;
419 fzap_count(zap_t
*zap
, uint64_t *count
)
421 ASSERT(!zap
->zap_ismicro
);
422 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
); /* unnecessary */
423 *count
= zap
->zap_f
.zap_phys
->zap_num_entries
;
424 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
429 * Routines for obtaining zap_leaf_t's
433 zap_put_leaf(zap_leaf_t
*l
)
435 rw_exit(&l
->l_rwlock
);
436 dmu_buf_rele(l
->l_dbuf
, NULL
);
441 zap_leaf_pageout(dmu_buf_t
*db
, void *vl
)
445 rw_destroy(&l
->l_rwlock
);
446 kmem_free(l
, sizeof (zap_leaf_t
));
450 zap_open_leaf(uint64_t blkid
, dmu_buf_t
*db
)
452 zap_leaf_t
*l
, *winner
;
456 l
= kmem_alloc(sizeof (zap_leaf_t
), KM_PUSHPAGE
);
457 rw_init(&l
->l_rwlock
, NULL
, RW_DEFAULT
, NULL
);
458 rw_enter(&l
->l_rwlock
, RW_WRITER
);
460 l
->l_bs
= highbit(db
->db_size
)-1;
464 winner
= dmu_buf_set_user(db
, l
, &l
->l_phys
, zap_leaf_pageout
);
466 rw_exit(&l
->l_rwlock
);
467 if (winner
!= NULL
) {
468 /* someone else set it first */
469 zap_leaf_pageout(NULL
, l
);
474 * lhr_pad was previously used for the next leaf in the leaf
475 * chain. There should be no chained leafs (as we have removed
478 ASSERT3U(l
->l_phys
->l_hdr
.lh_pad1
, ==, 0);
481 * There should be more hash entries than there can be
482 * chunks to put in the hash table
484 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l
), >, ZAP_LEAF_NUMCHUNKS(l
) / 3);
486 /* The chunks should begin at the end of the hash table */
487 ASSERT3P(&ZAP_LEAF_CHUNK(l
, 0), ==, (zap_leaf_chunk_t
*)
488 &l
->l_phys
->l_hash
[ZAP_LEAF_HASH_NUMENTRIES(l
)]);
490 /* The chunks should end at the end of the block */
491 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l
, ZAP_LEAF_NUMCHUNKS(l
)) -
492 (uintptr_t)l
->l_phys
, ==, l
->l_dbuf
->db_size
);
498 zap_get_leaf_byblk(zap_t
*zap
, uint64_t blkid
, dmu_tx_t
*tx
, krw_t lt
,
503 int bs
= FZAP_BLOCK_SHIFT(zap
);
506 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
508 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
509 blkid
<< bs
, NULL
, &db
, DMU_READ_NO_PREFETCH
);
513 ASSERT3U(db
->db_object
, ==, zap
->zap_object
);
514 ASSERT3U(db
->db_offset
, ==, blkid
<< bs
);
515 ASSERT3U(db
->db_size
, ==, 1 << bs
);
518 l
= dmu_buf_get_user(db
);
521 l
= zap_open_leaf(blkid
, db
);
523 rw_enter(&l
->l_rwlock
, lt
);
525 * Must lock before dirtying, otherwise l->l_phys could change,
526 * causing ASSERT below to fail.
529 dmu_buf_will_dirty(db
, tx
);
530 ASSERT3U(l
->l_blkid
, ==, blkid
);
531 ASSERT3P(l
->l_dbuf
, ==, db
);
532 ASSERT3P(l
->l_phys
, ==, l
->l_dbuf
->db_data
);
533 ASSERT3U(l
->l_phys
->l_hdr
.lh_block_type
, ==, ZBT_LEAF
);
534 ASSERT3U(l
->l_phys
->l_hdr
.lh_magic
, ==, ZAP_LEAF_MAGIC
);
541 zap_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t *valp
)
543 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
545 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
== 0) {
547 (1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
));
548 *valp
= ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
);
551 return (zap_table_load(zap
, &zap
->zap_f
.zap_phys
->zap_ptrtbl
,
557 zap_set_idx_to_blk(zap_t
*zap
, uint64_t idx
, uint64_t blk
, dmu_tx_t
*tx
)
560 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
562 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
== 0) {
563 ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
) = blk
;
566 return (zap_table_store(zap
, &zap
->zap_f
.zap_phys
->zap_ptrtbl
,
572 zap_deref_leaf(zap_t
*zap
, uint64_t h
, dmu_tx_t
*tx
, krw_t lt
, zap_leaf_t
**lp
)
577 ASSERT(zap
->zap_dbuf
== NULL
||
578 zap
->zap_f
.zap_phys
== zap
->zap_dbuf
->db_data
);
579 ASSERT3U(zap
->zap_f
.zap_phys
->zap_magic
, ==, ZAP_MAGIC
);
580 idx
= ZAP_HASH_IDX(h
, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
581 err
= zap_idx_to_blk(zap
, idx
, &blk
);
584 err
= zap_get_leaf_byblk(zap
, blk
, tx
, lt
, lp
);
586 ASSERT(err
|| ZAP_HASH_IDX(h
, (*lp
)->l_phys
->l_hdr
.lh_prefix_len
) ==
587 (*lp
)->l_phys
->l_hdr
.lh_prefix
);
592 zap_expand_leaf(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
, zap_leaf_t
**lp
)
594 zap_t
*zap
= zn
->zn_zap
;
595 uint64_t hash
= zn
->zn_hash
;
597 int prefix_diff
, i
, err
;
599 int old_prefix_len
= l
->l_phys
->l_hdr
.lh_prefix_len
;
601 ASSERT3U(old_prefix_len
, <=, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
602 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
604 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
605 l
->l_phys
->l_hdr
.lh_prefix
);
607 if (zap_tryupgradedir(zap
, tx
) == 0 ||
608 old_prefix_len
== zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
) {
609 /* We failed to upgrade, or need to grow the pointer table */
610 objset_t
*os
= zap
->zap_objset
;
611 uint64_t object
= zap
->zap_object
;
615 err
= zap_lockdir(os
, object
, tx
, RW_WRITER
,
616 FALSE
, FALSE
, &zn
->zn_zap
);
620 ASSERT(!zap
->zap_ismicro
);
622 while (old_prefix_len
==
623 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
) {
624 err
= zap_grow_ptrtbl(zap
, tx
);
629 err
= zap_deref_leaf(zap
, hash
, tx
, RW_WRITER
, &l
);
633 if (l
->l_phys
->l_hdr
.lh_prefix_len
!= old_prefix_len
) {
634 /* it split while our locks were down */
639 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
640 ASSERT3U(old_prefix_len
, <, zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
641 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
642 l
->l_phys
->l_hdr
.lh_prefix
);
644 prefix_diff
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
-
645 (old_prefix_len
+ 1);
646 sibling
= (ZAP_HASH_IDX(hash
, old_prefix_len
+ 1) | 1) << prefix_diff
;
648 /* check for i/o errors before doing zap_leaf_split */
649 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
651 err
= zap_idx_to_blk(zap
, sibling
+i
, &blk
);
654 ASSERT3U(blk
, ==, l
->l_blkid
);
657 nl
= zap_create_leaf(zap
, tx
);
658 zap_leaf_split(l
, nl
, zap
->zap_normflags
!= 0);
660 /* set sibling pointers */
661 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
662 err
= zap_set_idx_to_blk(zap
, sibling
+i
, nl
->l_blkid
, tx
);
663 ASSERT3U(err
, ==, 0); /* we checked for i/o errors above */
666 if (hash
& (1ULL << (64 - l
->l_phys
->l_hdr
.lh_prefix_len
))) {
667 /* we want the sibling */
679 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
)
681 zap_t
*zap
= zn
->zn_zap
;
682 int shift
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
683 int leaffull
= (l
->l_phys
->l_hdr
.lh_prefix_len
== shift
&&
684 l
->l_phys
->l_hdr
.lh_nfree
< ZAP_LEAF_LOW_WATER
);
688 if (leaffull
|| zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_nextblk
) {
692 * We are in the middle of growing the pointer table, or
693 * this leaf will soon make us grow it.
695 if (zap_tryupgradedir(zap
, tx
) == 0) {
696 objset_t
*os
= zap
->zap_objset
;
697 uint64_t zapobj
= zap
->zap_object
;
700 err
= zap_lockdir(os
, zapobj
, tx
,
701 RW_WRITER
, FALSE
, FALSE
, &zn
->zn_zap
);
707 /* could have finished growing while our locks were down */
708 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
== shift
)
709 (void) zap_grow_ptrtbl(zap
, tx
);
714 fzap_checkname(zap_name_t
*zn
)
716 if (zn
->zn_key_orig_numints
* zn
->zn_key_intlen
> ZAP_MAXNAMELEN
)
717 return (ENAMETOOLONG
);
722 fzap_checksize(uint64_t integer_size
, uint64_t num_integers
)
724 /* Only integer sizes supported by C */
725 switch (integer_size
) {
735 if (integer_size
* num_integers
> ZAP_MAXVALUELEN
)
742 fzap_check(zap_name_t
*zn
, uint64_t integer_size
, uint64_t num_integers
)
746 if ((err
= fzap_checkname(zn
)) != 0)
748 return (fzap_checksize(integer_size
, num_integers
));
752 * Routines for manipulating attributes.
755 fzap_lookup(zap_name_t
*zn
,
756 uint64_t integer_size
, uint64_t num_integers
, void *buf
,
757 char *realname
, int rn_len
, boolean_t
*ncp
)
761 zap_entry_handle_t zeh
;
763 if ((err
= fzap_checkname(zn
)) != 0)
766 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
769 err
= zap_leaf_lookup(l
, zn
, &zeh
);
771 if ((err
= fzap_checksize(integer_size
, num_integers
)) != 0) {
776 err
= zap_entry_read(&zeh
, integer_size
, num_integers
, buf
);
777 (void) zap_entry_read_name(zn
->zn_zap
, &zeh
, rn_len
, realname
);
779 *ncp
= zap_entry_normalization_conflict(&zeh
,
780 zn
, NULL
, zn
->zn_zap
);
789 fzap_add_cd(zap_name_t
*zn
,
790 uint64_t integer_size
, uint64_t num_integers
,
791 const void *val
, uint32_t cd
, dmu_tx_t
*tx
)
795 zap_entry_handle_t zeh
;
796 zap_t
*zap
= zn
->zn_zap
;
798 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
799 ASSERT(!zap
->zap_ismicro
);
800 ASSERT(fzap_check(zn
, integer_size
, num_integers
) == 0);
802 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
806 err
= zap_leaf_lookup(l
, zn
, &zeh
);
814 err
= zap_entry_create(l
, zn
, cd
,
815 integer_size
, num_integers
, val
, &zeh
);
818 zap_increment_num_entries(zap
, 1, tx
);
819 } else if (err
== EAGAIN
) {
820 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
821 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
828 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
833 fzap_add(zap_name_t
*zn
,
834 uint64_t integer_size
, uint64_t num_integers
,
835 const void *val
, dmu_tx_t
*tx
)
837 int err
= fzap_check(zn
, integer_size
, num_integers
);
841 return (fzap_add_cd(zn
, integer_size
, num_integers
,
842 val
, ZAP_NEED_CD
, tx
));
846 fzap_update(zap_name_t
*zn
,
847 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
851 zap_entry_handle_t zeh
;
852 zap_t
*zap
= zn
->zn_zap
;
854 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
855 err
= fzap_check(zn
, integer_size
, num_integers
);
859 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
863 err
= zap_leaf_lookup(l
, zn
, &zeh
);
864 create
= (err
== ENOENT
);
865 ASSERT(err
== 0 || err
== ENOENT
);
868 err
= zap_entry_create(l
, zn
, ZAP_NEED_CD
,
869 integer_size
, num_integers
, val
, &zeh
);
871 zap_increment_num_entries(zap
, 1, tx
);
873 err
= zap_entry_update(&zeh
, integer_size
, num_integers
, val
);
877 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
878 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
884 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
889 fzap_length(zap_name_t
*zn
,
890 uint64_t *integer_size
, uint64_t *num_integers
)
894 zap_entry_handle_t zeh
;
896 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
899 err
= zap_leaf_lookup(l
, zn
, &zeh
);
904 *integer_size
= zeh
.zeh_integer_size
;
906 *num_integers
= zeh
.zeh_num_integers
;
913 fzap_remove(zap_name_t
*zn
, dmu_tx_t
*tx
)
917 zap_entry_handle_t zeh
;
919 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
922 err
= zap_leaf_lookup(l
, zn
, &zeh
);
924 zap_entry_remove(&zeh
);
925 zap_increment_num_entries(zn
->zn_zap
, -1, tx
);
932 fzap_prefetch(zap_name_t
*zn
)
935 zap_t
*zap
= zn
->zn_zap
;
938 idx
= ZAP_HASH_IDX(zn
->zn_hash
,
939 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
);
940 if (zap_idx_to_blk(zap
, idx
, &blk
) != 0)
942 bs
= FZAP_BLOCK_SHIFT(zap
);
943 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
, blk
<< bs
, 1 << bs
);
947 * Helper functions for consumers.
951 zap_create_link(objset_t
*os
, dmu_object_type_t ot
, uint64_t parent_obj
,
952 const char *name
, dmu_tx_t
*tx
)
956 VERIFY((new_obj
= zap_create(os
, ot
, DMU_OT_NONE
, 0, tx
)) > 0);
957 VERIFY(zap_add(os
, parent_obj
, name
, sizeof (uint64_t), 1, &new_obj
,
964 zap_value_search(objset_t
*os
, uint64_t zapobj
, uint64_t value
, uint64_t mask
,
974 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_PUSHPAGE
);
975 for (zap_cursor_init(&zc
, os
, zapobj
);
976 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
977 zap_cursor_advance(&zc
)) {
978 if ((za
->za_first_integer
& mask
) == (value
& mask
)) {
979 (void) strcpy(name
, za
->za_name
);
983 zap_cursor_fini(&zc
);
984 kmem_free(za
, sizeof (zap_attribute_t
));
989 zap_join(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
, dmu_tx_t
*tx
)
995 for (zap_cursor_init(&zc
, os
, fromobj
);
996 zap_cursor_retrieve(&zc
, &za
) == 0;
997 (void) zap_cursor_advance(&zc
)) {
998 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1)
1000 err
= zap_add(os
, intoobj
, za
.za_name
,
1001 8, 1, &za
.za_first_integer
, tx
);
1005 zap_cursor_fini(&zc
);
1010 zap_join_key(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1011 uint64_t value
, dmu_tx_t
*tx
)
1017 for (zap_cursor_init(&zc
, os
, fromobj
);
1018 zap_cursor_retrieve(&zc
, &za
) == 0;
1019 (void) zap_cursor_advance(&zc
)) {
1020 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1)
1022 err
= zap_add(os
, intoobj
, za
.za_name
,
1027 zap_cursor_fini(&zc
);
1032 zap_join_increment(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1039 for (zap_cursor_init(&zc
, os
, fromobj
);
1040 zap_cursor_retrieve(&zc
, &za
) == 0;
1041 (void) zap_cursor_advance(&zc
)) {
1044 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1)
1047 err
= zap_lookup(os
, intoobj
, za
.za_name
, 8, 1, &delta
);
1048 if (err
!= 0 && err
!= ENOENT
)
1050 delta
+= za
.za_first_integer
;
1051 err
= zap_update(os
, intoobj
, za
.za_name
, 8, 1, &delta
, tx
);
1055 zap_cursor_fini(&zc
);
1060 zap_add_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1064 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1065 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
1069 zap_remove_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1073 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1074 return (zap_remove(os
, obj
, name
, tx
));
1078 zap_lookup_int(objset_t
*os
, uint64_t obj
, uint64_t value
)
1082 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1083 return (zap_lookup(os
, obj
, name
, 8, 1, &value
));
1087 zap_add_int_key(objset_t
*os
, uint64_t obj
,
1088 uint64_t key
, uint64_t value
, dmu_tx_t
*tx
)
1092 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1093 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
1097 zap_update_int_key(objset_t
*os
, uint64_t obj
,
1098 uint64_t key
, uint64_t value
, dmu_tx_t
*tx
)
1102 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1103 return (zap_update(os
, obj
, name
, 8, 1, &value
, tx
));
1107 zap_lookup_int_key(objset_t
*os
, uint64_t obj
, uint64_t key
, uint64_t *valuep
)
1111 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1112 return (zap_lookup(os
, obj
, name
, 8, 1, valuep
));
1116 zap_increment(objset_t
*os
, uint64_t obj
, const char *name
, int64_t delta
,
1125 err
= zap_lookup(os
, obj
, name
, 8, 1, &value
);
1126 if (err
!= 0 && err
!= ENOENT
)
1130 err
= zap_remove(os
, obj
, name
, tx
);
1132 err
= zap_update(os
, obj
, name
, 8, 1, &value
, tx
);
1137 zap_increment_int(objset_t
*os
, uint64_t obj
, uint64_t key
, int64_t delta
,
1142 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1143 return (zap_increment(os
, obj
, name
, delta
, tx
));
1147 * Routines for iterating over the attributes.
1151 fzap_cursor_retrieve(zap_t
*zap
, zap_cursor_t
*zc
, zap_attribute_t
*za
)
1154 zap_entry_handle_t zeh
;
1157 /* retrieve the next entry at or after zc_hash/zc_cd */
1158 /* if no entry, return ENOENT */
1161 (ZAP_HASH_IDX(zc
->zc_hash
,
1162 zc
->zc_leaf
->l_phys
->l_hdr
.lh_prefix_len
) !=
1163 zc
->zc_leaf
->l_phys
->l_hdr
.lh_prefix
)) {
1164 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1165 zap_put_leaf(zc
->zc_leaf
);
1170 if (zc
->zc_leaf
== NULL
) {
1171 err
= zap_deref_leaf(zap
, zc
->zc_hash
, NULL
, RW_READER
,
1176 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1180 err
= zap_leaf_lookup_closest(l
, zc
->zc_hash
, zc
->zc_cd
, &zeh
);
1182 if (err
== ENOENT
) {
1184 (1ULL << (64 - l
->l_phys
->l_hdr
.lh_prefix_len
)) - 1;
1185 zc
->zc_hash
= (zc
->zc_hash
& ~nocare
) + nocare
+ 1;
1187 if (l
->l_phys
->l_hdr
.lh_prefix_len
== 0 || zc
->zc_hash
== 0) {
1188 zc
->zc_hash
= -1ULL;
1190 zap_put_leaf(zc
->zc_leaf
);
1197 zc
->zc_hash
= zeh
.zeh_hash
;
1198 zc
->zc_cd
= zeh
.zeh_cd
;
1199 za
->za_integer_length
= zeh
.zeh_integer_size
;
1200 za
->za_num_integers
= zeh
.zeh_num_integers
;
1201 if (zeh
.zeh_num_integers
== 0) {
1202 za
->za_first_integer
= 0;
1204 err
= zap_entry_read(&zeh
, 8, 1, &za
->za_first_integer
);
1205 ASSERT(err
== 0 || err
== EOVERFLOW
);
1207 err
= zap_entry_read_name(zap
, &zeh
,
1208 sizeof (za
->za_name
), za
->za_name
);
1211 za
->za_normalization_conflict
=
1212 zap_entry_normalization_conflict(&zeh
,
1213 NULL
, za
->za_name
, zap
);
1215 rw_exit(&zc
->zc_leaf
->l_rwlock
);
1220 zap_stats_ptrtbl(zap_t
*zap
, uint64_t *tbl
, int len
, zap_stats_t
*zs
)
1223 uint64_t lastblk
= 0;
1226 * NB: if a leaf has more pointers than an entire ptrtbl block
1227 * can hold, then it'll be accounted for more than once, since
1228 * we won't have lastblk.
1230 for (i
= 0; i
< len
; i
++) {
1233 if (tbl
[i
] == lastblk
)
1237 err
= zap_get_leaf_byblk(zap
, tbl
[i
], NULL
, RW_READER
, &l
);
1239 zap_leaf_stats(zap
, l
, zs
);
1246 fzap_cursor_move_to_key(zap_cursor_t
*zc
, zap_name_t
*zn
)
1250 zap_entry_handle_t zeh
;
1252 if (zn
->zn_key_orig_numints
* zn
->zn_key_intlen
> ZAP_MAXNAMELEN
)
1253 return (ENAMETOOLONG
);
1255 err
= zap_deref_leaf(zc
->zc_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
1259 err
= zap_leaf_lookup(l
, zn
, &zeh
);
1264 zc
->zc_hash
= zeh
.zeh_hash
;
1265 zc
->zc_cd
= zeh
.zeh_cd
;
1271 fzap_get_stats(zap_t
*zap
, zap_stats_t
*zs
)
1273 int bs
= FZAP_BLOCK_SHIFT(zap
);
1274 zs
->zs_blocksize
= 1ULL << bs
;
1277 * Set zap_phys_t fields
1279 zs
->zs_num_leafs
= zap
->zap_f
.zap_phys
->zap_num_leafs
;
1280 zs
->zs_num_entries
= zap
->zap_f
.zap_phys
->zap_num_entries
;
1281 zs
->zs_num_blocks
= zap
->zap_f
.zap_phys
->zap_freeblk
;
1282 zs
->zs_block_type
= zap
->zap_f
.zap_phys
->zap_block_type
;
1283 zs
->zs_magic
= zap
->zap_f
.zap_phys
->zap_magic
;
1284 zs
->zs_salt
= zap
->zap_f
.zap_phys
->zap_salt
;
1287 * Set zap_ptrtbl fields
1289 zs
->zs_ptrtbl_len
= 1ULL << zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
1290 zs
->zs_ptrtbl_nextblk
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_nextblk
;
1291 zs
->zs_ptrtbl_blks_copied
=
1292 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blks_copied
;
1293 zs
->zs_ptrtbl_zt_blk
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
;
1294 zs
->zs_ptrtbl_zt_numblks
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
;
1295 zs
->zs_ptrtbl_zt_shift
= zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_shift
;
1297 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
== 0) {
1298 /* the ptrtbl is entirely in the header block. */
1299 zap_stats_ptrtbl(zap
, &ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
1300 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
), zs
);
1304 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
,
1305 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
<< bs
,
1306 zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
<< bs
);
1308 for (b
= 0; b
< zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_numblks
;
1313 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
1314 (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
+ b
) << bs
,
1315 FTAG
, &db
, DMU_READ_NO_PREFETCH
);
1317 zap_stats_ptrtbl(zap
, db
->db_data
,
1319 dmu_buf_rele(db
, FTAG
);
1326 fzap_count_write(zap_name_t
*zn
, int add
, uint64_t *towrite
,
1327 uint64_t *tooverwrite
)
1329 zap_t
*zap
= zn
->zn_zap
;
1334 * Account for the header block of the fatzap.
1336 if (!add
&& dmu_buf_freeable(zap
->zap_dbuf
)) {
1337 *tooverwrite
+= zap
->zap_dbuf
->db_size
;
1339 *towrite
+= zap
->zap_dbuf
->db_size
;
1343 * Account for the pointer table blocks.
1344 * If we are adding we need to account for the following cases :
1345 * - If the pointer table is embedded, this operation could force an
1346 * external pointer table.
1347 * - If this already has an external pointer table this operation
1348 * could extend the table.
1351 if (zap
->zap_f
.zap_phys
->zap_ptrtbl
.zt_blk
== 0)
1352 *towrite
+= zap
->zap_dbuf
->db_size
;
1354 *towrite
+= (zap
->zap_dbuf
->db_size
* 3);
1358 * Now, check if the block containing leaf is freeable
1359 * and account accordingly.
1361 err
= zap_deref_leaf(zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
1366 if (!add
&& dmu_buf_freeable(l
->l_dbuf
)) {
1367 *tooverwrite
+= l
->l_dbuf
->db_size
;
1370 * If this an add operation, the leaf block could split.
1371 * Hence, we need to account for an additional leaf block.
1373 *towrite
+= (add
? 2 : 1) * l
->l_dbuf
->db_size
;