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) 2014 Spectra Logic Corporation, All rights reserved.
16 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
17 * Copyright 2017 Nexenta Systems, Inc.
18 * Copyright (c) 2024, Klara, Inc.
19 * Copyright (c) 2026, TrueNAS.
20 */
21
22 #ifndef _SYS_ZAP_IMPL_H
23 #define _SYS_ZAP_IMPL_H
24
25 #include <sys/zap.h>
26 #include <sys/zfs_context.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 extern int fzap_default_block_shift;
33
34 #define ZAP_MAGIC 0x2F52AB2ABULL
35
36 #define FZAP_BLOCK_SHIFT(zap) ((zap)->zap_f.zap_block_shift)
37
38 #define MZAP_ENT_LEN 64
39 #define MZAP_NAME_LEN (MZAP_ENT_LEN - 8 - 4 - 2)
40
41 #define ZAP_NEED_CD (-1U)
42
43 typedef struct mzap_ent_phys {
44 uint64_t mze_value;
45 uint32_t mze_cd;
46 uint16_t mze_pad; /* in case we want to chain them someday */
47 char mze_name[MZAP_NAME_LEN];
48 } mzap_ent_phys_t;
49
50 typedef struct mzap_phys {
51 uint64_t mz_block_type; /* ZBT_MICRO */
52 uint64_t mz_salt;
53 uint64_t mz_normflags;
54 uint64_t mz_pad[5];
55
56 /* actually variable size depending on block size */
57 mzap_ent_phys_t mz_chunk[];
58 } mzap_phys_t;
59
60 typedef struct mzap_ent {
61 uint32_t mze_hash;
62 uint16_t mze_cd; /* copy from mze_phys->mze_cd */
63 uint16_t mze_chunkid;
64 } mzap_ent_t;
65
66 #define MZE_PHYS(zap, mze) \
67 (&zap_m_phys(zap)->mz_chunk[(mze)->mze_chunkid])
68
69 /*
70 * The (fat) zap is stored in one object. It is an array of
71 * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
72 *
73 * ptrtbl fits in first block:
74 * [zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
75 *
76 * ptrtbl too big for first block:
77 * [zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
78 *
79 */
80
81 struct dmu_buf;
82 struct zap_leaf;
83
84 #define ZBT_LEAF ((1ULL << 63) + 0)
85 #define ZBT_HEADER ((1ULL << 63) + 1)
86 #define ZBT_MICRO ((1ULL << 63) + 3)
87 /* any other values are ptrtbl blocks */
88
89 /*
90 * the embedded pointer table takes up half a block:
91 * block size / entry size (2^3) / 2
92 */
93 #define ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
94
95 /*
96 * The embedded pointer table starts half-way through the block. Since
97 * the pointer table itself is half the block, it starts at (64-bit)
98 * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
99 */
100 #define ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
101 ((uint64_t *)zap_f_phys(zap)) \
102 [(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
103
104 /*
105 * TAKE NOTE:
106 * If zap_phys_t is modified, zap_byteswap() must be modified.
107 */
108 typedef struct zap_phys {
109 uint64_t zap_block_type; /* ZBT_HEADER */
110 uint64_t zap_magic; /* ZAP_MAGIC */
111
112 struct zap_table_phys {
113 uint64_t zt_blk; /* starting block number */
114 uint64_t zt_numblks; /* number of blocks */
115 uint64_t zt_shift; /* bits to index it */
116 uint64_t zt_nextblk; /* next (larger) copy start block */
117 uint64_t zt_blks_copied; /* number source blocks copied */
118 } zap_ptrtbl;
119
120 uint64_t zap_freeblk; /* the next free block */
121 uint64_t zap_num_leafs; /* number of leafs */
122 uint64_t zap_num_entries; /* number of entries */
123 uint64_t zap_salt; /* salt to stir into hash function */
124 uint64_t zap_normflags; /* flags for u8_textprep_str() */
125 uint64_t zap_flags; /* zap_flags_t */
126 /*
127 * This structure is followed by padding, and then the embedded
128 * pointer table. The embedded pointer table takes up second
129 * half of the block. It is accessed using the
130 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
131 */
132 } zap_phys_t;
133
134 typedef struct zap_table_phys zap_table_phys_t;
135
136 typedef struct zap {
137 dmu_buf_user_t zap_dbu;
138 objset_t *zap_objset;
139 uint64_t zap_object;
140 dnode_t *zap_dnode;
141 struct dmu_buf *zap_dbuf;
142 krwlock_t zap_rwlock;
143 boolean_t zap_ismicro;
144 int zap_normflags;
145 uint64_t zap_salt;
146 union {
147 struct {
148 /*
149 * zap_num_entries_mtx protects
150 * zap_num_entries
151 */
152 kmutex_t zap_num_entries_mtx;
153 int zap_block_shift;
154 } zap_fat;
155 struct {
156 int16_t zap_num_entries;
157 int16_t zap_num_chunks;
158 int16_t zap_alloc_next;
159 zfs_btree_t zap_tree;
160 } zap_micro;
161 } zap_u;
162 } zap_t;
163
164 #define zap_f zap_u.zap_fat
165 #define zap_m zap_u.zap_micro
166
167 static inline zap_phys_t *
zap_f_phys(zap_t * zap)168 zap_f_phys(zap_t *zap)
169 {
170 return (zap->zap_dbuf->db_data);
171 }
172
173 static inline mzap_phys_t *
zap_m_phys(zap_t * zap)174 zap_m_phys(zap_t *zap)
175 {
176 return (zap->zap_dbuf->db_data);
177 }
178
179 /*
180 * zap_name_t carries the original key and whatever we've derived from it
181 * (normalised form, hash, etc) as we work through completing the operation.
182 */
183 typedef struct zap_name {
184 zap_t *zn_zap;
185 int zn_key_intlen;
186 const void *zn_key_orig;
187 int zn_key_orig_numints;
188 const void *zn_key_norm;
189 int zn_key_norm_numints;
190 uint64_t zn_hash;
191 matchtype_t zn_matchtype;
192 int zn_normflags;
193 int zn_normbuf_len;
194 char zn_normbuf[];
195 } zap_name_t;
196
197 /*
198 * Allocate a zap_name_t. The longname flag ensures there is enough room to
199 * hold a long filename when the 'longname' pool feature is active.
200 */
201 zap_name_t *zap_name_alloc(zap_t *zap, boolean_t longname);
202
203 /*
204 * Allocate a zap_name_t for the given key. zap_name_init_str() will be
205 * called to normalise the key and initialise the struct.
206 */
207 zap_name_t *zap_name_alloc_str(zap_t *zap, const char *key, matchtype_t mt);
208
209 /*
210 * Allocate a zap_name_t for a uint64 array key.
211 */
212 zap_name_t *zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints);
213
214 /*
215 * Free a zap_name_t.
216 */
217 void zap_name_free(zap_name_t *zn);
218
219 /*
220 * Initialise an existing zap_name_t with the normalised form of the key,
221 * computed according to the given matchtype.
222 */
223 int zap_name_init_str(zap_name_t *zn, const char *key, matchtype_t mt);
224
225 /*
226 * Compare 'matchname' with the name represented by the zap_name_t, applying
227 * the same normalisation method first. Returns true if the normalised forms
228 * match, false otherwise.
229 */
230 boolean_t zap_match(zap_name_t *zn, const char *matchname);
231
232 /*
233 * Compute and return the 64-bit hash for the name, according to the name
234 * type and hash flags.
235 */
236 uint64_t zap_hash(zap_name_t *zn);
237
238 /*
239 * Return a zap_t for the given on-disk object, locked and ready for use.
240 * The zap_t will be allocated and loaded from disk if its not already loaded.
241 */
242 int zap_lock(objset_t *os, uint64_t obj, dmu_tx_t *tx,
243 krw_t lti, boolean_t fatreader, boolean_t adding, const void *tag,
244 zap_t **zapp);
245 int zap_lock_by_dnode(dnode_t *dn, dmu_tx_t *tx,
246 krw_t lti, boolean_t fatreader, boolean_t adding, const void *tag,
247 zap_t **zapp);
248
249 /* Unlock and release a zap_t. */
250 void zap_unlock(zap_t *zap, const void *tag);
251
252 /*
253 * Try to upgrade a zap lock from READER to WRITER. If the upgrade is not
254 * possible without blocking, returns 0. If the upgrade happened, returns 1.
255 */
256 int zap_lock_try_upgrade(zap_t *zap, dmu_tx_t *tx);
257
258 /*
259 * Upgrade a zap lock from READER to WRITER. If it can't be upgraded
260 * immediately it will block.
261 */
262 void zap_lock_upgrade(zap_t *zap, dmu_tx_t *tx);
263
264 /* zap_t release function for when associated dbuf is evicted. */
265 void zap_evict_sync(void *dbu);
266
267 /* Misc internal state & config. */
268 int zap_hashbits(zap_t *zap);
269 uint32_t zap_maxcd(zap_t *zap);
270 uint64_t zap_getflags(zap_t *zap);
271
272 /* Microzap implementation. */
273 zap_t *mzap_open(dmu_buf_t *db);
274 int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags);
275 mzap_ent_t *mze_find(zap_name_t *zn, zfs_btree_index_t *idx);
276 boolean_t mze_canfit_fzap_leaf(zap_name_t *zn, uint64_t hash);
277 void mze_destroy(zap_t *zap);
278 boolean_t mzap_normalization_conflict(zap_t *zap, zap_name_t *zn,
279 mzap_ent_t *mze, zfs_btree_index_t *idx);
280 void mzap_addent(zap_name_t *zn, uint64_t value);
281 void mzap_byteswap(mzap_phys_t *buf, size_t size);
282 uint64_t zap_get_micro_max_size(spa_t *spa);
283
284 /* Fatzap implementation. */
285 void fzap_byteswap(void *buf, size_t size);
286 int fzap_count(zap_t *zap, uint64_t *count);
287 int fzap_lookup(zap_name_t *zn,
288 uint64_t integer_size, uint64_t num_integers, void *buf,
289 char *realname, int rn_len, boolean_t *normalization_conflictp,
290 uint64_t *actual_num_integers);
291 void fzap_prefetch(zap_name_t *zn);
292 int fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
293 const void *val, dmu_tx_t *tx);
294 int fzap_update(zap_name_t *zn, int integer_size, uint64_t num_integers,
295 const void *val, dmu_tx_t *tx);
296 int fzap_length(zap_name_t *zn,
297 uint64_t *integer_size, uint64_t *num_integers);
298 int fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
299 int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
300 void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
301 void zap_put_leaf(struct zap_leaf *l);
302 int fzap_add_cd(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
303 const void *val, uint32_t cd, dmu_tx_t *tx);
304 void fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
305
306 #ifdef __cplusplus
307 }
308 #endif
309
310 #endif /* _SYS_ZAP_IMPL_H */
311