xref: /illumos-gate/usr/src/uts/common/fs/zfs/sa.c (revision 06e0070d70ba2ee95f5aa2645423eb2cf1546788)
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  */
210a586ceaSMark Shellenbaum /*
22*06e0070dSMark Shellenbaum  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
230a586ceaSMark Shellenbaum  */
240a586ceaSMark Shellenbaum 
250a586ceaSMark Shellenbaum #include <sys/zfs_context.h>
260a586ceaSMark Shellenbaum #include <sys/types.h>
270a586ceaSMark Shellenbaum #include <sys/param.h>
280a586ceaSMark Shellenbaum #include <sys/systm.h>
290a586ceaSMark Shellenbaum #include <sys/sysmacros.h>
300a586ceaSMark Shellenbaum #include <sys/dmu.h>
310a586ceaSMark Shellenbaum #include <sys/dmu_impl.h>
320a586ceaSMark Shellenbaum #include <sys/dmu_objset.h>
330a586ceaSMark Shellenbaum #include <sys/dbuf.h>
340a586ceaSMark Shellenbaum #include <sys/dnode.h>
350a586ceaSMark Shellenbaum #include <sys/zap.h>
360a586ceaSMark Shellenbaum #include <sys/sa.h>
370a586ceaSMark Shellenbaum #include <sys/sunddi.h>
380a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
390a586ceaSMark Shellenbaum #include <sys/dnode.h>
400a586ceaSMark Shellenbaum #include <sys/errno.h>
410a586ceaSMark Shellenbaum #include <sys/zfs_context.h>
420a586ceaSMark Shellenbaum 
430a586ceaSMark Shellenbaum /*
440a586ceaSMark Shellenbaum  * ZFS System attributes:
450a586ceaSMark Shellenbaum  *
460a586ceaSMark Shellenbaum  * A generic mechanism to allow for arbitrary attributes
470a586ceaSMark Shellenbaum  * to be stored in a dnode.  The data will be stored in the bonus buffer of
480a586ceaSMark Shellenbaum  * the dnode and if necessary a special "spill" block will be used to handle
490a586ceaSMark Shellenbaum  * overflow situations.  The spill block will be sized to fit the data
500a586ceaSMark Shellenbaum  * from 512 - 128K.  When a spill block is used the BP (blkptr_t) for the
510a586ceaSMark Shellenbaum  * spill block is stored at the end of the current bonus buffer.  Any
520a586ceaSMark Shellenbaum  * attributes that would be in the way of the blkptr_t will be relocated
530a586ceaSMark Shellenbaum  * into the spill block.
540a586ceaSMark Shellenbaum  *
550a586ceaSMark Shellenbaum  * Attribute registration:
560a586ceaSMark Shellenbaum  *
570a586ceaSMark Shellenbaum  * Stored persistently on a per dataset basis
580a586ceaSMark Shellenbaum  * a mapping between attribute "string" names and their actual attribute
590a586ceaSMark Shellenbaum  * numeric values, length, and byteswap function.  The names are only used
600a586ceaSMark Shellenbaum  * during registration.  All  attributes are known by their unique attribute
610a586ceaSMark Shellenbaum  * id value.  If an attribute can have a variable size then the value
620a586ceaSMark Shellenbaum  * 0 will be used to indicate this.
630a586ceaSMark Shellenbaum  *
640a586ceaSMark Shellenbaum  * Attribute Layout:
650a586ceaSMark Shellenbaum  *
660a586ceaSMark Shellenbaum  * Attribute layouts are a way to compactly store multiple attributes, but
670a586ceaSMark Shellenbaum  * without taking the overhead associated with managing each attribute
680a586ceaSMark Shellenbaum  * individually.  Since you will typically have the same set of attributes
690a586ceaSMark Shellenbaum  * stored in the same order a single table will be used to represent that
700a586ceaSMark Shellenbaum  * layout.  The ZPL for example will usually have only about 10 different
710a586ceaSMark Shellenbaum  * layouts (regular files, device files, symlinks,
720a586ceaSMark Shellenbaum  * regular files + scanstamp, files/dir with extended attributes, and then
730a586ceaSMark Shellenbaum  * you have the possibility of all of those minus ACL, because it would
740a586ceaSMark Shellenbaum  * be kicked out into the spill block)
750a586ceaSMark Shellenbaum  *
760a586ceaSMark Shellenbaum  * Layouts are simply an array of the attributes and their
770a586ceaSMark Shellenbaum  * ordering i.e. [0, 1, 4, 5, 2]
780a586ceaSMark Shellenbaum  *
790a586ceaSMark Shellenbaum  * Each distinct layout is given a unique layout number and that is whats
800a586ceaSMark Shellenbaum  * stored in the header at the beginning of the SA data buffer.
810a586ceaSMark Shellenbaum  *
820a586ceaSMark Shellenbaum  * A layout only covers a single dbuf (bonus or spill).  If a set of
830a586ceaSMark Shellenbaum  * attributes is split up between the bonus buffer and a spill buffer then
840a586ceaSMark Shellenbaum  * two different layouts will be used.  This allows us to byteswap the
850a586ceaSMark Shellenbaum  * spill without looking at the bonus buffer and keeps the on disk format of
860a586ceaSMark Shellenbaum  * the bonus and spill buffer the same.
870a586ceaSMark Shellenbaum  *
880a586ceaSMark Shellenbaum  * Adding a single attribute will cause the entire set of attributes to
890a586ceaSMark Shellenbaum  * be rewritten and could result in a new layout number being constructed
900a586ceaSMark Shellenbaum  * as part of the rewrite if no such layout exists for the new set of
910a586ceaSMark Shellenbaum  * attribues.  The new attribute will be appended to the end of the already
920a586ceaSMark Shellenbaum  * existing attributes.
930a586ceaSMark Shellenbaum  *
940a586ceaSMark Shellenbaum  * Both the attribute registration and attribute layout information are
950a586ceaSMark Shellenbaum  * stored in normal ZAP attributes.  Their should be a small number of
960a586ceaSMark Shellenbaum  * known layouts and the set of attributes is assumed to typically be quite
970a586ceaSMark Shellenbaum  * small.
980a586ceaSMark Shellenbaum  *
990a586ceaSMark Shellenbaum  * The registered attributes and layout "table" information is maintained
1000a586ceaSMark Shellenbaum  * in core and a special "sa_os_t" is attached to the objset_t.
1010a586ceaSMark Shellenbaum  *
1020a586ceaSMark Shellenbaum  * A special interface is provided to allow for quickly applying
1030a586ceaSMark Shellenbaum  * a large set of attributes at once.  sa_replace_all_by_template() is
1040a586ceaSMark Shellenbaum  * used to set an array of attributes.  This is used by the ZPL when
1050a586ceaSMark Shellenbaum  * creating a brand new file.  The template that is passed into the function
1060a586ceaSMark Shellenbaum  * specifies the attribute, size for variable length attributes, location of
1070a586ceaSMark Shellenbaum  * data and special "data locator" function if the data isn't in a contiguous
1080a586ceaSMark Shellenbaum  * location.
1090a586ceaSMark Shellenbaum  *
1100a586ceaSMark Shellenbaum  * Byteswap implications:
1110a586ceaSMark Shellenbaum  * Since the SA attributes are not entirely self describing we can't do
1120a586ceaSMark Shellenbaum  * the normal byteswap processing.  The special ZAP layout attribute and
1130a586ceaSMark Shellenbaum  * attribute registration attributes define the byteswap function and the
1140a586ceaSMark Shellenbaum  * size of the attributes, unless it is variable sized.
1150a586ceaSMark Shellenbaum  * The normal ZFS byteswapping infrastructure assumes you don't need
1160a586ceaSMark Shellenbaum  * to read any objects in order to do the necessary byteswapping.  Whereas
1170a586ceaSMark Shellenbaum  * SA attributes can only be properly byteswapped if the dataset is opened
1180a586ceaSMark Shellenbaum  * and the layout/attribute ZAP attributes are available.  Because of this
1190a586ceaSMark Shellenbaum  * the SA attributes will be byteswapped when they are first accessed by
1200a586ceaSMark Shellenbaum  * the SA code that will read the SA data.
1210a586ceaSMark Shellenbaum  */
1220a586ceaSMark Shellenbaum 
1230a586ceaSMark Shellenbaum typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
1240a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t, void *userp);
1250a586ceaSMark Shellenbaum 
1260a586ceaSMark Shellenbaum static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
1270a586ceaSMark Shellenbaum static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
1280a586ceaSMark Shellenbaum static void *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
1290a586ceaSMark Shellenbaum     void *data);
1300a586ceaSMark Shellenbaum static void sa_idx_tab_rele(objset_t *os, void *arg);
1310a586ceaSMark Shellenbaum static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
1320a586ceaSMark Shellenbaum     int buflen);
1330a586ceaSMark Shellenbaum static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1340a586ceaSMark Shellenbaum     sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1350a586ceaSMark Shellenbaum     uint16_t buflen, dmu_tx_t *tx);
1360a586ceaSMark Shellenbaum 
1370a586ceaSMark Shellenbaum arc_byteswap_func_t *sa_bswap_table[] = {
1380a586ceaSMark Shellenbaum 	byteswap_uint64_array,
1390a586ceaSMark Shellenbaum 	byteswap_uint32_array,
1400a586ceaSMark Shellenbaum 	byteswap_uint16_array,
1410a586ceaSMark Shellenbaum 	byteswap_uint8_array,
1420a586ceaSMark Shellenbaum 	zfs_acl_byteswap,
1430a586ceaSMark Shellenbaum };
1440a586ceaSMark Shellenbaum 
1450a586ceaSMark Shellenbaum #define	SA_COPY_DATA(f, s, t, l) \
1460a586ceaSMark Shellenbaum 	{ \
1470a586ceaSMark Shellenbaum 		if (f == NULL) { \
1480a586ceaSMark Shellenbaum 			if (l == 8) { \
1490a586ceaSMark Shellenbaum 				*(uint64_t *)t = *(uint64_t *)s; \
1500a586ceaSMark Shellenbaum 			} else if (l == 16) { \
1510a586ceaSMark Shellenbaum 				*(uint64_t *)t = *(uint64_t *)s; \
1520a586ceaSMark Shellenbaum 				*(uint64_t *)((uintptr_t)t + 8) = \
1530a586ceaSMark Shellenbaum 				    *(uint64_t *)((uintptr_t)s + 8); \
1540a586ceaSMark Shellenbaum 			} else { \
1550a586ceaSMark Shellenbaum 				bcopy(s, t, l); \
1560a586ceaSMark Shellenbaum 			} \
1570a586ceaSMark Shellenbaum 		} else \
1580a586ceaSMark Shellenbaum 			sa_copy_data(f, s, t, l); \
1590a586ceaSMark Shellenbaum 	}
1600a586ceaSMark Shellenbaum 
1610a586ceaSMark Shellenbaum /*
1620a586ceaSMark Shellenbaum  * This table is fixed and cannot be changed.  Its purpose is to
1630a586ceaSMark Shellenbaum  * allow the SA code to work with both old/new ZPL file systems.
1640a586ceaSMark Shellenbaum  * It contains the list of legacy attributes.  These attributes aren't
1650a586ceaSMark Shellenbaum  * stored in the "attribute" registry zap objects, since older ZPL file systems
1660a586ceaSMark Shellenbaum  * won't have the registry.  Only objsets of type ZFS_TYPE_FILESYSTEM will
1670a586ceaSMark Shellenbaum  * use this static table.
1680a586ceaSMark Shellenbaum  */
1690a586ceaSMark Shellenbaum sa_attr_reg_t sa_legacy_attrs[] = {
1700a586ceaSMark Shellenbaum 	{"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
1710a586ceaSMark Shellenbaum 	{"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
1720a586ceaSMark Shellenbaum 	{"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
1730a586ceaSMark Shellenbaum 	{"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
1740a586ceaSMark Shellenbaum 	{"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
1750a586ceaSMark Shellenbaum 	{"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
1760a586ceaSMark Shellenbaum 	{"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
1770a586ceaSMark Shellenbaum 	{"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
1780a586ceaSMark Shellenbaum 	{"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
1790a586ceaSMark Shellenbaum 	{"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
1800a586ceaSMark Shellenbaum 	{"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
1810a586ceaSMark Shellenbaum 	{"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
1820a586ceaSMark Shellenbaum 	{"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
1830a586ceaSMark Shellenbaum 	{"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
1840a586ceaSMark Shellenbaum 	{"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
1850a586ceaSMark Shellenbaum 	{"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
1860a586ceaSMark Shellenbaum };
1870a586ceaSMark Shellenbaum 
1880a586ceaSMark Shellenbaum /*
1890a586ceaSMark Shellenbaum  * ZPL legacy layout
1900a586ceaSMark Shellenbaum  * This is only used for objects of type DMU_OT_ZNODE
1910a586ceaSMark Shellenbaum  */
1920a586ceaSMark Shellenbaum sa_attr_type_t sa_legacy_zpl_layout[] = {
1930a586ceaSMark Shellenbaum     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1940a586ceaSMark Shellenbaum };
1950a586ceaSMark Shellenbaum 
1960a586ceaSMark Shellenbaum /*
1970a586ceaSMark Shellenbaum  * Special dummy layout used for buffers with no attributes.
1980a586ceaSMark Shellenbaum  */
1990a586ceaSMark Shellenbaum 
2000a586ceaSMark Shellenbaum sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
2010a586ceaSMark Shellenbaum 
2020a586ceaSMark Shellenbaum static int sa_legacy_attr_count = 16;
2030a586ceaSMark Shellenbaum static kmem_cache_t *sa_cache = NULL;
2040a586ceaSMark Shellenbaum 
2050a586ceaSMark Shellenbaum /*ARGSUSED*/
2060a586ceaSMark Shellenbaum static int
2070a586ceaSMark Shellenbaum sa_cache_constructor(void *buf, void *unused, int kmflag)
2080a586ceaSMark Shellenbaum {
2090a586ceaSMark Shellenbaum 	sa_handle_t *hdl = buf;
2100a586ceaSMark Shellenbaum 
2110a586ceaSMark Shellenbaum 	hdl->sa_bonus_tab = NULL;
2120a586ceaSMark Shellenbaum 	hdl->sa_spill_tab = NULL;
2130a586ceaSMark Shellenbaum 	hdl->sa_os = NULL;
2140a586ceaSMark Shellenbaum 	hdl->sa_userp = NULL;
2150a586ceaSMark Shellenbaum 	hdl->sa_bonus = NULL;
2160a586ceaSMark Shellenbaum 	hdl->sa_spill = NULL;
2170a586ceaSMark Shellenbaum 	mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
2180a586ceaSMark Shellenbaum 	return (0);
2190a586ceaSMark Shellenbaum }
2200a586ceaSMark Shellenbaum 
2210a586ceaSMark Shellenbaum /*ARGSUSED*/
2220a586ceaSMark Shellenbaum static void
2230a586ceaSMark Shellenbaum sa_cache_destructor(void *buf, void *unused)
2240a586ceaSMark Shellenbaum {
2250a586ceaSMark Shellenbaum 	sa_handle_t *hdl = buf;
2260a586ceaSMark Shellenbaum 	mutex_destroy(&hdl->sa_lock);
2270a586ceaSMark Shellenbaum }
2280a586ceaSMark Shellenbaum 
2290a586ceaSMark Shellenbaum void
2300a586ceaSMark Shellenbaum sa_cache_init(void)
2310a586ceaSMark Shellenbaum {
2320a586ceaSMark Shellenbaum 	sa_cache = kmem_cache_create("sa_cache",
2330a586ceaSMark Shellenbaum 	    sizeof (sa_handle_t), 0, sa_cache_constructor,
2340a586ceaSMark Shellenbaum 	    sa_cache_destructor, NULL, NULL, NULL, 0);
2350a586ceaSMark Shellenbaum }
2360a586ceaSMark Shellenbaum 
2370a586ceaSMark Shellenbaum void
2380a586ceaSMark Shellenbaum sa_cache_fini(void)
2390a586ceaSMark Shellenbaum {
2400a586ceaSMark Shellenbaum 	if (sa_cache)
2410a586ceaSMark Shellenbaum 		kmem_cache_destroy(sa_cache);
2420a586ceaSMark Shellenbaum }
2430a586ceaSMark Shellenbaum 
2440a586ceaSMark Shellenbaum static int
2450a586ceaSMark Shellenbaum layout_num_compare(const void *arg1, const void *arg2)
2460a586ceaSMark Shellenbaum {
2470a586ceaSMark Shellenbaum 	const sa_lot_t *node1 = arg1;
2480a586ceaSMark Shellenbaum 	const sa_lot_t *node2 = arg2;
2490a586ceaSMark Shellenbaum 
2500a586ceaSMark Shellenbaum 	if (node1->lot_num > node2->lot_num)
2510a586ceaSMark Shellenbaum 		return (1);
2520a586ceaSMark Shellenbaum 	else if (node1->lot_num < node2->lot_num)
2530a586ceaSMark Shellenbaum 		return (-1);
2540a586ceaSMark Shellenbaum 	return (0);
2550a586ceaSMark Shellenbaum }
2560a586ceaSMark Shellenbaum 
2570a586ceaSMark Shellenbaum static int
2580a586ceaSMark Shellenbaum layout_hash_compare(const void *arg1, const void *arg2)
2590a586ceaSMark Shellenbaum {
2600a586ceaSMark Shellenbaum 	const sa_lot_t *node1 = arg1;
2610a586ceaSMark Shellenbaum 	const sa_lot_t *node2 = arg2;
2620a586ceaSMark Shellenbaum 
2630a586ceaSMark Shellenbaum 	if (node1->lot_hash > node2->lot_hash)
2640a586ceaSMark Shellenbaum 		return (1);
2650a586ceaSMark Shellenbaum 	if (node1->lot_hash < node2->lot_hash)
2660a586ceaSMark Shellenbaum 		return (-1);
2670a586ceaSMark Shellenbaum 	if (node1->lot_instance > node2->lot_instance)
2680a586ceaSMark Shellenbaum 		return (1);
2690a586ceaSMark Shellenbaum 	if (node1->lot_instance < node2->lot_instance)
2700a586ceaSMark Shellenbaum 		return (-1);
2710a586ceaSMark Shellenbaum 	return (0);
2720a586ceaSMark Shellenbaum }
2730a586ceaSMark Shellenbaum 
2740a586ceaSMark Shellenbaum boolean_t
2750a586ceaSMark Shellenbaum sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
2760a586ceaSMark Shellenbaum {
2770a586ceaSMark Shellenbaum 	int i;
2780a586ceaSMark Shellenbaum 
2790a586ceaSMark Shellenbaum 	if (count != tbf->lot_attr_count)
2800a586ceaSMark Shellenbaum 		return (1);
2810a586ceaSMark Shellenbaum 
2820a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
2830a586ceaSMark Shellenbaum 		if (attrs[i] != tbf->lot_attrs[i])
2840a586ceaSMark Shellenbaum 			return (1);
2850a586ceaSMark Shellenbaum 	}
2860a586ceaSMark Shellenbaum 	return (0);
2870a586ceaSMark Shellenbaum }
2880a586ceaSMark Shellenbaum 
2890a586ceaSMark Shellenbaum #define	SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
2900a586ceaSMark Shellenbaum 
2910a586ceaSMark Shellenbaum static uint64_t
2920a586ceaSMark Shellenbaum sa_layout_info_hash(sa_attr_type_t *attrs, int attr_count)
2930a586ceaSMark Shellenbaum {
2940a586ceaSMark Shellenbaum 	int i;
2950a586ceaSMark Shellenbaum 	uint64_t crc = -1ULL;
2960a586ceaSMark Shellenbaum 
2970a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++)
2980a586ceaSMark Shellenbaum 		crc ^= SA_ATTR_HASH(attrs[i]);
2990a586ceaSMark Shellenbaum 
3000a586ceaSMark Shellenbaum 	return (crc);
3010a586ceaSMark Shellenbaum }
3020a586ceaSMark Shellenbaum 
3030a586ceaSMark Shellenbaum static boolean_t
3040a586ceaSMark Shellenbaum sa_has_blkptr(sa_handle_t *hdl)
3050a586ceaSMark Shellenbaum {
3060a586ceaSMark Shellenbaum 	int rc;
3070a586ceaSMark Shellenbaum 	if (hdl->sa_spill == NULL) {
3080a586ceaSMark Shellenbaum 		if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
3090a586ceaSMark Shellenbaum 		    &hdl->sa_spill)) == 0)
3100a586ceaSMark Shellenbaum 			VERIFY(0 == sa_build_index(hdl, SA_SPILL));
3110a586ceaSMark Shellenbaum 	} else {
3120a586ceaSMark Shellenbaum 		rc = 0;
3130a586ceaSMark Shellenbaum 	}
3140a586ceaSMark Shellenbaum 
3150a586ceaSMark Shellenbaum 	return (rc == 0 ? B_TRUE : B_FALSE);
3160a586ceaSMark Shellenbaum }
3170a586ceaSMark Shellenbaum 
3180a586ceaSMark Shellenbaum /*
3190a586ceaSMark Shellenbaum  * Main attribute lookup/update function
3200a586ceaSMark Shellenbaum  * returns 0 for success or non zero for failures
3210a586ceaSMark Shellenbaum  *
3220a586ceaSMark Shellenbaum  * Operates on bulk array, first failure will abort further processing
3230a586ceaSMark Shellenbaum  */
3240a586ceaSMark Shellenbaum int
3250a586ceaSMark Shellenbaum sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
3260a586ceaSMark Shellenbaum     sa_data_op_t data_op, dmu_tx_t *tx)
3270a586ceaSMark Shellenbaum {
3280a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
3290a586ceaSMark Shellenbaum 	int i;
3300a586ceaSMark Shellenbaum 	int error = 0;
3310a586ceaSMark Shellenbaum 	sa_buf_type_t buftypes;
3320a586ceaSMark Shellenbaum 
3330a586ceaSMark Shellenbaum 	buftypes = 0;
3340a586ceaSMark Shellenbaum 
3350a586ceaSMark Shellenbaum 	ASSERT(count > 0);
3360a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
3370a586ceaSMark Shellenbaum 		ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
3380a586ceaSMark Shellenbaum 
3390a586ceaSMark Shellenbaum 		bulk[i].sa_addr = NULL;
3400a586ceaSMark Shellenbaum 		/* First check the bonus buffer */
3410a586ceaSMark Shellenbaum 
3420a586ceaSMark Shellenbaum 		if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
3430a586ceaSMark Shellenbaum 		    hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
3440a586ceaSMark Shellenbaum 			SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
3450a586ceaSMark Shellenbaum 			    SA_GET_HDR(hdl, SA_BONUS),
3460a586ceaSMark Shellenbaum 			    bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
3470a586ceaSMark Shellenbaum 			if (tx && !(buftypes & SA_BONUS)) {
3480a586ceaSMark Shellenbaum 				dmu_buf_will_dirty(hdl->sa_bonus, tx);
3490a586ceaSMark Shellenbaum 				buftypes |= SA_BONUS;
3500a586ceaSMark Shellenbaum 			}
3510a586ceaSMark Shellenbaum 		}
3520a586ceaSMark Shellenbaum 		if (bulk[i].sa_addr == NULL && sa_has_blkptr(hdl)) {
3530a586ceaSMark Shellenbaum 			if (TOC_ATTR_PRESENT(
3540a586ceaSMark Shellenbaum 			    hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
3550a586ceaSMark Shellenbaum 				SA_ATTR_INFO(sa, hdl->sa_spill_tab,
3560a586ceaSMark Shellenbaum 				    SA_GET_HDR(hdl, SA_SPILL),
3570a586ceaSMark Shellenbaum 				    bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
3580a586ceaSMark Shellenbaum 				if (tx && !(buftypes & SA_SPILL) &&
3590a586ceaSMark Shellenbaum 				    bulk[i].sa_size == bulk[i].sa_length) {
3600a586ceaSMark Shellenbaum 					dmu_buf_will_dirty(hdl->sa_spill, tx);
3610a586ceaSMark Shellenbaum 					buftypes |= SA_SPILL;
3620a586ceaSMark Shellenbaum 				}
3630a586ceaSMark Shellenbaum 			}
3640a586ceaSMark Shellenbaum 		}
3650a586ceaSMark Shellenbaum 		switch (data_op) {
3660a586ceaSMark Shellenbaum 		case SA_LOOKUP:
3670a586ceaSMark Shellenbaum 			if (bulk[i].sa_addr == NULL)
3680a586ceaSMark Shellenbaum 				return (ENOENT);
3690a586ceaSMark Shellenbaum 			if (bulk[i].sa_data) {
3700a586ceaSMark Shellenbaum 				SA_COPY_DATA(bulk[i].sa_data_func,
3710a586ceaSMark Shellenbaum 				    bulk[i].sa_addr, bulk[i].sa_data,
3720a586ceaSMark Shellenbaum 				    bulk[i].sa_size);
3730a586ceaSMark Shellenbaum 			}
3740a586ceaSMark Shellenbaum 			continue;
3750a586ceaSMark Shellenbaum 
3760a586ceaSMark Shellenbaum 		case SA_UPDATE:
3770a586ceaSMark Shellenbaum 			/* existing rewrite of attr */
3780a586ceaSMark Shellenbaum 			if (bulk[i].sa_addr &&
3790a586ceaSMark Shellenbaum 			    bulk[i].sa_size == bulk[i].sa_length) {
3800a586ceaSMark Shellenbaum 				SA_COPY_DATA(bulk[i].sa_data_func,
3810a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_addr,
3820a586ceaSMark Shellenbaum 				    bulk[i].sa_length);
3830a586ceaSMark Shellenbaum 				continue;
3840a586ceaSMark Shellenbaum 			} else if (bulk[i].sa_addr) { /* attr size change */
3850a586ceaSMark Shellenbaum 				error = sa_modify_attrs(hdl, bulk[i].sa_attr,
3860a586ceaSMark Shellenbaum 				    SA_REPLACE, bulk[i].sa_data_func,
3870a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_length, tx);
3880a586ceaSMark Shellenbaum 			} else { /* adding new attribute */
3890a586ceaSMark Shellenbaum 				error = sa_modify_attrs(hdl, bulk[i].sa_attr,
3900a586ceaSMark Shellenbaum 				    SA_ADD, bulk[i].sa_data_func,
3910a586ceaSMark Shellenbaum 				    bulk[i].sa_data, bulk[i].sa_length, tx);
3920a586ceaSMark Shellenbaum 			}
3930a586ceaSMark Shellenbaum 			if (error)
3940a586ceaSMark Shellenbaum 				return (error);
3950a586ceaSMark Shellenbaum 			break;
3960a586ceaSMark Shellenbaum 		}
3970a586ceaSMark Shellenbaum 	}
3980a586ceaSMark Shellenbaum 	return (error);
3990a586ceaSMark Shellenbaum }
4000a586ceaSMark Shellenbaum 
4010a586ceaSMark Shellenbaum static sa_lot_t *
4020a586ceaSMark Shellenbaum sa_add_layout_entry(objset_t *os, sa_attr_type_t *attrs, int attr_count,
4030a586ceaSMark Shellenbaum     uint64_t lot_num, uint64_t hash, boolean_t zapadd, dmu_tx_t *tx)
4040a586ceaSMark Shellenbaum {
4050a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
4060a586ceaSMark Shellenbaum 	sa_lot_t *tb, *findtb;
4070a586ceaSMark Shellenbaum 	int i;
4080a586ceaSMark Shellenbaum 	avl_index_t loc;
4090a586ceaSMark Shellenbaum 
4100a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
4110a586ceaSMark Shellenbaum 	tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
4120a586ceaSMark Shellenbaum 	tb->lot_attr_count = attr_count;
4130a586ceaSMark Shellenbaum 	tb->lot_attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
4140a586ceaSMark Shellenbaum 	    KM_SLEEP);
4150a586ceaSMark Shellenbaum 	bcopy(attrs, tb->lot_attrs, sizeof (sa_attr_type_t) * attr_count);
4160a586ceaSMark Shellenbaum 	tb->lot_num = lot_num;
4170a586ceaSMark Shellenbaum 	tb->lot_hash = hash;
4180a586ceaSMark Shellenbaum 	tb->lot_instance = 0;
4190a586ceaSMark Shellenbaum 
4200a586ceaSMark Shellenbaum 	if (zapadd) {
4210a586ceaSMark Shellenbaum 		char attr_name[8];
4220a586ceaSMark Shellenbaum 
4230a586ceaSMark Shellenbaum 		if (sa->sa_layout_attr_obj == 0) {
4240a586ceaSMark Shellenbaum 			int error;
4250a586ceaSMark Shellenbaum 			sa->sa_layout_attr_obj = zap_create(os,
4260a586ceaSMark Shellenbaum 			    DMU_OT_SA_ATTR_LAYOUTS, DMU_OT_NONE, 0, tx);
4270a586ceaSMark Shellenbaum 			error = zap_add(os, sa->sa_master_obj, SA_LAYOUTS, 8, 1,
4280a586ceaSMark Shellenbaum 			    &sa->sa_layout_attr_obj, tx);
4290a586ceaSMark Shellenbaum 			ASSERT3U(error, ==, 0);
4300a586ceaSMark Shellenbaum 		}
4310a586ceaSMark Shellenbaum 
4320a586ceaSMark Shellenbaum 		(void) snprintf(attr_name, sizeof (attr_name),
4330a586ceaSMark Shellenbaum 		    "%d", (int)lot_num);
4340a586ceaSMark Shellenbaum 		VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
4350a586ceaSMark Shellenbaum 		    attr_name, 2, attr_count, attrs, tx));
4360a586ceaSMark Shellenbaum 	}
4370a586ceaSMark Shellenbaum 
4380a586ceaSMark Shellenbaum 	list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
4390a586ceaSMark Shellenbaum 	    offsetof(sa_idx_tab_t, sa_next));
4400a586ceaSMark Shellenbaum 
4410a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++) {
4420a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
4430a586ceaSMark Shellenbaum 			tb->lot_var_sizes++;
4440a586ceaSMark Shellenbaum 	}
4450a586ceaSMark Shellenbaum 
4460a586ceaSMark Shellenbaum 	avl_add(&sa->sa_layout_num_tree, tb);
4470a586ceaSMark Shellenbaum 
4480a586ceaSMark Shellenbaum 	/* verify we don't have a hash collision */
4490a586ceaSMark Shellenbaum 	if ((findtb = avl_find(&sa->sa_layout_hash_tree, tb, &loc)) != NULL) {
4500a586ceaSMark Shellenbaum 		for (; findtb && findtb->lot_hash == hash;
4510a586ceaSMark Shellenbaum 		    findtb = AVL_NEXT(&sa->sa_layout_hash_tree, findtb)) {
4520a586ceaSMark Shellenbaum 			if (findtb->lot_instance != tb->lot_instance)
4530a586ceaSMark Shellenbaum 				break;
4540a586ceaSMark Shellenbaum 			tb->lot_instance++;
4550a586ceaSMark Shellenbaum 		}
4560a586ceaSMark Shellenbaum 	}
4570a586ceaSMark Shellenbaum 	avl_add(&sa->sa_layout_hash_tree, tb);
4580a586ceaSMark Shellenbaum 	return (tb);
4590a586ceaSMark Shellenbaum }
4600a586ceaSMark Shellenbaum 
4610a586ceaSMark Shellenbaum static void
4620a586ceaSMark Shellenbaum sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
4630a586ceaSMark Shellenbaum     int count, dmu_tx_t *tx, sa_lot_t **lot)
4640a586ceaSMark Shellenbaum {
4650a586ceaSMark Shellenbaum 	sa_lot_t *tb, tbsearch;
4660a586ceaSMark Shellenbaum 	avl_index_t loc;
4670a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
4680a586ceaSMark Shellenbaum 	boolean_t found = B_FALSE;
4690a586ceaSMark Shellenbaum 
4700a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
4710a586ceaSMark Shellenbaum 	tbsearch.lot_hash = hash;
4720a586ceaSMark Shellenbaum 	tbsearch.lot_instance = 0;
4730a586ceaSMark Shellenbaum 	tb = avl_find(&sa->sa_layout_hash_tree, &tbsearch, &loc);
4740a586ceaSMark Shellenbaum 	if (tb) {
4750a586ceaSMark Shellenbaum 		for (; tb && tb->lot_hash == hash;
4760a586ceaSMark Shellenbaum 		    tb = AVL_NEXT(&sa->sa_layout_hash_tree, tb)) {
4770a586ceaSMark Shellenbaum 			if (sa_layout_equal(tb, attrs, count) == 0) {
4780a586ceaSMark Shellenbaum 				found = B_TRUE;
4790a586ceaSMark Shellenbaum 				break;
4800a586ceaSMark Shellenbaum 			}
4810a586ceaSMark Shellenbaum 		}
4820a586ceaSMark Shellenbaum 	}
4830a586ceaSMark Shellenbaum 	if (!found) {
4840a586ceaSMark Shellenbaum 		tb = sa_add_layout_entry(os, attrs, count,
4850a586ceaSMark Shellenbaum 		    avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
4860a586ceaSMark Shellenbaum 	}
4870a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
4880a586ceaSMark Shellenbaum 	*lot = tb;
4890a586ceaSMark Shellenbaum }
4900a586ceaSMark Shellenbaum 
4910a586ceaSMark Shellenbaum static int
4920a586ceaSMark Shellenbaum sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
4930a586ceaSMark Shellenbaum {
4940a586ceaSMark Shellenbaum 	int error;
4950a586ceaSMark Shellenbaum 	uint32_t blocksize;
4960a586ceaSMark Shellenbaum 
4970a586ceaSMark Shellenbaum 	if (size == 0) {
4980a586ceaSMark Shellenbaum 		blocksize = SPA_MINBLOCKSIZE;
4990a586ceaSMark Shellenbaum 	} else if (size > SPA_MAXBLOCKSIZE) {
5000a586ceaSMark Shellenbaum 		ASSERT(0);
5010a586ceaSMark Shellenbaum 		return (EFBIG);
5020a586ceaSMark Shellenbaum 	} else {
5030a586ceaSMark Shellenbaum 		blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
5040a586ceaSMark Shellenbaum 	}
5050a586ceaSMark Shellenbaum 
5060a586ceaSMark Shellenbaum 	error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
5070a586ceaSMark Shellenbaum 	ASSERT(error == 0);
5080a586ceaSMark Shellenbaum 	return (error);
5090a586ceaSMark Shellenbaum }
5100a586ceaSMark Shellenbaum 
5110a586ceaSMark Shellenbaum static void
5120a586ceaSMark Shellenbaum sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
5130a586ceaSMark Shellenbaum {
5140a586ceaSMark Shellenbaum 	if (func == NULL) {
5150a586ceaSMark Shellenbaum 		bcopy(datastart, target, buflen);
5160a586ceaSMark Shellenbaum 	} else {
5170a586ceaSMark Shellenbaum 		boolean_t start;
5180a586ceaSMark Shellenbaum 		int bytes;
5190a586ceaSMark Shellenbaum 		void *dataptr;
5200a586ceaSMark Shellenbaum 		void *saptr = target;
5210a586ceaSMark Shellenbaum 		uint32_t length;
5220a586ceaSMark Shellenbaum 
5230a586ceaSMark Shellenbaum 		start = B_TRUE;
5240a586ceaSMark Shellenbaum 		bytes = 0;
5250a586ceaSMark Shellenbaum 		while (bytes < buflen) {
5260a586ceaSMark Shellenbaum 			func(&dataptr, &length, buflen, start, datastart);
5270a586ceaSMark Shellenbaum 			bcopy(dataptr, saptr, length);
5280a586ceaSMark Shellenbaum 			saptr = (void *)((caddr_t)saptr + length);
5290a586ceaSMark Shellenbaum 			bytes += length;
5300a586ceaSMark Shellenbaum 			start = B_FALSE;
5310a586ceaSMark Shellenbaum 		}
5320a586ceaSMark Shellenbaum 	}
5330a586ceaSMark Shellenbaum }
5340a586ceaSMark Shellenbaum 
5350a586ceaSMark Shellenbaum /*
5360a586ceaSMark Shellenbaum  * Determine several different sizes
5370a586ceaSMark Shellenbaum  * first the sa header size
5380a586ceaSMark Shellenbaum  * the number of bytes to be stored
5390a586ceaSMark Shellenbaum  * if spill would occur the index in the attribute array is returned
5400a586ceaSMark Shellenbaum  *
5410a586ceaSMark Shellenbaum  * the boolean will_spill will be set when spilling is necessary.  It
5420a586ceaSMark Shellenbaum  * is only set when the buftype is SA_BONUS
5430a586ceaSMark Shellenbaum  */
5440a586ceaSMark Shellenbaum static int
5450a586ceaSMark Shellenbaum sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
5460a586ceaSMark Shellenbaum     dmu_buf_t *db, sa_buf_type_t buftype, int *index, int *total,
5470a586ceaSMark Shellenbaum     boolean_t *will_spill)
5480a586ceaSMark Shellenbaum {
5490a586ceaSMark Shellenbaum 	int var_size = 0;
5500a586ceaSMark Shellenbaum 	int i;
5510a586ceaSMark Shellenbaum 	int full_space;
5520a586ceaSMark Shellenbaum 	int hdrsize;
5530a586ceaSMark Shellenbaum 	boolean_t done = B_FALSE;
5540a586ceaSMark Shellenbaum 
5550a586ceaSMark Shellenbaum 	if (buftype == SA_BONUS && sa->sa_force_spill) {
5560a586ceaSMark Shellenbaum 		*total = 0;
5570a586ceaSMark Shellenbaum 		*index = 0;
5580a586ceaSMark Shellenbaum 		*will_spill = B_TRUE;
5590a586ceaSMark Shellenbaum 		return (0);
5600a586ceaSMark Shellenbaum 	}
5610a586ceaSMark Shellenbaum 
5620a586ceaSMark Shellenbaum 	*index = -1;
5630a586ceaSMark Shellenbaum 	*total = 0;
5640a586ceaSMark Shellenbaum 
5650a586ceaSMark Shellenbaum 	if (buftype == SA_BONUS)
5660a586ceaSMark Shellenbaum 		*will_spill = B_FALSE;
5670a586ceaSMark Shellenbaum 
5680a586ceaSMark Shellenbaum 	hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
5690a586ceaSMark Shellenbaum 	    sizeof (sa_hdr_phys_t);
5700a586ceaSMark Shellenbaum 
5710a586ceaSMark Shellenbaum 	full_space = (buftype == SA_BONUS) ? DN_MAX_BONUSLEN : db->db_size;
5720a586ceaSMark Shellenbaum 
5730a586ceaSMark Shellenbaum 	for (i = 0; i != attr_count; i++) {
5740a586ceaSMark Shellenbaum 		boolean_t is_var_sz;
5750a586ceaSMark Shellenbaum 
5760a586ceaSMark Shellenbaum 		*total += attr_desc[i].sa_length;
5770a586ceaSMark Shellenbaum 		if (done)
5780a586ceaSMark Shellenbaum 			goto next;
5790a586ceaSMark Shellenbaum 
5800a586ceaSMark Shellenbaum 		is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
5810a586ceaSMark Shellenbaum 		if (is_var_sz) {
5820a586ceaSMark Shellenbaum 			var_size++;
5830a586ceaSMark Shellenbaum 		}
5840a586ceaSMark Shellenbaum 
5850a586ceaSMark Shellenbaum 		if (is_var_sz && var_size > 1) {
5860a586ceaSMark Shellenbaum 			if (P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
5870a586ceaSMark Shellenbaum 			    *total < full_space) {
5880a586ceaSMark Shellenbaum 				hdrsize += sizeof (uint16_t);
5890a586ceaSMark Shellenbaum 			} else {
5900a586ceaSMark Shellenbaum 				done = B_TRUE;
5910a586ceaSMark Shellenbaum 				*index = i;
5920a586ceaSMark Shellenbaum 				if (buftype == SA_BONUS)
5930a586ceaSMark Shellenbaum 					*will_spill = B_TRUE;
5940a586ceaSMark Shellenbaum 				continue;
5950a586ceaSMark Shellenbaum 			}
5960a586ceaSMark Shellenbaum 		}
5970a586ceaSMark Shellenbaum 
5980a586ceaSMark Shellenbaum 		/*
5990a586ceaSMark Shellenbaum 		 * find index of where spill *could* occur.
6000a586ceaSMark Shellenbaum 		 * Then continue to count of remainder attribute
6010a586ceaSMark Shellenbaum 		 * space.  The sum is used later for sizing bonus
6020a586ceaSMark Shellenbaum 		 * and spill buffer.
6030a586ceaSMark Shellenbaum 		 */
6040a586ceaSMark Shellenbaum 		if (buftype == SA_BONUS && *index == -1 &&
6050a586ceaSMark Shellenbaum 		    P2ROUNDUP(*total + hdrsize, 8) >
6060a586ceaSMark Shellenbaum 		    (full_space - sizeof (blkptr_t))) {
6070a586ceaSMark Shellenbaum 			*index = i;
6080a586ceaSMark Shellenbaum 			done = B_TRUE;
6090a586ceaSMark Shellenbaum 		}
6100a586ceaSMark Shellenbaum 
6110a586ceaSMark Shellenbaum next:
6120a586ceaSMark Shellenbaum 		if (P2ROUNDUP(*total + hdrsize, 8) > full_space &&
6130a586ceaSMark Shellenbaum 		    buftype == SA_BONUS)
6140a586ceaSMark Shellenbaum 			*will_spill = B_TRUE;
6150a586ceaSMark Shellenbaum 	}
6160a586ceaSMark Shellenbaum 
6170a586ceaSMark Shellenbaum 	hdrsize = P2ROUNDUP(hdrsize, 8);
6180a586ceaSMark Shellenbaum 	return (hdrsize);
6190a586ceaSMark Shellenbaum }
6200a586ceaSMark Shellenbaum 
6210a586ceaSMark Shellenbaum #define	BUF_SPACE_NEEDED(total, header) (total + header)
6220a586ceaSMark Shellenbaum 
6230a586ceaSMark Shellenbaum /*
6240a586ceaSMark Shellenbaum  * Find layout that corresponds to ordering of attributes
6250a586ceaSMark Shellenbaum  * If not found a new layout number is created and added to
6260a586ceaSMark Shellenbaum  * persistent layout tables.
6270a586ceaSMark Shellenbaum  */
6280a586ceaSMark Shellenbaum static int
6290a586ceaSMark Shellenbaum sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
6300a586ceaSMark Shellenbaum     dmu_tx_t *tx)
6310a586ceaSMark Shellenbaum {
6320a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
6330a586ceaSMark Shellenbaum 	uint64_t hash;
6340a586ceaSMark Shellenbaum 	sa_buf_type_t buftype;
6350a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sahdr;
6360a586ceaSMark Shellenbaum 	void *data_start;
6370a586ceaSMark Shellenbaum 	int buf_space;
6380a586ceaSMark Shellenbaum 	sa_attr_type_t *attrs, *attrs_start;
6390a586ceaSMark Shellenbaum 	int i, lot_count;
6400a586ceaSMark Shellenbaum 	int hdrsize, spillhdrsize;
6410a586ceaSMark Shellenbaum 	int used;
6420a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype;
6430a586ceaSMark Shellenbaum 	sa_lot_t *lot;
6440a586ceaSMark Shellenbaum 	int len_idx;
6450a586ceaSMark Shellenbaum 	int spill_used;
6460a586ceaSMark Shellenbaum 	boolean_t spilling;
6470a586ceaSMark Shellenbaum 
6480a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(hdl->sa_bonus, tx);
6490a586ceaSMark Shellenbaum 	bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
6500a586ceaSMark Shellenbaum 
6510a586ceaSMark Shellenbaum 	/* first determine bonus header size and sum of all attributes */
6520a586ceaSMark Shellenbaum 	hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
6530a586ceaSMark Shellenbaum 	    SA_BONUS, &i, &used, &spilling);
6540a586ceaSMark Shellenbaum 
6550a586ceaSMark Shellenbaum 	if (used > SPA_MAXBLOCKSIZE)
6560a586ceaSMark Shellenbaum 		return (EFBIG);
6570a586ceaSMark Shellenbaum 
6580a586ceaSMark Shellenbaum 	VERIFY(0 == dmu_set_bonus(hdl->sa_bonus, spilling ?
6590a586ceaSMark Shellenbaum 	    MIN(DN_MAX_BONUSLEN - sizeof (blkptr_t), used + hdrsize) :
6600a586ceaSMark Shellenbaum 	    used + hdrsize, tx));
6610a586ceaSMark Shellenbaum 
6620a586ceaSMark Shellenbaum 	ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
6630a586ceaSMark Shellenbaum 	    bonustype == DMU_OT_SA);
6640a586ceaSMark Shellenbaum 
6650a586ceaSMark Shellenbaum 	/* setup and size spill buffer when needed */
6660a586ceaSMark Shellenbaum 	if (spilling) {
6670a586ceaSMark Shellenbaum 		boolean_t dummy;
6680a586ceaSMark Shellenbaum 
6690a586ceaSMark Shellenbaum 		if (hdl->sa_spill == NULL) {
6700a586ceaSMark Shellenbaum 			int error;
6710a586ceaSMark Shellenbaum 			error = dmu_spill_hold_by_bonus(hdl->sa_bonus, NULL,
6720a586ceaSMark Shellenbaum 			    &hdl->sa_spill);
6730a586ceaSMark Shellenbaum 			ASSERT3U(error, ==, 0);
6740a586ceaSMark Shellenbaum 		}
6750a586ceaSMark Shellenbaum 		dmu_buf_will_dirty(hdl->sa_spill, tx);
6760a586ceaSMark Shellenbaum 
6770a586ceaSMark Shellenbaum 		spillhdrsize = sa_find_sizes(sa, &attr_desc[i],
6780a586ceaSMark Shellenbaum 		    attr_count - i, hdl->sa_spill, SA_SPILL, &i,
6790a586ceaSMark Shellenbaum 		    &spill_used, &dummy);
6800a586ceaSMark Shellenbaum 
6810a586ceaSMark Shellenbaum 		if (spill_used > SPA_MAXBLOCKSIZE)
6820a586ceaSMark Shellenbaum 			return (EFBIG);
6830a586ceaSMark Shellenbaum 
6840a586ceaSMark Shellenbaum 		buf_space = hdl->sa_spill->db_size - spillhdrsize;
6850a586ceaSMark Shellenbaum 		if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
6860a586ceaSMark Shellenbaum 		    hdl->sa_spill->db_size)
6870a586ceaSMark Shellenbaum 			VERIFY(0 == sa_resize_spill(hdl,
6880a586ceaSMark Shellenbaum 			    BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
6890a586ceaSMark Shellenbaum 	}
6900a586ceaSMark Shellenbaum 
6910a586ceaSMark Shellenbaum 	/* setup starting pointers to lay down data */
6920a586ceaSMark Shellenbaum 	data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
6930a586ceaSMark Shellenbaum 	sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
6940a586ceaSMark Shellenbaum 	buftype = SA_BONUS;
6950a586ceaSMark Shellenbaum 
6960a586ceaSMark Shellenbaum 	if (spilling)
6970a586ceaSMark Shellenbaum 		buf_space = (sa->sa_force_spill) ?
6980a586ceaSMark Shellenbaum 		    0 : SA_BLKPTR_SPACE - hdrsize;
6990a586ceaSMark Shellenbaum 	else
7000a586ceaSMark Shellenbaum 		buf_space = hdl->sa_bonus->db_size - hdrsize;
7010a586ceaSMark Shellenbaum 
7020a586ceaSMark Shellenbaum 	attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
7030a586ceaSMark Shellenbaum 	    KM_SLEEP);
7040a586ceaSMark Shellenbaum 	lot_count = 0;
7050a586ceaSMark Shellenbaum 
7060a586ceaSMark Shellenbaum 	for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
7070a586ceaSMark Shellenbaum 		uint16_t length;
7080a586ceaSMark Shellenbaum 
7090a586ceaSMark Shellenbaum 		attrs[i] = attr_desc[i].sa_attr;
7100a586ceaSMark Shellenbaum 		length = SA_REGISTERED_LEN(sa, attrs[i]);
7110a586ceaSMark Shellenbaum 		if (length == 0)
7120a586ceaSMark Shellenbaum 			length = attr_desc[i].sa_length;
7130a586ceaSMark Shellenbaum 
7140a586ceaSMark Shellenbaum 		if (buf_space < length) {  /* switch to spill buffer */
7150a586ceaSMark Shellenbaum 			ASSERT(bonustype != DMU_OT_ZNODE);
7160a586ceaSMark Shellenbaum 			if (buftype == SA_BONUS && !sa->sa_force_spill) {
7170a586ceaSMark Shellenbaum 				sa_find_layout(hdl->sa_os, hash, attrs_start,
7180a586ceaSMark Shellenbaum 				    lot_count, tx, &lot);
7190a586ceaSMark Shellenbaum 				SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
7200a586ceaSMark Shellenbaum 			}
7210a586ceaSMark Shellenbaum 
7220a586ceaSMark Shellenbaum 			buftype = SA_SPILL;
7230a586ceaSMark Shellenbaum 			hash = -1ULL;
7240a586ceaSMark Shellenbaum 			len_idx = 0;
7250a586ceaSMark Shellenbaum 
7260a586ceaSMark Shellenbaum 			sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
7270a586ceaSMark Shellenbaum 			sahdr->sa_magic = SA_MAGIC;
7280a586ceaSMark Shellenbaum 			data_start = (void *)((uintptr_t)sahdr +
7290a586ceaSMark Shellenbaum 			    spillhdrsize);
7300a586ceaSMark Shellenbaum 			attrs_start = &attrs[i];
7310a586ceaSMark Shellenbaum 			buf_space = hdl->sa_spill->db_size - spillhdrsize;
7320a586ceaSMark Shellenbaum 			lot_count = 0;
7330a586ceaSMark Shellenbaum 		}
7340a586ceaSMark Shellenbaum 		hash ^= SA_ATTR_HASH(attrs[i]);
7350a586ceaSMark Shellenbaum 		attr_desc[i].sa_addr = data_start;
7360a586ceaSMark Shellenbaum 		attr_desc[i].sa_size = length;
7370a586ceaSMark Shellenbaum 		SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
7380a586ceaSMark Shellenbaum 		    data_start, length);
7390a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
7400a586ceaSMark Shellenbaum 			sahdr->sa_lengths[len_idx++] = length;
7410a586ceaSMark Shellenbaum 		}
7420a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
7430a586ceaSMark Shellenbaum 		    length), 8);
7440a586ceaSMark Shellenbaum 		buf_space -= P2ROUNDUP(length, 8);
7450a586ceaSMark Shellenbaum 		lot_count++;
7460a586ceaSMark Shellenbaum 	}
7470a586ceaSMark Shellenbaum 
7480a586ceaSMark Shellenbaum 	sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
7490a586ceaSMark Shellenbaum 	if (bonustype == DMU_OT_SA) {
7500a586ceaSMark Shellenbaum 		SA_SET_HDR(sahdr, lot->lot_num,
7510a586ceaSMark Shellenbaum 		    buftype == SA_BONUS ? hdrsize : spillhdrsize);
7520a586ceaSMark Shellenbaum 	}
7530a586ceaSMark Shellenbaum 
7540a586ceaSMark Shellenbaum 	kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
7550a586ceaSMark Shellenbaum 	if (hdl->sa_bonus_tab) {
7560a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
7570a586ceaSMark Shellenbaum 		hdl->sa_bonus_tab = NULL;
7580a586ceaSMark Shellenbaum 	}
7590a586ceaSMark Shellenbaum 	if (!sa->sa_force_spill)
7600a586ceaSMark Shellenbaum 		VERIFY(0 == sa_build_index(hdl, SA_BONUS));
7610a586ceaSMark Shellenbaum 	if (hdl->sa_spill) {
7620a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
7630a586ceaSMark Shellenbaum 		if (!spilling) {
7640a586ceaSMark Shellenbaum 			/*
7650a586ceaSMark Shellenbaum 			 * remove spill block that is no longer needed.
7660a586ceaSMark Shellenbaum 			 * set sa_spill_remove to prevent sa_attr_op
7670a586ceaSMark Shellenbaum 			 * from trying to retrieve spill block before its
7680a586ceaSMark Shellenbaum 			 * been removed.  The flag will be cleared if/when
7690a586ceaSMark Shellenbaum 			 * the handle is destroyed recreated or
7700a586ceaSMark Shellenbaum 			 * sa_build_layouts() needs to spill again.
7710a586ceaSMark Shellenbaum 			 */
7720a586ceaSMark Shellenbaum 			dmu_buf_rele(hdl->sa_spill, NULL);
7730a586ceaSMark Shellenbaum 			hdl->sa_spill = NULL;
7740a586ceaSMark Shellenbaum 			hdl->sa_spill_tab = NULL;
7750a586ceaSMark Shellenbaum 			VERIFY(0 == dmu_rm_spill(hdl->sa_os,
7760a586ceaSMark Shellenbaum 			    sa_handle_object(hdl), tx));
7770a586ceaSMark Shellenbaum 		} else {
7780a586ceaSMark Shellenbaum 			VERIFY(0 == sa_build_index(hdl, SA_SPILL));
7790a586ceaSMark Shellenbaum 		}
7800a586ceaSMark Shellenbaum 	}
7810a586ceaSMark Shellenbaum 
7820a586ceaSMark Shellenbaum 	return (0);
7830a586ceaSMark Shellenbaum }
7840a586ceaSMark Shellenbaum 
7850a586ceaSMark Shellenbaum static void
7860a586ceaSMark Shellenbaum sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
7870a586ceaSMark Shellenbaum {
7880a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
7890a586ceaSMark Shellenbaum 	uint64_t sa_attr_count = 0;
7900a586ceaSMark Shellenbaum 	int error = 0;
7910a586ceaSMark Shellenbaum 	uint64_t attr_value;
7920a586ceaSMark Shellenbaum 	sa_attr_table_t *tb;
7930a586ceaSMark Shellenbaum 	zap_cursor_t zc;
7940a586ceaSMark Shellenbaum 	zap_attribute_t za;
7950a586ceaSMark Shellenbaum 	int registered_count = 0;
7960a586ceaSMark Shellenbaum 	int i;
7970a586ceaSMark Shellenbaum 	dmu_objset_type_t ostype = dmu_objset_type(os);
7980a586ceaSMark Shellenbaum 
7990a586ceaSMark Shellenbaum 	sa->sa_user_table =
8000a586ceaSMark Shellenbaum 	    kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
8010a586ceaSMark Shellenbaum 	sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
8020a586ceaSMark Shellenbaum 
8030a586ceaSMark Shellenbaum 	if (sa->sa_reg_attr_obj != 0)
8040a586ceaSMark Shellenbaum 		VERIFY(zap_count(os, sa->sa_reg_attr_obj, &sa_attr_count) == 0);
8050a586ceaSMark Shellenbaum 
8060a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
8070a586ceaSMark Shellenbaum 		sa_attr_count += sa_legacy_attr_count;
8080a586ceaSMark Shellenbaum 
8090a586ceaSMark Shellenbaum 	/* Allocate attribute numbers for attributes that aren't registered */
8100a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
8110a586ceaSMark Shellenbaum 		boolean_t found = B_FALSE;
8120a586ceaSMark Shellenbaum 		int j;
8130a586ceaSMark Shellenbaum 
8140a586ceaSMark Shellenbaum 		if (ostype == DMU_OST_ZFS) {
8150a586ceaSMark Shellenbaum 			for (j = 0; j != sa_legacy_attr_count; j++) {
8160a586ceaSMark Shellenbaum 				if (strcmp(reg_attrs[i].sa_name,
8170a586ceaSMark Shellenbaum 				    sa_legacy_attrs[j].sa_name) == 0) {
8180a586ceaSMark Shellenbaum 					sa->sa_user_table[i] =
8190a586ceaSMark Shellenbaum 					    sa_legacy_attrs[j].sa_attr;
8200a586ceaSMark Shellenbaum 					found = B_TRUE;
8210a586ceaSMark Shellenbaum 				}
8220a586ceaSMark Shellenbaum 			}
8230a586ceaSMark Shellenbaum 		}
8240a586ceaSMark Shellenbaum 		if (found)
8250a586ceaSMark Shellenbaum 			continue;
8260a586ceaSMark Shellenbaum 
8270a586ceaSMark Shellenbaum 		if (sa->sa_reg_attr_obj)
8280a586ceaSMark Shellenbaum 			error = zap_lookup(os, sa->sa_reg_attr_obj,
8290a586ceaSMark Shellenbaum 			    reg_attrs[i].sa_name, 8, 1, &attr_value);
8300a586ceaSMark Shellenbaum 		else
8310a586ceaSMark Shellenbaum 			error = ENOENT;
8320a586ceaSMark Shellenbaum 		switch (error) {
8330a586ceaSMark Shellenbaum 		default:
8340a586ceaSMark Shellenbaum 		case ENOENT:
8350a586ceaSMark Shellenbaum 			sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
8360a586ceaSMark Shellenbaum 			sa_attr_count++;
8370a586ceaSMark Shellenbaum 			break;
8380a586ceaSMark Shellenbaum 		case 0:
8390a586ceaSMark Shellenbaum 			sa->sa_user_table[i] = ATTR_NUM(attr_value);
8400a586ceaSMark Shellenbaum 			break;
8410a586ceaSMark Shellenbaum 		}
8420a586ceaSMark Shellenbaum 	}
8430a586ceaSMark Shellenbaum 
8440a586ceaSMark Shellenbaum 	os->os_sa->sa_num_attrs = sa_attr_count;
8450a586ceaSMark Shellenbaum 	tb = os->os_sa->sa_attr_table =
8460a586ceaSMark Shellenbaum 	    kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
8470a586ceaSMark Shellenbaum 
8480a586ceaSMark Shellenbaum 	/*
8490a586ceaSMark Shellenbaum 	 * Attribute table is constructed from requested attribute list,
8500a586ceaSMark Shellenbaum 	 * previously foreign registered attributes, and also the legacy
8510a586ceaSMark Shellenbaum 	 * ZPL set of attributes.
8520a586ceaSMark Shellenbaum 	 */
8530a586ceaSMark Shellenbaum 
8540a586ceaSMark Shellenbaum 	if (sa->sa_reg_attr_obj) {
8550a586ceaSMark Shellenbaum 		for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
8560a586ceaSMark Shellenbaum 		    zap_cursor_retrieve(&zc, &za) == 0;
8570a586ceaSMark Shellenbaum 		    zap_cursor_advance(&zc)) {
8580a586ceaSMark Shellenbaum 			uint64_t value;
8590a586ceaSMark Shellenbaum 			value  = za.za_first_integer;
8600a586ceaSMark Shellenbaum 
8610a586ceaSMark Shellenbaum 			registered_count++;
8620a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
8630a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
8640a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
8650a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_registered = B_TRUE;
8660a586ceaSMark Shellenbaum 
8670a586ceaSMark Shellenbaum 			if (tb[ATTR_NUM(value)].sa_name) {
8680a586ceaSMark Shellenbaum 				continue;
8690a586ceaSMark Shellenbaum 			}
8700a586ceaSMark Shellenbaum 			tb[ATTR_NUM(value)].sa_name =
8710a586ceaSMark Shellenbaum 			    kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
8720a586ceaSMark Shellenbaum 			(void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
8730a586ceaSMark Shellenbaum 			    strlen(za.za_name) +1);
8740a586ceaSMark Shellenbaum 		}
8750a586ceaSMark Shellenbaum 		zap_cursor_fini(&zc);
8760a586ceaSMark Shellenbaum 	}
8770a586ceaSMark Shellenbaum 
8780a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS) {
8790a586ceaSMark Shellenbaum 		for (i = 0; i != sa_legacy_attr_count; i++) {
8800a586ceaSMark Shellenbaum 			if (tb[i].sa_name)
8810a586ceaSMark Shellenbaum 				continue;
8820a586ceaSMark Shellenbaum 			tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
8830a586ceaSMark Shellenbaum 			tb[i].sa_length = sa_legacy_attrs[i].sa_length;
8840a586ceaSMark Shellenbaum 			tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
8850a586ceaSMark Shellenbaum 			tb[i].sa_registered = B_FALSE;
8860a586ceaSMark Shellenbaum 			tb[i].sa_name =
8870a586ceaSMark Shellenbaum 			    kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
8880a586ceaSMark Shellenbaum 			    KM_SLEEP);
8890a586ceaSMark Shellenbaum 			(void) strlcpy(tb[i].sa_name,
8900a586ceaSMark Shellenbaum 			    sa_legacy_attrs[i].sa_name,
8910a586ceaSMark Shellenbaum 			    strlen(sa_legacy_attrs[i].sa_name) + 1);
8920a586ceaSMark Shellenbaum 		}
8930a586ceaSMark Shellenbaum 	}
8940a586ceaSMark Shellenbaum 
8950a586ceaSMark Shellenbaum 	for (i = 0; i != count; i++) {
8960a586ceaSMark Shellenbaum 		sa_attr_type_t attr_id;
8970a586ceaSMark Shellenbaum 
8980a586ceaSMark Shellenbaum 		attr_id = sa->sa_user_table[i];
8990a586ceaSMark Shellenbaum 		if (tb[attr_id].sa_name)
9000a586ceaSMark Shellenbaum 			continue;
9010a586ceaSMark Shellenbaum 
9020a586ceaSMark Shellenbaum 		tb[attr_id].sa_length = reg_attrs[i].sa_length;
9030a586ceaSMark Shellenbaum 		tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
9040a586ceaSMark Shellenbaum 		tb[attr_id].sa_attr = attr_id;
9050a586ceaSMark Shellenbaum 		tb[attr_id].sa_name =
9060a586ceaSMark Shellenbaum 		    kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
9070a586ceaSMark Shellenbaum 		(void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
9080a586ceaSMark Shellenbaum 		    strlen(reg_attrs[i].sa_name) + 1);
9090a586ceaSMark Shellenbaum 	}
9100a586ceaSMark Shellenbaum 
9110a586ceaSMark Shellenbaum 	os->os_sa->sa_need_attr_registration =
9120a586ceaSMark Shellenbaum 	    (sa_attr_count != registered_count);
9130a586ceaSMark Shellenbaum }
9140a586ceaSMark Shellenbaum 
9150a586ceaSMark Shellenbaum sa_attr_type_t *
9160a586ceaSMark Shellenbaum sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count)
9170a586ceaSMark Shellenbaum {
9180a586ceaSMark Shellenbaum 	zap_cursor_t zc;
9190a586ceaSMark Shellenbaum 	zap_attribute_t za;
9200a586ceaSMark Shellenbaum 	sa_os_t *sa;
9210a586ceaSMark Shellenbaum 	dmu_objset_type_t ostype = dmu_objset_type(os);
9220a586ceaSMark Shellenbaum 	sa_attr_type_t *tb;
9230a586ceaSMark Shellenbaum 
9240a586ceaSMark Shellenbaum 	mutex_enter(&os->os_lock);
9250a586ceaSMark Shellenbaum 	if (os->os_sa) {
9260a586ceaSMark Shellenbaum 		mutex_enter(&os->os_sa->sa_lock);
9270a586ceaSMark Shellenbaum 		mutex_exit(&os->os_lock);
9280a586ceaSMark Shellenbaum 		tb = os->os_sa->sa_user_table;
9290a586ceaSMark Shellenbaum 		mutex_exit(&os->os_sa->sa_lock);
9300a586ceaSMark Shellenbaum 		return (tb);
9310a586ceaSMark Shellenbaum 	}
9320a586ceaSMark Shellenbaum 
9330a586ceaSMark Shellenbaum 	sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
9340a586ceaSMark Shellenbaum 	mutex_init(&sa->sa_lock, NULL, MUTEX_DEFAULT, NULL);
9350a586ceaSMark Shellenbaum 	sa->sa_master_obj = sa_obj;
9360a586ceaSMark Shellenbaum 
9370a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
9380a586ceaSMark Shellenbaum 	mutex_exit(&os->os_lock);
9390a586ceaSMark Shellenbaum 	avl_create(&sa->sa_layout_num_tree, layout_num_compare,
9400a586ceaSMark Shellenbaum 	    sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
9410a586ceaSMark Shellenbaum 	avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
9420a586ceaSMark Shellenbaum 	    sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
9430a586ceaSMark Shellenbaum 
9440a586ceaSMark Shellenbaum 	if (sa_obj) {
9450a586ceaSMark Shellenbaum 		int error;
9460a586ceaSMark Shellenbaum 		error = zap_lookup(os, sa_obj, SA_LAYOUTS,
9470a586ceaSMark Shellenbaum 		    8, 1, &sa->sa_layout_attr_obj);
9480a586ceaSMark Shellenbaum 		if (error != 0 && error != ENOENT) {
9490a586ceaSMark Shellenbaum 			return (NULL);
9500a586ceaSMark Shellenbaum 		}
9510a586ceaSMark Shellenbaum 		error = zap_lookup(os, sa_obj, SA_REGISTRY,
9520a586ceaSMark Shellenbaum 		    8, 1, &sa->sa_reg_attr_obj);
9530a586ceaSMark Shellenbaum 		if (error != 0 && error != ENOENT) {
9540a586ceaSMark Shellenbaum 			mutex_exit(&sa->sa_lock);
9550a586ceaSMark Shellenbaum 			return (NULL);
9560a586ceaSMark Shellenbaum 		}
9570a586ceaSMark Shellenbaum 	}
9580a586ceaSMark Shellenbaum 
9590a586ceaSMark Shellenbaum 	os->os_sa = sa;
9600a586ceaSMark Shellenbaum 	sa_attr_table_setup(os, reg_attrs, count);
9610a586ceaSMark Shellenbaum 
9620a586ceaSMark Shellenbaum 	if (sa->sa_layout_attr_obj != 0) {
9630a586ceaSMark Shellenbaum 		for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
9640a586ceaSMark Shellenbaum 		    zap_cursor_retrieve(&zc, &za) == 0;
9650a586ceaSMark Shellenbaum 		    zap_cursor_advance(&zc)) {
9660a586ceaSMark Shellenbaum 			sa_attr_type_t *lot_attrs;
9670a586ceaSMark Shellenbaum 			uint64_t lot_num;
9680a586ceaSMark Shellenbaum 
9690a586ceaSMark Shellenbaum 			lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
9700a586ceaSMark Shellenbaum 			    za.za_num_integers, KM_SLEEP);
9710a586ceaSMark Shellenbaum 
9720a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, sa->sa_layout_attr_obj,
9730a586ceaSMark Shellenbaum 			    za.za_name, 2, za.za_num_integers, lot_attrs) == 0);
9740a586ceaSMark Shellenbaum 			VERIFY(ddi_strtoull(za.za_name, NULL, 10,
9750a586ceaSMark Shellenbaum 			    (unsigned long long *)&lot_num) == 0);
9760a586ceaSMark Shellenbaum 
9770a586ceaSMark Shellenbaum 			(void) sa_add_layout_entry(os, lot_attrs,
9780a586ceaSMark Shellenbaum 			    za.za_num_integers, lot_num,
9790a586ceaSMark Shellenbaum 			    sa_layout_info_hash(lot_attrs,
9800a586ceaSMark Shellenbaum 			    za.za_num_integers), B_FALSE, NULL);
9810a586ceaSMark Shellenbaum 			kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
9820a586ceaSMark Shellenbaum 			    za.za_num_integers);
9830a586ceaSMark Shellenbaum 		}
9840a586ceaSMark Shellenbaum 		zap_cursor_fini(&zc);
9850a586ceaSMark Shellenbaum 	}
9860a586ceaSMark Shellenbaum 
9870a586ceaSMark Shellenbaum 	/* Add special layout number for old ZNODES */
9880a586ceaSMark Shellenbaum 	if (ostype == DMU_OST_ZFS) {
9890a586ceaSMark Shellenbaum 		(void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
9900a586ceaSMark Shellenbaum 		    sa_legacy_attr_count, 0,
9910a586ceaSMark Shellenbaum 		    sa_layout_info_hash(sa_legacy_zpl_layout,
9920a586ceaSMark Shellenbaum 		    sa_legacy_attr_count), B_FALSE, NULL);
9930a586ceaSMark Shellenbaum 
9940a586ceaSMark Shellenbaum 		(void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
9950a586ceaSMark Shellenbaum 		    0, B_FALSE, NULL);
9960a586ceaSMark Shellenbaum 	}
9970a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
9980a586ceaSMark Shellenbaum 	return (os->os_sa->sa_user_table);
9990a586ceaSMark Shellenbaum }
10000a586ceaSMark Shellenbaum 
10010a586ceaSMark Shellenbaum void
10020a586ceaSMark Shellenbaum sa_tear_down(objset_t *os)
10030a586ceaSMark Shellenbaum {
10040a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
10050a586ceaSMark Shellenbaum 	sa_lot_t *layout;
10060a586ceaSMark Shellenbaum 	void *cookie;
10070a586ceaSMark Shellenbaum 	int i;
10080a586ceaSMark Shellenbaum 
10090a586ceaSMark Shellenbaum 	kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
10100a586ceaSMark Shellenbaum 
10110a586ceaSMark Shellenbaum 	/* Free up attr table */
10120a586ceaSMark Shellenbaum 
10130a586ceaSMark Shellenbaum 	for (i = 0; i != sa->sa_num_attrs; i++) {
10140a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[i].sa_name)
10150a586ceaSMark Shellenbaum 			kmem_free(sa->sa_attr_table[i].sa_name,
10160a586ceaSMark Shellenbaum 			    strlen(sa->sa_attr_table[i].sa_name) + 1);
10170a586ceaSMark Shellenbaum 	}
10180a586ceaSMark Shellenbaum 
10190a586ceaSMark Shellenbaum 	kmem_free(sa->sa_attr_table,
10200a586ceaSMark Shellenbaum 	    sizeof (sa_attr_table_t) * sa->sa_num_attrs);
10210a586ceaSMark Shellenbaum 
10220a586ceaSMark Shellenbaum 	cookie = NULL;
10230a586ceaSMark Shellenbaum 	while (layout = avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie)) {
10240a586ceaSMark Shellenbaum 		sa_idx_tab_t *tab;
10250a586ceaSMark Shellenbaum 		while (tab = list_head(&layout->lot_idx_tab)) {
10260a586ceaSMark Shellenbaum 			ASSERT(refcount_count(&tab->sa_refcount));
10270a586ceaSMark Shellenbaum 			sa_idx_tab_rele(os, tab);
10280a586ceaSMark Shellenbaum 		}
10290a586ceaSMark Shellenbaum 	}
10300a586ceaSMark Shellenbaum 
10310a586ceaSMark Shellenbaum 	cookie = NULL;
10320a586ceaSMark Shellenbaum 	while (layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie)) {
10330a586ceaSMark Shellenbaum 		kmem_free(layout->lot_attrs,
10340a586ceaSMark Shellenbaum 		    sizeof (sa_attr_type_t) * layout->lot_attr_count);
10350a586ceaSMark Shellenbaum 		kmem_free(layout, sizeof (sa_lot_t));
10360a586ceaSMark Shellenbaum 	}
10370a586ceaSMark Shellenbaum 
10380a586ceaSMark Shellenbaum 	avl_destroy(&sa->sa_layout_hash_tree);
10390a586ceaSMark Shellenbaum 	avl_destroy(&sa->sa_layout_num_tree);
10400a586ceaSMark Shellenbaum 
10410a586ceaSMark Shellenbaum 	kmem_free(sa, sizeof (sa_os_t));
10420a586ceaSMark Shellenbaum 	os->os_sa = NULL;
10430a586ceaSMark Shellenbaum }
10440a586ceaSMark Shellenbaum 
10450a586ceaSMark Shellenbaum void
10460a586ceaSMark Shellenbaum sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
10470a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t var_length, void *userp)
10480a586ceaSMark Shellenbaum {
10490a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab = userp;
10500a586ceaSMark Shellenbaum 
10510a586ceaSMark Shellenbaum 	if (var_length) {
10520a586ceaSMark Shellenbaum 		ASSERT(idx_tab->sa_variable_lengths);
10530a586ceaSMark Shellenbaum 		idx_tab->sa_variable_lengths[length_idx] = length;
10540a586ceaSMark Shellenbaum 	}
10550a586ceaSMark Shellenbaum 	TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
10560a586ceaSMark Shellenbaum 	    (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
10570a586ceaSMark Shellenbaum }
10580a586ceaSMark Shellenbaum 
10590a586ceaSMark Shellenbaum static void
10600a586ceaSMark Shellenbaum sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
10610a586ceaSMark Shellenbaum     sa_iterfunc_t func, sa_lot_t *tab, void *userp)
10620a586ceaSMark Shellenbaum {
10630a586ceaSMark Shellenbaum 	void *data_start;
10640a586ceaSMark Shellenbaum 	sa_lot_t *tb = tab;
10650a586ceaSMark Shellenbaum 	sa_lot_t search;
10660a586ceaSMark Shellenbaum 	avl_index_t loc;
10670a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
10680a586ceaSMark Shellenbaum 	int i;
1069*06e0070dSMark Shellenbaum 	uint16_t *length_start = NULL;
10700a586ceaSMark Shellenbaum 	uint8_t length_idx = 0;
10710a586ceaSMark Shellenbaum 
10720a586ceaSMark Shellenbaum 	if (tab == NULL) {
10730a586ceaSMark Shellenbaum 		search.lot_num = SA_LAYOUT_NUM(hdr, type);
10740a586ceaSMark Shellenbaum 		tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
10750a586ceaSMark Shellenbaum 		ASSERT(tb);
10760a586ceaSMark Shellenbaum 	}
10770a586ceaSMark Shellenbaum 
10780a586ceaSMark Shellenbaum 	if (IS_SA_BONUSTYPE(type)) {
10790a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
10800a586ceaSMark Shellenbaum 		    offsetof(sa_hdr_phys_t, sa_lengths) +
10810a586ceaSMark Shellenbaum 		    (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
10820a586ceaSMark Shellenbaum 		length_start = hdr->sa_lengths;
10830a586ceaSMark Shellenbaum 	} else {
10840a586ceaSMark Shellenbaum 		data_start = hdr;
10850a586ceaSMark Shellenbaum 	}
10860a586ceaSMark Shellenbaum 
10870a586ceaSMark Shellenbaum 	for (i = 0; i != tb->lot_attr_count; i++) {
10880a586ceaSMark Shellenbaum 		int attr_length, reg_length;
10890a586ceaSMark Shellenbaum 		uint8_t idx_len;
10900a586ceaSMark Shellenbaum 
10910a586ceaSMark Shellenbaum 		reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
10920a586ceaSMark Shellenbaum 		if (reg_length) {
10930a586ceaSMark Shellenbaum 			attr_length = reg_length;
10940a586ceaSMark Shellenbaum 			idx_len = 0;
10950a586ceaSMark Shellenbaum 		} else {
10960a586ceaSMark Shellenbaum 			attr_length = length_start[length_idx];
10970a586ceaSMark Shellenbaum 			idx_len = length_idx++;
10980a586ceaSMark Shellenbaum 		}
10990a586ceaSMark Shellenbaum 
11000a586ceaSMark Shellenbaum 		func(hdr, data_start, tb->lot_attrs[i], attr_length,
11010a586ceaSMark Shellenbaum 		    idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
11020a586ceaSMark Shellenbaum 
11030a586ceaSMark Shellenbaum 		data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
11040a586ceaSMark Shellenbaum 		    attr_length), 8);
11050a586ceaSMark Shellenbaum 	}
11060a586ceaSMark Shellenbaum }
11070a586ceaSMark Shellenbaum 
11080a586ceaSMark Shellenbaum /*ARGSUSED*/
11090a586ceaSMark Shellenbaum void
11100a586ceaSMark Shellenbaum sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
11110a586ceaSMark Shellenbaum     uint16_t length, int length_idx, boolean_t variable_length, void *userp)
11120a586ceaSMark Shellenbaum {
11130a586ceaSMark Shellenbaum 	sa_handle_t *hdl = userp;
11140a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
11150a586ceaSMark Shellenbaum 
11160a586ceaSMark Shellenbaum 	sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
11170a586ceaSMark Shellenbaum }
11180a586ceaSMark Shellenbaum 
11190a586ceaSMark Shellenbaum void
11200a586ceaSMark Shellenbaum sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
11210a586ceaSMark Shellenbaum {
11220a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
11230a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db;
11240a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
11250a586ceaSMark Shellenbaum 	int num_lengths = 1;
11260a586ceaSMark Shellenbaum 	int i;
11270a586ceaSMark Shellenbaum 
11280a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
11290a586ceaSMark Shellenbaum 	if (sa_hdr_phys->sa_magic == SA_MAGIC)
11300a586ceaSMark Shellenbaum 		return;
11310a586ceaSMark Shellenbaum 
11320a586ceaSMark Shellenbaum 	db = SA_GET_DB(hdl, buftype);
11330a586ceaSMark Shellenbaum 
11340a586ceaSMark Shellenbaum 	if (buftype == SA_SPILL) {
11350a586ceaSMark Shellenbaum 		arc_release(db->db_buf, NULL);
11360a586ceaSMark Shellenbaum 		arc_buf_thaw(db->db_buf);
11370a586ceaSMark Shellenbaum 	}
11380a586ceaSMark Shellenbaum 
11390a586ceaSMark Shellenbaum 	sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
11400a586ceaSMark Shellenbaum 	sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
11410a586ceaSMark Shellenbaum 
11420a586ceaSMark Shellenbaum 	/*
11430a586ceaSMark Shellenbaum 	 * Determine number of variable lenghts in header
11440a586ceaSMark Shellenbaum 	 * The standard 8 byte header has one for free and a
11450a586ceaSMark Shellenbaum 	 * 16 byte header would have 4 + 1;
11460a586ceaSMark Shellenbaum 	 */
11470a586ceaSMark Shellenbaum 	if (SA_HDR_SIZE(sa_hdr_phys) > 8)
11480a586ceaSMark Shellenbaum 		num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
11490a586ceaSMark Shellenbaum 	for (i = 0; i != num_lengths; i++)
11500a586ceaSMark Shellenbaum 		sa_hdr_phys->sa_lengths[i] =
11510a586ceaSMark Shellenbaum 		    BSWAP_16(sa_hdr_phys->sa_lengths[i]);
11520a586ceaSMark Shellenbaum 
11530a586ceaSMark Shellenbaum 	sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
11540a586ceaSMark Shellenbaum 	    sa_byteswap_cb, NULL, hdl);
11550a586ceaSMark Shellenbaum 
11560a586ceaSMark Shellenbaum 	if (buftype == SA_SPILL)
11570a586ceaSMark Shellenbaum 		arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
11580a586ceaSMark Shellenbaum }
11590a586ceaSMark Shellenbaum 
11600a586ceaSMark Shellenbaum static int
11610a586ceaSMark Shellenbaum sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
11620a586ceaSMark Shellenbaum {
11630a586ceaSMark Shellenbaum 	sa_hdr_phys_t *sa_hdr_phys;
11640a586ceaSMark Shellenbaum 	dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
11650a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
11660a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
11670a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
11680a586ceaSMark Shellenbaum 
11690a586ceaSMark Shellenbaum 	sa_hdr_phys = SA_GET_HDR(hdl, buftype);
11700a586ceaSMark Shellenbaum 
11710a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
11720a586ceaSMark Shellenbaum 
11730a586ceaSMark Shellenbaum 	/* Do we need to byteswap? */
11740a586ceaSMark Shellenbaum 
11750a586ceaSMark Shellenbaum 	/* only check if not old znode */
11760a586ceaSMark Shellenbaum 	if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
11770a586ceaSMark Shellenbaum 	    sa_hdr_phys->sa_magic != 0) {
11780a586ceaSMark Shellenbaum 		VERIFY(BSWAP_32(sa_hdr_phys->sa_magic) == SA_MAGIC);
11790a586ceaSMark Shellenbaum 		sa_byteswap(hdl, buftype);
11800a586ceaSMark Shellenbaum 	}
11810a586ceaSMark Shellenbaum 
11820a586ceaSMark Shellenbaum 	idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
11830a586ceaSMark Shellenbaum 
11840a586ceaSMark Shellenbaum 	if (buftype == SA_BONUS)
11850a586ceaSMark Shellenbaum 		hdl->sa_bonus_tab = idx_tab;
11860a586ceaSMark Shellenbaum 	else
11870a586ceaSMark Shellenbaum 		hdl->sa_spill_tab = idx_tab;
11880a586ceaSMark Shellenbaum 
11890a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
11900a586ceaSMark Shellenbaum 	return (0);
11910a586ceaSMark Shellenbaum }
11920a586ceaSMark Shellenbaum 
11930a586ceaSMark Shellenbaum /*ARGSUSED*/
11940a586ceaSMark Shellenbaum void
11950a586ceaSMark Shellenbaum sa_evict(dmu_buf_t *db, void *sap)
11960a586ceaSMark Shellenbaum {
11970a586ceaSMark Shellenbaum 	panic("evicting sa dbuf %p\n", (void *)db);
11980a586ceaSMark Shellenbaum }
11990a586ceaSMark Shellenbaum 
12000a586ceaSMark Shellenbaum static void
12010a586ceaSMark Shellenbaum sa_idx_tab_rele(objset_t *os, void *arg)
12020a586ceaSMark Shellenbaum {
12030a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
12040a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab = arg;
12050a586ceaSMark Shellenbaum 
12060a586ceaSMark Shellenbaum 	if (idx_tab == NULL)
12070a586ceaSMark Shellenbaum 		return;
12080a586ceaSMark Shellenbaum 
12090a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
12100a586ceaSMark Shellenbaum 	if (refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
12110a586ceaSMark Shellenbaum 		list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
12120a586ceaSMark Shellenbaum 		if (idx_tab->sa_variable_lengths)
12130a586ceaSMark Shellenbaum 			kmem_free(idx_tab->sa_variable_lengths,
12140a586ceaSMark Shellenbaum 			    sizeof (uint16_t) *
12150a586ceaSMark Shellenbaum 			    idx_tab->sa_layout->lot_var_sizes);
12160a586ceaSMark Shellenbaum 		refcount_destroy(&idx_tab->sa_refcount);
12170a586ceaSMark Shellenbaum 		kmem_free(idx_tab->sa_idx_tab,
12180a586ceaSMark Shellenbaum 		    sizeof (uint32_t) * sa->sa_num_attrs);
12190a586ceaSMark Shellenbaum 		kmem_free(idx_tab, sizeof (sa_idx_tab_t));
12200a586ceaSMark Shellenbaum 	}
12210a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
12220a586ceaSMark Shellenbaum }
12230a586ceaSMark Shellenbaum 
12240a586ceaSMark Shellenbaum static void
12250a586ceaSMark Shellenbaum sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
12260a586ceaSMark Shellenbaum {
12270a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
12280a586ceaSMark Shellenbaum 
12290a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&sa->sa_lock));
12300a586ceaSMark Shellenbaum 	(void) refcount_add(&idx_tab->sa_refcount, NULL);
12310a586ceaSMark Shellenbaum }
12320a586ceaSMark Shellenbaum 
12330a586ceaSMark Shellenbaum void
12340a586ceaSMark Shellenbaum sa_handle_destroy(sa_handle_t *hdl)
12350a586ceaSMark Shellenbaum {
12360a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
12370a586ceaSMark Shellenbaum 	(void) dmu_buf_update_user((dmu_buf_t *)hdl->sa_bonus, hdl,
12380a586ceaSMark Shellenbaum 	    NULL, NULL, NULL);
12390a586ceaSMark Shellenbaum 
12400a586ceaSMark Shellenbaum 	if (hdl->sa_bonus_tab) {
12410a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
12420a586ceaSMark Shellenbaum 		hdl->sa_bonus_tab = NULL;
12430a586ceaSMark Shellenbaum 	}
12440a586ceaSMark Shellenbaum 	if (hdl->sa_spill_tab) {
12450a586ceaSMark Shellenbaum 		sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
12460a586ceaSMark Shellenbaum 		hdl->sa_spill_tab = NULL;
12470a586ceaSMark Shellenbaum 	}
12480a586ceaSMark Shellenbaum 
12490a586ceaSMark Shellenbaum 	dmu_buf_rele(hdl->sa_bonus, NULL);
12500a586ceaSMark Shellenbaum 
12510a586ceaSMark Shellenbaum 	if (hdl->sa_spill)
12520a586ceaSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
12530a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
12540a586ceaSMark Shellenbaum 
12550a586ceaSMark Shellenbaum 	kmem_cache_free(sa_cache, hdl);
12560a586ceaSMark Shellenbaum }
12570a586ceaSMark Shellenbaum 
12580a586ceaSMark Shellenbaum int
12590a586ceaSMark Shellenbaum sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
12600a586ceaSMark Shellenbaum     sa_handle_type_t hdl_type, sa_handle_t **handlepp)
12610a586ceaSMark Shellenbaum {
12620a586ceaSMark Shellenbaum 	int error = 0;
12630a586ceaSMark Shellenbaum 	dmu_object_info_t doi;
12640a586ceaSMark Shellenbaum 	sa_handle_t *handle;
12650a586ceaSMark Shellenbaum 
12660a586ceaSMark Shellenbaum #ifdef ZFS_DEBUG
12670a586ceaSMark Shellenbaum 	dmu_object_info_from_db(db, &doi);
12680a586ceaSMark Shellenbaum 	ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
12690a586ceaSMark Shellenbaum 	    doi.doi_bonus_type == DMU_OT_ZNODE);
12700a586ceaSMark Shellenbaum #endif
12710a586ceaSMark Shellenbaum 	/* find handle, if it exists */
12720a586ceaSMark Shellenbaum 	/* if one doesn't exist then create a new one, and initialize it */
12730a586ceaSMark Shellenbaum 
12740a586ceaSMark Shellenbaum 	handle = (hdl_type == SA_HDL_SHARED) ? dmu_buf_get_user(db) : NULL;
12750a586ceaSMark Shellenbaum 	if (handle == NULL) {
12760a586ceaSMark Shellenbaum 		sa_handle_t *newhandle;
12770a586ceaSMark Shellenbaum 		handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
12780a586ceaSMark Shellenbaum 		handle->sa_userp = userp;
12790a586ceaSMark Shellenbaum 		handle->sa_bonus = db;
12800a586ceaSMark Shellenbaum 		handle->sa_os = os;
12810a586ceaSMark Shellenbaum 		handle->sa_spill = NULL;
12820a586ceaSMark Shellenbaum 
12830a586ceaSMark Shellenbaum 		error = sa_build_index(handle, SA_BONUS);
12840a586ceaSMark Shellenbaum 		newhandle = (hdl_type == SA_HDL_SHARED) ?
12850a586ceaSMark Shellenbaum 		    dmu_buf_set_user_ie(db, handle,
12860a586ceaSMark Shellenbaum 		    NULL, sa_evict) : NULL;
12870a586ceaSMark Shellenbaum 
12880a586ceaSMark Shellenbaum 		if (newhandle != NULL) {
12890a586ceaSMark Shellenbaum 			kmem_cache_free(sa_cache, handle);
12900a586ceaSMark Shellenbaum 			handle = newhandle;
12910a586ceaSMark Shellenbaum 		}
12920a586ceaSMark Shellenbaum 	}
12930a586ceaSMark Shellenbaum 	*handlepp = handle;
12940a586ceaSMark Shellenbaum 
12950a586ceaSMark Shellenbaum 	return (error);
12960a586ceaSMark Shellenbaum }
12970a586ceaSMark Shellenbaum 
12980a586ceaSMark Shellenbaum int
12990a586ceaSMark Shellenbaum sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
13000a586ceaSMark Shellenbaum     sa_handle_type_t hdl_type, sa_handle_t **handlepp)
13010a586ceaSMark Shellenbaum {
13020a586ceaSMark Shellenbaum 	dmu_buf_t *db;
13030a586ceaSMark Shellenbaum 	int error;
13040a586ceaSMark Shellenbaum 
13050a586ceaSMark Shellenbaum 	if (error = dmu_bonus_hold(objset, objid, NULL, &db))
13060a586ceaSMark Shellenbaum 		return (error);
13070a586ceaSMark Shellenbaum 
13080a586ceaSMark Shellenbaum 	return (sa_handle_get_from_db(objset, db, userp, hdl_type,
13090a586ceaSMark Shellenbaum 	    handlepp));
13100a586ceaSMark Shellenbaum }
13110a586ceaSMark Shellenbaum 
13120a586ceaSMark Shellenbaum int
13130a586ceaSMark Shellenbaum sa_buf_hold(objset_t *objset, uint64_t obj_num, void *tag, dmu_buf_t **db)
13140a586ceaSMark Shellenbaum {
13150a586ceaSMark Shellenbaum 	return (dmu_bonus_hold(objset, obj_num, tag, db));
13160a586ceaSMark Shellenbaum }
13170a586ceaSMark Shellenbaum 
13180a586ceaSMark Shellenbaum void
13190a586ceaSMark Shellenbaum sa_buf_rele(dmu_buf_t *db, void *tag)
13200a586ceaSMark Shellenbaum {
13210a586ceaSMark Shellenbaum 	dmu_buf_rele(db, tag);
13220a586ceaSMark Shellenbaum }
13230a586ceaSMark Shellenbaum 
13240a586ceaSMark Shellenbaum int
13250a586ceaSMark Shellenbaum sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
13260a586ceaSMark Shellenbaum {
13270a586ceaSMark Shellenbaum 	ASSERT(hdl);
13280a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
13290a586ceaSMark Shellenbaum 	return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
13300a586ceaSMark Shellenbaum }
13310a586ceaSMark Shellenbaum 
13320a586ceaSMark Shellenbaum int
13330a586ceaSMark Shellenbaum sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
13340a586ceaSMark Shellenbaum {
13350a586ceaSMark Shellenbaum 	int error;
13360a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
13370a586ceaSMark Shellenbaum 
13380a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
13390a586ceaSMark Shellenbaum 	bulk.sa_data = buf;
13400a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
13410a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
13420a586ceaSMark Shellenbaum 
13430a586ceaSMark Shellenbaum 	ASSERT(hdl);
13440a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
13450a586ceaSMark Shellenbaum 	error = sa_lookup_impl(hdl, &bulk, 1);
13460a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
13470a586ceaSMark Shellenbaum 	return (error);
13480a586ceaSMark Shellenbaum }
13490a586ceaSMark Shellenbaum 
13500a586ceaSMark Shellenbaum #ifdef _KERNEL
13510a586ceaSMark Shellenbaum int
13520a586ceaSMark Shellenbaum sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, uio_t *uio)
13530a586ceaSMark Shellenbaum {
13540a586ceaSMark Shellenbaum 	int error;
13550a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
13560a586ceaSMark Shellenbaum 
13570a586ceaSMark Shellenbaum 	bulk.sa_data = NULL;
13580a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
13590a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
13600a586ceaSMark Shellenbaum 
13610a586ceaSMark Shellenbaum 	ASSERT(hdl);
13620a586ceaSMark Shellenbaum 
13630a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
13640a586ceaSMark Shellenbaum 	if (sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL) == 0) {
13650a586ceaSMark Shellenbaum 		error = uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
13660a586ceaSMark Shellenbaum 		    uio->uio_resid), UIO_READ, uio);
13670a586ceaSMark Shellenbaum 	} else {
13680a586ceaSMark Shellenbaum 		error = ENOENT;
13690a586ceaSMark Shellenbaum 	}
13700a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
13710a586ceaSMark Shellenbaum 	return (error);
13720a586ceaSMark Shellenbaum 
13730a586ceaSMark Shellenbaum }
13740a586ceaSMark Shellenbaum #endif
13750a586ceaSMark Shellenbaum 
13760a586ceaSMark Shellenbaum /*
13770a586ceaSMark Shellenbaum  * Find an already existing TOC from given os and data
13780a586ceaSMark Shellenbaum  * This is a special interface to be used by the ZPL for
13790a586ceaSMark Shellenbaum  * finding the uid/gid/gen attributes.
13800a586ceaSMark Shellenbaum  */
13810a586ceaSMark Shellenbaum void *
13820a586ceaSMark Shellenbaum sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, void *data)
13830a586ceaSMark Shellenbaum {
13840a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
13850a586ceaSMark Shellenbaum 	sa_hdr_phys_t *hdr = (sa_hdr_phys_t *)data;
13860a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
13870a586ceaSMark Shellenbaum 	sa_lot_t *tb, search;
13880a586ceaSMark Shellenbaum 	avl_index_t loc;
13890a586ceaSMark Shellenbaum 
13900a586ceaSMark Shellenbaum 	/*
13910a586ceaSMark Shellenbaum 	 * Deterimine layout number.  If SA node and header == 0 then
13920a586ceaSMark Shellenbaum 	 * force the index table to the dummy "1" empty layout.
13930a586ceaSMark Shellenbaum 	 *
13940a586ceaSMark Shellenbaum 	 * The layout number would only be zero for a newly created file
13950a586ceaSMark Shellenbaum 	 * that has not added any attributes yet, or with crypto enabled which
13960a586ceaSMark Shellenbaum 	 * doesn't write any attributes to the bonus buffer.
13970a586ceaSMark Shellenbaum 	 */
13980a586ceaSMark Shellenbaum 
13990a586ceaSMark Shellenbaum 	search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
14000a586ceaSMark Shellenbaum 
14010a586ceaSMark Shellenbaum 	tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
14020a586ceaSMark Shellenbaum 
14030a586ceaSMark Shellenbaum 	/* Verify header size is consistent with layout information */
14040a586ceaSMark Shellenbaum 	ASSERT(tb);
14050a586ceaSMark Shellenbaum 	ASSERT(IS_SA_BONUSTYPE(bonustype) &&
14060a586ceaSMark Shellenbaum 	    SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) || !IS_SA_BONUSTYPE(bonustype) ||
14070a586ceaSMark Shellenbaum 	    (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
14080a586ceaSMark Shellenbaum 
14090a586ceaSMark Shellenbaum 	/*
14100a586ceaSMark Shellenbaum 	 * See if any of the already existing TOC entries can be reused?
14110a586ceaSMark Shellenbaum 	 */
14120a586ceaSMark Shellenbaum 
14130a586ceaSMark Shellenbaum 	for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
14140a586ceaSMark Shellenbaum 	    idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
14150a586ceaSMark Shellenbaum 		boolean_t valid_idx = B_TRUE;
14160a586ceaSMark Shellenbaum 		int i;
14170a586ceaSMark Shellenbaum 
14180a586ceaSMark Shellenbaum 		if (tb->lot_var_sizes != 0 &&
14190a586ceaSMark Shellenbaum 		    idx_tab->sa_variable_lengths != NULL) {
14200a586ceaSMark Shellenbaum 			for (i = 0; i != tb->lot_var_sizes; i++) {
14210a586ceaSMark Shellenbaum 				if (hdr->sa_lengths[i] !=
14220a586ceaSMark Shellenbaum 				    idx_tab->sa_variable_lengths[i]) {
14230a586ceaSMark Shellenbaum 					valid_idx = B_FALSE;
14240a586ceaSMark Shellenbaum 					break;
14250a586ceaSMark Shellenbaum 				}
14260a586ceaSMark Shellenbaum 			}
14270a586ceaSMark Shellenbaum 		}
14280a586ceaSMark Shellenbaum 		if (valid_idx) {
14290a586ceaSMark Shellenbaum 			sa_idx_tab_hold(os, idx_tab);
14300a586ceaSMark Shellenbaum 			return (idx_tab);
14310a586ceaSMark Shellenbaum 		}
14320a586ceaSMark Shellenbaum 	}
14330a586ceaSMark Shellenbaum 
14340a586ceaSMark Shellenbaum 	/* No such luck, create a new entry */
14350a586ceaSMark Shellenbaum 	idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
14360a586ceaSMark Shellenbaum 	idx_tab->sa_idx_tab =
14370a586ceaSMark Shellenbaum 	    kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
14380a586ceaSMark Shellenbaum 	idx_tab->sa_layout = tb;
14390a586ceaSMark Shellenbaum 	refcount_create(&idx_tab->sa_refcount);
14400a586ceaSMark Shellenbaum 	if (tb->lot_var_sizes)
14410a586ceaSMark Shellenbaum 		idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
14420a586ceaSMark Shellenbaum 		    tb->lot_var_sizes, KM_SLEEP);
14430a586ceaSMark Shellenbaum 
14440a586ceaSMark Shellenbaum 	sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
14450a586ceaSMark Shellenbaum 	    tb, idx_tab);
14460a586ceaSMark Shellenbaum 	sa_idx_tab_hold(os, idx_tab);   /* one hold for consumer */
14470a586ceaSMark Shellenbaum 	sa_idx_tab_hold(os, idx_tab);	/* one for layout */
14480a586ceaSMark Shellenbaum 	list_insert_tail(&tb->lot_idx_tab, idx_tab);
14490a586ceaSMark Shellenbaum 	return (idx_tab);
14500a586ceaSMark Shellenbaum }
14510a586ceaSMark Shellenbaum 
14520a586ceaSMark Shellenbaum void
14530a586ceaSMark Shellenbaum sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
14540a586ceaSMark Shellenbaum     boolean_t start, void *userdata)
14550a586ceaSMark Shellenbaum {
14560a586ceaSMark Shellenbaum 	ASSERT(start);
14570a586ceaSMark Shellenbaum 
14580a586ceaSMark Shellenbaum 	*dataptr = userdata;
14590a586ceaSMark Shellenbaum 	*len = total_len;
14600a586ceaSMark Shellenbaum }
14610a586ceaSMark Shellenbaum 
14620a586ceaSMark Shellenbaum static void
14630a586ceaSMark Shellenbaum sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
14640a586ceaSMark Shellenbaum {
14650a586ceaSMark Shellenbaum 	uint64_t attr_value = 0;
14660a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
14670a586ceaSMark Shellenbaum 	sa_attr_table_t *tb = sa->sa_attr_table;
14680a586ceaSMark Shellenbaum 	int i;
14690a586ceaSMark Shellenbaum 
14700a586ceaSMark Shellenbaum 	mutex_enter(&sa->sa_lock);
14710a586ceaSMark Shellenbaum 
14720a586ceaSMark Shellenbaum 	if (!sa->sa_need_attr_registration || sa->sa_master_obj == NULL) {
14730a586ceaSMark Shellenbaum 		mutex_exit(&sa->sa_lock);
14740a586ceaSMark Shellenbaum 		return;
14750a586ceaSMark Shellenbaum 	}
14760a586ceaSMark Shellenbaum 
14770a586ceaSMark Shellenbaum 	if (sa->sa_reg_attr_obj == NULL) {
14780a586ceaSMark Shellenbaum 		int error;
14790a586ceaSMark Shellenbaum 		sa->sa_reg_attr_obj = zap_create(hdl->sa_os,
14800a586ceaSMark Shellenbaum 		    DMU_OT_SA_ATTR_REGISTRATION, DMU_OT_NONE, 0, tx);
14810a586ceaSMark Shellenbaum 		error = zap_add(hdl->sa_os, sa->sa_master_obj,
14820a586ceaSMark Shellenbaum 		    SA_REGISTRY, 8, 1, &sa->sa_reg_attr_obj, tx);
14830a586ceaSMark Shellenbaum 		ASSERT(error == 0);
14840a586ceaSMark Shellenbaum 	}
14850a586ceaSMark Shellenbaum 	for (i = 0; i != sa->sa_num_attrs; i++) {
14860a586ceaSMark Shellenbaum 		if (sa->sa_attr_table[i].sa_registered)
14870a586ceaSMark Shellenbaum 			continue;
14880a586ceaSMark Shellenbaum 		ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
14890a586ceaSMark Shellenbaum 		    tb[i].sa_byteswap);
14900a586ceaSMark Shellenbaum 		VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
14910a586ceaSMark Shellenbaum 		    tb[i].sa_name, 8, 1, &attr_value, tx));
14920a586ceaSMark Shellenbaum 		tb[i].sa_registered = B_TRUE;
14930a586ceaSMark Shellenbaum 	}
14940a586ceaSMark Shellenbaum 	sa->sa_need_attr_registration = B_FALSE;
14950a586ceaSMark Shellenbaum 	mutex_exit(&sa->sa_lock);
14960a586ceaSMark Shellenbaum }
14970a586ceaSMark Shellenbaum 
14980a586ceaSMark Shellenbaum /*
14990a586ceaSMark Shellenbaum  * Replace all attributes with attributes specified in template.
15000a586ceaSMark Shellenbaum  * If dnode had a spill buffer then those attributes will be
15010a586ceaSMark Shellenbaum  * also be replaced, possibly with just an empty spill block
15020a586ceaSMark Shellenbaum  *
15030a586ceaSMark Shellenbaum  * This interface is intended to only be used for bulk adding of
15040a586ceaSMark Shellenbaum  * attributes for a new file.  It will also be used by the ZPL
15050a586ceaSMark Shellenbaum  * when converting and old formatted znode to native SA support.
15060a586ceaSMark Shellenbaum  */
15070a586ceaSMark Shellenbaum int
15080a586ceaSMark Shellenbaum sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
15090a586ceaSMark Shellenbaum     int attr_count, dmu_tx_t *tx)
15100a586ceaSMark Shellenbaum {
15110a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
15120a586ceaSMark Shellenbaum 
15130a586ceaSMark Shellenbaum 	if (sa->sa_need_attr_registration)
15140a586ceaSMark Shellenbaum 		sa_attr_register_sync(hdl, tx);
15150a586ceaSMark Shellenbaum 	return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
15160a586ceaSMark Shellenbaum }
15170a586ceaSMark Shellenbaum 
15180a586ceaSMark Shellenbaum int
15190a586ceaSMark Shellenbaum sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
15200a586ceaSMark Shellenbaum     int attr_count, dmu_tx_t *tx)
15210a586ceaSMark Shellenbaum {
15220a586ceaSMark Shellenbaum 	int error;
15230a586ceaSMark Shellenbaum 
15240a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
15250a586ceaSMark Shellenbaum 	error = sa_replace_all_by_template_locked(hdl, attr_desc,
15260a586ceaSMark Shellenbaum 	    attr_count, tx);
15270a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
15280a586ceaSMark Shellenbaum 	return (error);
15290a586ceaSMark Shellenbaum }
15300a586ceaSMark Shellenbaum 
15310a586ceaSMark Shellenbaum /*
15320a586ceaSMark Shellenbaum  * add/remove/replace a single attribute and then rewrite the entire set
15330a586ceaSMark Shellenbaum  * of attributes.
15340a586ceaSMark Shellenbaum  */
15350a586ceaSMark Shellenbaum static int
15360a586ceaSMark Shellenbaum sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
15370a586ceaSMark Shellenbaum     sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
15380a586ceaSMark Shellenbaum     uint16_t buflen, dmu_tx_t *tx)
15390a586ceaSMark Shellenbaum {
15400a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
15410a586ceaSMark Shellenbaum 	sa_bulk_attr_t *attr_desc;
15420a586ceaSMark Shellenbaum 	void *old_data[2];
15430a586ceaSMark Shellenbaum 	int bonus_attr_count = 0;
15440a586ceaSMark Shellenbaum 	int bonus_data_size, spill_data_size;
15450a586ceaSMark Shellenbaum 	int spill_attr_count = 0;
15460a586ceaSMark Shellenbaum 	int error;
15470a586ceaSMark Shellenbaum 	uint16_t length;
15480a586ceaSMark Shellenbaum 	int i, j, k, length_idx;
15490a586ceaSMark Shellenbaum 	sa_hdr_phys_t *hdr;
15500a586ceaSMark Shellenbaum 	sa_idx_tab_t *idx_tab;
15510a586ceaSMark Shellenbaum 	int attr_count;
15520a586ceaSMark Shellenbaum 	int count;
15530a586ceaSMark Shellenbaum 
15540a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
15550a586ceaSMark Shellenbaum 
15560a586ceaSMark Shellenbaum 	/* First make of copy of the old data */
15570a586ceaSMark Shellenbaum 
15580a586ceaSMark Shellenbaum 	if (((dmu_buf_impl_t *)hdl->sa_bonus)->db_dnode->dn_bonuslen) {
15590a586ceaSMark Shellenbaum 		bonus_data_size = hdl->sa_bonus->db_size;
15600a586ceaSMark Shellenbaum 		old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
15610a586ceaSMark Shellenbaum 		bcopy(hdl->sa_bonus->db_data, old_data[0],
15620a586ceaSMark Shellenbaum 		    hdl->sa_bonus->db_size);
15630a586ceaSMark Shellenbaum 		bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
15640a586ceaSMark Shellenbaum 	} else {
15650a586ceaSMark Shellenbaum 		old_data[0] = NULL;
15660a586ceaSMark Shellenbaum 	}
15670a586ceaSMark Shellenbaum 
15680a586ceaSMark Shellenbaum 	/* Bring spill buffer online if it isn't currently */
15690a586ceaSMark Shellenbaum 
15700a586ceaSMark Shellenbaum 	if (sa_has_blkptr(hdl)) {
15710a586ceaSMark Shellenbaum 		spill_data_size = hdl->sa_spill->db_size;
15720a586ceaSMark Shellenbaum 		old_data[1] = kmem_alloc(spill_data_size, KM_SLEEP);
15730a586ceaSMark Shellenbaum 		bcopy(hdl->sa_spill->db_data, old_data[1],
15740a586ceaSMark Shellenbaum 		    hdl->sa_spill->db_size);
15750a586ceaSMark Shellenbaum 		spill_attr_count =
15760a586ceaSMark Shellenbaum 		    hdl->sa_spill_tab->sa_layout->lot_attr_count;
15770a586ceaSMark Shellenbaum 	} else {
15780a586ceaSMark Shellenbaum 		old_data[1] = NULL;
15790a586ceaSMark Shellenbaum 	}
15800a586ceaSMark Shellenbaum 
15810a586ceaSMark Shellenbaum 	/* build descriptor of all attributes */
15820a586ceaSMark Shellenbaum 
15830a586ceaSMark Shellenbaum 	attr_count = bonus_attr_count + spill_attr_count;
15840a586ceaSMark Shellenbaum 	if (action == SA_ADD)
15850a586ceaSMark Shellenbaum 		attr_count++;
15860a586ceaSMark Shellenbaum 	else if (action == SA_REMOVE)
15870a586ceaSMark Shellenbaum 		attr_count--;
15880a586ceaSMark Shellenbaum 
15890a586ceaSMark Shellenbaum 	attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
15900a586ceaSMark Shellenbaum 
15910a586ceaSMark Shellenbaum 	/*
15920a586ceaSMark Shellenbaum 	 * loop through bonus and spill buffer if it exists, and
15930a586ceaSMark Shellenbaum 	 * build up new attr_descriptor to reset the attributes
15940a586ceaSMark Shellenbaum 	 */
15950a586ceaSMark Shellenbaum 	k = j = 0;
15960a586ceaSMark Shellenbaum 	count = bonus_attr_count;
15970a586ceaSMark Shellenbaum 	hdr = SA_GET_HDR(hdl, SA_BONUS);
15980a586ceaSMark Shellenbaum 	idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
15990a586ceaSMark Shellenbaum 	for (; k != 2; k++) {
16000a586ceaSMark Shellenbaum 		/* iterate over each attribute in layout */
16010a586ceaSMark Shellenbaum 		for (i = 0, length_idx = 0; i != count; i++) {
16020a586ceaSMark Shellenbaum 			sa_attr_type_t attr;
16030a586ceaSMark Shellenbaum 
16040a586ceaSMark Shellenbaum 			attr = idx_tab->sa_layout->lot_attrs[i];
16050a586ceaSMark Shellenbaum 			if (attr == newattr) {
16060a586ceaSMark Shellenbaum 				if (action == SA_REMOVE) {
16070a586ceaSMark Shellenbaum 					j++;
16080a586ceaSMark Shellenbaum 					continue;
16090a586ceaSMark Shellenbaum 				}
16100a586ceaSMark Shellenbaum 				ASSERT(SA_REGISTERED_LEN(sa, attr) == 0);
16110a586ceaSMark Shellenbaum 				ASSERT(action == SA_REPLACE);
16120a586ceaSMark Shellenbaum 				SA_ADD_BULK_ATTR(attr_desc, j, attr,
16130a586ceaSMark Shellenbaum 				    locator, datastart, buflen);
16140a586ceaSMark Shellenbaum 			} else {
16150a586ceaSMark Shellenbaum 				length = SA_REGISTERED_LEN(sa, attr);
16160a586ceaSMark Shellenbaum 				if (length == 0) {
16170a586ceaSMark Shellenbaum 					length = hdr->sa_lengths[length_idx++];
16180a586ceaSMark Shellenbaum 				}
16190a586ceaSMark Shellenbaum 
16200a586ceaSMark Shellenbaum 				SA_ADD_BULK_ATTR(attr_desc, j, attr,
16210a586ceaSMark Shellenbaum 				    NULL, (void *)
16220a586ceaSMark Shellenbaum 				    (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
16230a586ceaSMark Shellenbaum 				    (uintptr_t)old_data[k]), length);
16240a586ceaSMark Shellenbaum 			}
16250a586ceaSMark Shellenbaum 		}
16260a586ceaSMark Shellenbaum 		if (k == 0 && hdl->sa_spill) {
16270a586ceaSMark Shellenbaum 			hdr = SA_GET_HDR(hdl, SA_SPILL);
16280a586ceaSMark Shellenbaum 			idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
16290a586ceaSMark Shellenbaum 			count = spill_attr_count;
16300a586ceaSMark Shellenbaum 		} else {
16310a586ceaSMark Shellenbaum 			break;
16320a586ceaSMark Shellenbaum 		}
16330a586ceaSMark Shellenbaum 	}
16340a586ceaSMark Shellenbaum 	if (action == SA_ADD) {
16350a586ceaSMark Shellenbaum 		length = SA_REGISTERED_LEN(sa, newattr);
16360a586ceaSMark Shellenbaum 		if (length == 0) {
16370a586ceaSMark Shellenbaum 			length = buflen;
16380a586ceaSMark Shellenbaum 		}
16390a586ceaSMark Shellenbaum 		SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
16400a586ceaSMark Shellenbaum 		    datastart, buflen);
16410a586ceaSMark Shellenbaum 	}
16420a586ceaSMark Shellenbaum 
16430a586ceaSMark Shellenbaum 	error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
16440a586ceaSMark Shellenbaum 
16450a586ceaSMark Shellenbaum 	if (old_data[0])
16460a586ceaSMark Shellenbaum 		kmem_free(old_data[0], bonus_data_size);
16470a586ceaSMark Shellenbaum 	if (old_data[1])
16480a586ceaSMark Shellenbaum 		kmem_free(old_data[1], spill_data_size);
16490a586ceaSMark Shellenbaum 	kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
16500a586ceaSMark Shellenbaum 
16510a586ceaSMark Shellenbaum 	return (error);
16520a586ceaSMark Shellenbaum }
16530a586ceaSMark Shellenbaum 
16540a586ceaSMark Shellenbaum static int
16550a586ceaSMark Shellenbaum sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
16560a586ceaSMark Shellenbaum     dmu_tx_t *tx)
16570a586ceaSMark Shellenbaum {
16580a586ceaSMark Shellenbaum 	int error;
16590a586ceaSMark Shellenbaum 	sa_os_t *sa = hdl->sa_os->os_sa;
16600a586ceaSMark Shellenbaum 	dmu_object_type_t bonustype;
16610a586ceaSMark Shellenbaum 
16620a586ceaSMark Shellenbaum 	bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
16630a586ceaSMark Shellenbaum 
16640a586ceaSMark Shellenbaum 	ASSERT(hdl);
16650a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
16660a586ceaSMark Shellenbaum 
16670a586ceaSMark Shellenbaum 	/* sync out registration table if necessary */
16680a586ceaSMark Shellenbaum 	if (sa->sa_need_attr_registration)
16690a586ceaSMark Shellenbaum 		sa_attr_register_sync(hdl, tx);
16700a586ceaSMark Shellenbaum 
16710a586ceaSMark Shellenbaum 	error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
16720a586ceaSMark Shellenbaum 	if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
16730a586ceaSMark Shellenbaum 		sa->sa_update_cb(hdl, tx);
16740a586ceaSMark Shellenbaum 
16750a586ceaSMark Shellenbaum 	return (error);
16760a586ceaSMark Shellenbaum }
16770a586ceaSMark Shellenbaum 
16780a586ceaSMark Shellenbaum /*
16790a586ceaSMark Shellenbaum  * update or add new attribute
16800a586ceaSMark Shellenbaum  */
16810a586ceaSMark Shellenbaum int
16820a586ceaSMark Shellenbaum sa_update(sa_handle_t *hdl, sa_attr_type_t type,
16830a586ceaSMark Shellenbaum     void *buf, uint32_t buflen, dmu_tx_t *tx)
16840a586ceaSMark Shellenbaum {
16850a586ceaSMark Shellenbaum 	int error;
16860a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
16870a586ceaSMark Shellenbaum 
16880a586ceaSMark Shellenbaum 	bulk.sa_attr = type;
16890a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
16900a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
16910a586ceaSMark Shellenbaum 	bulk.sa_data = buf;
16920a586ceaSMark Shellenbaum 
16930a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
16940a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
16950a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
16960a586ceaSMark Shellenbaum 	return (error);
16970a586ceaSMark Shellenbaum }
16980a586ceaSMark Shellenbaum 
16990a586ceaSMark Shellenbaum int
17000a586ceaSMark Shellenbaum sa_update_from_cb(sa_handle_t *hdl, sa_attr_type_t attr,
17010a586ceaSMark Shellenbaum     uint32_t buflen, sa_data_locator_t *locator, void *userdata, dmu_tx_t *tx)
17020a586ceaSMark Shellenbaum {
17030a586ceaSMark Shellenbaum 	int error;
17040a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
17050a586ceaSMark Shellenbaum 
17060a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
17070a586ceaSMark Shellenbaum 	bulk.sa_data = userdata;
17080a586ceaSMark Shellenbaum 	bulk.sa_data_func = locator;
17090a586ceaSMark Shellenbaum 	bulk.sa_length = buflen;
17100a586ceaSMark Shellenbaum 
17110a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
17120a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
17130a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
17140a586ceaSMark Shellenbaum 	return (error);
17150a586ceaSMark Shellenbaum }
17160a586ceaSMark Shellenbaum 
17170a586ceaSMark Shellenbaum /*
17180a586ceaSMark Shellenbaum  * Return size of an attribute
17190a586ceaSMark Shellenbaum  */
17200a586ceaSMark Shellenbaum 
17210a586ceaSMark Shellenbaum int
17220a586ceaSMark Shellenbaum sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
17230a586ceaSMark Shellenbaum {
17240a586ceaSMark Shellenbaum 	sa_bulk_attr_t bulk;
17250a586ceaSMark Shellenbaum 
17260a586ceaSMark Shellenbaum 	bulk.sa_data = NULL;
17270a586ceaSMark Shellenbaum 	bulk.sa_attr = attr;
17280a586ceaSMark Shellenbaum 	bulk.sa_data_func = NULL;
17290a586ceaSMark Shellenbaum 
17300a586ceaSMark Shellenbaum 	ASSERT(hdl);
17310a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
17320a586ceaSMark Shellenbaum 	if (sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) {
17330a586ceaSMark Shellenbaum 		mutex_exit(&hdl->sa_lock);
17340a586ceaSMark Shellenbaum 		return (ENOENT);
17350a586ceaSMark Shellenbaum 	}
17360a586ceaSMark Shellenbaum 	*size = bulk.sa_size;
17370a586ceaSMark Shellenbaum 
17380a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
17390a586ceaSMark Shellenbaum 	return (0);
17400a586ceaSMark Shellenbaum }
17410a586ceaSMark Shellenbaum 
17420a586ceaSMark Shellenbaum int
17430a586ceaSMark Shellenbaum sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
17440a586ceaSMark Shellenbaum {
17450a586ceaSMark Shellenbaum 	ASSERT(hdl);
17460a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&hdl->sa_lock));
17470a586ceaSMark Shellenbaum 	return (sa_lookup_impl(hdl, attrs, count));
17480a586ceaSMark Shellenbaum }
17490a586ceaSMark Shellenbaum 
17500a586ceaSMark Shellenbaum int
17510a586ceaSMark Shellenbaum sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
17520a586ceaSMark Shellenbaum {
17530a586ceaSMark Shellenbaum 	int error;
17540a586ceaSMark Shellenbaum 
17550a586ceaSMark Shellenbaum 	ASSERT(hdl);
17560a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
17570a586ceaSMark Shellenbaum 	error = sa_bulk_lookup_locked(hdl, attrs, count);
17580a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
17590a586ceaSMark Shellenbaum 	return (error);
17600a586ceaSMark Shellenbaum }
17610a586ceaSMark Shellenbaum 
17620a586ceaSMark Shellenbaum int
17630a586ceaSMark Shellenbaum sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
17640a586ceaSMark Shellenbaum {
17650a586ceaSMark Shellenbaum 	int error;
17660a586ceaSMark Shellenbaum 
17670a586ceaSMark Shellenbaum 	ASSERT(hdl);
17680a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
17690a586ceaSMark Shellenbaum 	error = sa_bulk_update_impl(hdl, attrs, count, tx);
17700a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
17710a586ceaSMark Shellenbaum 	return (error);
17720a586ceaSMark Shellenbaum }
17730a586ceaSMark Shellenbaum 
17740a586ceaSMark Shellenbaum int
17750a586ceaSMark Shellenbaum sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
17760a586ceaSMark Shellenbaum {
17770a586ceaSMark Shellenbaum 	int error;
17780a586ceaSMark Shellenbaum 
17790a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
17800a586ceaSMark Shellenbaum 	error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
17810a586ceaSMark Shellenbaum 	    NULL, 0, tx);
17820a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
17830a586ceaSMark Shellenbaum 	return (error);
17840a586ceaSMark Shellenbaum }
17850a586ceaSMark Shellenbaum 
17860a586ceaSMark Shellenbaum void
17870a586ceaSMark Shellenbaum sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
17880a586ceaSMark Shellenbaum {
17890a586ceaSMark Shellenbaum 	dmu_object_info_from_db((dmu_buf_t *)hdl->sa_bonus, doi);
17900a586ceaSMark Shellenbaum }
17910a586ceaSMark Shellenbaum 
17920a586ceaSMark Shellenbaum void
17930a586ceaSMark Shellenbaum sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
17940a586ceaSMark Shellenbaum {
17950a586ceaSMark Shellenbaum 	dmu_object_size_from_db((dmu_buf_t *)hdl->sa_bonus,
17960a586ceaSMark Shellenbaum 	    blksize, nblocks);
17970a586ceaSMark Shellenbaum }
17980a586ceaSMark Shellenbaum 
17990a586ceaSMark Shellenbaum void
18000a586ceaSMark Shellenbaum sa_update_user(sa_handle_t *newhdl, sa_handle_t *oldhdl)
18010a586ceaSMark Shellenbaum {
18020a586ceaSMark Shellenbaum 	(void) dmu_buf_update_user((dmu_buf_t *)newhdl->sa_bonus,
18030a586ceaSMark Shellenbaum 	    oldhdl, newhdl, NULL, sa_evict);
18040a586ceaSMark Shellenbaum 	oldhdl->sa_bonus = NULL;
18050a586ceaSMark Shellenbaum }
18060a586ceaSMark Shellenbaum 
18070a586ceaSMark Shellenbaum void
18080a586ceaSMark Shellenbaum sa_set_userp(sa_handle_t *hdl, void *ptr)
18090a586ceaSMark Shellenbaum {
18100a586ceaSMark Shellenbaum 	hdl->sa_userp = ptr;
18110a586ceaSMark Shellenbaum }
18120a586ceaSMark Shellenbaum 
18130a586ceaSMark Shellenbaum dmu_buf_t *
18140a586ceaSMark Shellenbaum sa_get_db(sa_handle_t *hdl)
18150a586ceaSMark Shellenbaum {
18160a586ceaSMark Shellenbaum 	return ((dmu_buf_t *)hdl->sa_bonus);
18170a586ceaSMark Shellenbaum }
18180a586ceaSMark Shellenbaum 
18190a586ceaSMark Shellenbaum void *
18200a586ceaSMark Shellenbaum sa_get_userdata(sa_handle_t *hdl)
18210a586ceaSMark Shellenbaum {
18220a586ceaSMark Shellenbaum 	return (hdl->sa_userp);
18230a586ceaSMark Shellenbaum }
18240a586ceaSMark Shellenbaum 
18250a586ceaSMark Shellenbaum void
18260a586ceaSMark Shellenbaum sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
18270a586ceaSMark Shellenbaum {
18280a586ceaSMark Shellenbaum 	ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
18290a586ceaSMark Shellenbaum 	os->os_sa->sa_update_cb = func;
18300a586ceaSMark Shellenbaum }
18310a586ceaSMark Shellenbaum 
18320a586ceaSMark Shellenbaum void
18330a586ceaSMark Shellenbaum sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
18340a586ceaSMark Shellenbaum {
18350a586ceaSMark Shellenbaum 
18360a586ceaSMark Shellenbaum 	mutex_enter(&os->os_sa->sa_lock);
18370a586ceaSMark Shellenbaum 	sa_register_update_callback_locked(os, func);
18380a586ceaSMark Shellenbaum 	mutex_exit(&os->os_sa->sa_lock);
18390a586ceaSMark Shellenbaum }
18400a586ceaSMark Shellenbaum 
18410a586ceaSMark Shellenbaum uint64_t
18420a586ceaSMark Shellenbaum sa_handle_object(sa_handle_t *hdl)
18430a586ceaSMark Shellenbaum {
18440a586ceaSMark Shellenbaum 	return (hdl->sa_bonus->db_object);
18450a586ceaSMark Shellenbaum }
18460a586ceaSMark Shellenbaum 
18470a586ceaSMark Shellenbaum boolean_t
18480a586ceaSMark Shellenbaum sa_enabled(objset_t *os)
18490a586ceaSMark Shellenbaum {
18500a586ceaSMark Shellenbaum 	return (os->os_sa == NULL);
18510a586ceaSMark Shellenbaum }
18520a586ceaSMark Shellenbaum 
18530a586ceaSMark Shellenbaum int
18540a586ceaSMark Shellenbaum sa_set_sa_object(objset_t *os, uint64_t sa_object)
18550a586ceaSMark Shellenbaum {
18560a586ceaSMark Shellenbaum 	sa_os_t *sa = os->os_sa;
18570a586ceaSMark Shellenbaum 
18580a586ceaSMark Shellenbaum 	if (sa->sa_master_obj)
18590a586ceaSMark Shellenbaum 		return (1);
18600a586ceaSMark Shellenbaum 
18610a586ceaSMark Shellenbaum 	sa->sa_master_obj = sa_object;
18620a586ceaSMark Shellenbaum 
18630a586ceaSMark Shellenbaum 	return (0);
18640a586ceaSMark Shellenbaum }
18650a586ceaSMark Shellenbaum 
18660a586ceaSMark Shellenbaum int
18670a586ceaSMark Shellenbaum sa_hdrsize(void *arg)
18680a586ceaSMark Shellenbaum {
18690a586ceaSMark Shellenbaum 	sa_hdr_phys_t *hdr = arg;
18700a586ceaSMark Shellenbaum 
18710a586ceaSMark Shellenbaum 	return (SA_HDR_SIZE(hdr));
18720a586ceaSMark Shellenbaum }
18730a586ceaSMark Shellenbaum 
18740a586ceaSMark Shellenbaum void
18750a586ceaSMark Shellenbaum sa_handle_lock(sa_handle_t *hdl)
18760a586ceaSMark Shellenbaum {
18770a586ceaSMark Shellenbaum 	ASSERT(hdl);
18780a586ceaSMark Shellenbaum 	mutex_enter(&hdl->sa_lock);
18790a586ceaSMark Shellenbaum }
18800a586ceaSMark Shellenbaum 
18810a586ceaSMark Shellenbaum void
18820a586ceaSMark Shellenbaum sa_handle_unlock(sa_handle_t *hdl)
18830a586ceaSMark Shellenbaum {
18840a586ceaSMark Shellenbaum 	ASSERT(hdl);
18850a586ceaSMark Shellenbaum 	mutex_exit(&hdl->sa_lock);
18860a586ceaSMark Shellenbaum }
1887