]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zap.c
MFV r305335: 7003 zap_lockdir() should tag hold
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zap.c
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25  */
26
27 /*
28  * This file contains the top half of the zfs directory structure
29  * implementation. The bottom half is in zap_leaf.c.
30  *
31  * The zdir is an extendable hash data structure. There is a table of
32  * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
33  * each a constant size and hold a variable number of directory entries.
34  * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
35  *
36  * The pointer table holds a power of 2 number of pointers.
37  * (1<<zap_t->zd_data->zd_phys->zd_prefix_len).  The bucket pointed to
38  * by the pointer at index i in the table holds entries whose hash value
39  * has a zd_prefix_len - bit prefix
40  */
41
42 #include <sys/spa.h>
43 #include <sys/dmu.h>
44 #include <sys/zfs_context.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/fs/zfs.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 extern inline zap_phys_t *zap_f_phys(zap_t *zap);
55
56 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
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, zap_flags_t flags)
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         zap->zap_dbu.dbu_evict_func = zap_evict;
85
86         mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
87         zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
88
89         zp = zap_f_phys(zap);
90         /*
91          * explicitly zero it since it might be coming from an
92          * initialized microzap
93          */
94         bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
95         zp->zap_block_type = ZBT_HEADER;
96         zp->zap_magic = ZAP_MAGIC;
97
98         zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
99
100         zp->zap_freeblk = 2;            /* block 1 will be the first leaf */
101         zp->zap_num_leafs = 1;
102         zp->zap_num_entries = 0;
103         zp->zap_salt = zap->zap_salt;
104         zp->zap_normflags = zap->zap_normflags;
105         zp->zap_flags = flags;
106
107         /* block 1 will be the first leaf */
108         for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
109                 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
110
111         /*
112          * set up block 1 - the first leaf
113          */
114         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
115             1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
116         dmu_buf_will_dirty(db, tx);
117
118         l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
119         l->l_dbuf = db;
120
121         zap_leaf_init(l, zp->zap_normflags != 0);
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                 ASSERT0(tbl->zt_blks_copied);
165                 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
166                     tbl->zt_blk << bs, tbl->zt_numblks << bs,
167                     ZIO_PRIORITY_SYNC_READ);
168         }
169
170         /*
171          * Copy the ptrtbl from the old to new location.
172          */
173
174         b = tbl->zt_blks_copied;
175         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
176             (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
177         if (err)
178                 return (err);
179
180         /* first half of entries in old[b] go to new[2*b+0] */
181         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
182             (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
183         dmu_buf_will_dirty(db_new, tx);
184         transfer_func(db_old->db_data, db_new->db_data, hepb);
185         dmu_buf_rele(db_new, FTAG);
186
187         /* second half of entries in old[b] go to new[2*b+1] */
188         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
189             (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
190         dmu_buf_will_dirty(db_new, tx);
191         transfer_func((uint64_t *)db_old->db_data + hepb,
192             db_new->db_data, hepb);
193         dmu_buf_rele(db_new, FTAG);
194
195         dmu_buf_rele(db_old, FTAG);
196
197         tbl->zt_blks_copied++;
198
199         dprintf("copied block %llu of %llu\n",
200             tbl->zt_blks_copied, tbl->zt_numblks);
201
202         if (tbl->zt_blks_copied == tbl->zt_numblks) {
203                 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
204                     tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
205
206                 tbl->zt_blk = newblk;
207                 tbl->zt_numblks *= 2;
208                 tbl->zt_shift++;
209                 tbl->zt_nextblk = 0;
210                 tbl->zt_blks_copied = 0;
211
212                 dprintf("finished; numblocks now %llu (%lluk entries)\n",
213                     tbl->zt_numblks, 1<<(tbl->zt_shift-10));
214         }
215
216         return (0);
217 }
218
219 static int
220 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
221     dmu_tx_t *tx)
222 {
223         int err;
224         uint64_t blk, off;
225         int bs = FZAP_BLOCK_SHIFT(zap);
226         dmu_buf_t *db;
227
228         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
229         ASSERT(tbl->zt_blk != 0);
230
231         dprintf("storing %llx at index %llx\n", val, idx);
232
233         blk = idx >> (bs-3);
234         off = idx & ((1<<(bs-3))-1);
235
236         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
237             (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
238         if (err)
239                 return (err);
240         dmu_buf_will_dirty(db, tx);
241
242         if (tbl->zt_nextblk != 0) {
243                 uint64_t idx2 = idx * 2;
244                 uint64_t blk2 = idx2 >> (bs-3);
245                 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
246                 dmu_buf_t *db2;
247
248                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
249                     (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
250                     DMU_READ_NO_PREFETCH);
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, DMU_READ_NO_PREFETCH);
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_READ_NO_PREFETCH);
298                 if (err == 0)
299                         dmu_buf_rele(db, FTAG);
300         }
301         return (err);
302 }
303
304 /*
305  * Routines for growing the ptrtbl.
306  */
307
308 static void
309 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
310 {
311         int i;
312         for (i = 0; i < n; i++) {
313                 uint64_t lb = src[i];
314                 dst[2*i+0] = lb;
315                 dst[2*i+1] = lb;
316         }
317 }
318
319 static int
320 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
321 {
322         /*
323          * The pointer table should never use more hash bits than we
324          * have (otherwise we'd be using useless zero bits to index it).
325          * If we are within 2 bits of running out, stop growing, since
326          * this is already an aberrant condition.
327          */
328         if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
329                 return (SET_ERROR(ENOSPC));
330
331         if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
332                 /*
333                  * We are outgrowing the "embedded" ptrtbl (the one
334                  * stored in the header block).  Give it its own entire
335                  * block, which will double the size of the ptrtbl.
336                  */
337                 uint64_t newblk;
338                 dmu_buf_t *db_new;
339                 int err;
340
341                 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
342                     ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
343                 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
344
345                 newblk = zap_allocate_blocks(zap, 1);
346                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
347                     newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
348                     DMU_READ_NO_PREFETCH);
349                 if (err)
350                         return (err);
351                 dmu_buf_will_dirty(db_new, tx);
352                 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
353                     db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
354                 dmu_buf_rele(db_new, FTAG);
355
356                 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
357                 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
358                 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
359
360                 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
361                     zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
362                     (FZAP_BLOCK_SHIFT(zap)-3));
363
364                 return (0);
365         } else {
366                 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
367                     zap_ptrtbl_transfer, tx));
368         }
369 }
370
371 static void
372 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
373 {
374         dmu_buf_will_dirty(zap->zap_dbuf, tx);
375         mutex_enter(&zap->zap_f.zap_num_entries_mtx);
376         ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
377         zap_f_phys(zap)->zap_num_entries += delta;
378         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
379 }
380
381 static uint64_t
382 zap_allocate_blocks(zap_t *zap, int nblocks)
383 {
384         uint64_t newblk;
385         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
386         newblk = zap_f_phys(zap)->zap_freeblk;
387         zap_f_phys(zap)->zap_freeblk += nblocks;
388         return (newblk);
389 }
390
391 static void
392 zap_leaf_pageout(void *dbu)
393 {
394         zap_leaf_t *l = dbu;
395
396         rw_destroy(&l->l_rwlock);
397         kmem_free(l, sizeof (zap_leaf_t));
398 }
399
400 static zap_leaf_t *
401 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
402 {
403         void *winner;
404         zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
405
406         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
407
408         rw_init(&l->l_rwlock, 0, 0, 0);
409         rw_enter(&l->l_rwlock, RW_WRITER);
410         l->l_blkid = zap_allocate_blocks(zap, 1);
411         l->l_dbuf = NULL;
412
413         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
414             l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
415             DMU_READ_NO_PREFETCH));
416         dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
417         winner = dmu_buf_set_user(l->l_dbuf, &l->l_dbu);
418         ASSERT(winner == NULL);
419         dmu_buf_will_dirty(l->l_dbuf, tx);
420
421         zap_leaf_init(l, zap->zap_normflags != 0);
422
423         zap_f_phys(zap)->zap_num_leafs++;
424
425         return (l);
426 }
427
428 int
429 fzap_count(zap_t *zap, uint64_t *count)
430 {
431         ASSERT(!zap->zap_ismicro);
432         mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
433         *count = zap_f_phys(zap)->zap_num_entries;
434         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
435         return (0);
436 }
437
438 /*
439  * Routines for obtaining zap_leaf_t's
440  */
441
442 void
443 zap_put_leaf(zap_leaf_t *l)
444 {
445         rw_exit(&l->l_rwlock);
446         dmu_buf_rele(l->l_dbuf, NULL);
447 }
448
449 static zap_leaf_t *
450 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
451 {
452         zap_leaf_t *l, *winner;
453
454         ASSERT(blkid != 0);
455
456         l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
457         rw_init(&l->l_rwlock, 0, 0, 0);
458         rw_enter(&l->l_rwlock, RW_WRITER);
459         l->l_blkid = blkid;
460         l->l_bs = highbit64(db->db_size) - 1;
461         l->l_dbuf = db;
462
463         dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
464         winner = dmu_buf_set_user(db, &l->l_dbu);
465
466         rw_exit(&l->l_rwlock);
467         if (winner != NULL) {
468                 /* someone else set it first */
469                 zap_leaf_pageout(&l->l_dbu);
470                 l = winner;
471         }
472
473         /*
474          * lhr_pad was previously used for the next leaf in the leaf
475          * chain.  There should be no chained leafs (as we have removed
476          * support for them).
477          */
478         ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
479
480         /*
481          * There should be more hash entries than there can be
482          * chunks to put in the hash table
483          */
484         ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
485
486         /* The chunks should begin at the end of the hash table */
487         ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
488             &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
489
490         /* The chunks should end at the end of the block */
491         ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
492             (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
493
494         return (l);
495 }
496
497 static int
498 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
499     zap_leaf_t **lp)
500 {
501         dmu_buf_t *db;
502         zap_leaf_t *l;
503         int bs = FZAP_BLOCK_SHIFT(zap);
504         int err;
505
506         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
507
508         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
509             blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
510         if (err)
511                 return (err);
512
513         ASSERT3U(db->db_object, ==, zap->zap_object);
514         ASSERT3U(db->db_offset, ==, blkid << bs);
515         ASSERT3U(db->db_size, ==, 1 << bs);
516         ASSERT(blkid != 0);
517
518         l = dmu_buf_get_user(db);
519
520         if (l == NULL)
521                 l = zap_open_leaf(blkid, db);
522
523         rw_enter(&l->l_rwlock, lt);
524         /*
525          * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
526          * causing ASSERT below to fail.
527          */
528         if (lt == RW_WRITER)
529                 dmu_buf_will_dirty(db, tx);
530         ASSERT3U(l->l_blkid, ==, blkid);
531         ASSERT3P(l->l_dbuf, ==, db);
532         ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
533         ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
534
535         *lp = l;
536         return (0);
537 }
538
539 static int
540 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
541 {
542         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
543
544         if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
545                 ASSERT3U(idx, <,
546                     (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
547                 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
548                 return (0);
549         } else {
550                 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
551                     idx, valp));
552         }
553 }
554
555 static int
556 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
557 {
558         ASSERT(tx != NULL);
559         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
560
561         if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
562                 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
563                 return (0);
564         } else {
565                 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
566                     idx, blk, tx));
567         }
568 }
569
570 static int
571 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
572 {
573         uint64_t idx, blk;
574         int err;
575
576         ASSERT(zap->zap_dbuf == NULL ||
577             zap_f_phys(zap) == zap->zap_dbuf->db_data);
578
579         /* Reality check for corrupt zap objects (leaf or header). */
580         if ((zap_f_phys(zap)->zap_block_type != ZBT_LEAF &&
581             zap_f_phys(zap)->zap_block_type != ZBT_HEADER) ||
582             zap_f_phys(zap)->zap_magic != ZAP_MAGIC) {
583                 return (SET_ERROR(EIO));
584         }
585
586         idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
587         err = zap_idx_to_blk(zap, idx, &blk);
588         if (err != 0)
589                 return (err);
590         err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
591
592         ASSERT(err ||
593             ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
594             zap_leaf_phys(*lp)->l_hdr.lh_prefix);
595         return (err);
596 }
597
598 static int
599 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l,
600     void *tag, dmu_tx_t *tx, zap_leaf_t **lp)
601 {
602         zap_t *zap = zn->zn_zap;
603         uint64_t hash = zn->zn_hash;
604         zap_leaf_t *nl;
605         int prefix_diff, i, err;
606         uint64_t sibling;
607         int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
608
609         ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
610         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
611
612         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
613             zap_leaf_phys(l)->l_hdr.lh_prefix);
614
615         if (zap_tryupgradedir(zap, tx) == 0 ||
616             old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
617                 /* We failed to upgrade, or need to grow the pointer table */
618                 objset_t *os = zap->zap_objset;
619                 uint64_t object = zap->zap_object;
620
621                 zap_put_leaf(l);
622                 zap_unlockdir(zap, tag);
623                 err = zap_lockdir(os, object, tx, RW_WRITER,
624                     FALSE, FALSE, tag, &zn->zn_zap);
625                 zap = zn->zn_zap;
626                 if (err)
627                         return (err);
628                 ASSERT(!zap->zap_ismicro);
629
630                 while (old_prefix_len ==
631                     zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
632                         err = zap_grow_ptrtbl(zap, tx);
633                         if (err)
634                                 return (err);
635                 }
636
637                 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
638                 if (err)
639                         return (err);
640
641                 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
642                         /* it split while our locks were down */
643                         *lp = l;
644                         return (0);
645                 }
646         }
647         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
648         ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
649         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
650             zap_leaf_phys(l)->l_hdr.lh_prefix);
651
652         prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
653             (old_prefix_len + 1);
654         sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
655
656         /* check for i/o errors before doing zap_leaf_split */
657         for (i = 0; i < (1ULL<<prefix_diff); i++) {
658                 uint64_t blk;
659                 err = zap_idx_to_blk(zap, sibling+i, &blk);
660                 if (err)
661                         return (err);
662                 ASSERT3U(blk, ==, l->l_blkid);
663         }
664
665         nl = zap_create_leaf(zap, tx);
666         zap_leaf_split(l, nl, zap->zap_normflags != 0);
667
668         /* set sibling pointers */
669         for (i = 0; i < (1ULL << prefix_diff); i++) {
670                 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
671                 ASSERT0(err); /* we checked for i/o errors above */
672         }
673
674         if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
675                 /* we want the sibling */
676                 zap_put_leaf(l);
677                 *lp = nl;
678         } else {
679                 zap_put_leaf(nl);
680                 *lp = l;
681         }
682
683         return (0);
684 }
685
686 static void
687 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l,
688     void *tag, dmu_tx_t *tx)
689 {
690         zap_t *zap = zn->zn_zap;
691         int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
692         int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
693             zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
694
695         zap_put_leaf(l);
696
697         if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
698                 int err;
699
700                 /*
701                  * We are in the middle of growing the pointer table, or
702                  * this leaf will soon make us grow it.
703                  */
704                 if (zap_tryupgradedir(zap, tx) == 0) {
705                         objset_t *os = zap->zap_objset;
706                         uint64_t zapobj = zap->zap_object;
707
708                         zap_unlockdir(zap, tag);
709                         err = zap_lockdir(os, zapobj, tx,
710                             RW_WRITER, FALSE, FALSE, tag, &zn->zn_zap);
711                         zap = zn->zn_zap;
712                         if (err)
713                                 return;
714                 }
715
716                 /* could have finished growing while our locks were down */
717                 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
718                         (void) zap_grow_ptrtbl(zap, tx);
719         }
720 }
721
722 static int
723 fzap_checkname(zap_name_t *zn)
724 {
725         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
726                 return (SET_ERROR(ENAMETOOLONG));
727         return (0);
728 }
729
730 static int
731 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
732 {
733         /* Only integer sizes supported by C */
734         switch (integer_size) {
735         case 1:
736         case 2:
737         case 4:
738         case 8:
739                 break;
740         default:
741                 return (SET_ERROR(EINVAL));
742         }
743
744         if (integer_size * num_integers > ZAP_MAXVALUELEN)
745                 return (E2BIG);
746
747         return (0);
748 }
749
750 static int
751 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
752 {
753         int err;
754
755         if ((err = fzap_checkname(zn)) != 0)
756                 return (err);
757         return (fzap_checksize(integer_size, num_integers));
758 }
759
760 /*
761  * Routines for manipulating attributes.
762  */
763 int
764 fzap_lookup(zap_name_t *zn,
765     uint64_t integer_size, uint64_t num_integers, void *buf,
766     char *realname, int rn_len, boolean_t *ncp)
767 {
768         zap_leaf_t *l;
769         int err;
770         zap_entry_handle_t zeh;
771
772         if ((err = fzap_checkname(zn)) != 0)
773                 return (err);
774
775         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
776         if (err != 0)
777                 return (err);
778         err = zap_leaf_lookup(l, zn, &zeh);
779         if (err == 0) {
780                 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
781                         zap_put_leaf(l);
782                         return (err);
783                 }
784
785                 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
786                 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
787                 if (ncp) {
788                         *ncp = zap_entry_normalization_conflict(&zeh,
789                             zn, NULL, zn->zn_zap);
790                 }
791         }
792
793         zap_put_leaf(l);
794         return (err);
795 }
796
797 int
798 fzap_add_cd(zap_name_t *zn,
799     uint64_t integer_size, uint64_t num_integers,
800     const void *val, uint32_t cd, void *tag, dmu_tx_t *tx)
801 {
802         zap_leaf_t *l;
803         int err;
804         zap_entry_handle_t zeh;
805         zap_t *zap = zn->zn_zap;
806
807         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
808         ASSERT(!zap->zap_ismicro);
809         ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
810
811         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
812         if (err != 0)
813                 return (err);
814 retry:
815         err = zap_leaf_lookup(l, zn, &zeh);
816         if (err == 0) {
817                 err = SET_ERROR(EEXIST);
818                 goto out;
819         }
820         if (err != ENOENT)
821                 goto out;
822
823         err = zap_entry_create(l, zn, cd,
824             integer_size, num_integers, val, &zeh);
825
826         if (err == 0) {
827                 zap_increment_num_entries(zap, 1, tx);
828         } else if (err == EAGAIN) {
829                 err = zap_expand_leaf(zn, l, tag, tx, &l);
830                 zap = zn->zn_zap;       /* zap_expand_leaf() may change zap */
831                 if (err == 0)
832                         goto retry;
833         }
834
835 out:
836         if (zap != NULL)
837                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
838         return (err);
839 }
840
841 int
842 fzap_add(zap_name_t *zn,
843     uint64_t integer_size, uint64_t num_integers,
844     const void *val, void *tag, dmu_tx_t *tx)
845 {
846         int err = fzap_check(zn, integer_size, num_integers);
847         if (err != 0)
848                 return (err);
849
850         return (fzap_add_cd(zn, integer_size, num_integers,
851             val, ZAP_NEED_CD, tag, tx));
852 }
853
854 int
855 fzap_update(zap_name_t *zn,
856     int integer_size, uint64_t num_integers, const void *val,
857     void *tag, dmu_tx_t *tx)
858 {
859         zap_leaf_t *l;
860         int err, create;
861         zap_entry_handle_t zeh;
862         zap_t *zap = zn->zn_zap;
863
864         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
865         err = fzap_check(zn, integer_size, num_integers);
866         if (err != 0)
867                 return (err);
868
869         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
870         if (err != 0)
871                 return (err);
872 retry:
873         err = zap_leaf_lookup(l, zn, &zeh);
874         create = (err == ENOENT);
875         ASSERT(err == 0 || err == ENOENT);
876
877         if (create) {
878                 err = zap_entry_create(l, zn, ZAP_NEED_CD,
879                     integer_size, num_integers, val, &zeh);
880                 if (err == 0)
881                         zap_increment_num_entries(zap, 1, tx);
882         } else {
883                 err = zap_entry_update(&zeh, integer_size, num_integers, val);
884         }
885
886         if (err == EAGAIN) {
887                 err = zap_expand_leaf(zn, l, tag, tx, &l);
888                 zap = zn->zn_zap;       /* zap_expand_leaf() may change zap */
889                 if (err == 0)
890                         goto retry;
891         }
892
893         if (zap != NULL)
894                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tag, tx);
895         return (err);
896 }
897
898 int
899 fzap_length(zap_name_t *zn,
900     uint64_t *integer_size, uint64_t *num_integers)
901 {
902         zap_leaf_t *l;
903         int err;
904         zap_entry_handle_t zeh;
905
906         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
907         if (err != 0)
908                 return (err);
909         err = zap_leaf_lookup(l, zn, &zeh);
910         if (err != 0)
911                 goto out;
912
913         if (integer_size)
914                 *integer_size = zeh.zeh_integer_size;
915         if (num_integers)
916                 *num_integers = zeh.zeh_num_integers;
917 out:
918         zap_put_leaf(l);
919         return (err);
920 }
921
922 int
923 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
924 {
925         zap_leaf_t *l;
926         int err;
927         zap_entry_handle_t zeh;
928
929         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
930         if (err != 0)
931                 return (err);
932         err = zap_leaf_lookup(l, zn, &zeh);
933         if (err == 0) {
934                 zap_entry_remove(&zeh);
935                 zap_increment_num_entries(zn->zn_zap, -1, tx);
936         }
937         zap_put_leaf(l);
938         return (err);
939 }
940
941 void
942 fzap_prefetch(zap_name_t *zn)
943 {
944         uint64_t idx, blk;
945         zap_t *zap = zn->zn_zap;
946         int bs;
947
948         idx = ZAP_HASH_IDX(zn->zn_hash,
949             zap_f_phys(zap)->zap_ptrtbl.zt_shift);
950         if (zap_idx_to_blk(zap, idx, &blk) != 0)
951                 return;
952         bs = FZAP_BLOCK_SHIFT(zap);
953         dmu_prefetch(zap->zap_objset, zap->zap_object, 0, blk << bs, 1 << bs,
954             ZIO_PRIORITY_SYNC_READ);
955 }
956
957 /*
958  * Helper functions for consumers.
959  */
960
961 uint64_t
962 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
963     const char *name, dmu_tx_t *tx)
964 {
965         uint64_t new_obj;
966
967         VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
968         VERIFY0(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
969             tx));
970
971         return (new_obj);
972 }
973
974 int
975 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
976     char *name)
977 {
978         zap_cursor_t zc;
979         zap_attribute_t *za;
980         int err;
981
982         if (mask == 0)
983                 mask = -1ULL;
984
985         za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
986         for (zap_cursor_init(&zc, os, zapobj);
987             (err = zap_cursor_retrieve(&zc, za)) == 0;
988             zap_cursor_advance(&zc)) {
989                 if ((za->za_first_integer & mask) == (value & mask)) {
990                         (void) strcpy(name, za->za_name);
991                         break;
992                 }
993         }
994         zap_cursor_fini(&zc);
995         kmem_free(za, sizeof (zap_attribute_t));
996         return (err);
997 }
998
999 int
1000 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
1001 {
1002         zap_cursor_t zc;
1003         zap_attribute_t za;
1004         int err;
1005
1006         err = 0;
1007         for (zap_cursor_init(&zc, os, fromobj);
1008             zap_cursor_retrieve(&zc, &za) == 0;
1009             (void) zap_cursor_advance(&zc)) {
1010                 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1011                         err = SET_ERROR(EINVAL);
1012                         break;
1013                 }
1014                 err = zap_add(os, intoobj, za.za_name,
1015                     8, 1, &za.za_first_integer, tx);
1016                 if (err)
1017                         break;
1018         }
1019         zap_cursor_fini(&zc);
1020         return (err);
1021 }
1022
1023 int
1024 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1025     uint64_t value, dmu_tx_t *tx)
1026 {
1027         zap_cursor_t zc;
1028         zap_attribute_t za;
1029         int err;
1030
1031         err = 0;
1032         for (zap_cursor_init(&zc, os, fromobj);
1033             zap_cursor_retrieve(&zc, &za) == 0;
1034             (void) zap_cursor_advance(&zc)) {
1035                 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1036                         err = SET_ERROR(EINVAL);
1037                         break;
1038                 }
1039                 err = zap_add(os, intoobj, za.za_name,
1040                     8, 1, &value, tx);
1041                 if (err)
1042                         break;
1043         }
1044         zap_cursor_fini(&zc);
1045         return (err);
1046 }
1047
1048 int
1049 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1050     dmu_tx_t *tx)
1051 {
1052         zap_cursor_t zc;
1053         zap_attribute_t za;
1054         int err;
1055
1056         err = 0;
1057         for (zap_cursor_init(&zc, os, fromobj);
1058             zap_cursor_retrieve(&zc, &za) == 0;
1059             (void) zap_cursor_advance(&zc)) {
1060                 uint64_t delta = 0;
1061
1062                 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1063                         err = SET_ERROR(EINVAL);
1064                         break;
1065                 }
1066
1067                 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1068                 if (err != 0 && err != ENOENT)
1069                         break;
1070                 delta += za.za_first_integer;
1071                 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1072                 if (err)
1073                         break;
1074         }
1075         zap_cursor_fini(&zc);
1076         return (err);
1077 }
1078
1079 int
1080 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1081 {
1082         char name[20];
1083
1084         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1085         return (zap_add(os, obj, name, 8, 1, &value, tx));
1086 }
1087
1088 int
1089 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1090 {
1091         char name[20];
1092
1093         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1094         return (zap_remove(os, obj, name, tx));
1095 }
1096
1097 int
1098 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1099 {
1100         char name[20];
1101
1102         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1103         return (zap_lookup(os, obj, name, 8, 1, &value));
1104 }
1105
1106 int
1107 zap_add_int_key(objset_t *os, uint64_t obj,
1108     uint64_t key, uint64_t value, dmu_tx_t *tx)
1109 {
1110         char name[20];
1111
1112         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1113         return (zap_add(os, obj, name, 8, 1, &value, tx));
1114 }
1115
1116 int
1117 zap_update_int_key(objset_t *os, uint64_t obj,
1118     uint64_t key, uint64_t value, dmu_tx_t *tx)
1119 {
1120         char name[20];
1121
1122         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1123         return (zap_update(os, obj, name, 8, 1, &value, tx));
1124 }
1125
1126 int
1127 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1128 {
1129         char name[20];
1130
1131         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1132         return (zap_lookup(os, obj, name, 8, 1, valuep));
1133 }
1134
1135 int
1136 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1137     dmu_tx_t *tx)
1138 {
1139         uint64_t value = 0;
1140         int err;
1141
1142         if (delta == 0)
1143                 return (0);
1144
1145         err = zap_lookup(os, obj, name, 8, 1, &value);
1146         if (err != 0 && err != ENOENT)
1147                 return (err);
1148         value += delta;
1149         if (value == 0)
1150                 err = zap_remove(os, obj, name, tx);
1151         else
1152                 err = zap_update(os, obj, name, 8, 1, &value, tx);
1153         return (err);
1154 }
1155
1156 int
1157 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1158     dmu_tx_t *tx)
1159 {
1160         char name[20];
1161
1162         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1163         return (zap_increment(os, obj, name, delta, tx));
1164 }
1165
1166 /*
1167  * Routines for iterating over the attributes.
1168  */
1169
1170 int
1171 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1172 {
1173         int err = ENOENT;
1174         zap_entry_handle_t zeh;
1175         zap_leaf_t *l;
1176
1177         /* retrieve the next entry at or after zc_hash/zc_cd */
1178         /* if no entry, return ENOENT */
1179
1180         if (zc->zc_leaf &&
1181             (ZAP_HASH_IDX(zc->zc_hash,
1182             zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1183             zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1184                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1185                 zap_put_leaf(zc->zc_leaf);
1186                 zc->zc_leaf = NULL;
1187         }
1188
1189 again:
1190         if (zc->zc_leaf == NULL) {
1191                 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1192                     &zc->zc_leaf);
1193                 if (err != 0)
1194                         return (err);
1195         } else {
1196                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1197         }
1198         l = zc->zc_leaf;
1199
1200         err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1201
1202         if (err == ENOENT) {
1203                 uint64_t nocare =
1204                     (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1205                 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1206                 zc->zc_cd = 0;
1207                 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1208                     zc->zc_hash == 0) {
1209                         zc->zc_hash = -1ULL;
1210                 } else {
1211                         zap_put_leaf(zc->zc_leaf);
1212                         zc->zc_leaf = NULL;
1213                         goto again;
1214                 }
1215         }
1216
1217         if (err == 0) {
1218                 zc->zc_hash = zeh.zeh_hash;
1219                 zc->zc_cd = zeh.zeh_cd;
1220                 za->za_integer_length = zeh.zeh_integer_size;
1221                 za->za_num_integers = zeh.zeh_num_integers;
1222                 if (zeh.zeh_num_integers == 0) {
1223                         za->za_first_integer = 0;
1224                 } else {
1225                         err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1226                         ASSERT(err == 0 || err == EOVERFLOW);
1227                 }
1228                 err = zap_entry_read_name(zap, &zeh,
1229                     sizeof (za->za_name), za->za_name);
1230                 ASSERT(err == 0);
1231
1232                 za->za_normalization_conflict =
1233                     zap_entry_normalization_conflict(&zeh,
1234                     NULL, za->za_name, zap);
1235         }
1236         rw_exit(&zc->zc_leaf->l_rwlock);
1237         return (err);
1238 }
1239
1240 static void
1241 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1242 {
1243         int i, err;
1244         uint64_t lastblk = 0;
1245
1246         /*
1247          * NB: if a leaf has more pointers than an entire ptrtbl block
1248          * can hold, then it'll be accounted for more than once, since
1249          * we won't have lastblk.
1250          */
1251         for (i = 0; i < len; i++) {
1252                 zap_leaf_t *l;
1253
1254                 if (tbl[i] == lastblk)
1255                         continue;
1256                 lastblk = tbl[i];
1257
1258                 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1259                 if (err == 0) {
1260                         zap_leaf_stats(zap, l, zs);
1261                         zap_put_leaf(l);
1262                 }
1263         }
1264 }
1265
1266 int
1267 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1268 {
1269         int err;
1270         zap_leaf_t *l;
1271         zap_entry_handle_t zeh;
1272
1273         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1274                 return (SET_ERROR(ENAMETOOLONG));
1275
1276         err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1277         if (err != 0)
1278                 return (err);
1279
1280         err = zap_leaf_lookup(l, zn, &zeh);
1281         if (err != 0)
1282                 return (err);
1283
1284         zc->zc_leaf = l;
1285         zc->zc_hash = zeh.zeh_hash;
1286         zc->zc_cd = zeh.zeh_cd;
1287
1288         return (err);
1289 }
1290
1291 void
1292 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1293 {
1294         int bs = FZAP_BLOCK_SHIFT(zap);
1295         zs->zs_blocksize = 1ULL << bs;
1296
1297         /*
1298          * Set zap_phys_t fields
1299          */
1300         zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1301         zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1302         zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1303         zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1304         zs->zs_magic = zap_f_phys(zap)->zap_magic;
1305         zs->zs_salt = zap_f_phys(zap)->zap_salt;
1306
1307         /*
1308          * Set zap_ptrtbl fields
1309          */
1310         zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1311         zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1312         zs->zs_ptrtbl_blks_copied =
1313             zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1314         zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1315         zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1316         zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1317
1318         if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1319                 /* the ptrtbl is entirely in the header block. */
1320                 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1321                     1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1322         } else {
1323                 int b;
1324
1325                 dmu_prefetch(zap->zap_objset, zap->zap_object, 0,
1326                     zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1327                     zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs,
1328                     ZIO_PRIORITY_SYNC_READ);
1329
1330                 for (b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1331                     b++) {
1332                         dmu_buf_t *db;
1333                         int err;
1334
1335                         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1336                             (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1337                             FTAG, &db, DMU_READ_NO_PREFETCH);
1338                         if (err == 0) {
1339                                 zap_stats_ptrtbl(zap, db->db_data,
1340                                     1<<(bs-3), zs);
1341                                 dmu_buf_rele(db, FTAG);
1342                         }
1343                 }
1344         }
1345 }
1346
1347 int
1348 fzap_count_write(zap_name_t *zn, int add, refcount_t *towrite,
1349     refcount_t *tooverwrite)
1350 {
1351         zap_t *zap = zn->zn_zap;
1352         zap_leaf_t *l;
1353         int err;
1354
1355         /*
1356          * Account for the header block of the fatzap.
1357          */
1358         if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
1359                 (void) refcount_add_many(tooverwrite,
1360                     zap->zap_dbuf->db_size, FTAG);
1361         } else {
1362                 (void) refcount_add_many(towrite,
1363                     zap->zap_dbuf->db_size, FTAG);
1364         }
1365
1366         /*
1367          * Account for the pointer table blocks.
1368          * If we are adding we need to account for the following cases :
1369          * - If the pointer table is embedded, this operation could force an
1370          *   external pointer table.
1371          * - If this already has an external pointer table this operation
1372          *   could extend the table.
1373          */
1374         if (add) {
1375                 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
1376                         (void) refcount_add_many(towrite,
1377                             zap->zap_dbuf->db_size, FTAG);
1378                 } else {
1379                         (void) refcount_add_many(towrite,
1380                             zap->zap_dbuf->db_size * 3, FTAG);
1381                 }
1382         }
1383
1384         /*
1385          * Now, check if the block containing leaf is freeable
1386          * and account accordingly.
1387          */
1388         err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1389         if (err != 0) {
1390                 return (err);
1391         }
1392
1393         if (!add && dmu_buf_freeable(l->l_dbuf)) {
1394                 (void) refcount_add_many(tooverwrite, l->l_dbuf->db_size, FTAG);
1395         } else {
1396                 /*
1397                  * If this an add operation, the leaf block could split.
1398                  * Hence, we need to account for an additional leaf block.
1399                  */
1400                 (void) refcount_add_many(towrite,
1401                     (add ? 2 : 1) * l->l_dbuf->db_size, FTAG);
1402         }
1403
1404         zap_put_leaf(l);
1405         return (0);
1406 }