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