xfs_attr.c 36.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2 3
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
Linus Torvalds's avatar
Linus Torvalds committed
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
Linus Torvalds's avatar
Linus Torvalds committed
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Linus Torvalds's avatar
Linus Torvalds committed
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Linus Torvalds's avatar
Linus Torvalds committed
17 18
 */
#include "xfs.h"
19
#include "xfs_fs.h"
20
#include "xfs_shared.h"
21 22 23
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
24
#include "xfs_bit.h"
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_mount.h"
28
#include "xfs_da_format.h"
29 30
#include "xfs_da_btree.h"
#include "xfs_attr_sf.h"
Linus Torvalds's avatar
Linus Torvalds committed
31
#include "xfs_inode.h"
32
#include "xfs_alloc.h"
33
#include "xfs_trans.h"
34
#include "xfs_inode_item.h"
Linus Torvalds's avatar
Linus Torvalds committed
35
#include "xfs_bmap.h"
36
#include "xfs_bmap_util.h"
37
#include "xfs_bmap_btree.h"
Linus Torvalds's avatar
Linus Torvalds committed
38 39
#include "xfs_attr.h"
#include "xfs_attr_leaf.h"
40
#include "xfs_attr_remote.h"
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43
#include "xfs_error.h"
#include "xfs_quota.h"
#include "xfs_trans_space.h"
44
#include "xfs_trace.h"
45
#include "xfs_dinode.h"
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/*
 * xfs_attr.c
 *
 * Provide the external interfaces to manage attribute lists.
 */

/*========================================================================
 * Function prototypes for the kernel.
 *========================================================================*/

/*
 * Internal routines when attribute list fits inside the inode.
 */
STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);

/*
 * Internal routines when attribute list is one block.
 */
65
STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69 70 71
STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);

/*
 * Internal routines when attribute list is more than one block.
 */
72
STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78
STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
STATIC int xfs_attr_refillstate(xfs_da_state_t *state);


79
STATIC int
80 81 82 83 84
xfs_attr_args_init(
	struct xfs_da_args	*args,
	struct xfs_inode	*dp,
	const unsigned char	*name,
	int			flags)
85
{
86 87

	if (!name)
88
		return EINVAL;
89 90

	memset(args, 0, sizeof(*args));
91
	args->geo = dp->i_mount->m_attr_geo;
92 93 94 95 96 97
	args->whichfork = XFS_ATTR_FORK;
	args->dp = dp;
	args->flags = flags;
	args->name = name;
	args->namelen = strlen((const char *)name);
	if (args->namelen >= MAXNAMELEN)
98 99
		return EFAULT;		/* match IRIX behaviour */

100
	args->hashval = xfs_da_hashname(args->name, args->namelen);
101 102
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
103

104
int
105 106 107 108 109 110 111 112 113 114
xfs_inode_hasattr(
	struct xfs_inode	*ip)
{
	if (!XFS_IFORK_Q(ip) ||
	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
	     ip->i_d.di_anextents == 0))
		return 0;
	return 1;
}

Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118
/*========================================================================
 * Overall external interface routines.
 *========================================================================*/

119 120
int
xfs_attr_get(
121
	struct xfs_inode	*ip,
122
	const unsigned char	*name,
123
	unsigned char		*value,
124 125
	int			*valuelenp,
	int			flags)
Linus Torvalds's avatar
Linus Torvalds committed
126
{
127 128 129 130 131 132 133 134
	struct xfs_da_args	args;
	uint			lock_mode;
	int			error;

	XFS_STATS_INC(xs_attr_get);

	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
		return EIO;
Linus Torvalds's avatar
Linus Torvalds committed
135

136 137
	if (!xfs_inode_hasattr(ip))
		return ENOATTR;
Linus Torvalds's avatar
Linus Torvalds committed
138

139
	error = xfs_attr_args_init(&args, ip, name, flags);
140 141 142
	if (error)
		return error;

Linus Torvalds's avatar
Linus Torvalds committed
143 144 145
	args.value = value;
	args.valuelen = *valuelenp;

146 147 148 149
	lock_mode = xfs_ilock_attr_map_shared(ip);
	if (!xfs_inode_hasattr(ip))
		error = ENOATTR;
	else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
Linus Torvalds's avatar
Linus Torvalds committed
150
		error = xfs_attr_shortform_getvalue(&args);
151
	else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
Linus Torvalds's avatar
Linus Torvalds committed
152
		error = xfs_attr_leaf_get(&args);
153
	else
Linus Torvalds's avatar
Linus Torvalds committed
154
		error = xfs_attr_node_get(&args);
155
	xfs_iunlock(ip, lock_mode);
Linus Torvalds's avatar
Linus Torvalds committed
156 157

	*valuelenp = args.valuelen;
158
	return error == EEXIST ? 0 : error;
Linus Torvalds's avatar
Linus Torvalds committed
159 160
}

161 162 163
/*
 * Calculate how many blocks we need for the new attribute,
 */
164
STATIC int
165
xfs_attr_calc_size(
166
	struct xfs_da_args	*args,
167 168
	int			*local)
{
169
	struct xfs_mount	*mp = args->dp->i_mount;
170 171 172 173 174 175 176
	int			size;
	int			nblks;

	/*
	 * Determine space new attribute will use, and if it would be
	 * "local" or "remote" (note: local != inline).
	 */
177
	size = xfs_attr_leaf_newentsize(args, local);
178 179
	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
	if (*local) {
180
		if (size > (args->geo->blksize / 2)) {
181 182 183 184 185 186 187 188
			/* Double split possible */
			nblks *= 2;
		}
	} else {
		/*
		 * Out of line attribute, cannot double split, but
		 * make room for the attribute value itself.
		 */
189
		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
190 191 192 193 194 195 196
		nblks += dblocks;
		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
	}

	return nblks;
}

