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