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.
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.
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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 iXsystems, Inc
25 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
30 #include <sys/zfs_context.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
36 #include <sys/dmu_impl.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dmu_tx.h>
40 #include <sys/dnode.h>
43 #include <sys/sunddi.h>
44 #include <sys/sa_impl.h>
45 #include <sys/dnode.h>
46 #include <sys/errno.h>
47 #include <sys/zfs_context.h>
50 * ZFS System attributes:
52 * A generic mechanism to allow for arbitrary attributes
53 * to be stored in a dnode. The data will be stored in the bonus buffer of
54 * the dnode and if necessary a special "spill" block will be used to handle
55 * overflow situations. The spill block will be sized to fit the data
56 * from 512 - 128K. When a spill block is used the BP (blkptr_t) for the
57 * spill block is stored at the end of the current bonus buffer. Any
58 * attributes that would be in the way of the blkptr_t will be relocated
59 * into the spill block.
61 * Attribute registration:
63 * Stored persistently on a per dataset basis
64 * a mapping between attribute "string" names and their actual attribute
65 * numeric values, length, and byteswap function. The names are only used
66 * during registration. All attributes are known by their unique attribute
67 * id value. If an attribute can have a variable size then the value
68 * 0 will be used to indicate this.
72 * Attribute layouts are a way to compactly store multiple attributes, but
73 * without taking the overhead associated with managing each attribute
74 * individually. Since you will typically have the same set of attributes
75 * stored in the same order a single table will be used to represent that
76 * layout. The ZPL for example will usually have only about 10 different
77 * layouts (regular files, device files, symlinks,
78 * regular files + scanstamp, files/dir with extended attributes, and then
79 * you have the possibility of all of those minus ACL, because it would
80 * be kicked out into the spill block)
82 * Layouts are simply an array of the attributes and their
83 * ordering i.e. [0, 1, 4, 5, 2]
85 * Each distinct layout is given a unique layout number and that is whats
86 * stored in the header at the beginning of the SA data buffer.
88 * A layout only covers a single dbuf (bonus or spill). If a set of
89 * attributes is split up between the bonus buffer and a spill buffer then
90 * two different layouts will be used. This allows us to byteswap the
91 * spill without looking at the bonus buffer and keeps the on disk format of
92 * the bonus and spill buffer the same.
94 * Adding a single attribute will cause the entire set of attributes to
95 * be rewritten and could result in a new layout number being constructed
96 * as part of the rewrite if no such layout exists for the new set of
97 * attribues. The new attribute will be appended to the end of the already
98 * existing attributes.
100 * Both the attribute registration and attribute layout information are
101 * stored in normal ZAP attributes. Their should be a small number of
102 * known layouts and the set of attributes is assumed to typically be quite
105 * The registered attributes and layout "table" information is maintained
106 * in core and a special "sa_os_t" is attached to the objset_t.
108 * A special interface is provided to allow for quickly applying
109 * a large set of attributes at once. sa_replace_all_by_template() is
110 * used to set an array of attributes. This is used by the ZPL when
111 * creating a brand new file. The template that is passed into the function
112 * specifies the attribute, size for variable length attributes, location of
113 * data and special "data locator" function if the data isn't in a contiguous
116 * Byteswap implications:
118 * Since the SA attributes are not entirely self describing we can't do
119 * the normal byteswap processing. The special ZAP layout attribute and
120 * attribute registration attributes define the byteswap function and the
121 * size of the attributes, unless it is variable sized.
122 * The normal ZFS byteswapping infrastructure assumes you don't need
123 * to read any objects in order to do the necessary byteswapping. Whereas
124 * SA attributes can only be properly byteswapped if the dataset is opened
125 * and the layout/attribute ZAP attributes are available. Because of this
126 * the SA attributes will be byteswapped when they are first accessed by
127 * the SA code that will read the SA data.
130 typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
131 uint16_t length, int length_idx, boolean_t, void *userp);
133 static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
134 static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
135 static sa_idx_tab_t *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
137 static void sa_idx_tab_rele(objset_t *os, void *arg);
138 static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
140 static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
141 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
142 uint16_t buflen, dmu_tx_t *tx);
144 arc_byteswap_func_t *sa_bswap_table[] = {
145 byteswap_uint64_array,
146 byteswap_uint32_array,
147 byteswap_uint16_array,
148 byteswap_uint8_array,
152 #define SA_COPY_DATA(f, s, t, l) \
156 *(uint64_t *)t = *(uint64_t *)s; \
157 } else if (l == 16) { \
158 *(uint64_t *)t = *(uint64_t *)s; \
159 *(uint64_t *)((uintptr_t)t + 8) = \
160 *(uint64_t *)((uintptr_t)s + 8); \
165 sa_copy_data(f, s, t, l); \
169 * This table is fixed and cannot be changed. Its purpose is to
170 * allow the SA code to work with both old/new ZPL file systems.
171 * It contains the list of legacy attributes. These attributes aren't
172 * stored in the "attribute" registry zap objects, since older ZPL file systems
173 * won't have the registry. Only objsets of type ZFS_TYPE_FILESYSTEM will
174 * use this static table.
176 sa_attr_reg_t sa_legacy_attrs[] = {
177 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
178 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
179 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
180 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
181 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
182 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
183 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
184 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
185 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
186 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
187 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
188 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
189 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
190 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
191 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
192 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
196 * This is only used for objects of type DMU_OT_ZNODE
198 sa_attr_type_t sa_legacy_zpl_layout[] = {
199 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
203 * Special dummy layout used for buffers with no attributes.
205 sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
207 static int sa_legacy_attr_count = 16;
208 static kmem_cache_t *sa_cache = NULL;
212 sa_cache_constructor(void *buf, void *unused, int kmflag)
214 sa_handle_t *hdl = buf;
216 mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
222 sa_cache_destructor(void *buf, void *unused)
224 sa_handle_t *hdl = buf;
225 mutex_destroy(&hdl->sa_lock);
231 sa_cache = kmem_cache_create("sa_cache",
232 sizeof (sa_handle_t), 0, sa_cache_constructor,
233 sa_cache_destructor, NULL, NULL, NULL, 0);
240 kmem_cache_destroy(sa_cache);
244 layout_num_compare(const void *arg1, const void *arg2)
246 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
247 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
249 return (AVL_CMP(node1->lot_num, node2->lot_num));
253 layout_hash_compare(const void *arg1, const void *arg2)
255 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
256 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
258 int cmp = AVL_CMP(node1->lot_hash, node2->lot_hash);
262 return (AVL_CMP(node1->lot_instance, node2->lot_instance));
266 sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
270 if (count != tbf->lot_attr_count)
273 for (i = 0; i != count; i++) {
274 if (attrs[i] != tbf->lot_attrs[i])
280 #define SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
283 sa_layout_info_hash(sa_attr_type_t *attrs, int attr_count)
286 uint64_t crc = -1ULL;
288 for (i = 0; i != attr_count; i++)
289 crc ^= SA_ATTR_HASH(attrs[i]);
295 sa_get_spill(sa_handle_t *hdl)
298 if (hdl->sa_spill == NULL) {
299 if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
300 &hdl->sa_spill)) == 0)
301 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
310 * Main attribute lookup/update function
311 * returns 0 for success or non zero for failures
313 * Operates on bulk array, first failure will abort further processing
316 sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
317 sa_data_op_t data_op, dmu_tx_t *tx)
319 sa_os_t *sa = hdl->sa_os->os_sa;
322 sa_buf_type_t buftypes;
327 for (i = 0; i != count; i++) {
328 ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
330 bulk[i].sa_addr = NULL;
331 /* First check the bonus buffer */
333 if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
334 hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
335 SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
336 SA_GET_HDR(hdl, SA_BONUS),
337 bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
338 if (tx && !(buftypes & SA_BONUS)) {
339 dmu_buf_will_dirty(hdl->sa_bonus, tx);
340 buftypes |= SA_BONUS;
343 if (bulk[i].sa_addr == NULL &&
344 ((error = sa_get_spill(hdl)) == 0)) {
345 if (TOC_ATTR_PRESENT(
346 hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
347 SA_ATTR_INFO(sa, hdl->sa_spill_tab,
348 SA_GET_HDR(hdl, SA_SPILL),
349 bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
350 if (tx && !(buftypes & SA_SPILL) &&
351 bulk[i].sa_size == bulk[i].sa_length) {
352 dmu_buf_will_dirty(hdl->sa_spill, tx);
353 buftypes |= SA_SPILL;
357 if (error && error != ENOENT) {
358 return ((error == ECKSUM) ? EIO : error);
363 if (bulk[i].sa_addr == NULL)
364 return (SET_ERROR(ENOENT));
365 if (bulk[i].sa_data) {
366 SA_COPY_DATA(bulk[i].sa_data_func,
367 bulk[i].sa_addr, bulk[i].sa_data,
373 /* existing rewrite of attr */
374 if (bulk[i].sa_addr &&
375 bulk[i].sa_size == bulk[i].sa_length) {
376 SA_COPY_DATA(bulk[i].sa_data_func,
377 bulk[i].sa_data, bulk[i].sa_addr,
380 } else if (bulk[i].sa_addr) { /* attr size change */
381 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
382 SA_REPLACE, bulk[i].sa_data_func,
383 bulk[i].sa_data, bulk[i].sa_length, tx);
384 } else { /* adding new attribute */
385 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
386 SA_ADD, bulk[i].sa_data_func,
387 bulk[i].sa_data, bulk[i].sa_length, tx);
398 sa_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)
401 sa_os_t *sa = os->os_sa;
402 sa_lot_t *tb, *findtb;
406 ASSERT(MUTEX_HELD(&sa->sa_lock));
407 tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
408 tb->lot_attr_count = attr_count;
409 tb->lot_attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
411 bcopy(attrs, tb->lot_attrs, sizeof (sa_attr_type_t) * attr_count);
412 tb->lot_num = lot_num;
414 tb->lot_instance = 0;
419 if (sa->sa_layout_attr_obj == 0) {
420 sa->sa_layout_attr_obj = zap_create_link(os,
421 DMU_OT_SA_ATTR_LAYOUTS,
422 sa->sa_master_obj, SA_LAYOUTS, tx);
425 (void) snprintf(attr_name, sizeof (attr_name),
427 VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
428 attr_name, 2, attr_count, attrs, tx));
431 list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
432 offsetof(sa_idx_tab_t, sa_next));
434 for (i = 0; i != attr_count; i++) {
435 if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
439 avl_add(&sa->sa_layout_num_tree, tb);
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)
450 avl_add(&sa->sa_layout_hash_tree, tb);
455 sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
456 int count, dmu_tx_t *tx, sa_lot_t **lot)
458 sa_lot_t *tb, tbsearch;
460 sa_os_t *sa = os->os_sa;
461 boolean_t found = B_FALSE;
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);
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) {
477 tb = sa_add_layout_entry(os, attrs, count,
478 avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
480 mutex_exit(&sa->sa_lock);
485 sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
491 blocksize = SPA_MINBLOCKSIZE;
492 } else if (size > SPA_OLD_MAXBLOCKSIZE) {
494 return (SET_ERROR(EFBIG));
496 blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
499 error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
505 sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
508 bcopy(datastart, target, buflen);
513 void *saptr = target;
518 while (bytes < buflen) {
519 func(&dataptr, &length, buflen, start, datastart);
520 bcopy(dataptr, saptr, length);
521 saptr = (void *)((caddr_t)saptr + length);
529 * Determine several different sizes
530 * first the sa header size
531 * the number of bytes to be stored
532 * if spill would occur the index in the attribute array is returned
534 * the boolean will_spill will be set when spilling is necessary. It
535 * is only set when the buftype is SA_BONUS
538 sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
539 dmu_buf_t *db, sa_buf_type_t buftype, int full_space, int *index,
540 int *total, boolean_t *will_spill)
547 if (buftype == SA_BONUS && sa->sa_force_spill) {
550 *will_spill = B_TRUE;
556 *will_spill = B_FALSE;
559 hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
560 sizeof (sa_hdr_phys_t);
562 ASSERT(IS_P2ALIGNED(full_space, 8));
564 for (i = 0; i != attr_count; i++) {
567 *total = P2ROUNDUP(*total, 8);
568 *total += attr_desc[i].sa_length;
572 is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
577 if (is_var_sz && var_size > 1) {
579 * Don't worry that the spill block might overflow.
580 * It will be resized if needed in sa_build_layouts().
582 if (buftype == SA_SPILL ||
583 P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
584 *total < full_space) {
586 * Account for header space used by array of
587 * optional sizes of variable-length attributes.
588 * Record the extra header size in case this
589 * increase needs to be reversed due to
592 hdrsize += sizeof (uint16_t);
594 extra_hdrsize += sizeof (uint16_t);
596 ASSERT(buftype == SA_BONUS);
599 *will_spill = B_TRUE;
605 * find index of where spill *could* occur.
606 * Then continue to count of remainder attribute
607 * space. The sum is used later for sizing bonus
610 if (buftype == SA_BONUS && *index == -1 &&
611 (*total + P2ROUNDUP(hdrsize, 8)) >
612 (full_space - sizeof (blkptr_t))) {
616 if ((*total + P2ROUNDUP(hdrsize, 8)) > full_space &&
618 *will_spill = B_TRUE;
622 hdrsize -= extra_hdrsize;
624 hdrsize = P2ROUNDUP(hdrsize, 8);
628 #define BUF_SPACE_NEEDED(total, header) (total + header)
631 * Find layout that corresponds to ordering of attributes
632 * If not found a new layout number is created and added to
633 * persistent layout tables.
636 sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
639 sa_os_t *sa = hdl->sa_os->os_sa;
641 sa_buf_type_t buftype;
642 sa_hdr_phys_t *sahdr;
645 sa_attr_type_t *attrs, *attrs_start;
649 int spillhdrsize = 0;
651 dmu_object_type_t bonustype;
658 dmu_buf_will_dirty(hdl->sa_bonus, tx);
659 bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
660 dmu_object_dnsize_from_db(hdl->sa_bonus, &dnodesize);
661 bonuslen = DN_BONUS_SIZE(dnodesize);
663 /* first determine bonus header size and sum of all attributes */
664 hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
665 SA_BONUS, bonuslen, &i, &used, &spilling);
667 if (used > SPA_OLD_MAXBLOCKSIZE)
668 return (SET_ERROR(EFBIG));
670 VERIFY(0 == dmu_set_bonus(hdl->sa_bonus, spilling ?
671 MIN(bonuslen - sizeof (blkptr_t), used + hdrsize) :
672 used + hdrsize, tx));
674 ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
675 bonustype == DMU_OT_SA);
677 /* setup and size spill buffer when needed */
681 if (hdl->sa_spill == NULL) {
682 VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, NULL,
683 &hdl->sa_spill) == 0);
685 dmu_buf_will_dirty(hdl->sa_spill, tx);
687 spillhdrsize = sa_find_sizes(sa, &attr_desc[i],
688 attr_count - i, hdl->sa_spill, SA_SPILL,
689 hdl->sa_spill->db_size, &i, &spill_used, &dummy);
691 if (spill_used > SPA_OLD_MAXBLOCKSIZE)
692 return (SET_ERROR(EFBIG));
694 buf_space = hdl->sa_spill->db_size - spillhdrsize;
695 if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
696 hdl->sa_spill->db_size)
697 VERIFY(0 == sa_resize_spill(hdl,
698 BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
701 /* setup starting pointers to lay down data */
702 data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
703 sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
707 buf_space = (sa->sa_force_spill) ?
708 0 : SA_BLKPTR_SPACE - hdrsize;
710 buf_space = hdl->sa_bonus->db_size - hdrsize;
712 attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
716 for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
719 ASSERT(IS_P2ALIGNED(data_start, 8));
720 ASSERT(IS_P2ALIGNED(buf_space, 8));
721 attrs[i] = attr_desc[i].sa_attr;
722 length = SA_REGISTERED_LEN(sa, attrs[i]);
724 length = attr_desc[i].sa_length;
726 VERIFY(length == attr_desc[i].sa_length);
728 if (buf_space < length) { /* switch to spill buffer */
730 VERIFY(bonustype == DMU_OT_SA);
731 if (buftype == SA_BONUS && !sa->sa_force_spill) {
732 sa_find_layout(hdl->sa_os, hash, attrs_start,
733 lot_count, tx, &lot);
734 SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
741 sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
742 sahdr->sa_magic = SA_MAGIC;
743 data_start = (void *)((uintptr_t)sahdr +
745 attrs_start = &attrs[i];
746 buf_space = hdl->sa_spill->db_size - spillhdrsize;
749 hash ^= SA_ATTR_HASH(attrs[i]);
750 attr_desc[i].sa_addr = data_start;
751 attr_desc[i].sa_size = length;
752 SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
754 if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
755 sahdr->sa_lengths[len_idx++] = length;
757 VERIFY((uintptr_t)data_start % 8 == 0);
758 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
760 buf_space -= P2ROUNDUP(length, 8);
764 sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
767 * Verify that old znodes always have layout number 0.
768 * Must be DMU_OT_SA for arbitrary layouts
770 VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
771 (bonustype == DMU_OT_SA && lot->lot_num > 1));
773 if (bonustype == DMU_OT_SA) {
774 SA_SET_HDR(sahdr, lot->lot_num,
775 buftype == SA_BONUS ? hdrsize : spillhdrsize);
778 kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
779 if (hdl->sa_bonus_tab) {
780 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
781 hdl->sa_bonus_tab = NULL;
783 if (!sa->sa_force_spill)
784 VERIFY(0 == sa_build_index(hdl, SA_BONUS));
786 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
789 * remove spill block that is no longer needed.
791 dmu_buf_rele(hdl->sa_spill, NULL);
792 hdl->sa_spill = NULL;
793 hdl->sa_spill_tab = NULL;
794 VERIFY(0 == dmu_rm_spill(hdl->sa_os,
795 sa_handle_object(hdl), tx));
797 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
805 sa_free_attr_table(sa_os_t *sa)
809 if (sa->sa_attr_table == NULL)
812 for (i = 0; i != sa->sa_num_attrs; i++) {
813 if (sa->sa_attr_table[i].sa_name)
814 kmem_free(sa->sa_attr_table[i].sa_name,
815 strlen(sa->sa_attr_table[i].sa_name) + 1);
818 kmem_free(sa->sa_attr_table,
819 sizeof (sa_attr_table_t) * sa->sa_num_attrs);
821 sa->sa_attr_table = NULL;
825 sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
827 sa_os_t *sa = os->os_sa;
828 uint64_t sa_attr_count = 0;
829 uint64_t sa_reg_count = 0;
835 int registered_count = 0;
837 dmu_objset_type_t ostype = dmu_objset_type(os);
840 kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
841 sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
843 if (sa->sa_reg_attr_obj != 0) {
844 error = zap_count(os, sa->sa_reg_attr_obj,
848 * Make sure we retrieved a count and that it isn't zero
850 if (error || (error == 0 && sa_attr_count == 0)) {
852 error = SET_ERROR(EINVAL);
855 sa_reg_count = sa_attr_count;
858 if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
859 sa_attr_count += sa_legacy_attr_count;
861 /* Allocate attribute numbers for attributes that aren't registered */
862 for (i = 0; i != count; i++) {
863 boolean_t found = B_FALSE;
866 if (ostype == DMU_OST_ZFS) {
867 for (j = 0; j != sa_legacy_attr_count; j++) {
868 if (strcmp(reg_attrs[i].sa_name,
869 sa_legacy_attrs[j].sa_name) == 0) {
870 sa->sa_user_table[i] =
871 sa_legacy_attrs[j].sa_attr;
879 if (sa->sa_reg_attr_obj)
880 error = zap_lookup(os, sa->sa_reg_attr_obj,
881 reg_attrs[i].sa_name, 8, 1, &attr_value);
883 error = SET_ERROR(ENOENT);
886 sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
890 sa->sa_user_table[i] = ATTR_NUM(attr_value);
897 sa->sa_num_attrs = sa_attr_count;
898 tb = sa->sa_attr_table =
899 kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
902 * Attribute table is constructed from requested attribute list,
903 * previously foreign registered attributes, and also the legacy
904 * ZPL set of attributes.
907 if (sa->sa_reg_attr_obj) {
908 for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
909 (error = zap_cursor_retrieve(&zc, &za)) == 0;
910 zap_cursor_advance(&zc)) {
912 value = za.za_first_integer;
915 tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
916 tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
917 tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
918 tb[ATTR_NUM(value)].sa_registered = B_TRUE;
920 if (tb[ATTR_NUM(value)].sa_name) {
923 tb[ATTR_NUM(value)].sa_name =
924 kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
925 (void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
926 strlen(za.za_name) +1);
928 zap_cursor_fini(&zc);
930 * Make sure we processed the correct number of registered
933 if (registered_count != sa_reg_count) {
940 if (ostype == DMU_OST_ZFS) {
941 for (i = 0; i != sa_legacy_attr_count; i++) {
944 tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
945 tb[i].sa_length = sa_legacy_attrs[i].sa_length;
946 tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
947 tb[i].sa_registered = B_FALSE;
949 kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
951 (void) strlcpy(tb[i].sa_name,
952 sa_legacy_attrs[i].sa_name,
953 strlen(sa_legacy_attrs[i].sa_name) + 1);
957 for (i = 0; i != count; i++) {
958 sa_attr_type_t attr_id;
960 attr_id = sa->sa_user_table[i];
961 if (tb[attr_id].sa_name)
964 tb[attr_id].sa_length = reg_attrs[i].sa_length;
965 tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
966 tb[attr_id].sa_attr = attr_id;
967 tb[attr_id].sa_name =
968 kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
969 (void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
970 strlen(reg_attrs[i].sa_name) + 1);
973 sa->sa_need_attr_registration =
974 (sa_attr_count != registered_count);
978 kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
979 sa->sa_user_table = NULL;
980 sa_free_attr_table(sa);
981 return ((error != 0) ? error : EINVAL);
985 sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
986 sa_attr_type_t **user_table)
991 dmu_objset_type_t ostype = dmu_objset_type(os);
995 mutex_enter(&os->os_user_ptr_lock);
997 mutex_enter(&os->os_sa->sa_lock);
998 mutex_exit(&os->os_user_ptr_lock);
999 tb = os->os_sa->sa_user_table;
1000 mutex_exit(&os->os_sa->sa_lock);
1005 sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
1006 mutex_init(&sa->sa_lock, NULL, MUTEX_DEFAULT, NULL);
1007 sa->sa_master_obj = sa_obj;
1010 mutex_enter(&sa->sa_lock);
1011 mutex_exit(&os->os_user_ptr_lock);
1012 avl_create(&sa->sa_layout_num_tree, layout_num_compare,
1013 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
1014 avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
1015 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
1018 error = zap_lookup(os, sa_obj, SA_LAYOUTS,
1019 8, 1, &sa->sa_layout_attr_obj);
1020 if (error != 0 && error != ENOENT)
1022 error = zap_lookup(os, sa_obj, SA_REGISTRY,
1023 8, 1, &sa->sa_reg_attr_obj);
1024 if (error != 0 && error != ENOENT)
1028 if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
1031 if (sa->sa_layout_attr_obj != 0) {
1032 uint64_t layout_count;
1034 error = zap_count(os, sa->sa_layout_attr_obj,
1038 * Layout number count should be > 0
1040 if (error || (error == 0 && layout_count == 0)) {
1042 error = SET_ERROR(EINVAL);
1046 for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
1047 (error = zap_cursor_retrieve(&zc, &za)) == 0;
1048 zap_cursor_advance(&zc)) {
1049 sa_attr_type_t *lot_attrs;
1052 lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
1053 za.za_num_integers, KM_SLEEP);
1055 if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
1056 za.za_name, 2, za.za_num_integers,
1057 lot_attrs))) != 0) {
1058 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1059 za.za_num_integers);
1062 VERIFY(ddi_strtoull(za.za_name, NULL, 10,
1063 (unsigned long long *)&lot_num) == 0);
1065 (void) sa_add_layout_entry(os, lot_attrs,
1066 za.za_num_integers, lot_num,
1067 sa_layout_info_hash(lot_attrs,
1068 za.za_num_integers), B_FALSE, NULL);
1069 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1070 za.za_num_integers);
1072 zap_cursor_fini(&zc);
1075 * Make sure layout count matches number of entries added
1078 if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
1084 /* Add special layout number for old ZNODES */
1085 if (ostype == DMU_OST_ZFS) {
1086 (void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
1087 sa_legacy_attr_count, 0,
1088 sa_layout_info_hash(sa_legacy_zpl_layout,
1089 sa_legacy_attr_count), B_FALSE, NULL);
1091 (void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
1094 *user_table = os->os_sa->sa_user_table;
1095 mutex_exit(&sa->sa_lock);
1099 sa_free_attr_table(sa);
1100 if (sa->sa_user_table)
1101 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1102 mutex_exit(&sa->sa_lock);
1103 avl_destroy(&sa->sa_layout_hash_tree);
1104 avl_destroy(&sa->sa_layout_num_tree);
1105 mutex_destroy(&sa->sa_lock);
1106 kmem_free(sa, sizeof (sa_os_t));
1107 return ((error == ECKSUM) ? EIO : error);
1111 sa_tear_down(objset_t *os)
1113 sa_os_t *sa = os->os_sa;
1117 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1119 /* Free up attr table */
1121 sa_free_attr_table(sa);
1124 while (layout = avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie)) {
1126 while (tab = list_head(&layout->lot_idx_tab)) {
1127 ASSERT(refcount_count(&tab->sa_refcount));
1128 sa_idx_tab_rele(os, tab);
1133 while (layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie)) {
1134 kmem_free(layout->lot_attrs,
1135 sizeof (sa_attr_type_t) * layout->lot_attr_count);
1136 kmem_free(layout, sizeof (sa_lot_t));
1139 avl_destroy(&sa->sa_layout_hash_tree);
1140 avl_destroy(&sa->sa_layout_num_tree);
1141 mutex_destroy(&sa->sa_lock);
1143 kmem_free(sa, sizeof (sa_os_t));
1148 sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
1149 uint16_t length, int length_idx, boolean_t var_length, void *userp)
1151 sa_idx_tab_t *idx_tab = userp;
1154 ASSERT(idx_tab->sa_variable_lengths);
1155 idx_tab->sa_variable_lengths[length_idx] = length;
1157 TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
1158 (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
1162 sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
1163 sa_iterfunc_t func, sa_lot_t *tab, void *userp)
1169 sa_os_t *sa = os->os_sa;
1171 uint16_t *length_start = NULL;
1172 uint8_t length_idx = 0;
1175 search.lot_num = SA_LAYOUT_NUM(hdr, type);
1176 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1180 if (IS_SA_BONUSTYPE(type)) {
1181 data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
1182 offsetof(sa_hdr_phys_t, sa_lengths) +
1183 (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
1184 length_start = hdr->sa_lengths;
1189 for (i = 0; i != tb->lot_attr_count; i++) {
1190 int attr_length, reg_length;
1193 reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
1195 attr_length = reg_length;
1198 attr_length = length_start[length_idx];
1199 idx_len = length_idx++;
1202 func(hdr, data_start, tb->lot_attrs[i], attr_length,
1203 idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
1205 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
1212 sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
1213 uint16_t length, int length_idx, boolean_t variable_length, void *userp)
1215 sa_handle_t *hdl = userp;
1216 sa_os_t *sa = hdl->sa_os->os_sa;
1218 sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
1222 sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
1224 sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1226 sa_os_t *sa = hdl->sa_os->os_sa;
1227 int num_lengths = 1;
1230 ASSERT(MUTEX_HELD(&sa->sa_lock));
1231 if (sa_hdr_phys->sa_magic == SA_MAGIC)
1234 db = SA_GET_DB(hdl, buftype);
1236 if (buftype == SA_SPILL) {
1237 arc_release(db->db_buf, NULL);
1238 arc_buf_thaw(db->db_buf);
1241 sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
1242 sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
1245 * Determine number of variable lenghts in header
1246 * The standard 8 byte header has one for free and a
1247 * 16 byte header would have 4 + 1;
1249 if (SA_HDR_SIZE(sa_hdr_phys) > 8)
1250 num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
1251 for (i = 0; i != num_lengths; i++)
1252 sa_hdr_phys->sa_lengths[i] =
1253 BSWAP_16(sa_hdr_phys->sa_lengths[i]);
1255 sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
1256 sa_byteswap_cb, NULL, hdl);
1258 if (buftype == SA_SPILL)
1259 arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
1263 sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
1265 sa_hdr_phys_t *sa_hdr_phys;
1266 dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
1267 dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
1268 sa_os_t *sa = hdl->sa_os->os_sa;
1269 sa_idx_tab_t *idx_tab;
1271 sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1273 mutex_enter(&sa->sa_lock);
1275 /* Do we need to byteswap? */
1277 /* only check if not old znode */
1278 if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
1279 sa_hdr_phys->sa_magic != 0) {
1280 VERIFY(BSWAP_32(sa_hdr_phys->sa_magic) == SA_MAGIC);
1281 sa_byteswap(hdl, buftype);
1284 idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
1286 if (buftype == SA_BONUS)
1287 hdl->sa_bonus_tab = idx_tab;
1289 hdl->sa_spill_tab = idx_tab;
1291 mutex_exit(&sa->sa_lock);
1297 sa_evict_sync(void *dbu)
1299 panic("evicting sa dbuf\n");
1303 sa_idx_tab_rele(objset_t *os, void *arg)
1305 sa_os_t *sa = os->os_sa;
1306 sa_idx_tab_t *idx_tab = arg;
1308 if (idx_tab == NULL)
1311 mutex_enter(&sa->sa_lock);
1312 if (refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
1313 list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
1314 if (idx_tab->sa_variable_lengths)
1315 kmem_free(idx_tab->sa_variable_lengths,
1317 idx_tab->sa_layout->lot_var_sizes);
1318 refcount_destroy(&idx_tab->sa_refcount);
1319 kmem_free(idx_tab->sa_idx_tab,
1320 sizeof (uint32_t) * sa->sa_num_attrs);
1321 kmem_free(idx_tab, sizeof (sa_idx_tab_t));
1323 mutex_exit(&sa->sa_lock);
1327 sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
1329 sa_os_t *sa = os->os_sa;
1331 ASSERT(MUTEX_HELD(&sa->sa_lock));
1332 (void) refcount_add(&idx_tab->sa_refcount, NULL);
1336 sa_handle_destroy(sa_handle_t *hdl)
1338 dmu_buf_t *db = hdl->sa_bonus;
1340 mutex_enter(&hdl->sa_lock);
1341 (void) dmu_buf_remove_user(db, &hdl->sa_dbu);
1343 if (hdl->sa_bonus_tab)
1344 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
1346 if (hdl->sa_spill_tab)
1347 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1349 dmu_buf_rele(hdl->sa_bonus, NULL);
1352 dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
1353 mutex_exit(&hdl->sa_lock);
1355 kmem_cache_free(sa_cache, hdl);
1359 sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
1360 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1363 dmu_object_info_t doi;
1364 sa_handle_t *handle = NULL;
1367 dmu_object_info_from_db(db, &doi);
1368 ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
1369 doi.doi_bonus_type == DMU_OT_ZNODE);
1371 /* find handle, if it exists */
1372 /* if one doesn't exist then create a new one, and initialize it */
1374 if (hdl_type == SA_HDL_SHARED)
1375 handle = dmu_buf_get_user(db);
1377 if (handle == NULL) {
1378 sa_handle_t *winner = NULL;
1380 handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
1381 handle->sa_dbu.dbu_evict_func_sync = NULL;
1382 handle->sa_dbu.dbu_evict_func_async = NULL;
1383 handle->sa_userp = userp;
1384 handle->sa_bonus = db;
1386 handle->sa_spill = NULL;
1387 handle->sa_bonus_tab = NULL;
1388 handle->sa_spill_tab = NULL;
1390 error = sa_build_index(handle, SA_BONUS);
1392 if (hdl_type == SA_HDL_SHARED) {
1393 dmu_buf_init_user(&handle->sa_dbu, sa_evict_sync, NULL,
1395 winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1398 if (winner != NULL) {
1399 kmem_cache_free(sa_cache, handle);
1409 sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
1410 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1415 if (error = dmu_bonus_hold(objset, objid, NULL, &db))
1418 return (sa_handle_get_from_db(objset, db, userp, hdl_type,
1423 sa_buf_hold(objset_t *objset, uint64_t obj_num, void *tag, dmu_buf_t **db)
1425 return (dmu_bonus_hold(objset, obj_num, tag, db));
1429 sa_buf_rele(dmu_buf_t *db, void *tag)
1431 dmu_buf_rele(db, tag);
1435 sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
1438 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1439 return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
1443 sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1446 sa_bulk_attr_t bulk;
1448 bulk.sa_attr = attr;
1450 bulk.sa_length = buflen;
1451 bulk.sa_data_func = NULL;
1454 mutex_enter(&hdl->sa_lock);
1455 error = sa_lookup_impl(hdl, &bulk, 1);
1456 mutex_exit(&hdl->sa_lock);
1462 sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, uio_t *uio)
1465 sa_bulk_attr_t bulk;
1467 bulk.sa_data = NULL;
1468 bulk.sa_attr = attr;
1469 bulk.sa_data_func = NULL;
1473 mutex_enter(&hdl->sa_lock);
1474 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
1475 error = uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
1476 uio->uio_resid), UIO_READ, uio);
1478 mutex_exit(&hdl->sa_lock);
1484 static sa_idx_tab_t *
1485 sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, sa_hdr_phys_t *hdr)
1487 sa_idx_tab_t *idx_tab;
1488 sa_os_t *sa = os->os_sa;
1489 sa_lot_t *tb, search;
1493 * Deterimine layout number. If SA node and header == 0 then
1494 * force the index table to the dummy "1" empty layout.
1496 * The layout number would only be zero for a newly created file
1497 * that has not added any attributes yet, or with crypto enabled which
1498 * doesn't write any attributes to the bonus buffer.
1501 search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
1503 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1505 /* Verify header size is consistent with layout information */
1507 ASSERT(IS_SA_BONUSTYPE(bonustype) &&
1508 SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) || !IS_SA_BONUSTYPE(bonustype) ||
1509 (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
1512 * See if any of the already existing TOC entries can be reused?
1515 for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
1516 idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
1517 boolean_t valid_idx = B_TRUE;
1520 if (tb->lot_var_sizes != 0 &&
1521 idx_tab->sa_variable_lengths != NULL) {
1522 for (i = 0; i != tb->lot_var_sizes; i++) {
1523 if (hdr->sa_lengths[i] !=
1524 idx_tab->sa_variable_lengths[i]) {
1525 valid_idx = B_FALSE;
1531 sa_idx_tab_hold(os, idx_tab);
1536 /* No such luck, create a new entry */
1537 idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
1538 idx_tab->sa_idx_tab =
1539 kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
1540 idx_tab->sa_layout = tb;
1541 refcount_create(&idx_tab->sa_refcount);
1542 if (tb->lot_var_sizes)
1543 idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
1544 tb->lot_var_sizes, KM_SLEEP);
1546 sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
1548 sa_idx_tab_hold(os, idx_tab); /* one hold for consumer */
1549 sa_idx_tab_hold(os, idx_tab); /* one for layout */
1550 list_insert_tail(&tb->lot_idx_tab, idx_tab);
1555 sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
1556 boolean_t start, void *userdata)
1560 *dataptr = userdata;
1565 sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
1567 uint64_t attr_value = 0;
1568 sa_os_t *sa = hdl->sa_os->os_sa;
1569 sa_attr_table_t *tb = sa->sa_attr_table;
1572 mutex_enter(&sa->sa_lock);
1574 if (!sa->sa_need_attr_registration || sa->sa_master_obj == 0) {
1575 mutex_exit(&sa->sa_lock);
1579 if (sa->sa_reg_attr_obj == 0) {
1580 sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1581 DMU_OT_SA_ATTR_REGISTRATION,
1582 sa->sa_master_obj, SA_REGISTRY, tx);
1584 for (i = 0; i != sa->sa_num_attrs; i++) {
1585 if (sa->sa_attr_table[i].sa_registered)
1587 ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
1589 VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
1590 tb[i].sa_name, 8, 1, &attr_value, tx));
1591 tb[i].sa_registered = B_TRUE;
1593 sa->sa_need_attr_registration = B_FALSE;
1594 mutex_exit(&sa->sa_lock);
1598 * Replace all attributes with attributes specified in template.
1599 * If dnode had a spill buffer then those attributes will be
1600 * also be replaced, possibly with just an empty spill block
1602 * This interface is intended to only be used for bulk adding of
1603 * attributes for a new file. It will also be used by the ZPL
1604 * when converting and old formatted znode to native SA support.
1607 sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1608 int attr_count, dmu_tx_t *tx)
1610 sa_os_t *sa = hdl->sa_os->os_sa;
1612 if (sa->sa_need_attr_registration)
1613 sa_attr_register_sync(hdl, tx);
1614 return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
1618 sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1619 int attr_count, dmu_tx_t *tx)
1623 mutex_enter(&hdl->sa_lock);
1624 error = sa_replace_all_by_template_locked(hdl, attr_desc,
1626 mutex_exit(&hdl->sa_lock);
1631 * Add/remove a single attribute or replace a variable-sized attribute value
1632 * with a value of a different size, and then rewrite the entire set
1634 * Same-length attribute value replacement (including fixed-length attributes)
1635 * is handled more efficiently by the upper layers.
1638 sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1639 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1640 uint16_t buflen, dmu_tx_t *tx)
1642 sa_os_t *sa = hdl->sa_os->os_sa;
1643 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1645 sa_bulk_attr_t *attr_desc;
1647 int bonus_attr_count = 0;
1648 int bonus_data_size = 0;
1649 int spill_data_size = 0;
1650 int spill_attr_count = 0;
1652 uint16_t length, reg_length;
1653 int i, j, k, length_idx;
1655 sa_idx_tab_t *idx_tab;
1659 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1661 /* First make of copy of the old data */
1665 if (dn->dn_bonuslen != 0) {
1666 bonus_data_size = hdl->sa_bonus->db_size;
1667 old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
1668 bcopy(hdl->sa_bonus->db_data, old_data[0],
1669 hdl->sa_bonus->db_size);
1670 bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
1676 /* Bring spill buffer online if it isn't currently */
1678 if ((error = sa_get_spill(hdl)) == 0) {
1679 spill_data_size = hdl->sa_spill->db_size;
1680 old_data[1] = kmem_alloc(spill_data_size, KM_SLEEP);
1681 bcopy(hdl->sa_spill->db_data, old_data[1],
1682 hdl->sa_spill->db_size);
1684 hdl->sa_spill_tab->sa_layout->lot_attr_count;
1685 } else if (error && error != ENOENT) {
1687 kmem_free(old_data[0], bonus_data_size);
1693 /* build descriptor of all attributes */
1695 attr_count = bonus_attr_count + spill_attr_count;
1696 if (action == SA_ADD)
1698 else if (action == SA_REMOVE)
1701 attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
1704 * loop through bonus and spill buffer if it exists, and
1705 * build up new attr_descriptor to reset the attributes
1708 count = bonus_attr_count;
1709 hdr = SA_GET_HDR(hdl, SA_BONUS);
1710 idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
1711 for (; k != 2; k++) {
1713 * Iterate over each attribute in layout. Fetch the
1714 * size of variable-length attributes needing rewrite
1715 * from sa_lengths[].
1717 for (i = 0, length_idx = 0; i != count; i++) {
1718 sa_attr_type_t attr;
1720 attr = idx_tab->sa_layout->lot_attrs[i];
1721 reg_length = SA_REGISTERED_LEN(sa, attr);
1722 if (reg_length == 0) {
1723 length = hdr->sa_lengths[length_idx];
1726 length = reg_length;
1728 if (attr == newattr) {
1730 * There is nothing to do for SA_REMOVE,
1731 * so it is just skipped.
1733 if (action == SA_REMOVE)
1737 * Duplicate attributes are not allowed, so the
1738 * action can not be SA_ADD here.
1740 ASSERT3S(action, ==, SA_REPLACE);
1743 * Only a variable-sized attribute can be
1744 * replaced here, and its size must be changing.
1746 ASSERT3U(reg_length, ==, 0);
1747 ASSERT3U(length, !=, buflen);
1748 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1749 locator, datastart, buflen);
1751 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1753 (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
1754 (uintptr_t)old_data[k]), length);
1757 if (k == 0 && hdl->sa_spill) {
1758 hdr = SA_GET_HDR(hdl, SA_SPILL);
1759 idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
1760 count = spill_attr_count;
1765 if (action == SA_ADD) {
1766 reg_length = SA_REGISTERED_LEN(sa, newattr);
1767 IMPLY(reg_length != 0, reg_length == buflen);
1768 SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
1771 ASSERT3U(j, ==, attr_count);
1773 error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
1776 kmem_free(old_data[0], bonus_data_size);
1778 kmem_free(old_data[1], spill_data_size);
1779 kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
1785 sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
1789 sa_os_t *sa = hdl->sa_os->os_sa;
1790 dmu_object_type_t bonustype;
1792 bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
1795 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1797 /* sync out registration table if necessary */
1798 if (sa->sa_need_attr_registration)
1799 sa_attr_register_sync(hdl, tx);
1801 error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
1802 if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
1803 sa->sa_update_cb(hdl, tx);
1809 * update or add new attribute
1812 sa_update(sa_handle_t *hdl, sa_attr_type_t type,
1813 void *buf, uint32_t buflen, dmu_tx_t *tx)
1816 sa_bulk_attr_t bulk;
1818 bulk.sa_attr = type;
1819 bulk.sa_data_func = NULL;
1820 bulk.sa_length = buflen;
1823 mutex_enter(&hdl->sa_lock);
1824 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
1825 mutex_exit(&hdl->sa_lock);
1830 sa_update_from_cb(sa_handle_t *hdl, sa_attr_type_t attr,
1831 uint32_t buflen, sa_data_locator_t *locator, void *userdata, dmu_tx_t *tx)
1834 sa_bulk_attr_t bulk;
1836 bulk.sa_attr = attr;
1837 bulk.sa_data = userdata;
1838 bulk.sa_data_func = locator;
1839 bulk.sa_length = buflen;
1841 mutex_enter(&hdl->sa_lock);
1842 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
1843 mutex_exit(&hdl->sa_lock);
1848 * Return size of an attribute
1852 sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
1854 sa_bulk_attr_t bulk;
1857 bulk.sa_data = NULL;
1858 bulk.sa_attr = attr;
1859 bulk.sa_data_func = NULL;
1862 mutex_enter(&hdl->sa_lock);
1863 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
1864 mutex_exit(&hdl->sa_lock);
1867 *size = bulk.sa_size;
1869 mutex_exit(&hdl->sa_lock);
1874 sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1877 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1878 return (sa_lookup_impl(hdl, attrs, count));
1882 sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1887 mutex_enter(&hdl->sa_lock);
1888 error = sa_bulk_lookup_locked(hdl, attrs, count);
1889 mutex_exit(&hdl->sa_lock);
1894 sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
1899 mutex_enter(&hdl->sa_lock);
1900 error = sa_bulk_update_impl(hdl, attrs, count, tx);
1901 mutex_exit(&hdl->sa_lock);
1906 sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
1910 mutex_enter(&hdl->sa_lock);
1911 error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
1913 mutex_exit(&hdl->sa_lock);
1918 sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
1920 dmu_object_info_from_db((dmu_buf_t *)hdl->sa_bonus, doi);
1924 sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
1926 dmu_object_size_from_db((dmu_buf_t *)hdl->sa_bonus,
1931 sa_set_userp(sa_handle_t *hdl, void *ptr)
1933 hdl->sa_userp = ptr;
1937 sa_get_db(sa_handle_t *hdl)
1939 return ((dmu_buf_t *)hdl->sa_bonus);
1943 sa_get_userdata(sa_handle_t *hdl)
1945 return (hdl->sa_userp);
1949 sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
1951 ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
1952 os->os_sa->sa_update_cb = func;
1956 sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
1959 mutex_enter(&os->os_sa->sa_lock);
1960 sa_register_update_callback_locked(os, func);
1961 mutex_exit(&os->os_sa->sa_lock);
1965 sa_handle_object(sa_handle_t *hdl)
1967 return (hdl->sa_bonus->db_object);
1971 sa_enabled(objset_t *os)
1973 return (os->os_sa == NULL);
1977 sa_set_sa_object(objset_t *os, uint64_t sa_object)
1979 sa_os_t *sa = os->os_sa;
1981 if (sa->sa_master_obj)
1984 sa->sa_master_obj = sa_object;
1990 sa_hdrsize(void *arg)
1992 sa_hdr_phys_t *hdr = arg;
1994 return (SA_HDR_SIZE(hdr));
1998 sa_handle_lock(sa_handle_t *hdl)
2001 mutex_enter(&hdl->sa_lock);
2005 sa_handle_unlock(sa_handle_t *hdl)
2008 mutex_exit(&hdl->sa_lock);