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