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 * Copyright (c) 2013 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/systm.h>
32 #include <sys/sysmacros.h>
34 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dmu_tx.h>
38 #include <sys/dnode.h>
41 #include <sys/sunddi.h>
42 #include <sys/sa_impl.h>
43 #include <sys/dnode.h>
44 #include <sys/errno.h>
45 #include <sys/zfs_context.h>
48 * ZFS System attributes:
50 * A generic mechanism to allow for arbitrary attributes
51 * to be stored in a dnode. The data will be stored in the bonus buffer of
52 * the dnode and if necessary a special "spill" block will be used to handle
53 * overflow situations. The spill block will be sized to fit the data
54 * from 512 - 128K. When a spill block is used the BP (blkptr_t) for the
55 * spill block is stored at the end of the current bonus buffer. Any
56 * attributes that would be in the way of the blkptr_t will be relocated
57 * into the spill block.
59 * Attribute registration:
61 * Stored persistently on a per dataset basis
62 * a mapping between attribute "string" names and their actual attribute
63 * numeric values, length, and byteswap function. The names are only used
64 * during registration. All attributes are known by their unique attribute
65 * id value. If an attribute can have a variable size then the value
66 * 0 will be used to indicate this.
70 * Attribute layouts are a way to compactly store multiple attributes, but
71 * without taking the overhead associated with managing each attribute
72 * individually. Since you will typically have the same set of attributes
73 * stored in the same order a single table will be used to represent that
74 * layout. The ZPL for example will usually have only about 10 different
75 * layouts (regular files, device files, symlinks,
76 * regular files + scanstamp, files/dir with extended attributes, and then
77 * you have the possibility of all of those minus ACL, because it would
78 * be kicked out into the spill block)
80 * Layouts are simply an array of the attributes and their
81 * ordering i.e. [0, 1, 4, 5, 2]
83 * Each distinct layout is given a unique layout number and that is whats
84 * stored in the header at the beginning of the SA data buffer.
86 * A layout only covers a single dbuf (bonus or spill). If a set of
87 * attributes is split up between the bonus buffer and a spill buffer then
88 * two different layouts will be used. This allows us to byteswap the
89 * spill without looking at the bonus buffer and keeps the on disk format of
90 * the bonus and spill buffer the same.
92 * Adding a single attribute will cause the entire set of attributes to
93 * be rewritten and could result in a new layout number being constructed
94 * as part of the rewrite if no such layout exists for the new set of
95 * attribues. The new attribute will be appended to the end of the already
96 * existing attributes.
98 * Both the attribute registration and attribute layout information are
99 * stored in normal ZAP attributes. Their should be a small number of
100 * known layouts and the set of attributes is assumed to typically be quite
103 * The registered attributes and layout "table" information is maintained
104 * in core and a special "sa_os_t" is attached to the objset_t.
106 * A special interface is provided to allow for quickly applying
107 * a large set of attributes at once. sa_replace_all_by_template() is
108 * used to set an array of attributes. This is used by the ZPL when
109 * creating a brand new file. The template that is passed into the function
110 * specifies the attribute, size for variable length attributes, location of
111 * data and special "data locator" function if the data isn't in a contiguous
114 * Byteswap implications:
116 * Since the SA attributes are not entirely self describing we can't do
117 * the normal byteswap processing. The special ZAP layout attribute and
118 * attribute registration attributes define the byteswap function and the
119 * size of the attributes, unless it is variable sized.
120 * The normal ZFS byteswapping infrastructure assumes you don't need
121 * to read any objects in order to do the necessary byteswapping. Whereas
122 * SA attributes can only be properly byteswapped if the dataset is opened
123 * and the layout/attribute ZAP attributes are available. Because of this
124 * the SA attributes will be byteswapped when they are first accessed by
125 * the SA code that will read the SA data.
128 typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
129 uint16_t length, int length_idx, boolean_t, void *userp);
131 static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
132 static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
133 static void *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
135 static void sa_idx_tab_rele(objset_t *os, void *arg);
136 static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
138 static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
139 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
140 uint16_t buflen, dmu_tx_t *tx);
142 arc_byteswap_func_t sa_bswap_table[] = {
143 byteswap_uint64_array,
144 byteswap_uint32_array,
145 byteswap_uint16_array,
146 byteswap_uint8_array,
150 #define SA_COPY_DATA(f, s, t, l) \
154 *(uint64_t *)t = *(uint64_t *)s; \
155 } else if (l == 16) { \
156 *(uint64_t *)t = *(uint64_t *)s; \
157 *(uint64_t *)((uintptr_t)t + 8) = \
158 *(uint64_t *)((uintptr_t)s + 8); \
163 sa_copy_data(f, s, t, l); \
167 * This table is fixed and cannot be changed. Its purpose is to
168 * allow the SA code to work with both old/new ZPL file systems.
169 * It contains the list of legacy attributes. These attributes aren't
170 * stored in the "attribute" registry zap objects, since older ZPL file systems
171 * won't have the registry. Only objsets of type ZFS_TYPE_FILESYSTEM will
172 * use this static table.
174 sa_attr_reg_t sa_legacy_attrs[] = {
175 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
176 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
177 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
178 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
179 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
180 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
181 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
182 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
183 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
184 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
185 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
186 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
187 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
188 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
189 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
190 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
194 * This is only used for objects of type DMU_OT_ZNODE
196 sa_attr_type_t sa_legacy_zpl_layout[] = {
197 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
201 * Special dummy layout used for buffers with no attributes.
203 sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
205 static int sa_legacy_attr_count = ARRAY_SIZE(sa_legacy_attrs);
206 static kmem_cache_t *sa_cache = NULL;
210 sa_cache_constructor(void *buf, void *unused, int kmflag)
212 sa_handle_t *hdl = buf;
214 mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
220 sa_cache_destructor(void *buf, void *unused)
222 sa_handle_t *hdl = buf;
223 mutex_destroy(&hdl->sa_lock);
229 sa_cache = kmem_cache_create("sa_cache",
230 sizeof (sa_handle_t), 0, sa_cache_constructor,
231 sa_cache_destructor, NULL, NULL, NULL, 0);
238 kmem_cache_destroy(sa_cache);
242 layout_num_compare(const void *arg1, const void *arg2)
244 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
245 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
247 return (AVL_CMP(node1->lot_num, node2->lot_num));
251 layout_hash_compare(const void *arg1, const void *arg2)
253 const sa_lot_t *node1 = (const sa_lot_t *)arg1;
254 const sa_lot_t *node2 = (const sa_lot_t *)arg2;
256 int cmp = AVL_CMP(node1->lot_hash, node2->lot_hash);
260 return (AVL_CMP(node1->lot_instance, node2->lot_instance));
264 sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
268 if (count != tbf->lot_attr_count)
271 for (i = 0; i != count; i++) {
272 if (attrs[i] != tbf->lot_attrs[i])
278 #define SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
281 sa_layout_info_hash(sa_attr_type_t *attrs, int attr_count)
284 uint64_t crc = -1ULL;
286 for (i = 0; i != attr_count; i++)
287 crc ^= SA_ATTR_HASH(attrs[i]);
293 sa_get_spill(sa_handle_t *hdl)
296 if (hdl->sa_spill == NULL) {
297 if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
298 &hdl->sa_spill)) == 0)
299 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
308 * Main attribute lookup/update function
309 * returns 0 for success or non zero for failures
311 * Operates on bulk array, first failure will abort further processing
314 sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
315 sa_data_op_t data_op, dmu_tx_t *tx)
317 sa_os_t *sa = hdl->sa_os->os_sa;
320 sa_buf_type_t buftypes;
325 for (i = 0; i != count; i++) {
326 ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
328 bulk[i].sa_addr = NULL;
329 /* First check the bonus buffer */
331 if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
332 hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
333 SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
334 SA_GET_HDR(hdl, SA_BONUS),
335 bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
336 if (tx && !(buftypes & SA_BONUS)) {
337 dmu_buf_will_dirty(hdl->sa_bonus, tx);
338 buftypes |= SA_BONUS;
341 if (bulk[i].sa_addr == NULL &&
342 ((error = sa_get_spill(hdl)) == 0)) {
343 if (TOC_ATTR_PRESENT(
344 hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
345 SA_ATTR_INFO(sa, hdl->sa_spill_tab,
346 SA_GET_HDR(hdl, SA_SPILL),
347 bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
348 if (tx && !(buftypes & SA_SPILL) &&
349 bulk[i].sa_size == bulk[i].sa_length) {
350 dmu_buf_will_dirty(hdl->sa_spill, tx);
351 buftypes |= SA_SPILL;
355 if (error && error != ENOENT) {
356 return ((error == ECKSUM) ? EIO : error);
361 if (bulk[i].sa_addr == NULL)
362 return (SET_ERROR(ENOENT));
363 if (bulk[i].sa_data) {
364 SA_COPY_DATA(bulk[i].sa_data_func,
365 bulk[i].sa_addr, bulk[i].sa_data,
371 /* existing rewrite of attr */
372 if (bulk[i].sa_addr &&
373 bulk[i].sa_size == bulk[i].sa_length) {
374 SA_COPY_DATA(bulk[i].sa_data_func,
375 bulk[i].sa_data, bulk[i].sa_addr,
378 } else if (bulk[i].sa_addr) { /* attr size change */
379 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
380 SA_REPLACE, bulk[i].sa_data_func,
381 bulk[i].sa_data, bulk[i].sa_length, tx);
382 } else { /* adding new attribute */
383 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
384 SA_ADD, bulk[i].sa_data_func,
385 bulk[i].sa_data, bulk[i].sa_length, tx);
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 values pertaining to system attribute
532 * Return the size of the sa_hdr_phys_t header for the buffer. Each
533 * variable length attribute except the first contributes two bytes to
534 * the header size, which is then rounded up to an 8-byte boundary.
536 * The following output parameters are also computed.
538 * index - The index of the first attribute in attr_desc that will
539 * spill over. Only valid if will_spill is set.
541 * total - The total number of bytes of all system attributes described
544 * will_spill - Set when spilling is necessary. It is only set when
545 * the buftype is SA_BONUS.
548 sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
549 dmu_buf_t *db, sa_buf_type_t buftype, int full_space, int *index,
550 int *total, boolean_t *will_spill)
552 int var_size_count = 0;
557 if (buftype == SA_BONUS && sa->sa_force_spill) {
560 *will_spill = B_TRUE;
566 *will_spill = B_FALSE;
569 hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
570 sizeof (sa_hdr_phys_t);
572 ASSERT(IS_P2ALIGNED(full_space, 8));
574 for (i = 0; i != attr_count; i++) {
575 boolean_t is_var_sz, might_spill_here;
578 *total = P2ROUNDUP(*total, 8);
579 *total += attr_desc[i].sa_length;
583 is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
588 * Calculate what the SA header size would be if this
589 * attribute doesn't spill.
591 tmp_hdrsize = hdrsize + ((is_var_sz && var_size_count > 1) ?
592 sizeof (uint16_t) : 0);
595 * Check whether this attribute spans into the space
596 * that would be used by the spill block pointer should
597 * a spill block be needed.
600 buftype == SA_BONUS && *index == -1 &&
601 (*total + P2ROUNDUP(tmp_hdrsize, 8)) >
602 (full_space - sizeof (blkptr_t));
604 if (is_var_sz && var_size_count > 1) {
605 if (buftype == SA_SPILL ||
606 tmp_hdrsize + *total < full_space) {
608 * Record the extra header size in case this
609 * increase needs to be reversed due to
612 hdrsize = tmp_hdrsize;
613 if (*index != -1 || might_spill_here)
614 extra_hdrsize += sizeof (uint16_t);
616 ASSERT(buftype == SA_BONUS);
619 *will_spill = B_TRUE;
625 * Store index of where spill *could* occur. Then
626 * continue to count the remaining attribute sizes. The
627 * sum is used later for sizing bonus and spill buffer.
629 if (might_spill_here)
632 if ((*total + P2ROUNDUP(hdrsize, 8)) > full_space &&
634 *will_spill = B_TRUE;
638 hdrsize -= extra_hdrsize;
640 hdrsize = P2ROUNDUP(hdrsize, 8);
644 #define BUF_SPACE_NEEDED(total, header) (total + header)
647 * Find layout that corresponds to ordering of attributes
648 * If not found a new layout number is created and added to
649 * persistent layout tables.
652 sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
655 sa_os_t *sa = hdl->sa_os->os_sa;
657 sa_buf_type_t buftype;
658 sa_hdr_phys_t *sahdr;
660 sa_attr_type_t *attrs, *attrs_start;
665 int spillhdrsize = 0;
667 dmu_object_type_t bonustype;
674 dmu_buf_will_dirty(hdl->sa_bonus, tx);
675 bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
676 dmu_object_dnsize_from_db(hdl->sa_bonus, &dnodesize);
677 bonuslen = DN_BONUS_SIZE(dnodesize);
679 /* first determine bonus header size and sum of all attributes */
680 hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
681 SA_BONUS, bonuslen, &spill_idx, &used, &spilling);
683 if (used > SPA_OLD_MAXBLOCKSIZE)
684 return (SET_ERROR(EFBIG));
686 VERIFY0(dmu_set_bonus(hdl->sa_bonus, spilling ?
687 MIN(bonuslen - sizeof (blkptr_t), used + hdrsize) :
688 used + hdrsize, tx));
690 ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
691 bonustype == DMU_OT_SA);
693 /* setup and size spill buffer when needed */
697 if (hdl->sa_spill == NULL) {
698 VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, NULL,
699 &hdl->sa_spill) == 0);
701 dmu_buf_will_dirty(hdl->sa_spill, tx);
703 spillhdrsize = sa_find_sizes(sa, &attr_desc[spill_idx],
704 attr_count - spill_idx, hdl->sa_spill, SA_SPILL,
705 hdl->sa_spill->db_size, &i, &spill_used, &dummy);
707 if (spill_used > SPA_OLD_MAXBLOCKSIZE)
708 return (SET_ERROR(EFBIG));
710 if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
711 hdl->sa_spill->db_size)
712 VERIFY(0 == sa_resize_spill(hdl,
713 BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
716 /* setup starting pointers to lay down data */
717 data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
718 sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
721 attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
725 for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
728 ASSERT(IS_P2ALIGNED(data_start, 8));
729 attrs[i] = attr_desc[i].sa_attr;
730 length = SA_REGISTERED_LEN(sa, attrs[i]);
732 length = attr_desc[i].sa_length;
734 if (spilling && i == spill_idx) { /* switch to spill buffer */
735 VERIFY(bonustype == DMU_OT_SA);
736 if (buftype == SA_BONUS && !sa->sa_force_spill) {
737 sa_find_layout(hdl->sa_os, hash, attrs_start,
738 lot_count, tx, &lot);
739 SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
746 sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
747 sahdr->sa_magic = SA_MAGIC;
748 data_start = (void *)((uintptr_t)sahdr +
750 attrs_start = &attrs[i];
753 hash ^= SA_ATTR_HASH(attrs[i]);
754 attr_desc[i].sa_addr = data_start;
755 attr_desc[i].sa_size = length;
756 SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
758 if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
759 sahdr->sa_lengths[len_idx++] = length;
761 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
766 sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
769 * Verify that old znodes always have layout number 0.
770 * Must be DMU_OT_SA for arbitrary layouts
772 VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
773 (bonustype == DMU_OT_SA && lot->lot_num > 1));
775 if (bonustype == DMU_OT_SA) {
776 SA_SET_HDR(sahdr, lot->lot_num,
777 buftype == SA_BONUS ? hdrsize : spillhdrsize);
780 kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
781 if (hdl->sa_bonus_tab) {
782 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
783 hdl->sa_bonus_tab = NULL;
785 if (!sa->sa_force_spill)
786 VERIFY(0 == sa_build_index(hdl, SA_BONUS));
788 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
791 * remove spill block that is no longer needed.
793 dmu_buf_rele(hdl->sa_spill, NULL);
794 hdl->sa_spill = NULL;
795 hdl->sa_spill_tab = NULL;
796 VERIFY(0 == dmu_rm_spill(hdl->sa_os,
797 sa_handle_object(hdl), tx));
799 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
807 sa_free_attr_table(sa_os_t *sa)
811 if (sa->sa_attr_table == NULL)
814 for (i = 0; i != sa->sa_num_attrs; i++) {
815 if (sa->sa_attr_table[i].sa_name)
816 kmem_free(sa->sa_attr_table[i].sa_name,
817 strlen(sa->sa_attr_table[i].sa_name) + 1);
820 kmem_free(sa->sa_attr_table,
821 sizeof (sa_attr_table_t) * sa->sa_num_attrs);
823 sa->sa_attr_table = NULL;
827 sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
829 sa_os_t *sa = os->os_sa;
830 uint64_t sa_attr_count = 0;
831 uint64_t sa_reg_count = 0;
837 int registered_count = 0;
839 dmu_objset_type_t ostype = dmu_objset_type(os);
842 kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
843 sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
845 if (sa->sa_reg_attr_obj != 0) {
846 error = zap_count(os, sa->sa_reg_attr_obj,
850 * Make sure we retrieved a count and that it isn't zero
852 if (error || (error == 0 && sa_attr_count == 0)) {
854 error = SET_ERROR(EINVAL);
857 sa_reg_count = sa_attr_count;
860 if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
861 sa_attr_count += sa_legacy_attr_count;
863 /* Allocate attribute numbers for attributes that aren't registered */
864 for (i = 0; i != count; i++) {
865 boolean_t found = B_FALSE;
868 if (ostype == DMU_OST_ZFS) {
869 for (j = 0; j != sa_legacy_attr_count; j++) {
870 if (strcmp(reg_attrs[i].sa_name,
871 sa_legacy_attrs[j].sa_name) == 0) {
872 sa->sa_user_table[i] =
873 sa_legacy_attrs[j].sa_attr;
881 if (sa->sa_reg_attr_obj)
882 error = zap_lookup(os, sa->sa_reg_attr_obj,
883 reg_attrs[i].sa_name, 8, 1, &attr_value);
885 error = SET_ERROR(ENOENT);
888 sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
892 sa->sa_user_table[i] = ATTR_NUM(attr_value);
899 sa->sa_num_attrs = sa_attr_count;
900 tb = sa->sa_attr_table =
901 kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
904 * Attribute table is constructed from requested attribute list,
905 * previously foreign registered attributes, and also the legacy
906 * ZPL set of attributes.
909 if (sa->sa_reg_attr_obj) {
910 for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
911 (error = zap_cursor_retrieve(&zc, &za)) == 0;
912 zap_cursor_advance(&zc)) {
914 value = za.za_first_integer;
917 tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
918 tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
919 tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
920 tb[ATTR_NUM(value)].sa_registered = B_TRUE;
922 if (tb[ATTR_NUM(value)].sa_name) {
925 tb[ATTR_NUM(value)].sa_name =
926 kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
927 (void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
928 strlen(za.za_name) +1);
930 zap_cursor_fini(&zc);
932 * Make sure we processed the correct number of registered
935 if (registered_count != sa_reg_count) {
942 if (ostype == DMU_OST_ZFS) {
943 for (i = 0; i != sa_legacy_attr_count; i++) {
946 tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
947 tb[i].sa_length = sa_legacy_attrs[i].sa_length;
948 tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
949 tb[i].sa_registered = B_FALSE;
951 kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
953 (void) strlcpy(tb[i].sa_name,
954 sa_legacy_attrs[i].sa_name,
955 strlen(sa_legacy_attrs[i].sa_name) + 1);
959 for (i = 0; i != count; i++) {
960 sa_attr_type_t attr_id;
962 attr_id = sa->sa_user_table[i];
963 if (tb[attr_id].sa_name)
966 tb[attr_id].sa_length = reg_attrs[i].sa_length;
967 tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
968 tb[attr_id].sa_attr = attr_id;
969 tb[attr_id].sa_name =
970 kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
971 (void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
972 strlen(reg_attrs[i].sa_name) + 1);
975 sa->sa_need_attr_registration =
976 (sa_attr_count != registered_count);
980 kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
981 sa->sa_user_table = NULL;
982 sa_free_attr_table(sa);
983 return ((error != 0) ? error : EINVAL);
987 sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
988 sa_attr_type_t **user_table)
993 dmu_objset_type_t ostype = dmu_objset_type(os);
997 mutex_enter(&os->os_user_ptr_lock);
999 mutex_enter(&os->os_sa->sa_lock);
1000 mutex_exit(&os->os_user_ptr_lock);
1001 tb = os->os_sa->sa_user_table;
1002 mutex_exit(&os->os_sa->sa_lock);
1007 sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
1008 mutex_init(&sa->sa_lock, NULL, MUTEX_DEFAULT, NULL);
1009 sa->sa_master_obj = sa_obj;
1012 mutex_enter(&sa->sa_lock);
1013 mutex_exit(&os->os_user_ptr_lock);
1014 avl_create(&sa->sa_layout_num_tree, layout_num_compare,
1015 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
1016 avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
1017 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
1020 error = zap_lookup(os, sa_obj, SA_LAYOUTS,
1021 8, 1, &sa->sa_layout_attr_obj);
1022 if (error != 0 && error != ENOENT)
1024 error = zap_lookup(os, sa_obj, SA_REGISTRY,
1025 8, 1, &sa->sa_reg_attr_obj);
1026 if (error != 0 && error != ENOENT)
1030 if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
1033 if (sa->sa_layout_attr_obj != 0) {
1034 uint64_t layout_count;
1036 error = zap_count(os, sa->sa_layout_attr_obj,
1040 * Layout number count should be > 0
1042 if (error || (error == 0 && layout_count == 0)) {
1044 error = SET_ERROR(EINVAL);
1048 for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
1049 (error = zap_cursor_retrieve(&zc, &za)) == 0;
1050 zap_cursor_advance(&zc)) {
1051 sa_attr_type_t *lot_attrs;
1054 lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
1055 za.za_num_integers, KM_SLEEP);
1057 if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
1058 za.za_name, 2, za.za_num_integers,
1059 lot_attrs))) != 0) {
1060 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1061 za.za_num_integers);
1064 VERIFY(ddi_strtoull(za.za_name, NULL, 10,
1065 (unsigned long long *)&lot_num) == 0);
1067 (void) sa_add_layout_entry(os, lot_attrs,
1068 za.za_num_integers, lot_num,
1069 sa_layout_info_hash(lot_attrs,
1070 za.za_num_integers), B_FALSE, NULL);
1071 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1072 za.za_num_integers);
1074 zap_cursor_fini(&zc);
1077 * Make sure layout count matches number of entries added
1080 if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
1086 /* Add special layout number for old ZNODES */
1087 if (ostype == DMU_OST_ZFS) {
1088 (void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
1089 sa_legacy_attr_count, 0,
1090 sa_layout_info_hash(sa_legacy_zpl_layout,
1091 sa_legacy_attr_count), B_FALSE, NULL);
1093 (void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
1096 *user_table = os->os_sa->sa_user_table;
1097 mutex_exit(&sa->sa_lock);
1101 sa_free_attr_table(sa);
1102 if (sa->sa_user_table)
1103 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1104 mutex_exit(&sa->sa_lock);
1105 avl_destroy(&sa->sa_layout_hash_tree);
1106 avl_destroy(&sa->sa_layout_num_tree);
1107 mutex_destroy(&sa->sa_lock);
1108 kmem_free(sa, sizeof (sa_os_t));
1109 return ((error == ECKSUM) ? EIO : error);
1113 sa_tear_down(objset_t *os)
1115 sa_os_t *sa = os->os_sa;
1119 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1121 /* Free up attr table */
1123 sa_free_attr_table(sa);
1127 avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie))) {
1129 while ((tab = list_head(&layout->lot_idx_tab))) {
1130 ASSERT(refcount_count(&tab->sa_refcount));
1131 sa_idx_tab_rele(os, tab);
1136 while ((layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie))) {
1137 kmem_free(layout->lot_attrs,
1138 sizeof (sa_attr_type_t) * layout->lot_attr_count);
1139 kmem_free(layout, sizeof (sa_lot_t));
1142 avl_destroy(&sa->sa_layout_hash_tree);
1143 avl_destroy(&sa->sa_layout_num_tree);
1144 mutex_destroy(&sa->sa_lock);
1146 kmem_free(sa, sizeof (sa_os_t));
1151 sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
1152 uint16_t length, int length_idx, boolean_t var_length, void *userp)
1154 sa_idx_tab_t *idx_tab = userp;
1157 ASSERT(idx_tab->sa_variable_lengths);
1158 idx_tab->sa_variable_lengths[length_idx] = length;
1160 TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
1161 (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
1165 sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
1166 sa_iterfunc_t func, sa_lot_t *tab, void *userp)
1172 sa_os_t *sa = os->os_sa;
1174 uint16_t *length_start = NULL;
1175 uint8_t length_idx = 0;
1178 search.lot_num = SA_LAYOUT_NUM(hdr, type);
1179 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1183 if (IS_SA_BONUSTYPE(type)) {
1184 data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
1185 offsetof(sa_hdr_phys_t, sa_lengths) +
1186 (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
1187 length_start = hdr->sa_lengths;
1192 for (i = 0; i != tb->lot_attr_count; i++) {
1193 int attr_length, reg_length;
1196 reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
1198 attr_length = reg_length;
1201 attr_length = length_start[length_idx];
1202 idx_len = length_idx++;
1205 func(hdr, data_start, tb->lot_attrs[i], attr_length,
1206 idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
1208 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
1215 sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
1216 uint16_t length, int length_idx, boolean_t variable_length, void *userp)
1218 sa_handle_t *hdl = userp;
1219 sa_os_t *sa = hdl->sa_os->os_sa;
1221 sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
1225 sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
1227 sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1229 int num_lengths = 1;
1231 ASSERTV(sa_os_t *sa = hdl->sa_os->os_sa);
1233 ASSERT(MUTEX_HELD(&sa->sa_lock));
1234 if (sa_hdr_phys->sa_magic == SA_MAGIC)
1237 db = SA_GET_DB(hdl, buftype);
1239 if (buftype == SA_SPILL) {
1240 arc_release(db->db_buf, NULL);
1241 arc_buf_thaw(db->db_buf);
1244 sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
1245 sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
1248 * Determine number of variable lengths in header
1249 * The standard 8 byte header has one for free and a
1250 * 16 byte header would have 4 + 1;
1252 if (SA_HDR_SIZE(sa_hdr_phys) > 8)
1253 num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
1254 for (i = 0; i != num_lengths; i++)
1255 sa_hdr_phys->sa_lengths[i] =
1256 BSWAP_16(sa_hdr_phys->sa_lengths[i]);
1258 sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
1259 sa_byteswap_cb, NULL, hdl);
1261 if (buftype == SA_SPILL)
1262 arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
1266 sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
1268 sa_hdr_phys_t *sa_hdr_phys;
1269 dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
1270 dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
1271 sa_os_t *sa = hdl->sa_os->os_sa;
1272 sa_idx_tab_t *idx_tab;
1274 sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1276 mutex_enter(&sa->sa_lock);
1278 /* Do we need to byteswap? */
1280 /* only check if not old znode */
1281 if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
1282 sa_hdr_phys->sa_magic != 0) {
1283 VERIFY(BSWAP_32(sa_hdr_phys->sa_magic) == SA_MAGIC);
1284 sa_byteswap(hdl, buftype);
1287 idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
1289 if (buftype == SA_BONUS)
1290 hdl->sa_bonus_tab = idx_tab;
1292 hdl->sa_spill_tab = idx_tab;
1294 mutex_exit(&sa->sa_lock);
1302 panic("evicting sa dbuf\n");
1306 sa_idx_tab_rele(objset_t *os, void *arg)
1308 sa_os_t *sa = os->os_sa;
1309 sa_idx_tab_t *idx_tab = arg;
1311 if (idx_tab == NULL)
1314 mutex_enter(&sa->sa_lock);
1315 if (refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
1316 list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
1317 if (idx_tab->sa_variable_lengths)
1318 kmem_free(idx_tab->sa_variable_lengths,
1320 idx_tab->sa_layout->lot_var_sizes);
1321 refcount_destroy(&idx_tab->sa_refcount);
1322 kmem_free(idx_tab->sa_idx_tab,
1323 sizeof (uint32_t) * sa->sa_num_attrs);
1324 kmem_free(idx_tab, sizeof (sa_idx_tab_t));
1326 mutex_exit(&sa->sa_lock);
1330 sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
1332 ASSERTV(sa_os_t *sa = os->os_sa);
1334 ASSERT(MUTEX_HELD(&sa->sa_lock));
1335 (void) refcount_add(&idx_tab->sa_refcount, NULL);
1339 sa_spill_rele(sa_handle_t *hdl)
1341 mutex_enter(&hdl->sa_lock);
1342 if (hdl->sa_spill) {
1343 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1344 dmu_buf_rele(hdl->sa_spill, NULL);
1345 hdl->sa_spill = NULL;
1346 hdl->sa_spill_tab = NULL;
1348 mutex_exit(&hdl->sa_lock);
1352 sa_handle_destroy(sa_handle_t *hdl)
1354 dmu_buf_t *db = hdl->sa_bonus;
1356 mutex_enter(&hdl->sa_lock);
1357 (void) dmu_buf_remove_user(db, &hdl->sa_dbu);
1359 if (hdl->sa_bonus_tab)
1360 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
1362 if (hdl->sa_spill_tab)
1363 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1365 dmu_buf_rele(hdl->sa_bonus, NULL);
1368 dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
1369 mutex_exit(&hdl->sa_lock);
1371 kmem_cache_free(sa_cache, hdl);
1375 sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
1376 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1379 sa_handle_t *handle = NULL;
1381 dmu_object_info_t doi;
1383 dmu_object_info_from_db(db, &doi);
1384 ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
1385 doi.doi_bonus_type == DMU_OT_ZNODE);
1387 /* find handle, if it exists */
1388 /* if one doesn't exist then create a new one, and initialize it */
1390 if (hdl_type == SA_HDL_SHARED)
1391 handle = dmu_buf_get_user(db);
1393 if (handle == NULL) {
1394 sa_handle_t *winner = NULL;
1396 handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
1397 handle->sa_dbu.dbu_evict_func = NULL;
1398 handle->sa_userp = userp;
1399 handle->sa_bonus = db;
1401 handle->sa_spill = NULL;
1402 handle->sa_bonus_tab = NULL;
1403 handle->sa_spill_tab = NULL;
1405 error = sa_build_index(handle, SA_BONUS);
1407 if (hdl_type == SA_HDL_SHARED) {
1408 dmu_buf_init_user(&handle->sa_dbu, sa_evict, NULL);
1409 winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1412 if (winner != NULL) {
1413 kmem_cache_free(sa_cache, handle);
1423 sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
1424 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1429 if ((error = dmu_bonus_hold(objset, objid, NULL, &db)))
1432 return (sa_handle_get_from_db(objset, db, userp, hdl_type,
1437 sa_buf_hold(objset_t *objset, uint64_t obj_num, void *tag, dmu_buf_t **db)
1439 return (dmu_bonus_hold(objset, obj_num, tag, db));
1443 sa_buf_rele(dmu_buf_t *db, void *tag)
1445 dmu_buf_rele(db, tag);
1449 sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
1452 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1453 return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
1457 sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1460 sa_bulk_attr_t bulk;
1462 VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
1464 bulk.sa_attr = attr;
1466 bulk.sa_length = buflen;
1467 bulk.sa_data_func = NULL;
1470 mutex_enter(&hdl->sa_lock);
1471 error = sa_lookup_impl(hdl, &bulk, 1);
1472 mutex_exit(&hdl->sa_lock);
1478 sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, uio_t *uio)
1481 sa_bulk_attr_t bulk;
1483 bulk.sa_data = NULL;
1484 bulk.sa_attr = attr;
1485 bulk.sa_data_func = NULL;
1489 mutex_enter(&hdl->sa_lock);
1490 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
1491 error = uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
1492 uio->uio_resid), UIO_READ, uio);
1494 mutex_exit(&hdl->sa_lock);
1500 sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, void *data)
1502 sa_idx_tab_t *idx_tab;
1503 sa_hdr_phys_t *hdr = (sa_hdr_phys_t *)data;
1504 sa_os_t *sa = os->os_sa;
1505 sa_lot_t *tb, search;
1509 * Deterimine layout number. If SA node and header == 0 then
1510 * force the index table to the dummy "1" empty layout.
1512 * The layout number would only be zero for a newly created file
1513 * that has not added any attributes yet, or with crypto enabled which
1514 * doesn't write any attributes to the bonus buffer.
1517 search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
1519 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1521 /* Verify header size is consistent with layout information */
1523 ASSERT((IS_SA_BONUSTYPE(bonustype) &&
1524 SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb)) || !IS_SA_BONUSTYPE(bonustype) ||
1525 (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
1528 * See if any of the already existing TOC entries can be reused?
1531 for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
1532 idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
1533 boolean_t valid_idx = B_TRUE;
1536 if (tb->lot_var_sizes != 0 &&
1537 idx_tab->sa_variable_lengths != NULL) {
1538 for (i = 0; i != tb->lot_var_sizes; i++) {
1539 if (hdr->sa_lengths[i] !=
1540 idx_tab->sa_variable_lengths[i]) {
1541 valid_idx = B_FALSE;
1547 sa_idx_tab_hold(os, idx_tab);
1552 /* No such luck, create a new entry */
1553 idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
1554 idx_tab->sa_idx_tab =
1555 kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
1556 idx_tab->sa_layout = tb;
1557 refcount_create(&idx_tab->sa_refcount);
1558 if (tb->lot_var_sizes)
1559 idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
1560 tb->lot_var_sizes, KM_SLEEP);
1562 sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
1564 sa_idx_tab_hold(os, idx_tab); /* one hold for consumer */
1565 sa_idx_tab_hold(os, idx_tab); /* one for layout */
1566 list_insert_tail(&tb->lot_idx_tab, idx_tab);
1571 sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
1572 boolean_t start, void *userdata)
1576 *dataptr = userdata;
1581 sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
1583 uint64_t attr_value = 0;
1584 sa_os_t *sa = hdl->sa_os->os_sa;
1585 sa_attr_table_t *tb = sa->sa_attr_table;
1588 mutex_enter(&sa->sa_lock);
1590 if (!sa->sa_need_attr_registration || sa->sa_master_obj == 0) {
1591 mutex_exit(&sa->sa_lock);
1595 if (sa->sa_reg_attr_obj == 0) {
1596 sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1597 DMU_OT_SA_ATTR_REGISTRATION,
1598 sa->sa_master_obj, SA_REGISTRY, tx);
1600 for (i = 0; i != sa->sa_num_attrs; i++) {
1601 if (sa->sa_attr_table[i].sa_registered)
1603 ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
1605 VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
1606 tb[i].sa_name, 8, 1, &attr_value, tx));
1607 tb[i].sa_registered = B_TRUE;
1609 sa->sa_need_attr_registration = B_FALSE;
1610 mutex_exit(&sa->sa_lock);
1614 * Replace all attributes with attributes specified in template.
1615 * If dnode had a spill buffer then those attributes will be
1616 * also be replaced, possibly with just an empty spill block
1618 * This interface is intended to only be used for bulk adding of
1619 * attributes for a new file. It will also be used by the ZPL
1620 * when converting and old formatted znode to native SA support.
1623 sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1624 int attr_count, dmu_tx_t *tx)
1626 sa_os_t *sa = hdl->sa_os->os_sa;
1628 if (sa->sa_need_attr_registration)
1629 sa_attr_register_sync(hdl, tx);
1630 return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
1634 sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1635 int attr_count, dmu_tx_t *tx)
1639 mutex_enter(&hdl->sa_lock);
1640 error = sa_replace_all_by_template_locked(hdl, attr_desc,
1642 mutex_exit(&hdl->sa_lock);
1647 * Add/remove a single attribute or replace a variable-sized attribute value
1648 * with a value of a different size, and then rewrite the entire set
1650 * Same-length attribute value replacement (including fixed-length attributes)
1651 * is handled more efficiently by the upper layers.
1654 sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1655 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1656 uint16_t buflen, dmu_tx_t *tx)
1658 sa_os_t *sa = hdl->sa_os->os_sa;
1659 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1661 sa_bulk_attr_t *attr_desc;
1663 int bonus_attr_count = 0;
1664 int bonus_data_size = 0;
1665 int spill_data_size = 0;
1666 int spill_attr_count = 0;
1668 uint16_t length, reg_length;
1669 int i, j, k, length_idx;
1671 sa_idx_tab_t *idx_tab;
1675 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1677 /* First make of copy of the old data */
1681 if (dn->dn_bonuslen != 0) {
1682 bonus_data_size = hdl->sa_bonus->db_size;
1683 old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
1684 bcopy(hdl->sa_bonus->db_data, old_data[0],
1685 hdl->sa_bonus->db_size);
1686 bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
1692 /* Bring spill buffer online if it isn't currently */
1694 if ((error = sa_get_spill(hdl)) == 0) {
1695 spill_data_size = hdl->sa_spill->db_size;
1696 old_data[1] = vmem_alloc(spill_data_size, KM_SLEEP);
1697 bcopy(hdl->sa_spill->db_data, old_data[1],
1698 hdl->sa_spill->db_size);
1700 hdl->sa_spill_tab->sa_layout->lot_attr_count;
1701 } else if (error && error != ENOENT) {
1703 kmem_free(old_data[0], bonus_data_size);
1709 /* build descriptor of all attributes */
1711 attr_count = bonus_attr_count + spill_attr_count;
1712 if (action == SA_ADD)
1714 else if (action == SA_REMOVE)
1717 attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
1720 * loop through bonus and spill buffer if it exists, and
1721 * build up new attr_descriptor to reset the attributes
1724 count = bonus_attr_count;
1725 hdr = SA_GET_HDR(hdl, SA_BONUS);
1726 idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
1727 for (; k != 2; k++) {
1729 * Iterate over each attribute in layout. Fetch the
1730 * size of variable-length attributes needing rewrite
1731 * from sa_lengths[].
1733 for (i = 0, length_idx = 0; i != count; i++) {
1734 sa_attr_type_t attr;
1736 attr = idx_tab->sa_layout->lot_attrs[i];
1737 reg_length = SA_REGISTERED_LEN(sa, attr);
1738 if (reg_length == 0) {
1739 length = hdr->sa_lengths[length_idx];
1742 length = reg_length;
1744 if (attr == newattr) {
1746 * There is nothing to do for SA_REMOVE,
1747 * so it is just skipped.
1749 if (action == SA_REMOVE)
1753 * Duplicate attributes are not allowed, so the
1754 * action can not be SA_ADD here.
1756 ASSERT3S(action, ==, SA_REPLACE);
1759 * Only a variable-sized attribute can be
1760 * replaced here, and its size must be changing.
1762 ASSERT3U(reg_length, ==, 0);
1763 ASSERT3U(length, !=, buflen);
1764 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1765 locator, datastart, buflen);
1767 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1769 (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
1770 (uintptr_t)old_data[k]), length);
1773 if (k == 0 && hdl->sa_spill) {
1774 hdr = SA_GET_HDR(hdl, SA_SPILL);
1775 idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
1776 count = spill_attr_count;
1781 if (action == SA_ADD) {
1782 reg_length = SA_REGISTERED_LEN(sa, newattr);
1783 IMPLY(reg_length != 0, reg_length == buflen);
1784 SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
1787 ASSERT3U(j, ==, attr_count);
1789 error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
1792 kmem_free(old_data[0], bonus_data_size);
1794 vmem_free(old_data[1], spill_data_size);
1795 kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
1801 sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
1805 sa_os_t *sa = hdl->sa_os->os_sa;
1806 dmu_object_type_t bonustype;
1807 dmu_buf_t *saved_spill;
1810 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1812 bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
1813 saved_spill = hdl->sa_spill;
1815 /* sync out registration table if necessary */
1816 if (sa->sa_need_attr_registration)
1817 sa_attr_register_sync(hdl, tx);
1819 error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
1820 if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
1821 sa->sa_update_cb(hdl, tx);
1824 * If saved_spill is NULL and current sa_spill is not NULL that
1825 * means we increased the refcount of the spill buffer through
1826 * sa_get_spill() or dmu_spill_hold_by_dnode(). Therefore we
1827 * must release the hold before calling dmu_tx_commit() to avoid
1828 * making a copy of this buffer in dbuf_sync_leaf() due to the
1829 * reference count now being greater than 1.
1831 if (!saved_spill && hdl->sa_spill) {
1832 if (hdl->sa_spill_tab) {
1833 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1834 hdl->sa_spill_tab = NULL;
1837 dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
1838 hdl->sa_spill = NULL;
1845 * update or add new attribute
1848 sa_update(sa_handle_t *hdl, sa_attr_type_t type,
1849 void *buf, uint32_t buflen, dmu_tx_t *tx)
1852 sa_bulk_attr_t bulk;
1854 VERIFY3U(buflen, <=, SA_ATTR_MAX_LEN);
1856 bulk.sa_attr = type;
1857 bulk.sa_data_func = NULL;
1858 bulk.sa_length = buflen;
1861 mutex_enter(&hdl->sa_lock);
1862 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
1863 mutex_exit(&hdl->sa_lock);
1868 * Return size of an attribute
1872 sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
1874 sa_bulk_attr_t bulk;
1877 bulk.sa_data = NULL;
1878 bulk.sa_attr = attr;
1879 bulk.sa_data_func = NULL;
1882 mutex_enter(&hdl->sa_lock);
1883 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
1884 mutex_exit(&hdl->sa_lock);
1887 *size = bulk.sa_size;
1889 mutex_exit(&hdl->sa_lock);
1894 sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1897 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1898 return (sa_lookup_impl(hdl, attrs, count));
1902 sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1907 mutex_enter(&hdl->sa_lock);
1908 error = sa_bulk_lookup_locked(hdl, attrs, count);
1909 mutex_exit(&hdl->sa_lock);
1914 sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
1919 mutex_enter(&hdl->sa_lock);
1920 error = sa_bulk_update_impl(hdl, attrs, count, tx);
1921 mutex_exit(&hdl->sa_lock);
1926 sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
1930 mutex_enter(&hdl->sa_lock);
1931 error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
1933 mutex_exit(&hdl->sa_lock);
1938 sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
1940 dmu_object_info_from_db((dmu_buf_t *)hdl->sa_bonus, doi);
1944 sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
1946 dmu_object_size_from_db((dmu_buf_t *)hdl->sa_bonus,
1951 sa_set_userp(sa_handle_t *hdl, void *ptr)
1953 hdl->sa_userp = ptr;
1957 sa_get_db(sa_handle_t *hdl)
1959 return ((dmu_buf_t *)hdl->sa_bonus);
1963 sa_get_userdata(sa_handle_t *hdl)
1965 return (hdl->sa_userp);
1969 sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
1971 ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
1972 os->os_sa->sa_update_cb = func;
1976 sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
1979 mutex_enter(&os->os_sa->sa_lock);
1980 sa_register_update_callback_locked(os, func);
1981 mutex_exit(&os->os_sa->sa_lock);
1985 sa_handle_object(sa_handle_t *hdl)
1987 return (hdl->sa_bonus->db_object);
1991 sa_enabled(objset_t *os)
1993 return (os->os_sa == NULL);
1997 sa_set_sa_object(objset_t *os, uint64_t sa_object)
1999 sa_os_t *sa = os->os_sa;
2001 if (sa->sa_master_obj)
2004 sa->sa_master_obj = sa_object;
2010 sa_hdrsize(void *arg)
2012 sa_hdr_phys_t *hdr = arg;
2014 return (SA_HDR_SIZE(hdr));
2018 sa_handle_lock(sa_handle_t *hdl)
2021 mutex_enter(&hdl->sa_lock);
2025 sa_handle_unlock(sa_handle_t *hdl)
2028 mutex_exit(&hdl->sa_lock);
2032 EXPORT_SYMBOL(sa_handle_get);
2033 EXPORT_SYMBOL(sa_handle_get_from_db);
2034 EXPORT_SYMBOL(sa_handle_destroy);
2035 EXPORT_SYMBOL(sa_buf_hold);
2036 EXPORT_SYMBOL(sa_buf_rele);
2037 EXPORT_SYMBOL(sa_spill_rele);
2038 EXPORT_SYMBOL(sa_lookup);
2039 EXPORT_SYMBOL(sa_update);
2040 EXPORT_SYMBOL(sa_remove);
2041 EXPORT_SYMBOL(sa_bulk_lookup);
2042 EXPORT_SYMBOL(sa_bulk_lookup_locked);
2043 EXPORT_SYMBOL(sa_bulk_update);
2044 EXPORT_SYMBOL(sa_size);
2045 EXPORT_SYMBOL(sa_object_info);
2046 EXPORT_SYMBOL(sa_object_size);
2047 EXPORT_SYMBOL(sa_get_userdata);
2048 EXPORT_SYMBOL(sa_set_userp);
2049 EXPORT_SYMBOL(sa_get_db);
2050 EXPORT_SYMBOL(sa_handle_object);
2051 EXPORT_SYMBOL(sa_register_update_callback);
2052 EXPORT_SYMBOL(sa_setup);
2053 EXPORT_SYMBOL(sa_replace_all_by_template);
2054 EXPORT_SYMBOL(sa_replace_all_by_template_locked);
2055 EXPORT_SYMBOL(sa_enabled);
2056 EXPORT_SYMBOL(sa_cache_init);
2057 EXPORT_SYMBOL(sa_cache_fini);
2058 EXPORT_SYMBOL(sa_set_sa_object);
2059 EXPORT_SYMBOL(sa_hdrsize);
2060 EXPORT_SYMBOL(sa_handle_lock);
2061 EXPORT_SYMBOL(sa_handle_unlock);
2062 EXPORT_SYMBOL(sa_lookup_uio);
2063 #endif /* _KERNEL */