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