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