]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zap_micro.c
OpenZFS 6842 - Fix empty xattr dir causing lockup
[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 * 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 uint64_t *zap_hdr = (uint64_t *)db->db_data;
370 uint64_t zap_block_type = zap_hdr[0];
371 uint64_t zap_magic = zap_hdr[1];
372
373 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
374
375 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
376 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
377 rw_enter(&zap->zap_rwlock, RW_WRITER);
378 zap->zap_objset = os;
379 zap->zap_object = obj;
380 zap->zap_dbuf = db;
381
382 if (zap_block_type != ZBT_MICRO) {
383 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
384 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
385 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
386 winner = NULL; /* No actual winner here... */
387 goto handle_winner;
388 }
389 } else {
390 zap->zap_ismicro = TRUE;
391 }
392
393 /*
394 * Make sure that zap_ismicro is set before we let others see
395 * it, because zap_lockdir() checks zap_ismicro without the lock
396 * held.
397 */
398 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
399 winner = dmu_buf_set_user(db, &zap->zap_dbu);
400
401 if (winner != NULL)
402 goto handle_winner;
403
404 if (zap->zap_ismicro) {
405 zap->zap_salt = zap_m_phys(zap)->mz_salt;
406 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
407 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
408 avl_create(&zap->zap_m.zap_avl, mze_compare,
409 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
410
411 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
412 mzap_ent_phys_t *mze =
413 &zap_m_phys(zap)->mz_chunk[i];
414 if (mze->mze_name[0]) {
415 zap_name_t *zn;
416
417 zap->zap_m.zap_num_entries++;
418 zn = zap_name_alloc(zap, mze->mze_name,
419 MT_EXACT);
420 mze_insert(zap, i, zn->zn_hash);
421 zap_name_free(zn);
422 }
423 }
424 } else {
425 zap->zap_salt = zap_f_phys(zap)->zap_salt;
426 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
427
428 ASSERT3U(sizeof (struct zap_leaf_header), ==,
429 2*ZAP_LEAF_CHUNKSIZE);
430
431 /*
432 * The embedded pointer table should not overlap the
433 * other members.
434 */
435 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
436 &zap_f_phys(zap)->zap_salt);
437
438 /*
439 * The embedded pointer table should end at the end of
440 * the block
441 */
442 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
443 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
444 (uintptr_t)zap_f_phys(zap), ==,
445 zap->zap_dbuf->db_size);
446 }
447 rw_exit(&zap->zap_rwlock);
448 return (zap);
449
450 handle_winner:
451 rw_exit(&zap->zap_rwlock);
452 rw_destroy(&zap->zap_rwlock);
453 if (!zap->zap_ismicro)
454 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
455 kmem_free(zap, sizeof (zap_t));
456 return (winner);
457 }
458
459 int
460 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
461 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
462 {
463 dmu_object_info_t doi;
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 dmu_object_info_from_db(db, &doi);
476 if (DMU_OT_BYTESWAP(doi.doi_type) != DMU_BSWAP_ZAP)
477 return (SET_ERROR(EINVAL));
478
479 zap = dmu_buf_get_user(db);
480 if (zap == NULL) {
481 zap = mzap_open(os, obj, db);
482 if (zap == NULL) {
483 /*
484 * mzap_open() didn't like what it saw on-disk.
485 * Check for corruption!
486 */
487 dmu_buf_rele(db, NULL);
488 return (SET_ERROR(EIO));
489 }
490 }
491
492 /*
493 * We're checking zap_ismicro without the lock held, in order to
494 * tell what type of lock we want. Once we have some sort of
495 * lock, see if it really is the right type. In practice this
496 * can only be different if it was upgraded from micro to fat,
497 * and micro wanted WRITER but fat only needs READER.
498 */
499 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
500 rw_enter(&zap->zap_rwlock, lt);
501 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
502 /* it was upgraded, now we only need reader */
503 ASSERT(lt == RW_WRITER);
504 ASSERT(RW_READER ==
505 ((!zap->zap_ismicro && fatreader) ? RW_READER : lti));
506 rw_downgrade(&zap->zap_rwlock);
507 lt = RW_READER;
508 }
509
510 zap->zap_objset = os;
511
512 if (lt == RW_WRITER)
513 dmu_buf_will_dirty(db, tx);
514
515 ASSERT3P(zap->zap_dbuf, ==, db);
516
517 ASSERT(!zap->zap_ismicro ||
518 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
519 if (zap->zap_ismicro && tx && adding &&
520 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
521 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
522 if (newsz > MZAP_MAX_BLKSZ) {
523 dprintf("upgrading obj %llu: num_entries=%u\n",
524 obj, zap->zap_m.zap_num_entries);
525 *zapp = zap;
526 return (mzap_upgrade(zapp, tx, 0));
527 }
528 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
529 ASSERT0(err);
530 zap->zap_m.zap_num_chunks =
531 db->db_size / MZAP_ENT_LEN - 1;
532 }
533
534 *zapp = zap;
535 return (0);
536 }
537
538 void
539 zap_unlockdir(zap_t *zap)
540 {
541 rw_exit(&zap->zap_rwlock);
542 dmu_buf_rele(zap->zap_dbuf, NULL);
543 }
544
545 static int
546 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
547 {
548 mzap_phys_t *mzp;
549 int i, sz, nchunks;
550 int err = 0;
551 zap_t *zap = *zapp;
552
553 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
554
555 sz = zap->zap_dbuf->db_size;
556 mzp = zio_buf_alloc(sz);
557 bcopy(zap->zap_dbuf->db_data, mzp, sz);
558 nchunks = zap->zap_m.zap_num_chunks;
559
560 if (!flags) {
561 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
562 1ULL << fzap_default_block_shift, 0, tx);
563 if (err) {
564 zio_buf_free(mzp, sz);
565 return (err);
566 }
567 }
568
569 dprintf("upgrading obj=%llu with %u chunks\n",
570 zap->zap_object, nchunks);
571 /* XXX destroy the avl later, so we can use the stored hash value */
572 mze_destroy(zap);
573
574 fzap_upgrade(zap, tx, flags);
575
576 for (i = 0; i < nchunks; i++) {
577 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
578 zap_name_t *zn;
579 if (mze->mze_name[0] == 0)
580 continue;
581 dprintf("adding %s=%llu\n",
582 mze->mze_name, mze->mze_value);
583 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
584 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
585 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
586 zap_name_free(zn);
587 if (err)
588 break;
589 }
590 zio_buf_free(mzp, sz);
591 *zapp = zap;
592 return (err);
593 }
594
595 void
596 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
597 dmu_tx_t *tx)
598 {
599 dmu_buf_t *db;
600 mzap_phys_t *zp;
601
602 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
603
604 #ifdef ZFS_DEBUG
605 {
606 dmu_object_info_t doi;
607 dmu_object_info_from_db(db, &doi);
608 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
609 }
610 #endif
611
612 dmu_buf_will_dirty(db, tx);
613 zp = db->db_data;
614 zp->mz_block_type = ZBT_MICRO;
615 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
616 zp->mz_normflags = normflags;
617 dmu_buf_rele(db, FTAG);
618
619 if (flags != 0) {
620 zap_t *zap;
621 /* Only fat zap supports flags; upgrade immediately. */
622 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
623 B_FALSE, B_FALSE, &zap));
624 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
625 zap_unlockdir(zap);
626 }
627 }
628
629 int
630 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
631 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
632 {
633 return (zap_create_claim_norm(os, obj,
634 0, ot, bonustype, bonuslen, tx));
635 }
636
637 int
638 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
639 dmu_object_type_t ot,
640 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
641 {
642 int err;
643
644 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
645 if (err != 0)
646 return (err);
647 mzap_create_impl(os, obj, normflags, 0, tx);
648 return (0);
649 }
650
651 uint64_t
652 zap_create(objset_t *os, dmu_object_type_t ot,
653 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
654 {
655 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
656 }
657
658 uint64_t
659 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
660 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
661 {
662 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
663
664 mzap_create_impl(os, obj, normflags, 0, tx);
665 return (obj);
666 }
667
668 uint64_t
669 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
670 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
671 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
672 {
673 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
674
675 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
676 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
677 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
678 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
679
680 VERIFY(dmu_object_set_blocksize(os, obj,
681 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
682
683 mzap_create_impl(os, obj, normflags, flags, tx);
684 return (obj);
685 }
686
687 int
688 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
689 {
690 /*
691 * dmu_object_free will free the object number and free the
692 * data. Freeing the data will cause our pageout function to be
693 * called, which will destroy our data (zap_leaf_t's and zap_t).
694 */
695
696 return (dmu_object_free(os, zapobj, tx));
697 }
698
699 void
700 zap_evict(void *dbu)
701 {
702 zap_t *zap = dbu;
703
704 rw_destroy(&zap->zap_rwlock);
705
706 if (zap->zap_ismicro)
707 mze_destroy(zap);
708 else
709 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
710
711 kmem_free(zap, sizeof (zap_t));
712 }
713
714 int
715 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
716 {
717 zap_t *zap;
718 int err;
719
720 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
721 if (err)
722 return (err);
723 if (!zap->zap_ismicro) {
724 err = fzap_count(zap, count);
725 } else {
726 *count = zap->zap_m.zap_num_entries;
727 }
728 zap_unlockdir(zap);
729 return (err);
730 }
731
732 /*
733 * zn may be NULL; if not specified, it will be computed if needed.
734 * See also the comment above zap_entry_normalization_conflict().
735 */
736 static boolean_t
737 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
738 {
739 mzap_ent_t *other;
740 int direction = AVL_BEFORE;
741 boolean_t allocdzn = B_FALSE;
742
743 if (zap->zap_normflags == 0)
744 return (B_FALSE);
745
746 again:
747 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
748 other && other->mze_hash == mze->mze_hash;
749 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
750
751 if (zn == NULL) {
752 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
753 MT_FIRST);
754 allocdzn = B_TRUE;
755 }
756 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
757 if (allocdzn)
758 zap_name_free(zn);
759 return (B_TRUE);
760 }
761 }
762
763 if (direction == AVL_BEFORE) {
764 direction = AVL_AFTER;
765 goto again;
766 }
767
768 if (allocdzn)
769 zap_name_free(zn);
770 return (B_FALSE);
771 }
772
773 /*
774 * Routines for manipulating attributes.
775 */
776
777 int
778 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
779 uint64_t integer_size, uint64_t num_integers, void *buf)
780 {
781 return (zap_lookup_norm(os, zapobj, name, integer_size,
782 num_integers, buf, MT_EXACT, NULL, 0, NULL));
783 }
784
785 int
786 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
787 uint64_t integer_size, uint64_t num_integers, void *buf,
788 matchtype_t mt, char *realname, int rn_len,
789 boolean_t *ncp)
790 {
791 zap_t *zap;
792 int err;
793 mzap_ent_t *mze;
794 zap_name_t *zn;
795
796 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
797 if (err)
798 return (err);
799 zn = zap_name_alloc(zap, name, mt);
800 if (zn == NULL) {
801 zap_unlockdir(zap);
802 return (SET_ERROR(ENOTSUP));
803 }
804
805 if (!zap->zap_ismicro) {
806 err = fzap_lookup(zn, integer_size, num_integers, buf,
807 realname, rn_len, ncp);
808 } else {
809 mze = mze_find(zn);
810 if (mze == NULL) {
811 err = SET_ERROR(ENOENT);
812 } else {
813 if (num_integers < 1) {
814 err = SET_ERROR(EOVERFLOW);
815 } else if (integer_size != 8) {
816 err = SET_ERROR(EINVAL);
817 } else {
818 *(uint64_t *)buf =
819 MZE_PHYS(zap, mze)->mze_value;
820 (void) strlcpy(realname,
821 MZE_PHYS(zap, mze)->mze_name, rn_len);
822 if (ncp) {
823 *ncp = mzap_normalization_conflict(zap,
824 zn, mze);
825 }
826 }
827 }
828 }
829 zap_name_free(zn);
830 zap_unlockdir(zap);
831 return (err);
832 }
833
834 int
835 zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
836 {
837 zap_t *zap;
838 int err;
839 zap_name_t *zn;
840
841 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
842 if (err)
843 return (err);
844 zn = zap_name_alloc(zap, name, MT_EXACT);
845 if (zn == NULL) {
846 zap_unlockdir(zap);
847 return (SET_ERROR(ENOTSUP));
848 }
849
850 fzap_prefetch(zn);
851 zap_name_free(zn);
852 zap_unlockdir(zap);
853 return (err);
854 }
855
856 int
857 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
858 int key_numints)
859 {
860 zap_t *zap;
861 int err;
862 zap_name_t *zn;
863
864 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
865 if (err)
866 return (err);
867 zn = zap_name_alloc_uint64(zap, key, key_numints);
868 if (zn == NULL) {
869 zap_unlockdir(zap);
870 return (SET_ERROR(ENOTSUP));
871 }
872
873 fzap_prefetch(zn);
874 zap_name_free(zn);
875 zap_unlockdir(zap);
876 return (err);
877 }
878
879 int
880 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
881 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
882 {
883 zap_t *zap;
884 int err;
885 zap_name_t *zn;
886
887 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
888 if (err)
889 return (err);
890 zn = zap_name_alloc_uint64(zap, key, key_numints);
891 if (zn == NULL) {
892 zap_unlockdir(zap);
893 return (SET_ERROR(ENOTSUP));
894 }
895
896 err = fzap_lookup(zn, integer_size, num_integers, buf,
897 NULL, 0, NULL);
898 zap_name_free(zn);
899 zap_unlockdir(zap);
900 return (err);
901 }
902
903 int
904 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
905 {
906 int err = zap_lookup_norm(os, zapobj, name, 0,
907 0, NULL, MT_EXACT, NULL, 0, NULL);
908 if (err == EOVERFLOW || err == EINVAL)
909 err = 0; /* found, but skipped reading the value */
910 return (err);
911 }
912
913 int
914 zap_length(objset_t *os, uint64_t zapobj, const char *name,
915 uint64_t *integer_size, uint64_t *num_integers)
916 {
917 zap_t *zap;
918 int err;
919 mzap_ent_t *mze;
920 zap_name_t *zn;
921
922 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
923 if (err)
924 return (err);
925 zn = zap_name_alloc(zap, name, MT_EXACT);
926 if (zn == NULL) {
927 zap_unlockdir(zap);
928 return (SET_ERROR(ENOTSUP));
929 }
930 if (!zap->zap_ismicro) {
931 err = fzap_length(zn, integer_size, num_integers);
932 } else {
933 mze = mze_find(zn);
934 if (mze == NULL) {
935 err = SET_ERROR(ENOENT);
936 } else {
937 if (integer_size)
938 *integer_size = 8;
939 if (num_integers)
940 *num_integers = 1;
941 }
942 }
943 zap_name_free(zn);
944 zap_unlockdir(zap);
945 return (err);
946 }
947
948 int
949 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
950 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
951 {
952 zap_t *zap;
953 int err;
954 zap_name_t *zn;
955
956 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
957 if (err)
958 return (err);
959 zn = zap_name_alloc_uint64(zap, key, key_numints);
960 if (zn == NULL) {
961 zap_unlockdir(zap);
962 return (SET_ERROR(ENOTSUP));
963 }
964 err = fzap_length(zn, integer_size, num_integers);
965 zap_name_free(zn);
966 zap_unlockdir(zap);
967 return (err);
968 }
969
970 static void
971 mzap_addent(zap_name_t *zn, uint64_t value)
972 {
973 int i;
974 zap_t *zap = zn->zn_zap;
975 int start = zap->zap_m.zap_alloc_next;
976 uint32_t cd;
977
978 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
979
980 #ifdef ZFS_DEBUG
981 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
982 ASSERTV(mzap_ent_phys_t *mze);
983 ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
984 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
985 }
986 #endif
987
988 cd = mze_find_unused_cd(zap, zn->zn_hash);
989 /* given the limited size of the microzap, this can't happen */
990 ASSERT(cd < zap_maxcd(zap));
991
992 again:
993 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
994 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
995 if (mze->mze_name[0] == 0) {
996 mze->mze_value = value;
997 mze->mze_cd = cd;
998 (void) strcpy(mze->mze_name, zn->zn_key_orig);
999 zap->zap_m.zap_num_entries++;
1000 zap->zap_m.zap_alloc_next = i+1;
1001 if (zap->zap_m.zap_alloc_next ==
1002 zap->zap_m.zap_num_chunks)
1003 zap->zap_m.zap_alloc_next = 0;
1004 mze_insert(zap, i, zn->zn_hash);
1005 return;
1006 }
1007 }
1008 if (start != 0) {
1009 start = 0;
1010 goto again;
1011 }
1012 cmn_err(CE_PANIC, "out of entries!");
1013 }
1014
1015 int
1016 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1017 int integer_size, uint64_t num_integers,
1018 const void *val, dmu_tx_t *tx)
1019 {
1020 zap_t *zap;
1021 int err;
1022 mzap_ent_t *mze;
1023 const uint64_t *intval = val;
1024 zap_name_t *zn;
1025
1026 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1027 if (err)
1028 return (err);
1029 zn = zap_name_alloc(zap, key, MT_EXACT);
1030 if (zn == NULL) {
1031 zap_unlockdir(zap);
1032 return (SET_ERROR(ENOTSUP));
1033 }
1034 if (!zap->zap_ismicro) {
1035 err = fzap_add(zn, integer_size, num_integers, val, tx);
1036 zap = zn->zn_zap; /* fzap_add() may change zap */
1037 } else if (integer_size != 8 || num_integers != 1 ||
1038 strlen(key) >= MZAP_NAME_LEN) {
1039 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1040 if (err == 0)
1041 err = fzap_add(zn, integer_size, num_integers, val, tx);
1042 zap = zn->zn_zap; /* fzap_add() may change zap */
1043 } else {
1044 mze = mze_find(zn);
1045 if (mze != NULL) {
1046 err = SET_ERROR(EEXIST);
1047 } else {
1048 mzap_addent(zn, *intval);
1049 }
1050 }
1051 ASSERT(zap == zn->zn_zap);
1052 zap_name_free(zn);
1053 if (zap != NULL) /* may be NULL if fzap_add() failed */
1054 zap_unlockdir(zap);
1055 return (err);
1056 }
1057
1058 int
1059 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1060 int key_numints, int integer_size, uint64_t num_integers,
1061 const void *val, dmu_tx_t *tx)
1062 {
1063 zap_t *zap;
1064 int err;
1065 zap_name_t *zn;
1066
1067 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1068 if (err)
1069 return (err);
1070 zn = zap_name_alloc_uint64(zap, key, key_numints);
1071 if (zn == NULL) {
1072 zap_unlockdir(zap);
1073 return (SET_ERROR(ENOTSUP));
1074 }
1075 err = fzap_add(zn, integer_size, num_integers, val, tx);
1076 zap = zn->zn_zap; /* fzap_add() may change zap */
1077 zap_name_free(zn);
1078 if (zap != NULL) /* may be NULL if fzap_add() failed */
1079 zap_unlockdir(zap);
1080 return (err);
1081 }
1082
1083 int
1084 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1085 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1086 {
1087 zap_t *zap;
1088 mzap_ent_t *mze;
1089 const uint64_t *intval = val;
1090 zap_name_t *zn;
1091 int err;
1092
1093 #ifdef ZFS_DEBUG
1094 uint64_t oldval;
1095
1096 /*
1097 * If there is an old value, it shouldn't change across the
1098 * lockdir (eg, due to bprewrite's xlation).
1099 */
1100 if (integer_size == 8 && num_integers == 1)
1101 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1102 #endif
1103
1104 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1105 if (err)
1106 return (err);
1107 zn = zap_name_alloc(zap, name, MT_EXACT);
1108 if (zn == NULL) {
1109 zap_unlockdir(zap);
1110 return (SET_ERROR(ENOTSUP));
1111 }
1112 if (!zap->zap_ismicro) {
1113 err = fzap_update(zn, integer_size, num_integers, val, tx);
1114 zap = zn->zn_zap; /* fzap_update() may change zap */
1115 } else if (integer_size != 8 || num_integers != 1 ||
1116 strlen(name) >= MZAP_NAME_LEN) {
1117 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1118 zapobj, integer_size, num_integers, name);
1119 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1120 if (err == 0)
1121 err = fzap_update(zn, integer_size, num_integers,
1122 val, tx);
1123 zap = zn->zn_zap; /* fzap_update() may change zap */
1124 } else {
1125 mze = mze_find(zn);
1126 if (mze != NULL) {
1127 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1128 MZE_PHYS(zap, mze)->mze_value = *intval;
1129 } else {
1130 mzap_addent(zn, *intval);
1131 }
1132 }
1133 ASSERT(zap == zn->zn_zap);
1134 zap_name_free(zn);
1135 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1136 zap_unlockdir(zap);
1137 return (err);
1138 }
1139
1140 int
1141 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1142 int key_numints,
1143 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1144 {
1145 zap_t *zap;
1146 zap_name_t *zn;
1147 int err;
1148
1149 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1150 if (err)
1151 return (err);
1152 zn = zap_name_alloc_uint64(zap, key, key_numints);
1153 if (zn == NULL) {
1154 zap_unlockdir(zap);
1155 return (SET_ERROR(ENOTSUP));
1156 }
1157 err = fzap_update(zn, integer_size, num_integers, val, tx);
1158 zap = zn->zn_zap; /* fzap_update() may change zap */
1159 zap_name_free(zn);
1160 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1161 zap_unlockdir(zap);
1162 return (err);
1163 }
1164
1165 int
1166 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1167 {
1168 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1169 }
1170
1171 int
1172 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1173 matchtype_t mt, dmu_tx_t *tx)
1174 {
1175 zap_t *zap;
1176 int err;
1177 mzap_ent_t *mze;
1178 zap_name_t *zn;
1179
1180 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1181 if (err)
1182 return (err);
1183 zn = zap_name_alloc(zap, name, mt);
1184 if (zn == NULL) {
1185 zap_unlockdir(zap);
1186 return (SET_ERROR(ENOTSUP));
1187 }
1188 if (!zap->zap_ismicro) {
1189 err = fzap_remove(zn, tx);
1190 } else {
1191 mze = mze_find(zn);
1192 if (mze == NULL) {
1193 err = SET_ERROR(ENOENT);
1194 } else {
1195 zap->zap_m.zap_num_entries--;
1196 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1197 sizeof (mzap_ent_phys_t));
1198 mze_remove(zap, mze);
1199 }
1200 }
1201 zap_name_free(zn);
1202 zap_unlockdir(zap);
1203 return (err);
1204 }
1205
1206 int
1207 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1208 int key_numints, dmu_tx_t *tx)
1209 {
1210 zap_t *zap;
1211 int err;
1212 zap_name_t *zn;
1213
1214 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1215 if (err)
1216 return (err);
1217 zn = zap_name_alloc_uint64(zap, key, key_numints);
1218 if (zn == NULL) {
1219 zap_unlockdir(zap);
1220 return (SET_ERROR(ENOTSUP));
1221 }
1222 err = fzap_remove(zn, tx);
1223 zap_name_free(zn);
1224 zap_unlockdir(zap);
1225 return (err);
1226 }
1227
1228 /*
1229 * Routines for iterating over the attributes.
1230 */
1231
1232 void
1233 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1234 uint64_t serialized)
1235 {
1236 zc->zc_objset = os;
1237 zc->zc_zap = NULL;
1238 zc->zc_leaf = NULL;
1239 zc->zc_zapobj = zapobj;
1240 zc->zc_serialized = serialized;
1241 zc->zc_hash = 0;
1242 zc->zc_cd = 0;
1243 }
1244
1245 void
1246 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1247 {
1248 zap_cursor_init_serialized(zc, os, zapobj, 0);
1249 }
1250
1251 void
1252 zap_cursor_fini(zap_cursor_t *zc)
1253 {
1254 if (zc->zc_zap) {
1255 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1256 zap_unlockdir(zc->zc_zap);
1257 zc->zc_zap = NULL;
1258 }
1259 if (zc->zc_leaf) {
1260 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1261 zap_put_leaf(zc->zc_leaf);
1262 zc->zc_leaf = NULL;
1263 }
1264 zc->zc_objset = NULL;
1265 }
1266
1267 uint64_t
1268 zap_cursor_serialize(zap_cursor_t *zc)
1269 {
1270 if (zc->zc_hash == -1ULL)
1271 return (-1ULL);
1272 if (zc->zc_zap == NULL)
1273 return (zc->zc_serialized);
1274 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1275 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1276
1277 /*
1278 * We want to keep the high 32 bits of the cursor zero if we can, so
1279 * that 32-bit programs can access this. So usually use a small
1280 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1281 * of the cursor.
1282 *
1283 * [ collision differentiator | zap_hashbits()-bit hash value ]
1284 */
1285 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1286 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1287 }
1288
1289 int
1290 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1291 {
1292 int err;
1293 avl_index_t idx;
1294 mzap_ent_t mze_tofind;
1295 mzap_ent_t *mze;
1296
1297 if (zc->zc_hash == -1ULL)
1298 return (SET_ERROR(ENOENT));
1299
1300 if (zc->zc_zap == NULL) {
1301 int hb;
1302 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1303 RW_READER, TRUE, FALSE, &zc->zc_zap);
1304 if (err)
1305 return (err);
1306
1307 /*
1308 * To support zap_cursor_init_serialized, advance, retrieve,
1309 * we must add to the existing zc_cd, which may already
1310 * be 1 due to the zap_cursor_advance.
1311 */
1312 ASSERT(zc->zc_hash == 0);
1313 hb = zap_hashbits(zc->zc_zap);
1314 zc->zc_hash = zc->zc_serialized << (64 - hb);
1315 zc->zc_cd += zc->zc_serialized >> hb;
1316 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1317 zc->zc_cd = 0;
1318 } else {
1319 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1320 }
1321 if (!zc->zc_zap->zap_ismicro) {
1322 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1323 } else {
1324 mze_tofind.mze_hash = zc->zc_hash;
1325 mze_tofind.mze_cd = zc->zc_cd;
1326
1327 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1328 if (mze == NULL) {
1329 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1330 idx, AVL_AFTER);
1331 }
1332 if (mze) {
1333 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1334 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1335 za->za_normalization_conflict =
1336 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1337 za->za_integer_length = 8;
1338 za->za_num_integers = 1;
1339 za->za_first_integer = mzep->mze_value;
1340 (void) strcpy(za->za_name, mzep->mze_name);
1341 zc->zc_hash = mze->mze_hash;
1342 zc->zc_cd = mze->mze_cd;
1343 err = 0;
1344 } else {
1345 zc->zc_hash = -1ULL;
1346 err = SET_ERROR(ENOENT);
1347 }
1348 }
1349 rw_exit(&zc->zc_zap->zap_rwlock);
1350 return (err);
1351 }
1352
1353 void
1354 zap_cursor_advance(zap_cursor_t *zc)
1355 {
1356 if (zc->zc_hash == -1ULL)
1357 return;
1358 zc->zc_cd++;
1359 }
1360
1361 int
1362 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1363 {
1364 int err;
1365 zap_t *zap;
1366
1367 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1368 if (err)
1369 return (err);
1370
1371 bzero(zs, sizeof (zap_stats_t));
1372
1373 if (zap->zap_ismicro) {
1374 zs->zs_blocksize = zap->zap_dbuf->db_size;
1375 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1376 zs->zs_num_blocks = 1;
1377 } else {
1378 fzap_get_stats(zap, zs);
1379 }
1380 zap_unlockdir(zap);
1381 return (0);
1382 }
1383
1384 int
1385 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1386 uint64_t *towrite, uint64_t *tooverwrite)
1387 {
1388 zap_t *zap;
1389 int err = 0;
1390
1391 /*
1392 * Since, we don't have a name, we cannot figure out which blocks will
1393 * be affected in this operation. So, account for the worst case :
1394 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1395 * - 4 new blocks written if adding:
1396 * - 2 blocks for possibly split leaves,
1397 * - 2 grown ptrtbl blocks
1398 *
1399 * This also accomodates the case where an add operation to a fairly
1400 * large microzap results in a promotion to fatzap.
1401 */
1402 if (name == NULL) {
1403 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1404 return (err);
1405 }
1406
1407 /*
1408 * We lock the zap with adding == FALSE. Because, if we pass
1409 * the actual value of add, it could trigger a mzap_upgrade().
1410 * At present we are just evaluating the possibility of this operation
1411 * and hence we donot want to trigger an upgrade.
1412 */
1413 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1414 if (err)
1415 return (err);
1416
1417 if (!zap->zap_ismicro) {
1418 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1419 if (zn) {
1420 err = fzap_count_write(zn, add, towrite,
1421 tooverwrite);
1422 zap_name_free(zn);
1423 } else {
1424 /*
1425 * We treat this case as similar to (name == NULL)
1426 */
1427 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1428 }
1429 } else {
1430 /*
1431 * We are here if (name != NULL) and this is a micro-zap.
1432 * We account for the header block depending on whether it
1433 * is freeable.
1434 *
1435 * Incase of an add-operation it is hard to find out
1436 * if this add will promote this microzap to fatzap.
1437 * Hence, we consider the worst case and account for the
1438 * blocks assuming this microzap would be promoted to a
1439 * fatzap.
1440 *
1441 * 1 block overwritten : header block
1442 * 4 new blocks written : 2 new split leaf, 2 grown
1443 * ptrtbl blocks
1444 */
1445 if (dmu_buf_freeable(zap->zap_dbuf))
1446 *tooverwrite += MZAP_MAX_BLKSZ;
1447 else
1448 *towrite += MZAP_MAX_BLKSZ;
1449
1450 if (add) {
1451 *towrite += 4 * MZAP_MAX_BLKSZ;
1452 }
1453 }
1454
1455 zap_unlockdir(zap);
1456 return (err);
1457 }
1458
1459 #if defined(_KERNEL) && defined(HAVE_SPL)
1460 EXPORT_SYMBOL(zap_create);
1461 EXPORT_SYMBOL(zap_create_norm);
1462 EXPORT_SYMBOL(zap_create_flags);
1463 EXPORT_SYMBOL(zap_create_claim);
1464 EXPORT_SYMBOL(zap_create_claim_norm);
1465 EXPORT_SYMBOL(zap_destroy);
1466 EXPORT_SYMBOL(zap_lookup);
1467 EXPORT_SYMBOL(zap_lookup_norm);
1468 EXPORT_SYMBOL(zap_lookup_uint64);
1469 EXPORT_SYMBOL(zap_contains);
1470 EXPORT_SYMBOL(zap_prefetch);
1471 EXPORT_SYMBOL(zap_prefetch_uint64);
1472 EXPORT_SYMBOL(zap_count_write);
1473 EXPORT_SYMBOL(zap_add);
1474 EXPORT_SYMBOL(zap_add_uint64);
1475 EXPORT_SYMBOL(zap_update);
1476 EXPORT_SYMBOL(zap_update_uint64);
1477 EXPORT_SYMBOL(zap_length);
1478 EXPORT_SYMBOL(zap_length_uint64);
1479 EXPORT_SYMBOL(zap_remove);
1480 EXPORT_SYMBOL(zap_remove_norm);
1481 EXPORT_SYMBOL(zap_remove_uint64);
1482 EXPORT_SYMBOL(zap_count);
1483 EXPORT_SYMBOL(zap_value_search);
1484 EXPORT_SYMBOL(zap_join);
1485 EXPORT_SYMBOL(zap_join_increment);
1486 EXPORT_SYMBOL(zap_add_int);
1487 EXPORT_SYMBOL(zap_remove_int);
1488 EXPORT_SYMBOL(zap_lookup_int);
1489 EXPORT_SYMBOL(zap_increment_int);
1490 EXPORT_SYMBOL(zap_add_int_key);
1491 EXPORT_SYMBOL(zap_lookup_int_key);
1492 EXPORT_SYMBOL(zap_increment);
1493 EXPORT_SYMBOL(zap_cursor_init);
1494 EXPORT_SYMBOL(zap_cursor_fini);
1495 EXPORT_SYMBOL(zap_cursor_retrieve);
1496 EXPORT_SYMBOL(zap_cursor_advance);
1497 EXPORT_SYMBOL(zap_cursor_serialize);
1498 EXPORT_SYMBOL(zap_cursor_init_serialized);
1499 EXPORT_SYMBOL(zap_get_stats);
1500 #endif