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