197 198 199 200 201 202 203
int
xfs_attr_set(
	struct xfs_inode	*dp,
	const unsigned char	*name,
	unsigned char		*value,
	int			valuelen,
	int			flags)
Linus Torvalds's avatar
Linus Torvalds committed
204
{
205
	struct xfs_mount	*mp = dp->i_mount;
206 207
	struct xfs_da_args	args;
	struct xfs_bmap_free	flist;
208
	struct xfs_trans_res	tres;
209
	xfs_fsblock_t		firstblock;
210
	int			rsvd = (flags & ATTR_ROOT) != 0;
211 212 213 214 215 216 217
	int			error, err2, committed, local;

	XFS_STATS_INC(xs_attr_set);

	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
		return EIO;

218
	error = xfs_attr_args_init(&args, dp, name, flags);
219 220
	if (error)
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
221

222 223 224 225 226
	args.value = value;
	args.valuelen = valuelen;
	args.firstblock = &firstblock;
	args.flist = &flist;
	args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
227
	args.total = xfs_attr_calc_size(&args, &local);
Linus Torvalds's avatar
Linus Torvalds committed
228

Christoph Hellwig's avatar
Christoph Hellwig committed
229 230 231
	error = xfs_qm_dqattach(dp, 0);
	if (error)
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236 237

	/*
	 * If the inode doesn't have an attribute fork, add one.
	 * (inode must not be locked when we call this routine)
	 */
	if (XFS_IFORK_Q(dp) == 0) {
238
		int sf_size = sizeof(xfs_attr_sf_hdr_t) +
239
			XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
240

241 242 243
		error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
		if (error)
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	}

	/*
	 * Start our first transaction of the day.
	 *
	 * All future transactions during this code must be "chained" off
	 * this one via the trans_dup() call.  All transactions will contain
	 * the inode, and the inode will always be marked with trans_ihold().
	 * Since the inode will be locked in all transactions, we must log
	 * the inode in every transaction to let it float upward through
	 * the log.
	 */
	args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);

	/*
	 * Root fork attributes can use reserved data blocks for this
	 * operation if necessary
	 */

	if (rsvd)
		args.trans->t_flags |= XFS_TRANS_RESERVE;

266 267 268 269 270
	tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
			 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
	tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
	error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
271
	if (error) {
Linus Torvalds's avatar
Linus Torvalds committed
272
		xfs_trans_cancel(args.trans, 0);
273
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276
	}
	xfs_ilock(dp, XFS_ILOCK_EXCL);

Christoph Hellwig's avatar
Christoph Hellwig committed
277
	error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
278 279
				rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
				       XFS_QMOPT_RES_REGBLKS);
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282
	if (error) {
		xfs_iunlock(dp, XFS_ILOCK_EXCL);
		xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
283
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
284 285
	}

286
	xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
287 288

	/*
289
	 * If the attribute list is non-existent or a shortform list,
Linus Torvalds's avatar
Linus Torvalds committed
290 291
	 * upgrade it to a single-leaf-block attribute list.
	 */
292 293 294
	if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
	     dp->i_d.di_anextents == 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299

		/*
		 * Build initial attribute list (if required).
		 */
		if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
300
			xfs_attr_shortform_create(&args);
Linus Torvalds's avatar
Linus Torvalds committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

		/*
		 * Try to add the attr to the attribute list in
		 * the inode.
		 */
		error = xfs_attr_shortform_addname(&args);
		if (error != ENOSPC) {
			/*
			 * Commit the shortform mods, and we're done.
			 * NOTE: this is also the error path (EEXIST, etc).
			 */
			ASSERT(args.trans != NULL);

			/*
			 * If this is a synchronous mount, make sure that
			 * the transaction goes to disk before returning
			 * to the user.
			 */
319
			if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds's avatar
Linus Torvalds committed
320
				xfs_trans_set_sync(args.trans);
321 322 323 324 325

			if (!error && (flags & ATTR_KERNOTIME) == 0) {
				xfs_trans_ichgtime(args.trans, dp,
							XFS_ICHGTIME_CHG);
			}
Linus Torvalds's avatar
Linus Torvalds committed
326
			err2 = xfs_trans_commit(args.trans,
327
						 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds's avatar
Linus Torvalds committed
328 329
			xfs_iunlock(dp, XFS_ILOCK_EXCL);

330
			return error ? error : err2;
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334 335 336
		}

		/*
		 * It won't fit in the shortform, transform to a leaf block.
		 * GROT: another possible req'mt for a double-split btree op.
		 */
337
		xfs_bmap_init(args.flist, args.firstblock);
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340
		error = xfs_attr_shortform_to_leaf(&args);
		if (!error) {
			error = xfs_bmap_finish(&args.trans, args.flist,
341
						&committed);
Linus Torvalds's avatar
Linus Torvalds committed
342 343 344 345 346 347 348 349 350 351 352 353
		}
		if (error) {
			ASSERT(committed);
			args.trans = NULL;
			xfs_bmap_cancel(&flist);
			goto out;
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
354
		if (committed)
355
			xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360

		/*
		 * Commit the leaf transformation.  We'll need another (linked)
		 * transaction to add the new attribute to the leaf.
		 */
361 362 363

		error = xfs_trans_roll(&args.trans, dp);
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367
			goto out;

	}

368
	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds's avatar
Linus Torvalds committed
369
		error = xfs_attr_leaf_addname(&args);
370
	else
Linus Torvalds's avatar
Linus Torvalds committed
371
		error = xfs_attr_node_addname(&args);
372
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
373 374 375 376 377 378
		goto out;

	/*
	 * If this is a synchronous mount, make sure that the
	 * transaction goes to disk before returning to the user.
	 */
379
	if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds's avatar
Linus Torvalds committed
380 381
		xfs_trans_set_sync(args.trans);

382 383 384
	if ((flags & ATTR_KERNOTIME) == 0)
		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);

