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