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