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