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