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