Linux Audio

Check our new training course

Loading...
v4.17
 
   1/*
   2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it would be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write the Free Software Foundation,
  16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17 */
  18#include "xfs.h"
  19#include "xfs_fs.h"
  20#include "xfs_shared.h"
  21#include "xfs_format.h"
  22#include "xfs_log_format.h"
  23#include "xfs_trans_resv.h"
  24#include "xfs_bit.h"
  25#include "xfs_mount.h"
  26#include "xfs_defer.h"
  27#include "xfs_da_format.h"
  28#include "xfs_da_btree.h"
  29#include "xfs_attr_sf.h"
  30#include "xfs_inode.h"
  31#include "xfs_alloc.h"
  32#include "xfs_trans.h"
  33#include "xfs_inode_item.h"
  34#include "xfs_bmap.h"
  35#include "xfs_bmap_util.h"
  36#include "xfs_bmap_btree.h"
  37#include "xfs_attr.h"
  38#include "xfs_attr_leaf.h"
  39#include "xfs_attr_remote.h"
  40#include "xfs_error.h"
  41#include "xfs_quota.h"
  42#include "xfs_trans_space.h"
  43#include "xfs_trace.h"
 
 
 
 
 
  44
  45/*
  46 * xfs_attr.c
  47 *
  48 * Provide the external interfaces to manage attribute lists.
  49 */
  50
  51/*========================================================================
  52 * Function prototypes for the kernel.
  53 *========================================================================*/
  54
  55/*
  56 * Internal routines when attribute list fits inside the inode.
  57 */
  58STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  59
  60/*
  61 * Internal routines when attribute list is one block.
  62 */
  63STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  64STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  65STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
 
  66
  67/*
  68 * Internal routines when attribute list is more than one block.
  69 */
  70STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  71STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  72STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  73STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  74STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
 
 
  75
 
 
 
 
 
 
 
 
 
 
 
  76
  77STATIC int
  78xfs_attr_args_init(
  79	struct xfs_da_args	*args,
  80	struct xfs_inode	*dp,
  81	const unsigned char	*name,
  82	int			flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  83{
 
 
 
  84
  85	if (!name)
  86		return -EINVAL;
  87
  88	memset(args, 0, sizeof(*args));
  89	args->geo = dp->i_mount->m_attr_geo;
  90	args->whichfork = XFS_ATTR_FORK;
  91	args->dp = dp;
  92	args->flags = flags;
  93	args->name = name;
  94	args->namelen = strlen((const char *)name);
  95	if (args->namelen >= MAXNAMELEN)
  96		return -EFAULT;		/* match IRIX behaviour */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  97
  98	args->hashval = xfs_da_hashname(args->name, args->namelen);
  99	return 0;
 100}
 101
 102int
 103xfs_inode_hasattr(
 104	struct xfs_inode	*ip)
 
 
 
 
 
 105{
 106	if (!XFS_IFORK_Q(ip) ||
 107	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 108	     ip->i_d.di_anextents == 0))
 109		return 0;
 110	return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 111}
 
 
 
 112
 113/*========================================================================
 114 * Overall external interface routines.
 115 *========================================================================*/
 116
 117/* Retrieve an extended attribute and its value.  Must have ilock. */
 
 
 
 118int
 119xfs_attr_get_ilocked(
 120	struct xfs_inode	*ip,
 121	struct xfs_da_args	*args)
 122{
 123	ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
 124
 125	if (!xfs_inode_hasattr(ip))
 
 
 126		return -ENOATTR;
 127	else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
 
 
 
 
 
 
 
 
 
 128		return xfs_attr_shortform_getvalue(args);
 129	else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
 130		return xfs_attr_leaf_get(args);
 131	else
 132		return xfs_attr_node_get(args);
 133}
 134
 135/* Retrieve an extended attribute by name, and its value. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 136int
 137xfs_attr_get(
 138	struct xfs_inode	*ip,
 139	const unsigned char	*name,
 140	unsigned char		*value,
 141	int			*valuelenp,
 142	int			flags)
 143{
 144	struct xfs_da_args	args;
 145	uint			lock_mode;
 146	int			error;
 147
 148	XFS_STATS_INC(ip->i_mount, xs_attr_get);
 149
 150	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
 151		return -EIO;
 152
 153	error = xfs_attr_args_init(&args, ip, name, flags);
 154	if (error)
 155		return error;
 
 
 156
 157	args.value = value;
 158	args.valuelen = *valuelenp;
 159	/* Entirely possible to look up a name which doesn't exist */
 160	args.op_flags = XFS_DA_OP_OKNOENT;
 161
 162	lock_mode = xfs_ilock_attr_map_shared(ip);
 163	error = xfs_attr_get_ilocked(ip, &args);
 164	xfs_iunlock(ip, lock_mode);
 165
 166	*valuelenp = args.valuelen;
 167	return error == -EEXIST ? 0 : error;
 168}
 169
 170/*
 171 * Calculate how many blocks we need for the new attribute,
 172 */
 173STATIC int
 174xfs_attr_calc_size(
 175	struct xfs_da_args	*args,
 176	int			*local)
 177{
 178	struct xfs_mount	*mp = args->dp->i_mount;
 179	int			size;
 180	int			nblks;
 181
 182	/*
 183	 * Determine space new attribute will use, and if it would be
 184	 * "local" or "remote" (note: local != inline).
 185	 */
 186	size = xfs_attr_leaf_newentsize(args, local);
 187	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
 188	if (*local) {
 189		if (size > (args->geo->blksize / 2)) {
 190			/* Double split possible */
 191			nblks *= 2;
 192		}
 193	} else {
 194		/*
 195		 * Out of line attribute, cannot double split, but
 196		 * make room for the attribute value itself.
 197		 */
 198		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
 199		nblks += dblocks;
 200		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
 201	}
 202
 203	return nblks;
 204}
 205
 206int
 207xfs_attr_set(
 208	struct xfs_inode	*dp,
 209	const unsigned char	*name,
 210	unsigned char		*value,
 211	int			valuelen,
 212	int			flags)
 213{
 214	struct xfs_mount	*mp = dp->i_mount;
 215	struct xfs_buf		*leaf_bp = NULL;
 216	struct xfs_da_args	args;
 217	struct xfs_defer_ops	dfops;
 218	struct xfs_trans_res	tres;
 219	xfs_fsblock_t		firstblock;
 220	int			rsvd = (flags & ATTR_ROOT) != 0;
 221	int			error, err2, local;
 222
 223	XFS_STATS_INC(mp, xs_attr_set);
 
 224
 225	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
 226		return -EIO;
 
 
 
 
 
 
 
 
 227
 228	error = xfs_attr_args_init(&args, dp, name, flags);
 229	if (error)
 230		return error;
 231
 232	args.value = value;
 233	args.valuelen = valuelen;
 234	args.firstblock = &firstblock;
 235	args.dfops = &dfops;
 236	args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
 237	args.total = xfs_attr_calc_size(&args, &local);
 238
 239	error = xfs_qm_dqattach(dp, 0);
 240	if (error)
 241		return error;
 242
 243	/*
 244	 * If the inode doesn't have an attribute fork, add one.
 245	 * (inode must not be locked when we call this routine)
 246	 */
 247	if (XFS_IFORK_Q(dp) == 0) {
 248		int sf_size = sizeof(xfs_attr_sf_hdr_t) +
 249			XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
 250
 251		error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
 252		if (error)
 253			return error;
 254	}
 255
 256	tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
 257			 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
 258	tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
 259	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
 
 
 
 
 
 
 
 
 
 
 
 
 
 260
 261	/*
 262	 * Root fork attributes can use reserved data blocks for this
 263	 * operation if necessary
 264	 */
 265	error = xfs_trans_alloc(mp, &tres, args.total, 0,
 266			rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
 267	if (error)
 268		return error;
 269
 270	xfs_ilock(dp, XFS_ILOCK_EXCL);
 271	error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
 272				rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
 273				       XFS_QMOPT_RES_REGBLKS);
 274	if (error) {
 275		xfs_iunlock(dp, XFS_ILOCK_EXCL);
 276		xfs_trans_cancel(args.trans);
 277		return error;
 278	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 279
 280	xfs_trans_ijoin(args.trans, dp, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 281
 282	/*
 283	 * If the attribute list is non-existent or a shortform list,
 284	 * upgrade it to a single-leaf-block attribute list.
 285	 */
 286	if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
 287	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 288	     dp->i_d.di_anextents == 0)) {
 
 
 
 
 
 
 289
 
 290		/*
 291		 * Build initial attribute list (if required).
 
 
 292		 */
 293		if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
 294			xfs_attr_shortform_create(&args);
 
 
 
 
 
 295
 296		/*
 297		 * Try to add the attr to the attribute list in
 298		 * the inode.
 299		 */
 300		error = xfs_attr_shortform_addname(&args);
 301		if (error != -ENOSPC) {
 302			/*
 303			 * Commit the shortform mods, and we're done.
 304			 * NOTE: this is also the error path (EEXIST, etc).
 305			 */
 306			ASSERT(args.trans != NULL);
 307
 308			/*
 309			 * If this is a synchronous mount, make sure that
 310			 * the transaction goes to disk before returning
 311			 * to the user.
 312			 */
 313			if (mp->m_flags & XFS_MOUNT_WSYNC)
 314				xfs_trans_set_sync(args.trans);
 315
 316			if (!error && (flags & ATTR_KERNOTIME) == 0) {
 317				xfs_trans_ichgtime(args.trans, dp,
 318							XFS_ICHGTIME_CHG);
 319			}
 320			err2 = xfs_trans_commit(args.trans);
 321			xfs_iunlock(dp, XFS_ILOCK_EXCL);
 322
 323			return error ? error : err2;
 324		}
 
 
 325
 326		/*
 327		 * It won't fit in the shortform, transform to a leaf block.
 328		 * GROT: another possible req'mt for a double-split btree op.
 329		 */
 330		xfs_defer_init(args.dfops, args.firstblock);
 331		error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 332		if (error)
 333			goto out_defer_cancel;
 334		/*
 335		 * Prevent the leaf buffer from being unlocked so that a
 336		 * concurrent AIL push cannot grab the half-baked leaf
 337		 * buffer and run into problems with the write verifier.
 338		 */
 339		xfs_trans_bhold(args.trans, leaf_bp);
 340		xfs_defer_bjoin(args.dfops, leaf_bp);
 341		xfs_defer_ijoin(args.dfops, dp);
 342		error = xfs_defer_finish(&args.trans, args.dfops);
 343		if (error)
 344			goto out_defer_cancel;
 345
 346		/*
 347		 * Commit the leaf transformation.  We'll need another (linked)
 348		 * transaction to add the new attribute to the leaf, which
 349		 * means that we have to hold & join the leaf buffer here too.
 350		 */
 351		error = xfs_trans_roll_inode(&args.trans, dp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 352		if (error)
 
 
 
 353			goto out;
 354		xfs_trans_bjoin(args.trans, leaf_bp);
 355		leaf_bp = NULL;
 356	}
 357
 358	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
 359		error = xfs_attr_leaf_addname(&args);
 360	else
 361		error = xfs_attr_node_addname(&args);
 362	if (error)
 363		goto out;
 364
 
 
 365	/*
 366	 * If this is a synchronous mount, make sure that the
 367	 * transaction goes to disk before returning to the user.
 
 368	 */
 369	if (mp->m_flags & XFS_MOUNT_WSYNC)
 370		xfs_trans_set_sync(args.trans);
 
 
 
 
 371
 372	if ((flags & ATTR_KERNOTIME) == 0)
 373		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
 
 
 
 
 
 
 
 
 374
 375	/*
 376	 * Commit the last in the sequence of transactions.
 
 
 377	 */
 378	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
 379	error = xfs_trans_commit(args.trans);
 380	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 381
 382	return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 383
 384out_defer_cancel:
 385	xfs_defer_cancel(&dfops);
 
 
 
 
 
 
 
 
 386out:
 387	if (leaf_bp)
 388		xfs_trans_brelse(args.trans, leaf_bp);
 389	if (args.trans)
 390		xfs_trans_cancel(args.trans);
 391	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 392	return error;
 393}
 394
 395/*
 396 * Generic handler routine to remove a name from an attribute list.
 397 * Transitions attribute list from Btree to shortform as necessary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 398 */
 399int
 400xfs_attr_remove(
 401	struct xfs_inode	*dp,
 402	const unsigned char	*name,
 403	int			flags)
 404{
 405	struct xfs_mount	*mp = dp->i_mount;
 406	struct xfs_da_args	args;
 407	struct xfs_defer_ops	dfops;
 408	xfs_fsblock_t		firstblock;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 409	int			error;
 410
 411	XFS_STATS_INC(mp, xs_attr_remove);
 
 412
 413	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
 414		return -EIO;
 
 
 
 415
 416	error = xfs_attr_args_init(&args, dp, name, flags);
 
 417	if (error)
 418		return error;
 419
 420	args.firstblock = &firstblock;
 421	args.dfops = &dfops;
 422
 423	/*
 424	 * we have no control over the attribute names that userspace passes us
 425	 * to remove, so we have to allow the name lookup prior to attribute
 426	 * removal to fail.
 427	 */
 428	args.op_flags = XFS_DA_OP_OKNOENT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 429
 430	error = xfs_qm_dqattach(dp, 0);
 
 431	if (error)
 432		return error;
 433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 434	/*
 435	 * Root fork attributes can use reserved data blocks for this
 436	 * operation if necessary
 437	 */
 438	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
 439			XFS_ATTRRM_SPACE_RES(mp), 0,
 440			(flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
 441			&args.trans);
 442	if (error)
 443		return error;
 444
 445	xfs_ilock(dp, XFS_ILOCK_EXCL);
 446	/*
 447	 * No need to make quota reservations here. We expect to release some
 448	 * blocks not allocate in the common case.
 449	 */
 450	xfs_trans_ijoin(args.trans, dp, 0);
 451
 452	if (!xfs_inode_hasattr(dp)) {
 453		error = -ENOATTR;
 454	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
 455		ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
 456		error = xfs_attr_shortform_remove(&args);
 457	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
 458		error = xfs_attr_leaf_removename(&args);
 459	} else {
 460		error = xfs_attr_node_removename(&args);
 461	}
 462
 463	if (error)
 464		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 465
 466	/*
 467	 * If this is a synchronous mount, make sure that the
 468	 * transaction goes to disk before returning to the user.
 469	 */
 470	if (mp->m_flags & XFS_MOUNT_WSYNC)
 471		xfs_trans_set_sync(args.trans);
 472
 473	if ((flags & ATTR_KERNOTIME) == 0)
 474		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
 475
 476	/*
 477	 * Commit the last in the sequence of transactions.
 478	 */
 479	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
 480	error = xfs_trans_commit(args.trans);
 
 481	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 482
 483	return error;
 484
 485out:
 486	if (args.trans)
 487		xfs_trans_cancel(args.trans);
 488	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 489	return error;
 490}
 491
 492/*========================================================================
 493 * External routines when attribute list is inside the inode
 494 *========================================================================*/
 495
 
 
 
 
 
 
 
 496/*
 497 * Add a name to the shortform attribute list structure
 498 * This is the external routine.
 499 */
 500STATIC int
 501xfs_attr_shortform_addname(xfs_da_args_t *args)
 
 502{
 503	int newsize, forkoff, retval;
 504
 505	trace_xfs_attr_sf_addname(args);
 506
 507	retval = xfs_attr_shortform_lookup(args);
 508	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
 509		return retval;
 510	} else if (retval == -EEXIST) {
 511		if (args->flags & ATTR_CREATE)
 512			return retval;
 513		retval = xfs_attr_shortform_remove(args);
 514		if (retval)
 515			return retval;
 516		/*
 517		 * Since we have removed the old attr, clear ATTR_REPLACE so
 518		 * that the leaf format add routine won't trip over the attr
 519		 * not being around.
 
 520		 */
 521		args->flags &= ~ATTR_REPLACE;
 
 
 522	}
 523
 524	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
 525	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
 526		return -ENOSPC;
 527
 528	newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
 529	newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
 530
 531	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
 532	if (!forkoff)
 533		return -ENOSPC;
 534
 535	xfs_attr_shortform_add(args, forkoff);
 536	return 0;
 537}
 538
 539
 540/*========================================================================
 541 * External routines when attribute list is one block
 542 *========================================================================*/
 543
 544/*
 545 * Add a name to the leaf attribute list structure
 546 *
 547 * This leaf block cannot have a "remote" value, we only call this routine
 548 * if bmap_one_block() says there is only one block (ie: no remote blks).
 549 */
 550STATIC int
 551xfs_attr_leaf_addname(xfs_da_args_t *args)
 
 
 552{
 553	xfs_inode_t *dp;
 554	struct xfs_buf *bp;
 555	int retval, error, forkoff;
 556
 557	trace_xfs_attr_leaf_addname(args);
 558
 559	/*
 560	 * Read the (only) block in the attribute list in.
 561	 */
 562	dp = args->dp;
 563	args->blkno = 0;
 564	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
 565	if (error)
 566		return error;
 567
 568	/*
 569	 * Look up the given attribute in the leaf block.  Figure out if
 570	 * the given flags produce an error or call for an atomic rename.
 571	 */
 572	retval = xfs_attr3_leaf_lookup_int(bp, args);
 573	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
 574		xfs_trans_brelse(args->trans, bp);
 575		return retval;
 576	} else if (retval == -EEXIST) {
 577		if (args->flags & ATTR_CREATE) {	/* pure create op */
 578			xfs_trans_brelse(args->trans, bp);
 579			return retval;
 580		}
 581
 582		trace_xfs_attr_leaf_replace(args);
 583
 584		/* save the attribute state for later removal*/
 585		args->op_flags |= XFS_DA_OP_RENAME;	/* an atomic rename */
 586		args->blkno2 = args->blkno;		/* set 2nd entry info*/
 587		args->index2 = args->index;
 588		args->rmtblkno2 = args->rmtblkno;
 589		args->rmtblkcnt2 = args->rmtblkcnt;
 590		args->rmtvaluelen2 = args->rmtvaluelen;
 591
 592		/*
 593		 * clear the remote attr state now that it is saved so that the
 594		 * values reflect the state of the attribute we are about to
 595		 * add, not the attribute we just found and will remove later.
 596		 */
 597		args->rmtblkno = 0;
 598		args->rmtblkcnt = 0;
 599		args->rmtvaluelen = 0;
 600	}
 601
 602	/*
 603	 * Add the attribute to the leaf block, transitioning to a Btree
 604	 * if required.
 605	 */
 606	retval = xfs_attr3_leaf_add(bp, args);
 607	if (retval == -ENOSPC) {
 608		/*
 609		 * Promote the attribute list to the Btree format, then
 610		 * Commit that transaction so that the node_addname() call
 611		 * can manage its own transactions.
 612		 */
 613		xfs_defer_init(args->dfops, args->firstblock);
 614		error = xfs_attr3_leaf_to_node(args);
 615		if (error)
 616			goto out_defer_cancel;
 617		xfs_defer_ijoin(args->dfops, dp);
 618		error = xfs_defer_finish(&args->trans, args->dfops);
 619		if (error)
 620			goto out_defer_cancel;
 621
 622		/*
 623		 * Commit the current trans (including the inode) and start
 624		 * a new one.
 625		 */
 626		error = xfs_trans_roll_inode(&args->trans, dp);
 627		if (error)
 628			return error;
 629
 630		/*
 631		 * Fob the whole rest of the problem off on the Btree code.
 632		 */
 633		error = xfs_attr_node_addname(args);
 634		return error;
 635	}
 636
 637	/*
 638	 * Commit the transaction that added the attr name so that
 639	 * later routines can manage their own transactions.
 640	 */
 641	error = xfs_trans_roll_inode(&args->trans, dp);
 642	if (error)
 643		return error;
 644
 645	/*
 646	 * If there was an out-of-line value, allocate the blocks we
 647	 * identified for its storage and copy the value.  This is done
 648	 * after we create the attribute so that we don't overflow the
 649	 * maximum size of a transaction and/or hit a deadlock.
 650	 */
 651	if (args->rmtblkno > 0) {
 652		error = xfs_attr_rmtval_set(args);
 653		if (error)
 654			return error;
 655	}
 656
 657	/*
 658	 * If this is an atomic rename operation, we must "flip" the
 659	 * incomplete flags on the "new" and "old" attribute/value pairs
 660	 * so that one disappears and one appears atomically.  Then we
 661	 * must remove the "old" attribute/value pair.
 662	 */
 663	if (args->op_flags & XFS_DA_OP_RENAME) {
 664		/*
 665		 * In a separate transaction, set the incomplete flag on the
 666		 * "old" attr and clear the incomplete flag on the "new" attr.
 667		 */
 668		error = xfs_attr3_leaf_flipflags(args);
 669		if (error)
 670			return error;
 671
 672		/*
 673		 * Dismantle the "old" attribute/value pair by removing
 674		 * a "remote" value (if it exists).
 675		 */
 676		args->index = args->index2;
 677		args->blkno = args->blkno2;
 678		args->rmtblkno = args->rmtblkno2;
 679		args->rmtblkcnt = args->rmtblkcnt2;
 680		args->rmtvaluelen = args->rmtvaluelen2;
 681		if (args->rmtblkno) {
 682			error = xfs_attr_rmtval_remove(args);
 683			if (error)
 684				return error;
 685		}
 686
 687		/*
 688		 * Read in the block containing the "old" attr, then
 689		 * remove the "old" attr from that block (neat, huh!)
 690		 */
 691		error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
 692					   -1, &bp);
 693		if (error)
 694			return error;
 695
 696		xfs_attr3_leaf_remove(bp, args);
 697
 698		/*
 699		 * If the result is small enough, shrink it all into the inode.
 700		 */
 701		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
 702			xfs_defer_init(args->dfops, args->firstblock);
 703			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
 704			/* bp is gone due to xfs_da_shrink_inode */
 705			if (error)
 706				goto out_defer_cancel;
 707			xfs_defer_ijoin(args->dfops, dp);
 708			error = xfs_defer_finish(&args->trans, args->dfops);
 709			if (error)
 710				goto out_defer_cancel;
 711		}
 712
 713		/*
 714		 * Commit the remove and start the next trans in series.
 715		 */
 716		error = xfs_trans_roll_inode(&args->trans, dp);
 717
 718	} else if (args->rmtblkno > 0) {
 719		/*
 720		 * Added a "remote" value, just clear the incomplete flag.
 721		 */
 722		error = xfs_attr3_leaf_clearflag(args);
 723	}
 724	return error;
 725out_defer_cancel:
 726	xfs_defer_cancel(args->dfops);
 727	return error;
 728}
 729
 730/*
 731 * Remove a name from the leaf attribute list structure
 732 *
 733 * This leaf block cannot have a "remote" value, we only call this routine
 734 * if bmap_one_block() says there is only one block (ie: no remote blks).
 735 */
 736STATIC int
 737xfs_attr_leaf_removename(xfs_da_args_t *args)
 
 738{
 739	xfs_inode_t *dp;
 740	struct xfs_buf *bp;
 741	int error, forkoff;
 742
 743	trace_xfs_attr_leaf_removename(args);
 744
 745	/*
 746	 * Remove the attribute.
 747	 */
 748	dp = args->dp;
 749	args->blkno = 0;
 750	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
 751	if (error)
 752		return error;
 753
 754	error = xfs_attr3_leaf_lookup_int(bp, args);
 755	if (error == -ENOATTR) {
 756		xfs_trans_brelse(args->trans, bp);
 
 
 
 
 757		return error;
 758	}
 759
 760	xfs_attr3_leaf_remove(bp, args);
 761
 762	/*
 763	 * If the result is small enough, shrink it all into the inode.
 764	 */
 765	if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
 766		xfs_defer_init(args->dfops, args->firstblock);
 767		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
 768		/* bp is gone due to xfs_da_shrink_inode */
 769		if (error)
 770			goto out_defer_cancel;
 771		xfs_defer_ijoin(args->dfops, dp);
 772		error = xfs_defer_finish(&args->trans, args->dfops);
 773		if (error)
 774			goto out_defer_cancel;
 775	}
 776	return 0;
 777out_defer_cancel:
 778	xfs_defer_cancel(args->dfops);
 779	return error;
 780}
 781
 782/*
 783 * Look up a name in a leaf attribute list structure.
 784 *
 785 * This leaf block cannot have a "remote" value, we only call this routine
 786 * if bmap_one_block() says there is only one block (ie: no remote blks).
 
 
 787 */
 788STATIC int
 789xfs_attr_leaf_get(xfs_da_args_t *args)
 790{
 791	struct xfs_buf *bp;
 792	int error;
 793
 794	trace_xfs_attr_leaf_get(args);
 795
 796	args->blkno = 0;
 797	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
 798	if (error)
 799		return error;
 800
 801	error = xfs_attr3_leaf_lookup_int(bp, args);
 802	if (error != -EEXIST)  {
 803		xfs_trans_brelse(args->trans, bp);
 804		return error;
 805	}
 
 
 
 806	error = xfs_attr3_leaf_getvalue(bp, args);
 807	xfs_trans_brelse(args->trans, bp);
 808	if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
 809		error = xfs_attr_rmtval_get(args);
 810	}
 811	return error;
 812}
 813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 814/*========================================================================
 815 * External routines when attribute list size > geo->blksize
 816 *========================================================================*/
 817
 818/*
 819 * Add a name to a Btree-format attribute list.
 820 *
 821 * This will involve walking down the Btree, and may involve splitting
 822 * leaf nodes and even splitting intermediate nodes up to and including
 823 * the root node (a special case of an intermediate node).
 824 *
 825 * "Remote" attribute values confuse the issue and atomic rename operations
 826 * add a whole extra layer of confusion on top of that.
 827 */
 828STATIC int
 829xfs_attr_node_addname(xfs_da_args_t *args)
 
 830{
 831	xfs_da_state_t *state;
 832	xfs_da_state_blk_t *blk;
 833	xfs_inode_t *dp;
 834	xfs_mount_t *mp;
 835	int retval, error;
 836
 837	trace_xfs_attr_node_addname(args);
 838
 839	/*
 840	 * Fill in bucket of arguments/results/context to carry around.
 841	 */
 842	dp = args->dp;
 843	mp = dp->i_mount;
 844restart:
 845	state = xfs_da_state_alloc();
 846	state->args = args;
 847	state->mp = mp;
 848
 849	/*
 850	 * Search to see if name already exists, and get back a pointer
 851	 * to where it should go.
 852	 */
 853	error = xfs_da3_node_lookup_int(state, &retval);
 854	if (error)
 855		goto out;
 856	blk = &state->path.blk[ state->path.active-1 ];
 857	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
 858	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
 859		goto out;
 860	} else if (retval == -EEXIST) {
 861		if (args->flags & ATTR_CREATE)
 862			goto out;
 863
 864		trace_xfs_attr_node_replace(args);
 865
 866		/* save the attribute state for later removal*/
 867		args->op_flags |= XFS_DA_OP_RENAME;	/* atomic rename op */
 868		args->blkno2 = args->blkno;		/* set 2nd entry info*/
 869		args->index2 = args->index;
 870		args->rmtblkno2 = args->rmtblkno;
 871		args->rmtblkcnt2 = args->rmtblkcnt;
 872		args->rmtvaluelen2 = args->rmtvaluelen;
 873
 
 874		/*
 875		 * clear the remote attr state now that it is saved so that the
 876		 * values reflect the state of the attribute we are about to
 877		 * add, not the attribute we just found and will remove later.
 878		 */
 879		args->rmtblkno = 0;
 880		args->rmtblkcnt = 0;
 881		args->rmtvaluelen = 0;
 
 
 
 
 
 
 
 
 
 
 882	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 883
 884	retval = xfs_attr3_leaf_add(blk->bp, state->args);
 885	if (retval == -ENOSPC) {
 
 
 886		if (state->path.active == 1) {
 887			/*
 888			 * Its really a single leaf node, but it had
 889			 * out-of-line values so it looked like it *might*
 890			 * have been a b-tree.
 891			 */
 892			xfs_da_state_free(state);
 893			state = NULL;
 894			xfs_defer_init(args->dfops, args->firstblock);
 895			error = xfs_attr3_leaf_to_node(args);
 896			if (error)
 897				goto out_defer_cancel;
 898			xfs_defer_ijoin(args->dfops, dp);
 899			error = xfs_defer_finish(&args->trans, args->dfops);
 900			if (error)
 901				goto out_defer_cancel;
 902
 903			/*
 904			 * Commit the node conversion and start the next
 905			 * trans in the chain.
 906			 */
 907			error = xfs_trans_roll_inode(&args->trans, dp);
 908			if (error)
 909				goto out;
 910
 911			goto restart;
 912		}
 913
 914		/*
 915		 * Split as many Btree elements as required.
 916		 * This code tracks the new and old attr's location
 917		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
 918		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
 919		 */
 920		xfs_defer_init(args->dfops, args->firstblock);
 921		error = xfs_da3_split(state);
 922		if (error)
 923			goto out_defer_cancel;
 924		xfs_defer_ijoin(args->dfops, dp);
 925		error = xfs_defer_finish(&args->trans, args->dfops);
 926		if (error)
 927			goto out_defer_cancel;
 928	} else {
 929		/*
 930		 * Addition succeeded, update Btree hashvals.
 931		 */
 932		xfs_da3_fixhashpath(state, &state->path);
 933	}
 934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 935	/*
 936	 * Kill the state structure, we're done with it and need to
 937	 * allow the buffers to come back later.
 938	 */
 939	xfs_da_state_free(state);
 940	state = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 941
 942	/*
 943	 * Commit the leaf addition or btree split and start the next
 944	 * trans in the chain.
 
 945	 */
 946	error = xfs_trans_roll_inode(&args->trans, dp);
 
 947	if (error)
 948		goto out;
 949
 950	/*
 951	 * If there was an out-of-line value, allocate the blocks we
 952	 * identified for its storage and copy the value.  This is done
 953	 * after we create the attribute so that we don't overflow the
 954	 * maximum size of a transaction and/or hit a deadlock.
 955	 */
 956	if (args->rmtblkno > 0) {
 957		error = xfs_attr_rmtval_set(args);
 958		if (error)
 959			return error;
 960	}
 961
 962	/*
 963	 * If this is an atomic rename operation, we must "flip" the
 964	 * incomplete flags on the "new" and "old" attribute/value pairs
 965	 * so that one disappears and one appears atomically.  Then we
 966	 * must remove the "old" attribute/value pair.
 967	 */
 968	if (args->op_flags & XFS_DA_OP_RENAME) {
 969		/*
 970		 * In a separate transaction, set the incomplete flag on the
 971		 * "old" attr and clear the incomplete flag on the "new" attr.
 972		 */
 973		error = xfs_attr3_leaf_flipflags(args);
 974		if (error)
 975			goto out;
 976
 977		/*
 978		 * Dismantle the "old" attribute/value pair by removing
 979		 * a "remote" value (if it exists).
 980		 */
 981		args->index = args->index2;
 982		args->blkno = args->blkno2;
 983		args->rmtblkno = args->rmtblkno2;
 984		args->rmtblkcnt = args->rmtblkcnt2;
 985		args->rmtvaluelen = args->rmtvaluelen2;
 986		if (args->rmtblkno) {
 987			error = xfs_attr_rmtval_remove(args);
 988			if (error)
 989				return error;
 990		}
 991
 992		/*
 993		 * Re-find the "old" attribute entry after any split ops.
 994		 * The INCOMPLETE flag means that we will find the "old"
 995		 * attr, not the "new" one.
 996		 */
 997		args->flags |= XFS_ATTR_INCOMPLETE;
 998		state = xfs_da_state_alloc();
 999		state->args = args;
1000		state->mp = mp;
1001		state->inleaf = 0;
1002		error = xfs_da3_node_lookup_int(state, &retval);
1003		if (error)
1004			goto out;
1005
1006		/*
1007		 * Remove the name and update the hashvals in the tree.
1008		 */
1009		blk = &state->path.blk[ state->path.active-1 ];
1010		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1011		error = xfs_attr3_leaf_remove(blk->bp, args);
1012		xfs_da3_fixhashpath(state, &state->path);
1013
1014		/*
1015		 * Check to see if the tree needs to be collapsed.
1016		 */
1017		if (retval && (state->path.active > 1)) {
1018			xfs_defer_init(args->dfops, args->firstblock);
1019			error = xfs_da3_join(state);
1020			if (error)
1021				goto out_defer_cancel;
1022			xfs_defer_ijoin(args->dfops, dp);
1023			error = xfs_defer_finish(&args->trans, args->dfops);
1024			if (error)
1025				goto out_defer_cancel;
1026		}
1027
1028		/*
1029		 * Commit and start the next trans in the chain.
1030		 */
1031		error = xfs_trans_roll_inode(&args->trans, dp);
1032		if (error)
1033			goto out;
1034
1035	} else if (args->rmtblkno > 0) {
1036		/*
1037		 * Added a "remote" value, just clear the incomplete flag.
1038		 */
1039		error = xfs_attr3_leaf_clearflag(args);
1040		if (error)
1041			goto out;
1042	}
1043	retval = error = 0;
1044
1045out:
1046	if (state)
1047		xfs_da_state_free(state);
1048	if (error)
1049		return error;
1050	return retval;
1051out_defer_cancel:
1052	xfs_defer_cancel(args->dfops);
1053	goto out;
1054}
1055
1056/*
1057 * Remove a name from a B-tree attribute list.
1058 *
1059 * This will involve walking down the Btree, and may involve joining
1060 * leaf nodes and even joining intermediate nodes up to and including
1061 * the root node (a special case of an intermediate node).
 
 
1062 */
1063STATIC int
1064xfs_attr_node_removename(xfs_da_args_t *args)
 
