]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zap_micro.c
Implement large_dnode pool feature
[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_dnsize(os, obj, ot, bonustype, bonuslen,
634 0, tx));
635 }
636
637 int
638 zap_create_claim_dnsize(objset_t *os, uint64_t obj, dmu_object_type_t ot,
639 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
640 {
641 return (zap_create_claim_norm_dnsize(os, obj,
642 0, ot, bonustype, bonuslen, dnodesize, tx));
643 }
644
645 int
646 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
647 dmu_object_type_t ot,
648 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
649 {
650 return (zap_create_claim_norm_dnsize(os, obj, normflags, ot, bonustype,
651 bonuslen, 0, tx));
652 }
653
654 int
655 zap_create_claim_norm_dnsize(objset_t *os, uint64_t obj, int normflags,
656 dmu_object_type_t ot, dmu_object_type_t bonustype, int bonuslen,
657 int dnodesize, dmu_tx_t *tx)
658 {
659 int err;
660
661 err = dmu_object_claim_dnsize(os, obj, ot, 0, bonustype, bonuslen,
662 dnodesize, tx);
663 if (err != 0)
664 return (err);
665 mzap_create_impl(os, obj, normflags, 0, tx);
666 return (0);
667 }
668
669 uint64_t
670 zap_create(objset_t *os, dmu_object_type_t ot,
671 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
672 {
673 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
674 }
675
676 uint64_t
677 zap_create_dnsize(objset_t *os, dmu_object_type_t ot,
678 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
679 {
680 return (zap_create_norm_dnsize(os, 0, ot, bonustype, bonuslen,
681 dnodesize, tx));
682 }
683
684 uint64_t
685 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
686 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
687 {
688 return (zap_create_norm_dnsize(os, normflags, ot, bonustype, bonuslen,
689 0, tx));
690 }
691
692 uint64_t
693 zap_create_norm_dnsize(objset_t *os, int normflags, dmu_object_type_t ot,
694 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
695 {
696 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
697 dnodesize, tx);
698
699 mzap_create_impl(os, obj, normflags, 0, tx);
700 return (obj);
701 }
702
703 uint64_t
704 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
705 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
706 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
707 {
708 return (zap_create_flags_dnsize(os, normflags, flags, ot,
709 leaf_blockshift, indirect_blockshift, bonustype, bonuslen, 0, tx));
710 }
711
712 uint64_t
713 zap_create_flags_dnsize(objset_t *os, int normflags, zap_flags_t flags,
714 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
715 dmu_object_type_t bonustype, int bonuslen, int dnodesize, dmu_tx_t *tx)
716 {
717 uint64_t obj = dmu_object_alloc_dnsize(os, ot, 0, bonustype, bonuslen,
718 dnodesize, tx);
719
720 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
721 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
722 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
723 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
724
725 VERIFY(dmu_object_set_blocksize(os, obj,
726 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
727
728 mzap_create_impl(os, obj, normflags, flags, tx);
729 return (obj);
730 }
731
732 int
733 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
734 {
735 /*
736 * dmu_object_free will free the object number and free the
737 * data. Freeing the data will cause our pageout function to be
738 * called, which will destroy our data (zap_leaf_t's and zap_t).
739 */
740
741 return (dmu_object_free(os, zapobj, tx));
742 }
743
744 void
745 zap_evict(void *dbu)
746 {
747 zap_t *zap = dbu;
748
749 rw_destroy(&zap->zap_rwlock);
750
751 if (zap->zap_ismicro)
752 mze_destroy(zap);
753 else
754 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
755
756 kmem_free(zap, sizeof (zap_t));
757 }
758
759 int
760 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
761 {
762 zap_t *zap;
763 int err;
764
765 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
766 if (err)
767 return (err);
768 if (!zap->zap_ismicro) {
769 err = fzap_count(zap, count);
770 } else {
771 *count = zap->zap_m.zap_num_entries;
772 }
773 zap_unlockdir(zap);
774 return (err);
775 }
776
777 /*
778 * zn may be NULL; if not specified, it will be computed if needed.
779 * See also the comment above zap_entry_normalization_conflict().
780 */
781 static boolean_t
782 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
783 {
784 mzap_ent_t *other;
785 int direction = AVL_BEFORE;
786 boolean_t allocdzn = B_FALSE;
787
788 if (zap->zap_normflags == 0)
789 return (B_FALSE);
790
791 again:
792 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
793 other && other->mze_hash == mze->mze_hash;
794 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
795
796 if (zn == NULL) {
797 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
798 MT_FIRST);
799 allocdzn = B_TRUE;
800 }
801 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
802 if (allocdzn)
803 zap_name_free(zn);
804 return (B_TRUE);
805 }
806 }
807
808 if (direction == AVL_BEFORE) {
809 direction = AVL_AFTER;
810 goto again;
811 }
812
813 if (allocdzn)
814 zap_name_free(zn);
815 return (B_FALSE);
816 }
817
818 /*
819 * Routines for manipulating attributes.
820 */
821
822 int
823 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
824 uint64_t integer_size, uint64_t num_integers, void *buf)
825 {
826 return (zap_lookup_norm(os, zapobj, name, integer_size,
827 num_integers, buf, MT_EXACT, NULL, 0, NULL));
828 }
829
830 int
831 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
832 uint64_t integer_size, uint64_t num_integers, void *buf,
833 matchtype_t mt, char *realname, int rn_len,
834 boolean_t *ncp)
835 {
836 zap_t *zap;
837 int err;
838 mzap_ent_t *mze;
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);
845 if (zn == NULL) {
846 zap_unlockdir(zap);
847 return (SET_ERROR(ENOTSUP));
848 }
849
850 if (!zap->zap_ismicro) {
851 err = fzap_lookup(zn, integer_size, num_integers, buf,
852 realname, rn_len, ncp);
853 } else {
854 mze = mze_find(zn);
855 if (mze == NULL) {
856 err = SET_ERROR(ENOENT);
857 } else {
858 if (num_integers < 1) {
859 err = SET_ERROR(EOVERFLOW);
860 } else if (integer_size != 8) {
861 err = SET_ERROR(EINVAL);
862 } else {
863 *(uint64_t *)buf =
864 MZE_PHYS(zap, mze)->mze_value;
865 (void) strlcpy(realname,
866 MZE_PHYS(zap, mze)->mze_name, rn_len);
867 if (ncp) {
868 *ncp = mzap_normalization_conflict(zap,
869 zn, mze);
870 }
871 }
872 }
873 }
874 zap_name_free(zn);
875 zap_unlockdir(zap);
876 return (err);
877 }
878
879 int
880 zap_prefetch(objset_t *os, uint64_t zapobj, const char *name)
881 {
882 zap_t *zap;
883 int err;
884 zap_name_t *zn;
885
886 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
887 if (err)
888 return (err);
889 zn = zap_name_alloc(zap, name, MT_EXACT);
890 if (zn == NULL) {
891 zap_unlockdir(zap);
892 return (SET_ERROR(ENOTSUP));
893 }
894
895 fzap_prefetch(zn);
896 zap_name_free(zn);
897 zap_unlockdir(zap);
898 return (err);
899 }
900
901 int
902 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
903 int key_numints)
904 {
905 zap_t *zap;
906 int err;
907 zap_name_t *zn;
908
909 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
910 if (err)
911 return (err);
912 zn = zap_name_alloc_uint64(zap, key, key_numints);
913 if (zn == NULL) {
914 zap_unlockdir(zap);
915 return (SET_ERROR(ENOTSUP));
916 }
917
918 fzap_prefetch(zn);
919 zap_name_free(zn);
920 zap_unlockdir(zap);
921 return (err);
922 }
923
924 int
925 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
926 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
927 {
928 zap_t *zap;
929 int err;
930 zap_name_t *zn;
931
932 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
933 if (err)
934 return (err);
935 zn = zap_name_alloc_uint64(zap, key, key_numints);
936 if (zn == NULL) {
937 zap_unlockdir(zap);
938 return (SET_ERROR(ENOTSUP));
939 }
940
941 err = fzap_lookup(zn, integer_size, num_integers, buf,
942 NULL, 0, NULL);
943 zap_name_free(zn);
944 zap_unlockdir(zap);
945 return (err);
946 }
947
948 int
949 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
950 {
951 int err = zap_lookup_norm(os, zapobj, name, 0,
952 0, NULL, MT_EXACT, NULL, 0, NULL);
953 if (err == EOVERFLOW || err == EINVAL)
954 err = 0; /* found, but skipped reading the value */
955 return (err);
956 }
957
958 int
959 zap_length(objset_t *os, uint64_t zapobj, const char *name,
960 uint64_t *integer_size, uint64_t *num_integers)
961 {
962 zap_t *zap;
963 int err;
964 mzap_ent_t *mze;
965 zap_name_t *zn;
966
967 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
968 if (err)
969 return (err);
970 zn = zap_name_alloc(zap, name, MT_EXACT);
971 if (zn == NULL) {
972 zap_unlockdir(zap);
973 return (SET_ERROR(ENOTSUP));
974 }
975 if (!zap->zap_ismicro) {
976 err = fzap_length(zn, integer_size, num_integers);
977 } else {
978 mze = mze_find(zn);
979 if (mze == NULL) {
980 err = SET_ERROR(ENOENT);
981 } else {
982 if (integer_size)
983 *integer_size = 8;
984 if (num_integers)
985 *num_integers = 1;
986 }
987 }
988 zap_name_free(zn);
989 zap_unlockdir(zap);
990 return (err);
991 }
992
993 int
994 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
995 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
996 {
997 zap_t *zap;
998 int err;
999 zap_name_t *zn;
1000
1001 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1002 if (err)
1003 return (err);
1004 zn = zap_name_alloc_uint64(zap, key, key_numints);
1005 if (zn == NULL) {
1006 zap_unlockdir(zap);
1007 return (SET_ERROR(ENOTSUP));
1008 }
1009 err = fzap_length(zn, integer_size, num_integers);
1010 zap_name_free(zn);
1011 zap_unlockdir(zap);
1012 return (err);
1013 }
1014
1015 static void
1016 mzap_addent(zap_name_t *zn, uint64_t value)
1017 {
1018 int i;
1019 zap_t *zap = zn->zn_zap;
1020 int start = zap->zap_m.zap_alloc_next;
1021 uint32_t cd;
1022
1023 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
1024
1025 #ifdef ZFS_DEBUG
1026 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
1027 ASSERTV(mzap_ent_phys_t *mze);
1028 ASSERT(mze = &zap_m_phys(zap)->mz_chunk[i]);
1029 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
1030 }
1031 #endif
1032
1033 cd = mze_find_unused_cd(zap, zn->zn_hash);
1034 /* given the limited size of the microzap, this can't happen */
1035 ASSERT(cd < zap_maxcd(zap));
1036
1037 again:
1038 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
1039 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
1040 if (mze->mze_name[0] == 0) {
1041 mze->mze_value = value;
1042 mze->mze_cd = cd;
1043 (void) strcpy(mze->mze_name, zn->zn_key_orig);
1044 zap->zap_m.zap_num_entries++;
1045 zap->zap_m.zap_alloc_next = i+1;
1046 if (zap->zap_m.zap_alloc_next ==
1047 zap->zap_m.zap_num_chunks)
1048 zap->zap_m.zap_alloc_next = 0;
1049 mze_insert(zap, i, zn->zn_hash);
1050 return;
1051 }
1052 }
1053 if (start != 0) {
1054 start = 0;
1055 goto again;
1056 }
1057 cmn_err(CE_PANIC, "out of entries!");
1058 }
1059
1060 int
1061 zap_add(objset_t *os, uint64_t zapobj, const char *key,
1062 int integer_size, uint64_t num_integers,
1063 const void *val, dmu_tx_t *tx)
1064 {
1065 zap_t *zap;
1066 int err;
1067 mzap_ent_t *mze;
1068 const uint64_t *intval = val;
1069 zap_name_t *zn;
1070
1071 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1072 if (err)
1073 return (err);
1074 zn = zap_name_alloc(zap, key, MT_EXACT);
1075 if (zn == NULL) {
1076 zap_unlockdir(zap);
1077 return (SET_ERROR(ENOTSUP));
1078 }
1079 if (!zap->zap_ismicro) {
1080 err = fzap_add(zn, integer_size, num_integers, val, tx);
1081 zap = zn->zn_zap; /* fzap_add() may change zap */
1082 } else if (integer_size != 8 || num_integers != 1 ||
1083 strlen(key) >= MZAP_NAME_LEN) {
1084 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1085 if (err == 0)
1086 err = fzap_add(zn, integer_size, num_integers, val, tx);
1087 zap = zn->zn_zap; /* fzap_add() may change zap */
1088 } else {
1089 mze = mze_find(zn);
1090 if (mze != NULL) {
1091 err = SET_ERROR(EEXIST);
1092 } else {
1093 mzap_addent(zn, *intval);
1094 }
1095 }
1096 ASSERT(zap == zn->zn_zap);
1097 zap_name_free(zn);
1098 if (zap != NULL) /* may be NULL if fzap_add() failed */
1099 zap_unlockdir(zap);
1100 return (err);
1101 }
1102
1103 int
1104 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1105 int key_numints, int integer_size, uint64_t num_integers,
1106 const void *val, dmu_tx_t *tx)
1107 {
1108 zap_t *zap;
1109 int err;
1110 zap_name_t *zn;
1111
1112 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1113 if (err)
1114 return (err);
1115 zn = zap_name_alloc_uint64(zap, key, key_numints);
1116 if (zn == NULL) {
1117 zap_unlockdir(zap);
1118 return (SET_ERROR(ENOTSUP));
1119 }
1120 err = fzap_add(zn, integer_size, num_integers, val, tx);
1121 zap = zn->zn_zap; /* fzap_add() may change zap */
1122 zap_name_free(zn);
1123 if (zap != NULL) /* may be NULL if fzap_add() failed */
1124 zap_unlockdir(zap);
1125 return (err);
1126 }
1127
1128 int
1129 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1130 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1131 {
1132 zap_t *zap;
1133 mzap_ent_t *mze;
1134 const uint64_t *intval = val;
1135 zap_name_t *zn;
1136 int err;
1137
1138 #ifdef ZFS_DEBUG
1139 uint64_t oldval;
1140
1141 /*
1142 * If there is an old value, it shouldn't change across the
1143 * lockdir (eg, due to bprewrite's xlation).
1144 */
1145 if (integer_size == 8 && num_integers == 1)
1146 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1147 #endif
1148
1149 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1150 if (err)
1151 return (err);
1152 zn = zap_name_alloc(zap, name, MT_EXACT);
1153 if (zn == NULL) {
1154 zap_unlockdir(zap);
1155 return (SET_ERROR(ENOTSUP));
1156 }
1157 if (!zap->zap_ismicro) {
1158 err = fzap_update(zn, integer_size, num_integers, val, tx);
1159 zap = zn->zn_zap; /* fzap_update() may change zap */
1160 } else if (integer_size != 8 || num_integers != 1 ||
1161 strlen(name) >= MZAP_NAME_LEN) {
1162 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1163 zapobj, integer_size, num_integers, name);
1164 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1165 if (err == 0)
1166 err = fzap_update(zn, integer_size, num_integers,
1167 val, tx);
1168 zap = zn->zn_zap; /* fzap_update() may change zap */
1169 } else {
1170 mze = mze_find(zn);
1171 if (mze != NULL) {
1172 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1173 MZE_PHYS(zap, mze)->mze_value = *intval;
1174 } else {
1175 mzap_addent(zn, *intval);
1176 }
1177 }
1178 ASSERT(zap == zn->zn_zap);
1179 zap_name_free(zn);
1180 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1181 zap_unlockdir(zap);
1182 return (err);
1183 }
1184
1185 int
1186 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1187 int key_numints,
1188 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1189 {
1190 zap_t *zap;
1191 zap_name_t *zn;
1192 int err;
1193
1194 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1195 if (err)
1196 return (err);
1197 zn = zap_name_alloc_uint64(zap, key, key_numints);
1198 if (zn == NULL) {
1199 zap_unlockdir(zap);
1200 return (SET_ERROR(ENOTSUP));
1201 }
1202 err = fzap_update(zn, integer_size, num_integers, val, tx);
1203 zap = zn->zn_zap; /* fzap_update() may change zap */
1204 zap_name_free(zn);
1205 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1206 zap_unlockdir(zap);
1207 return (err);
1208 }
1209
1210 int
1211 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1212 {
1213 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1214 }
1215
1216 int
1217 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1218 matchtype_t mt, dmu_tx_t *tx)
1219 {
1220 zap_t *zap;
1221 int err;
1222 mzap_ent_t *mze;
1223 zap_name_t *zn;
1224
1225 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1226 if (err)
1227 return (err);
1228 zn = zap_name_alloc(zap, name, mt);
1229 if (zn == NULL) {
1230 zap_unlockdir(zap);
1231 return (SET_ERROR(ENOTSUP));
1232 }
1233 if (!zap->zap_ismicro) {
1234 err = fzap_remove(zn, tx);
1235 } else {
1236 mze = mze_find(zn);
1237 if (mze == NULL) {
1238 err = SET_ERROR(ENOENT);
1239 } else {
1240 zap->zap_m.zap_num_entries--;
1241 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1242 sizeof (mzap_ent_phys_t));
1243 mze_remove(zap, mze);
1244 }
1245 }
1246 zap_name_free(zn);
1247 zap_unlockdir(zap);
1248 return (err);
1249 }
1250
1251 int
1252 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1253 int key_numints, dmu_tx_t *tx)
1254 {
1255 zap_t *zap;
1256 int err;
1257 zap_name_t *zn;
1258
1259 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1260 if (err)
1261 return (err);
1262 zn = zap_name_alloc_uint64(zap, key, key_numints);
1263 if (zn == NULL) {
1264 zap_unlockdir(zap);
1265 return (SET_ERROR(ENOTSUP));
1266 }
1267 err = fzap_remove(zn, tx);
1268 zap_name_free(zn);
1269 zap_unlockdir(zap);
1270 return (err);
1271 }
1272
1273 /*
1274 * Routines for iterating over the attributes.
1275 */
1276
1277 void
1278 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1279 uint64_t serialized)
1280 {
1281 zc->zc_objset = os;
1282 zc->zc_zap = NULL;
1283 zc->zc_leaf = NULL;
1284 zc->zc_zapobj = zapobj;
1285 zc->zc_serialized = serialized;
1286 zc->zc_hash = 0;
1287 zc->zc_cd = 0;
1288 }
1289
1290 void
1291 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1292 {
1293 zap_cursor_init_serialized(zc, os, zapobj, 0);
1294 }
1295
1296 void
1297 zap_cursor_fini(zap_cursor_t *zc)
1298 {
1299 if (zc->zc_zap) {
1300 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1301 zap_unlockdir(zc->zc_zap);
1302 zc->zc_zap = NULL;
1303 }
1304 if (zc->zc_leaf) {
1305 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1306 zap_put_leaf(zc->zc_leaf);
1307 zc->zc_leaf = NULL;
1308 }
1309 zc->zc_objset = NULL;
1310 }
1311
1312 uint64_t
1313 zap_cursor_serialize(zap_cursor_t *zc)
1314 {
1315 if (zc->zc_hash == -1ULL)
1316 return (-1ULL);
1317 if (zc->zc_zap == NULL)
1318 return (zc->zc_serialized);
1319 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1320 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1321
1322 /*
1323 * We want to keep the high 32 bits of the cursor zero if we can, so
1324 * that 32-bit programs can access this. So usually use a small
1325 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1326 * of the cursor.
1327 *
1328 * [ collision differentiator | zap_hashbits()-bit hash value ]
1329 */
1330 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1331 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1332 }
1333
1334 int
1335 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1336 {
1337 int err;
1338 avl_index_t idx;
1339 mzap_ent_t mze_tofind;
1340 mzap_ent_t *mze;
1341
1342 if (zc->zc_hash == -1ULL)
1343 return (SET_ERROR(ENOENT));
1344
1345 if (zc->zc_zap == NULL) {
1346 int hb;
1347 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1348 RW_READER, TRUE, FALSE, &zc->zc_zap);
1349 if (err)
1350 return (err);
1351
1352 /*
1353 * To support zap_cursor_init_serialized, advance, retrieve,
1354 * we must add to the existing zc_cd, which may already
1355 * be 1 due to the zap_cursor_advance.
1356 */
1357 ASSERT(zc->zc_hash == 0);
1358 hb = zap_hashbits(zc->zc_zap);
1359 zc->zc_hash = zc->zc_serialized << (64 - hb);
1360 zc->zc_cd += zc->zc_serialized >> hb;
1361 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1362 zc->zc_cd = 0;
1363 } else {
1364 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1365 }
1366 if (!zc->zc_zap->zap_ismicro) {
1367 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1368 } else {
1369 mze_tofind.mze_hash = zc->zc_hash;
1370 mze_tofind.mze_cd = zc->zc_cd;
1371
1372 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1373 if (mze == NULL) {
1374 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1375 idx, AVL_AFTER);
1376 }
1377 if (mze) {
1378 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1379 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1380 za->za_normalization_conflict =
1381 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1382 za->za_integer_length = 8;
1383 za->za_num_integers = 1;
1384 za->za_first_integer = mzep->mze_value;
1385 (void) strcpy(za->za_name, mzep->mze_name);
1386 zc->zc_hash = mze->mze_hash;
1387 zc->zc_cd = mze->mze_cd;
1388 err = 0;
1389 } else {
1390 zc->zc_hash = -1ULL;
1391 err = SET_ERROR(ENOENT);
1392 }
1393 }
1394 rw_exit(&zc->zc_zap->zap_rwlock);
1395 return (err);
1396 }
1397
1398 void
1399 zap_cursor_advance(zap_cursor_t *zc)
1400 {
1401 if (zc->zc_hash == -1ULL)
1402 return;
1403 zc->zc_cd++;
1404 }
1405
1406 int
1407 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1408 {
1409 int err;
1410 zap_t *zap;
1411
1412 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1413 if (err)
1414 return (err);
1415
1416 bzero(zs, sizeof (zap_stats_t));
1417
1418 if (zap->zap_ismicro) {
1419 zs->zs_blocksize = zap->zap_dbuf->db_size;
1420 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1421 zs->zs_num_blocks = 1;
1422 } else {
1423 fzap_get_stats(zap, zs);
1424 }
1425 zap_unlockdir(zap);
1426 return (0);
1427 }
1428
1429 int
1430 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1431 uint64_t *towrite, uint64_t *tooverwrite)
1432 {
1433 zap_t *zap;
1434 int err = 0;
1435
1436 /*
1437 * Since, we don't have a name, we cannot figure out which blocks will
1438 * be affected in this operation. So, account for the worst case :
1439 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1440 * - 4 new blocks written if adding:
1441 * - 2 blocks for possibly split leaves,
1442 * - 2 grown ptrtbl blocks
1443 *
1444 * This also accomodates the case where an add operation to a fairly
1445 * large microzap results in a promotion to fatzap.
1446 */
1447 if (name == NULL) {
1448 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1449 return (err);
1450 }
1451
1452 /*
1453 * We lock the zap with adding == FALSE. Because, if we pass
1454 * the actual value of add, it could trigger a mzap_upgrade().
1455 * At present we are just evaluating the possibility of this operation
1456 * and hence we donot want to trigger an upgrade.
1457 */
1458 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1459 if (err)
1460 return (err);
1461
1462 if (!zap->zap_ismicro) {
1463 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1464 if (zn) {
1465 err = fzap_count_write(zn, add, towrite,
1466 tooverwrite);
1467 zap_name_free(zn);
1468 } else {
1469 /*
1470 * We treat this case as similar to (name == NULL)
1471 */
1472 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1473 }
1474 } else {
1475 /*
1476 * We are here if (name != NULL) and this is a micro-zap.
1477 * We account for the header block depending on whether it
1478 * is freeable.
1479 *
1480 * Incase of an add-operation it is hard to find out
1481 * if this add will promote this microzap to fatzap.
1482 * Hence, we consider the worst case and account for the
1483 * blocks assuming this microzap would be promoted to a
1484 * fatzap.
1485 *
1486 * 1 block overwritten : header block
1487 * 4 new blocks written : 2 new split leaf, 2 grown
1488 * ptrtbl blocks
1489 */
1490 if (dmu_buf_freeable(zap->zap_dbuf))
1491 *tooverwrite += MZAP_MAX_BLKSZ;
1492 else
1493 *towrite += MZAP_MAX_BLKSZ;
1494
1495 if (add) {
1496 *towrite += 4 * MZAP_MAX_BLKSZ;
1497 }
1498 }
1499
1500 zap_unlockdir(zap);
1501 return (err);
1502 }
1503
1504 #if defined(_KERNEL) && defined(HAVE_SPL)
1505 EXPORT_SYMBOL(zap_create);
1506 EXPORT_SYMBOL(zap_create_dnsize);
1507 EXPORT_SYMBOL(zap_create_norm);
1508 EXPORT_SYMBOL(zap_create_norm_dnsize);
1509 EXPORT_SYMBOL(zap_create_flags);
1510 EXPORT_SYMBOL(zap_create_flags_dnsize);
1511 EXPORT_SYMBOL(zap_create_claim);
1512 EXPORT_SYMBOL(zap_create_claim_norm);
1513 EXPORT_SYMBOL(zap_create_claim_norm_dnsize);
1514 EXPORT_SYMBOL(zap_destroy);
1515 EXPORT_SYMBOL(zap_lookup);
1516 EXPORT_SYMBOL(zap_lookup_norm);
1517 EXPORT_SYMBOL(zap_lookup_uint64);
1518 EXPORT_SYMBOL(zap_contains);
1519 EXPORT_SYMBOL(zap_prefetch);
1520 EXPORT_SYMBOL(zap_prefetch_uint64);
1521 EXPORT_SYMBOL(zap_count_write);
1522 EXPORT_SYMBOL(zap_add);
1523 EXPORT_SYMBOL(zap_add_uint64);
1524 EXPORT_SYMBOL(zap_update);
1525 EXPORT_SYMBOL(zap_update_uint64);
1526 EXPORT_SYMBOL(zap_length);
1527 EXPORT_SYMBOL(zap_length_uint64);
1528 EXPORT_SYMBOL(zap_remove);
1529 EXPORT_SYMBOL(zap_remove_norm);
1530 EXPORT_SYMBOL(zap_remove_uint64);
1531 EXPORT_SYMBOL(zap_count);
1532 EXPORT_SYMBOL(zap_value_search);
1533 EXPORT_SYMBOL(zap_join);
1534 EXPORT_SYMBOL(zap_join_increment);
1535 EXPORT_SYMBOL(zap_add_int);
1536 EXPORT_SYMBOL(zap_remove_int);
1537 EXPORT_SYMBOL(zap_lookup_int);
1538 EXPORT_SYMBOL(zap_increment_int);
1539 EXPORT_SYMBOL(zap_add_int_key);
1540 EXPORT_SYMBOL(zap_lookup_int_key);
1541 EXPORT_SYMBOL(zap_increment);
1542 EXPORT_SYMBOL(zap_cursor_init);
1543 EXPORT_SYMBOL(zap_cursor_fini);
1544 EXPORT_SYMBOL(zap_cursor_retrieve);
1545 EXPORT_SYMBOL(zap_cursor_advance);
1546 EXPORT_SYMBOL(zap_cursor_serialize);
1547 EXPORT_SYMBOL(zap_cursor_init_serialized);
1548 EXPORT_SYMBOL(zap_get_stats);
1549 #endif