xref: /freebsd/sys/contrib/openzfs/include/sys/zap_impl.h (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2017 Nexenta Systems, Inc.
27*7a7741afSMartin Matuska  * Copyright (c) 2024, Klara, Inc.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #ifndef	_SYS_ZAP_IMPL_H
31eda14cbcSMatt Macy #define	_SYS_ZAP_IMPL_H
32eda14cbcSMatt Macy 
33eda14cbcSMatt Macy #include <sys/zap.h>
34eda14cbcSMatt Macy #include <sys/zfs_context.h>
35eda14cbcSMatt Macy #include <sys/avl.h>
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy #ifdef	__cplusplus
38eda14cbcSMatt Macy extern "C" {
39eda14cbcSMatt Macy #endif
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy extern int fzap_default_block_shift;
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy #define	ZAP_MAGIC 0x2F52AB2ABULL
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_f.zap_block_shift)
46eda14cbcSMatt Macy 
47eda14cbcSMatt Macy #define	MZAP_ENT_LEN		64
48eda14cbcSMatt Macy #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy #define	ZAP_NEED_CD		(-1U)
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy typedef struct mzap_ent_phys {
53eda14cbcSMatt Macy 	uint64_t mze_value;
54eda14cbcSMatt Macy 	uint32_t mze_cd;
55eda14cbcSMatt Macy 	uint16_t mze_pad;	/* in case we want to chain them someday */
56eda14cbcSMatt Macy 	char mze_name[MZAP_NAME_LEN];
57eda14cbcSMatt Macy } mzap_ent_phys_t;
58eda14cbcSMatt Macy 
59eda14cbcSMatt Macy typedef struct mzap_phys {
60eda14cbcSMatt Macy 	uint64_t mz_block_type;	/* ZBT_MICRO */
61eda14cbcSMatt Macy 	uint64_t mz_salt;
62eda14cbcSMatt Macy 	uint64_t mz_normflags;
63eda14cbcSMatt Macy 	uint64_t mz_pad[5];
64eda14cbcSMatt Macy 	mzap_ent_phys_t mz_chunk[1];
65eda14cbcSMatt Macy 	/* actually variable size depending on block size */
66eda14cbcSMatt Macy } mzap_phys_t;
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy typedef struct mzap_ent {
69dbd5678dSMartin Matuska 	uint32_t mze_hash;
70dbd5678dSMartin Matuska 	uint16_t mze_cd; /* copy from mze_phys->mze_cd */
71dbd5678dSMartin Matuska 	uint16_t mze_chunkid;
72eda14cbcSMatt Macy } mzap_ent_t;
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy #define	MZE_PHYS(zap, mze) \
75eda14cbcSMatt Macy 	(&zap_m_phys(zap)->mz_chunk[(mze)->mze_chunkid])
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy /*
78eda14cbcSMatt Macy  * The (fat) zap is stored in one object. It is an array of
79eda14cbcSMatt Macy  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
80eda14cbcSMatt Macy  *
81eda14cbcSMatt Macy  * ptrtbl fits in first block:
82eda14cbcSMatt Macy  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
83eda14cbcSMatt Macy  *
84eda14cbcSMatt Macy  * ptrtbl too big for first block:
85eda14cbcSMatt Macy  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
86eda14cbcSMatt Macy  *
87eda14cbcSMatt Macy  */
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy struct dmu_buf;
90eda14cbcSMatt Macy struct zap_leaf;
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy #define	ZBT_LEAF		((1ULL << 63) + 0)
93eda14cbcSMatt Macy #define	ZBT_HEADER		((1ULL << 63) + 1)
94eda14cbcSMatt Macy #define	ZBT_MICRO		((1ULL << 63) + 3)
95eda14cbcSMatt Macy /* any other values are ptrtbl blocks */
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy /*
98eda14cbcSMatt Macy  * the embedded pointer table takes up half a block:
99eda14cbcSMatt Macy  * block size / entry size (2^3) / 2
100eda14cbcSMatt Macy  */
101eda14cbcSMatt Macy #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy /*
104eda14cbcSMatt Macy  * The embedded pointer table starts half-way through the block.  Since
105eda14cbcSMatt Macy  * the pointer table itself is half the block, it starts at (64-bit)
106eda14cbcSMatt Macy  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
107eda14cbcSMatt Macy  */
108eda14cbcSMatt Macy #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
109eda14cbcSMatt Macy 	((uint64_t *)zap_f_phys(zap)) \
110eda14cbcSMatt Macy 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy /*
113eda14cbcSMatt Macy  * TAKE NOTE:
114eda14cbcSMatt Macy  * If zap_phys_t is modified, zap_byteswap() must be modified.
115eda14cbcSMatt Macy  */
116eda14cbcSMatt Macy typedef struct zap_phys {
117eda14cbcSMatt Macy 	uint64_t zap_block_type;	/* ZBT_HEADER */
118eda14cbcSMatt Macy 	uint64_t zap_magic;		/* ZAP_MAGIC */
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy 	struct zap_table_phys {
121eda14cbcSMatt Macy 		uint64_t zt_blk;	/* starting block number */
122eda14cbcSMatt Macy 		uint64_t zt_numblks;	/* number of blocks */
123eda14cbcSMatt Macy 		uint64_t zt_shift;	/* bits to index it */
124eda14cbcSMatt Macy 		uint64_t zt_nextblk;	/* next (larger) copy start block */
125eda14cbcSMatt Macy 		uint64_t zt_blks_copied; /* number source blocks copied */
126eda14cbcSMatt Macy 	} zap_ptrtbl;
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy 	uint64_t zap_freeblk;		/* the next free block */
129eda14cbcSMatt Macy 	uint64_t zap_num_leafs;		/* number of leafs */
130eda14cbcSMatt Macy 	uint64_t zap_num_entries;	/* number of entries */
131eda14cbcSMatt Macy 	uint64_t zap_salt;		/* salt to stir into hash function */
132eda14cbcSMatt Macy 	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
133eda14cbcSMatt Macy 	uint64_t zap_flags;		/* zap_flags_t */
134eda14cbcSMatt Macy 	/*
135eda14cbcSMatt Macy 	 * This structure is followed by padding, and then the embedded
136eda14cbcSMatt Macy 	 * pointer table.  The embedded pointer table takes up second
137eda14cbcSMatt Macy 	 * half of the block.  It is accessed using the
138eda14cbcSMatt Macy 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
139eda14cbcSMatt Macy 	 */
140eda14cbcSMatt Macy } zap_phys_t;
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy typedef struct zap_table_phys zap_table_phys_t;
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy typedef struct zap {
145eda14cbcSMatt Macy 	dmu_buf_user_t zap_dbu;
146eda14cbcSMatt Macy 	objset_t *zap_objset;
147eda14cbcSMatt Macy 	uint64_t zap_object;
148783d3ff6SMartin Matuska 	dnode_t *zap_dnode;
149eda14cbcSMatt Macy 	struct dmu_buf *zap_dbuf;
150eda14cbcSMatt Macy 	krwlock_t zap_rwlock;
151eda14cbcSMatt Macy 	boolean_t zap_ismicro;
152eda14cbcSMatt Macy 	int zap_normflags;
153eda14cbcSMatt Macy 	uint64_t zap_salt;
154eda14cbcSMatt Macy 	union {
155eda14cbcSMatt Macy 		struct {
156eda14cbcSMatt Macy 			/*
157eda14cbcSMatt Macy 			 * zap_num_entries_mtx protects
158eda14cbcSMatt Macy 			 * zap_num_entries
159eda14cbcSMatt Macy 			 */
160eda14cbcSMatt Macy 			kmutex_t zap_num_entries_mtx;
161eda14cbcSMatt Macy 			int zap_block_shift;
162eda14cbcSMatt Macy 		} zap_fat;
163eda14cbcSMatt Macy 		struct {
164eda14cbcSMatt Macy 			int16_t zap_num_entries;
165eda14cbcSMatt Macy 			int16_t zap_num_chunks;
166eda14cbcSMatt Macy 			int16_t zap_alloc_next;
167dbd5678dSMartin Matuska 			zfs_btree_t zap_tree;
168eda14cbcSMatt Macy 		} zap_micro;
169eda14cbcSMatt Macy 	} zap_u;
170eda14cbcSMatt Macy } zap_t;
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy static inline zap_phys_t *
173eda14cbcSMatt Macy zap_f_phys(zap_t *zap)
174eda14cbcSMatt Macy {
175eda14cbcSMatt Macy 	return (zap->zap_dbuf->db_data);
176eda14cbcSMatt Macy }
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy static inline mzap_phys_t *
179eda14cbcSMatt Macy zap_m_phys(zap_t *zap)
180eda14cbcSMatt Macy {
181eda14cbcSMatt Macy 	return (zap->zap_dbuf->db_data);
182eda14cbcSMatt Macy }
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy typedef struct zap_name {
185eda14cbcSMatt Macy 	zap_t *zn_zap;
186eda14cbcSMatt Macy 	int zn_key_intlen;
187eda14cbcSMatt Macy 	const void *zn_key_orig;
188eda14cbcSMatt Macy 	int zn_key_orig_numints;
189eda14cbcSMatt Macy 	const void *zn_key_norm;
190eda14cbcSMatt Macy 	int zn_key_norm_numints;
191eda14cbcSMatt Macy 	uint64_t zn_hash;
192eda14cbcSMatt Macy 	matchtype_t zn_matchtype;
193eda14cbcSMatt Macy 	int zn_normflags;
194*7a7741afSMartin Matuska 	int zn_normbuf_len;
195*7a7741afSMartin Matuska 	char zn_normbuf[];
196eda14cbcSMatt Macy } zap_name_t;
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy #define	zap_f	zap_u.zap_fat
199eda14cbcSMatt Macy #define	zap_m	zap_u.zap_micro
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy boolean_t zap_match(zap_name_t *zn, const char *matchname);
202eda14cbcSMatt Macy int zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
203a0b956f5SMartin Matuska     krw_t lti, boolean_t fatreader, boolean_t adding, const void *tag,
204a0b956f5SMartin Matuska     zap_t **zapp);
205a0b956f5SMartin Matuska void zap_unlockdir(zap_t *zap, const void *tag);
206eda14cbcSMatt Macy void zap_evict_sync(void *dbu);
207dbd5678dSMartin Matuska zap_name_t *zap_name_alloc_str(zap_t *zap, const char *key, matchtype_t mt);
208eda14cbcSMatt Macy void zap_name_free(zap_name_t *zn);
209eda14cbcSMatt Macy int zap_hashbits(zap_t *zap);
210eda14cbcSMatt Macy uint32_t zap_maxcd(zap_t *zap);
211eda14cbcSMatt Macy uint64_t zap_getflags(zap_t *zap);
212eda14cbcSMatt Macy 
213*7a7741afSMartin Matuska uint64_t zap_get_micro_max_size(spa_t *spa);
214*7a7741afSMartin Matuska 
215eda14cbcSMatt Macy #define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
216eda14cbcSMatt Macy 
217eda14cbcSMatt Macy void fzap_byteswap(void *buf, size_t size);
218eda14cbcSMatt Macy int fzap_count(zap_t *zap, uint64_t *count);
219eda14cbcSMatt Macy int fzap_lookup(zap_name_t *zn,
220eda14cbcSMatt Macy     uint64_t integer_size, uint64_t num_integers, void *buf,
221eda14cbcSMatt Macy     char *realname, int rn_len, boolean_t *normalization_conflictp);
222eda14cbcSMatt Macy void fzap_prefetch(zap_name_t *zn);
223eda14cbcSMatt Macy int fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
224a0b956f5SMartin Matuska     const void *val, const void *tag, dmu_tx_t *tx);
225eda14cbcSMatt Macy int fzap_update(zap_name_t *zn,
226eda14cbcSMatt Macy     int integer_size, uint64_t num_integers, const void *val,
227a0b956f5SMartin Matuska     const void *tag, dmu_tx_t *tx);
228eda14cbcSMatt Macy int fzap_length(zap_name_t *zn,
229eda14cbcSMatt Macy     uint64_t *integer_size, uint64_t *num_integers);
230eda14cbcSMatt Macy int fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
231eda14cbcSMatt Macy int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
232eda14cbcSMatt Macy void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
233eda14cbcSMatt Macy void zap_put_leaf(struct zap_leaf *l);
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy int fzap_add_cd(zap_name_t *zn,
236eda14cbcSMatt Macy     uint64_t integer_size, uint64_t num_integers,
237a0b956f5SMartin Matuska     const void *val, uint32_t cd, const void *tag, dmu_tx_t *tx);
238eda14cbcSMatt Macy void fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy #ifdef	__cplusplus
241eda14cbcSMatt Macy }
242eda14cbcSMatt Macy #endif
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy #endif /* _SYS_ZAP_IMPL_H */
245