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