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