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