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