]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zap.c
OpenZFS 7004 - dmu_tx_hold_zap() does dnode_hold() 7x on same object
[mirror_zfs.git] / module / zfs / zap.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
8bea9815 23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
0c66c32d 24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27/*
28 * This file contains the top half of the zfs directory structure
29 * implementation. The bottom half is in zap_leaf.c.
30 *
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.
35 *
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
40 */
41
42#include <sys/spa.h>
43#include <sys/dmu.h>
44#include <sys/zfs_context.h>
45#include <sys/zfs_znode.h>
9babb374 46#include <sys/fs/zfs.h>
34dc7c2f
BB
47#include <sys/zap.h>
48#include <sys/refcount.h>
49#include <sys/zap_impl.h>
50#include <sys/zap_leaf.h>
51
52int fzap_default_block_shift = 14; /* 16k blocksize */
53
d683ddbb
JG
54extern inline zap_phys_t *zap_f_phys(zap_t *zap);
55
34dc7c2f
BB
56static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
57
34dc7c2f
BB
58void
59fzap_byteswap(void *vbuf, size_t size)
60{
61 uint64_t block_type;
62
63 block_type = *(uint64_t *)vbuf;
64
65 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
66 zap_leaf_byteswap(vbuf, size);
67 else {
68 /* it's a ptrtbl block */
69 byteswap_uint64_array(vbuf, size);
70 }
71}
72
73void
428870ff 74fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
34dc7c2f
BB
75{
76 dmu_buf_t *db;
77 zap_leaf_t *l;
78 int i;
79 zap_phys_t *zp;
80
81 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
82 zap->zap_ismicro = FALSE;
83
0c66c32d 84 zap->zap_dbu.dbu_evict_func = zap_evict;
34dc7c2f
BB
85
86 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
9bd274dd 87 zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
34dc7c2f 88
d683ddbb 89 zp = zap_f_phys(zap);
34dc7c2f
BB
90 /*
91 * explicitly zero it since it might be coming from an
92 * initialized microzap
93 */
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;
97
98 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
99
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;
428870ff 105 zp->zap_flags = flags;
34dc7c2f
BB
106
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;
110
111 /*
112 * set up block 1 - the first leaf
113 */
114 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff 115 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
34dc7c2f
BB
116 dmu_buf_will_dirty(db, tx);
117
79c76d5b 118 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
34dc7c2f 119 l->l_dbuf = db;
34dc7c2f
BB
120
121 zap_leaf_init(l, zp->zap_normflags != 0);
122
123 kmem_free(l, sizeof (zap_leaf_t));
124 dmu_buf_rele(db, FTAG);
125}
126
127static int
128zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
129{
130 if (RW_WRITE_HELD(&zap->zap_rwlock))
131 return (1);
132 if (rw_tryupgrade(&zap->zap_rwlock)) {
133 dmu_buf_will_dirty(zap->zap_dbuf, tx);
134 return (1);
135 }
136 return (0);
137}
138
139/*
140 * Generic routines for dealing with the pointer & cookie tables.
141 */
142
143static int
144zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
145 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
146 dmu_tx_t *tx)
147{
148 uint64_t b, newblk;
149 dmu_buf_t *db_old, *db_new;
150 int err;
151 int bs = FZAP_BLOCK_SHIFT(zap);
152 int hepb = 1<<(bs-4);
153 /* hepb = half the number of entries in a block */
154
155 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
156 ASSERT(tbl->zt_blk != 0);
157 ASSERT(tbl->zt_numblks > 0);
158
159 if (tbl->zt_nextblk != 0) {
160 newblk = tbl->zt_nextblk;
161 } else {
162 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
163 tbl->zt_nextblk = newblk;
c99c9001 164 ASSERT0(tbl->zt_blks_copied);
fcff0f35
PD
165 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
166 tbl->zt_blk << bs, tbl->zt_numblks << bs,
167 ZIO_PRIORITY_SYNC_READ);
34dc7c2f
BB
168 }
169
170 /*
171 * Copy the ptrtbl from the old to new location.
172 */
173
174 b = tbl->zt_blks_copied;
175 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff 176 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
34dc7c2f
BB
177 if (err)
178 return (err);
179
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,
428870ff 182 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
34dc7c2f
BB
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);
186
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,
428870ff 189 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
34dc7c2f
BB
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);
194
195 dmu_buf_rele(db_old, FTAG);
196
197 tbl->zt_blks_copied++;
198
199 dprintf("copied block %llu of %llu\n",
200 tbl->zt_blks_copied, tbl->zt_numblks);
201
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);
205
206 tbl->zt_blk = newblk;
207 tbl->zt_numblks *= 2;
208 tbl->zt_shift++;
209 tbl->zt_nextblk = 0;
210 tbl->zt_blks_copied = 0;
211
29e57d15 212 dprintf("finished; numblocks now %llu (%uk entries)\n",
34dc7c2f
BB
213 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
214 }
215
216 return (0);
217}
218
219static int
220zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
221 dmu_tx_t *tx)
222{
223 int err;
224 uint64_t blk, off;
225 int bs = FZAP_BLOCK_SHIFT(zap);
226 dmu_buf_t *db;
227
228 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
229 ASSERT(tbl->zt_blk != 0);
230
231 dprintf("storing %llx at index %llx\n", val, idx);
232
233 blk = idx >> (bs-3);
234 off = idx & ((1<<(bs-3))-1);
235
236 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff 237 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
34dc7c2f
BB
238 if (err)
239 return (err);
240 dmu_buf_will_dirty(db, tx);
241
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);
246 dmu_buf_t *db2;
247
248 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff
BB
249 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
250 DMU_READ_NO_PREFETCH);
34dc7c2f
BB
251 if (err) {
252 dmu_buf_rele(db, FTAG);
253 return (err);
254 }
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);
259 }
260
261 ((uint64_t *)db->db_data)[off] = val;
262 dmu_buf_rele(db, FTAG);
263
264 return (0);
265}
266
267static int
268zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
269{
270 uint64_t blk, off;
271 int err;
272 dmu_buf_t *db;
2bce8049 273 dnode_t *dn;
34dc7c2f
BB
274 int bs = FZAP_BLOCK_SHIFT(zap);
275
276 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
277
278 blk = idx >> (bs-3);
279 off = idx & ((1<<(bs-3))-1);
280
2bce8049
MA
281 /*
282 * Note: this is equivalent to dmu_buf_hold(), but we use
283 * _dnode_enter / _by_dnode because it's faster because we don't
284 * have to hold the dnode.
285 */
286 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
287 err = dmu_buf_hold_by_dnode(dn,
428870ff 288 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
2bce8049 289 dmu_buf_dnode_exit(zap->zap_dbuf);
34dc7c2f
BB
290 if (err)
291 return (err);
292 *valp = ((uint64_t *)db->db_data)[off];
293 dmu_buf_rele(db, FTAG);
294
295 if (tbl->zt_nextblk != 0) {
296 /*
297 * read the nextblk for the sake of i/o error checking,
298 * so that zap_table_load() will catch errors for
299 * zap_table_store.
300 */
301 blk = (idx*2) >> (bs-3);
302
2bce8049
MA
303 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
304 err = dmu_buf_hold_by_dnode(dn,
428870ff
BB
305 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
306 DMU_READ_NO_PREFETCH);
2bce8049 307 dmu_buf_dnode_exit(zap->zap_dbuf);
3a84951d
WA
308 if (err == 0)
309 dmu_buf_rele(db, FTAG);
34dc7c2f
BB
310 }
311 return (err);
312}
313
314/*
315 * Routines for growing the ptrtbl.
316 */
317
318static void
319zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
320{
321 int i;
322 for (i = 0; i < n; i++) {
323 uint64_t lb = src[i];
324 dst[2*i+0] = lb;
325 dst[2*i+1] = lb;
326 }
327}
328
329static int
330zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
331{
428870ff
BB
332 /*
333 * The pointer table should never use more hash bits than we
334 * have (otherwise we'd be using useless zero bits to index it).
335 * If we are within 2 bits of running out, stop growing, since
336 * this is already an aberrant condition.
337 */
d683ddbb 338 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
2e528b49 339 return (SET_ERROR(ENOSPC));
34dc7c2f 340
d683ddbb 341 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
34dc7c2f
BB
342 /*
343 * We are outgrowing the "embedded" ptrtbl (the one
344 * stored in the header block). Give it its own entire
345 * block, which will double the size of the ptrtbl.
346 */
347 uint64_t newblk;
348 dmu_buf_t *db_new;
349 int err;
350
d683ddbb 351 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
34dc7c2f 352 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
d683ddbb 353 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
34dc7c2f
BB
354
355 newblk = zap_allocate_blocks(zap, 1);
356 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff
BB
357 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
358 DMU_READ_NO_PREFETCH);
34dc7c2f
BB
359 if (err)
360 return (err);
361 dmu_buf_will_dirty(db_new, tx);
362 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
363 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
364 dmu_buf_rele(db_new, FTAG);
365
d683ddbb
JG
366 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
367 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
368 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
34dc7c2f 369
d683ddbb
JG
370 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
371 zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
34dc7c2f
BB
372 (FZAP_BLOCK_SHIFT(zap)-3));
373
374 return (0);
375 } else {
d683ddbb 376 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
34dc7c2f
BB
377 zap_ptrtbl_transfer, tx));
378 }
379}
380
381static void
382zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
383{
384 dmu_buf_will_dirty(zap->zap_dbuf, tx);
385 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
d683ddbb
JG
386 ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
387 zap_f_phys(zap)->zap_num_entries += delta;
34dc7c2f
BB
388 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
389}
390
391static uint64_t
392zap_allocate_blocks(zap_t *zap, int nblocks)
393{
394 uint64_t newblk;
395 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
d683ddbb
JG
396 newblk = zap_f_phys(zap)->zap_freeblk;
397 zap_f_phys(zap)->zap_freeblk += nblocks;
34dc7c2f
BB
398 return (newblk);
399}
400
0c66c32d
JG
401static void
402zap_leaf_pageout(void *dbu)
403{
404 zap_leaf_t *l = dbu;
405
406 rw_destroy(&l->l_rwlock);
407 kmem_free(l, sizeof (zap_leaf_t));
408}
409
34dc7c2f
BB
410static zap_leaf_t *
411zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
412{
413 void *winner;
0c66c32d 414 zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
34dc7c2f
BB
415
416 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
417
448d7aaa 418 rw_init(&l->l_rwlock, NULL, RW_NOLOCKDEP, NULL);
34dc7c2f
BB
419 rw_enter(&l->l_rwlock, RW_WRITER);
420 l->l_blkid = zap_allocate_blocks(zap, 1);
421 l->l_dbuf = NULL;
34dc7c2f
BB
422
423 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
428870ff
BB
424 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
425 DMU_READ_NO_PREFETCH));
0c66c32d
JG
426 dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
427 winner = dmu_buf_set_user(l->l_dbuf, &l->l_dbu);
34dc7c2f
BB
428 ASSERT(winner == NULL);
429 dmu_buf_will_dirty(l->l_dbuf, tx);
430
431 zap_leaf_init(l, zap->zap_normflags != 0);
432
d683ddbb 433 zap_f_phys(zap)->zap_num_leafs++;
34dc7c2f
BB
434
435 return (l);
436}
437
438int
439fzap_count(zap_t *zap, uint64_t *count)
440{
441 ASSERT(!zap->zap_ismicro);
442 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
d683ddbb 443 *count = zap_f_phys(zap)->zap_num_entries;
34dc7c2f
BB
444 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
445 return (0);
446}
447
448/*
449 * Routines for obtaining zap_leaf_t's
450 */
451
452void
453zap_put_leaf(zap_leaf_t *l)
454{
455 rw_exit(&l->l_rwlock);
456 dmu_buf_rele(l->l_dbuf, NULL);
457}
458
34dc7c2f
BB
459static zap_leaf_t *
460zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
461{
462 zap_leaf_t *l, *winner;
463
464 ASSERT(blkid != 0);
465
0c66c32d 466 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
ef5319df 467 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, NULL);
34dc7c2f
BB
468 rw_enter(&l->l_rwlock, RW_WRITER);
469 l->l_blkid = blkid;
9bd274dd 470 l->l_bs = highbit64(db->db_size) - 1;
34dc7c2f 471 l->l_dbuf = db;
34dc7c2f 472
0c66c32d
JG
473 dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
474 winner = dmu_buf_set_user(db, &l->l_dbu);
34dc7c2f
BB
475
476 rw_exit(&l->l_rwlock);
477 if (winner != NULL) {
478 /* someone else set it first */
0c66c32d 479 zap_leaf_pageout(&l->l_dbu);
34dc7c2f
BB
480 l = winner;
481 }
482
483 /*
484 * lhr_pad was previously used for the next leaf in the leaf
485 * chain. There should be no chained leafs (as we have removed
486 * support for them).
487 */
d683ddbb 488 ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
34dc7c2f
BB
489
490 /*
491 * There should be more hash entries than there can be
492 * chunks to put in the hash table
493 */
494 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
495
496 /* The chunks should begin at the end of the hash table */
b8864a23 497 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==, (zap_leaf_chunk_t *)
d683ddbb 498 &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
34dc7c2f
BB
499
500 /* The chunks should end at the end of the block */
501 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
d683ddbb 502 (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
34dc7c2f
BB
503
504 return (l);
505}
506
507static int
508zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
509 zap_leaf_t **lp)
510{
511 dmu_buf_t *db;
512 zap_leaf_t *l;
513 int bs = FZAP_BLOCK_SHIFT(zap);
514 int err;
2bce8049 515 dnode_t *dn;
34dc7c2f
BB
516
517 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
518
29572ccd
CC
519 /*
520 * If system crashed just after dmu_free_long_range in zfs_rmnode, we
521 * would be left with an empty xattr dir in delete queue. blkid=0
522 * would be passed in when doing zfs_purgedir. If that's the case we
523 * should just return immediately. The underlying objects should
524 * already be freed, so this should be perfectly fine.
525 */
526 if (blkid == 0)
527 return (ENOENT);
528
2bce8049
MA
529 dn = dmu_buf_dnode_enter(zap->zap_dbuf);
530 err = dmu_buf_hold_by_dnode(dn,
428870ff 531 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
2bce8049 532 dmu_buf_dnode_exit(zap->zap_dbuf);
34dc7c2f
BB
533 if (err)
534 return (err);
535
536 ASSERT3U(db->db_object, ==, zap->zap_object);
537 ASSERT3U(db->db_offset, ==, blkid << bs);
538 ASSERT3U(db->db_size, ==, 1 << bs);
539 ASSERT(blkid != 0);
540
541 l = dmu_buf_get_user(db);
542
543 if (l == NULL)
544 l = zap_open_leaf(blkid, db);
545
546 rw_enter(&l->l_rwlock, lt);
547 /*
d683ddbb 548 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
34dc7c2f
BB
549 * causing ASSERT below to fail.
550 */
551 if (lt == RW_WRITER)
552 dmu_buf_will_dirty(db, tx);
553 ASSERT3U(l->l_blkid, ==, blkid);
554 ASSERT3P(l->l_dbuf, ==, db);
d683ddbb
JG
555 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
556 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
34dc7c2f
BB
557
558 *lp = l;
559 return (0);
560}
561
562static int
563zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
564{
565 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
566
d683ddbb 567 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
34dc7c2f 568 ASSERT3U(idx, <,
d683ddbb 569 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
34dc7c2f
BB
570 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
571 return (0);
572 } else {
d683ddbb 573 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
34dc7c2f
BB
574 idx, valp));
575 }
576}
577
578static int
579zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
580{
581 ASSERT(tx != NULL);
582 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
583
d683ddbb 584 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
34dc7c2f
BB
585 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
586 return (0);
587 } else {
d683ddbb 588 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
34dc7c2f
BB
589 idx, blk, tx));
590 }
591}
592
593static int
594zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
595{
596 uint64_t idx, blk;
597 int err;
598
599 ASSERT(zap->zap_dbuf == NULL ||
d683ddbb 600 zap_f_phys(zap) == zap->zap_dbuf->db_data);
32c8c946
CC
601
602 /* Reality check for corrupt zap objects (leaf or header). */
603 if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
604 zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
605 zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
606 return (SET_ERROR(EIO));
607 }
d683ddbb 608 idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
34dc7c2f
BB
609 err = zap_idx_to_blk(zap, idx, &blk);
610 if (err != 0)
611 return (err);
612 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
613
d683ddbb
JG
614 ASSERT(err ||
615 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
616 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
34dc7c2f
BB
617 return (err);
618}
619
620static int
8bea9815
MA
621zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
622 void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
34dc7c2f
BB
623{
624 zap_t *zap = zn->zn_zap;
625 uint64_t hash = zn->zn_hash;
626 zap_leaf_t *nl;
627 int prefix_diff, i, err;
628 uint64_t sibling;
d683ddbb 629 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
34dc7c2f 630
d683ddbb 631 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
34dc7c2f
BB
632 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
633
634 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
d683ddbb 635 zap_leaf_phys(l)->l_hdr.lh_prefix);
34dc7c2f
BB
636
637 if (zap_tryupgradedir(zap, tx) == 0 ||
d683ddbb 638 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
34dc7c2f
BB
639 /* We failed to upgrade, or need to grow the pointer table */
640 objset_t *os = zap->zap_objset;
641 uint64_t object = zap->zap_object;
642
643 zap_put_leaf(l);
8bea9815 644 zap_unlockdir(zap, tag);
34dc7c2f 645 err = zap_lockdir(os, object, tx, RW_WRITER,
8bea9815 646 FALSE, FALSE, tag, &zn->zn_zap);
34dc7c2f
BB
647 zap = zn->zn_zap;
648 if (err)
649 return (err);
650 ASSERT(!zap->zap_ismicro);
651
652 while (old_prefix_len ==
d683ddbb 653 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
34dc7c2f
BB
654 err = zap_grow_ptrtbl(zap, tx);
655 if (err)
656 return (err);
657 }
658
659 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
660 if (err)
661 return (err);
662
d683ddbb 663 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
34dc7c2f
BB
664 /* it split while our locks were down */
665 *lp = l;
666 return (0);
667 }
668 }
669 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
d683ddbb 670 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
34dc7c2f 671 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
d683ddbb 672 zap_leaf_phys(l)->l_hdr.lh_prefix);
34dc7c2f 673
d683ddbb 674 prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
34dc7c2f
BB
675 (old_prefix_len + 1);
676 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
677
678 /* check for i/o errors before doing zap_leaf_split */
679 for (i = 0; i < (1ULL<<prefix_diff); i++) {
680 uint64_t blk;
681 err = zap_idx_to_blk(zap, sibling+i, &blk);
682 if (err)
683 return (err);
684 ASSERT3U(blk, ==, l->l_blkid);
685 }
686
687 nl = zap_create_leaf(zap, tx);
688 zap_leaf_split(l, nl, zap->zap_normflags != 0);
689
690 /* set sibling pointers */
c99c9001 691 for (i = 0; i < (1ULL << prefix_diff); i++) {
34dc7c2f 692 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
c99c9001 693 ASSERT0(err); /* we checked for i/o errors above */
34dc7c2f
BB
694 }
695
d683ddbb 696 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
34dc7c2f
BB
697 /* we want the sibling */
698 zap_put_leaf(l);
699 *lp = nl;
700 } else {
701 zap_put_leaf(nl);
702 *lp = l;
703 }
704
705 return (0);
706}
707
708static void
8bea9815
MA
709zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
710 void *tag, dmu_tx_t *tx)
34dc7c2f
BB
711{
712 zap_t *zap = zn->zn_zap;
d683ddbb
JG
713 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
714 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
715 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
34dc7c2f
BB
716
717 zap_put_leaf(l);
718
d683ddbb 719 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
34dc7c2f
BB
720 int err;
721
722 /*
723 * We are in the middle of growing the pointer table, or
724 * this leaf will soon make us grow it.
725 */
726 if (zap_tryupgradedir(zap, tx) == 0) {
727 objset_t *os = zap->zap_objset;
728 uint64_t zapobj = zap->zap_object;
729
8bea9815 730 zap_unlockdir(zap, tag);
34dc7c2f 731 err = zap_lockdir(os, zapobj, tx,
8bea9815 732 RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
34dc7c2f
BB
733 zap = zn->zn_zap;
734 if (err)
735 return;
736 }
737
738 /* could have finished growing while our locks were down */
d683ddbb 739 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
34dc7c2f
BB
740 (void) zap_grow_ptrtbl(zap, tx);
741 }
742}
743
34dc7c2f 744static int
428870ff 745fzap_checkname(zap_name_t *zn)
34dc7c2f 746{
428870ff 747 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
2e528b49 748 return (SET_ERROR(ENAMETOOLONG));
428870ff
BB
749 return (0);
750}
34dc7c2f 751
428870ff
BB
752static int
753fzap_checksize(uint64_t integer_size, uint64_t num_integers)
754{
34dc7c2f
BB
755 /* Only integer sizes supported by C */
756 switch (integer_size) {
757 case 1:
758 case 2:
759 case 4:
760 case 8:
761 break;
762 default:
2e528b49 763 return (SET_ERROR(EINVAL));
34dc7c2f
BB
764 }
765
766 if (integer_size * num_integers > ZAP_MAXVALUELEN)
767 return (E2BIG);
768
769 return (0);
770}
771
428870ff
BB
772static int
773fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
774{
775 int err;
776
777 if ((err = fzap_checkname(zn)) != 0)
778 return (err);
779 return (fzap_checksize(integer_size, num_integers));
780}
781
34dc7c2f
BB
782/*
783 * Routines for manipulating attributes.
784 */
785int
786fzap_lookup(zap_name_t *zn,
787 uint64_t integer_size, uint64_t num_integers, void *buf,
788 char *realname, int rn_len, boolean_t *ncp)
789{
790 zap_leaf_t *l;
791 int err;
792 zap_entry_handle_t zeh;
793
428870ff 794 if ((err = fzap_checkname(zn)) != 0)
34dc7c2f
BB
795 return (err);
796
797 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
798 if (err != 0)
799 return (err);
800 err = zap_leaf_lookup(l, zn, &zeh);
801 if (err == 0) {
428870ff
BB
802 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
803 zap_put_leaf(l);
804 return (err);
805 }
806
34dc7c2f 807 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
428870ff 808 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
34dc7c2f
BB
809 if (ncp) {
810 *ncp = zap_entry_normalization_conflict(&zeh,
811 zn, NULL, zn->zn_zap);
812 }
813 }
814
815 zap_put_leaf(l);
816 return (err);
817}
818
819int
820fzap_add_cd(zap_name_t *zn,
821 uint64_t integer_size, uint64_t num_integers,
8bea9815 822 const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
34dc7c2f
BB
823{
824 zap_leaf_t *l;
825 int err;
826 zap_entry_handle_t zeh;
827 zap_t *zap = zn->zn_zap;
828
829 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
830 ASSERT(!zap->zap_ismicro);
428870ff 831 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
34dc7c2f
BB
832
833 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
834 if (err != 0)
835 return (err);
836retry:
837 err = zap_leaf_lookup(l, zn, &zeh);
838 if (err == 0) {
2e528b49 839 err = SET_ERROR(EEXIST);
34dc7c2f
BB
840 goto out;
841 }
842 if (err != ENOENT)
843 goto out;
844
428870ff 845 err = zap_entry_create(l, zn, cd,
34dc7c2f
BB
846 integer_size, num_integers, val, &zeh);
847
848 if (err == 0) {
849 zap_increment_num_entries(zap, 1, tx);
850 } else if (err == EAGAIN) {
8bea9815 851 err = zap_expand_leaf(zn, l, tag, tx, &l);
34dc7c2f
BB
852 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
853 if (err == 0)
854 goto retry;
855 }
856
857out:
858 if (zap != NULL)
8bea9815 859 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
34dc7c2f
BB
860 return (err);
861}
862
863int
864fzap_add(zap_name_t *zn,
865 uint64_t integer_size, uint64_t num_integers,
8bea9815 866 const void *val, void *tag, dmu_tx_t *tx)
34dc7c2f 867{
428870ff 868 int err = fzap_check(zn, integer_size, num_integers);
34dc7c2f
BB
869 if (err != 0)
870 return (err);
871
872 return (fzap_add_cd(zn, integer_size, num_integers,
8bea9815 873 val, ZAP_NEED_CD, tag, tx));
34dc7c2f
BB
874}
875
876int
877fzap_update(zap_name_t *zn,
8bea9815
MA
878 int integer_size, uint64_t num_integers, const void *val,
879 void *tag, dmu_tx_t *tx)
34dc7c2f
BB
880{
881 zap_leaf_t *l;
882 int err, create;
883 zap_entry_handle_t zeh;
884 zap_t *zap = zn->zn_zap;
885
886 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
428870ff 887 err = fzap_check(zn, integer_size, num_integers);
34dc7c2f
BB
888 if (err != 0)
889 return (err);
890
891 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
892 if (err != 0)
893 return (err);
894retry:
895 err = zap_leaf_lookup(l, zn, &zeh);
896 create = (err == ENOENT);
897 ASSERT(err == 0 || err == ENOENT);
898
899 if (create) {
428870ff
BB
900 err = zap_entry_create(l, zn, ZAP_NEED_CD,
901 integer_size, num_integers, val, &zeh);
34dc7c2f
BB
902 if (err == 0)
903 zap_increment_num_entries(zap, 1, tx);
904 } else {
905 err = zap_entry_update(&zeh, integer_size, num_integers, val);
906 }
907
908 if (err == EAGAIN) {
8bea9815 909 err = zap_expand_leaf(zn, l, tag, tx, &l);
34dc7c2f
BB
910 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
911 if (err == 0)
912 goto retry;
913 }
914
915 if (zap != NULL)
8bea9815 916 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
34dc7c2f
BB
917 return (err);
918}
919
920int
921fzap_length(zap_name_t *zn,
922 uint64_t *integer_size, uint64_t *num_integers)
923{
924 zap_leaf_t *l;
925 int err;
926 zap_entry_handle_t zeh;
927
928 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
929 if (err != 0)
930 return (err);
931 err = zap_leaf_lookup(l, zn, &zeh);
932 if (err != 0)
933 goto out;
934
935 if (integer_size)
936 *integer_size = zeh.zeh_integer_size;
937 if (num_integers)
938 *num_integers = zeh.zeh_num_integers;
939out:
940 zap_put_leaf(l);
941 return (err);
942}
943
944int
945fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
946{
947 zap_leaf_t *l;
948 int err;
949 zap_entry_handle_t zeh;
950
951 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
952 if (err != 0)
953 return (err);
954 err = zap_leaf_lookup(l, zn, &zeh);
955 if (err == 0) {
956 zap_entry_remove(&zeh);
957 zap_increment_num_entries(zn->zn_zap, -1, tx);
958 }
959 zap_put_leaf(l);
960 return (err);
961}
962
428870ff
BB
963void
964fzap_prefetch(zap_name_t *zn)
965{
966 uint64_t idx, blk;
967 zap_t *zap = zn->zn_zap;
968 int bs;
969
970 idx = ZAP_HASH_IDX(zn->zn_hash,
d683ddbb 971 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
428870ff
BB
972 if (zap_idx_to_blk(zap, idx, &blk) != 0)
973 return;
974 bs = FZAP_BLOCK_SHIFT(zap);
fcff0f35
PD
975 dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
976 ZIO_PRIORITY_SYNC_READ);
428870ff
BB
977}
978
b128c09f
BB
979/*
980 * Helper functions for consumers.
981 */
982
9ae529ec
CS
983uint64_t
984zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
985 const char *name, dmu_tx_t *tx)
50c957f7
NB
986{
987 return (zap_create_link_dnsize(os, ot, parent_obj, name, 0, tx));
988}
989
990uint64_t
991zap_create_link_dnsize(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
992 const char *name, int dnodesize, dmu_tx_t *tx)
9ae529ec
CS
993{
994 uint64_t new_obj;
995
50c957f7
NB
996 VERIFY((new_obj = zap_create_dnsize(os, ot, DMU_OT_NONE, 0,
997 dnodesize, tx)) > 0);
e0ab3ab5
JS
998 VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
999 tx));
9ae529ec
CS
1000
1001 return (new_obj);
1002}
1003
34dc7c2f
BB
1004int
1005zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
1006 char *name)
1007{
1008 zap_cursor_t zc;
1009 zap_attribute_t *za;
1010 int err;
1011
1012 if (mask == 0)
1013 mask = -1ULL;
1014
79c76d5b 1015 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
34dc7c2f
BB
1016 for (zap_cursor_init(&zc, os, zapobj);
1017 (err = zap_cursor_retrieve(&zc, za)) == 0;
1018 zap_cursor_advance(&zc)) {
1019 if ((za->za_first_integer & mask) == (value & mask)) {
1020 (void) strcpy(name, za->za_name);
1021 break;
1022 }
1023 }
1024 zap_cursor_fini(&zc);
1025 kmem_free(za, sizeof (zap_attribute_t));
1026 return (err);
1027}
1028
b128c09f
BB
1029int
1030zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1031{
1032 zap_cursor_t zc;
1033 zap_attribute_t za;
1034 int err;
1035
3a84951d 1036 err = 0;
b128c09f
BB
1037 for (zap_cursor_init(&zc, os, fromobj);
1038 zap_cursor_retrieve(&zc, &za) == 0;
1039 (void) zap_cursor_advance(&zc)) {
3a84951d
WA
1040 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1041 err = SET_ERROR(EINVAL);
1042 break;
1043 }
b128c09f
BB
1044 err = zap_add(os, intoobj, za.za_name,
1045 8, 1, &za.za_first_integer, tx);
1046 if (err)
3a84951d 1047 break;
b128c09f
BB
1048 }
1049 zap_cursor_fini(&zc);
3a84951d 1050 return (err);
b128c09f
BB
1051}
1052
428870ff
BB
1053int
1054zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1055 uint64_t value, dmu_tx_t *tx)
1056{
1057 zap_cursor_t zc;
1058 zap_attribute_t za;
1059 int err;
1060
3a84951d 1061 err = 0;
428870ff
BB
1062 for (zap_cursor_init(&zc, os, fromobj);
1063 zap_cursor_retrieve(&zc, &za) == 0;
1064 (void) zap_cursor_advance(&zc)) {
3a84951d
WA
1065 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1066 err = SET_ERROR(EINVAL);
1067 break;
1068 }
428870ff
BB
1069 err = zap_add(os, intoobj, za.za_name,
1070 8, 1, &value, tx);
1071 if (err)
3a84951d 1072 break;
428870ff
BB
1073 }
1074 zap_cursor_fini(&zc);
3a84951d 1075 return (err);
428870ff
BB
1076}
1077
1078int
1079zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1080 dmu_tx_t *tx)
1081{
1082 zap_cursor_t zc;
1083 zap_attribute_t za;
1084 int err;
1085
3a84951d 1086 err = 0;
428870ff
BB
1087 for (zap_cursor_init(&zc, os, fromobj);
1088 zap_cursor_retrieve(&zc, &za) == 0;
1089 (void) zap_cursor_advance(&zc)) {
1090 uint64_t delta = 0;
1091
3a84951d
WA
1092 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1093 err = SET_ERROR(EINVAL);
1094 break;
1095 }
428870ff
BB
1096
1097 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1098 if (err != 0 && err != ENOENT)
3a84951d 1099 break;
428870ff
BB
1100 delta += za.za_first_integer;
1101 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1102 if (err)
3a84951d 1103 break;
428870ff
BB
1104 }
1105 zap_cursor_fini(&zc);
3a84951d 1106 return (err);
428870ff
BB
1107}
1108
b128c09f
BB
1109int
1110zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1111{
1112 char name[20];
1113
1114 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1115 return (zap_add(os, obj, name, 8, 1, &value, tx));
1116}
1117
1118int
1119zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1120{
1121 char name[20];
1122
1123 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1124 return (zap_remove(os, obj, name, tx));
1125}
1126
1127int
1128zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1129{
1130 char name[20];
1131
1132 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1133 return (zap_lookup(os, obj, name, 8, 1, &value));
1134}
34dc7c2f 1135
428870ff
BB
1136int
1137zap_add_int_key(objset_t *os, uint64_t obj,
1138 uint64_t key, uint64_t value, dmu_tx_t *tx)
1139{
1140 char name[20];
1141
1142 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1143 return (zap_add(os, obj, name, 8, 1, &value, tx));
1144}
1145
753c3839
MA
1146int
1147zap_update_int_key(objset_t *os, uint64_t obj,
1148 uint64_t key, uint64_t value, dmu_tx_t *tx)
1149{
1150 char name[20];
1151
1152 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1153 return (zap_update(os, obj, name, 8, 1, &value, tx));
1154}
1155
428870ff
BB
1156int
1157zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1158{
1159 char name[20];
1160
1161 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1162 return (zap_lookup(os, obj, name, 8, 1, valuep));
1163}
1164
1165int
1166zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1167 dmu_tx_t *tx)
1168{
1169 uint64_t value = 0;
1170 int err;
1171
1172 if (delta == 0)
1173 return (0);
1174
1175 err = zap_lookup(os, obj, name, 8, 1, &value);
1176 if (err != 0 && err != ENOENT)
1177 return (err);
1178 value += delta;
1179 if (value == 0)
1180 err = zap_remove(os, obj, name, tx);
1181 else
1182 err = zap_update(os, obj, name, 8, 1, &value, tx);
1183 return (err);
1184}
1185
1186int
1187zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1188 dmu_tx_t *tx)
1189{
1190 char name[20];
1191
1192 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1193 return (zap_increment(os, obj, name, delta, tx));
1194}
1195
34dc7c2f
BB
1196/*
1197 * Routines for iterating over the attributes.
1198 */
1199
1200int
1201fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1202{
1203 int err = ENOENT;
1204 zap_entry_handle_t zeh;
1205 zap_leaf_t *l;
1206
1207 /* retrieve the next entry at or after zc_hash/zc_cd */
1208 /* if no entry, return ENOENT */
1209
1210 if (zc->zc_leaf &&
1211 (ZAP_HASH_IDX(zc->zc_hash,
d683ddbb
JG
1212 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1213 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
34dc7c2f
BB
1214 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1215 zap_put_leaf(zc->zc_leaf);
1216 zc->zc_leaf = NULL;
1217 }
1218
1219again:
1220 if (zc->zc_leaf == NULL) {
1221 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1222 &zc->zc_leaf);
1223 if (err != 0)
1224 return (err);
1225 } else {
1226 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1227 }
1228 l = zc->zc_leaf;
1229
1230 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1231
1232 if (err == ENOENT) {
1233 uint64_t nocare =
d683ddbb 1234 (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
34dc7c2f
BB
1235 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1236 zc->zc_cd = 0;
d683ddbb
JG
1237 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1238 zc->zc_hash == 0) {
34dc7c2f
BB
1239 zc->zc_hash = -1ULL;
1240 } else {
1241 zap_put_leaf(zc->zc_leaf);
1242 zc->zc_leaf = NULL;
1243 goto again;
1244 }
1245 }
1246
1247 if (err == 0) {
1248 zc->zc_hash = zeh.zeh_hash;
1249 zc->zc_cd = zeh.zeh_cd;
1250 za->za_integer_length = zeh.zeh_integer_size;
1251 za->za_num_integers = zeh.zeh_num_integers;
1252 if (zeh.zeh_num_integers == 0) {
1253 za->za_first_integer = 0;
1254 } else {
1255 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1256 ASSERT(err == 0 || err == EOVERFLOW);
1257 }
428870ff 1258 err = zap_entry_read_name(zap, &zeh,
34dc7c2f
BB
1259 sizeof (za->za_name), za->za_name);
1260 ASSERT(err == 0);
1261
1262 za->za_normalization_conflict =
1263 zap_entry_normalization_conflict(&zeh,
1264 NULL, za->za_name, zap);
1265 }
1266 rw_exit(&zc->zc_leaf->l_rwlock);
1267 return (err);
1268}
1269
34dc7c2f
BB
1270static void
1271zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1272{
1273 int i, err;
1274 uint64_t lastblk = 0;
1275
1276 /*
1277 * NB: if a leaf has more pointers than an entire ptrtbl block
1278 * can hold, then it'll be accounted for more than once, since
1279 * we won't have lastblk.
1280 */
1281 for (i = 0; i < len; i++) {
1282 zap_leaf_t *l;
1283
1284 if (tbl[i] == lastblk)
1285 continue;
1286 lastblk = tbl[i];
1287
1288 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1289 if (err == 0) {
1290 zap_leaf_stats(zap, l, zs);
1291 zap_put_leaf(l);
1292 }
1293 }
1294}
1295
1296void
1297fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1298{
1299 int bs = FZAP_BLOCK_SHIFT(zap);
1300 zs->zs_blocksize = 1ULL << bs;
1301
1302 /*
1303 * Set zap_phys_t fields
1304 */
d683ddbb
JG
1305 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1306 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1307 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1308 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1309 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1310 zs->zs_salt = zap_f_phys(zap)->zap_salt;
34dc7c2f
BB
1311
1312 /*
1313 * Set zap_ptrtbl fields
1314 */
d683ddbb
JG
1315 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1316 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
34dc7c2f 1317 zs->zs_ptrtbl_blks_copied =
d683ddbb
JG
1318 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1319 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1320 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1321 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
34dc7c2f 1322
d683ddbb 1323 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
34dc7c2f
BB
1324 /* the ptrtbl is entirely in the header block. */
1325 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1326 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1327 } else {
1328 int b;
1329
fcff0f35 1330 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
d683ddbb 1331 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
fcff0f35
PD
1332 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1333 ZIO_PRIORITY_SYNC_READ);
34dc7c2f 1334
d683ddbb 1335 for (b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
34dc7c2f
BB
1336 b++) {
1337 dmu_buf_t *db;
1338 int err;
1339
1340 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
d683ddbb 1341 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
428870ff 1342 FTAG, &db, DMU_READ_NO_PREFETCH);
34dc7c2f
BB
1343 if (err == 0) {
1344 zap_stats_ptrtbl(zap, db->db_data,
1345 1<<(bs-3), zs);
1346 dmu_buf_rele(db, FTAG);
1347 }
1348 }
1349 }
1350}
9babb374
BB
1351
1352int
1353fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
1354 uint64_t *tooverwrite)
1355{
1356 zap_t *zap = zn->zn_zap;
1357 zap_leaf_t *l;
1358 int err;
1359
1360 /*
1361 * Account for the header block of the fatzap.
1362 */
1363 if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
428870ff 1364 *tooverwrite += zap->zap_dbuf->db_size;
9babb374 1365 } else {
428870ff 1366 *towrite += zap->zap_dbuf->db_size;
9babb374
BB
1367 }
1368
1369 /*
1370 * Account for the pointer table blocks.
1371 * If we are adding we need to account for the following cases :
1372 * - If the pointer table is embedded, this operation could force an
1373 * external pointer table.
1374 * - If this already has an external pointer table this operation
1375 * could extend the table.
1376 */
1377 if (add) {
d683ddbb 1378 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0)
428870ff 1379 *towrite += zap->zap_dbuf->db_size;
9babb374 1380 else
428870ff 1381 *towrite += (zap->zap_dbuf->db_size * 3);
9babb374
BB
1382 }
1383
1384 /*
1385 * Now, check if the block containing leaf is freeable
1386 * and account accordingly.
1387 */
1388 err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1389 if (err != 0) {
1390 return (err);
1391 }
1392
1393 if (!add && dmu_buf_freeable(l->l_dbuf)) {
428870ff 1394 *tooverwrite += l->l_dbuf->db_size;
9babb374
BB
1395 } else {
1396 /*
1397 * If this an add operation, the leaf block could split.
1398 * Hence, we need to account for an additional leaf block.
1399 */
428870ff 1400 *towrite += (add ? 2 : 1) * l->l_dbuf->db_size;
9babb374
BB
1401 }
1402
1403 zap_put_leaf(l);
1404 return (0);
1405}