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