File Index
Files and directories
Sign in
FILE

xfs_attr_leaf.c

File preview with syntax highlighting.

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