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