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