Linus Torvalds's avatar
Linus Torvalds committed
385 386 387 388
	/*
	 * Commit the last in the sequence of transactions.
	 */
	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
389
	error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds's avatar
Linus Torvalds committed
390 391
	xfs_iunlock(dp, XFS_ILOCK_EXCL);

392
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
393 394

out:
395
	if (args.trans) {
Linus Torvalds's avatar
Linus Torvalds committed
396 397
		xfs_trans_cancel(args.trans,
			XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
398
	}
Linus Torvalds's avatar
Linus Torvalds committed
399
	xfs_iunlock(dp, XFS_ILOCK_EXCL);
400
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
401 402
}

403 404 405 406 407
/*
 * Generic handler routine to remove a name from an attribute list.
 * Transitions attribute list from Btree to shortform as necessary.
 */
int
408 409 410 411
xfs_attr_remove(
	struct xfs_inode	*dp,
	const unsigned char	*name,
	int			flags)
Linus Torvalds's avatar
Linus Torvalds committed
412
{
413 414 415 416 417
	struct xfs_mount	*mp = dp->i_mount;
	struct xfs_da_args	args;
	struct xfs_bmap_free	flist;
	xfs_fsblock_t		firstblock;
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
418

419
	XFS_STATS_INC(xs_attr_remove);
Linus Torvalds's avatar
Linus Torvalds committed
420

421
	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
422 423 424 425
		return EIO;

	if (!xfs_inode_hasattr(dp))
		return ENOATTR;
Linus Torvalds's avatar
Linus Torvalds committed
426

427
	error = xfs_attr_args_init(&args, dp, name, flags);
428 429 430
	if (error)
		return error;

Linus Torvalds's avatar
Linus Torvalds committed
431 432 433
	args.firstblock = &firstblock;
	args.flist = &flist;

434 435 436 437 438 439 440
	/*
	 * we have no control over the attribute names that userspace passes us
	 * to remove, so we have to allow the name lookup prior to attribute
	 * removal to fail.
	 */
	args.op_flags = XFS_DA_OP_OKNOENT;

Christoph Hellwig's avatar
Christoph Hellwig committed
441 442 443
	error = xfs_qm_dqattach(dp, 0);
	if (error)
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

	/*
	 * Start our first transaction of the day.
	 *
	 * All future transactions during this code must be "chained" off
	 * this one via the trans_dup() call.  All transactions will contain
	 * the inode, and the inode will always be marked with trans_ihold().
	 * Since the inode will be locked in all transactions, we must log
	 * the inode in every transaction to let it float upward through
	 * the log.
	 */
	args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);

	/*
	 * Root fork attributes can use reserved data blocks for this
	 * operation if necessary
	 */

	if (flags & ATTR_ROOT)
		args.trans->t_flags |= XFS_TRANS_RESERVE;

465 466 467
	error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
				  XFS_ATTRRM_SPACE_RES(mp), 0);
	if (error) {
Linus Torvalds's avatar
Linus Torvalds committed
468
		xfs_trans_cancel(args.trans, 0);
469
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
470 471 472 473 474 475 476
	}

	xfs_ilock(dp, XFS_ILOCK_EXCL);
	/*
	 * No need to make quota reservations here. We expect to release some
	 * blocks not allocate in the common case.
	 */
477
	xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
478

479
	if (!xfs_inode_hasattr(dp)) {
Linus Torvalds's avatar
Linus Torvalds committed
480
		error = XFS_ERROR(ENOATTR);
481
	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484 485 486 487 488
		ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
		error = xfs_attr_shortform_remove(&args);
	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
		error = xfs_attr_leaf_removename(&args);
	} else {
		error = xfs_attr_node_removename(&args);
	}
489 490

	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496
		goto out;

	/*
	 * If this is a synchronous mount, make sure that the
	 * transaction goes to disk before returning to the user.
	 */
497
	if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds's avatar
Linus Torvalds committed
498 499
		xfs_trans_set_sync(args.trans);

500 501 502
	if ((flags & ATTR_KERNOTIME) == 0)
		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);

Linus Torvalds's avatar
Linus Torvalds committed
503 504 505 506
	/*
	 * Commit the last in the sequence of transactions.
	 */
	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
507
	error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds's avatar
Linus Torvalds committed
508 509
	xfs_iunlock(dp, XFS_ILOCK_EXCL);

510
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
511 512

out:
513
	if (args.trans) {
Linus Torvalds's avatar
Linus Torvalds committed
514 515
		xfs_trans_cancel(args.trans,
			XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
516
	}
517 518
	xfs_iunlock(dp, XFS_ILOCK_EXCL);
	return error;
519 520
}

Linus Torvalds's avatar
Linus Torvalds committed
521 522 523 524 525 526 527 528 529 530 531
/*========================================================================
 * External routines when attribute list is inside the inode
 *========================================================================*/

/*
 * Add a name to the shortform attribute list structure
 * This is the external routine.
 */
STATIC int
xfs_attr_shortform_addname(xfs_da_args_t *args)
{
532
	int newsize, forkoff, retval;
Linus Torvalds's avatar
Linus Torvalds committed
533

534 535
	trace_xfs_attr_sf_addname(args);

Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539 540 541 542 543 544 545
	retval = xfs_attr_shortform_lookup(args);
	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
		return(retval);
	} else if (retval == EEXIST) {
		if (args->flags & ATTR_CREATE)
			return(retval);
		retval = xfs_attr_shortform_remove(args);
		ASSERT(retval == 0);
	}

