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