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