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