xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zap_impl.h (revision b5152584)
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  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23d5ee8a13SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #ifndef	_SYS_ZAP_IMPL_H
27fa9e4066Sahrens #define	_SYS_ZAP_IMPL_H
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/zap.h>
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/avl.h>
32fa9e4066Sahrens 
33fa9e4066Sahrens #ifdef	__cplusplus
34fa9e4066Sahrens extern "C" {
35fa9e4066Sahrens #endif
36fa9e4066Sahrens 
37f65e61c0Sahrens extern int fzap_default_block_shift;
38f65e61c0Sahrens 
395ad82045Snd #define	ZAP_MAGIC 0x2F52AB2ABULL
40fa9e4066Sahrens 
41f65e61c0Sahrens #define	FZAP_BLOCK_SHIFT(zap)	((zap)->zap_f.zap_block_shift)
42fa9e4066Sahrens 
43fa9e4066Sahrens #define	MZAP_ENT_LEN		64
44fa9e4066Sahrens #define	MZAP_NAME_LEN		(MZAP_ENT_LEN - 8 - 4 - 2)
45*b5152584SMatthew Ahrens #define	MZAP_MAX_BLKSZ		SPA_OLD_MAXBLOCKSIZE
46fa9e4066Sahrens 
47b24ab676SJeff Bonwick #define	ZAP_NEED_CD		(-1U)
48b24ab676SJeff Bonwick 
49fa9e4066Sahrens typedef struct mzap_ent_phys {
50fa9e4066Sahrens 	uint64_t mze_value;
51fa9e4066Sahrens 	uint32_t mze_cd;
52fa9e4066Sahrens 	uint16_t mze_pad;	/* in case we want to chain them someday */
53fa9e4066Sahrens 	char mze_name[MZAP_NAME_LEN];
54fa9e4066Sahrens } mzap_ent_phys_t;
55fa9e4066Sahrens 
56fa9e4066Sahrens typedef struct mzap_phys {
57fa9e4066Sahrens 	uint64_t mz_block_type;	/* ZBT_MICRO */
58fa9e4066Sahrens 	uint64_t mz_salt;
59da6c28aaSamw 	uint64_t mz_normflags;
60da6c28aaSamw 	uint64_t mz_pad[5];
61fa9e4066Sahrens 	mzap_ent_phys_t mz_chunk[1];
62fa9e4066Sahrens 	/* actually variable size depending on block size */
63fa9e4066Sahrens } mzap_phys_t;
64fa9e4066Sahrens 
65fa9e4066Sahrens typedef struct mzap_ent {
66fa9e4066Sahrens 	avl_node_t mze_node;
67fa9e4066Sahrens 	int mze_chunkid;
68fa9e4066Sahrens 	uint64_t mze_hash;
693f9d6ad7SLin Ling 	uint32_t mze_cd; /* copy from mze_phys->mze_cd */
70fa9e4066Sahrens } mzap_ent_t;
71fa9e4066Sahrens 
723f9d6ad7SLin Ling #define	MZE_PHYS(zap, mze) \
733f9d6ad7SLin Ling 	(&(zap)->zap_m.zap_phys->mz_chunk[(mze)->mze_chunkid])
743f9d6ad7SLin Ling 
75fa9e4066Sahrens /*
76fa9e4066Sahrens  * The (fat) zap is stored in one object. It is an array of
77f65e61c0Sahrens  * 1<<FZAP_BLOCK_SHIFT byte blocks. The layout looks like one of:
78fa9e4066Sahrens  *
79fa9e4066Sahrens  * ptrtbl fits in first block:
80fa9e4066Sahrens  * 	[zap_phys_t zap_ptrtbl_shift < 6] [zap_leaf_t] ...
81fa9e4066Sahrens  *
82fa9e4066Sahrens  * ptrtbl too big for first block:
83fa9e4066Sahrens  * 	[zap_phys_t zap_ptrtbl_shift >= 6] [zap_leaf_t] [ptrtbl] ...
84fa9e4066Sahrens  *
85fa9e4066Sahrens  */
86fa9e4066Sahrens 
87fa9e4066Sahrens struct dmu_buf;
88fa9e4066Sahrens struct zap_leaf;
89fa9e4066Sahrens 
90fa9e4066Sahrens #define	ZBT_LEAF		((1ULL << 63) + 0)
91fa9e4066Sahrens #define	ZBT_HEADER		((1ULL << 63) + 1)
92fa9e4066Sahrens #define	ZBT_MICRO		((1ULL << 63) + 3)
93fa9e4066Sahrens /* any other values are ptrtbl blocks */
94fa9e4066Sahrens 
95f65e61c0Sahrens /*
96f65e61c0Sahrens  * the embedded pointer table takes up half a block:
97f65e61c0Sahrens  * block size / entry size (2^3) / 2
98f65e61c0Sahrens  */
99f65e61c0Sahrens #define	ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1)
100f65e61c0Sahrens 
101f65e61c0Sahrens /*
102f65e61c0Sahrens  * The embedded pointer table starts half-way through the block.  Since
103f65e61c0Sahrens  * the pointer table itself is half the block, it starts at (64-bit)
104f65e61c0Sahrens  * word number (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)).
105f65e61c0Sahrens  */
106f65e61c0Sahrens #define	ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) \
107f65e61c0Sahrens 	((uint64_t *)(zap)->zap_f.zap_phys) \
108f65e61c0Sahrens 	[(idx) + (1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap))]
109fa9e4066Sahrens 
110fa9e4066Sahrens /*
111fa9e4066Sahrens  * TAKE NOTE:
112fa9e4066Sahrens  * If zap_phys_t is modified, zap_byteswap() must be modified.
113fa9e4066Sahrens  */
114fa9e4066Sahrens typedef struct zap_phys {
115fa9e4066Sahrens 	uint64_t zap_block_type;	/* ZBT_HEADER */
116fa9e4066Sahrens 	uint64_t zap_magic;		/* ZAP_MAGIC */
117fa9e4066Sahrens 
118fa9e4066Sahrens 	struct zap_table_phys {
119fa9e4066Sahrens 		uint64_t zt_blk;	/* starting block number */
120fa9e4066Sahrens 		uint64_t zt_numblks;	/* number of blocks */
121fa9e4066Sahrens 		uint64_t zt_shift;	/* bits to index it */
122fa9e4066Sahrens 		uint64_t zt_nextblk;	/* next (larger) copy start block */
123fa9e4066Sahrens 		uint64_t zt_blks_copied; /* number source blocks copied */
124fa9e4066Sahrens 	} zap_ptrtbl;
125fa9e4066Sahrens 
126fa9e4066Sahrens 	uint64_t zap_freeblk;		/* the next free block */
127fa9e4066Sahrens 	uint64_t zap_num_leafs;		/* number of leafs */
128fa9e4066Sahrens 	uint64_t zap_num_entries;	/* number of entries */
129fa9e4066Sahrens 	uint64_t zap_salt;		/* salt to stir into hash function */
130da6c28aaSamw 	uint64_t zap_normflags;		/* flags for u8_textprep_str() */
131b24ab676SJeff Bonwick 	uint64_t zap_flags;		/* zap_flags_t */
132f65e61c0Sahrens 	/*
133f65e61c0Sahrens 	 * This structure is followed by padding, and then the embedded
134f65e61c0Sahrens 	 * pointer table.  The embedded pointer table takes up second
135f65e61c0Sahrens 	 * half of the block.  It is accessed using the
136f65e61c0Sahrens 	 * ZAP_EMBEDDED_PTRTBL_ENT() macro.
137f65e61c0Sahrens 	 */
138fa9e4066Sahrens } zap_phys_t;
139fa9e4066Sahrens 
140fa9e4066Sahrens typedef struct zap_table_phys zap_table_phys_t;
141fa9e4066Sahrens 
142fa9e4066Sahrens typedef struct zap {
143fa9e4066Sahrens 	objset_t *zap_objset;
144fa9e4066Sahrens 	uint64_t zap_object;
145fa9e4066Sahrens 	struct dmu_buf *zap_dbuf;
146fa9e4066Sahrens 	krwlock_t zap_rwlock;
147da6c28aaSamw 	boolean_t zap_ismicro;
148da6c28aaSamw 	int zap_normflags;
149fa9e4066Sahrens 	uint64_t zap_salt;
150fa9e4066Sahrens 	union {
151fa9e4066Sahrens 		struct {
152fa9e4066Sahrens 			zap_phys_t *zap_phys;
153fa9e4066Sahrens 
154fa9e4066Sahrens 			/*
155fa9e4066Sahrens 			 * zap_num_entries_mtx protects
156fa9e4066Sahrens 			 * zap_num_entries
157fa9e4066Sahrens 			 */
158fa9e4066Sahrens 			kmutex_t zap_num_entries_mtx;
159f65e61c0Sahrens 			int zap_block_shift;
160fa9e4066Sahrens 		} zap_fat;
161fa9e4066Sahrens 		struct {
162fa9e4066Sahrens 			mzap_phys_t *zap_phys;
163fa9e4066Sahrens 			int16_t zap_num_entries;
164fa9e4066Sahrens 			int16_t zap_num_chunks;
165fa9e4066Sahrens 			int16_t zap_alloc_next;
166fa9e4066Sahrens 			avl_tree_t zap_avl;
167fa9e4066Sahrens 		} zap_micro;
168fa9e4066Sahrens 	} zap_u;
169fa9e4066Sahrens } zap_t;
170fa9e4066Sahrens 
171da6c28aaSamw typedef struct zap_name {
172da6c28aaSamw 	zap_t *zn_zap;
173b24ab676SJeff Bonwick 	int zn_key_intlen;
174b24ab676SJeff Bonwick 	const void *zn_key_orig;
175486ae710SMatthew Ahrens 	int zn_key_orig_numints;
176b24ab676SJeff Bonwick 	const void *zn_key_norm;
177486ae710SMatthew Ahrens 	int zn_key_norm_numints;
178da6c28aaSamw 	uint64_t zn_hash;
179da6c28aaSamw 	matchtype_t zn_matchtype;
180da6c28aaSamw 	char zn_normbuf[ZAP_MAXNAMELEN];
181da6c28aaSamw } zap_name_t;
182da6c28aaSamw 
183fa9e4066Sahrens #define	zap_f	zap_u.zap_fat
184fa9e4066Sahrens #define	zap_m	zap_u.zap_micro
185fa9e4066Sahrens 
186da6c28aaSamw boolean_t zap_match(zap_name_t *zn, const char *matchname);
187fa9e4066Sahrens int zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
188c5f9e431Sahrens     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp);
189fa9e4066Sahrens void zap_unlockdir(zap_t *zap);
190455d5089Sahrens void zap_evict(dmu_buf_t *db, void *vmzap);
191b24ab676SJeff Bonwick zap_name_t *zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt);
192da6c28aaSamw void zap_name_free(zap_name_t *zn);
193b24ab676SJeff Bonwick int zap_hashbits(zap_t *zap);
194b24ab676SJeff Bonwick uint32_t zap_maxcd(zap_t *zap);
195b24ab676SJeff Bonwick uint64_t zap_getflags(zap_t *zap);
196fa9e4066Sahrens 
197fa9e4066Sahrens #define	ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
198fa9e4066Sahrens 
199fa9e4066Sahrens void fzap_byteswap(void *buf, size_t size);
200fa9e4066Sahrens int fzap_count(zap_t *zap, uint64_t *count);
201da6c28aaSamw int fzap_lookup(zap_name_t *zn,
202da6c28aaSamw     uint64_t integer_size, uint64_t num_integers, void *buf,
203da6c28aaSamw     char *realname, int rn_len, boolean_t *normalization_conflictp);
204c7cd2421SGeorge Wilson void fzap_prefetch(zap_name_t *zn);
2053d692628SSanjeev Bagewadi int fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
2063d692628SSanjeev Bagewadi     uint64_t *tooverwrite);
207da6c28aaSamw int fzap_add(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers,
208fa9e4066Sahrens     const void *val, dmu_tx_t *tx);
209da6c28aaSamw int fzap_update(zap_name_t *zn,
210fa9e4066Sahrens     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx);
211da6c28aaSamw int fzap_length(zap_name_t *zn,
212fa9e4066Sahrens     uint64_t *integer_size, uint64_t *num_integers);
213da6c28aaSamw int fzap_remove(zap_name_t *zn, dmu_tx_t *tx);
214fa9e4066Sahrens int fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za);
215fa9e4066Sahrens void fzap_get_stats(zap_t *zap, zap_stats_t *zs);
21687e5029aSahrens void zap_put_leaf(struct zap_leaf *l);
217fa9e4066Sahrens 
218da6c28aaSamw int fzap_add_cd(zap_name_t *zn,
219fa9e4066Sahrens     uint64_t integer_size, uint64_t num_integers,
220ea8dc4b6Seschrock     const void *val, uint32_t cd, dmu_tx_t *tx);
221b24ab676SJeff Bonwick void fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags);
222fa9e4066Sahrens 
223fa9e4066Sahrens #ifdef	__cplusplus
224fa9e4066Sahrens }
225fa9e4066Sahrens #endif
226fa9e4066Sahrens 
227fa9e4066Sahrens #endif /* _SYS_ZAP_IMPL_H */
228