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