xref: /illumos-gate/usr/src/uts/common/fs/zfs/sa.c (revision 449d1bcc)
10a586ceaSMark Shellenbaum /*
20a586ceaSMark Shellenbaum  * CDDL HEADER START
30a586ceaSMark Shellenbaum  *
40a586ceaSMark Shellenbaum  * The contents of this file are subject to the terms of the
50a586ceaSMark Shellenbaum  * Common Development and Distribution License (the "License").
60a586ceaSMark Shellenbaum  * You may not use this file except in compliance with the License.
70a586ceaSMark Shellenbaum  *
80a586ceaSMark Shellenbaum  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90a586ceaSMark Shellenbaum  * or http://www.opensolaris.org/os/licensing.
100a586ceaSMark Shellenbaum  * See the License for the specific language governing permissions
110a586ceaSMark Shellenbaum  * and limitations under the License.
120a586ceaSMark Shellenbaum  *
130a586ceaSMark Shellenbaum  * When distributing Covered Code, include this CDDL HEADER in each
140a586ceaSMark Shellenbaum  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150a586ceaSMark Shellenbaum  * If applicable, add the following below this CDDL HEADER, with the
160a586ceaSMark Shellenbaum  * fields enclosed by brackets "[]" replaced with your own identifying
170a586ceaSMark Shellenbaum  * information: Portions Copyright [yyyy] [name of copyright owner]
180a586ceaSMark Shellenbaum  *
190a586ceaSMark Shellenbaum  * CDDL HEADER END
200a586ceaSMark Shellenbaum  */
21ad135b5dSChristopher Siden 
220a586ceaSMark Shellenbaum /*
2306e0070dSMark Shellenbaum  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24383e7c74SXin Li  * Portions Copyright 2011 iXsystems, Inc
257f0bdb42SMatthew Ahrens  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
28f67950b2SNasf-Fan  * Copyright 2019 Joyent, Inc.
2906799660SGordon Ross  * Copyright 2023 RackTop Systems, Inc.
300a586ceaSMark Shellenbaum  */
310a586ceaSMark Shellenbaum 
320a586ceaSMark Shellenbaum #include <sys/zfs_context.h>
330a586ceaSMark Shellenbaum #include <sys/types.h>
340a586ceaSMark Shellenbaum #include <sys/param.h>
350a586ceaSMark Shellenbaum #include <sys/systm.h>
360a586ceaSMark Shellenbaum #include <sys/sysmacros.h>
370a586ceaSMark Shellenbaum #include <sys/dmu.h>
380a586ceaSMark Shellenbaum #include <sys/dmu_impl.h>
390a586ceaSMark Shellenbaum #include <sys/dmu_objset.h>
4054811da5SToomas Soome #include <sys/dmu_tx.h>
410a586ceaSMark Shellenbaum #include <sys/dbuf.h>
420a586ceaSMark Shellenbaum #include <sys/dnode.h>
430a586ceaSMark Shellenbaum #include <sys/zap.h>
440a586ceaSMark Shellenbaum #include <sys/sa.h>
450a586ceaSMark Shellenbaum #include <sys/sunddi.h>
460a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
470a586ceaSMark Shellenbaum #include <sys/dnode.h>
480a586ceaSMark Shellenbaum #include <sys/errno.h>
490a586ceaSMark Shellenbaum #include <sys/zfs_context.h>
500a586ceaSMark Shellenbaum 
51f67950b2SNasf-Fan #ifdef _KERNEL
52f67950b2SNasf-Fan #include <sys/zfs_znode.h>
53f67950b2SNasf-Fan #endif
54f67950b2SNasf-Fan 
550a586ceaSMark Shellenbaum /*
560a586ceaSMark Shellenbaum  * ZFS System attributes:
570a586ceaSMark Shellenbaum  *
580a586ceaSMark Shellenbaum  * A generic mechanism to allow for arbitrary attributes
590a586ceaSMark Shellenbaum  * to be stored in a dnode.  The data will be stored in the bonus buffer of
600a586ceaSMark Shellenbaum  * the dnode and if necessary a special "spill" block will be used to handle
610a586ceaSMark Shellenbaum  * overflow situations.  The spill block will be sized to fit the data
620a586ceaSMark Shellenbaum  * from 512 - 128K.  When a spill block is used the BP (blkptr_t) for the
630a586ceaSMark Shellenbaum  * spill block is stored at the end of the current bonus buffer.  Any
640a586ceaSMark Shellenbaum  * attributes that would be in the way of the blkptr_t will be relocated
650a586ceaSMark Shellenbaum  * into the spill block.
660a586ceaSMark Shellenbaum  *
670a586ceaSMark Shellenbaum  * Attribute registration:
680a586ceaSMark Shellenbaum  *
690a586ceaSMark Shellenbaum  * Stored persistently on a per dataset basis
700a586ceaSMark Shellenbaum  * a mapping between attribute "string" names and their actual attribute
710a586ceaSMark Shellenbaum  * numeric values, length, and byteswap function.  The names are only used
720a586ceaSMark Shellenbaum  * during registration.  All  attributes are known by their unique attribute
730a586ceaSMark Shellenbaum  * id value.  If an attribute can have a variable size then the value
740a586ceaSMark Shellenbaum  * 0 will be used to indicate this.
750a586ceaSMark Shellenbaum  *
760a586ceaSMark Shellenbaum  * Attribute Layout:
770a586ceaSMark Shellenbaum  *
780a586ceaSMark Shellenbaum  * Attribute layouts are a way to compactly store multiple attributes, but
790a586ceaSMark Shellenbaum  * without taking the overhead associated with managing each attribute
800a586ceaSMark Shellenbaum  * individually.  Since you will typically have the same set of attributes
810a586ceaSMark Shellenbaum  * stored in the same order a single table will be used to represent that
820a586ceaSMark Shellenbaum  * layout.  The ZPL for example will usually have only about 10 different
830a586ceaSMark Shellenbaum  * layouts (regular files, device files, symlinks,
840a586ceaSMark Shellenbaum  * regular files + scanstamp, files/dir with extended attributes, and then
850a586ceaSMark Shellenbaum  * you have the possibility of all of those minus ACL, because it would
860a586ceaSMark Shellenbaum  * be kicked out into the spill block)
870a586ceaSMark Shellenbaum  *
880a586ceaSMark Shellenbaum  * Layouts are simply an array of the attributes and their
890a586ceaSMark Shellenbaum  * ordering i.e. [0, 1, 4, 5, 2]
900a586ceaSMark Shellenbaum  *
910a586ceaSMark Shellenbaum  * Each distinct layout is given a unique layout number and that is whats
920a586ceaSMark Shellenbaum  * stored in the header at the beginning of the SA data buffer.
930a586ceaSMark Shellenbaum  *
940a586ceaSMark Shellenbaum  * A layout only covers a single dbuf (bonus or spill).  If a set of
950a586ceaSMark Shellenbaum  * attributes is split up between the bonus buffer and a spill buffer then
960a586ceaSMark Shellenbaum  * two different layouts will be used.  This allows us to byteswap the
970a586ceaSMark Shellenbaum  * spill without looking at the bonus buffer and keeps the on disk format of
980a586ceaSMark Shellenbaum  * the bonus and spill buffer the same.
990a586ceaSMark Shellenbaum  *
1000a586ceaSMark Shellenbaum  * Adding a single attribute will cause the entire set of attributes to
1010a586ceaSMark Shellenbaum  * be rewritten and could result in a new layout number being constructed
1020a586ceaSMark Shellenbaum  * as part of the rewrite if no such layout exists for the new set of
1030a586ceaSMark Shellenbaum  * attribues.  The new attribute will be appended to the end of the already
1040a586ceaSMark Shellenbaum  * existing attributes.
1050a586ceaSMark Shellenbaum  *
1060a586ceaSMark Shellenbaum  * Both the attribute registration and attribute layout information are
1070a586ceaSMark Shellenbaum  * stored in normal ZAP attributes.  Their should be a small number of
1080a586ceaSMark Shellenbaum  * known layouts and the set of attributes is assumed to typically be quite
1090a586ceaSMark Shellenbaum  * small.
1100a586ceaSMark Shellenbaum  *
1110a586ceaSMark Shellenbaum  * The registered attributes and layout "table" information is maintained
1120a586ceaSMark Shellenbaum  * in core and a special "sa_os_t" is attached to the objset_t.
1130a586ceaSMark Shellenbaum  *
1140a586ceaSMark Shellenbaum  * A special interface is provided to allow for quickly applying
1150a586ceaSMark Shellenbaum  * a large set of attributes at once.  sa_replace_all_by_template() is
1160a586ceaSMark Shellenbaum  * used to set an array of attributes.  This is used by the ZPL when
1170a586ceaSMark Shellenbaum  * creating a brand new file.  The template that is passed into the function
1180a586ceaSMark Shellenbaum  * specifies the attribute, size for variable length attributes, location of
1190a586ceaSMark Shellenbaum  * data and special "data locator" function if the data isn't in a contiguous
1200a586ceaSMark Shellenbaum  * location.
1210a586ceaSMark Shellenbaum  *
1220a586ceaSMark Shellenbaum  * Byteswap implications:
123f7170741SWill Andrews  *
1240a586ceaSMark Shellenbaum  * Since the SA attributes are not entirely self describing we can't do
1250a586ceaSMark Shellenbaum  * the normal byteswap processing.  The special ZAP layout attribute and
1260a586ceaSMark Shellenbaum  * attribute registration attributes define the byteswap function and the
1270a586ceaSMark Shellenbaum  * size of the attributes, unless it is variable sized.
1280a586ceaSMark Shellenbaum  * The normal ZFS byteswapping infrastructure assumes you don't need
1290a586ceaSMark Shellenbaum  * to read any objects in order to do the necessary byteswapping.  Whereas
1300a586ceaSMark Shellenbaum  * SA attributes can only be properly byteswapped if the dataset is opened
1310a586ceaSMark Shellenbaum  * and the layout/attribute ZAP attributes are available.  Because of this
1320a586ceaSMark Shellenbaum  * the SA attributes will be byteswapped when they are first accessed by
1330a586ceaSMark Shellenbaum  * the SA code that will read the SA data.
1340a586ceaSMark Shellenbaum  */
1350a586ceaSMark Shellenbaum 
1360a586ceaSMark Shellenbaum typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
1370a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t, void *userp);
1380a586ceaSMark Shellenbaum 
1390a586ceaSMark Shellenbaum static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
1400a586ceaSMark Shellenbaum static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
1417f0bdb42SMatthew Ahrens static sa_idx_tab_t *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
1427f0bdb42SMatthew Ahrens     sa_hdr_phys_t *hdr);
1430a586ceaSMark Shellenbaum static void sa_idx_tab_rele(objset_t *os, void *arg);
1440a586ceaSMark Shellenbaum static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
1450a586ceaSMark Shellenbaum     int buflen);
1460a586ceaSMark Shellenbaum static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1470a586ceaSMark Shellenbaum     sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1480a586ceaSMark Shellenbaum     uint16_t buflen, dmu_tx_t *tx);
1490a586ceaSMark Shellenbaum 
1500a586ceaSMark Shellenbaum arc_byteswap_func_t *sa_bswap_table[] = {
1510a586ceaSMark Shellenbaum 	byteswap_uint64_array,
1520a586ceaSMark Shellenbaum 	byteswap_uint32_array,
1530a586ceaSMark Shellenbaum 	byteswap_uint16_array,
1540a586ceaSMark Shellenbaum 	byteswap_uint8_array,
1550a586ceaSMark Shellenbaum 	zfs_acl_byteswap,
1560a586ceaSMark Shellenbaum };
1570a586ceaSMark Shellenbaum 
1580a586ceaSMark Shellenbaum #define	SA_COPY_DATA(f, s, t, l) \
1590a586ceaSMark Shellenbaum 	{ \
1600a586ceaSMark Shellenbaum 		if (f == NULL) { \
1610a586ceaSMark Shellenbaum 			if (l == 8) { \
1620a586ceaSMark Shellenbaum 				*(uint64_t *)t = *(uint64_t *)s; \
1630a586ceaSMark Shellenbaum 			} else if (l == 16) { \
1640a586ceaSMark Shellenbaum 				*(uint64_t *)t = *(uint64_t *)s; \
1650a586ceaSMark Shellenbaum 				*(uint64_t *)((uintptr_t)t + 8) = \
1660a586ceaSMark Shellenbaum 				    *(uint64_t *)((uintptr_t)s + 8); \
1670a586ceaSMark Shellenbaum 			} else { \
1680a586ceaSMark Shellenbaum 				bcopy(s, t, l); \
1690a586ceaSMark Shellenbaum 			} \
1700a586ceaSMark Shellenbaum 		} else \
1710a586ceaSMark Shellenbaum 			sa_copy_data(f, s, t, l); \
1720a586ceaSMark Shellenbaum 	}
1730a586ceaSMark Shellenbaum 
1740a586ceaSMark Shellenbaum /*
1750a586ceaSMark Shellenbaum  * This table is fixed and cannot be changed.  Its purpose is to
1760a586ceaSMark Shellenbaum  * allow the SA code to work with both old/new ZPL file systems.
1770a586ceaSMark Shellenbaum  * It contains the list of legacy attributes.  These attributes aren't
1780a586ceaSMark Shellenbaum  * stored in the "attribute" registry zap objects, since older ZPL file systems
1790a586ceaSMark Shellenbaum  * won't have the registry.  Only objsets of type ZFS_TYPE_FILESYSTEM will
1800a586ceaSMark Shellenbaum  * use this static table.
1810a586ceaSMark Shellenbaum  */
1820a586ceaSMark Shellenbaum sa_attr_reg_t sa_legacy_attrs[] = {
1830a586ceaSMark Shellenbaum 	{"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
1840a586ceaSMark Shellenbaum 	{"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
1850a586ceaSMark Shellenbaum 	{"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
1860a586ceaSMark Shellenbaum 	{"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
1870a586ceaSMark Shellenbaum 	{"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
1880a586ceaSMark Shellenbaum 	{"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
1890a586ceaSMark Shellenbaum 	{"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
1900a586ceaSMark Shellenbaum 	{"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
1910a586ceaSMark Shellenbaum 	{"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
1920a586ceaSMark Shellenbaum 	{"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
1930a586ceaSMark Shellenbaum 	{"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
1940a586ceaSMark Shellenbaum 	{"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
1950a586ceaSMark Shellenbaum 	{"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
1960a586ceaSMark Shellenbaum 	{"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
1970a586ceaSMark Shellenbaum 	{"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
1980a586ceaSMark Shellenbaum 	{"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
1990a586ceaSMark Shellenbaum };
2000a586ceaSMark Shellenbaum 
2010a586ceaSMark Shellenbaum /*
2020a586ceaSMark Shellenbaum  * This is only used for objects of type DMU_OT_ZNODE
2030a586ceaSMark Shellenbaum  */
2040a586ceaSMark Shellenbaum sa_attr_type_t sa_legacy_zpl_layout[] = {
2050a586ceaSMark Shellenbaum     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
2060a586ceaSMark Shellenbaum };
2070a586ceaSMark Shellenbaum 
2080a586ceaSMark Shellenbaum /*
2090a586ceaSMark Shellenbaum  * Special dummy layout used for buffers with no attributes.
2100a586ceaSMark Shellenbaum  */
2110a586ceaSMark Shellenbaum sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
2120a586ceaSMark Shellenbaum 
2130a586ceaSMark Shellenbaum static int sa_legacy_attr_count = 16;
2140a586ceaSMark Shellenbaum static kmem_cache_t *sa_cache = NULL;
2150a586ceaSMark Shellenbaum 
2160a586ceaSMark Shellenbaum /*ARGSUSED*/
2170a586ceaSMark Shellenbaum static int
sa_cache_constructor(void * buf,void * unused,int kmflag)2180a586ceaSMark Shellenbaum sa_cache_constructor(void *buf, void *unused, int kmflag)
2190a586ceaSMark Shellenbaum {
2200a586ceaSMark Shellenbaum 	sa_handle_t *hdl = buf;
2210a586ceaSMark Shellenbaum 
2220a586ceaSMark Shellenbaum 	mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
2230a586ceaSMark Shellenbaum 	return (0);
2240a586ceaSMark Shellenbaum }
2250a586ceaSMark Shellenbaum 
2260a586ceaSMark Shellenbaum /*ARGSUSED*/
2270a586ceaSMark Shellenbaum static void
sa_cache_destructor(void * buf,void * unused)2280a586ceaSMark Shellenbaum sa_cache_destructor(void *buf, void *unused)
2290a586ceaSMark Shellenbaum {
2300a586ceaSMark Shellenbaum 	sa_handle_t *hdl = buf;
2310a586ceaSMark Shellenbaum 	mutex_destroy(&hdl->sa_lock);
2320a586ceaSMark Shellenbaum }
2330a586ceaSMark Shellenbaum 
2340a586ceaSMark Shellenbaum void
sa_cache_init(void)2350a586ceaSMark Shellenbaum sa_cache_init(void)
2360a586ceaSMark Shellenbaum {
2370a586ceaSMark Shellenbaum 	sa_cache = kmem_cache_create("sa_cache",
2380a586ceaSMark Shellenbaum 	    sizeof (sa_handle_t), 0, sa_cache_constructor,
2390a586ceaSMark Shellenbaum 	    sa_cache_destructor, NULL, NULL, NULL, 0);
2400a586ceaSMark Shellenbaum }
2410a586ceaSMark Shellenbaum 
2420a586ceaSMark Shellenbaum void
sa_cache_fini(void)2430a586ceaSMark Shellenbaum sa_cache_fini(void)
2440a586ceaSMark Shellenbaum {
2450a586ceaSMark Shellenbaum 	if (sa_cache)
2460a586ceaSMark Shellenbaum 		kmem_cache_destroy(sa_cache);
2470a586ceaSMark Shellenbaum }
2480a586ceaSMark Shellenbaum 
2490a586ceaSMark Shellenbaum static int
layout_num_compare(const void * arg1,const void * arg2)2500a586ceaSMark Shellenbaum layout_num_compare(const void *arg1, const void *arg2)
2510a586ceaSMark Shellenbaum {
252c4ab0d3fSGvozden Neskovic 	const sa_lot_t *node1 = (const sa_lot_t *)arg1;
253c4ab0d3fSGvozden Neskovic 	const sa_lot_t *node2 = (const sa_lot_t *)arg2;
2540a586ceaSMark Shellenbaum 
2554d7988d6SPaul Dagnelie 	return (TREE_CMP(node1->lot_num, node2->lot_num));
2560a586ceaSMark Shellenbaum }
2570a586ceaSMark Shellenbaum 
2580a586ceaSMark Shellenbaum static int
layout_hash_compare(const void * arg1,const void * arg2)2590a586ceaSMark Shellenbaum layout_hash_compare(const void *arg1, const void *arg2)
2600a586ceaSMark Shellenbaum {
261c4ab0d3fSGvozden Neskovic 	const sa_lot_t *node1 = (const sa_lot_t *)arg1;
262c4ab0d3fSGvozden Neskovic 	const sa_lot_t *node2 = (const sa_lot_t *)arg2;
2630a586ceaSMark Shellenbaum 
2644d7988d6SPaul Dagnelie 	int cmp = TREE_CMP(node1->lot_hash, node2->lot_hash);
265c4ab0d3fSGvozden Neskovic 	if (likely(cmp))
266c4ab0d3fSGvozden Neskovic 		return (cmp);
267c4ab0d3fSGvozden Neskovic 
2684d7988d6SPaul Dagnelie 	return (TREE_CMP(node1->lot_instance, node2->lot_instance));
2690a586ceaSMark Shellenbaum }
2700a586ceaSMark Shellenbaum 
2710a586ceaSMark Shellenbaum boolean_t
sa_layout_equal(sa_lot_t * tbf,sa_attr_type_t * attrs,int count)2720a586ceaSMark Shellenbaum sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
2730a586ceaSMark Shellenbaum {
2740a586ceaSMark Shellenbaum 	int i;
2750a586ceaSMark Shellenbaum 
2760a586ceaSMark Shellenbaum 	if (count != tbf->lot_attr_count)
2770a586ceaSMark Shellenbaum 		return (1);
2780a586ceaSMark Shellenbaum 
2790a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
2800a586ceaSMark Shellenbaum 		if (attrs[i] != tbf->lot_attrs[i])
2810a586ceaSMark Shellenbaum 			return (1);
2820a586ceaSMark Shellenbaum 	}
2830a586ceaSMark Shellenbaum 	return (0);
2840a586ceaSMark Shellenbaum }
2850a586ceaSMark Shellenbaum 
2860a586ceaSMark Shellenbaum #define	SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
2870a586ceaSMark Shellenbaum 
2880a586ceaSMark Shellenbaum static uint64_t
sa_layout_info_hash(sa_attr_type_t * attrs,int attr_count)2890a586ceaSMark Shellenbaum sa_layout_info_hash(sa_attr_type_t *attrs, int attr_count)
2900a586ceaSMark Shellenbaum {
2910a586ceaSMark Shellenbaum 	int i;
2920a586ceaSMark Shellenbaum 	uint64_t crc = -1ULL;
2930a586ceaSMark Shellenbaum 
2940a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++)
2950a586ceaSMark Shellenbaum 		crc ^= SA_ATTR_HASH(attrs[i]);
2960a586ceaSMark Shellenbaum 
2970a586ceaSMark Shellenbaum 	return (crc);
2980a586ceaSMark Shellenbaum }
2990a586ceaSMark Shellenbaum 
3001d8ccc7bSMark Shellenbaum static int
sa_get_spill(sa_handle_t * hdl)3011d8ccc7bSMark Shellenbaum sa_get_spill(sa_handle_t *hdl)
3020a586ceaSMark Shellenbaum {
3030a586ceaSMark Shellenbaum 	int rc;
3040a586ceaSMark Shellenbaum 	if (hdl->sa_spill == NULL) {
3050a586ceaSMark Shellenbaum 		if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
3060a586ceaSMark Shellenbaum 		    &hdl->sa_spill)) == 0)
3070a586ceaSMark Shellenbaum 			VERIFY(0 == sa_build_index(hdl, SA_SPILL));
3080a586ceaSMark Shellenbaum 	} else {
3090a586ceaSMark Shellenbaum 		rc = 0;
3100a586ceaSMark Shellenbaum 	}
3110a586ceaSMark Shellenbaum 
3121d8ccc7bSMark Shellenbaum 	return (rc);
3130a586ceaSMark Shellenbaum }
3140a586ceaSMark Shellenbaum 
3150a586ceaSMark Shellenbaum /*
3160a586ceaSMark Shellenbaum  * Main attribute lookup/update function
3170a586ceaSMark Shellenbaum  * returns 0 for success or non zero for failures
3180a586ceaSMark Shellenbaum  *
3190a586ceaSMark Shellenbaum  * Operates on bulk array, first failure will abort further processing
3200a586ceaSMark Shellenbaum  */
3210a586ceaSMark Shellenbaum int
sa_attr_op(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count,sa_data_op_t data_op,dmu_tx_t * tx)3220a586ceaSMark Shellenbaum sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
3230a586ceaSMark Shellenbaum     sa_data_op_t data_op, dmu_tx_t *tx)
3240a586ceaSMark Shellenbaum {
3250a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
3260a586ceaSMark Shellenbaum 	int i;
3270a586ceaSMark Shellenbaum 	int error = 0;
3280a586ceaSMark Shellenbaum 	sa_buf_type_t buftypes;
3290a586ceaSMark Shellenbaum 
3300a586ceaSMark Shellenbaum 	buftypes = 0;
3310a586ceaSMark Shellenbaum 
3320a586ceaSMark Shellenbaum 	ASSERT(count > 0);
3330a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
3340a586ceaSMark Shellenbaum 		ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
3350a586ceaSMark Shellenbaum 
3360a586ceaSMark Shellenbaum 		bulk[i].sa_addr = NULL;
3370a586ceaSMark Shellenbaum 		/* First check the bonus buffer */
3380a586ceaSMark Shellenbaum 
3390a586ceaSMark Shellenbaum 		if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
3400a586ceaSMark Shellenbaum 		    hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
3410a586ceaSMark Shellenbaum 			SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
3420a586ceaSMark Shellenbaum 			    SA_GET_HDR(hdl, SA_BONUS),
3430a586ceaSMark Shellenbaum 			    bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
3440a586ceaSMark Shellenbaum 			if (tx && !(buftypes & SA_BONUS)) {
3450a586ceaSMark Shellenbaum 				dmu_buf_will_dirty(hdl->sa_bonus, tx);
3460a586ceaSMark Shellenbaum 				buftypes |= SA_BONUS;
3470a586ceaSMark Shellenbaum 			}
3480a586ceaSMark Shellenbaum 		}
3491d8ccc7bSMark Shellenbaum 		if (bulk[i].sa_addr == NULL &&
3501d8ccc7bSMark Shellenbaum 		    ((error = sa_get_spill(hdl)) == 0)) {
3510a586ceaSMark Shellenbaum 			if (TOC_ATTR_PRESENT(
3520a586ceaSMark Shellenbaum 			    hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
3530a586ceaSMark Shellenbaum 				SA_ATTR_INFO(sa, hdl->sa_spill_tab,
3540a586ceaSMark Shellenbaum 				    SA_GET_HDR(hdl, SA_SPILL),
3550a586ceaSMark Shellenbaum 				    bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
3560a586ceaSMark Shellenbaum 				if (tx && !(buftypes & SA_SPILL) &&
3570a586ceaSMark Shellenbaum 				    bulk[i].sa_size == bulk[i].sa_length) {
3580a586ceaSMark Shellenbaum 					dmu_buf_will_dirty(hdl->sa_spill, tx);
3590a586ceaSMark Shellenbaum 					buftypes |= SA_SPILL;
3600a586ceaSMark Shellenbaum 				}
3610a586ceaSMark Shellenbaum 			}
3620a586ceaSMark Shellenbaum 		}
3631d8ccc7bSMark Shellenbaum 		if (error && error != ENOENT) {
3641d8ccc7bSMark Shellenbaum 			return ((error == ECKSUM) ? EIO : error);
3651d8ccc7bSMark Shellenbaum 		}
3661d8ccc7bSMark Shellenbaum 
3670a586ceaSMark Shellenbaum 		switch (data_op) {
3680a586ceaSMark Shellenbaum 		case SA_LOOKUP:
3690a586ceaSMark Shellenbaum 			if (bulk[i].sa_addr == NULL)
370be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOENT));
3710a586ceaSMark Shellenbaum 			if (bulk[i].sa_data) {
3720a586ceaSMark Shellenbaum 				SA_COPY_DATA(bulk[i].sa_data_func,
3730a586ceaSMark Shellenbaum 				    bulk[i].sa_addr, bulk[i].sa_data,
374*449d1bccSJason King 				    MIN(bulk[i].sa_size, bulk[i].sa_length));
3750a586ceaSMark Shellenbaum 			}
3760a586ceaSMark Shellenbaum 			continue;
3770a586ceaSMark Shellenbaum 
3780a586ceaSMark Shellenbaum 		case SA_UPDATE:
3790a586ceaSMark Shellenbaum 			/* existing rewrite of attr */
3800a586ceaSMark Shellenbaum 			if (bulk[i].sa_addr &&
3810a586ceaSMark Shellenbaum 			    bulk[i].sa_size == bulk[i].sa_length) {
3820a586ceaSMark Shellenbaum 				SA_COPY_DATA(bulk[i].sa_data_func,
3830a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_addr,
3840a586ceaSMark Shellenbaum 				    bulk[i].sa_length);
3850a586ceaSMark Shellenbaum 				continue;
3860a586ceaSMark Shellenbaum 			} else if (bulk[i].sa_addr) { /* attr size change */
3870a586ceaSMark Shellenbaum 				error = sa_modify_attrs(hdl, bulk[i].sa_attr,
3880a586ceaSMark Shellenbaum 				    SA_REPLACE, bulk[i].sa_data_func,
3890a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_length, tx);
3900a586ceaSMark Shellenbaum 			} else { /* adding new attribute */
3910a586ceaSMark Shellenbaum 				error = sa_modify_attrs(hdl, bulk[i].sa_attr,
3920a586ceaSMark Shellenbaum 				    SA_ADD, bulk[i].sa_data_func,
3930a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_length, tx);
3940a586ceaSMark Shellenbaum 			}
3950a586ceaSMark Shellenbaum 			if (error)
3960a586ceaSMark Shellenbaum 				return (error);
3970a586ceaSMark Shellenbaum 			break;
3980a586ceaSMark Shellenbaum 		}
3990a586ceaSMark Shellenbaum 	}
4000a586ceaSMark Shellenbaum 	return (error);
4010a586ceaSMark Shellenbaum }
4020a586ceaSMark Shellenbaum 
4030a586ceaSMark Shellenbaum static sa_lot_t *
sa_add_layout_entry(objset_t * os,sa_attr_type_t * attrs,int attr_count,uint64_t lot_num,uint64_t hash,boolean_t zapadd,dmu_tx_t * tx)4040a586ceaSMark Shellenbaum sa_add_layout_entry(objset_t *os, sa_attr_type_t *attrs, int attr_count,
4050a586ceaSMark Shellenbaum     uint64_t lot_num, uint64_t hash, boolean_t zapadd, dmu_tx_t *tx)
4060a586ceaSMark Shellenbaum {
4070a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
4080a586ceaSMark Shellenbaum 	sa_lot_t *tb, *findtb;
409d1580181SBryan Cantrill 	int i, size;
4100a586ceaSMark Shellenbaum 	avl_index_t loc;
4110a586ceaSMark Shellenbaum 
4120a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
4130a586ceaSMark Shellenbaum 	tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
4140a586ceaSMark Shellenbaum 	tb->lot_attr_count = attr_count;
415d1580181SBryan Cantrill 
416d1580181SBryan Cantrill 	if ((size = sizeof (sa_attr_type_t) * attr_count) != 0) {
417d1580181SBryan Cantrill 		tb->lot_attrs = kmem_alloc(size, KM_SLEEP);
418d1580181SBryan Cantrill 		bcopy(attrs, tb->lot_attrs, size);
419d1580181SBryan Cantrill 	}
420d1580181SBryan Cantrill 
4210a586ceaSMark Shellenbaum 	tb->lot_num = lot_num;
4220a586ceaSMark Shellenbaum 	tb->lot_hash = hash;
4230a586ceaSMark Shellenbaum 	tb->lot_instance = 0;
4240a586ceaSMark Shellenbaum 
4250a586ceaSMark Shellenbaum 	if (zapadd) {
4260a586ceaSMark Shellenbaum 		char attr_name[8];
4270a586ceaSMark Shellenbaum 
4280a586ceaSMark Shellenbaum 		if (sa->sa_layout_attr_obj == 0) {
429ad135b5dSChristopher Siden 			sa->sa_layout_attr_obj = zap_create_link(os,
430ad135b5dSChristopher Siden 			    DMU_OT_SA_ATTR_LAYOUTS,
431ad135b5dSChristopher Siden 			    sa->sa_master_obj, SA_LAYOUTS, tx);
4320a586ceaSMark Shellenbaum 		}
4330a586ceaSMark Shellenbaum 
4340a586ceaSMark Shellenbaum 		(void) snprintf(attr_name, sizeof (attr_name),
4350a586ceaSMark Shellenbaum 		    "%d", (int)lot_num);
4360a586ceaSMark Shellenbaum 		VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
4370a586ceaSMark Shellenbaum 		    attr_name, 2, attr_count, attrs, tx));
4380a586ceaSMark Shellenbaum 	}
4390a586ceaSMark Shellenbaum 
4400a586ceaSMark Shellenbaum 	list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
4410a586ceaSMark Shellenbaum 	    offsetof(sa_idx_tab_t, sa_next));
4420a586ceaSMark Shellenbaum 
4430a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++) {
4440a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
4450a586ceaSMark Shellenbaum 			tb->lot_var_sizes++;
4460a586ceaSMark Shellenbaum 	}
4470a586ceaSMark Shellenbaum 
4480a586ceaSMark Shellenbaum 	avl_add(&sa->sa_layout_num_tree, tb);
4490a586ceaSMark Shellenbaum 
4500a586ceaSMark Shellenbaum 	/* verify we don't have a hash collision */
4510a586ceaSMark Shellenbaum 	if ((findtb = avl_find(&sa->sa_layout_hash_tree, tb, &loc)) != NULL) {
4520a586ceaSMark Shellenbaum 		for (; findtb && findtb->lot_hash == hash;
4530a586ceaSMark Shellenbaum 		    findtb = AVL_NEXT(&sa->sa_layout_hash_tree, findtb)) {
4540a586ceaSMark Shellenbaum 			if (findtb->lot_instance != tb->lot_instance)
4550a586ceaSMark Shellenbaum 				break;
4560a586ceaSMark Shellenbaum 			tb->lot_instance++;
4570a586ceaSMark Shellenbaum 		}
4580a586ceaSMark Shellenbaum 	}
4590a586ceaSMark Shellenbaum 	avl_add(&sa->sa_layout_hash_tree, tb);
4600a586ceaSMark Shellenbaum 	return (tb);
4610a586ceaSMark Shellenbaum }
4620a586ceaSMark Shellenbaum 
4630a586ceaSMark Shellenbaum static void
sa_find_layout(objset_t * os,uint64_t hash,sa_attr_type_t * attrs,int count,dmu_tx_t * tx,sa_lot_t ** lot)4640a586ceaSMark Shellenbaum sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
4650a586ceaSMark Shellenbaum     int count, dmu_tx_t *tx, sa_lot_t **lot)
4660a586ceaSMark Shellenbaum {
4670a586ceaSMark Shellenbaum 	sa_lot_t *tb, tbsearch;
4680a586ceaSMark Shellenbaum 	avl_index_t loc;
4690a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
4700a586ceaSMark Shellenbaum 	boolean_t found = B_FALSE;
4710a586ceaSMark Shellenbaum 
4720a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
4730a586ceaSMark Shellenbaum 	tbsearch.lot_hash = hash;
4740a586ceaSMark Shellenbaum 	tbsearch.lot_instance = 0;
4750a586ceaSMark Shellenbaum 	tb = avl_find(&sa->sa_layout_hash_tree, &tbsearch, &loc);
4760a586ceaSMark Shellenbaum 	if (tb) {
4770a586ceaSMark Shellenbaum 		for (; tb && tb->lot_hash == hash;
4780a586ceaSMark Shellenbaum 		    tb = AVL_NEXT(&sa->sa_layout_hash_tree, tb)) {
4790a586ceaSMark Shellenbaum 			if (sa_layout_equal(tb, attrs, count) == 0) {
4800a586ceaSMark Shellenbaum 				found = B_TRUE;
4810a586ceaSMark Shellenbaum 				break;
4820a586ceaSMark Shellenbaum 			}
4830a586ceaSMark Shellenbaum 		}
4840a586ceaSMark Shellenbaum 	}
4850a586ceaSMark Shellenbaum 	if (!found) {
4860a586ceaSMark Shellenbaum 		tb = sa_add_layout_entry(os, attrs, count,
4870a586ceaSMark Shellenbaum 		    avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
4880a586ceaSMark Shellenbaum 	}
4890a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
4900a586ceaSMark Shellenbaum 	*lot = tb;
4910a586ceaSMark Shellenbaum }
4920a586ceaSMark Shellenbaum 
4930a586ceaSMark Shellenbaum static int
sa_resize_spill(sa_handle_t * hdl,uint32_t size,dmu_tx_t * tx)4940a586ceaSMark Shellenbaum sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
4950a586ceaSMark Shellenbaum {
4960a586ceaSMark Shellenbaum 	int error;
4970a586ceaSMark Shellenbaum 	uint32_t blocksize;
4980a586ceaSMark Shellenbaum 
4990a586ceaSMark Shellenbaum 	if (size == 0) {
5000a586ceaSMark Shellenbaum 		blocksize = SPA_MINBLOCKSIZE;
501b5152584SMatthew Ahrens 	} else if (size > SPA_OLD_MAXBLOCKSIZE) {
5020a586ceaSMark Shellenbaum 		ASSERT(0);
503be6fd75aSMatthew Ahrens 		return (SET_ERROR(EFBIG));
5040a586ceaSMark Shellenbaum 	} else {
5050a586ceaSMark Shellenbaum 		blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
5060a586ceaSMark Shellenbaum 	}
5070a586ceaSMark Shellenbaum 
5080a586ceaSMark Shellenbaum 	error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
5090a586ceaSMark Shellenbaum 	ASSERT(error == 0);
5100a586ceaSMark Shellenbaum 	return (error);
5110a586ceaSMark Shellenbaum }
5120a586ceaSMark Shellenbaum 
5130a586ceaSMark Shellenbaum static void
sa_copy_data(sa_data_locator_t * func,void * datastart,void * target,int buflen)5140a586ceaSMark Shellenbaum sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
5150a586ceaSMark Shellenbaum {
5160a586ceaSMark Shellenbaum 	if (func == NULL) {
5170a586ceaSMark Shellenbaum 		bcopy(datastart, target, buflen);
5180a586ceaSMark Shellenbaum 	} else {
5190a586ceaSMark Shellenbaum 		boolean_t start;
5200a586ceaSMark Shellenbaum 		int bytes;
5210a586ceaSMark Shellenbaum 		void *dataptr;
5220a586ceaSMark Shellenbaum 		void *saptr = target;
5230a586ceaSMark Shellenbaum 		uint32_t length;
5240a586ceaSMark Shellenbaum 
5250a586ceaSMark Shellenbaum 		start = B_TRUE;
5260a586ceaSMark Shellenbaum 		bytes = 0;
5270a586ceaSMark Shellenbaum 		while (bytes < buflen) {
5280a586ceaSMark Shellenbaum 			func(&dataptr, &length, buflen, start, datastart);
5290a586ceaSMark Shellenbaum 			bcopy(dataptr, saptr, length);
5300a586ceaSMark Shellenbaum 			saptr = (void *)((caddr_t)saptr + length);
5310a586ceaSMark Shellenbaum 			bytes += length;
5320a586ceaSMark Shellenbaum 			start = B_FALSE;
5330a586ceaSMark Shellenbaum 		}
5340a586ceaSMark Shellenbaum 	}
5350a586ceaSMark Shellenbaum }
5360a586ceaSMark Shellenbaum 
5370a586ceaSMark Shellenbaum /*
5380a586ceaSMark Shellenbaum  * Determine several different sizes
5390a586ceaSMark Shellenbaum  * first the sa header size
5400a586ceaSMark Shellenbaum  * the number of bytes to be stored
5410a586ceaSMark Shellenbaum  * if spill would occur the index in the attribute array is returned
5420a586ceaSMark Shellenbaum  *
5430a586ceaSMark Shellenbaum  * the boolean will_spill will be set when spilling is necessary.  It
5440a586ceaSMark Shellenbaum  * is only set when the buftype is SA_BONUS
5450a586ceaSMark Shellenbaum  */
5460a586ceaSMark Shellenbaum static int
sa_find_sizes(sa_os_t * sa,sa_bulk_attr_t * attr_desc,int attr_count,dmu_buf_t * db,sa_buf_type_t buftype,int full_space,int * index,int * total,boolean_t * will_spill)5470a586ceaSMark Shellenbaum sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
54854811da5SToomas Soome     dmu_buf_t *db, sa_buf_type_t buftype, int full_space, int *index,
54954811da5SToomas Soome     int *total, boolean_t *will_spill)
5500a586ceaSMark Shellenbaum {
5510a586ceaSMark Shellenbaum 	int var_size = 0;
5520a586ceaSMark Shellenbaum 	int i;
5530a586ceaSMark Shellenbaum 	int hdrsize;
5543502ed6eSJames Pan 	int extra_hdrsize;
5550a586ceaSMark Shellenbaum 
5560a586ceaSMark Shellenbaum 	if (buftype == SA_BONUS && sa->sa_force_spill) {
5570a586ceaSMark Shellenbaum 		*total = 0;
5580a586ceaSMark Shellenbaum 		*index = 0;
5590a586ceaSMark Shellenbaum 		*will_spill = B_TRUE;
5600a586ceaSMark Shellenbaum 		return (0);
5610a586ceaSMark Shellenbaum 	}
5620a586ceaSMark Shellenbaum 
5630a586ceaSMark Shellenbaum 	*index = -1;
5640a586ceaSMark Shellenbaum 	*total = 0;
5653502ed6eSJames Pan 	*will_spill = B_FALSE;
5660a586ceaSMark Shellenbaum 
5673502ed6eSJames Pan 	extra_hdrsize = 0;
5680a586ceaSMark Shellenbaum 	hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
5690a586ceaSMark Shellenbaum 	    sizeof (sa_hdr_phys_t);
5700a586ceaSMark Shellenbaum 
571644b9528SNed Bass 	ASSERT(IS_P2ALIGNED(full_space, 8));
5720a586ceaSMark Shellenbaum 
5730a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++) {
5740a586ceaSMark Shellenbaum 		boolean_t is_var_sz;
5750a586ceaSMark Shellenbaum 
576644b9528SNed Bass 		*total = P2ROUNDUP(*total, 8);
5770a586ceaSMark Shellenbaum 		*total += attr_desc[i].sa_length;
5783502ed6eSJames Pan 		if (*will_spill)
5793502ed6eSJames Pan 			continue;
5800a586ceaSMark Shellenbaum 
5810a586ceaSMark Shellenbaum 		is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
5820a586ceaSMark Shellenbaum 		if (is_var_sz) {
5830a586ceaSMark Shellenbaum 			var_size++;
5840a586ceaSMark Shellenbaum 		}
5850a586ceaSMark Shellenbaum 
5860a586ceaSMark Shellenbaum 		if (is_var_sz && var_size > 1) {
5873502ed6eSJames Pan 			/*
5883502ed6eSJames Pan 			 * Don't worry that the spill block might overflow.
5893502ed6eSJames Pan 			 * It will be resized if needed in sa_build_layouts().
5903502ed6eSJames Pan 			 */
5913502ed6eSJames Pan 			if (buftype == SA_SPILL ||
5923502ed6eSJames Pan 			    P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
5930a586ceaSMark Shellenbaum 			    *total < full_space) {
594644b9528SNed Bass 				/*
595644b9528SNed Bass 				 * Account for header space used by array of
596644b9528SNed Bass 				 * optional sizes of variable-length attributes.
5973502ed6eSJames Pan 				 * Record the extra header size in case this
5983502ed6eSJames Pan 				 * increase needs to be reversed due to
5993502ed6eSJames Pan 				 * spill-over.
600644b9528SNed Bass 				 */
6010a586ceaSMark Shellenbaum 				hdrsize += sizeof (uint16_t);
6023502ed6eSJames Pan 				if (*index != -1)
6033502ed6eSJames Pan 					extra_hdrsize += sizeof (uint16_t);
6040a586ceaSMark Shellenbaum 			} else {
6053502ed6eSJames Pan 				ASSERT(buftype == SA_BONUS);
6063502ed6eSJames Pan 				if (*index == -1)
6073502ed6eSJames Pan 					*index = i;
6083502ed6eSJames Pan 				*will_spill = B_TRUE;
6090a586ceaSMark Shellenbaum 				continue;
6100a586ceaSMark Shellenbaum 			}
6110a586ceaSMark Shellenbaum 		}
6120a586ceaSMark Shellenbaum 
6130a586ceaSMark Shellenbaum 		/*
6140a586ceaSMark Shellenbaum 		 * find index of where spill *could* occur.
6150a586ceaSMark Shellenbaum 		 * Then continue to count of remainder attribute
6160a586ceaSMark Shellenbaum 		 * space.  The sum is used later for sizing bonus
6170a586ceaSMark Shellenbaum 		 * and spill buffer.
6180a586ceaSMark Shellenbaum 		 */
6190a586ceaSMark Shellenbaum 		if (buftype == SA_BONUS && *index == -1 &&
620383e7c74SXin Li 		    *total + P2ROUNDUP(hdrsize, 8) >
6210a586ceaSMark Shellenbaum 		    (full_space - sizeof (blkptr_t))) {
6220a586ceaSMark Shellenbaum 			*index = i;
6230a586ceaSMark Shellenbaum 		}
6240a586ceaSMark Shellenbaum 
625383e7c74SXin Li 		if (*total + P2ROUNDUP(hdrsize, 8) > full_space &&
6260a586ceaSMark Shellenbaum 		    buftype == SA_BONUS)
6270a586ceaSMark Shellenbaum 			*will_spill = B_TRUE;
6280a586ceaSMark Shellenbaum 	}
6290a586ceaSMark Shellenbaum 
6303502ed6eSJames Pan 	if (*will_spill)
6313502ed6eSJames Pan 		hdrsize -= extra_hdrsize;
632644b9528SNed Bass 
6330a586ceaSMark Shellenbaum 	hdrsize = P2ROUNDUP(hdrsize, 8);
6340a586ceaSMark Shellenbaum 	return (hdrsize);
6350a586ceaSMark Shellenbaum }
6360a586ceaSMark Shellenbaum 
6370a586ceaSMark Shellenbaum #define	BUF_SPACE_NEEDED(total, header) (total + header)
6380a586ceaSMark Shellenbaum 
6390a586ceaSMark Shellenbaum /*
6400a586ceaSMark Shellenbaum  * Find layout that corresponds to ordering of attributes
6410a586ceaSMark Shellenbaum  * If not found a new layout number is created and added to
6420a586ceaSMark Shellenbaum  * persistent layout tables.
6430a586ceaSMark Shellenbaum  */
6440a586ceaSMark Shellenbaum static int
sa_build_layouts(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)6450a586ceaSMark Shellenbaum sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
6460a586ceaSMark Shellenbaum     dmu_tx_t *tx)
6470a586ceaSMark Shellenbaum {
6480a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
6490a586ceaSMark Shellenbaum 	uint64_t hash;
6500a586ceaSMark Shellenbaum 	sa_buf_type_t buftype;
6510a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sahdr;
6520a586ceaSMark Shellenbaum 	void *data_start;
6530a586ceaSMark Shellenbaum 	int buf_space;
6540a586ceaSMark Shellenbaum 	sa_attr_type_t *attrs, *attrs_start;
6550a586ceaSMark Shellenbaum 	int i, lot_count;
65654811da5SToomas Soome 	int dnodesize;
657d5285caeSGeorge Wilson 	int hdrsize;
658d5285caeSGeorge Wilson 	int spillhdrsize = 0;
6590a586ceaSMark Shellenbaum 	int used;
6600a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype;
6610a586ceaSMark Shellenbaum 	sa_lot_t *lot;
6620a586ceaSMark Shellenbaum 	int len_idx;
6630a586ceaSMark Shellenbaum 	int spill_used;
66454811da5SToomas Soome 	int bonuslen;
6650a586ceaSMark Shellenbaum 	boolean_t spilling;
6660a586ceaSMark Shellenbaum 
6670a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(hdl->sa_bonus, tx);
6680a586ceaSMark Shellenbaum 	bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
6690a586ceaSMark Shellenbaum 
67054811da5SToomas Soome 	dmu_object_dnsize_from_db(hdl->sa_bonus, &dnodesize);
67154811da5SToomas Soome 	bonuslen = DN_BONUS_SIZE(dnodesize);
67254811da5SToomas Soome 
6730a586ceaSMark Shellenbaum 	/* first determine bonus header size and sum of all attributes */
6740a586ceaSMark Shellenbaum 	hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
67554811da5SToomas Soome 	    SA_BONUS, bonuslen, &i, &used, &spilling);
6760a586ceaSMark Shellenbaum 
677b5152584SMatthew Ahrens 	if (used > SPA_OLD_MAXBLOCKSIZE)
678be6fd75aSMatthew Ahrens 		return (SET_ERROR(EFBIG));
6790a586ceaSMark Shellenbaum 
6800a586ceaSMark Shellenbaum 	VERIFY(0 == dmu_set_bonus(hdl->sa_bonus, spilling ?
68154811da5SToomas Soome 	    MIN(bonuslen - sizeof (blkptr_t), used + hdrsize) :
6820a586ceaSMark Shellenbaum 	    used + hdrsize, tx));
6830a586ceaSMark Shellenbaum 
6840a586ceaSMark Shellenbaum 	ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
6850a586ceaSMark Shellenbaum 	    bonustype == DMU_OT_SA);
6860a586ceaSMark Shellenbaum 
6870a586ceaSMark Shellenbaum 	/* setup and size spill buffer when needed */
6880a586ceaSMark Shellenbaum 	if (spilling) {
6890a586ceaSMark Shellenbaum 		boolean_t dummy;
6900a586ceaSMark Shellenbaum 
6910a586ceaSMark Shellenbaum 		if (hdl->sa_spill == NULL) {
692eb633035STom Caputi 			VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, 0, NULL,
6931d8ccc7bSMark Shellenbaum 			    &hdl->sa_spill) == 0);
6940a586ceaSMark Shellenbaum 		}
6950a586ceaSMark Shellenbaum 		dmu_buf_will_dirty(hdl->sa_spill, tx);
6960a586ceaSMark Shellenbaum 
6970a586ceaSMark Shellenbaum 		spillhdrsize = sa_find_sizes(sa, &attr_desc[i],
69854811da5SToomas Soome 		    attr_count - i, hdl->sa_spill, SA_SPILL,
69954811da5SToomas Soome 		    hdl->sa_spill->db_size, &i, &spill_used, &dummy);
7000a586ceaSMark Shellenbaum 
701b5152584SMatthew Ahrens 		if (spill_used > SPA_OLD_MAXBLOCKSIZE)
702be6fd75aSMatthew Ahrens 			return (SET_ERROR(EFBIG));
7030a586ceaSMark Shellenbaum 
7040a586ceaSMark Shellenbaum 		buf_space = hdl->sa_spill->db_size - spillhdrsize;
7050a586ceaSMark Shellenbaum 		if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
7060a586ceaSMark Shellenbaum 		    hdl->sa_spill->db_size)
7070a586ceaSMark Shellenbaum 			VERIFY(0 == sa_resize_spill(hdl,
7080a586ceaSMark Shellenbaum 			    BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
7090a586ceaSMark Shellenbaum 	}
7100a586ceaSMark Shellenbaum 
7110a586ceaSMark Shellenbaum 	/* setup starting pointers to lay down data */
7120a586ceaSMark Shellenbaum 	data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
7130a586ceaSMark Shellenbaum 	sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
7140a586ceaSMark Shellenbaum 	buftype = SA_BONUS;
7150a586ceaSMark Shellenbaum 
7160a586ceaSMark Shellenbaum 	if (spilling)
7170a586ceaSMark Shellenbaum 		buf_space = (sa->sa_force_spill) ?
7180a586ceaSMark Shellenbaum 		    0 : SA_BLKPTR_SPACE - hdrsize;
7190a586ceaSMark Shellenbaum 	else
7200a586ceaSMark Shellenbaum 		buf_space = hdl->sa_bonus->db_size - hdrsize;
7210a586ceaSMark Shellenbaum 
7220a586ceaSMark Shellenbaum 	attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
7230a586ceaSMark Shellenbaum 	    KM_SLEEP);
7240a586ceaSMark Shellenbaum 	lot_count = 0;
7250a586ceaSMark Shellenbaum 
7260a586ceaSMark Shellenbaum 	for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
7270a586ceaSMark Shellenbaum 		uint16_t length;
7280a586ceaSMark Shellenbaum 
729644b9528SNed Bass 		ASSERT(IS_P2ALIGNED(data_start, 8));
730644b9528SNed Bass 		ASSERT(IS_P2ALIGNED(buf_space, 8));
7310a586ceaSMark Shellenbaum 		attrs[i] = attr_desc[i].sa_attr;
7320a586ceaSMark Shellenbaum 		length = SA_REGISTERED_LEN(sa, attrs[i]);
7330a586ceaSMark Shellenbaum 		if (length == 0)
7340a586ceaSMark Shellenbaum 			length = attr_desc[i].sa_length;
7350a586ceaSMark Shellenbaum 
7360a586ceaSMark Shellenbaum 		if (buf_space < length) {  /* switch to spill buffer */
737644b9528SNed Bass 			VERIFY(spilling);
7381412a1a2SMark Shellenbaum 			VERIFY(bonustype == DMU_OT_SA);
7390a586ceaSMark Shellenbaum 			if (buftype == SA_BONUS && !sa->sa_force_spill) {
7400a586ceaSMark Shellenbaum 				sa_find_layout(hdl->sa_os, hash, attrs_start,
7410a586ceaSMark Shellenbaum 				    lot_count, tx, &lot);
7420a586ceaSMark Shellenbaum 				SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
7430a586ceaSMark Shellenbaum 			}
7440a586ceaSMark Shellenbaum 
7450a586ceaSMark Shellenbaum 			buftype = SA_SPILL;
7460a586ceaSMark Shellenbaum 			hash = -1ULL;
7470a586ceaSMark Shellenbaum 			len_idx = 0;
7480a586ceaSMark Shellenbaum 
7490a586ceaSMark Shellenbaum 			sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
7500a586ceaSMark Shellenbaum 			sahdr->sa_magic = SA_MAGIC;
7510a586ceaSMark Shellenbaum 			data_start = (void *)((uintptr_t)sahdr +
7520a586ceaSMark Shellenbaum 			    spillhdrsize);
7530a586ceaSMark Shellenbaum 			attrs_start = &attrs[i];
7540a586ceaSMark Shellenbaum 			buf_space = hdl->sa_spill->db_size - spillhdrsize;
7550a586ceaSMark Shellenbaum 			lot_count = 0;
7560a586ceaSMark Shellenbaum 		}
7570a586ceaSMark Shellenbaum 		hash ^= SA_ATTR_HASH(attrs[i]);
7580a586ceaSMark Shellenbaum 		attr_desc[i].sa_addr = data_start;
7590a586ceaSMark Shellenbaum 		attr_desc[i].sa_size = length;
7600a586ceaSMark Shellenbaum 		SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
7610a586ceaSMark Shellenbaum 		    data_start, length);
7620a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
7630a586ceaSMark Shellenbaum 			sahdr->sa_lengths[len_idx++] = length;
7640a586ceaSMark Shellenbaum 		}
7650a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
7660a586ceaSMark Shellenbaum 		    length), 8);
7670a586ceaSMark Shellenbaum 		buf_space -= P2ROUNDUP(length, 8);
7680a586ceaSMark Shellenbaum 		lot_count++;
7690a586ceaSMark Shellenbaum 	}
7700a586ceaSMark Shellenbaum 
7710a586ceaSMark Shellenbaum 	sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
7721412a1a2SMark Shellenbaum 
7731412a1a2SMark Shellenbaum 	/*
7741412a1a2SMark Shellenbaum 	 * Verify that old znodes always have layout number 0.
7751412a1a2SMark Shellenbaum 	 * Must be DMU_OT_SA for arbitrary layouts
7761412a1a2SMark Shellenbaum 	 */
7771412a1a2SMark Shellenbaum 	VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
7781412a1a2SMark Shellenbaum 	    (bonustype == DMU_OT_SA && lot->lot_num > 1));
7791412a1a2SMark Shellenbaum 
7800a586ceaSMark Shellenbaum 	if (bonustype == DMU_OT_SA) {
7810a586ceaSMark Shellenbaum 		SA_SET_HDR(sahdr, lot->lot_num,
7820a586ceaSMark Shellenbaum 		    buftype == SA_BONUS ? hdrsize : spillhdrsize);
7830a586ceaSMark Shellenbaum 	}
7840a586ceaSMark Shellenbaum 
7850a586ceaSMark Shellenbaum 	kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
7860a586ceaSMark Shellenbaum 	if (hdl->sa_bonus_tab) {
7870a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
7880a586ceaSMark Shellenbaum 		hdl->sa_bonus_tab = NULL;
7890a586ceaSMark Shellenbaum 	}
7900a586ceaSMark Shellenbaum 	if (!sa->sa_force_spill)
7910a586ceaSMark Shellenbaum 		VERIFY(0 == sa_build_index(hdl, SA_BONUS));
7920a586ceaSMark Shellenbaum 	if (hdl->sa_spill) {
7930a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
7940a586ceaSMark Shellenbaum 		if (!spilling) {
7950a586ceaSMark Shellenbaum 			/*
7960a586ceaSMark Shellenbaum 			 * remove spill block that is no longer needed.
7970a586ceaSMark Shellenbaum 			 */
7980a586ceaSMark Shellenbaum 			dmu_buf_rele(hdl->sa_spill, NULL);
7990a586ceaSMark Shellenbaum 			hdl->sa_spill = NULL;
8000a586ceaSMark Shellenbaum 			hdl->sa_spill_tab = NULL;
8010a586ceaSMark Shellenbaum 			VERIFY(0 == dmu_rm_spill(hdl->sa_os,
8020a586ceaSMark Shellenbaum 			    sa_handle_object(hdl), tx));
8030a586ceaSMark Shellenbaum 		} else {
8040a586ceaSMark Shellenbaum 			VERIFY(0 == sa_build_index(hdl, SA_SPILL));
8050a586ceaSMark Shellenbaum 		}
8060a586ceaSMark Shellenbaum 	}
8070a586ceaSMark Shellenbaum 
8080a586ceaSMark Shellenbaum 	return (0);
8090a586ceaSMark Shellenbaum }
8100a586ceaSMark Shellenbaum 
8110a586ceaSMark Shellenbaum static void
sa_free_attr_table(sa_os_t * sa)8121d8ccc7bSMark Shellenbaum sa_free_attr_table(sa_os_t *sa)
8131d8ccc7bSMark Shellenbaum {
8141d8ccc7bSMark Shellenbaum 	int i;
8151d8ccc7bSMark Shellenbaum 
8161d8ccc7bSMark Shellenbaum 	if (sa->sa_attr_table == NULL)
8171d8ccc7bSMark Shellenbaum 		return;
8181d8ccc7bSMark Shellenbaum 
8191d8ccc7bSMark Shellenbaum 	for (i = 0; i != sa->sa_num_attrs; i++) {
8201d8ccc7bSMark Shellenbaum 		if (sa->sa_attr_table[i].sa_name)
8211d8ccc7bSMark Shellenbaum 			kmem_free(sa->sa_attr_table[i].sa_name,
8221d8ccc7bSMark Shellenbaum 			    strlen(sa->sa_attr_table[i].sa_name) + 1);
8231d8ccc7bSMark Shellenbaum 	}
8241d8ccc7bSMark Shellenbaum 
8251d8ccc7bSMark Shellenbaum 	kmem_free(sa->sa_attr_table,
8261d8ccc7bSMark Shellenbaum 	    sizeof (sa_attr_table_t) * sa->sa_num_attrs);
8271d8ccc7bSMark Shellenbaum 
8281d8ccc7bSMark Shellenbaum 	sa->sa_attr_table = NULL;
8291d8ccc7bSMark Shellenbaum }
8301d8ccc7bSMark Shellenbaum 
8311d8ccc7bSMark Shellenbaum static int
sa_attr_table_setup(objset_t * os,sa_attr_reg_t * reg_attrs,int count)8320a586ceaSMark Shellenbaum sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
8330a586ceaSMark Shellenbaum {
8340a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
8350a586ceaSMark Shellenbaum 	uint64_t sa_attr_count = 0;
836d5285caeSGeorge Wilson 	uint64_t sa_reg_count = 0;
8370a586ceaSMark Shellenbaum 	int error = 0;
8380a586ceaSMark Shellenbaum 	uint64_t attr_value;
8390a586ceaSMark Shellenbaum 	sa_attr_table_t *tb;
8400a586ceaSMark Shellenbaum 	zap_cursor_t zc;
8410a586ceaSMark Shellenbaum 	zap_attribute_t za;
8420a586ceaSMark Shellenbaum 	int registered_count = 0;
8430a586ceaSMark Shellenbaum 	int i;
8440a586ceaSMark Shellenbaum 	dmu_objset_type_t ostype = dmu_objset_type(os);
8450a586ceaSMark Shellenbaum 
8460a586ceaSMark Shellenbaum 	sa->sa_user_table =
8470a586ceaSMark Shellenbaum 	    kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
8480a586ceaSMark Shellenbaum 	sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
8490a586ceaSMark Shellenbaum 
8501d8ccc7bSMark Shellenbaum 	if (sa->sa_reg_attr_obj != 0) {
8511d8ccc7bSMark Shellenbaum 		error = zap_count(os, sa->sa_reg_attr_obj,
8521d8ccc7bSMark Shellenbaum 		    &sa_attr_count);
8531d8ccc7bSMark Shellenbaum 
8541d8ccc7bSMark Shellenbaum 		/*
8551d8ccc7bSMark Shellenbaum 		 * Make sure we retrieved a count and that it isn't zero
8561d8ccc7bSMark Shellenbaum 		 */
8571d8ccc7bSMark Shellenbaum 		if (error || (error == 0 && sa_attr_count == 0)) {
8581d8ccc7bSMark Shellenbaum 			if (error == 0)
859be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
8601d8ccc7bSMark Shellenbaum 			goto bail;
8611d8ccc7bSMark Shellenbaum 		}
8621d8ccc7bSMark Shellenbaum 		sa_reg_count = sa_attr_count;
8631d8ccc7bSMark Shellenbaum 	}
8640a586ceaSMark Shellenbaum 
8650a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
8660a586ceaSMark Shellenbaum 		sa_attr_count += sa_legacy_attr_count;
8670a586ceaSMark Shellenbaum 
8680a586ceaSMark Shellenbaum 	/* Allocate attribute numbers for attributes that aren't registered */
8690a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
8700a586ceaSMark Shellenbaum 		boolean_t found = B_FALSE;
8710a586ceaSMark Shellenbaum 		int j;
8720a586ceaSMark Shellenbaum 
8730a586ceaSMark Shellenbaum 		if (ostype == DMU_OST_ZFS) {
8740a586ceaSMark Shellenbaum 			for (j = 0; j != sa_legacy_attr_count; j++) {
8750a586ceaSMark Shellenbaum 				if (strcmp(reg_attrs[i].sa_name,
8760a586ceaSMark Shellenbaum 				    sa_legacy_attrs[j].sa_name) == 0) {
8770a586ceaSMark Shellenbaum 					sa->sa_user_table[i] =
8780a586ceaSMark Shellenbaum 					    sa_legacy_attrs[j].sa_attr;
8790a586ceaSMark Shellenbaum 					found = B_TRUE;
8800a586ceaSMark Shellenbaum 				}
8810a586ceaSMark Shellenbaum 			}
8820a586ceaSMark Shellenbaum 		}
8830a586ceaSMark Shellenbaum 		if (found)
8840a586ceaSMark Shellenbaum 			continue;
8850a586ceaSMark Shellenbaum 
8860a586ceaSMark Shellenbaum 		if (sa->sa_reg_attr_obj)
8870a586ceaSMark Shellenbaum 			error = zap_lookup(os, sa->sa_reg_attr_obj,
8880a586ceaSMark Shellenbaum 			    reg_attrs[i].sa_name, 8, 1, &attr_value);
8890a586ceaSMark Shellenbaum 		else
890be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOENT);
8910a586ceaSMark Shellenbaum 		switch (error) {
8920a586ceaSMark Shellenbaum 		case ENOENT:
8930a586ceaSMark Shellenbaum 			sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
8940a586ceaSMark Shellenbaum 			sa_attr_count++;
8950a586ceaSMark Shellenbaum 			break;
8960a586ceaSMark Shellenbaum 		case 0:
8970a586ceaSMark Shellenbaum 			sa->sa_user_table[i] = ATTR_NUM(attr_value);
8980a586ceaSMark Shellenbaum 			break;
8991d8ccc7bSMark Shellenbaum 		default:
9001d8ccc7bSMark Shellenbaum 			goto bail;
9010a586ceaSMark Shellenbaum 		}
9020a586ceaSMark Shellenbaum 	}
9030a586ceaSMark Shellenbaum 
9041d8ccc7bSMark Shellenbaum 	sa->sa_num_attrs = sa_attr_count;
9051d8ccc7bSMark Shellenbaum 	tb = sa->sa_attr_table =
9060a586ceaSMark Shellenbaum 	    kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
9070a586ceaSMark Shellenbaum 
9080a586ceaSMark Shellenbaum 	/*
9090a586ceaSMark Shellenbaum 	 * Attribute table is constructed from requested attribute list,
9100a586ceaSMark Shellenbaum 	 * previously foreign registered attributes, and also the legacy
9110a586ceaSMark Shellenbaum 	 * ZPL set of attributes.
9120a586ceaSMark Shellenbaum 	 */
9130a586ceaSMark Shellenbaum 
9140a586ceaSMark Shellenbaum 	if (sa->sa_reg_attr_obj) {
9150a586ceaSMark Shellenbaum 		for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
9161d8ccc7bSMark Shellenbaum 		    (error = zap_cursor_retrieve(&zc, &za)) == 0;
9170a586ceaSMark Shellenbaum 		    zap_cursor_advance(&zc)) {
9180a586ceaSMark Shellenbaum 			uint64_t value;
9190a586ceaSMark Shellenbaum 			value  = za.za_first_integer;
9200a586ceaSMark Shellenbaum 
9210a586ceaSMark Shellenbaum 			registered_count++;
9220a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
9230a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
9240a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
9250a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_registered = B_TRUE;
9260a586ceaSMark Shellenbaum 
9270a586ceaSMark Shellenbaum 			if (tb[ATTR_NUM(value)].sa_name) {
9280a586ceaSMark Shellenbaum 				continue;
9290a586ceaSMark Shellenbaum 			}
9300a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_name =
9310a586ceaSMark Shellenbaum 			    kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
9320a586ceaSMark Shellenbaum 			(void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
9330a586ceaSMark Shellenbaum 			    strlen(za.za_name) +1);
9340a586ceaSMark Shellenbaum 		}
9350a586ceaSMark Shellenbaum 		zap_cursor_fini(&zc);
9361d8ccc7bSMark Shellenbaum 		/*
9371d8ccc7bSMark Shellenbaum 		 * Make sure we processed the correct number of registered
9381d8ccc7bSMark Shellenbaum 		 * attributes
9391d8ccc7bSMark Shellenbaum 		 */
9401d8ccc7bSMark Shellenbaum 		if (registered_count != sa_reg_count) {
9411d8ccc7bSMark Shellenbaum 			ASSERT(error != 0);
9421d8ccc7bSMark Shellenbaum 			goto bail;
9431d8ccc7bSMark Shellenbaum 		}
9441d8ccc7bSMark Shellenbaum 
9450a586ceaSMark Shellenbaum 	}
9460a586ceaSMark Shellenbaum 
9470a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS) {
9480a586ceaSMark Shellenbaum 		for (i = 0; i != sa_legacy_attr_count; i++) {
9490a586ceaSMark Shellenbaum 			if (tb[i].sa_name)
9500a586ceaSMark Shellenbaum 				continue;
9510a586ceaSMark Shellenbaum 			tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
9520a586ceaSMark Shellenbaum 			tb[i].sa_length = sa_legacy_attrs[i].sa_length;
9530a586ceaSMark Shellenbaum 			tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
9540a586ceaSMark Shellenbaum 			tb[i].sa_registered = B_FALSE;
9550a586ceaSMark Shellenbaum 			tb[i].sa_name =
9560a586ceaSMark Shellenbaum 			    kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
9570a586ceaSMark Shellenbaum 			    KM_SLEEP);
9580a586ceaSMark Shellenbaum 			(void) strlcpy(tb[i].sa_name,
9590a586ceaSMark Shellenbaum 			    sa_legacy_attrs[i].sa_name,
9600a586ceaSMark Shellenbaum 			    strlen(sa_legacy_attrs[i].sa_name) + 1);
9610a586ceaSMark Shellenbaum 		}
9620a586ceaSMark Shellenbaum 	}
9630a586ceaSMark Shellenbaum 
9640a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
9650a586ceaSMark Shellenbaum 		sa_attr_type_t attr_id;
9660a586ceaSMark Shellenbaum 
9670a586ceaSMark Shellenbaum 		attr_id = sa->sa_user_table[i];
9680a586ceaSMark Shellenbaum 		if (tb[attr_id].sa_name)
9690a586ceaSMark Shellenbaum 			continue;
9700a586ceaSMark Shellenbaum 
9710a586ceaSMark Shellenbaum 		tb[attr_id].sa_length = reg_attrs[i].sa_length;
9720a586ceaSMark Shellenbaum 		tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
9730a586ceaSMark Shellenbaum 		tb[attr_id].sa_attr = attr_id;
9740a586ceaSMark Shellenbaum 		tb[attr_id].sa_name =
9750a586ceaSMark Shellenbaum 		    kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
9760a586ceaSMark Shellenbaum 		(void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
9770a586ceaSMark Shellenbaum 		    strlen(reg_attrs[i].sa_name) + 1);
9780a586ceaSMark Shellenbaum 	}
9790a586ceaSMark Shellenbaum 
9801d8ccc7bSMark Shellenbaum 	sa->sa_need_attr_registration =
9810a586ceaSMark Shellenbaum 	    (sa_attr_count != registered_count);
9821d8ccc7bSMark Shellenbaum 
9831d8ccc7bSMark Shellenbaum 	return (0);
9841d8ccc7bSMark Shellenbaum bail:
9851d8ccc7bSMark Shellenbaum 	kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
9861d8ccc7bSMark Shellenbaum 	sa->sa_user_table = NULL;
9871d8ccc7bSMark Shellenbaum 	sa_free_attr_table(sa);
9881d8ccc7bSMark Shellenbaum 	return ((error != 0) ? error : EINVAL);
9890a586ceaSMark Shellenbaum }
9900a586ceaSMark Shellenbaum 
9911d8ccc7bSMark Shellenbaum int
sa_setup(objset_t * os,uint64_t sa_obj,sa_attr_reg_t * reg_attrs,int count,sa_attr_type_t ** user_table)9921d8ccc7bSMark Shellenbaum sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
9931d8ccc7bSMark Shellenbaum     sa_attr_type_t **user_table)
9940a586ceaSMark Shellenbaum {
9950a586ceaSMark Shellenbaum 	zap_cursor_t zc;
9960a586ceaSMark Shellenbaum 	zap_attribute_t za;
9970a586ceaSMark Shellenbaum 	sa_os_t *sa;
9980a586ceaSMark Shellenbaum 	dmu_objset_type_t ostype = dmu_objset_type(os);
9990a586ceaSMark Shellenbaum 	sa_attr_type_t *tb;
10001d8ccc7bSMark Shellenbaum 	int error;
10010a586ceaSMark Shellenbaum 
10023b2aab18SMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
10030a586ceaSMark Shellenbaum 	if (os->os_sa) {
10040a586ceaSMark Shellenbaum 		mutex_enter(&os->os_sa->sa_lock);
10053b2aab18SMatthew Ahrens 		mutex_exit(&os->os_user_ptr_lock);
10060a586ceaSMark Shellenbaum 		tb = os->os_sa->sa_user_table;
10070a586ceaSMark Shellenbaum 		mutex_exit(&os->os_sa->sa_lock);
10081d8ccc7bSMark Shellenbaum 		*user_table = tb;
10091d8ccc7bSMark Shellenbaum 		return (0);
10100a586ceaSMark Shellenbaum 	}
10110a586ceaSMark Shellenbaum 
10120a586ceaSMark Shellenbaum 	sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
10130a586ceaSMark Shellenbaum 	mutex_init(&sa->sa_lock, NULL, MUTEX_DEFAULT, NULL);
10140a586ceaSMark Shellenbaum 	sa->sa_master_obj = sa_obj;
10150a586ceaSMark Shellenbaum 
10161d8ccc7bSMark Shellenbaum 	os->os_sa = sa;
10170a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
10183b2aab18SMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
10190a586ceaSMark Shellenbaum 	avl_create(&sa->sa_layout_num_tree, layout_num_compare,
10200a586ceaSMark Shellenbaum 	    sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
10210a586ceaSMark Shellenbaum 	avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
10220a586ceaSMark Shellenbaum 	    sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
10230a586ceaSMark Shellenbaum 
10240a586ceaSMark Shellenbaum 	if (sa_obj) {
10250a586ceaSMark Shellenbaum 		error = zap_lookup(os, sa_obj, SA_LAYOUTS,
10260a586ceaSMark Shellenbaum 		    8, 1, &sa->sa_layout_attr_obj);
10271d8ccc7bSMark Shellenbaum 		if (error != 0 && error != ENOENT)
10281d8ccc7bSMark Shellenbaum 			goto fail;
10290a586ceaSMark Shellenbaum 		error = zap_lookup(os, sa_obj, SA_REGISTRY,
10300a586ceaSMark Shellenbaum 		    8, 1, &sa->sa_reg_attr_obj);
10311d8ccc7bSMark Shellenbaum 		if (error != 0 && error != ENOENT)
10321d8ccc7bSMark Shellenbaum 			goto fail;
10330a586ceaSMark Shellenbaum 	}
10340a586ceaSMark Shellenbaum 
10351d8ccc7bSMark Shellenbaum 	if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
10361d8ccc7bSMark Shellenbaum 		goto fail;
10370a586ceaSMark Shellenbaum 
10380a586ceaSMark Shellenbaum 	if (sa->sa_layout_attr_obj != 0) {
10391d8ccc7bSMark Shellenbaum 		uint64_t layout_count;
10401d8ccc7bSMark Shellenbaum 
10411d8ccc7bSMark Shellenbaum 		error = zap_count(os, sa->sa_layout_attr_obj,
10421d8ccc7bSMark Shellenbaum 		    &layout_count);
10431d8ccc7bSMark Shellenbaum 
10441d8ccc7bSMark Shellenbaum 		/*
10451d8ccc7bSMark Shellenbaum 		 * Layout number count should be > 0
10461d8ccc7bSMark Shellenbaum 		 */
10471d8ccc7bSMark Shellenbaum 		if (error || (error == 0 && layout_count == 0)) {
10481d8ccc7bSMark Shellenbaum 			if (error == 0)
1049be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
10501d8ccc7bSMark Shellenbaum 			goto fail;
10511d8ccc7bSMark Shellenbaum 		}
10521d8ccc7bSMark Shellenbaum 
10530a586ceaSMark Shellenbaum 		for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
10541d8ccc7bSMark Shellenbaum 		    (error = zap_cursor_retrieve(&zc, &za)) == 0;
10550a586ceaSMark Shellenbaum 		    zap_cursor_advance(&zc)) {
10560a586ceaSMark Shellenbaum 			sa_attr_type_t *lot_attrs;
10570a586ceaSMark Shellenbaum 			uint64_t lot_num;
10580a586ceaSMark Shellenbaum 
10590a586ceaSMark Shellenbaum 			lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
10600a586ceaSMark Shellenbaum 			    za.za_num_integers, KM_SLEEP);
10610a586ceaSMark Shellenbaum 
10621d8ccc7bSMark Shellenbaum 			if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
10631d8ccc7bSMark Shellenbaum 			    za.za_name, 2, za.za_num_integers,
10641d8ccc7bSMark Shellenbaum 			    lot_attrs))) != 0) {
10651d8ccc7bSMark Shellenbaum 				kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
10661d8ccc7bSMark Shellenbaum 				    za.za_num_integers);
10671d8ccc7bSMark Shellenbaum 				break;
10681d8ccc7bSMark Shellenbaum 			}
10690a586ceaSMark Shellenbaum 			VERIFY(ddi_strtoull(za.za_name, NULL, 10,
10700a586ceaSMark Shellenbaum 			    (unsigned long long *)&lot_num) == 0);
10710a586ceaSMark Shellenbaum 
10720a586ceaSMark Shellenbaum 			(void) sa_add_layout_entry(os, lot_attrs,
10730a586ceaSMark Shellenbaum 			    za.za_num_integers, lot_num,
10740a586ceaSMark Shellenbaum 			    sa_layout_info_hash(lot_attrs,
10750a586ceaSMark Shellenbaum 			    za.za_num_integers), B_FALSE, NULL);
10760a586ceaSMark Shellenbaum 			kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
10770a586ceaSMark Shellenbaum 			    za.za_num_integers);
10780a586ceaSMark Shellenbaum 		}
10790a586ceaSMark Shellenbaum 		zap_cursor_fini(&zc);
10801d8ccc7bSMark Shellenbaum 
10811d8ccc7bSMark Shellenbaum 		/*
10821d8ccc7bSMark Shellenbaum 		 * Make sure layout count matches number of entries added
10831d8ccc7bSMark Shellenbaum 		 * to AVL tree
10841d8ccc7bSMark Shellenbaum 		 */
10851d8ccc7bSMark Shellenbaum 		if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
10861d8ccc7bSMark Shellenbaum 			ASSERT(error != 0);
10871d8ccc7bSMark Shellenbaum 			goto fail;
10881d8ccc7bSMark Shellenbaum 		}
10890a586ceaSMark Shellenbaum 	}
10900a586ceaSMark Shellenbaum 
10910a586ceaSMark Shellenbaum 	/* Add special layout number for old ZNODES */
10920a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS) {
10930a586ceaSMark Shellenbaum 		(void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
10940a586ceaSMark Shellenbaum 		    sa_legacy_attr_count, 0,
10950a586ceaSMark Shellenbaum 		    sa_layout_info_hash(sa_legacy_zpl_layout,
10960a586ceaSMark Shellenbaum 		    sa_legacy_attr_count), B_FALSE, NULL);
10970a586ceaSMark Shellenbaum 
10980a586ceaSMark Shellenbaum 		(void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
10990a586ceaSMark Shellenbaum 		    0, B_FALSE, NULL);
11000a586ceaSMark Shellenbaum 	}
11011d8ccc7bSMark Shellenbaum 	*user_table = os->os_sa->sa_user_table;
11020a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
11031d8ccc7bSMark Shellenbaum 	return (0);
11041d8ccc7bSMark Shellenbaum fail:
11051d8ccc7bSMark Shellenbaum 	os->os_sa = NULL;
11061d8ccc7bSMark Shellenbaum 	sa_free_attr_table(sa);
11071d8ccc7bSMark Shellenbaum 	if (sa->sa_user_table)
11081d8ccc7bSMark Shellenbaum 		kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
11091d8ccc7bSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
1110d2b3cbbdSJorgen Lundman 	avl_destroy(&sa->sa_layout_hash_tree);
1111d2b3cbbdSJorgen Lundman 	avl_destroy(&sa->sa_layout_num_tree);
1112d2b3cbbdSJorgen Lundman 	mutex_destroy(&sa->sa_lock);
11131d8ccc7bSMark Shellenbaum 	kmem_free(sa, sizeof (sa_os_t));
11141d8ccc7bSMark Shellenbaum 	return ((error == ECKSUM) ? EIO : error);
11150a586ceaSMark Shellenbaum }
11160a586ceaSMark Shellenbaum 
11170a586ceaSMark Shellenbaum void
sa_tear_down(objset_t * os)11180a586ceaSMark Shellenbaum sa_tear_down(objset_t *os)
11190a586ceaSMark Shellenbaum {
11200a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
11210a586ceaSMark Shellenbaum 	sa_lot_t *layout;
11220a586ceaSMark Shellenbaum 	void *cookie;
11230a586ceaSMark Shellenbaum 
11240a586ceaSMark Shellenbaum 	kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
11250a586ceaSMark Shellenbaum 
11260a586ceaSMark Shellenbaum 	/* Free up attr table */
11270a586ceaSMark Shellenbaum 
11281d8ccc7bSMark Shellenbaum 	sa_free_attr_table(sa);
11290a586ceaSMark Shellenbaum 
11300a586ceaSMark Shellenbaum 	cookie = NULL;
11310a586ceaSMark Shellenbaum 	while (layout = avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie)) {
11320a586ceaSMark Shellenbaum 		sa_idx_tab_t *tab;
11330a586ceaSMark Shellenbaum 		while (tab = list_head(&layout->lot_idx_tab)) {
1134e914ace2STim Schumacher 			ASSERT(zfs_refcount_count(&tab->sa_refcount));
11350a586ceaSMark Shellenbaum 			sa_idx_tab_rele(os, tab);
11360a586ceaSMark Shellenbaum 		}
11370a586ceaSMark Shellenbaum 	}
11380a586ceaSMark Shellenbaum 
11390a586ceaSMark Shellenbaum 	cookie = NULL;
11400a586ceaSMark Shellenbaum 	while (layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie)) {
11410a586ceaSMark Shellenbaum 		kmem_free(layout->lot_attrs,
11420a586ceaSMark Shellenbaum 		    sizeof (sa_attr_type_t) * layout->lot_attr_count);
11430a586ceaSMark Shellenbaum 		kmem_free(layout, sizeof (sa_lot_t));
11440a586ceaSMark Shellenbaum 	}
11450a586ceaSMark Shellenbaum 
11460a586ceaSMark Shellenbaum 	avl_destroy(&sa->sa_layout_hash_tree);
11470a586ceaSMark Shellenbaum 	avl_destroy(&sa->sa_layout_num_tree);
1148d2b3cbbdSJorgen Lundman 	mutex_destroy(&sa->sa_lock);
11490a586ceaSMark Shellenbaum 
11500a586ceaSMark Shellenbaum 	kmem_free(sa, sizeof (sa_os_t));
11510a586ceaSMark Shellenbaum 	os->os_sa = NULL;
11520a586ceaSMark Shellenbaum }
11530a586ceaSMark Shellenbaum 
11540a586ceaSMark Shellenbaum void
sa_build_idx_tab(void * hdr,void * attr_addr,sa_attr_type_t attr,uint16_t length,int length_idx,boolean_t var_length,void * userp)11550a586ceaSMark Shellenbaum sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
11560a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t var_length, void *userp)
11570a586ceaSMark Shellenbaum {
11580a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab = userp;
11590a586ceaSMark Shellenbaum 
11600a586ceaSMark Shellenbaum 	if (var_length) {
11610a586ceaSMark Shellenbaum 		ASSERT(idx_tab->sa_variable_lengths);
11620a586ceaSMark Shellenbaum 		idx_tab->sa_variable_lengths[length_idx] = length;
11630a586ceaSMark Shellenbaum 	}
11640a586ceaSMark Shellenbaum 	TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
11650a586ceaSMark Shellenbaum 	    (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
11660a586ceaSMark Shellenbaum }
11670a586ceaSMark Shellenbaum 
11680a586ceaSMark Shellenbaum static void
sa_attr_iter(objset_t * os,sa_hdr_phys_t * hdr,dmu_object_type_t type,sa_iterfunc_t func,sa_lot_t * tab,void * userp)11690a586ceaSMark Shellenbaum sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
11700a586ceaSMark Shellenbaum     sa_iterfunc_t func, sa_lot_t *tab, void *userp)
11710a586ceaSMark Shellenbaum {
11720a586ceaSMark Shellenbaum 	void *data_start;
11730a586ceaSMark Shellenbaum 	sa_lot_t *tb = tab;
11740a586ceaSMark Shellenbaum 	sa_lot_t search;
11750a586ceaSMark Shellenbaum 	avl_index_t loc;
11760a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
11770a586ceaSMark Shellenbaum 	int i;
117806e0070dSMark Shellenbaum 	uint16_t *length_start = NULL;
11790a586ceaSMark Shellenbaum 	uint8_t length_idx = 0;
11800a586ceaSMark Shellenbaum 
11810a586ceaSMark Shellenbaum 	if (tab == NULL) {
11820a586ceaSMark Shellenbaum 		search.lot_num = SA_LAYOUT_NUM(hdr, type);
11830a586ceaSMark Shellenbaum 		tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
11840a586ceaSMark Shellenbaum 		ASSERT(tb);
11850a586ceaSMark Shellenbaum 	}
11860a586ceaSMark Shellenbaum 
11870a586ceaSMark Shellenbaum 	if (IS_SA_BONUSTYPE(type)) {
11880a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
11890a586ceaSMark Shellenbaum 		    offsetof(sa_hdr_phys_t, sa_lengths) +
11900a586ceaSMark Shellenbaum 		    (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
11910a586ceaSMark Shellenbaum 		length_start = hdr->sa_lengths;
11920a586ceaSMark Shellenbaum 	} else {
11930a586ceaSMark Shellenbaum 		data_start = hdr;
11940a586ceaSMark Shellenbaum 	}
11950a586ceaSMark Shellenbaum 
11960a586ceaSMark Shellenbaum 	for (i = 0; i != tb->lot_attr_count; i++) {
11970a586ceaSMark Shellenbaum 		int attr_length, reg_length;
11980a586ceaSMark Shellenbaum 		uint8_t idx_len;
11990a586ceaSMark Shellenbaum 
12000a586ceaSMark Shellenbaum 		reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
12010a586ceaSMark Shellenbaum 		if (reg_length) {
12020a586ceaSMark Shellenbaum 			attr_length = reg_length;
12030a586ceaSMark Shellenbaum 			idx_len = 0;
12040a586ceaSMark Shellenbaum 		} else {
12050a586ceaSMark Shellenbaum 			attr_length = length_start[length_idx];
12060a586ceaSMark Shellenbaum 			idx_len = length_idx++;
12070a586ceaSMark Shellenbaum 		}
12080a586ceaSMark Shellenbaum 
12090a586ceaSMark Shellenbaum 		func(hdr, data_start, tb->lot_attrs[i], attr_length,
12100a586ceaSMark Shellenbaum 		    idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
12110a586ceaSMark Shellenbaum 
12120a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
12130a586ceaSMark Shellenbaum 		    attr_length), 8);
12140a586ceaSMark Shellenbaum 	}
12150a586ceaSMark Shellenbaum }
12160a586ceaSMark Shellenbaum 
12170a586ceaSMark Shellenbaum /*ARGSUSED*/
12180a586ceaSMark Shellenbaum void
sa_byteswap_cb(void * hdr,void * attr_addr,sa_attr_type_t attr,uint16_t length,int length_idx,boolean_t variable_length,void * userp)12190a586ceaSMark Shellenbaum sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
12200a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t variable_length, void *userp)
12210a586ceaSMark Shellenbaum {
12220a586ceaSMark Shellenbaum 	sa_handle_t *hdl = userp;
12230a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
12240a586ceaSMark Shellenbaum 
12250a586ceaSMark Shellenbaum 	sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
12260a586ceaSMark Shellenbaum }
12270a586ceaSMark Shellenbaum 
12280a586ceaSMark Shellenbaum void
sa_byteswap(sa_handle_t * hdl,sa_buf_type_t buftype)12290a586ceaSMark Shellenbaum sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
12300a586ceaSMark Shellenbaum {
12310a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
12320a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db;
12330a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
12340a586ceaSMark Shellenbaum 	int num_lengths = 1;
12350a586ceaSMark Shellenbaum 	int i;
12360a586ceaSMark Shellenbaum 
12370a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
12380a586ceaSMark Shellenbaum 	if (sa_hdr_phys->sa_magic == SA_MAGIC)
12390a586ceaSMark Shellenbaum 		return;
12400a586ceaSMark Shellenbaum 
12410a586ceaSMark Shellenbaum 	db = SA_GET_DB(hdl, buftype);
12420a586ceaSMark Shellenbaum 
12430a586ceaSMark Shellenbaum 	if (buftype == SA_SPILL) {
12440a586ceaSMark Shellenbaum 		arc_release(db->db_buf, NULL);
12450a586ceaSMark Shellenbaum 		arc_buf_thaw(db->db_buf);
12460a586ceaSMark Shellenbaum 	}
12470a586ceaSMark Shellenbaum 
12480a586ceaSMark Shellenbaum 	sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
12490a586ceaSMark Shellenbaum 	sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
12500a586ceaSMark Shellenbaum 
12510a586ceaSMark Shellenbaum 	/*
12520a586ceaSMark Shellenbaum 	 * Determine number of variable lenghts in header
12530a586ceaSMark Shellenbaum 	 * The standard 8 byte header has one for free and a
12540a586ceaSMark Shellenbaum 	 * 16 byte header would have 4 + 1;
12550a586ceaSMark Shellenbaum 	 */
12560a586ceaSMark Shellenbaum 	if (SA_HDR_SIZE(sa_hdr_phys) > 8)
12570a586ceaSMark Shellenbaum 		num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
12580a586ceaSMark Shellenbaum 	for (i = 0; i != num_lengths; i++)
12590a586ceaSMark Shellenbaum 		sa_hdr_phys->sa_lengths[i] =
12600a586ceaSMark Shellenbaum 		    BSWAP_16(sa_hdr_phys->sa_lengths[i]);
12610a586ceaSMark Shellenbaum 
12620a586ceaSMark Shellenbaum 	sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
12630a586ceaSMark Shellenbaum 	    sa_byteswap_cb, NULL, hdl);
12640a586ceaSMark Shellenbaum 
12650a586ceaSMark Shellenbaum 	if (buftype == SA_SPILL)
12660a586ceaSMark Shellenbaum 		arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
12670a586ceaSMark Shellenbaum }
12680a586ceaSMark Shellenbaum 
12690a586ceaSMark Shellenbaum static int
sa_build_index(sa_handle_t * hdl,sa_buf_type_t buftype)12700a586ceaSMark Shellenbaum sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
12710a586ceaSMark Shellenbaum {
12720a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sa_hdr_phys;
12730a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
12740a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
12750a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
12760a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
12770a586ceaSMark Shellenbaum 
12780a586ceaSMark Shellenbaum 	sa_hdr_phys = SA_GET_HDR(hdl, buftype);
12790a586ceaSMark Shellenbaum 
12800a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
12810a586ceaSMark Shellenbaum 
12820a586ceaSMark Shellenbaum 	/* Do we need to byteswap? */
12830a586ceaSMark Shellenbaum 
12840a586ceaSMark Shellenbaum 	/* only check if not old znode */
12850a586ceaSMark Shellenbaum 	if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
12860a586ceaSMark Shellenbaum 	    sa_hdr_phys->sa_magic != 0) {
12870a586ceaSMark Shellenbaum 		VERIFY(BSWAP_32(sa_hdr_phys->sa_magic) == SA_MAGIC);
12880a586ceaSMark Shellenbaum 		sa_byteswap(hdl, buftype);
12890a586ceaSMark Shellenbaum 	}
12900a586ceaSMark Shellenbaum 
12910a586ceaSMark Shellenbaum 	idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
12920a586ceaSMark Shellenbaum 
12930a586ceaSMark Shellenbaum 	if (buftype == SA_BONUS)
12940a586ceaSMark Shellenbaum 		hdl->sa_bonus_tab = idx_tab;
12950a586ceaSMark Shellenbaum 	else
12960a586ceaSMark Shellenbaum 		hdl->sa_spill_tab = idx_tab;
12970a586ceaSMark Shellenbaum 
12980a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
12990a586ceaSMark Shellenbaum 	return (0);
13000a586ceaSMark Shellenbaum }
13010a586ceaSMark Shellenbaum 
13020a586ceaSMark Shellenbaum /*ARGSUSED*/
1303bc9014e6SJustin Gibbs static void
sa_evict_sync(void * dbu)130440510e8eSJosef 'Jeff' Sipek sa_evict_sync(void *dbu)
13050a586ceaSMark Shellenbaum {
1306bc9014e6SJustin Gibbs 	panic("evicting sa dbuf\n");
13070a586ceaSMark Shellenbaum }
13080a586ceaSMark Shellenbaum 
13090a586ceaSMark Shellenbaum static void
sa_idx_tab_rele(objset_t * os,void * arg)13100a586ceaSMark Shellenbaum sa_idx_tab_rele(objset_t *os, void *arg)
13110a586ceaSMark Shellenbaum {
13120a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
13130a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab = arg;
13140a586ceaSMark Shellenbaum 
13150a586ceaSMark Shellenbaum 	if (idx_tab == NULL)
13160a586ceaSMark Shellenbaum 		return;
13170a586ceaSMark Shellenbaum 
13180a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
1319e914ace2STim Schumacher 	if (zfs_refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
13200a586ceaSMark Shellenbaum 		list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
13210a586ceaSMark Shellenbaum 		if (idx_tab->sa_variable_lengths)
13220a586ceaSMark Shellenbaum 			kmem_free(idx_tab->sa_variable_lengths,
13230a586ceaSMark Shellenbaum 			    sizeof (uint16_t) *
13240a586ceaSMark Shellenbaum 			    idx_tab->sa_layout->lot_var_sizes);
1325e914ace2STim Schumacher 		zfs_refcount_destroy(&idx_tab->sa_refcount);
13260a586ceaSMark Shellenbaum 		kmem_free(idx_tab->sa_idx_tab,
13270a586ceaSMark Shellenbaum 		    sizeof (uint32_t) * sa->sa_num_attrs);
13280a586ceaSMark Shellenbaum 		kmem_free(idx_tab, sizeof (sa_idx_tab_t));
13290a586ceaSMark Shellenbaum 	}
13300a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
13310a586ceaSMark Shellenbaum }
13320a586ceaSMark Shellenbaum 
13330a586ceaSMark Shellenbaum static void
sa_idx_tab_hold(objset_t * os,sa_idx_tab_t * idx_tab)13340a586ceaSMark Shellenbaum sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
13350a586ceaSMark Shellenbaum {
13360a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
13370a586ceaSMark Shellenbaum 
13380a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
1339e914ace2STim Schumacher 	(void) zfs_refcount_add(&idx_tab->sa_refcount, NULL);
13400a586ceaSMark Shellenbaum }
13410a586ceaSMark Shellenbaum 
13420a586ceaSMark Shellenbaum void
sa_handle_destroy(sa_handle_t * hdl)13430a586ceaSMark Shellenbaum sa_handle_destroy(sa_handle_t *hdl)
13440a586ceaSMark Shellenbaum {
1345bc9014e6SJustin Gibbs 	dmu_buf_t *db = hdl->sa_bonus;
1346bc9014e6SJustin Gibbs 
13470a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
1348bc9014e6SJustin Gibbs 	(void) dmu_buf_remove_user(db, &hdl->sa_dbu);
13490a586ceaSMark Shellenbaum 
13500fda3cc5SJustin T. Gibbs 	if (hdl->sa_bonus_tab)
13510a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
13520fda3cc5SJustin T. Gibbs 
13530fda3cc5SJustin T. Gibbs 	if (hdl->sa_spill_tab)
13540a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
13550a586ceaSMark Shellenbaum 
13560a586ceaSMark Shellenbaum 	dmu_buf_rele(hdl->sa_bonus, NULL);
13570a586ceaSMark Shellenbaum 
13580a586ceaSMark Shellenbaum 	if (hdl->sa_spill)
13590a586ceaSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
13600a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
13610a586ceaSMark Shellenbaum 
13620a586ceaSMark Shellenbaum 	kmem_cache_free(sa_cache, hdl);
13630a586ceaSMark Shellenbaum }
13640a586ceaSMark Shellenbaum 
13650a586ceaSMark Shellenbaum int
sa_handle_get_from_db(objset_t * os,dmu_buf_t * db,void * userp,sa_handle_type_t hdl_type,sa_handle_t ** handlepp)13660a586ceaSMark Shellenbaum sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
13670a586ceaSMark Shellenbaum     sa_handle_type_t hdl_type, sa_handle_t **handlepp)
13680a586ceaSMark Shellenbaum {
13690a586ceaSMark Shellenbaum 	int error = 0;
13700a586ceaSMark Shellenbaum 	dmu_object_info_t doi;
1371bc9014e6SJustin Gibbs 	sa_handle_t *handle = NULL;
13720a586ceaSMark Shellenbaum 
13730a586ceaSMark Shellenbaum #ifdef ZFS_DEBUG
13740a586ceaSMark Shellenbaum 	dmu_object_info_from_db(db, &doi);
13750a586ceaSMark Shellenbaum 	ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
13760a586ceaSMark Shellenbaum 	    doi.doi_bonus_type == DMU_OT_ZNODE);
13770a586ceaSMark Shellenbaum #endif
13780a586ceaSMark Shellenbaum 	/* find handle, if it exists */
13790a586ceaSMark Shellenbaum 	/* if one doesn't exist then create a new one, and initialize it */
13800a586ceaSMark Shellenbaum 
1381bc9014e6SJustin Gibbs 	if (hdl_type == SA_HDL_SHARED)
1382bc9014e6SJustin Gibbs 		handle = dmu_buf_get_user(db);
1383bc9014e6SJustin Gibbs 
13840a586ceaSMark Shellenbaum 	if (handle == NULL) {
1385bc9014e6SJustin Gibbs 		sa_handle_t *winner = NULL;
1386bc9014e6SJustin Gibbs 
13870a586ceaSMark Shellenbaum 		handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
138840510e8eSJosef 'Jeff' Sipek 		handle->sa_dbu.dbu_evict_func_sync = NULL;
138940510e8eSJosef 'Jeff' Sipek 		handle->sa_dbu.dbu_evict_func_async = NULL;
13900a586ceaSMark Shellenbaum 		handle->sa_userp = userp;
13910a586ceaSMark Shellenbaum 		handle->sa_bonus = db;
13920a586ceaSMark Shellenbaum 		handle->sa_os = os;
13930a586ceaSMark Shellenbaum 		handle->sa_spill = NULL;
13940fda3cc5SJustin T. Gibbs 		handle->sa_bonus_tab = NULL;
13950fda3cc5SJustin T. Gibbs 		handle->sa_spill_tab = NULL;
13960a586ceaSMark Shellenbaum 
13970a586ceaSMark Shellenbaum 		error = sa_build_index(handle, SA_BONUS);
13980a586ceaSMark Shellenbaum 
1399bc9014e6SJustin Gibbs 		if (hdl_type == SA_HDL_SHARED) {
140040510e8eSJosef 'Jeff' Sipek 			dmu_buf_init_user(&handle->sa_dbu, sa_evict_sync, NULL,
140140510e8eSJosef 'Jeff' Sipek 			    NULL);
1402bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1403bc9014e6SJustin Gibbs 		}
1404bc9014e6SJustin Gibbs 
1405bc9014e6SJustin Gibbs 		if (winner != NULL) {
14060a586ceaSMark Shellenbaum 			kmem_cache_free(sa_cache, handle);
1407bc9014e6SJustin Gibbs 			handle = winner;
14080a586ceaSMark Shellenbaum 		}
14090a586ceaSMark Shellenbaum 	}
14100a586ceaSMark Shellenbaum 	*handlepp = handle;
14110a586ceaSMark Shellenbaum 
14120a586ceaSMark Shellenbaum 	return (error);
14130a586ceaSMark Shellenbaum }
14140a586ceaSMark Shellenbaum 
14150a586ceaSMark Shellenbaum int
sa_handle_get(objset_t * objset,uint64_t objid,void * userp,sa_handle_type_t hdl_type,sa_handle_t ** handlepp)14160a586ceaSMark Shellenbaum sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
14170a586ceaSMark Shellenbaum     sa_handle_type_t hdl_type, sa_handle_t **handlepp)
14180a586ceaSMark Shellenbaum {
14190a586ceaSMark Shellenbaum 	dmu_buf_t *db;
14200a586ceaSMark Shellenbaum 	int error;
14210a586ceaSMark Shellenbaum 
14220a586ceaSMark Shellenbaum 	if (error = dmu_bonus_hold(objset, objid, NULL, &db))
14230a586ceaSMark Shellenbaum 		return (error);
14240a586ceaSMark Shellenbaum 
14250a586ceaSMark Shellenbaum 	return (sa_handle_get_from_db(objset, db, userp, hdl_type,
14260a586ceaSMark Shellenbaum 	    handlepp));
14270a586ceaSMark Shellenbaum }
14280a586ceaSMark Shellenbaum 
14290a586ceaSMark Shellenbaum int
sa_buf_hold(objset_t * objset,uint64_t obj_num,void * tag,dmu_buf_t ** db)14300a586ceaSMark Shellenbaum sa_buf_hold(objset_t *objset, uint64_t obj_num, void *tag, dmu_buf_t **db)
14310a586ceaSMark Shellenbaum {
14320a586ceaSMark Shellenbaum 	return (dmu_bonus_hold(objset, obj_num, tag, db));
14330a586ceaSMark Shellenbaum }
14340a586ceaSMark Shellenbaum 
14350a586ceaSMark Shellenbaum void
sa_buf_rele(dmu_buf_t * db,void * tag)14360a586ceaSMark Shellenbaum sa_buf_rele(dmu_buf_t *db, void *tag)
14370a586ceaSMark Shellenbaum {
14380a586ceaSMark Shellenbaum 	dmu_buf_rele(db, tag);
14390a586ceaSMark Shellenbaum }
14400a586ceaSMark Shellenbaum 
14410a586ceaSMark Shellenbaum int
sa_lookup_impl(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count)14420a586ceaSMark Shellenbaum sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
14430a586ceaSMark Shellenbaum {
14440a586ceaSMark Shellenbaum 	ASSERT(hdl);
14450a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
14460a586ceaSMark Shellenbaum 	return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
14470a586ceaSMark Shellenbaum }
14480a586ceaSMark Shellenbaum 
1449f67950b2SNasf-Fan static int
sa_lookup_locked(sa_handle_t * hdl,sa_attr_type_t attr,void * buf,uint32_t buflen)1450f67950b2SNasf-Fan sa_lookup_locked(sa_handle_t *hdl, sa_attr_type_t attr, void *buf,
1451f67950b2SNasf-Fan     uint32_t buflen)
14520a586ceaSMark Shellenbaum {
14530a586ceaSMark Shellenbaum 	int error;
14540a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
14550a586ceaSMark Shellenbaum 
14560a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
14570a586ceaSMark Shellenbaum 	bulk.sa_data = buf;
14580a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
14590a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
14600a586ceaSMark Shellenbaum 
14610a586ceaSMark Shellenbaum 	ASSERT(hdl);
14620a586ceaSMark Shellenbaum 	error = sa_lookup_impl(hdl, &bulk, 1);
1463f67950b2SNasf-Fan 	return (error);
1464f67950b2SNasf-Fan }
1465f67950b2SNasf-Fan 
1466f67950b2SNasf-Fan int
sa_lookup(sa_handle_t * hdl,sa_attr_type_t attr,void * buf,uint32_t buflen)1467f67950b2SNasf-Fan sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1468f67950b2SNasf-Fan {
1469f67950b2SNasf-Fan 	int error;
1470f67950b2SNasf-Fan 
1471f67950b2SNasf-Fan 	mutex_enter(&hdl->sa_lock);
1472f67950b2SNasf-Fan 	error = sa_lookup_locked(hdl, attr, buf, buflen);
14730a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
1474f67950b2SNasf-Fan 
14750a586ceaSMark Shellenbaum 	return (error);
14760a586ceaSMark Shellenbaum }
14770a586ceaSMark Shellenbaum 
14780a586ceaSMark Shellenbaum #ifdef _KERNEL
14790a586ceaSMark Shellenbaum int
sa_lookup_uio(sa_handle_t * hdl,sa_attr_type_t attr,uio_t * uio)14800a586ceaSMark Shellenbaum sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, uio_t *uio)
14810a586ceaSMark Shellenbaum {
14820a586ceaSMark Shellenbaum 	int error;
14830a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
14840a586ceaSMark Shellenbaum 
14850a586ceaSMark Shellenbaum 	bulk.sa_data = NULL;
14860a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
14870a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
14880a586ceaSMark Shellenbaum 
14890a586ceaSMark Shellenbaum 	ASSERT(hdl);
14900a586ceaSMark Shellenbaum 
14910a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
14921d8ccc7bSMark Shellenbaum 	if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
14930a586ceaSMark Shellenbaum 		error = uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
14940a586ceaSMark Shellenbaum 		    uio->uio_resid), UIO_READ, uio);
14950a586ceaSMark Shellenbaum 	}
14960a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
14970a586ceaSMark Shellenbaum 	return (error);
14980a586ceaSMark Shellenbaum 
14990a586ceaSMark Shellenbaum }
1500f67950b2SNasf-Fan 
1501f67950b2SNasf-Fan /*
1502f67950b2SNasf-Fan  * For the existing object that is upgraded from old system, its ondisk layout
1503f67950b2SNasf-Fan  * has no slot for the project ID attribute. But quota accounting logic needs
1504f67950b2SNasf-Fan  * to access related slots by offset directly. So we need to adjust these old
1505f67950b2SNasf-Fan  * objects' layout to make the project ID to some unified and fixed offset.
1506f67950b2SNasf-Fan  */
1507f67950b2SNasf-Fan int
sa_add_projid(sa_handle_t * hdl,dmu_tx_t * tx,uint64_t projid)1508f67950b2SNasf-Fan sa_add_projid(sa_handle_t *hdl, dmu_tx_t *tx, uint64_t projid)
1509f67950b2SNasf-Fan {
1510f67950b2SNasf-Fan 	znode_t *zp = sa_get_userdata(hdl);
1511f67950b2SNasf-Fan 	dmu_buf_t *db = sa_get_db(hdl);
1512f67950b2SNasf-Fan 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1513f67950b2SNasf-Fan 	int count = 0, err = 0;
1514f67950b2SNasf-Fan 	sa_bulk_attr_t *bulk, *attrs;
1515f67950b2SNasf-Fan 	zfs_acl_locator_cb_t locate = { 0 };
1516f67950b2SNasf-Fan 	uint64_t uid, gid, mode, rdev, xattr = 0, parent, gen, links;
1517f67950b2SNasf-Fan 	uint64_t crtime[2], mtime[2], ctime[2], atime[2];
1518f67950b2SNasf-Fan 	zfs_acl_phys_t znode_acl = { 0 };
1519f67950b2SNasf-Fan 	char scanstamp[AV_SCANSTAMP_SZ];
1520f67950b2SNasf-Fan 
1521f67950b2SNasf-Fan 	if (zp->z_acl_cached == NULL) {
1522f67950b2SNasf-Fan 		zfs_acl_t *aclp;
1523f67950b2SNasf-Fan 
152406799660SGordon Ross 		rw_enter(&zp->z_acl_lock, RW_WRITER);
1525f67950b2SNasf-Fan 		err = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
152606799660SGordon Ross 		rw_exit(&zp->z_acl_lock);
1527f67950b2SNasf-Fan 		if (err != 0 && err != ENOENT)
1528f67950b2SNasf-Fan 			return (err);
1529f67950b2SNasf-Fan 	}
1530f67950b2SNasf-Fan 
1531f67950b2SNasf-Fan 	bulk = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1532f67950b2SNasf-Fan 	attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1533f67950b2SNasf-Fan 	mutex_enter(&hdl->sa_lock);
1534f67950b2SNasf-Fan 	mutex_enter(&zp->z_lock);
1535f67950b2SNasf-Fan 
1536f67950b2SNasf-Fan 	err = sa_lookup_locked(hdl, SA_ZPL_PROJID(zfsvfs), &projid,
1537f67950b2SNasf-Fan 	    sizeof (uint64_t));
1538f67950b2SNasf-Fan 	if (unlikely(err == 0))
1539f67950b2SNasf-Fan 		/* Someone has added project ID attr by race. */
1540f67950b2SNasf-Fan 		err = EEXIST;
1541f67950b2SNasf-Fan 	if (err != ENOENT)
1542f67950b2SNasf-Fan 		goto out;
1543f67950b2SNasf-Fan 
1544f67950b2SNasf-Fan 	/* First do a bulk query of the attributes that aren't cached */
1545f67950b2SNasf-Fan 	if (zp->z_is_sa) {
1546f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1547f67950b2SNasf-Fan 		    &mode, 8);
1548f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1549f67950b2SNasf-Fan 		    &gen, 8);
1550f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1551f67950b2SNasf-Fan 		    &uid, 8);
1552f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1553f67950b2SNasf-Fan 		    &gid, 8);
1554f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1555f67950b2SNasf-Fan 		    &parent, 8);
1556f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1557f67950b2SNasf-Fan 		    &atime, 16);
1558f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1559f67950b2SNasf-Fan 		    &mtime, 16);
1560f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1561f67950b2SNasf-Fan 		    &ctime, 16);
1562f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1563f67950b2SNasf-Fan 		    &crtime, 16);
1564f67950b2SNasf-Fan 		if (S_ISBLK(zp->z_mode) || S_ISCHR(zp->z_mode))
1565f67950b2SNasf-Fan 			SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1566f67950b2SNasf-Fan 			    &rdev, 8);
1567f67950b2SNasf-Fan 	} else {
1568f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1569f67950b2SNasf-Fan 		    &atime, 16);
1570f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1571f67950b2SNasf-Fan 		    &mtime, 16);
1572f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1573f67950b2SNasf-Fan 		    &ctime, 16);
1574f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1575f67950b2SNasf-Fan 		    &crtime, 16);
1576f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1577f67950b2SNasf-Fan 		    &gen, 8);
1578f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1579f67950b2SNasf-Fan 		    &mode, 8);
1580f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1581f67950b2SNasf-Fan 		    &parent, 8);
1582f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL,
1583f67950b2SNasf-Fan 		    &xattr, 8);
1584f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1585f67950b2SNasf-Fan 		    &rdev, 8);
1586f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1587f67950b2SNasf-Fan 		    &uid, 8);
1588f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1589f67950b2SNasf-Fan 		    &gid, 8);
1590f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1591f67950b2SNasf-Fan 		    &znode_acl, 88);
1592f67950b2SNasf-Fan 	}
1593f67950b2SNasf-Fan 	err = sa_bulk_lookup_locked(hdl, bulk, count);
1594f67950b2SNasf-Fan 	if (err != 0)
1595f67950b2SNasf-Fan 		goto out;
1596f67950b2SNasf-Fan 
1597f67950b2SNasf-Fan 	err = sa_lookup_locked(hdl, SA_ZPL_XATTR(zfsvfs), &xattr, 8);
1598f67950b2SNasf-Fan 	if (err != 0 && err != ENOENT)
1599f67950b2SNasf-Fan 		goto out;
1600f67950b2SNasf-Fan 
1601f67950b2SNasf-Fan 	zp->z_projid = projid;
1602f67950b2SNasf-Fan 	zp->z_pflags |= ZFS_PROJID;
1603f67950b2SNasf-Fan 	links = zp->z_links;
1604f67950b2SNasf-Fan 	count = 0;
1605f67950b2SNasf-Fan 	err = 0;
1606f67950b2SNasf-Fan 
1607f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
1608f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
1609f67950b2SNasf-Fan 	    &zp->z_size, 8);
1610f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GEN(zfsvfs), NULL, &gen, 8);
1611f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
1612f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
1613f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
1614f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1615f67950b2SNasf-Fan 	    &zp->z_pflags, 8);
1616f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
1617f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1618f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1619f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1620f67950b2SNasf-Fan 	    &crtime, 16);
1621f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
1622f67950b2SNasf-Fan 	SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PROJID(zfsvfs), NULL, &projid, 8);
1623f67950b2SNasf-Fan 
1624f67950b2SNasf-Fan 	if (S_ISBLK(zp->z_mode) || S_ISCHR(zp->z_mode))
1625f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
1626f67950b2SNasf-Fan 		    &rdev, 8);
1627f67950b2SNasf-Fan 
1628f67950b2SNasf-Fan 	if (zp->z_acl_cached != NULL) {
1629f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1630f67950b2SNasf-Fan 		    &zp->z_acl_cached->z_acl_count, 8);
1631f67950b2SNasf-Fan 		if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
1632f67950b2SNasf-Fan 			zfs_acl_xform(zp, zp->z_acl_cached, CRED());
1633f67950b2SNasf-Fan 		locate.cb_aclp = zp->z_acl_cached;
1634f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
1635f67950b2SNasf-Fan 		    zfs_acl_data_locator, &locate,
1636f67950b2SNasf-Fan 		    zp->z_acl_cached->z_acl_bytes);
1637f67950b2SNasf-Fan 	}
1638f67950b2SNasf-Fan 
1639f67950b2SNasf-Fan 	if (xattr)
1640f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_XATTR(zfsvfs), NULL,
1641f67950b2SNasf-Fan 		    &xattr, 8);
1642f67950b2SNasf-Fan 
1643f67950b2SNasf-Fan 	if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
1644f67950b2SNasf-Fan 		bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
1645f67950b2SNasf-Fan 		    scanstamp, AV_SCANSTAMP_SZ);
1646f67950b2SNasf-Fan 		SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SCANSTAMP(zfsvfs), NULL,
1647f67950b2SNasf-Fan 		    scanstamp, AV_SCANSTAMP_SZ);
1648f67950b2SNasf-Fan 		zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
1649f67950b2SNasf-Fan 	}
1650f67950b2SNasf-Fan 
1651f67950b2SNasf-Fan 	VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
1652f67950b2SNasf-Fan 	VERIFY(sa_replace_all_by_template_locked(hdl, attrs, count, tx) == 0);
1653f67950b2SNasf-Fan 	if (znode_acl.z_acl_extern_obj) {
1654f67950b2SNasf-Fan 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
1655f67950b2SNasf-Fan 		    znode_acl.z_acl_extern_obj, tx));
1656f67950b2SNasf-Fan 	}
1657f67950b2SNasf-Fan 
1658f67950b2SNasf-Fan 	zp->z_is_sa = B_TRUE;
1659f67950b2SNasf-Fan 
1660f67950b2SNasf-Fan out:
1661f67950b2SNasf-Fan 	mutex_exit(&zp->z_lock);
1662f67950b2SNasf-Fan 	mutex_exit(&hdl->sa_lock);
1663f67950b2SNasf-Fan 	kmem_free(attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
1664f67950b2SNasf-Fan 	kmem_free(bulk, sizeof (sa_bulk_attr_t) * ZPL_END);
1665f67950b2SNasf-Fan 	return (err);
1666f67950b2SNasf-Fan }
16670a586ceaSMark Shellenbaum #endif
16680a586ceaSMark Shellenbaum 
16697f0bdb42SMatthew Ahrens static sa_idx_tab_t *
sa_find_idx_tab(objset_t * os,dmu_object_type_t bonustype,sa_hdr_phys_t * hdr)16707f0bdb42SMatthew Ahrens sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, sa_hdr_phys_t *hdr)
16710a586ceaSMark Shellenbaum {
16720a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
16730a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
16740a586ceaSMark Shellenbaum 	sa_lot_t *tb, search;
16750a586ceaSMark Shellenbaum 	avl_index_t loc;
16760a586ceaSMark Shellenbaum 
16770a586ceaSMark Shellenbaum 	/*
16780a586ceaSMark Shellenbaum 	 * Deterimine layout number.  If SA node and header == 0 then
16790a586ceaSMark Shellenbaum 	 * force the index table to the dummy "1" empty layout.
16800a586ceaSMark Shellenbaum 	 *
16810a586ceaSMark Shellenbaum 	 * The layout number would only be zero for a newly created file
16820a586ceaSMark Shellenbaum 	 * that has not added any attributes yet, or with crypto enabled which
16830a586ceaSMark Shellenbaum 	 * doesn't write any attributes to the bonus buffer.
16840a586ceaSMark Shellenbaum 	 */
16850a586ceaSMark Shellenbaum 
16860a586ceaSMark Shellenbaum 	search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
16870a586ceaSMark Shellenbaum 
16880a586ceaSMark Shellenbaum 	tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
16890a586ceaSMark Shellenbaum 
16900a586ceaSMark Shellenbaum 	/* Verify header size is consistent with layout information */
16910a586ceaSMark Shellenbaum 	ASSERT(tb);
16920a586ceaSMark Shellenbaum 	ASSERT(IS_SA_BONUSTYPE(bonustype) &&
16930a586ceaSMark Shellenbaum 	    SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) || !IS_SA_BONUSTYPE(bonustype) ||
16940a586ceaSMark Shellenbaum 	    (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
16950a586ceaSMark Shellenbaum 
16960a586ceaSMark Shellenbaum 	/*
16970a586ceaSMark Shellenbaum 	 * See if any of the already existing TOC entries can be reused?
16980a586ceaSMark Shellenbaum 	 */
16990a586ceaSMark Shellenbaum 
17000a586ceaSMark Shellenbaum 	for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
17010a586ceaSMark Shellenbaum 	    idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
17020a586ceaSMark Shellenbaum 		boolean_t valid_idx = B_TRUE;
17030a586ceaSMark Shellenbaum 		int i;
17040a586ceaSMark Shellenbaum 
17050a586ceaSMark Shellenbaum 		if (tb->lot_var_sizes != 0 &&
17060a586ceaSMark Shellenbaum 		    idx_tab->sa_variable_lengths != NULL) {
17070a586ceaSMark Shellenbaum 			for (i = 0; i != tb->lot_var_sizes; i++) {
17080a586ceaSMark Shellenbaum 				if (hdr->sa_lengths[i] !=
17090a586ceaSMark Shellenbaum 				    idx_tab->sa_variable_lengths[i]) {
17100a586ceaSMark Shellenbaum 					valid_idx = B_FALSE;
17110a586ceaSMark Shellenbaum 					break;
17120a586ceaSMark Shellenbaum 				}
17130a586ceaSMark Shellenbaum 			}
17140a586ceaSMark Shellenbaum 		}
17150a586ceaSMark Shellenbaum 		if (valid_idx) {
17160a586ceaSMark Shellenbaum 			sa_idx_tab_hold(os, idx_tab);
17170a586ceaSMark Shellenbaum 			return (idx_tab);
17180a586ceaSMark Shellenbaum 		}
17190a586ceaSMark Shellenbaum 	}
17200a586ceaSMark Shellenbaum 
17210a586ceaSMark Shellenbaum 	/* No such luck, create a new entry */
17220a586ceaSMark Shellenbaum 	idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
17230a586ceaSMark Shellenbaum 	idx_tab->sa_idx_tab =
17240a586ceaSMark Shellenbaum 	    kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
17250a586ceaSMark Shellenbaum 	idx_tab->sa_layout = tb;
1726e914ace2STim Schumacher 	zfs_refcount_create(&idx_tab->sa_refcount);
17270a586ceaSMark Shellenbaum 	if (tb->lot_var_sizes)
17280a586ceaSMark Shellenbaum 		idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
17290a586ceaSMark Shellenbaum 		    tb->lot_var_sizes, KM_SLEEP);
17300a586ceaSMark Shellenbaum 
17310a586ceaSMark Shellenbaum 	sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
17320a586ceaSMark Shellenbaum 	    tb, idx_tab);
17330a586ceaSMark Shellenbaum 	sa_idx_tab_hold(os, idx_tab);   /* one hold for consumer */
17340a586ceaSMark Shellenbaum 	sa_idx_tab_hold(os, idx_tab);	/* one for layout */
17350a586ceaSMark Shellenbaum 	list_insert_tail(&tb->lot_idx_tab, idx_tab);
17360a586ceaSMark Shellenbaum 	return (idx_tab);
17370a586ceaSMark Shellenbaum }
17380a586ceaSMark Shellenbaum 
17390a586ceaSMark Shellenbaum void
sa_default_locator(void ** dataptr,uint32_t * len,uint32_t total_len,boolean_t start,void * userdata)17400a586ceaSMark Shellenbaum sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
17410a586ceaSMark Shellenbaum     boolean_t start, void *userdata)
17420a586ceaSMark Shellenbaum {
17430a586ceaSMark Shellenbaum 	ASSERT(start);
17440a586ceaSMark Shellenbaum 
17450a586ceaSMark Shellenbaum 	*dataptr = userdata;
17460a586ceaSMark Shellenbaum 	*len = total_len;
17470a586ceaSMark Shellenbaum }
17480a586ceaSMark Shellenbaum 
17490a586ceaSMark Shellenbaum static void
sa_attr_register_sync(sa_handle_t * hdl,dmu_tx_t * tx)17500a586ceaSMark Shellenbaum sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
17510a586ceaSMark Shellenbaum {
17520a586ceaSMark Shellenbaum 	uint64_t attr_value = 0;
17530a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
17540a586ceaSMark Shellenbaum 	sa_attr_table_t *tb = sa->sa_attr_table;
17550a586ceaSMark Shellenbaum 	int i;
17560a586ceaSMark Shellenbaum 
17570a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
17580a586ceaSMark Shellenbaum 
1759dd328bf6SToomas Soome 	if (!sa->sa_need_attr_registration || sa->sa_master_obj == 0) {
17600a586ceaSMark Shellenbaum 		mutex_exit(&sa->sa_lock);
17610a586ceaSMark Shellenbaum 		return;
17620a586ceaSMark Shellenbaum 	}
17630a586ceaSMark Shellenbaum 
1764dd328bf6SToomas Soome 	if (sa->sa_reg_attr_obj == 0) {
1765ad135b5dSChristopher Siden 		sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1766ad135b5dSChristopher Siden 		    DMU_OT_SA_ATTR_REGISTRATION,
1767ad135b5dSChristopher Siden 		    sa->sa_master_obj, SA_REGISTRY, tx);
17680a586ceaSMark Shellenbaum 	}
17690a586ceaSMark Shellenbaum 	for (i = 0; i != sa->sa_num_attrs; i++) {
17700a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[i].sa_registered)
17710a586ceaSMark Shellenbaum 			continue;
17720a586ceaSMark Shellenbaum 		ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
17730a586ceaSMark Shellenbaum 		    tb[i].sa_byteswap);
17740a586ceaSMark Shellenbaum 		VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
17750a586ceaSMark Shellenbaum 		    tb[i].sa_name, 8, 1, &attr_value, tx));
17760a586ceaSMark Shellenbaum 		tb[i].sa_registered = B_TRUE;
17770a586ceaSMark Shellenbaum 	}
17780a586ceaSMark Shellenbaum 	sa->sa_need_attr_registration = B_FALSE;
17790a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
17800a586ceaSMark Shellenbaum }
17810a586ceaSMark Shellenbaum 
17820a586ceaSMark Shellenbaum /*
17830a586ceaSMark Shellenbaum  * Replace all attributes with attributes specified in template.
17840a586ceaSMark Shellenbaum  * If dnode had a spill buffer then those attributes will be
17850a586ceaSMark Shellenbaum  * also be replaced, possibly with just an empty spill block
17860a586ceaSMark Shellenbaum  *
17870a586ceaSMark Shellenbaum  * This interface is intended to only be used for bulk adding of
17880a586ceaSMark Shellenbaum  * attributes for a new file.  It will also be used by the ZPL
17890a586ceaSMark Shellenbaum  * when converting and old formatted znode to native SA support.
17900a586ceaSMark Shellenbaum  */
17910a586ceaSMark Shellenbaum int
sa_replace_all_by_template_locked(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)17920a586ceaSMark Shellenbaum sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
17930a586ceaSMark Shellenbaum     int attr_count, dmu_tx_t *tx)
17940a586ceaSMark Shellenbaum {
17950a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
17960a586ceaSMark Shellenbaum 
17970a586ceaSMark Shellenbaum 	if (sa->sa_need_attr_registration)
17980a586ceaSMark Shellenbaum 		sa_attr_register_sync(hdl, tx);
17990a586ceaSMark Shellenbaum 	return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
18000a586ceaSMark Shellenbaum }
18010a586ceaSMark Shellenbaum 
18020a586ceaSMark Shellenbaum int
sa_replace_all_by_template(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)18030a586ceaSMark Shellenbaum sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
18040a586ceaSMark Shellenbaum     int attr_count, dmu_tx_t *tx)
18050a586ceaSMark Shellenbaum {
18060a586ceaSMark Shellenbaum 	int error;
18070a586ceaSMark Shellenbaum 
18080a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
18090a586ceaSMark Shellenbaum 	error = sa_replace_all_by_template_locked(hdl, attr_desc,
18100a586ceaSMark Shellenbaum 	    attr_count, tx);
18110a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
18120a586ceaSMark Shellenbaum 	return (error);
18130a586ceaSMark Shellenbaum }
18140a586ceaSMark Shellenbaum 
18150a586ceaSMark Shellenbaum /*
1816e7e978b1SAndriy Gapon  * Add/remove a single attribute or replace a variable-sized attribute value
1817e7e978b1SAndriy Gapon  * with a value of a different size, and then rewrite the entire set
18180a586ceaSMark Shellenbaum  * of attributes.
1819e7e978b1SAndriy Gapon  * Same-length attribute value replacement (including fixed-length attributes)
1820e7e978b1SAndriy Gapon  * is handled more efficiently by the upper layers.
18210a586ceaSMark Shellenbaum  */
18220a586ceaSMark Shellenbaum static int
sa_modify_attrs(sa_handle_t * hdl,sa_attr_type_t newattr,sa_data_op_t action,sa_data_locator_t * locator,void * datastart,uint16_t buflen,dmu_tx_t * tx)18230a586ceaSMark Shellenbaum sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
18240a586ceaSMark Shellenbaum     sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
18250a586ceaSMark Shellenbaum     uint16_t buflen, dmu_tx_t *tx)
18260a586ceaSMark Shellenbaum {
18270a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
1828744947dcSTom Erickson 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1829744947dcSTom Erickson 	dnode_t *dn;
18300a586ceaSMark Shellenbaum 	sa_bulk_attr_t *attr_desc;
18310a586ceaSMark Shellenbaum 	void *old_data[2];
18320a586ceaSMark Shellenbaum 	int bonus_attr_count = 0;
1833d5285caeSGeorge Wilson 	int bonus_data_size = 0;
1834d5285caeSGeorge Wilson 	int spill_data_size = 0;
18350a586ceaSMark Shellenbaum 	int spill_attr_count = 0;
18360a586ceaSMark Shellenbaum 	int error;
1837e7e978b1SAndriy Gapon 	uint16_t length, reg_length;
18380a586ceaSMark Shellenbaum 	int i, j, k, length_idx;
18390a586ceaSMark Shellenbaum 	sa_hdr_phys_t *hdr;
18400a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
18410a586ceaSMark Shellenbaum 	int attr_count;
18420a586ceaSMark Shellenbaum 	int count;
18430a586ceaSMark Shellenbaum 
18440a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
18450a586ceaSMark Shellenbaum 
18460a586ceaSMark Shellenbaum 	/* First make of copy of the old data */
18470a586ceaSMark Shellenbaum 
1848744947dcSTom Erickson 	DB_DNODE_ENTER(db);
1849744947dcSTom Erickson 	dn = DB_DNODE(db);
1850744947dcSTom Erickson 	if (dn->dn_bonuslen != 0) {
18510a586ceaSMark Shellenbaum 		bonus_data_size = hdl->sa_bonus->db_size;
18520a586ceaSMark Shellenbaum 		old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
18530a586ceaSMark Shellenbaum 		bcopy(hdl->sa_bonus->db_data, old_data[0],
18540a586ceaSMark Shellenbaum 		    hdl->sa_bonus->db_size);
18550a586ceaSMark Shellenbaum 		bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
18560a586ceaSMark Shellenbaum 	} else {
18570a586ceaSMark Shellenbaum 		old_data[0] = NULL;
18580a586ceaSMark Shellenbaum 	}
1859744947dcSTom Erickson 	DB_DNODE_EXIT(db);
18600a586ceaSMark Shellenbaum 
18610a586ceaSMark Shellenbaum 	/* Bring spill buffer online if it isn't currently */
18620a586ceaSMark Shellenbaum 
18631d8ccc7bSMark Shellenbaum 	if ((error = sa_get_spill(hdl)) == 0) {
18640a586ceaSMark Shellenbaum 		spill_data_size = hdl->sa_spill->db_size;
18650a586ceaSMark Shellenbaum 		old_data[1] = kmem_alloc(spill_data_size, KM_SLEEP);
18660a586ceaSMark Shellenbaum 		bcopy(hdl->sa_spill->db_data, old_data[1],
18670a586ceaSMark Shellenbaum 		    hdl->sa_spill->db_size);
18680a586ceaSMark Shellenbaum 		spill_attr_count =
18690a586ceaSMark Shellenbaum 		    hdl->sa_spill_tab->sa_layout->lot_attr_count;
18701d8ccc7bSMark Shellenbaum 	} else if (error && error != ENOENT) {
18711d8ccc7bSMark Shellenbaum 		if (old_data[0])
18721d8ccc7bSMark Shellenbaum 			kmem_free(old_data[0], bonus_data_size);
18731d8ccc7bSMark Shellenbaum 		return (error);
18740a586ceaSMark Shellenbaum 	} else {
18750a586ceaSMark Shellenbaum 		old_data[1] = NULL;
18760a586ceaSMark Shellenbaum 	}
18770a586ceaSMark Shellenbaum 
18780a586ceaSMark Shellenbaum 	/* build descriptor of all attributes */
18790a586ceaSMark Shellenbaum 
18800a586ceaSMark Shellenbaum 	attr_count = bonus_attr_count + spill_attr_count;
18810a586ceaSMark Shellenbaum 	if (action == SA_ADD)
18820a586ceaSMark Shellenbaum 		attr_count++;
18830a586ceaSMark Shellenbaum 	else if (action == SA_REMOVE)
18840a586ceaSMark Shellenbaum 		attr_count--;
18850a586ceaSMark Shellenbaum 
18860a586ceaSMark Shellenbaum 	attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
18870a586ceaSMark Shellenbaum 
18880a586ceaSMark Shellenbaum 	/*
18890a586ceaSMark Shellenbaum 	 * loop through bonus and spill buffer if it exists, and
18900a586ceaSMark Shellenbaum 	 * build up new attr_descriptor to reset the attributes
18910a586ceaSMark Shellenbaum 	 */
18920a586ceaSMark Shellenbaum 	k = j = 0;
18930a586ceaSMark Shellenbaum 	count = bonus_attr_count;
18940a586ceaSMark Shellenbaum 	hdr = SA_GET_HDR(hdl, SA_BONUS);
18950a586ceaSMark Shellenbaum 	idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
18960a586ceaSMark Shellenbaum 	for (; k != 2; k++) {
1897e7e978b1SAndriy Gapon 		/*
1898e7e978b1SAndriy Gapon 		 * Iterate over each attribute in layout.  Fetch the
1899e7e978b1SAndriy Gapon 		 * size of variable-length attributes needing rewrite
1900e7e978b1SAndriy Gapon 		 * from sa_lengths[].
1901e7e978b1SAndriy Gapon 		 */
19020a586ceaSMark Shellenbaum 		for (i = 0, length_idx = 0; i != count; i++) {
19030a586ceaSMark Shellenbaum 			sa_attr_type_t attr;
19040a586ceaSMark Shellenbaum 
19050a586ceaSMark Shellenbaum 			attr = idx_tab->sa_layout->lot_attrs[i];
1906e7e978b1SAndriy Gapon 			reg_length = SA_REGISTERED_LEN(sa, attr);
1907e7e978b1SAndriy Gapon 			if (reg_length == 0) {
1908e7e978b1SAndriy Gapon 				length = hdr->sa_lengths[length_idx];
1909e7e978b1SAndriy Gapon 				length_idx++;
1910e7e978b1SAndriy Gapon 			} else {
1911e7e978b1SAndriy Gapon 				length = reg_length;
1912e7e978b1SAndriy Gapon 			}
19130a586ceaSMark Shellenbaum 			if (attr == newattr) {
1914e7e978b1SAndriy Gapon 				/*
1915e7e978b1SAndriy Gapon 				 * There is nothing to do for SA_REMOVE,
1916e7e978b1SAndriy Gapon 				 * so it is just skipped.
1917e7e978b1SAndriy Gapon 				 */
1918e7e978b1SAndriy Gapon 				if (action == SA_REMOVE)
19190a586ceaSMark Shellenbaum 					continue;
1920e7e978b1SAndriy Gapon 
1921e7e978b1SAndriy Gapon 				/*
1922e7e978b1SAndriy Gapon 				 * Duplicate attributes are not allowed, so the
1923e7e978b1SAndriy Gapon 				 * action can not be SA_ADD here.
1924e7e978b1SAndriy Gapon 				 */
1925e7e978b1SAndriy Gapon 				ASSERT3S(action, ==, SA_REPLACE);
1926e7e978b1SAndriy Gapon 
1927e7e978b1SAndriy Gapon 				/*
1928e7e978b1SAndriy Gapon 				 * Only a variable-sized attribute can be
1929e7e978b1SAndriy Gapon 				 * replaced here, and its size must be changing.
1930e7e978b1SAndriy Gapon 				 */
1931e7e978b1SAndriy Gapon 				ASSERT3U(reg_length, ==, 0);
1932e7e978b1SAndriy Gapon 				ASSERT3U(length, !=, buflen);
19330a586ceaSMark Shellenbaum 				SA_ADD_BULK_ATTR(attr_desc, j, attr,
19340a586ceaSMark Shellenbaum 				    locator, datastart, buflen);
19350a586ceaSMark Shellenbaum 			} else {
19360a586ceaSMark Shellenbaum 				SA_ADD_BULK_ATTR(attr_desc, j, attr,
19370a586ceaSMark Shellenbaum 				    NULL, (void *)
19380a586ceaSMark Shellenbaum 				    (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
19390a586ceaSMark Shellenbaum 				    (uintptr_t)old_data[k]), length);
19400a586ceaSMark Shellenbaum 			}
19410a586ceaSMark Shellenbaum 		}
19420a586ceaSMark Shellenbaum 		if (k == 0 && hdl->sa_spill) {
19430a586ceaSMark Shellenbaum 			hdr = SA_GET_HDR(hdl, SA_SPILL);
19440a586ceaSMark Shellenbaum 			idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
19450a586ceaSMark Shellenbaum 			count = spill_attr_count;
19460a586ceaSMark Shellenbaum 		} else {
19470a586ceaSMark Shellenbaum 			break;
19480a586ceaSMark Shellenbaum 		}
19490a586ceaSMark Shellenbaum 	}
19500a586ceaSMark Shellenbaum 	if (action == SA_ADD) {
1951e7e978b1SAndriy Gapon 		reg_length = SA_REGISTERED_LEN(sa, newattr);
1952e7e978b1SAndriy Gapon 		IMPLY(reg_length != 0, reg_length == buflen);
19530a586ceaSMark Shellenbaum 		SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
19540a586ceaSMark Shellenbaum 		    datastart, buflen);
19550a586ceaSMark Shellenbaum 	}
1956e7e978b1SAndriy Gapon 	ASSERT3U(j, ==, attr_count);
19570a586ceaSMark Shellenbaum 
19580a586ceaSMark Shellenbaum 	error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
19590a586ceaSMark Shellenbaum 
19600a586ceaSMark Shellenbaum 	if (old_data[0])
19610a586ceaSMark Shellenbaum 		kmem_free(old_data[0], bonus_data_size);
19620a586ceaSMark Shellenbaum 	if (old_data[1])
19630a586ceaSMark Shellenbaum 		kmem_free(old_data[1], spill_data_size);
19640a586ceaSMark Shellenbaum 	kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
19650a586ceaSMark Shellenbaum 
19660a586ceaSMark Shellenbaum 	return (error);
19670a586ceaSMark Shellenbaum }
19680a586ceaSMark Shellenbaum 
19690a586ceaSMark Shellenbaum static int
sa_bulk_update_impl(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count,dmu_tx_t * tx)19700a586ceaSMark Shellenbaum sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
19710a586ceaSMark Shellenbaum     dmu_tx_t *tx)
19720a586ceaSMark Shellenbaum {
19730a586ceaSMark Shellenbaum 	int error;
19740a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
19750a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype;
19760a586ceaSMark Shellenbaum 
19770a586ceaSMark Shellenbaum 	bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
19780a586ceaSMark Shellenbaum 
19790a586ceaSMark Shellenbaum 	ASSERT(hdl);
19800a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
19810a586ceaSMark Shellenbaum 
19820a586ceaSMark Shellenbaum 	/* sync out registration table if necessary */
19830a586ceaSMark Shellenbaum 	if (sa->sa_need_attr_registration)
19840a586ceaSMark Shellenbaum 		sa_attr_register_sync(hdl, tx);
19850a586ceaSMark Shellenbaum 
19860a586ceaSMark Shellenbaum 	error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
19870a586ceaSMark Shellenbaum 	if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
19880a586ceaSMark Shellenbaum 		sa->sa_update_cb(hdl, tx);
19890a586ceaSMark Shellenbaum 
19900a586ceaSMark Shellenbaum 	return (error);
19910a586ceaSMark Shellenbaum }
19920a586ceaSMark Shellenbaum 
19930a586ceaSMark Shellenbaum /*
19940a586ceaSMark Shellenbaum  * update or add new attribute
19950a586ceaSMark Shellenbaum  */
19960a586ceaSMark Shellenbaum int
sa_update(sa_handle_t * hdl,sa_attr_type_t type,void * buf,uint32_t buflen,dmu_tx_t * tx)19970a586ceaSMark Shellenbaum sa_update(sa_handle_t *hdl, sa_attr_type_t type,
19980a586ceaSMark Shellenbaum     void *buf, uint32_t buflen, dmu_tx_t *tx)
19990a586ceaSMark Shellenbaum {
20000a586ceaSMark Shellenbaum 	int error;
20010a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
20020a586ceaSMark Shellenbaum 
20030a586ceaSMark Shellenbaum 	bulk.sa_attr = type;
20040a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
20050a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
20060a586ceaSMark Shellenbaum 	bulk.sa_data = buf;
20070a586ceaSMark Shellenbaum 
20080a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20090a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
20100a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20110a586ceaSMark Shellenbaum 	return (error);
20120a586ceaSMark Shellenbaum }
20130a586ceaSMark Shellenbaum 
20140a586ceaSMark Shellenbaum int
sa_update_from_cb(sa_handle_t * hdl,sa_attr_type_t attr,uint32_t buflen,sa_data_locator_t * locator,void * userdata,dmu_tx_t * tx)20150a586ceaSMark Shellenbaum sa_update_from_cb(sa_handle_t *hdl, sa_attr_type_t attr,
20160a586ceaSMark Shellenbaum     uint32_t buflen, sa_data_locator_t *locator, void *userdata, dmu_tx_t *tx)
20170a586ceaSMark Shellenbaum {
20180a586ceaSMark Shellenbaum 	int error;
20190a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
20200a586ceaSMark Shellenbaum 
20210a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
20220a586ceaSMark Shellenbaum 	bulk.sa_data = userdata;
20230a586ceaSMark Shellenbaum 	bulk.sa_data_func = locator;
20240a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
20250a586ceaSMark Shellenbaum 
20260a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20270a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
20280a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20290a586ceaSMark Shellenbaum 	return (error);
20300a586ceaSMark Shellenbaum }
20310a586ceaSMark Shellenbaum 
20320a586ceaSMark Shellenbaum /*
20330a586ceaSMark Shellenbaum  * Return size of an attribute
20340a586ceaSMark Shellenbaum  */
20350a586ceaSMark Shellenbaum 
20360a586ceaSMark Shellenbaum int
sa_size(sa_handle_t * hdl,sa_attr_type_t attr,int * size)20370a586ceaSMark Shellenbaum sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
20380a586ceaSMark Shellenbaum {
20390a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
20401d8ccc7bSMark Shellenbaum 	int error;
20410a586ceaSMark Shellenbaum 
20420a586ceaSMark Shellenbaum 	bulk.sa_data = NULL;
20430a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
20440a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
20450a586ceaSMark Shellenbaum 
20460a586ceaSMark Shellenbaum 	ASSERT(hdl);
20470a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20481d8ccc7bSMark Shellenbaum 	if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
20490a586ceaSMark Shellenbaum 		mutex_exit(&hdl->sa_lock);
20501d8ccc7bSMark Shellenbaum 		return (error);
20510a586ceaSMark Shellenbaum 	}
20520a586ceaSMark Shellenbaum 	*size = bulk.sa_size;
20530a586ceaSMark Shellenbaum 
20540a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20550a586ceaSMark Shellenbaum 	return (0);
20560a586ceaSMark Shellenbaum }
20570a586ceaSMark Shellenbaum 
20580a586ceaSMark Shellenbaum int
sa_bulk_lookup_locked(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count)20590a586ceaSMark Shellenbaum sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
20600a586ceaSMark Shellenbaum {
20610a586ceaSMark Shellenbaum 	ASSERT(hdl);
20620a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
20630a586ceaSMark Shellenbaum 	return (sa_lookup_impl(hdl, attrs, count));
20640a586ceaSMark Shellenbaum }
20650a586ceaSMark Shellenbaum 
20660a586ceaSMark Shellenbaum int
sa_bulk_lookup(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count)20670a586ceaSMark Shellenbaum sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
20680a586ceaSMark Shellenbaum {
20690a586ceaSMark Shellenbaum 	int error;
20700a586ceaSMark Shellenbaum 
20710a586ceaSMark Shellenbaum 	ASSERT(hdl);
20720a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20730a586ceaSMark Shellenbaum 	error = sa_bulk_lookup_locked(hdl, attrs, count);
20740a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20750a586ceaSMark Shellenbaum 	return (error);
20760a586ceaSMark Shellenbaum }
20770a586ceaSMark Shellenbaum 
20780a586ceaSMark Shellenbaum int
sa_bulk_update(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count,dmu_tx_t * tx)20790a586ceaSMark Shellenbaum sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
20800a586ceaSMark Shellenbaum {
20810a586ceaSMark Shellenbaum 	int error;
20820a586ceaSMark Shellenbaum 
20830a586ceaSMark Shellenbaum 	ASSERT(hdl);
20840a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20850a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, attrs, count, tx);
20860a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20870a586ceaSMark Shellenbaum 	return (error);
20880a586ceaSMark Shellenbaum }
20890a586ceaSMark Shellenbaum 
20900a586ceaSMark Shellenbaum int
sa_remove(sa_handle_t * hdl,sa_attr_type_t attr,dmu_tx_t * tx)20910a586ceaSMark Shellenbaum sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
20920a586ceaSMark Shellenbaum {
20930a586ceaSMark Shellenbaum 	int error;
20940a586ceaSMark Shellenbaum 
20950a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
20960a586ceaSMark Shellenbaum 	error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
20970a586ceaSMark Shellenbaum 	    NULL, 0, tx);
20980a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
20990a586ceaSMark Shellenbaum 	return (error);
21000a586ceaSMark Shellenbaum }
21010a586ceaSMark Shellenbaum 
21020a586ceaSMark Shellenbaum void
sa_object_info(sa_handle_t * hdl,dmu_object_info_t * doi)21030a586ceaSMark Shellenbaum sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
21040a586ceaSMark Shellenbaum {
21050a586ceaSMark Shellenbaum 	dmu_object_info_from_db((dmu_buf_t *)hdl->sa_bonus, doi);
21060a586ceaSMark Shellenbaum }
21070a586ceaSMark Shellenbaum 
21080a586ceaSMark Shellenbaum void
sa_object_size(sa_handle_t * hdl,uint32_t * blksize,u_longlong_t * nblocks)21090a586ceaSMark Shellenbaum sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
21100a586ceaSMark Shellenbaum {
21110a586ceaSMark Shellenbaum 	dmu_object_size_from_db((dmu_buf_t *)hdl->sa_bonus,
21120a586ceaSMark Shellenbaum 	    blksize, nblocks);
21130a586ceaSMark Shellenbaum }
21140a586ceaSMark Shellenbaum 
21150a586ceaSMark Shellenbaum void
sa_set_userp(sa_handle_t * hdl,void * ptr)21160a586ceaSMark Shellenbaum sa_set_userp(sa_handle_t *hdl, void *ptr)
21170a586ceaSMark Shellenbaum {
21180a586ceaSMark Shellenbaum 	hdl->sa_userp = ptr;
21190a586ceaSMark Shellenbaum }
21200a586ceaSMark Shellenbaum 
21210a586ceaSMark Shellenbaum dmu_buf_t *
sa_get_db(sa_handle_t * hdl)21220a586ceaSMark Shellenbaum sa_get_db(sa_handle_t *hdl)
21230a586ceaSMark Shellenbaum {
21240a586ceaSMark Shellenbaum 	return ((dmu_buf_t *)hdl->sa_bonus);
21250a586ceaSMark Shellenbaum }
21260a586ceaSMark Shellenbaum 
21270a586ceaSMark Shellenbaum void *
sa_get_userdata(sa_handle_t * hdl)21280a586ceaSMark Shellenbaum sa_get_userdata(sa_handle_t *hdl)
21290a586ceaSMark Shellenbaum {
21300a586ceaSMark Shellenbaum 	return (hdl->sa_userp);
21310a586ceaSMark Shellenbaum }
21320a586ceaSMark Shellenbaum 
21330a586ceaSMark Shellenbaum void
sa_register_update_callback_locked(objset_t * os,sa_update_cb_t * func)21340a586ceaSMark Shellenbaum sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
21350a586ceaSMark Shellenbaum {
21360a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
21370a586ceaSMark Shellenbaum 	os->os_sa->sa_update_cb = func;
21380a586ceaSMark Shellenbaum }
21390a586ceaSMark Shellenbaum 
21400a586ceaSMark Shellenbaum void
sa_register_update_callback(objset_t * os,sa_update_cb_t * func)21410a586ceaSMark Shellenbaum sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
21420a586ceaSMark Shellenbaum {
21430a586ceaSMark Shellenbaum 
21440a586ceaSMark Shellenbaum 	mutex_enter(&os->os_sa->sa_lock);
21450a586ceaSMark Shellenbaum 	sa_register_update_callback_locked(os, func);
21460a586ceaSMark Shellenbaum 	mutex_exit(&os->os_sa->sa_lock);
21470a586ceaSMark Shellenbaum }
21480a586ceaSMark Shellenbaum 
21490a586ceaSMark Shellenbaum uint64_t
sa_handle_object(sa_handle_t * hdl)21500a586ceaSMark Shellenbaum sa_handle_object(sa_handle_t *hdl)
21510a586ceaSMark Shellenbaum {
21520a586ceaSMark Shellenbaum 	return (hdl->sa_bonus->db_object);
21530a586ceaSMark Shellenbaum }
21540a586ceaSMark Shellenbaum 
21550a586ceaSMark Shellenbaum boolean_t
sa_enabled(objset_t * os)21560a586ceaSMark Shellenbaum sa_enabled(objset_t *os)
21570a586ceaSMark Shellenbaum {
21580a586ceaSMark Shellenbaum 	return (os->os_sa == NULL);
21590a586ceaSMark Shellenbaum }
21600a586ceaSMark Shellenbaum 
21610a586ceaSMark Shellenbaum int
sa_set_sa_object(objset_t * os,uint64_t sa_object)21620a586ceaSMark Shellenbaum sa_set_sa_object(objset_t *os, uint64_t sa_object)
21630a586ceaSMark Shellenbaum {
21640a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
21650a586ceaSMark Shellenbaum 
21660a586ceaSMark Shellenbaum 	if (sa->sa_master_obj)
21670a586ceaSMark Shellenbaum 		return (1);
21680a586ceaSMark Shellenbaum 
21690a586ceaSMark Shellenbaum 	sa->sa_master_obj = sa_object;
21700a586ceaSMark Shellenbaum 
21710a586ceaSMark Shellenbaum 	return (0);
21720a586ceaSMark Shellenbaum }
21730a586ceaSMark Shellenbaum 
21740a586ceaSMark Shellenbaum int
sa_hdrsize(void * arg)21750a586ceaSMark Shellenbaum sa_hdrsize(void *arg)
21760a586ceaSMark Shellenbaum {
21770a586ceaSMark Shellenbaum 	sa_hdr_phys_t *hdr = arg;
21780a586ceaSMark Shellenbaum 
21790a586ceaSMark Shellenbaum 	return (SA_HDR_SIZE(hdr));
21800a586ceaSMark Shellenbaum }
21810a586ceaSMark Shellenbaum 
21820a586ceaSMark Shellenbaum void
sa_handle_lock(sa_handle_t * hdl)21830a586ceaSMark Shellenbaum sa_handle_lock(sa_handle_t *hdl)
21840a586ceaSMark Shellenbaum {
21850a586ceaSMark Shellenbaum 	ASSERT(hdl);
21860a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
21870a586ceaSMark Shellenbaum }
21880a586ceaSMark Shellenbaum 
21890a586ceaSMark Shellenbaum void
sa_handle_unlock(sa_handle_t * hdl)21900a586ceaSMark Shellenbaum sa_handle_unlock(sa_handle_t *hdl)
21910a586ceaSMark Shellenbaum {
21920a586ceaSMark Shellenbaum 	ASSERT(hdl);
21930a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
21940a586ceaSMark Shellenbaum }
2195