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