xref: /freebsd/sys/contrib/openzfs/include/sys/sa_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2013 by Delphix. All rights reserved.
15  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
16  */
17 
18 #ifndef	_SYS_SA_IMPL_H
19 #define	_SYS_SA_IMPL_H
20 
21 #include <sys/dmu.h>
22 #include <sys/zfs_refcount.h>
23 #include <sys/list.h>
24 
25 /*
26  * Array of known attributes and their
27  * various characteristics.
28  */
29 typedef struct sa_attr_table {
30 	sa_attr_type_t	sa_attr;
31 	uint8_t sa_registered;
32 	uint16_t sa_length;
33 	sa_bswap_type_t sa_byteswap;
34 	char *sa_name;
35 } sa_attr_table_t;
36 
37 /*
38  * Zap attribute format for attribute registration
39  *
40  * 64      56      48      40      32      24      16      8       0
41  * +-------+-------+-------+-------+-------+-------+-------+-------+
42  * |        unused         |      len      | bswap |   attr num    |
43  * +-------+-------+-------+-------+-------+-------+-------+-------+
44  *
45  * Zap attribute format for layout information.
46  *
47  * layout information is stored as an array of attribute numbers
48  * The name of the attribute is the layout number (0, 1, 2, ...)
49  *
50  * 16       0
51  * +---- ---+
52  * | attr # |
53  * +--------+
54  * | attr # |
55  * +--- ----+
56  *  ......
57  *
58  */
59 
60 #define	ATTR_BSWAP(x)	BF32_GET(x, 16, 8)
61 #define	ATTR_LENGTH(x)	BF32_GET(x, 24, 16)
62 #define	ATTR_NUM(x)	BF32_GET(x, 0, 16)
63 #define	ATTR_ENCODE(x, attr, length, bswap) \
64 { \
65 	BF64_SET(x, 24, 16, length); \
66 	BF64_SET(x, 16, 8, bswap); \
67 	BF64_SET(x, 0, 16, attr); \
68 }
69 
70 #define	TOC_OFF(x)		BF32_GET(x, 0, 23)
71 #define	TOC_ATTR_PRESENT(x)	BF32_GET(x, 31, 1)
72 #define	TOC_LEN_IDX(x)		BF32_GET(x, 24, 4)
73 #define	TOC_ATTR_ENCODE(x, len_idx, offset) \
74 { \
75 	BF32_SET(x, 31, 1, 1); \
76 	BF32_SET(x, 24, 7, len_idx); \
77 	BF32_SET(x, 0, 24, offset); \
78 }
79 
80 #define	SA_LAYOUTS	"LAYOUTS"
81 #define	SA_REGISTRY	"REGISTRY"
82 
83 /*
84  * Each unique layout will have their own table
85  * sa_lot (layout_table)
86  */
87 typedef struct sa_lot {
88 	avl_node_t lot_num_node;
89 	avl_node_t lot_hash_node;
90 	uint64_t lot_num;
91 	uint64_t lot_hash;
92 	sa_attr_type_t *lot_attrs;	/* array of attr #'s */
93 	uint32_t lot_var_sizes;	/* how many aren't fixed size */
94 	uint32_t lot_attr_count;	/* total attr count */
95 	list_t 	lot_idx_tab;	/* should be only a couple of entries */
96 	int	lot_instance;	/* used with lot_hash to identify entry */
97 } sa_lot_t;
98 
99 /* index table of offsets */
100 typedef struct sa_idx_tab {
101 	list_node_t	sa_next;
102 	sa_lot_t	*sa_layout;
103 	uint16_t	*sa_variable_lengths;
104 	zfs_refcount_t	sa_refcount;
105 	uint32_t	*sa_idx_tab;	/* array of offsets */
106 } sa_idx_tab_t;
107 
108 /*
109  * Since the offset/index information into the actual data
110  * will usually be identical we can share that information with
111  * all handles that have the exact same offsets.
112  *
113  * You would typically only have a large number of different table of
114  * contents if you had a several variable sized attributes.
115  *
116  * Two AVL trees are used to track the attribute layout numbers.
117  * one is keyed by number and will be consulted when a DMU_OT_SA
118  * object is first read.  The second tree is keyed by the hash signature
119  * of the attributes and will be consulted when an attribute is added
120  * to determine if we already have an instance of that layout.  Both
121  * of these tree's are interconnected.  The only difference is that
122  * when an entry is found in the "hash" tree the list of attributes will
123  * need to be compared against the list of attributes you have in hand.
124  * The assumption is that typically attributes will just be updated and
125  * adding a completely new attribute is a very rare operation.
126  */
127 struct sa_os {
128 	kmutex_t 	sa_lock;
129 	boolean_t	sa_need_attr_registration;
130 	boolean_t	sa_force_spill;
131 	uint64_t	sa_master_obj;
132 	uint64_t	sa_reg_attr_obj;
133 	uint64_t	sa_layout_attr_obj;
134 	int		sa_num_attrs;
135 	sa_attr_table_t *sa_attr_table;	 /* private attr table */
136 	sa_update_cb_t	*sa_update_cb;
137 	avl_tree_t	sa_layout_num_tree;  /* keyed by layout number */
138 	avl_tree_t	sa_layout_hash_tree; /* keyed by layout hash value */
139 	int		sa_user_table_sz;
140 	sa_attr_type_t	*sa_user_table; /* user name->attr mapping table */
141 };
142 
143 /*
144  * header for all bonus and spill buffers.
145  *
146  * The header has a fixed portion with a variable number
147  * of "lengths" depending on the number of variable sized
148  * attributes which are determined by the "layout number"
149  */
150 
151 #define	SA_MAGIC	0x2F505A  /* ZFS SA */
152 typedef struct sa_hdr_phys {
153 	uint32_t sa_magic;
154 	/*
155 	 * Encoded with hdrsize and layout number as follows:
156 	 * 16      10       0
157 	 * +--------+-------+
158 	 * | hdrsz  |layout |
159 	 * +--------+-------+
160 	 *
161 	 * Bits 0-9 (10 bits) are the layout number (0-1023)
162 	 * Bits 10-15 (6 bits) are the size of the header (0-63)
163 	 * The hdrsize is the number * 8
164 	 *
165 	 * For example.
166 	 * hdrsz of 1 ==> 8 byte header
167 	 *          2 ==> 16 byte header
168 	 *
169 	 */
170 	uint16_t sa_layout_info;
171 	uint16_t sa_lengths[1];	/* optional sizes for variable length attrs */
172 	/* ... Data follows the lengths.  */
173 } sa_hdr_phys_t;
174 
175 #define	SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10)
176 #define	SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 6, 3, 0)
177 #define	SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \
178 { \
179 	BF32_SET_SB(x, 10, 6, 3, 0, size); \
180 	BF32_SET(x, 0, 10, num); \
181 }
182 
183 typedef enum sa_buf_type {
184 	SA_BONUS = 1,
185 	SA_SPILL = 2
186 } sa_buf_type_t;
187 
188 typedef enum sa_data_op {
189 	SA_LOOKUP,
190 	SA_UPDATE,
191 	SA_ADD,
192 	SA_REPLACE,
193 	SA_REMOVE
194 } sa_data_op_t;
195 
196 /*
197  * Opaque handle used for most sa functions
198  *
199  * This needs to be kept as small as possible.
200  */
201 
202 struct sa_handle {
203 	dmu_buf_user_t	sa_dbu;
204 	kmutex_t	sa_lock;
205 	dmu_buf_t	*sa_bonus;
206 	dmu_buf_t	*sa_spill;
207 	objset_t	*sa_os;
208 	void		*sa_userp;
209 	sa_idx_tab_t	*sa_bonus_tab;	 /* idx of bonus */
210 	sa_idx_tab_t	*sa_spill_tab; /* only present if spill activated */
211 };
212 
213 #define	SA_GET_DB(hdl, type)	\
214 	(dmu_buf_impl_t *)((type == SA_BONUS) ? hdl->sa_bonus : hdl->sa_spill)
215 
216 #define	SA_GET_HDR(hdl, type) \
217 	((sa_hdr_phys_t *)((dmu_buf_impl_t *)(SA_GET_DB(hdl, \
218 	type))->db.db_data))
219 
220 #define	SA_IDX_TAB_GET(hdl, type) \
221 	(type == SA_BONUS ? hdl->sa_bonus_tab : hdl->sa_spill_tab)
222 
223 #define	IS_SA_BONUSTYPE(a)	\
224 	((a == DMU_OT_SA) ? B_TRUE : B_FALSE)
225 
226 #define	SA_BONUSTYPE_FROM_DB(db) \
227 	(dmu_get_bonustype((dmu_buf_t *)db))
228 
229 #define	SA_BLKPTR_SPACE	(DN_OLD_MAX_BONUSLEN - sizeof (blkptr_t))
230 
231 #define	SA_LAYOUT_NUM(x, type) \
232 	((!IS_SA_BONUSTYPE(type) ? 0 : (((IS_SA_BONUSTYPE(type)) && \
233 	((SA_HDR_LAYOUT_NUM(x)) == 0)) ? 1 : SA_HDR_LAYOUT_NUM(x))))
234 
235 
236 #define	SA_REGISTERED_LEN(sa, attr) sa->sa_attr_table[attr].sa_length
237 
238 #define	SA_ATTR_LEN(sa, idx, attr, hdr) ((SA_REGISTERED_LEN(sa, attr) == 0) ?\
239 	hdr->sa_lengths[TOC_LEN_IDX(idx->sa_idx_tab[attr])] : \
240 	SA_REGISTERED_LEN(sa, attr))
241 
242 #define	SA_SET_HDR(hdr, num, size) \
243 	{ \
244 		hdr->sa_magic = SA_MAGIC; \
245 		SA_HDR_LAYOUT_INFO_ENCODE(hdr->sa_layout_info, num, size); \
246 	}
247 
248 #define	SA_ATTR_INFO(sa, idx, hdr, attr, bulk, type, hdl) \
249 	{ \
250 		bulk.sa_size = SA_ATTR_LEN(sa, idx, attr, hdr); \
251 		bulk.sa_buftype = type; \
252 		bulk.sa_addr = \
253 		    (void *)((uintptr_t)TOC_OFF(idx->sa_idx_tab[attr]) + \
254 		    (uintptr_t)hdr); \
255 }
256 
257 #define	SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) \
258 	(SA_HDR_SIZE(hdr) == (sizeof (sa_hdr_phys_t) + \
259 	(tb->lot_var_sizes > 1 ? P2ROUNDUP((tb->lot_var_sizes - 1) * \
260 	sizeof (uint16_t), 8) : 0)))
261 
262 int sa_add_impl(sa_handle_t *, sa_attr_type_t,
263     uint32_t, sa_data_locator_t, void *, dmu_tx_t *);
264 
265 void sa_register_update_callback_locked(objset_t *, sa_update_cb_t *);
266 
267 void sa_default_locator(void **, uint32_t *, uint32_t, boolean_t, void *);
268 int sa_attr_size(sa_os_t *, sa_idx_tab_t *, sa_attr_type_t,
269     uint16_t *, sa_hdr_phys_t *);
270 
271 #ifdef	__cplusplus
272 extern "C" {
273 #endif
274 
275 #ifdef	__cplusplus
276 }
277 #endif
278 
279 #endif	/* _SYS_SA_IMPL_H */
280