]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zap_micro.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zap_micro.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) 2011, 2014 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25  */
26
27 #include <sys/zio.h>
28 #include <sys/spa.h>
29 #include <sys/dmu.h>
30 #include <sys/zfs_context.h>
31 #include <sys/zap.h>
32 #include <sys/refcount.h>
33 #include <sys/zap_impl.h>
34 #include <sys/zap_leaf.h>
35 #include <sys/avl.h>
36 #include <sys/arc.h>
37 #include <sys/dmu_objset.h>
38
39 #ifdef _KERNEL
40 #include <sys/sunddi.h>
41 #endif
42
43 extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
44
45 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags);
46
47 uint64_t
48 zap_getflags(zap_t *zap)
49 {
50         if (zap->zap_ismicro)
51                 return (0);
52         return (zap_f_phys(zap)->zap_flags);
53 }
54
55 int
56 zap_hashbits(zap_t *zap)
57 {
58         if (zap_getflags(zap) & ZAP_FLAG_HASH64)
59                 return (48);
60         else
61                 return (28);
62 }
63
64 uint32_t
65 zap_maxcd(zap_t *zap)
66 {
67         if (zap_getflags(zap) & ZAP_FLAG_HASH64)
68                 return ((1<<16)-1);
69         else
70                 return (-1U);
71 }
72
73 static uint64_t
74 zap_hash(zap_name_t *zn)
75 {
76         zap_t *zap = zn->zn_zap;
77         uint64_t h = 0;
78
79         if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
80                 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
81                 h = *(uint64_t *)zn->zn_key_orig;
82         } else {
83                 h = zap->zap_salt;
84                 ASSERT(h != 0);
85                 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
86
87                 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
88                         int i;
89                         const uint64_t *wp = zn->zn_key_norm;
90
91                         ASSERT(zn->zn_key_intlen == 8);
92                         for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
93                                 int j;
94                                 uint64_t word = *wp;
95
96                                 for (j = 0; j < zn->zn_key_intlen; j++) {
97                                         h = (h >> 8) ^
98                                             zfs_crc64_table[(h ^ word) & 0xFF];
99                                         word >>= NBBY;
100                                 }
101                         }
102                 } else {
103                         int i, len;
104                         const uint8_t *cp = zn->zn_key_norm;
105
106                         /*
107                          * We previously stored the terminating null on
108                          * disk, but didn't hash it, so we need to
109                          * continue to not hash it.  (The
110                          * zn_key_*_numints includes the terminating
111                          * null for non-binary keys.)
112                          */
113                         len = zn->zn_key_norm_numints - 1;
114
115                         ASSERT(zn->zn_key_intlen == 1);
116                         for (i = 0; i < len; cp++, i++) {
117                                 h = (h >> 8) ^
118                                     zfs_crc64_table[(h ^ *cp) & 0xFF];
119                         }
120                 }
121         }
122         /*
123          * Don't use all 64 bits, since we need some in the cookie for
124          * the collision differentiator.  We MUST use the high bits,
125          * since those are the ones that we first pay attention to when
126          * chosing the bucket.
127          */
128         h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
129
130         return (h);
131 }
132
133 static int
134 zap_normalize(zap_t *zap, const char *name, char *namenorm)
135 {
136         size_t inlen, outlen;
137         int err;
138
139         ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
140
141         inlen = strlen(name) + 1;
142         outlen = ZAP_MAXNAMELEN;
143
144         err = 0;
145         (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
146             zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
147             U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
148
149         return (err);
150 }
151
152 boolean_t
153 zap_match(zap_name_t *zn, const char *matchname)
154 {
155         ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
156
157         if (zn->zn_matchtype == MT_FIRST) {
158                 char norm[ZAP_MAXNAMELEN];
159
160                 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
161                         return (B_FALSE);
162
163                 return (strcmp(zn->zn_key_norm, norm) == 0);
164         } else {
165                 /* MT_BEST or MT_EXACT */
166                 return (strcmp(zn->zn_key_orig, matchname) == 0);
167         }
168 }
169
170 void
171 zap_name_free(zap_name_t *zn)
172 {
173         kmem_free(zn, sizeof (zap_name_t));
174 }
175
176 zap_name_t *
177 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
178 {
179         zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
180
181         zn->zn_zap = zap;
182         zn->zn_key_intlen = sizeof (*key);
183         zn->zn_key_orig = key;
184         zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
185         zn->zn_matchtype = mt;
186         if (zap->zap_normflags) {
187                 if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
188                         zap_name_free(zn);
189                         return (NULL);
190                 }
191                 zn->zn_key_norm = zn->zn_normbuf;
192                 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
193         } else {
194                 if (mt != MT_EXACT) {
195                         zap_name_free(zn);
196                         return (NULL);
197                 }
198                 zn->zn_key_norm = zn->zn_key_orig;
199                 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
200         }
201
202         zn->zn_hash = zap_hash(zn);
203         return (zn);
204 }
205
206 zap_name_t *
207 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
208 {
209         zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
210
211         ASSERT(zap->zap_normflags == 0);
212         zn->zn_zap = zap;
213         zn->zn_key_intlen = sizeof (*key);
214         zn->zn_key_orig = zn->zn_key_norm = key;
215         zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
216         zn->zn_matchtype = MT_EXACT;
217
218         zn->zn_hash = zap_hash(zn);
219         return (zn);
220 }
221
222 static void
223 mzap_byteswap(mzap_phys_t *buf, size_t size)
224 {
225         int i, max;
226         buf->mz_block_type = BSWAP_64(buf->mz_block_type);
227         buf->mz_salt = BSWAP_64(buf->mz_salt);
228         buf->mz_normflags = BSWAP_64(buf->mz_normflags);
229         max = (size / MZAP_ENT_LEN) - 1;
230         for (i = 0; i < max; i++) {
231                 buf->mz_chunk[i].mze_value =
232                     BSWAP_64(buf->mz_chunk[i].mze_value);
233                 buf->mz_chunk[i].mze_cd =
234                     BSWAP_32(buf->mz_chunk[i].mze_cd);
235         }
236 }
237
238 void
239 zap_byteswap(void *buf, size_t size)
240 {
241         uint64_t block_type;
242
243         block_type = *(uint64_t *)buf;
244
245         if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
246                 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
247                 mzap_byteswap(buf, size);
248         } else {
249                 fzap_byteswap(buf, size);
250         }
251 }
252
253 static int
254 mze_compare(const void *arg1, const void *arg2)
255 {
256         const mzap_ent_t *mze1 = arg1;
257         const mzap_ent_t *mze2 = arg2;
258
259         if (mze1->mze_hash > mze2->mze_hash)
260                 return (+1);
261         if (mze1->mze_hash < mze2->mze_hash)
262                 return (-1);
263         if (mze1->mze_cd > mze2->mze_cd)
264                 return (+1);
265         if (mze1->mze_cd < mze2->mze_cd)
266                 return (-1);
267         return (0);
268 }
269
270 static int
271 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
272 {
273         mzap_ent_t *mze;
274         avl_index_t idx;
275
276         ASSERT(zap->zap_ismicro);
277         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
278
279         mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
280         mze->mze_chunkid = chunkid;
281         mze->mze_hash = hash;
282         mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
283         ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
284         if (avl_find(&zap->zap_m.zap_avl, mze, &idx) != NULL) {
285                 kmem_free(mze, sizeof (mzap_ent_t));
286                 return (EEXIST);
287         }
288         avl_insert(&zap->zap_m.zap_avl, mze, idx);
289         return (0);
290 }
291
292 static mzap_ent_t *
293 mze_find(zap_name_t *zn)
294 {
295         mzap_ent_t mze_tofind;
296         mzap_ent_t *mze;
297         avl_index_t idx;
298         avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
299
300         ASSERT(zn->zn_zap->zap_ismicro);
301         ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
302
303         mze_tofind.mze_hash = zn->zn_hash;
304         mze_tofind.mze_cd = 0;
305
306 again:
307         mze = avl_find(avl, &mze_tofind, &idx);
308         if (mze == NULL)
309                 mze = avl_nearest(avl, idx, AVL_AFTER);
310         for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
311                 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
312                 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
313                         return (mze);
314         }
315         if (zn->zn_matchtype == MT_BEST) {
316                 zn->zn_matchtype = MT_FIRST;
317                 goto again;
318         }
319         return (NULL);
320 }
321
322 static uint32_t
323 mze_find_unused_cd(zap_t *zap, uint64_t hash)
324 {
325         mzap_ent_t mze_tofind;
326         mzap_ent_t *mze;
327         avl_index_t idx;
328         avl_tree_t *avl = &zap->zap_m.zap_avl;
329         uint32_t cd;
330
331         ASSERT(zap->zap_ismicro);
332         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
333
334         mze_tofind.mze_hash = hash;
335         mze_tofind.mze_cd = 0;
336
337         cd = 0;
338         for (mze = avl_find(avl, &mze_tofind, &idx);
339             mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
340                 if (mze->mze_cd != cd)
341                         break;
342                 cd++;
343         }
344
345         return (cd);
346 }
347
348 static void
349 mze_remove(zap_t *zap, mzap_ent_t *mze)
350 {
351         ASSERT(zap->zap_ismicro);
352         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
353
354         avl_remove(&zap->zap_m.zap_avl, mze);
355         kmem_free(mze, sizeof (mzap_ent_t));
356 }
357
358 static void
359 mze_destroy(zap_t *zap)
360 {
361         mzap_ent_t *mze;
362         void *avlcookie = NULL;
363
364         while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
365                 kmem_free(mze, sizeof (mzap_ent_t));
366         avl_destroy(&zap->zap_m.zap_avl);
367 }
368
369 static zap_t *
370 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
371 {
372         zap_t *winner;
373         zap_t *zap;
374         int i;
375
376         ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
377
378         zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
379         rw_init(&zap->zap_rwlock, 0, 0, 0);
380         rw_enter(&zap->zap_rwlock, RW_WRITER);
381         zap->zap_objset = os;
382         zap->zap_object = obj;
383         zap->zap_dbuf = db;
384
385         if (*(uint64_t *)db->db_data != ZBT_MICRO) {
386                 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
387                 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
388         } else {
389                 zap->zap_ismicro = TRUE;
390         }
391
392         /*
393          * Make sure that zap_ismicro is set before we let others see
394          * it, because zap_lockdir() checks zap_ismicro without the lock
395          * held.
396          */
397         dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
398         winner = dmu_buf_set_user(db, &zap->zap_dbu);
399
400         if (winner != NULL) {
401                 rw_exit(&zap->zap_rwlock);
402                 rw_destroy(&zap->zap_rwlock);
403                 if (!zap->zap_ismicro)
404                         mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
405                 kmem_free(zap, sizeof (zap_t));
406                 return (winner);
407         }
408
409         if (zap->zap_ismicro) {
410                 zap->zap_salt = zap_m_phys(zap)->mz_salt;
411                 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
412                 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
413                 avl_create(&zap->zap_m.zap_avl, mze_compare,
414                     sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
415
416                 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
417                         mzap_ent_phys_t *mze =
418                             &zap_m_phys(zap)->mz_chunk[i];
419                         if (mze->mze_name[0]) {
420                                 zap_name_t *zn;
421
422                                 zn = zap_name_alloc(zap, mze->mze_name,
423                                     MT_EXACT);
424                                 if (mze_insert(zap, i, zn->zn_hash) == 0)
425                                         zap->zap_m.zap_num_entries++;
426                                 else {
427                                         printf("ZFS WARNING: Duplicated ZAP "
428                                             "entry detected (%s).\n",
429                                             mze->mze_name);
430                                 }
431                                 zap_name_free(zn);
432                         }
433                 }
434         } else {
435                 zap->zap_salt = zap_f_phys(zap)->zap_salt;
436                 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
437
438                 ASSERT3U(sizeof (struct zap_leaf_header), ==,
439                     2*ZAP_LEAF_CHUNKSIZE);
440
441                 /*
442                  * The embedded pointer table should not overlap the
443                  * other members.
444                  */
445                 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
446                     &zap_f_phys(zap)->zap_salt);
447
448                 /*
449                  * The embedded pointer table should end at the end of
450                  * the block
451                  */
452                 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
453                     1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
454                     (uintptr_t)zap_f_phys(zap), ==,
455                     zap->zap_dbuf->db_size);
456         }
457         rw_exit(&zap->zap_rwlock);
458         return (zap);
459 }
460
461 int
462 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
463     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
464 {
465         zap_t *zap;
466         dmu_buf_t *db;
467         krw_t lt;
468         int err;
469
470         *zapp = NULL;
471
472         err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
473         if (err)
474                 return (err);
475
476 #ifdef ZFS_DEBUG
477         {
478                 dmu_object_info_t doi;
479                 dmu_object_info_from_db(db, &doi);
480                 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
481         }
482 #endif
483
484         zap = dmu_buf_get_user(db);
485         if (zap == NULL)
486                 zap = mzap_open(os, obj, db);
487
488         /*
489          * We're checking zap_ismicro without the lock held, in order to
490          * tell what type of lock we want.  Once we have some sort of
491          * lock, see if it really is the right type.  In practice this
492          * can only be different if it was upgraded from micro to fat,
493          * and micro wanted WRITER but fat only needs READER.
494          */
495         lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
496         rw_enter(&zap->zap_rwlock, lt);
497         if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
498                 /* it was upgraded, now we only need reader */
499                 ASSERT(lt == RW_WRITER);
500                 ASSERT(RW_READER ==
501                     (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
502                 rw_downgrade(&zap->zap_rwlock);
503                 lt = RW_READER;
504         }
505
506         zap->zap_objset = os;
507
508         if (lt == RW_WRITER)
509                 dmu_buf_will_dirty(db, tx);
510
511         ASSERT3P(zap->zap_dbuf, ==, db);
512
513         ASSERT(!zap->zap_ismicro ||
514             zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
515         if (zap->zap_ismicro && tx && adding &&
516             zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
517                 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
518                 if (newsz > MZAP_MAX_BLKSZ) {
519                         dprintf("upgrading obj %llu: num_entries=%u\n",
520                             obj, zap->zap_m.zap_num_entries);
521                         *zapp = zap;
522                         return (mzap_upgrade(zapp, tx, 0));
523                 }
524                 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
525                 ASSERT0(err);
526                 zap->zap_m.zap_num_chunks =
527                     db->db_size / MZAP_ENT_LEN - 1;
528         }
529
530         *zapp = zap;
531         return (0);
532 }
533
534 void
535 zap_unlockdir(zap_t *zap)
536 {
537         rw_exit(&zap->zap_rwlock);
538         dmu_buf_rele(zap->zap_dbuf, NULL);
539 }
540
541 static int
542 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
543 {
544         mzap_phys_t *mzp;
545         int i, sz, nchunks;
546         int err = 0;
547         zap_t *zap = *zapp;
548
549         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
550
551         sz = zap->zap_dbuf->db_size;
552         mzp = zio_buf_alloc(sz);
553         bcopy(zap->zap_dbuf->db_data, mzp, sz);
554         nchunks = zap->zap_m.zap_num_chunks;
555
556         if (!flags) {
557                 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
558                     1ULL << fzap_default_block_shift, 0, tx);
559                 if (err) {
560                         zio_buf_free(mzp, sz);
561                         return (err);
562                 }
563         }
564
565         dprintf("upgrading obj=%llu with %u chunks\n",
566             zap->zap_object, nchunks);
567         /* XXX destroy the avl later, so we can use the stored hash value */
568         mze_destroy(zap);
569
570         fzap_upgrade(zap, tx, flags);
571
572         for (i = 0; i < nchunks; i++) {
573                 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
574                 zap_name_t *zn;
575                 if (mze->mze_name[0] == 0)
576                         continue;
577                 dprintf("adding %s=%llu\n",
578                     mze->mze_name, mze->mze_value);
579                 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
580                 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
581                 zap = zn->zn_zap;       /* fzap_add_cd() may change zap */
582                 zap_name_free(zn);
583                 if (err)
584                         break;
585         }
586         zio_buf_free(mzp, sz);
587         *zapp = zap;
588         return (err);
589 }
590
591 void
592 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
593     dmu_tx_t *tx)
594 {
595         dmu_buf_t *db;
596         mzap_phys_t *zp;
597
598         VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
599
600 #ifdef ZFS_DEBUG
601         {
602                 dmu_object_info_t doi;
603                 dmu_object_info_from_db(db, &doi);
604                 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
605         }
606 #endif
607
608         dmu_buf_will_dirty(db, tx);
609         zp = db->db_data;
610         zp->mz_block_type = ZBT_MICRO;
611         zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
612         zp->mz_normflags = normflags;
613         dmu_buf_rele(db, FTAG);
614
615         if (flags != 0) {
616                 zap_t *zap;
617                 /* Only fat zap supports flags; upgrade immediately. */
618                 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
619                     B_FALSE, B_FALSE, &zap));
620                 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
621                 zap_unlockdir(zap);
622         }
623 }
624
625 int
626 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
627     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
628 {
629         return (zap_create_claim_norm(os, obj,
630             0, ot, bonustype, bonuslen, tx));
631 }
632
633 int
634 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
635     dmu_object_type_t ot,
636     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
637 {
638         int err;
639
640         err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
641         if (err != 0)
642                 return (err);
643         mzap_create_impl(os, obj, normflags, 0, tx);
644         return (0);
645 }
646
647 uint64_t
648 zap_create(objset_t *os, dmu_object_type_t ot,
649     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
650 {
651         return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
652 }
653
654 uint64_t
655 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
656     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
657 {
658         uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
659
660         mzap_create_impl(os, obj, normflags, 0, tx);
661         return (obj);
662 }
663
664 uint64_t
665 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
666     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
667     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
668 {
669         uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
670
671         ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
672             leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
673             indirect_blockshift >= SPA_MINBLOCKSHIFT &&
674             indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
675
676         VERIFY(dmu_object_set_blocksize(os, obj,
677             1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
678
679         mzap_create_impl(os, obj, normflags, flags, tx);
680         return (obj);
681 }
682
683 int
684 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
685 {
686         /*
687          * dmu_object_free will free the object number and free the
688          * data.  Freeing the data will cause our pageout function to be
689          * called, which will destroy our data (zap_leaf_t's and zap_t).
690          */
691
692         return (dmu_object_free(os, zapobj, tx));
693 }
694
695 void
696 zap_evict(void *dbu)
697 {
698         zap_t *zap = dbu;
699
700         rw_destroy(&zap->zap_rwlock);
701
702         if (zap->zap_ismicro)
703                 mze_destroy(zap);
704         else
705                 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
706
707         kmem_free(zap, sizeof (zap_t));
708 }
709
710 int
711 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
712 {
713         zap_t *zap;
714         int err;
715
716         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
717         if (err)
718                 return (err);
719         if (!zap->zap_ismicro) {
720                 err = fzap_count(zap, count);
721         } else {
722                 *count = zap->zap_m.zap_num_entries;
723         }
724         zap_unlockdir(zap);
725         return (err);
726 }
727
728 /*
729  * zn may be NULL; if not specified, it will be computed if needed.
730  * See also the comment above zap_entry_normalization_conflict().
731  */
732 static boolean_t
733 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
734 {
735         mzap_ent_t *other;
736         int direction = AVL_BEFORE;
737         boolean_t allocdzn = B_FALSE;
738
739         if (zap->zap_normflags == 0)
740                 return (B_FALSE);
741
742 again:
743         for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
744             other && other->mze_hash == mze->mze_hash;
745             other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
746
747                 if (zn == NULL) {
748                         zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
749                             MT_FIRST);
750                         allocdzn = B_TRUE;
751                 }
752                 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
753                         if (allocdzn)
754                                 zap_name_free(zn);
755                         return (B_TRUE);
756                 }
757         }
758
759         if (direction == AVL_BEFORE) {
760                 direction = AVL_AFTER;
761                 goto again;
762         }
763
764         if (allocdzn)
765                 zap_name_free(zn);
766         return (B_FALSE);
767 }
768
769 /*
770  * Routines for manipulating attributes.
771  */
772
773 int
774 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
775     uint64_t integer_size, uint64_t num_integers, void *buf)
776 {
777         return (zap_lookup_norm(os, zapobj, name, integer_size,
778             num_integers, buf, MT_EXACT, NULL, 0, NULL));
779 }
780
781 int
782 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
783     uint64_t integer_size, uint64_t num_integers, void *buf,
784     matchtype_t mt, char *realname, int rn_len,
785     boolean_t *ncp)
786 {
787         zap_t *zap;
788         int err;
789         mzap_ent_t *mze;
790         zap_name_t *zn;
791
792         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
793         if (err)
794                 return (err);
795         zn = zap_name_alloc(zap, name, mt);
796         if (zn == NULL) {
797                 zap_unlockdir(zap);
798                 return (SET_ERROR(ENOTSUP));
799         }
800
801         if (!zap->zap_ismicro) {
802                 err = fzap_lookup(zn, integer_size, num_integers, buf,
803                     realname, rn_len, ncp);
804         } else {
805                 mze = mze_find(zn);
806                 if (mze == NULL) {
807                         err = SET_ERROR(ENOENT);
808                 } else {
809                         if (num_integers < 1) {
810                                 err = SET_ERROR(EOVERFLOW);
811                         } else if (integer_size != 8) {
812                                 err = SET_ERROR(EINVAL);
813                         } else {
814                                 *(uint64_t *)buf =
815                                     MZE_PHYS(zap, mze)->mze_value;
816                                 (void) strlcpy(realname,
817                                     MZE_PHYS(zap, mze)->mze_name, rn_len);
818                                 if (ncp) {
819                                         *ncp = mzap_normalization_conflict(zap,
820                                             zn, mze);
821                                 }
822                         }
823                 }
824         }
825         zap_name_free(zn);
826         zap_unlockdir(zap);
827         return (err);
828 }
829
830 int
831 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
832     int key_numints)
833 {
834         zap_t *zap;
835         int err;
836         zap_name_t *zn;
837
838         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
839         if (err)
840                 return (err);
841         zn = zap_name_alloc_uint64(zap, key, key_numints);
842         if (zn == NULL) {
843                 zap_unlockdir(zap);
844                 return (SET_ERROR(ENOTSUP));
845         }
846
847         fzap_prefetch(zn);
848         zap_name_free(zn);
849         zap_unlockdir(zap);
850         return (err);
851 }
852
853 int
854 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
855     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
856 {
857         zap_t *zap;
858         int err;
859         zap_name_t *zn;
860
861         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
862         if (err)
863                 return (err);
864         zn = zap_name_alloc_uint64(zap, key, key_numints);
865         if (zn == NULL) {
866                 zap_unlockdir(zap);
867                 return (SET_ERROR(ENOTSUP));
868         }
869
870         err = fzap_lookup(zn, integer_size, num_integers, buf,
871             NULL, 0, NULL);
872         zap_name_free(zn);
873         zap_unlockdir(zap);
874         return (err);
875 }
876
877 int
878 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
879 {
880         int err = zap_lookup_norm(os, zapobj, name, 0,
881             0, NULL, MT_EXACT, NULL, 0, NULL);
882         if (err == EOVERFLOW || err == EINVAL)
883                 err = 0; /* found, but skipped reading the value */
884         return (err);
885 }
886
887 int
888 zap_length(objset_t *os, uint64_t zapobj, const char *name,
889     uint64_t *integer_size, uint64_t *num_integers)
890 {
891         zap_t *zap;
892         int err;
893         mzap_ent_t *mze;
894         zap_name_t *zn;
895
896         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
897         if (err)
898                 return (err);
899         zn = zap_name_alloc(zap, name, MT_EXACT);
900         if (zn == NULL) {
901                 zap_unlockdir(zap);
902                 return (SET_ERROR(ENOTSUP));
903         }
904         if (!zap->zap_ismicro) {
905                 err = fzap_length(zn, integer_size, num_integers);
906         } else {
907                 mze = mze_find(zn);
908                 if (mze == NULL) {
909                         err = SET_ERROR(ENOENT);
910                 } else {
911                         if (integer_size)
912                                 *integer_size = 8;
913                         if (num_integers)
914                                 *num_integers = 1;
915                 }
916         }
917         zap_name_free(zn);
918         zap_unlockdir(zap);
919         return (err);
920 }
921
922 int
923 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
924     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
925 {
926         zap_t *zap;
927         int err;
928         zap_name_t *zn;
929
930         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
931         if (err)
932                 return (err);
933         zn = zap_name_alloc_uint64(zap, key, key_numints);
934         if (zn == NULL) {
935                 zap_unlockdir(zap);
936                 return (SET_ERROR(ENOTSUP));
937         }
938         err = fzap_length(zn, integer_size, num_integers);
939         zap_name_free(zn);
940         zap_unlockdir(zap);
941         return (err);
942 }
943
944 static void
945 mzap_addent(zap_name_t *zn, uint64_t value)
946 {
947         int i;
948         zap_t *zap = zn->zn_zap;
949         int start = zap->zap_m.zap_alloc_next;
950         uint32_t cd;
951
952         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
953
954 #ifdef ZFS_DEBUG
955         for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
956                 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
957                 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
958         }
959 #endif
960
961         cd = mze_find_unused_cd(zap, zn->zn_hash);
962         /* given the limited size of the microzap, this can't happen */
963         ASSERT(cd < zap_maxcd(zap));
964
965 again:
966         for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
967                 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
968                 if (mze->mze_name[0] == 0) {
969                         mze->mze_value = value;
970                         mze->mze_cd = cd;
971                         (void) strcpy(mze->mze_name, zn->zn_key_orig);
972                         zap->zap_m.zap_num_entries++;
973                         zap->zap_m.zap_alloc_next = i+1;
974                         if (zap->zap_m.zap_alloc_next ==
975                             zap->zap_m.zap_num_chunks)
976                                 zap->zap_m.zap_alloc_next = 0;
977                         VERIFY(0 == mze_insert(zap, i, zn->zn_hash));
978                         return;
979                 }
980         }
981         if (start != 0) {
982                 start = 0;
983                 goto again;
984         }
985         ASSERT(!"out of entries!");
986 }
987
988 int
989 zap_add(objset_t *os, uint64_t zapobj, const char *key,
990     int integer_size, uint64_t num_integers,
991     const void *val, dmu_tx_t *tx)
992 {
993         zap_t *zap;
994         int err;
995         mzap_ent_t *mze;
996         const uint64_t *intval = val;
997         zap_name_t *zn;
998
999         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1000         if (err)
1001                 return (err);
1002         zn = zap_name_alloc(zap, key, MT_EXACT);
1003         if (zn == NULL) {
1004                 zap_unlockdir(zap);
1005                 return (SET_ERROR(ENOTSUP));
1006         }
1007         if (!zap->zap_ismicro) {
1008                 err = fzap_add(zn, integer_size, num_integers, val, tx);
1009                 zap = zn->zn_zap;       /* fzap_add() may change zap */
1010         } else if (integer_size != 8 || num_integers != 1 ||
1011             strlen(key) >= MZAP_NAME_LEN) {
1012                 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1013                 if (err == 0)
1014                         err = fzap_add(zn, integer_size, num_integers, val, tx);
1015                 zap = zn->zn_zap;       /* fzap_add() may change zap */
1016         } else {
1017                 mze = mze_find(zn);
1018                 if (mze != NULL) {
1019                         err = SET_ERROR(EEXIST);
1020                 } else {
1021                         mzap_addent(zn, *intval);
1022                 }
1023         }
1024         ASSERT(zap == zn->zn_zap);
1025         zap_name_free(zn);
1026         if (zap != NULL)        /* may be NULL if fzap_add() failed */
1027                 zap_unlockdir(zap);
1028         return (err);
1029 }
1030
1031 int
1032 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1033     int key_numints, int integer_size, uint64_t num_integers,
1034     const void *val, dmu_tx_t *tx)
1035 {
1036         zap_t *zap;
1037         int err;
1038         zap_name_t *zn;
1039
1040         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1041         if (err)
1042                 return (err);
1043         zn = zap_name_alloc_uint64(zap, key, key_numints);
1044         if (zn == NULL) {
1045                 zap_unlockdir(zap);
1046                 return (SET_ERROR(ENOTSUP));
1047         }
1048         err = fzap_add(zn, integer_size, num_integers, val, tx);
1049         zap = zn->zn_zap;       /* fzap_add() may change zap */
1050         zap_name_free(zn);
1051         if (zap != NULL)        /* may be NULL if fzap_add() failed */
1052                 zap_unlockdir(zap);
1053         return (err);
1054 }
1055
1056 int
1057 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1058     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1059 {
1060         zap_t *zap;
1061         mzap_ent_t *mze;
1062         uint64_t oldval;
1063         const uint64_t *intval = val;
1064         zap_name_t *zn;
1065         int err;
1066
1067 #ifdef ZFS_DEBUG
1068         /*
1069          * If there is an old value, it shouldn't change across the
1070          * lockdir (eg, due to bprewrite's xlation).
1071          */
1072         if (integer_size == 8 && num_integers == 1)
1073                 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1074 #endif
1075
1076         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1077         if (err)
1078                 return (err);
1079         zn = zap_name_alloc(zap, name, MT_EXACT);
1080         if (zn == NULL) {
1081                 zap_unlockdir(zap);
1082                 return (SET_ERROR(ENOTSUP));
1083         }
1084         if (!zap->zap_ismicro) {
1085                 err = fzap_update(zn, integer_size, num_integers, val, tx);
1086                 zap = zn->zn_zap;       /* fzap_update() may change zap */
1087         } else if (integer_size != 8 || num_integers != 1 ||
1088             strlen(name) >= MZAP_NAME_LEN) {
1089                 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1090                     zapobj, integer_size, num_integers, name);
1091                 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1092                 if (err == 0)
1093                         err = fzap_update(zn, integer_size, num_integers,
1094                             val, tx);
1095                 zap = zn->zn_zap;       /* fzap_update() may change zap */
1096         } else {
1097                 mze = mze_find(zn);
1098                 if (mze != NULL) {
1099                         ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1100                         MZE_PHYS(zap, mze)->mze_value = *intval;
1101                 } else {
1102                         mzap_addent(zn, *intval);
1103                 }
1104         }
1105         ASSERT(zap == zn->zn_zap);
1106         zap_name_free(zn);
1107         if (zap != NULL)        /* may be NULL if fzap_upgrade() failed */
1108                 zap_unlockdir(zap);
1109         return (err);
1110 }
1111
1112 int
1113 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1114     int key_numints,
1115     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1116 {
1117         zap_t *zap;
1118         zap_name_t *zn;
1119         int err;
1120
1121         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1122         if (err)
1123                 return (err);
1124         zn = zap_name_alloc_uint64(zap, key, key_numints);
1125         if (zn == NULL) {
1126                 zap_unlockdir(zap);
1127                 return (SET_ERROR(ENOTSUP));
1128         }
1129         err = fzap_update(zn, integer_size, num_integers, val, tx);
1130         zap = zn->zn_zap;       /* fzap_update() may change zap */
1131         zap_name_free(zn);
1132         if (zap != NULL)        /* may be NULL if fzap_upgrade() failed */
1133                 zap_unlockdir(zap);
1134         return (err);
1135 }
1136
1137 int
1138 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1139 {
1140         return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1141 }
1142
1143 int
1144 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1145     matchtype_t mt, dmu_tx_t *tx)
1146 {
1147         zap_t *zap;
1148         int err;
1149         mzap_ent_t *mze;
1150         zap_name_t *zn;
1151
1152         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1153         if (err)
1154                 return (err);
1155         zn = zap_name_alloc(zap, name, mt);
1156         if (zn == NULL) {
1157                 zap_unlockdir(zap);
1158                 return (SET_ERROR(ENOTSUP));
1159         }
1160         if (!zap->zap_ismicro) {
1161                 err = fzap_remove(zn, tx);
1162         } else {
1163                 mze = mze_find(zn);
1164                 if (mze == NULL) {
1165                         err = SET_ERROR(ENOENT);
1166                 } else {
1167                         zap->zap_m.zap_num_entries--;
1168                         bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1169                             sizeof (mzap_ent_phys_t));
1170                         mze_remove(zap, mze);
1171                 }
1172         }
1173         zap_name_free(zn);
1174         zap_unlockdir(zap);
1175         return (err);
1176 }
1177
1178 int
1179 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1180     int key_numints, dmu_tx_t *tx)
1181 {
1182         zap_t *zap;
1183         int err;
1184         zap_name_t *zn;
1185
1186         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1187         if (err)
1188                 return (err);
1189         zn = zap_name_alloc_uint64(zap, key, key_numints);
1190         if (zn == NULL) {
1191                 zap_unlockdir(zap);
1192                 return (SET_ERROR(ENOTSUP));
1193         }
1194         err = fzap_remove(zn, tx);
1195         zap_name_free(zn);
1196         zap_unlockdir(zap);
1197         return (err);
1198 }
1199
1200 /*
1201  * Routines for iterating over the attributes.
1202  */
1203
1204 void
1205 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1206     uint64_t serialized)
1207 {
1208         zc->zc_objset = os;
1209         zc->zc_zap = NULL;
1210         zc->zc_leaf = NULL;
1211         zc->zc_zapobj = zapobj;
1212         zc->zc_serialized = serialized;
1213         zc->zc_hash = 0;
1214         zc->zc_cd = 0;
1215 }
1216
1217 void
1218 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1219 {
1220         zap_cursor_init_serialized(zc, os, zapobj, 0);
1221 }
1222
1223 void
1224 zap_cursor_fini(zap_cursor_t *zc)
1225 {
1226         if (zc->zc_zap) {
1227                 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1228                 zap_unlockdir(zc->zc_zap);
1229                 zc->zc_zap = NULL;
1230         }
1231         if (zc->zc_leaf) {
1232                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1233                 zap_put_leaf(zc->zc_leaf);
1234                 zc->zc_leaf = NULL;
1235         }
1236         zc->zc_objset = NULL;
1237 }
1238
1239 uint64_t
1240 zap_cursor_serialize(zap_cursor_t *zc)
1241 {
1242         if (zc->zc_hash == -1ULL)
1243                 return (-1ULL);
1244         if (zc->zc_zap == NULL)
1245                 return (zc->zc_serialized);
1246         ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1247         ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1248
1249         /*
1250          * We want to keep the high 32 bits of the cursor zero if we can, so
1251          * that 32-bit programs can access this.  So usually use a small
1252          * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1253          * of the cursor.
1254          *
1255          * [ collision differentiator | zap_hashbits()-bit hash value ]
1256          */
1257         return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1258             ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1259 }
1260
1261 int
1262 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1263 {
1264         int err;
1265         avl_index_t idx;
1266         mzap_ent_t mze_tofind;
1267         mzap_ent_t *mze;
1268
1269         if (zc->zc_hash == -1ULL)
1270                 return (SET_ERROR(ENOENT));
1271
1272         if (zc->zc_zap == NULL) {
1273                 int hb;
1274                 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1275                     RW_READER, TRUE, FALSE, &zc->zc_zap);
1276                 if (err)
1277                         return (err);
1278
1279                 /*
1280                  * To support zap_cursor_init_serialized, advance, retrieve,
1281                  * we must add to the existing zc_cd, which may already
1282                  * be 1 due to the zap_cursor_advance.
1283                  */
1284                 ASSERT(zc->zc_hash == 0);
1285                 hb = zap_hashbits(zc->zc_zap);
1286                 zc->zc_hash = zc->zc_serialized << (64 - hb);
1287                 zc->zc_cd += zc->zc_serialized >> hb;
1288                 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1289                         zc->zc_cd = 0;
1290         } else {
1291                 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1292         }
1293         if (!zc->zc_zap->zap_ismicro) {
1294                 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1295         } else {
1296                 mze_tofind.mze_hash = zc->zc_hash;
1297                 mze_tofind.mze_cd = zc->zc_cd;
1298
1299                 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1300                 if (mze == NULL) {
1301                         mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1302                             idx, AVL_AFTER);
1303                 }
1304                 if (mze) {
1305                         mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1306                         ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1307                         za->za_normalization_conflict =
1308                             mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1309                         za->za_integer_length = 8;
1310                         za->za_num_integers = 1;
1311                         za->za_first_integer = mzep->mze_value;
1312                         (void) strcpy(za->za_name, mzep->mze_name);
1313                         zc->zc_hash = mze->mze_hash;
1314                         zc->zc_cd = mze->mze_cd;
1315                         err = 0;
1316                 } else {
1317                         zc->zc_hash = -1ULL;
1318                         err = SET_ERROR(ENOENT);
1319                 }
1320         }
1321         rw_exit(&zc->zc_zap->zap_rwlock);
1322         return (err);
1323 }
1324
1325 void
1326 zap_cursor_advance(zap_cursor_t *zc)
1327 {
1328         if (zc->zc_hash == -1ULL)
1329                 return;
1330         zc->zc_cd++;
1331 }
1332
1333 int
1334 zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt)
1335 {
1336         int err = 0;
1337         mzap_ent_t *mze;
1338         zap_name_t *zn;
1339
1340         if (zc->zc_zap == NULL) {
1341                 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1342                     RW_READER, TRUE, FALSE, &zc->zc_zap);
1343                 if (err)
1344                         return (err);
1345         } else {
1346                 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1347         }
1348
1349         zn = zap_name_alloc(zc->zc_zap, name, mt);
1350         if (zn == NULL) {
1351                 rw_exit(&zc->zc_zap->zap_rwlock);
1352                 return (SET_ERROR(ENOTSUP));
1353         }
1354
1355         if (!zc->zc_zap->zap_ismicro) {
1356                 err = fzap_cursor_move_to_key(zc, zn);
1357         } else {
1358                 mze = mze_find(zn);
1359                 if (mze == NULL) {
1360                         err = SET_ERROR(ENOENT);
1361                         goto out;
1362                 }
1363                 zc->zc_hash = mze->mze_hash;
1364                 zc->zc_cd = mze->mze_cd;
1365         }
1366
1367 out:
1368         zap_name_free(zn);
1369         rw_exit(&zc->zc_zap->zap_rwlock);
1370         return (err);
1371 }
1372
1373 int
1374 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1375 {
1376         int err;
1377         zap_t *zap;
1378
1379         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1380         if (err)
1381                 return (err);
1382
1383         bzero(zs, sizeof (zap_stats_t));
1384
1385         if (zap->zap_ismicro) {
1386                 zs->zs_blocksize = zap->zap_dbuf->db_size;
1387                 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1388                 zs->zs_num_blocks = 1;
1389         } else {
1390                 fzap_get_stats(zap, zs);
1391         }
1392         zap_unlockdir(zap);
1393         return (0);
1394 }
1395
1396 int
1397 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1398     uint64_t *towrite, uint64_t *tooverwrite)
1399 {
1400         zap_t *zap;
1401         int err = 0;
1402
1403         /*
1404          * Since, we don't have a name, we cannot figure out which blocks will
1405          * be affected in this operation. So, account for the worst case :
1406          * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1407          * - 4 new blocks written if adding:
1408          *      - 2 blocks for possibly split leaves,
1409          *      - 2 grown ptrtbl blocks
1410          *
1411          * This also accomodates the case where an add operation to a fairly
1412          * large microzap results in a promotion to fatzap.
1413          */
1414         if (name == NULL) {
1415                 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1416                 return (err);
1417         }
1418
1419         /*
1420          * We lock the zap with adding == FALSE. Because, if we pass
1421          * the actual value of add, it could trigger a mzap_upgrade().
1422          * At present we are just evaluating the possibility of this operation
1423          * and hence we donot want to trigger an upgrade.
1424          */
1425         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1426         if (err)
1427                 return (err);
1428
1429         if (!zap->zap_ismicro) {
1430                 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1431                 if (zn) {
1432                         err = fzap_count_write(zn, add, towrite,
1433                             tooverwrite);
1434                         zap_name_free(zn);
1435                 } else {
1436                         /*
1437                          * We treat this case as similar to (name == NULL)
1438                          */
1439                         *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1440                 }
1441         } else {
1442                 /*
1443                  * We are here if (name != NULL) and this is a micro-zap.
1444                  * We account for the header block depending on whether it
1445                  * is freeable.
1446                  *
1447                  * Incase of an add-operation it is hard to find out
1448                  * if this add will promote this microzap to fatzap.
1449                  * Hence, we consider the worst case and account for the
1450                  * blocks assuming this microzap would be promoted to a
1451                  * fatzap.
1452                  *
1453                  * 1 block overwritten  : header block
1454                  * 4 new blocks written : 2 new split leaf, 2 grown
1455                  *                      ptrtbl blocks
1456                  */
1457                 if (dmu_buf_freeable(zap->zap_dbuf))
1458                         *tooverwrite += MZAP_MAX_BLKSZ;
1459                 else
1460                         *towrite += MZAP_MAX_BLKSZ;
1461
1462                 if (add) {
1463                         *towrite += 4 * MZAP_MAX_BLKSZ;
1464                 }
1465         }
1466
1467         zap_unlockdir(zap);
1468         return (err);
1469 }