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