PageRenderTime 11ms CodeModel.GetById 3ms app.highlight 96ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/xfs/xfs_attr.c

https://bitbucket.org/evzijst/gittest
C | 2660 lines | 1750 code | 268 blank | 642 comment | 377 complexity | 3a9f8f69ef4e0203c1cddc99f3952426 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of version 2 of the GNU General Public License as
   6 * published by the Free Software Foundation.
   7 *
   8 * This program is distributed in the hope that it would be useful, but
   9 * WITHOUT ANY WARRANTY; without even the implied warranty of
  10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11 *
  12 * Further, this software is distributed without any warranty that it is
  13 * free of the rightful claim of any third person regarding infringement
  14 * or the like.  Any license provided herein, whether implied or
  15 * otherwise, applies only to this software file.  Patent licenses, if
  16 * any, provided herein do not apply to combinations of this program with
  17 * other software, or any other product whatsoever.
  18 *
  19 * You should have received a copy of the GNU General Public License along
  20 * with this program; if not, write the Free Software Foundation, Inc., 59
  21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22 *
  23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24 * Mountain View, CA  94043, or:
  25 *
  26 * http://www.sgi.com
  27 *
  28 * For further information regarding this notice, see:
  29 *
  30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31 */
  32
  33#include "xfs.h"
  34
  35#include "xfs_macros.h"
  36#include "xfs_types.h"
  37#include "xfs_inum.h"
  38#include "xfs_log.h"
  39#include "xfs_trans.h"
  40#include "xfs_sb.h"
  41#include "xfs_ag.h"
  42#include "xfs_dir.h"
  43#include "xfs_dir2.h"
  44#include "xfs_dmapi.h"
  45#include "xfs_mount.h"
  46#include "xfs_alloc_btree.h"
  47#include "xfs_bmap_btree.h"
  48#include "xfs_ialloc_btree.h"
  49#include "xfs_alloc.h"
  50#include "xfs_btree.h"
  51#include "xfs_attr_sf.h"
  52#include "xfs_dir_sf.h"
  53#include "xfs_dir2_sf.h"
  54#include "xfs_dinode.h"
  55#include "xfs_inode_item.h"
  56#include "xfs_inode.h"
  57#include "xfs_bmap.h"
  58#include "xfs_da_btree.h"
  59#include "xfs_attr.h"
  60#include "xfs_attr_leaf.h"
  61#include "xfs_error.h"
  62#include "xfs_bit.h"
  63#include "xfs_quota.h"
  64#include "xfs_rw.h"
  65#include "xfs_trans_space.h"
  66#include "xfs_acl.h"
  67
  68/*
  69 * xfs_attr.c
  70 *
  71 * Provide the external interfaces to manage attribute lists.
  72 */
  73
  74/*========================================================================
  75 * Function prototypes for the kernel.
  76 *========================================================================*/
  77
  78/*
  79 * Internal routines when attribute list fits inside the inode.
  80 */
  81STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  82
  83/*
  84 * Internal routines when attribute list is one block.
  85 */
  86STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  87STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  88STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  89
  90/*
  91 * Internal routines when attribute list is more than one block.
  92 */
  93STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  94STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  95STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
  96STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  97STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  98
  99/*
 100 * Routines to manipulate out-of-line attribute values.
 101 */
 102STATIC int xfs_attr_rmtval_get(xfs_da_args_t *args);
 103STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
 104STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
 105
 106#define ATTR_RMTVALUE_MAPSIZE	1	/* # of map entries at once */
 107
 108#if defined(XFS_ATTR_TRACE)
 109ktrace_t *xfs_attr_trace_buf;
 110#endif
 111
 112
 113/*========================================================================
 114 * Overall external interface routines.
 115 *========================================================================*/
 116
 117int
 118xfs_attr_fetch(xfs_inode_t *ip, char *name, int namelen,
 119	       char *value, int *valuelenp, int flags, struct cred *cred)
 120{
 121	xfs_da_args_t   args;
 122	int             error;
 123
 124	if ((XFS_IFORK_Q(ip) == 0) ||
 125	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 126	     ip->i_d.di_anextents == 0))
 127		return(ENOATTR);
 128
 129	if (!(flags & (ATTR_KERNACCESS|ATTR_SECURE))) {
 130		if ((error = xfs_iaccess(ip, S_IRUSR, cred)))
 131			return(XFS_ERROR(error));
 132	}
 133
 134	/*
 135	 * Fill in the arg structure for this request.
 136	 */
 137	memset((char *)&args, 0, sizeof(args));
 138	args.name = name;
 139	args.namelen = namelen;
 140	args.value = value;
 141	args.valuelen = *valuelenp;
 142	args.flags = flags;
 143	args.hashval = xfs_da_hashname(args.name, args.namelen);
 144	args.dp = ip;
 145	args.whichfork = XFS_ATTR_FORK;
 146
 147	/*
 148	 * Decide on what work routines to call based on the inode size.
 149	 */
 150	if (XFS_IFORK_Q(ip) == 0 ||
 151	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 152	     ip->i_d.di_anextents == 0)) {
 153		error = XFS_ERROR(ENOATTR);
 154	} else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
 155		error = xfs_attr_shortform_getvalue(&args);
 156	} else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
 157		error = xfs_attr_leaf_get(&args);
 158	} else {
 159		error = xfs_attr_node_get(&args);
 160	}
 161
 162	/*
 163	 * Return the number of bytes in the value to the caller.
 164	 */
 165	*valuelenp = args.valuelen;
 166
 167	if (error == EEXIST)
 168		error = 0;
 169	return(error);
 170}
 171
 172int
 173xfs_attr_get(bhv_desc_t *bdp, char *name, char *value, int *valuelenp,
 174	     int flags, struct cred *cred)
 175{
 176	xfs_inode_t	*ip = XFS_BHVTOI(bdp);
 177	int		error, namelen;
 178
 179	XFS_STATS_INC(xs_attr_get);
 180
 181	if (!name)
 182		return(EINVAL);
 183	namelen = strlen(name);
 184	if (namelen >= MAXNAMELEN)
 185		return(EFAULT);		/* match IRIX behaviour */
 186
 187	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
 188		return(EIO);
 189
 190	xfs_ilock(ip, XFS_ILOCK_SHARED);
 191	error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
 192	xfs_iunlock(ip, XFS_ILOCK_SHARED);
 193	return(error);
 194}
 195
 196/*ARGSUSED*/
 197int								/* error */
 198xfs_attr_set(bhv_desc_t *bdp, char *name, char *value, int valuelen, int flags,
 199		     struct cred *cred)
 200{
 201	xfs_da_args_t	args;
 202	xfs_inode_t	*dp;
 203	xfs_fsblock_t	firstblock;
 204	xfs_bmap_free_t flist;
 205	int		error, err2, committed;
 206	int		local, size;
 207	uint		nblks;
 208	xfs_mount_t	*mp;
 209	int             rsvd = (flags & ATTR_ROOT) != 0;
 210	int             namelen;
 211
 212	namelen = strlen(name);
 213	if (namelen >= MAXNAMELEN)
 214		return EFAULT;		/* match IRIX behaviour */
 215
 216	XFS_STATS_INC(xs_attr_set);
 217
 218	dp = XFS_BHVTOI(bdp);
 219	mp = dp->i_mount;
 220	if (XFS_FORCED_SHUTDOWN(mp))
 221		return (EIO);
 222
 223	xfs_ilock(dp, XFS_ILOCK_SHARED);
 224	if (!(flags & ATTR_SECURE) &&
 225	     (error = xfs_iaccess(dp, S_IWUSR, cred))) {
 226		xfs_iunlock(dp, XFS_ILOCK_SHARED);
 227		return(XFS_ERROR(error));
 228	}
 229	xfs_iunlock(dp, XFS_ILOCK_SHARED);
 230
 231	/*
 232	 * Attach the dquots to the inode.
 233	 */
 234	if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
 235		return (error);
 236
 237	/*
 238	 * If the inode doesn't have an attribute fork, add one.
 239	 * (inode must not be locked when we call this routine)
 240	 */
 241	if (XFS_IFORK_Q(dp) == 0) {
 242		error = xfs_bmap_add_attrfork(dp, rsvd);
 243		if (error)
 244			return(error);
 245	}
 246
 247	/*
 248	 * Fill in the arg structure for this request.
 249	 */
 250	memset((char *)&args, 0, sizeof(args));
 251	args.name = name;
 252	args.namelen = namelen;
 253	args.value = value;
 254	args.valuelen = valuelen;
 255	args.flags = flags;
 256	args.hashval = xfs_da_hashname(args.name, args.namelen);
 257	args.dp = dp;
 258	args.firstblock = &firstblock;
 259	args.flist = &flist;
 260	args.whichfork = XFS_ATTR_FORK;
 261	args.oknoent = 1;
 262
 263	/* Determine space new attribute will use, and if it will be inline
 264	 * or out of line.
 265	 */
 266	size = xfs_attr_leaf_newentsize(&args, mp->m_sb.sb_blocksize, &local);
 267
 268	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
 269	if (local) {
 270		if (size > (mp->m_sb.sb_blocksize >> 1)) {
 271			/* Double split possible */
 272			nblks <<= 1;
 273		}
 274	} else {
 275		uint	dblocks = XFS_B_TO_FSB(mp, valuelen);
 276		/* Out of line attribute, cannot double split, but make
 277		 * room for the attribute value itself.
 278		 */
 279		nblks += dblocks;
 280		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
 281	}
 282
 283	/* Size is now blocks for attribute data */
 284	args.total = nblks;
 285
 286	/*
 287	 * Start our first transaction of the day.
 288	 *
 289	 * All future transactions during this code must be "chained" off
 290	 * this one via the trans_dup() call.  All transactions will contain
 291	 * the inode, and the inode will always be marked with trans_ihold().
 292	 * Since the inode will be locked in all transactions, we must log
 293	 * the inode in every transaction to let it float upward through
 294	 * the log.
 295	 */
 296	args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
 297
 298	/*
 299	 * Root fork attributes can use reserved data blocks for this
 300	 * operation if necessary
 301	 */
 302
 303	if (rsvd)
 304		args.trans->t_flags |= XFS_TRANS_RESERVE;
 305
 306	if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
 307				      XFS_ATTRSET_LOG_RES(mp, nblks),
 308				      0, XFS_TRANS_PERM_LOG_RES,
 309				      XFS_ATTRSET_LOG_COUNT))) {
 310		xfs_trans_cancel(args.trans, 0);
 311		return(error);
 312	}
 313	xfs_ilock(dp, XFS_ILOCK_EXCL);
 314
 315	error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
 316			 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
 317				XFS_QMOPT_RES_REGBLKS);
 318	if (error) {
 319		xfs_iunlock(dp, XFS_ILOCK_EXCL);
 320		xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
 321		return (error);
 322	}
 323
 324	xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
 325	xfs_trans_ihold(args.trans, dp);
 326
 327	/*
 328	 * If the attribute list is non-existant or a shortform list,
 329	 * upgrade it to a single-leaf-block attribute list.
 330	 */
 331	if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
 332	    ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
 333	     (dp->i_d.di_anextents == 0))) {
 334
 335		/*
 336		 * Build initial attribute list (if required).
 337		 */
 338		if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
 339			(void)xfs_attr_shortform_create(&args);
 340
 341		/*
 342		 * Try to add the attr to the attribute list in
 343		 * the inode.
 344		 */
 345		error = xfs_attr_shortform_addname(&args);
 346		if (error != ENOSPC) {
 347			/*
 348			 * Commit the shortform mods, and we're done.
 349			 * NOTE: this is also the error path (EEXIST, etc).
 350			 */
 351			ASSERT(args.trans != NULL);
 352
 353			/*
 354			 * If this is a synchronous mount, make sure that
 355			 * the transaction goes to disk before returning
 356			 * to the user.
 357			 */
 358			if (mp->m_flags & XFS_MOUNT_WSYNC) {
 359				xfs_trans_set_sync(args.trans);
 360			}
 361			err2 = xfs_trans_commit(args.trans,
 362						 XFS_TRANS_RELEASE_LOG_RES,
 363						 NULL);
 364			xfs_iunlock(dp, XFS_ILOCK_EXCL);
 365
 366			/*
 367			 * Hit the inode change time.
 368			 */
 369			if (!error && (flags & ATTR_KERNOTIME) == 0) {
 370				xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
 371			}
 372			return(error == 0 ? err2 : error);
 373		}
 374
 375		/*
 376		 * It won't fit in the shortform, transform to a leaf block.
 377		 * GROT: another possible req'mt for a double-split btree op.
 378		 */
 379		XFS_BMAP_INIT(args.flist, args.firstblock);
 380		error = xfs_attr_shortform_to_leaf(&args);
 381		if (!error) {
 382			error = xfs_bmap_finish(&args.trans, args.flist,
 383						*args.firstblock, &committed);
 384		}
 385		if (error) {
 386			ASSERT(committed);
 387			args.trans = NULL;
 388			xfs_bmap_cancel(&flist);
 389			goto out;
 390		}
 391
 392		/*
 393		 * bmap_finish() may have committed the last trans and started
 394		 * a new one.  We need the inode to be in all transactions.
 395		 */
 396		if (committed) {
 397			xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
 398			xfs_trans_ihold(args.trans, dp);
 399		}
 400
 401		/*
 402		 * Commit the leaf transformation.  We'll need another (linked)
 403		 * transaction to add the new attribute to the leaf.
 404		 */
 405		if ((error = xfs_attr_rolltrans(&args.trans, dp)))
 406			goto out;
 407
 408	}
 409
 410	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
 411		error = xfs_attr_leaf_addname(&args);
 412	} else {
 413		error = xfs_attr_node_addname(&args);
 414	}
 415	if (error) {
 416		goto out;
 417	}
 418
 419	/*
 420	 * If this is a synchronous mount, make sure that the
 421	 * transaction goes to disk before returning to the user.
 422	 */
 423	if (mp->m_flags & XFS_MOUNT_WSYNC) {
 424		xfs_trans_set_sync(args.trans);
 425	}
 426
 427	/*
 428	 * Commit the last in the sequence of transactions.
 429	 */
 430	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
 431	error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
 432				 NULL);
 433	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 434
 435	/*
 436	 * Hit the inode change time.
 437	 */
 438	if (!error && (flags & ATTR_KERNOTIME) == 0) {
 439		xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
 440	}
 441
 442	return(error);
 443
 444out:
 445	if (args.trans)
 446		xfs_trans_cancel(args.trans,
 447			XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
 448	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 449	return(error);
 450}
 451
 452/*
 453 * Generic handler routine to remove a name from an attribute list.
 454 * Transitions attribute list from Btree to shortform as necessary.
 455 */
 456/*ARGSUSED*/
 457int								/* error */
 458xfs_attr_remove(bhv_desc_t *bdp, char *name, int flags, struct cred *cred)
 459{
 460	xfs_da_args_t       args;
 461	xfs_inode_t         *dp;
 462	xfs_fsblock_t       firstblock;
 463	xfs_bmap_free_t     flist;
 464	int                 error;
 465	xfs_mount_t         *mp;
 466	int                 namelen;
 467
 468	ASSERT(MAXNAMELEN-1<=0xff); /* length is stored in uint8 */
 469	namelen = strlen(name);
 470	if (namelen>=MAXNAMELEN)
 471		return EFAULT; /* match irix behaviour */
 472
 473	XFS_STATS_INC(xs_attr_remove);
 474
 475	dp = XFS_BHVTOI(bdp);
 476	mp = dp->i_mount;
 477	if (XFS_FORCED_SHUTDOWN(mp))
 478		return (EIO);
 479
 480	xfs_ilock(dp, XFS_ILOCK_SHARED);
 481	if (!(flags & ATTR_SECURE) &&
 482	     (error = xfs_iaccess(dp, S_IWUSR, cred))) {
 483		xfs_iunlock(dp, XFS_ILOCK_SHARED);
 484		return(XFS_ERROR(error));
 485	} else if (XFS_IFORK_Q(dp) == 0 ||
 486		   (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 487		    dp->i_d.di_anextents == 0)) {
 488		xfs_iunlock(dp, XFS_ILOCK_SHARED);
 489		return(XFS_ERROR(ENOATTR));
 490	}
 491	xfs_iunlock(dp, XFS_ILOCK_SHARED);
 492
 493	/*
 494	 * Fill in the arg structure for this request.
 495	 */
 496	memset((char *)&args, 0, sizeof(args));
 497	args.name = name;
 498	args.namelen = namelen;
 499	args.flags = flags;
 500	args.hashval = xfs_da_hashname(args.name, args.namelen);
 501	args.dp = dp;
 502	args.firstblock = &firstblock;
 503	args.flist = &flist;
 504	args.total = 0;
 505	args.whichfork = XFS_ATTR_FORK;
 506
 507	/*
 508	 * Attach the dquots to the inode.
 509	 */
 510	if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
 511		return (error);
 512
 513	/*
 514	 * Start our first transaction of the day.
 515	 *
 516	 * All future transactions during this code must be "chained" off
 517	 * this one via the trans_dup() call.  All transactions will contain
 518	 * the inode, and the inode will always be marked with trans_ihold().
 519	 * Since the inode will be locked in all transactions, we must log
 520	 * the inode in every transaction to let it float upward through
 521	 * the log.
 522	 */
 523	args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
 524
 525	/*
 526	 * Root fork attributes can use reserved data blocks for this
 527	 * operation if necessary
 528	 */
 529
 530	if (flags & ATTR_ROOT)
 531		args.trans->t_flags |= XFS_TRANS_RESERVE;
 532
 533	if ((error = xfs_trans_reserve(args.trans,
 534				      XFS_ATTRRM_SPACE_RES(mp),
 535				      XFS_ATTRRM_LOG_RES(mp),
 536				      0, XFS_TRANS_PERM_LOG_RES,
 537				      XFS_ATTRRM_LOG_COUNT))) {
 538		xfs_trans_cancel(args.trans, 0);
 539		return(error);
 540
 541	}
 542
 543	xfs_ilock(dp, XFS_ILOCK_EXCL);
 544	/*
 545	 * No need to make quota reservations here. We expect to release some
 546	 * blocks not allocate in the common case.
 547	 */
 548	xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
 549	xfs_trans_ihold(args.trans, dp);
 550
 551	/*
 552	 * Decide on what work routines to call based on the inode size.
 553	 */
 554	if (XFS_IFORK_Q(dp) == 0 ||
 555	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 556	     dp->i_d.di_anextents == 0)) {
 557		error = XFS_ERROR(ENOATTR);
 558		goto out;
 559	}
 560	if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
 561		ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
 562		error = xfs_attr_shortform_remove(&args);
 563		if (error) {
 564			goto out;
 565		}
 566	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
 567		error = xfs_attr_leaf_removename(&args);
 568	} else {
 569		error = xfs_attr_node_removename(&args);
 570	}
 571	if (error) {
 572		goto out;
 573	}
 574
 575	/*
 576	 * If this is a synchronous mount, make sure that the
 577	 * transaction goes to disk before returning to the user.
 578	 */
 579	if (mp->m_flags & XFS_MOUNT_WSYNC) {
 580		xfs_trans_set_sync(args.trans);
 581	}
 582
 583	/*
 584	 * Commit the last in the sequence of transactions.
 585	 */
 586	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
 587	error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
 588				 NULL);
 589	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 590
 591	/*
 592	 * Hit the inode change time.
 593	 */
 594	if (!error && (flags & ATTR_KERNOTIME) == 0) {
 595		xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
 596	}
 597
 598	return(error);
 599
 600out:
 601	if (args.trans)
 602		xfs_trans_cancel(args.trans,
 603			XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
 604	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 605	return(error);
 606}
 607
 608/*
 609 * Generate a list of extended attribute names and optionally
 610 * also value lengths.  Positive return value follows the XFS
 611 * convention of being an error, zero or negative return code
 612 * is the length of the buffer returned (negated), indicating
 613 * success.
 614 */
 615int
 616xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
 617		      attrlist_cursor_kern_t *cursor, struct cred *cred)
 618{
 619	xfs_attr_list_context_t context;
 620	xfs_inode_t *dp;
 621	int error;
 622
 623	XFS_STATS_INC(xs_attr_list);
 624
 625	/*
 626	 * Validate the cursor.
 627	 */
 628	if (cursor->pad1 || cursor->pad2)
 629		return(XFS_ERROR(EINVAL));
 630	if ((cursor->initted == 0) &&
 631	    (cursor->hashval || cursor->blkno || cursor->offset))
 632		return(XFS_ERROR(EINVAL));
 633
 634	/*
 635	 * Check for a properly aligned buffer.
 636	 */
 637	if (((long)buffer) & (sizeof(int)-1))
 638		return(XFS_ERROR(EFAULT));
 639	if (flags & ATTR_KERNOVAL)
 640		bufsize = 0;
 641
 642	/*
 643	 * Initialize the output buffer.
 644	 */
 645	context.dp = dp = XFS_BHVTOI(bdp);
 646	context.cursor = cursor;
 647	context.count = 0;
 648	context.dupcnt = 0;
 649	context.resynch = 1;
 650	context.flags = flags;
 651	if (!(flags & ATTR_KERNAMELS)) {
 652		context.bufsize = (bufsize & ~(sizeof(int)-1));  /* align */
 653		context.firstu = context.bufsize;
 654		context.alist = (attrlist_t *)buffer;
 655		context.alist->al_count = 0;
 656		context.alist->al_more = 0;
 657		context.alist->al_offset[0] = context.bufsize;
 658	}
 659	else {
 660		context.bufsize = bufsize;
 661		context.firstu = context.bufsize;
 662		context.alist = (attrlist_t *)buffer;
 663	}
 664
 665	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
 666		return (EIO);
 667
 668	xfs_ilock(dp, XFS_ILOCK_SHARED);
 669	if (!(flags & ATTR_SECURE) &&
 670	     (error = xfs_iaccess(dp, S_IRUSR, cred))) {
 671		xfs_iunlock(dp, XFS_ILOCK_SHARED);
 672		return(XFS_ERROR(error));
 673	}
 674
 675	/*
 676	 * Decide on what work routines to call based on the inode size.
 677	 */
 678	xfs_attr_trace_l_c("syscall start", &context);
 679	if (XFS_IFORK_Q(dp) == 0 ||
 680	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 681	     dp->i_d.di_anextents == 0)) {
 682		error = 0;
 683	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
 684		error = xfs_attr_shortform_list(&context);
 685	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
 686		error = xfs_attr_leaf_list(&context);
 687	} else {
 688		error = xfs_attr_node_list(&context);
 689	}
 690	xfs_iunlock(dp, XFS_ILOCK_SHARED);
 691	xfs_attr_trace_l_c("syscall end", &context);
 692
 693	if (!(context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS))) {
 694		ASSERT(error >= 0);
 695	}
 696	else {	/* must return negated buffer size or the error */
 697		if (context.count < 0)
 698			error = XFS_ERROR(ERANGE);
 699		else
 700			error = -context.count;
 701	}
 702
 703	return(error);
 704}
 705
 706int								/* error */
 707xfs_attr_inactive(xfs_inode_t *dp)
 708{
 709	xfs_trans_t *trans;
 710	xfs_mount_t *mp;
 711	int error;
 712
 713	mp = dp->i_mount;
 714	ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
 715
 716	xfs_ilock(dp, XFS_ILOCK_SHARED);
 717	if ((XFS_IFORK_Q(dp) == 0) ||
 718	    (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
 719	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 720	     dp->i_d.di_anextents == 0)) {
 721		xfs_iunlock(dp, XFS_ILOCK_SHARED);
 722		return(0);
 723	}
 724	xfs_iunlock(dp, XFS_ILOCK_SHARED);
 725
 726	/*
 727	 * Start our first transaction of the day.
 728	 *
 729	 * All future transactions during this code must be "chained" off
 730	 * this one via the trans_dup() call.  All transactions will contain
 731	 * the inode, and the inode will always be marked with trans_ihold().
 732	 * Since the inode will be locked in all transactions, we must log
 733	 * the inode in every transaction to let it float upward through
 734	 * the log.
 735	 */
 736	trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
 737	if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
 738				      XFS_TRANS_PERM_LOG_RES,
 739				      XFS_ATTRINVAL_LOG_COUNT))) {
 740		xfs_trans_cancel(trans, 0);
 741		return(error);
 742	}
 743	xfs_ilock(dp, XFS_ILOCK_EXCL);
 744
 745	/*
 746	 * No need to make quota reservations here. We expect to release some
 747	 * blocks, not allocate, in the common case.
 748	 */
 749	xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
 750	xfs_trans_ihold(trans, dp);
 751
 752	/*
 753	 * Decide on what work routines to call based on the inode size.
 754	 */
 755	if ((XFS_IFORK_Q(dp) == 0) ||
 756	    (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
 757	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
 758	     dp->i_d.di_anextents == 0)) {
 759		error = 0;
 760		goto out;
 761	}
 762	error = xfs_attr_root_inactive(&trans, dp);
 763	if (error)
 764		goto out;
 765	/*
 766	 * signal synchronous inactive transactions unless this
 767	 * is a synchronous mount filesystem in which case we
 768	 * know that we're here because we've been called out of
 769	 * xfs_inactive which means that the last reference is gone
 770	 * and the unlink transaction has already hit the disk so
 771	 * async inactive transactions are safe.
 772	 */
 773	if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
 774				(!(mp->m_flags & XFS_MOUNT_WSYNC)
 775				 ? 1 : 0))))
 776		goto out;
 777
 778	/*
 779	 * Commit the last in the sequence of transactions.
 780	 */
 781	xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
 782	error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES,
 783				 NULL);
 784	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 785
 786	return(error);
 787
 788out:
 789	xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
 790	xfs_iunlock(dp, XFS_ILOCK_EXCL);
 791	return(error);
 792}
 793
 794
 795
 796/*========================================================================
 797 * External routines when attribute list is inside the inode
 798 *========================================================================*/
 799
 800/*
 801 * Add a name to the shortform attribute list structure
 802 * This is the external routine.
 803 */
 804STATIC int
 805xfs_attr_shortform_addname(xfs_da_args_t *args)
 806{
 807	int newsize, retval;
 808
 809	retval = xfs_attr_shortform_lookup(args);
 810	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
 811		return(retval);
 812	} else if (retval == EEXIST) {
 813		if (args->flags & ATTR_CREATE)
 814			return(retval);
 815		retval = xfs_attr_shortform_remove(args);
 816		ASSERT(retval == 0);
 817	}
 818
 819	newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
 820	newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
 821	if ((newsize <= XFS_IFORK_ASIZE(args->dp)) &&
 822	    (args->namelen < XFS_ATTR_SF_ENTSIZE_MAX) &&
 823	    (args->valuelen < XFS_ATTR_SF_ENTSIZE_MAX)) {
 824		retval = xfs_attr_shortform_add(args);
 825		ASSERT(retval == 0);
 826	} else {
 827		return(XFS_ERROR(ENOSPC));
 828	}
 829	return(0);
 830}
 831
 832
 833/*========================================================================
 834 * External routines when attribute list is one block
 835 *========================================================================*/
 836
 837/*
 838 * Add a name to the leaf attribute list structure
 839 *
 840 * This leaf block cannot have a "remote" value, we only call this routine
 841 * if bmap_one_block() says there is only one block (ie: no remote blks).
 842 */
 843int
 844xfs_attr_leaf_addname(xfs_da_args_t *args)
 845{
 846	xfs_inode_t *dp;
 847	xfs_dabuf_t *bp;
 848	int retval, error, committed;
 849
 850	/*
 851	 * Read the (only) block in the attribute list in.
 852	 */
 853	dp = args->dp;
 854	args->blkno = 0;
 855	error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
 856					     XFS_ATTR_FORK);
 857	if (error)
 858		return(error);
 859	ASSERT(bp != NULL);
 860
 861	/*
 862	 * Look up the given attribute in the leaf block.  Figure out if
 863	 * the given flags produce an error or call for an atomic rename.
 864	 */
 865	retval = xfs_attr_leaf_lookup_int(bp, args);
 866	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
 867		xfs_da_brelse(args->trans, bp);
 868		return(retval);
 869	} else if (retval == EEXIST) {
 870		if (args->flags & ATTR_CREATE) {	/* pure create op */
 871			xfs_da_brelse(args->trans, bp);
 872			return(retval);
 873		}
 874		args->rename = 1;			/* an atomic rename */
 875		args->blkno2 = args->blkno;		/* set 2nd entry info*/
 876		args->index2 = args->index;
 877		args->rmtblkno2 = args->rmtblkno;
 878		args->rmtblkcnt2 = args->rmtblkcnt;
 879	}
 880
 881	/*
 882	 * Add the attribute to the leaf block, transitioning to a Btree
 883	 * if required.
 884	 */
 885	retval = xfs_attr_leaf_add(bp, args);
 886	xfs_da_buf_done(bp);
 887	if (retval == ENOSPC) {
 888		/*
 889		 * Promote the attribute list to the Btree format, then
 890		 * Commit that transaction so that the node_addname() call
 891		 * can manage its own transactions.
 892		 */
 893		XFS_BMAP_INIT(args->flist, args->firstblock);
 894		error = xfs_attr_leaf_to_node(args);
 895		if (!error) {
 896			error = xfs_bmap_finish(&args->trans, args->flist,
 897						*args->firstblock, &committed);
 898		}
 899		if (error) {
 900			ASSERT(committed);
 901			args->trans = NULL;
 902			xfs_bmap_cancel(args->flist);
 903			return(error);
 904		}
 905
 906		/*
 907		 * bmap_finish() may have committed the last trans and started
 908		 * a new one.  We need the inode to be in all transactions.
 909		 */
 910		if (committed) {
 911			xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
 912			xfs_trans_ihold(args->trans, dp);
 913		}
 914
 915		/*
 916		 * Commit the current trans (including the inode) and start
 917		 * a new one.
 918		 */
 919		if ((error = xfs_attr_rolltrans(&args->trans, dp)))
 920			return (error);
 921
 922		/*
 923		 * Fob the whole rest of the problem off on the Btree code.
 924		 */
 925		error = xfs_attr_node_addname(args);
 926		return(error);
 927	}
 928
 929	/*
 930	 * Commit the transaction that added the attr name so that
 931	 * later routines can manage their own transactions.
 932	 */
 933	if ((error = xfs_attr_rolltrans(&args->trans, dp)))
 934		return (error);
 935
 936	/*
 937	 * If there was an out-of-line value, allocate the blocks we
 938	 * identified for its storage and copy the value.  This is done
 939	 * after we create the attribute so that we don't overflow the
 940	 * maximum size of a transaction and/or hit a deadlock.
 941	 */
 942	if (args->rmtblkno > 0) {
 943		error = xfs_attr_rmtval_set(args);
 944		if (error)
 945			return(error);
 946	}
 947
 948	/*
 949	 * If this is an atomic rename operation, we must "flip" the
 950	 * incomplete flags on the "new" and "old" attribute/value pairs
 951	 * so that one disappears and one appears atomically.  Then we
 952	 * must remove the "old" attribute/value pair.
 953	 */
 954	if (args->rename) {
 955		/*
 956		 * In a separate transaction, set the incomplete flag on the
 957		 * "old" attr and clear the incomplete flag on the "new" attr.
 958		 */
 959		error = xfs_attr_leaf_flipflags(args);
 960		if (error)
 961			return(error);
 962
 963		/*
 964		 * Dismantle the "old" attribute/value pair by removing
 965		 * a "remote" value (if it exists).
 966		 */
 967		args->index = args->index2;
 968		args->blkno = args->blkno2;
 969		args->rmtblkno = args->rmtblkno2;
 970		args->rmtblkcnt = args->rmtblkcnt2;
 971		if (args->rmtblkno) {
 972			error = xfs_attr_rmtval_remove(args);
 973			if (error)
 974				return(error);
 975		}
 976
 977		/*
 978		 * Read in the block containing the "old" attr, then
 979		 * remove the "old" attr from that block (neat, huh!)
 980		 */
 981		error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
 982						     &bp, XFS_ATTR_FORK);
 983		if (error)
 984			return(error);
 985		ASSERT(bp != NULL);
 986		(void)xfs_attr_leaf_remove(bp, args);
 987
 988		/*
 989		 * If the result is small enough, shrink it all into the inode.
 990		 */
 991		if (xfs_attr_shortform_allfit(bp, dp)) {
 992			XFS_BMAP_INIT(args->flist, args->firstblock);
 993			error = xfs_attr_leaf_to_shortform(bp, args);
 994			/* bp is gone due to xfs_da_shrink_inode */
 995			if (!error) {
 996				error = xfs_bmap_finish(&args->trans,
 997							args->flist,
 998							*args->firstblock,
 999							&committed);
1000			}
1001			if (error) {
1002				ASSERT(committed);
1003				args->trans = NULL;
1004				xfs_bmap_cancel(args->flist);
1005				return(error);
1006			}
1007
1008			/*
1009			 * bmap_finish() may have committed the last trans
1010			 * and started a new one.  We need the inode to be
1011			 * in all transactions.
1012			 */
1013			if (committed) {
1014				xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1015				xfs_trans_ihold(args->trans, dp);
1016			}
1017		} else
1018			xfs_da_buf_done(bp);
1019
1020		/*
1021		 * Commit the remove and start the next trans in series.
1022		 */
1023		error = xfs_attr_rolltrans(&args->trans, dp);
1024
1025	} else if (args->rmtblkno > 0) {
1026		/*
1027		 * Added a "remote" value, just clear the incomplete flag.
1028		 */
1029		error = xfs_attr_leaf_clearflag(args);
1030	}
1031	return(error);
1032}
1033
1034/*
1035 * Remove a name from the leaf attribute list structure
1036 *
1037 * This leaf block cannot have a "remote" value, we only call this routine
1038 * if bmap_one_block() says there is only one block (ie: no remote blks).
1039 */
1040STATIC int
1041xfs_attr_leaf_removename(xfs_da_args_t *args)
1042{
1043	xfs_inode_t *dp;
1044	xfs_dabuf_t *bp;
1045	int committed;
1046	int error;
1047
1048	/*
1049	 * Remove the attribute.
1050	 */
1051	dp = args->dp;
1052	args->blkno = 0;
1053	error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1054					     XFS_ATTR_FORK);
1055	if (error) {
1056		return(error);
1057	}
1058
1059	ASSERT(bp != NULL);
1060	error = xfs_attr_leaf_lookup_int(bp, args);
1061	if (error == ENOATTR) {
1062		xfs_da_brelse(args->trans, bp);
1063		return(error);
1064	}
1065
1066	(void)xfs_attr_leaf_remove(bp, args);
1067
1068	/*
1069	 * If the result is small enough, shrink it all into the inode.
1070	 */
1071	if (xfs_attr_shortform_allfit(bp, dp)) {
1072		XFS_BMAP_INIT(args->flist, args->firstblock);
1073		error = xfs_attr_leaf_to_shortform(bp, args);
1074		/* bp is gone due to xfs_da_shrink_inode */
1075		if (!error) {
1076			error = xfs_bmap_finish(&args->trans, args->flist,
1077						*args->firstblock, &committed);
1078		}
1079		if (error) {
1080			ASSERT(committed);
1081			args->trans = NULL;
1082			xfs_bmap_cancel(args->flist);
1083			return(error);
1084		}
1085
1086		/*
1087		 * bmap_finish() may have committed the last trans and started
1088		 * a new one.  We need the inode to be in all transactions.
1089		 */
1090		if (committed) {
1091			xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1092			xfs_trans_ihold(args->trans, dp);
1093		}
1094	} else
1095		xfs_da_buf_done(bp);
1096	return(0);
1097}
1098
1099/*
1100 * Look up a name in a leaf attribute list structure.
1101 *
1102 * This leaf block cannot have a "remote" value, we only call this routine
1103 * if bmap_one_block() says there is only one block (ie: no remote blks).
1104 */
1105int
1106xfs_attr_leaf_get(xfs_da_args_t *args)
1107{
1108	xfs_dabuf_t *bp;
1109	int error;
1110
1111	args->blkno = 0;
1112	error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1113					     XFS_ATTR_FORK);
1114	if (error)
1115		return(error);
1116	ASSERT(bp != NULL);
1117
1118	error = xfs_attr_leaf_lookup_int(bp, args);
1119	if (error != EEXIST)  {
1120		xfs_da_brelse(args->trans, bp);
1121		return(error);
1122	}
1123	error = xfs_attr_leaf_getvalue(bp, args);
1124	xfs_da_brelse(args->trans, bp);
1125	if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1126		error = xfs_attr_rmtval_get(args);
1127	}
1128	return(error);
1129}
1130
1131/*
1132 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1133 */
1134STATIC int
1135xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1136{
1137	xfs_attr_leafblock_t *leaf;
1138	int error;
1139	xfs_dabuf_t *bp;
1140
1141	context->cursor->blkno = 0;
1142	error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1143	if (error)
1144		return(error);
1145	ASSERT(bp != NULL);
1146	leaf = bp->data;
1147	if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1148						!= XFS_ATTR_LEAF_MAGIC)) {
1149		XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1150				     context->dp->i_mount, leaf);
1151		xfs_da_brelse(NULL, bp);
1152		return(XFS_ERROR(EFSCORRUPTED));
1153	}
1154
1155	(void)xfs_attr_leaf_list_int(bp, context);
1156	xfs_da_brelse(NULL, bp);
1157	return(0);
1158}
1159
1160
1161/*========================================================================
1162 * External routines when attribute list size > XFS_LBSIZE(mp).
1163 *========================================================================*/
1164
1165/*
1166 * Add a name to a Btree-format attribute list.
1167 *
1168 * This will involve walking down the Btree, and may involve splitting
1169 * leaf nodes and even splitting intermediate nodes up to and including
1170 * the root node (a special case of an intermediate node).
1171 *
1172 * "Remote" attribute values confuse the issue and atomic rename operations
1173 * add a whole extra layer of confusion on top of that.
1174 */
1175STATIC int
1176xfs_attr_node_addname(xfs_da_args_t *args)
1177{
1178	xfs_da_state_t *state;
1179	xfs_da_state_blk_t *blk;
1180	xfs_inode_t *dp;
1181	xfs_mount_t *mp;
1182	int committed, retval, error;
1183
1184	/*
1185	 * Fill in bucket of arguments/results/context to carry around.
1186	 */
1187	dp = args->dp;
1188	mp = dp->i_mount;
1189restart:
1190	state = xfs_da_state_alloc();
1191	state->args = args;
1192	state->mp = mp;
1193	state->blocksize = state->mp->m_sb.sb_blocksize;
1194	state->node_ents = state->mp->m_attr_node_ents;
1195
1196	/*
1197	 * Search to see if name already exists, and get back a pointer
1198	 * to where it should go.
1199	 */
1200	error = xfs_da_node_lookup_int(state, &retval);
1201	if (error)
1202		goto out;
1203	blk = &state->path.blk[ state->path.active-1 ];
1204	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1205	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1206		goto out;
1207	} else if (retval == EEXIST) {
1208		if (args->flags & ATTR_CREATE)
1209			goto out;
1210		args->rename = 1;			/* atomic rename op */
1211		args->blkno2 = args->blkno;		/* set 2nd entry info*/
1212		args->index2 = args->index;
1213		args->rmtblkno2 = args->rmtblkno;
1214		args->rmtblkcnt2 = args->rmtblkcnt;
1215		args->rmtblkno = 0;
1216		args->rmtblkcnt = 0;
1217	}
1218
1219	retval = xfs_attr_leaf_add(blk->bp, state->args);
1220	if (retval == ENOSPC) {
1221		if (state->path.active == 1) {
1222			/*
1223			 * Its really a single leaf node, but it had
1224			 * out-of-line values so it looked like it *might*
1225			 * have been a b-tree.
1226			 */
1227			xfs_da_state_free(state);
1228			XFS_BMAP_INIT(args->flist, args->firstblock);
1229			error = xfs_attr_leaf_to_node(args);
1230			if (!error) {
1231				error = xfs_bmap_finish(&args->trans,
1232							args->flist,
1233							*args->firstblock,
1234							&committed);
1235			}
1236			if (error) {
1237				ASSERT(committed);
1238				args->trans = NULL;
1239				xfs_bmap_cancel(args->flist);
1240				goto out;
1241			}
1242
1243			/*
1244			 * bmap_finish() may have committed the last trans
1245			 * and started a new one.  We need the inode to be
1246			 * in all transactions.
1247			 */
1248			if (committed) {
1249				xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1250				xfs_trans_ihold(args->trans, dp);
1251			}
1252
1253			/*
1254			 * Commit the node conversion and start the next
1255			 * trans in the chain.
1256			 */
1257			if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1258				goto out;
1259
1260			goto restart;
1261		}
1262
1263		/*
1264		 * Split as many Btree elements as required.
1265		 * This code tracks the new and old attr's location
1266		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1267		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1268		 */
1269		XFS_BMAP_INIT(args->flist, args->firstblock);
1270		error = xfs_da_split(state);
1271		if (!error) {
1272			error = xfs_bmap_finish(&args->trans, args->flist,
1273						*args->firstblock, &committed);
1274		}
1275		if (error) {
1276			ASSERT(committed);
1277			args->trans = NULL;
1278			xfs_bmap_cancel(args->flist);
1279			goto out;
1280		}
1281
1282		/*
1283		 * bmap_finish() may have committed the last trans and started
1284		 * a new one.  We need the inode to be in all transactions.
1285		 */
1286		if (committed) {
1287			xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1288			xfs_trans_ihold(args->trans, dp);
1289		}
1290	} else {
1291		/*
1292		 * Addition succeeded, update Btree hashvals.
1293		 */
1294		xfs_da_fixhashpath(state, &state->path);
1295	}
1296
1297	/*
1298	 * Kill the state structure, we're done with it and need to
1299	 * allow the buffers to come back later.
1300	 */
1301	xfs_da_state_free(state);
1302	state = NULL;
1303
1304	/*
1305	 * Commit the leaf addition or btree split and start the next
1306	 * trans in the chain.
1307	 */
1308	if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1309		goto out;
1310
1311	/*
1312	 * If there was an out-of-line value, allocate the blocks we
1313	 * identified for its storage and copy the value.  This is done
1314	 * after we create the attribute so that we don't overflow the
1315	 * maximum size of a transaction and/or hit a deadlock.
1316	 */
1317	if (args->rmtblkno > 0) {
1318		error = xfs_attr_rmtval_set(args);
1319		if (error)
1320			return(error);
1321	}
1322
1323	/*
1324	 * If this is an atomic rename operation, we must "flip" the
1325	 * incomplete flags on the "new" and "old" attribute/value pairs
1326	 * so that one disappears and one appears atomically.  Then we
1327	 * must remove the "old" attribute/value pair.
1328	 */
1329	if (args->rename) {
1330		/*
1331		 * In a separate transaction, set the incomplete flag on the
1332		 * "old" attr and clear the incomplete flag on the "new" attr.
1333		 */
1334		error = xfs_attr_leaf_flipflags(args);
1335		if (error)
1336			goto out;
1337
1338		/*
1339		 * Dismantle the "old" attribute/value pair by removing
1340		 * a "remote" value (if it exists).
1341		 */
1342		args->index = args->index2;
1343		args->blkno = args->blkno2;
1344		args->rmtblkno = args->rmtblkno2;
1345		args->rmtblkcnt = args->rmtblkcnt2;
1346		if (args->rmtblkno) {
1347			error = xfs_attr_rmtval_remove(args);
1348			if (error)
1349				return(error);
1350		}
1351
1352		/*
1353		 * Re-find the "old" attribute entry after any split ops.
1354		 * The INCOMPLETE flag means that we will find the "old"
1355		 * attr, not the "new" one.
1356		 */
1357		args->flags |= XFS_ATTR_INCOMPLETE;
1358		state = xfs_da_state_alloc();
1359		state->args = args;
1360		state->mp = mp;
1361		state->blocksize = state->mp->m_sb.sb_blocksize;
1362		state->node_ents = state->mp->m_attr_node_ents;
1363		state->inleaf = 0;
1364		error = xfs_da_node_lookup_int(state, &retval);
1365		if (error)
1366			goto out;
1367
1368		/*
1369		 * Remove the name and update the hashvals in the tree.
1370		 */
1371		blk = &state->path.blk[ state->path.active-1 ];
1372		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1373		error = xfs_attr_leaf_remove(blk->bp, args);
1374		xfs_da_fixhashpath(state, &state->path);
1375
1376		/*
1377		 * Check to see if the tree needs to be collapsed.
1378		 */
1379		if (retval && (state->path.active > 1)) {
1380			XFS_BMAP_INIT(args->flist, args->firstblock);
1381			error = xfs_da_join(state);
1382			if (!error) {
1383				error = xfs_bmap_finish(&args->trans,
1384							args->flist,
1385							*args->firstblock,
1386							&committed);
1387			}
1388			if (error) {
1389				ASSERT(committed);
1390				args->trans = NULL;
1391				xfs_bmap_cancel(args->flist);
1392				goto out;
1393			}
1394
1395			/*
1396			 * bmap_finish() may have committed the last trans
1397			 * and started a new one.  We need the inode to be
1398			 * in all transactions.
1399			 */
1400			if (committed) {
1401				xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1402				xfs_trans_ihold(args->trans, dp);
1403			}
1404		}
1405
1406		/*
1407		 * Commit and start the next trans in the chain.
1408		 */
1409		if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1410			goto out;
1411
1412	} else if (args->rmtblkno > 0) {
1413		/*
1414		 * Added a "remote" value, just clear the incomplete flag.
1415		 */
1416		error = xfs_attr_leaf_clearflag(args);
1417		if (error)
1418			goto out;
1419	}
1420	retval = error = 0;
1421
1422out:
1423	if (state)
1424		xfs_da_state_free(state);
1425	if (error)
1426		return(error);
1427	return(retval);
1428}
1429
1430/*
1431 * Remove a name from a B-tree attribute list.
1432 *
1433 * This will involve walking down the Btree, and may involve joining
1434 * leaf nodes and even joining intermediate nodes up to and including
1435 * the root node (a special case of an intermediate node).
1436 */
1437STATIC int
1438xfs_attr_node_removename(xfs_da_args_t *args)
1439{
1440	xfs_da_state_t *state;
1441	xfs_da_state_blk_t *blk;
1442	xfs_inode_t *dp;
1443	xfs_dabuf_t *bp;
1444	int retval, error, committed;
1445
1446	/*
1447	 * Tie a string around our finger to remind us where we are.
1448	 */
1449	dp = args->dp;
1450	state = xfs_da_state_alloc();
1451	state->args = args;
1452	state->mp = dp->i_mount;
1453	state->blocksize = state->mp->m_sb.sb_blocksize;
1454	state->node_ents = state->mp->m_attr_node_ents;
1455
1456	/*
1457	 * Search to see if name exists, and get back a pointer to it.
1458	 */
1459	error = xfs_da_node_lookup_int(state, &retval);
1460	if (error || (retval != EEXIST)) {
1461		if (error == 0)
1462			error = retval;
1463		goto out;
1464	}
1465
1466	/*
1467	 * If there is an out-of-line value, de-allocate the blocks.
1468	 * This is done before we remove the attribute so that we don't
1469	 * overflow the maximum size of a transaction and/or hit a deadlock.
1470	 */
1471	blk = &state->path.blk[ state->path.active-1 ];
1472	ASSERT(blk->bp != NULL);
1473	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1474	if (args->rmtblkno > 0) {
1475		/*
1476		 * Fill in disk block numbers in the state structure
1477		 * so that we can get the buffers back after we commit
1478		 * several transactions in the following calls.
1479		 */
1480		error = xfs_attr_fillstate(state);
1481		if (error)
1482			goto out;
1483
1484		/*
1485		 * Mark the attribute as INCOMPLETE, then bunmapi() the
1486		 * remote value.
1487		 */
1488		error = xfs_attr_leaf_setflag(args);
1489		if (error)
1490			goto out;
1491		error = xfs_attr_rmtval_remove(args);
1492		if (error)
1493			goto out;
1494
1495		/*
1496		 * Refill the state structure with buffers, the prior calls
1497		 * released our buffers.
1498		 */
1499		error = xfs_attr_refillstate(state);
1500		if (error)
1501			goto out;
1502	}
1503
1504	/*
1505	 * Remove the name and update the hashvals in the tree.
1506	 */
1507	blk = &state->path.blk[ state->path.active-1 ];
1508	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1509	retval = xfs_attr_leaf_remove(blk->bp, args);
1510	xfs_da_fixhashpath(state, &state->path);
1511
1512	/*
1513	 * Check to see if the tree needs to be collapsed.
1514	 */
1515	if (retval && (state->path.active > 1)) {
1516		XFS_BMAP_INIT(args->flist, args->firstblock);
1517		error = xfs_da_join(state);
1518		if (!error) {
1519			error = xfs_bmap_finish(&args->trans, args->flist,
1520						*args->firstblock, &committed);
1521		}
1522		if (error) {
1523			ASSERT(committed);
1524			args->trans = NULL;
1525			xfs_bmap_cancel(args->flist);
1526			goto out;
1527		}
1528
1529		/*
1530		 * bmap_finish() may have committed the last trans and started
1531		 * a new one.  We need the inode to be in all transactions.
1532		 */
1533		if (committed) {
1534			xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1535			xfs_trans_ihold(args->trans, dp);
1536		}
1537
1538		/*
1539		 * Commit the Btree join operation and start a new trans.
1540		 */
1541		if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1542			goto out;
1543	}
1544
1545	/*
1546	 * If the result is small enough, push it all into the inode.
1547	 */
1548	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1549		/*
1550		 * Have to get rid of the copy of this dabuf in the state.
1551		 */
1552		ASSERT(state->path.active == 1);
1553		ASSERT(state->path.blk[0].bp);
1554		xfs_da_buf_done(state->path.blk[0].bp);
1555		state->path.blk[0].bp = NULL;
1556
1557		error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1558						     XFS_ATTR_FORK);
1559		if (error)
1560			goto out;
1561		ASSERT(INT_GET(((xfs_attr_leafblock_t *)
1562				      bp->data)->hdr.info.magic, ARCH_CONVERT)
1563						       == XFS_ATTR_LEAF_MAGIC);
1564
1565		if (xfs_attr_shortform_allfit(bp, dp)) {
1566			XFS_BMAP_INIT(args->flist, args->firstblock);
1567			error = xfs_attr_leaf_to_shortform(bp, args);
1568			/* bp is gone due to xfs_da_shrink_inode */
1569			if (!error) {
1570				error = xfs_bmap_finish(&args->trans,
1571							args->flist,
1572							*args->firstblock,
1573							&committed);
1574			}
1575			if (error) {
1576				ASSERT(committed);
1577				args->trans = NULL;
1578				xfs_bmap_cancel(args->flist);
1579				goto out;
1580			}
1581
1582			/*
1583			 * bmap_finish() may have committed the last trans
1584			 * and started a new one.  We need the inode to be
1585			 * in all transactions.
1586			 */
1587			if (committed) {
1588				xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1589				xfs_trans_ihold(args->trans, dp);
1590			}
1591		} else
1592			xfs_da_brelse(args->trans, bp);
1593	}
1594	error = 0;
1595
1596out:
1597	xfs_da_state_free(state);
1598	return(error);
1599}
1600
1601/*
1602 * Fill in the disk block numbers in the state structure for the buffers
1603 * that are attached to the state structure.
1604 * This is done so that we can quickly reattach ourselves to those buffers
1605 * after some set of transaction commit's has released these buffers.
1606 */
1607STATIC int
1608xfs_attr_fillstate(xfs_da_state_t *state)
1609{
1610	xfs_da_state_path_t *path;
1611	xfs_da_state_blk_t *blk;
1612	int level;
1613
1614	/*
1615	 * Roll down the "path" in the state structure, storing the on-disk
1616	 * block number for those buffers in the "path".
1617	 */
1618	path = &state->path;
1619	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1620	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1621		if (blk->bp) {
1622			blk->disk_blkno = xfs_da_blkno(blk->bp);
1623			xfs_da_buf_done(blk->bp);
1624			blk->bp = NULL;
1625		} else {
1626			blk->disk_blkno = 0;
1627		}
1628	}
1629
1630	/*
1631	 * Roll down the "altpath" in the state structure, storing the on-disk
1632	 * block number for those buffers in the "altpath".
1633	 */
1634	path = &state->altpath;
1635	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1636	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1637		if (blk->bp) {
1638			blk->disk_blkno = xfs_da_blkno(blk->bp);
1639			xfs_da_buf_done(blk->bp);
1640			blk->bp = NULL;
1641		} else {
1642			blk->disk_blkno = 0;
1643		}
1644	}
1645
1646	return(0);
1647}
1648
1649/*
1650 * Reattach the buffers to the state structure based on the disk block
1651 * numbers stored in the state structure.
1652 * This is done after some set of transaction commit's has released those
1653 * buffers from our grip.
1654 */
1655STATIC int
1656xfs_attr_refillstate(xfs_da_state_t *state)
1657{
1658	xfs_da_state_path_t *path;
1659	xfs_da_state_blk_t *blk;
1660	int level, error;
1661
1662	/*
1663	 * Roll down the "path" in the state structure, storing the on-disk
1664	 * block number for those buffers in the "path".
1665	 */
1666	path = &state->path;
1667	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1668	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1669		if (blk->disk_blkno) {
1670			error = xfs_da_read_buf(state->args->trans,
1671						state->args->dp,
1672						blk->blkno, blk->disk_blkno,
1673						&blk->bp, XFS_ATTR_FORK);
1674			if (error)
1675				return(error);
1676		} else {
1677			blk->bp = NULL;
1678		}
1679	}
1680
1681	/*
1682	 * Roll down the "altpath" in the state structure, storing the on-disk
1683	 * block number for those buffers in the "altpath".
1684	 */
1685	path = &state->altpath;
1686	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1687	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1688		if (blk->disk_blkno) {
1689			error = xfs_da_read_buf(state->args->trans,
1690						state->args->dp,
1691						blk->blkno, blk->disk_blkno,
1692						&blk->bp, XFS_ATTR_FORK);
1693			if (error)
1694				return(error);
1695		} else {
1696			blk->bp = NULL;
1697		}
1698	}
1699
1700	return(0);
1701}
1702
1703/*
1704 * Look up a filename in a node attribute list.
1705 *
1706 * This routine gets called for any attribute fork that has more than one
1707 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1708 * "remote" values taking up more blocks.
1709 */
1710int
1711xfs_attr_node_get(xfs_da_args_t *args)
1712{
1713	xfs_da_state_t *state;
1714	xfs_da_state_blk_t *blk;
1715	int error, retval;
1716	int i;
1717
1718	state = xfs_da_state_alloc();
1719	state->args = args;
1720	state->mp = args->dp->i_mount;
1721	state->blocksize = state->mp->m_sb.sb_blocksize;
1722	state->node_ents = state->mp->m_attr_node_ents;
1723
1724	/*
1725	 * Search to see if name exists, and get back a pointer to it.
1726	 */
1727	error = xfs_da_node_lookup_int(state, &retval);
1728	if (error) {
1729		retval = error;
1730	} else if (retval == EEXIST) {
1731		blk = &state->path.blk[ state->path.active-1 ];
1732		ASSERT(blk->bp != NULL);
1733		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1734
1735		/*
1736		 * Get the value, local or "remote"
1737		 */
1738		retval = xfs_attr_leaf_getvalue(blk->bp, args);
1739		if (!retval && (args->rmtblkno > 0)
1740		    && !(args->flags & ATTR_KERNOVAL)) {
1741			retval = xfs_attr_rmtval_get(args);
1742		}
1743	}
1744
1745	/*
1746	 * If not in a transaction, we have to release all the buffers.
1747	 */
1748	for (i = 0; i < state->path.active; i++) {
1749		xfs_da_brelse(args->trans, state->path.blk[i].bp);
1750		state->path.blk[i].bp = NULL;
1751	}
1752
1753	xfs_da_state_free(state);
1754	return(retval);
1755}
1756
1757STATIC int							/* error */
1758xfs_attr_node_list(xfs_attr_list_context_t *context)
1759{
1760	attrlist_cursor_kern_t *cursor;
1761	xfs_attr_leafblock_t *leaf;
1762	xfs_da_intnode_t *node;
1763	xfs_da_node_entry_t *btree;
1764	int error, i;
1765	xfs_dabuf_t *bp;
1766
1767	cursor = context->cursor;
1768	cursor->initted = 1;
1769
1770	/*
1771	 * Do all sorts of validation on the passed-in cursor structure.
1772	 * If anything is amiss, ignore the cursor and look up the hashval
1773	 * starting from the btree root.
1774	 */
1775	bp = NULL;
1776	if (cursor->blkno > 0) {
1777		error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1778					      &bp, XFS_ATTR_FORK);
1779		if ((error != 0) && (error != EFSCORRUPTED))
1780			return(error);
1781		if (bp) {
1782			node = bp->data;
1783			switch (INT_GET(node->hdr.info.magic, ARCH_CONVERT)) {
1784			case XFS_DA_NODE_MAGIC:
1785				xfs_attr_trace_l_cn("wrong blk", context, node);
1786				xfs_da_brelse(NULL, bp);
1787				bp = NULL;
1788				break;
1789			case XFS_ATTR_LEAF_MAGIC:
1790				leaf = bp->data;
1791				if (cursor->hashval >
1792				    INT_GET(leaf->entries[
1793					 INT_GET(leaf->hdr.count,
1794						ARCH_CONVERT)-1].hashval,
1795							ARCH_CONVERT)) {
1796					xfs_attr_trace_l_cl("wrong blk",
1797							   context, leaf);
1798					xfs_da_brelse(NULL, bp);
1799					bp = NULL;
1800				} else if (cursor->hashval <=
1801					     INT_GET(leaf->entries[0].hashval,
1802							ARCH_CONVERT)) {
1803					xfs_attr_trace_l_cl("maybe wrong blk",
1804							   context, leaf);
1805					xfs_da_brelse(NULL, bp);
1806					bp = NULL;
1807				}
1808				break;
1809			default:
1810				xfs_attr_trace_l_c("wrong blk - ??", context);
1811				xfs_da_brelse(NULL, bp);
1812				bp = NULL;
1813			}
1814		}
1815	}
1816
1817	/*
1818	 * We did not find what we expected given the cursor's contents,
1819	 * so we start from the top and work down based on the hash value.
1820	 * Note that start of node block is same as start of leaf block.
1821	 */
1822	if (bp == NULL) {
1823		cursor->blkno = 0;
1824		for (;;) {
1825			error = xfs_da_read_buf(NULL, context->dp,
1826						      cursor->blkno, -1, &bp,
1827						      XFS_ATTR_FORK);
1828			if (error)
1829				return(error);
1830			if (unlikely(bp == NULL)) {
1831				XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1832						 XFS_ERRLEVEL_LOW,
1833						 context->dp->i_mount);
1834				return(XFS_ERROR(EFSCORRUPTED));
1835			}
1836			node = bp->data;
1837			if (INT_GET(node->hdr.info.magic, ARCH_CONVERT)
1838							== XFS_ATTR_LEAF_MAGIC)
1839				break;
1840			if (unlikely(INT_GET(node->hdr.info.magic, ARCH_CONVERT)
1841							!= XFS_DA_NODE_MAGIC)) {
1842				XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1843						     XFS_ERRLEVEL_LOW,
1844						     context->dp->i_mount,
1845						     node);
1846				xfs_da_brelse(NULL, bp);
1847				return(XFS_ERROR(EFSCORRUPTED));
1848			}
1849			btree = node->btree;
1850			for (i = 0;
1851				i < INT_GET(node->hdr.count, ARCH_CONVERT);
1852								btree++, i++) {
1853				if (cursor->hashval
1854						<= INT_GET(btree->hashval,
1855							    ARCH_CONVERT)) {
1856					cursor->blkno = INT_GET(btree->before, ARCH_CONVERT);
1857					xfs_attr_trace_l_cb("descending",
1858							    context, btree);
1859					break;
1860				}
1861			}
1862			if (i == INT_GET(node->hdr.count, ARCH_CONVERT)) {
1863				xfs_da_brelse(NULL, bp);
1864				return(0);
1865			}
1866			xfs_da_brelse(NULL, bp);
1867		}
1868	}
1869	ASSERT(bp != NULL);
1870
1871	/*
1872	 * Roll upward through the blocks, processing each leaf block in
1873	 * order.  As long as there is space in the result buffer, keep
1874	 * adding the information.
1875	 */
1876	for (;;) {
1877		leaf = bp->data;
1878		if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1879						!= XFS_ATTR_LEAF_MAGIC)) {
1880			XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1881					     XFS_ERRLEVEL_LOW,
1882					     context->dp->i_mount, leaf);
1883			xfs_da_brelse(NULL, bp);
1884			return(XFS_ERROR(EFSCORRUPTED));
1885		}
1886		error = xfs_attr_leaf_list_int(bp, context);
1887		if (error || !leaf->hdr.info.forw)
1888			break;	/* not really an error, buffer full or EOF */
1889		cursor->blkno = INT_GET(leaf->hdr.info.forw, ARCH_CONVERT);
1890		xfs_da_brelse(NULL, bp);
1891		error = xfs_da_read_buf(NULL, context->dp, cursor->blk

Large files files are truncated, but you can click here to view the full file