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