1219089Spjd/* 2219089Spjd * CDDL HEADER START 3219089Spjd * 4219089Spjd * The contents of this file are subject to the terms of the 5219089Spjd * Common Development and Distribution License (the "License"). 6219089Spjd * You may not use this file except in compliance with the License. 7219089Spjd * 8219089Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9219089Spjd * or http://www.opensolaris.org/os/licensing. 10219089Spjd * See the License for the specific language governing permissions 11219089Spjd * and limitations under the License. 12219089Spjd * 13219089Spjd * When distributing Covered Code, include this CDDL HEADER in each 14219089Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15219089Spjd * If applicable, add the following below this CDDL HEADER, with the 16219089Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17219089Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18219089Spjd * 19219089Spjd * CDDL HEADER END 20219089Spjd */ 21219089Spjd/* 22219089Spjd * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 23260764Savg * Copyright (c) 2013 by Delphix. All rights reserved. 24219089Spjd */ 25219089Spjd 26219089Spjd#ifndef _SYS_SA_IMPL_H 27219089Spjd#define _SYS_SA_IMPL_H 28219089Spjd 29219089Spjd#include <sys/dmu.h> 30219089Spjd#include <sys/refcount.h> 31219089Spjd#include <sys/list.h> 32219089Spjd 33219089Spjd/* 34219089Spjd * Array of known attributes and their 35219089Spjd * various characteristics. 36219089Spjd */ 37219089Spjdtypedef struct sa_attr_table { 38219089Spjd sa_attr_type_t sa_attr; 39219089Spjd uint8_t sa_registered; 40219089Spjd uint16_t sa_length; 41219089Spjd sa_bswap_type_t sa_byteswap; 42219089Spjd char *sa_name; 43219089Spjd} sa_attr_table_t; 44219089Spjd 45219089Spjd/* 46219089Spjd * Zap attribute format for attribute registration 47219089Spjd * 48219089Spjd * 64 56 48 40 32 24 16 8 0 49219089Spjd * +-------+-------+-------+-------+-------+-------+-------+-------+ 50219089Spjd * | unused | len | bswap | attr num | 51219089Spjd * +-------+-------+-------+-------+-------+-------+-------+-------+ 52219089Spjd * 53219089Spjd * Zap attribute format for layout information. 54219089Spjd * 55219089Spjd * layout information is stored as an array of attribute numbers 56219089Spjd * The name of the attribute is the layout number (0, 1, 2, ...) 57219089Spjd * 58219089Spjd * 16 0 59219089Spjd * +---- ---+ 60219089Spjd * | attr # | 61219089Spjd * +--------+ 62219089Spjd * | attr # | 63219089Spjd * +--- ----+ 64219089Spjd * ...... 65219089Spjd * 66219089Spjd */ 67219089Spjd 68219089Spjd#define ATTR_BSWAP(x) BF32_GET(x, 16, 8) 69219089Spjd#define ATTR_LENGTH(x) BF32_GET(x, 24, 16) 70219089Spjd#define ATTR_NUM(x) BF32_GET(x, 0, 16) 71219089Spjd#define ATTR_ENCODE(x, attr, length, bswap) \ 72219089Spjd{ \ 73219089Spjd BF64_SET(x, 24, 16, length); \ 74219089Spjd BF64_SET(x, 16, 8, bswap); \ 75219089Spjd BF64_SET(x, 0, 16, attr); \ 76219089Spjd} 77219089Spjd 78219089Spjd#define TOC_OFF(x) BF32_GET(x, 0, 23) 79219089Spjd#define TOC_ATTR_PRESENT(x) BF32_GET(x, 31, 1) 80219089Spjd#define TOC_LEN_IDX(x) BF32_GET(x, 24, 4) 81219089Spjd#define TOC_ATTR_ENCODE(x, len_idx, offset) \ 82219089Spjd{ \ 83219089Spjd BF32_SET(x, 31, 1, 1); \ 84219089Spjd BF32_SET(x, 24, 7, len_idx); \ 85219089Spjd BF32_SET(x, 0, 24, offset); \ 86219089Spjd} 87219089Spjd 88219089Spjd#define SA_LAYOUTS "LAYOUTS" 89219089Spjd#define SA_REGISTRY "REGISTRY" 90219089Spjd 91219089Spjd/* 92219089Spjd * Each unique layout will have their own table 93219089Spjd * sa_lot (layout_table) 94219089Spjd */ 95219089Spjdtypedef struct sa_lot { 96219089Spjd avl_node_t lot_num_node; 97219089Spjd avl_node_t lot_hash_node; 98219089Spjd uint64_t lot_num; 99219089Spjd uint64_t lot_hash; 100219089Spjd sa_attr_type_t *lot_attrs; /* array of attr #'s */ 101219089Spjd uint32_t lot_var_sizes; /* how many aren't fixed size */ 102219089Spjd uint32_t lot_attr_count; /* total attr count */ 103219089Spjd list_t lot_idx_tab; /* should be only a couple of entries */ 104219089Spjd int lot_instance; /* used with lot_hash to identify entry */ 105219089Spjd} sa_lot_t; 106219089Spjd 107219089Spjd/* index table of offsets */ 108219089Spjdtypedef struct sa_idx_tab { 109219089Spjd list_node_t sa_next; 110219089Spjd sa_lot_t *sa_layout; 111219089Spjd uint16_t *sa_variable_lengths; 112219089Spjd refcount_t sa_refcount; 113219089Spjd uint32_t *sa_idx_tab; /* array of offsets */ 114219089Spjd} sa_idx_tab_t; 115219089Spjd 116219089Spjd/* 117219089Spjd * Since the offset/index information into the actual data 118219089Spjd * will usually be identical we can share that information with 119219089Spjd * all handles that have the exact same offsets. 120219089Spjd * 121219089Spjd * You would typically only have a large number of different table of 122219089Spjd * contents if you had a several variable sized attributes. 123219089Spjd * 124219089Spjd * Two AVL trees are used to track the attribute layout numbers. 125219089Spjd * one is keyed by number and will be consulted when a DMU_OT_SA 126219089Spjd * object is first read. The second tree is keyed by the hash signature 127219089Spjd * of the attributes and will be consulted when an attribute is added 128219089Spjd * to determine if we already have an instance of that layout. Both 129219089Spjd * of these tree's are interconnected. The only difference is that 130219089Spjd * when an entry is found in the "hash" tree the list of attributes will 131219089Spjd * need to be compared against the list of attributes you have in hand. 132219089Spjd * The assumption is that typically attributes will just be updated and 133219089Spjd * adding a completely new attribute is a very rare operation. 134219089Spjd */ 135219089Spjdstruct sa_os { 136219089Spjd kmutex_t sa_lock; 137219089Spjd boolean_t sa_need_attr_registration; 138219089Spjd boolean_t sa_force_spill; 139219089Spjd uint64_t sa_master_obj; 140219089Spjd uint64_t sa_reg_attr_obj; 141219089Spjd uint64_t sa_layout_attr_obj; 142219089Spjd int sa_num_attrs; 143219089Spjd sa_attr_table_t *sa_attr_table; /* private attr table */ 144219089Spjd sa_update_cb_t *sa_update_cb; 145219089Spjd avl_tree_t sa_layout_num_tree; /* keyed by layout number */ 146219089Spjd avl_tree_t sa_layout_hash_tree; /* keyed by layout hash value */ 147219089Spjd int sa_user_table_sz; 148219089Spjd sa_attr_type_t *sa_user_table; /* user name->attr mapping table */ 149219089Spjd}; 150219089Spjd 151219089Spjd/* 152219089Spjd * header for all bonus and spill buffers. 153252751Sdelphij * 154219089Spjd * The header has a fixed portion with a variable number 155219089Spjd * of "lengths" depending on the number of variable sized 156260764Savg * attributes which are determined by the "layout number" 157219089Spjd */ 158219089Spjd 159219089Spjd#define SA_MAGIC 0x2F505A /* ZFS SA */ 160219089Spjdtypedef struct sa_hdr_phys { 161219089Spjd uint32_t sa_magic; 162260764Savg /* BEGIN CSTYLED */ 163252751Sdelphij /* 164252751Sdelphij * Encoded with hdrsize and layout number as follows: 165252751Sdelphij * 16 10 0 166252751Sdelphij * +--------+-------+ 167252751Sdelphij * | hdrsz |layout | 168252751Sdelphij * +--------+-------+ 169252751Sdelphij * 170252751Sdelphij * Bits 0-10 are the layout number 171252751Sdelphij * Bits 11-16 are the size of the header. 172252751Sdelphij * The hdrsize is the number * 8 173252751Sdelphij * 174252751Sdelphij * For example. 175252751Sdelphij * hdrsz of 1 ==> 8 byte header 176252751Sdelphij * 2 ==> 16 byte header 177252751Sdelphij * 178252751Sdelphij */ 179260764Savg /* END CSTYLED */ 180252751Sdelphij uint16_t sa_layout_info; 181219089Spjd uint16_t sa_lengths[1]; /* optional sizes for variable length attrs */ 182219089Spjd /* ... Data follows the lengths. */ 183219089Spjd} sa_hdr_phys_t; 184219089Spjd 185219089Spjd#define SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10) 186243674Smm#define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 6, 3, 0) 187219089Spjd#define SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \ 188219089Spjd{ \ 189219089Spjd BF32_SET_SB(x, 10, 6, 3, 0, size); \ 190219089Spjd BF32_SET(x, 0, 10, num); \ 191219089Spjd} 192219089Spjd 193219089Spjdtypedef enum sa_buf_type { 194219089Spjd SA_BONUS = 1, 195219089Spjd SA_SPILL = 2 196219089Spjd} sa_buf_type_t; 197219089Spjd 198219089Spjdtypedef enum sa_data_op { 199219089Spjd SA_LOOKUP, 200219089Spjd SA_UPDATE, 201219089Spjd SA_ADD, 202219089Spjd SA_REPLACE, 203219089Spjd SA_REMOVE 204219089Spjd} sa_data_op_t; 205219089Spjd 206219089Spjd/* 207219089Spjd * Opaque handle used for most sa functions 208219089Spjd * 209219089Spjd * This needs to be kept as small as possible. 210219089Spjd */ 211219089Spjd 212219089Spjdstruct sa_handle { 213219089Spjd kmutex_t sa_lock; 214219089Spjd dmu_buf_t *sa_bonus; 215219089Spjd dmu_buf_t *sa_spill; 216219089Spjd objset_t *sa_os; 217219089Spjd void *sa_userp; 218219089Spjd sa_idx_tab_t *sa_bonus_tab; /* idx of bonus */ 219219089Spjd sa_idx_tab_t *sa_spill_tab; /* only present if spill activated */ 220219089Spjd}; 221219089Spjd 222219089Spjd#define SA_GET_DB(hdl, type) \ 223219089Spjd (dmu_buf_impl_t *)((type == SA_BONUS) ? hdl->sa_bonus : hdl->sa_spill) 224219089Spjd 225219089Spjd#define SA_GET_HDR(hdl, type) \ 226219089Spjd ((sa_hdr_phys_t *)((dmu_buf_impl_t *)(SA_GET_DB(hdl, \ 227219089Spjd type))->db.db_data)) 228219089Spjd 229219089Spjd#define SA_IDX_TAB_GET(hdl, type) \ 230219089Spjd (type == SA_BONUS ? hdl->sa_bonus_tab : hdl->sa_spill_tab) 231219089Spjd 232219089Spjd#define IS_SA_BONUSTYPE(a) \ 233219089Spjd ((a == DMU_OT_SA) ? B_TRUE : B_FALSE) 234219089Spjd 235219089Spjd#define SA_BONUSTYPE_FROM_DB(db) \ 236219089Spjd (dmu_get_bonustype((dmu_buf_t *)db)) 237219089Spjd 238219089Spjd#define SA_BLKPTR_SPACE (DN_MAX_BONUSLEN - sizeof (blkptr_t)) 239219089Spjd 240219089Spjd#define SA_LAYOUT_NUM(x, type) \ 241219089Spjd ((!IS_SA_BONUSTYPE(type) ? 0 : (((IS_SA_BONUSTYPE(type)) && \ 242219089Spjd ((SA_HDR_LAYOUT_NUM(x)) == 0)) ? 1 : SA_HDR_LAYOUT_NUM(x)))) 243219089Spjd 244219089Spjd 245219089Spjd#define SA_REGISTERED_LEN(sa, attr) sa->sa_attr_table[attr].sa_length 246219089Spjd 247219089Spjd#define SA_ATTR_LEN(sa, idx, attr, hdr) ((SA_REGISTERED_LEN(sa, attr) == 0) ?\ 248219089Spjd hdr->sa_lengths[TOC_LEN_IDX(idx->sa_idx_tab[attr])] : \ 249219089Spjd SA_REGISTERED_LEN(sa, attr)) 250219089Spjd 251219089Spjd#define SA_SET_HDR(hdr, num, size) \ 252219089Spjd { \ 253219089Spjd hdr->sa_magic = SA_MAGIC; \ 254219089Spjd SA_HDR_LAYOUT_INFO_ENCODE(hdr->sa_layout_info, num, size); \ 255219089Spjd } 256219089Spjd 257219089Spjd#define SA_ATTR_INFO(sa, idx, hdr, attr, bulk, type, hdl) \ 258219089Spjd { \ 259219089Spjd bulk.sa_size = SA_ATTR_LEN(sa, idx, attr, hdr); \ 260219089Spjd bulk.sa_buftype = type; \ 261219089Spjd bulk.sa_addr = \ 262219089Spjd (void *)((uintptr_t)TOC_OFF(idx->sa_idx_tab[attr]) + \ 263219089Spjd (uintptr_t)hdr); \ 264219089Spjd} 265219089Spjd 266219089Spjd#define SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) \ 267219089Spjd (SA_HDR_SIZE(hdr) == (sizeof (sa_hdr_phys_t) + \ 268219089Spjd (tb->lot_var_sizes > 1 ? P2ROUNDUP((tb->lot_var_sizes - 1) * \ 269219089Spjd sizeof (uint16_t), 8) : 0))) 270219089Spjd 271219089Spjdint sa_add_impl(sa_handle_t *, sa_attr_type_t, 272219089Spjd uint32_t, sa_data_locator_t, void *, dmu_tx_t *); 273219089Spjd 274219089Spjdvoid sa_register_update_callback_locked(objset_t *, sa_update_cb_t *); 275219089Spjdint sa_size_locked(sa_handle_t *, sa_attr_type_t, int *); 276219089Spjd 277219089Spjdvoid sa_default_locator(void **, uint32_t *, uint32_t, boolean_t, void *); 278219089Spjdint sa_attr_size(sa_os_t *, sa_idx_tab_t *, sa_attr_type_t, 279219089Spjd uint16_t *, sa_hdr_phys_t *); 280219089Spjd 281219089Spjd#ifdef __cplusplus 282219089Spjdextern "C" { 283219089Spjd#endif 284219089Spjd 285219089Spjd#ifdef __cplusplus 286219089Spjd} 287219089Spjd#endif 288219089Spjd 289219089Spjd#endif /* _SYS_SA_IMPL_H */ 290