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, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * This file contains the top half of the zfs directory structure
29 * implementation. The bottom half is in zap_leaf.c.
31 * The zdir is an extendable hash data structure. There is a table of
32 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
33 * each a constant size and hold a variable number of directory entries.
34 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
36 * The pointer table holds a power of 2 number of pointers.
37 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
38 * by the pointer at index i in the table holds entries whose hash value
39 * has a zd_prefix_len - bit prefix
44 #include <sys/zfs_context.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/fs/zfs.h>
48 #include <sys/refcount.h>
49 #include <sys/zap_impl.h>
50 #include <sys/zap_leaf.h>
52 int fzap_default_block_shift
= 14; /* 16k blocksize */
54 extern inline zap_phys_t
*zap_f_phys(zap_t
*zap
);
56 static uint64_t zap_allocate_blocks(zap_t
*zap
, int nblocks
);
59 fzap_byteswap(void *vbuf
, size_t size
)
63 block_type
= *(uint64_t *)vbuf
;
65 if (block_type
== ZBT_LEAF
|| block_type
== BSWAP_64(ZBT_LEAF
))
66 zap_leaf_byteswap(vbuf
, size
);
68 /* it's a ptrtbl block */
69 byteswap_uint64_array(vbuf
, size
);
74 fzap_upgrade(zap_t
*zap
, dmu_tx_t
*tx
, zap_flags_t flags
)
81 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
82 zap
->zap_ismicro
= FALSE
;
84 zap
->zap_dbu
.dbu_evict_func
= zap_evict
;
86 mutex_init(&zap
->zap_f
.zap_num_entries_mtx
, 0, 0, 0);
87 zap
->zap_f
.zap_block_shift
= highbit64(zap
->zap_dbuf
->db_size
) - 1;
91 * explicitly zero it since it might be coming from an
92 * initialized microzap
94 bzero(zap
->zap_dbuf
->db_data
, zap
->zap_dbuf
->db_size
);
95 zp
->zap_block_type
= ZBT_HEADER
;
96 zp
->zap_magic
= ZAP_MAGIC
;
98 zp
->zap_ptrtbl
.zt_shift
= ZAP_EMBEDDED_PTRTBL_SHIFT(zap
);
100 zp
->zap_freeblk
= 2; /* block 1 will be the first leaf */
101 zp
->zap_num_leafs
= 1;
102 zp
->zap_num_entries
= 0;
103 zp
->zap_salt
= zap
->zap_salt
;
104 zp
->zap_normflags
= zap
->zap_normflags
;
105 zp
->zap_flags
= flags
;
107 /* block 1 will be the first leaf */
108 for (i
= 0; i
< (1<<zp
->zap_ptrtbl
.zt_shift
); i
++)
109 ZAP_EMBEDDED_PTRTBL_ENT(zap
, i
) = 1;
112 * set up block 1 - the first leaf
114 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
115 1<<FZAP_BLOCK_SHIFT(zap
), FTAG
, &db
, DMU_READ_NO_PREFETCH
));
116 dmu_buf_will_dirty(db
, tx
);
118 l
= kmem_zalloc(sizeof (zap_leaf_t
), KM_SLEEP
);
121 zap_leaf_init(l
, zp
->zap_normflags
!= 0);
123 kmem_free(l
, sizeof (zap_leaf_t
));
124 dmu_buf_rele(db
, FTAG
);
128 zap_tryupgradedir(zap_t
*zap
, dmu_tx_t
*tx
)
130 if (RW_WRITE_HELD(&zap
->zap_rwlock
))
132 if (rw_tryupgrade(&zap
->zap_rwlock
)) {
133 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
140 * Generic routines for dealing with the pointer & cookie tables.
144 zap_table_grow(zap_t
*zap
, zap_table_phys_t
*tbl
,
145 void (*transfer_func
)(const uint64_t *src
, uint64_t *dst
, int n
),
149 dmu_buf_t
*db_old
, *db_new
;
151 int bs
= FZAP_BLOCK_SHIFT(zap
);
152 int hepb
= 1<<(bs
-4);
153 /* hepb = half the number of entries in a block */
155 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
156 ASSERT(tbl
->zt_blk
!= 0);
157 ASSERT(tbl
->zt_numblks
> 0);
159 if (tbl
->zt_nextblk
!= 0) {
160 newblk
= tbl
->zt_nextblk
;
162 newblk
= zap_allocate_blocks(zap
, tbl
->zt_numblks
* 2);
163 tbl
->zt_nextblk
= newblk
;
164 ASSERT0(tbl
->zt_blks_copied
);
165 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
, 0,
166 tbl
->zt_blk
<< bs
, tbl
->zt_numblks
<< bs
,
167 ZIO_PRIORITY_SYNC_READ
);
171 * Copy the ptrtbl from the old to new location.
174 b
= tbl
->zt_blks_copied
;
175 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
176 (tbl
->zt_blk
+ b
) << bs
, FTAG
, &db_old
, DMU_READ_NO_PREFETCH
);
180 /* first half of entries in old[b] go to new[2*b+0] */
181 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
182 (newblk
+ 2*b
+0) << bs
, FTAG
, &db_new
, DMU_READ_NO_PREFETCH
));
183 dmu_buf_will_dirty(db_new
, tx
);
184 transfer_func(db_old
->db_data
, db_new
->db_data
, hepb
);
185 dmu_buf_rele(db_new
, FTAG
);
187 /* second half of entries in old[b] go to new[2*b+1] */
188 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
189 (newblk
+ 2*b
+1) << bs
, FTAG
, &db_new
, DMU_READ_NO_PREFETCH
));
190 dmu_buf_will_dirty(db_new
, tx
);
191 transfer_func((uint64_t *)db_old
->db_data
+ hepb
,
192 db_new
->db_data
, hepb
);
193 dmu_buf_rele(db_new
, FTAG
);
195 dmu_buf_rele(db_old
, FTAG
);
197 tbl
->zt_blks_copied
++;
199 dprintf("copied block %llu of %llu\n",
200 tbl
->zt_blks_copied
, tbl
->zt_numblks
);
202 if (tbl
->zt_blks_copied
== tbl
->zt_numblks
) {
203 (void) dmu_free_range(zap
->zap_objset
, zap
->zap_object
,
204 tbl
->zt_blk
<< bs
, tbl
->zt_numblks
<< bs
, tx
);
206 tbl
->zt_blk
= newblk
;
207 tbl
->zt_numblks
*= 2;
210 tbl
->zt_blks_copied
= 0;
212 dprintf("finished; numblocks now %llu (%lluk entries)\n",
213 tbl
->zt_numblks
, 1<<(tbl
->zt_shift
-10));
220 zap_table_store(zap_t
*zap
, zap_table_phys_t
*tbl
, uint64_t idx
, uint64_t val
,
225 int bs
= FZAP_BLOCK_SHIFT(zap
);
228 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
229 ASSERT(tbl
->zt_blk
!= 0);
231 dprintf("storing %llx at index %llx\n", val
, idx
);
234 off
= idx
& ((1<<(bs
-3))-1);
236 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
237 (tbl
->zt_blk
+ blk
) << bs
, FTAG
, &db
, DMU_READ_NO_PREFETCH
);
240 dmu_buf_will_dirty(db
, tx
);
242 if (tbl
->zt_nextblk
!= 0) {
243 uint64_t idx2
= idx
* 2;
244 uint64_t blk2
= idx2
>> (bs
-3);
245 uint64_t off2
= idx2
& ((1<<(bs
-3))-1);
248 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
249 (tbl
->zt_nextblk
+ blk2
) << bs
, FTAG
, &db2
,
250 DMU_READ_NO_PREFETCH
);
252 dmu_buf_rele(db
, FTAG
);
255 dmu_buf_will_dirty(db2
, tx
);
256 ((uint64_t *)db2
->db_data
)[off2
] = val
;
257 ((uint64_t *)db2
->db_data
)[off2
+1] = val
;
258 dmu_buf_rele(db2
, FTAG
);
261 ((uint64_t *)db
->db_data
)[off
] = val
;
262 dmu_buf_rele(db
, FTAG
);
268 zap_table_load(zap_t
*zap
, zap_table_phys_t
*tbl
, uint64_t idx
, uint64_t *valp
)
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
);
299 dmu_buf_rele(db
, FTAG
);
305 * Routines for growing the ptrtbl.
309 zap_ptrtbl_transfer(const uint64_t *src
, uint64_t *dst
, int n
)
312 for (i
= 0; i
< n
; i
++) {
313 uint64_t lb
= src
[i
];
320 zap_grow_ptrtbl(zap_t
*zap
, dmu_tx_t
*tx
)
323 * The pointer table should never use more hash bits than we
324 * have (otherwise we'd be using useless zero bits to index it).
325 * If we are within 2 bits of running out, stop growing, since
326 * this is already an aberrant condition.
328 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
>= zap_hashbits(zap
) - 2)
329 return (SET_ERROR(ENOSPC
));
331 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
333 * We are outgrowing the "embedded" ptrtbl (the one
334 * stored in the header block). Give it its own entire
335 * block, which will double the size of the ptrtbl.
341 ASSERT3U(zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
, ==,
342 ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
343 ASSERT0(zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
);
345 newblk
= zap_allocate_blocks(zap
, 1);
346 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
347 newblk
<< FZAP_BLOCK_SHIFT(zap
), FTAG
, &db_new
,
348 DMU_READ_NO_PREFETCH
);
351 dmu_buf_will_dirty(db_new
, tx
);
352 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
353 db_new
->db_data
, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
));
354 dmu_buf_rele(db_new
, FTAG
);
356 zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
= newblk
;
357 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
= 1;
358 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
++;
360 ASSERT3U(1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
, ==,
361 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
<<
362 (FZAP_BLOCK_SHIFT(zap
)-3));
366 return (zap_table_grow(zap
, &zap_f_phys(zap
)->zap_ptrtbl
,
367 zap_ptrtbl_transfer
, tx
));
372 zap_increment_num_entries(zap_t
*zap
, int delta
, dmu_tx_t
*tx
)
374 dmu_buf_will_dirty(zap
->zap_dbuf
, tx
);
375 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
);
376 ASSERT(delta
> 0 || zap_f_phys(zap
)->zap_num_entries
>= -delta
);
377 zap_f_phys(zap
)->zap_num_entries
+= delta
;
378 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
382 zap_allocate_blocks(zap_t
*zap
, int nblocks
)
385 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
386 newblk
= zap_f_phys(zap
)->zap_freeblk
;
387 zap_f_phys(zap
)->zap_freeblk
+= nblocks
;
392 zap_leaf_pageout(void *dbu
)
396 rw_destroy(&l
->l_rwlock
);
397 kmem_free(l
, sizeof (zap_leaf_t
));
401 zap_create_leaf(zap_t
*zap
, dmu_tx_t
*tx
)
404 zap_leaf_t
*l
= kmem_zalloc(sizeof (zap_leaf_t
), KM_SLEEP
);
406 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
408 rw_init(&l
->l_rwlock
, 0, 0, 0);
409 rw_enter(&l
->l_rwlock
, RW_WRITER
);
410 l
->l_blkid
= zap_allocate_blocks(zap
, 1);
413 VERIFY(0 == dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
414 l
->l_blkid
<< FZAP_BLOCK_SHIFT(zap
), NULL
, &l
->l_dbuf
,
415 DMU_READ_NO_PREFETCH
));
416 dmu_buf_init_user(&l
->l_dbu
, zap_leaf_pageout
, &l
->l_dbuf
);
417 winner
= dmu_buf_set_user(l
->l_dbuf
, &l
->l_dbu
);
418 ASSERT(winner
== NULL
);
419 dmu_buf_will_dirty(l
->l_dbuf
, tx
);
421 zap_leaf_init(l
, zap
->zap_normflags
!= 0);
423 zap_f_phys(zap
)->zap_num_leafs
++;
429 fzap_count(zap_t
*zap
, uint64_t *count
)
431 ASSERT(!zap
->zap_ismicro
);
432 mutex_enter(&zap
->zap_f
.zap_num_entries_mtx
); /* unnecessary */
433 *count
= zap_f_phys(zap
)->zap_num_entries
;
434 mutex_exit(&zap
->zap_f
.zap_num_entries_mtx
);
439 * Routines for obtaining zap_leaf_t's
443 zap_put_leaf(zap_leaf_t
*l
)
445 rw_exit(&l
->l_rwlock
);
446 dmu_buf_rele(l
->l_dbuf
, NULL
);
450 zap_open_leaf(uint64_t blkid
, dmu_buf_t
*db
)
452 zap_leaf_t
*l
, *winner
;
456 l
= kmem_zalloc(sizeof (zap_leaf_t
), KM_SLEEP
);
457 rw_init(&l
->l_rwlock
, 0, 0, 0);
458 rw_enter(&l
->l_rwlock
, RW_WRITER
);
460 l
->l_bs
= highbit64(db
->db_size
) - 1;
463 dmu_buf_init_user(&l
->l_dbu
, zap_leaf_pageout
, &l
->l_dbuf
);
464 winner
= dmu_buf_set_user(db
, &l
->l_dbu
);
466 rw_exit(&l
->l_rwlock
);
467 if (winner
!= NULL
) {
468 /* someone else set it first */
469 zap_leaf_pageout(&l
->l_dbu
);
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 ASSERT0(zap_leaf_phys(l
)->l_hdr
.lh_pad1
);
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), ==,
488 &zap_leaf_phys(l
)->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)zap_leaf_phys(l
), ==, 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 zap_leaf_phys(l) 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 ASSERT3U(zap_leaf_phys(l
)->l_hdr
.lh_block_type
, ==, ZBT_LEAF
);
533 ASSERT3U(zap_leaf_phys(l
)->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_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
546 (1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
));
547 *valp
= ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
);
550 return (zap_table_load(zap
, &zap_f_phys(zap
)->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_f_phys(zap
)->zap_ptrtbl
.zt_blk
== 0) {
562 ZAP_EMBEDDED_PTRTBL_ENT(zap
, idx
) = blk
;
565 return (zap_table_store(zap
, &zap_f_phys(zap
)->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_f_phys(zap
) == zap
->zap_dbuf
->db_data
);
579 /* Reality check for corrupt zap objects (leaf or header). */
580 if ((zap_f_phys(zap
)->zap_block_type
!= ZBT_LEAF
&&
581 zap_f_phys(zap
)->zap_block_type
!= ZBT_HEADER
) ||
582 zap_f_phys(zap
)->zap_magic
!= ZAP_MAGIC
) {
583 return (SET_ERROR(EIO
));
586 idx
= ZAP_HASH_IDX(h
, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
587 err
= zap_idx_to_blk(zap
, idx
, &blk
);
590 err
= zap_get_leaf_byblk(zap
, blk
, tx
, lt
, lp
);
593 ZAP_HASH_IDX(h
, zap_leaf_phys(*lp
)->l_hdr
.lh_prefix_len
) ==
594 zap_leaf_phys(*lp
)->l_hdr
.lh_prefix
);
599 zap_expand_leaf(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
, zap_leaf_t
**lp
)
601 zap_t
*zap
= zn
->zn_zap
;
602 uint64_t hash
= zn
->zn_hash
;
604 int prefix_diff
, i
, err
;
606 int old_prefix_len
= zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
;
608 ASSERT3U(old_prefix_len
, <=, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
609 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
611 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
612 zap_leaf_phys(l
)->l_hdr
.lh_prefix
);
614 if (zap_tryupgradedir(zap
, tx
) == 0 ||
615 old_prefix_len
== zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
) {
616 /* We failed to upgrade, or need to grow the pointer table */
617 objset_t
*os
= zap
->zap_objset
;
618 uint64_t object
= zap
->zap_object
;
622 err
= zap_lockdir(os
, object
, tx
, RW_WRITER
,
623 FALSE
, FALSE
, &zn
->zn_zap
);
627 ASSERT(!zap
->zap_ismicro
);
629 while (old_prefix_len
==
630 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
) {
631 err
= zap_grow_ptrtbl(zap
, tx
);
636 err
= zap_deref_leaf(zap
, hash
, tx
, RW_WRITER
, &l
);
640 if (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
!= old_prefix_len
) {
641 /* it split while our locks were down */
646 ASSERT(RW_WRITE_HELD(&zap
->zap_rwlock
));
647 ASSERT3U(old_prefix_len
, <, zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
648 ASSERT3U(ZAP_HASH_IDX(hash
, old_prefix_len
), ==,
649 zap_leaf_phys(l
)->l_hdr
.lh_prefix
);
651 prefix_diff
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
-
652 (old_prefix_len
+ 1);
653 sibling
= (ZAP_HASH_IDX(hash
, old_prefix_len
+ 1) | 1) << prefix_diff
;
655 /* check for i/o errors before doing zap_leaf_split */
656 for (i
= 0; i
< (1ULL<<prefix_diff
); i
++) {
658 err
= zap_idx_to_blk(zap
, sibling
+i
, &blk
);
661 ASSERT3U(blk
, ==, l
->l_blkid
);
664 nl
= zap_create_leaf(zap
, tx
);
665 zap_leaf_split(l
, nl
, zap
->zap_normflags
!= 0);
667 /* set sibling pointers */
668 for (i
= 0; i
< (1ULL << prefix_diff
); i
++) {
669 err
= zap_set_idx_to_blk(zap
, sibling
+i
, nl
->l_blkid
, tx
);
670 ASSERT0(err
); /* we checked for i/o errors above */
673 if (hash
& (1ULL << (64 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
))) {
674 /* we want the sibling */
686 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t
*zn
, zap_leaf_t
*l
, dmu_tx_t
*tx
)
688 zap_t
*zap
= zn
->zn_zap
;
689 int shift
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
690 int leaffull
= (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
== shift
&&
691 zap_leaf_phys(l
)->l_hdr
.lh_nfree
< ZAP_LEAF_LOW_WATER
);
695 if (leaffull
|| zap_f_phys(zap
)->zap_ptrtbl
.zt_nextblk
) {
699 * We are in the middle of growing the pointer table, or
700 * this leaf will soon make us grow it.
702 if (zap_tryupgradedir(zap
, tx
) == 0) {
703 objset_t
*os
= zap
->zap_objset
;
704 uint64_t zapobj
= zap
->zap_object
;
707 err
= zap_lockdir(os
, zapobj
, tx
,
708 RW_WRITER
, FALSE
, FALSE
, &zn
->zn_zap
);
714 /* could have finished growing while our locks were down */
715 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
== shift
)
716 (void) zap_grow_ptrtbl(zap
, tx
);
721 fzap_checkname(zap_name_t
*zn
)
723 if (zn
->zn_key_orig_numints
* zn
->zn_key_intlen
> ZAP_MAXNAMELEN
)
724 return (SET_ERROR(ENAMETOOLONG
));
729 fzap_checksize(uint64_t integer_size
, uint64_t num_integers
)
731 /* Only integer sizes supported by C */
732 switch (integer_size
) {
739 return (SET_ERROR(EINVAL
));
742 if (integer_size
* num_integers
> ZAP_MAXVALUELEN
)
749 fzap_check(zap_name_t
*zn
, uint64_t integer_size
, uint64_t num_integers
)
753 if ((err
= fzap_checkname(zn
)) != 0)
755 return (fzap_checksize(integer_size
, num_integers
));
759 * Routines for manipulating attributes.
762 fzap_lookup(zap_name_t
*zn
,
763 uint64_t integer_size
, uint64_t num_integers
, void *buf
,
764 char *realname
, int rn_len
, boolean_t
*ncp
)
768 zap_entry_handle_t zeh
;
770 if ((err
= fzap_checkname(zn
)) != 0)
773 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
776 err
= zap_leaf_lookup(l
, zn
, &zeh
);
778 if ((err
= fzap_checksize(integer_size
, num_integers
)) != 0) {
783 err
= zap_entry_read(&zeh
, integer_size
, num_integers
, buf
);
784 (void) zap_entry_read_name(zn
->zn_zap
, &zeh
, rn_len
, realname
);
786 *ncp
= zap_entry_normalization_conflict(&zeh
,
787 zn
, NULL
, zn
->zn_zap
);
796 fzap_add_cd(zap_name_t
*zn
,
797 uint64_t integer_size
, uint64_t num_integers
,
798 const void *val
, uint32_t cd
, dmu_tx_t
*tx
)
802 zap_entry_handle_t zeh
;
803 zap_t
*zap
= zn
->zn_zap
;
805 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
806 ASSERT(!zap
->zap_ismicro
);
807 ASSERT(fzap_check(zn
, integer_size
, num_integers
) == 0);
809 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
813 err
= zap_leaf_lookup(l
, zn
, &zeh
);
815 err
= SET_ERROR(EEXIST
);
821 err
= zap_entry_create(l
, zn
, cd
,
822 integer_size
, num_integers
, val
, &zeh
);
825 zap_increment_num_entries(zap
, 1, tx
);
826 } else if (err
== EAGAIN
) {
827 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
828 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
835 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
840 fzap_add(zap_name_t
*zn
,
841 uint64_t integer_size
, uint64_t num_integers
,
842 const void *val
, dmu_tx_t
*tx
)
844 int err
= fzap_check(zn
, integer_size
, num_integers
);
848 return (fzap_add_cd(zn
, integer_size
, num_integers
,
849 val
, ZAP_NEED_CD
, tx
));
853 fzap_update(zap_name_t
*zn
,
854 int integer_size
, uint64_t num_integers
, const void *val
, dmu_tx_t
*tx
)
858 zap_entry_handle_t zeh
;
859 zap_t
*zap
= zn
->zn_zap
;
861 ASSERT(RW_LOCK_HELD(&zap
->zap_rwlock
));
862 err
= fzap_check(zn
, integer_size
, num_integers
);
866 err
= zap_deref_leaf(zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
870 err
= zap_leaf_lookup(l
, zn
, &zeh
);
871 create
= (err
== ENOENT
);
872 ASSERT(err
== 0 || err
== ENOENT
);
875 err
= zap_entry_create(l
, zn
, ZAP_NEED_CD
,
876 integer_size
, num_integers
, val
, &zeh
);
878 zap_increment_num_entries(zap
, 1, tx
);
880 err
= zap_entry_update(&zeh
, integer_size
, num_integers
, val
);
884 err
= zap_expand_leaf(zn
, l
, tx
, &l
);
885 zap
= zn
->zn_zap
; /* zap_expand_leaf() may change zap */
891 zap_put_leaf_maybe_grow_ptrtbl(zn
, l
, tx
);
896 fzap_length(zap_name_t
*zn
,
897 uint64_t *integer_size
, uint64_t *num_integers
)
901 zap_entry_handle_t zeh
;
903 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
906 err
= zap_leaf_lookup(l
, zn
, &zeh
);
911 *integer_size
= zeh
.zeh_integer_size
;
913 *num_integers
= zeh
.zeh_num_integers
;
920 fzap_remove(zap_name_t
*zn
, dmu_tx_t
*tx
)
924 zap_entry_handle_t zeh
;
926 err
= zap_deref_leaf(zn
->zn_zap
, zn
->zn_hash
, tx
, RW_WRITER
, &l
);
929 err
= zap_leaf_lookup(l
, zn
, &zeh
);
931 zap_entry_remove(&zeh
);
932 zap_increment_num_entries(zn
->zn_zap
, -1, tx
);
939 fzap_prefetch(zap_name_t
*zn
)
942 zap_t
*zap
= zn
->zn_zap
;
945 idx
= ZAP_HASH_IDX(zn
->zn_hash
,
946 zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
);
947 if (zap_idx_to_blk(zap
, idx
, &blk
) != 0)
949 bs
= FZAP_BLOCK_SHIFT(zap
);
950 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
, 0, blk
<< bs
, 1 << bs
,
951 ZIO_PRIORITY_SYNC_READ
);
955 * Helper functions for consumers.
959 zap_create_link(objset_t
*os
, dmu_object_type_t ot
, uint64_t parent_obj
,
960 const char *name
, dmu_tx_t
*tx
)
964 VERIFY((new_obj
= zap_create(os
, ot
, DMU_OT_NONE
, 0, tx
)) > 0);
965 VERIFY0(zap_add(os
, parent_obj
, name
, sizeof (uint64_t), 1, &new_obj
,
972 zap_value_search(objset_t
*os
, uint64_t zapobj
, uint64_t value
, uint64_t mask
,
982 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
983 for (zap_cursor_init(&zc
, os
, zapobj
);
984 (err
= zap_cursor_retrieve(&zc
, za
)) == 0;
985 zap_cursor_advance(&zc
)) {
986 if ((za
->za_first_integer
& mask
) == (value
& mask
)) {
987 (void) strcpy(name
, za
->za_name
);
991 zap_cursor_fini(&zc
);
992 kmem_free(za
, sizeof (zap_attribute_t
));
997 zap_join(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
, dmu_tx_t
*tx
)
1004 for (zap_cursor_init(&zc
, os
, fromobj
);
1005 zap_cursor_retrieve(&zc
, &za
) == 0;
1006 (void) zap_cursor_advance(&zc
)) {
1007 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1) {
1008 err
= SET_ERROR(EINVAL
);
1011 err
= zap_add(os
, intoobj
, za
.za_name
,
1012 8, 1, &za
.za_first_integer
, tx
);
1016 zap_cursor_fini(&zc
);
1021 zap_join_key(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1022 uint64_t value
, dmu_tx_t
*tx
)
1029 for (zap_cursor_init(&zc
, os
, fromobj
);
1030 zap_cursor_retrieve(&zc
, &za
) == 0;
1031 (void) zap_cursor_advance(&zc
)) {
1032 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1) {
1033 err
= SET_ERROR(EINVAL
);
1036 err
= zap_add(os
, intoobj
, za
.za_name
,
1041 zap_cursor_fini(&zc
);
1046 zap_join_increment(objset_t
*os
, uint64_t fromobj
, uint64_t intoobj
,
1054 for (zap_cursor_init(&zc
, os
, fromobj
);
1055 zap_cursor_retrieve(&zc
, &za
) == 0;
1056 (void) zap_cursor_advance(&zc
)) {
1059 if (za
.za_integer_length
!= 8 || za
.za_num_integers
!= 1) {
1060 err
= SET_ERROR(EINVAL
);
1064 err
= zap_lookup(os
, intoobj
, za
.za_name
, 8, 1, &delta
);
1065 if (err
!= 0 && err
!= ENOENT
)
1067 delta
+= za
.za_first_integer
;
1068 err
= zap_update(os
, intoobj
, za
.za_name
, 8, 1, &delta
, tx
);
1072 zap_cursor_fini(&zc
);
1077 zap_add_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1081 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1082 return (zap_add(os
, obj
, name
, 8, 1, &value
, tx
));
1086 zap_remove_int(objset_t
*os
, uint64_t obj
, uint64_t value
, dmu_tx_t
*tx
)
1090 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1091 return (zap_remove(os
, obj
, name
, tx
));
1095 zap_lookup_int(objset_t
*os
, uint64_t obj
, uint64_t value
)
1099 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)value
);
1100 return (zap_lookup(os
, obj
, name
, 8, 1, &value
));
1104 zap_add_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_add(os
, obj
, name
, 8, 1, &value
, tx
));
1114 zap_update_int_key(objset_t
*os
, uint64_t obj
,
1115 uint64_t key
, uint64_t value
, dmu_tx_t
*tx
)
1119 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1120 return (zap_update(os
, obj
, name
, 8, 1, &value
, tx
));
1124 zap_lookup_int_key(objset_t
*os
, uint64_t obj
, uint64_t key
, uint64_t *valuep
)
1128 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1129 return (zap_lookup(os
, obj
, name
, 8, 1, valuep
));
1133 zap_increment(objset_t
*os
, uint64_t obj
, const char *name
, int64_t delta
,
1142 err
= zap_lookup(os
, obj
, name
, 8, 1, &value
);
1143 if (err
!= 0 && err
!= ENOENT
)
1147 err
= zap_remove(os
, obj
, name
, tx
);
1149 err
= zap_update(os
, obj
, name
, 8, 1, &value
, tx
);
1154 zap_increment_int(objset_t
*os
, uint64_t obj
, uint64_t key
, int64_t delta
,
1159 (void) snprintf(name
, sizeof (name
), "%llx", (longlong_t
)key
);
1160 return (zap_increment(os
, obj
, name
, delta
, tx
));
1164 * Routines for iterating over the attributes.
1168 fzap_cursor_retrieve(zap_t
*zap
, zap_cursor_t
*zc
, zap_attribute_t
*za
)
1171 zap_entry_handle_t zeh
;
1174 /* retrieve the next entry at or after zc_hash/zc_cd */
1175 /* if no entry, return ENOENT */
1178 (ZAP_HASH_IDX(zc
->zc_hash
,
1179 zap_leaf_phys(zc
->zc_leaf
)->l_hdr
.lh_prefix_len
) !=
1180 zap_leaf_phys(zc
->zc_leaf
)->l_hdr
.lh_prefix
)) {
1181 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1182 zap_put_leaf(zc
->zc_leaf
);
1187 if (zc
->zc_leaf
== NULL
) {
1188 err
= zap_deref_leaf(zap
, zc
->zc_hash
, NULL
, RW_READER
,
1193 rw_enter(&zc
->zc_leaf
->l_rwlock
, RW_READER
);
1197 err
= zap_leaf_lookup_closest(l
, zc
->zc_hash
, zc
->zc_cd
, &zeh
);
1199 if (err
== ENOENT
) {
1201 (1ULL << (64 - zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
)) - 1;
1202 zc
->zc_hash
= (zc
->zc_hash
& ~nocare
) + nocare
+ 1;
1204 if (zap_leaf_phys(l
)->l_hdr
.lh_prefix_len
== 0 ||
1206 zc
->zc_hash
= -1ULL;
1208 zap_put_leaf(zc
->zc_leaf
);
1215 zc
->zc_hash
= zeh
.zeh_hash
;
1216 zc
->zc_cd
= zeh
.zeh_cd
;
1217 za
->za_integer_length
= zeh
.zeh_integer_size
;
1218 za
->za_num_integers
= zeh
.zeh_num_integers
;
1219 if (zeh
.zeh_num_integers
== 0) {
1220 za
->za_first_integer
= 0;
1222 err
= zap_entry_read(&zeh
, 8, 1, &za
->za_first_integer
);
1223 ASSERT(err
== 0 || err
== EOVERFLOW
);
1225 err
= zap_entry_read_name(zap
, &zeh
,
1226 sizeof (za
->za_name
), za
->za_name
);
1229 za
->za_normalization_conflict
=
1230 zap_entry_normalization_conflict(&zeh
,
1231 NULL
, za
->za_name
, zap
);
1233 rw_exit(&zc
->zc_leaf
->l_rwlock
);
1238 zap_stats_ptrtbl(zap_t
*zap
, uint64_t *tbl
, int len
, zap_stats_t
*zs
)
1241 uint64_t lastblk
= 0;
1244 * NB: if a leaf has more pointers than an entire ptrtbl block
1245 * can hold, then it'll be accounted for more than once, since
1246 * we won't have lastblk.
1248 for (i
= 0; i
< len
; i
++) {
1251 if (tbl
[i
] == lastblk
)
1255 err
= zap_get_leaf_byblk(zap
, tbl
[i
], NULL
, RW_READER
, &l
);
1257 zap_leaf_stats(zap
, l
, zs
);
1264 fzap_get_stats(zap_t
*zap
, zap_stats_t
*zs
)
1266 int bs
= FZAP_BLOCK_SHIFT(zap
);
1267 zs
->zs_blocksize
= 1ULL << bs
;
1270 * Set zap_phys_t fields
1272 zs
->zs_num_leafs
= zap_f_phys(zap
)->zap_num_leafs
;
1273 zs
->zs_num_entries
= zap_f_phys(zap
)->zap_num_entries
;
1274 zs
->zs_num_blocks
= zap_f_phys(zap
)->zap_freeblk
;
1275 zs
->zs_block_type
= zap_f_phys(zap
)->zap_block_type
;
1276 zs
->zs_magic
= zap_f_phys(zap
)->zap_magic
;
1277 zs
->zs_salt
= zap_f_phys(zap
)->zap_salt
;
1280 * Set zap_ptrtbl fields
1282 zs
->zs_ptrtbl_len
= 1ULL << zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
1283 zs
->zs_ptrtbl_nextblk
= zap_f_phys(zap
)->zap_ptrtbl
.zt_nextblk
;
1284 zs
->zs_ptrtbl_blks_copied
=
1285 zap_f_phys(zap
)->zap_ptrtbl
.zt_blks_copied
;
1286 zs
->zs_ptrtbl_zt_blk
= zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
;
1287 zs
->zs_ptrtbl_zt_numblks
= zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
;
1288 zs
->zs_ptrtbl_zt_shift
= zap_f_phys(zap
)->zap_ptrtbl
.zt_shift
;
1290 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
== 0) {
1291 /* the ptrtbl is entirely in the header block. */
1292 zap_stats_ptrtbl(zap
, &ZAP_EMBEDDED_PTRTBL_ENT(zap
, 0),
1293 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap
), zs
);
1297 dmu_prefetch(zap
->zap_objset
, zap
->zap_object
, 0,
1298 zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
<< bs
,
1299 zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
<< bs
,
1300 ZIO_PRIORITY_SYNC_READ
);
1302 for (b
= 0; b
< zap_f_phys(zap
)->zap_ptrtbl
.zt_numblks
;
1307 err
= dmu_buf_hold(zap
->zap_objset
, zap
->zap_object
,
1308 (zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
+ b
) << bs
,
1309 FTAG
, &db
, DMU_READ_NO_PREFETCH
);
1311 zap_stats_ptrtbl(zap
, db
->db_data
,
1313 dmu_buf_rele(db
, FTAG
);
1320 fzap_count_write(zap_name_t
*zn
, int add
, refcount_t
*towrite
,
1321 refcount_t
*tooverwrite
)
1323 zap_t
*zap
= zn
->zn_zap
;
1328 * Account for the header block of the fatzap.
1330 if (!add
&& dmu_buf_freeable(zap
->zap_dbuf
)) {
1331 (void) refcount_add_many(tooverwrite
,
1332 zap
->zap_dbuf
->db_size
, FTAG
);
1334 (void) refcount_add_many(towrite
,
1335 zap
->zap_dbuf
->db_size
, FTAG
);
1339 * Account for the pointer table blocks.
1340 * If we are adding we need to account for the following cases :
1341 * - If the pointer table is embedded, this operation could force an
1342 * external pointer table.
1343 * - If this already has an external pointer table this operation
1344 * could extend the table.
1347 if (zap_f_phys(zap
)->zap_ptrtbl
.zt_blk
== 0) {
1348 (void) refcount_add_many(towrite
,
1349 zap
->zap_dbuf
->db_size
, FTAG
);
1351 (void) refcount_add_many(towrite
,
1352 zap
->zap_dbuf
->db_size
* 3, FTAG
);
1357 * Now, check if the block containing leaf is freeable
1358 * and account accordingly.
1360 err
= zap_deref_leaf(zap
, zn
->zn_hash
, NULL
, RW_READER
, &l
);
1365 if (!add
&& dmu_buf_freeable(l
->l_dbuf
)) {
1366 (void) refcount_add_many(tooverwrite
, l
->l_dbuf
->db_size
, FTAG
);
1369 * If this an add operation, the leaf block could split.
1370 * Hence, we need to account for an additional leaf block.
1372 (void) refcount_add_many(towrite
,
1373 (add
? 2 : 1) * l
->l_dbuf
->db_size
, FTAG
);