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. 23260763Savg * Copyright (c) 2013 by Delphix. All rights reserved. 24288549Smav * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25219089Spjd */ 26219089Spjd 27219089Spjd#ifndef _SYS_SA_IMPL_H 28219089Spjd#define _SYS_SA_IMPL_H 29219089Spjd 30219089Spjd#include <sys/dmu.h> 31219089Spjd#include <sys/refcount.h> 32219089Spjd#include <sys/list.h> 33219089Spjd 34219089Spjd/* 35219089Spjd * Array of known attributes and their 36219089Spjd * various characteristics. 37219089Spjd */ 38219089Spjdtypedef struct sa_attr_table { 39219089Spjd sa_attr_type_t sa_attr; 40219089Spjd uint8_t sa_registered; 41219089Spjd uint16_t sa_length; 42219089Spjd sa_bswap_type_t sa_byteswap; 43219089Spjd char *sa_name; 44219089Spjd} sa_attr_table_t; 45219089Spjd 46219089Spjd/* 47219089Spjd * Zap attribute format for attribute registration 48219089Spjd * 49219089Spjd * 64 56 48 40 32 24 16 8 0 50219089Spjd * +-------+-------+-------+-------+-------+-------+-------+-------+ 51219089Spjd * | unused | len | bswap | attr num | 52219089Spjd * +-------+-------+-------+-------+-------+-------+-------+-------+ 53219089Spjd * 54219089Spjd * Zap attribute format for layout information. 55219089Spjd * 56219089Spjd * layout information is stored as an array of attribute numbers 57219089Spjd * The name of the attribute is the layout number (0, 1, 2, ...) 58219089Spjd * 59219089Spjd * 16 0 60219089Spjd * +---- ---+ 61219089Spjd * | attr # | 62219089Spjd * +--------+ 63219089Spjd * | attr # | 64219089Spjd * +--- ----+ 65219089Spjd * ...... 66219089Spjd * 67219089Spjd */ 68219089Spjd 69219089Spjd#define ATTR_BSWAP(x) BF32_GET(x, 16, 8) 70219089Spjd#define ATTR_LENGTH(x) BF32_GET(x, 24, 16) 71219089Spjd#define ATTR_NUM(x) BF32_GET(x, 0, 16) 72219089Spjd#define ATTR_ENCODE(x, attr, length, bswap) \ 73219089Spjd{ \ 74219089Spjd BF64_SET(x, 24, 16, length); \ 75219089Spjd BF64_SET(x, 16, 8, bswap); \ 76219089Spjd BF64_SET(x, 0, 16, attr); \ 77219089Spjd} 78219089Spjd 79219089Spjd#define TOC_OFF(x) BF32_GET(x, 0, 23) 80219089Spjd#define TOC_ATTR_PRESENT(x) BF32_GET(x, 31, 1) 81219089Spjd#define TOC_LEN_IDX(x) BF32_GET(x, 24, 4) 82219089Spjd#define TOC_ATTR_ENCODE(x, len_idx, offset) \ 83219089Spjd{ \ 84219089Spjd BF32_SET(x, 31, 1, 1); \ 85219089Spjd BF32_SET(x, 24, 7, len_idx); \ 86219089Spjd BF32_SET(x, 0, 24, offset); \ 87219089Spjd} 88219089Spjd 89219089Spjd#define SA_LAYOUTS "LAYOUTS" 90219089Spjd#define SA_REGISTRY "REGISTRY" 91219089Spjd 92219089Spjd/* 93219089Spjd * Each unique layout will have their own table 94219089Spjd * sa_lot (layout_table) 95219089Spjd */ 96219089Spjdtypedef struct sa_lot { 97219089Spjd avl_node_t lot_num_node; 98219089Spjd avl_node_t lot_hash_node; 99219089Spjd uint64_t lot_num; 100219089Spjd uint64_t lot_hash; 101219089Spjd sa_attr_type_t *lot_attrs; /* array of attr #'s */ 102219089Spjd uint32_t lot_var_sizes; /* how many aren't fixed size */ 103219089Spjd uint32_t lot_attr_count; /* total attr count */ 104219089Spjd list_t lot_idx_tab; /* should be only a couple of entries */ 105219089Spjd int lot_instance; /* used with lot_hash to identify entry */ 106219089Spjd} sa_lot_t; 107219089Spjd 108219089Spjd/* index table of offsets */ 109219089Spjdtypedef struct sa_idx_tab { 110219089Spjd list_node_t sa_next; 111219089Spjd sa_lot_t *sa_layout; 112219089Spjd uint16_t *sa_variable_lengths; 113219089Spjd refcount_t sa_refcount; 114219089Spjd uint32_t *sa_idx_tab; /* array of offsets */ 115219089Spjd} sa_idx_tab_t; 116219089Spjd 117219089Spjd/* 118219089Spjd * Since the offset/index information into the actual data 119219089Spjd * will usually be identical we can share that information with 120219089Spjd * all handles that have the exact same offsets. 121219089Spjd * 122219089Spjd * You would typically only have a large number of different table of 123219089Spjd * contents if you had a several variable sized attributes. 124219089Spjd * 125219089Spjd * Two AVL trees are used to track the attribute layout numbers. 126219089Spjd * one is keyed by number and will be consulted when a DMU_OT_SA 127219089Spjd * object is first read. The second tree is keyed by the hash signature 128219089Spjd * of the attributes and will be consulted when an attribute is added 129219089Spjd * to determine if we already have an instance of that layout. Both 130219089Spjd * of these tree's are interconnected. The only difference is that 131219089Spjd * when an entry is found in the "hash" tree the list of attributes will 132219089Spjd * need to be compared against the list of attributes you have in hand. 133219089Spjd * The assumption is that typically attributes will just be updated and 134219089Spjd * adding a completely new attribute is a very rare operation. 135219089Spjd */ 136219089Spjdstruct sa_os { 137219089Spjd kmutex_t sa_lock; 138219089Spjd boolean_t sa_need_attr_registration; 139219089Spjd boolean_t sa_force_spill; 140219089Spjd uint64_t sa_master_obj; 141219089Spjd uint64_t sa_reg_attr_obj; 142219089Spjd uint64_t sa_layout_attr_obj; 143219089Spjd int sa_num_attrs; 144219089Spjd sa_attr_table_t *sa_attr_table; /* private attr table */ 145219089Spjd sa_update_cb_t *sa_update_cb; 146219089Spjd avl_tree_t sa_layout_num_tree; /* keyed by layout number */ 147219089Spjd avl_tree_t sa_layout_hash_tree; /* keyed by layout hash value */ 148219089Spjd int sa_user_table_sz; 149219089Spjd sa_attr_type_t *sa_user_table; /* user name->attr mapping table */ 150219089Spjd}; 151219089Spjd 152219089Spjd/* 153219089Spjd * header for all bonus and spill buffers. 154251631Sdelphij * 155219089Spjd * The header has a fixed portion with a variable number 156219089Spjd * of "lengths" depending on the number of variable sized 157260763Savg * attributes which are determined by the "layout number" 158219089Spjd */ 159219089Spjd 160219089Spjd#define SA_MAGIC 0x2F505A /* ZFS SA */ 161219089Spjdtypedef struct sa_hdr_phys { 162219089Spjd uint32_t sa_magic; 163260763Savg /* BEGIN CSTYLED */ 164251631Sdelphij /* 165251631Sdelphij * Encoded with hdrsize and layout number as follows: 166251631Sdelphij * 16 10 0 167251631Sdelphij * +--------+-------+ 168251631Sdelphij * | hdrsz |layout | 169251631Sdelphij * +--------+-------+ 170251631Sdelphij * 171251631Sdelphij * Bits 0-10 are the layout number 172251631Sdelphij * Bits 11-16 are the size of the header. 173251631Sdelphij * The hdrsize is the number * 8 174251631Sdelphij * 175251631Sdelphij * For example. 176251631Sdelphij * hdrsz of 1 ==> 8 byte header 177251631Sdelphij * 2 ==> 16 byte header 178251631Sdelphij * 179251631Sdelphij */ 180260763Savg /* END CSTYLED */ 181251631Sdelphij uint16_t sa_layout_info; 182219089Spjd uint16_t sa_lengths[1]; /* optional sizes for variable length attrs */ 183219089Spjd /* ... Data follows the lengths. */ 184219089Spjd} sa_hdr_phys_t; 185219089Spjd 186219089Spjd#define SA_HDR_LAYOUT_NUM(hdr) BF32_GET(hdr->sa_layout_info, 0, 10) 187240955Smm#define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 6, 3, 0) 188219089Spjd#define SA_HDR_LAYOUT_INFO_ENCODE(x, num, size) \ 189219089Spjd{ \ 190219089Spjd BF32_SET_SB(x, 10, 6, 3, 0, size); \ 191219089Spjd BF32_SET(x, 0, 10, num); \ 192219089Spjd} 193219089Spjd 194219089Spjdtypedef enum sa_buf_type { 195219089Spjd SA_BONUS = 1, 196219089Spjd SA_SPILL = 2 197219089Spjd} sa_buf_type_t; 198219089Spjd 199219089Spjdtypedef enum sa_data_op { 200219089Spjd SA_LOOKUP, 201219089Spjd SA_UPDATE, 202219089Spjd SA_ADD, 203219089Spjd SA_REPLACE, 204219089Spjd SA_REMOVE 205219089Spjd} sa_data_op_t; 206219089Spjd 207219089Spjd/* 208219089Spjd * Opaque handle used for most sa functions 209219089Spjd * 210219089Spjd * This needs to be kept as small as possible. 211219089Spjd */ 212219089Spjd 213219089Spjdstruct sa_handle { 214288549Smav dmu_buf_user_t sa_dbu; 215219089Spjd kmutex_t sa_lock; 216219089Spjd dmu_buf_t *sa_bonus; 217219089Spjd dmu_buf_t *sa_spill; 218219089Spjd objset_t *sa_os; 219288549Smav void *sa_userp; 220219089Spjd sa_idx_tab_t *sa_bonus_tab; /* idx of bonus */ 221219089Spjd sa_idx_tab_t *sa_spill_tab; /* only present if spill activated */ 222219089Spjd}; 223219089Spjd 224219089Spjd#define SA_GET_DB(hdl, type) \ 225219089Spjd (dmu_buf_impl_t *)((type == SA_BONUS) ? hdl->sa_bonus : hdl->sa_spill) 226219089Spjd 227219089Spjd#define SA_GET_HDR(hdl, type) \ 228219089Spjd ((sa_hdr_phys_t *)((dmu_buf_impl_t *)(SA_GET_DB(hdl, \ 229219089Spjd type))->db.db_data)) 230219089Spjd 231219089Spjd#define SA_IDX_TAB_GET(hdl, type) \ 232219089Spjd (type == SA_BONUS ? hdl->sa_bonus_tab : hdl->sa_spill_tab) 233219089Spjd 234219089Spjd#define IS_SA_BONUSTYPE(a) \ 235219089Spjd ((a == DMU_OT_SA) ? B_TRUE : B_FALSE) 236219089Spjd 237219089Spjd#define SA_BONUSTYPE_FROM_DB(db) \ 238219089Spjd (dmu_get_bonustype((dmu_buf_t *)db)) 239219089Spjd 240219089Spjd#define SA_BLKPTR_SPACE (DN_MAX_BONUSLEN - sizeof (blkptr_t)) 241219089Spjd 242219089Spjd#define SA_LAYOUT_NUM(x, type) \ 243219089Spjd ((!IS_SA_BONUSTYPE(type) ? 0 : (((IS_SA_BONUSTYPE(type)) && \ 244219089Spjd ((SA_HDR_LAYOUT_NUM(x)) == 0)) ? 1 : SA_HDR_LAYOUT_NUM(x)))) 245219089Spjd 246219089Spjd 247219089Spjd#define SA_REGISTERED_LEN(sa, attr) sa->sa_attr_table[attr].sa_length 248219089Spjd 249219089Spjd#define SA_ATTR_LEN(sa, idx, attr, hdr) ((SA_REGISTERED_LEN(sa, attr) == 0) ?\ 250219089Spjd hdr->sa_lengths[TOC_LEN_IDX(idx->sa_idx_tab[attr])] : \ 251219089Spjd SA_REGISTERED_LEN(sa, attr)) 252219089Spjd 253219089Spjd#define SA_SET_HDR(hdr, num, size) \ 254219089Spjd { \ 255219089Spjd hdr->sa_magic = SA_MAGIC; \ 256219089Spjd SA_HDR_LAYOUT_INFO_ENCODE(hdr->sa_layout_info, num, size); \ 257219089Spjd } 258219089Spjd 259219089Spjd#define SA_ATTR_INFO(sa, idx, hdr, attr, bulk, type, hdl) \ 260219089Spjd { \ 261219089Spjd bulk.sa_size = SA_ATTR_LEN(sa, idx, attr, hdr); \ 262219089Spjd bulk.sa_buftype = type; \ 263219089Spjd bulk.sa_addr = \ 264219089Spjd (void *)((uintptr_t)TOC_OFF(idx->sa_idx_tab[attr]) + \ 265219089Spjd (uintptr_t)hdr); \ 266219089Spjd} 267219089Spjd 268219089Spjd#define SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) \ 269219089Spjd (SA_HDR_SIZE(hdr) == (sizeof (sa_hdr_phys_t) + \ 270219089Spjd (tb->lot_var_sizes > 1 ? P2ROUNDUP((tb->lot_var_sizes - 1) * \ 271219089Spjd sizeof (uint16_t), 8) : 0))) 272219089Spjd 273219089Spjdint sa_add_impl(sa_handle_t *, sa_attr_type_t, 274219089Spjd uint32_t, sa_data_locator_t, void *, dmu_tx_t *); 275219089Spjd 276219089Spjdvoid sa_register_update_callback_locked(objset_t *, sa_update_cb_t *); 277219089Spjdint sa_size_locked(sa_handle_t *, sa_attr_type_t, int *); 278219089Spjd 279219089Spjdvoid sa_default_locator(void **, uint32_t *, uint32_t, boolean_t, void *); 280219089Spjdint sa_attr_size(sa_os_t *, sa_idx_tab_t *, sa_attr_type_t, 281219089Spjd uint16_t *, sa_hdr_phys_t *); 282219089Spjd 283219089Spjd#ifdef __cplusplus 284219089Spjdextern "C" { 285219089Spjd#endif 286219089Spjd 287219089Spjd#ifdef __cplusplus 288219089Spjd} 289219089Spjd#endif 290219089Spjd 291219089Spjd#endif /* _SYS_SA_IMPL_H */ 292