1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
25eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2614c2e0a0SMartin Matuska * Copyright 2023 RackTop Systems, Inc.
27eda14cbcSMatt Macy */
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy #include <sys/zfs_context.h>
30eda14cbcSMatt Macy #include <sys/types.h>
31eda14cbcSMatt Macy #include <sys/param.h>
32eda14cbcSMatt Macy #include <sys/sysmacros.h>
33eda14cbcSMatt Macy #include <sys/dmu.h>
34eda14cbcSMatt Macy #include <sys/dmu_impl.h>
35eda14cbcSMatt Macy #include <sys/dmu_objset.h>
36eda14cbcSMatt Macy #include <sys/dmu_tx.h>
37eda14cbcSMatt Macy #include <sys/dbuf.h>
38eda14cbcSMatt Macy #include <sys/dnode.h>
39eda14cbcSMatt Macy #include <sys/zap.h>
40eda14cbcSMatt Macy #include <sys/sa.h>
41eda14cbcSMatt Macy #include <sys/sunddi.h>
42eda14cbcSMatt Macy #include <sys/sa_impl.h>
43eda14cbcSMatt Macy #include <sys/errno.h>
44eda14cbcSMatt Macy #include <sys/zfs_context.h>
45eda14cbcSMatt Macy
46eda14cbcSMatt Macy #ifdef _KERNEL
47eda14cbcSMatt Macy #include <sys/zfs_znode.h>
48eda14cbcSMatt Macy #endif
49eda14cbcSMatt Macy
50eda14cbcSMatt Macy /*
51eda14cbcSMatt Macy * ZFS System attributes:
52eda14cbcSMatt Macy *
53eda14cbcSMatt Macy * A generic mechanism to allow for arbitrary attributes
54eda14cbcSMatt Macy * to be stored in a dnode. The data will be stored in the bonus buffer of
55eda14cbcSMatt Macy * the dnode and if necessary a special "spill" block will be used to handle
56eda14cbcSMatt Macy * overflow situations. The spill block will be sized to fit the data
57eda14cbcSMatt Macy * from 512 - 128K. When a spill block is used the BP (blkptr_t) for the
58eda14cbcSMatt Macy * spill block is stored at the end of the current bonus buffer. Any
59eda14cbcSMatt Macy * attributes that would be in the way of the blkptr_t will be relocated
60eda14cbcSMatt Macy * into the spill block.
61eda14cbcSMatt Macy *
62eda14cbcSMatt Macy * Attribute registration:
63eda14cbcSMatt Macy *
64eda14cbcSMatt Macy * Stored persistently on a per dataset basis
65eda14cbcSMatt Macy * a mapping between attribute "string" names and their actual attribute
66eda14cbcSMatt Macy * numeric values, length, and byteswap function. The names are only used
67eda14cbcSMatt Macy * during registration. All attributes are known by their unique attribute
68eda14cbcSMatt Macy * id value. If an attribute can have a variable size then the value
69eda14cbcSMatt Macy * 0 will be used to indicate this.
70eda14cbcSMatt Macy *
71eda14cbcSMatt Macy * Attribute Layout:
72eda14cbcSMatt Macy *
73eda14cbcSMatt Macy * Attribute layouts are a way to compactly store multiple attributes, but
74eda14cbcSMatt Macy * without taking the overhead associated with managing each attribute
75eda14cbcSMatt Macy * individually. Since you will typically have the same set of attributes
76eda14cbcSMatt Macy * stored in the same order a single table will be used to represent that
77eda14cbcSMatt Macy * layout. The ZPL for example will usually have only about 10 different
78eda14cbcSMatt Macy * layouts (regular files, device files, symlinks,
79eda14cbcSMatt Macy * regular files + scanstamp, files/dir with extended attributes, and then
80eda14cbcSMatt Macy * you have the possibility of all of those minus ACL, because it would
81eda14cbcSMatt Macy * be kicked out into the spill block)
82eda14cbcSMatt Macy *
83eda14cbcSMatt Macy * Layouts are simply an array of the attributes and their
84eda14cbcSMatt Macy * ordering i.e. [0, 1, 4, 5, 2]
85eda14cbcSMatt Macy *
86eda14cbcSMatt Macy * Each distinct layout is given a unique layout number and that is what's
87eda14cbcSMatt Macy * stored in the header at the beginning of the SA data buffer.
88eda14cbcSMatt Macy *
89eda14cbcSMatt Macy * A layout only covers a single dbuf (bonus or spill). If a set of
90eda14cbcSMatt Macy * attributes is split up between the bonus buffer and a spill buffer then
91eda14cbcSMatt Macy * two different layouts will be used. This allows us to byteswap the
92eda14cbcSMatt Macy * spill without looking at the bonus buffer and keeps the on disk format of
93eda14cbcSMatt Macy * the bonus and spill buffer the same.
94eda14cbcSMatt Macy *
95eda14cbcSMatt Macy * Adding a single attribute will cause the entire set of attributes to
96eda14cbcSMatt Macy * be rewritten and could result in a new layout number being constructed
97eda14cbcSMatt Macy * as part of the rewrite if no such layout exists for the new set of
98eda14cbcSMatt Macy * attributes. The new attribute will be appended to the end of the already
99eda14cbcSMatt Macy * existing attributes.
100eda14cbcSMatt Macy *
101eda14cbcSMatt Macy * Both the attribute registration and attribute layout information are
102eda14cbcSMatt Macy * stored in normal ZAP attributes. Their should be a small number of
103eda14cbcSMatt Macy * known layouts and the set of attributes is assumed to typically be quite
104eda14cbcSMatt Macy * small.
105eda14cbcSMatt Macy *
106eda14cbcSMatt Macy * The registered attributes and layout "table" information is maintained
107eda14cbcSMatt Macy * in core and a special "sa_os_t" is attached to the objset_t.
108eda14cbcSMatt Macy *
109eda14cbcSMatt Macy * A special interface is provided to allow for quickly applying
110eda14cbcSMatt Macy * a large set of attributes at once. sa_replace_all_by_template() is
111eda14cbcSMatt Macy * used to set an array of attributes. This is used by the ZPL when
112eda14cbcSMatt Macy * creating a brand new file. The template that is passed into the function
113eda14cbcSMatt Macy * specifies the attribute, size for variable length attributes, location of
114eda14cbcSMatt Macy * data and special "data locator" function if the data isn't in a contiguous
115eda14cbcSMatt Macy * location.
116eda14cbcSMatt Macy *
117eda14cbcSMatt Macy * Byteswap implications:
118eda14cbcSMatt Macy *
119eda14cbcSMatt Macy * Since the SA attributes are not entirely self describing we can't do
120eda14cbcSMatt Macy * the normal byteswap processing. The special ZAP layout attribute and
121eda14cbcSMatt Macy * attribute registration attributes define the byteswap function and the
122eda14cbcSMatt Macy * size of the attributes, unless it is variable sized.
123eda14cbcSMatt Macy * The normal ZFS byteswapping infrastructure assumes you don't need
124eda14cbcSMatt Macy * to read any objects in order to do the necessary byteswapping. Whereas
125eda14cbcSMatt Macy * SA attributes can only be properly byteswapped if the dataset is opened
126eda14cbcSMatt Macy * and the layout/attribute ZAP attributes are available. Because of this
127eda14cbcSMatt Macy * the SA attributes will be byteswapped when they are first accessed by
128eda14cbcSMatt Macy * the SA code that will read the SA data.
129eda14cbcSMatt Macy */
130eda14cbcSMatt Macy
131eda14cbcSMatt Macy typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
132eda14cbcSMatt Macy uint16_t length, int length_idx, boolean_t, void *userp);
133eda14cbcSMatt Macy
134eda14cbcSMatt Macy static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
135eda14cbcSMatt Macy static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
136eda14cbcSMatt Macy static sa_idx_tab_t *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
137eda14cbcSMatt Macy sa_hdr_phys_t *hdr);
138eda14cbcSMatt Macy static void sa_idx_tab_rele(objset_t *os, void *arg);
139eda14cbcSMatt Macy static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
140eda14cbcSMatt Macy int buflen);
141eda14cbcSMatt Macy static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
142eda14cbcSMatt Macy sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
143eda14cbcSMatt Macy uint16_t buflen, dmu_tx_t *tx);
144eda14cbcSMatt Macy
145be181ee2SMartin Matuska static arc_byteswap_func_t sa_bswap_table[] = {
146eda14cbcSMatt Macy byteswap_uint64_array,
147eda14cbcSMatt Macy byteswap_uint32_array,
148eda14cbcSMatt Macy byteswap_uint16_array,
149eda14cbcSMatt Macy byteswap_uint8_array,
150eda14cbcSMatt Macy zfs_acl_byteswap,
151eda14cbcSMatt Macy };
152eda14cbcSMatt Macy
153eda14cbcSMatt Macy #ifdef HAVE_EFFICIENT_UNALIGNED_ACCESS
154eda14cbcSMatt Macy #define SA_COPY_DATA(f, s, t, l) \
155eda14cbcSMatt Macy do { \
156eda14cbcSMatt Macy if (f == NULL) { \
157eda14cbcSMatt Macy if (l == 8) { \
158eda14cbcSMatt Macy *(uint64_t *)t = *(uint64_t *)s; \
159eda14cbcSMatt Macy } else if (l == 16) { \
160eda14cbcSMatt Macy *(uint64_t *)t = *(uint64_t *)s; \
161eda14cbcSMatt Macy *(uint64_t *)((uintptr_t)t + 8) = \
162eda14cbcSMatt Macy *(uint64_t *)((uintptr_t)s + 8); \
163eda14cbcSMatt Macy } else { \
164da5137abSMartin Matuska memcpy(t, s, l); \
165eda14cbcSMatt Macy } \
166eda14cbcSMatt Macy } else { \
167eda14cbcSMatt Macy sa_copy_data(f, s, t, l); \
168eda14cbcSMatt Macy } \
169eda14cbcSMatt Macy } while (0)
170eda14cbcSMatt Macy #else
171eda14cbcSMatt Macy #define SA_COPY_DATA(f, s, t, l) sa_copy_data(f, s, t, l)
172eda14cbcSMatt Macy #endif
173eda14cbcSMatt Macy
174eda14cbcSMatt Macy /*
175eda14cbcSMatt Macy * This table is fixed and cannot be changed. Its purpose is to
176eda14cbcSMatt Macy * allow the SA code to work with both old/new ZPL file systems.
177eda14cbcSMatt Macy * It contains the list of legacy attributes. These attributes aren't
178eda14cbcSMatt Macy * stored in the "attribute" registry zap objects, since older ZPL file systems
179eda14cbcSMatt Macy * won't have the registry. Only objsets of type ZFS_TYPE_FILESYSTEM will
180eda14cbcSMatt Macy * use this static table.
181eda14cbcSMatt Macy */
182e92ffd9bSMartin Matuska static const sa_attr_reg_t sa_legacy_attrs[] = {
183eda14cbcSMatt Macy {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
184eda14cbcSMatt Macy {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
185eda14cbcSMatt Macy {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
186eda14cbcSMatt Macy {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
187eda14cbcSMatt Macy {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
188eda14cbcSMatt Macy {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
189eda14cbcSMatt Macy {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
190eda14cbcSMatt Macy {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
191eda14cbcSMatt Macy {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
192eda14cbcSMatt Macy {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
193eda14cbcSMatt Macy {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
194eda14cbcSMatt Macy {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
195eda14cbcSMatt Macy {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
196eda14cbcSMatt Macy {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
197eda14cbcSMatt Macy {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
198eda14cbcSMatt Macy {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
199eda14cbcSMatt Macy };
200eda14cbcSMatt Macy
201eda14cbcSMatt Macy /*
202eda14cbcSMatt Macy * This is only used for objects of type DMU_OT_ZNODE
203eda14cbcSMatt Macy */
204e92ffd9bSMartin Matuska static const sa_attr_type_t sa_legacy_zpl_layout[] = {
205eda14cbcSMatt Macy 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
206eda14cbcSMatt Macy };
207eda14cbcSMatt Macy
208eda14cbcSMatt Macy /*
209eda14cbcSMatt Macy * Special dummy layout used for buffers with no attributes.
210eda14cbcSMatt Macy */
211e92ffd9bSMartin Matuska static const sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
212eda14cbcSMatt Macy
213e92ffd9bSMartin Matuska static const size_t sa_legacy_attr_count = ARRAY_SIZE(sa_legacy_attrs);
214eda14cbcSMatt Macy static kmem_cache_t *sa_cache = NULL;
215eda14cbcSMatt Macy
216eda14cbcSMatt Macy static int
sa_cache_constructor(void * buf,void * unused,int kmflag)217eda14cbcSMatt Macy sa_cache_constructor(void *buf, void *unused, int kmflag)
218eda14cbcSMatt Macy {
219e92ffd9bSMartin Matuska (void) unused, (void) kmflag;
220eda14cbcSMatt Macy sa_handle_t *hdl = buf;
221eda14cbcSMatt Macy
222eda14cbcSMatt Macy mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
223eda14cbcSMatt Macy return (0);
224eda14cbcSMatt Macy }
225eda14cbcSMatt Macy
226eda14cbcSMatt Macy static void
sa_cache_destructor(void * buf,void * unused)227eda14cbcSMatt Macy sa_cache_destructor(void *buf, void *unused)
228eda14cbcSMatt Macy {
229e92ffd9bSMartin Matuska (void) unused;
230eda14cbcSMatt Macy sa_handle_t *hdl = buf;
231eda14cbcSMatt Macy mutex_destroy(&hdl->sa_lock);
232eda14cbcSMatt Macy }
233eda14cbcSMatt Macy
234eda14cbcSMatt Macy void
sa_cache_init(void)235eda14cbcSMatt Macy sa_cache_init(void)
236eda14cbcSMatt Macy {
237eda14cbcSMatt Macy sa_cache = kmem_cache_create("sa_cache",
238eda14cbcSMatt Macy sizeof (sa_handle_t), 0, sa_cache_constructor,
239ce4dcb97SMartin Matuska sa_cache_destructor, NULL, NULL, NULL, KMC_RECLAIMABLE);
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy
242eda14cbcSMatt Macy void
sa_cache_fini(void)243eda14cbcSMatt Macy sa_cache_fini(void)
244eda14cbcSMatt Macy {
245eda14cbcSMatt Macy if (sa_cache)
246eda14cbcSMatt Macy kmem_cache_destroy(sa_cache);
247eda14cbcSMatt Macy }
248eda14cbcSMatt Macy
249eda14cbcSMatt Macy static int
layout_num_compare(const void * arg1,const void * arg2)250eda14cbcSMatt Macy layout_num_compare(const void *arg1, const void *arg2)
251eda14cbcSMatt Macy {
252eda14cbcSMatt Macy const sa_lot_t *node1 = (const sa_lot_t *)arg1;
253eda14cbcSMatt Macy const sa_lot_t *node2 = (const sa_lot_t *)arg2;
254eda14cbcSMatt Macy
255eda14cbcSMatt Macy return (TREE_CMP(node1->lot_num, node2->lot_num));
256eda14cbcSMatt Macy }
257eda14cbcSMatt Macy
258eda14cbcSMatt Macy static int
layout_hash_compare(const void * arg1,const void * arg2)259eda14cbcSMatt Macy layout_hash_compare(const void *arg1, const void *arg2)
260eda14cbcSMatt Macy {
261eda14cbcSMatt Macy const sa_lot_t *node1 = (const sa_lot_t *)arg1;
262eda14cbcSMatt Macy const sa_lot_t *node2 = (const sa_lot_t *)arg2;
263eda14cbcSMatt Macy
264eda14cbcSMatt Macy int cmp = TREE_CMP(node1->lot_hash, node2->lot_hash);
265eda14cbcSMatt Macy if (likely(cmp))
266eda14cbcSMatt Macy return (cmp);
267eda14cbcSMatt Macy
268eda14cbcSMatt Macy return (TREE_CMP(node1->lot_instance, node2->lot_instance));
269eda14cbcSMatt Macy }
270eda14cbcSMatt Macy
271eda14cbcSMatt Macy static boolean_t
sa_layout_equal(sa_lot_t * tbf,sa_attr_type_t * attrs,int count)272eda14cbcSMatt Macy sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
273eda14cbcSMatt Macy {
274eda14cbcSMatt Macy int i;
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy if (count != tbf->lot_attr_count)
277eda14cbcSMatt Macy return (1);
278eda14cbcSMatt Macy
279eda14cbcSMatt Macy for (i = 0; i != count; i++) {
280eda14cbcSMatt Macy if (attrs[i] != tbf->lot_attrs[i])
281eda14cbcSMatt Macy return (1);
282eda14cbcSMatt Macy }
283eda14cbcSMatt Macy return (0);
284eda14cbcSMatt Macy }
285eda14cbcSMatt Macy
286eda14cbcSMatt Macy #define SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
287eda14cbcSMatt Macy
288eda14cbcSMatt Macy static uint64_t
sa_layout_info_hash(const sa_attr_type_t * attrs,int attr_count)289e92ffd9bSMartin Matuska sa_layout_info_hash(const sa_attr_type_t *attrs, int attr_count)
290eda14cbcSMatt Macy {
291eda14cbcSMatt Macy uint64_t crc = -1ULL;
292eda14cbcSMatt Macy
293e92ffd9bSMartin Matuska for (int i = 0; i != attr_count; i++)
294eda14cbcSMatt Macy crc ^= SA_ATTR_HASH(attrs[i]);
295eda14cbcSMatt Macy
296eda14cbcSMatt Macy return (crc);
297eda14cbcSMatt Macy }
298eda14cbcSMatt Macy
299eda14cbcSMatt Macy static int
sa_get_spill(sa_handle_t * hdl)300eda14cbcSMatt Macy sa_get_spill(sa_handle_t *hdl)
301eda14cbcSMatt Macy {
302eda14cbcSMatt Macy int rc;
303eda14cbcSMatt Macy if (hdl->sa_spill == NULL) {
304eda14cbcSMatt Macy if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
305eda14cbcSMatt Macy &hdl->sa_spill)) == 0)
306eda14cbcSMatt Macy VERIFY(0 == sa_build_index(hdl, SA_SPILL));
307eda14cbcSMatt Macy } else {
308eda14cbcSMatt Macy rc = 0;
309eda14cbcSMatt Macy }
310eda14cbcSMatt Macy
311eda14cbcSMatt Macy return (rc);
312eda14cbcSMatt Macy }
313eda14cbcSMatt Macy
314eda14cbcSMatt Macy /*
315eda14cbcSMatt Macy * Main attribute lookup/update function
316eda14cbcSMatt Macy * returns 0 for success or non zero for failures
317eda14cbcSMatt Macy *
318eda14cbcSMatt Macy * Operates on bulk array, first failure will abort further processing
319eda14cbcSMatt Macy */
320eda14cbcSMatt Macy static int
sa_attr_op(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count,sa_data_op_t data_op,dmu_tx_t * tx)321eda14cbcSMatt Macy sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
322eda14cbcSMatt Macy sa_data_op_t data_op, dmu_tx_t *tx)
323eda14cbcSMatt Macy {
324eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
325eda14cbcSMatt Macy int i;
326eda14cbcSMatt Macy int error = 0;
327eda14cbcSMatt Macy sa_buf_type_t buftypes;
328eda14cbcSMatt Macy
329eda14cbcSMatt Macy buftypes = 0;
330eda14cbcSMatt Macy
331eda14cbcSMatt Macy ASSERT(count > 0);
332eda14cbcSMatt Macy for (i = 0; i != count; i++) {
333eda14cbcSMatt Macy ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
334eda14cbcSMatt Macy
335eda14cbcSMatt Macy bulk[i].sa_addr = NULL;
336eda14cbcSMatt Macy /* First check the bonus buffer */
337eda14cbcSMatt Macy
338eda14cbcSMatt Macy if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
339eda14cbcSMatt Macy hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
340eda14cbcSMatt Macy SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
341eda14cbcSMatt Macy SA_GET_HDR(hdl, SA_BONUS),
342eda14cbcSMatt Macy bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
343eda14cbcSMatt Macy if (tx && !(buftypes & SA_BONUS)) {
344eda14cbcSMatt Macy dmu_buf_will_dirty(hdl->sa_bonus, tx);
345eda14cbcSMatt Macy buftypes |= SA_BONUS;
346eda14cbcSMatt Macy }
347eda14cbcSMatt Macy }
348eda14cbcSMatt Macy if (bulk[i].sa_addr == NULL &&
349eda14cbcSMatt Macy ((error = sa_get_spill(hdl)) == 0)) {
350eda14cbcSMatt Macy if (TOC_ATTR_PRESENT(
351eda14cbcSMatt Macy hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
352eda14cbcSMatt Macy SA_ATTR_INFO(sa, hdl->sa_spill_tab,
353eda14cbcSMatt Macy SA_GET_HDR(hdl, SA_SPILL),
354eda14cbcSMatt Macy bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
355eda14cbcSMatt Macy if (tx && !(buftypes & SA_SPILL) &&
356eda14cbcSMatt Macy bulk[i].sa_size == bulk[i].sa_length) {
357eda14cbcSMatt Macy dmu_buf_will_dirty(hdl->sa_spill, tx);
358eda14cbcSMatt Macy buftypes |= SA_SPILL;
359eda14cbcSMatt Macy }
360eda14cbcSMatt Macy }
361eda14cbcSMatt Macy }
362eda14cbcSMatt Macy if (error && error != ENOENT) {
363eda14cbcSMatt Macy return ((error == ECKSUM) ? EIO : error);
364eda14cbcSMatt Macy }
365eda14cbcSMatt Macy
366eda14cbcSMatt Macy switch (data_op) {
367eda14cbcSMatt Macy case SA_LOOKUP:
368eda14cbcSMatt Macy if (bulk[i].sa_addr == NULL)
369eda14cbcSMatt Macy return (SET_ERROR(ENOENT));
370eda14cbcSMatt Macy if (bulk[i].sa_data) {
371eda14cbcSMatt Macy SA_COPY_DATA(bulk[i].sa_data_func,
372eda14cbcSMatt Macy bulk[i].sa_addr, bulk[i].sa_data,
37314c2e0a0SMartin Matuska MIN(bulk[i].sa_size, bulk[i].sa_length));
374eda14cbcSMatt Macy }
375eda14cbcSMatt Macy continue;
376eda14cbcSMatt Macy
377eda14cbcSMatt Macy case SA_UPDATE:
378eda14cbcSMatt Macy /* existing rewrite of attr */
379eda14cbcSMatt Macy if (bulk[i].sa_addr &&
380eda14cbcSMatt Macy bulk[i].sa_size == bulk[i].sa_length) {
381eda14cbcSMatt Macy SA_COPY_DATA(bulk[i].sa_data_func,
382eda14cbcSMatt Macy bulk[i].sa_data, bulk[i].sa_addr,
383eda14cbcSMatt Macy bulk[i].sa_length);
384eda14cbcSMatt Macy continue;
385eda14cbcSMatt Macy } else if (bulk[i].sa_addr) { /* attr size change */
386eda14cbcSMatt Macy error = sa_modify_attrs(hdl, bulk[i].sa_attr,
387eda14cbcSMatt Macy SA_REPLACE, bulk[i].sa_data_func,
388eda14cbcSMatt Macy bulk[i].sa_data, bulk[i].sa_length, tx);
389eda14cbcSMatt Macy } else { /* adding new attribute */
390eda14cbcSMatt Macy error = sa_modify_attrs(hdl, bulk[i].sa_attr,
391eda14cbcSMatt Macy SA_ADD, bulk[i].sa_data_func,
392eda14cbcSMatt Macy bulk[i].sa_data, bulk[i].sa_length, tx);
393eda14cbcSMatt Macy }
394eda14cbcSMatt Macy if (error)
395eda14cbcSMatt Macy return (error);
396eda14cbcSMatt Macy break;
397eda14cbcSMatt Macy default:
398eda14cbcSMatt Macy break;
399eda14cbcSMatt Macy }
400eda14cbcSMatt Macy }
401eda14cbcSMatt Macy return (error);
402eda14cbcSMatt Macy }
403eda14cbcSMatt Macy
404eda14cbcSMatt Macy static sa_lot_t *
sa_add_layout_entry(objset_t * os,const sa_attr_type_t * attrs,int attr_count,uint64_t lot_num,uint64_t hash,boolean_t zapadd,dmu_tx_t * tx)405e92ffd9bSMartin Matuska sa_add_layout_entry(objset_t *os, const sa_attr_type_t *attrs, int attr_count,
406eda14cbcSMatt Macy uint64_t lot_num, uint64_t hash, boolean_t zapadd, dmu_tx_t *tx)
407eda14cbcSMatt Macy {
408eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
409eda14cbcSMatt Macy sa_lot_t *tb, *findtb;
410eda14cbcSMatt Macy int i;
411eda14cbcSMatt Macy avl_index_t loc;
412eda14cbcSMatt Macy
413eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&sa->sa_lock));
414eda14cbcSMatt Macy tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
415eda14cbcSMatt Macy tb->lot_attr_count = attr_count;
416eda14cbcSMatt Macy tb->lot_attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
417eda14cbcSMatt Macy KM_SLEEP);
418da5137abSMartin Matuska memcpy(tb->lot_attrs, attrs, sizeof (sa_attr_type_t) * attr_count);
419eda14cbcSMatt Macy tb->lot_num = lot_num;
420eda14cbcSMatt Macy tb->lot_hash = hash;
421eda14cbcSMatt Macy tb->lot_instance = 0;
422eda14cbcSMatt Macy
423eda14cbcSMatt Macy if (zapadd) {
424eda14cbcSMatt Macy char attr_name[8];
425eda14cbcSMatt Macy
426eda14cbcSMatt Macy if (sa->sa_layout_attr_obj == 0) {
427eda14cbcSMatt Macy sa->sa_layout_attr_obj = zap_create_link(os,
428eda14cbcSMatt Macy DMU_OT_SA_ATTR_LAYOUTS,
429eda14cbcSMatt Macy sa->sa_master_obj, SA_LAYOUTS, tx);
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy
432eda14cbcSMatt Macy (void) snprintf(attr_name, sizeof (attr_name),
433eda14cbcSMatt Macy "%d", (int)lot_num);
434eda14cbcSMatt Macy VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
435eda14cbcSMatt Macy attr_name, 2, attr_count, attrs, tx));
436eda14cbcSMatt Macy }
437eda14cbcSMatt Macy
438eda14cbcSMatt Macy list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
439eda14cbcSMatt Macy offsetof(sa_idx_tab_t, sa_next));
440eda14cbcSMatt Macy
441eda14cbcSMatt Macy for (i = 0; i != attr_count; i++) {
442eda14cbcSMatt Macy if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
443eda14cbcSMatt Macy tb->lot_var_sizes++;
444eda14cbcSMatt Macy }
445eda14cbcSMatt Macy
446eda14cbcSMatt Macy avl_add(&sa->sa_layout_num_tree, tb);
447eda14cbcSMatt Macy
448eda14cbcSMatt Macy /* verify we don't have a hash collision */
449eda14cbcSMatt Macy if ((findtb = avl_find(&sa->sa_layout_hash_tree, tb, &loc)) != NULL) {
450eda14cbcSMatt Macy for (; findtb && findtb->lot_hash == hash;
451eda14cbcSMatt Macy findtb = AVL_NEXT(&sa->sa_layout_hash_tree, findtb)) {
452eda14cbcSMatt Macy if (findtb->lot_instance != tb->lot_instance)
453eda14cbcSMatt Macy break;
454eda14cbcSMatt Macy tb->lot_instance++;
455eda14cbcSMatt Macy }
456eda14cbcSMatt Macy }
457eda14cbcSMatt Macy avl_add(&sa->sa_layout_hash_tree, tb);
458eda14cbcSMatt Macy return (tb);
459eda14cbcSMatt Macy }
460eda14cbcSMatt Macy
461eda14cbcSMatt Macy static void
sa_find_layout(objset_t * os,uint64_t hash,sa_attr_type_t * attrs,int count,dmu_tx_t * tx,sa_lot_t ** lot)462eda14cbcSMatt Macy sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
463eda14cbcSMatt Macy int count, dmu_tx_t *tx, sa_lot_t **lot)
464eda14cbcSMatt Macy {
465eda14cbcSMatt Macy sa_lot_t *tb, tbsearch;
466eda14cbcSMatt Macy avl_index_t loc;
467eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
468eda14cbcSMatt Macy boolean_t found = B_FALSE;
469eda14cbcSMatt Macy
470eda14cbcSMatt Macy mutex_enter(&sa->sa_lock);
471eda14cbcSMatt Macy tbsearch.lot_hash = hash;
472eda14cbcSMatt Macy tbsearch.lot_instance = 0;
473eda14cbcSMatt Macy tb = avl_find(&sa->sa_layout_hash_tree, &tbsearch, &loc);
474eda14cbcSMatt Macy if (tb) {
475eda14cbcSMatt Macy for (; tb && tb->lot_hash == hash;
476eda14cbcSMatt Macy tb = AVL_NEXT(&sa->sa_layout_hash_tree, tb)) {
477eda14cbcSMatt Macy if (sa_layout_equal(tb, attrs, count) == 0) {
478eda14cbcSMatt Macy found = B_TRUE;
479eda14cbcSMatt Macy break;
480eda14cbcSMatt Macy }
481eda14cbcSMatt Macy }
482eda14cbcSMatt Macy }
483eda14cbcSMatt Macy if (!found) {
484eda14cbcSMatt Macy tb = sa_add_layout_entry(os, attrs, count,
485eda14cbcSMatt Macy avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
486eda14cbcSMatt Macy }
487eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
488eda14cbcSMatt Macy *lot = tb;
489eda14cbcSMatt Macy }
490eda14cbcSMatt Macy
491eda14cbcSMatt Macy static int
sa_resize_spill(sa_handle_t * hdl,uint32_t size,dmu_tx_t * tx)492eda14cbcSMatt Macy sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
493eda14cbcSMatt Macy {
494eda14cbcSMatt Macy int error;
495eda14cbcSMatt Macy uint32_t blocksize;
496eda14cbcSMatt Macy
497eda14cbcSMatt Macy if (size == 0) {
498eda14cbcSMatt Macy blocksize = SPA_MINBLOCKSIZE;
499eda14cbcSMatt Macy } else if (size > SPA_OLD_MAXBLOCKSIZE) {
500eda14cbcSMatt Macy ASSERT(0);
501eda14cbcSMatt Macy return (SET_ERROR(EFBIG));
502eda14cbcSMatt Macy } else {
503eda14cbcSMatt Macy blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
504eda14cbcSMatt Macy }
505eda14cbcSMatt Macy
506eda14cbcSMatt Macy error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
507eda14cbcSMatt Macy ASSERT(error == 0);
508eda14cbcSMatt Macy return (error);
509eda14cbcSMatt Macy }
510eda14cbcSMatt Macy
511eda14cbcSMatt Macy static void
sa_copy_data(sa_data_locator_t * func,void * datastart,void * target,int buflen)512eda14cbcSMatt Macy sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
513eda14cbcSMatt Macy {
514eda14cbcSMatt Macy if (func == NULL) {
515da5137abSMartin Matuska memcpy(target, datastart, buflen);
516eda14cbcSMatt Macy } else {
517eda14cbcSMatt Macy boolean_t start;
518eda14cbcSMatt Macy int bytes;
519eda14cbcSMatt Macy void *dataptr;
520eda14cbcSMatt Macy void *saptr = target;
521eda14cbcSMatt Macy uint32_t length;
522eda14cbcSMatt Macy
523eda14cbcSMatt Macy start = B_TRUE;
524eda14cbcSMatt Macy bytes = 0;
525eda14cbcSMatt Macy while (bytes < buflen) {
526eda14cbcSMatt Macy func(&dataptr, &length, buflen, start, datastart);
527da5137abSMartin Matuska memcpy(saptr, dataptr, length);
528eda14cbcSMatt Macy saptr = (void *)((caddr_t)saptr + length);
529eda14cbcSMatt Macy bytes += length;
530eda14cbcSMatt Macy start = B_FALSE;
531eda14cbcSMatt Macy }
532eda14cbcSMatt Macy }
533eda14cbcSMatt Macy }
534eda14cbcSMatt Macy
535eda14cbcSMatt Macy /*
536eda14cbcSMatt Macy * Determine several different values pertaining to system attribute
537eda14cbcSMatt Macy * buffers.
538eda14cbcSMatt Macy *
539eda14cbcSMatt Macy * Return the size of the sa_hdr_phys_t header for the buffer. Each
540eda14cbcSMatt Macy * variable length attribute except the first contributes two bytes to
541eda14cbcSMatt Macy * the header size, which is then rounded up to an 8-byte boundary.
542eda14cbcSMatt Macy *
543eda14cbcSMatt Macy * The following output parameters are also computed.
544eda14cbcSMatt Macy *
545eda14cbcSMatt Macy * index - The index of the first attribute in attr_desc that will
546eda14cbcSMatt Macy * spill over. Only valid if will_spill is set.
547eda14cbcSMatt Macy *
548eda14cbcSMatt Macy * total - The total number of bytes of all system attributes described
549eda14cbcSMatt Macy * in attr_desc.
550eda14cbcSMatt Macy *
551eda14cbcSMatt Macy * will_spill - Set when spilling is necessary. It is only set when
552eda14cbcSMatt Macy * the buftype is SA_BONUS.
553eda14cbcSMatt Macy */
554eda14cbcSMatt Macy static int
sa_find_sizes(sa_os_t * sa,sa_bulk_attr_t * attr_desc,int attr_count,dmu_buf_t * db,sa_buf_type_t buftype,int full_space,int * index,int * total,boolean_t * will_spill)555eda14cbcSMatt Macy sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
556eda14cbcSMatt Macy dmu_buf_t *db, sa_buf_type_t buftype, int full_space, int *index,
557eda14cbcSMatt Macy int *total, boolean_t *will_spill)
558eda14cbcSMatt Macy {
559eda14cbcSMatt Macy int var_size_count = 0;
560eda14cbcSMatt Macy int i;
561eda14cbcSMatt Macy int hdrsize;
562eda14cbcSMatt Macy int extra_hdrsize;
563eda14cbcSMatt Macy
564eda14cbcSMatt Macy if (buftype == SA_BONUS && sa->sa_force_spill) {
565eda14cbcSMatt Macy *total = 0;
566eda14cbcSMatt Macy *index = 0;
567eda14cbcSMatt Macy *will_spill = B_TRUE;
568eda14cbcSMatt Macy return (0);
569eda14cbcSMatt Macy }
570eda14cbcSMatt Macy
571eda14cbcSMatt Macy *index = -1;
572eda14cbcSMatt Macy *total = 0;
573eda14cbcSMatt Macy *will_spill = B_FALSE;
574eda14cbcSMatt Macy
575eda14cbcSMatt Macy extra_hdrsize = 0;
576eda14cbcSMatt Macy hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
577eda14cbcSMatt Macy sizeof (sa_hdr_phys_t);
578eda14cbcSMatt Macy
579eda14cbcSMatt Macy ASSERT(IS_P2ALIGNED(full_space, 8));
580eda14cbcSMatt Macy
581eda14cbcSMatt Macy for (i = 0; i != attr_count; i++) {
582eda14cbcSMatt Macy boolean_t is_var_sz, might_spill_here;
583eda14cbcSMatt Macy int tmp_hdrsize;
584eda14cbcSMatt Macy
585eda14cbcSMatt Macy *total = P2ROUNDUP(*total, 8);
586eda14cbcSMatt Macy *total += attr_desc[i].sa_length;
587eda14cbcSMatt Macy if (*will_spill)
588eda14cbcSMatt Macy continue;
589eda14cbcSMatt Macy
590eda14cbcSMatt Macy is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
591eda14cbcSMatt Macy if (is_var_sz)
592eda14cbcSMatt Macy var_size_count++;
593eda14cbcSMatt Macy
594eda14cbcSMatt Macy /*
595eda14cbcSMatt Macy * Calculate what the SA header size would be if this
596eda14cbcSMatt Macy * attribute doesn't spill.
597eda14cbcSMatt Macy */
598eda14cbcSMatt Macy tmp_hdrsize = hdrsize + ((is_var_sz && var_size_count > 1) ?
599eda14cbcSMatt Macy sizeof (uint16_t) : 0);
600eda14cbcSMatt Macy
601eda14cbcSMatt Macy /*
602eda14cbcSMatt Macy * Check whether this attribute spans into the space
603eda14cbcSMatt Macy * that would be used by the spill block pointer should
604eda14cbcSMatt Macy * a spill block be needed.
605eda14cbcSMatt Macy */
606eda14cbcSMatt Macy might_spill_here =
607eda14cbcSMatt Macy buftype == SA_BONUS && *index == -1 &&
608eda14cbcSMatt Macy (*total + P2ROUNDUP(tmp_hdrsize, 8)) >
609eda14cbcSMatt Macy (full_space - sizeof (blkptr_t));
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy if (is_var_sz && var_size_count > 1) {
612eda14cbcSMatt Macy if (buftype == SA_SPILL ||
613eda14cbcSMatt Macy tmp_hdrsize + *total < full_space) {
614eda14cbcSMatt Macy /*
615eda14cbcSMatt Macy * Record the extra header size in case this
616eda14cbcSMatt Macy * increase needs to be reversed due to
617eda14cbcSMatt Macy * spill-over.
618eda14cbcSMatt Macy */
619eda14cbcSMatt Macy hdrsize = tmp_hdrsize;
620eda14cbcSMatt Macy if (*index != -1 || might_spill_here)
621eda14cbcSMatt Macy extra_hdrsize += sizeof (uint16_t);
622eda14cbcSMatt Macy } else {
623eda14cbcSMatt Macy ASSERT(buftype == SA_BONUS);
624eda14cbcSMatt Macy if (*index == -1)
625eda14cbcSMatt Macy *index = i;
626eda14cbcSMatt Macy *will_spill = B_TRUE;
627eda14cbcSMatt Macy continue;
628eda14cbcSMatt Macy }
629eda14cbcSMatt Macy }
630eda14cbcSMatt Macy
631eda14cbcSMatt Macy /*
632eda14cbcSMatt Macy * Store index of where spill *could* occur. Then
633eda14cbcSMatt Macy * continue to count the remaining attribute sizes. The
634eda14cbcSMatt Macy * sum is used later for sizing bonus and spill buffer.
635eda14cbcSMatt Macy */
636eda14cbcSMatt Macy if (might_spill_here)
637eda14cbcSMatt Macy *index = i;
638eda14cbcSMatt Macy
639eda14cbcSMatt Macy if ((*total + P2ROUNDUP(hdrsize, 8)) > full_space &&
640eda14cbcSMatt Macy buftype == SA_BONUS)
641eda14cbcSMatt Macy *will_spill = B_TRUE;
642eda14cbcSMatt Macy }
643eda14cbcSMatt Macy
644eda14cbcSMatt Macy if (*will_spill)
645eda14cbcSMatt Macy hdrsize -= extra_hdrsize;
646eda14cbcSMatt Macy
647eda14cbcSMatt Macy hdrsize = P2ROUNDUP(hdrsize, 8);
648eda14cbcSMatt Macy return (hdrsize);
649eda14cbcSMatt Macy }
650eda14cbcSMatt Macy
651eda14cbcSMatt Macy #define BUF_SPACE_NEEDED(total, header) (total + header)
652eda14cbcSMatt Macy
653eda14cbcSMatt Macy /*
654eda14cbcSMatt Macy * Find layout that corresponds to ordering of attributes
655eda14cbcSMatt Macy * If not found a new layout number is created and added to
656eda14cbcSMatt Macy * persistent layout tables.
657eda14cbcSMatt Macy */
658eda14cbcSMatt Macy static int
sa_build_layouts(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)659eda14cbcSMatt Macy sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
660eda14cbcSMatt Macy dmu_tx_t *tx)
661eda14cbcSMatt Macy {
662eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
663eda14cbcSMatt Macy uint64_t hash;
664eda14cbcSMatt Macy sa_buf_type_t buftype;
665eda14cbcSMatt Macy sa_hdr_phys_t *sahdr;
666eda14cbcSMatt Macy void *data_start;
667eda14cbcSMatt Macy sa_attr_type_t *attrs, *attrs_start;
668eda14cbcSMatt Macy int i, lot_count;
669eda14cbcSMatt Macy int dnodesize;
670eda14cbcSMatt Macy int spill_idx;
671eda14cbcSMatt Macy int hdrsize;
672eda14cbcSMatt Macy int spillhdrsize = 0;
673eda14cbcSMatt Macy int used;
674eda14cbcSMatt Macy dmu_object_type_t bonustype;
675eda14cbcSMatt Macy sa_lot_t *lot;
676eda14cbcSMatt Macy int len_idx;
677eda14cbcSMatt Macy int spill_used;
678eda14cbcSMatt Macy int bonuslen;
679eda14cbcSMatt Macy boolean_t spilling;
680eda14cbcSMatt Macy
681eda14cbcSMatt Macy dmu_buf_will_dirty(hdl->sa_bonus, tx);
682eda14cbcSMatt Macy bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
683eda14cbcSMatt Macy dmu_object_dnsize_from_db(hdl->sa_bonus, &dnodesize);
684eda14cbcSMatt Macy bonuslen = DN_BONUS_SIZE(dnodesize);
685eda14cbcSMatt Macy
686eda14cbcSMatt Macy /* first determine bonus header size and sum of all attributes */
687eda14cbcSMatt Macy hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
688eda14cbcSMatt Macy SA_BONUS, bonuslen, &spill_idx, &used, &spilling);
689eda14cbcSMatt Macy
690eda14cbcSMatt Macy if (used > SPA_OLD_MAXBLOCKSIZE)
691eda14cbcSMatt Macy return (SET_ERROR(EFBIG));
692eda14cbcSMatt Macy
693eda14cbcSMatt Macy VERIFY0(dmu_set_bonus(hdl->sa_bonus, spilling ?
694eda14cbcSMatt Macy MIN(bonuslen - sizeof (blkptr_t), used + hdrsize) :
695eda14cbcSMatt Macy used + hdrsize, tx));
696eda14cbcSMatt Macy
697eda14cbcSMatt Macy ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
698eda14cbcSMatt Macy bonustype == DMU_OT_SA);
699eda14cbcSMatt Macy
700eda14cbcSMatt Macy /* setup and size spill buffer when needed */
701eda14cbcSMatt Macy if (spilling) {
702eda14cbcSMatt Macy boolean_t dummy;
703eda14cbcSMatt Macy
704eda14cbcSMatt Macy if (hdl->sa_spill == NULL) {
705eda14cbcSMatt Macy VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, 0, NULL,
706eda14cbcSMatt Macy &hdl->sa_spill) == 0);
707eda14cbcSMatt Macy }
708eda14cbcSMatt Macy dmu_buf_will_dirty(hdl->sa_spill, tx);
709eda14cbcSMatt Macy
710eda14cbcSMatt Macy spillhdrsize = sa_find_sizes(sa, &attr_desc[spill_idx],
711eda14cbcSMatt Macy attr_count - spill_idx, hdl->sa_spill, SA_SPILL,
712eda14cbcSMatt Macy hdl->sa_spill->db_size, &i, &spill_used, &dummy);
713eda14cbcSMatt Macy
714eda14cbcSMatt Macy if (spill_used > SPA_OLD_MAXBLOCKSIZE)
715eda14cbcSMatt Macy return (SET_ERROR(EFBIG));
716eda14cbcSMatt Macy
717eda14cbcSMatt Macy if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
718eda14cbcSMatt Macy hdl->sa_spill->db_size)
719eda14cbcSMatt Macy VERIFY(0 == sa_resize_spill(hdl,
720eda14cbcSMatt Macy BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy
723eda14cbcSMatt Macy /* setup starting pointers to lay down data */
724eda14cbcSMatt Macy data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
725eda14cbcSMatt Macy sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
726eda14cbcSMatt Macy buftype = SA_BONUS;
727eda14cbcSMatt Macy
728eda14cbcSMatt Macy attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
729eda14cbcSMatt Macy KM_SLEEP);
730eda14cbcSMatt Macy lot_count = 0;
731eda14cbcSMatt Macy
732eda14cbcSMatt Macy for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
733eda14cbcSMatt Macy uint16_t length;
734eda14cbcSMatt Macy
735eda14cbcSMatt Macy ASSERT(IS_P2ALIGNED(data_start, 8));
736eda14cbcSMatt Macy attrs[i] = attr_desc[i].sa_attr;
737eda14cbcSMatt Macy length = SA_REGISTERED_LEN(sa, attrs[i]);
738eda14cbcSMatt Macy if (length == 0)
739eda14cbcSMatt Macy length = attr_desc[i].sa_length;
740eda14cbcSMatt Macy
741eda14cbcSMatt Macy if (spilling && i == spill_idx) { /* switch to spill buffer */
742eda14cbcSMatt Macy VERIFY(bonustype == DMU_OT_SA);
743eda14cbcSMatt Macy if (buftype == SA_BONUS && !sa->sa_force_spill) {
744eda14cbcSMatt Macy sa_find_layout(hdl->sa_os, hash, attrs_start,
745eda14cbcSMatt Macy lot_count, tx, &lot);
746eda14cbcSMatt Macy SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
747eda14cbcSMatt Macy }
748eda14cbcSMatt Macy
749eda14cbcSMatt Macy buftype = SA_SPILL;
750eda14cbcSMatt Macy hash = -1ULL;
751eda14cbcSMatt Macy len_idx = 0;
752eda14cbcSMatt Macy
753eda14cbcSMatt Macy sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
754eda14cbcSMatt Macy sahdr->sa_magic = SA_MAGIC;
755eda14cbcSMatt Macy data_start = (void *)((uintptr_t)sahdr +
756eda14cbcSMatt Macy spillhdrsize);
757eda14cbcSMatt Macy attrs_start = &attrs[i];
758eda14cbcSMatt Macy lot_count = 0;
759eda14cbcSMatt Macy }
760eda14cbcSMatt Macy hash ^= SA_ATTR_HASH(attrs[i]);
761eda14cbcSMatt Macy attr_desc[i].sa_addr = data_start;
762eda14cbcSMatt Macy attr_desc[i].sa_size = length;
763eda14cbcSMatt Macy SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
764eda14cbcSMatt Macy data_start, length);
765eda14cbcSMatt Macy if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
766eda14cbcSMatt Macy sahdr->sa_lengths[len_idx++] = length;
767eda14cbcSMatt Macy }
768eda14cbcSMatt Macy data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
769eda14cbcSMatt Macy length), 8);
770eda14cbcSMatt Macy lot_count++;
771eda14cbcSMatt Macy }
772eda14cbcSMatt Macy
773eda14cbcSMatt Macy sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
774eda14cbcSMatt Macy
775eda14cbcSMatt Macy /*
776eda14cbcSMatt Macy * Verify that old znodes always have layout number 0.
777eda14cbcSMatt Macy * Must be DMU_OT_SA for arbitrary layouts
778eda14cbcSMatt Macy */
779eda14cbcSMatt Macy VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
780eda14cbcSMatt Macy (bonustype == DMU_OT_SA && lot->lot_num > 1));
781eda14cbcSMatt Macy
782eda14cbcSMatt Macy if (bonustype == DMU_OT_SA) {
783eda14cbcSMatt Macy SA_SET_HDR(sahdr, lot->lot_num,
784eda14cbcSMatt Macy buftype == SA_BONUS ? hdrsize : spillhdrsize);
785eda14cbcSMatt Macy }
786eda14cbcSMatt Macy
787eda14cbcSMatt Macy kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
788eda14cbcSMatt Macy if (hdl->sa_bonus_tab) {
789eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
790eda14cbcSMatt Macy hdl->sa_bonus_tab = NULL;
791eda14cbcSMatt Macy }
792eda14cbcSMatt Macy if (!sa->sa_force_spill)
793eda14cbcSMatt Macy VERIFY(0 == sa_build_index(hdl, SA_BONUS));
794eda14cbcSMatt Macy if (hdl->sa_spill) {
795eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
796eda14cbcSMatt Macy if (!spilling) {
797eda14cbcSMatt Macy /*
798eda14cbcSMatt Macy * remove spill block that is no longer needed.
799eda14cbcSMatt Macy */
800eda14cbcSMatt Macy dmu_buf_rele(hdl->sa_spill, NULL);
801eda14cbcSMatt Macy hdl->sa_spill = NULL;
802eda14cbcSMatt Macy hdl->sa_spill_tab = NULL;
803eda14cbcSMatt Macy VERIFY(0 == dmu_rm_spill(hdl->sa_os,
804eda14cbcSMatt Macy sa_handle_object(hdl), tx));
805eda14cbcSMatt Macy } else {
806eda14cbcSMatt Macy VERIFY(0 == sa_build_index(hdl, SA_SPILL));
807eda14cbcSMatt Macy }
808eda14cbcSMatt Macy }
809eda14cbcSMatt Macy
810eda14cbcSMatt Macy return (0);
811eda14cbcSMatt Macy }
812eda14cbcSMatt Macy
813eda14cbcSMatt Macy static void
sa_free_attr_table(sa_os_t * sa)814eda14cbcSMatt Macy sa_free_attr_table(sa_os_t *sa)
815eda14cbcSMatt Macy {
816eda14cbcSMatt Macy int i;
817eda14cbcSMatt Macy
818eda14cbcSMatt Macy if (sa->sa_attr_table == NULL)
819eda14cbcSMatt Macy return;
820eda14cbcSMatt Macy
821eda14cbcSMatt Macy for (i = 0; i != sa->sa_num_attrs; i++) {
822eda14cbcSMatt Macy if (sa->sa_attr_table[i].sa_name)
823eda14cbcSMatt Macy kmem_free(sa->sa_attr_table[i].sa_name,
824eda14cbcSMatt Macy strlen(sa->sa_attr_table[i].sa_name) + 1);
825eda14cbcSMatt Macy }
826eda14cbcSMatt Macy
827eda14cbcSMatt Macy kmem_free(sa->sa_attr_table,
828eda14cbcSMatt Macy sizeof (sa_attr_table_t) * sa->sa_num_attrs);
829eda14cbcSMatt Macy
830eda14cbcSMatt Macy sa->sa_attr_table = NULL;
831eda14cbcSMatt Macy }
832eda14cbcSMatt Macy
833eda14cbcSMatt Macy static int
sa_attr_table_setup(objset_t * os,const sa_attr_reg_t * reg_attrs,int count)834e92ffd9bSMartin Matuska sa_attr_table_setup(objset_t *os, const sa_attr_reg_t *reg_attrs, int count)
835eda14cbcSMatt Macy {
836eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
837eda14cbcSMatt Macy uint64_t sa_attr_count = 0;
838eda14cbcSMatt Macy uint64_t sa_reg_count = 0;
839eda14cbcSMatt Macy int error = 0;
840eda14cbcSMatt Macy uint64_t attr_value;
841eda14cbcSMatt Macy sa_attr_table_t *tb;
842eda14cbcSMatt Macy zap_cursor_t zc;
843*7a7741afSMartin Matuska zap_attribute_t *za;
844eda14cbcSMatt Macy int registered_count = 0;
845eda14cbcSMatt Macy int i;
846eda14cbcSMatt Macy dmu_objset_type_t ostype = dmu_objset_type(os);
847eda14cbcSMatt Macy
848eda14cbcSMatt Macy sa->sa_user_table =
849eda14cbcSMatt Macy kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
850eda14cbcSMatt Macy sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
851eda14cbcSMatt Macy
852eda14cbcSMatt Macy if (sa->sa_reg_attr_obj != 0) {
853eda14cbcSMatt Macy error = zap_count(os, sa->sa_reg_attr_obj,
854eda14cbcSMatt Macy &sa_attr_count);
855eda14cbcSMatt Macy
856eda14cbcSMatt Macy /*
857eda14cbcSMatt Macy * Make sure we retrieved a count and that it isn't zero
858eda14cbcSMatt Macy */
859eda14cbcSMatt Macy if (error || (error == 0 && sa_attr_count == 0)) {
860eda14cbcSMatt Macy if (error == 0)
861eda14cbcSMatt Macy error = SET_ERROR(EINVAL);
862eda14cbcSMatt Macy goto bail;
863eda14cbcSMatt Macy }
864eda14cbcSMatt Macy sa_reg_count = sa_attr_count;
865eda14cbcSMatt Macy }
866eda14cbcSMatt Macy
867eda14cbcSMatt Macy if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
868eda14cbcSMatt Macy sa_attr_count += sa_legacy_attr_count;
869eda14cbcSMatt Macy
870eda14cbcSMatt Macy /* Allocate attribute numbers for attributes that aren't registered */
871eda14cbcSMatt Macy for (i = 0; i != count; i++) {
872eda14cbcSMatt Macy boolean_t found = B_FALSE;
873eda14cbcSMatt Macy int j;
874eda14cbcSMatt Macy
875eda14cbcSMatt Macy if (ostype == DMU_OST_ZFS) {
876eda14cbcSMatt Macy for (j = 0; j != sa_legacy_attr_count; j++) {
877eda14cbcSMatt Macy if (strcmp(reg_attrs[i].sa_name,
878eda14cbcSMatt Macy sa_legacy_attrs[j].sa_name) == 0) {
879eda14cbcSMatt Macy sa->sa_user_table[i] =
880eda14cbcSMatt Macy sa_legacy_attrs[j].sa_attr;
881eda14cbcSMatt Macy found = B_TRUE;
882eda14cbcSMatt Macy }
883eda14cbcSMatt Macy }
884eda14cbcSMatt Macy }
885eda14cbcSMatt Macy if (found)
886eda14cbcSMatt Macy continue;
887eda14cbcSMatt Macy
888eda14cbcSMatt Macy if (sa->sa_reg_attr_obj)
889eda14cbcSMatt Macy error = zap_lookup(os, sa->sa_reg_attr_obj,
890eda14cbcSMatt Macy reg_attrs[i].sa_name, 8, 1, &attr_value);
891eda14cbcSMatt Macy else
892eda14cbcSMatt Macy error = SET_ERROR(ENOENT);
893eda14cbcSMatt Macy switch (error) {
894eda14cbcSMatt Macy case ENOENT:
895eda14cbcSMatt Macy sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
896eda14cbcSMatt Macy sa_attr_count++;
897eda14cbcSMatt Macy break;
898eda14cbcSMatt Macy case 0:
899eda14cbcSMatt Macy sa->sa_user_table[i] = ATTR_NUM(attr_value);
900eda14cbcSMatt Macy break;
901eda14cbcSMatt Macy default:
902eda14cbcSMatt Macy goto bail;
903eda14cbcSMatt Macy }
904eda14cbcSMatt Macy }
905eda14cbcSMatt Macy
906eda14cbcSMatt Macy sa->sa_num_attrs = sa_attr_count;
907eda14cbcSMatt Macy tb = sa->sa_attr_table =
908eda14cbcSMatt Macy kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
909eda14cbcSMatt Macy
910eda14cbcSMatt Macy /*
911eda14cbcSMatt Macy * Attribute table is constructed from requested attribute list,
912eda14cbcSMatt Macy * previously foreign registered attributes, and also the legacy
913eda14cbcSMatt Macy * ZPL set of attributes.
914eda14cbcSMatt Macy */
915eda14cbcSMatt Macy
916eda14cbcSMatt Macy if (sa->sa_reg_attr_obj) {
917*7a7741afSMartin Matuska za = zap_attribute_alloc();
918eda14cbcSMatt Macy for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
919*7a7741afSMartin Matuska (error = zap_cursor_retrieve(&zc, za)) == 0;
920eda14cbcSMatt Macy zap_cursor_advance(&zc)) {
921eda14cbcSMatt Macy uint64_t value;
922*7a7741afSMartin Matuska value = za->za_first_integer;
923eda14cbcSMatt Macy
924eda14cbcSMatt Macy registered_count++;
925eda14cbcSMatt Macy tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
926eda14cbcSMatt Macy tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
927eda14cbcSMatt Macy tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
928eda14cbcSMatt Macy tb[ATTR_NUM(value)].sa_registered = B_TRUE;
929eda14cbcSMatt Macy
930eda14cbcSMatt Macy if (tb[ATTR_NUM(value)].sa_name) {
931eda14cbcSMatt Macy continue;
932eda14cbcSMatt Macy }
933eda14cbcSMatt Macy tb[ATTR_NUM(value)].sa_name =
934*7a7741afSMartin Matuska kmem_zalloc(strlen(za->za_name) +1, KM_SLEEP);
935*7a7741afSMartin Matuska (void) strlcpy(tb[ATTR_NUM(value)].sa_name, za->za_name,
936*7a7741afSMartin Matuska strlen(za->za_name) +1);
937eda14cbcSMatt Macy }
938eda14cbcSMatt Macy zap_cursor_fini(&zc);
939*7a7741afSMartin Matuska zap_attribute_free(za);
940eda14cbcSMatt Macy /*
941eda14cbcSMatt Macy * Make sure we processed the correct number of registered
942eda14cbcSMatt Macy * attributes
943eda14cbcSMatt Macy */
944eda14cbcSMatt Macy if (registered_count != sa_reg_count) {
945eda14cbcSMatt Macy ASSERT(error != 0);
946eda14cbcSMatt Macy goto bail;
947eda14cbcSMatt Macy }
948eda14cbcSMatt Macy
949eda14cbcSMatt Macy }
950eda14cbcSMatt Macy
951eda14cbcSMatt Macy if (ostype == DMU_OST_ZFS) {
952eda14cbcSMatt Macy for (i = 0; i != sa_legacy_attr_count; i++) {
953eda14cbcSMatt Macy if (tb[i].sa_name)
954eda14cbcSMatt Macy continue;
955eda14cbcSMatt Macy tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
956eda14cbcSMatt Macy tb[i].sa_length = sa_legacy_attrs[i].sa_length;
957eda14cbcSMatt Macy tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
958eda14cbcSMatt Macy tb[i].sa_registered = B_FALSE;
959eda14cbcSMatt Macy tb[i].sa_name =
960eda14cbcSMatt Macy kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
961eda14cbcSMatt Macy KM_SLEEP);
962eda14cbcSMatt Macy (void) strlcpy(tb[i].sa_name,
963eda14cbcSMatt Macy sa_legacy_attrs[i].sa_name,
964eda14cbcSMatt Macy strlen(sa_legacy_attrs[i].sa_name) + 1);
965eda14cbcSMatt Macy }
966eda14cbcSMatt Macy }
967eda14cbcSMatt Macy
968eda14cbcSMatt Macy for (i = 0; i != count; i++) {
969eda14cbcSMatt Macy sa_attr_type_t attr_id;
970eda14cbcSMatt Macy
971eda14cbcSMatt Macy attr_id = sa->sa_user_table[i];
972eda14cbcSMatt Macy if (tb[attr_id].sa_name)
973eda14cbcSMatt Macy continue;
974eda14cbcSMatt Macy
975eda14cbcSMatt Macy tb[attr_id].sa_length = reg_attrs[i].sa_length;
976eda14cbcSMatt Macy tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
977eda14cbcSMatt Macy tb[attr_id].sa_attr = attr_id;
978eda14cbcSMatt Macy tb[attr_id].sa_name =
979eda14cbcSMatt Macy kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
980eda14cbcSMatt Macy (void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
981eda14cbcSMatt Macy strlen(reg_attrs[i].sa_name) + 1);
982eda14cbcSMatt Macy }
983eda14cbcSMatt Macy
984eda14cbcSMatt Macy sa->sa_need_attr_registration =
985eda14cbcSMatt Macy (sa_attr_count != registered_count);
986eda14cbcSMatt Macy
987eda14cbcSMatt Macy return (0);
988eda14cbcSMatt Macy bail:
989eda14cbcSMatt Macy kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
990eda14cbcSMatt Macy sa->sa_user_table = NULL;
991eda14cbcSMatt Macy sa_free_attr_table(sa);
992eda14cbcSMatt Macy ASSERT(error != 0);
993eda14cbcSMatt Macy return (error);
994eda14cbcSMatt Macy }
995eda14cbcSMatt Macy
996eda14cbcSMatt Macy int
sa_setup(objset_t * os,uint64_t sa_obj,const sa_attr_reg_t * reg_attrs,int count,sa_attr_type_t ** user_table)997e92ffd9bSMartin Matuska sa_setup(objset_t *os, uint64_t sa_obj, const sa_attr_reg_t *reg_attrs,
998e92ffd9bSMartin Matuska int count, sa_attr_type_t **user_table)
999eda14cbcSMatt Macy {
1000eda14cbcSMatt Macy zap_cursor_t zc;
1001*7a7741afSMartin Matuska zap_attribute_t *za;
1002eda14cbcSMatt Macy sa_os_t *sa;
1003eda14cbcSMatt Macy dmu_objset_type_t ostype = dmu_objset_type(os);
1004eda14cbcSMatt Macy sa_attr_type_t *tb;
1005eda14cbcSMatt Macy int error;
1006eda14cbcSMatt Macy
1007eda14cbcSMatt Macy mutex_enter(&os->os_user_ptr_lock);
1008eda14cbcSMatt Macy if (os->os_sa) {
1009eda14cbcSMatt Macy mutex_enter(&os->os_sa->sa_lock);
1010eda14cbcSMatt Macy mutex_exit(&os->os_user_ptr_lock);
1011eda14cbcSMatt Macy tb = os->os_sa->sa_user_table;
1012eda14cbcSMatt Macy mutex_exit(&os->os_sa->sa_lock);
1013eda14cbcSMatt Macy *user_table = tb;
1014eda14cbcSMatt Macy return (0);
1015eda14cbcSMatt Macy }
1016eda14cbcSMatt Macy
1017eda14cbcSMatt Macy sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
1018eda14cbcSMatt Macy mutex_init(&sa->sa_lock, NULL, MUTEX_NOLOCKDEP, NULL);
1019eda14cbcSMatt Macy sa->sa_master_obj = sa_obj;
1020eda14cbcSMatt Macy
1021eda14cbcSMatt Macy os->os_sa = sa;
1022eda14cbcSMatt Macy mutex_enter(&sa->sa_lock);
1023eda14cbcSMatt Macy mutex_exit(&os->os_user_ptr_lock);
1024eda14cbcSMatt Macy avl_create(&sa->sa_layout_num_tree, layout_num_compare,
1025eda14cbcSMatt Macy sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
1026eda14cbcSMatt Macy avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
1027eda14cbcSMatt Macy sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
1028eda14cbcSMatt Macy
1029eda14cbcSMatt Macy if (sa_obj) {
1030eda14cbcSMatt Macy error = zap_lookup(os, sa_obj, SA_LAYOUTS,
1031eda14cbcSMatt Macy 8, 1, &sa->sa_layout_attr_obj);
1032eda14cbcSMatt Macy if (error != 0 && error != ENOENT)
1033eda14cbcSMatt Macy goto fail;
1034eda14cbcSMatt Macy error = zap_lookup(os, sa_obj, SA_REGISTRY,
1035eda14cbcSMatt Macy 8, 1, &sa->sa_reg_attr_obj);
1036eda14cbcSMatt Macy if (error != 0 && error != ENOENT)
1037eda14cbcSMatt Macy goto fail;
1038eda14cbcSMatt Macy }
1039eda14cbcSMatt Macy
1040eda14cbcSMatt Macy if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
1041eda14cbcSMatt Macy goto fail;
1042eda14cbcSMatt Macy
1043eda14cbcSMatt Macy if (sa->sa_layout_attr_obj != 0) {
1044eda14cbcSMatt Macy uint64_t layout_count;
1045eda14cbcSMatt Macy
1046eda14cbcSMatt Macy error = zap_count(os, sa->sa_layout_attr_obj,
1047eda14cbcSMatt Macy &layout_count);
1048eda14cbcSMatt Macy
1049eda14cbcSMatt Macy /*
1050eda14cbcSMatt Macy * Layout number count should be > 0
1051eda14cbcSMatt Macy */
1052eda14cbcSMatt Macy if (error || (error == 0 && layout_count == 0)) {
1053eda14cbcSMatt Macy if (error == 0)
1054eda14cbcSMatt Macy error = SET_ERROR(EINVAL);
1055eda14cbcSMatt Macy goto fail;
1056eda14cbcSMatt Macy }
1057eda14cbcSMatt Macy
1058*7a7741afSMartin Matuska za = zap_attribute_alloc();
1059eda14cbcSMatt Macy for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
1060*7a7741afSMartin Matuska (error = zap_cursor_retrieve(&zc, za)) == 0;
1061eda14cbcSMatt Macy zap_cursor_advance(&zc)) {
1062eda14cbcSMatt Macy sa_attr_type_t *lot_attrs;
1063eda14cbcSMatt Macy uint64_t lot_num;
1064eda14cbcSMatt Macy
1065eda14cbcSMatt Macy lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
1066*7a7741afSMartin Matuska za->za_num_integers, KM_SLEEP);
1067eda14cbcSMatt Macy
1068eda14cbcSMatt Macy if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
1069*7a7741afSMartin Matuska za->za_name, 2, za->za_num_integers,
1070eda14cbcSMatt Macy lot_attrs))) != 0) {
1071eda14cbcSMatt Macy kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1072*7a7741afSMartin Matuska za->za_num_integers);
1073eda14cbcSMatt Macy break;
1074eda14cbcSMatt Macy }
1075*7a7741afSMartin Matuska VERIFY0(ddi_strtoull(za->za_name, NULL, 10,
1076716fd348SMartin Matuska (unsigned long long *)&lot_num));
1077eda14cbcSMatt Macy
1078eda14cbcSMatt Macy (void) sa_add_layout_entry(os, lot_attrs,
1079*7a7741afSMartin Matuska za->za_num_integers, lot_num,
1080eda14cbcSMatt Macy sa_layout_info_hash(lot_attrs,
1081*7a7741afSMartin Matuska za->za_num_integers), B_FALSE, NULL);
1082eda14cbcSMatt Macy kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1083*7a7741afSMartin Matuska za->za_num_integers);
1084eda14cbcSMatt Macy }
1085eda14cbcSMatt Macy zap_cursor_fini(&zc);
1086*7a7741afSMartin Matuska zap_attribute_free(za);
1087eda14cbcSMatt Macy
1088eda14cbcSMatt Macy /*
1089eda14cbcSMatt Macy * Make sure layout count matches number of entries added
1090eda14cbcSMatt Macy * to AVL tree
1091eda14cbcSMatt Macy */
1092eda14cbcSMatt Macy if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
1093eda14cbcSMatt Macy ASSERT(error != 0);
1094eda14cbcSMatt Macy goto fail;
1095eda14cbcSMatt Macy }
1096eda14cbcSMatt Macy }
1097eda14cbcSMatt Macy
1098eda14cbcSMatt Macy /* Add special layout number for old ZNODES */
1099eda14cbcSMatt Macy if (ostype == DMU_OST_ZFS) {
1100eda14cbcSMatt Macy (void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
1101eda14cbcSMatt Macy sa_legacy_attr_count, 0,
1102eda14cbcSMatt Macy sa_layout_info_hash(sa_legacy_zpl_layout,
1103eda14cbcSMatt Macy sa_legacy_attr_count), B_FALSE, NULL);
1104eda14cbcSMatt Macy
1105eda14cbcSMatt Macy (void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
1106eda14cbcSMatt Macy 0, B_FALSE, NULL);
1107eda14cbcSMatt Macy }
1108eda14cbcSMatt Macy *user_table = os->os_sa->sa_user_table;
1109eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1110eda14cbcSMatt Macy return (0);
1111eda14cbcSMatt Macy fail:
1112eda14cbcSMatt Macy os->os_sa = NULL;
1113eda14cbcSMatt Macy sa_free_attr_table(sa);
1114eda14cbcSMatt Macy if (sa->sa_user_table)
1115eda14cbcSMatt Macy kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1116eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1117eda14cbcSMatt Macy avl_destroy(&sa->sa_layout_hash_tree);
1118eda14cbcSMatt Macy avl_destroy(&sa->sa_layout_num_tree);
1119eda14cbcSMatt Macy mutex_destroy(&sa->sa_lock);
1120eda14cbcSMatt Macy kmem_free(sa, sizeof (sa_os_t));
1121eda14cbcSMatt Macy return ((error == ECKSUM) ? EIO : error);
1122eda14cbcSMatt Macy }
1123eda14cbcSMatt Macy
1124eda14cbcSMatt Macy void
sa_tear_down(objset_t * os)1125eda14cbcSMatt Macy sa_tear_down(objset_t *os)
1126eda14cbcSMatt Macy {
1127eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
1128eda14cbcSMatt Macy sa_lot_t *layout;
1129eda14cbcSMatt Macy void *cookie;
1130eda14cbcSMatt Macy
1131eda14cbcSMatt Macy kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1132eda14cbcSMatt Macy
1133eda14cbcSMatt Macy /* Free up attr table */
1134eda14cbcSMatt Macy
1135eda14cbcSMatt Macy sa_free_attr_table(sa);
1136eda14cbcSMatt Macy
1137eda14cbcSMatt Macy cookie = NULL;
1138eda14cbcSMatt Macy while ((layout =
1139eda14cbcSMatt Macy avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie))) {
1140eda14cbcSMatt Macy sa_idx_tab_t *tab;
1141eda14cbcSMatt Macy while ((tab = list_head(&layout->lot_idx_tab))) {
1142eda14cbcSMatt Macy ASSERT(zfs_refcount_count(&tab->sa_refcount));
1143eda14cbcSMatt Macy sa_idx_tab_rele(os, tab);
1144eda14cbcSMatt Macy }
1145eda14cbcSMatt Macy }
1146eda14cbcSMatt Macy
1147eda14cbcSMatt Macy cookie = NULL;
1148eda14cbcSMatt Macy while ((layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie))) {
1149eda14cbcSMatt Macy kmem_free(layout->lot_attrs,
1150eda14cbcSMatt Macy sizeof (sa_attr_type_t) * layout->lot_attr_count);
1151eda14cbcSMatt Macy kmem_free(layout, sizeof (sa_lot_t));
1152eda14cbcSMatt Macy }
1153eda14cbcSMatt Macy
1154eda14cbcSMatt Macy avl_destroy(&sa->sa_layout_hash_tree);
1155eda14cbcSMatt Macy avl_destroy(&sa->sa_layout_num_tree);
1156eda14cbcSMatt Macy mutex_destroy(&sa->sa_lock);
1157eda14cbcSMatt Macy
1158eda14cbcSMatt Macy kmem_free(sa, sizeof (sa_os_t));
1159eda14cbcSMatt Macy os->os_sa = NULL;
1160eda14cbcSMatt Macy }
1161eda14cbcSMatt Macy
1162eda14cbcSMatt Macy static void
sa_build_idx_tab(void * hdr,void * attr_addr,sa_attr_type_t attr,uint16_t length,int length_idx,boolean_t var_length,void * userp)1163eda14cbcSMatt Macy sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
1164eda14cbcSMatt Macy uint16_t length, int length_idx, boolean_t var_length, void *userp)
1165eda14cbcSMatt Macy {
1166eda14cbcSMatt Macy sa_idx_tab_t *idx_tab = userp;
1167eda14cbcSMatt Macy
1168eda14cbcSMatt Macy if (var_length) {
1169eda14cbcSMatt Macy ASSERT(idx_tab->sa_variable_lengths);
1170eda14cbcSMatt Macy idx_tab->sa_variable_lengths[length_idx] = length;
1171eda14cbcSMatt Macy }
1172eda14cbcSMatt Macy TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
1173eda14cbcSMatt Macy (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
1174eda14cbcSMatt Macy }
1175eda14cbcSMatt Macy
1176eda14cbcSMatt Macy static void
sa_attr_iter(objset_t * os,sa_hdr_phys_t * hdr,dmu_object_type_t type,sa_iterfunc_t func,sa_lot_t * tab,void * userp)1177eda14cbcSMatt Macy sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
1178eda14cbcSMatt Macy sa_iterfunc_t func, sa_lot_t *tab, void *userp)
1179eda14cbcSMatt Macy {
1180eda14cbcSMatt Macy void *data_start;
1181eda14cbcSMatt Macy sa_lot_t *tb = tab;
1182eda14cbcSMatt Macy sa_lot_t search;
1183eda14cbcSMatt Macy avl_index_t loc;
1184eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
1185eda14cbcSMatt Macy int i;
1186eda14cbcSMatt Macy uint16_t *length_start = NULL;
1187eda14cbcSMatt Macy uint8_t length_idx = 0;
1188eda14cbcSMatt Macy
1189eda14cbcSMatt Macy if (tab == NULL) {
1190eda14cbcSMatt Macy search.lot_num = SA_LAYOUT_NUM(hdr, type);
1191eda14cbcSMatt Macy tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1192eda14cbcSMatt Macy ASSERT(tb);
1193eda14cbcSMatt Macy }
1194eda14cbcSMatt Macy
1195eda14cbcSMatt Macy if (IS_SA_BONUSTYPE(type)) {
1196eda14cbcSMatt Macy data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
1197eda14cbcSMatt Macy offsetof(sa_hdr_phys_t, sa_lengths) +
1198eda14cbcSMatt Macy (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
1199eda14cbcSMatt Macy length_start = hdr->sa_lengths;
1200eda14cbcSMatt Macy } else {
1201eda14cbcSMatt Macy data_start = hdr;
1202eda14cbcSMatt Macy }
1203eda14cbcSMatt Macy
1204eda14cbcSMatt Macy for (i = 0; i != tb->lot_attr_count; i++) {
1205eda14cbcSMatt Macy int attr_length, reg_length;
1206eda14cbcSMatt Macy uint8_t idx_len;
1207eda14cbcSMatt Macy
1208eda14cbcSMatt Macy reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
12092a58b312SMartin Matuska IMPLY(reg_length == 0, IS_SA_BONUSTYPE(type));
1210eda14cbcSMatt Macy if (reg_length) {
1211eda14cbcSMatt Macy attr_length = reg_length;
1212eda14cbcSMatt Macy idx_len = 0;
1213eda14cbcSMatt Macy } else {
1214eda14cbcSMatt Macy attr_length = length_start[length_idx];
1215eda14cbcSMatt Macy idx_len = length_idx++;
1216eda14cbcSMatt Macy }
1217eda14cbcSMatt Macy
1218eda14cbcSMatt Macy func(hdr, data_start, tb->lot_attrs[i], attr_length,
1219eda14cbcSMatt Macy idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
1220eda14cbcSMatt Macy
1221eda14cbcSMatt Macy data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
1222eda14cbcSMatt Macy attr_length), 8);
1223eda14cbcSMatt Macy }
1224eda14cbcSMatt Macy }
1225eda14cbcSMatt Macy
1226eda14cbcSMatt Macy static void
sa_byteswap_cb(void * hdr,void * attr_addr,sa_attr_type_t attr,uint16_t length,int length_idx,boolean_t variable_length,void * userp)1227eda14cbcSMatt Macy sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
1228eda14cbcSMatt Macy uint16_t length, int length_idx, boolean_t variable_length, void *userp)
1229eda14cbcSMatt Macy {
1230e92ffd9bSMartin Matuska (void) hdr, (void) length_idx, (void) variable_length;
1231eda14cbcSMatt Macy sa_handle_t *hdl = userp;
1232eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
1233eda14cbcSMatt Macy
1234eda14cbcSMatt Macy sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
1235eda14cbcSMatt Macy }
1236eda14cbcSMatt Macy
1237eda14cbcSMatt Macy static void
sa_byteswap(sa_handle_t * hdl,sa_buf_type_t buftype)1238eda14cbcSMatt Macy sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
1239eda14cbcSMatt Macy {
1240eda14cbcSMatt Macy sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1241eda14cbcSMatt Macy dmu_buf_impl_t *db;
1242eda14cbcSMatt Macy int num_lengths = 1;
1243eda14cbcSMatt Macy int i;
1244eda14cbcSMatt Macy sa_os_t *sa __maybe_unused = hdl->sa_os->os_sa;
1245eda14cbcSMatt Macy
1246eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&sa->sa_lock));
1247eda14cbcSMatt Macy if (sa_hdr_phys->sa_magic == SA_MAGIC)
1248eda14cbcSMatt Macy return;
1249eda14cbcSMatt Macy
1250eda14cbcSMatt Macy db = SA_GET_DB(hdl, buftype);
1251eda14cbcSMatt Macy
1252eda14cbcSMatt Macy if (buftype == SA_SPILL) {
1253eda14cbcSMatt Macy arc_release(db->db_buf, NULL);
1254eda14cbcSMatt Macy arc_buf_thaw(db->db_buf);
1255eda14cbcSMatt Macy }
1256eda14cbcSMatt Macy
1257eda14cbcSMatt Macy sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
1258eda14cbcSMatt Macy sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
1259eda14cbcSMatt Macy
1260eda14cbcSMatt Macy /*
1261eda14cbcSMatt Macy * Determine number of variable lengths in header
1262eda14cbcSMatt Macy * The standard 8 byte header has one for free and a
1263eda14cbcSMatt Macy * 16 byte header would have 4 + 1;
1264eda14cbcSMatt Macy */
1265eda14cbcSMatt Macy if (SA_HDR_SIZE(sa_hdr_phys) > 8)
1266eda14cbcSMatt Macy num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
1267eda14cbcSMatt Macy for (i = 0; i != num_lengths; i++)
1268eda14cbcSMatt Macy sa_hdr_phys->sa_lengths[i] =
1269eda14cbcSMatt Macy BSWAP_16(sa_hdr_phys->sa_lengths[i]);
1270eda14cbcSMatt Macy
1271eda14cbcSMatt Macy sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
1272eda14cbcSMatt Macy sa_byteswap_cb, NULL, hdl);
1273eda14cbcSMatt Macy
1274eda14cbcSMatt Macy if (buftype == SA_SPILL)
1275eda14cbcSMatt Macy arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
1276eda14cbcSMatt Macy }
1277eda14cbcSMatt Macy
1278eda14cbcSMatt Macy static int
sa_build_index(sa_handle_t * hdl,sa_buf_type_t buftype)1279eda14cbcSMatt Macy sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
1280eda14cbcSMatt Macy {
1281eda14cbcSMatt Macy sa_hdr_phys_t *sa_hdr_phys;
1282eda14cbcSMatt Macy dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
1283eda14cbcSMatt Macy dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
1284eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
1285eda14cbcSMatt Macy sa_idx_tab_t *idx_tab;
1286eda14cbcSMatt Macy
1287eda14cbcSMatt Macy sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1288eda14cbcSMatt Macy
1289eda14cbcSMatt Macy mutex_enter(&sa->sa_lock);
1290eda14cbcSMatt Macy
1291eda14cbcSMatt Macy /* Do we need to byteswap? */
1292eda14cbcSMatt Macy
1293eda14cbcSMatt Macy /* only check if not old znode */
1294eda14cbcSMatt Macy if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
1295eda14cbcSMatt Macy sa_hdr_phys->sa_magic != 0) {
1296eda14cbcSMatt Macy if (BSWAP_32(sa_hdr_phys->sa_magic) != SA_MAGIC) {
1297eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1298eda14cbcSMatt Macy zfs_dbgmsg("Buffer Header: %x != SA_MAGIC:%x "
1299eda14cbcSMatt Macy "object=%#llx\n", sa_hdr_phys->sa_magic, SA_MAGIC,
130033b8c039SMartin Matuska (u_longlong_t)db->db.db_object);
1301eda14cbcSMatt Macy return (SET_ERROR(EIO));
1302eda14cbcSMatt Macy }
1303eda14cbcSMatt Macy sa_byteswap(hdl, buftype);
1304eda14cbcSMatt Macy }
1305eda14cbcSMatt Macy
1306eda14cbcSMatt Macy idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
1307eda14cbcSMatt Macy
1308eda14cbcSMatt Macy if (buftype == SA_BONUS)
1309eda14cbcSMatt Macy hdl->sa_bonus_tab = idx_tab;
1310eda14cbcSMatt Macy else
1311eda14cbcSMatt Macy hdl->sa_spill_tab = idx_tab;
1312eda14cbcSMatt Macy
1313eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1314eda14cbcSMatt Macy return (0);
1315eda14cbcSMatt Macy }
1316eda14cbcSMatt Macy
1317eda14cbcSMatt Macy static void
sa_evict_sync(void * dbu)1318eda14cbcSMatt Macy sa_evict_sync(void *dbu)
1319eda14cbcSMatt Macy {
1320e92ffd9bSMartin Matuska (void) dbu;
1321eda14cbcSMatt Macy panic("evicting sa dbuf\n");
1322eda14cbcSMatt Macy }
1323eda14cbcSMatt Macy
1324eda14cbcSMatt Macy static void
sa_idx_tab_rele(objset_t * os,void * arg)1325eda14cbcSMatt Macy sa_idx_tab_rele(objset_t *os, void *arg)
1326eda14cbcSMatt Macy {
1327eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
1328eda14cbcSMatt Macy sa_idx_tab_t *idx_tab = arg;
1329eda14cbcSMatt Macy
1330eda14cbcSMatt Macy if (idx_tab == NULL)
1331eda14cbcSMatt Macy return;
1332eda14cbcSMatt Macy
1333eda14cbcSMatt Macy mutex_enter(&sa->sa_lock);
1334eda14cbcSMatt Macy if (zfs_refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
1335eda14cbcSMatt Macy list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
1336eda14cbcSMatt Macy if (idx_tab->sa_variable_lengths)
1337eda14cbcSMatt Macy kmem_free(idx_tab->sa_variable_lengths,
1338eda14cbcSMatt Macy sizeof (uint16_t) *
1339eda14cbcSMatt Macy idx_tab->sa_layout->lot_var_sizes);
1340eda14cbcSMatt Macy zfs_refcount_destroy(&idx_tab->sa_refcount);
1341eda14cbcSMatt Macy kmem_free(idx_tab->sa_idx_tab,
1342eda14cbcSMatt Macy sizeof (uint32_t) * sa->sa_num_attrs);
1343eda14cbcSMatt Macy kmem_free(idx_tab, sizeof (sa_idx_tab_t));
1344eda14cbcSMatt Macy }
1345eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1346eda14cbcSMatt Macy }
1347eda14cbcSMatt Macy
1348eda14cbcSMatt Macy static void
sa_idx_tab_hold(objset_t * os,sa_idx_tab_t * idx_tab)1349eda14cbcSMatt Macy sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
1350eda14cbcSMatt Macy {
1351eda14cbcSMatt Macy sa_os_t *sa __maybe_unused = os->os_sa;
1352eda14cbcSMatt Macy
1353eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&sa->sa_lock));
1354eda14cbcSMatt Macy (void) zfs_refcount_add(&idx_tab->sa_refcount, NULL);
1355eda14cbcSMatt Macy }
1356eda14cbcSMatt Macy
1357eda14cbcSMatt Macy void
sa_spill_rele(sa_handle_t * hdl)1358eda14cbcSMatt Macy sa_spill_rele(sa_handle_t *hdl)
1359eda14cbcSMatt Macy {
1360eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1361eda14cbcSMatt Macy if (hdl->sa_spill) {
1362eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1363eda14cbcSMatt Macy dmu_buf_rele(hdl->sa_spill, NULL);
1364eda14cbcSMatt Macy hdl->sa_spill = NULL;
1365eda14cbcSMatt Macy hdl->sa_spill_tab = NULL;
1366eda14cbcSMatt Macy }
1367eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1368eda14cbcSMatt Macy }
1369eda14cbcSMatt Macy
1370eda14cbcSMatt Macy void
sa_handle_destroy(sa_handle_t * hdl)1371eda14cbcSMatt Macy sa_handle_destroy(sa_handle_t *hdl)
1372eda14cbcSMatt Macy {
1373eda14cbcSMatt Macy dmu_buf_t *db = hdl->sa_bonus;
1374eda14cbcSMatt Macy
1375eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1376eda14cbcSMatt Macy (void) dmu_buf_remove_user(db, &hdl->sa_dbu);
1377eda14cbcSMatt Macy
1378eda14cbcSMatt Macy if (hdl->sa_bonus_tab)
1379eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
1380eda14cbcSMatt Macy
1381eda14cbcSMatt Macy if (hdl->sa_spill_tab)
1382eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1383eda14cbcSMatt Macy
1384eda14cbcSMatt Macy dmu_buf_rele(hdl->sa_bonus, NULL);
1385eda14cbcSMatt Macy
1386eda14cbcSMatt Macy if (hdl->sa_spill)
1387eda14cbcSMatt Macy dmu_buf_rele(hdl->sa_spill, NULL);
1388eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1389eda14cbcSMatt Macy
1390eda14cbcSMatt Macy kmem_cache_free(sa_cache, hdl);
1391eda14cbcSMatt Macy }
1392eda14cbcSMatt Macy
1393eda14cbcSMatt Macy int
sa_handle_get_from_db(objset_t * os,dmu_buf_t * db,void * userp,sa_handle_type_t hdl_type,sa_handle_t ** handlepp)1394eda14cbcSMatt Macy sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
1395eda14cbcSMatt Macy sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1396eda14cbcSMatt Macy {
1397eda14cbcSMatt Macy int error = 0;
1398eda14cbcSMatt Macy sa_handle_t *handle = NULL;
1399eda14cbcSMatt Macy #ifdef ZFS_DEBUG
1400eda14cbcSMatt Macy dmu_object_info_t doi;
1401eda14cbcSMatt Macy
1402eda14cbcSMatt Macy dmu_object_info_from_db(db, &doi);
1403eda14cbcSMatt Macy ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
1404eda14cbcSMatt Macy doi.doi_bonus_type == DMU_OT_ZNODE);
1405eda14cbcSMatt Macy #endif
1406eda14cbcSMatt Macy /* find handle, if it exists */
1407eda14cbcSMatt Macy /* if one doesn't exist then create a new one, and initialize it */
1408eda14cbcSMatt Macy
1409eda14cbcSMatt Macy if (hdl_type == SA_HDL_SHARED)
1410eda14cbcSMatt Macy handle = dmu_buf_get_user(db);
1411eda14cbcSMatt Macy
1412eda14cbcSMatt Macy if (handle == NULL) {
1413eda14cbcSMatt Macy sa_handle_t *winner = NULL;
1414eda14cbcSMatt Macy
1415eda14cbcSMatt Macy handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
1416eda14cbcSMatt Macy handle->sa_dbu.dbu_evict_func_sync = NULL;
1417eda14cbcSMatt Macy handle->sa_dbu.dbu_evict_func_async = NULL;
1418eda14cbcSMatt Macy handle->sa_userp = userp;
1419eda14cbcSMatt Macy handle->sa_bonus = db;
1420eda14cbcSMatt Macy handle->sa_os = os;
1421eda14cbcSMatt Macy handle->sa_spill = NULL;
1422eda14cbcSMatt Macy handle->sa_bonus_tab = NULL;
1423eda14cbcSMatt Macy handle->sa_spill_tab = NULL;
1424eda14cbcSMatt Macy
1425eda14cbcSMatt Macy error = sa_build_index(handle, SA_BONUS);
1426eda14cbcSMatt Macy
1427eda14cbcSMatt Macy if (hdl_type == SA_HDL_SHARED) {
1428eda14cbcSMatt Macy dmu_buf_init_user(&handle->sa_dbu, sa_evict_sync, NULL,
1429eda14cbcSMatt Macy NULL);
1430eda14cbcSMatt Macy winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1431eda14cbcSMatt Macy }
1432eda14cbcSMatt Macy
1433eda14cbcSMatt Macy if (winner != NULL) {
1434eda14cbcSMatt Macy kmem_cache_free(sa_cache, handle);
1435eda14cbcSMatt Macy handle = winner;
1436eda14cbcSMatt Macy }
1437eda14cbcSMatt Macy }
1438eda14cbcSMatt Macy *handlepp = handle;
1439eda14cbcSMatt Macy
1440eda14cbcSMatt Macy return (error);
1441eda14cbcSMatt Macy }
1442eda14cbcSMatt Macy
1443eda14cbcSMatt Macy int
sa_handle_get(objset_t * objset,uint64_t objid,void * userp,sa_handle_type_t hdl_type,sa_handle_t ** handlepp)1444eda14cbcSMatt Macy sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
1445eda14cbcSMatt Macy sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1446eda14cbcSMatt Macy {
1447eda14cbcSMatt Macy dmu_buf_t *db;
1448eda14cbcSMatt Macy int error;
1449eda14cbcSMatt Macy
1450eda14cbcSMatt Macy if ((error = dmu_bonus_hold(objset, objid, NULL, &db)))
1451eda14cbcSMatt Macy return (error);
1452eda14cbcSMatt Macy
1453eda14cbcSMatt Macy return (sa_handle_get_from_db(objset, db, userp, hdl_type,
1454eda14cbcSMatt Macy handlepp));
1455eda14cbcSMatt Macy }
1456eda14cbcSMatt Macy
1457eda14cbcSMatt Macy int
sa_buf_hold(objset_t * objset,uint64_t obj_num,const void * tag,dmu_buf_t ** db)1458a0b956f5SMartin Matuska sa_buf_hold(objset_t *objset, uint64_t obj_num, const void *tag, dmu_buf_t **db)
1459eda14cbcSMatt Macy {
1460eda14cbcSMatt Macy return (dmu_bonus_hold(objset, obj_num, tag, db));
1461eda14cbcSMatt Macy }
1462eda14cbcSMatt Macy
1463eda14cbcSMatt Macy void
sa_buf_rele(dmu_buf_t * db,const void * tag)1464a0b956f5SMartin Matuska sa_buf_rele(dmu_buf_t *db, const void *tag)
1465eda14cbcSMatt Macy {
1466eda14cbcSMatt Macy dmu_buf_rele(db, tag);
1467eda14cbcSMatt Macy }
1468eda14cbcSMatt Macy
1469eda14cbcSMatt Macy static int
sa_lookup_impl(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count)1470eda14cbcSMatt Macy sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
1471eda14cbcSMatt Macy {
1472eda14cbcSMatt Macy ASSERT(hdl);
1473eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&hdl->sa_lock));
1474eda14cbcSMatt Macy return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
1475eda14cbcSMatt Macy }
1476eda14cbcSMatt Macy
1477eda14cbcSMatt Macy static int
sa_lookup_locked(sa_handle_t * hdl,sa_attr_type_t attr,void * buf,uint32_t buflen)1478eda14cbcSMatt Macy sa_lookup_locked(sa_handle_t *hdl, sa_attr_type_t attr, void *buf,
1479eda14cbcSMatt Macy uint32_t buflen)
1480eda14cbcSMatt Macy {
1481eda14cbcSMatt Macy int error;
1482eda14cbcSMatt Macy sa_bulk_attr_t bulk;
1483eda14cbcSMatt Macy
1484eda14cbcSMatt Macy VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
1485eda14cbcSMatt Macy
1486eda14cbcSMatt Macy bulk.sa_attr = attr;
1487eda14cbcSMatt Macy bulk.sa_data = buf;
1488eda14cbcSMatt Macy bulk.sa_length = buflen;
1489eda14cbcSMatt Macy bulk.sa_data_func = NULL;
1490eda14cbcSMatt Macy
1491eda14cbcSMatt Macy ASSERT(hdl);
1492eda14cbcSMatt Macy error = sa_lookup_impl(hdl, &bulk, 1);
1493eda14cbcSMatt Macy return (error);
1494eda14cbcSMatt Macy }
1495eda14cbcSMatt Macy
1496eda14cbcSMatt Macy int
sa_lookup(sa_handle_t * hdl,sa_attr_type_t attr,void * buf,uint32_t buflen)1497eda14cbcSMatt Macy sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1498eda14cbcSMatt Macy {
1499eda14cbcSMatt Macy int error;
1500eda14cbcSMatt Macy
1501eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1502eda14cbcSMatt Macy error = sa_lookup_locked(hdl, attr, buf, buflen);
1503eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1504eda14cbcSMatt Macy
1505eda14cbcSMatt Macy return (error);
1506eda14cbcSMatt Macy }
1507eda14cbcSMatt Macy
1508ce4dcb97SMartin Matuska /*
1509ce4dcb97SMartin Matuska * Return size of an attribute
1510ce4dcb97SMartin Matuska */
1511ce4dcb97SMartin Matuska
1512ce4dcb97SMartin Matuska static int
sa_size_locked(sa_handle_t * hdl,sa_attr_type_t attr,int * size)1513ce4dcb97SMartin Matuska sa_size_locked(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
1514ce4dcb97SMartin Matuska {
1515ce4dcb97SMartin Matuska sa_bulk_attr_t bulk;
1516ce4dcb97SMartin Matuska int error;
1517ce4dcb97SMartin Matuska
1518ce4dcb97SMartin Matuska bulk.sa_data = NULL;
1519ce4dcb97SMartin Matuska bulk.sa_attr = attr;
1520ce4dcb97SMartin Matuska bulk.sa_data_func = NULL;
1521ce4dcb97SMartin Matuska
1522ce4dcb97SMartin Matuska ASSERT(hdl);
1523ce4dcb97SMartin Matuska ASSERT(MUTEX_HELD(&hdl->sa_lock));
1524ce4dcb97SMartin Matuska if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
1525ce4dcb97SMartin Matuska return (error);
1526ce4dcb97SMartin Matuska }
1527ce4dcb97SMartin Matuska *size = bulk.sa_size;
1528ce4dcb97SMartin Matuska
1529ce4dcb97SMartin Matuska return (0);
1530ce4dcb97SMartin Matuska }
1531ce4dcb97SMartin Matuska
1532ce4dcb97SMartin Matuska int
sa_size(sa_handle_t * hdl,sa_attr_type_t attr,int * size)1533ce4dcb97SMartin Matuska sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
1534ce4dcb97SMartin Matuska {
1535ce4dcb97SMartin Matuska int error;
1536ce4dcb97SMartin Matuska
1537ce4dcb97SMartin Matuska mutex_enter(&hdl->sa_lock);
1538ce4dcb97SMartin Matuska error = sa_size_locked(hdl, attr, size);
1539ce4dcb97SMartin Matuska mutex_exit(&hdl->sa_lock);
1540ce4dcb97SMartin Matuska
1541ce4dcb97SMartin Matuska return (error);
1542ce4dcb97SMartin Matuska }
1543ce4dcb97SMartin Matuska
1544eda14cbcSMatt Macy #ifdef _KERNEL
1545eda14cbcSMatt Macy int
sa_lookup_uio(sa_handle_t * hdl,sa_attr_type_t attr,zfs_uio_t * uio)1546184c1b94SMartin Matuska sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, zfs_uio_t *uio)
1547eda14cbcSMatt Macy {
1548eda14cbcSMatt Macy int error;
1549eda14cbcSMatt Macy sa_bulk_attr_t bulk;
1550eda14cbcSMatt Macy
1551eda14cbcSMatt Macy bulk.sa_data = NULL;
1552eda14cbcSMatt Macy bulk.sa_attr = attr;
1553eda14cbcSMatt Macy bulk.sa_data_func = NULL;
1554eda14cbcSMatt Macy
1555eda14cbcSMatt Macy ASSERT(hdl);
1556eda14cbcSMatt Macy
1557eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1558eda14cbcSMatt Macy if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
1559184c1b94SMartin Matuska error = zfs_uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
1560184c1b94SMartin Matuska zfs_uio_resid(uio)), UIO_READ, uio);
1561eda14cbcSMatt Macy }
1562eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1563eda14cbcSMatt Macy return (error);
1564eda14cbcSMatt Macy }
1565eda14cbcSMatt Macy
1566eda14cbcSMatt Macy /*
1567eda14cbcSMatt Macy * For the existed object that is upgraded from old system, its ondisk layout
1568eda14cbcSMatt Macy * has no slot for the project ID attribute. But quota accounting logic needs
1569eda14cbcSMatt Macy * to access related slots by offset directly. So we need to adjust these old
1570eda14cbcSMatt Macy * objects' layout to make the project ID to some unified and fixed offset.
1571eda14cbcSMatt Macy */
1572eda14cbcSMatt Macy int
sa_add_projid(sa_handle_t * hdl,dmu_tx_t * tx,uint64_t projid)1573eda14cbcSMatt Macy sa_add_projid(sa_handle_t *hdl, dmu_tx_t *tx, uint64_t projid)
1574eda14cbcSMatt Macy {
1575eda14cbcSMatt Macy znode_t *zp = sa_get_userdata(hdl);
1576eda14cbcSMatt Macy dmu_buf_t *db = sa_get_db(hdl);
1577eda14cbcSMatt Macy zfsvfs_t *zfsvfs = ZTOZSB(zp);
1578eda14cbcSMatt Macy int count = 0, err = 0;
1579eda14cbcSMatt Macy sa_bulk_attr_t *bulk, *attrs;
1580eda14cbcSMatt Macy zfs_acl_locator_cb_t locate = { 0 };
1581eda14cbcSMatt Macy uint64_t uid, gid, mode, rdev, xattr = 0, parent, gen, links;
1582eda14cbcSMatt Macy uint64_t crtime[2], mtime[2], ctime[2], atime[2];
1583eda14cbcSMatt Macy zfs_acl_phys_t znode_acl = { 0 };
1584eda14cbcSMatt Macy char scanstamp[AV_SCANSTAMP_SZ];
1585ce4dcb97SMartin Matuska char *dxattr_obj = NULL;
1586ce4dcb97SMartin Matuska int dxattr_size = 0;
1587eda14cbcSMatt Macy
1588eda14cbcSMatt Macy if (zp->z_acl_cached == NULL) {
1589eda14cbcSMatt Macy zfs_acl_t *aclp;
1590eda14cbcSMatt Macy
1591eda14cbcSMatt Macy mutex_enter(&zp->z_acl_lock);
1592eda14cbcSMatt Macy err = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
1593eda14cbcSMatt Macy mutex_exit(&zp->z_acl_lock);
1594eda14cbcSMatt Macy if (err != 0 && err != ENOENT)
1595eda14cbcSMatt Macy return (err);
1596eda14cbcSMatt Macy }
1597eda14cbcSMatt Macy
1598eda14cbcSMatt Macy bulk = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1599eda14cbcSMatt Macy attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1600eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1601eda14cbcSMatt Macy mutex_enter(&zp->z_lock);
1602eda14cbcSMatt Macy
1603eda14cbcSMatt Macy err = sa_lookup_locked(hdl, SA_ZPL_PROJID(zfsvfs), &projid,
1604eda14cbcSMatt Macy sizeof (uint64_t));
1605eda14cbcSMatt Macy if (unlikely(err == 0))
1606eda14cbcSMatt Macy /* Someone has added project ID attr by race. */
1607eda14cbcSMatt Macy err = EEXIST;
1608eda14cbcSMatt Macy if (err != ENOENT)
1609eda14cbcSMatt Macy goto out;
1610eda14cbcSMatt Macy
1611eda14cbcSMatt Macy /* First do a bulk query of the attributes that aren't cached */
1612eda14cbcSMatt Macy if (zp->z_is_sa) {
1613eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1614eda14cbcSMatt Macy &mode, 8);
1615eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1616eda14cbcSMatt Macy &gen, 8);
1617eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1618eda14cbcSMatt Macy &uid, 8);
1619eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1620eda14cbcSMatt Macy &gid, 8);
1621eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1622eda14cbcSMatt Macy &parent, 8);
1623eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1624eda14cbcSMatt Macy &atime, 16);
1625eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1626eda14cbcSMatt Macy &mtime, 16);
1627eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1628eda14cbcSMatt Macy &ctime, 16);
1629eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1630eda14cbcSMatt Macy &crtime, 16);
1631eda14cbcSMatt Macy if (Z_ISBLK(ZTOTYPE(zp)) || Z_ISCHR(ZTOTYPE(zp)))
1632eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1633eda14cbcSMatt Macy &rdev, 8);
1634eda14cbcSMatt Macy } else {
1635eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1636eda14cbcSMatt Macy &atime, 16);
1637eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1638eda14cbcSMatt Macy &mtime, 16);
1639eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1640eda14cbcSMatt Macy &ctime, 16);
1641eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1642eda14cbcSMatt Macy &crtime, 16);
1643eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1644eda14cbcSMatt Macy &gen, 8);
1645eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1646eda14cbcSMatt Macy &mode, 8);
1647eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1648eda14cbcSMatt Macy &parent, 8);
1649eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL,
1650eda14cbcSMatt Macy &xattr, 8);
1651eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1652eda14cbcSMatt Macy &rdev, 8);
1653eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1654eda14cbcSMatt Macy &uid, 8);
1655eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1656eda14cbcSMatt Macy &gid, 8);
1657eda14cbcSMatt Macy SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1658eda14cbcSMatt Macy &znode_acl, 88);
1659eda14cbcSMatt Macy }
1660eda14cbcSMatt Macy err = sa_bulk_lookup_locked(hdl, bulk, count);
1661eda14cbcSMatt Macy if (err != 0)
1662eda14cbcSMatt Macy goto out;
1663eda14cbcSMatt Macy
1664eda14cbcSMatt Macy err = sa_lookup_locked(hdl, SA_ZPL_XATTR(zfsvfs), &xattr, 8);
1665eda14cbcSMatt Macy if (err != 0 && err != ENOENT)
1666eda14cbcSMatt Macy goto out;
1667eda14cbcSMatt Macy
1668ce4dcb97SMartin Matuska err = sa_size_locked(hdl, SA_ZPL_DXATTR(zfsvfs), &dxattr_size);
1669ce4dcb97SMartin Matuska if (err != 0 && err != ENOENT)
1670ce4dcb97SMartin Matuska goto out;
1671ce4dcb97SMartin Matuska if (dxattr_size != 0) {
1672ce4dcb97SMartin Matuska dxattr_obj = vmem_alloc(dxattr_size, KM_SLEEP);
1673ce4dcb97SMartin Matuska err = sa_lookup_locked(hdl, SA_ZPL_DXATTR(zfsvfs), dxattr_obj,
1674ce4dcb97SMartin Matuska dxattr_size);
1675ce4dcb97SMartin Matuska if (err != 0 && err != ENOENT)
1676ce4dcb97SMartin Matuska goto out;
1677ce4dcb97SMartin Matuska }
1678ce4dcb97SMartin Matuska
1679eda14cbcSMatt Macy zp->z_projid = projid;
1680eda14cbcSMatt Macy zp->z_pflags |= ZFS_PROJID;
1681eda14cbcSMatt Macy links = ZTONLNK(zp);
1682eda14cbcSMatt Macy count = 0;
1683eda14cbcSMatt Macy err = 0;
1684eda14cbcSMatt Macy
1685eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
1686eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
1687eda14cbcSMatt Macy &zp->z_size, 8);
1688eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GEN(zfsvfs), NULL, &gen, 8);
1689eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
1690eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
1691eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
1692eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1693eda14cbcSMatt Macy &zp->z_pflags, 8);
1694eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
1695eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1696eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1697eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1698eda14cbcSMatt Macy &crtime, 16);
1699eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
1700eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PROJID(zfsvfs), NULL, &projid, 8);
1701eda14cbcSMatt Macy
1702eda14cbcSMatt Macy if (Z_ISBLK(ZTOTYPE(zp)) || Z_ISCHR(ZTOTYPE(zp)))
1703eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
1704eda14cbcSMatt Macy &rdev, 8);
1705eda14cbcSMatt Macy
1706eda14cbcSMatt Macy if (zp->z_acl_cached != NULL) {
1707eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1708eda14cbcSMatt Macy &zp->z_acl_cached->z_acl_count, 8);
1709eda14cbcSMatt Macy if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
1710eda14cbcSMatt Macy zfs_acl_xform(zp, zp->z_acl_cached, CRED());
1711eda14cbcSMatt Macy locate.cb_aclp = zp->z_acl_cached;
1712eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
1713eda14cbcSMatt Macy zfs_acl_data_locator, &locate,
1714eda14cbcSMatt Macy zp->z_acl_cached->z_acl_bytes);
1715eda14cbcSMatt Macy }
1716eda14cbcSMatt Macy
1717eda14cbcSMatt Macy if (xattr)
1718eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_XATTR(zfsvfs), NULL,
1719eda14cbcSMatt Macy &xattr, 8);
1720eda14cbcSMatt Macy
1721eda14cbcSMatt Macy if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
1722da5137abSMartin Matuska memcpy(scanstamp,
1723da5137abSMartin Matuska (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
1724da5137abSMartin Matuska AV_SCANSTAMP_SZ);
1725eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SCANSTAMP(zfsvfs), NULL,
1726eda14cbcSMatt Macy scanstamp, AV_SCANSTAMP_SZ);
1727eda14cbcSMatt Macy zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
1728eda14cbcSMatt Macy }
1729eda14cbcSMatt Macy
1730ce4dcb97SMartin Matuska if (dxattr_obj) {
1731ce4dcb97SMartin Matuska SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DXATTR(zfsvfs),
1732ce4dcb97SMartin Matuska NULL, dxattr_obj, dxattr_size);
1733ce4dcb97SMartin Matuska }
1734ce4dcb97SMartin Matuska
1735eda14cbcSMatt Macy VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
1736eda14cbcSMatt Macy VERIFY(sa_replace_all_by_template_locked(hdl, attrs, count, tx) == 0);
1737eda14cbcSMatt Macy if (znode_acl.z_acl_extern_obj) {
1738eda14cbcSMatt Macy VERIFY(0 == dmu_object_free(zfsvfs->z_os,
1739eda14cbcSMatt Macy znode_acl.z_acl_extern_obj, tx));
1740eda14cbcSMatt Macy }
1741eda14cbcSMatt Macy
1742eda14cbcSMatt Macy zp->z_is_sa = B_TRUE;
1743eda14cbcSMatt Macy
1744eda14cbcSMatt Macy out:
1745eda14cbcSMatt Macy mutex_exit(&zp->z_lock);
1746eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1747eda14cbcSMatt Macy kmem_free(attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
1748eda14cbcSMatt Macy kmem_free(bulk, sizeof (sa_bulk_attr_t) * ZPL_END);
1749ce4dcb97SMartin Matuska if (dxattr_obj)
1750ce4dcb97SMartin Matuska vmem_free(dxattr_obj, dxattr_size);
1751eda14cbcSMatt Macy return (err);
1752eda14cbcSMatt Macy }
1753eda14cbcSMatt Macy #endif
1754eda14cbcSMatt Macy
1755eda14cbcSMatt Macy static sa_idx_tab_t *
sa_find_idx_tab(objset_t * os,dmu_object_type_t bonustype,sa_hdr_phys_t * hdr)1756eda14cbcSMatt Macy sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, sa_hdr_phys_t *hdr)
1757eda14cbcSMatt Macy {
1758eda14cbcSMatt Macy sa_idx_tab_t *idx_tab;
1759eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
1760eda14cbcSMatt Macy sa_lot_t *tb, search;
1761eda14cbcSMatt Macy avl_index_t loc;
1762eda14cbcSMatt Macy
1763eda14cbcSMatt Macy /*
1764eda14cbcSMatt Macy * Deterimine layout number. If SA node and header == 0 then
1765eda14cbcSMatt Macy * force the index table to the dummy "1" empty layout.
1766eda14cbcSMatt Macy *
1767eda14cbcSMatt Macy * The layout number would only be zero for a newly created file
1768eda14cbcSMatt Macy * that has not added any attributes yet, or with crypto enabled which
1769eda14cbcSMatt Macy * doesn't write any attributes to the bonus buffer.
1770eda14cbcSMatt Macy */
1771eda14cbcSMatt Macy
1772eda14cbcSMatt Macy search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
1773eda14cbcSMatt Macy
1774eda14cbcSMatt Macy tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1775eda14cbcSMatt Macy
1776eda14cbcSMatt Macy /* Verify header size is consistent with layout information */
1777eda14cbcSMatt Macy ASSERT(tb);
1778eda14cbcSMatt Macy ASSERT((IS_SA_BONUSTYPE(bonustype) &&
1779eda14cbcSMatt Macy SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb)) || !IS_SA_BONUSTYPE(bonustype) ||
1780eda14cbcSMatt Macy (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
1781eda14cbcSMatt Macy
1782eda14cbcSMatt Macy /*
1783eda14cbcSMatt Macy * See if any of the already existing TOC entries can be reused?
1784eda14cbcSMatt Macy */
1785eda14cbcSMatt Macy
1786eda14cbcSMatt Macy for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
1787eda14cbcSMatt Macy idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
1788eda14cbcSMatt Macy boolean_t valid_idx = B_TRUE;
1789eda14cbcSMatt Macy int i;
1790eda14cbcSMatt Macy
1791eda14cbcSMatt Macy if (tb->lot_var_sizes != 0 &&
1792eda14cbcSMatt Macy idx_tab->sa_variable_lengths != NULL) {
1793eda14cbcSMatt Macy for (i = 0; i != tb->lot_var_sizes; i++) {
1794eda14cbcSMatt Macy if (hdr->sa_lengths[i] !=
1795eda14cbcSMatt Macy idx_tab->sa_variable_lengths[i]) {
1796eda14cbcSMatt Macy valid_idx = B_FALSE;
1797eda14cbcSMatt Macy break;
1798eda14cbcSMatt Macy }
1799eda14cbcSMatt Macy }
1800eda14cbcSMatt Macy }
1801eda14cbcSMatt Macy if (valid_idx) {
1802eda14cbcSMatt Macy sa_idx_tab_hold(os, idx_tab);
1803eda14cbcSMatt Macy return (idx_tab);
1804eda14cbcSMatt Macy }
1805eda14cbcSMatt Macy }
1806eda14cbcSMatt Macy
1807eda14cbcSMatt Macy /* No such luck, create a new entry */
1808eda14cbcSMatt Macy idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
1809eda14cbcSMatt Macy idx_tab->sa_idx_tab =
1810eda14cbcSMatt Macy kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
1811eda14cbcSMatt Macy idx_tab->sa_layout = tb;
1812eda14cbcSMatt Macy zfs_refcount_create(&idx_tab->sa_refcount);
1813eda14cbcSMatt Macy if (tb->lot_var_sizes)
1814eda14cbcSMatt Macy idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
1815eda14cbcSMatt Macy tb->lot_var_sizes, KM_SLEEP);
1816eda14cbcSMatt Macy
1817eda14cbcSMatt Macy sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
1818eda14cbcSMatt Macy tb, idx_tab);
1819eda14cbcSMatt Macy sa_idx_tab_hold(os, idx_tab); /* one hold for consumer */
1820eda14cbcSMatt Macy sa_idx_tab_hold(os, idx_tab); /* one for layout */
1821eda14cbcSMatt Macy list_insert_tail(&tb->lot_idx_tab, idx_tab);
1822eda14cbcSMatt Macy return (idx_tab);
1823eda14cbcSMatt Macy }
1824eda14cbcSMatt Macy
1825eda14cbcSMatt Macy void
sa_default_locator(void ** dataptr,uint32_t * len,uint32_t total_len,boolean_t start,void * userdata)1826eda14cbcSMatt Macy sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
1827eda14cbcSMatt Macy boolean_t start, void *userdata)
1828eda14cbcSMatt Macy {
1829eda14cbcSMatt Macy ASSERT(start);
1830eda14cbcSMatt Macy
1831eda14cbcSMatt Macy *dataptr = userdata;
1832eda14cbcSMatt Macy *len = total_len;
1833eda14cbcSMatt Macy }
1834eda14cbcSMatt Macy
1835eda14cbcSMatt Macy static void
sa_attr_register_sync(sa_handle_t * hdl,dmu_tx_t * tx)1836eda14cbcSMatt Macy sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
1837eda14cbcSMatt Macy {
1838eda14cbcSMatt Macy uint64_t attr_value = 0;
1839eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
1840eda14cbcSMatt Macy sa_attr_table_t *tb = sa->sa_attr_table;
1841eda14cbcSMatt Macy int i;
1842eda14cbcSMatt Macy
1843eda14cbcSMatt Macy mutex_enter(&sa->sa_lock);
1844eda14cbcSMatt Macy
1845eda14cbcSMatt Macy if (!sa->sa_need_attr_registration || sa->sa_master_obj == 0) {
1846eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1847eda14cbcSMatt Macy return;
1848eda14cbcSMatt Macy }
1849eda14cbcSMatt Macy
1850eda14cbcSMatt Macy if (sa->sa_reg_attr_obj == 0) {
1851eda14cbcSMatt Macy sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1852eda14cbcSMatt Macy DMU_OT_SA_ATTR_REGISTRATION,
1853eda14cbcSMatt Macy sa->sa_master_obj, SA_REGISTRY, tx);
1854eda14cbcSMatt Macy }
1855eda14cbcSMatt Macy for (i = 0; i != sa->sa_num_attrs; i++) {
1856eda14cbcSMatt Macy if (sa->sa_attr_table[i].sa_registered)
1857eda14cbcSMatt Macy continue;
1858eda14cbcSMatt Macy ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
1859eda14cbcSMatt Macy tb[i].sa_byteswap);
1860eda14cbcSMatt Macy VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
1861eda14cbcSMatt Macy tb[i].sa_name, 8, 1, &attr_value, tx));
1862eda14cbcSMatt Macy tb[i].sa_registered = B_TRUE;
1863eda14cbcSMatt Macy }
1864eda14cbcSMatt Macy sa->sa_need_attr_registration = B_FALSE;
1865eda14cbcSMatt Macy mutex_exit(&sa->sa_lock);
1866eda14cbcSMatt Macy }
1867eda14cbcSMatt Macy
1868eda14cbcSMatt Macy /*
1869eda14cbcSMatt Macy * Replace all attributes with attributes specified in template.
1870eda14cbcSMatt Macy * If dnode had a spill buffer then those attributes will be
1871eda14cbcSMatt Macy * also be replaced, possibly with just an empty spill block
1872eda14cbcSMatt Macy *
1873eda14cbcSMatt Macy * This interface is intended to only be used for bulk adding of
1874eda14cbcSMatt Macy * attributes for a new file. It will also be used by the ZPL
1875eda14cbcSMatt Macy * when converting and old formatted znode to native SA support.
1876eda14cbcSMatt Macy */
1877eda14cbcSMatt Macy int
sa_replace_all_by_template_locked(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)1878eda14cbcSMatt Macy sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1879eda14cbcSMatt Macy int attr_count, dmu_tx_t *tx)
1880eda14cbcSMatt Macy {
1881eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
1882eda14cbcSMatt Macy
1883eda14cbcSMatt Macy if (sa->sa_need_attr_registration)
1884eda14cbcSMatt Macy sa_attr_register_sync(hdl, tx);
1885eda14cbcSMatt Macy return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
1886eda14cbcSMatt Macy }
1887eda14cbcSMatt Macy
1888eda14cbcSMatt Macy int
sa_replace_all_by_template(sa_handle_t * hdl,sa_bulk_attr_t * attr_desc,int attr_count,dmu_tx_t * tx)1889eda14cbcSMatt Macy sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1890eda14cbcSMatt Macy int attr_count, dmu_tx_t *tx)
1891eda14cbcSMatt Macy {
1892eda14cbcSMatt Macy int error;
1893eda14cbcSMatt Macy
1894eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
1895eda14cbcSMatt Macy error = sa_replace_all_by_template_locked(hdl, attr_desc,
1896eda14cbcSMatt Macy attr_count, tx);
1897eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
1898eda14cbcSMatt Macy return (error);
1899eda14cbcSMatt Macy }
1900eda14cbcSMatt Macy
1901eda14cbcSMatt Macy /*
1902eda14cbcSMatt Macy * Add/remove a single attribute or replace a variable-sized attribute value
1903eda14cbcSMatt Macy * with a value of a different size, and then rewrite the entire set
1904eda14cbcSMatt Macy * of attributes.
1905eda14cbcSMatt Macy * Same-length attribute value replacement (including fixed-length attributes)
1906eda14cbcSMatt Macy * is handled more efficiently by the upper layers.
1907eda14cbcSMatt Macy */
1908eda14cbcSMatt Macy static int
sa_modify_attrs(sa_handle_t * hdl,sa_attr_type_t newattr,sa_data_op_t action,sa_data_locator_t * locator,void * datastart,uint16_t buflen,dmu_tx_t * tx)1909eda14cbcSMatt Macy sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1910eda14cbcSMatt Macy sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1911eda14cbcSMatt Macy uint16_t buflen, dmu_tx_t *tx)
1912eda14cbcSMatt Macy {
1913eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
1914eda14cbcSMatt Macy dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1915eda14cbcSMatt Macy sa_bulk_attr_t *attr_desc;
1916eda14cbcSMatt Macy void *old_data[2];
1917eda14cbcSMatt Macy int bonus_attr_count = 0;
1918eda14cbcSMatt Macy int bonus_data_size = 0;
1919eda14cbcSMatt Macy int spill_data_size = 0;
1920eda14cbcSMatt Macy int spill_attr_count = 0;
1921eda14cbcSMatt Macy int error;
1922eda14cbcSMatt Macy uint16_t length, reg_length;
1923eda14cbcSMatt Macy int i, j, k, length_idx;
1924eda14cbcSMatt Macy sa_hdr_phys_t *hdr;
1925eda14cbcSMatt Macy sa_idx_tab_t *idx_tab;
1926eda14cbcSMatt Macy int attr_count;
1927eda14cbcSMatt Macy int count;
1928eda14cbcSMatt Macy
1929eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&hdl->sa_lock));
1930eda14cbcSMatt Macy
1931eda14cbcSMatt Macy /* First make of copy of the old data */
1932eda14cbcSMatt Macy
1933eda14cbcSMatt Macy DB_DNODE_ENTER(db);
1934ce4dcb97SMartin Matuska if (DB_DNODE(db)->dn_bonuslen != 0) {
1935eda14cbcSMatt Macy bonus_data_size = hdl->sa_bonus->db_size;
1936eda14cbcSMatt Macy old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
1937da5137abSMartin Matuska memcpy(old_data[0], hdl->sa_bonus->db_data,
1938eda14cbcSMatt Macy hdl->sa_bonus->db_size);
1939eda14cbcSMatt Macy bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
1940eda14cbcSMatt Macy } else {
1941eda14cbcSMatt Macy old_data[0] = NULL;
1942eda14cbcSMatt Macy }
1943eda14cbcSMatt Macy DB_DNODE_EXIT(db);
1944eda14cbcSMatt Macy
1945eda14cbcSMatt Macy /* Bring spill buffer online if it isn't currently */
1946eda14cbcSMatt Macy
1947eda14cbcSMatt Macy if ((error = sa_get_spill(hdl)) == 0) {
1948eda14cbcSMatt Macy spill_data_size = hdl->sa_spill->db_size;
1949eda14cbcSMatt Macy old_data[1] = vmem_alloc(spill_data_size, KM_SLEEP);
1950da5137abSMartin Matuska memcpy(old_data[1], hdl->sa_spill->db_data,
1951eda14cbcSMatt Macy hdl->sa_spill->db_size);
1952eda14cbcSMatt Macy spill_attr_count =
1953eda14cbcSMatt Macy hdl->sa_spill_tab->sa_layout->lot_attr_count;
1954eda14cbcSMatt Macy } else if (error && error != ENOENT) {
1955eda14cbcSMatt Macy if (old_data[0])
1956eda14cbcSMatt Macy kmem_free(old_data[0], bonus_data_size);
1957eda14cbcSMatt Macy return (error);
1958eda14cbcSMatt Macy } else {
1959eda14cbcSMatt Macy old_data[1] = NULL;
1960eda14cbcSMatt Macy }
1961eda14cbcSMatt Macy
1962eda14cbcSMatt Macy /* build descriptor of all attributes */
1963eda14cbcSMatt Macy
1964eda14cbcSMatt Macy attr_count = bonus_attr_count + spill_attr_count;
1965eda14cbcSMatt Macy if (action == SA_ADD)
1966eda14cbcSMatt Macy attr_count++;
1967eda14cbcSMatt Macy else if (action == SA_REMOVE)
1968eda14cbcSMatt Macy attr_count--;
1969eda14cbcSMatt Macy
1970eda14cbcSMatt Macy attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
1971eda14cbcSMatt Macy
1972eda14cbcSMatt Macy /*
1973eda14cbcSMatt Macy * loop through bonus and spill buffer if it exists, and
1974eda14cbcSMatt Macy * build up new attr_descriptor to reset the attributes
1975eda14cbcSMatt Macy */
1976eda14cbcSMatt Macy k = j = 0;
1977eda14cbcSMatt Macy count = bonus_attr_count;
1978eda14cbcSMatt Macy hdr = SA_GET_HDR(hdl, SA_BONUS);
1979eda14cbcSMatt Macy idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
19802a58b312SMartin Matuska for (; ; k++) {
1981eda14cbcSMatt Macy /*
1982eda14cbcSMatt Macy * Iterate over each attribute in layout. Fetch the
1983eda14cbcSMatt Macy * size of variable-length attributes needing rewrite
1984eda14cbcSMatt Macy * from sa_lengths[].
1985eda14cbcSMatt Macy */
1986eda14cbcSMatt Macy for (i = 0, length_idx = 0; i != count; i++) {
1987eda14cbcSMatt Macy sa_attr_type_t attr;
1988eda14cbcSMatt Macy
1989eda14cbcSMatt Macy attr = idx_tab->sa_layout->lot_attrs[i];
1990eda14cbcSMatt Macy reg_length = SA_REGISTERED_LEN(sa, attr);
1991eda14cbcSMatt Macy if (reg_length == 0) {
1992eda14cbcSMatt Macy length = hdr->sa_lengths[length_idx];
1993eda14cbcSMatt Macy length_idx++;
1994eda14cbcSMatt Macy } else {
1995eda14cbcSMatt Macy length = reg_length;
1996eda14cbcSMatt Macy }
1997eda14cbcSMatt Macy if (attr == newattr) {
1998eda14cbcSMatt Macy /*
1999eda14cbcSMatt Macy * There is nothing to do for SA_REMOVE,
2000eda14cbcSMatt Macy * so it is just skipped.
2001eda14cbcSMatt Macy */
2002eda14cbcSMatt Macy if (action == SA_REMOVE)
2003eda14cbcSMatt Macy continue;
2004eda14cbcSMatt Macy
2005eda14cbcSMatt Macy /*
2006eda14cbcSMatt Macy * Duplicate attributes are not allowed, so the
2007eda14cbcSMatt Macy * action can not be SA_ADD here.
2008eda14cbcSMatt Macy */
2009eda14cbcSMatt Macy ASSERT3S(action, ==, SA_REPLACE);
2010eda14cbcSMatt Macy
2011eda14cbcSMatt Macy /*
2012eda14cbcSMatt Macy * Only a variable-sized attribute can be
2013eda14cbcSMatt Macy * replaced here, and its size must be changing.
2014eda14cbcSMatt Macy */
2015eda14cbcSMatt Macy ASSERT3U(reg_length, ==, 0);
2016eda14cbcSMatt Macy ASSERT3U(length, !=, buflen);
2017eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attr_desc, j, attr,
2018eda14cbcSMatt Macy locator, datastart, buflen);
2019eda14cbcSMatt Macy } else {
2020eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attr_desc, j, attr,
2021eda14cbcSMatt Macy NULL, (void *)
2022eda14cbcSMatt Macy (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
2023eda14cbcSMatt Macy (uintptr_t)old_data[k]), length);
2024eda14cbcSMatt Macy }
2025eda14cbcSMatt Macy }
2026eda14cbcSMatt Macy if (k == 0 && hdl->sa_spill) {
2027eda14cbcSMatt Macy hdr = SA_GET_HDR(hdl, SA_SPILL);
2028eda14cbcSMatt Macy idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
2029eda14cbcSMatt Macy count = spill_attr_count;
2030eda14cbcSMatt Macy } else {
2031eda14cbcSMatt Macy break;
2032eda14cbcSMatt Macy }
2033eda14cbcSMatt Macy }
2034eda14cbcSMatt Macy if (action == SA_ADD) {
2035eda14cbcSMatt Macy reg_length = SA_REGISTERED_LEN(sa, newattr);
2036eda14cbcSMatt Macy IMPLY(reg_length != 0, reg_length == buflen);
2037eda14cbcSMatt Macy SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
2038eda14cbcSMatt Macy datastart, buflen);
2039eda14cbcSMatt Macy }
2040eda14cbcSMatt Macy ASSERT3U(j, ==, attr_count);
2041eda14cbcSMatt Macy
2042eda14cbcSMatt Macy error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
2043eda14cbcSMatt Macy
2044eda14cbcSMatt Macy if (old_data[0])
2045eda14cbcSMatt Macy kmem_free(old_data[0], bonus_data_size);
2046eda14cbcSMatt Macy if (old_data[1])
2047eda14cbcSMatt Macy vmem_free(old_data[1], spill_data_size);
2048eda14cbcSMatt Macy kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
2049eda14cbcSMatt Macy
2050eda14cbcSMatt Macy return (error);
2051eda14cbcSMatt Macy }
2052eda14cbcSMatt Macy
2053eda14cbcSMatt Macy static int
sa_bulk_update_impl(sa_handle_t * hdl,sa_bulk_attr_t * bulk,int count,dmu_tx_t * tx)2054eda14cbcSMatt Macy sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
2055eda14cbcSMatt Macy dmu_tx_t *tx)
2056eda14cbcSMatt Macy {
2057eda14cbcSMatt Macy int error;
2058eda14cbcSMatt Macy sa_os_t *sa = hdl->sa_os->os_sa;
2059eda14cbcSMatt Macy dmu_object_type_t bonustype;
2060eda14cbcSMatt Macy dmu_buf_t *saved_spill;
2061eda14cbcSMatt Macy
2062eda14cbcSMatt Macy ASSERT(hdl);
2063eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&hdl->sa_lock));
2064eda14cbcSMatt Macy
2065eda14cbcSMatt Macy bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
2066eda14cbcSMatt Macy saved_spill = hdl->sa_spill;
2067eda14cbcSMatt Macy
2068eda14cbcSMatt Macy /* sync out registration table if necessary */
2069eda14cbcSMatt Macy if (sa->sa_need_attr_registration)
2070eda14cbcSMatt Macy sa_attr_register_sync(hdl, tx);
2071eda14cbcSMatt Macy
2072eda14cbcSMatt Macy error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
2073eda14cbcSMatt Macy if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
2074eda14cbcSMatt Macy sa->sa_update_cb(hdl, tx);
2075eda14cbcSMatt Macy
2076eda14cbcSMatt Macy /*
2077eda14cbcSMatt Macy * If saved_spill is NULL and current sa_spill is not NULL that
2078eda14cbcSMatt Macy * means we increased the refcount of the spill buffer through
2079eda14cbcSMatt Macy * sa_get_spill() or dmu_spill_hold_by_dnode(). Therefore we
2080eda14cbcSMatt Macy * must release the hold before calling dmu_tx_commit() to avoid
2081eda14cbcSMatt Macy * making a copy of this buffer in dbuf_sync_leaf() due to the
2082eda14cbcSMatt Macy * reference count now being greater than 1.
2083eda14cbcSMatt Macy */
2084eda14cbcSMatt Macy if (!saved_spill && hdl->sa_spill) {
2085eda14cbcSMatt Macy if (hdl->sa_spill_tab) {
2086eda14cbcSMatt Macy sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
2087eda14cbcSMatt Macy hdl->sa_spill_tab = NULL;
2088eda14cbcSMatt Macy }
2089eda14cbcSMatt Macy
2090eda14cbcSMatt Macy dmu_buf_rele(hdl->sa_spill, NULL);
2091eda14cbcSMatt Macy hdl->sa_spill = NULL;
2092eda14cbcSMatt Macy }
2093eda14cbcSMatt Macy
2094eda14cbcSMatt Macy return (error);
2095eda14cbcSMatt Macy }
2096eda14cbcSMatt Macy
2097eda14cbcSMatt Macy /*
2098eda14cbcSMatt Macy * update or add new attribute
2099eda14cbcSMatt Macy */
2100eda14cbcSMatt Macy int
sa_update(sa_handle_t * hdl,sa_attr_type_t type,void * buf,uint32_t buflen,dmu_tx_t * tx)2101eda14cbcSMatt Macy sa_update(sa_handle_t *hdl, sa_attr_type_t type,
2102eda14cbcSMatt Macy void *buf, uint32_t buflen, dmu_tx_t *tx)
2103eda14cbcSMatt Macy {
2104eda14cbcSMatt Macy int error;
2105eda14cbcSMatt Macy sa_bulk_attr_t bulk;
2106eda14cbcSMatt Macy
2107eda14cbcSMatt Macy VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
2108eda14cbcSMatt Macy
2109eda14cbcSMatt Macy bulk.sa_attr = type;
2110eda14cbcSMatt Macy bulk.sa_data_func = NULL;
2111eda14cbcSMatt Macy bulk.sa_length = buflen;
2112eda14cbcSMatt Macy bulk.sa_data = buf;
2113eda14cbcSMatt Macy
2114eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
2115eda14cbcSMatt Macy error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
2116eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
2117eda14cbcSMatt Macy return (error);
2118eda14cbcSMatt Macy }
2119eda14cbcSMatt Macy
2120eda14cbcSMatt Macy int
sa_bulk_lookup_locked(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count)2121eda14cbcSMatt Macy sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
2122eda14cbcSMatt Macy {
2123eda14cbcSMatt Macy ASSERT(hdl);
2124eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&hdl->sa_lock));
2125eda14cbcSMatt Macy return (sa_lookup_impl(hdl, attrs, count));
2126eda14cbcSMatt Macy }
2127eda14cbcSMatt Macy
2128eda14cbcSMatt Macy int
sa_bulk_lookup(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count)2129eda14cbcSMatt Macy sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
2130eda14cbcSMatt Macy {
2131eda14cbcSMatt Macy int error;
2132eda14cbcSMatt Macy
2133eda14cbcSMatt Macy ASSERT(hdl);
2134eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
2135eda14cbcSMatt Macy error = sa_bulk_lookup_locked(hdl, attrs, count);
2136eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
2137eda14cbcSMatt Macy return (error);
2138eda14cbcSMatt Macy }
2139eda14cbcSMatt Macy
2140eda14cbcSMatt Macy int
sa_bulk_update(sa_handle_t * hdl,sa_bulk_attr_t * attrs,int count,dmu_tx_t * tx)2141eda14cbcSMatt Macy sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
2142eda14cbcSMatt Macy {
2143eda14cbcSMatt Macy int error;
2144eda14cbcSMatt Macy
2145eda14cbcSMatt Macy ASSERT(hdl);
2146eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
2147eda14cbcSMatt Macy error = sa_bulk_update_impl(hdl, attrs, count, tx);
2148eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
2149eda14cbcSMatt Macy return (error);
2150eda14cbcSMatt Macy }
2151eda14cbcSMatt Macy
2152eda14cbcSMatt Macy int
sa_remove(sa_handle_t * hdl,sa_attr_type_t attr,dmu_tx_t * tx)2153eda14cbcSMatt Macy sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
2154eda14cbcSMatt Macy {
2155eda14cbcSMatt Macy int error;
2156eda14cbcSMatt Macy
2157eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
2158eda14cbcSMatt Macy error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
2159eda14cbcSMatt Macy NULL, 0, tx);
2160eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
2161eda14cbcSMatt Macy return (error);
2162eda14cbcSMatt Macy }
2163eda14cbcSMatt Macy
2164eda14cbcSMatt Macy void
sa_object_info(sa_handle_t * hdl,dmu_object_info_t * doi)2165eda14cbcSMatt Macy sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
2166eda14cbcSMatt Macy {
2167eda14cbcSMatt Macy dmu_object_info_from_db(hdl->sa_bonus, doi);
2168eda14cbcSMatt Macy }
2169eda14cbcSMatt Macy
2170eda14cbcSMatt Macy void
sa_object_size(sa_handle_t * hdl,uint32_t * blksize,u_longlong_t * nblocks)2171eda14cbcSMatt Macy sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
2172eda14cbcSMatt Macy {
2173eda14cbcSMatt Macy dmu_object_size_from_db(hdl->sa_bonus,
2174eda14cbcSMatt Macy blksize, nblocks);
2175eda14cbcSMatt Macy }
2176eda14cbcSMatt Macy
2177eda14cbcSMatt Macy void
sa_set_userp(sa_handle_t * hdl,void * ptr)2178eda14cbcSMatt Macy sa_set_userp(sa_handle_t *hdl, void *ptr)
2179eda14cbcSMatt Macy {
2180eda14cbcSMatt Macy hdl->sa_userp = ptr;
2181eda14cbcSMatt Macy }
2182eda14cbcSMatt Macy
2183eda14cbcSMatt Macy dmu_buf_t *
sa_get_db(sa_handle_t * hdl)2184eda14cbcSMatt Macy sa_get_db(sa_handle_t *hdl)
2185eda14cbcSMatt Macy {
2186eda14cbcSMatt Macy return (hdl->sa_bonus);
2187eda14cbcSMatt Macy }
2188eda14cbcSMatt Macy
2189eda14cbcSMatt Macy void *
sa_get_userdata(sa_handle_t * hdl)2190eda14cbcSMatt Macy sa_get_userdata(sa_handle_t *hdl)
2191eda14cbcSMatt Macy {
2192eda14cbcSMatt Macy return (hdl->sa_userp);
2193eda14cbcSMatt Macy }
2194eda14cbcSMatt Macy
2195eda14cbcSMatt Macy void
sa_register_update_callback_locked(objset_t * os,sa_update_cb_t * func)2196eda14cbcSMatt Macy sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
2197eda14cbcSMatt Macy {
2198eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
2199eda14cbcSMatt Macy os->os_sa->sa_update_cb = func;
2200eda14cbcSMatt Macy }
2201eda14cbcSMatt Macy
2202eda14cbcSMatt Macy void
sa_register_update_callback(objset_t * os,sa_update_cb_t * func)2203eda14cbcSMatt Macy sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
2204eda14cbcSMatt Macy {
2205eda14cbcSMatt Macy
2206eda14cbcSMatt Macy mutex_enter(&os->os_sa->sa_lock);
2207eda14cbcSMatt Macy sa_register_update_callback_locked(os, func);
2208eda14cbcSMatt Macy mutex_exit(&os->os_sa->sa_lock);
2209eda14cbcSMatt Macy }
2210eda14cbcSMatt Macy
2211eda14cbcSMatt Macy uint64_t
sa_handle_object(sa_handle_t * hdl)2212eda14cbcSMatt Macy sa_handle_object(sa_handle_t *hdl)
2213eda14cbcSMatt Macy {
2214eda14cbcSMatt Macy return (hdl->sa_bonus->db_object);
2215eda14cbcSMatt Macy }
2216eda14cbcSMatt Macy
2217eda14cbcSMatt Macy boolean_t
sa_enabled(objset_t * os)2218eda14cbcSMatt Macy sa_enabled(objset_t *os)
2219eda14cbcSMatt Macy {
2220eda14cbcSMatt Macy return (os->os_sa == NULL);
2221eda14cbcSMatt Macy }
2222eda14cbcSMatt Macy
2223eda14cbcSMatt Macy int
sa_set_sa_object(objset_t * os,uint64_t sa_object)2224eda14cbcSMatt Macy sa_set_sa_object(objset_t *os, uint64_t sa_object)
2225eda14cbcSMatt Macy {
2226eda14cbcSMatt Macy sa_os_t *sa = os->os_sa;
2227eda14cbcSMatt Macy
2228eda14cbcSMatt Macy if (sa->sa_master_obj)
2229eda14cbcSMatt Macy return (1);
2230eda14cbcSMatt Macy
2231eda14cbcSMatt Macy sa->sa_master_obj = sa_object;
2232eda14cbcSMatt Macy
2233eda14cbcSMatt Macy return (0);
2234eda14cbcSMatt Macy }
2235eda14cbcSMatt Macy
2236eda14cbcSMatt Macy int
sa_hdrsize(void * arg)2237eda14cbcSMatt Macy sa_hdrsize(void *arg)
2238eda14cbcSMatt Macy {
2239eda14cbcSMatt Macy sa_hdr_phys_t *hdr = arg;
2240eda14cbcSMatt Macy
2241eda14cbcSMatt Macy return (SA_HDR_SIZE(hdr));
2242eda14cbcSMatt Macy }
2243eda14cbcSMatt Macy
2244eda14cbcSMatt Macy void
sa_handle_lock(sa_handle_t * hdl)2245eda14cbcSMatt Macy sa_handle_lock(sa_handle_t *hdl)
2246eda14cbcSMatt Macy {
2247eda14cbcSMatt Macy ASSERT(hdl);
2248eda14cbcSMatt Macy mutex_enter(&hdl->sa_lock);
2249eda14cbcSMatt Macy }
2250eda14cbcSMatt Macy
2251eda14cbcSMatt Macy void
sa_handle_unlock(sa_handle_t * hdl)2252eda14cbcSMatt Macy sa_handle_unlock(sa_handle_t *hdl)
2253eda14cbcSMatt Macy {
2254eda14cbcSMatt Macy ASSERT(hdl);
2255eda14cbcSMatt Macy mutex_exit(&hdl->sa_lock);
2256eda14cbcSMatt Macy }
2257eda14cbcSMatt Macy
2258eda14cbcSMatt Macy #ifdef _KERNEL
2259eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_get);
2260eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_get_from_db);
2261eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_destroy);
2262eda14cbcSMatt Macy EXPORT_SYMBOL(sa_buf_hold);
2263eda14cbcSMatt Macy EXPORT_SYMBOL(sa_buf_rele);
2264eda14cbcSMatt Macy EXPORT_SYMBOL(sa_spill_rele);
2265eda14cbcSMatt Macy EXPORT_SYMBOL(sa_lookup);
2266eda14cbcSMatt Macy EXPORT_SYMBOL(sa_update);
2267eda14cbcSMatt Macy EXPORT_SYMBOL(sa_remove);
2268eda14cbcSMatt Macy EXPORT_SYMBOL(sa_bulk_lookup);
2269eda14cbcSMatt Macy EXPORT_SYMBOL(sa_bulk_lookup_locked);
2270eda14cbcSMatt Macy EXPORT_SYMBOL(sa_bulk_update);
2271eda14cbcSMatt Macy EXPORT_SYMBOL(sa_size);
2272eda14cbcSMatt Macy EXPORT_SYMBOL(sa_object_info);
2273eda14cbcSMatt Macy EXPORT_SYMBOL(sa_object_size);
2274eda14cbcSMatt Macy EXPORT_SYMBOL(sa_get_userdata);
2275eda14cbcSMatt Macy EXPORT_SYMBOL(sa_set_userp);
2276eda14cbcSMatt Macy EXPORT_SYMBOL(sa_get_db);
2277eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_object);
2278eda14cbcSMatt Macy EXPORT_SYMBOL(sa_register_update_callback);
2279eda14cbcSMatt Macy EXPORT_SYMBOL(sa_setup);
2280eda14cbcSMatt Macy EXPORT_SYMBOL(sa_replace_all_by_template);
2281eda14cbcSMatt Macy EXPORT_SYMBOL(sa_replace_all_by_template_locked);
2282eda14cbcSMatt Macy EXPORT_SYMBOL(sa_enabled);
2283eda14cbcSMatt Macy EXPORT_SYMBOL(sa_cache_init);
2284eda14cbcSMatt Macy EXPORT_SYMBOL(sa_cache_fini);
2285eda14cbcSMatt Macy EXPORT_SYMBOL(sa_set_sa_object);
2286eda14cbcSMatt Macy EXPORT_SYMBOL(sa_hdrsize);
2287eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_lock);
2288eda14cbcSMatt Macy EXPORT_SYMBOL(sa_handle_unlock);
2289eda14cbcSMatt Macy EXPORT_SYMBOL(sa_lookup_uio);
2290eda14cbcSMatt Macy EXPORT_SYMBOL(sa_add_projid);
2291eda14cbcSMatt Macy #endif /* _KERNEL */
2292