Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / uts / common / fs / zfs / zap_micro.c
blob917da32ec07d100698c251bc0348e26595f14870
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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>
37 #ifdef _KERNEL
38 #include <sys/sunddi.h>
39 #endif
41 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx);
44 static uint64_t
45 zap_hash(zap_t *zap, const char *normname)
47 const uint8_t *cp;
48 uint8_t c;
49 uint64_t crc = zap->zap_salt;
51 /* NB: name must already be normalized, if necessary */
53 ASSERT(crc != 0);
54 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
55 for (cp = (const uint8_t *)normname; (c = *cp) != '\0'; cp++) {
56 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ c) & 0xFF];
60 * Only use 28 bits, since we need 4 bits in the cookie for the
61 * collision differentiator. We MUST use the high bits, since
62 * those are the ones that we first pay attention to when
63 * chosing the bucket.
65 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
67 return (crc);
70 static int
71 zap_normalize(zap_t *zap, const char *name, char *namenorm)
73 size_t inlen, outlen;
74 int err;
76 inlen = strlen(name) + 1;
77 outlen = ZAP_MAXNAMELEN;
79 err = 0;
80 (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
81 zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL, U8_UNICODE_LATEST,
82 &err);
84 return (err);
87 boolean_t
88 zap_match(zap_name_t *zn, const char *matchname)
90 if (zn->zn_matchtype == MT_FIRST) {
91 char norm[ZAP_MAXNAMELEN];
93 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
94 return (B_FALSE);
96 return (strcmp(zn->zn_name_norm, norm) == 0);
97 } else {
98 /* MT_BEST or MT_EXACT */
99 return (strcmp(zn->zn_name_orij, matchname) == 0);
103 void
104 zap_name_free(zap_name_t *zn)
106 kmem_free(zn, sizeof (zap_name_t));
109 /* XXX combine this with zap_lockdir()? */
110 zap_name_t *
111 zap_name_alloc(zap_t *zap, const char *name, matchtype_t mt)
113 zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
115 zn->zn_zap = zap;
116 zn->zn_name_orij = name;
117 zn->zn_matchtype = mt;
118 if (zap->zap_normflags) {
119 if (zap_normalize(zap, name, zn->zn_normbuf) != 0) {
120 zap_name_free(zn);
121 return (NULL);
123 zn->zn_name_norm = zn->zn_normbuf;
124 } else {
125 if (mt != MT_EXACT) {
126 zap_name_free(zn);
127 return (NULL);
129 zn->zn_name_norm = zn->zn_name_orij;
132 zn->zn_hash = zap_hash(zap, zn->zn_name_norm);
133 return (zn);
136 static void
137 mzap_byteswap(mzap_phys_t *buf, size_t size)
139 int i, max;
140 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
141 buf->mz_salt = BSWAP_64(buf->mz_salt);
142 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
143 max = (size / MZAP_ENT_LEN) - 1;
144 for (i = 0; i < max; i++) {
145 buf->mz_chunk[i].mze_value =
146 BSWAP_64(buf->mz_chunk[i].mze_value);
147 buf->mz_chunk[i].mze_cd =
148 BSWAP_32(buf->mz_chunk[i].mze_cd);
152 void
153 zap_byteswap(void *buf, size_t size)
155 uint64_t block_type;
157 block_type = *(uint64_t *)buf;
159 if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
160 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
161 mzap_byteswap(buf, size);
162 } else {
163 fzap_byteswap(buf, size);
167 static int
168 mze_compare(const void *arg1, const void *arg2)
170 const mzap_ent_t *mze1 = arg1;
171 const mzap_ent_t *mze2 = arg2;
173 if (mze1->mze_hash > mze2->mze_hash)
174 return (+1);
175 if (mze1->mze_hash < mze2->mze_hash)
176 return (-1);
177 if (mze1->mze_phys.mze_cd > mze2->mze_phys.mze_cd)
178 return (+1);
179 if (mze1->mze_phys.mze_cd < mze2->mze_phys.mze_cd)
180 return (-1);
181 return (0);
184 static void
185 mze_insert(zap_t *zap, int chunkid, uint64_t hash, mzap_ent_phys_t *mzep)
187 mzap_ent_t *mze;
189 ASSERT(zap->zap_ismicro);
190 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
191 ASSERT(mzep->mze_cd < ZAP_MAXCD);
193 mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
194 mze->mze_chunkid = chunkid;
195 mze->mze_hash = hash;
196 mze->mze_phys = *mzep;
197 avl_add(&zap->zap_m.zap_avl, mze);
200 static mzap_ent_t *
201 mze_find(zap_name_t *zn)
203 mzap_ent_t mze_tofind;
204 mzap_ent_t *mze;
205 avl_index_t idx;
206 avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
208 ASSERT(zn->zn_zap->zap_ismicro);
209 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
211 if (strlen(zn->zn_name_norm) >= sizeof (mze_tofind.mze_phys.mze_name))
212 return (NULL);
214 mze_tofind.mze_hash = zn->zn_hash;
215 mze_tofind.mze_phys.mze_cd = 0;
217 again:
218 mze = avl_find(avl, &mze_tofind, &idx);
219 if (mze == NULL)
220 mze = avl_nearest(avl, idx, AVL_AFTER);
221 for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
222 if (zap_match(zn, mze->mze_phys.mze_name))
223 return (mze);
225 if (zn->zn_matchtype == MT_BEST) {
226 zn->zn_matchtype = MT_FIRST;
227 goto again;
229 return (NULL);
232 static uint32_t
233 mze_find_unused_cd(zap_t *zap, uint64_t hash)
235 mzap_ent_t mze_tofind;
236 mzap_ent_t *mze;
237 avl_index_t idx;
238 avl_tree_t *avl = &zap->zap_m.zap_avl;
239 uint32_t cd;
241 ASSERT(zap->zap_ismicro);
242 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
244 mze_tofind.mze_hash = hash;
245 mze_tofind.mze_phys.mze_cd = 0;
247 cd = 0;
248 for (mze = avl_find(avl, &mze_tofind, &idx);
249 mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
250 if (mze->mze_phys.mze_cd != cd)
251 break;
252 cd++;
255 return (cd);
258 static void
259 mze_remove(zap_t *zap, mzap_ent_t *mze)
261 ASSERT(zap->zap_ismicro);
262 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
264 avl_remove(&zap->zap_m.zap_avl, mze);
265 kmem_free(mze, sizeof (mzap_ent_t));
268 static void
269 mze_destroy(zap_t *zap)
271 mzap_ent_t *mze;
272 void *avlcookie = NULL;
274 while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
275 kmem_free(mze, sizeof (mzap_ent_t));
276 avl_destroy(&zap->zap_m.zap_avl);
279 static zap_t *
280 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
282 zap_t *winner;
283 zap_t *zap;
284 int i;
286 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
288 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
289 rw_init(&zap->zap_rwlock, 0, 0, 0);
290 rw_enter(&zap->zap_rwlock, RW_WRITER);
291 zap->zap_objset = os;
292 zap->zap_object = obj;
293 zap->zap_dbuf = db;
295 if (*(uint64_t *)db->db_data != ZBT_MICRO) {
296 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
297 zap->zap_f.zap_block_shift = highbit(db->db_size) - 1;
298 } else {
299 zap->zap_ismicro = TRUE;
303 * Make sure that zap_ismicro is set before we let others see
304 * it, because zap_lockdir() checks zap_ismicro without the lock
305 * held.
307 winner = dmu_buf_set_user(db, zap, &zap->zap_m.zap_phys, zap_evict);
309 if (winner != NULL) {
310 rw_exit(&zap->zap_rwlock);
311 rw_destroy(&zap->zap_rwlock);
312 if (!zap->zap_ismicro)
313 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
314 kmem_free(zap, sizeof (zap_t));
315 return (winner);
318 if (zap->zap_ismicro) {
319 zap->zap_salt = zap->zap_m.zap_phys->mz_salt;
320 zap->zap_normflags = zap->zap_m.zap_phys->mz_normflags;
321 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
322 avl_create(&zap->zap_m.zap_avl, mze_compare,
323 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
325 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
326 mzap_ent_phys_t *mze =
327 &zap->zap_m.zap_phys->mz_chunk[i];
328 if (mze->mze_name[0]) {
329 zap_name_t *zn;
331 zap->zap_m.zap_num_entries++;
332 zn = zap_name_alloc(zap, mze->mze_name,
333 MT_EXACT);
334 mze_insert(zap, i, zn->zn_hash, mze);
335 zap_name_free(zn);
338 } else {
339 zap->zap_salt = zap->zap_f.zap_phys->zap_salt;
340 zap->zap_normflags = zap->zap_f.zap_phys->zap_normflags;
342 ASSERT3U(sizeof (struct zap_leaf_header), ==,
343 2*ZAP_LEAF_CHUNKSIZE);
346 * The embedded pointer table should not overlap the
347 * other members.
349 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
350 &zap->zap_f.zap_phys->zap_salt);
353 * The embedded pointer table should end at the end of
354 * the block
356 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
357 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
358 (uintptr_t)zap->zap_f.zap_phys, ==,
359 zap->zap_dbuf->db_size);
361 rw_exit(&zap->zap_rwlock);
362 return (zap);
366 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
367 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
369 zap_t *zap;
370 dmu_buf_t *db;
371 krw_t lt;
372 int err;
374 *zapp = NULL;
376 err = dmu_buf_hold(os, obj, 0, NULL, &db);
377 if (err)
378 return (err);
380 #ifdef ZFS_DEBUG
382 dmu_object_info_t doi;
383 dmu_object_info_from_db(db, &doi);
384 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
386 #endif
388 zap = dmu_buf_get_user(db);
389 if (zap == NULL)
390 zap = mzap_open(os, obj, db);
393 * We're checking zap_ismicro without the lock held, in order to
394 * tell what type of lock we want. Once we have some sort of
395 * lock, see if it really is the right type. In practice this
396 * can only be different if it was upgraded from micro to fat,
397 * and micro wanted WRITER but fat only needs READER.
399 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
400 rw_enter(&zap->zap_rwlock, lt);
401 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
402 /* it was upgraded, now we only need reader */
403 ASSERT(lt == RW_WRITER);
404 ASSERT(RW_READER ==
405 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
406 rw_downgrade(&zap->zap_rwlock);
407 lt = RW_READER;
410 zap->zap_objset = os;
412 if (lt == RW_WRITER)
413 dmu_buf_will_dirty(db, tx);
415 ASSERT3P(zap->zap_dbuf, ==, db);
417 ASSERT(!zap->zap_ismicro ||
418 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
419 if (zap->zap_ismicro && tx && adding &&
420 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
421 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
422 if (newsz > MZAP_MAX_BLKSZ) {
423 dprintf("upgrading obj %llu: num_entries=%u\n",
424 obj, zap->zap_m.zap_num_entries);
425 *zapp = zap;
426 return (mzap_upgrade(zapp, tx));
428 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
429 ASSERT3U(err, ==, 0);
430 zap->zap_m.zap_num_chunks =
431 db->db_size / MZAP_ENT_LEN - 1;
434 *zapp = zap;
435 return (0);
438 void
439 zap_unlockdir(zap_t *zap)
441 rw_exit(&zap->zap_rwlock);
442 dmu_buf_rele(zap->zap_dbuf, NULL);
445 static int
446 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx)
448 mzap_phys_t *mzp;
449 int i, sz, nchunks, err;
450 zap_t *zap = *zapp;
452 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
454 sz = zap->zap_dbuf->db_size;
455 mzp = kmem_alloc(sz, KM_SLEEP);
456 bcopy(zap->zap_dbuf->db_data, mzp, sz);
457 nchunks = zap->zap_m.zap_num_chunks;
459 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
460 1ULL << fzap_default_block_shift, 0, tx);
461 if (err) {
462 kmem_free(mzp, sz);
463 return (err);
466 dprintf("upgrading obj=%llu with %u chunks\n",
467 zap->zap_object, nchunks);
468 /* XXX destroy the avl later, so we can use the stored hash value */
469 mze_destroy(zap);
471 fzap_upgrade(zap, tx);
473 for (i = 0; i < nchunks; i++) {
474 int err;
475 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
476 zap_name_t *zn;
477 if (mze->mze_name[0] == 0)
478 continue;
479 dprintf("adding %s=%llu\n",
480 mze->mze_name, mze->mze_value);
481 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
482 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
483 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
484 zap_name_free(zn);
485 if (err)
486 break;
488 kmem_free(mzp, sz);
489 *zapp = zap;
490 return (err);
493 static void
494 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, dmu_tx_t *tx)
496 dmu_buf_t *db;
497 mzap_phys_t *zp;
499 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db));
501 #ifdef ZFS_DEBUG
503 dmu_object_info_t doi;
504 dmu_object_info_from_db(db, &doi);
505 ASSERT(dmu_ot[doi.doi_type].ot_byteswap == zap_byteswap);
507 #endif
509 dmu_buf_will_dirty(db, tx);
510 zp = db->db_data;
511 zp->mz_block_type = ZBT_MICRO;
512 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
513 zp->mz_normflags = normflags;
514 dmu_buf_rele(db, FTAG);
518 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
519 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
521 return (zap_create_claim_norm(os, obj,
522 0, ot, bonustype, bonuslen, tx));
526 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
527 dmu_object_type_t ot,
528 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
530 int err;
532 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
533 if (err != 0)
534 return (err);
535 mzap_create_impl(os, obj, normflags, tx);
536 return (0);
539 uint64_t
540 zap_create(objset_t *os, dmu_object_type_t ot,
541 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
543 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
546 uint64_t
547 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
548 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
550 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
552 mzap_create_impl(os, obj, normflags, tx);
553 return (obj);
557 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
560 * dmu_object_free will free the object number and free the
561 * data. Freeing the data will cause our pageout function to be
562 * called, which will destroy our data (zap_leaf_t's and zap_t).
565 return (dmu_object_free(os, zapobj, tx));
568 _NOTE(ARGSUSED(0))
569 void
570 zap_evict(dmu_buf_t *db, void *vzap)
572 zap_t *zap = vzap;
574 rw_destroy(&zap->zap_rwlock);
576 if (zap->zap_ismicro)
577 mze_destroy(zap);
578 else
579 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
581 kmem_free(zap, sizeof (zap_t));
585 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
587 zap_t *zap;
588 int err;
590 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
591 if (err)
592 return (err);
593 if (!zap->zap_ismicro) {
594 err = fzap_count(zap, count);
595 } else {
596 *count = zap->zap_m.zap_num_entries;
598 zap_unlockdir(zap);
599 return (err);
603 * zn may be NULL; if not specified, it will be computed if needed.
604 * See also the comment above zap_entry_normalization_conflict().
606 static boolean_t
607 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
609 mzap_ent_t *other;
610 int direction = AVL_BEFORE;
611 boolean_t allocdzn = B_FALSE;
613 if (zap->zap_normflags == 0)
614 return (B_FALSE);
616 again:
617 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
618 other && other->mze_hash == mze->mze_hash;
619 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
621 if (zn == NULL) {
622 zn = zap_name_alloc(zap, mze->mze_phys.mze_name,
623 MT_FIRST);
624 allocdzn = B_TRUE;
626 if (zap_match(zn, other->mze_phys.mze_name)) {
627 if (allocdzn)
628 zap_name_free(zn);
629 return (B_TRUE);
633 if (direction == AVL_BEFORE) {
634 direction = AVL_AFTER;
635 goto again;
638 if (allocdzn)
639 zap_name_free(zn);
640 return (B_FALSE);
644 * Routines for manipulating attributes.
648 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
649 uint64_t integer_size, uint64_t num_integers, void *buf)
651 return (zap_lookup_norm(os, zapobj, name, integer_size,
652 num_integers, buf, MT_EXACT, NULL, 0, NULL));
656 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
657 uint64_t integer_size, uint64_t num_integers, void *buf,
658 matchtype_t mt, char *realname, int rn_len,
659 boolean_t *ncp)
661 zap_t *zap;
662 int err;
663 mzap_ent_t *mze;
664 zap_name_t *zn;
666 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
667 if (err)
668 return (err);
669 zn = zap_name_alloc(zap, name, mt);
670 if (zn == NULL) {
671 zap_unlockdir(zap);
672 return (ENOTSUP);
675 if (!zap->zap_ismicro) {
676 err = fzap_lookup(zn, integer_size, num_integers, buf,
677 realname, rn_len, ncp);
678 } else {
679 mze = mze_find(zn);
680 if (mze == NULL) {
681 err = ENOENT;
682 } else {
683 if (num_integers < 1) {
684 err = EOVERFLOW;
685 } else if (integer_size != 8) {
686 err = EINVAL;
687 } else {
688 *(uint64_t *)buf = mze->mze_phys.mze_value;
689 if (realname != NULL)
690 (void) strlcpy(realname,
691 mze->mze_phys.mze_name, rn_len);
692 if (ncp) {
693 *ncp = mzap_normalization_conflict(zap,
694 zn, mze);
699 zap_name_free(zn);
700 zap_unlockdir(zap);
701 return (err);
705 zap_length(objset_t *os, uint64_t zapobj, const char *name,
706 uint64_t *integer_size, uint64_t *num_integers)
708 zap_t *zap;
709 int err;
710 mzap_ent_t *mze;
711 zap_name_t *zn;
713 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
714 if (err)
715 return (err);
716 zn = zap_name_alloc(zap, name, MT_EXACT);
717 if (zn == NULL) {
718 zap_unlockdir(zap);
719 return (ENOTSUP);
721 if (!zap->zap_ismicro) {
722 err = fzap_length(zn, integer_size, num_integers);
723 } else {
724 mze = mze_find(zn);
725 if (mze == NULL) {
726 err = ENOENT;
727 } else {
728 if (integer_size)
729 *integer_size = 8;
730 if (num_integers)
731 *num_integers = 1;
734 zap_name_free(zn);
735 zap_unlockdir(zap);
736 return (err);
739 static void
740 mzap_addent(zap_name_t *zn, uint64_t value)
742 int i;
743 zap_t *zap = zn->zn_zap;
744 int start = zap->zap_m.zap_alloc_next;
745 uint32_t cd;
747 dprintf("obj=%llu %s=%llu\n", zap->zap_object,
748 zn->zn_name_orij, value);
749 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
751 #ifdef ZFS_DEBUG
752 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
753 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
754 ASSERT(strcmp(zn->zn_name_orij, mze->mze_name) != 0);
756 #endif
758 cd = mze_find_unused_cd(zap, zn->zn_hash);
759 /* given the limited size of the microzap, this can't happen */
760 ASSERT(cd != ZAP_MAXCD);
762 again:
763 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
764 mzap_ent_phys_t *mze = &zap->zap_m.zap_phys->mz_chunk[i];
765 if (mze->mze_name[0] == 0) {
766 mze->mze_value = value;
767 mze->mze_cd = cd;
768 (void) strcpy(mze->mze_name, zn->zn_name_orij);
769 zap->zap_m.zap_num_entries++;
770 zap->zap_m.zap_alloc_next = i+1;
771 if (zap->zap_m.zap_alloc_next ==
772 zap->zap_m.zap_num_chunks)
773 zap->zap_m.zap_alloc_next = 0;
774 mze_insert(zap, i, zn->zn_hash, mze);
775 return;
778 if (start != 0) {
779 start = 0;
780 goto again;
782 ASSERT(!"out of entries!");
786 zap_add(objset_t *os, uint64_t zapobj, const char *name,
787 int integer_size, uint64_t num_integers,
788 const void *val, dmu_tx_t *tx)
790 zap_t *zap;
791 int err;
792 mzap_ent_t *mze;
793 const uint64_t *intval = val;
794 zap_name_t *zn;
796 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
797 if (err)
798 return (err);
799 zn = zap_name_alloc(zap, name, MT_EXACT);
800 if (zn == NULL) {
801 zap_unlockdir(zap);
802 return (ENOTSUP);
804 if (!zap->zap_ismicro) {
805 err = fzap_add(zn, integer_size, num_integers, val, tx);
806 zap = zn->zn_zap; /* fzap_add() may change zap */
807 } else if (integer_size != 8 || num_integers != 1 ||
808 strlen(name) >= MZAP_NAME_LEN) {
809 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
810 zapobj, integer_size, num_integers, name);
811 err = mzap_upgrade(&zn->zn_zap, tx);
812 if (err == 0)
813 err = fzap_add(zn, integer_size, num_integers, val, tx);
814 zap = zn->zn_zap; /* fzap_add() may change zap */
815 } else {
816 mze = mze_find(zn);
817 if (mze != NULL) {
818 err = EEXIST;
819 } else {
820 mzap_addent(zn, *intval);
823 ASSERT(zap == zn->zn_zap);
824 zap_name_free(zn);
825 if (zap != NULL) /* may be NULL if fzap_add() failed */
826 zap_unlockdir(zap);
827 return (err);
831 zap_update(objset_t *os, uint64_t zapobj, const char *name,
832 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
834 zap_t *zap;
835 mzap_ent_t *mze;
836 const uint64_t *intval = val;
837 zap_name_t *zn;
838 int err;
840 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
841 if (err)
842 return (err);
843 zn = zap_name_alloc(zap, name, MT_EXACT);
844 if (zn == NULL) {
845 zap_unlockdir(zap);
846 return (ENOTSUP);
848 if (!zap->zap_ismicro) {
849 err = fzap_update(zn, integer_size, num_integers, val, tx);
850 zap = zn->zn_zap; /* fzap_update() may change zap */
851 } else if (integer_size != 8 || num_integers != 1 ||
852 strlen(name) >= MZAP_NAME_LEN) {
853 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
854 zapobj, integer_size, num_integers, name);
855 err = mzap_upgrade(&zn->zn_zap, tx);
856 if (err == 0)
857 err = fzap_update(zn, integer_size, num_integers,
858 val, tx);
859 zap = zn->zn_zap; /* fzap_update() may change zap */
860 } else {
861 mze = mze_find(zn);
862 if (mze != NULL) {
863 mze->mze_phys.mze_value = *intval;
864 zap->zap_m.zap_phys->mz_chunk
865 [mze->mze_chunkid].mze_value = *intval;
866 } else {
867 mzap_addent(zn, *intval);
870 ASSERT(zap == zn->zn_zap);
871 zap_name_free(zn);
872 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
873 zap_unlockdir(zap);
874 return (err);
878 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
880 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
884 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
885 matchtype_t mt, dmu_tx_t *tx)
887 zap_t *zap;
888 int err;
889 mzap_ent_t *mze;
890 zap_name_t *zn;
892 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
893 if (err)
894 return (err);
895 zn = zap_name_alloc(zap, name, mt);
896 if (zn == NULL) {
897 zap_unlockdir(zap);
898 return (ENOTSUP);
900 if (!zap->zap_ismicro) {
901 err = fzap_remove(zn, tx);
902 } else {
903 mze = mze_find(zn);
904 if (mze == NULL) {
905 err = ENOENT;
906 } else {
907 zap->zap_m.zap_num_entries--;
908 bzero(&zap->zap_m.zap_phys->mz_chunk[mze->mze_chunkid],
909 sizeof (mzap_ent_phys_t));
910 mze_remove(zap, mze);
913 zap_name_free(zn);
914 zap_unlockdir(zap);
915 return (err);
919 * Routines for iterating over the attributes.
923 * We want to keep the high 32 bits of the cursor zero if we can, so
924 * that 32-bit programs can access this. So use a small hash value so
925 * we can fit 4 bits of cd into the 32-bit cursor.
927 * [ 4 zero bits | 32-bit collision differentiator | 28-bit hash value ]
929 void
930 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
931 uint64_t serialized)
933 zc->zc_objset = os;
934 zc->zc_zap = NULL;
935 zc->zc_leaf = NULL;
936 zc->zc_zapobj = zapobj;
937 if (serialized == -1ULL) {
938 zc->zc_hash = -1ULL;
939 zc->zc_cd = 0;
940 } else {
941 zc->zc_hash = serialized << (64-ZAP_HASHBITS);
942 zc->zc_cd = serialized >> ZAP_HASHBITS;
943 if (zc->zc_cd >= ZAP_MAXCD) /* corrupt serialized */
944 zc->zc_cd = 0;
948 void
949 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
951 zap_cursor_init_serialized(zc, os, zapobj, 0);
954 void
955 zap_cursor_fini(zap_cursor_t *zc)
957 if (zc->zc_zap) {
958 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
959 zap_unlockdir(zc->zc_zap);
960 zc->zc_zap = NULL;
962 if (zc->zc_leaf) {
963 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
964 zap_put_leaf(zc->zc_leaf);
965 zc->zc_leaf = NULL;
967 zc->zc_objset = NULL;
970 uint64_t
971 zap_cursor_serialize(zap_cursor_t *zc)
973 if (zc->zc_hash == -1ULL)
974 return (-1ULL);
975 ASSERT((zc->zc_hash & (ZAP_MAXCD-1)) == 0);
976 ASSERT(zc->zc_cd < ZAP_MAXCD);
977 return ((zc->zc_hash >> (64-ZAP_HASHBITS)) |
978 ((uint64_t)zc->zc_cd << ZAP_HASHBITS));
982 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
984 int err;
985 avl_index_t idx;
986 mzap_ent_t mze_tofind;
987 mzap_ent_t *mze;
989 if (zc->zc_hash == -1ULL)
990 return (ENOENT);
992 if (zc->zc_zap == NULL) {
993 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
994 RW_READER, TRUE, FALSE, &zc->zc_zap);
995 if (err)
996 return (err);
997 } else {
998 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1000 if (!zc->zc_zap->zap_ismicro) {
1001 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1002 } else {
1003 err = ENOENT;
1005 mze_tofind.mze_hash = zc->zc_hash;
1006 mze_tofind.mze_phys.mze_cd = zc->zc_cd;
1008 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1009 if (mze == NULL) {
1010 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1011 idx, AVL_AFTER);
1013 if (mze) {
1014 ASSERT(0 == bcmp(&mze->mze_phys,
1015 &zc->zc_zap->zap_m.zap_phys->mz_chunk
1016 [mze->mze_chunkid], sizeof (mze->mze_phys)));
1018 za->za_normalization_conflict =
1019 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1020 za->za_integer_length = 8;
1021 za->za_num_integers = 1;
1022 za->za_first_integer = mze->mze_phys.mze_value;
1023 (void) strcpy(za->za_name, mze->mze_phys.mze_name);
1024 zc->zc_hash = mze->mze_hash;
1025 zc->zc_cd = mze->mze_phys.mze_cd;
1026 err = 0;
1027 } else {
1028 zc->zc_hash = -1ULL;
1031 rw_exit(&zc->zc_zap->zap_rwlock);
1032 return (err);
1035 void
1036 zap_cursor_advance(zap_cursor_t *zc)
1038 if (zc->zc_hash == -1ULL)
1039 return;
1040 zc->zc_cd++;
1041 if (zc->zc_cd >= ZAP_MAXCD) {
1042 zc->zc_cd = 0;
1043 zc->zc_hash += 1ULL<<(64-ZAP_HASHBITS);
1044 if (zc->zc_hash == 0) /* EOF */
1045 zc->zc_hash = -1ULL;
1050 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1052 int err;
1053 zap_t *zap;
1055 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1056 if (err)
1057 return (err);
1059 bzero(zs, sizeof (zap_stats_t));
1061 if (zap->zap_ismicro) {
1062 zs->zs_blocksize = zap->zap_dbuf->db_size;
1063 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1064 zs->zs_num_blocks = 1;
1065 } else {
1066 fzap_get_stats(zap, zs);
1068 zap_unlockdir(zap);
1069 return (0);