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