546 547 548 549
	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
		return(XFS_ERROR(ENOSPC));

Linus Torvalds's avatar
Linus Torvalds committed
550 551
	newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
	newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
552 553 554

	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
	if (!forkoff)
Linus Torvalds's avatar
Linus Torvalds committed
555
		return(XFS_ERROR(ENOSPC));
556 557

	xfs_attr_shortform_add(args, forkoff);
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571
	return(0);
}


/*========================================================================
 * External routines when attribute list is one block
 *========================================================================*/

/*
 * Add a name to the leaf attribute list structure
 *
 * This leaf block cannot have a "remote" value, we only call this routine
 * if bmap_one_block() says there is only one block (ie: no remote blks).
 */
572
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
573 574 575
xfs_attr_leaf_addname(xfs_da_args_t *args)
{
	xfs_inode_t *dp;
576
	struct xfs_buf *bp;
577
	int retval, error, committed, forkoff;
Linus Torvalds's avatar
Linus Torvalds committed
578

579 580
	trace_xfs_attr_leaf_addname(args);

Linus Torvalds's avatar
Linus Torvalds committed
581 582 583 584 585
	/*
	 * Read the (only) block in the attribute list in.
	 */
	dp = args->dp;
	args->blkno = 0;
586
	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds's avatar
Linus Torvalds committed
587
	if (error)
588
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593

	/*
	 * Look up the given attribute in the leaf block.  Figure out if
	 * the given flags produce an error or call for an atomic rename.
	 */
594
	retval = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
595
	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
596
		xfs_trans_brelse(args->trans, bp);
597
		return retval;
Linus Torvalds's avatar
Linus Torvalds committed
598 599
	} else if (retval == EEXIST) {
		if (args->flags & ATTR_CREATE) {	/* pure create op */
600
			xfs_trans_brelse(args->trans, bp);
601
			return retval;
Linus Torvalds's avatar
Linus Torvalds committed
602
		}
603 604 605

		trace_xfs_attr_leaf_replace(args);

606
		/* save the attribute state for later removal*/
607
		args->op_flags |= XFS_DA_OP_RENAME;	/* an atomic rename */
Linus Torvalds's avatar
Linus Torvalds committed
608 609 610 611
		args->blkno2 = args->blkno;		/* set 2nd entry info*/
		args->index2 = args->index;
		args->rmtblkno2 = args->rmtblkno;
		args->rmtblkcnt2 = args->rmtblkcnt;
612 613 614 615 616 617 618 619 620 621
		args->rmtvaluelen2 = args->rmtvaluelen;

		/*
		 * clear the remote attr state now that it is saved so that the
		 * values reflect the state of the attribute we are about to
		 * add, not the attribute we just found and will remove later.
		 */
		args->rmtblkno = 0;
		args->rmtblkcnt = 0;
		args->rmtvaluelen = 0;
Linus Torvalds's avatar
Linus Torvalds committed
622 623 624 625 626 627
	}

	/*
	 * Add the attribute to the leaf block, transitioning to a Btree
	 * if required.
	 */
628
	retval = xfs_attr3_leaf_add(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
629 630 631 632 633 634
	if (retval == ENOSPC) {
		/*
		 * Promote the attribute list to the Btree format, then
		 * Commit that transaction so that the node_addname() call
		 * can manage its own transactions.
		 */
635
		xfs_bmap_init(args->flist, args->firstblock);
636
		error = xfs_attr3_leaf_to_node(args);
Linus Torvalds's avatar
Linus Torvalds committed
637 638
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
639
						&committed);
Linus Torvalds's avatar
Linus Torvalds committed
640 641 642 643 644 645 646 647 648 649 650 651
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
			return(error);
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
652
		if (committed)
653
			xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658

		/*
		 * Commit the current trans (including the inode) and start
		 * a new one.
		 */
659 660
		error = xfs_trans_roll(&args->trans, dp);
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
661 662 663 664 665 666 667 668 669 670 671 672 673
			return (error);

		/*
		 * Fob the whole rest of the problem off on the Btree code.
		 */
		error = xfs_attr_node_addname(args);
		return(error);
	}

	/*
	 * Commit the transaction that added the attr name so that
	 * later routines can manage their own transactions.
	 */
674 675
	error = xfs_trans_roll(&args->trans, dp);
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
		return (error);

	/*
	 * If there was an out-of-line value, allocate the blocks we
	 * identified for its storage and copy the value.  This is done
	 * after we create the attribute so that we don't overflow the
	 * maximum size of a transaction and/or hit a deadlock.
	 */
	if (args->rmtblkno > 0) {
		error = xfs_attr_rmtval_set(args);
		if (error)
			return(error);
	}

	/*
	 * If this is an atomic rename operation, we must "flip" the
	 * incomplete flags on the "new" and "old" attribute/value pairs
	 * so that one disappears and one appears atomically.  Then we
	 * must remove the "old" attribute/value pair.
	 */
696
	if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds's avatar
Linus Torvalds committed
697 698 699 700
		/*
		 * In a separate transaction, set the incomplete flag on the
		 * "old" attr and clear the incomplete flag on the "new" attr.
		 */
701
		error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705 706 707 708 709 710 711 712
		if (error)
			return(error);

		/*
		 * Dismantle the "old" attribute/value pair by removing
		 * a "remote" value (if it exists).
		 */
		args->index = args->index2;
		args->blkno = args->blkno2;
		args->rmtblkno = args->rmtblkno2;
		args->rmtblkcnt = args->rmtblkcnt2;
