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