xref: /linux/fs/xfs/libxfs/xfs_attr.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_attr_sf.h"
18 #include "xfs_inode.h"
19 #include "xfs_alloc.h"
20 #include "xfs_trans.h"
21 #include "xfs_inode_item.h"
22 #include "xfs_bmap.h"
23 #include "xfs_bmap_util.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_attr.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_attr_remote.h"
28 #include "xfs_error.h"
29 #include "xfs_quota.h"
30 #include "xfs_trans_space.h"
31 #include "xfs_trace.h"
32 
33 /*
34  * xfs_attr.c
35  *
36  * Provide the external interfaces to manage attribute lists.
37  */
38 
39 /*========================================================================
40  * Function prototypes for the kernel.
41  *========================================================================*/
42 
43 /*
44  * Internal routines when attribute list fits inside the inode.
45  */
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
47 
48 /*
49  * Internal routines when attribute list is one block.
50  */
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
54 
55 /*
56  * Internal routines when attribute list is more than one block.
57  */
58 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
59 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
60 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
61 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
62 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
63 
64 
65 STATIC int
66 xfs_attr_args_init(
67 	struct xfs_da_args	*args,
68 	struct xfs_inode	*dp,
69 	const unsigned char	*name,
70 	int			flags)
71 {
72 
73 	if (!name)
74 		return -EINVAL;
75 
76 	memset(args, 0, sizeof(*args));
77 	args->geo = dp->i_mount->m_attr_geo;
78 	args->whichfork = XFS_ATTR_FORK;
79 	args->dp = dp;
80 	args->flags = flags;
81 	args->name = name;
82 	args->namelen = strlen((const char *)name);
83 	if (args->namelen >= MAXNAMELEN)
84 		return -EFAULT;		/* match IRIX behaviour */
85 
86 	args->hashval = xfs_da_hashname(args->name, args->namelen);
87 	return 0;
88 }
89 
90 int
91 xfs_inode_hasattr(
92 	struct xfs_inode	*ip)
93 {
94 	if (!XFS_IFORK_Q(ip) ||
95 	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
96 	     ip->i_d.di_anextents == 0))
97 		return 0;
98 	return 1;
99 }
100 
101 /*========================================================================
102  * Overall external interface routines.
103  *========================================================================*/
104 
105 /* Retrieve an extended attribute and its value.  Must have ilock. */
106 int
107 xfs_attr_get_ilocked(
108 	struct xfs_inode	*ip,
109 	struct xfs_da_args	*args)
110 {
111 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
112 
113 	if (!xfs_inode_hasattr(ip))
114 		return -ENOATTR;
115 	else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
116 		return xfs_attr_shortform_getvalue(args);
117 	else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
118 		return xfs_attr_leaf_get(args);
119 	else
120 		return xfs_attr_node_get(args);
121 }
122 
123 /* Retrieve an extended attribute by name, and its value. */
124 int
125 xfs_attr_get(
126 	struct xfs_inode	*ip,
127 	const unsigned char	*name,
128 	unsigned char		*value,
129 	int			*valuelenp,
130 	int			flags)
131 {
132 	struct xfs_da_args	args;
133 	uint			lock_mode;
134 	int			error;
135 
136 	XFS_STATS_INC(ip->i_mount, xs_attr_get);
137 
138 	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
139 		return -EIO;
140 
141 	error = xfs_attr_args_init(&args, ip, name, flags);
142 	if (error)
143 		return error;
144 
145 	args.value = value;
146 	args.valuelen = *valuelenp;
147 	/* Entirely possible to look up a name which doesn't exist */
148 	args.op_flags = XFS_DA_OP_OKNOENT;
149 
150 	lock_mode = xfs_ilock_attr_map_shared(ip);
151 	error = xfs_attr_get_ilocked(ip, &args);
152 	xfs_iunlock(ip, lock_mode);
153 
154 	*valuelenp = args.valuelen;
155 	return error == -EEXIST ? 0 : error;
156 }
157 
158 /*
159  * Calculate how many blocks we need for the new attribute,
160  */
161 STATIC int
162 xfs_attr_calc_size(
163 	struct xfs_da_args	*args,
164 	int			*local)
165 {
166 	struct xfs_mount	*mp = args->dp->i_mount;
167 	int			size;
168 	int			nblks;
169 
170 	/*
171 	 * Determine space new attribute will use, and if it would be
172 	 * "local" or "remote" (note: local != inline).
173 	 */
174 	size = xfs_attr_leaf_newentsize(args, local);
175 	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 	if (*local) {
177 		if (size > (args->geo->blksize / 2)) {
178 			/* Double split possible */
179 			nblks *= 2;
180 		}
181 	} else {
182 		/*
183 		 * Out of line attribute, cannot double split, but
184 		 * make room for the attribute value itself.
185 		 */
186 		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
187 		nblks += dblocks;
188 		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189 	}
190 
191 	return nblks;
192 }
193 
194 int
195 xfs_attr_set(
196 	struct xfs_inode	*dp,
197 	const unsigned char	*name,
198 	unsigned char		*value,
199 	int			valuelen,
200 	int			flags)
201 {
202 	struct xfs_mount	*mp = dp->i_mount;
203 	struct xfs_buf		*leaf_bp = NULL;
204 	struct xfs_da_args	args;
205 	struct xfs_trans_res	tres;
206 	int			rsvd = (flags & ATTR_ROOT) != 0;
207 	int			error, err2, local;
208 
209 	XFS_STATS_INC(mp, xs_attr_set);
210 
211 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
212 		return -EIO;
213 
214 	error = xfs_attr_args_init(&args, dp, name, flags);
215 	if (error)
216 		return error;
217 
218 	args.value = value;
219 	args.valuelen = valuelen;
220 	args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
221 	args.total = xfs_attr_calc_size(&args, &local);
222 
223 	error = xfs_qm_dqattach(dp);
224 	if (error)
225 		return error;
226 
227 	/*
228 	 * If the inode doesn't have an attribute fork, add one.
229 	 * (inode must not be locked when we call this routine)
230 	 */
231 	if (XFS_IFORK_Q(dp) == 0) {
232 		int sf_size = sizeof(xfs_attr_sf_hdr_t) +
233 			XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
234 
235 		error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
236 		if (error)
237 			return error;
238 	}
239 
240 	tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
241 			 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
242 	tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
243 	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
244 
245 	/*
246 	 * Root fork attributes can use reserved data blocks for this
247 	 * operation if necessary
248 	 */
249 	error = xfs_trans_alloc(mp, &tres, args.total, 0,
250 			rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
251 	if (error)
252 		return error;
253 
254 	xfs_ilock(dp, XFS_ILOCK_EXCL);
255 	error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
256 				rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
257 				       XFS_QMOPT_RES_REGBLKS);
258 	if (error) {
259 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
260 		xfs_trans_cancel(args.trans);
261 		return error;
262 	}
263 
264 	xfs_trans_ijoin(args.trans, dp, 0);
265 
266 	/*
267 	 * If the attribute list is non-existent or a shortform list,
268 	 * upgrade it to a single-leaf-block attribute list.
269 	 */
270 	if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
271 	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
272 	     dp->i_d.di_anextents == 0)) {
273 
274 		/*
275 		 * Build initial attribute list (if required).
276 		 */
277 		if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
278 			xfs_attr_shortform_create(&args);
279 
280 		/*
281 		 * Try to add the attr to the attribute list in
282 		 * the inode.
283 		 */
284 		error = xfs_attr_shortform_addname(&args);
285 		if (error != -ENOSPC) {
286 			/*
287 			 * Commit the shortform mods, and we're done.
288 			 * NOTE: this is also the error path (EEXIST, etc).
289 			 */
290 			ASSERT(args.trans != NULL);
291 
292 			/*
293 			 * If this is a synchronous mount, make sure that
294 			 * the transaction goes to disk before returning
295 			 * to the user.
296 			 */
297 			if (mp->m_flags & XFS_MOUNT_WSYNC)
298 				xfs_trans_set_sync(args.trans);
299 
300 			if (!error && (flags & ATTR_KERNOTIME) == 0) {
301 				xfs_trans_ichgtime(args.trans, dp,
302 							XFS_ICHGTIME_CHG);
303 			}
304 			err2 = xfs_trans_commit(args.trans);
305 			xfs_iunlock(dp, XFS_ILOCK_EXCL);
306 
307 			return error ? error : err2;
308 		}
309 
310 		/*
311 		 * It won't fit in the shortform, transform to a leaf block.
312 		 * GROT: another possible req'mt for a double-split btree op.
313 		 */
314 		error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
315 		if (error)
316 			goto out;
317 		/*
318 		 * Prevent the leaf buffer from being unlocked so that a
319 		 * concurrent AIL push cannot grab the half-baked leaf
320 		 * buffer and run into problems with the write verifier.
321 		 */
322 		xfs_trans_bhold(args.trans, leaf_bp);
323 		error = xfs_defer_finish(&args.trans);
324 		if (error)
325 			goto out;
326 
327 		/*
328 		 * Commit the leaf transformation.  We'll need another (linked)
329 		 * transaction to add the new attribute to the leaf, which
330 		 * means that we have to hold & join the leaf buffer here too.
331 		 */
332 		error = xfs_trans_roll_inode(&args.trans, dp);
333 		if (error)
334 			goto out;
335 		xfs_trans_bjoin(args.trans, leaf_bp);
336 		leaf_bp = NULL;
337 	}
338 
339 	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
340 		error = xfs_attr_leaf_addname(&args);
341 	else
342 		error = xfs_attr_node_addname(&args);
343 	if (error)
344 		goto out;
345 
346 	/*
347 	 * If this is a synchronous mount, make sure that the
348 	 * transaction goes to disk before returning to the user.
349 	 */
350 	if (mp->m_flags & XFS_MOUNT_WSYNC)
351 		xfs_trans_set_sync(args.trans);
352 
353 	if ((flags & ATTR_KERNOTIME) == 0)
354 		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
355 
356 	/*
357 	 * Commit the last in the sequence of transactions.
358 	 */
359 	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
360 	error = xfs_trans_commit(args.trans);
361 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
362 
363 	return error;
364 
365 out:
366 	if (leaf_bp)
367 		xfs_trans_brelse(args.trans, leaf_bp);
368 	if (args.trans)
369 		xfs_trans_cancel(args.trans);
370 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
371 	return error;
372 }
373 
374 /*
375  * Generic handler routine to remove a name from an attribute list.
376  * Transitions attribute list from Btree to shortform as necessary.
377  */
378 int
379 xfs_attr_remove(
380 	struct xfs_inode	*dp,
381 	const unsigned char	*name,
382 	int			flags)
383 {
384 	struct xfs_mount	*mp = dp->i_mount;
385 	struct xfs_da_args	args;
386 	int			error;
387 
388 	XFS_STATS_INC(mp, xs_attr_remove);
389 
390 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
391 		return -EIO;
392 
393 	error = xfs_attr_args_init(&args, dp, name, flags);
394 	if (error)
395 		return error;
396 
397 	/*
398 	 * we have no control over the attribute names that userspace passes us
399 	 * to remove, so we have to allow the name lookup prior to attribute
400 	 * removal to fail.
401 	 */
402 	args.op_flags = XFS_DA_OP_OKNOENT;
403 
404 	error = xfs_qm_dqattach(dp);
405 	if (error)
406 		return error;
407 
408 	/*
409 	 * Root fork attributes can use reserved data blocks for this
410 	 * operation if necessary
411 	 */
412 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
413 			XFS_ATTRRM_SPACE_RES(mp), 0,
414 			(flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
415 			&args.trans);
416 	if (error)
417 		return error;
418 
419 	xfs_ilock(dp, XFS_ILOCK_EXCL);
420 	/*
421 	 * No need to make quota reservations here. We expect to release some
422 	 * blocks not allocate in the common case.
423 	 */
424 	xfs_trans_ijoin(args.trans, dp, 0);
425 
426 	if (!xfs_inode_hasattr(dp)) {
427 		error = -ENOATTR;
428 	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
429 		ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
430 		error = xfs_attr_shortform_remove(&args);
431 	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
432 		error = xfs_attr_leaf_removename(&args);
433 	} else {
434 		error = xfs_attr_node_removename(&args);
435 	}
436 
437 	if (error)
438 		goto out;
439 
440 	/*
441 	 * If this is a synchronous mount, make sure that the
442 	 * transaction goes to disk before returning to the user.
443 	 */
444 	if (mp->m_flags & XFS_MOUNT_WSYNC)
445 		xfs_trans_set_sync(args.trans);
446 
447 	if ((flags & ATTR_KERNOTIME) == 0)
448 		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
449 
450 	/*
451 	 * Commit the last in the sequence of transactions.
452 	 */
453 	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
454 	error = xfs_trans_commit(args.trans);
455 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
456 
457 	return error;
458 
459 out:
460 	if (args.trans)
461 		xfs_trans_cancel(args.trans);
462 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
463 	return error;
464 }
465 
466 /*========================================================================
467  * External routines when attribute list is inside the inode
468  *========================================================================*/
469 
470 /*
471  * Add a name to the shortform attribute list structure
472  * This is the external routine.
473  */
474 STATIC int
475 xfs_attr_shortform_addname(xfs_da_args_t *args)
476 {
477 	int newsize, forkoff, retval;
478 
479 	trace_xfs_attr_sf_addname(args);
480 
481 	retval = xfs_attr_shortform_lookup(args);
482 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
483 		return retval;
484 	} else if (retval == -EEXIST) {
485 		if (args->flags & ATTR_CREATE)
486 			return retval;
487 		retval = xfs_attr_shortform_remove(args);
488 		if (retval)
489 			return retval;
490 		/*
491 		 * Since we have removed the old attr, clear ATTR_REPLACE so
492 		 * that the leaf format add routine won't trip over the attr
493 		 * not being around.
494 		 */
495 		args->flags &= ~ATTR_REPLACE;
496 	}
497 
498 	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
499 	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
500 		return -ENOSPC;
501 
502 	newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
503 	newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
504 
505 	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
506 	if (!forkoff)
507 		return -ENOSPC;
508 
509 	xfs_attr_shortform_add(args, forkoff);
510 	return 0;
511 }
512 
513 
514 /*========================================================================
515  * External routines when attribute list is one block
516  *========================================================================*/
517 
518 /*
519  * Add a name to the leaf attribute list structure
520  *
521  * This leaf block cannot have a "remote" value, we only call this routine
522  * if bmap_one_block() says there is only one block (ie: no remote blks).
523  */
524 STATIC int
525 xfs_attr_leaf_addname(
526 	struct xfs_da_args	*args)
527 {
528 	struct xfs_inode	*dp;
529 	struct xfs_buf		*bp;
530 	int			retval, error, forkoff;
531 
532 	trace_xfs_attr_leaf_addname(args);
533 
534 	/*
535 	 * Read the (only) block in the attribute list in.
536 	 */
537 	dp = args->dp;
538 	args->blkno = 0;
539 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
540 	if (error)
541 		return error;
542 
543 	/*
544 	 * Look up the given attribute in the leaf block.  Figure out if
545 	 * the given flags produce an error or call for an atomic rename.
546 	 */
547 	retval = xfs_attr3_leaf_lookup_int(bp, args);
548 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
549 		xfs_trans_brelse(args->trans, bp);
550 		return retval;
551 	} else if (retval == -EEXIST) {
552 		if (args->flags & ATTR_CREATE) {	/* pure create op */
553 			xfs_trans_brelse(args->trans, bp);
554 			return retval;
555 		}
556 
557 		trace_xfs_attr_leaf_replace(args);
558 
559 		/* save the attribute state for later removal*/
560 		args->op_flags |= XFS_DA_OP_RENAME;	/* an atomic rename */
561 		args->blkno2 = args->blkno;		/* set 2nd entry info*/
562 		args->index2 = args->index;
563 		args->rmtblkno2 = args->rmtblkno;
564 		args->rmtblkcnt2 = args->rmtblkcnt;
565 		args->rmtvaluelen2 = args->rmtvaluelen;
566 
567 		/*
568 		 * clear the remote attr state now that it is saved so that the
569 		 * values reflect the state of the attribute we are about to
570 		 * add, not the attribute we just found and will remove later.
571 		 */
572 		args->rmtblkno = 0;
573 		args->rmtblkcnt = 0;
574 		args->rmtvaluelen = 0;
575 	}
576 
577 	/*
578 	 * Add the attribute to the leaf block, transitioning to a Btree
579 	 * if required.
580 	 */
581 	retval = xfs_attr3_leaf_add(bp, args);
582 	if (retval == -ENOSPC) {
583 		/*
584 		 * Promote the attribute list to the Btree format, then
585 		 * Commit that transaction so that the node_addname() call
586 		 * can manage its own transactions.
587 		 */
588 		error = xfs_attr3_leaf_to_node(args);
589 		if (error)
590 			goto out_defer_cancel;
591 		error = xfs_defer_finish(&args->trans);
592 		if (error)
593 			return error;
594 
595 		/*
596 		 * Commit the current trans (including the inode) and start
597 		 * a new one.
598 		 */
599 		error = xfs_trans_roll_inode(&args->trans, dp);
600 		if (error)
601 			return error;
602 
603 		/*
604 		 * Fob the whole rest of the problem off on the Btree code.
605 		 */
606 		error = xfs_attr_node_addname(args);
607 		return error;
608 	}
609 
610 	/*
611 	 * Commit the transaction that added the attr name so that
612 	 * later routines can manage their own transactions.
613 	 */
614 	error = xfs_trans_roll_inode(&args->trans, dp);
615 	if (error)
616 		return error;
617 
618 	/*
619 	 * If there was an out-of-line value, allocate the blocks we
620 	 * identified for its storage and copy the value.  This is done
621 	 * after we create the attribute so that we don't overflow the
622 	 * maximum size of a transaction and/or hit a deadlock.
623 	 */
624 	if (args->rmtblkno > 0) {
625 		error = xfs_attr_rmtval_set(args);
626 		if (error)
627 			return error;
628 	}
629 
630 	/*
631 	 * If this is an atomic rename operation, we must "flip" the
632 	 * incomplete flags on the "new" and "old" attribute/value pairs
633 	 * so that one disappears and one appears atomically.  Then we
634 	 * must remove the "old" attribute/value pair.
635 	 */
636 	if (args->op_flags & XFS_DA_OP_RENAME) {
637 		/*
638 		 * In a separate transaction, set the incomplete flag on the
639 		 * "old" attr and clear the incomplete flag on the "new" attr.
640 		 */
641 		error = xfs_attr3_leaf_flipflags(args);
642 		if (error)
643 			return error;
644 
645 		/*
646 		 * Dismantle the "old" attribute/value pair by removing
647 		 * a "remote" value (if it exists).
648 		 */
649 		args->index = args->index2;
650 		args->blkno = args->blkno2;
651 		args->rmtblkno = args->rmtblkno2;
652 		args->rmtblkcnt = args->rmtblkcnt2;
653 		args->rmtvaluelen = args->rmtvaluelen2;
654 		if (args->rmtblkno) {
655 			error = xfs_attr_rmtval_remove(args);
656 			if (error)
657 				return error;
658 		}
659 
660 		/*
661 		 * Read in the block containing the "old" attr, then
662 		 * remove the "old" attr from that block (neat, huh!)
663 		 */
664 		error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
665 					   -1, &bp);
666 		if (error)
667 			return error;
668 
669 		xfs_attr3_leaf_remove(bp, args);
670 
671 		/*
672 		 * If the result is small enough, shrink it all into the inode.
673 		 */
674 		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
675 			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
676 			/* bp is gone due to xfs_da_shrink_inode */
677 			if (error)
678 				goto out_defer_cancel;
679 			error = xfs_defer_finish(&args->trans);
680 			if (error)
681 				return error;
682 		}
683 
684 		/*
685 		 * Commit the remove and start the next trans in series.
686 		 */
687 		error = xfs_trans_roll_inode(&args->trans, dp);
688 
689 	} else if (args->rmtblkno > 0) {
690 		/*
691 		 * Added a "remote" value, just clear the incomplete flag.
692 		 */
693 		error = xfs_attr3_leaf_clearflag(args);
694 	}
695 	return error;
696 out_defer_cancel:
697 	xfs_defer_cancel(args->trans);
698 	return error;
699 }
700 
701 /*
702  * Remove a name from the leaf attribute list structure
703  *
704  * This leaf block cannot have a "remote" value, we only call this routine
705  * if bmap_one_block() says there is only one block (ie: no remote blks).
706  */
707 STATIC int
708 xfs_attr_leaf_removename(
709 	struct xfs_da_args	*args)
710 {
711 	struct xfs_inode	*dp;
712 	struct xfs_buf		*bp;
713 	int			error, forkoff;
714 
715 	trace_xfs_attr_leaf_removename(args);
716 
717 	/*
718 	 * Remove the attribute.
719 	 */
720 	dp = args->dp;
721 	args->blkno = 0;
722 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
723 	if (error)
724 		return error;
725 
726 	error = xfs_attr3_leaf_lookup_int(bp, args);
727 	if (error == -ENOATTR) {
728 		xfs_trans_brelse(args->trans, bp);
729 		return error;
730 	}
731 
732 	xfs_attr3_leaf_remove(bp, args);
733 
734 	/*
735 	 * If the result is small enough, shrink it all into the inode.
736 	 */
737 	if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
738 		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
739 		/* bp is gone due to xfs_da_shrink_inode */
740 		if (error)
741 			goto out_defer_cancel;
742 		error = xfs_defer_finish(&args->trans);
743 		if (error)
744 			return error;
745 	}
746 	return 0;
747 out_defer_cancel:
748 	xfs_defer_cancel(args->trans);
749 	return error;
750 }
751 
752 /*
753  * Look up a name in a leaf attribute list structure.
754  *
755  * This leaf block cannot have a "remote" value, we only call this routine
756  * if bmap_one_block() says there is only one block (ie: no remote blks).
757  */
758 STATIC int
759 xfs_attr_leaf_get(xfs_da_args_t *args)
760 {
761 	struct xfs_buf *bp;
762 	int error;
763 
764 	trace_xfs_attr_leaf_get(args);
765 
766 	args->blkno = 0;
767 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
768 	if (error)
769 		return error;
770 
771 	error = xfs_attr3_leaf_lookup_int(bp, args);
772 	if (error != -EEXIST)  {
773 		xfs_trans_brelse(args->trans, bp);
774 		return error;
775 	}
776 	error = xfs_attr3_leaf_getvalue(bp, args);
777 	xfs_trans_brelse(args->trans, bp);
778 	if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
779 		error = xfs_attr_rmtval_get(args);
780 	}
781 	return error;
782 }
783 
784 /*========================================================================
785  * External routines when attribute list size > geo->blksize
786  *========================================================================*/
787 
788 /*
789  * Add a name to a Btree-format attribute list.
790  *
791  * This will involve walking down the Btree, and may involve splitting
792  * leaf nodes and even splitting intermediate nodes up to and including
793  * the root node (a special case of an intermediate node).
794  *
795  * "Remote" attribute values confuse the issue and atomic rename operations
796  * add a whole extra layer of confusion on top of that.
797  */
798 STATIC int
799 xfs_attr_node_addname(
800 	struct xfs_da_args	*args)
801 {
802 	struct xfs_da_state	*state;
803 	struct xfs_da_state_blk	*blk;
804 	struct xfs_inode	*dp;
805 	struct xfs_mount	*mp;
806 	int			retval, error;
807 
808 	trace_xfs_attr_node_addname(args);
809 
810 	/*
811 	 * Fill in bucket of arguments/results/context to carry around.
812 	 */
813 	dp = args->dp;
814 	mp = dp->i_mount;
815 restart:
816 	state = xfs_da_state_alloc();
817 	state->args = args;
818 	state->mp = mp;
819 
820 	/*
821 	 * Search to see if name already exists, and get back a pointer
822 	 * to where it should go.
823 	 */
824 	error = xfs_da3_node_lookup_int(state, &retval);
825 	if (error)
826 		goto out;
827 	blk = &state->path.blk[ state->path.active-1 ];
828 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
829 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
830 		goto out;
831 	} else if (retval == -EEXIST) {
832 		if (args->flags & ATTR_CREATE)
833 			goto out;
834 
835 		trace_xfs_attr_node_replace(args);
836 
837 		/* save the attribute state for later removal*/
838 		args->op_flags |= XFS_DA_OP_RENAME;	/* atomic rename op */
839 		args->blkno2 = args->blkno;		/* set 2nd entry info*/
840 		args->index2 = args->index;
841 		args->rmtblkno2 = args->rmtblkno;
842 		args->rmtblkcnt2 = args->rmtblkcnt;
843 		args->rmtvaluelen2 = args->rmtvaluelen;
844 
845 		/*
846 		 * clear the remote attr state now that it is saved so that the
847 		 * values reflect the state of the attribute we are about to
848 		 * add, not the attribute we just found and will remove later.
849 		 */
850 		args->rmtblkno = 0;
851 		args->rmtblkcnt = 0;
852 		args->rmtvaluelen = 0;
853 	}
854 
855 	retval = xfs_attr3_leaf_add(blk->bp, state->args);
856 	if (retval == -ENOSPC) {
857 		if (state->path.active == 1) {
858 			/*
859 			 * Its really a single leaf node, but it had
860 			 * out-of-line values so it looked like it *might*
861 			 * have been a b-tree.
862 			 */
863 			xfs_da_state_free(state);
864 			state = NULL;
865 			error = xfs_attr3_leaf_to_node(args);
866 			if (error)
867 				goto out_defer_cancel;
868 			error = xfs_defer_finish(&args->trans);
869 			if (error)
870 				goto out;
871 
872 			/*
873 			 * Commit the node conversion and start the next
874 			 * trans in the chain.
875 			 */
876 			error = xfs_trans_roll_inode(&args->trans, dp);
877 			if (error)
878 				goto out;
879 
880 			goto restart;
881 		}
882 
883 		/*
884 		 * Split as many Btree elements as required.
885 		 * This code tracks the new and old attr's location
886 		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
887 		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
888 		 */
889 		error = xfs_da3_split(state);
890 		if (error)
891 			goto out_defer_cancel;
892 		error = xfs_defer_finish(&args->trans);
893 		if (error)
894 			goto out;
895 	} else {
896 		/*
897 		 * Addition succeeded, update Btree hashvals.
898 		 */
899 		xfs_da3_fixhashpath(state, &state->path);
900 	}
901 
902 	/*
903 	 * Kill the state structure, we're done with it and need to
904 	 * allow the buffers to come back later.
905 	 */
906 	xfs_da_state_free(state);
907 	state = NULL;
908 
909 	/*
910 	 * Commit the leaf addition or btree split and start the next
911 	 * trans in the chain.
912 	 */
913 	error = xfs_trans_roll_inode(&args->trans, dp);
914 	if (error)
915 		goto out;
916 
917 	/*
918 	 * If there was an out-of-line value, allocate the blocks we
919 	 * identified for its storage and copy the value.  This is done
920 	 * after we create the attribute so that we don't overflow the
921 	 * maximum size of a transaction and/or hit a deadlock.
922 	 */
923 	if (args->rmtblkno > 0) {
924 		error = xfs_attr_rmtval_set(args);
925 		if (error)
926 			return error;
927 	}
928 
929 	/*
930 	 * If this is an atomic rename operation, we must "flip" the
931 	 * incomplete flags on the "new" and "old" attribute/value pairs
932 	 * so that one disappears and one appears atomically.  Then we
933 	 * must remove the "old" attribute/value pair.
934 	 */
935 	if (args->op_flags & XFS_DA_OP_RENAME) {
936 		/*
937 		 * In a separate transaction, set the incomplete flag on the
938 		 * "old" attr and clear the incomplete flag on the "new" attr.
939 		 */
940 		error = xfs_attr3_leaf_flipflags(args);
941 		if (error)
942 			goto out;
943 
944 		/*
945 		 * Dismantle the "old" attribute/value pair by removing
946 		 * a "remote" value (if it exists).
947 		 */
948 		args->index = args->index2;
949 		args->blkno = args->blkno2;
950 		args->rmtblkno = args->rmtblkno2;
951 		args->rmtblkcnt = args->rmtblkcnt2;
952 		args->rmtvaluelen = args->rmtvaluelen2;
953 		if (args->rmtblkno) {
954 			error = xfs_attr_rmtval_remove(args);
955 			if (error)
956 				return error;
957 		}
958 
959 		/*
960 		 * Re-find the "old" attribute entry after any split ops.
961 		 * The INCOMPLETE flag means that we will find the "old"
962 		 * attr, not the "new" one.
963 		 */
964 		args->flags |= XFS_ATTR_INCOMPLETE;
965 		state = xfs_da_state_alloc();
966 		state->args = args;
967 		state->mp = mp;
968 		state->inleaf = 0;
969 		error = xfs_da3_node_lookup_int(state, &retval);
970 		if (error)
971 			goto out;
972 
973 		/*
974 		 * Remove the name and update the hashvals in the tree.
975 		 */
976 		blk = &state->path.blk[ state->path.active-1 ];
977 		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
978 		error = xfs_attr3_leaf_remove(blk->bp, args);
979 		xfs_da3_fixhashpath(state, &state->path);
980 
981 		/*
982 		 * Check to see if the tree needs to be collapsed.
983 		 */
984 		if (retval && (state->path.active > 1)) {
985 			error = xfs_da3_join(state);
986 			if (error)
987 				goto out_defer_cancel;
988 			error = xfs_defer_finish(&args->trans);
989 			if (error)
990 				goto out;
991 		}
992 
993 		/*
994 		 * Commit and start the next trans in the chain.
995 		 */
996 		error = xfs_trans_roll_inode(&args->trans, dp);
997 		if (error)
998 			goto out;
999 
1000 	} else if (args->rmtblkno > 0) {
1001 		/*
1002 		 * Added a "remote" value, just clear the incomplete flag.
1003 		 */
1004 		error = xfs_attr3_leaf_clearflag(args);
1005 		if (error)
1006 			goto out;
1007 	}
1008 	retval = error = 0;
1009 
1010 out:
1011 	if (state)
1012 		xfs_da_state_free(state);
1013 	if (error)
1014 		return error;
1015 	return retval;
1016 out_defer_cancel:
1017 	xfs_defer_cancel(args->trans);
1018 	goto out;
1019 }
1020 
1021 /*
1022  * Remove a name from a B-tree attribute list.
1023  *
1024  * This will involve walking down the Btree, and may involve joining
1025  * leaf nodes and even joining intermediate nodes up to and including
1026  * the root node (a special case of an intermediate node).
1027  */
1028 STATIC int
1029 xfs_attr_node_removename(
1030 	struct xfs_da_args	*args)
1031 {
1032 	struct xfs_da_state	*state;
1033 	struct xfs_da_state_blk	*blk;
1034 	struct xfs_inode	*dp;
1035 	struct xfs_buf		*bp;
1036 	int			retval, error, forkoff;
1037 
1038 	trace_xfs_attr_node_removename(args);
1039 
1040 	/*
1041 	 * Tie a string around our finger to remind us where we are.
1042 	 */
1043 	dp = args->dp;
1044 	state = xfs_da_state_alloc();
1045 	state->args = args;
1046 	state->mp = dp->i_mount;
1047 
1048 	/*
1049 	 * Search to see if name exists, and get back a pointer to it.
1050 	 */
1051 	error = xfs_da3_node_lookup_int(state, &retval);
1052 	if (error || (retval != -EEXIST)) {
1053 		if (error == 0)
1054 			error = retval;
1055 		goto out;
1056 	}
1057 
1058 	/*
1059 	 * If there is an out-of-line value, de-allocate the blocks.
1060 	 * This is done before we remove the attribute so that we don't
1061 	 * overflow the maximum size of a transaction and/or hit a deadlock.
1062 	 */
1063 	blk = &state->path.blk[ state->path.active-1 ];
1064 	ASSERT(blk->bp != NULL);
1065 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1066 	if (args->rmtblkno > 0) {
1067 		/*
1068 		 * Fill in disk block numbers in the state structure
1069 		 * so that we can get the buffers back after we commit
1070 		 * several transactions in the following calls.
1071 		 */
1072 		error = xfs_attr_fillstate(state);
1073 		if (error)
1074 			goto out;
1075 
1076 		/*
1077 		 * Mark the attribute as INCOMPLETE, then bunmapi() the
1078 		 * remote value.
1079 		 */
1080 		error = xfs_attr3_leaf_setflag(args);
1081 		if (error)
1082 			goto out;
1083 		error = xfs_attr_rmtval_remove(args);
1084 		if (error)
1085 			goto out;
1086 
1087 		/*
1088 		 * Refill the state structure with buffers, the prior calls
1089 		 * released our buffers.
1090 		 */
1091 		error = xfs_attr_refillstate(state);
1092 		if (error)
1093 			goto out;
1094 	}
1095 
1096 	/*
1097 	 * Remove the name and update the hashvals in the tree.
1098 	 */
1099 	blk = &state->path.blk[ state->path.active-1 ];
1100 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1101 	retval = xfs_attr3_leaf_remove(blk->bp, args);
1102 	xfs_da3_fixhashpath(state, &state->path);
1103 
1104 	/*
1105 	 * Check to see if the tree needs to be collapsed.
1106 	 */
1107 	if (retval && (state->path.active > 1)) {
1108 		error = xfs_da3_join(state);
1109 		if (error)
1110 			goto out_defer_cancel;
1111 		error = xfs_defer_finish(&args->trans);
1112 		if (error)
1113 			goto out;
1114 		/*
1115 		 * Commit the Btree join operation and start a new trans.
1116 		 */
1117 		error = xfs_trans_roll_inode(&args->trans, dp);
1118 		if (error)
1119 			goto out;
1120 	}
1121 
1122 	/*
1123 	 * If the result is small enough, push it all into the inode.
1124 	 */
1125 	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1126 		/*
1127 		 * Have to get rid of the copy of this dabuf in the state.
1128 		 */
1129 		ASSERT(state->path.active == 1);
1130 		ASSERT(state->path.blk[0].bp);
1131 		state->path.blk[0].bp = NULL;
1132 
1133 		error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1134 		if (error)
1135 			goto out;
1136 
1137 		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1138 			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1139 			/* bp is gone due to xfs_da_shrink_inode */
1140 			if (error)
1141 				goto out_defer_cancel;
1142 			error = xfs_defer_finish(&args->trans);
1143 			if (error)
1144 				goto out;
1145 		} else
1146 			xfs_trans_brelse(args->trans, bp);
1147 	}
1148 	error = 0;
1149 
1150 out:
1151 	xfs_da_state_free(state);
1152 	return error;
1153 out_defer_cancel:
1154 	xfs_defer_cancel(args->trans);
1155 	goto out;
1156 }
1157 
1158 /*
1159  * Fill in the disk block numbers in the state structure for the buffers
1160  * that are attached to the state structure.
1161  * This is done so that we can quickly reattach ourselves to those buffers
1162  * after some set of transaction commits have released these buffers.
1163  */
1164 STATIC int
1165 xfs_attr_fillstate(xfs_da_state_t *state)
1166 {
1167 	xfs_da_state_path_t *path;
1168 	xfs_da_state_blk_t *blk;
1169 	int level;
1170 
1171 	trace_xfs_attr_fillstate(state->args);
1172 
1173 	/*
1174 	 * Roll down the "path" in the state structure, storing the on-disk
1175 	 * block number for those buffers in the "path".
1176 	 */
1177 	path = &state->path;
1178 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1179 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1180 		if (blk->bp) {
1181 			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1182 			blk->bp = NULL;
1183 		} else {
1184 			blk->disk_blkno = 0;
1185 		}
1186 	}
1187 
1188 	/*
1189 	 * Roll down the "altpath" in the state structure, storing the on-disk
1190 	 * block number for those buffers in the "altpath".
1191 	 */
1192 	path = &state->altpath;
1193 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1194 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1195 		if (blk->bp) {
1196 			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1197 			blk->bp = NULL;
1198 		} else {
1199 			blk->disk_blkno = 0;
1200 		}
1201 	}
1202 
1203 	return 0;
1204 }
1205 
1206 /*
1207  * Reattach the buffers to the state structure based on the disk block
1208  * numbers stored in the state structure.
1209  * This is done after some set of transaction commits have released those
1210  * buffers from our grip.
1211  */
1212 STATIC int
1213 xfs_attr_refillstate(xfs_da_state_t *state)
1214 {
1215 	xfs_da_state_path_t *path;
1216 	xfs_da_state_blk_t *blk;
1217 	int level, error;
1218 
1219 	trace_xfs_attr_refillstate(state->args);
1220 
1221 	/*
1222 	 * Roll down the "path" in the state structure, storing the on-disk
1223 	 * block number for those buffers in the "path".
1224 	 */
1225 	path = &state->path;
1226 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1227 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1228 		if (blk->disk_blkno) {
1229 			error = xfs_da3_node_read(state->args->trans,
1230 						state->args->dp,
1231 						blk->blkno, blk->disk_blkno,
1232 						&blk->bp, XFS_ATTR_FORK);
1233 			if (error)
1234 				return error;
1235 		} else {
1236 			blk->bp = NULL;
1237 		}
1238 	}
1239 
1240 	/*
1241 	 * Roll down the "altpath" in the state structure, storing the on-disk
1242 	 * block number for those buffers in the "altpath".
1243 	 */
1244 	path = &state->altpath;
1245 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1246 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1247 		if (blk->disk_blkno) {
1248 			error = xfs_da3_node_read(state->args->trans,
1249 						state->args->dp,
1250 						blk->blkno, blk->disk_blkno,
1251 						&blk->bp, XFS_ATTR_FORK);
1252 			if (error)
1253 				return error;
1254 		} else {
1255 			blk->bp = NULL;
1256 		}
1257 	}
1258 
1259 	return 0;
1260 }
1261 
1262 /*
1263  * Look up a filename in a node attribute list.
1264  *
1265  * This routine gets called for any attribute fork that has more than one
1266  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1267  * "remote" values taking up more blocks.
1268  */
1269 STATIC int
1270 xfs_attr_node_get(xfs_da_args_t *args)
1271 {
1272 	xfs_da_state_t *state;
1273 	xfs_da_state_blk_t *blk;
1274 	int error, retval;
1275 	int i;
1276 
1277 	trace_xfs_attr_node_get(args);
1278 
1279 	state = xfs_da_state_alloc();
1280 	state->args = args;
1281 	state->mp = args->dp->i_mount;
1282 
1283 	/*
1284 	 * Search to see if name exists, and get back a pointer to it.
1285 	 */
1286 	error = xfs_da3_node_lookup_int(state, &retval);
1287 	if (error) {
1288 		retval = error;
1289 	} else if (retval == -EEXIST) {
1290 		blk = &state->path.blk[ state->path.active-1 ];
1291 		ASSERT(blk->bp != NULL);
1292 		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1293 
1294 		/*
1295 		 * Get the value, local or "remote"
1296 		 */
1297 		retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1298 		if (!retval && (args->rmtblkno > 0)
1299 		    && !(args->flags & ATTR_KERNOVAL)) {
1300 			retval = xfs_attr_rmtval_get(args);
1301 		}
1302 	}
1303 
1304 	/*
1305 	 * If not in a transaction, we have to release all the buffers.
1306 	 */
1307 	for (i = 0; i < state->path.active; i++) {
1308 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1309 		state->path.blk[i].bp = NULL;
1310 	}
1311 
1312 	xfs_da_state_free(state);
1313 	return retval;
1314 }
1315