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