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