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