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