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 https://opensource.org/licenses/CDDL-1.0.
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 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 #include <sys/zfs_context.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/sysmacros.h>
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_tx.h>
37 #include <sys/dnode.h>
40 #include <sys/sunddi.h>
41 #include <sys/sa_impl.h>
42 #include <sys/errno.h>
43 #include <sys/zfs_context.h>
46 #include <sys/zfs_znode.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 what's
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 * attributes. 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 static const arc_byteswap_func_t sa_bswap_table[] = {
145 byteswap_uint64_array,
146 byteswap_uint32_array,
147 byteswap_uint16_array,
148 byteswap_uint8_array,
152 #ifdef HAVE_EFFICIENT_UNALIGNED_ACCESS
153 #define SA_COPY_DATA(f, s, t, l) \
157 *(uint64_t *)t = *(uint64_t *)s; \
158 } else if (l == 16) { \
159 *(uint64_t *)t = *(uint64_t *)s; \
160 *(uint64_t *)((uintptr_t)t + 8) = \
161 *(uint64_t *)((uintptr_t)s + 8); \
166 sa_copy_data(f, s, t, l); \
170 #define SA_COPY_DATA(f, s, t, l) sa_copy_data(f, s, t, l)
174 * This table is fixed and cannot be changed. Its purpose is to
175 * allow the SA code to work with both old/new ZPL file systems.
176 * It contains the list of legacy attributes. These attributes aren't
177 * stored in the "attribute" registry zap objects, since older ZPL file systems
178 * won't have the registry. Only objsets of type ZFS_TYPE_FILESYSTEM will
179 * use this static table.
181 static const sa_attr_reg_t sa_legacy_attrs[] = {
182 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
183 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
184 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
185 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
186 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
187 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
188 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
189 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
190 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
191 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
192 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
193 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
194 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
195 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
196 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
197 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
201 * This is only used for objects of type DMU_OT_ZNODE
203 static const sa_attr_type_t sa_legacy_zpl_layout[] = {
204 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
208 * Special dummy layout used for buffers with no attributes.
210 static const sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
212 static const size_t sa_legacy_attr_count = ARRAY_SIZE(sa_legacy_attrs);
213 static kmem_cache_t *sa_cache = NULL;
216 sa_cache_constructor(void *buf, void *unused, int kmflag)
218 (void) unused, (void) kmflag;
219 sa_handle_t *hdl = buf;
221 mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
226 sa_cache_destructor(void *buf, void *unused)
229 sa_handle_t *hdl = buf;
230 mutex_destroy(&hdl->sa_lock);
236 sa_cache = kmem_cache_create("sa_cache",
237 sizeof (sa_handle_t), 0, sa_cache_constructor,
238 sa_cache_destructor, NULL, NULL, NULL, 0);
245 kmem_cache_destroy(sa_cache);
249 layout_num_compare(const void *arg1, const void *arg2)
251 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
252 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
254 return (TREE_CMP(node1->lot_num, node2->lot_num));
258 layout_hash_compare(const void *arg1, const void *arg2)
260 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
261 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
263 int cmp = TREE_CMP(node1->lot_hash, node2->lot_hash);
267 return (TREE_CMP(node1->lot_instance, node2->lot_instance));
271 sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
275 if (count != tbf->lot_attr_count)
278 for (i = 0; i != count; i++) {
279 if (attrs[i] != tbf->lot_attrs[i])
285 #define SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
288 sa_layout_info_hash(const sa_attr_type_t *attrs, int attr_count)
290 uint64_t crc = -1ULL;
292 for (int i = 0; i != attr_count; i++)
293 crc ^= SA_ATTR_HASH(attrs[i]);
299 sa_get_spill(sa_handle_t *hdl)
302 if (hdl->sa_spill == NULL) {
303 if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
304 &hdl->sa_spill)) == 0)
305 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
314 * Main attribute lookup/update function
315 * returns 0 for success or non zero for failures
317 * Operates on bulk array, first failure will abort further processing
320 sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
321 sa_data_op_t data_op, dmu_tx_t *tx)
323 sa_os_t *sa = hdl->sa_os->os_sa;
326 sa_buf_type_t buftypes;
331 for (i = 0; i != count; i++) {
332 ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
334 bulk[i].sa_addr = NULL;
335 /* First check the bonus buffer */
337 if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
338 hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
339 SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
340 SA_GET_HDR(hdl, SA_BONUS),
341 bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
342 if (tx && !(buftypes & SA_BONUS)) {
343 dmu_buf_will_dirty(hdl->sa_bonus, tx);
344 buftypes |= SA_BONUS;
347 if (bulk[i].sa_addr == NULL &&
348 ((error = sa_get_spill(hdl)) == 0)) {
349 if (TOC_ATTR_PRESENT(
350 hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
351 SA_ATTR_INFO(sa, hdl->sa_spill_tab,
352 SA_GET_HDR(hdl, SA_SPILL),
353 bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
354 if (tx && !(buftypes & SA_SPILL) &&
355 bulk[i].sa_size == bulk[i].sa_length) {
356 dmu_buf_will_dirty(hdl->sa_spill, tx);
357 buftypes |= SA_SPILL;
361 if (error && error != ENOENT) {
362 return ((error == ECKSUM) ? EIO : error);
367 if (bulk[i].sa_addr == NULL)
368 return (SET_ERROR(ENOENT));
369 if (bulk[i].sa_data) {
370 SA_COPY_DATA(bulk[i].sa_data_func,
371 bulk[i].sa_addr, bulk[i].sa_data,
377 /* existing rewrite of attr */
378 if (bulk[i].sa_addr &&
379 bulk[i].sa_size == bulk[i].sa_length) {
380 SA_COPY_DATA(bulk[i].sa_data_func,
381 bulk[i].sa_data, bulk[i].sa_addr,
384 } else if (bulk[i].sa_addr) { /* attr size change */
385 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
386 SA_REPLACE, bulk[i].sa_data_func,
387 bulk[i].sa_data, bulk[i].sa_length, tx);
388 } else { /* adding new attribute */
389 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
390 SA_ADD, bulk[i].sa_data_func,
391 bulk[i].sa_data, bulk[i].sa_length, tx);
404 sa_add_layout_entry(objset_t *os, const sa_attr_type_t *attrs, int attr_count,
405 uint64_t lot_num, uint64_t hash, boolean_t zapadd, dmu_tx_t *tx)
407 sa_os_t *sa = os->os_sa;
408 sa_lot_t *tb, *findtb;
412 ASSERT(MUTEX_HELD(&sa->sa_lock));
413 tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
414 tb->lot_attr_count = attr_count;
415 tb->lot_attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
417 memcpy(tb->lot_attrs, attrs, sizeof (sa_attr_type_t) * attr_count);
418 tb->lot_num = lot_num;
420 tb->lot_instance = 0;
425 if (sa->sa_layout_attr_obj == 0) {
426 sa->sa_layout_attr_obj = zap_create_link(os,
427 DMU_OT_SA_ATTR_LAYOUTS,
428 sa->sa_master_obj, SA_LAYOUTS, tx);
431 (void) snprintf(attr_name, sizeof (attr_name),
433 VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
434 attr_name, 2, attr_count, attrs, tx));
437 list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
438 offsetof(sa_idx_tab_t, sa_next));
440 for (i = 0; i != attr_count; i++) {
441 if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
445 avl_add(&sa->sa_layout_num_tree, tb);
447 /* verify we don't have a hash collision */
448 if ((findtb = avl_find(&sa->sa_layout_hash_tree, tb, &loc)) != NULL) {
449 for (; findtb && findtb->lot_hash == hash;
450 findtb = AVL_NEXT(&sa->sa_layout_hash_tree, findtb)) {
451 if (findtb->lot_instance != tb->lot_instance)
456 avl_add(&sa->sa_layout_hash_tree, tb);
461 sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
462 int count, dmu_tx_t *tx, sa_lot_t **lot)
464 sa_lot_t *tb, tbsearch;
466 sa_os_t *sa = os->os_sa;
467 boolean_t found = B_FALSE;
469 mutex_enter(&sa->sa_lock);
470 tbsearch.lot_hash = hash;
471 tbsearch.lot_instance = 0;
472 tb = avl_find(&sa->sa_layout_hash_tree, &tbsearch, &loc);
474 for (; tb && tb->lot_hash == hash;
475 tb = AVL_NEXT(&sa->sa_layout_hash_tree, tb)) {
476 if (sa_layout_equal(tb, attrs, count) == 0) {
483 tb = sa_add_layout_entry(os, attrs, count,
484 avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
486 mutex_exit(&sa->sa_lock);
491 sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
497 blocksize = SPA_MINBLOCKSIZE;
498 } else if (size > SPA_OLD_MAXBLOCKSIZE) {
500 return (SET_ERROR(EFBIG));
502 blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
505 error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
511 sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
514 memcpy(target, datastart, buflen);
519 void *saptr = target;
524 while (bytes < buflen) {
525 func(&dataptr, &length, buflen, start, datastart);
526 memcpy(saptr, dataptr, length);
527 saptr = (void *)((caddr_t)saptr + length);
535 * Determine several different values pertaining to system attribute
538 * Return the size of the sa_hdr_phys_t header for the buffer. Each
539 * variable length attribute except the first contributes two bytes to
540 * the header size, which is then rounded up to an 8-byte boundary.
542 * The following output parameters are also computed.
544 * index - The index of the first attribute in attr_desc that will
545 * spill over. Only valid if will_spill is set.
547 * total - The total number of bytes of all system attributes described
550 * will_spill - Set when spilling is necessary. It is only set when
551 * the buftype is SA_BONUS.
554 sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
555 dmu_buf_t *db, sa_buf_type_t buftype, int full_space, int *index,
556 int *total, boolean_t *will_spill)
558 int var_size_count = 0;
563 if (buftype == SA_BONUS && sa->sa_force_spill) {
566 *will_spill = B_TRUE;
572 *will_spill = B_FALSE;
575 hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
576 sizeof (sa_hdr_phys_t);
578 ASSERT(IS_P2ALIGNED(full_space, 8));
580 for (i = 0; i != attr_count; i++) {
581 boolean_t is_var_sz, might_spill_here;
584 *total = P2ROUNDUP(*total, 8);
585 *total += attr_desc[i].sa_length;
589 is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
594 * Calculate what the SA header size would be if this
595 * attribute doesn't spill.
597 tmp_hdrsize = hdrsize + ((is_var_sz && var_size_count > 1) ?
598 sizeof (uint16_t) : 0);
601 * Check whether this attribute spans into the space
602 * that would be used by the spill block pointer should
603 * a spill block be needed.
606 buftype == SA_BONUS && *index == -1 &&
607 (*total + P2ROUNDUP(tmp_hdrsize, 8)) >
608 (full_space - sizeof (blkptr_t));
610 if (is_var_sz && var_size_count > 1) {
611 if (buftype == SA_SPILL ||
612 tmp_hdrsize + *total < full_space) {
614 * Record the extra header size in case this
615 * increase needs to be reversed due to
618 hdrsize = tmp_hdrsize;
619 if (*index != -1 || might_spill_here)
620 extra_hdrsize += sizeof (uint16_t);
622 ASSERT(buftype == SA_BONUS);
625 *will_spill = B_TRUE;
631 * Store index of where spill *could* occur. Then
632 * continue to count the remaining attribute sizes. The
633 * sum is used later for sizing bonus and spill buffer.
635 if (might_spill_here)
638 if ((*total + P2ROUNDUP(hdrsize, 8)) > full_space &&
640 *will_spill = B_TRUE;
644 hdrsize -= extra_hdrsize;
646 hdrsize = P2ROUNDUP(hdrsize, 8);
650 #define BUF_SPACE_NEEDED(total, header) (total + header)
653 * Find layout that corresponds to ordering of attributes
654 * If not found a new layout number is created and added to
655 * persistent layout tables.
658 sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
661 sa_os_t *sa = hdl->sa_os->os_sa;
663 sa_buf_type_t buftype;
664 sa_hdr_phys_t *sahdr;
666 sa_attr_type_t *attrs, *attrs_start;
671 int spillhdrsize = 0;
673 dmu_object_type_t bonustype;
680 dmu_buf_will_dirty(hdl->sa_bonus, tx);
681 bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
682 dmu_object_dnsize_from_db(hdl->sa_bonus, &dnodesize);
683 bonuslen = DN_BONUS_SIZE(dnodesize);
685 /* first determine bonus header size and sum of all attributes */
686 hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
687 SA_BONUS, bonuslen, &spill_idx, &used, &spilling);
689 if (used > SPA_OLD_MAXBLOCKSIZE)
690 return (SET_ERROR(EFBIG));
692 VERIFY0(dmu_set_bonus(hdl->sa_bonus, spilling ?
693 MIN(bonuslen - sizeof (blkptr_t), used + hdrsize) :
694 used + hdrsize, tx));
696 ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
697 bonustype == DMU_OT_SA);
699 /* setup and size spill buffer when needed */
703 if (hdl->sa_spill == NULL) {
704 VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, 0, NULL,
705 &hdl->sa_spill) == 0);
707 dmu_buf_will_dirty(hdl->sa_spill, tx);
709 spillhdrsize = sa_find_sizes(sa, &attr_desc[spill_idx],
710 attr_count - spill_idx, hdl->sa_spill, SA_SPILL,
711 hdl->sa_spill->db_size, &i, &spill_used, &dummy);
713 if (spill_used > SPA_OLD_MAXBLOCKSIZE)
714 return (SET_ERROR(EFBIG));
716 if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
717 hdl->sa_spill->db_size)
718 VERIFY(0 == sa_resize_spill(hdl,
719 BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
722 /* setup starting pointers to lay down data */
723 data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
724 sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
727 attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
731 for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
734 ASSERT(IS_P2ALIGNED(data_start, 8));
735 attrs[i] = attr_desc[i].sa_attr;
736 length = SA_REGISTERED_LEN(sa, attrs[i]);
738 length = attr_desc[i].sa_length;
740 if (spilling && i == spill_idx) { /* switch to spill buffer */
741 VERIFY(bonustype == DMU_OT_SA);
742 if (buftype == SA_BONUS && !sa->sa_force_spill) {
743 sa_find_layout(hdl->sa_os, hash, attrs_start,
744 lot_count, tx, &lot);
745 SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
752 sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
753 sahdr->sa_magic = SA_MAGIC;
754 data_start = (void *)((uintptr_t)sahdr +
756 attrs_start = &attrs[i];
759 hash ^= SA_ATTR_HASH(attrs[i]);
760 attr_desc[i].sa_addr = data_start;
761 attr_desc[i].sa_size = length;
762 SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
764 if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
765 sahdr->sa_lengths[len_idx++] = length;
767 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
772 sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
775 * Verify that old znodes always have layout number 0.
776 * Must be DMU_OT_SA for arbitrary layouts
778 VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
779 (bonustype == DMU_OT_SA && lot->lot_num > 1));
781 if (bonustype == DMU_OT_SA) {
782 SA_SET_HDR(sahdr, lot->lot_num,
783 buftype == SA_BONUS ? hdrsize : spillhdrsize);
786 kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
787 if (hdl->sa_bonus_tab) {
788 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
789 hdl->sa_bonus_tab = NULL;
791 if (!sa->sa_force_spill)
792 VERIFY(0 == sa_build_index(hdl, SA_BONUS));
794 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
797 * remove spill block that is no longer needed.
799 dmu_buf_rele(hdl->sa_spill, NULL);
800 hdl->sa_spill = NULL;
801 hdl->sa_spill_tab = NULL;
802 VERIFY(0 == dmu_rm_spill(hdl->sa_os,
803 sa_handle_object(hdl), tx));
805 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
813 sa_free_attr_table(sa_os_t *sa)
817 if (sa->sa_attr_table == NULL)
820 for (i = 0; i != sa->sa_num_attrs; i++) {
821 if (sa->sa_attr_table[i].sa_name)
822 kmem_free(sa->sa_attr_table[i].sa_name,
823 strlen(sa->sa_attr_table[i].sa_name) + 1);
826 kmem_free(sa->sa_attr_table,
827 sizeof (sa_attr_table_t) * sa->sa_num_attrs);
829 sa->sa_attr_table = NULL;
833 sa_attr_table_setup(objset_t *os, const sa_attr_reg_t *reg_attrs, int count)
835 sa_os_t *sa = os->os_sa;
836 uint64_t sa_attr_count = 0;
837 uint64_t sa_reg_count = 0;
843 int registered_count = 0;
845 dmu_objset_type_t ostype = dmu_objset_type(os);
848 kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
849 sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
851 if (sa->sa_reg_attr_obj != 0) {
852 error = zap_count(os, sa->sa_reg_attr_obj,
856 * Make sure we retrieved a count and that it isn't zero
858 if (error || (error == 0 && sa_attr_count == 0)) {
860 error = SET_ERROR(EINVAL);
863 sa_reg_count = sa_attr_count;
866 if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
867 sa_attr_count += sa_legacy_attr_count;
869 /* Allocate attribute numbers for attributes that aren't registered */
870 for (i = 0; i != count; i++) {
871 boolean_t found = B_FALSE;
874 if (ostype == DMU_OST_ZFS) {
875 for (j = 0; j != sa_legacy_attr_count; j++) {
876 if (strcmp(reg_attrs[i].sa_name,
877 sa_legacy_attrs[j].sa_name) == 0) {
878 sa->sa_user_table[i] =
879 sa_legacy_attrs[j].sa_attr;
887 if (sa->sa_reg_attr_obj)
888 error = zap_lookup(os, sa->sa_reg_attr_obj,
889 reg_attrs[i].sa_name, 8, 1, &attr_value);
891 error = SET_ERROR(ENOENT);
894 sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
898 sa->sa_user_table[i] = ATTR_NUM(attr_value);
905 sa->sa_num_attrs = sa_attr_count;
906 tb = sa->sa_attr_table =
907 kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
910 * Attribute table is constructed from requested attribute list,
911 * previously foreign registered attributes, and also the legacy
912 * ZPL set of attributes.
915 if (sa->sa_reg_attr_obj) {
916 for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
917 (error = zap_cursor_retrieve(&zc, &za)) == 0;
918 zap_cursor_advance(&zc)) {
920 value = za.za_first_integer;
923 tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
924 tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
925 tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
926 tb[ATTR_NUM(value)].sa_registered = B_TRUE;
928 if (tb[ATTR_NUM(value)].sa_name) {
931 tb[ATTR_NUM(value)].sa_name =
932 kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
933 (void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
934 strlen(za.za_name) +1);
936 zap_cursor_fini(&zc);
938 * Make sure we processed the correct number of registered
941 if (registered_count != sa_reg_count) {
948 if (ostype == DMU_OST_ZFS) {
949 for (i = 0; i != sa_legacy_attr_count; i++) {
952 tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
953 tb[i].sa_length = sa_legacy_attrs[i].sa_length;
954 tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
955 tb[i].sa_registered = B_FALSE;
957 kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
959 (void) strlcpy(tb[i].sa_name,
960 sa_legacy_attrs[i].sa_name,
961 strlen(sa_legacy_attrs[i].sa_name) + 1);
965 for (i = 0; i != count; i++) {
966 sa_attr_type_t attr_id;
968 attr_id = sa->sa_user_table[i];
969 if (tb[attr_id].sa_name)
972 tb[attr_id].sa_length = reg_attrs[i].sa_length;
973 tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
974 tb[attr_id].sa_attr = attr_id;
975 tb[attr_id].sa_name =
976 kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
977 (void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
978 strlen(reg_attrs[i].sa_name) + 1);
981 sa->sa_need_attr_registration =
982 (sa_attr_count != registered_count);
986 kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
987 sa->sa_user_table = NULL;
988 sa_free_attr_table(sa);
994 sa_setup(objset_t *os, uint64_t sa_obj, const sa_attr_reg_t *reg_attrs,
995 int count, sa_attr_type_t **user_table)
1000 dmu_objset_type_t ostype = dmu_objset_type(os);
1004 mutex_enter(&os->os_user_ptr_lock);
1006 mutex_enter(&os->os_sa->sa_lock);
1007 mutex_exit(&os->os_user_ptr_lock);
1008 tb = os->os_sa->sa_user_table;
1009 mutex_exit(&os->os_sa->sa_lock);
1014 sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
1015 mutex_init(&sa->sa_lock, NULL, MUTEX_NOLOCKDEP, NULL);
1016 sa->sa_master_obj = sa_obj;
1019 mutex_enter(&sa->sa_lock);
1020 mutex_exit(&os->os_user_ptr_lock);
1021 avl_create(&sa->sa_layout_num_tree, layout_num_compare,
1022 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
1023 avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
1024 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
1027 error = zap_lookup(os, sa_obj, SA_LAYOUTS,
1028 8, 1, &sa->sa_layout_attr_obj);
1029 if (error != 0 && error != ENOENT)
1031 error = zap_lookup(os, sa_obj, SA_REGISTRY,
1032 8, 1, &sa->sa_reg_attr_obj);
1033 if (error != 0 && error != ENOENT)
1037 if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
1040 if (sa->sa_layout_attr_obj != 0) {
1041 uint64_t layout_count;
1043 error = zap_count(os, sa->sa_layout_attr_obj,
1047 * Layout number count should be > 0
1049 if (error || (error == 0 && layout_count == 0)) {
1051 error = SET_ERROR(EINVAL);
1055 for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
1056 (error = zap_cursor_retrieve(&zc, &za)) == 0;
1057 zap_cursor_advance(&zc)) {
1058 sa_attr_type_t *lot_attrs;
1061 lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
1062 za.za_num_integers, KM_SLEEP);
1064 if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
1065 za.za_name, 2, za.za_num_integers,
1066 lot_attrs))) != 0) {
1067 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1068 za.za_num_integers);
1071 VERIFY0(ddi_strtoull(za.za_name, NULL, 10,
1072 (unsigned long long *)&lot_num));
1074 (void) sa_add_layout_entry(os, lot_attrs,
1075 za.za_num_integers, lot_num,
1076 sa_layout_info_hash(lot_attrs,
1077 za.za_num_integers), B_FALSE, NULL);
1078 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1079 za.za_num_integers);
1081 zap_cursor_fini(&zc);
1084 * Make sure layout count matches number of entries added
1087 if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
1093 /* Add special layout number for old ZNODES */
1094 if (ostype == DMU_OST_ZFS) {
1095 (void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
1096 sa_legacy_attr_count, 0,
1097 sa_layout_info_hash(sa_legacy_zpl_layout,
1098 sa_legacy_attr_count), B_FALSE, NULL);
1100 (void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
1103 *user_table = os->os_sa->sa_user_table;
1104 mutex_exit(&sa->sa_lock);
1108 sa_free_attr_table(sa);
1109 if (sa->sa_user_table)
1110 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1111 mutex_exit(&sa->sa_lock);
1112 avl_destroy(&sa->sa_layout_hash_tree);
1113 avl_destroy(&sa->sa_layout_num_tree);
1114 mutex_destroy(&sa->sa_lock);
1115 kmem_free(sa, sizeof (sa_os_t));
1116 return ((error == ECKSUM) ? EIO : error);
1120 sa_tear_down(objset_t *os)
1122 sa_os_t *sa = os->os_sa;
1126 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1128 /* Free up attr table */
1130 sa_free_attr_table(sa);
1134 avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie))) {
1136 while ((tab = list_head(&layout->lot_idx_tab))) {
1137 ASSERT(zfs_refcount_count(&tab->sa_refcount));
1138 sa_idx_tab_rele(os, tab);
1143 while ((layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie))) {
1144 kmem_free(layout->lot_attrs,
1145 sizeof (sa_attr_type_t) * layout->lot_attr_count);
1146 kmem_free(layout, sizeof (sa_lot_t));
1149 avl_destroy(&sa->sa_layout_hash_tree);
1150 avl_destroy(&sa->sa_layout_num_tree);
1151 mutex_destroy(&sa->sa_lock);
1153 kmem_free(sa, sizeof (sa_os_t));
1158 sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
1159 uint16_t length, int length_idx, boolean_t var_length, void *userp)
1161 sa_idx_tab_t *idx_tab = userp;
1164 ASSERT(idx_tab->sa_variable_lengths);
1165 idx_tab->sa_variable_lengths[length_idx] = length;
1167 TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
1168 (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
1172 sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
1173 sa_iterfunc_t func, sa_lot_t *tab, void *userp)
1179 sa_os_t *sa = os->os_sa;
1181 uint16_t *length_start = NULL;
1182 uint8_t length_idx = 0;
1185 search.lot_num = SA_LAYOUT_NUM(hdr, type);
1186 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1190 if (IS_SA_BONUSTYPE(type)) {
1191 data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
1192 offsetof(sa_hdr_phys_t, sa_lengths) +
1193 (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
1194 length_start = hdr->sa_lengths;
1199 for (i = 0; i != tb->lot_attr_count; i++) {
1200 int attr_length, reg_length;
1203 reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
1205 attr_length = reg_length;
1208 attr_length = length_start[length_idx];
1209 idx_len = length_idx++;
1212 func(hdr, data_start, tb->lot_attrs[i], attr_length,
1213 idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
1215 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
1221 sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
1222 uint16_t length, int length_idx, boolean_t variable_length, void *userp)
1224 (void) hdr, (void) length_idx, (void) variable_length;
1225 sa_handle_t *hdl = userp;
1226 sa_os_t *sa = hdl->sa_os->os_sa;
1228 sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
1232 sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
1234 sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1236 int num_lengths = 1;
1238 sa_os_t *sa __maybe_unused = hdl->sa_os->os_sa;
1240 ASSERT(MUTEX_HELD(&sa->sa_lock));
1241 if (sa_hdr_phys->sa_magic == SA_MAGIC)
1244 db = SA_GET_DB(hdl, buftype);
1246 if (buftype == SA_SPILL) {
1247 arc_release(db->db_buf, NULL);
1248 arc_buf_thaw(db->db_buf);
1251 sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
1252 sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
1255 * Determine number of variable lengths in header
1256 * The standard 8 byte header has one for free and a
1257 * 16 byte header would have 4 + 1;
1259 if (SA_HDR_SIZE(sa_hdr_phys) > 8)
1260 num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
1261 for (i = 0; i != num_lengths; i++)
1262 sa_hdr_phys->sa_lengths[i] =
1263 BSWAP_16(sa_hdr_phys->sa_lengths[i]);
1265 sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
1266 sa_byteswap_cb, NULL, hdl);
1268 if (buftype == SA_SPILL)
1269 arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
1273 sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
1275 sa_hdr_phys_t *sa_hdr_phys;
1276 dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
1277 dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
1278 sa_os_t *sa = hdl->sa_os->os_sa;
1279 sa_idx_tab_t *idx_tab;
1281 sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1283 mutex_enter(&sa->sa_lock);
1285 /* Do we need to byteswap? */
1287 /* only check if not old znode */
1288 if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
1289 sa_hdr_phys->sa_magic != 0) {
1290 if (BSWAP_32(sa_hdr_phys->sa_magic) != SA_MAGIC) {
1291 mutex_exit(&sa->sa_lock);
1292 zfs_dbgmsg("Buffer Header: %x != SA_MAGIC:%x "
1293 "object=%#llx\n", sa_hdr_phys->sa_magic, SA_MAGIC,
1294 (u_longlong_t)db->db.db_object);
1295 return (SET_ERROR(EIO));
1297 sa_byteswap(hdl, buftype);
1300 idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
1302 if (buftype == SA_BONUS)
1303 hdl->sa_bonus_tab = idx_tab;
1305 hdl->sa_spill_tab = idx_tab;
1307 mutex_exit(&sa->sa_lock);
1312 sa_evict_sync(void *dbu)
1315 panic("evicting sa dbuf\n");
1319 sa_idx_tab_rele(objset_t *os, void *arg)
1321 sa_os_t *sa = os->os_sa;
1322 sa_idx_tab_t *idx_tab = arg;
1324 if (idx_tab == NULL)
1327 mutex_enter(&sa->sa_lock);
1328 if (zfs_refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
1329 list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
1330 if (idx_tab->sa_variable_lengths)
1331 kmem_free(idx_tab->sa_variable_lengths,
1333 idx_tab->sa_layout->lot_var_sizes);
1334 zfs_refcount_destroy(&idx_tab->sa_refcount);
1335 kmem_free(idx_tab->sa_idx_tab,
1336 sizeof (uint32_t) * sa->sa_num_attrs);
1337 kmem_free(idx_tab, sizeof (sa_idx_tab_t));
1339 mutex_exit(&sa->sa_lock);
1343 sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
1345 sa_os_t *sa __maybe_unused = os->os_sa;
1347 ASSERT(MUTEX_HELD(&sa->sa_lock));
1348 (void) zfs_refcount_add(&idx_tab->sa_refcount, NULL);
1352 sa_spill_rele(sa_handle_t *hdl)
1354 mutex_enter(&hdl->sa_lock);
1355 if (hdl->sa_spill) {
1356 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1357 dmu_buf_rele(hdl->sa_spill, NULL);
1358 hdl->sa_spill = NULL;
1359 hdl->sa_spill_tab = NULL;
1361 mutex_exit(&hdl->sa_lock);
1365 sa_handle_destroy(sa_handle_t *hdl)
1367 dmu_buf_t *db = hdl->sa_bonus;
1369 mutex_enter(&hdl->sa_lock);
1370 (void) dmu_buf_remove_user(db, &hdl->sa_dbu);
1372 if (hdl->sa_bonus_tab)
1373 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
1375 if (hdl->sa_spill_tab)
1376 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1378 dmu_buf_rele(hdl->sa_bonus, NULL);
1381 dmu_buf_rele(hdl->sa_spill, NULL);
1382 mutex_exit(&hdl->sa_lock);
1384 kmem_cache_free(sa_cache, hdl);
1388 sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
1389 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1392 sa_handle_t *handle = NULL;
1394 dmu_object_info_t doi;
1396 dmu_object_info_from_db(db, &doi);
1397 ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
1398 doi.doi_bonus_type == DMU_OT_ZNODE);
1400 /* find handle, if it exists */
1401 /* if one doesn't exist then create a new one, and initialize it */
1403 if (hdl_type == SA_HDL_SHARED)
1404 handle = dmu_buf_get_user(db);
1406 if (handle == NULL) {
1407 sa_handle_t *winner = NULL;
1409 handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
1410 handle->sa_dbu.dbu_evict_func_sync = NULL;
1411 handle->sa_dbu.dbu_evict_func_async = NULL;
1412 handle->sa_userp = userp;
1413 handle->sa_bonus = db;
1415 handle->sa_spill = NULL;
1416 handle->sa_bonus_tab = NULL;
1417 handle->sa_spill_tab = NULL;
1419 error = sa_build_index(handle, SA_BONUS);
1421 if (hdl_type == SA_HDL_SHARED) {
1422 dmu_buf_init_user(&handle->sa_dbu, sa_evict_sync, NULL,
1424 winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1427 if (winner != NULL) {
1428 kmem_cache_free(sa_cache, handle);
1438 sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
1439 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1444 if ((error = dmu_bonus_hold(objset, objid, NULL, &db)))
1447 return (sa_handle_get_from_db(objset, db, userp, hdl_type,
1452 sa_buf_hold(objset_t *objset, uint64_t obj_num, const void *tag, dmu_buf_t **db)
1454 return (dmu_bonus_hold(objset, obj_num, tag, db));
1458 sa_buf_rele(dmu_buf_t *db, const void *tag)
1460 dmu_buf_rele(db, tag);
1464 sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
1467 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1468 return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
1472 sa_lookup_locked(sa_handle_t *hdl, sa_attr_type_t attr, void *buf,
1476 sa_bulk_attr_t bulk;
1478 VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
1480 bulk.sa_attr = attr;
1482 bulk.sa_length = buflen;
1483 bulk.sa_data_func = NULL;
1486 error = sa_lookup_impl(hdl, &bulk, 1);
1491 sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1495 mutex_enter(&hdl->sa_lock);
1496 error = sa_lookup_locked(hdl, attr, buf, buflen);
1497 mutex_exit(&hdl->sa_lock);
1504 sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, zfs_uio_t *uio)
1507 sa_bulk_attr_t bulk;
1509 bulk.sa_data = NULL;
1510 bulk.sa_attr = attr;
1511 bulk.sa_data_func = NULL;
1515 mutex_enter(&hdl->sa_lock);
1516 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
1517 error = zfs_uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
1518 zfs_uio_resid(uio)), UIO_READ, uio);
1520 mutex_exit(&hdl->sa_lock);
1525 * For the existed object that is upgraded from old system, its ondisk layout
1526 * has no slot for the project ID attribute. But quota accounting logic needs
1527 * to access related slots by offset directly. So we need to adjust these old
1528 * objects' layout to make the project ID to some unified and fixed offset.
1531 sa_add_projid(sa_handle_t *hdl, dmu_tx_t *tx, uint64_t projid)
1533 znode_t *zp = sa_get_userdata(hdl);
1534 dmu_buf_t *db = sa_get_db(hdl);
1535 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1536 int count = 0, err = 0;
1537 sa_bulk_attr_t *bulk, *attrs;
1538 zfs_acl_locator_cb_t locate = { 0 };
1539 uint64_t uid, gid, mode, rdev, xattr = 0, parent, gen, links;
1540 uint64_t crtime[2], mtime[2], ctime[2], atime[2];
1541 zfs_acl_phys_t znode_acl = { 0 };
1542 char scanstamp[AV_SCANSTAMP_SZ];
1544 if (zp->z_acl_cached == NULL) {
1547 mutex_enter(&zp->z_acl_lock);
1548 err = zfs_acl_node_read(zp, B_FALSE, &aclp, B_FALSE);
1549 mutex_exit(&zp->z_acl_lock);
1550 if (err != 0 && err != ENOENT)
1554 bulk = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1555 attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
1556 mutex_enter(&hdl->sa_lock);
1557 mutex_enter(&zp->z_lock);
1559 err = sa_lookup_locked(hdl, SA_ZPL_PROJID(zfsvfs), &projid,
1561 if (unlikely(err == 0))
1562 /* Someone has added project ID attr by race. */
1567 /* First do a bulk query of the attributes that aren't cached */
1569 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1571 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1573 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1575 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1577 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1579 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1581 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1583 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1585 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1587 if (Z_ISBLK(ZTOTYPE(zp)) || Z_ISCHR(ZTOTYPE(zp)))
1588 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1591 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1593 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
1595 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
1597 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1599 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1601 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1603 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
1605 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL,
1607 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1609 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1611 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1613 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
1616 err = sa_bulk_lookup_locked(hdl, bulk, count);
1620 err = sa_lookup_locked(hdl, SA_ZPL_XATTR(zfsvfs), &xattr, 8);
1621 if (err != 0 && err != ENOENT)
1624 zp->z_projid = projid;
1625 zp->z_pflags |= ZFS_PROJID;
1626 links = ZTONLNK(zp);
1630 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
1631 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
1633 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GEN(zfsvfs), NULL, &gen, 8);
1634 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
1635 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
1636 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
1637 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1639 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
1640 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1641 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1642 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
1644 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
1645 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_PROJID(zfsvfs), NULL, &projid, 8);
1647 if (Z_ISBLK(ZTOTYPE(zp)) || Z_ISCHR(ZTOTYPE(zp)))
1648 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
1651 if (zp->z_acl_cached != NULL) {
1652 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1653 &zp->z_acl_cached->z_acl_count, 8);
1654 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
1655 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
1656 locate.cb_aclp = zp->z_acl_cached;
1657 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
1658 zfs_acl_data_locator, &locate,
1659 zp->z_acl_cached->z_acl_bytes);
1663 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_XATTR(zfsvfs), NULL,
1666 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
1668 (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
1670 SA_ADD_BULK_ATTR(attrs, count, SA_ZPL_SCANSTAMP(zfsvfs), NULL,
1671 scanstamp, AV_SCANSTAMP_SZ);
1672 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
1675 VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
1676 VERIFY(sa_replace_all_by_template_locked(hdl, attrs, count, tx) == 0);
1677 if (znode_acl.z_acl_extern_obj) {
1678 VERIFY(0 == dmu_object_free(zfsvfs->z_os,
1679 znode_acl.z_acl_extern_obj, tx));
1682 zp->z_is_sa = B_TRUE;
1685 mutex_exit(&zp->z_lock);
1686 mutex_exit(&hdl->sa_lock);
1687 kmem_free(attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
1688 kmem_free(bulk, sizeof (sa_bulk_attr_t) * ZPL_END);
1693 static sa_idx_tab_t *
1694 sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, sa_hdr_phys_t *hdr)
1696 sa_idx_tab_t *idx_tab;
1697 sa_os_t *sa = os->os_sa;
1698 sa_lot_t *tb, search;
1702 * Deterimine layout number. If SA node and header == 0 then
1703 * force the index table to the dummy "1" empty layout.
1705 * The layout number would only be zero for a newly created file
1706 * that has not added any attributes yet, or with crypto enabled which
1707 * doesn't write any attributes to the bonus buffer.
1710 search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
1712 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1714 /* Verify header size is consistent with layout information */
1716 ASSERT((IS_SA_BONUSTYPE(bonustype) &&
1717 SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb)) || !IS_SA_BONUSTYPE(bonustype) ||
1718 (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
1721 * See if any of the already existing TOC entries can be reused?
1724 for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
1725 idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
1726 boolean_t valid_idx = B_TRUE;
1729 if (tb->lot_var_sizes != 0 &&
1730 idx_tab->sa_variable_lengths != NULL) {
1731 for (i = 0; i != tb->lot_var_sizes; i++) {
1732 if (hdr->sa_lengths[i] !=
1733 idx_tab->sa_variable_lengths[i]) {
1734 valid_idx = B_FALSE;
1740 sa_idx_tab_hold(os, idx_tab);
1745 /* No such luck, create a new entry */
1746 idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
1747 idx_tab->sa_idx_tab =
1748 kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
1749 idx_tab->sa_layout = tb;
1750 zfs_refcount_create(&idx_tab->sa_refcount);
1751 if (tb->lot_var_sizes)
1752 idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
1753 tb->lot_var_sizes, KM_SLEEP);
1755 sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
1757 sa_idx_tab_hold(os, idx_tab); /* one hold for consumer */
1758 sa_idx_tab_hold(os, idx_tab); /* one for layout */
1759 list_insert_tail(&tb->lot_idx_tab, idx_tab);
1764 sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
1765 boolean_t start, void *userdata)
1769 *dataptr = userdata;
1774 sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
1776 uint64_t attr_value = 0;
1777 sa_os_t *sa = hdl->sa_os->os_sa;
1778 sa_attr_table_t *tb = sa->sa_attr_table;
1781 mutex_enter(&sa->sa_lock);
1783 if (!sa->sa_need_attr_registration || sa->sa_master_obj == 0) {
1784 mutex_exit(&sa->sa_lock);
1788 if (sa->sa_reg_attr_obj == 0) {
1789 sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1790 DMU_OT_SA_ATTR_REGISTRATION,
1791 sa->sa_master_obj, SA_REGISTRY, tx);
1793 for (i = 0; i != sa->sa_num_attrs; i++) {
1794 if (sa->sa_attr_table[i].sa_registered)
1796 ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
1798 VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
1799 tb[i].sa_name, 8, 1, &attr_value, tx));
1800 tb[i].sa_registered = B_TRUE;
1802 sa->sa_need_attr_registration = B_FALSE;
1803 mutex_exit(&sa->sa_lock);
1807 * Replace all attributes with attributes specified in template.
1808 * If dnode had a spill buffer then those attributes will be
1809 * also be replaced, possibly with just an empty spill block
1811 * This interface is intended to only be used for bulk adding of
1812 * attributes for a new file. It will also be used by the ZPL
1813 * when converting and old formatted znode to native SA support.
1816 sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1817 int attr_count, dmu_tx_t *tx)
1819 sa_os_t *sa = hdl->sa_os->os_sa;
1821 if (sa->sa_need_attr_registration)
1822 sa_attr_register_sync(hdl, tx);
1823 return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
1827 sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1828 int attr_count, dmu_tx_t *tx)
1832 mutex_enter(&hdl->sa_lock);
1833 error = sa_replace_all_by_template_locked(hdl, attr_desc,
1835 mutex_exit(&hdl->sa_lock);
1840 * Add/remove a single attribute or replace a variable-sized attribute value
1841 * with a value of a different size, and then rewrite the entire set
1843 * Same-length attribute value replacement (including fixed-length attributes)
1844 * is handled more efficiently by the upper layers.
1847 sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1848 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1849 uint16_t buflen, dmu_tx_t *tx)
1851 sa_os_t *sa = hdl->sa_os->os_sa;
1852 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1854 sa_bulk_attr_t *attr_desc;
1856 int bonus_attr_count = 0;
1857 int bonus_data_size = 0;
1858 int spill_data_size = 0;
1859 int spill_attr_count = 0;
1861 uint16_t length, reg_length;
1862 int i, j, k, length_idx;
1864 sa_idx_tab_t *idx_tab;
1868 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1870 /* First make of copy of the old data */
1874 if (dn->dn_bonuslen != 0) {
1875 bonus_data_size = hdl->sa_bonus->db_size;
1876 old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
1877 memcpy(old_data[0], hdl->sa_bonus->db_data,
1878 hdl->sa_bonus->db_size);
1879 bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
1885 /* Bring spill buffer online if it isn't currently */
1887 if ((error = sa_get_spill(hdl)) == 0) {
1888 spill_data_size = hdl->sa_spill->db_size;
1889 old_data[1] = vmem_alloc(spill_data_size, KM_SLEEP);
1890 memcpy(old_data[1], hdl->sa_spill->db_data,
1891 hdl->sa_spill->db_size);
1893 hdl->sa_spill_tab->sa_layout->lot_attr_count;
1894 } else if (error && error != ENOENT) {
1896 kmem_free(old_data[0], bonus_data_size);
1902 /* build descriptor of all attributes */
1904 attr_count = bonus_attr_count + spill_attr_count;
1905 if (action == SA_ADD)
1907 else if (action == SA_REMOVE)
1910 attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
1913 * loop through bonus and spill buffer if it exists, and
1914 * build up new attr_descriptor to reset the attributes
1917 count = bonus_attr_count;
1918 hdr = SA_GET_HDR(hdl, SA_BONUS);
1919 idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
1920 for (; k != 2; k++) {
1922 * Iterate over each attribute in layout. Fetch the
1923 * size of variable-length attributes needing rewrite
1924 * from sa_lengths[].
1926 for (i = 0, length_idx = 0; i != count; i++) {
1927 sa_attr_type_t attr;
1929 attr = idx_tab->sa_layout->lot_attrs[i];
1930 reg_length = SA_REGISTERED_LEN(sa, attr);
1931 if (reg_length == 0) {
1932 length = hdr->sa_lengths[length_idx];
1935 length = reg_length;
1937 if (attr == newattr) {
1939 * There is nothing to do for SA_REMOVE,
1940 * so it is just skipped.
1942 if (action == SA_REMOVE)
1946 * Duplicate attributes are not allowed, so the
1947 * action can not be SA_ADD here.
1949 ASSERT3S(action, ==, SA_REPLACE);
1952 * Only a variable-sized attribute can be
1953 * replaced here, and its size must be changing.
1955 ASSERT3U(reg_length, ==, 0);
1956 ASSERT3U(length, !=, buflen);
1957 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1958 locator, datastart, buflen);
1960 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1962 (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
1963 (uintptr_t)old_data[k]), length);
1966 if (k == 0 && hdl->sa_spill) {
1967 hdr = SA_GET_HDR(hdl, SA_SPILL);
1968 idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
1969 count = spill_attr_count;
1974 if (action == SA_ADD) {
1975 reg_length = SA_REGISTERED_LEN(sa, newattr);
1976 IMPLY(reg_length != 0, reg_length == buflen);
1977 SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
1980 ASSERT3U(j, ==, attr_count);
1982 error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
1985 kmem_free(old_data[0], bonus_data_size);
1987 vmem_free(old_data[1], spill_data_size);
1988 kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
1994 sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
1998 sa_os_t *sa = hdl->sa_os->os_sa;
1999 dmu_object_type_t bonustype;
2000 dmu_buf_t *saved_spill;
2003 ASSERT(MUTEX_HELD(&hdl->sa_lock));
2005 bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
2006 saved_spill = hdl->sa_spill;
2008 /* sync out registration table if necessary */
2009 if (sa->sa_need_attr_registration)
2010 sa_attr_register_sync(hdl, tx);
2012 error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
2013 if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
2014 sa->sa_update_cb(hdl, tx);
2017 * If saved_spill is NULL and current sa_spill is not NULL that
2018 * means we increased the refcount of the spill buffer through
2019 * sa_get_spill() or dmu_spill_hold_by_dnode(). Therefore we
2020 * must release the hold before calling dmu_tx_commit() to avoid
2021 * making a copy of this buffer in dbuf_sync_leaf() due to the
2022 * reference count now being greater than 1.
2024 if (!saved_spill && hdl->sa_spill) {
2025 if (hdl->sa_spill_tab) {
2026 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
2027 hdl->sa_spill_tab = NULL;
2030 dmu_buf_rele(hdl->sa_spill, NULL);
2031 hdl->sa_spill = NULL;
2038 * update or add new attribute
2041 sa_update(sa_handle_t *hdl, sa_attr_type_t type,
2042 void *buf, uint32_t buflen, dmu_tx_t *tx)
2045 sa_bulk_attr_t bulk;
2047 VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
2049 bulk.sa_attr = type;
2050 bulk.sa_data_func = NULL;
2051 bulk.sa_length = buflen;
2054 mutex_enter(&hdl->sa_lock);
2055 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
2056 mutex_exit(&hdl->sa_lock);
2061 * Return size of an attribute
2065 sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
2067 sa_bulk_attr_t bulk;
2070 bulk.sa_data = NULL;
2071 bulk.sa_attr = attr;
2072 bulk.sa_data_func = NULL;
2075 mutex_enter(&hdl->sa_lock);
2076 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
2077 mutex_exit(&hdl->sa_lock);
2080 *size = bulk.sa_size;
2082 mutex_exit(&hdl->sa_lock);
2087 sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
2090 ASSERT(MUTEX_HELD(&hdl->sa_lock));
2091 return (sa_lookup_impl(hdl, attrs, count));
2095 sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
2100 mutex_enter(&hdl->sa_lock);
2101 error = sa_bulk_lookup_locked(hdl, attrs, count);
2102 mutex_exit(&hdl->sa_lock);
2107 sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
2112 mutex_enter(&hdl->sa_lock);
2113 error = sa_bulk_update_impl(hdl, attrs, count, tx);
2114 mutex_exit(&hdl->sa_lock);
2119 sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
2123 mutex_enter(&hdl->sa_lock);
2124 error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
2126 mutex_exit(&hdl->sa_lock);
2131 sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
2133 dmu_object_info_from_db(hdl->sa_bonus, doi);
2137 sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
2139 dmu_object_size_from_db(hdl->sa_bonus,
2144 sa_set_userp(sa_handle_t *hdl, void *ptr)
2146 hdl->sa_userp = ptr;
2150 sa_get_db(sa_handle_t *hdl)
2152 return (hdl->sa_bonus);
2156 sa_get_userdata(sa_handle_t *hdl)
2158 return (hdl->sa_userp);
2162 sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
2164 ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
2165 os->os_sa->sa_update_cb = func;
2169 sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
2172 mutex_enter(&os->os_sa->sa_lock);
2173 sa_register_update_callback_locked(os, func);
2174 mutex_exit(&os->os_sa->sa_lock);
2178 sa_handle_object(sa_handle_t *hdl)
2180 return (hdl->sa_bonus->db_object);
2184 sa_enabled(objset_t *os)
2186 return (os->os_sa == NULL);
2190 sa_set_sa_object(objset_t *os, uint64_t sa_object)
2192 sa_os_t *sa = os->os_sa;
2194 if (sa->sa_master_obj)
2197 sa->sa_master_obj = sa_object;
2203 sa_hdrsize(void *arg)
2205 sa_hdr_phys_t *hdr = arg;
2207 return (SA_HDR_SIZE(hdr));
2211 sa_handle_lock(sa_handle_t *hdl)
2214 mutex_enter(&hdl->sa_lock);
2218 sa_handle_unlock(sa_handle_t *hdl)
2221 mutex_exit(&hdl->sa_lock);
2225 EXPORT_SYMBOL(sa_handle_get);
2226 EXPORT_SYMBOL(sa_handle_get_from_db);
2227 EXPORT_SYMBOL(sa_handle_destroy);
2228 EXPORT_SYMBOL(sa_buf_hold);
2229 EXPORT_SYMBOL(sa_buf_rele);
2230 EXPORT_SYMBOL(sa_spill_rele);
2231 EXPORT_SYMBOL(sa_lookup);
2232 EXPORT_SYMBOL(sa_update);
2233 EXPORT_SYMBOL(sa_remove);
2234 EXPORT_SYMBOL(sa_bulk_lookup);
2235 EXPORT_SYMBOL(sa_bulk_lookup_locked);
2236 EXPORT_SYMBOL(sa_bulk_update);
2237 EXPORT_SYMBOL(sa_size);
2238 EXPORT_SYMBOL(sa_object_info);
2239 EXPORT_SYMBOL(sa_object_size);
2240 EXPORT_SYMBOL(sa_get_userdata);
2241 EXPORT_SYMBOL(sa_set_userp);
2242 EXPORT_SYMBOL(sa_get_db);
2243 EXPORT_SYMBOL(sa_handle_object);
2244 EXPORT_SYMBOL(sa_register_update_callback);
2245 EXPORT_SYMBOL(sa_setup);
2246 EXPORT_SYMBOL(sa_replace_all_by_template);
2247 EXPORT_SYMBOL(sa_replace_all_by_template_locked);
2248 EXPORT_SYMBOL(sa_enabled);
2249 EXPORT_SYMBOL(sa_cache_init);
2250 EXPORT_SYMBOL(sa_cache_fini);
2251 EXPORT_SYMBOL(sa_set_sa_object);
2252 EXPORT_SYMBOL(sa_hdrsize);
2253 EXPORT_SYMBOL(sa_handle_lock);
2254 EXPORT_SYMBOL(sa_handle_unlock);
2255 EXPORT_SYMBOL(sa_lookup_uio);
2256 EXPORT_SYMBOL(sa_add_projid);
2257 #endif /* _KERNEL */