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