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