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