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