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