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