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