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