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