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