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