1065{
1066	xfs_da_state_t *state;
1067	xfs_da_state_blk_t *blk;
1068	xfs_inode_t *dp;
1069	struct xfs_buf *bp;
1070	int retval, error, forkoff;
1071
1072	trace_xfs_attr_node_removename(args);
1073
1074	/*
1075	 * Tie a string around our finger to remind us where we are.
1076	 */
1077	dp = args->dp;
1078	state = xfs_da_state_alloc();
1079	state->args = args;
1080	state->mp = dp->i_mount;
1081
1082	/*
1083	 * Search to see if name exists, and get back a pointer to it.
1084	 */
1085	error = xfs_da3_node_lookup_int(state, &retval);
1086	if (error || (retval != -EEXIST)) {
1087		if (error == 0)
1088			error = retval;
1089		goto out;
1090	}
1091
1092	/*
1093	 * If there is an out-of-line value, de-allocate the blocks.
1094	 * This is done before we remove the attribute so that we don't
1095	 * overflow the maximum size of a transaction and/or hit a deadlock.
1096	 */
1097	blk = &state->path.blk[ state->path.active-1 ];
1098	ASSERT(blk->bp != NULL);
1099	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1100	if (args->rmtblkno > 0) {
1101		/*
1102		 * Fill in disk block numbers in the state structure
1103		 * so that we can get the buffers back after we commit
1104		 * several transactions in the following calls.
1105		 */
1106		error = xfs_attr_fillstate(state);
1107		if (error)
1108			goto out;
1109
1110		/*
1111		 * Mark the attribute as INCOMPLETE, then bunmapi() the
1112		 * remote value.
1113		 */
1114		error = xfs_attr3_leaf_setflag(args);
1115		if (error)
1116			goto out;
1117		error = xfs_attr_rmtval_remove(args);
1118		if (error)
1119			goto out;
1120
1121		/*
1122		 * Refill the state structure with buffers, the prior calls
1123		 * released our buffers.
1124		 */
1125		error = xfs_attr_refillstate(state);
1126		if (error)
1127			goto out;
1128	}
1129
1130	/*
1131	 * Remove the name and update the hashvals in the tree.
1132	 */
1133	blk = &state->path.blk[ state->path.active-1 ];
1134	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1135	retval = xfs_attr3_leaf_remove(blk->bp, args);
1136	xfs_da3_fixhashpath(state, &state->path);
1137
1138	/*
1139	 * Check to see if the tree needs to be collapsed.
1140	 */
1141	if (retval && (state->path.active > 1)) {
1142		xfs_defer_init(args->dfops, args->firstblock);
1143		error = xfs_da3_join(state);
1144		if (error)
1145			goto out_defer_cancel;
1146		xfs_defer_ijoin(args->dfops, dp);
1147		error = xfs_defer_finish(&args->trans, args->dfops);
1148		if (error)
1149			goto out_defer_cancel;
1150		/*
1151		 * Commit the Btree join operation and start a new trans.
1152		 */
1153		error = xfs_trans_roll_inode(&args->trans, dp);
1154		if (error)
1155			goto out;
1156	}
1157
1158	/*
1159	 * If the result is small enough, push it all into the inode.
1160	 */
1161	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1162		/*
1163		 * Have to get rid of the copy of this dabuf in the state.
1164		 */
1165		ASSERT(state->path.active == 1);
1166		ASSERT(state->path.blk[0].bp);
1167		state->path.blk[0].bp = NULL;
1168
1169		error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1170		if (error)
1171			goto out;
1172
1173		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1174			xfs_defer_init(args->dfops, args->firstblock);
1175			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1176			/* bp is gone due to xfs_da_shrink_inode */
1177			if (error)
1178				goto out_defer_cancel;
1179			xfs_defer_ijoin(args->dfops, dp);
1180			error = xfs_defer_finish(&args->trans, args->dfops);
1181			if (error)
1182				goto out_defer_cancel;
1183		} else
1184			xfs_trans_brelse(args->trans, bp);
1185	}
1186	error = 0;
1187
1188out:
1189	xfs_da_state_free(state);
1190	return error;
1191out_defer_cancel:
1192	xfs_defer_cancel(args->dfops);
1193	goto out;
1194}
1195
1196/*
1197 * Fill in the disk block numbers in the state structure for the buffers
1198 * that are attached to the state structure.
1199 * This is done so that we can quickly reattach ourselves to those buffers
1200 * after some set of transaction commits have released these buffers.
1201 */
1202STATIC int
1203xfs_attr_fillstate(xfs_da_state_t *state)
1204{
1205	xfs_da_state_path_t *path;
1206	xfs_da_state_blk_t *blk;
1207	int level;
1208
1209	trace_xfs_attr_fillstate(state->args);
1210
1211	/*
1212	 * Roll down the "path" in the state structure, storing the on-disk
1213	 * block number for those buffers in the "path".
1214	 */
1215	path = &state->path;
1216	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1217	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1218		if (blk->bp) {
1219			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1220			blk->bp = NULL;
1221		} else {
1222			blk->disk_blkno = 0;
1223		}
1224	}
1225
1226	/*
1227	 * Roll down the "altpath" in the state structure, storing the on-disk
1228	 * block number for those buffers in the "altpath".
1229	 */
1230	path = &state->altpath;
1231	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1232	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1233		if (blk->bp) {
1234			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1235			blk->bp = NULL;
1236		} else {
1237			blk->disk_blkno = 0;
1238		}
1239	}
1240
1241	return 0;
1242}
1243
1244/*
1245 * Reattach the buffers to the state structure based on the disk block
1246 * numbers stored in the state structure.
1247 * This is done after some set of transaction commits have released those
1248 * buffers from our grip.
1249 */
1250STATIC int
1251xfs_attr_refillstate(xfs_da_state_t *state)
1252{
1253	xfs_da_state_path_t *path;
1254	xfs_da_state_blk_t *blk;
1255	int level, error;
1256
1257	trace_xfs_attr_refillstate(state->args);
1258
1259	/*
1260	 * Roll down the "path" in the state structure, storing the on-disk
1261	 * block number for those buffers in the "path".
1262	 */
1263	path = &state->path;
1264	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1265	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1266		if (blk->disk_blkno) {
1267			error = xfs_da3_node_read(state->args->trans,
1268						state->args->dp,
1269						blk->blkno, blk->disk_blkno,
1270						&blk->bp, XFS_ATTR_FORK);
1271			if (error)
1272				return error;
1273		} else {
1274			blk->bp = NULL;
1275		}
1276	}
1277
1278	/*
1279	 * Roll down the "altpath" in the state structure, storing the on-disk
1280	 * block number for those buffers in the "altpath".
1281	 */
1282	path = &state->altpath;
1283	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1284	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1285		if (blk->disk_blkno) {
1286			error = xfs_da3_node_read(state->args->trans,
1287						state->args->dp,
1288						blk->blkno, blk->disk_blkno,
1289						&blk->bp, XFS_ATTR_FORK);
1290			if (error)
1291				return error;
1292		} else {
1293			blk->bp = NULL;
1294		}
1295	}
1296
1297	return 0;
 
1298}
1299
1300/*
1301 * Look up a filename in a node attribute list.
1302 *
1303 * This routine gets called for any attribute fork that has more than one
1304 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1305 * "remote" values taking up more blocks.
1306 */
1307STATIC int
1308xfs_attr_node_get(xfs_da_args_t *args)
1309{
1310	xfs_da_state_t *state;
1311	xfs_da_state_blk_t *blk;
1312	int error, retval;
1313	int i;
1314
1315	trace_xfs_attr_node_get(args);
1316
1317	state = xfs_da_state_alloc();
1318	state->args = args;
1319	state->mp = args->dp->i_mount;
1320
1321	/*
1322	 * Search to see if name exists, and get back a pointer to it.
1323	 */
1324	error = xfs_da3_node_lookup_int(state, &retval);
1325	if (error) {
1326		retval = error;
1327	} else if (retval == -EEXIST) {
1328		blk = &state->path.blk[ state->path.active-1 ];
1329		ASSERT(blk->bp != NULL);
1330		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1331
1332		/*
1333		 * Get the value, local or "remote"
1334		 */
1335		retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1336		if (!retval && (args->rmtblkno > 0)
1337		    && !(args->flags & ATTR_KERNOVAL)) {
1338			retval = xfs_attr_rmtval_get(args);
1339		}
1340	}
1341
1342	/*
1343	 * If not in a transaction, we have to release all the buffers.
1344	 */
1345	for (i = 0; i < state->path.active; i++) {
1346		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1347		state->path.blk[i].bp = NULL;
1348	}
1349
1350	xfs_da_state_free(state);
1351	return retval;
 
 
 
1352}
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
   4 * All Rights Reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
   5 */
   6#include "xfs.h"
   7#include "xfs_fs.h"
   8#include "xfs_shared.h"
   9#include "xfs_format.h"
  10#include "xfs_log_format.h"
  11#include "xfs_trans_resv.h"
 
  12#include "xfs_mount.h"
  13#include "xfs_defer.h"
  14#include "xfs_da_format.h"
  15#include "xfs_da_btree.h"
  16#include "xfs_attr_sf.h"
  17#include "xfs_inode.h"
 
  18#include "xfs_trans.h"
 
  19#include "xfs_bmap.h"
 
  20#include "xfs_bmap_btree.h"
  21#include "xfs_attr.h"
  22#include "xfs_attr_leaf.h"
  23#include "xfs_attr_remote.h"
 
  24#include "xfs_quota.h"
  25#include "xfs_trans_space.h"
  26#include "xfs_trace.h"
  27#include "xfs_attr_item.h"
  28#include "xfs_xattr.h"
  29#include "xfs_parent.h"
  30
  31struct kmem_cache		*xfs_attr_intent_cache;
  32
  33/*
  34 * xfs_attr.c
  35 *
  36 * Provide the external interfaces to manage attribute lists.
  37 */
  38
  39/*========================================================================
  40 * Function prototypes for the kernel.
  41 *========================================================================*/
  42
  43/*
  44 * Internal routines when attribute list fits inside the inode.
  45 */
  46STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  47
  48/*
  49 * Internal routines when attribute list is one block.
  50 */
  51STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
 
  52STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  53STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
  54
  55/*
  56 * Internal routines when attribute list is more than one block.
  57 */
  58STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  59STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
  60static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
  61STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
  62STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
  63STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
  64		struct xfs_da_state *state);
  65
  66int
  67xfs_inode_hasattr(
  68	struct xfs_inode	*ip)
  69{
  70	if (!xfs_inode_has_attr_fork(ip))
  71		return 0;
  72	if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
  73	    ip->i_af.if_nextents == 0)
  74		return 0;
  75	return 1;
  76}
  77
  78/*
  79 * Returns true if the there is exactly only block in the attr fork, in which
  80 * case the attribute fork consists of a single leaf block entry.
  81 */
  82bool
  83xfs_attr_is_leaf(
  84	struct xfs_inode	*ip)
  85{
  86	struct xfs_ifork	*ifp = &ip->i_af;
  87	struct xfs_iext_cursor	icur;
  88	struct xfs_bmbt_irec	imap;
  89
  90	ASSERT(!xfs_need_iread_extents(ifp));
  91
  92	if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
  93		return false;
  94
  95	xfs_iext_first(ifp, &icur);
  96	xfs_iext_get_extent(ifp, &icur, &imap);
  97	return imap.br_startoff == 0 && imap.br_blockcount == 1;
  98}
  99
 100/*
 101 * XXX (dchinner): name path state saving and refilling is an optimisation to
 102 * avoid needing to look up name entries after rolling transactions removing
 103 * remote xattr blocks between the name entry lookup and name entry removal.
 104 * This optimisation got sidelined when combining the set and remove state
 105 * machines, but the code has been left in place because it is worthwhile to
 106 * restore the optimisation once the combined state machine paths have settled.
 107 *
 108 * This comment is a public service announcement to remind Future Dave that he
 109 * still needs to restore this code to working order.
 110 */
 111#if 0
 112/*
 113 * Fill in the disk block numbers in the state structure for the buffers
 114 * that are attached to the state structure.
 115 * This is done so that we can quickly reattach ourselves to those buffers
 116 * after some set of transaction commits have released these buffers.
 117 */
 118static int
 119xfs_attr_fillstate(xfs_da_state_t *state)
 120{
 121	xfs_da_state_path_t *path;
 122	xfs_da_state_blk_t *blk;
 123	int level;
 124
 125	trace_xfs_attr_fillstate(state->args);
 
 126
 127	/*
 128	 * Roll down the "path" in the state structure, storing the on-disk
 129	 * block number for those buffers in the "path".
 130	 */
 131	path = &state->path;
 132	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
 133	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
 134		if (blk->bp) {
 135			blk->disk_blkno = xfs_buf_daddr(blk->bp);
 136			blk->bp = NULL;
 137		} else {
 138			blk->disk_blkno = 0;
 139		}
 140	}
 141
 142	/*
 143	 * Roll down the "altpath" in the state structure, storing the on-disk
 144	 * block number for those buffers in the "altpath".
 145	 */
 146	path = &state->altpath;
 147	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
 148	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
 149		if (blk->bp) {
 150			blk->disk_blkno = xfs_buf_daddr(blk->bp);
 151			blk->bp = NULL;
 152		} else {
 153			blk->disk_blkno = 0;
 154		}
 155	}
 156
 
 157	return 0;
 158}
 159
 160/*
 161 * Reattach the buffers to the state structure based on the disk block
 162 * numbers stored in the state structure.
 163 * This is done after some set of transaction commits have released those
 164 * buffers from our grip.
 165 */
 166static int
 167xfs_attr_refillstate(xfs_da_state_t *state)
 168{
 169	xfs_da_state_path_t *path;
 170	xfs_da_state_blk_t *blk;
 171	int level, error;
 172
 173	trace_xfs_attr_refillstate(state->args);
 174
 175	/*
 176	 * Roll down the "path" in the state structure, storing the on-disk
 177	 * block number for those buffers in the "path".
 178	 */
 179	path = &state->path;
 180	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
 181	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
 182		if (blk->disk_blkno) {
 183			error = xfs_da3_node_read_mapped(state->args->trans,
 184					state->args->dp, blk->disk_blkno,
 185					&blk->bp, XFS_ATTR_FORK);
 186			if (error)
 187				return error;
 188		} else {
 189			blk->bp = NULL;
 190		}
 191	}
 192
 193	/*
 194	 * Roll down the "altpath" in the state structure, storing the on-disk
 195	 * block number for those buffers in the "altpath".
 196	 */
 197	path = &state->altpath;
 198	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
 199	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
 200		if (blk->disk_blkno) {
 201			error = xfs_da3_node_read_mapped(state->args->trans,
 202					state->args->dp, blk->disk_blkno,
 203					&blk->bp, XFS_ATTR_FORK);
 204			if (error)
 205				return error;
 206		} else {
 207			blk->bp = NULL;
 208		}
 209	}
 210
 211	return 0;
 212}
 213#else
 214static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
 215#endif
 216
 217/*========================================================================
 218 * Overall external interface routines.
 219 *========================================================================*/
 220
 221/*
 222 * Retrieve an extended attribute and its value.  Must have ilock.
 223 * Returns 0 on successful retrieval, otherwise an error.
 224 */
 225int
 226xfs_attr_get_ilocked(
 
 227	struct xfs_da_args	*args)
 228{
 229	int			error;
 230
 231	xfs_assert_ilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
 232
 233	if (!xfs_inode_hasattr(args->dp))
 234		return -ENOATTR;
 235
 236	/*
 237	 * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf
 238	 * to work correctly.
 239	 */
 240	error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
 241	if (error)
 242		return error;
 243
 244	if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
 245		return xfs_attr_shortform_getvalue(args);
 246	if (xfs_attr_is_leaf(args->dp))
 247		return xfs_attr_leaf_get(args);
 248	return xfs_attr_node_get(args);
 
 249}
 250
 251/*
 252 * Retrieve an extended attribute by name, and its value if requested.
 253 *
 254 * If args->valuelen is zero, then the caller does not want the value, just an
 255 * indication whether the attribute exists and the size of the value if it
 256 * exists. The size is returned in args.valuelen.
 257 *
 258 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
 259 * for the value after existence of the attribute has been determined. The
 260 * caller always has to free args->value if it is set, no matter if this
 261 * function was successful or not.
 262 *
 263 * If the attribute is found, but exceeds the size limit set by the caller in
 264 * args->valuelen, return -ERANGE with the size of the attribute that was found
 265 * in args->valuelen.
 266 */
 267int
 268xfs_attr_get(
 269	struct xfs_da_args	*args)
 
 
 
 
 270{
 
 271	uint			lock_mode;
 272	int			error;
 273
 274	XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
 275
 276	if (xfs_is_shutdown(args->dp->i_mount))
 277		return -EIO;
 278
 279	if (!args->owner)
 280		args->owner = args->dp->i_ino;
 281	args->geo = args->dp->i_mount->m_attr_geo;
 282	args->whichfork = XFS_ATTR_FORK;
 283	xfs_attr_sethash(args);
 284
 
 
 285	/* Entirely possible to look up a name which doesn't exist */
 286	args->op_flags = XFS_DA_OP_OKNOENT;
 287
 288	lock_mode = xfs_ilock_attr_map_shared(args->dp);
 289	error = xfs_attr_get_ilocked(args);
 290	xfs_iunlock(args->dp, lock_mode);
 291
 292	return error;
 
 293}
 294
 295/*
 296 * Calculate how many blocks we need for the new attribute,
 297 */
 298int
 299xfs_attr_calc_size(
 300	struct xfs_da_args	*args,
 301	int			*local)
 302{
 303	struct xfs_mount	*mp = args->dp->i_mount;
 304	int			size;
 305	int			nblks;
 306
 307	/*
 308	 * Determine space new attribute will use, and if it would be
 309	 * "local" or "remote" (note: local != inline).
 310	 */
 311	size = xfs_attr_leaf_newentsize(args, local);
 312	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
 313	if (*local) {
 314		if (size > (args->geo->blksize / 2)) {
 315			/* Double split possible */
 316			nblks *= 2;
 317		}
 318	} else {
 319		/*
 320		 * Out of line attribute, cannot double split, but
 321		 * make room for the attribute value itself.
 322		 */
 323		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
 324		nblks += dblocks;
 325		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
 326	}
 327
 328	return nblks;
 329}
 330
 331/* Initialize transaction reservation for an xattr set/replace/upsert */
 332inline struct xfs_trans_res
 333xfs_attr_set_resv(
 334	const struct xfs_da_args	*args)
 
 
 
 335{
 336	struct xfs_mount		*mp = args->dp->i_mount;
 337	struct xfs_trans_res		ret = {
 338		.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
 339			    M_RES(mp)->tr_attrsetrt.tr_logres * args->total,
 340		.tr_logcount		= XFS_ATTRSET_LOG_COUNT,
 341		.tr_logflags		= XFS_TRANS_PERM_LOG_RES,
 342	};
 
 343
 344	return ret;
 345}
 346
 347/*
 348 * Add an attr to a shortform fork. If there is no space,
 349 * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
 350 * to use.
 351 */
 352STATIC int
 353xfs_attr_try_sf_addname(
 354	struct xfs_inode	*dp,
 355	struct xfs_da_args	*args)
 356{
 357
 358	int			error;
 
 
 359
 360	/*
 361	 * Build initial attribute list (if required).
 362	 */
 363	if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
 364		xfs_attr_shortform_create(args);
 
 365
 366	error = xfs_attr_shortform_addname(args);
 367	if (error == -ENOSPC)
 368		return error;
 369
 370	/*
 371	 * Commit the shortform mods, and we're done.
 372	 * NOTE: this is also the error path (EEXIST, etc).
 373	 */
 374	if (!error)
 375		xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
 
 376
 377	if (xfs_has_wsync(dp->i_mount))
 378		xfs_trans_set_sync(args->trans);
 
 
 379
 380	return error;
 381}
 382
 383static int
 384xfs_attr_sf_addname(
 385	struct xfs_attr_intent		*attr)
 386{
 387	struct xfs_da_args		*args = attr->xattri_da_args;
 388	struct xfs_inode		*dp = args->dp;
 389	int				error = 0;
 390
 391	error = xfs_attr_try_sf_addname(dp, args);
 392	if (error != -ENOSPC) {
 393		ASSERT(!error || error == -EEXIST);
 394		attr->xattri_dela_state = XFS_DAS_DONE;
 395		goto out;
 396	}
 397
 398	/*
 399	 * It won't fit in the shortform, transform to a leaf block.  GROT:
 400	 * another possible req'mt for a double-split btree op.
 401	 */
 402	error = xfs_attr_shortform_to_leaf(args);
 
 403	if (error)
 404		return error;
 405
 406	attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
 407out:
 408	trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
 409	return error;
 410}
 411
 412/* Compute the hash value for a user/root/secure extended attribute */
 413xfs_dahash_t
 414xfs_attr_hashname(
 415	const uint8_t		*name,
 416	int			namelen)
 417{
 418	return xfs_da_hashname(name, namelen);
 419}
 420
 421/* Compute the hash value for any extended attribute from any namespace. */
 422xfs_dahash_t
 423xfs_attr_hashval(
 424	struct xfs_mount	*mp,
 425	unsigned int		attr_flags,
 426	const uint8_t		*name,
 427	int			namelen,
 428	const void		*value,
 429	int			valuelen)
 430{
 431	ASSERT(xfs_attr_check_namespace(attr_flags));
 432
 433	if (attr_flags & XFS_ATTR_PARENT)
 434		return xfs_parent_hashattr(mp, name, namelen, value, valuelen);
 435
 436	return xfs_attr_hashname(name, namelen);
 437}
 438
 439/* Save the current remote block info and clear the current pointers. */
 440static void
 441xfs_attr_save_rmt_blk(
 442	struct xfs_da_args	*args)
 443{
 444	args->blkno2 = args->blkno;
 445	args->index2 = args->index;
 446	args->rmtblkno2 = args->rmtblkno;
 447	args->rmtblkcnt2 = args->rmtblkcnt;
 448	args->rmtvaluelen2 = args->rmtvaluelen;
 449	args->rmtblkno = 0;
 450	args->rmtblkcnt = 0;
 451	args->rmtvaluelen = 0;
 452}
 453
 454/* Set stored info about a remote block */
 455static void
 456xfs_attr_restore_rmt_blk(
 457	struct xfs_da_args	*args)
 458{
 459	args->blkno = args->blkno2;
 460	args->index = args->index2;
 461	args->rmtblkno = args->rmtblkno2;
 462	args->rmtblkcnt = args->rmtblkcnt2;
 463	args->rmtvaluelen = args->rmtvaluelen2;
 464}
 465
 466/*
 467 * PPTR_REPLACE operations require the caller to set the old and new names and
 468 * values explicitly.  Update the canonical fields to the new name and value
 469 * here now that the removal phase has finished.
 470 */
 471static void
 472xfs_attr_update_pptr_replace_args(
 473	struct xfs_da_args	*args)
 474{
 475	ASSERT(args->new_namelen > 0);
 476	args->name = args->new_name;
 477	args->namelen = args->new_namelen;
 478	args->value = args->new_value;
 479	args->valuelen = args->new_valuelen;
 480	xfs_attr_sethash(args);
 481}
 482
 483/*
 484 * Handle the state change on completion of a multi-state attr operation.
 485 *
 486 * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
 487 * modification in a attr replace operation and we still have to do the second
 488 * state, indicated by @replace_state.
 489 *
 490 * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
 491 * completion of the second half of the attr replace operation we correctly
 492 * signal that it is done.
 493 */
 494static enum xfs_delattr_state
 495xfs_attr_complete_op(
 496	struct xfs_attr_intent	*attr,
 497	enum xfs_delattr_state	replace_state)
 498{
 499	struct xfs_da_args	*args = attr->xattri_da_args;
 500
 501	if (!(args->op_flags & XFS_DA_OP_REPLACE))
 502		replace_state = XFS_DAS_DONE;
 503	else if (xfs_attr_intent_op(attr) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE)
 504		xfs_attr_update_pptr_replace_args(args);
 505
 506	args->op_flags &= ~XFS_DA_OP_REPLACE;
 507	args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
 508	return replace_state;
 509}
 510
 511/*
 512 * Try to add an attribute to an inode in leaf form.
 513 */
 514static int
 515xfs_attr_leaf_addname(
 516	struct xfs_attr_intent	*attr)
 517{
 518	struct xfs_da_args	*args = attr->xattri_da_args;
 519	struct xfs_buf		*bp;
 520	int			error;
 521
 522	ASSERT(xfs_attr_is_leaf(args->dp));
 523
 524	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
 525	if (error)
 526		return error;
 527
 528	/*
 529	 * Look up the xattr name to set the insertion point for the new xattr.
 
 530	 */
 531	error = xfs_attr3_leaf_lookup_int(bp, args);
 532	switch (error) {
 533	case -ENOATTR:
 534		if (args->op_flags & XFS_DA_OP_REPLACE)
 535			goto out_brelse;
 536		break;
 537	case -EEXIST:
 538		if (!(args->op_flags & XFS_DA_OP_REPLACE))
 539			goto out_brelse;
 540
 541		trace_xfs_attr_leaf_replace(args);
 542		/*
 543		 * Save the existing remote attr state so that the current
 544		 * values reflect the state of the new attribute we are about to
 545		 * add, not the attribute we just found and will remove later.
 546		 */
 547		xfs_attr_save_rmt_blk(args);
 548		break;
 549	case 0:
 550		break;
 551	default:
 552		goto out_brelse;
 553	}
 554
 555	/*
 556	 * We need to commit and roll if we need to allocate remote xattr blocks
 557	 * or perform more xattr manipulations. Otherwise there is nothing more
 558	 * to do and we can return success.
 559	 */
 560	if (!xfs_attr3_leaf_add(bp, args)) {
 561		error = xfs_attr3_leaf_to_node(args);
 562		if (error)
 563			return error;
 
 
 564
 565		attr->xattri_dela_state = XFS_DAS_NODE_ADD;
 566	} else if (args->rmtblkno) {
 567		attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
 568	} else {
 569		attr->xattri_dela_state =
 570			xfs_attr_complete_op(attr, XFS_DAS_LEAF_REPLACE);
 571	}
 572
 573	trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
 574	return 0;
 
 
 
 
 575
 576out_brelse:
 577	xfs_trans_brelse(args->trans, bp);
 578	return error;
 579}
 580
 581/*
 582 * Add an entry to a node format attr tree.
 583 *
 584 * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
 585 * the difference between leaf + remote attr blocks and a node format tree,
 586 * so we may still end up having to convert from leaf to node format here.
 587 */
 588static int
 589xfs_attr_node_addname(
 590	struct xfs_attr_intent	*attr)
 591{
 592	struct xfs_da_args	*args = attr->xattri_da_args;
 593	int			error;
 594
 595	error = xfs_attr_node_addname_find_attr(attr);
 596	if (error)
 597		return error;
 598
 599	error = xfs_attr_node_try_addname(attr);
 600	if (error == 1) {
 601		error = xfs_attr3_leaf_to_node(args);
 602		if (error)
 603			return error;
 604		/*
 605		 * No state change, we really are in node form now
 606		 * but we need the transaction rolled to continue.
 
 607		 */
 608		goto out;
 609	}
 610	if (error)
 611		return error;
 
 
 612
 613	if (args->rmtblkno)
 614		attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
 615	else
 616		attr->xattri_dela_state = xfs_attr_complete_op(attr,
 617							XFS_DAS_NODE_REPLACE);
 618out:
 619	trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
 620	return error;
 621}
 622
 623static int
 624xfs_attr_rmtval_alloc(
 625	struct xfs_attr_intent		*attr)
 626{
 627	struct xfs_da_args              *args = attr->xattri_da_args;
 628	int				error = 0;
 629
 630	/*
 631	 * If there was an out-of-line value, allocate the blocks we
 632	 * identified for its storage and copy the value.  This is done
 633	 * after we create the attribute so that we don't overflow the
 634	 * maximum size of a transaction and/or hit a deadlock.
 635	 */
 636	if (attr->xattri_blkcnt > 0) {
 637		error = xfs_attr_rmtval_set_blk(attr);
 638		if (error)
 639			return error;
 640		/* Roll the transaction only if there is more to allocate. */
 641		if (attr->xattri_blkcnt > 0)
 642			goto out;
 
 
 643	}
 644
 645	error = xfs_attr_rmtval_set_value(args);
 
 
 
 646	if (error)
 647		return error;
 648
 649	attr->xattri_dela_state = xfs_attr_complete_op(attr,
 650						++attr->xattri_dela_state);
 651	/*
 652	 * If we are not doing a rename, we've finished the operation but still
 653	 * have to clear the incomplete flag protecting the new attr from
 654	 * exposing partially initialised state if we crash during creation.
 655	 */
 656	if (attr->xattri_dela_state == XFS_DAS_DONE)
 657		error = xfs_attr3_leaf_clearflag(args);
 658out:
 659	trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
 660	return error;
 661}
 662
 663/*
 664 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
 665 * for later deletion of the entry.
 666 */
 667static int
 668xfs_attr_leaf_mark_incomplete(
 669	struct xfs_da_args	*args,
 670	struct xfs_da_state	*state)
 671{
 672	int			error;
 673
 674	/*
 675	 * Fill in disk block numbers in the state structure
 676	 * so that we can get the buffers back after we commit
 677	 * several transactions in the following calls.
 678	 */
 679	error = xfs_attr_fillstate(state);
 680	if (error)
 681		return error;
 682
 683	/*
 684	 * Mark the attribute as INCOMPLETE
 685	 */
 686	return xfs_attr3_leaf_setflag(args);
 687}
 688
 689/* Ensure the da state of an xattr deferred work item is ready to go. */
 690static inline void
 691xfs_attr_item_init_da_state(
 692	struct xfs_attr_intent	*attr)
 693{
 694	struct xfs_da_args	*args = attr->xattri_da_args;
 695
 696	if (!attr->xattri_da_state)
 697		attr->xattri_da_state = xfs_da_state_alloc(args);
 698	else
 699		xfs_da_state_reset(attr->xattri_da_state, args);
 700}
 701
 702/*
 703 * Initial setup for xfs_attr_node_removename.  Make sure the attr is there and
 704 * the blocks are valid.  Attr keys with remote blocks will be marked
 705 * incomplete.
 706 */
 707static
 708int xfs_attr_node_removename_setup(
 709	struct xfs_attr_intent		*attr)
 710{
 711	struct xfs_da_args		*args = attr->xattri_da_args;
 712	struct xfs_da_state		*state;
 713	int				error;
 714
 715	xfs_attr_item_init_da_state(attr);
 716	error = xfs_attr_node_lookup(args, attr->xattri_da_state);
 717	if (error != -EEXIST)
 718		goto out;
 719	error = 0;
 720
 721	state = attr->xattri_da_state;
 722	ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
 723	ASSERT(state->path.blk[state->path.active - 1].magic ==
 724		XFS_ATTR_LEAF_MAGIC);
 725
 726	error = xfs_attr_leaf_mark_incomplete(args, state);
 727	if (error)
 728		goto out;
 729	if (args->rmtblkno > 0)
 730		error = xfs_attr_rmtval_invalidate(args);
 731out:
 732	if (error) {
 733		xfs_da_state_free(attr->xattri_da_state);
 734		attr->xattri_da_state = NULL;
 735	}
 736
 737	return error;
 738}
 739
 740/*
 741 * Remove the original attr we have just replaced. This is dependent on the
 742 * original lookup and insert placing the old attr in args->blkno/args->index
 743 * and the new attr in args->blkno2/args->index2.
 744 */
 745static int
 746xfs_attr_leaf_remove_attr(
 747	struct xfs_attr_intent		*attr)
 748{
 749	struct xfs_da_args              *args = attr->xattri_da_args;
 750	struct xfs_inode		*dp = args->dp;
 751	struct xfs_buf			*bp = NULL;
 752	int				forkoff;
 753	int				error;
 754
 755	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
 756			args->blkno, &bp);
 757	if (error)
 758		return error;
 759
 760	xfs_attr3_leaf_remove(bp, args);
 761
 762	forkoff = xfs_attr_shortform_allfit(bp, dp);
 763	if (forkoff)
 764		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
 765		/* bp is gone due to xfs_da_shrink_inode */
 766
 767	return error;
 768}
 769
 770/*
 771 * Shrink an attribute from leaf to shortform. Used by the node format remove
 772 * path when the node format collapses to a single block and so we have to check
 773 * if it can be collapsed further.
 774 */
 775static int
 776xfs_attr_leaf_shrink(
 777	struct xfs_da_args	*args)
 778{
 779	struct xfs_inode	*dp = args->dp;
 780	struct xfs_buf		*bp;
 781	int			forkoff;
 782	int			error;
 783
 784	if (!xfs_attr_is_leaf(dp))
 785		return 0;
 786
 787	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
 788	if (error)
 789		return error;
 790
 791	forkoff = xfs_attr_shortform_allfit(bp, dp);
 792	if (forkoff) {
 793		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
 794		/* bp is gone due to xfs_da_shrink_inode */
 795	} else {
 796		xfs_trans_brelse(args->trans, bp);
 797	}
 798
 799	return error;
 800}
 801
 802/*
 803 * Run the attribute operation specified in @attr.
 804 *
 805 * This routine is meant to function as a delayed operation and will set the
 806 * state to XFS_DAS_DONE when the operation is complete.  Calling functions will
 807 * need to handle this, and recall the function until either an error or
 808 * XFS_DAS_DONE is detected.
 809 */
 810int
 811xfs_attr_set_iter(
 812	struct xfs_attr_intent		*attr)
 
 
 813{
 814	struct xfs_da_args              *args = attr->xattri_da_args;
 815	int				error = 0;
 816
 817	/* State machine switch */
 818next_state:
 819	switch (attr->xattri_dela_state) {
 820	case XFS_DAS_UNINIT:
 821		ASSERT(0);
 822		return -EFSCORRUPTED;
 823	case XFS_DAS_SF_ADD:
 824		return xfs_attr_sf_addname(attr);
 825	case XFS_DAS_LEAF_ADD:
 826		return xfs_attr_leaf_addname(attr);
 827	case XFS_DAS_NODE_ADD:
 828		return xfs_attr_node_addname(attr);
 829
 830	case XFS_DAS_SF_REMOVE:
 831		error = xfs_attr_sf_removename(args);
 832		attr->xattri_dela_state = xfs_attr_complete_op(attr,
 833						xfs_attr_init_add_state(args));
 834		break;
 835	case XFS_DAS_LEAF_REMOVE:
 836		error = xfs_attr_leaf_removename(args);
 837		attr->xattri_dela_state = xfs_attr_complete_op(attr,
 838						xfs_attr_init_add_state(args));
 839		break;
 840	case XFS_DAS_NODE_REMOVE:
 841		error = xfs_attr_node_removename_setup(attr);
 842		if (error == -ENOATTR &&
 843		    (args->op_flags & XFS_DA_OP_RECOVERY)) {
 844			attr->xattri_dela_state = xfs_attr_complete_op(attr,
 845						xfs_attr_init_add_state(args));
 846			error = 0;
 847			break;
 848		}
 849		if (error)
 850			return error;
 851		attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
 852		if (args->rmtblkno == 0)
 853			attr->xattri_dela_state++;
 854		break;
 855
 856	case XFS_DAS_LEAF_SET_RMT:
 857	case XFS_DAS_NODE_SET_RMT:
 858		error = xfs_attr_rmtval_find_space(attr);
 859		if (error)
 860			return error;
 861		attr->xattri_dela_state++;
 862		fallthrough;
 863
 864	case XFS_DAS_LEAF_ALLOC_RMT:
 865	case XFS_DAS_NODE_ALLOC_RMT:
 866		error = xfs_attr_rmtval_alloc(attr);
 867		if (error)
 868			return error;
 869		if (attr->xattri_dela_state == XFS_DAS_DONE)
 870			break;
 871		goto next_state;
 872
 873	case XFS_DAS_LEAF_REPLACE:
 874	case XFS_DAS_NODE_REPLACE:
 875		/*
 876		 * We must "flip" the incomplete flags on the "new" and "old"
 877		 * attribute/value pairs so that one disappears and one appears
 878		 * atomically.
 879		 */
 880		error = xfs_attr3_leaf_flipflags(args);
 881		if (error)
 882			return error;
 883		/*
 884		 * We must commit the flag value change now to make it atomic
 885		 * and then we can start the next trans in series at REMOVE_OLD.
 886		 */
 887		attr->xattri_dela_state++;
 888		break;
 889
 890	case XFS_DAS_LEAF_REMOVE_OLD:
 891	case XFS_DAS_NODE_REMOVE_OLD:
 892		/*
 893		 * If we have a remote attr, start the process of removing it
 894		 * by invalidating any cached buffers.
 895		 *
 896		 * If we don't have a remote attr, we skip the remote block
 897		 * removal state altogether with a second state increment.
 898		 */
 899		xfs_attr_restore_rmt_blk(args);
 900		if (args->rmtblkno) {
 901			error = xfs_attr_rmtval_invalidate(args);
 902			if (error)
 903				return error;
 904		} else {
 905			attr->xattri_dela_state++;
 906		}
 907
 908		attr->xattri_dela_state++;
 909		goto next_state;
 910
 911	case XFS_DAS_LEAF_REMOVE_RMT:
 912	case XFS_DAS_NODE_REMOVE_RMT:
 913		error = xfs_attr_rmtval_remove(attr);
 914		if (error == -EAGAIN) {
 915			error = 0;
 916			break;
 917		}
 918		if (error)
 919			return error;
 920
 921		/*
 922		 * We've finished removing the remote attr blocks, so commit the
 923		 * transaction and move on to removing the attr name from the
 924		 * leaf/node block. Removing the attr might require a full
 925		 * transaction reservation for btree block freeing, so we
 926		 * can't do that in the same transaction where we removed the
 927		 * remote attr blocks.
 928		 */
 929		attr->xattri_dela_state++;
 930		break;
 931
 932	case XFS_DAS_LEAF_REMOVE_ATTR:
 933		error = xfs_attr_leaf_remove_attr(attr);
 934		attr->xattri_dela_state = xfs_attr_complete_op(attr,
 935						xfs_attr_init_add_state(args));
 936		break;
 937
 938	case XFS_DAS_NODE_REMOVE_ATTR:
 939		error = xfs_attr_node_remove_attr(attr);
 940		if (!error)
 941			error = xfs_attr_leaf_shrink(args);
 942		attr->xattri_dela_state = xfs_attr_complete_op(attr,
 943						xfs_attr_init_add_state(args));
 944		break;
 945	default:
 946		ASSERT(0);
 947		break;
 948	}
 949
 950	trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
 951	return error;
 952}
 953
 954
 955/*
 956 * Return EEXIST if attr is found, or ENOATTR if not
 957 */
 958static int
 959xfs_attr_lookup(
 960	struct xfs_da_args	*args)
 961{
 962	struct xfs_inode	*dp = args->dp;
 963	struct xfs_buf		*bp = NULL;
 964	struct xfs_da_state	*state;
 965	int			error;
 966
 967	if (!xfs_inode_hasattr(dp))
 968		return -ENOATTR;
 969
 970	if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) {
 971		if (xfs_attr_sf_findname(args))
 972			return -EEXIST;
 973		return -ENOATTR;
 974	}
 975
 976	/* Prerequisite for xfs_attr_is_leaf */
 977	error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
 978	if (error)
 979		return error;
 980
 981	if (xfs_attr_is_leaf(dp)) {
 982		error = xfs_attr_leaf_hasname(args, &bp);
 983
 984		if (bp)
 985			xfs_trans_brelse(args->trans, bp);
 986
 987		return error;
 988	}
 989
 990	state = xfs_da_state_alloc(args);
 991	error = xfs_attr_node_lookup(args, state);
 992	xfs_da_state_free(state);
 993	return error;
 994}
 995
 996int
 997xfs_attr_add_fork(
 998	struct xfs_inode	*ip,		/* incore inode pointer */
 999	int			size,		/* space new attribute needs */
1000	int			rsvd)		/* xact may use reserved blks */
1001{
1002	struct xfs_mount	*mp = ip->i_mount;
1003	struct xfs_trans	*tp;		/* transaction pointer */
1004	unsigned int		blks;		/* space reservation */
1005	int			error;		/* error return value */
1006
1007	if (xfs_is_metadir_inode(ip))
1008		ASSERT(XFS_IS_DQDETACHED(ip));
1009	else
1010		ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1011
1012	blks = XFS_ADDAFORK_SPACE_RES(mp);
1013
1014	error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
1015			rsvd, &tp);
1016	if (error)
1017		return error;
1018
1019	if (xfs_inode_has_attr_fork(ip))
1020		goto trans_cancel;
1021
1022	error = xfs_bmap_add_attrfork(tp, ip, size, rsvd);
1023	if (error)
1024		goto trans_cancel;
1025
1026	error = xfs_trans_commit(tp);
1027	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1028	return error;
1029
1030trans_cancel:
1031	xfs_trans_cancel(tp);
1032	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1033	return error;
1034}
1035
1036/*
1037 * Make a change to the xattr structure.
1038 *
1039 * The caller must have initialized @args, attached dquots, and must not hold
1040 * any ILOCKs.  Reserved data blocks may be used if @rsvd is set.
1041 *
1042 * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists.
1043 * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist.
1044 * Returns 0 on success, or a negative errno if something else went wrong.
1045 */
1046int
1047xfs_attr_set(
1048	struct xfs_da_args	*args,
1049	enum xfs_attr_update	op,
1050	bool			rsvd)
1051{
1052	struct xfs_inode	*dp = args->dp;
1053	struct xfs_mount	*mp = dp->i_mount;
1054	struct xfs_trans_res	tres;
1055	int			error, local;
1056	int			rmt_blks = 0;
1057	unsigned int		total = 0;
1058
1059	ASSERT(!args->trans);
1060
1061	switch (op) {
1062	case XFS_ATTRUPDATE_UPSERT:
1063	case XFS_ATTRUPDATE_CREATE:
1064	case XFS_ATTRUPDATE_REPLACE:
1065		XFS_STATS_INC(mp, xs_attr_set);
1066		args->total = xfs_attr_calc_size(args, &local);
1067
1068		/*
1069		 * If the inode doesn't have an attribute fork, add one.
1070		 * (inode must not be locked when we call this routine)
1071		 */
1072		if (xfs_inode_has_attr_fork(dp) == 0) {
1073			int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1074				xfs_attr_sf_entsize_byname(args->namelen,
1075						args->valuelen);
1076
1077			error = xfs_attr_add_fork(dp, sf_size, rsvd);
1078			if (error)
1079				return error;
1080		}
1081
1082		if (!local)
1083			rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1084
1085		tres = xfs_attr_set_resv(args);
1086		total = args->total;
1087		break;
1088	case XFS_ATTRUPDATE_REMOVE:
1089		XFS_STATS_INC(mp, xs_attr_remove);
1090		rmt_blks = xfs_attr3_max_rmt_blocks(mp);
1091		tres = M_RES(mp)->tr_attrrm;
1092		total = XFS_ATTRRM_SPACE_RES(mp);
1093		break;
1094	}
1095
1096	/*
1097	 * Root fork attributes can use reserved data blocks for this
1098	 * operation if necessary
1099	 */
1100	error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
 
 
 
1101	if (error)
1102		return error;
1103
1104	if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(dp)) {
1105		error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK,
1106				XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1107		if (error)
1108			goto out_trans_cancel;
 
 
 
 
 
 
 
 
 
 
 
1109	}
1110
1111	error = xfs_attr_lookup(args);
1112	switch (error) {
1113	case -EEXIST:
1114		if (op == XFS_ATTRUPDATE_REMOVE) {
1115			/* if no value, we are performing a remove operation */
1116			xfs_attr_defer_add(args, XFS_ATTR_DEFER_REMOVE);
1117			break;
1118		}
1119
1120		/* Pure create fails if the attr already exists */
1121		if (op == XFS_ATTRUPDATE_CREATE)
1122			goto out_trans_cancel;
1123		xfs_attr_defer_add(args, XFS_ATTR_DEFER_REPLACE);
1124		break;
1125	case -ENOATTR:
1126		/* Can't remove what isn't there. */
1127		if (op == XFS_ATTRUPDATE_REMOVE)
1128			goto out_trans_cancel;
1129
1130		/* Pure replace fails if no existing attr to replace. */
1131		if (op == XFS_ATTRUPDATE_REPLACE)
1132			goto out_trans_cancel;
1133		xfs_attr_defer_add(args, XFS_ATTR_DEFER_SET);
1134		break;
1135	default:
1136		goto out_trans_cancel;
1137	}
1138
1139	/*
1140	 * If this is a synchronous mount, make sure that the
1141	 * transaction goes to disk before returning to the user.
1142	 */
1143	if (xfs_has_wsync(mp))
1144		xfs_trans_set_sync(args->trans);
1145
1146	xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
 
1147
1148	/*
1149	 * Commit the last in the sequence of transactions.
1150	 */
1151	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1152	error = xfs_trans_commit(args->trans);
1153out_unlock:
1154	xfs_iunlock(dp, XFS_ILOCK_EXCL);
1155	args->trans = NULL;
1156	return error;
1157
1158out_trans_cancel:
1159	if (args->trans)
1160		xfs_trans_cancel(args->trans);
1161	goto out_unlock;
 
1162}
1163
1164/*========================================================================
1165 * External routines when attribute list is inside the inode
1166 *========================================================================*/
1167
1168int xfs_attr_sf_totsize(struct xfs_inode *dp)
1169{
1170	struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
1171
1172	return be16_to_cpu(sf->totsize);
1173}
1174
1175/*
1176 * Add a name to the shortform attribute list structure
1177 * This is the external routine.
1178 */
1179static int
1180xfs_attr_shortform_addname(
1181	struct xfs_da_args	*args)
1182{
1183	int			newsize, forkoff;
1184
1185	trace_xfs_attr_sf_addname(args);
1186
1187	if (xfs_attr_sf_findname(args)) {
1188		int		error;
1189
1190		ASSERT(args->op_flags & XFS_DA_OP_REPLACE);
1191
1192		error = xfs_attr_sf_removename(args);
1193		if (error)
1194			return error;
1195
1196		/*
1197		 * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1198		 * so that the new attr doesn't fit in shortform format, the
1199		 * leaf format add routine won't trip over the attr not being
1200		 * around.
1201		 */
1202		args->op_flags &= ~XFS_DA_OP_REPLACE;
1203	} else {
1204		ASSERT(!(args->op_flags & XFS_DA_OP_REPLACE));
1205	}
1206
1207	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1208	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1209		return -ENOSPC;
1210
1211	newsize = xfs_attr_sf_totsize(args->dp);
1212	newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1213
1214	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1215	if (!forkoff)
1216		return -ENOSPC;
1217
1218	xfs_attr_shortform_add(args, forkoff);
1219	return 0;
1220}
1221
1222
1223/*========================================================================
1224 * External routines when attribute list is one block
1225 *========================================================================*/
1226
1227/*
1228 * Return EEXIST if attr is found, or ENOATTR if not
 
 
 
1229 */
1230STATIC int
1231xfs_attr_leaf_hasname(
1232	struct xfs_da_args	*args,
1233	struct xfs_buf		**bp)
1234{
1235	int                     error = 0;
 
 
 
 
1236
1237	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, bp);
 
 
 
 
 
1238	if (error)
1239		return error;
1240
1241	error = xfs_attr3_leaf_lookup_int(*bp, args);
1242	if (error != -ENOATTR && error != -EEXIST)
1243		xfs_trans_brelse(args->trans, *bp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1245	return error;
1246}
1247
1248/*
1249 * Remove a name from the leaf attribute list structure
1250 *
1251 * This leaf block cannot have a "remote" value, we only call this routine
1252 * if bmap_one_block() says there is only one block (ie: no remote blks).
1253 */
1254STATIC int
1255xfs_attr_leaf_removename(
1256	struct xfs_da_args	*args)
1257{
1258	struct xfs_inode	*dp;
1259	struct xfs_buf		*bp;
1260	int			error, forkoff;
1261
1262	trace_xfs_attr_leaf_removename(args);
1263
1264	/*
1265	 * Remove the attribute.
1266	 */
1267	dp = args->dp;
 
 
 
 
1268
1269	error = xfs_attr_leaf_hasname(args, &bp);
1270	if (error == -ENOATTR) {
1271		xfs_trans_brelse(args->trans, bp);
1272		if (args->op_flags & XFS_DA_OP_RECOVERY)
1273			return 0;
1274		return error;
1275	} else if (error != -EEXIST)
1276		return error;
 
1277
1278	xfs_attr3_leaf_remove(bp, args);
1279
1280	/*
1281	 * If the result is small enough, shrink it all into the inode.
1282	 */
1283	forkoff = xfs_attr_shortform_allfit(bp, dp);
1284	if (forkoff)
1285		return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1286		/* bp is gone due to xfs_da_shrink_inode */
1287
 
 
 
 
 
 
1288	return 0;
 
 
 
1289}
1290
1291/*
1292 * Look up a name in a leaf attribute list structure.
1293 *
1294 * This leaf block cannot have a "remote" value, we only call this routine
1295 * if bmap_one_block() says there is only one block (ie: no remote blks).
1296 *
1297 * Returns 0 on successful retrieval, otherwise an error.
1298 */
1299STATIC int
1300xfs_attr_leaf_get(xfs_da_args_t *args)
1301{
1302	struct xfs_buf *bp;
1303	int error;
1304
1305	trace_xfs_attr_leaf_get(args);
1306
1307	error = xfs_attr_leaf_hasname(args, &bp);
 
 
 
1308
1309	if (error == -ENOATTR)  {
 
1310		xfs_trans_brelse(args->trans, bp);
1311		return error;
1312	} else if (error != -EEXIST)
1313		return error;
1314
1315
1316	error = xfs_attr3_leaf_getvalue(bp, args);
1317	xfs_trans_brelse(args->trans, bp);
 
 
 
1318	return error;
1319}
1320
1321/* Return EEXIST if attr is found, or ENOATTR if not. */
1322STATIC int
1323xfs_attr_node_lookup(
1324	struct xfs_da_args	*args,
1325	struct xfs_da_state	*state)
1326{
1327	int			retval, error;
1328
1329	/*
1330	 * Search to see if name exists, and get back a pointer to it.
1331	 */
1332	error = xfs_da3_node_lookup_int(state, &retval);
1333	if (error)
1334		return error;
1335
1336	return retval;
1337}
1338
1339/*========================================================================
1340 * External routines when attribute list size > geo->blksize
1341 *========================================================================*/
1342
 
 
 
 
 
 
 
 
 
 
1343STATIC int
1344xfs_attr_node_addname_find_attr(
1345	 struct xfs_attr_intent	*attr)
1346{
1347	struct xfs_da_args	*args = attr->xattri_da_args;
1348	int			error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1349
1350	/*
1351	 * Search to see if name already exists, and get back a pointer
1352	 * to where it should go.
1353	 */
1354	xfs_attr_item_init_da_state(attr);
1355	error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1356	switch (error) {
1357	case -ENOATTR:
1358		if (args->op_flags & XFS_DA_OP_REPLACE)
1359			goto error;
1360		break;
1361	case -EEXIST:
1362		if (!(args->op_flags & XFS_DA_OP_REPLACE))
1363			goto error;
 
 
1364
 
 
 
 
 
 
 
1365
1366		trace_xfs_attr_node_replace(args);
1367		/*
1368		 * Save the existing remote attr state so that the current
1369		 * values reflect the state of the new attribute we are about to
1370		 * add, not the attribute we just found and will remove later.
1371		 */
1372		xfs_attr_save_rmt_blk(args);
1373		break;
1374	case 0:
1375		break;
1376	default:
1377		goto error;
1378	}
1379
1380	return 0;
1381error:
1382	if (attr->xattri_da_state) {
1383		xfs_da_state_free(attr->xattri_da_state);
1384		attr->xattri_da_state = NULL;
1385	}
1386	return error;
1387}
1388
1389/*
1390 * Add a name to a Btree-format attribute list.
1391 *
1392 * This will involve walking down the Btree, and may involve splitting leaf
1393 * nodes and even splitting intermediate nodes up to and including the root
1394 * node (a special case of an intermediate node).
1395 *
1396 * If the tree was still in single leaf format and needs to converted to
1397 * real node format return 1 and let the caller handle that.
1398 */
1399static int
1400xfs_attr_node_try_addname(
1401	struct xfs_attr_intent		*attr)
1402{
1403	struct xfs_da_state		*state = attr->xattri_da_state;
1404	struct xfs_da_state_blk		*blk;
1405	int				error = 0;
1406
1407	trace_xfs_attr_node_addname(state->args);
1408
1409	blk = &state->path.blk[state->path.active-1];
1410	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1411
1412	if (!xfs_attr3_leaf_add(blk->bp, state->args)) {
1413		if (state->path.active == 1) {
1414			/*
1415			 * Its really a single leaf node, but it had
1416			 * out-of-line values so it looked like it *might*
1417			 * have been a b-tree. Let the caller deal with this.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1418			 */
1419			error = 1;
1420			goto out;
 
 
 
1421		}
1422
1423		/*
1424		 * Split as many Btree elements as required.
1425		 * This code tracks the new and old attr's location
1426		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1427		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1428		 */
 
1429		error = xfs_da3_split(state);
1430		if (error)
1431			goto out;
 
 
 
 
1432	} else {
1433		/*
1434		 * Addition succeeded, update Btree hashvals.
1435		 */
1436		xfs_da3_fixhashpath(state, &state->path);
1437	}
1438
1439out:
1440	xfs_da_state_free(state);
1441	attr->xattri_da_state = NULL;
1442	return error;
1443}
1444
1445static int
1446xfs_attr_node_removename(
1447	struct xfs_da_args	*args,
1448	struct xfs_da_state	*state)
1449{
1450	struct xfs_da_state_blk	*blk;
1451	int			retval;
1452
1453	/*
1454	 * Remove the name and update the hashvals in the tree.
 
1455	 */
1456	blk = &state->path.blk[state->path.active-1];
1457	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1458	retval = xfs_attr3_leaf_remove(blk->bp, args);
1459	xfs_da3_fixhashpath(state, &state->path);
1460
1461	return retval;
1462}
1463
1464static int
1465xfs_attr_node_remove_attr(
1466	struct xfs_attr_intent		*attr)
1467{
1468	struct xfs_da_args		*args = attr->xattri_da_args;
1469	struct xfs_da_state		*state = xfs_da_state_alloc(args);
1470	int				retval = 0;
1471	int				error = 0;
1472
1473	/*
1474	 * The attr we are removing has already been marked incomplete, so
1475	 * we need to set the filter appropriately to re-find the "old"
1476	 * attribute entry after any split ops.
1477	 */
1478	args->attr_filter |= XFS_ATTR_INCOMPLETE;
1479	error = xfs_da3_node_lookup_int(state, &retval);
1480	if (error)
1481		goto out;
1482
1483	error = xfs_attr_node_removename(args, state);
 
 
 
 
 
 
 
 
 
 
1484
1485	/*
1486	 * Check to see if the tree needs to be collapsed.
 
 
 
1487	 */
1488	if (retval && (state->path.active > 1)) {
1489		error = xfs_da3_join(state);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1490		if (error)
1491			goto out;
1492	}
1493	retval = error = 0;
1494
1495out:
1496	xfs_da_state_free(state);
 
1497	if (error)
1498		return error;
1499	return retval;
 
 
 
1500}
1501
1502/*
1503 * Retrieve the attribute data from a node attribute list.
1504 *
1505 * This routine gets called for any attribute fork that has more than one
1506 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1507 * "remote" values taking up more blocks.
1508 *
1509 * Returns 0 on successful retrieval, otherwise an error.
1510 */
1511STATIC int
1512xfs_attr_node_get(
1513	struct xfs_da_args	*args)
1514{
1515	struct xfs_da_state	*state;
1516	struct xfs_da_state_blk	*blk;
1517	int			i;
1518	int			error;
 
 
 
1519
1520	trace_xfs_attr_node_get(args);
 
 
 
 
 
 
1521
1522	/*
1523	 * Search to see if name exists, and get back a pointer to it.
1524	 */
1525	state = xfs_da_state_alloc(args);
1526	error = xfs_attr_node_lookup(args, state);
1527	if (error != -EEXIST)
1528		goto out_release;
 
 
1529
1530	/*
1531	 * Get the value, local or "remote"
 
 
1532	 */
1533	blk = &state->path.blk[state->path.active - 1];
1534	error = xfs_attr3_leaf_getvalue(blk->bp, args);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1535
1536	/*
1537	 * If not in a transaction, we have to release all the buffers.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1538	 */
1539out_release:
1540	for (i = 0; i < state->path.active; i++) {
1541		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1542		state->path.blk[i].bp = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1543	}
 
1544
 
1545	xfs_da_state_free(state);
1546	return error;
 
 
 
1547}
1548
1549/* Enforce that there is at most one namespace bit per attr. */
1550inline bool xfs_attr_check_namespace(unsigned int attr_flags)
 
 
 
 
 
 
1551{
1552	return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1553}
1554
1555/* Returns true if the attribute entry name is valid. */
1556bool
1557xfs_attr_namecheck(
1558	unsigned int	attr_flags,
1559	const void	*name,
1560	size_t		length)
 
 
1561{
1562	/* Only one namespace bit allowed. */
1563	if (!xfs_attr_check_namespace(attr_flags))
1564		return false;
 
 
1565
1566	/*
1567	 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1568	 * out, so use >= for the length check.
1569	 */
1570	if (length >= MAXNAMELEN)
1571		return false;
 
 
 
 
 
 
 
 
 
 
 
 
1572
1573	/* Parent pointers have their own validation. */
1574	if (attr_flags & XFS_ATTR_PARENT)
1575		return xfs_parent_namecheck(attr_flags, name, length);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1576
1577	/* There shouldn't be any nulls here */
1578	return !memchr(name, 0, length);
1579}
1580
1581int __init
1582xfs_attr_intent_init_cache(void)
 
 
 
 
 
 
 
1583{
1584	xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1585			sizeof(struct xfs_attr_intent),
1586			0, 0, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1587
1588	return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1589}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1590
1591void
1592xfs_attr_intent_destroy_cache(void)
1593{
1594	kmem_cache_destroy(xfs_attr_intent_cache);
1595	xfs_attr_intent_cache = NULL;
1596}