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