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