1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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"
20 #include "xfs_bmap_btree.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"
31 * Provide the external interfaces to manage attribute lists.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Internal routines when attribute list fits inside the inode.
41 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
44 * Internal routines when attribute list is one block.
46 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
47 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
49 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
52 * Internal routines when attribute list is more than one block.
54 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
55 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
56 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
57 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
58 struct xfs_da_state **state);
59 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
60 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
66 if (!XFS_IFORK_Q(ip) ||
67 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
68 ip->i_afp->if_nextents == 0))
73 /*========================================================================
74 * Overall external interface routines.
75 *========================================================================*/
78 * Retrieve an extended attribute and its value. Must have ilock.
79 * Returns 0 on successful retrieval, otherwise an error.
83 struct xfs_da_args *args)
85 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
87 if (!xfs_inode_hasattr(args->dp))
90 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
91 return xfs_attr_shortform_getvalue(args);
92 if (xfs_bmap_one_block(args->dp, XFS_ATTR_FORK))
93 return xfs_attr_leaf_get(args);
94 return xfs_attr_node_get(args);
98 * Retrieve an extended attribute by name, and its value if requested.
100 * If args->valuelen is zero, then the caller does not want the value, just an
101 * indication whether the attribute exists and the size of the value if it
102 * exists. The size is returned in args.valuelen.
104 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
105 * for the value after existence of the attribute has been determined. The
106 * caller always has to free args->value if it is set, no matter if this
107 * function was successful or not.
109 * If the attribute is found, but exceeds the size limit set by the caller in
110 * args->valuelen, return -ERANGE with the size of the attribute that was found
115 struct xfs_da_args *args)
120 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
122 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
125 args->geo = args->dp->i_mount->m_attr_geo;
126 args->whichfork = XFS_ATTR_FORK;
127 args->hashval = xfs_da_hashname(args->name, args->namelen);
129 /* Entirely possible to look up a name which doesn't exist */
130 args->op_flags = XFS_DA_OP_OKNOENT;
132 lock_mode = xfs_ilock_attr_map_shared(args->dp);
133 error = xfs_attr_get_ilocked(args);
134 xfs_iunlock(args->dp, lock_mode);
140 * Calculate how many blocks we need for the new attribute,
144 struct xfs_da_args *args,
147 struct xfs_mount *mp = args->dp->i_mount;
152 * Determine space new attribute will use, and if it would be
153 * "local" or "remote" (note: local != inline).
155 size = xfs_attr_leaf_newentsize(args, local);
156 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
158 if (size > (args->geo->blksize / 2)) {
159 /* Double split possible */
164 * Out of line attribute, cannot double split, but
165 * make room for the attribute value itself.
167 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
169 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
176 xfs_attr_try_sf_addname(
177 struct xfs_inode *dp,
178 struct xfs_da_args *args)
184 * Build initial attribute list (if required).
186 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
187 xfs_attr_shortform_create(args);
189 error = xfs_attr_shortform_addname(args);
190 if (error == -ENOSPC)
194 * Commit the shortform mods, and we're done.
195 * NOTE: this is also the error path (EEXIST, etc).
197 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
198 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
200 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
201 xfs_trans_set_sync(args->trans);
207 * Check to see if the attr should be upgraded from non-existent or shortform to
208 * single-leaf-block attribute list.
211 xfs_attr_is_shortform(
212 struct xfs_inode *ip)
214 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
215 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
216 ip->i_afp->if_nextents == 0);
220 * Attempts to set an attr in shortform, or converts short form to leaf form if
221 * there is not enough room. If the attr is set, the transaction is committed
225 xfs_attr_set_shortform(
226 struct xfs_da_args *args,
227 struct xfs_buf **leaf_bp)
229 struct xfs_inode *dp = args->dp;
230 int error, error2 = 0;
233 * Try to add the attr to the attribute list in the inode.
235 error = xfs_attr_try_sf_addname(dp, args);
236 if (error != -ENOSPC) {
237 error2 = xfs_trans_commit(args->trans);
239 return error ? error : error2;
242 * It won't fit in the shortform, transform to a leaf block. GROT:
243 * another possible req'mt for a double-split btree op.
245 error = xfs_attr_shortform_to_leaf(args, leaf_bp);
250 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
251 * push cannot grab the half-baked leaf buffer and run into problems
252 * with the write verifier. Once we're done rolling the transaction we
253 * can release the hold and add the attr to the leaf.
255 xfs_trans_bhold(args->trans, *leaf_bp);
256 error = xfs_defer_finish(&args->trans);
257 xfs_trans_bhold_release(args->trans, *leaf_bp);
259 xfs_trans_brelse(args->trans, *leaf_bp);
267 * Set the attribute specified in @args.
271 struct xfs_da_args *args)
273 struct xfs_inode *dp = args->dp;
274 struct xfs_buf *leaf_bp = NULL;
278 * If the attribute list is already in leaf format, jump straight to
279 * leaf handling. Otherwise, try to add the attribute to the shortform
280 * list; if there's no room then convert the list to leaf format and try
283 if (xfs_attr_is_shortform(dp)) {
286 * If the attr was successfully set in shortform, the
287 * transaction is committed and set to NULL. Otherwise, is it
288 * converted from shortform to leaf, and the transaction is
291 error = xfs_attr_set_shortform(args, &leaf_bp);
292 if (error || !args->trans)
296 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
297 error = xfs_attr_leaf_addname(args);
298 if (error != -ENOSPC)
302 * Promote the attribute list to the Btree format.
304 error = xfs_attr3_leaf_to_node(args);
309 * Finish any deferred work items and roll the transaction once
310 * more. The goal here is to call node_addname with the inode
311 * and transaction in the same state (inode locked and joined,
312 * transaction clean) no matter how we got to this step.
314 error = xfs_defer_finish(&args->trans);
319 * Commit the current trans (including the inode) and
322 error = xfs_trans_roll_inode(&args->trans, dp);
327 error = xfs_attr_node_addname(args);
332 * Return EEXIST if attr is found, or ENOATTR if not
336 struct xfs_da_args *args)
338 struct xfs_inode *dp = args->dp;
339 struct xfs_buf *bp = NULL;
342 if (!xfs_inode_hasattr(dp))
345 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
346 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
347 return xfs_attr_sf_findname(args, NULL, NULL);
350 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
351 error = xfs_attr_leaf_hasname(args, &bp);
354 xfs_trans_brelse(args->trans, bp);
359 return xfs_attr_node_hasname(args, NULL);
363 * Remove the attribute specified in @args.
366 xfs_attr_remove_args(
367 struct xfs_da_args *args)
369 struct xfs_inode *dp = args->dp;
372 if (!xfs_inode_hasattr(dp)) {
374 } else if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
375 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
376 error = xfs_attr_shortform_remove(args);
377 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
378 error = xfs_attr_leaf_removename(args);
380 error = xfs_attr_node_removename(args);
387 * Note: If args->value is NULL the attribute will be removed, just like the
388 * Linux ->setattr API.
392 struct xfs_da_args *args)
394 struct xfs_inode *dp = args->dp;
395 struct xfs_mount *mp = dp->i_mount;
396 struct xfs_trans_res tres;
397 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
402 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
405 error = xfs_qm_dqattach(dp);
409 args->geo = mp->m_attr_geo;
410 args->whichfork = XFS_ATTR_FORK;
411 args->hashval = xfs_da_hashname(args->name, args->namelen);
414 * We have no control over the attribute names that userspace passes us
415 * to remove, so we have to allow the name lookup prior to attribute
416 * removal to fail as well.
418 args->op_flags = XFS_DA_OP_OKNOENT;
421 XFS_STATS_INC(mp, xs_attr_set);
423 args->op_flags |= XFS_DA_OP_ADDNAME;
424 args->total = xfs_attr_calc_size(args, &local);
427 * If the inode doesn't have an attribute fork, add one.
428 * (inode must not be locked when we call this routine)
430 if (XFS_IFORK_Q(dp) == 0) {
431 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
432 xfs_attr_sf_entsize_byname(args->namelen,
435 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
440 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
441 M_RES(mp)->tr_attrsetrt.tr_logres *
443 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
444 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
448 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
450 XFS_STATS_INC(mp, xs_attr_remove);
452 tres = M_RES(mp)->tr_attrrm;
453 total = XFS_ATTRRM_SPACE_RES(mp);
454 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
458 * Root fork attributes can use reserved data blocks for this
459 * operation if necessary
461 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
465 if (args->value || xfs_inode_hasattr(dp)) {
466 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
467 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
469 goto out_trans_cancel;
473 error = xfs_has_attr(args);
474 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
475 goto out_trans_cancel;
476 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
477 goto out_trans_cancel;
478 if (error != -ENOATTR && error != -EEXIST)
479 goto out_trans_cancel;
481 error = xfs_attr_set_args(args);
483 goto out_trans_cancel;
484 /* shortform attribute has already been committed */
488 error = xfs_has_attr(args);
489 if (error != -EEXIST)
490 goto out_trans_cancel;
492 error = xfs_attr_remove_args(args);
494 goto out_trans_cancel;
498 * If this is a synchronous mount, make sure that the
499 * transaction goes to disk before returning to the user.
501 if (mp->m_flags & XFS_MOUNT_WSYNC)
502 xfs_trans_set_sync(args->trans);
504 if (!(args->op_flags & XFS_DA_OP_NOTIME))
505 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
508 * Commit the last in the sequence of transactions.
510 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
511 error = xfs_trans_commit(args->trans);
513 xfs_iunlock(dp, XFS_ILOCK_EXCL);
518 xfs_trans_cancel(args->trans);
522 /*========================================================================
523 * External routines when attribute list is inside the inode
524 *========================================================================*/
526 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
528 struct xfs_attr_shortform *sf;
530 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
531 return be16_to_cpu(sf->hdr.totsize);
535 * Add a name to the shortform attribute list structure
536 * This is the external routine.
539 xfs_attr_shortform_addname(xfs_da_args_t *args)
541 int newsize, forkoff, retval;
543 trace_xfs_attr_sf_addname(args);
545 retval = xfs_attr_shortform_lookup(args);
546 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
548 if (retval == -EEXIST) {
549 if (args->attr_flags & XATTR_CREATE)
551 retval = xfs_attr_shortform_remove(args);
555 * Since we have removed the old attr, clear ATTR_REPLACE so
556 * that the leaf format add routine won't trip over the attr
559 args->attr_flags &= ~XATTR_REPLACE;
562 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
563 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
566 newsize = xfs_attr_sf_totsize(args->dp);
567 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
569 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
573 xfs_attr_shortform_add(args, forkoff);
578 /*========================================================================
579 * External routines when attribute list is one block
580 *========================================================================*/
582 /* Store info about a remote block */
584 xfs_attr_save_rmt_blk(
585 struct xfs_da_args *args)
587 args->blkno2 = args->blkno;
588 args->index2 = args->index;
589 args->rmtblkno2 = args->rmtblkno;
590 args->rmtblkcnt2 = args->rmtblkcnt;
591 args->rmtvaluelen2 = args->rmtvaluelen;
594 /* Set stored info about a remote block */
596 xfs_attr_restore_rmt_blk(
597 struct xfs_da_args *args)
599 args->blkno = args->blkno2;
600 args->index = args->index2;
601 args->rmtblkno = args->rmtblkno2;
602 args->rmtblkcnt = args->rmtblkcnt2;
603 args->rmtvaluelen = args->rmtvaluelen2;
607 * Tries to add an attribute to an inode in leaf form
609 * This function is meant to execute as part of a delayed operation and leaves
610 * the transaction handling to the caller. On success the attribute is added
611 * and the inode and transaction are left dirty. If there is not enough space,
612 * the attr data is converted to node format and -ENOSPC is returned. Caller is
613 * responsible for handling the dirty inode and transaction or adding the attr
617 xfs_attr_leaf_try_add(
618 struct xfs_da_args *args,
624 * Look up the given attribute in the leaf block. Figure out if
625 * the given flags produce an error or call for an atomic rename.
627 retval = xfs_attr_leaf_hasname(args, &bp);
628 if (retval != -ENOATTR && retval != -EEXIST)
630 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
632 if (retval == -EEXIST) {
633 if (args->attr_flags & XATTR_CREATE)
636 trace_xfs_attr_leaf_replace(args);
638 /* save the attribute state for later removal*/
639 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
640 xfs_attr_save_rmt_blk(args);
643 * clear the remote attr state now that it is saved so that the
644 * values reflect the state of the attribute we are about to
645 * add, not the attribute we just found and will remove later.
649 args->rmtvaluelen = 0;
653 * Add the attribute to the leaf block
655 return xfs_attr3_leaf_add(bp, args);
658 xfs_trans_brelse(args->trans, bp);
664 * Add a name to the leaf attribute list structure
666 * This leaf block cannot have a "remote" value, we only call this routine
667 * if bmap_one_block() says there is only one block (ie: no remote blks).
670 xfs_attr_leaf_addname(
671 struct xfs_da_args *args)
674 struct xfs_buf *bp = NULL;
675 struct xfs_inode *dp = args->dp;
677 trace_xfs_attr_leaf_addname(args);
679 error = xfs_attr_leaf_try_add(args, bp);
684 * Commit the transaction that added the attr name so that
685 * later routines can manage their own transactions.
687 error = xfs_trans_roll_inode(&args->trans, dp);
692 * If there was an out-of-line value, allocate the blocks we
693 * identified for its storage and copy the value. This is done
694 * after we create the attribute so that we don't overflow the
695 * maximum size of a transaction and/or hit a deadlock.
697 if (args->rmtblkno > 0) {
698 error = xfs_attr_rmtval_set(args);
703 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
705 * Added a "remote" value, just clear the incomplete flag.
707 if (args->rmtblkno > 0)
708 error = xfs_attr3_leaf_clearflag(args);
714 * If this is an atomic rename operation, we must "flip" the incomplete
715 * flags on the "new" and "old" attribute/value pairs so that one
716 * disappears and one appears atomically. Then we must remove the "old"
717 * attribute/value pair.
719 * In a separate transaction, set the incomplete flag on the "old" attr
720 * and clear the incomplete flag on the "new" attr.
723 error = xfs_attr3_leaf_flipflags(args);
727 * Commit the flag value change and start the next trans in series.
729 error = xfs_trans_roll_inode(&args->trans, args->dp);
734 * Dismantle the "old" attribute/value pair by removing a "remote" value
737 xfs_attr_restore_rmt_blk(args);
739 if (args->rmtblkno) {
740 error = xfs_attr_rmtval_invalidate(args);
744 error = xfs_attr_rmtval_remove(args);
750 * Read in the block containing the "old" attr, then remove the "old"
751 * attr from that block (neat, huh!)
753 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
758 xfs_attr3_leaf_remove(bp, args);
761 * If the result is small enough, shrink it all into the inode.
763 forkoff = xfs_attr_shortform_allfit(bp, dp);
765 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
766 /* bp is gone due to xfs_da_shrink_inode */
772 * Return EEXIST if attr is found, or ENOATTR if not
775 xfs_attr_leaf_hasname(
776 struct xfs_da_args *args,
781 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
785 error = xfs_attr3_leaf_lookup_int(*bp, args);
786 if (error != -ENOATTR && error != -EEXIST)
787 xfs_trans_brelse(args->trans, *bp);
793 * Remove a name from the leaf attribute list structure
795 * This leaf block cannot have a "remote" value, we only call this routine
796 * if bmap_one_block() says there is only one block (ie: no remote blks).
799 xfs_attr_leaf_removename(
800 struct xfs_da_args *args)
802 struct xfs_inode *dp;
806 trace_xfs_attr_leaf_removename(args);
809 * Remove the attribute.
813 error = xfs_attr_leaf_hasname(args, &bp);
815 if (error == -ENOATTR) {
816 xfs_trans_brelse(args->trans, bp);
818 } else if (error != -EEXIST)
821 xfs_attr3_leaf_remove(bp, args);
824 * If the result is small enough, shrink it all into the inode.
826 forkoff = xfs_attr_shortform_allfit(bp, dp);
828 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
829 /* bp is gone due to xfs_da_shrink_inode */
835 * Look up a name in a leaf attribute list structure.
837 * This leaf block cannot have a "remote" value, we only call this routine
838 * if bmap_one_block() says there is only one block (ie: no remote blks).
840 * Returns 0 on successful retrieval, otherwise an error.
843 xfs_attr_leaf_get(xfs_da_args_t *args)
848 trace_xfs_attr_leaf_get(args);
850 error = xfs_attr_leaf_hasname(args, &bp);
852 if (error == -ENOATTR) {
853 xfs_trans_brelse(args->trans, bp);
855 } else if (error != -EEXIST)
859 error = xfs_attr3_leaf_getvalue(bp, args);
860 xfs_trans_brelse(args->trans, bp);
865 * Return EEXIST if attr is found, or ENOATTR if not
866 * statep: If not null is set to point at the found state. Caller will
867 * be responsible for freeing the state in this case.
870 xfs_attr_node_hasname(
871 struct xfs_da_args *args,
872 struct xfs_da_state **statep)
874 struct xfs_da_state *state;
877 state = xfs_da_state_alloc(args);
882 * Search to see if name exists, and get back a pointer to it.
884 error = xfs_da3_node_lookup_int(state, &retval);
886 xfs_da_state_free(state);
893 xfs_da_state_free(state);
897 /*========================================================================
898 * External routines when attribute list size > geo->blksize
899 *========================================================================*/
902 * Add a name to a Btree-format attribute list.
904 * This will involve walking down the Btree, and may involve splitting
905 * leaf nodes and even splitting intermediate nodes up to and including
906 * the root node (a special case of an intermediate node).
908 * "Remote" attribute values confuse the issue and atomic rename operations
909 * add a whole extra layer of confusion on top of that.
912 xfs_attr_node_addname(
913 struct xfs_da_args *args)
915 struct xfs_da_state *state;
916 struct xfs_da_state_blk *blk;
917 struct xfs_inode *dp;
920 trace_xfs_attr_node_addname(args);
923 * Fill in bucket of arguments/results/context to carry around.
928 * Search to see if name already exists, and get back a pointer
929 * to where it should go.
932 retval = xfs_attr_node_hasname(args, &state);
933 if (retval != -ENOATTR && retval != -EEXIST)
936 blk = &state->path.blk[ state->path.active-1 ];
937 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
938 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
940 if (retval == -EEXIST) {
941 if (args->attr_flags & XATTR_CREATE)
944 trace_xfs_attr_node_replace(args);
946 /* save the attribute state for later removal*/
947 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
948 xfs_attr_save_rmt_blk(args);
951 * clear the remote attr state now that it is saved so that the
952 * values reflect the state of the attribute we are about to
953 * add, not the attribute we just found and will remove later.
957 args->rmtvaluelen = 0;
960 retval = xfs_attr3_leaf_add(blk->bp, state->args);
961 if (retval == -ENOSPC) {
962 if (state->path.active == 1) {
964 * Its really a single leaf node, but it had
965 * out-of-line values so it looked like it *might*
966 * have been a b-tree.
968 xfs_da_state_free(state);
970 error = xfs_attr3_leaf_to_node(args);
973 error = xfs_defer_finish(&args->trans);
978 * Commit the node conversion and start the next
979 * trans in the chain.
981 error = xfs_trans_roll_inode(&args->trans, dp);
989 * Split as many Btree elements as required.
990 * This code tracks the new and old attr's location
991 * in the index/blkno/rmtblkno/rmtblkcnt fields and
992 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
994 error = xfs_da3_split(state);
997 error = xfs_defer_finish(&args->trans);
1002 * Addition succeeded, update Btree hashvals.
1004 xfs_da3_fixhashpath(state, &state->path);
1008 * Kill the state structure, we're done with it and need to
1009 * allow the buffers to come back later.
1011 xfs_da_state_free(state);
1015 * Commit the leaf addition or btree split and start the next
1016 * trans in the chain.
1018 error = xfs_trans_roll_inode(&args->trans, dp);
1023 * If there was an out-of-line value, allocate the blocks we
1024 * identified for its storage and copy the value. This is done
1025 * after we create the attribute so that we don't overflow the
1026 * maximum size of a transaction and/or hit a deadlock.
1028 if (args->rmtblkno > 0) {
1029 error = xfs_attr_rmtval_set(args);
1034 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
1036 * Added a "remote" value, just clear the incomplete flag.
1038 if (args->rmtblkno > 0)
1039 error = xfs_attr3_leaf_clearflag(args);
1045 * If this is an atomic rename operation, we must "flip" the incomplete
1046 * flags on the "new" and "old" attribute/value pairs so that one
1047 * disappears and one appears atomically. Then we must remove the "old"
1048 * attribute/value pair.
1050 * In a separate transaction, set the incomplete flag on the "old" attr
1051 * and clear the incomplete flag on the "new" attr.
1053 error = xfs_attr3_leaf_flipflags(args);
1057 * Commit the flag value change and start the next trans in series
1059 error = xfs_trans_roll_inode(&args->trans, args->dp);
1064 * Dismantle the "old" attribute/value pair by removing a "remote" value
1067 xfs_attr_restore_rmt_blk(args);
1069 if (args->rmtblkno) {
1070 error = xfs_attr_rmtval_invalidate(args);
1074 error = xfs_attr_rmtval_remove(args);
1080 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1081 * flag means that we will find the "old" attr, not the "new" one.
1083 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1084 state = xfs_da_state_alloc(args);
1086 error = xfs_da3_node_lookup_int(state, &retval);
1091 * Remove the name and update the hashvals in the tree.
1093 blk = &state->path.blk[state->path.active-1];
1094 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1095 error = xfs_attr3_leaf_remove(blk->bp, args);
1096 xfs_da3_fixhashpath(state, &state->path);
1099 * Check to see if the tree needs to be collapsed.
1101 if (retval && (state->path.active > 1)) {
1102 error = xfs_da3_join(state);
1110 xfs_da_state_free(state);
1117 * Shrink an attribute from leaf to shortform
1120 xfs_attr_node_shrink(
1121 struct xfs_da_args *args,
1122 struct xfs_da_state *state)
1124 struct xfs_inode *dp = args->dp;
1129 * Have to get rid of the copy of this dabuf in the state.
1131 ASSERT(state->path.active == 1);
1132 ASSERT(state->path.blk[0].bp);
1133 state->path.blk[0].bp = NULL;
1135 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1139 forkoff = xfs_attr_shortform_allfit(bp, dp);
1141 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1142 /* bp is gone due to xfs_da_shrink_inode */
1144 xfs_trans_brelse(args->trans, bp);
1150 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1151 * for later deletion of the entry.
1154 xfs_attr_leaf_mark_incomplete(
1155 struct xfs_da_args *args,
1156 struct xfs_da_state *state)
1161 * Fill in disk block numbers in the state structure
1162 * so that we can get the buffers back after we commit
1163 * several transactions in the following calls.
1165 error = xfs_attr_fillstate(state);
1170 * Mark the attribute as INCOMPLETE
1172 return xfs_attr3_leaf_setflag(args);
1176 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1177 * the blocks are valid. Attr keys with remote blocks will be marked
1181 int xfs_attr_node_removename_setup(
1182 struct xfs_da_args *args,
1183 struct xfs_da_state **state)
1187 error = xfs_attr_node_hasname(args, state);
1188 if (error != -EEXIST)
1191 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1192 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1193 XFS_ATTR_LEAF_MAGIC);
1195 if (args->rmtblkno > 0) {
1196 error = xfs_attr_leaf_mark_incomplete(args, *state);
1200 return xfs_attr_rmtval_invalidate(args);
1207 xfs_attr_node_remove_rmt(
1208 struct xfs_da_args *args,
1209 struct xfs_da_state *state)
1213 error = xfs_attr_rmtval_remove(args);
1218 * Refill the state structure with buffers, the prior calls released our
1221 return xfs_attr_refillstate(state);
1225 * Remove a name from a B-tree attribute list.
1227 * This will involve walking down the Btree, and may involve joining
1228 * leaf nodes and even joining intermediate nodes up to and including
1229 * the root node (a special case of an intermediate node).
1232 xfs_attr_node_removename(
1233 struct xfs_da_args *args)
1235 struct xfs_da_state *state;
1236 struct xfs_da_state_blk *blk;
1238 struct xfs_inode *dp = args->dp;
1240 trace_xfs_attr_node_removename(args);
1242 error = xfs_attr_node_removename_setup(args, &state);
1247 * If there is an out-of-line value, de-allocate the blocks.
1248 * This is done before we remove the attribute so that we don't
1249 * overflow the maximum size of a transaction and/or hit a deadlock.
1251 if (args->rmtblkno > 0) {
1252 error = xfs_attr_node_remove_rmt(args, state);
1258 * Remove the name and update the hashvals in the tree.
1260 blk = &state->path.blk[ state->path.active-1 ];
1261 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1262 retval = xfs_attr3_leaf_remove(blk->bp, args);
1263 xfs_da3_fixhashpath(state, &state->path);
1266 * Check to see if the tree needs to be collapsed.
1268 if (retval && (state->path.active > 1)) {
1269 error = xfs_da3_join(state);
1272 error = xfs_defer_finish(&args->trans);
1276 * Commit the Btree join operation and start a new trans.
1278 error = xfs_trans_roll_inode(&args->trans, dp);
1284 * If the result is small enough, push it all into the inode.
1286 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
1287 error = xfs_attr_node_shrink(args, state);
1291 xfs_da_state_free(state);
1296 * Fill in the disk block numbers in the state structure for the buffers
1297 * that are attached to the state structure.
1298 * This is done so that we can quickly reattach ourselves to those buffers
1299 * after some set of transaction commits have released these buffers.
1302 xfs_attr_fillstate(xfs_da_state_t *state)
1304 xfs_da_state_path_t *path;
1305 xfs_da_state_blk_t *blk;
1308 trace_xfs_attr_fillstate(state->args);
1311 * Roll down the "path" in the state structure, storing the on-disk
1312 * block number for those buffers in the "path".
1314 path = &state->path;
1315 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1316 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1318 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1321 blk->disk_blkno = 0;
1326 * Roll down the "altpath" in the state structure, storing the on-disk
1327 * block number for those buffers in the "altpath".
1329 path = &state->altpath;
1330 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1331 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1333 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1336 blk->disk_blkno = 0;
1344 * Reattach the buffers to the state structure based on the disk block
1345 * numbers stored in the state structure.
1346 * This is done after some set of transaction commits have released those
1347 * buffers from our grip.
1350 xfs_attr_refillstate(xfs_da_state_t *state)
1352 xfs_da_state_path_t *path;
1353 xfs_da_state_blk_t *blk;
1356 trace_xfs_attr_refillstate(state->args);
1359 * Roll down the "path" in the state structure, storing the on-disk
1360 * block number for those buffers in the "path".
1362 path = &state->path;
1363 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1364 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1365 if (blk->disk_blkno) {
1366 error = xfs_da3_node_read_mapped(state->args->trans,
1367 state->args->dp, blk->disk_blkno,
1368 &blk->bp, XFS_ATTR_FORK);
1377 * Roll down the "altpath" in the state structure, storing the on-disk
1378 * block number for those buffers in the "altpath".
1380 path = &state->altpath;
1381 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1382 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1383 if (blk->disk_blkno) {
1384 error = xfs_da3_node_read_mapped(state->args->trans,
1385 state->args->dp, blk->disk_blkno,
1386 &blk->bp, XFS_ATTR_FORK);
1398 * Retrieve the attribute data from a node attribute list.
1400 * This routine gets called for any attribute fork that has more than one
1401 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1402 * "remote" values taking up more blocks.
1404 * Returns 0 on successful retrieval, otherwise an error.
1408 struct xfs_da_args *args)
1410 struct xfs_da_state *state;
1411 struct xfs_da_state_blk *blk;
1415 trace_xfs_attr_node_get(args);
1418 * Search to see if name exists, and get back a pointer to it.
1420 error = xfs_attr_node_hasname(args, &state);
1421 if (error != -EEXIST)
1425 * Get the value, local or "remote"
1427 blk = &state->path.blk[state->path.active - 1];
1428 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1431 * If not in a transaction, we have to release all the buffers.
1434 for (i = 0; state != NULL && i < state->path.active; i++) {
1435 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1436 state->path.blk[i].bp = NULL;
1440 xfs_da_state_free(state);
1444 /* Returns true if the attribute entry name is valid. */
1451 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1452 * out, so use >= for the length check.
1454 if (length >= MAXNAMELEN)
1457 /* There shouldn't be any nulls here */
1458 return !memchr(name, 0, length);