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