713
		args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717 718 719 720 721 722 723
		if (args->rmtblkno) {
			error = xfs_attr_rmtval_remove(args);
			if (error)
				return(error);
		}

		/*
		 * Read in the block containing the "old" attr, then
		 * remove the "old" attr from that block (neat, huh!)
		 */
724
		error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
725
					   -1, &bp);
Linus Torvalds's avatar
Linus Torvalds committed
726
		if (error)
727 728
			return error;

729
		xfs_attr3_leaf_remove(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
730 731 732 733

		/*
		 * If the result is small enough, shrink it all into the inode.
		 */
734
		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
735
			xfs_bmap_init(args->flist, args->firstblock);
736
			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds's avatar
Linus Torvalds committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
			/* bp is gone due to xfs_da_shrink_inode */
			if (!error) {
				error = xfs_bmap_finish(&args->trans,
							args->flist,
							&committed);
			}
			if (error) {
				ASSERT(committed);
				args->trans = NULL;
				xfs_bmap_cancel(args->flist);
				return(error);
			}

			/*
			 * bmap_finish() may have committed the last trans
			 * and started a new one.  We need the inode to be
			 * in all transactions.
			 */
755
			if (committed)
756
				xfs_trans_ijoin(args->trans, dp, 0);
757
		}
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760 761

		/*
		 * Commit the remove and start the next trans in series.
		 */
762
		error = xfs_trans_roll(&args->trans, dp);
Linus Torvalds's avatar
Linus Torvalds committed
763 764 765 766 767

	} else if (args->rmtblkno > 0) {
		/*
		 * Added a "remote" value, just clear the incomplete flag.
		 */
768
		error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds's avatar
Linus Torvalds committed
769
	}
770
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
771 772 773 774 775 776 777 778 779 780 781 782
}

/*
 * Remove a name from the leaf attribute list structure
 *
 * This leaf block cannot have a "remote" value, we only call this routine
 * if bmap_one_block() says there is only one block (ie: no remote blks).
 */
STATIC int
xfs_attr_leaf_removename(xfs_da_args_t *args)
{
	xfs_inode_t *dp;
783
	struct xfs_buf *bp;
784
	int error, committed, forkoff;
Linus Torvalds's avatar
Linus Torvalds committed
785

786 787
	trace_xfs_attr_leaf_removename(args);

Linus Torvalds's avatar
Linus Torvalds committed
788 789 790 791 792
	/*
	 * Remove the attribute.
	 */
	dp = args->dp;
	args->blkno = 0;
793
	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
794 795
	if (error)
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
796

797
	error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
798
	if (error == ENOATTR) {
799
		xfs_trans_brelse(args->trans, bp);
800
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
801 802
	}

803
	xfs_attr3_leaf_remove(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
804 805 806 807

	/*
	 * If the result is small enough, shrink it all into the inode.
	 */
808
	if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
809
		xfs_bmap_init(args->flist, args->firstblock);
810
		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813
		/* bp is gone due to xfs_da_shrink_inode */
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
814
						&committed);
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
820
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
821 822 823 824 825 826
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
827
		if (committed)
828
			xfs_trans_ijoin(args->trans, dp, 0);
829
	}
830
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835 836 837 838
}

/*
 * Look up a name in a leaf attribute list structure.
 *
 * This leaf block cannot have a "remote" value, we only call this routine
 * if bmap_one_block() says there is only one block (ie: no remote blks).
 */
839
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
840 841
xfs_attr_leaf_get(xfs_da_args_t *args)
{
842
	struct xfs_buf *bp;
Linus Torvalds's avatar
Linus Torvalds committed
843 844
	int error;

845 846
	trace_xfs_attr_leaf_get(args);

Linus Torvalds's avatar
Linus Torvalds committed
847
	args->blkno = 0;
848
	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds's avatar
Linus Torvalds committed
849
	if (error)
850
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
851

852
	error = xfs_attr3_leaf_lookup_int(bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
853
	if (error != EEXIST)  {
854
		xfs_trans_brelse(args->trans, bp);
855
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
856
	}
857
	error = xfs_attr3_leaf_getvalue(bp, args);
858
	xfs_trans_brelse(args->trans, bp);
Linus Torvalds's avatar
Linus Torvalds committed
859 860 861
	if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
		error = xfs_attr_rmtval_get(args);
	}
862
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
863 864 865
}

/*========================================================================
866
 * External routines when attribute list size > geo->blksize
Linus Torvalds's avatar
Linus Torvalds committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
 *========================================================================*/

/*
 * Add a name to a Btree-format attribute list.
 *
 * This will involve walking down the Btree, and may involve splitting
 * leaf nodes and even splitting intermediate nodes up to and including
 * the root node (a special case of an intermediate node).
 *
 * "Remote" attribute values confuse the issue and atomic rename operations
 * add a whole extra layer of confusion on top of that.
 */
