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