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