1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * Copyright (c) 2013 Red Hat, Inc. 5 * All Rights Reserved. 6 */ 7 #include "xfs.h" 8 #include "xfs_fs.h" 9 #include "xfs_shared.h" 10 #include "xfs_format.h" 11 #include "xfs_log_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_sb.h" 14 #include "xfs_mount.h" 15 #include "xfs_da_format.h" 16 #include "xfs_da_btree.h" 17 #include "xfs_inode.h" 18 #include "xfs_trans.h" 19 #include "xfs_bmap_btree.h" 20 #include "xfs_bmap.h" 21 #include "xfs_attr_sf.h" 22 #include "xfs_attr.h" 23 #include "xfs_attr_remote.h" 24 #include "xfs_attr_leaf.h" 25 #include "xfs_error.h" 26 #include "xfs_trace.h" 27 #include "xfs_buf_item.h" 28 #include "xfs_dir2.h" 29 #include "xfs_log.h" 30 #include "xfs_ag.h" 31 #include "xfs_errortag.h" 32 #include "xfs_health.h" 33 34 35 /* 36 * xfs_attr_leaf.c 37 * 38 * Routines to implement leaf blocks of attributes as Btrees of hashed names. 39 */ 40 41 /*======================================================================== 42 * Function prototypes for the kernel. 43 *========================================================================*/ 44 45 /* 46 * Routines used for growing the Btree. 47 */ 48 STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args, 49 xfs_dablk_t which_block, struct xfs_buf **bpp); 50 STATIC void xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer, 51 struct xfs_attr3_icleaf_hdr *ichdr, 52 struct xfs_da_args *args, int freemap_index); 53 STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args, 54 struct xfs_attr3_icleaf_hdr *ichdr, 55 struct xfs_buf *leaf_buffer); 56 STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state, 57 xfs_da_state_blk_t *blk1, 58 xfs_da_state_blk_t *blk2); 59 STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state, 60 xfs_da_state_blk_t *leaf_blk_1, 61 struct xfs_attr3_icleaf_hdr *ichdr1, 62 xfs_da_state_blk_t *leaf_blk_2, 63 struct xfs_attr3_icleaf_hdr *ichdr2, 64 int *number_entries_in_blk1, 65 int *number_usedbytes_in_blk1); 66 67 /* 68 * Utility routines. 69 */ 70 STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, 71 struct xfs_attr_leafblock *src_leaf, 72 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start, 73 struct xfs_attr_leafblock *dst_leaf, 74 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start, 75 int move_count); 76 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index); 77 78 /* 79 * attr3 block 'firstused' conversion helpers. 80 * 81 * firstused refers to the offset of the first used byte of the nameval region 82 * of an attr leaf block. The region starts at the tail of the block and expands 83 * backwards towards the middle. As such, firstused is initialized to the block 84 * size for an empty leaf block and is reduced from there. 85 * 86 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k. 87 * The in-core firstused field is 32-bit and thus supports the maximum fsb size. 88 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this 89 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent 90 * the attr block size. The following helpers manage the conversion between the 91 * in-core and on-disk formats. 92 */ 93 94 static void 95 xfs_attr3_leaf_firstused_from_disk( 96 struct xfs_da_geometry *geo, 97 struct xfs_attr3_icleaf_hdr *to, 98 struct xfs_attr_leafblock *from) 99 { 100 struct xfs_attr3_leaf_hdr *hdr3; 101 102 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { 103 hdr3 = (struct xfs_attr3_leaf_hdr *) from; 104 to->firstused = be16_to_cpu(hdr3->firstused); 105 } else { 106 to->firstused = be16_to_cpu(from->hdr.firstused); 107 } 108 109 /* 110 * Convert from the magic fsb size value to actual blocksize. This 111 * should only occur for empty blocks when the block size overflows 112 * 16-bits. 113 */ 114 if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) { 115 ASSERT(!to->count && !to->usedbytes); 116 ASSERT(geo->blksize > USHRT_MAX); 117 to->firstused = geo->blksize; 118 } 119 } 120 121 static void 122 xfs_attr3_leaf_firstused_to_disk( 123 struct xfs_da_geometry *geo, 124 struct xfs_attr_leafblock *to, 125 struct xfs_attr3_icleaf_hdr *from) 126 { 127 struct xfs_attr3_leaf_hdr *hdr3; 128 uint32_t firstused; 129 130 /* magic value should only be seen on disk */ 131 ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF); 132 133 /* 134 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk 135 * value. This only overflows at the max supported value of 64k. Use the 136 * magic on-disk value to represent block size in this case. 137 */ 138 firstused = from->firstused; 139 if (firstused > USHRT_MAX) { 140 ASSERT(from->firstused == geo->blksize); 141 firstused = XFS_ATTR3_LEAF_NULLOFF; 142 } 143 144 if (from->magic == XFS_ATTR3_LEAF_MAGIC) { 145 hdr3 = (struct xfs_attr3_leaf_hdr *) to; 146 hdr3->firstused = cpu_to_be16(firstused); 147 } else { 148 to->hdr.firstused = cpu_to_be16(firstused); 149 } 150 } 151 152 void 153 xfs_attr3_leaf_hdr_from_disk( 154 struct xfs_da_geometry *geo, 155 struct xfs_attr3_icleaf_hdr *to, 156 struct xfs_attr_leafblock *from) 157 { 158 int i; 159 160 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || 161 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); 162 163 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { 164 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from; 165 166 to->forw = be32_to_cpu(hdr3->info.hdr.forw); 167 to->back = be32_to_cpu(hdr3->info.hdr.back); 168 to->magic = be16_to_cpu(hdr3->info.hdr.magic); 169 to->count = be16_to_cpu(hdr3->count); 170 to->usedbytes = be16_to_cpu(hdr3->usedbytes); 171 xfs_attr3_leaf_firstused_from_disk(geo, to, from); 172 to->holes = hdr3->holes; 173 174 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 175 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base); 176 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size); 177 } 178 return; 179 } 180 to->forw = be32_to_cpu(from->hdr.info.forw); 181 to->back = be32_to_cpu(from->hdr.info.back); 182 to->magic = be16_to_cpu(from->hdr.info.magic); 183 to->count = be16_to_cpu(from->hdr.count); 184 to->usedbytes = be16_to_cpu(from->hdr.usedbytes); 185 xfs_attr3_leaf_firstused_from_disk(geo, to, from); 186 to->holes = from->hdr.holes; 187 188 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 189 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base); 190 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size); 191 } 192 } 193 194 void 195 xfs_attr3_leaf_hdr_to_disk( 196 struct xfs_da_geometry *geo, 197 struct xfs_attr_leafblock *to, 198 struct xfs_attr3_icleaf_hdr *from) 199 { 200 int i; 201 202 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC || 203 from->magic == XFS_ATTR3_LEAF_MAGIC); 204 205 if (from->magic == XFS_ATTR3_LEAF_MAGIC) { 206 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to; 207 208 hdr3->info.hdr.forw = cpu_to_be32(from->forw); 209 hdr3->info.hdr.back = cpu_to_be32(from->back); 210 hdr3->info.hdr.magic = cpu_to_be16(from->magic); 211 hdr3->count = cpu_to_be16(from->count); 212 hdr3->usedbytes = cpu_to_be16(from->usedbytes); 213 xfs_attr3_leaf_firstused_to_disk(geo, to, from); 214 hdr3->holes = from->holes; 215 hdr3->pad1 = 0; 216 217 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 218 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base); 219 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size); 220 } 221 return; 222 } 223 to->hdr.info.forw = cpu_to_be32(from->forw); 224 to->hdr.info.back = cpu_to_be32(from->back); 225 to->hdr.info.magic = cpu_to_be16(from->magic); 226 to->hdr.count = cpu_to_be16(from->count); 227 to->hdr.usedbytes = cpu_to_be16(from->usedbytes); 228 xfs_attr3_leaf_firstused_to_disk(geo, to, from); 229 to->hdr.holes = from->holes; 230 to->hdr.pad1 = 0; 231 232 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 233 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base); 234 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size); 235 } 236 } 237 238 static xfs_failaddr_t 239 xfs_attr3_leaf_verify_entry( 240 struct xfs_mount *mp, 241 char *buf_end, 242 struct xfs_attr_leafblock *leaf, 243 struct xfs_attr3_icleaf_hdr *leafhdr, 244 struct xfs_attr_leaf_entry *ent, 245 int idx, 246 __u32 *last_hashval) 247 { 248 struct xfs_attr_leaf_name_local *lentry; 249 struct xfs_attr_leaf_name_remote *rentry; 250 char *name_end; 251 unsigned int nameidx; 252 unsigned int namesize; 253 __u32 hashval; 254 255 /* hash order check */ 256 hashval = be32_to_cpu(ent->hashval); 257 if (hashval < *last_hashval) 258 return __this_address; 259 *last_hashval = hashval; 260 261 nameidx = be16_to_cpu(ent->nameidx); 262 if (nameidx < leafhdr->firstused || nameidx >= mp->m_attr_geo->blksize) 263 return __this_address; 264 265 /* 266 * Check the name information. The namelen fields are u8 so we can't 267 * possibly exceed the maximum name length of 255 bytes. 268 */ 269 if (ent->flags & XFS_ATTR_LOCAL) { 270 lentry = xfs_attr3_leaf_name_local(leaf, idx); 271 namesize = xfs_attr_leaf_entsize_local(lentry->namelen, 272 be16_to_cpu(lentry->valuelen)); 273 name_end = (char *)lentry + namesize; 274 if (lentry->namelen == 0) 275 return __this_address; 276 } else { 277 rentry = xfs_attr3_leaf_name_remote(leaf, idx); 278 namesize = xfs_attr_leaf_entsize_remote(rentry->namelen); 279 name_end = (char *)rentry + namesize; 280 if (rentry->namelen == 0) 281 return __this_address; 282 if (!(ent->flags & XFS_ATTR_INCOMPLETE) && 283 rentry->valueblk == 0) 284 return __this_address; 285 } 286 287 if (name_end > buf_end) 288 return __this_address; 289 290 return NULL; 291 } 292 293 /* 294 * Validate an attribute leaf block. 295 * 296 * Empty leaf blocks can occur under the following circumstances: 297 * 298 * 1. setxattr adds a new extended attribute to a file; 299 * 2. The file has zero existing attributes; 300 * 3. The attribute is too large to fit in the attribute fork; 301 * 4. The attribute is small enough to fit in a leaf block; 302 * 5. A log flush occurs after committing the transaction that creates 303 * the (empty) leaf block; and 304 * 6. The filesystem goes down after the log flush but before the new 305 * attribute can be committed to the leaf block. 306 * 307 * Hence we need to ensure that we don't fail the validation purely 308 * because the leaf is empty. 309 */ 310 static xfs_failaddr_t 311 xfs_attr3_leaf_verify( 312 struct xfs_buf *bp) 313 { 314 struct xfs_attr3_icleaf_hdr ichdr; 315 struct xfs_mount *mp = bp->b_mount; 316 struct xfs_attr_leafblock *leaf = bp->b_addr; 317 struct xfs_attr_leaf_entry *entries; 318 struct xfs_attr_leaf_entry *ent; 319 char *buf_end; 320 uint32_t end; /* must be 32bit - see below */ 321 __u32 last_hashval = 0; 322 int i; 323 xfs_failaddr_t fa; 324 325 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf); 326 327 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr); 328 if (fa) 329 return fa; 330 331 /* 332 * firstused is the block offset of the first name info structure. 333 * Make sure it doesn't go off the block or crash into the header. 334 */ 335 if (ichdr.firstused > mp->m_attr_geo->blksize) 336 return __this_address; 337 if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf)) 338 return __this_address; 339 340 /* Make sure the entries array doesn't crash into the name info. */ 341 entries = xfs_attr3_leaf_entryp(bp->b_addr); 342 if ((char *)&entries[ichdr.count] > 343 (char *)bp->b_addr + ichdr.firstused) 344 return __this_address; 345 346 /* 347 * NOTE: This verifier historically failed empty leaf buffers because 348 * we expect the fork to be in another format. Empty attr fork format 349 * conversions are possible during xattr set, however, and format 350 * conversion is not atomic with the xattr set that triggers it. We 351 * cannot assume leaf blocks are non-empty until that is addressed. 352 */ 353 buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize; 354 for (i = 0, ent = entries; i < ichdr.count; ent++, i++) { 355 fa = xfs_attr3_leaf_verify_entry(mp, buf_end, leaf, &ichdr, 356 ent, i, &last_hashval); 357 if (fa) 358 return fa; 359 } 360 361 /* 362 * Quickly check the freemap information. Attribute data has to be 363 * aligned to 4-byte boundaries, and likewise for the free space. 364 * 365 * Note that for 64k block size filesystems, the freemap entries cannot 366 * overflow as they are only be16 fields. However, when checking end 367 * pointer of the freemap, we have to be careful to detect overflows and 368 * so use uint32_t for those checks. 369 */ 370 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 371 if (ichdr.freemap[i].base > mp->m_attr_geo->blksize) 372 return __this_address; 373 if (ichdr.freemap[i].base & 0x3) 374 return __this_address; 375 if (ichdr.freemap[i].size > mp->m_attr_geo->blksize) 376 return __this_address; 377 if (ichdr.freemap[i].size & 0x3) 378 return __this_address; 379 380 /* be care of 16 bit overflows here */ 381 end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size; 382 if (end < ichdr.freemap[i].base) 383 return __this_address; 384 if (end > mp->m_attr_geo->blksize) 385 return __this_address; 386 } 387 388 return NULL; 389 } 390 391 xfs_failaddr_t 392 xfs_attr3_leaf_header_check( 393 struct xfs_buf *bp, 394 xfs_ino_t owner) 395 { 396 struct xfs_mount *mp = bp->b_mount; 397 398 if (xfs_has_crc(mp)) { 399 struct xfs_attr3_leafblock *hdr3 = bp->b_addr; 400 401 if (hdr3->hdr.info.hdr.magic != 402 cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) 403 return __this_address; 404 405 if (be64_to_cpu(hdr3->hdr.info.owner) != owner) 406 return __this_address; 407 } 408 409 return NULL; 410 } 411 412 static void 413 xfs_attr3_leaf_write_verify( 414 struct xfs_buf *bp) 415 { 416 struct xfs_mount *mp = bp->b_mount; 417 struct xfs_buf_log_item *bip = bp->b_log_item; 418 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr; 419 xfs_failaddr_t fa; 420 421 fa = xfs_attr3_leaf_verify(bp); 422 if (fa) { 423 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 424 return; 425 } 426 427 if (!xfs_has_crc(mp)) 428 return; 429 430 if (bip) 431 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); 432 433 xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF); 434 } 435 436 /* 437 * leaf/node format detection on trees is sketchy, so a node read can be done on 438 * leaf level blocks when detection identifies the tree as a node format tree 439 * incorrectly. In this case, we need to swap the verifier to match the correct 440 * format of the block being read. 441 */ 442 static void 443 xfs_attr3_leaf_read_verify( 444 struct xfs_buf *bp) 445 { 446 struct xfs_mount *mp = bp->b_mount; 447 xfs_failaddr_t fa; 448 449 if (xfs_has_crc(mp) && 450 !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF)) 451 xfs_verifier_error(bp, -EFSBADCRC, __this_address); 452 else { 453 fa = xfs_attr3_leaf_verify(bp); 454 if (fa) 455 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 456 } 457 } 458 459 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = { 460 .name = "xfs_attr3_leaf", 461 .magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC), 462 cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) }, 463 .verify_read = xfs_attr3_leaf_read_verify, 464 .verify_write = xfs_attr3_leaf_write_verify, 465 .verify_struct = xfs_attr3_leaf_verify, 466 }; 467 468 int 469 xfs_attr3_leaf_read( 470 struct xfs_trans *tp, 471 struct xfs_inode *dp, 472 xfs_ino_t owner, 473 xfs_dablk_t bno, 474 struct xfs_buf **bpp) 475 { 476 xfs_failaddr_t fa; 477 int err; 478 479 err = xfs_da_read_buf(tp, dp, bno, 0, bpp, XFS_ATTR_FORK, 480 &xfs_attr3_leaf_buf_ops); 481 if (err || !(*bpp)) 482 return err; 483 484 fa = xfs_attr3_leaf_header_check(*bpp, owner); 485 if (fa) { 486 __xfs_buf_mark_corrupt(*bpp, fa); 487 xfs_trans_brelse(tp, *bpp); 488 *bpp = NULL; 489 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK); 490 return -EFSCORRUPTED; 491 } 492 493 if (tp) 494 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF); 495 return 0; 496 } 497 498 /*======================================================================== 499 * Namespace helper routines 500 *========================================================================*/ 501 502 /* 503 * If we are in log recovery, then we want the lookup to ignore the INCOMPLETE 504 * flag on disk - if there's an incomplete attr then recovery needs to tear it 505 * down. If there's no incomplete attr, then recovery needs to tear that attr 506 * down to replace it with the attr that has been logged. In this case, the 507 * INCOMPLETE flag will not be set in attr->attr_filter, but rather 508 * XFS_DA_OP_RECOVERY will be set in args->op_flags. 509 */ 510 static inline unsigned int xfs_attr_match_mask(const struct xfs_da_args *args) 511 { 512 if (args->op_flags & XFS_DA_OP_RECOVERY) 513 return XFS_ATTR_NSP_ONDISK_MASK; 514 return XFS_ATTR_NSP_ONDISK_MASK | XFS_ATTR_INCOMPLETE; 515 } 516 517 static inline bool 518 xfs_attr_parent_match( 519 const struct xfs_da_args *args, 520 const void *value, 521 unsigned int valuelen) 522 { 523 ASSERT(args->value != NULL); 524 525 /* Parent pointers do not use remote values */ 526 if (!value) 527 return false; 528 529 /* 530 * The only value we support is a parent rec. However, we'll accept 531 * any valuelen so that offline repair can delete ATTR_PARENT values 532 * that are not parent pointers. 533 */ 534 if (valuelen != args->valuelen) 535 return false; 536 537 return memcmp(args->value, value, valuelen) == 0; 538 } 539 540 static bool 541 xfs_attr_match( 542 struct xfs_da_args *args, 543 unsigned int attr_flags, 544 const unsigned char *name, 545 unsigned int namelen, 546 const void *value, 547 unsigned int valuelen) 548 { 549 unsigned int mask = xfs_attr_match_mask(args); 550 551 if (args->namelen != namelen) 552 return false; 553 if ((args->attr_filter & mask) != (attr_flags & mask)) 554 return false; 555 if (memcmp(args->name, name, namelen) != 0) 556 return false; 557 558 if (attr_flags & XFS_ATTR_PARENT) 559 return xfs_attr_parent_match(args, value, valuelen); 560 561 return true; 562 } 563 564 static int 565 xfs_attr_copy_value( 566 struct xfs_da_args *args, 567 unsigned char *value, 568 int valuelen) 569 { 570 /* 571 * Parent pointer lookups require the caller to specify the name and 572 * value, so don't copy anything. 573 */ 574 if (args->attr_filter & XFS_ATTR_PARENT) 575 return 0; 576 577 /* 578 * No copy if all we have to do is get the length 579 */ 580 if (!args->valuelen) { 581 args->valuelen = valuelen; 582 return 0; 583 } 584 585 /* 586 * No copy if the length of the existing buffer is too small 587 */ 588 if (args->valuelen < valuelen) { 589 args->valuelen = valuelen; 590 return -ERANGE; 591 } 592 593 if (!args->value) { 594 args->value = kvmalloc(valuelen, GFP_KERNEL | __GFP_NOLOCKDEP); 595 if (!args->value) 596 return -ENOMEM; 597 } 598 args->valuelen = valuelen; 599 600 /* remote block xattr requires IO for copy-in */ 601 if (args->rmtblkno) 602 return xfs_attr_rmtval_get(args); 603 604 /* 605 * This is to prevent a GCC warning because the remote xattr case 606 * doesn't have a value to pass in. In that case, we never reach here, 607 * but GCC can't work that out and so throws a "passing NULL to 608 * memcpy" warning. 609 */ 610 if (!value) 611 return -EINVAL; 612 memcpy(args->value, value, valuelen); 613 return 0; 614 } 615 616 /*======================================================================== 617 * External routines when attribute fork size < XFS_LITINO(mp). 618 *========================================================================*/ 619 620 /* 621 * Query whether the total requested number of attr fork bytes of extended 622 * attribute space will be able to fit inline. 623 * 624 * Returns zero if not, else the i_forkoff fork offset to be used in the 625 * literal area for attribute data once the new bytes have been added. 626 * 627 * i_forkoff must be 8 byte aligned, hence is stored as a >>3 value; 628 * special case for dev/uuid inodes, they have fixed size data forks. 629 */ 630 int 631 xfs_attr_shortform_bytesfit( 632 struct xfs_inode *dp, 633 int bytes) 634 { 635 struct xfs_mount *mp = dp->i_mount; 636 int64_t dsize; 637 int minforkoff; 638 int maxforkoff; 639 int offset; 640 641 /* 642 * Check if the new size could fit at all first: 643 */ 644 if (bytes > XFS_LITINO(mp)) 645 return 0; 646 647 /* rounded down */ 648 offset = (XFS_LITINO(mp) - bytes) >> 3; 649 650 if (dp->i_df.if_format == XFS_DINODE_FMT_DEV) { 651 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; 652 return (offset >= minforkoff) ? minforkoff : 0; 653 } 654 655 /* 656 * If the requested numbers of bytes is smaller or equal to the 657 * current attribute fork size we can always proceed. 658 * 659 * Note that if_bytes in the data fork might actually be larger than 660 * the current data fork size is due to delalloc extents. In that 661 * case either the extent count will go down when they are converted 662 * to real extents, or the delalloc conversion will take care of the 663 * literal area rebalancing. 664 */ 665 if (bytes <= xfs_inode_attr_fork_size(dp)) 666 return dp->i_forkoff; 667 668 /* 669 * For attr2 we can try to move the forkoff if there is space in the 670 * literal area 671 */ 672 dsize = dp->i_df.if_bytes; 673 674 switch (dp->i_df.if_format) { 675 case XFS_DINODE_FMT_EXTENTS: 676 /* 677 * If there is no attr fork and the data fork is extents, 678 * determine if creating the default attr fork will result 679 * in the extents form migrating to btree. If so, the 680 * minimum offset only needs to be the space required for 681 * the btree root. 682 */ 683 if (!dp->i_forkoff && dp->i_df.if_bytes > 684 xfs_default_attroffset(dp)) 685 dsize = xfs_bmdr_space_calc(MINDBTPTRS); 686 break; 687 case XFS_DINODE_FMT_BTREE: 688 /* 689 * If we have a data btree then keep forkoff if we have one, 690 * otherwise we are adding a new attr, so then we set 691 * minforkoff to where the btree root can finish so we have 692 * plenty of room for attrs 693 */ 694 if (dp->i_forkoff) { 695 if (offset < dp->i_forkoff) 696 return 0; 697 return dp->i_forkoff; 698 } 699 dsize = xfs_bmap_bmdr_space(dp->i_df.if_broot); 700 break; 701 } 702 703 /* 704 * A data fork btree root must have space for at least 705 * MINDBTPTRS key/ptr pairs if the data fork is small or empty. 706 */ 707 minforkoff = max_t(int64_t, dsize, xfs_bmdr_space_calc(MINDBTPTRS)); 708 minforkoff = roundup(minforkoff, 8) >> 3; 709 710 /* attr fork btree root can have at least this many key/ptr pairs */ 711 maxforkoff = XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS); 712 maxforkoff = maxforkoff >> 3; /* rounded down */ 713 714 if (offset >= maxforkoff) 715 return maxforkoff; 716 if (offset >= minforkoff) 717 return offset; 718 return 0; 719 } 720 721 /* 722 * Switch on the ATTR2 superblock bit (implies also FEATURES2) unless 723 * on-disk version bit says it is already set 724 */ 725 STATIC void 726 xfs_sbversion_add_attr2( 727 struct xfs_mount *mp, 728 struct xfs_trans *tp) 729 { 730 if (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT) 731 return; 732 733 spin_lock(&mp->m_sb_lock); 734 xfs_add_attr2(mp); 735 spin_unlock(&mp->m_sb_lock); 736 xfs_log_sb(tp); 737 } 738 739 /* 740 * Create the initial contents of a shortform attribute list. 741 */ 742 void 743 xfs_attr_shortform_create( 744 struct xfs_da_args *args) 745 { 746 struct xfs_inode *dp = args->dp; 747 struct xfs_ifork *ifp = &dp->i_af; 748 struct xfs_attr_sf_hdr *hdr; 749 750 trace_xfs_attr_sf_create(args); 751 752 ASSERT(ifp->if_bytes == 0); 753 if (ifp->if_format == XFS_DINODE_FMT_EXTENTS) 754 ifp->if_format = XFS_DINODE_FMT_LOCAL; 755 756 hdr = xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK); 757 memset(hdr, 0, sizeof(*hdr)); 758 hdr->totsize = cpu_to_be16(sizeof(*hdr)); 759 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); 760 } 761 762 /* 763 * Return the entry if the attr in args is found, or NULL if not. 764 */ 765 struct xfs_attr_sf_entry * 766 xfs_attr_sf_findname( 767 struct xfs_da_args *args) 768 { 769 struct xfs_attr_sf_hdr *sf = args->dp->i_af.if_data; 770 struct xfs_attr_sf_entry *sfe; 771 772 for (sfe = xfs_attr_sf_firstentry(sf); 773 sfe < xfs_attr_sf_endptr(sf); 774 sfe = xfs_attr_sf_nextentry(sfe)) { 775 if (xfs_attr_match(args, sfe->flags, sfe->nameval, 776 sfe->namelen, &sfe->nameval[sfe->namelen], 777 sfe->valuelen)) 778 return sfe; 779 } 780 781 return NULL; 782 } 783 784 /* 785 * Add a name/value pair to the shortform attribute list. 786 * Overflow from the inode has already been checked for. 787 */ 788 void 789 xfs_attr_shortform_add( 790 struct xfs_da_args *args, 791 int forkoff) 792 { 793 struct xfs_inode *dp = args->dp; 794 struct xfs_mount *mp = dp->i_mount; 795 struct xfs_ifork *ifp = &dp->i_af; 796 struct xfs_attr_sf_hdr *sf = ifp->if_data; 797 struct xfs_attr_sf_entry *sfe; 798 int size; 799 800 trace_xfs_attr_sf_add(args); 801 802 dp->i_forkoff = forkoff; 803 804 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); 805 ASSERT(!xfs_attr_sf_findname(args)); 806 807 size = xfs_attr_sf_entsize_byname(args->namelen, args->valuelen); 808 sf = xfs_idata_realloc(dp, size, XFS_ATTR_FORK); 809 810 sfe = xfs_attr_sf_endptr(sf); 811 sfe->namelen = args->namelen; 812 sfe->valuelen = args->valuelen; 813 sfe->flags = args->attr_filter; 814 memcpy(sfe->nameval, args->name, args->namelen); 815 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen); 816 sf->count++; 817 be16_add_cpu(&sf->totsize, size); 818 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); 819 820 xfs_sbversion_add_attr2(mp, args->trans); 821 } 822 823 /* 824 * After the last attribute is removed revert to original inode format, 825 * making all literal area available to the data fork once more. 826 */ 827 void 828 xfs_attr_fork_remove( 829 struct xfs_inode *ip, 830 struct xfs_trans *tp) 831 { 832 ASSERT(ip->i_af.if_nextents == 0); 833 834 xfs_ifork_zap_attr(ip); 835 ip->i_forkoff = 0; 836 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 837 } 838 839 /* 840 * Remove an attribute from the shortform attribute list structure. 841 */ 842 int 843 xfs_attr_sf_removename( 844 struct xfs_da_args *args) 845 { 846 struct xfs_inode *dp = args->dp; 847 struct xfs_mount *mp = dp->i_mount; 848 struct xfs_attr_sf_hdr *sf = dp->i_af.if_data; 849 struct xfs_attr_sf_entry *sfe; 850 uint16_t totsize = be16_to_cpu(sf->totsize); 851 void *next, *end; 852 int size = 0; 853 854 trace_xfs_attr_sf_remove(args); 855 856 sfe = xfs_attr_sf_findname(args); 857 if (!sfe) { 858 /* 859 * If we are recovering an operation, finding nothing to remove 860 * is not an error, it just means there was nothing to clean up. 861 */ 862 if (args->op_flags & XFS_DA_OP_RECOVERY) 863 return 0; 864 return -ENOATTR; 865 } 866 867 /* 868 * Fix up the attribute fork data, covering the hole 869 */ 870 size = xfs_attr_sf_entsize(sfe); 871 next = xfs_attr_sf_nextentry(sfe); 872 end = xfs_attr_sf_endptr(sf); 873 if (next < end) 874 memmove(sfe, next, end - next); 875 sf->count--; 876 totsize -= size; 877 sf->totsize = cpu_to_be16(totsize); 878 879 /* 880 * Fix up the start offset of the attribute fork 881 */ 882 if (totsize == sizeof(struct xfs_attr_sf_hdr) && 883 (dp->i_df.if_format != XFS_DINODE_FMT_BTREE) && 884 !(args->op_flags & (XFS_DA_OP_ADDNAME | XFS_DA_OP_REPLACE)) && 885 !xfs_has_parent(mp)) { 886 xfs_attr_fork_remove(dp, args->trans); 887 } else { 888 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); 889 dp->i_forkoff = xfs_attr_shortform_bytesfit(dp, totsize); 890 ASSERT(dp->i_forkoff); 891 ASSERT(totsize > sizeof(struct xfs_attr_sf_hdr) || 892 (args->op_flags & XFS_DA_OP_ADDNAME) || 893 dp->i_df.if_format == XFS_DINODE_FMT_BTREE || 894 xfs_has_parent(mp)); 895 xfs_trans_log_inode(args->trans, dp, 896 XFS_ILOG_CORE | XFS_ILOG_ADATA); 897 } 898 899 xfs_sbversion_add_attr2(mp, args->trans); 900 901 return 0; 902 } 903 904 /* 905 * Retrieve the attribute value and length. 906 * 907 * If args->valuelen is zero, only the length needs to be returned. Unlike a 908 * lookup, we only return an error if the attribute does not exist or we can't 909 * retrieve the value. 910 */ 911 int 912 xfs_attr_shortform_getvalue( 913 struct xfs_da_args *args) 914 { 915 struct xfs_attr_sf_entry *sfe; 916 917 ASSERT(args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL); 918 919 trace_xfs_attr_sf_lookup(args); 920 921 sfe = xfs_attr_sf_findname(args); 922 if (!sfe) 923 return -ENOATTR; 924 return xfs_attr_copy_value(args, &sfe->nameval[args->namelen], 925 sfe->valuelen); 926 } 927 928 /* Convert from using the shortform to the leaf format. */ 929 int 930 xfs_attr_shortform_to_leaf( 931 struct xfs_da_args *args) 932 { 933 struct xfs_inode *dp = args->dp; 934 struct xfs_ifork *ifp = &dp->i_af; 935 struct xfs_attr_sf_hdr *sf = ifp->if_data; 936 struct xfs_attr_sf_entry *sfe; 937 int size = be16_to_cpu(sf->totsize); 938 struct xfs_da_args nargs; 939 char *tmpbuffer; 940 int error, i; 941 xfs_dablk_t blkno; 942 struct xfs_buf *bp; 943 944 trace_xfs_attr_sf_to_leaf(args); 945 946 tmpbuffer = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL); 947 memcpy(tmpbuffer, ifp->if_data, size); 948 sf = (struct xfs_attr_sf_hdr *)tmpbuffer; 949 950 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); 951 xfs_bmap_local_to_extents_empty(args->trans, dp, XFS_ATTR_FORK); 952 953 bp = NULL; 954 error = xfs_da_grow_inode(args, &blkno); 955 if (error) 956 goto out; 957 958 ASSERT(blkno == 0); 959 error = xfs_attr3_leaf_create(args, blkno, &bp); 960 if (error) 961 goto out; 962 963 memset((char *)&nargs, 0, sizeof(nargs)); 964 nargs.dp = dp; 965 nargs.geo = args->geo; 966 nargs.total = args->total; 967 nargs.whichfork = XFS_ATTR_FORK; 968 nargs.trans = args->trans; 969 nargs.op_flags = XFS_DA_OP_OKNOENT; 970 nargs.owner = args->owner; 971 972 sfe = xfs_attr_sf_firstentry(sf); 973 for (i = 0; i < sf->count; i++) { 974 nargs.name = sfe->nameval; 975 nargs.namelen = sfe->namelen; 976 nargs.value = &sfe->nameval[nargs.namelen]; 977 nargs.valuelen = sfe->valuelen; 978 nargs.attr_filter = sfe->flags & XFS_ATTR_NSP_ONDISK_MASK; 979 if (!xfs_attr_check_namespace(sfe->flags)) { 980 xfs_da_mark_sick(args); 981 error = -EFSCORRUPTED; 982 goto out; 983 } 984 xfs_attr_sethash(&nargs); 985 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */ 986 ASSERT(error == -ENOATTR); 987 if (!xfs_attr3_leaf_add(bp, &nargs)) 988 ASSERT(0); 989 sfe = xfs_attr_sf_nextentry(sfe); 990 } 991 error = 0; 992 out: 993 kfree(tmpbuffer); 994 return error; 995 } 996 997 /* 998 * Check a leaf attribute block to see if all the entries would fit into 999 * a shortform attribute list. 1000 */ 1001 int 1002 xfs_attr_shortform_allfit( 1003 struct xfs_buf *bp, 1004 struct xfs_inode *dp) 1005 { 1006 struct xfs_attr_leafblock *leaf; 1007 struct xfs_attr_leaf_entry *entry; 1008 xfs_attr_leaf_name_local_t *name_loc; 1009 struct xfs_attr3_icleaf_hdr leafhdr; 1010 int bytes; 1011 int i; 1012 struct xfs_mount *mp = bp->b_mount; 1013 1014 leaf = bp->b_addr; 1015 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf); 1016 entry = xfs_attr3_leaf_entryp(leaf); 1017 1018 bytes = sizeof(struct xfs_attr_sf_hdr); 1019 for (i = 0; i < leafhdr.count; entry++, i++) { 1020 if (entry->flags & XFS_ATTR_INCOMPLETE) 1021 continue; /* don't copy partial entries */ 1022 if (!(entry->flags & XFS_ATTR_LOCAL)) 1023 return 0; 1024 name_loc = xfs_attr3_leaf_name_local(leaf, i); 1025 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX) 1026 return 0; 1027 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX) 1028 return 0; 1029 bytes += xfs_attr_sf_entsize_byname(name_loc->namelen, 1030 be16_to_cpu(name_loc->valuelen)); 1031 } 1032 if ((dp->i_df.if_format != XFS_DINODE_FMT_BTREE) && 1033 (bytes == sizeof(struct xfs_attr_sf_hdr))) 1034 return -1; 1035 return xfs_attr_shortform_bytesfit(dp, bytes); 1036 } 1037 1038 /* Verify the consistency of a raw inline attribute fork. */ 1039 xfs_failaddr_t 1040 xfs_attr_shortform_verify( 1041 struct xfs_attr_sf_hdr *sfp, 1042 size_t size) 1043 { 1044 struct xfs_attr_sf_entry *sfep = xfs_attr_sf_firstentry(sfp); 1045 struct xfs_attr_sf_entry *next_sfep; 1046 char *endp; 1047 int i; 1048 1049 /* 1050 * Give up if the attribute is way too short. 1051 */ 1052 if (size < sizeof(struct xfs_attr_sf_hdr)) 1053 return __this_address; 1054 1055 endp = (char *)sfp + size; 1056 1057 /* Check all reported entries */ 1058 for (i = 0; i < sfp->count; i++) { 1059 /* 1060 * struct xfs_attr_sf_entry has a variable length. 1061 * Check the fixed-offset parts of the structure are 1062 * within the data buffer. 1063 * xfs_attr_sf_entry is defined with a 1-byte variable 1064 * array at the end, so we must subtract that off. 1065 */ 1066 if (((char *)sfep + sizeof(*sfep)) >= endp) 1067 return __this_address; 1068 1069 /* Don't allow names with known bad length. */ 1070 if (sfep->namelen == 0) 1071 return __this_address; 1072 1073 /* 1074 * Check that the variable-length part of the structure is 1075 * within the data buffer. The next entry starts after the 1076 * name component, so nextentry is an acceptable test. 1077 */ 1078 next_sfep = xfs_attr_sf_nextentry(sfep); 1079 if ((char *)next_sfep > endp) 1080 return __this_address; 1081 1082 /* 1083 * Check for unknown flags. Short form doesn't support 1084 * the incomplete or local bits, so we can use the namespace 1085 * mask here. 1086 */ 1087 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK) 1088 return __this_address; 1089 1090 /* 1091 * Check for invalid namespace combinations. We only allow 1092 * one namespace flag per xattr, so we can just count the 1093 * bits (i.e. hweight) here. 1094 */ 1095 if (!xfs_attr_check_namespace(sfep->flags)) 1096 return __this_address; 1097 1098 sfep = next_sfep; 1099 } 1100 if ((void *)sfep != (void *)endp) 1101 return __this_address; 1102 1103 return NULL; 1104 } 1105 1106 /* 1107 * Convert a leaf attribute list to shortform attribute list 1108 */ 1109 int 1110 xfs_attr3_leaf_to_shortform( 1111 struct xfs_buf *bp, 1112 struct xfs_da_args *args, 1113 int forkoff) 1114 { 1115 struct xfs_attr_leafblock *leaf; 1116 struct xfs_attr3_icleaf_hdr ichdr; 1117 struct xfs_attr_leaf_entry *entry; 1118 struct xfs_attr_leaf_name_local *name_loc; 1119 struct xfs_da_args nargs; 1120 struct xfs_inode *dp = args->dp; 1121 char *tmpbuffer; 1122 int error; 1123 int i; 1124 1125 trace_xfs_attr_leaf_to_sf(args); 1126 1127 tmpbuffer = kvmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL); 1128 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); 1129 1130 leaf = (xfs_attr_leafblock_t *)tmpbuffer; 1131 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 1132 entry = xfs_attr3_leaf_entryp(leaf); 1133 1134 /* XXX (dgc): buffer is about to be marked stale - why zero it? */ 1135 memset(bp->b_addr, 0, args->geo->blksize); 1136 1137 /* 1138 * Clean out the prior contents of the attribute list. 1139 */ 1140 error = xfs_da_shrink_inode(args, 0, bp); 1141 if (error) 1142 goto out; 1143 1144 if (forkoff == -1) { 1145 /* 1146 * Don't remove the attr fork if this operation is the first 1147 * part of a attr replace operations. We're going to add a new 1148 * attr immediately, so we need to keep the attr fork around in 1149 * this case. 1150 */ 1151 if (!(args->op_flags & XFS_DA_OP_REPLACE)) { 1152 ASSERT(dp->i_df.if_format != XFS_DINODE_FMT_BTREE); 1153 xfs_attr_fork_remove(dp, args->trans); 1154 } 1155 goto out; 1156 } 1157 1158 xfs_attr_shortform_create(args); 1159 1160 /* 1161 * Copy the attributes 1162 */ 1163 memset((char *)&nargs, 0, sizeof(nargs)); 1164 nargs.geo = args->geo; 1165 nargs.dp = dp; 1166 nargs.total = args->total; 1167 nargs.whichfork = XFS_ATTR_FORK; 1168 nargs.trans = args->trans; 1169 nargs.op_flags = XFS_DA_OP_OKNOENT; 1170 nargs.owner = args->owner; 1171 1172 for (i = 0; i < ichdr.count; entry++, i++) { 1173 if (entry->flags & XFS_ATTR_INCOMPLETE) 1174 continue; /* don't copy partial entries */ 1175 if (!entry->nameidx) 1176 continue; 1177 ASSERT(entry->flags & XFS_ATTR_LOCAL); 1178 name_loc = xfs_attr3_leaf_name_local(leaf, i); 1179 nargs.name = name_loc->nameval; 1180 nargs.namelen = name_loc->namelen; 1181 nargs.value = &name_loc->nameval[nargs.namelen]; 1182 nargs.valuelen = be16_to_cpu(name_loc->valuelen); 1183 nargs.hashval = be32_to_cpu(entry->hashval); 1184 nargs.attr_filter = entry->flags & XFS_ATTR_NSP_ONDISK_MASK; 1185 xfs_attr_shortform_add(&nargs, forkoff); 1186 } 1187 error = 0; 1188 1189 out: 1190 kvfree(tmpbuffer); 1191 return error; 1192 } 1193 1194 /* 1195 * Convert from using a single leaf to a root node and a leaf. 1196 */ 1197 int 1198 xfs_attr3_leaf_to_node( 1199 struct xfs_da_args *args) 1200 { 1201 struct xfs_attr_leafblock *leaf; 1202 struct xfs_attr3_icleaf_hdr icleafhdr; 1203 struct xfs_attr_leaf_entry *entries; 1204 struct xfs_da3_icnode_hdr icnodehdr; 1205 struct xfs_da_intnode *node; 1206 struct xfs_inode *dp = args->dp; 1207 struct xfs_mount *mp = dp->i_mount; 1208 struct xfs_buf *bp1 = NULL; 1209 struct xfs_buf *bp2 = NULL; 1210 xfs_dablk_t blkno; 1211 int error; 1212 1213 trace_xfs_attr_leaf_to_node(args); 1214 1215 if (XFS_TEST_ERROR(mp, XFS_ERRTAG_ATTR_LEAF_TO_NODE)) { 1216 error = -EIO; 1217 goto out; 1218 } 1219 1220 error = xfs_da_grow_inode(args, &blkno); 1221 if (error) 1222 goto out; 1223 error = xfs_attr3_leaf_read(args->trans, dp, args->owner, 0, &bp1); 1224 if (error) 1225 goto out; 1226 1227 error = xfs_da_get_buf(args->trans, dp, blkno, &bp2, XFS_ATTR_FORK); 1228 if (error) 1229 goto out; 1230 1231 /* 1232 * Copy leaf to new buffer and log it. 1233 */ 1234 xfs_da_buf_copy(bp2, bp1, args->geo->blksize); 1235 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1); 1236 1237 /* 1238 * Set up the new root node. 1239 */ 1240 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK); 1241 if (error) 1242 goto out; 1243 node = bp1->b_addr; 1244 xfs_da3_node_hdr_from_disk(mp, &icnodehdr, node); 1245 1246 leaf = bp2->b_addr; 1247 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf); 1248 entries = xfs_attr3_leaf_entryp(leaf); 1249 1250 /* both on-disk, don't endian-flip twice */ 1251 icnodehdr.btree[0].hashval = entries[icleafhdr.count - 1].hashval; 1252 icnodehdr.btree[0].before = cpu_to_be32(blkno); 1253 icnodehdr.count = 1; 1254 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &icnodehdr); 1255 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1); 1256 error = 0; 1257 out: 1258 return error; 1259 } 1260 1261 /*======================================================================== 1262 * Routines used for growing the Btree. 1263 *========================================================================*/ 1264 1265 /* 1266 * Create the initial contents of a leaf attribute list 1267 * or a leaf in a node attribute list. 1268 */ 1269 STATIC int 1270 xfs_attr3_leaf_create( 1271 struct xfs_da_args *args, 1272 xfs_dablk_t blkno, 1273 struct xfs_buf **bpp) 1274 { 1275 struct xfs_attr_leafblock *leaf; 1276 struct xfs_attr3_icleaf_hdr ichdr; 1277 struct xfs_inode *dp = args->dp; 1278 struct xfs_mount *mp = dp->i_mount; 1279 struct xfs_buf *bp; 1280 int error; 1281 1282 trace_xfs_attr_leaf_create(args); 1283 1284 error = xfs_da_get_buf(args->trans, args->dp, blkno, &bp, 1285 XFS_ATTR_FORK); 1286 if (error) 1287 return error; 1288 bp->b_ops = &xfs_attr3_leaf_buf_ops; 1289 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF); 1290 leaf = bp->b_addr; 1291 memset(leaf, 0, args->geo->blksize); 1292 1293 memset(&ichdr, 0, sizeof(ichdr)); 1294 ichdr.firstused = args->geo->blksize; 1295 1296 if (xfs_has_crc(mp)) { 1297 struct xfs_da3_blkinfo *hdr3 = bp->b_addr; 1298 1299 ichdr.magic = XFS_ATTR3_LEAF_MAGIC; 1300 1301 hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp)); 1302 hdr3->owner = cpu_to_be64(args->owner); 1303 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); 1304 1305 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr); 1306 } else { 1307 ichdr.magic = XFS_ATTR_LEAF_MAGIC; 1308 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr); 1309 } 1310 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base; 1311 1312 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); 1313 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1); 1314 1315 *bpp = bp; 1316 return 0; 1317 } 1318 1319 /* 1320 * Split the leaf node, rebalance, then add the new entry. 1321 * 1322 * Returns 0 if the entry was added, 1 if a further split is needed or a 1323 * negative error number otherwise. 1324 */ 1325 int 1326 xfs_attr3_leaf_split( 1327 struct xfs_da_state *state, 1328 struct xfs_da_state_blk *oldblk, 1329 struct xfs_da_state_blk *newblk) 1330 { 1331 bool added; 1332 xfs_dablk_t blkno; 1333 int error; 1334 1335 trace_xfs_attr_leaf_split(state->args); 1336 1337 /* 1338 * Allocate space for a new leaf node. 1339 */ 1340 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC); 1341 error = xfs_da_grow_inode(state->args, &blkno); 1342 if (error) 1343 return error; 1344 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp); 1345 if (error) 1346 return error; 1347 newblk->blkno = blkno; 1348 newblk->magic = XFS_ATTR_LEAF_MAGIC; 1349 1350 /* 1351 * Rebalance the entries across the two leaves. 1352 * NOTE: rebalance() currently depends on the 2nd block being empty. 1353 */ 1354 xfs_attr3_leaf_rebalance(state, oldblk, newblk); 1355 error = xfs_da3_blk_link(state, oldblk, newblk); 1356 if (error) 1357 return error; 1358 1359 /* 1360 * Save info on "old" attribute for "atomic rename" ops, leaf_add() 1361 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the 1362 * "new" attrs info. Will need the "old" info to remove it later. 1363 * 1364 * Insert the "new" entry in the correct block. 1365 */ 1366 if (state->inleaf) { 1367 trace_xfs_attr_leaf_add_old(state->args); 1368 added = xfs_attr3_leaf_add(oldblk->bp, state->args); 1369 } else { 1370 trace_xfs_attr_leaf_add_new(state->args); 1371 added = xfs_attr3_leaf_add(newblk->bp, state->args); 1372 } 1373 1374 /* 1375 * Update last hashval in each block since we added the name. 1376 */ 1377 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL); 1378 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL); 1379 if (!added) 1380 return 1; 1381 return 0; 1382 } 1383 1384 /* 1385 * Add a name to the leaf attribute list structure. 1386 */ 1387 bool 1388 xfs_attr3_leaf_add( 1389 struct xfs_buf *bp, 1390 struct xfs_da_args *args) 1391 { 1392 struct xfs_attr_leafblock *leaf; 1393 struct xfs_attr3_icleaf_hdr ichdr; 1394 int tablesize; 1395 int entsize; 1396 bool added = true; 1397 int sum; 1398 int tmp; 1399 int i; 1400 1401 trace_xfs_attr_leaf_add(args); 1402 1403 leaf = bp->b_addr; 1404 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 1405 ASSERT(args->index >= 0 && args->index <= ichdr.count); 1406 entsize = xfs_attr_leaf_newentsize(args, NULL); 1407 1408 /* 1409 * Search through freemap for first-fit on new name length. 1410 * (may need to figure in size of entry struct too) 1411 */ 1412 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t) 1413 + xfs_attr3_leaf_hdr_size(leaf); 1414 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) { 1415 if (tablesize > ichdr.firstused) { 1416 sum += ichdr.freemap[i].size; 1417 continue; 1418 } 1419 if (!ichdr.freemap[i].size) 1420 continue; /* no space in this map */ 1421 tmp = entsize; 1422 if (ichdr.freemap[i].base < ichdr.firstused) 1423 tmp += sizeof(xfs_attr_leaf_entry_t); 1424 if (ichdr.freemap[i].size >= tmp) { 1425 xfs_attr3_leaf_add_work(bp, &ichdr, args, i); 1426 goto out_log_hdr; 1427 } 1428 sum += ichdr.freemap[i].size; 1429 } 1430 1431 /* 1432 * If there are no holes in the address space of the block, 1433 * and we don't have enough freespace, then compaction will do us 1434 * no good and we should just give up. 1435 */ 1436 if (!ichdr.holes && sum < entsize) 1437 return false; 1438 1439 /* 1440 * Compact the entries to coalesce free space. 1441 * This may change the hdr->count via dropping INCOMPLETE entries. 1442 */ 1443 xfs_attr3_leaf_compact(args, &ichdr, bp); 1444 1445 /* 1446 * After compaction, the block is guaranteed to have only one 1447 * free region, in freemap[0]. If it is not big enough, give up. 1448 */ 1449 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) { 1450 added = false; 1451 goto out_log_hdr; 1452 } 1453 1454 xfs_attr3_leaf_add_work(bp, &ichdr, args, 0); 1455 1456 out_log_hdr: 1457 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); 1458 xfs_trans_log_buf(args->trans, bp, 1459 XFS_DA_LOGRANGE(leaf, &leaf->hdr, 1460 xfs_attr3_leaf_hdr_size(leaf))); 1461 return added; 1462 } 1463 1464 /* 1465 * Add a name to a leaf attribute list structure. 1466 */ 1467 STATIC void 1468 xfs_attr3_leaf_add_work( 1469 struct xfs_buf *bp, 1470 struct xfs_attr3_icleaf_hdr *ichdr, 1471 struct xfs_da_args *args, 1472 int mapindex) 1473 { 1474 struct xfs_attr_leafblock *leaf; 1475 struct xfs_attr_leaf_entry *entry; 1476 struct xfs_attr_leaf_name_local *name_loc; 1477 struct xfs_attr_leaf_name_remote *name_rmt; 1478 struct xfs_mount *mp; 1479 int tmp; 1480 int i; 1481 1482 trace_xfs_attr_leaf_add_work(args); 1483 1484 leaf = bp->b_addr; 1485 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE); 1486 ASSERT(args->index >= 0 && args->index <= ichdr->count); 1487 1488 /* 1489 * Force open some space in the entry array and fill it in. 1490 */ 1491 entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; 1492 if (args->index < ichdr->count) { 1493 tmp = ichdr->count - args->index; 1494 tmp *= sizeof(xfs_attr_leaf_entry_t); 1495 memmove(entry + 1, entry, tmp); 1496 xfs_trans_log_buf(args->trans, bp, 1497 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry))); 1498 } 1499 ichdr->count++; 1500 1501 /* 1502 * Allocate space for the new string (at the end of the run). 1503 */ 1504 mp = args->trans->t_mountp; 1505 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize); 1506 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0); 1507 ASSERT(ichdr->freemap[mapindex].size >= 1508 xfs_attr_leaf_newentsize(args, NULL)); 1509 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize); 1510 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0); 1511 1512 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp); 1513 1514 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base + 1515 ichdr->freemap[mapindex].size); 1516 entry->hashval = cpu_to_be32(args->hashval); 1517 entry->flags = args->attr_filter; 1518 if (tmp) 1519 entry->flags |= XFS_ATTR_LOCAL; 1520 if (args->op_flags & XFS_DA_OP_REPLACE) { 1521 if (!(args->op_flags & XFS_DA_OP_LOGGED)) 1522 entry->flags |= XFS_ATTR_INCOMPLETE; 1523 if ((args->blkno2 == args->blkno) && 1524 (args->index2 <= args->index)) { 1525 args->index2++; 1526 } 1527 } 1528 xfs_trans_log_buf(args->trans, bp, 1529 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); 1530 ASSERT((args->index == 0) || 1531 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval))); 1532 ASSERT((args->index == ichdr->count - 1) || 1533 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval))); 1534 1535 /* 1536 * For "remote" attribute values, simply note that we need to 1537 * allocate space for the "remote" value. We can't actually 1538 * allocate the extents in this transaction, and we can't decide 1539 * which blocks they should be as we might allocate more blocks 1540 * as part of this transaction (a split operation for example). 1541 */ 1542 if (entry->flags & XFS_ATTR_LOCAL) { 1543 name_loc = xfs_attr3_leaf_name_local(leaf, args->index); 1544 name_loc->namelen = args->namelen; 1545 name_loc->valuelen = cpu_to_be16(args->valuelen); 1546 memcpy((char *)name_loc->nameval, args->name, args->namelen); 1547 memcpy((char *)&name_loc->nameval[args->namelen], args->value, 1548 be16_to_cpu(name_loc->valuelen)); 1549 } else { 1550 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); 1551 name_rmt->namelen = args->namelen; 1552 memcpy((char *)name_rmt->name, args->name, args->namelen); 1553 entry->flags |= XFS_ATTR_INCOMPLETE; 1554 /* just in case */ 1555 name_rmt->valuelen = 0; 1556 name_rmt->valueblk = 0; 1557 args->rmtblkno = 1; 1558 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen); 1559 args->rmtvaluelen = args->valuelen; 1560 } 1561 xfs_trans_log_buf(args->trans, bp, 1562 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), 1563 xfs_attr_leaf_entsize(leaf, args->index))); 1564 1565 /* 1566 * Update the control info for this leaf node 1567 */ 1568 if (be16_to_cpu(entry->nameidx) < ichdr->firstused) 1569 ichdr->firstused = be16_to_cpu(entry->nameidx); 1570 1571 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t) 1572 + xfs_attr3_leaf_hdr_size(leaf)); 1573 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t) 1574 + xfs_attr3_leaf_hdr_size(leaf); 1575 1576 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 1577 if (ichdr->freemap[i].base == tmp) { 1578 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t); 1579 ichdr->freemap[i].size -= 1580 min_t(uint16_t, ichdr->freemap[i].size, 1581 sizeof(xfs_attr_leaf_entry_t)); 1582 } 1583 } 1584 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index); 1585 } 1586 1587 /* 1588 * Garbage collect a leaf attribute list block by copying it to a new buffer. 1589 */ 1590 STATIC void 1591 xfs_attr3_leaf_compact( 1592 struct xfs_da_args *args, 1593 struct xfs_attr3_icleaf_hdr *ichdr_dst, 1594 struct xfs_buf *bp) 1595 { 1596 struct xfs_attr_leafblock *leaf_src; 1597 struct xfs_attr_leafblock *leaf_dst; 1598 struct xfs_attr3_icleaf_hdr ichdr_src; 1599 struct xfs_trans *trans = args->trans; 1600 char *tmpbuffer; 1601 1602 trace_xfs_attr_leaf_compact(args); 1603 1604 tmpbuffer = kvmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL); 1605 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); 1606 memset(bp->b_addr, 0, args->geo->blksize); 1607 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer; 1608 leaf_dst = bp->b_addr; 1609 1610 /* 1611 * Copy the on-disk header back into the destination buffer to ensure 1612 * all the information in the header that is not part of the incore 1613 * header structure is preserved. 1614 */ 1615 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src)); 1616 1617 /* Initialise the incore headers */ 1618 ichdr_src = *ichdr_dst; /* struct copy */ 1619 ichdr_dst->firstused = args->geo->blksize; 1620 ichdr_dst->usedbytes = 0; 1621 ichdr_dst->count = 0; 1622 ichdr_dst->holes = 0; 1623 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src); 1624 ichdr_dst->freemap[0].size = ichdr_dst->firstused - 1625 ichdr_dst->freemap[0].base; 1626 1627 /* write the header back to initialise the underlying buffer */ 1628 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst); 1629 1630 /* 1631 * Copy all entry's in the same (sorted) order, 1632 * but allocate name/value pairs packed and in sequence. 1633 */ 1634 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0, 1635 leaf_dst, ichdr_dst, 0, ichdr_src.count); 1636 /* 1637 * this logs the entire buffer, but the caller must write the header 1638 * back to the buffer when it is finished modifying it. 1639 */ 1640 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1); 1641 1642 kvfree(tmpbuffer); 1643 } 1644 1645 /* 1646 * Compare two leaf blocks "order". 1647 * Return 0 unless leaf2 should go before leaf1. 1648 */ 1649 static int 1650 xfs_attr3_leaf_order( 1651 struct xfs_buf *leaf1_bp, 1652 struct xfs_attr3_icleaf_hdr *leaf1hdr, 1653 struct xfs_buf *leaf2_bp, 1654 struct xfs_attr3_icleaf_hdr *leaf2hdr) 1655 { 1656 struct xfs_attr_leaf_entry *entries1; 1657 struct xfs_attr_leaf_entry *entries2; 1658 1659 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr); 1660 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr); 1661 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 && 1662 ((be32_to_cpu(entries2[0].hashval) < 1663 be32_to_cpu(entries1[0].hashval)) || 1664 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) < 1665 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) { 1666 return 1; 1667 } 1668 return 0; 1669 } 1670 1671 int 1672 xfs_attr_leaf_order( 1673 struct xfs_buf *leaf1_bp, 1674 struct xfs_buf *leaf2_bp) 1675 { 1676 struct xfs_attr3_icleaf_hdr ichdr1; 1677 struct xfs_attr3_icleaf_hdr ichdr2; 1678 struct xfs_mount *mp = leaf1_bp->b_mount; 1679 1680 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr); 1681 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr); 1682 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2); 1683 } 1684 1685 /* 1686 * Redistribute the attribute list entries between two leaf nodes, 1687 * taking into account the size of the new entry. 1688 * 1689 * NOTE: if new block is empty, then it will get the upper half of the 1690 * old block. At present, all (one) callers pass in an empty second block. 1691 * 1692 * This code adjusts the args->index/blkno and args->index2/blkno2 fields 1693 * to match what it is doing in splitting the attribute leaf block. Those 1694 * values are used in "atomic rename" operations on attributes. Note that 1695 * the "new" and "old" values can end up in different blocks. 1696 */ 1697 STATIC void 1698 xfs_attr3_leaf_rebalance( 1699 struct xfs_da_state *state, 1700 struct xfs_da_state_blk *blk1, 1701 struct xfs_da_state_blk *blk2) 1702 { 1703 struct xfs_da_args *args; 1704 struct xfs_attr_leafblock *leaf1; 1705 struct xfs_attr_leafblock *leaf2; 1706 struct xfs_attr3_icleaf_hdr ichdr1; 1707 struct xfs_attr3_icleaf_hdr ichdr2; 1708 struct xfs_attr_leaf_entry *entries1; 1709 struct xfs_attr_leaf_entry *entries2; 1710 int count; 1711 int totallen; 1712 int max; 1713 int space; 1714 int swap; 1715 1716 /* 1717 * Set up environment. 1718 */ 1719 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC); 1720 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC); 1721 leaf1 = blk1->bp->b_addr; 1722 leaf2 = blk2->bp->b_addr; 1723 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1); 1724 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2); 1725 ASSERT(ichdr2.count == 0); 1726 args = state->args; 1727 1728 trace_xfs_attr_leaf_rebalance(args); 1729 1730 /* 1731 * Check ordering of blocks, reverse if it makes things simpler. 1732 * 1733 * NOTE: Given that all (current) callers pass in an empty 1734 * second block, this code should never set "swap". 1735 */ 1736 swap = 0; 1737 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) { 1738 swap(blk1, blk2); 1739 1740 /* swap structures rather than reconverting them */ 1741 swap(ichdr1, ichdr2); 1742 1743 leaf1 = blk1->bp->b_addr; 1744 leaf2 = blk2->bp->b_addr; 1745 swap = 1; 1746 } 1747 1748 /* 1749 * Examine entries until we reduce the absolute difference in 1750 * byte usage between the two blocks to a minimum. Then get 1751 * the direction to copy and the number of elements to move. 1752 * 1753 * "inleaf" is true if the new entry should be inserted into blk1. 1754 * If "swap" is also true, then reverse the sense of "inleaf". 1755 */ 1756 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1, 1757 blk2, &ichdr2, 1758 &count, &totallen); 1759 if (swap) 1760 state->inleaf = !state->inleaf; 1761 1762 /* 1763 * Move any entries required from leaf to leaf: 1764 */ 1765 if (count < ichdr1.count) { 1766 /* 1767 * Figure the total bytes to be added to the destination leaf. 1768 */ 1769 /* number entries being moved */ 1770 count = ichdr1.count - count; 1771 space = ichdr1.usedbytes - totallen; 1772 space += count * sizeof(xfs_attr_leaf_entry_t); 1773 1774 /* 1775 * leaf2 is the destination, compact it if it looks tight. 1776 */ 1777 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1); 1778 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t); 1779 if (space > max) 1780 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp); 1781 1782 /* 1783 * Move high entries from leaf1 to low end of leaf2. 1784 */ 1785 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1, 1786 ichdr1.count - count, leaf2, &ichdr2, 0, count); 1787 1788 } else if (count > ichdr1.count) { 1789 /* 1790 * I assert that since all callers pass in an empty 1791 * second buffer, this code should never execute. 1792 */ 1793 ASSERT(0); 1794 1795 /* 1796 * Figure the total bytes to be added to the destination leaf. 1797 */ 1798 /* number entries being moved */ 1799 count -= ichdr1.count; 1800 space = totallen - ichdr1.usedbytes; 1801 space += count * sizeof(xfs_attr_leaf_entry_t); 1802 1803 /* 1804 * leaf1 is the destination, compact it if it looks tight. 1805 */ 1806 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1); 1807 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t); 1808 if (space > max) 1809 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp); 1810 1811 /* 1812 * Move low entries from leaf2 to high end of leaf1. 1813 */ 1814 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1, 1815 ichdr1.count, count); 1816 } 1817 1818 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1); 1819 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2); 1820 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1); 1821 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1); 1822 1823 /* 1824 * Copy out last hashval in each block for B-tree code. 1825 */ 1826 entries1 = xfs_attr3_leaf_entryp(leaf1); 1827 entries2 = xfs_attr3_leaf_entryp(leaf2); 1828 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval); 1829 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval); 1830 1831 /* 1832 * Adjust the expected index for insertion. 1833 * NOTE: this code depends on the (current) situation that the 1834 * second block was originally empty. 1835 * 1836 * If the insertion point moved to the 2nd block, we must adjust 1837 * the index. We must also track the entry just following the 1838 * new entry for use in an "atomic rename" operation, that entry 1839 * is always the "old" entry and the "new" entry is what we are 1840 * inserting. The index/blkno fields refer to the "old" entry, 1841 * while the index2/blkno2 fields refer to the "new" entry. 1842 */ 1843 if (blk1->index > ichdr1.count) { 1844 ASSERT(state->inleaf == 0); 1845 blk2->index = blk1->index - ichdr1.count; 1846 args->index = args->index2 = blk2->index; 1847 args->blkno = args->blkno2 = blk2->blkno; 1848 } else if (blk1->index == ichdr1.count) { 1849 if (state->inleaf) { 1850 args->index = blk1->index; 1851 args->blkno = blk1->blkno; 1852 args->index2 = 0; 1853 args->blkno2 = blk2->blkno; 1854 } else { 1855 /* 1856 * On a double leaf split, the original attr location 1857 * is already stored in blkno2/index2, so don't 1858 * overwrite it overwise we corrupt the tree. 1859 */ 1860 blk2->index = blk1->index - ichdr1.count; 1861 args->index = blk2->index; 1862 args->blkno = blk2->blkno; 1863 if (!state->extravalid) { 1864 /* 1865 * set the new attr location to match the old 1866 * one and let the higher level split code 1867 * decide where in the leaf to place it. 1868 */ 1869 args->index2 = blk2->index; 1870 args->blkno2 = blk2->blkno; 1871 } 1872 } 1873 } else { 1874 ASSERT(state->inleaf == 1); 1875 args->index = args->index2 = blk1->index; 1876 args->blkno = args->blkno2 = blk1->blkno; 1877 } 1878 } 1879 1880 /* 1881 * Examine entries until we reduce the absolute difference in 1882 * byte usage between the two blocks to a minimum. 1883 * GROT: Is this really necessary? With other than a 512 byte blocksize, 1884 * GROT: there will always be enough room in either block for a new entry. 1885 * GROT: Do a double-split for this case? 1886 */ 1887 STATIC int 1888 xfs_attr3_leaf_figure_balance( 1889 struct xfs_da_state *state, 1890 struct xfs_da_state_blk *blk1, 1891 struct xfs_attr3_icleaf_hdr *ichdr1, 1892 struct xfs_da_state_blk *blk2, 1893 struct xfs_attr3_icleaf_hdr *ichdr2, 1894 int *countarg, 1895 int *usedbytesarg) 1896 { 1897 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr; 1898 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr; 1899 struct xfs_attr_leaf_entry *entry; 1900 int count; 1901 int max; 1902 int index; 1903 int totallen = 0; 1904 int half; 1905 int lastdelta; 1906 int foundit = 0; 1907 int tmp; 1908 1909 /* 1910 * Examine entries until we reduce the absolute difference in 1911 * byte usage between the two blocks to a minimum. 1912 */ 1913 max = ichdr1->count + ichdr2->count; 1914 half = (max + 1) * sizeof(*entry); 1915 half += ichdr1->usedbytes + ichdr2->usedbytes + 1916 xfs_attr_leaf_newentsize(state->args, NULL); 1917 half /= 2; 1918 lastdelta = state->args->geo->blksize; 1919 entry = xfs_attr3_leaf_entryp(leaf1); 1920 for (count = index = 0; count < max; entry++, index++, count++) { 1921 1922 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A)) 1923 /* 1924 * The new entry is in the first block, account for it. 1925 */ 1926 if (count == blk1->index) { 1927 tmp = totallen + sizeof(*entry) + 1928 xfs_attr_leaf_newentsize(state->args, NULL); 1929 if (XFS_ATTR_ABS(half - tmp) > lastdelta) 1930 break; 1931 lastdelta = XFS_ATTR_ABS(half - tmp); 1932 totallen = tmp; 1933 foundit = 1; 1934 } 1935 1936 /* 1937 * Wrap around into the second block if necessary. 1938 */ 1939 if (count == ichdr1->count) { 1940 leaf1 = leaf2; 1941 entry = xfs_attr3_leaf_entryp(leaf1); 1942 index = 0; 1943 } 1944 1945 /* 1946 * Figure out if next leaf entry would be too much. 1947 */ 1948 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1, 1949 index); 1950 if (XFS_ATTR_ABS(half - tmp) > lastdelta) 1951 break; 1952 lastdelta = XFS_ATTR_ABS(half - tmp); 1953 totallen = tmp; 1954 #undef XFS_ATTR_ABS 1955 } 1956 1957 /* 1958 * Calculate the number of usedbytes that will end up in lower block. 1959 * If new entry not in lower block, fix up the count. 1960 */ 1961 totallen -= count * sizeof(*entry); 1962 if (foundit) { 1963 totallen -= sizeof(*entry) + 1964 xfs_attr_leaf_newentsize(state->args, NULL); 1965 } 1966 1967 *countarg = count; 1968 *usedbytesarg = totallen; 1969 return foundit; 1970 } 1971 1972 /*======================================================================== 1973 * Routines used for shrinking the Btree. 1974 *========================================================================*/ 1975 1976 /* 1977 * Check a leaf block and its neighbors to see if the block should be 1978 * collapsed into one or the other neighbor. Always keep the block 1979 * with the smaller block number. 1980 * If the current block is over 50% full, don't try to join it, return 0. 1981 * If the block is empty, fill in the state structure and return 2. 1982 * If it can be collapsed, fill in the state structure and return 1. 1983 * If nothing can be done, return 0. 1984 * 1985 * GROT: allow for INCOMPLETE entries in calculation. 1986 */ 1987 int 1988 xfs_attr3_leaf_toosmall( 1989 struct xfs_da_state *state, 1990 int *action) 1991 { 1992 struct xfs_attr_leafblock *leaf; 1993 struct xfs_da_state_blk *blk; 1994 struct xfs_attr3_icleaf_hdr ichdr; 1995 struct xfs_buf *bp; 1996 xfs_dablk_t blkno; 1997 int bytes; 1998 int forward; 1999 int error; 2000 int retval; 2001 int i; 2002 2003 trace_xfs_attr_leaf_toosmall(state->args); 2004 2005 /* 2006 * Check for the degenerate case of the block being over 50% full. 2007 * If so, it's not worth even looking to see if we might be able 2008 * to coalesce with a sibling. 2009 */ 2010 blk = &state->path.blk[ state->path.active-1 ]; 2011 leaf = blk->bp->b_addr; 2012 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf); 2013 bytes = xfs_attr3_leaf_hdr_size(leaf) + 2014 ichdr.count * sizeof(xfs_attr_leaf_entry_t) + 2015 ichdr.usedbytes; 2016 if (bytes > (state->args->geo->blksize >> 1)) { 2017 *action = 0; /* blk over 50%, don't try to join */ 2018 return 0; 2019 } 2020 2021 /* 2022 * Check for the degenerate case of the block being empty. 2023 * If the block is empty, we'll simply delete it, no need to 2024 * coalesce it with a sibling block. We choose (arbitrarily) 2025 * to merge with the forward block unless it is NULL. 2026 */ 2027 if (ichdr.count == 0) { 2028 /* 2029 * Make altpath point to the block we want to keep and 2030 * path point to the block we want to drop (this one). 2031 */ 2032 forward = (ichdr.forw != 0); 2033 memcpy(&state->altpath, &state->path, sizeof(state->path)); 2034 error = xfs_da3_path_shift(state, &state->altpath, forward, 2035 0, &retval); 2036 if (error) 2037 return error; 2038 if (retval) { 2039 *action = 0; 2040 } else { 2041 *action = 2; 2042 } 2043 return 0; 2044 } 2045 2046 /* 2047 * Examine each sibling block to see if we can coalesce with 2048 * at least 25% free space to spare. We need to figure out 2049 * whether to merge with the forward or the backward block. 2050 * We prefer coalescing with the lower numbered sibling so as 2051 * to shrink an attribute list over time. 2052 */ 2053 /* start with smaller blk num */ 2054 forward = ichdr.forw < ichdr.back; 2055 for (i = 0; i < 2; forward = !forward, i++) { 2056 struct xfs_attr3_icleaf_hdr ichdr2; 2057 if (forward) 2058 blkno = ichdr.forw; 2059 else 2060 blkno = ichdr.back; 2061 if (blkno == 0) 2062 continue; 2063 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp, 2064 state->args->owner, blkno, &bp); 2065 if (error) 2066 return error; 2067 2068 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr); 2069 2070 bytes = state->args->geo->blksize - 2071 (state->args->geo->blksize >> 2) - 2072 ichdr.usedbytes - ichdr2.usedbytes - 2073 ((ichdr.count + ichdr2.count) * 2074 sizeof(xfs_attr_leaf_entry_t)) - 2075 xfs_attr3_leaf_hdr_size(leaf); 2076 2077 xfs_trans_brelse(state->args->trans, bp); 2078 if (bytes >= 0) 2079 break; /* fits with at least 25% to spare */ 2080 } 2081 if (i >= 2) { 2082 *action = 0; 2083 return 0; 2084 } 2085 2086 /* 2087 * Make altpath point to the block we want to keep (the lower 2088 * numbered block) and path point to the block we want to drop. 2089 */ 2090 memcpy(&state->altpath, &state->path, sizeof(state->path)); 2091 if (blkno < blk->blkno) { 2092 error = xfs_da3_path_shift(state, &state->altpath, forward, 2093 0, &retval); 2094 } else { 2095 error = xfs_da3_path_shift(state, &state->path, forward, 2096 0, &retval); 2097 } 2098 if (error) 2099 return error; 2100 if (retval) { 2101 *action = 0; 2102 } else { 2103 *action = 1; 2104 } 2105 return 0; 2106 } 2107 2108 /* 2109 * Remove a name from the leaf attribute list structure. 2110 * 2111 * Return 1 if leaf is less than 37% full, 0 if >= 37% full. 2112 * If two leaves are 37% full, when combined they will leave 25% free. 2113 */ 2114 int 2115 xfs_attr3_leaf_remove( 2116 struct xfs_buf *bp, 2117 struct xfs_da_args *args) 2118 { 2119 struct xfs_attr_leafblock *leaf; 2120 struct xfs_attr3_icleaf_hdr ichdr; 2121 struct xfs_attr_leaf_entry *entry; 2122 int before; 2123 int after; 2124 int smallest; 2125 int entsize; 2126 int tablesize; 2127 int tmp; 2128 int i; 2129 2130 trace_xfs_attr_leaf_remove(args); 2131 2132 leaf = bp->b_addr; 2133 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 2134 2135 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8); 2136 ASSERT(args->index >= 0 && args->index < ichdr.count); 2137 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) + 2138 xfs_attr3_leaf_hdr_size(leaf)); 2139 2140 entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; 2141 2142 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); 2143 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); 2144 2145 /* 2146 * Scan through free region table: 2147 * check for adjacency of free'd entry with an existing one, 2148 * find smallest free region in case we need to replace it, 2149 * adjust any map that borders the entry table, 2150 */ 2151 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t) 2152 + xfs_attr3_leaf_hdr_size(leaf); 2153 tmp = ichdr.freemap[0].size; 2154 before = after = -1; 2155 smallest = XFS_ATTR_LEAF_MAPSIZE - 1; 2156 entsize = xfs_attr_leaf_entsize(leaf, args->index); 2157 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { 2158 ASSERT(ichdr.freemap[i].base < args->geo->blksize); 2159 ASSERT(ichdr.freemap[i].size < args->geo->blksize); 2160 if (ichdr.freemap[i].base == tablesize) { 2161 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t); 2162 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t); 2163 } 2164 2165 if (ichdr.freemap[i].base + ichdr.freemap[i].size == 2166 be16_to_cpu(entry->nameidx)) { 2167 before = i; 2168 } else if (ichdr.freemap[i].base == 2169 (be16_to_cpu(entry->nameidx) + entsize)) { 2170 after = i; 2171 } else if (ichdr.freemap[i].size < tmp) { 2172 tmp = ichdr.freemap[i].size; 2173 smallest = i; 2174 } 2175 } 2176 2177 /* 2178 * Coalesce adjacent freemap regions, 2179 * or replace the smallest region. 2180 */ 2181 if ((before >= 0) || (after >= 0)) { 2182 if ((before >= 0) && (after >= 0)) { 2183 ichdr.freemap[before].size += entsize; 2184 ichdr.freemap[before].size += ichdr.freemap[after].size; 2185 ichdr.freemap[after].base = 0; 2186 ichdr.freemap[after].size = 0; 2187 } else if (before >= 0) { 2188 ichdr.freemap[before].size += entsize; 2189 } else { 2190 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx); 2191 ichdr.freemap[after].size += entsize; 2192 } 2193 } else { 2194 /* 2195 * Replace smallest region (if it is smaller than free'd entry) 2196 */ 2197 if (ichdr.freemap[smallest].size < entsize) { 2198 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx); 2199 ichdr.freemap[smallest].size = entsize; 2200 } 2201 } 2202 2203 /* 2204 * Did we remove the first entry? 2205 */ 2206 if (be16_to_cpu(entry->nameidx) == ichdr.firstused) 2207 smallest = 1; 2208 else 2209 smallest = 0; 2210 2211 /* 2212 * Compress the remaining entries and zero out the removed stuff. 2213 */ 2214 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize); 2215 ichdr.usedbytes -= entsize; 2216 xfs_trans_log_buf(args->trans, bp, 2217 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), 2218 entsize)); 2219 2220 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t); 2221 memmove(entry, entry + 1, tmp); 2222 ichdr.count--; 2223 xfs_trans_log_buf(args->trans, bp, 2224 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t))); 2225 2226 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count]; 2227 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t)); 2228 2229 /* 2230 * If we removed the first entry, re-find the first used byte 2231 * in the name area. Note that if the entry was the "firstused", 2232 * then we don't have a "hole" in our block resulting from 2233 * removing the name. 2234 */ 2235 if (smallest) { 2236 tmp = args->geo->blksize; 2237 entry = xfs_attr3_leaf_entryp(leaf); 2238 for (i = ichdr.count - 1; i >= 0; entry++, i--) { 2239 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); 2240 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); 2241 2242 if (be16_to_cpu(entry->nameidx) < tmp) 2243 tmp = be16_to_cpu(entry->nameidx); 2244 } 2245 ichdr.firstused = tmp; 2246 ASSERT(ichdr.firstused != 0); 2247 } else { 2248 ichdr.holes = 1; /* mark as needing compaction */ 2249 } 2250 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); 2251 xfs_trans_log_buf(args->trans, bp, 2252 XFS_DA_LOGRANGE(leaf, &leaf->hdr, 2253 xfs_attr3_leaf_hdr_size(leaf))); 2254 2255 /* 2256 * Check if leaf is less than 50% full, caller may want to 2257 * "join" the leaf with a sibling if so. 2258 */ 2259 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) + 2260 ichdr.count * sizeof(xfs_attr_leaf_entry_t); 2261 2262 return tmp < args->geo->magicpct; /* leaf is < 37% full */ 2263 } 2264 2265 /* 2266 * Move all the attribute list entries from drop_leaf into save_leaf. 2267 */ 2268 void 2269 xfs_attr3_leaf_unbalance( 2270 struct xfs_da_state *state, 2271 struct xfs_da_state_blk *drop_blk, 2272 struct xfs_da_state_blk *save_blk) 2273 { 2274 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr; 2275 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr; 2276 struct xfs_attr3_icleaf_hdr drophdr; 2277 struct xfs_attr3_icleaf_hdr savehdr; 2278 struct xfs_attr_leaf_entry *entry; 2279 2280 trace_xfs_attr_leaf_unbalance(state->args); 2281 2282 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf); 2283 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf); 2284 entry = xfs_attr3_leaf_entryp(drop_leaf); 2285 2286 /* 2287 * Save last hashval from dying block for later Btree fixup. 2288 */ 2289 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval); 2290 2291 /* 2292 * Check if we need a temp buffer, or can we do it in place. 2293 * Note that we don't check "leaf" for holes because we will 2294 * always be dropping it, toosmall() decided that for us already. 2295 */ 2296 if (savehdr.holes == 0) { 2297 /* 2298 * dest leaf has no holes, so we add there. May need 2299 * to make some room in the entry array. 2300 */ 2301 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, 2302 drop_blk->bp, &drophdr)) { 2303 xfs_attr3_leaf_moveents(state->args, 2304 drop_leaf, &drophdr, 0, 2305 save_leaf, &savehdr, 0, 2306 drophdr.count); 2307 } else { 2308 xfs_attr3_leaf_moveents(state->args, 2309 drop_leaf, &drophdr, 0, 2310 save_leaf, &savehdr, 2311 savehdr.count, drophdr.count); 2312 } 2313 } else { 2314 /* 2315 * Destination has holes, so we make a temporary copy 2316 * of the leaf and add them both to that. 2317 */ 2318 struct xfs_attr_leafblock *tmp_leaf; 2319 struct xfs_attr3_icleaf_hdr tmphdr; 2320 2321 tmp_leaf = kvzalloc(state->args->geo->blksize, 2322 GFP_KERNEL | __GFP_NOFAIL); 2323 2324 /* 2325 * Copy the header into the temp leaf so that all the stuff 2326 * not in the incore header is present and gets copied back in 2327 * once we've moved all the entries. 2328 */ 2329 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf)); 2330 2331 memset(&tmphdr, 0, sizeof(tmphdr)); 2332 tmphdr.magic = savehdr.magic; 2333 tmphdr.forw = savehdr.forw; 2334 tmphdr.back = savehdr.back; 2335 tmphdr.firstused = state->args->geo->blksize; 2336 2337 /* write the header to the temp buffer to initialise it */ 2338 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr); 2339 2340 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, 2341 drop_blk->bp, &drophdr)) { 2342 xfs_attr3_leaf_moveents(state->args, 2343 drop_leaf, &drophdr, 0, 2344 tmp_leaf, &tmphdr, 0, 2345 drophdr.count); 2346 xfs_attr3_leaf_moveents(state->args, 2347 save_leaf, &savehdr, 0, 2348 tmp_leaf, &tmphdr, tmphdr.count, 2349 savehdr.count); 2350 } else { 2351 xfs_attr3_leaf_moveents(state->args, 2352 save_leaf, &savehdr, 0, 2353 tmp_leaf, &tmphdr, 0, 2354 savehdr.count); 2355 xfs_attr3_leaf_moveents(state->args, 2356 drop_leaf, &drophdr, 0, 2357 tmp_leaf, &tmphdr, tmphdr.count, 2358 drophdr.count); 2359 } 2360 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize); 2361 savehdr = tmphdr; /* struct copy */ 2362 kvfree(tmp_leaf); 2363 } 2364 2365 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr); 2366 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0, 2367 state->args->geo->blksize - 1); 2368 2369 /* 2370 * Copy out last hashval in each block for B-tree code. 2371 */ 2372 entry = xfs_attr3_leaf_entryp(save_leaf); 2373 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval); 2374 } 2375 2376 /*======================================================================== 2377 * Routines used for finding things in the Btree. 2378 *========================================================================*/ 2379 2380 /* 2381 * Look up a name in a leaf attribute list structure. 2382 * This is the internal routine, it uses the caller's buffer. 2383 * 2384 * Note that duplicate keys are allowed, but only check within the 2385 * current leaf node. The Btree code must check in adjacent leaf nodes. 2386 * 2387 * Return in args->index the index into the entry[] array of either 2388 * the found entry, or where the entry should have been (insert before 2389 * that entry). 2390 * 2391 * Don't change the args->value unless we find the attribute. 2392 */ 2393 int 2394 xfs_attr3_leaf_lookup_int( 2395 struct xfs_buf *bp, 2396 struct xfs_da_args *args) 2397 { 2398 struct xfs_attr_leafblock *leaf; 2399 struct xfs_attr3_icleaf_hdr ichdr; 2400 struct xfs_attr_leaf_entry *entry; 2401 struct xfs_attr_leaf_entry *entries; 2402 struct xfs_attr_leaf_name_local *name_loc; 2403 struct xfs_attr_leaf_name_remote *name_rmt; 2404 xfs_dahash_t hashval; 2405 int probe; 2406 int span; 2407 2408 trace_xfs_attr_leaf_lookup(args); 2409 2410 leaf = bp->b_addr; 2411 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 2412 entries = xfs_attr3_leaf_entryp(leaf); 2413 if (ichdr.count >= args->geo->blksize / 8) { 2414 xfs_buf_mark_corrupt(bp); 2415 xfs_da_mark_sick(args); 2416 return -EFSCORRUPTED; 2417 } 2418 2419 /* 2420 * Binary search. (note: small blocks will skip this loop) 2421 */ 2422 hashval = args->hashval; 2423 probe = span = ichdr.count / 2; 2424 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) { 2425 span /= 2; 2426 if (be32_to_cpu(entry->hashval) < hashval) 2427 probe += span; 2428 else if (be32_to_cpu(entry->hashval) > hashval) 2429 probe -= span; 2430 else 2431 break; 2432 } 2433 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count))) { 2434 xfs_buf_mark_corrupt(bp); 2435 xfs_da_mark_sick(args); 2436 return -EFSCORRUPTED; 2437 } 2438 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval)) { 2439 xfs_buf_mark_corrupt(bp); 2440 xfs_da_mark_sick(args); 2441 return -EFSCORRUPTED; 2442 } 2443 2444 /* 2445 * Since we may have duplicate hashval's, find the first matching 2446 * hashval in the leaf. 2447 */ 2448 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) { 2449 entry--; 2450 probe--; 2451 } 2452 while (probe < ichdr.count && 2453 be32_to_cpu(entry->hashval) < hashval) { 2454 entry++; 2455 probe++; 2456 } 2457 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) { 2458 args->index = probe; 2459 return -ENOATTR; 2460 } 2461 2462 /* 2463 * Duplicate keys may be present, so search all of them for a match. 2464 */ 2465 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval); 2466 entry++, probe++) { 2467 /* 2468 * GROT: Add code to remove incomplete entries. 2469 */ 2470 if (entry->flags & XFS_ATTR_LOCAL) { 2471 name_loc = xfs_attr3_leaf_name_local(leaf, probe); 2472 if (!xfs_attr_match(args, entry->flags, 2473 name_loc->nameval, name_loc->namelen, 2474 &name_loc->nameval[name_loc->namelen], 2475 be16_to_cpu(name_loc->valuelen))) 2476 continue; 2477 args->index = probe; 2478 return -EEXIST; 2479 } else { 2480 unsigned int valuelen; 2481 2482 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe); 2483 valuelen = be32_to_cpu(name_rmt->valuelen); 2484 if (!xfs_attr_match(args, entry->flags, name_rmt->name, 2485 name_rmt->namelen, NULL, valuelen)) 2486 continue; 2487 args->index = probe; 2488 args->rmtvaluelen = valuelen; 2489 args->rmtblkno = be32_to_cpu(name_rmt->valueblk); 2490 args->rmtblkcnt = xfs_attr3_rmt_blocks( 2491 args->dp->i_mount, 2492 args->rmtvaluelen); 2493 return -EEXIST; 2494 } 2495 } 2496 args->index = probe; 2497 return -ENOATTR; 2498 } 2499 2500 /* 2501 * Get the value associated with an attribute name from a leaf attribute 2502 * list structure. 2503 * 2504 * If args->valuelen is zero, only the length needs to be returned. Unlike a 2505 * lookup, we only return an error if the attribute does not exist or we can't 2506 * retrieve the value. 2507 */ 2508 int 2509 xfs_attr3_leaf_getvalue( 2510 struct xfs_buf *bp, 2511 struct xfs_da_args *args) 2512 { 2513 struct xfs_attr_leafblock *leaf; 2514 struct xfs_attr3_icleaf_hdr ichdr; 2515 struct xfs_attr_leaf_entry *entry; 2516 struct xfs_attr_leaf_name_local *name_loc; 2517 struct xfs_attr_leaf_name_remote *name_rmt; 2518 2519 leaf = bp->b_addr; 2520 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 2521 ASSERT(ichdr.count < args->geo->blksize / 8); 2522 ASSERT(args->index < ichdr.count); 2523 2524 entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; 2525 if (entry->flags & XFS_ATTR_LOCAL) { 2526 name_loc = xfs_attr3_leaf_name_local(leaf, args->index); 2527 ASSERT(name_loc->namelen == args->namelen); 2528 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0); 2529 return xfs_attr_copy_value(args, 2530 &name_loc->nameval[args->namelen], 2531 be16_to_cpu(name_loc->valuelen)); 2532 } 2533 2534 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); 2535 ASSERT(name_rmt->namelen == args->namelen); 2536 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0); 2537 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen); 2538 args->rmtblkno = be32_to_cpu(name_rmt->valueblk); 2539 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount, 2540 args->rmtvaluelen); 2541 return xfs_attr_copy_value(args, NULL, args->rmtvaluelen); 2542 } 2543 2544 /*======================================================================== 2545 * Utility routines. 2546 *========================================================================*/ 2547 2548 /* 2549 * Move the indicated entries from one leaf to another. 2550 * NOTE: this routine modifies both source and destination leaves. 2551 */ 2552 /*ARGSUSED*/ 2553 STATIC void 2554 xfs_attr3_leaf_moveents( 2555 struct xfs_da_args *args, 2556 struct xfs_attr_leafblock *leaf_s, 2557 struct xfs_attr3_icleaf_hdr *ichdr_s, 2558 int start_s, 2559 struct xfs_attr_leafblock *leaf_d, 2560 struct xfs_attr3_icleaf_hdr *ichdr_d, 2561 int start_d, 2562 int count) 2563 { 2564 struct xfs_attr_leaf_entry *entry_s; 2565 struct xfs_attr_leaf_entry *entry_d; 2566 int desti; 2567 int tmp; 2568 int i; 2569 2570 /* 2571 * Check for nothing to do. 2572 */ 2573 if (count == 0) 2574 return; 2575 2576 /* 2577 * Set up environment. 2578 */ 2579 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC || 2580 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC); 2581 ASSERT(ichdr_s->magic == ichdr_d->magic); 2582 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8); 2583 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s)) 2584 + xfs_attr3_leaf_hdr_size(leaf_s)); 2585 ASSERT(ichdr_d->count < args->geo->blksize / 8); 2586 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d)) 2587 + xfs_attr3_leaf_hdr_size(leaf_d)); 2588 2589 ASSERT(start_s < ichdr_s->count); 2590 ASSERT(start_d <= ichdr_d->count); 2591 ASSERT(count <= ichdr_s->count); 2592 2593 2594 /* 2595 * Move the entries in the destination leaf up to make a hole? 2596 */ 2597 if (start_d < ichdr_d->count) { 2598 tmp = ichdr_d->count - start_d; 2599 tmp *= sizeof(xfs_attr_leaf_entry_t); 2600 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; 2601 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count]; 2602 memmove(entry_d, entry_s, tmp); 2603 } 2604 2605 /* 2606 * Copy all entry's in the same (sorted) order, 2607 * but allocate attribute info packed and in sequence. 2608 */ 2609 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; 2610 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; 2611 desti = start_d; 2612 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) { 2613 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused); 2614 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i); 2615 #ifdef GROT 2616 /* 2617 * Code to drop INCOMPLETE entries. Difficult to use as we 2618 * may also need to change the insertion index. Code turned 2619 * off for 6.2, should be revisited later. 2620 */ 2621 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */ 2622 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); 2623 ichdr_s->usedbytes -= tmp; 2624 ichdr_s->count -= 1; 2625 entry_d--; /* to compensate for ++ in loop hdr */ 2626 desti--; 2627 if ((start_s + i) < offset) 2628 result++; /* insertion index adjustment */ 2629 } else { 2630 #endif /* GROT */ 2631 ichdr_d->firstused -= tmp; 2632 /* both on-disk, don't endian flip twice */ 2633 entry_d->hashval = entry_s->hashval; 2634 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused); 2635 entry_d->flags = entry_s->flags; 2636 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp 2637 <= args->geo->blksize); 2638 memmove(xfs_attr3_leaf_name(leaf_d, desti), 2639 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp); 2640 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp 2641 <= args->geo->blksize); 2642 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); 2643 ichdr_s->usedbytes -= tmp; 2644 ichdr_d->usedbytes += tmp; 2645 ichdr_s->count -= 1; 2646 ichdr_d->count += 1; 2647 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t) 2648 + xfs_attr3_leaf_hdr_size(leaf_d); 2649 ASSERT(ichdr_d->firstused >= tmp); 2650 #ifdef GROT 2651 } 2652 #endif /* GROT */ 2653 } 2654 2655 /* 2656 * Zero out the entries we just copied. 2657 */ 2658 if (start_s == ichdr_s->count) { 2659 tmp = count * sizeof(xfs_attr_leaf_entry_t); 2660 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; 2661 ASSERT(((char *)entry_s + tmp) <= 2662 ((char *)leaf_s + args->geo->blksize)); 2663 memset(entry_s, 0, tmp); 2664 } else { 2665 /* 2666 * Move the remaining entries down to fill the hole, 2667 * then zero the entries at the top. 2668 */ 2669 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t); 2670 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count]; 2671 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; 2672 memmove(entry_d, entry_s, tmp); 2673 2674 tmp = count * sizeof(xfs_attr_leaf_entry_t); 2675 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count]; 2676 ASSERT(((char *)entry_s + tmp) <= 2677 ((char *)leaf_s + args->geo->blksize)); 2678 memset(entry_s, 0, tmp); 2679 } 2680 2681 /* 2682 * Fill in the freemap information 2683 */ 2684 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d); 2685 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t); 2686 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; 2687 ichdr_d->freemap[1].base = 0; 2688 ichdr_d->freemap[2].base = 0; 2689 ichdr_d->freemap[1].size = 0; 2690 ichdr_d->freemap[2].size = 0; 2691 ichdr_s->holes = 1; /* leaf may not be compact */ 2692 } 2693 2694 /* 2695 * Pick up the last hashvalue from a leaf block. 2696 */ 2697 xfs_dahash_t 2698 xfs_attr_leaf_lasthash( 2699 struct xfs_buf *bp, 2700 int *count) 2701 { 2702 struct xfs_attr3_icleaf_hdr ichdr; 2703 struct xfs_attr_leaf_entry *entries; 2704 struct xfs_mount *mp = bp->b_mount; 2705 2706 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr); 2707 entries = xfs_attr3_leaf_entryp(bp->b_addr); 2708 if (count) 2709 *count = ichdr.count; 2710 if (!ichdr.count) 2711 return 0; 2712 return be32_to_cpu(entries[ichdr.count - 1].hashval); 2713 } 2714 2715 /* 2716 * Calculate the number of bytes used to store the indicated attribute 2717 * (whether local or remote only calculate bytes in this block). 2718 */ 2719 STATIC int 2720 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index) 2721 { 2722 struct xfs_attr_leaf_entry *entries; 2723 xfs_attr_leaf_name_local_t *name_loc; 2724 xfs_attr_leaf_name_remote_t *name_rmt; 2725 int size; 2726 2727 entries = xfs_attr3_leaf_entryp(leaf); 2728 if (entries[index].flags & XFS_ATTR_LOCAL) { 2729 name_loc = xfs_attr3_leaf_name_local(leaf, index); 2730 size = xfs_attr_leaf_entsize_local(name_loc->namelen, 2731 be16_to_cpu(name_loc->valuelen)); 2732 } else { 2733 name_rmt = xfs_attr3_leaf_name_remote(leaf, index); 2734 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen); 2735 } 2736 return size; 2737 } 2738 2739 /* 2740 * Calculate the number of bytes that would be required to store the new 2741 * attribute (whether local or remote only calculate bytes in this block). 2742 * This routine decides as a side effect whether the attribute will be 2743 * a "local" or a "remote" attribute. 2744 */ 2745 int 2746 xfs_attr_leaf_newentsize( 2747 struct xfs_da_args *args, 2748 int *local) 2749 { 2750 int size; 2751 2752 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen); 2753 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) { 2754 if (local) 2755 *local = 1; 2756 return size; 2757 } 2758 if (local) 2759 *local = 0; 2760 return xfs_attr_leaf_entsize_remote(args->namelen); 2761 } 2762 2763 2764 /*======================================================================== 2765 * Manage the INCOMPLETE flag in a leaf entry 2766 *========================================================================*/ 2767 2768 /* 2769 * Clear the INCOMPLETE flag on an entry in a leaf block. 2770 */ 2771 int 2772 xfs_attr3_leaf_clearflag( 2773 struct xfs_da_args *args) 2774 { 2775 struct xfs_attr_leafblock *leaf; 2776 struct xfs_attr_leaf_entry *entry; 2777 struct xfs_attr_leaf_name_remote *name_rmt; 2778 struct xfs_buf *bp; 2779 int error; 2780 #ifdef DEBUG 2781 struct xfs_attr3_icleaf_hdr ichdr; 2782 xfs_attr_leaf_name_local_t *name_loc; 2783 int namelen; 2784 char *name; 2785 #endif /* DEBUG */ 2786 2787 trace_xfs_attr_leaf_clearflag(args); 2788 /* 2789 * Set up the operation. 2790 */ 2791 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 2792 args->blkno, &bp); 2793 if (error) 2794 return error; 2795 2796 leaf = bp->b_addr; 2797 entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; 2798 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE); 2799 2800 #ifdef DEBUG 2801 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 2802 ASSERT(args->index < ichdr.count); 2803 ASSERT(args->index >= 0); 2804 2805 if (entry->flags & XFS_ATTR_LOCAL) { 2806 name_loc = xfs_attr3_leaf_name_local(leaf, args->index); 2807 namelen = name_loc->namelen; 2808 name = (char *)name_loc->nameval; 2809 } else { 2810 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); 2811 namelen = name_rmt->namelen; 2812 name = (char *)name_rmt->name; 2813 } 2814 ASSERT(be32_to_cpu(entry->hashval) == args->hashval); 2815 ASSERT(namelen == args->namelen); 2816 ASSERT(memcmp(name, args->name, namelen) == 0); 2817 #endif /* DEBUG */ 2818 2819 entry->flags &= ~XFS_ATTR_INCOMPLETE; 2820 xfs_trans_log_buf(args->trans, bp, 2821 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); 2822 2823 if (args->rmtblkno) { 2824 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0); 2825 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); 2826 name_rmt->valueblk = cpu_to_be32(args->rmtblkno); 2827 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); 2828 xfs_trans_log_buf(args->trans, bp, 2829 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); 2830 } 2831 2832 return 0; 2833 } 2834 2835 /* 2836 * Set the INCOMPLETE flag on an entry in a leaf block. 2837 */ 2838 int 2839 xfs_attr3_leaf_setflag( 2840 struct xfs_da_args *args) 2841 { 2842 struct xfs_attr_leafblock *leaf; 2843 struct xfs_attr_leaf_entry *entry; 2844 struct xfs_attr_leaf_name_remote *name_rmt; 2845 struct xfs_buf *bp; 2846 int error; 2847 #ifdef DEBUG 2848 struct xfs_attr3_icleaf_hdr ichdr; 2849 #endif 2850 2851 trace_xfs_attr_leaf_setflag(args); 2852 2853 /* 2854 * Set up the operation. 2855 */ 2856 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 2857 args->blkno, &bp); 2858 if (error) 2859 return error; 2860 2861 leaf = bp->b_addr; 2862 #ifdef DEBUG 2863 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); 2864 ASSERT(args->index < ichdr.count); 2865 ASSERT(args->index >= 0); 2866 #endif 2867 entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; 2868 2869 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0); 2870 entry->flags |= XFS_ATTR_INCOMPLETE; 2871 xfs_trans_log_buf(args->trans, bp, 2872 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); 2873 if ((entry->flags & XFS_ATTR_LOCAL) == 0) { 2874 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); 2875 name_rmt->valueblk = 0; 2876 name_rmt->valuelen = 0; 2877 xfs_trans_log_buf(args->trans, bp, 2878 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); 2879 } 2880 2881 return 0; 2882 } 2883 2884 /* 2885 * In a single transaction, clear the INCOMPLETE flag on the leaf entry 2886 * given by args->blkno/index and set the INCOMPLETE flag on the leaf 2887 * entry given by args->blkno2/index2. 2888 * 2889 * Note that they could be in different blocks, or in the same block. 2890 */ 2891 int 2892 xfs_attr3_leaf_flipflags( 2893 struct xfs_da_args *args) 2894 { 2895 struct xfs_attr_leafblock *leaf1; 2896 struct xfs_attr_leafblock *leaf2; 2897 struct xfs_attr_leaf_entry *entry1; 2898 struct xfs_attr_leaf_entry *entry2; 2899 struct xfs_attr_leaf_name_remote *name_rmt; 2900 struct xfs_buf *bp1; 2901 struct xfs_buf *bp2; 2902 int error; 2903 #ifdef DEBUG 2904 struct xfs_attr3_icleaf_hdr ichdr1; 2905 struct xfs_attr3_icleaf_hdr ichdr2; 2906 xfs_attr_leaf_name_local_t *name_loc; 2907 int namelen1, namelen2; 2908 char *name1, *name2; 2909 #endif /* DEBUG */ 2910 2911 trace_xfs_attr_leaf_flipflags(args); 2912 2913 /* 2914 * Read the block containing the "old" attr 2915 */ 2916 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 2917 args->blkno, &bp1); 2918 if (error) 2919 return error; 2920 2921 /* 2922 * Read the block containing the "new" attr, if it is different 2923 */ 2924 if (args->blkno2 != args->blkno) { 2925 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 2926 args->blkno2, &bp2); 2927 if (error) 2928 return error; 2929 } else { 2930 bp2 = bp1; 2931 } 2932 2933 leaf1 = bp1->b_addr; 2934 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index]; 2935 2936 leaf2 = bp2->b_addr; 2937 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2]; 2938 2939 #ifdef DEBUG 2940 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1); 2941 ASSERT(args->index < ichdr1.count); 2942 ASSERT(args->index >= 0); 2943 2944 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2); 2945 ASSERT(args->index2 < ichdr2.count); 2946 ASSERT(args->index2 >= 0); 2947 2948 if (entry1->flags & XFS_ATTR_LOCAL) { 2949 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index); 2950 namelen1 = name_loc->namelen; 2951 name1 = (char *)name_loc->nameval; 2952 } else { 2953 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); 2954 namelen1 = name_rmt->namelen; 2955 name1 = (char *)name_rmt->name; 2956 } 2957 if (entry2->flags & XFS_ATTR_LOCAL) { 2958 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2); 2959 namelen2 = name_loc->namelen; 2960 name2 = (char *)name_loc->nameval; 2961 } else { 2962 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); 2963 namelen2 = name_rmt->namelen; 2964 name2 = (char *)name_rmt->name; 2965 } 2966 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval)); 2967 ASSERT(namelen1 == namelen2); 2968 ASSERT(memcmp(name1, name2, namelen1) == 0); 2969 #endif /* DEBUG */ 2970 2971 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE); 2972 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0); 2973 2974 entry1->flags &= ~XFS_ATTR_INCOMPLETE; 2975 xfs_trans_log_buf(args->trans, bp1, 2976 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1))); 2977 if (args->rmtblkno) { 2978 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0); 2979 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); 2980 name_rmt->valueblk = cpu_to_be32(args->rmtblkno); 2981 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); 2982 xfs_trans_log_buf(args->trans, bp1, 2983 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt))); 2984 } 2985 2986 entry2->flags |= XFS_ATTR_INCOMPLETE; 2987 xfs_trans_log_buf(args->trans, bp2, 2988 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2))); 2989 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) { 2990 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); 2991 name_rmt->valueblk = 0; 2992 name_rmt->valuelen = 0; 2993 xfs_trans_log_buf(args->trans, bp2, 2994 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt))); 2995 } 2996 2997 return 0; 2998 } 2999