STATIC int
xfs_attr_node_addname(xfs_da_args_t *args)
{
	xfs_da_state_t *state;
	xfs_da_state_blk_t *blk;
	xfs_inode_t *dp;
	xfs_mount_t *mp;
	int committed, retval, error;

888 889
	trace_xfs_attr_node_addname(args);

Linus Torvalds's avatar
Linus Torvalds committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903
	/*
	 * Fill in bucket of arguments/results/context to carry around.
	 */
	dp = args->dp;
	mp = dp->i_mount;
restart:
	state = xfs_da_state_alloc();
	state->args = args;
	state->mp = mp;

	/*
	 * Search to see if name already exists, and get back a pointer
	 * to where it should go.
	 */
904
	error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds's avatar
Linus Torvalds committed
905 906 907 908 909 910 911 912 913
	if (error)
		goto out;
	blk = &state->path.blk[ state->path.active-1 ];
	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
	if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
		goto out;
	} else if (retval == EEXIST) {
		if (args->flags & ATTR_CREATE)
			goto out;
914 915 916

		trace_xfs_attr_node_replace(args);

917
		/* save the attribute state for later removal*/
918
		args->op_flags |= XFS_DA_OP_RENAME;	/* atomic rename op */
Linus Torvalds's avatar
Linus Torvalds committed
919 920 921 922
		args->blkno2 = args->blkno;		/* set 2nd entry info*/
		args->index2 = args->index;
		args->rmtblkno2 = args->rmtblkno;
		args->rmtblkcnt2 = args->rmtblkcnt;
923 924 925 926 927 928 929
		args->rmtvaluelen2 = args->rmtvaluelen;

		/*
		 * clear the remote attr state now that it is saved so that the
		 * values reflect the state of the attribute we are about to
		 * add, not the attribute we just found and will remove later.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
930 931
		args->rmtblkno = 0;
		args->rmtblkcnt = 0;
932
		args->rmtvaluelen = 0;
Linus Torvalds's avatar
Linus Torvalds committed
933 934
	}

935
	retval = xfs_attr3_leaf_add(blk->bp, state->args);
Linus Torvalds's avatar
Linus Torvalds committed
936 937 938 939 940 941 942 943
	if (retval == ENOSPC) {
		if (state->path.active == 1) {
			/*
			 * Its really a single leaf node, but it had
			 * out-of-line values so it looked like it *might*
			 * have been a b-tree.
			 */
			xfs_da_state_free(state);
944
			state = NULL;
945
			xfs_bmap_init(args->flist, args->firstblock);
946
			error = xfs_attr3_leaf_to_node(args);
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
			if (!error) {
				error = xfs_bmap_finish(&args->trans,
							args->flist,
							&committed);
			}
			if (error) {
				ASSERT(committed);
				args->trans = NULL;
				xfs_bmap_cancel(args->flist);
				goto out;
			}

			/*
			 * bmap_finish() may have committed the last trans
			 * and started a new one.  We need the inode to be
			 * in all transactions.
			 */
964
			if (committed)
965
				xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
966 967 968 969 970

			/*
			 * Commit the node conversion and start the next
			 * trans in the chain.
			 */
971 972
			error = xfs_trans_roll(&args->trans, dp);
			if (error)
Linus Torvalds's avatar
Linus Torvalds committed
973 974 975 976 977 978 979 980 981 982 983
				goto out;

			goto restart;
		}

		/*
		 * Split as many Btree elements as required.
		 * This code tracks the new and old attr's location
		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
		 */
984
		xfs_bmap_init(args->flist, args->firstblock);
985
		error = xfs_da3_split(state);
Linus Torvalds's avatar
Linus Torvalds committed
986 987
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
988
						&committed);
Linus Torvalds's avatar
Linus Torvalds committed
989 990 991 992 993 994 995 996 997 998 999 1000
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
			goto out;
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
1001
		if (committed)
1002
			xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1003 1004 1005 1006
	} else {
		/*
		 * Addition succeeded, update Btree hashvals.
		 */
1007
		xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	}

	/*
	 * Kill the state structure, we're done with it and need to
	 * allow the buffers to come back later.
	 */
	xfs_da_state_free(state);
	state = NULL;

	/*
	 * Commit the leaf addition or btree split and start the next
	 * trans in the chain.
	 */
1021 1022
	error = xfs_trans_roll(&args->trans, dp);
	if (error)
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
		goto out;

	/*
	 * If there was an out-of-line value, allocate the blocks we
	 * identified for its storage and copy the value.  This is done
	 * after we create the attribute so that we don't overflow the
	 * maximum size of a transaction and/or hit a deadlock.
	 */
	if (args->rmtblkno > 0) {
		error = xfs_attr_rmtval_set(args);
		if (error)
			return(error);
	}

	/*
	 * If this is an atomic rename operation, we must "flip" the
	 * incomplete flags on the "new" and "old" attribute/value pairs
	 * so that one disappears and one appears atomically.  Then we
	 * must remove the "old" attribute/value pair.
	 */
1043
	if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds's avatar
Linus Torvalds committed
1044 1045 1046 1047
		/*
		 * In a separate transaction, set the incomplete flag on the
		 * "old" attr and clear the incomplete flag on the "new" attr.
		 */
1048
		error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds's avatar
Linus Torvalds committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		if (error)
			goto out;

		/*
		 * Dismantle the "old" attribute/value pair by removing
		 * a "remote" value (if it exists).
		 */
		args->index = args->index2;
		args->blkno = args->blkno2;
		args->rmtblkno = args->rmtblkno2;
		args->rmtblkcnt = args->rmtblkcnt2;
1060
		args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
		if (args->rmtblkno) {
			error = xfs_attr_rmtval_remove(args);
			if (error)
				return(error);
		}

		/*
		 * Re-find the "old" attribute entry after any split ops.
		 * The INCOMPLETE flag means that we will find the "old"
		 * attr, not the "new" one.
		 */
		args->flags |= XFS_ATTR_INCOMPLETE;
		state = xfs_da_state_alloc();
		state->args = args;
		state->mp = mp;
		state->inleaf = 0;
1077
		error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079 1080 1081 1082 1083 1084 1085
		if (error)
			goto out;

		/*
		 * Remove the name and update the hashvals in the tree.
		 */
		blk = &state->path.blk[ state->path.active-1 ];
		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1086
		error = xfs_attr3_leaf_remove(blk->bp, args);
