xref: /freebsd/sys/contrib/openzfs/module/zfs/zap.c (revision 8311bc5f17dec348749f763b82dfe2737bc53cd7)
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 https://opensource.org/licenses/CDDL-1.0.
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/zap_impl.h>
49 #include <sys/zap_leaf.h>
50 
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  */
79 static int zap_iterate_prefetch = B_TRUE;
80 
81 int fzap_default_block_shift = 14; /* 16k blocksize */
82 
83 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
84 
85 void
86 fzap_byteswap(void *vbuf, size_t size)
87 {
88 	uint64_t block_type = *(uint64_t *)vbuf;
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 
98 void
99 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
100 {
101 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
102 	zap->zap_ismicro = FALSE;
103 
104 	zap->zap_dbu.dbu_evict_func_sync = zap_evict_sync;
105 	zap->zap_dbu.dbu_evict_func_async = NULL;
106 
107 	mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT, 0);
108 	zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
109 
110 	zap_phys_t *zp = zap_f_phys(zap);
111 	/*
112 	 * explicitly zero it since it might be coming from an
113 	 * initialized microzap
114 	 */
115 	memset(zap->zap_dbuf->db_data, 0, zap->zap_dbuf->db_size);
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;
126 	zp->zap_flags = flags;
127 
128 	/* block 1 will be the first leaf */
129 	for (int i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
130 		ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
131 
132 	/*
133 	 * set up block 1 - the first leaf
134 	 */
135 	dmu_buf_t *db;
136 	VERIFY0(dmu_buf_hold_by_dnode(zap->zap_dnode,
137 	    1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
138 	dmu_buf_will_dirty(db, tx);
139 
140 	zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
141 	l->l_dbuf = db;
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 
149 static int
150 zap_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 
165 static int
166 zap_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 {
170 	uint64_t newblk;
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;
184 		ASSERT0(tbl->zt_blks_copied);
185 		dmu_prefetch_by_dnode(zap->zap_dnode, 0,
186 		    tbl->zt_blk << bs, tbl->zt_numblks << bs,
187 		    ZIO_PRIORITY_SYNC_READ);
188 	}
189 
190 	/*
191 	 * Copy the ptrtbl from the old to new location.
192 	 */
193 
194 	uint64_t b = tbl->zt_blks_copied;
195 	dmu_buf_t *db_old;
196 	int err = dmu_buf_hold_by_dnode(zap->zap_dnode,
197 	    (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
198 	if (err != 0)
199 		return (err);
200 
201 	/* first half of entries in old[b] go to new[2*b+0] */
202 	dmu_buf_t *db_new;
203 	VERIFY0(dmu_buf_hold_by_dnode(zap->zap_dnode,
204 	    (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
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] */
210 	VERIFY0(dmu_buf_hold_by_dnode(zap->zap_dnode,
211 	    (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
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",
222 	    (u_longlong_t)tbl->zt_blks_copied,
223 	    (u_longlong_t)tbl->zt_numblks);
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 
235 		dprintf("finished; numblocks now %llu (%uk entries)\n",
236 		    (u_longlong_t)tbl->zt_numblks, 1<<(tbl->zt_shift-10));
237 	}
238 
239 	return (0);
240 }
241 
242 static int
243 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
244     dmu_tx_t *tx)
245 {
246 	int bs = FZAP_BLOCK_SHIFT(zap);
247 
248 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
249 	ASSERT(tbl->zt_blk != 0);
250 
251 	dprintf("storing %llx at index %llx\n", (u_longlong_t)val,
252 	    (u_longlong_t)idx);
253 
254 	uint64_t blk = idx >> (bs-3);
255 	uint64_t off = idx & ((1<<(bs-3))-1);
256 
257 	dmu_buf_t *db;
258 	int err = dmu_buf_hold_by_dnode(zap->zap_dnode,
259 	    (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
260 	if (err != 0)
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 
270 		err = dmu_buf_hold_by_dnode(zap->zap_dnode,
271 		    (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
272 		    DMU_READ_NO_PREFETCH);
273 		if (err != 0) {
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 
289 static int
290 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
291 {
292 	int bs = FZAP_BLOCK_SHIFT(zap);
293 
294 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
295 
296 	uint64_t blk = idx >> (bs-3);
297 	uint64_t off = idx & ((1<<(bs-3))-1);
298 
299 	dmu_buf_t *db;
300 	int err = dmu_buf_hold_by_dnode(zap->zap_dnode,
301 	    (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
302 	if (err != 0)
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 
315 		err = dmu_buf_hold_by_dnode(zap->zap_dnode,
316 		    (tbl->zt_nextblk + blk) << bs, FTAG, &db,
317 		    DMU_READ_NO_PREFETCH);
318 		if (err == 0)
319 			dmu_buf_rele(db, FTAG);
320 	}
321 	return (err);
322 }
323 
324 /*
325  * Routines for growing the ptrtbl.
326  */
327 
328 static void
329 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
330 {
331 	for (int i = 0; i < n; i++) {
332 		uint64_t lb = src[i];
333 		dst[2 * i + 0] = lb;
334 		dst[2 * i + 1] = lb;
335 	}
336 }
337 
338 static int
339 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
340 {
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 	 */
347 	if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
348 		return (SET_ERROR(ENOSPC));
349 
350 	if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
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 		 */
356 		ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
357 		    ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
358 		ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
359 
360 		uint64_t newblk = zap_allocate_blocks(zap, 1);
361 		dmu_buf_t *db_new;
362 		int err = dmu_buf_hold_by_dnode(zap->zap_dnode,
363 		    newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
364 		    DMU_READ_NO_PREFETCH);
365 		if (err != 0)
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 
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++;
375 
376 		ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
377 		    zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
378 		    (FZAP_BLOCK_SHIFT(zap)-3));
379 
380 		return (0);
381 	} else {
382 		return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
383 		    zap_ptrtbl_transfer, tx));
384 	}
385 }
386 
387 static void
388 zap_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);
392 	ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
393 	zap_f_phys(zap)->zap_num_entries += delta;
394 	mutex_exit(&zap->zap_f.zap_num_entries_mtx);
395 }
396 
397 static uint64_t
398 zap_allocate_blocks(zap_t *zap, int nblocks)
399 {
400 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
401 	uint64_t newblk = zap_f_phys(zap)->zap_freeblk;
402 	zap_f_phys(zap)->zap_freeblk += nblocks;
403 	return (newblk);
404 }
405 
406 static void
407 zap_leaf_evict_sync(void *dbu)
408 {
409 	zap_leaf_t *l = dbu;
410 
411 	rw_destroy(&l->l_rwlock);
412 	kmem_free(l, sizeof (zap_leaf_t));
413 }
414 
415 static zap_leaf_t *
416 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
417 {
418 	zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
419 
420 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
421 
422 	rw_init(&l->l_rwlock, NULL, RW_NOLOCKDEP, NULL);
423 	rw_enter(&l->l_rwlock, RW_WRITER);
424 	l->l_blkid = zap_allocate_blocks(zap, 1);
425 	l->l_dbuf = NULL;
426 
427 	VERIFY0(dmu_buf_hold_by_dnode(zap->zap_dnode,
428 	    l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
429 	    DMU_READ_NO_PREFETCH));
430 	dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
431 	VERIFY3P(NULL, ==, dmu_buf_set_user(l->l_dbuf, &l->l_dbu));
432 	dmu_buf_will_dirty(l->l_dbuf, tx);
433 
434 	zap_leaf_init(l, zap->zap_normflags != 0);
435 
436 	zap_f_phys(zap)->zap_num_leafs++;
437 
438 	return (l);
439 }
440 
441 int
442 fzap_count(zap_t *zap, uint64_t *count)
443 {
444 	ASSERT(!zap->zap_ismicro);
445 	mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
446 	*count = zap_f_phys(zap)->zap_num_entries;
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 
455 void
456 zap_put_leaf(zap_leaf_t *l)
457 {
458 	rw_exit(&l->l_rwlock);
459 	dmu_buf_rele(l->l_dbuf, NULL);
460 }
461 
462 static zap_leaf_t *
463 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
464 {
465 	ASSERT(blkid != 0);
466 
467 	zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
468 	rw_init(&l->l_rwlock, NULL, RW_DEFAULT, NULL);
469 	rw_enter(&l->l_rwlock, RW_WRITER);
470 	l->l_blkid = blkid;
471 	l->l_bs = highbit64(db->db_size) - 1;
472 	l->l_dbuf = db;
473 
474 	dmu_buf_init_user(&l->l_dbu, zap_leaf_evict_sync, NULL, &l->l_dbuf);
475 	zap_leaf_t *winner = dmu_buf_set_user(db, &l->l_dbu);
476 
477 	rw_exit(&l->l_rwlock);
478 	if (winner != NULL) {
479 		/* someone else set it first */
480 		zap_leaf_evict_sync(&l->l_dbu);
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 	 */
489 	ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
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 */
498 	ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==, (zap_leaf_chunk_t *)
499 	    &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
500 
501 	/* The chunks should end at the end of the block */
502 	ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
503 	    (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
504 
505 	return (l);
506 }
507 
508 static int
509 zap_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;
513 
514 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
515 
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)
524 		return (SET_ERROR(ENOENT));
525 
526 	int bs = FZAP_BLOCK_SHIFT(zap);
527 	int err = dmu_buf_hold_by_dnode(zap->zap_dnode,
528 	    blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
529 	if (err != 0)
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 
537 	zap_leaf_t *l = dmu_buf_get_user(db);
538 
539 	if (l == NULL)
540 		l = zap_open_leaf(blkid, db);
541 
542 	rw_enter(&l->l_rwlock, lt);
543 	/*
544 	 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
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);
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);
553 
554 	*lp = l;
555 	return (0);
556 }
557 
558 static int
559 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
560 {
561 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
562 
563 	if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
564 		ASSERT3U(idx, <,
565 		    (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
566 		*valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
567 		return (0);
568 	} else {
569 		return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
570 		    idx, valp));
571 	}
572 }
573 
574 static int
575 zap_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 
580 	if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
581 		ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
582 		return (0);
583 	} else {
584 		return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
585 		    idx, blk, tx));
586 	}
587 }
588 
589 static int
590 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
591 {
592 	uint64_t blk;
593 
594 	ASSERT(zap->zap_dbuf == NULL ||
595 	    zap_f_phys(zap) == zap->zap_dbuf->db_data);
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 	}
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);
606 	if (err != 0)
607 		return (err);
608 	err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
609 
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);
613 	return (err);
614 }
615 
616 static int
617 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
618     const void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
619 {
620 	zap_t *zap = zn->zn_zap;
621 	uint64_t hash = zn->zn_hash;
622 	int err;
623 	int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
624 
625 	ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
626 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
627 
628 	ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
629 	    zap_leaf_phys(l)->l_hdr.lh_prefix);
630 
631 	if (zap_tryupgradedir(zap, tx) == 0 ||
632 	    old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
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);
638 		zap_unlockdir(zap, tag);
639 		err = zap_lockdir(os, object, tx, RW_WRITER,
640 		    FALSE, FALSE, tag, &zn->zn_zap);
641 		zap = zn->zn_zap;
642 		if (err != 0)
643 			return (err);
644 		ASSERT(!zap->zap_ismicro);
645 
646 		while (old_prefix_len ==
647 		    zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
648 			err = zap_grow_ptrtbl(zap, tx);
649 			if (err != 0)
650 				return (err);
651 		}
652 
653 		err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
654 		if (err != 0)
655 			return (err);
656 
657 		if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
658 			/* it split while our locks were down */
659 			*lp = l;
660 			return (0);
661 		}
662 	}
663 	ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
664 	ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
665 	ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
666 	    zap_leaf_phys(l)->l_hdr.lh_prefix);
667 
668 	int prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
669 	    (old_prefix_len + 1);
670 	uint64_t sibling =
671 	    (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
672 
673 	/* check for i/o errors before doing zap_leaf_split */
674 	for (int i = 0; i < (1ULL << prefix_diff); i++) {
675 		uint64_t blk;
676 		err = zap_idx_to_blk(zap, sibling + i, &blk);
677 		if (err != 0)
678 			return (err);
679 		ASSERT3U(blk, ==, l->l_blkid);
680 	}
681 
682 	zap_leaf_t *nl = zap_create_leaf(zap, tx);
683 	zap_leaf_split(l, nl, zap->zap_normflags != 0);
684 
685 	/* set sibling pointers */
686 	for (int i = 0; i < (1ULL << prefix_diff); i++) {
687 		err = zap_set_idx_to_blk(zap, sibling + i, nl->l_blkid, tx);
688 		ASSERT0(err); /* we checked for i/o errors above */
689 	}
690 
691 	ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_prefix_len, >, 0);
692 
693 	if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
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 
705 static void
706 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
707     const void *tag, dmu_tx_t *tx)
708 {
709 	zap_t *zap = zn->zn_zap;
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);
713 
714 	zap_put_leaf(l);
715 
716 	if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
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 
725 			zap_unlockdir(zap, tag);
726 			int err = zap_lockdir(os, zapobj, tx,
727 			    RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
728 			zap = zn->zn_zap;
729 			if (err != 0)
730 				return;
731 		}
732 
733 		/* could have finished growing while our locks were down */
734 		if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
735 			(void) zap_grow_ptrtbl(zap, tx);
736 	}
737 }
738 
739 static int
740 fzap_checkname(zap_name_t *zn)
741 {
742 	if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
743 		return (SET_ERROR(ENAMETOOLONG));
744 	return (0);
745 }
746 
747 static int
748 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
749 {
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:
758 		return (SET_ERROR(EINVAL));
759 	}
760 
761 	if (integer_size * num_integers > ZAP_MAXVALUELEN)
762 		return (SET_ERROR(E2BIG));
763 
764 	return (0);
765 }
766 
767 static int
768 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
769 {
770 	int err = fzap_checkname(zn);
771 	if (err != 0)
772 		return (err);
773 	return (fzap_checksize(integer_size, num_integers));
774 }
775 
776 /*
777  * Routines for manipulating attributes.
778  */
779 int
780 fzap_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;
785 	zap_entry_handle_t zeh;
786 
787 	int err = fzap_checkname(zn);
788 	if (err != 0)
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) {
796 		if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
797 			zap_put_leaf(l);
798 			return (err);
799 		}
800 
801 		err = zap_entry_read(&zeh, integer_size, num_integers, buf);
802 		(void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
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 
813 int
814 fzap_add_cd(zap_name_t *zn,
815     uint64_t integer_size, uint64_t num_integers,
816     const void *val, uint32_t cd, const void *tag, dmu_tx_t *tx)
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);
825 	ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
826 
827 	err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
828 	if (err != 0)
829 		return (err);
830 retry:
831 	err = zap_leaf_lookup(l, zn, &zeh);
832 	if (err == 0) {
833 		err = SET_ERROR(EEXIST);
834 		goto out;
835 	}
836 	if (err != ENOENT)
837 		goto out;
838 
839 	err = zap_entry_create(l, zn, cd,
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) {
845 		err = zap_expand_leaf(zn, l, tag, tx, &l);
846 		zap = zn->zn_zap;	/* zap_expand_leaf() may change zap */
847 		if (err == 0) {
848 			goto retry;
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 		}
857 	}
858 
859 out:
860 	if (zap != NULL)
861 		zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
862 	return (err);
863 }
864 
865 int
866 fzap_add(zap_name_t *zn,
867     uint64_t integer_size, uint64_t num_integers,
868     const void *val, const void *tag, dmu_tx_t *tx)
869 {
870 	int err = fzap_check(zn, integer_size, num_integers);
871 	if (err != 0)
872 		return (err);
873 
874 	return (fzap_add_cd(zn, integer_size, num_integers,
875 	    val, ZAP_NEED_CD, tag, tx));
876 }
877 
878 int
879 fzap_update(zap_name_t *zn,
880     int integer_size, uint64_t num_integers, const void *val,
881     const void *tag, dmu_tx_t *tx)
882 {
883 	zap_leaf_t *l;
884 	int err;
885 	boolean_t create;
886 	zap_entry_handle_t zeh;
887 	zap_t *zap = zn->zn_zap;
888 
889 	ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
890 	err = fzap_check(zn, integer_size, num_integers);
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);
897 retry:
898 	err = zap_leaf_lookup(l, zn, &zeh);
899 	create = (err == ENOENT);
900 	ASSERT(err == 0 || err == ENOENT);
901 
902 	if (create) {
903 		err = zap_entry_create(l, zn, ZAP_NEED_CD,
904 		    integer_size, num_integers, val, &zeh);
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) {
912 		err = zap_expand_leaf(zn, l, tag, tx, &l);
913 		zap = zn->zn_zap;	/* zap_expand_leaf() may change zap */
914 		if (err == 0)
915 			goto retry;
916 	}
917 
918 	if (zap != NULL)
919 		zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
920 	return (err);
921 }
922 
923 int
924 fzap_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 
938 	if (integer_size != NULL)
939 		*integer_size = zeh.zeh_integer_size;
940 	if (num_integers != NULL)
941 		*num_integers = zeh.zeh_num_integers;
942 out:
943 	zap_put_leaf(l);
944 	return (err);
945 }
946 
947 int
948 fzap_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 
966 void
967 fzap_prefetch(zap_name_t *zn)
968 {
969 	uint64_t blk;
970 	zap_t *zap = zn->zn_zap;
971 
972 	uint64_t idx = ZAP_HASH_IDX(zn->zn_hash,
973 	    zap_f_phys(zap)->zap_ptrtbl.zt_shift);
974 	if (zap_idx_to_blk(zap, idx, &blk) != 0)
975 		return;
976 	int bs = FZAP_BLOCK_SHIFT(zap);
977 	dmu_prefetch_by_dnode(zap->zap_dnode, 0, blk << bs, 1 << bs,
978 	    ZIO_PRIORITY_SYNC_READ);
979 }
980 
981 /*
982  * Helper functions for consumers.
983  */
984 
985 uint64_t
986 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
987     const char *name, dmu_tx_t *tx)
988 {
989 	return (zap_create_link_dnsize(os, ot, parent_obj, name, 0, tx));
990 }
991 
992 uint64_t
993 zap_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)
995 {
996 	uint64_t new_obj;
997 
998 	new_obj = zap_create_dnsize(os, ot, DMU_OT_NONE, 0, dnodesize, tx);
999 	VERIFY(new_obj != 0);
1000 	VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
1001 	    tx));
1002 
1003 	return (new_obj);
1004 }
1005 
1006 int
1007 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
1008     char *name)
1009 {
1010 	zap_cursor_t zc;
1011 	int err;
1012 
1013 	if (mask == 0)
1014 		mask = -1ULL;
1015 
1016 	zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
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)) {
1021 			(void) strlcpy(name, za->za_name, MAXNAMELEN);
1022 			break;
1023 		}
1024 	}
1025 	zap_cursor_fini(&zc);
1026 	kmem_free(za, sizeof (*za));
1027 	return (err);
1028 }
1029 
1030 int
1031 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1032 {
1033 	zap_cursor_t zc;
1034 	int err = 0;
1035 
1036 	zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1037 	for (zap_cursor_init(&zc, os, fromobj);
1038 	    zap_cursor_retrieve(&zc, za) == 0;
1039 	    (void) zap_cursor_advance(&zc)) {
1040 		if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1041 			err = SET_ERROR(EINVAL);
1042 			break;
1043 		}
1044 		err = zap_add(os, intoobj, za->za_name,
1045 		    8, 1, &za->za_first_integer, tx);
1046 		if (err != 0)
1047 			break;
1048 	}
1049 	zap_cursor_fini(&zc);
1050 	kmem_free(za, sizeof (*za));
1051 	return (err);
1052 }
1053 
1054 int
1055 zap_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;
1059 	int err = 0;
1060 
1061 	zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1062 	for (zap_cursor_init(&zc, os, fromobj);
1063 	    zap_cursor_retrieve(&zc, za) == 0;
1064 	    (void) zap_cursor_advance(&zc)) {
1065 		if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1066 			err = SET_ERROR(EINVAL);
1067 			break;
1068 		}
1069 		err = zap_add(os, intoobj, za->za_name,
1070 		    8, 1, &value, tx);
1071 		if (err != 0)
1072 			break;
1073 	}
1074 	zap_cursor_fini(&zc);
1075 	kmem_free(za, sizeof (*za));
1076 	return (err);
1077 }
1078 
1079 int
1080 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1081     dmu_tx_t *tx)
1082 {
1083 	zap_cursor_t zc;
1084 	int err = 0;
1085 
1086 	zap_attribute_t *za = kmem_alloc(sizeof (*za), KM_SLEEP);
1087 	for (zap_cursor_init(&zc, os, fromobj);
1088 	    zap_cursor_retrieve(&zc, za) == 0;
1089 	    (void) zap_cursor_advance(&zc)) {
1090 		uint64_t delta = 0;
1091 
1092 		if (za->za_integer_length != 8 || za->za_num_integers != 1) {
1093 			err = SET_ERROR(EINVAL);
1094 			break;
1095 		}
1096 
1097 		err = zap_lookup(os, intoobj, za->za_name, 8, 1, &delta);
1098 		if (err != 0 && err != ENOENT)
1099 			break;
1100 		delta += za->za_first_integer;
1101 		err = zap_update(os, intoobj, za->za_name, 8, 1, &delta, tx);
1102 		if (err != 0)
1103 			break;
1104 	}
1105 	zap_cursor_fini(&zc);
1106 	kmem_free(za, sizeof (*za));
1107 	return (err);
1108 }
1109 
1110 int
1111 zap_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 
1119 int
1120 zap_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 
1128 int
1129 zap_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 }
1136 
1137 int
1138 zap_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 
1147 int
1148 zap_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 
1157 int
1158 zap_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 
1166 int
1167 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1168     dmu_tx_t *tx)
1169 {
1170 	uint64_t value = 0;
1171 
1172 	if (delta == 0)
1173 		return (0);
1174 
1175 	int err = zap_lookup(os, obj, name, 8, 1, &value);
1176 	if (err != 0 && err != ENOENT)
1177 		return (err);
1178 	value += delta;
1179 	if (value == 0)
1180 		err = zap_remove(os, obj, name, tx);
1181 	else
1182 		err = zap_update(os, obj, name, 8, 1, &value, tx);
1183 	return (err);
1184 }
1185 
1186 int
1187 zap_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 
1196 /*
1197  * Routines for iterating over the attributes.
1198  */
1199 
1200 int
1201 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1202 {
1203 	int err = ENOENT;
1204 	zap_entry_handle_t zeh;
1205 	zap_leaf_t *l;
1206 
1207 	/* retrieve the next entry at or after zc_hash/zc_cd */
1208 	/* if no entry, return ENOENT */
1209 
1210 	/*
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) {
1220 		dmu_prefetch_by_dnode(zap->zap_dnode, 0, 0,
1221 		    zap_f_phys(zap)->zap_freeblk << FZAP_BLOCK_SHIFT(zap),
1222 		    ZIO_PRIORITY_ASYNC_READ);
1223 	}
1224 
1225 	if (zc->zc_leaf &&
1226 	    (ZAP_HASH_IDX(zc->zc_hash,
1227 	    zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1228 	    zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1229 		rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1230 		zap_put_leaf(zc->zc_leaf);
1231 		zc->zc_leaf = NULL;
1232 	}
1233 
1234 again:
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) {
1248 		if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0) {
1249 			zc->zc_hash = -1ULL;
1250 			zc->zc_cd = 0;
1251 		} else {
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 			}
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 		}
1279 		err = zap_entry_read_name(zap, &zeh,
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 
1291 static void
1292 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1293 {
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 	 */
1301 	for (int i = 0; i < len; i++) {
1302 		zap_leaf_t *l;
1303 
1304 		if (tbl[i] == lastblk)
1305 			continue;
1306 		lastblk = tbl[i];
1307 
1308 		int err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1309 		if (err == 0) {
1310 			zap_leaf_stats(zap, l, zs);
1311 			zap_put_leaf(l);
1312 		}
1313 	}
1314 }
1315 
1316 void
1317 fzap_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 	 */
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;
1331 
1332 	/*
1333 	 * Set zap_ptrtbl fields
1334 	 */
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;
1337 	zs->zs_ptrtbl_blks_copied =
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;
1342 
1343 	if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
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 {
1348 		dmu_prefetch_by_dnode(zap->zap_dnode, 0,
1349 		    zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1350 		    zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1351 		    ZIO_PRIORITY_SYNC_READ);
1352 
1353 		for (int b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1354 		    b++) {
1355 			dmu_buf_t *db;
1356 			int err;
1357 
1358 			err = dmu_buf_hold_by_dnode(zap->zap_dnode,
1359 			    (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1360 			    FTAG, &db, DMU_READ_NO_PREFETCH);
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 }
1369 
1370 /* CSTYLED */
1371 ZFS_MODULE_PARAM(zfs, , zap_iterate_prefetch, INT, ZMOD_RW,
1372 	"When iterating ZAP object, prefetch it");
1373