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