1087
		xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds's avatar
Linus Torvalds committed
1088 1089 1090 1091 1092

		/*
		 * Check to see if the tree needs to be collapsed.
		 */
		if (retval && (state->path.active > 1)) {
1093
			xfs_bmap_init(args->flist, args->firstblock);
1094
			error = xfs_da3_join(state);
Linus Torvalds's avatar
Linus Torvalds committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
			if (!error) {
				error = xfs_bmap_finish(&args->trans,
							args->flist,
							&committed);
			}
			if (error) {
				ASSERT(committed);
				args->trans = NULL;
				xfs_bmap_cancel(args->flist);
				goto out;
			}

			/*
			 * bmap_finish() may have committed the last trans
			 * and started a new one.  We need the inode to be
			 * in all transactions.
			 */
1112
			if (committed)
1113
				xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1114 1115 1116 1117 1118
		}

		/*
		 * Commit and start the next trans in the chain.
		 */
1119 1120
		error = xfs_trans_roll(&args->trans, dp);
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
1121 1122 1123 1124 1125 1126
			goto out;

	} else if (args->rmtblkno > 0) {
		/*
		 * Added a "remote" value, just clear the incomplete flag.
		 */
1127
		error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
		if (error)
			goto out;
	}
	retval = error = 0;

out:
	if (state)
		xfs_da_state_free(state);
	if (error)
		return(error);
	return(retval);
}

/*
 * Remove a name from a B-tree attribute list.
 *
 * This will involve walking down the Btree, and may involve joining
 * leaf nodes and even joining intermediate nodes up to and including
 * the root node (a special case of an intermediate node).
 */
STATIC int
xfs_attr_node_removename(xfs_da_args_t *args)
{
	xfs_da_state_t *state;
	xfs_da_state_blk_t *blk;
	xfs_inode_t *dp;
1154
	struct xfs_buf *bp;
1155
	int retval, error, committed, forkoff;
Linus Torvalds's avatar
Linus Torvalds committed
1156

1157 1158
	trace_xfs_attr_node_removename(args);

Linus Torvalds's avatar
Linus Torvalds committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	/*
	 * Tie a string around our finger to remind us where we are.
	 */
	dp = args->dp;
	state = xfs_da_state_alloc();
	state->args = args;
	state->mp = dp->i_mount;

	/*
	 * Search to see if name exists, and get back a pointer to it.
	 */
1170
	error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds's avatar
Linus Torvalds committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	if (error || (retval != EEXIST)) {
		if (error == 0)
			error = retval;
		goto out;
	}

	/*
	 * If there is an out-of-line value, de-allocate the blocks.
	 * This is done before we remove the attribute so that we don't
	 * overflow the maximum size of a transaction and/or hit a deadlock.
	 */
	blk = &state->path.blk[ state->path.active-1 ];
	ASSERT(blk->bp != NULL);
	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
	if (args->rmtblkno > 0) {
		/*
		 * Fill in disk block numbers in the state structure
		 * so that we can get the buffers back after we commit
		 * several transactions in the following calls.
		 */
		error = xfs_attr_fillstate(state);
		if (error)
			goto out;

		/*
		 * Mark the attribute as INCOMPLETE, then bunmapi() the
		 * remote value.
		 */
1199
		error = xfs_attr3_leaf_setflag(args);
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		if (error)
			goto out;
		error = xfs_attr_rmtval_remove(args);
		if (error)
			goto out;

		/*
		 * Refill the state structure with buffers, the prior calls
		 * released our buffers.
		 */
		error = xfs_attr_refillstate(state);
		if (error)
			goto out;
	}

	/*
	 * Remove the name and update the hashvals in the tree.
	 */
	blk = &state->path.blk[ state->path.active-1 ];
	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1220
	retval = xfs_attr3_leaf_remove(blk->bp, args);
1221
	xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223 1224 1225 1226

	/*
	 * Check to see if the tree needs to be collapsed.
	 */
	if (retval && (state->path.active > 1)) {
1227
		xfs_bmap_init(args->flist, args->firstblock);
1228
		error = xfs_da3_join(state);
Linus Torvalds's avatar
Linus Torvalds committed
1229 1230
		if (!error) {
			error = xfs_bmap_finish(&args->trans, args->flist,
1231
						&committed);
Linus Torvalds's avatar
Linus Torvalds committed
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
		}
		if (error) {
			ASSERT(committed);
			args->trans = NULL;
			xfs_bmap_cancel(args->flist);
			goto out;
		}

		/*
		 * bmap_finish() may have committed the last trans and started
		 * a new one.  We need the inode to be in all transactions.
		 */
1244
		if (committed)
1245
			xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1246 1247 1248 1249

		/*
		 * Commit the Btree join operation and start a new trans.
		 */
1250 1251
		error = xfs_trans_roll(&args->trans, dp);
		if (error)
Linus Torvalds's avatar
Linus Torvalds committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
			goto out;
	}

	/*
	 * If the result is small enough, push it all into the inode.
	 */
	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
		/*
		 * Have to get rid of the copy of this dabuf in the state.
		 */
		ASSERT(state->path.active == 1);
		ASSERT(state->path.blk[0].bp);
		state->path.blk[0].bp = NULL;

1266
		error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269
		if (error)
			goto out;

1270
		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1271
			xfs_bmap_init(args->flist, args->firstblock);
1272
			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds's avatar
Linus Torvalds committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
			/* bp is gone due to xfs_da_shrink_inode */
			if (!error) {
				error = xfs_bmap_finish(&args->trans,
							args->flist,
							&committed);
			}
			if (error) {
				ASSERT(committed);
				args->trans = NULL;
				xfs_bmap_cancel(args->flist);
				goto out;
			}

			/*
			 * bmap_finish() may have committed the last trans
			 * and started a new one.  We need the inode to be
			 * in all transactions.
			 */
1291
			if (committed)
1292
				xfs_trans_ijoin(args->trans, dp, 0);
Linus Torvalds's avatar
Linus Torvalds committed
1293
		} else
