1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
16 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
17 * Copyright 2017 Nexenta Systems, Inc.
18 * Copyright (c) 2024, Klara, Inc.
19 */
20
21 #include <sys/zio.h>
22 #include <sys/spa.h>
23 #include <sys/dmu.h>
24 #include <sys/zfs_context.h>
25 #include <sys/zap.h>
26 #include <sys/zap_impl.h>
27 #include <sys/zap_leaf.h>
28 #include <sys/btree.h>
29 #include <sys/arc.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/spa_impl.h>
32
33 #ifdef _KERNEL
34 #include <sys/sunddi.h>
35 #endif
36
37 /*
38 * The maximum size (in bytes) of a microzap before it is converted to a
39 * fatzap. It will be rounded up to next multiple of 512 (SPA_MINBLOCKSIZE).
40 *
41 * By definition, a microzap must fit into a single block, so this has
42 * traditionally been SPA_OLD_MAXBLOCKSIZE, and is set to that by default.
43 * Setting this higher requires both the large_blocks feature (to even create
44 * blocks that large) and the large_microzap feature (to enable the stream
45 * machinery to understand not to try to split a microzap block).
46 *
47 * If large_microzap is enabled, this value will be clamped to
48 * spa_maxblocksize(), up to 1M. If not, it will be clamped to
49 * SPA_OLD_MAXBLOCKSIZE.
50 */
51 static int zap_micro_max_size = SPA_OLD_MAXBLOCKSIZE;
52
53 /*
54 * The 1M upper limit is necessary because the count of chunks in a microzap
55 * block is stored as a uint16_t (mze_chunkid). Each chunk is 64 bytes, and the
56 * first is used to store a header, so there are 32767 usable chunks, which is
57 * just under 2M. 1M is the largest power-2-rounded block size under 2M, so we
58 * must set the limit there.
59 */
60 #define MZAP_MAX_SIZE (1048576)
61
62 uint64_t
zap_get_micro_max_size(spa_t * spa)63 zap_get_micro_max_size(spa_t *spa)
64 {
65 uint64_t maxsz = MIN(MZAP_MAX_SIZE,
66 P2ROUNDUP(zap_micro_max_size, SPA_MINBLOCKSIZE));
67 if (maxsz <= SPA_OLD_MAXBLOCKSIZE)
68 return (maxsz);
69 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_MICROZAP))
70 return (MIN(maxsz, spa_maxblocksize(spa)));
71 return (SPA_OLD_MAXBLOCKSIZE);
72 }
73
74 void
mzap_byteswap(mzap_phys_t * buf,size_t size)75 mzap_byteswap(mzap_phys_t *buf, size_t size)
76 {
77 buf->mz_block_type = BSWAP_64(buf->mz_block_type);
78 buf->mz_salt = BSWAP_64(buf->mz_salt);
79 buf->mz_normflags = BSWAP_64(buf->mz_normflags);
80 int max = (size / MZAP_ENT_LEN) - 1;
81 for (int i = 0; i < max; i++) {
82 buf->mz_chunk[i].mze_value =
83 BSWAP_64(buf->mz_chunk[i].mze_value);
84 buf->mz_chunk[i].mze_cd =
85 BSWAP_32(buf->mz_chunk[i].mze_cd);
86 }
87 }
88
89 __attribute__((always_inline)) inline
90 static int
mze_compare(const void * arg1,const void * arg2)91 mze_compare(const void *arg1, const void *arg2)
92 {
93 const mzap_ent_t *mze1 = arg1;
94 const mzap_ent_t *mze2 = arg2;
95
96 return (TREE_CMP((uint64_t)(mze1->mze_hash) << 32 | mze1->mze_cd,
97 (uint64_t)(mze2->mze_hash) << 32 | mze2->mze_cd));
98 }
99
ZFS_BTREE_FIND_IN_BUF_FUNC(mze_find_in_buf,mzap_ent_t,mze_compare)100 ZFS_BTREE_FIND_IN_BUF_FUNC(mze_find_in_buf, mzap_ent_t,
101 mze_compare)
102
103 static void
104 mze_insert(zap_t *zap, uint16_t chunkid, uint64_t hash)
105 {
106 mzap_ent_t mze;
107
108 ASSERT(zap->zap_ismicro);
109 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
110
111 mze.mze_chunkid = chunkid;
112 ASSERT0(hash & 0xffffffff);
113 mze.mze_hash = hash >> 32;
114 ASSERT3U(MZE_PHYS(zap, &mze)->mze_cd, <=, 0xffff);
115 mze.mze_cd = (uint16_t)MZE_PHYS(zap, &mze)->mze_cd;
116 ASSERT(MZE_PHYS(zap, &mze)->mze_name[0] != 0);
117 zfs_btree_add(&zap->zap_m.zap_tree, &mze);
118 }
119
120 mzap_ent_t *
mze_find(zap_name_t * zn,zfs_btree_index_t * idx)121 mze_find(zap_name_t *zn, zfs_btree_index_t *idx)
122 {
123 mzap_ent_t mze_tofind;
124 mzap_ent_t *mze;
125 zfs_btree_t *tree = &zn->zn_zap->zap_m.zap_tree;
126
127 ASSERT(zn->zn_zap->zap_ismicro);
128 ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
129
130 ASSERT0(zn->zn_hash & 0xffffffff);
131 mze_tofind.mze_hash = zn->zn_hash >> 32;
132 mze_tofind.mze_cd = 0;
133
134 mze = zfs_btree_find(tree, &mze_tofind, idx);
135 if (mze == NULL)
136 mze = zfs_btree_next(tree, idx, idx);
137 for (; mze && mze->mze_hash == mze_tofind.mze_hash;
138 mze = zfs_btree_next(tree, idx, idx)) {
139 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
140 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
141 return (mze);
142 }
143
144 return (NULL);
145 }
146
147 static uint32_t
mze_find_unused_cd(zap_t * zap,uint64_t hash)148 mze_find_unused_cd(zap_t *zap, uint64_t hash)
149 {
150 mzap_ent_t mze_tofind;
151 zfs_btree_index_t idx;
152 zfs_btree_t *tree = &zap->zap_m.zap_tree;
153
154 ASSERT(zap->zap_ismicro);
155 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
156
157 ASSERT0(hash & 0xffffffff);
158 hash >>= 32;
159 mze_tofind.mze_hash = hash;
160 mze_tofind.mze_cd = 0;
161
162 uint32_t cd = 0;
163 for (mzap_ent_t *mze = zfs_btree_find(tree, &mze_tofind, &idx);
164 mze && mze->mze_hash == hash;
165 mze = zfs_btree_next(tree, &idx, &idx)) {
166 if (mze->mze_cd != cd)
167 break;
168 cd++;
169 }
170
171 return (cd);
172 }
173
174 /*
175 * Each mzap entry requires at max : 4 chunks
176 * 3 chunks for names + 1 chunk for value.
177 */
178 #define MZAP_ENT_CHUNKS (1 + ZAP_LEAF_ARRAY_NCHUNKS(MZAP_NAME_LEN) + \
179 ZAP_LEAF_ARRAY_NCHUNKS(sizeof (uint64_t)))
180
181 /*
182 * Check if the current entry keeps the colliding entries under the fatzap leaf
183 * size.
184 */
185 boolean_t
mze_canfit_fzap_leaf(zap_name_t * zn,uint64_t hash)186 mze_canfit_fzap_leaf(zap_name_t *zn, uint64_t hash)
187 {
188 zap_t *zap = zn->zn_zap;
189 mzap_ent_t mze_tofind;
190 zfs_btree_index_t idx;
191 zfs_btree_t *tree = &zap->zap_m.zap_tree;
192 uint32_t mzap_ents = 0;
193
194 ASSERT0(hash & 0xffffffff);
195 hash >>= 32;
196 mze_tofind.mze_hash = hash;
197 mze_tofind.mze_cd = 0;
198
199 for (mzap_ent_t *mze = zfs_btree_find(tree, &mze_tofind, &idx);
200 mze && mze->mze_hash == hash;
201 mze = zfs_btree_next(tree, &idx, &idx)) {
202 mzap_ents++;
203 }
204
205 /* Include the new entry being added */
206 mzap_ents++;
207
208 return (ZAP_LEAF_NUMCHUNKS_DEF > (mzap_ents * MZAP_ENT_CHUNKS));
209 }
210
211 void
mze_destroy(zap_t * zap)212 mze_destroy(zap_t *zap)
213 {
214 zfs_btree_clear(&zap->zap_m.zap_tree);
215 zfs_btree_destroy(&zap->zap_m.zap_tree);
216 }
217
218 zap_t *
mzap_open(dmu_buf_t * db)219 mzap_open(dmu_buf_t *db)
220 {
221 zap_t *winner;
222 uint64_t *zap_hdr = (uint64_t *)db->db_data;
223 uint64_t zap_block_type = zap_hdr[0];
224 uint64_t zap_magic = zap_hdr[1];
225
226 ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
227
228 zap_t *zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
229 rw_init(&zap->zap_rwlock, NULL, RW_DEFAULT, NULL);
230 rw_enter(&zap->zap_rwlock, RW_WRITER);
231 zap->zap_objset = dmu_buf_get_objset(db);
232 zap->zap_object = db->db_object;
233 zap->zap_dbuf = db;
234
235 if (zap_block_type != ZBT_MICRO) {
236 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, MUTEX_DEFAULT,
237 0);
238 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
239 if (zap_block_type != ZBT_HEADER || zap_magic != ZAP_MAGIC) {
240 winner = NULL; /* No actual winner here... */
241 goto handle_winner;
242 }
243 } else {
244 zap->zap_ismicro = TRUE;
245 }
246
247 /*
248 * Make sure that zap_ismicro is set before we let others see it,
249 * because zap_lock() checks zap_ismicro without the lock held.
250 */
251 dmu_buf_init_user(&zap->zap_dbu, zap_evict_sync, NULL, &zap->zap_dbuf);
252 winner = dmu_buf_set_user(db, &zap->zap_dbu);
253
254 if (winner != NULL)
255 goto handle_winner;
256
257 if (zap->zap_ismicro) {
258 zap->zap_salt = zap_m_phys(zap)->mz_salt;
259 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
260 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
261
262 /*
263 * Reduce B-tree leaf from 4KB to 512 bytes to reduce memmove()
264 * overhead on massive inserts below. It still allows to store
265 * 62 entries before we have to add 2KB B-tree core node.
266 */
267 zfs_btree_create_custom(&zap->zap_m.zap_tree, mze_compare,
268 mze_find_in_buf, sizeof (mzap_ent_t), 512);
269
270 zap_name_t *zn = zap_name_alloc(zap, B_FALSE);
271 for (uint16_t i = 0; i < zap->zap_m.zap_num_chunks; i++) {
272 mzap_ent_phys_t *mze =
273 &zap_m_phys(zap)->mz_chunk[i];
274 if (mze->mze_name[0]) {
275 zap->zap_m.zap_num_entries++;
276 zap_name_init_str(zn, mze->mze_name, 0);
277 mze_insert(zap, i, zn->zn_hash);
278 }
279 }
280 zap_name_free(zn);
281 } else {
282 zap->zap_salt = zap_f_phys(zap)->zap_salt;
283 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
284
285 ASSERT3U(sizeof (struct zap_leaf_header), ==,
286 2*ZAP_LEAF_CHUNKSIZE);
287
288 /*
289 * The embedded pointer table should not overlap the
290 * other members.
291 */
292 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
293 &zap_f_phys(zap)->zap_salt);
294
295 /*
296 * The embedded pointer table should end at the end of
297 * the block
298 */
299 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
300 1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
301 (uintptr_t)zap_f_phys(zap), ==,
302 zap->zap_dbuf->db_size);
303 }
304 rw_exit(&zap->zap_rwlock);
305 return (zap);
306
307 handle_winner:
308 rw_exit(&zap->zap_rwlock);
309 rw_destroy(&zap->zap_rwlock);
310 if (!zap->zap_ismicro)
311 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
312 kmem_free(zap, sizeof (zap_t));
313 return (winner);
314 }
315
316 int
mzap_upgrade(zap_t ** zapp,dmu_tx_t * tx,zap_flags_t flags)317 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
318 {
319 int err = 0;
320 zap_t *zap = *zapp;
321
322 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
323
324 int sz = zap->zap_dbuf->db_size;
325 mzap_phys_t *mzp = vmem_alloc(sz, KM_SLEEP);
326 memcpy(mzp, zap->zap_dbuf->db_data, sz);
327 int nchunks = zap->zap_m.zap_num_chunks;
328
329 if (!flags) {
330 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
331 1ULL << fzap_default_block_shift, 0, tx);
332 if (err != 0) {
333 vmem_free(mzp, sz);
334 return (err);
335 }
336 }
337
338 dprintf("upgrading obj=%llu with %u chunks\n",
339 (u_longlong_t)zap->zap_object, nchunks);
340 /* XXX destroy the tree later, so we can use the stored hash value */
341 mze_destroy(zap);
342
343 fzap_upgrade(zap, tx, flags);
344
345 zap_name_t *zn = zap_name_alloc(zap, B_FALSE);
346 for (int i = 0; i < nchunks; i++) {
347 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
348 if (mze->mze_name[0] == 0)
349 continue;
350 dprintf("adding %s=%llu\n",
351 mze->mze_name, (u_longlong_t)mze->mze_value);
352 zap_name_init_str(zn, mze->mze_name, 0);
353 /* If we fail here, we would end up losing entries */
354 VERIFY0(fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd,
355 tx));
356 }
357 zap_name_free(zn);
358 vmem_free(mzp, sz);
359 *zapp = zap;
360 return (0);
361 }
362
363 /*
364 * The "normflags" determine the behavior of the matchtype_t which is
365 * passed to zap_lookup_norm(). Names which have the same normalized
366 * version will be stored with the same hash value, and therefore we can
367 * perform normalization-insensitive lookups. We can be Unicode form-
368 * insensitive and/or case-insensitive. The following flags are valid for
369 * "normflags":
370 *
371 * U8_TEXTPREP_NFC
372 * U8_TEXTPREP_NFD
373 * U8_TEXTPREP_NFKC
374 * U8_TEXTPREP_NFKD
375 * U8_TEXTPREP_TOUPPER
376 *
377 * The *_NF* (Normalization Form) flags are mutually exclusive; at most one
378 * of them may be supplied.
379 */
380 void
mzap_create_impl(dnode_t * dn,int normflags,zap_flags_t flags,dmu_tx_t * tx)381 mzap_create_impl(dnode_t *dn, int normflags, zap_flags_t flags, dmu_tx_t *tx)
382 {
383 dmu_buf_t *db;
384
385 VERIFY0(dmu_buf_hold_by_dnode(dn, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
386
387 dmu_buf_will_dirty(db, tx);
388 mzap_phys_t *zp = db->db_data;
389 zp->mz_block_type = ZBT_MICRO;
390 zp->mz_salt =
391 ((uintptr_t)db ^ (uintptr_t)tx ^ (dn->dn_object << 1)) | 1ULL;
392 zp->mz_normflags = normflags;
393
394 if (flags != 0) {
395 zap_t *zap;
396 /* Only fat zap supports flags; upgrade immediately. */
397 VERIFY0(zap_lock_by_dnode(dn, tx,
398 RW_WRITER, B_FALSE, B_FALSE, FTAG, &zap));
399 VERIFY0(mzap_upgrade(&zap, tx, flags));
400 zap_unlock(zap, FTAG);
401 }
402
403 dmu_buf_rele(db, FTAG);
404 }
405
406 /*
407 * zn may be NULL; if not specified, it will be computed if needed.
408 * See also the comment above zap_entry_normalization_conflict().
409 */
410 boolean_t
mzap_normalization_conflict(zap_t * zap,zap_name_t * zn,mzap_ent_t * mze,zfs_btree_index_t * idx)411 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze,
412 zfs_btree_index_t *idx)
413 {
414 boolean_t allocdzn = B_FALSE;
415 mzap_ent_t *other;
416 zfs_btree_index_t oidx;
417
418 if (zap->zap_normflags == 0)
419 return (B_FALSE);
420
421 for (other = zfs_btree_prev(&zap->zap_m.zap_tree, idx, &oidx);
422 other && other->mze_hash == mze->mze_hash;
423 other = zfs_btree_prev(&zap->zap_m.zap_tree, &oidx, &oidx)) {
424
425 if (zn == NULL) {
426 zn = zap_name_alloc_str(zap,
427 MZE_PHYS(zap, mze)->mze_name, MT_NORMALIZE);
428 allocdzn = B_TRUE;
429 }
430 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
431 if (allocdzn)
432 zap_name_free(zn);
433 return (B_TRUE);
434 }
435 }
436
437 for (other = zfs_btree_next(&zap->zap_m.zap_tree, idx, &oidx);
438 other && other->mze_hash == mze->mze_hash;
439 other = zfs_btree_next(&zap->zap_m.zap_tree, &oidx, &oidx)) {
440
441 if (zn == NULL) {
442 zn = zap_name_alloc_str(zap,
443 MZE_PHYS(zap, mze)->mze_name, MT_NORMALIZE);
444 allocdzn = B_TRUE;
445 }
446 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
447 if (allocdzn)
448 zap_name_free(zn);
449 return (B_TRUE);
450 }
451 }
452
453 if (allocdzn)
454 zap_name_free(zn);
455 return (B_FALSE);
456 }
457
458 void
mzap_addent(zap_name_t * zn,uint64_t value)459 mzap_addent(zap_name_t *zn, uint64_t value)
460 {
461 zap_t *zap = zn->zn_zap;
462 uint16_t start = zap->zap_m.zap_alloc_next;
463
464 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
465
466 #ifdef ZFS_DEBUG
467 for (int i = 0; i < zap->zap_m.zap_num_chunks; i++) {
468 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
469 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
470 }
471 #endif
472
473 uint32_t cd = mze_find_unused_cd(zap, zn->zn_hash);
474 /* given the limited size of the microzap, this can't happen */
475 ASSERT(cd < zap_maxcd(zap));
476
477 again:
478 for (uint16_t i = start; i < zap->zap_m.zap_num_chunks; i++) {
479 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
480 if (mze->mze_name[0] == 0) {
481 mze->mze_value = value;
482 mze->mze_cd = cd;
483 (void) strlcpy(mze->mze_name, zn->zn_key_orig,
484 sizeof (mze->mze_name));
485 zap->zap_m.zap_num_entries++;
486 zap->zap_m.zap_alloc_next = i+1;
487 if (zap->zap_m.zap_alloc_next ==
488 zap->zap_m.zap_num_chunks)
489 zap->zap_m.zap_alloc_next = 0;
490 mze_insert(zap, i, zn->zn_hash);
491 return;
492 }
493 }
494 if (start != 0) {
495 start = 0;
496 goto again;
497 }
498 cmn_err(CE_PANIC, "out of entries!");
499 }
500
501 ZFS_MODULE_PARAM(zfs, , zap_micro_max_size, INT, ZMOD_RW,
502 "Maximum micro ZAP size before converting to a fat ZAP, "
503 "in bytes (max 1M)");
504