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