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