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