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