1294
			xfs_trans_brelse(args->trans, bp);
Linus Torvalds's avatar
Linus Torvalds committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	}
	error = 0;

out:
	xfs_da_state_free(state);
	return(error);
}

/*
 * Fill in the disk block numbers in the state structure for the buffers
 * that are attached to the state structure.
 * This is done so that we can quickly reattach ourselves to those buffers
1307
 * after some set of transaction commits have released these buffers.
Linus Torvalds's avatar
Linus Torvalds committed
1308 1309 1310 1311 1312 1313 1314 1315
 */
STATIC int
xfs_attr_fillstate(xfs_da_state_t *state)
{
	xfs_da_state_path_t *path;
	xfs_da_state_blk_t *blk;
	int level;

1316 1317
	trace_xfs_attr_fillstate(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1318 1319 1320 1321 1322 1323 1324 1325
	/*
	 * Roll down the "path" in the state structure, storing the on-disk
	 * block number for those buffers in the "path".
	 */
	path = &state->path;
	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
		if (blk->bp) {
1326
			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds's avatar
Linus Torvalds committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
			blk->bp = NULL;
		} else {
			blk->disk_blkno = 0;
		}
	}

	/*
	 * Roll down the "altpath" in the state structure, storing the on-disk
	 * block number for those buffers in the "altpath".
	 */
	path = &state->altpath;
	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
		if (blk->bp) {
1341
			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds's avatar
Linus Torvalds committed
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
			blk->bp = NULL;
		} else {
			blk->disk_blkno = 0;
		}
	}

	return(0);
}

/*
 * Reattach the buffers to the state structure based on the disk block
 * numbers stored in the state structure.
1354
 * This is done after some set of transaction commits have released those
Linus Torvalds's avatar
Linus Torvalds committed
1355 1356 1357 1358 1359 1360 1361 1362 1363
 * buffers from our grip.
 */
STATIC int
xfs_attr_refillstate(xfs_da_state_t *state)
{
	xfs_da_state_path_t *path;
	xfs_da_state_blk_t *blk;
	int level, error;

1364 1365
	trace_xfs_attr_refillstate(state->args);

Linus Torvalds's avatar
Linus Torvalds committed
1366 1367 1368 1369 1370 1371 1372 1373
	/*
	 * Roll down the "path" in the state structure, storing the on-disk
	 * block number for those buffers in the "path".
	 */
	path = &state->path;
	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
		if (blk->disk_blkno) {
1374
			error = xfs_da3_node_read(state->args->trans,
Linus Torvalds's avatar
Linus Torvalds committed
1375 1376
						state->args->dp,
						blk->blkno, blk->disk_blkno,
1377
						&blk->bp, XFS_ATTR_FORK);
Linus Torvalds's avatar
Linus Torvalds committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
			if (error)
				return(error);
		} else {
			blk->bp = NULL;
		}
	}

	/*
	 * Roll down the "altpath" in the state structure, storing the on-disk
	 * block number for those buffers in the "altpath".
	 */
	path = &state->altpath;
	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
		if (blk->disk_blkno) {
1393
			error = xfs_da3_node_read(state->args->trans,
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395
						state->args->dp,
						blk->blkno, blk->disk_blkno,
1396
						&blk->bp, XFS_ATTR_FORK);
Linus Torvalds's avatar
Linus Torvalds committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
			if (error)
				return(error);
		} else {
			blk->bp = NULL;
		}
	}

	return(0);
}

/*
 * Look up a filename in a node attribute list.
 *
 * This routine gets called for any attribute fork that has more than one
 * block, ie: both true Btree attr lists and for single-leaf-blocks with
 * "remote" values taking up more blocks.
 */
1414
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
1415 1416 1417 1418 1419 1420 1421
xfs_attr_node_get(xfs_da_args_t *args)
{
	xfs_da_state_t *state;
	xfs_da_state_blk_t *blk;
	int error, retval;
	int i;

1422 1423
	trace_xfs_attr_node_get(args);

Linus Torvalds's avatar
Linus Torvalds committed
1424 1425 1426 1427 1428 1429 1430
	state = xfs_da_state_alloc();
	state->args = args;
	state->mp = args->dp->i_mount;

	/*
	 * Search to see if name exists, and get back a pointer to it.
	 */
1431
	error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds's avatar
Linus Torvalds committed
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
	if (error) {
		retval = error;
	} else if (retval == EEXIST) {
		blk = &state->path.blk[ state->path.active-1 ];
		ASSERT(blk->bp != NULL);
		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);

		/*
		 * Get the value, local or "remote"
		 */
1442
		retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds's avatar
Linus Torvalds committed
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
		if (!retval && (args->rmtblkno > 0)
		    && !(args->flags & ATTR_KERNOVAL)) {
			retval = xfs_attr_rmtval_get(args);
		}
	}

	/*
	 * If not in a transaction, we have to release all the buffers.
	 */
	for (i = 0; i < state->path.active; i++) {
1453
		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds's avatar
Linus Torvalds committed
1454 1455 1456 1457 1458 1459
		state->path.blk[i].bp = NULL;
	}

	xfs_da_state_free(state);
	return(retval);
}