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