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