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