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 * Finish any deferred work items and roll the transaction once
303 * more. The goal here is to call node_addname with the inode
304 * and transaction in the same state (inode locked and joined,
305 * transaction clean) no matter how we got to this step.
307 error = xfs_defer_finish(&args->trans);
312 * Commit the current trans (including the inode) and
315 error = xfs_trans_roll_inode(&args->trans, dp);
320 error = xfs_attr_node_addname(args);
325 * Return EEXIST if attr is found, or ENOATTR if not
329 struct xfs_da_args *args)
331 struct xfs_inode *dp = args->dp;
332 struct xfs_buf *bp = NULL;
335 if (!xfs_inode_hasattr(dp))
338 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
339 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
340 return xfs_attr_sf_findname(args, NULL, NULL);
343 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
344 error = xfs_attr_leaf_hasname(args, &bp);
347 xfs_trans_brelse(args->trans, bp);
352 return xfs_attr_node_hasname(args, NULL);
356 * Remove the attribute specified in @args.
359 xfs_attr_remove_args(
360 struct xfs_da_args *args)
362 struct xfs_inode *dp = args->dp;
365 if (!xfs_inode_hasattr(dp)) {
367 } else if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
368 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
369 error = xfs_attr_shortform_remove(args);
370 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
371 error = xfs_attr_leaf_removename(args);
373 error = xfs_attr_node_removename(args);
380 * Note: If args->value is NULL the attribute will be removed, just like the
381 * Linux ->setattr API.
385 struct xfs_da_args *args)
387 struct xfs_inode *dp = args->dp;
388 struct xfs_mount *mp = dp->i_mount;
389 struct xfs_trans_res tres;
390 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
394 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
397 error = xfs_qm_dqattach(dp);
401 args->geo = mp->m_attr_geo;
402 args->whichfork = XFS_ATTR_FORK;
403 args->hashval = xfs_da_hashname(args->name, args->namelen);
406 * We have no control over the attribute names that userspace passes us
407 * to remove, so we have to allow the name lookup prior to attribute
408 * removal to fail as well.
410 args->op_flags = XFS_DA_OP_OKNOENT;
413 XFS_STATS_INC(mp, xs_attr_set);
415 args->op_flags |= XFS_DA_OP_ADDNAME;
416 args->total = xfs_attr_calc_size(args, &local);
419 * If the inode doesn't have an attribute fork, add one.
420 * (inode must not be locked when we call this routine)
422 if (XFS_IFORK_Q(dp) == 0) {
423 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
424 XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen,
427 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
432 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
433 M_RES(mp)->tr_attrsetrt.tr_logres *
435 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
436 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
439 XFS_STATS_INC(mp, xs_attr_remove);
441 tres = M_RES(mp)->tr_attrrm;
442 total = XFS_ATTRRM_SPACE_RES(mp);
446 * Root fork attributes can use reserved data blocks for this
447 * operation if necessary
449 error = xfs_trans_alloc(mp, &tres, total, 0,
450 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
454 xfs_ilock(dp, XFS_ILOCK_EXCL);
455 xfs_trans_ijoin(args->trans, dp, 0);
457 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
460 quota_flags |= XFS_QMOPT_FORCE_RES;
461 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
462 args->total, 0, quota_flags);
464 goto out_trans_cancel;
466 error = xfs_has_attr(args);
467 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
468 goto out_trans_cancel;
469 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
470 goto out_trans_cancel;
471 if (error != -ENOATTR && error != -EEXIST)
472 goto out_trans_cancel;
474 error = xfs_attr_set_args(args);
476 goto out_trans_cancel;
477 /* shortform attribute has already been committed */
481 error = xfs_has_attr(args);
482 if (error != -EEXIST)
483 goto out_trans_cancel;
485 error = xfs_attr_remove_args(args);
487 goto out_trans_cancel;
491 * If this is a synchronous mount, make sure that the
492 * transaction goes to disk before returning to the user.
494 if (mp->m_flags & XFS_MOUNT_WSYNC)
495 xfs_trans_set_sync(args->trans);
497 if (!(args->op_flags & XFS_DA_OP_NOTIME))
498 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
501 * Commit the last in the sequence of transactions.
503 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
504 error = xfs_trans_commit(args->trans);
506 xfs_iunlock(dp, XFS_ILOCK_EXCL);
511 xfs_trans_cancel(args->trans);
515 /*========================================================================
516 * External routines when attribute list is inside the inode
517 *========================================================================*/
520 * Add a name to the shortform attribute list structure
521 * This is the external routine.
524 xfs_attr_shortform_addname(xfs_da_args_t *args)
526 int newsize, forkoff, retval;
528 trace_xfs_attr_sf_addname(args);
530 retval = xfs_attr_shortform_lookup(args);
531 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
533 if (retval == -EEXIST) {
534 if (args->attr_flags & XATTR_CREATE)
536 retval = xfs_attr_shortform_remove(args);
540 * Since we have removed the old attr, clear ATTR_REPLACE so
541 * that the leaf format add routine won't trip over the attr
544 args->attr_flags &= ~XATTR_REPLACE;
547 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
548 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
551 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
552 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
554 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
558 xfs_attr_shortform_add(args, forkoff);
563 /*========================================================================
564 * External routines when attribute list is one block
565 *========================================================================*/
567 /* Store info about a remote block */
569 xfs_attr_save_rmt_blk(
570 struct xfs_da_args *args)
572 args->blkno2 = args->blkno;
573 args->index2 = args->index;
574 args->rmtblkno2 = args->rmtblkno;
575 args->rmtblkcnt2 = args->rmtblkcnt;
576 args->rmtvaluelen2 = args->rmtvaluelen;
579 /* Set stored info about a remote block */
581 xfs_attr_restore_rmt_blk(
582 struct xfs_da_args *args)
584 args->blkno = args->blkno2;
585 args->index = args->index2;
586 args->rmtblkno = args->rmtblkno2;
587 args->rmtblkcnt = args->rmtblkcnt2;
588 args->rmtvaluelen = args->rmtvaluelen2;
592 * Tries to add an attribute to an inode in leaf form
594 * This function is meant to execute as part of a delayed operation and leaves
595 * the transaction handling to the caller. On success the attribute is added
596 * and the inode and transaction are left dirty. If there is not enough space,
597 * the attr data is converted to node format and -ENOSPC is returned. Caller is
598 * responsible for handling the dirty inode and transaction or adding the attr
602 xfs_attr_leaf_try_add(
603 struct xfs_da_args *args,
609 * Look up the given attribute in the leaf block. Figure out if
610 * the given flags produce an error or call for an atomic rename.
612 retval = xfs_attr_leaf_hasname(args, &bp);
613 if (retval != -ENOATTR && retval != -EEXIST)
615 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
617 if (retval == -EEXIST) {
618 if (args->attr_flags & XATTR_CREATE)
621 trace_xfs_attr_leaf_replace(args);
623 /* save the attribute state for later removal*/
624 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
625 xfs_attr_save_rmt_blk(args);
628 * clear the remote attr state now that it is saved so that the
629 * values reflect the state of the attribute we are about to
630 * add, not the attribute we just found and will remove later.
634 args->rmtvaluelen = 0;
638 * Add the attribute to the leaf block, transitioning to a Btree
641 retval = xfs_attr3_leaf_add(bp, args);
642 if (retval == -ENOSPC) {
644 * Promote the attribute list to the Btree format. Unless an
645 * error occurs, retain the -ENOSPC retval
647 error = xfs_attr3_leaf_to_node(args);
653 xfs_trans_brelse(args->trans, bp);
659 * Add a name to the leaf attribute list structure
661 * This leaf block cannot have a "remote" value, we only call this routine
662 * if bmap_one_block() says there is only one block (ie: no remote blks).
665 xfs_attr_leaf_addname(
666 struct xfs_da_args *args)
669 struct xfs_buf *bp = NULL;
670 struct xfs_inode *dp = args->dp;
672 trace_xfs_attr_leaf_addname(args);
674 error = xfs_attr_leaf_try_add(args, bp);
679 * Commit the transaction that added the attr name so that
680 * later routines can manage their own transactions.
682 error = xfs_trans_roll_inode(&args->trans, dp);
687 * If there was an out-of-line value, allocate the blocks we
688 * identified for its storage and copy the value. This is done
689 * after we create the attribute so that we don't overflow the
690 * maximum size of a transaction and/or hit a deadlock.
692 if (args->rmtblkno > 0) {
693 error = xfs_attr_rmtval_set(args);
699 * If this is an atomic rename operation, we must "flip" the
700 * incomplete flags on the "new" and "old" attribute/value pairs
701 * so that one disappears and one appears atomically. Then we
702 * must remove the "old" attribute/value pair.
704 if (args->op_flags & XFS_DA_OP_RENAME) {
706 * In a separate transaction, set the incomplete flag on the
707 * "old" attr and clear the incomplete flag on the "new" attr.
709 error = xfs_attr3_leaf_flipflags(args);
713 * Commit the flag value change and start the next trans in
716 error = xfs_trans_roll_inode(&args->trans, args->dp);
721 * Dismantle the "old" attribute/value pair by removing
722 * a "remote" value (if it exists).
724 xfs_attr_restore_rmt_blk(args);
726 if (args->rmtblkno) {
727 error = xfs_attr_rmtval_invalidate(args);
731 error = xfs_attr_rmtval_remove(args);
737 * Read in the block containing the "old" attr, then
738 * remove the "old" attr from that block (neat, huh!)
740 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
745 xfs_attr3_leaf_remove(bp, args);
748 * If the result is small enough, shrink it all into the inode.
750 forkoff = xfs_attr_shortform_allfit(bp, dp);
752 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
753 /* bp is gone due to xfs_da_shrink_inode */
754 } else if (args->rmtblkno > 0) {
756 * Added a "remote" value, just clear the incomplete flag.
758 error = xfs_attr3_leaf_clearflag(args);
764 * Return EEXIST if attr is found, or ENOATTR if not
767 xfs_attr_leaf_hasname(
768 struct xfs_da_args *args,
773 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
777 error = xfs_attr3_leaf_lookup_int(*bp, args);
778 if (error != -ENOATTR && error != -EEXIST)
779 xfs_trans_brelse(args->trans, *bp);
785 * Remove a name from the leaf attribute list structure
787 * This leaf block cannot have a "remote" value, we only call this routine
788 * if bmap_one_block() says there is only one block (ie: no remote blks).
791 xfs_attr_leaf_removename(
792 struct xfs_da_args *args)
794 struct xfs_inode *dp;
798 trace_xfs_attr_leaf_removename(args);
801 * Remove the attribute.
805 error = xfs_attr_leaf_hasname(args, &bp);
807 if (error == -ENOATTR) {
808 xfs_trans_brelse(args->trans, bp);
810 } else if (error != -EEXIST)
813 xfs_attr3_leaf_remove(bp, args);
816 * If the result is small enough, shrink it all into the inode.
818 forkoff = xfs_attr_shortform_allfit(bp, dp);
820 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
821 /* bp is gone due to xfs_da_shrink_inode */
827 * Look up a name in a leaf attribute list structure.
829 * This leaf block cannot have a "remote" value, we only call this routine
830 * if bmap_one_block() says there is only one block (ie: no remote blks).
832 * Returns 0 on successful retrieval, otherwise an error.
835 xfs_attr_leaf_get(xfs_da_args_t *args)
840 trace_xfs_attr_leaf_get(args);
842 error = xfs_attr_leaf_hasname(args, &bp);
844 if (error == -ENOATTR) {
845 xfs_trans_brelse(args->trans, bp);
847 } else if (error != -EEXIST)
851 error = xfs_attr3_leaf_getvalue(bp, args);
852 xfs_trans_brelse(args->trans, bp);
857 * Return EEXIST if attr is found, or ENOATTR if not
858 * statep: If not null is set to point at the found state. Caller will
859 * be responsible for freeing the state in this case.
862 xfs_attr_node_hasname(
863 struct xfs_da_args *args,
864 struct xfs_da_state **statep)
866 struct xfs_da_state *state;
869 state = xfs_da_state_alloc(args);
874 * Search to see if name exists, and get back a pointer to it.
876 error = xfs_da3_node_lookup_int(state, &retval);
878 xfs_da_state_free(state);
885 xfs_da_state_free(state);
889 /*========================================================================
890 * External routines when attribute list size > geo->blksize
891 *========================================================================*/
894 * Add a name to a Btree-format attribute list.
896 * This will involve walking down the Btree, and may involve splitting
897 * leaf nodes and even splitting intermediate nodes up to and including
898 * the root node (a special case of an intermediate node).
900 * "Remote" attribute values confuse the issue and atomic rename operations
901 * add a whole extra layer of confusion on top of that.
904 xfs_attr_node_addname(
905 struct xfs_da_args *args)
907 struct xfs_da_state *state;
908 struct xfs_da_state_blk *blk;
909 struct xfs_inode *dp;
912 trace_xfs_attr_node_addname(args);
915 * Fill in bucket of arguments/results/context to carry around.
920 * Search to see if name already exists, and get back a pointer
921 * to where it should go.
923 retval = xfs_attr_node_hasname(args, &state);
924 if (retval != -ENOATTR && retval != -EEXIST)
927 blk = &state->path.blk[ state->path.active-1 ];
928 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
929 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
931 if (retval == -EEXIST) {
932 if (args->attr_flags & XATTR_CREATE)
935 trace_xfs_attr_node_replace(args);
937 /* save the attribute state for later removal*/
938 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
939 xfs_attr_save_rmt_blk(args);
942 * clear the remote attr state now that it is saved so that the
943 * values reflect the state of the attribute we are about to
944 * add, not the attribute we just found and will remove later.
948 args->rmtvaluelen = 0;
951 retval = xfs_attr3_leaf_add(blk->bp, state->args);
952 if (retval == -ENOSPC) {
953 if (state->path.active == 1) {
955 * Its really a single leaf node, but it had
956 * out-of-line values so it looked like it *might*
957 * have been a b-tree.
959 xfs_da_state_free(state);
961 error = xfs_attr3_leaf_to_node(args);
964 error = xfs_defer_finish(&args->trans);
969 * Commit the node conversion and start the next
970 * trans in the chain.
972 error = xfs_trans_roll_inode(&args->trans, dp);
980 * Split as many Btree elements as required.
981 * This code tracks the new and old attr's location
982 * in the index/blkno/rmtblkno/rmtblkcnt fields and
983 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
985 error = xfs_da3_split(state);
988 error = xfs_defer_finish(&args->trans);
993 * Addition succeeded, update Btree hashvals.
995 xfs_da3_fixhashpath(state, &state->path);
999 * Kill the state structure, we're done with it and need to
1000 * allow the buffers to come back later.
1002 xfs_da_state_free(state);
1006 * Commit the leaf addition or btree split and start the next
1007 * trans in the chain.
1009 error = xfs_trans_roll_inode(&args->trans, dp);
1014 * If there was an out-of-line value, allocate the blocks we
1015 * identified for its storage and copy the value. This is done
1016 * after we create the attribute so that we don't overflow the
1017 * maximum size of a transaction and/or hit a deadlock.
1019 if (args->rmtblkno > 0) {
1020 error = xfs_attr_rmtval_set(args);
1026 * If this is an atomic rename operation, we must "flip" the
1027 * incomplete flags on the "new" and "old" attribute/value pairs
1028 * so that one disappears and one appears atomically. Then we
1029 * must remove the "old" attribute/value pair.
1031 if (args->op_flags & XFS_DA_OP_RENAME) {
1033 * In a separate transaction, set the incomplete flag on the
1034 * "old" attr and clear the incomplete flag on the "new" attr.
1036 error = xfs_attr3_leaf_flipflags(args);
1040 * Commit the flag value change and start the next trans in
1043 error = xfs_trans_roll_inode(&args->trans, args->dp);
1048 * Dismantle the "old" attribute/value pair by removing
1049 * a "remote" value (if it exists).
1051 xfs_attr_restore_rmt_blk(args);
1053 if (args->rmtblkno) {
1054 error = xfs_attr_rmtval_invalidate(args);
1058 error = xfs_attr_rmtval_remove(args);
1064 * Re-find the "old" attribute entry after any split ops.
1065 * The INCOMPLETE flag means that we will find the "old"
1066 * attr, not the "new" one.
1068 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1069 state = xfs_da_state_alloc(args);
1072 error = xfs_da3_node_lookup_int(state, &retval);
1077 * Remove the name and update the hashvals in the tree.
1079 blk = &state->path.blk[ state->path.active-1 ];
1080 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1081 error = xfs_attr3_leaf_remove(blk->bp, args);
1082 xfs_da3_fixhashpath(state, &state->path);
1085 * Check to see if the tree needs to be collapsed.
1087 if (retval && (state->path.active > 1)) {
1088 error = xfs_da3_join(state);
1093 } else if (args->rmtblkno > 0) {
1095 * Added a "remote" value, just clear the incomplete flag.
1097 error = xfs_attr3_leaf_clearflag(args);
1105 xfs_da_state_free(state);
1112 * Shrink an attribute from leaf to shortform
1115 xfs_attr_node_shrink(
1116 struct xfs_da_args *args,
1117 struct xfs_da_state *state)
1119 struct xfs_inode *dp = args->dp;
1124 * Have to get rid of the copy of this dabuf in the state.
1126 ASSERT(state->path.active == 1);
1127 ASSERT(state->path.blk[0].bp);
1128 state->path.blk[0].bp = NULL;
1130 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1134 forkoff = xfs_attr_shortform_allfit(bp, dp);
1136 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1137 /* bp is gone due to xfs_da_shrink_inode */
1139 xfs_trans_brelse(args->trans, bp);
1145 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1146 * for later deletion of the entry.
1149 xfs_attr_leaf_mark_incomplete(
1150 struct xfs_da_args *args,
1151 struct xfs_da_state *state)
1156 * Fill in disk block numbers in the state structure
1157 * so that we can get the buffers back after we commit
1158 * several transactions in the following calls.
1160 error = xfs_attr_fillstate(state);
1165 * Mark the attribute as INCOMPLETE
1167 return xfs_attr3_leaf_setflag(args);
1171 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1172 * the blocks are valid. Attr keys with remote blocks will be marked
1176 int xfs_attr_node_removename_setup(
1177 struct xfs_da_args *args,
1178 struct xfs_da_state **state)
1182 error = xfs_attr_node_hasname(args, state);
1183 if (error != -EEXIST)
1186 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1187 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1188 XFS_ATTR_LEAF_MAGIC);
1190 if (args->rmtblkno > 0) {
1191 error = xfs_attr_leaf_mark_incomplete(args, *state);
1195 return xfs_attr_rmtval_invalidate(args);
1202 xfs_attr_node_remove_rmt(
1203 struct xfs_da_args *args,
1204 struct xfs_da_state *state)
1208 error = xfs_attr_rmtval_remove(args);
1213 * Refill the state structure with buffers, the prior calls released our
1216 return xfs_attr_refillstate(state);
1220 * Remove a name from a B-tree attribute list.
1222 * This will involve walking down the Btree, and may involve joining
1223 * leaf nodes and even joining intermediate nodes up to and including
1224 * the root node (a special case of an intermediate node).
1227 xfs_attr_node_removename(
1228 struct xfs_da_args *args)
1230 struct xfs_da_state *state;
1231 struct xfs_da_state_blk *blk;
1233 struct xfs_inode *dp = args->dp;
1235 trace_xfs_attr_node_removename(args);
1237 error = xfs_attr_node_removename_setup(args, &state);
1242 * If there is an out-of-line value, de-allocate the blocks.
1243 * This is done before we remove the attribute so that we don't
1244 * overflow the maximum size of a transaction and/or hit a deadlock.
1246 if (args->rmtblkno > 0) {
1247 error = xfs_attr_node_remove_rmt(args, state);
1253 * Remove the name and update the hashvals in the tree.
1255 blk = &state->path.blk[ state->path.active-1 ];
1256 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1257 retval = xfs_attr3_leaf_remove(blk->bp, args);
1258 xfs_da3_fixhashpath(state, &state->path);
1261 * Check to see if the tree needs to be collapsed.
1263 if (retval && (state->path.active > 1)) {
1264 error = xfs_da3_join(state);
1267 error = xfs_defer_finish(&args->trans);
1271 * Commit the Btree join operation and start a new trans.
1273 error = xfs_trans_roll_inode(&args->trans, dp);
1279 * If the result is small enough, push it all into the inode.
1281 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
1282 error = xfs_attr_node_shrink(args, state);
1286 xfs_da_state_free(state);
1291 * Fill in the disk block numbers in the state structure for the buffers
1292 * that are attached to the state structure.
1293 * This is done so that we can quickly reattach ourselves to those buffers
1294 * after some set of transaction commits have released these buffers.
1297 xfs_attr_fillstate(xfs_da_state_t *state)
1299 xfs_da_state_path_t *path;
1300 xfs_da_state_blk_t *blk;
1303 trace_xfs_attr_fillstate(state->args);
1306 * Roll down the "path" in the state structure, storing the on-disk
1307 * block number for those buffers in the "path".
1309 path = &state->path;
1310 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1311 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1313 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1316 blk->disk_blkno = 0;
1321 * Roll down the "altpath" in the state structure, storing the on-disk
1322 * block number for those buffers in the "altpath".
1324 path = &state->altpath;
1325 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1326 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1328 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1331 blk->disk_blkno = 0;
1339 * Reattach the buffers to the state structure based on the disk block
1340 * numbers stored in the state structure.
1341 * This is done after some set of transaction commits have released those
1342 * buffers from our grip.
1345 xfs_attr_refillstate(xfs_da_state_t *state)
1347 xfs_da_state_path_t *path;
1348 xfs_da_state_blk_t *blk;
1351 trace_xfs_attr_refillstate(state->args);
1354 * Roll down the "path" in the state structure, storing the on-disk
1355 * block number for those buffers in the "path".
1357 path = &state->path;
1358 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1359 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1360 if (blk->disk_blkno) {
1361 error = xfs_da3_node_read_mapped(state->args->trans,
1362 state->args->dp, blk->disk_blkno,
1363 &blk->bp, XFS_ATTR_FORK);
1372 * Roll down the "altpath" in the state structure, storing the on-disk
1373 * block number for those buffers in the "altpath".
1375 path = &state->altpath;
1376 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1377 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1378 if (blk->disk_blkno) {
1379 error = xfs_da3_node_read_mapped(state->args->trans,
1380 state->args->dp, blk->disk_blkno,
1381 &blk->bp, XFS_ATTR_FORK);
1393 * Retrieve the attribute data from a node attribute list.
1395 * This routine gets called for any attribute fork that has more than one
1396 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1397 * "remote" values taking up more blocks.
1399 * Returns 0 on successful retrieval, otherwise an error.
1403 struct xfs_da_args *args)
1405 struct xfs_da_state *state;
1406 struct xfs_da_state_blk *blk;
1410 trace_xfs_attr_node_get(args);
1413 * Search to see if name exists, and get back a pointer to it.
1415 error = xfs_attr_node_hasname(args, &state);
1416 if (error != -EEXIST)
1420 * Get the value, local or "remote"
1422 blk = &state->path.blk[state->path.active - 1];
1423 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1426 * If not in a transaction, we have to release all the buffers.
1429 for (i = 0; state != NULL && i < state->path.active; i++) {
1430 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1431 state->path.blk[i].bp = NULL;
1435 xfs_da_state_free(state);
1439 /* Returns true if the attribute entry name is valid. */
1446 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1447 * out, so use >= for the length check.
1449 if (length >= MAXNAMELEN)
1452 /* There shouldn't be any nulls here */
1453 return !memchr(name, 0, length);