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