Print this page
6842 Fix empty xattr dir causing lockup
Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Dan McDonald <danmcd@omniti.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
| 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))
|
↓ open down ↓ |
359 lines elided |
↑ open up ↑ |
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 + uint64_t *zap_hdr = (uint64_t *)db->db_data;
371 + uint64_t zap_block_type = zap_hdr[0];
372 + uint64_t zap_magic = zap_hdr[1];
370 373
371 374 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
372 375
373 376 zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
374 377 rw_init(&zap->zap_rwlock, 0, 0, 0);
375 378 rw_enter(&zap->zap_rwlock, RW_WRITER);
376 379 zap->zap_objset = os;
377 380 zap->zap_object = obj;
378 381 zap->zap_dbuf = db;
379 382
380 - if (*(uint64_t *)db->db_data != ZBT_MICRO) {
383 + if (zap_block_type != ZBT_MICRO) {
381 384 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
382 385 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
386 + if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
387 + winner = NULL; /* No actual winner here... */
388 + goto handle_winner;
389 + }
383 390 } else {
384 391 zap->zap_ismicro = TRUE;
385 392 }
386 393
387 394 /*
388 395 * Make sure that zap_ismicro is set before we let others see
389 396 * it, because zap_lockdir() checks zap_ismicro without the lock
390 397 * held.
391 398 */
392 399 dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
393 400 winner = dmu_buf_set_user(db, &zap->zap_dbu);
394 401
395 - if (winner != NULL) {
396 - rw_exit(&zap->zap_rwlock);
397 - rw_destroy(&zap->zap_rwlock);
398 - if (!zap->zap_ismicro)
399 - mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
400 - kmem_free(zap, sizeof (zap_t));
401 - return (winner);
402 - }
402 + if (winner != NULL)
403 + goto handle_winner;
403 404
404 405 if (zap->zap_ismicro) {
405 406 zap->zap_salt = zap_m_phys(zap)->mz_salt;
406 407 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
407 408 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
408 409 avl_create(&zap->zap_m.zap_avl, mze_compare,
409 410 sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
410 411
411 412 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
412 413 mzap_ent_phys_t *mze =
413 414 &zap_m_phys(zap)->mz_chunk[i];
414 415 if (mze->mze_name[0]) {
415 416 zap_name_t *zn;
416 417
417 418 zap->zap_m.zap_num_entries++;
418 419 zn = zap_name_alloc(zap, mze->mze_name,
419 420 MT_EXACT);
420 421 mze_insert(zap, i, zn->zn_hash);
421 422 zap_name_free(zn);
422 423 }
423 424 }
424 425 } else {
425 426 zap->zap_salt = zap_f_phys(zap)->zap_salt;
426 427 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
427 428
428 429 ASSERT3U(sizeof (struct zap_leaf_header), ==,
429 430 2*ZAP_LEAF_CHUNKSIZE);
430 431
431 432 /*
432 433 * The embedded pointer table should not overlap the
433 434 * other members.
434 435 */
435 436 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
436 437 &zap_f_phys(zap)->zap_salt);
437 438
438 439 /*
|
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
439 440 * The embedded pointer table should end at the end of
440 441 * the block
441 442 */
442 443 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
443 444 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
444 445 (uintptr_t)zap_f_phys(zap), ==,
445 446 zap->zap_dbuf->db_size);
446 447 }
447 448 rw_exit(&zap->zap_rwlock);
448 449 return (zap);
450 +
451 +handle_winner:
452 + rw_exit(&zap->zap_rwlock);
453 + rw_destroy(&zap->zap_rwlock);
454 + if (!zap->zap_ismicro)
455 + mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
456 + kmem_free(zap, sizeof (zap_t));
457 + return (winner);
449 458 }
450 459
451 460 int
452 461 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
453 462 krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
454 463 {
455 464 zap_t *zap;
456 465 dmu_buf_t *db;
457 466 krw_t lt;
458 467 int err;
459 468
460 469 *zapp = NULL;
461 470
462 471 err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
463 472 if (err)
464 473 return (err);
|
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
465 474
466 475 #ifdef ZFS_DEBUG
467 476 {
468 477 dmu_object_info_t doi;
469 478 dmu_object_info_from_db(db, &doi);
470 479 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
471 480 }
472 481 #endif
473 482
474 483 zap = dmu_buf_get_user(db);
475 - if (zap == NULL)
484 + if (zap == NULL) {
476 485 zap = mzap_open(os, obj, db);
486 + if (zap == NULL) {
487 + /*
488 + * mzap_open() didn't like what it saw on-disk.
489 + * Check for corruption!
490 + */
491 + dmu_buf_rele(db, NULL);
492 + return (SET_ERROR(EIO));
493 + }
494 + }
477 495
478 496 /*
479 497 * We're checking zap_ismicro without the lock held, in order to
480 498 * tell what type of lock we want. Once we have some sort of
481 499 * lock, see if it really is the right type. In practice this
482 500 * can only be different if it was upgraded from micro to fat,
483 501 * and micro wanted WRITER but fat only needs READER.
484 502 */
485 503 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
486 504 rw_enter(&zap->zap_rwlock, lt);
487 505 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
488 506 /* it was upgraded, now we only need reader */
489 507 ASSERT(lt == RW_WRITER);
490 508 ASSERT(RW_READER ==
491 509 (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
492 510 rw_downgrade(&zap->zap_rwlock);
493 511 lt = RW_READER;
494 512 }
495 513
496 514 zap->zap_objset = os;
497 515
498 516 if (lt == RW_WRITER)
499 517 dmu_buf_will_dirty(db, tx);
500 518
501 519 ASSERT3P(zap->zap_dbuf, ==, db);
502 520
503 521 ASSERT(!zap->zap_ismicro ||
504 522 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
505 523 if (zap->zap_ismicro && tx && adding &&
506 524 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
507 525 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
508 526 if (newsz > MZAP_MAX_BLKSZ) {
509 527 dprintf("upgrading obj %llu: num_entries=%u\n",
510 528 obj, zap->zap_m.zap_num_entries);
511 529 *zapp = zap;
512 530 return (mzap_upgrade(zapp, tx, 0));
513 531 }
514 532 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
515 533 ASSERT0(err);
516 534 zap->zap_m.zap_num_chunks =
517 535 db->db_size / MZAP_ENT_LEN - 1;
518 536 }
519 537
520 538 *zapp = zap;
521 539 return (0);
522 540 }
523 541
524 542 void
525 543 zap_unlockdir(zap_t *zap)
526 544 {
527 545 rw_exit(&zap->zap_rwlock);
528 546 dmu_buf_rele(zap->zap_dbuf, NULL);
529 547 }
530 548
531 549 static int
532 550 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
533 551 {
534 552 mzap_phys_t *mzp;
535 553 int i, sz, nchunks;
536 554 int err = 0;
537 555 zap_t *zap = *zapp;
538 556
539 557 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
540 558
541 559 sz = zap->zap_dbuf->db_size;
542 560 mzp = zio_buf_alloc(sz);
543 561 bcopy(zap->zap_dbuf->db_data, mzp, sz);
544 562 nchunks = zap->zap_m.zap_num_chunks;
545 563
546 564 if (!flags) {
547 565 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
548 566 1ULL << fzap_default_block_shift, 0, tx);
549 567 if (err) {
550 568 zio_buf_free(mzp, sz);
551 569 return (err);
552 570 }
553 571 }
554 572
555 573 dprintf("upgrading obj=%llu with %u chunks\n",
556 574 zap->zap_object, nchunks);
557 575 /* XXX destroy the avl later, so we can use the stored hash value */
558 576 mze_destroy(zap);
559 577
560 578 fzap_upgrade(zap, tx, flags);
561 579
562 580 for (i = 0; i < nchunks; i++) {
563 581 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
564 582 zap_name_t *zn;
565 583 if (mze->mze_name[0] == 0)
566 584 continue;
567 585 dprintf("adding %s=%llu\n",
568 586 mze->mze_name, mze->mze_value);
569 587 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
570 588 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
571 589 zap = zn->zn_zap; /* fzap_add_cd() may change zap */
572 590 zap_name_free(zn);
573 591 if (err)
574 592 break;
575 593 }
576 594 zio_buf_free(mzp, sz);
577 595 *zapp = zap;
578 596 return (err);
579 597 }
580 598
581 599 void
582 600 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
583 601 dmu_tx_t *tx)
584 602 {
585 603 dmu_buf_t *db;
586 604 mzap_phys_t *zp;
587 605
588 606 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
589 607
590 608 #ifdef ZFS_DEBUG
591 609 {
592 610 dmu_object_info_t doi;
593 611 dmu_object_info_from_db(db, &doi);
594 612 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
595 613 }
596 614 #endif
597 615
598 616 dmu_buf_will_dirty(db, tx);
599 617 zp = db->db_data;
600 618 zp->mz_block_type = ZBT_MICRO;
601 619 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
602 620 zp->mz_normflags = normflags;
603 621 dmu_buf_rele(db, FTAG);
604 622
605 623 if (flags != 0) {
606 624 zap_t *zap;
607 625 /* Only fat zap supports flags; upgrade immediately. */
608 626 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
609 627 B_FALSE, B_FALSE, &zap));
610 628 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
611 629 zap_unlockdir(zap);
612 630 }
613 631 }
614 632
615 633 int
616 634 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
617 635 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
618 636 {
619 637 return (zap_create_claim_norm(os, obj,
620 638 0, ot, bonustype, bonuslen, tx));
621 639 }
622 640
623 641 int
624 642 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
625 643 dmu_object_type_t ot,
626 644 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
627 645 {
628 646 int err;
629 647
630 648 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
631 649 if (err != 0)
632 650 return (err);
633 651 mzap_create_impl(os, obj, normflags, 0, tx);
634 652 return (0);
635 653 }
636 654
637 655 uint64_t
638 656 zap_create(objset_t *os, dmu_object_type_t ot,
639 657 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
640 658 {
641 659 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
642 660 }
643 661
644 662 uint64_t
645 663 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
646 664 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
647 665 {
648 666 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
649 667
650 668 mzap_create_impl(os, obj, normflags, 0, tx);
651 669 return (obj);
652 670 }
653 671
654 672 uint64_t
655 673 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
656 674 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
657 675 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
658 676 {
659 677 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
660 678
661 679 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
662 680 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
663 681 indirect_blockshift >= SPA_MINBLOCKSHIFT &&
664 682 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
665 683
666 684 VERIFY(dmu_object_set_blocksize(os, obj,
667 685 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
668 686
669 687 mzap_create_impl(os, obj, normflags, flags, tx);
670 688 return (obj);
671 689 }
672 690
673 691 int
674 692 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
675 693 {
676 694 /*
677 695 * dmu_object_free will free the object number and free the
678 696 * data. Freeing the data will cause our pageout function to be
679 697 * called, which will destroy our data (zap_leaf_t's and zap_t).
680 698 */
681 699
682 700 return (dmu_object_free(os, zapobj, tx));
683 701 }
684 702
685 703 void
686 704 zap_evict(void *dbu)
687 705 {
688 706 zap_t *zap = dbu;
689 707
690 708 rw_destroy(&zap->zap_rwlock);
691 709
692 710 if (zap->zap_ismicro)
693 711 mze_destroy(zap);
694 712 else
695 713 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
696 714
697 715 kmem_free(zap, sizeof (zap_t));
698 716 }
699 717
700 718 int
701 719 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
702 720 {
703 721 zap_t *zap;
704 722 int err;
705 723
706 724 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
707 725 if (err)
708 726 return (err);
709 727 if (!zap->zap_ismicro) {
710 728 err = fzap_count(zap, count);
711 729 } else {
712 730 *count = zap->zap_m.zap_num_entries;
713 731 }
714 732 zap_unlockdir(zap);
715 733 return (err);
716 734 }
717 735
718 736 /*
719 737 * zn may be NULL; if not specified, it will be computed if needed.
720 738 * See also the comment above zap_entry_normalization_conflict().
721 739 */
722 740 static boolean_t
723 741 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
724 742 {
725 743 mzap_ent_t *other;
726 744 int direction = AVL_BEFORE;
727 745 boolean_t allocdzn = B_FALSE;
728 746
729 747 if (zap->zap_normflags == 0)
730 748 return (B_FALSE);
731 749
732 750 again:
733 751 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
734 752 other && other->mze_hash == mze->mze_hash;
735 753 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
736 754
737 755 if (zn == NULL) {
738 756 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
739 757 MT_FIRST);
740 758 allocdzn = B_TRUE;
741 759 }
742 760 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
743 761 if (allocdzn)
744 762 zap_name_free(zn);
745 763 return (B_TRUE);
746 764 }
747 765 }
748 766
749 767 if (direction == AVL_BEFORE) {
750 768 direction = AVL_AFTER;
751 769 goto again;
752 770 }
753 771
754 772 if (allocdzn)
755 773 zap_name_free(zn);
756 774 return (B_FALSE);
757 775 }
758 776
759 777 /*
760 778 * Routines for manipulating attributes.
761 779 */
762 780
763 781 int
764 782 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
765 783 uint64_t integer_size, uint64_t num_integers, void *buf)
766 784 {
767 785 return (zap_lookup_norm(os, zapobj, name, integer_size,
768 786 num_integers, buf, MT_EXACT, NULL, 0, NULL));
769 787 }
770 788
771 789 int
772 790 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
773 791 uint64_t integer_size, uint64_t num_integers, void *buf,
774 792 matchtype_t mt, char *realname, int rn_len,
775 793 boolean_t *ncp)
776 794 {
777 795 zap_t *zap;
778 796 int err;
779 797 mzap_ent_t *mze;
780 798 zap_name_t *zn;
781 799
782 800 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
783 801 if (err)
784 802 return (err);
785 803 zn = zap_name_alloc(zap, name, mt);
786 804 if (zn == NULL) {
787 805 zap_unlockdir(zap);
788 806 return (SET_ERROR(ENOTSUP));
789 807 }
790 808
791 809 if (!zap->zap_ismicro) {
792 810 err = fzap_lookup(zn, integer_size, num_integers, buf,
793 811 realname, rn_len, ncp);
794 812 } else {
795 813 mze = mze_find(zn);
796 814 if (mze == NULL) {
797 815 err = SET_ERROR(ENOENT);
798 816 } else {
799 817 if (num_integers < 1) {
800 818 err = SET_ERROR(EOVERFLOW);
801 819 } else if (integer_size != 8) {
802 820 err = SET_ERROR(EINVAL);
803 821 } else {
804 822 *(uint64_t *)buf =
805 823 MZE_PHYS(zap, mze)->mze_value;
806 824 (void) strlcpy(realname,
807 825 MZE_PHYS(zap, mze)->mze_name, rn_len);
808 826 if (ncp) {
809 827 *ncp = mzap_normalization_conflict(zap,
810 828 zn, mze);
811 829 }
812 830 }
813 831 }
814 832 }
815 833 zap_name_free(zn);
816 834 zap_unlockdir(zap);
817 835 return (err);
818 836 }
819 837
820 838 int
821 839 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
822 840 int key_numints)
823 841 {
824 842 zap_t *zap;
825 843 int err;
826 844 zap_name_t *zn;
827 845
828 846 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
829 847 if (err)
830 848 return (err);
831 849 zn = zap_name_alloc_uint64(zap, key, key_numints);
832 850 if (zn == NULL) {
833 851 zap_unlockdir(zap);
834 852 return (SET_ERROR(ENOTSUP));
835 853 }
836 854
837 855 fzap_prefetch(zn);
838 856 zap_name_free(zn);
839 857 zap_unlockdir(zap);
840 858 return (err);
841 859 }
842 860
843 861 int
844 862 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
845 863 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
846 864 {
847 865 zap_t *zap;
848 866 int err;
849 867 zap_name_t *zn;
850 868
851 869 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
852 870 if (err)
853 871 return (err);
854 872 zn = zap_name_alloc_uint64(zap, key, key_numints);
855 873 if (zn == NULL) {
856 874 zap_unlockdir(zap);
857 875 return (SET_ERROR(ENOTSUP));
858 876 }
859 877
860 878 err = fzap_lookup(zn, integer_size, num_integers, buf,
861 879 NULL, 0, NULL);
862 880 zap_name_free(zn);
863 881 zap_unlockdir(zap);
864 882 return (err);
865 883 }
866 884
867 885 int
868 886 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
869 887 {
870 888 int err = zap_lookup_norm(os, zapobj, name, 0,
871 889 0, NULL, MT_EXACT, NULL, 0, NULL);
872 890 if (err == EOVERFLOW || err == EINVAL)
873 891 err = 0; /* found, but skipped reading the value */
874 892 return (err);
875 893 }
876 894
877 895 int
878 896 zap_length(objset_t *os, uint64_t zapobj, const char *name,
879 897 uint64_t *integer_size, uint64_t *num_integers)
880 898 {
881 899 zap_t *zap;
882 900 int err;
883 901 mzap_ent_t *mze;
884 902 zap_name_t *zn;
885 903
886 904 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
887 905 if (err)
888 906 return (err);
889 907 zn = zap_name_alloc(zap, name, MT_EXACT);
890 908 if (zn == NULL) {
891 909 zap_unlockdir(zap);
892 910 return (SET_ERROR(ENOTSUP));
893 911 }
894 912 if (!zap->zap_ismicro) {
895 913 err = fzap_length(zn, integer_size, num_integers);
896 914 } else {
897 915 mze = mze_find(zn);
898 916 if (mze == NULL) {
899 917 err = SET_ERROR(ENOENT);
900 918 } else {
901 919 if (integer_size)
902 920 *integer_size = 8;
903 921 if (num_integers)
904 922 *num_integers = 1;
905 923 }
906 924 }
907 925 zap_name_free(zn);
908 926 zap_unlockdir(zap);
909 927 return (err);
910 928 }
911 929
912 930 int
913 931 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
914 932 int key_numints, uint64_t *integer_size, uint64_t *num_integers)
915 933 {
916 934 zap_t *zap;
917 935 int err;
918 936 zap_name_t *zn;
919 937
920 938 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
921 939 if (err)
922 940 return (err);
923 941 zn = zap_name_alloc_uint64(zap, key, key_numints);
924 942 if (zn == NULL) {
925 943 zap_unlockdir(zap);
926 944 return (SET_ERROR(ENOTSUP));
927 945 }
928 946 err = fzap_length(zn, integer_size, num_integers);
929 947 zap_name_free(zn);
930 948 zap_unlockdir(zap);
931 949 return (err);
932 950 }
933 951
934 952 static void
935 953 mzap_addent(zap_name_t *zn, uint64_t value)
936 954 {
937 955 int i;
938 956 zap_t *zap = zn->zn_zap;
939 957 int start = zap->zap_m.zap_alloc_next;
940 958 uint32_t cd;
941 959
942 960 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
943 961
944 962 #ifdef ZFS_DEBUG
945 963 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
946 964 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
947 965 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
948 966 }
949 967 #endif
950 968
951 969 cd = mze_find_unused_cd(zap, zn->zn_hash);
952 970 /* given the limited size of the microzap, this can't happen */
953 971 ASSERT(cd < zap_maxcd(zap));
954 972
955 973 again:
956 974 for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
957 975 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
958 976 if (mze->mze_name[0] == 0) {
959 977 mze->mze_value = value;
960 978 mze->mze_cd = cd;
961 979 (void) strcpy(mze->mze_name, zn->zn_key_orig);
962 980 zap->zap_m.zap_num_entries++;
963 981 zap->zap_m.zap_alloc_next = i+1;
964 982 if (zap->zap_m.zap_alloc_next ==
965 983 zap->zap_m.zap_num_chunks)
966 984 zap->zap_m.zap_alloc_next = 0;
967 985 mze_insert(zap, i, zn->zn_hash);
968 986 return;
969 987 }
970 988 }
971 989 if (start != 0) {
972 990 start = 0;
973 991 goto again;
974 992 }
975 993 ASSERT(!"out of entries!");
976 994 }
977 995
978 996 int
979 997 zap_add(objset_t *os, uint64_t zapobj, const char *key,
980 998 int integer_size, uint64_t num_integers,
981 999 const void *val, dmu_tx_t *tx)
982 1000 {
983 1001 zap_t *zap;
984 1002 int err;
985 1003 mzap_ent_t *mze;
986 1004 const uint64_t *intval = val;
987 1005 zap_name_t *zn;
988 1006
989 1007 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
990 1008 if (err)
991 1009 return (err);
992 1010 zn = zap_name_alloc(zap, key, MT_EXACT);
993 1011 if (zn == NULL) {
994 1012 zap_unlockdir(zap);
995 1013 return (SET_ERROR(ENOTSUP));
996 1014 }
997 1015 if (!zap->zap_ismicro) {
998 1016 err = fzap_add(zn, integer_size, num_integers, val, tx);
999 1017 zap = zn->zn_zap; /* fzap_add() may change zap */
1000 1018 } else if (integer_size != 8 || num_integers != 1 ||
1001 1019 strlen(key) >= MZAP_NAME_LEN) {
1002 1020 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1003 1021 if (err == 0)
1004 1022 err = fzap_add(zn, integer_size, num_integers, val, tx);
1005 1023 zap = zn->zn_zap; /* fzap_add() may change zap */
1006 1024 } else {
1007 1025 mze = mze_find(zn);
1008 1026 if (mze != NULL) {
1009 1027 err = SET_ERROR(EEXIST);
1010 1028 } else {
1011 1029 mzap_addent(zn, *intval);
1012 1030 }
1013 1031 }
1014 1032 ASSERT(zap == zn->zn_zap);
1015 1033 zap_name_free(zn);
1016 1034 if (zap != NULL) /* may be NULL if fzap_add() failed */
1017 1035 zap_unlockdir(zap);
1018 1036 return (err);
1019 1037 }
1020 1038
1021 1039 int
1022 1040 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1023 1041 int key_numints, int integer_size, uint64_t num_integers,
1024 1042 const void *val, dmu_tx_t *tx)
1025 1043 {
1026 1044 zap_t *zap;
1027 1045 int err;
1028 1046 zap_name_t *zn;
1029 1047
1030 1048 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1031 1049 if (err)
1032 1050 return (err);
1033 1051 zn = zap_name_alloc_uint64(zap, key, key_numints);
1034 1052 if (zn == NULL) {
1035 1053 zap_unlockdir(zap);
1036 1054 return (SET_ERROR(ENOTSUP));
1037 1055 }
1038 1056 err = fzap_add(zn, integer_size, num_integers, val, tx);
1039 1057 zap = zn->zn_zap; /* fzap_add() may change zap */
1040 1058 zap_name_free(zn);
1041 1059 if (zap != NULL) /* may be NULL if fzap_add() failed */
1042 1060 zap_unlockdir(zap);
1043 1061 return (err);
1044 1062 }
1045 1063
1046 1064 int
1047 1065 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1048 1066 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1049 1067 {
1050 1068 zap_t *zap;
1051 1069 mzap_ent_t *mze;
1052 1070 uint64_t oldval;
1053 1071 const uint64_t *intval = val;
1054 1072 zap_name_t *zn;
1055 1073 int err;
1056 1074
1057 1075 #ifdef ZFS_DEBUG
1058 1076 /*
1059 1077 * If there is an old value, it shouldn't change across the
1060 1078 * lockdir (eg, due to bprewrite's xlation).
1061 1079 */
1062 1080 if (integer_size == 8 && num_integers == 1)
1063 1081 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1064 1082 #endif
1065 1083
1066 1084 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1067 1085 if (err)
1068 1086 return (err);
1069 1087 zn = zap_name_alloc(zap, name, MT_EXACT);
1070 1088 if (zn == NULL) {
1071 1089 zap_unlockdir(zap);
1072 1090 return (SET_ERROR(ENOTSUP));
1073 1091 }
1074 1092 if (!zap->zap_ismicro) {
1075 1093 err = fzap_update(zn, integer_size, num_integers, val, tx);
1076 1094 zap = zn->zn_zap; /* fzap_update() may change zap */
1077 1095 } else if (integer_size != 8 || num_integers != 1 ||
1078 1096 strlen(name) >= MZAP_NAME_LEN) {
1079 1097 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1080 1098 zapobj, integer_size, num_integers, name);
1081 1099 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1082 1100 if (err == 0)
1083 1101 err = fzap_update(zn, integer_size, num_integers,
1084 1102 val, tx);
1085 1103 zap = zn->zn_zap; /* fzap_update() may change zap */
1086 1104 } else {
1087 1105 mze = mze_find(zn);
1088 1106 if (mze != NULL) {
1089 1107 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1090 1108 MZE_PHYS(zap, mze)->mze_value = *intval;
1091 1109 } else {
1092 1110 mzap_addent(zn, *intval);
1093 1111 }
1094 1112 }
1095 1113 ASSERT(zap == zn->zn_zap);
1096 1114 zap_name_free(zn);
1097 1115 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1098 1116 zap_unlockdir(zap);
1099 1117 return (err);
1100 1118 }
1101 1119
1102 1120 int
1103 1121 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1104 1122 int key_numints,
1105 1123 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1106 1124 {
1107 1125 zap_t *zap;
1108 1126 zap_name_t *zn;
1109 1127 int err;
1110 1128
1111 1129 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1112 1130 if (err)
1113 1131 return (err);
1114 1132 zn = zap_name_alloc_uint64(zap, key, key_numints);
1115 1133 if (zn == NULL) {
1116 1134 zap_unlockdir(zap);
1117 1135 return (SET_ERROR(ENOTSUP));
1118 1136 }
1119 1137 err = fzap_update(zn, integer_size, num_integers, val, tx);
1120 1138 zap = zn->zn_zap; /* fzap_update() may change zap */
1121 1139 zap_name_free(zn);
1122 1140 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */
1123 1141 zap_unlockdir(zap);
1124 1142 return (err);
1125 1143 }
1126 1144
1127 1145 int
1128 1146 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1129 1147 {
1130 1148 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1131 1149 }
1132 1150
1133 1151 int
1134 1152 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1135 1153 matchtype_t mt, dmu_tx_t *tx)
1136 1154 {
1137 1155 zap_t *zap;
1138 1156 int err;
1139 1157 mzap_ent_t *mze;
1140 1158 zap_name_t *zn;
1141 1159
1142 1160 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1143 1161 if (err)
1144 1162 return (err);
1145 1163 zn = zap_name_alloc(zap, name, mt);
1146 1164 if (zn == NULL) {
1147 1165 zap_unlockdir(zap);
1148 1166 return (SET_ERROR(ENOTSUP));
1149 1167 }
1150 1168 if (!zap->zap_ismicro) {
1151 1169 err = fzap_remove(zn, tx);
1152 1170 } else {
1153 1171 mze = mze_find(zn);
1154 1172 if (mze == NULL) {
1155 1173 err = SET_ERROR(ENOENT);
1156 1174 } else {
1157 1175 zap->zap_m.zap_num_entries--;
1158 1176 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1159 1177 sizeof (mzap_ent_phys_t));
1160 1178 mze_remove(zap, mze);
1161 1179 }
1162 1180 }
1163 1181 zap_name_free(zn);
1164 1182 zap_unlockdir(zap);
1165 1183 return (err);
1166 1184 }
1167 1185
1168 1186 int
1169 1187 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1170 1188 int key_numints, dmu_tx_t *tx)
1171 1189 {
1172 1190 zap_t *zap;
1173 1191 int err;
1174 1192 zap_name_t *zn;
1175 1193
1176 1194 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1177 1195 if (err)
1178 1196 return (err);
1179 1197 zn = zap_name_alloc_uint64(zap, key, key_numints);
1180 1198 if (zn == NULL) {
1181 1199 zap_unlockdir(zap);
1182 1200 return (SET_ERROR(ENOTSUP));
1183 1201 }
1184 1202 err = fzap_remove(zn, tx);
1185 1203 zap_name_free(zn);
1186 1204 zap_unlockdir(zap);
1187 1205 return (err);
1188 1206 }
1189 1207
1190 1208 /*
1191 1209 * Routines for iterating over the attributes.
1192 1210 */
1193 1211
1194 1212 void
1195 1213 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1196 1214 uint64_t serialized)
1197 1215 {
1198 1216 zc->zc_objset = os;
1199 1217 zc->zc_zap = NULL;
1200 1218 zc->zc_leaf = NULL;
1201 1219 zc->zc_zapobj = zapobj;
1202 1220 zc->zc_serialized = serialized;
1203 1221 zc->zc_hash = 0;
1204 1222 zc->zc_cd = 0;
1205 1223 }
1206 1224
1207 1225 void
1208 1226 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1209 1227 {
1210 1228 zap_cursor_init_serialized(zc, os, zapobj, 0);
1211 1229 }
1212 1230
1213 1231 void
1214 1232 zap_cursor_fini(zap_cursor_t *zc)
1215 1233 {
1216 1234 if (zc->zc_zap) {
1217 1235 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1218 1236 zap_unlockdir(zc->zc_zap);
1219 1237 zc->zc_zap = NULL;
1220 1238 }
1221 1239 if (zc->zc_leaf) {
1222 1240 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1223 1241 zap_put_leaf(zc->zc_leaf);
1224 1242 zc->zc_leaf = NULL;
1225 1243 }
1226 1244 zc->zc_objset = NULL;
1227 1245 }
1228 1246
1229 1247 uint64_t
1230 1248 zap_cursor_serialize(zap_cursor_t *zc)
1231 1249 {
1232 1250 if (zc->zc_hash == -1ULL)
1233 1251 return (-1ULL);
1234 1252 if (zc->zc_zap == NULL)
1235 1253 return (zc->zc_serialized);
1236 1254 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1237 1255 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1238 1256
1239 1257 /*
1240 1258 * We want to keep the high 32 bits of the cursor zero if we can, so
1241 1259 * that 32-bit programs can access this. So usually use a small
1242 1260 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1243 1261 * of the cursor.
1244 1262 *
1245 1263 * [ collision differentiator | zap_hashbits()-bit hash value ]
1246 1264 */
1247 1265 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1248 1266 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1249 1267 }
1250 1268
1251 1269 int
1252 1270 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1253 1271 {
1254 1272 int err;
1255 1273 avl_index_t idx;
1256 1274 mzap_ent_t mze_tofind;
1257 1275 mzap_ent_t *mze;
1258 1276
1259 1277 if (zc->zc_hash == -1ULL)
1260 1278 return (SET_ERROR(ENOENT));
1261 1279
1262 1280 if (zc->zc_zap == NULL) {
1263 1281 int hb;
1264 1282 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1265 1283 RW_READER, TRUE, FALSE, &zc->zc_zap);
1266 1284 if (err)
1267 1285 return (err);
1268 1286
1269 1287 /*
1270 1288 * To support zap_cursor_init_serialized, advance, retrieve,
1271 1289 * we must add to the existing zc_cd, which may already
1272 1290 * be 1 due to the zap_cursor_advance.
1273 1291 */
1274 1292 ASSERT(zc->zc_hash == 0);
1275 1293 hb = zap_hashbits(zc->zc_zap);
1276 1294 zc->zc_hash = zc->zc_serialized << (64 - hb);
1277 1295 zc->zc_cd += zc->zc_serialized >> hb;
1278 1296 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1279 1297 zc->zc_cd = 0;
1280 1298 } else {
1281 1299 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1282 1300 }
1283 1301 if (!zc->zc_zap->zap_ismicro) {
1284 1302 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1285 1303 } else {
1286 1304 mze_tofind.mze_hash = zc->zc_hash;
1287 1305 mze_tofind.mze_cd = zc->zc_cd;
1288 1306
1289 1307 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1290 1308 if (mze == NULL) {
1291 1309 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1292 1310 idx, AVL_AFTER);
1293 1311 }
1294 1312 if (mze) {
1295 1313 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1296 1314 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1297 1315 za->za_normalization_conflict =
1298 1316 mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1299 1317 za->za_integer_length = 8;
1300 1318 za->za_num_integers = 1;
1301 1319 za->za_first_integer = mzep->mze_value;
1302 1320 (void) strcpy(za->za_name, mzep->mze_name);
1303 1321 zc->zc_hash = mze->mze_hash;
1304 1322 zc->zc_cd = mze->mze_cd;
1305 1323 err = 0;
1306 1324 } else {
1307 1325 zc->zc_hash = -1ULL;
1308 1326 err = SET_ERROR(ENOENT);
1309 1327 }
1310 1328 }
1311 1329 rw_exit(&zc->zc_zap->zap_rwlock);
1312 1330 return (err);
1313 1331 }
1314 1332
1315 1333 void
1316 1334 zap_cursor_advance(zap_cursor_t *zc)
1317 1335 {
1318 1336 if (zc->zc_hash == -1ULL)
1319 1337 return;
1320 1338 zc->zc_cd++;
1321 1339 }
1322 1340
1323 1341 int
1324 1342 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1325 1343 {
1326 1344 int err;
1327 1345 zap_t *zap;
1328 1346
1329 1347 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1330 1348 if (err)
1331 1349 return (err);
1332 1350
1333 1351 bzero(zs, sizeof (zap_stats_t));
1334 1352
1335 1353 if (zap->zap_ismicro) {
1336 1354 zs->zs_blocksize = zap->zap_dbuf->db_size;
1337 1355 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1338 1356 zs->zs_num_blocks = 1;
1339 1357 } else {
1340 1358 fzap_get_stats(zap, zs);
1341 1359 }
1342 1360 zap_unlockdir(zap);
1343 1361 return (0);
1344 1362 }
1345 1363
1346 1364 int
1347 1365 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1348 1366 uint64_t *towrite, uint64_t *tooverwrite)
1349 1367 {
1350 1368 zap_t *zap;
1351 1369 int err = 0;
1352 1370
1353 1371 /*
1354 1372 * Since, we don't have a name, we cannot figure out which blocks will
1355 1373 * be affected in this operation. So, account for the worst case :
1356 1374 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1357 1375 * - 4 new blocks written if adding:
1358 1376 * - 2 blocks for possibly split leaves,
1359 1377 * - 2 grown ptrtbl blocks
1360 1378 *
1361 1379 * This also accomodates the case where an add operation to a fairly
1362 1380 * large microzap results in a promotion to fatzap.
1363 1381 */
1364 1382 if (name == NULL) {
1365 1383 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1366 1384 return (err);
1367 1385 }
1368 1386
1369 1387 /*
1370 1388 * We lock the zap with adding == FALSE. Because, if we pass
1371 1389 * the actual value of add, it could trigger a mzap_upgrade().
1372 1390 * At present we are just evaluating the possibility of this operation
1373 1391 * and hence we donot want to trigger an upgrade.
1374 1392 */
1375 1393 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1376 1394 if (err)
1377 1395 return (err);
1378 1396
1379 1397 if (!zap->zap_ismicro) {
1380 1398 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1381 1399 if (zn) {
1382 1400 err = fzap_count_write(zn, add, towrite,
1383 1401 tooverwrite);
1384 1402 zap_name_free(zn);
1385 1403 } else {
1386 1404 /*
1387 1405 * We treat this case as similar to (name == NULL)
1388 1406 */
1389 1407 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1390 1408 }
1391 1409 } else {
1392 1410 /*
1393 1411 * We are here if (name != NULL) and this is a micro-zap.
1394 1412 * We account for the header block depending on whether it
1395 1413 * is freeable.
1396 1414 *
1397 1415 * Incase of an add-operation it is hard to find out
1398 1416 * if this add will promote this microzap to fatzap.
1399 1417 * Hence, we consider the worst case and account for the
1400 1418 * blocks assuming this microzap would be promoted to a
1401 1419 * fatzap.
1402 1420 *
1403 1421 * 1 block overwritten : header block
1404 1422 * 4 new blocks written : 2 new split leaf, 2 grown
1405 1423 * ptrtbl blocks
1406 1424 */
1407 1425 if (dmu_buf_freeable(zap->zap_dbuf))
1408 1426 *tooverwrite += MZAP_MAX_BLKSZ;
1409 1427 else
1410 1428 *towrite += MZAP_MAX_BLKSZ;
1411 1429
1412 1430 if (add) {
1413 1431 *towrite += 4 * MZAP_MAX_BLKSZ;
1414 1432 }
1415 1433 }
1416 1434
1417 1435 zap_unlockdir(zap);
1418 1436 return (err);
1419 1437 }
|
↓ open down ↓ |
933 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX