Linux Audio

Check our new training course

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