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