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 * Copyright (c) 2013 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25 */ 26 27 #ifndef _SYS_SA_IMPL_H 28 #define _SYS_SA_IMPL_H 29 30 #include <sys/dmu.h> 31 #include <sys/refcount.h> 32 #include <sys/list.h> 33 34 /* 35 * Array of known attributes and their 36 * various characteristics. 37 */ 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; 45 46 /* 47 * Zap attribute format for attribute registration 48 * 49 * 64 56 48 40 32 24 16 8 0 50 * +-------+-------+-------+-------+-------+-------+-------+-------+ 51 * | unused | len | bswap | attr num | 52 * +-------+-------+-------+-------+-------+-------+-------+-------+ 53 * 54 * Zap attribute format for layout information. 55 * 56 * layout information is stored as an array of attribute numbers 57 * The name of the attribute is the layout number (0, 1, 2, ...) 58 * 59 * 16 0 60 * +---- ---+ 61 * | attr # | 62 * +--------+ 63 * | attr # | 64 * +--- ----+ 65 * ...... 66 * 67 */ 68 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); \ 77 } 78 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); \ 87 } 88 89 #define SA_LAYOUTS "LAYOUTS" 90 #define SA_REGISTRY "REGISTRY" 91 92 /* 93 * Each unique layout will have their own table 94 * sa_lot (layout_table) 95 */ 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; 107 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 refcount_t sa_refcount; 114 uint32_t *sa_idx_tab; /* array of offsets */ 115 } sa_idx_tab_t; 116 117 /* 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. 121 * 122 * You would typically only have a large number of different table of 123 * contents if you had a several variable sized attributes. 124 * 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. 135 */ 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 */ 150 }; 151 152 /* 153 * header for all bonus and spill buffers. 154 * 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" 158 */ 159 160 #define SA_MAGIC 0x2F505A /* ZFS SA */ 161 typedef struct sa_hdr_phys { 162 uint32_t sa_magic; 163 /* BEGIN CSTYLED */ 164 /* 165 * Encoded with hdrsize and layout number as follows: 166 * 16 10 0 167 * +--------+-------+ 168 * | hdrsz |layout | 169 * +--------+-------+ 170 * 171 * Bits 0-10 are the layout number 172 * Bits 11-16 are the size of the header. 173 * The hdrsize is the number * 8 174 * 175 * For example. 176 * hdrsz of 1 ==> 8 byte header 177 * 2 ==> 16 byte header 178 * 179 */ 180 /* END CSTYLED */ 181 uint16_t sa_layout_info; 182 uint16_t sa_lengths[1]; /* optional sizes for variable length attrs */ 183 /* ... Data follows the lengths. */ 184 } sa_hdr_phys_t; 185 186 #define SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10) 187 #define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 6, 3, 0) 188 #define SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \ 189 { \ 190 BF32_SET_SB(x, 10, 6, 3, 0, size); \ 191 BF32_SET(x, 0, 10, num); \ 192 } 193 194 typedef enum sa_buf_type { 195 SA_BONUS = 1, 196 SA_SPILL = 2 197 } sa_buf_type_t; 198 199 typedef enum sa_data_op { 200 SA_LOOKUP, 201 SA_UPDATE, 202 SA_ADD, 203 SA_REPLACE, 204 SA_REMOVE 205 } sa_data_op_t; 206 207 /* 208 * Opaque handle used for most sa functions 209 * 210 * This needs to be kept as small as possible. 211 */ 212 213 struct sa_handle { 214 dmu_buf_user_t sa_dbu; 215 kmutex_t sa_lock; 216 dmu_buf_t *sa_bonus; 217 dmu_buf_t *sa_spill; 218 objset_t *sa_os; 219 void *sa_userp; 220 sa_idx_tab_t *sa_bonus_tab; /* idx of bonus */ 221 sa_idx_tab_t *sa_spill_tab; /* only present if spill activated */ 222 }; 223 224 #define SA_GET_DB(hdl, type) \ 225 (dmu_buf_impl_t *)((type == SA_BONUS) ? hdl->sa_bonus : hdl->sa_spill) 226 227 #define SA_GET_HDR(hdl, type) \ 228 ((sa_hdr_phys_t *)((dmu_buf_impl_t *)(SA_GET_DB(hdl, \ 229 type))->db.db_data)) 230 231 #define SA_IDX_TAB_GET(hdl, type) \ 232 (type == SA_BONUS ? hdl->sa_bonus_tab : hdl->sa_spill_tab) 233 234 #define IS_SA_BONUSTYPE(a) \ 235 ((a == DMU_OT_SA) ? B_TRUE : B_FALSE) 236 237 #define SA_BONUSTYPE_FROM_DB(db) \ 238 (dmu_get_bonustype((dmu_buf_t *)db)) 239 240 #define SA_BLKPTR_SPACE (DN_MAX_BONUSLEN - sizeof (blkptr_t)) 241 242 #define SA_LAYOUT_NUM(x, type) \ 243 ((!IS_SA_BONUSTYPE(type) ? 0 : (((IS_SA_BONUSTYPE(type)) && \ 244 ((SA_HDR_LAYOUT_NUM(x)) == 0)) ? 1 : SA_HDR_LAYOUT_NUM(x)))) 245 246 247 #define SA_REGISTERED_LEN(sa, attr) sa->sa_attr_table[attr].sa_length 248 249 #define SA_ATTR_LEN(sa, idx, attr, hdr) ((SA_REGISTERED_LEN(sa, attr) == 0) ?\ 250 hdr->sa_lengths[TOC_LEN_IDX(idx->sa_idx_tab[attr])] : \ 251 SA_REGISTERED_LEN(sa, attr)) 252 253 #define SA_SET_HDR(hdr, num, size) \ 254 { \ 255 hdr->sa_magic = SA_MAGIC; \ 256 SA_HDR_LAYOUT_INFO_ENCODE(hdr->sa_layout_info, num, size); \ 257 } 258 259 #define SA_ATTR_INFO(sa, idx, hdr, attr, bulk, type, hdl) \ 260 { \ 261 bulk.sa_size = SA_ATTR_LEN(sa, idx, attr, hdr); \ 262 bulk.sa_buftype = type; \ 263 bulk.sa_addr = \ 264 (void *)((uintptr_t)TOC_OFF(idx->sa_idx_tab[attr]) + \ 265 (uintptr_t)hdr); \ 266 } 267 268 #define SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) \ 269 (SA_HDR_SIZE(hdr) == (sizeof (sa_hdr_phys_t) + \ 270 (tb->lot_var_sizes > 1 ? P2ROUNDUP((tb->lot_var_sizes - 1) * \ 271 sizeof (uint16_t), 8) : 0))) 272 273 int sa_add_impl(sa_handle_t *, sa_attr_type_t, 274 uint32_t, sa_data_locator_t, void *, dmu_tx_t *); 275 276 void sa_register_update_callback_locked(objset_t *, sa_update_cb_t *); 277 int sa_size_locked(sa_handle_t *, sa_attr_type_t, int *); 278 279 void sa_default_locator(void **, uint32_t *, uint32_t, boolean_t, void *); 280 int sa_attr_size(sa_os_t *, sa_idx_tab_t *, sa_attr_type_t, 281 uint16_t *, sa_hdr_phys_t *); 282 283 #ifdef __cplusplus 284 extern "C" { 285 #endif 286 287 #ifdef __cplusplus 288 } 289 #endif 290 291 #endif /* _SYS_SA_IMPL_H */ 292