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"
27 #include "xfs_attr_item.h"
28 #include "xfs_xattr.h"
29 #include "xfs_parent.h"
31 struct kmem_cache *xfs_attr_intent_cache;
36 * Provide the external interfaces to manage attribute lists.
39 /*========================================================================
40 * Function prototypes for the kernel.
41 *========================================================================*/
44 * Internal routines when attribute list fits inside the inode.
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
49 * Internal routines when attribute list is one block.
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
54 STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args);
57 * Internal routines when attribute list is more than one block.
59 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
60 STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
61 static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
62 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
63 STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
64 STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
65 struct xfs_da_state *state);
71 if (!xfs_inode_has_attr_fork(ip))
73 if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
74 ip->i_af.if_nextents == 0)
80 * Returns true if the there is exactly only block in the attr fork, in which
81 * case the attribute fork consists of a single leaf block entry.
87 struct xfs_ifork *ifp = &ip->i_af;
88 struct xfs_iext_cursor icur;
89 struct xfs_bmbt_irec imap;
91 ASSERT(!xfs_need_iread_extents(ifp));
93 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
96 xfs_iext_first(ifp, &icur);
97 xfs_iext_get_extent(ifp, &icur, &imap);
98 return imap.br_startoff == 0 && imap.br_blockcount == 1;
102 * XXX (dchinner): name path state saving and refilling is an optimisation to
103 * avoid needing to look up name entries after rolling transactions removing
104 * remote xattr blocks between the name entry lookup and name entry removal.
105 * This optimisation got sidelined when combining the set and remove state
106 * machines, but the code has been left in place because it is worthwhile to
107 * restore the optimisation once the combined state machine paths have settled.
109 * This comment is a public service announcement to remind Future Dave that he
110 * still needs to restore this code to working order.
114 * Fill in the disk block numbers in the state structure for the buffers
115 * that are attached to the state structure.
116 * This is done so that we can quickly reattach ourselves to those buffers
117 * after some set of transaction commits have released these buffers.
120 xfs_attr_fillstate(xfs_da_state_t *state)
122 xfs_da_state_path_t *path;
123 xfs_da_state_blk_t *blk;
126 trace_xfs_attr_fillstate(state->args);
129 * Roll down the "path" in the state structure, storing the on-disk
130 * block number for those buffers in the "path".
133 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
134 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
136 blk->disk_blkno = xfs_buf_daddr(blk->bp);
144 * Roll down the "altpath" in the state structure, storing the on-disk
145 * block number for those buffers in the "altpath".
147 path = &state->altpath;
148 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
149 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
151 blk->disk_blkno = xfs_buf_daddr(blk->bp);
162 * Reattach the buffers to the state structure based on the disk block
163 * numbers stored in the state structure.
164 * This is done after some set of transaction commits have released those
165 * buffers from our grip.
168 xfs_attr_refillstate(xfs_da_state_t *state)
170 xfs_da_state_path_t *path;
171 xfs_da_state_blk_t *blk;
174 trace_xfs_attr_refillstate(state->args);
177 * Roll down the "path" in the state structure, storing the on-disk
178 * block number for those buffers in the "path".
181 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
182 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
183 if (blk->disk_blkno) {
184 error = xfs_da3_node_read_mapped(state->args->trans,
185 state->args->dp, blk->disk_blkno,
186 &blk->bp, XFS_ATTR_FORK);
195 * Roll down the "altpath" in the state structure, storing the on-disk
196 * block number for those buffers in the "altpath".
198 path = &state->altpath;
199 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
200 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
201 if (blk->disk_blkno) {
202 error = xfs_da3_node_read_mapped(state->args->trans,
203 state->args->dp, blk->disk_blkno,
204 &blk->bp, XFS_ATTR_FORK);
215 static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
218 /*========================================================================
219 * Overall external interface routines.
220 *========================================================================*/
223 * Retrieve an extended attribute and its value. Must have ilock.
224 * Returns 0 on successful retrieval, otherwise an error.
227 xfs_attr_get_ilocked(
228 struct xfs_da_args *args)
232 xfs_assert_ilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
234 if (!xfs_inode_hasattr(args->dp))
238 * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf
241 error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
245 if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
246 return xfs_attr_shortform_getvalue(args);
247 if (xfs_attr_is_leaf(args->dp))
248 return xfs_attr_leaf_get(args);
249 return xfs_attr_node_get(args);
253 * Retrieve an extended attribute by name, and its value if requested.
255 * If args->valuelen is zero, then the caller does not want the value, just an
256 * indication whether the attribute exists and the size of the value if it
257 * exists. The size is returned in args.valuelen.
259 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
260 * for the value after existence of the attribute has been determined. The
261 * caller always has to free args->value if it is set, no matter if this
262 * function was successful or not.
264 * If the attribute is found, but exceeds the size limit set by the caller in
265 * args->valuelen, return -ERANGE with the size of the attribute that was found
270 struct xfs_da_args *args)
275 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
277 if (xfs_is_shutdown(args->dp->i_mount))
281 args->owner = args->dp->i_ino;
282 args->geo = args->dp->i_mount->m_attr_geo;
283 args->whichfork = XFS_ATTR_FORK;
284 xfs_attr_sethash(args);
286 /* Entirely possible to look up a name which doesn't exist */
287 args->op_flags = XFS_DA_OP_OKNOENT;
289 lock_mode = xfs_ilock_attr_map_shared(args->dp);
290 error = xfs_attr_get_ilocked(args);
291 xfs_iunlock(args->dp, lock_mode);
297 * Calculate how many blocks we need for the new attribute,
301 struct xfs_da_args *args,
304 struct xfs_mount *mp = args->dp->i_mount;
309 * Determine space new attribute will use, and if it would be
310 * "local" or "remote" (note: local != inline).
312 size = xfs_attr_leaf_newentsize(args, local);
313 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
315 if (size > (args->geo->blksize / 2)) {
316 /* Double split possible */
321 * Out of line attribute, cannot double split, but
322 * make room for the attribute value itself.
324 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
326 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
332 /* Initialize transaction reservation for attr operations */
335 struct xfs_da_args *args,
336 struct xfs_trans_res *tres,
339 struct xfs_mount *mp = args->dp->i_mount;
342 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
343 M_RES(mp)->tr_attrsetrt.tr_logres *
345 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
346 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
347 *total = args->total;
349 *tres = M_RES(mp)->tr_attrrm;
350 *total = XFS_ATTRRM_SPACE_RES(mp);
355 * Add an attr to a shortform fork. If there is no space,
356 * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
360 xfs_attr_try_sf_addname(
361 struct xfs_inode *dp,
362 struct xfs_da_args *args)
368 * Build initial attribute list (if required).
370 if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
371 xfs_attr_shortform_create(args);
373 error = xfs_attr_shortform_addname(args);
374 if (error == -ENOSPC)
378 * Commit the shortform mods, and we're done.
379 * NOTE: this is also the error path (EEXIST, etc).
382 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
384 if (xfs_has_wsync(dp->i_mount))
385 xfs_trans_set_sync(args->trans);
392 struct xfs_attr_intent *attr)
394 struct xfs_da_args *args = attr->xattri_da_args;
395 struct xfs_inode *dp = args->dp;
398 error = xfs_attr_try_sf_addname(dp, args);
399 if (error != -ENOSPC) {
400 ASSERT(!error || error == -EEXIST);
401 attr->xattri_dela_state = XFS_DAS_DONE;
406 * It won't fit in the shortform, transform to a leaf block. GROT:
407 * another possible req'mt for a double-split btree op.
409 error = xfs_attr_shortform_to_leaf(args);
413 attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
415 trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
419 /* Compute the hash value for a user/root/secure extended attribute */
425 return xfs_da_hashname(name, namelen);
428 /* Compute the hash value for any extended attribute from any namespace. */
431 struct xfs_mount *mp,
432 unsigned int attr_flags,
438 ASSERT(xfs_attr_check_namespace(attr_flags));
440 if (attr_flags & XFS_ATTR_PARENT)
441 return xfs_parent_hashattr(mp, name, namelen, value, valuelen);
443 return xfs_attr_hashname(name, namelen);
447 * PPTR_REPLACE operations require the caller to set the old and new names and
448 * values explicitly. Update the canonical fields to the new name and value
449 * here now that the removal phase has finished.
452 xfs_attr_update_pptr_replace_args(
453 struct xfs_da_args *args)
455 ASSERT(args->new_namelen > 0);
456 args->name = args->new_name;
457 args->namelen = args->new_namelen;
458 args->value = args->new_value;
459 args->valuelen = args->new_valuelen;
460 xfs_attr_sethash(args);
464 * Handle the state change on completion of a multi-state attr operation.
466 * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
467 * modification in a attr replace operation and we still have to do the second
468 * state, indicated by @replace_state.
470 * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
471 * completion of the second half of the attr replace operation we correctly
472 * signal that it is done.
474 static enum xfs_delattr_state
475 xfs_attr_complete_op(
476 struct xfs_attr_intent *attr,
477 enum xfs_delattr_state replace_state)
479 struct xfs_da_args *args = attr->xattri_da_args;
481 if (!(args->op_flags & XFS_DA_OP_REPLACE))
482 replace_state = XFS_DAS_DONE;
483 else if (xfs_attr_intent_op(attr) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE)
484 xfs_attr_update_pptr_replace_args(args);
486 args->op_flags &= ~XFS_DA_OP_REPLACE;
487 args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
488 return replace_state;
492 xfs_attr_leaf_addname(
493 struct xfs_attr_intent *attr)
495 struct xfs_da_args *args = attr->xattri_da_args;
498 ASSERT(xfs_attr_is_leaf(args->dp));
501 * Use the leaf buffer we may already hold locked as a result of
502 * a sf-to-leaf conversion.
504 error = xfs_attr_leaf_try_add(args);
506 if (error == -ENOSPC) {
507 error = xfs_attr3_leaf_to_node(args);
512 * We're not in leaf format anymore, so roll the transaction and
513 * retry the add to the newly allocated node block.
515 attr->xattri_dela_state = XFS_DAS_NODE_ADD;
522 * We need to commit and roll if we need to allocate remote xattr blocks
523 * or perform more xattr manipulations. Otherwise there is nothing more
524 * to do and we can return success.
527 attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
529 attr->xattri_dela_state = xfs_attr_complete_op(attr,
530 XFS_DAS_LEAF_REPLACE);
532 trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
537 * Add an entry to a node format attr tree.
539 * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
540 * the difference between leaf + remote attr blocks and a node format tree,
541 * so we may still end up having to convert from leaf to node format here.
544 xfs_attr_node_addname(
545 struct xfs_attr_intent *attr)
547 struct xfs_da_args *args = attr->xattri_da_args;
550 error = xfs_attr_node_addname_find_attr(attr);
554 error = xfs_attr_node_try_addname(attr);
555 if (error == -ENOSPC) {
556 error = xfs_attr3_leaf_to_node(args);
560 * No state change, we really are in node form now
561 * but we need the transaction rolled to continue.
569 attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
571 attr->xattri_dela_state = xfs_attr_complete_op(attr,
572 XFS_DAS_NODE_REPLACE);
574 trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
579 xfs_attr_rmtval_alloc(
580 struct xfs_attr_intent *attr)
582 struct xfs_da_args *args = attr->xattri_da_args;
586 * If there was an out-of-line value, allocate the blocks we
587 * identified for its storage and copy the value. This is done
588 * after we create the attribute so that we don't overflow the
589 * maximum size of a transaction and/or hit a deadlock.
591 if (attr->xattri_blkcnt > 0) {
592 error = xfs_attr_rmtval_set_blk(attr);
595 /* Roll the transaction only if there is more to allocate. */
596 if (attr->xattri_blkcnt > 0)
600 error = xfs_attr_rmtval_set_value(args);
604 attr->xattri_dela_state = xfs_attr_complete_op(attr,
605 ++attr->xattri_dela_state);
607 * If we are not doing a rename, we've finished the operation but still
608 * have to clear the incomplete flag protecting the new attr from
609 * exposing partially initialised state if we crash during creation.
611 if (attr->xattri_dela_state == XFS_DAS_DONE)
612 error = xfs_attr3_leaf_clearflag(args);
614 trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
619 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
620 * for later deletion of the entry.
623 xfs_attr_leaf_mark_incomplete(
624 struct xfs_da_args *args,
625 struct xfs_da_state *state)
630 * Fill in disk block numbers in the state structure
631 * so that we can get the buffers back after we commit
632 * several transactions in the following calls.
634 error = xfs_attr_fillstate(state);
639 * Mark the attribute as INCOMPLETE
641 return xfs_attr3_leaf_setflag(args);
644 /* Ensure the da state of an xattr deferred work item is ready to go. */
646 xfs_attr_item_init_da_state(
647 struct xfs_attr_intent *attr)
649 struct xfs_da_args *args = attr->xattri_da_args;
651 if (!attr->xattri_da_state)
652 attr->xattri_da_state = xfs_da_state_alloc(args);
654 xfs_da_state_reset(attr->xattri_da_state, args);
658 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
659 * the blocks are valid. Attr keys with remote blocks will be marked
663 int xfs_attr_node_removename_setup(
664 struct xfs_attr_intent *attr)
666 struct xfs_da_args *args = attr->xattri_da_args;
667 struct xfs_da_state *state;
670 xfs_attr_item_init_da_state(attr);
671 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
672 if (error != -EEXIST)
676 state = attr->xattri_da_state;
677 ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
678 ASSERT(state->path.blk[state->path.active - 1].magic ==
679 XFS_ATTR_LEAF_MAGIC);
681 error = xfs_attr_leaf_mark_incomplete(args, state);
684 if (args->rmtblkno > 0)
685 error = xfs_attr_rmtval_invalidate(args);
688 xfs_da_state_free(attr->xattri_da_state);
689 attr->xattri_da_state = NULL;
696 * Remove the original attr we have just replaced. This is dependent on the
697 * original lookup and insert placing the old attr in args->blkno/args->index
698 * and the new attr in args->blkno2/args->index2.
701 xfs_attr_leaf_remove_attr(
702 struct xfs_attr_intent *attr)
704 struct xfs_da_args *args = attr->xattri_da_args;
705 struct xfs_inode *dp = args->dp;
706 struct xfs_buf *bp = NULL;
710 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
715 xfs_attr3_leaf_remove(bp, args);
717 forkoff = xfs_attr_shortform_allfit(bp, dp);
719 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
720 /* bp is gone due to xfs_da_shrink_inode */
726 * Shrink an attribute from leaf to shortform. Used by the node format remove
727 * path when the node format collapses to a single block and so we have to check
728 * if it can be collapsed further.
731 xfs_attr_leaf_shrink(
732 struct xfs_da_args *args)
734 struct xfs_inode *dp = args->dp;
739 if (!xfs_attr_is_leaf(dp))
742 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
746 forkoff = xfs_attr_shortform_allfit(bp, dp);
748 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
749 /* bp is gone due to xfs_da_shrink_inode */
751 xfs_trans_brelse(args->trans, bp);
758 * Run the attribute operation specified in @attr.
760 * This routine is meant to function as a delayed operation and will set the
761 * state to XFS_DAS_DONE when the operation is complete. Calling functions will
762 * need to handle this, and recall the function until either an error or
763 * XFS_DAS_DONE is detected.
767 struct xfs_attr_intent *attr)
769 struct xfs_da_args *args = attr->xattri_da_args;
772 /* State machine switch */
774 switch (attr->xattri_dela_state) {
777 return -EFSCORRUPTED;
779 return xfs_attr_sf_addname(attr);
780 case XFS_DAS_LEAF_ADD:
781 return xfs_attr_leaf_addname(attr);
782 case XFS_DAS_NODE_ADD:
783 return xfs_attr_node_addname(attr);
785 case XFS_DAS_SF_REMOVE:
786 error = xfs_attr_sf_removename(args);
787 attr->xattri_dela_state = xfs_attr_complete_op(attr,
788 xfs_attr_init_add_state(args));
790 case XFS_DAS_LEAF_REMOVE:
791 error = xfs_attr_leaf_removename(args);
792 attr->xattri_dela_state = xfs_attr_complete_op(attr,
793 xfs_attr_init_add_state(args));
795 case XFS_DAS_NODE_REMOVE:
796 error = xfs_attr_node_removename_setup(attr);
797 if (error == -ENOATTR &&
798 (args->op_flags & XFS_DA_OP_RECOVERY)) {
799 attr->xattri_dela_state = xfs_attr_complete_op(attr,
800 xfs_attr_init_add_state(args));
806 attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
807 if (args->rmtblkno == 0)
808 attr->xattri_dela_state++;
811 case XFS_DAS_LEAF_SET_RMT:
812 case XFS_DAS_NODE_SET_RMT:
813 error = xfs_attr_rmtval_find_space(attr);
816 attr->xattri_dela_state++;
819 case XFS_DAS_LEAF_ALLOC_RMT:
820 case XFS_DAS_NODE_ALLOC_RMT:
821 error = xfs_attr_rmtval_alloc(attr);
824 if (attr->xattri_dela_state == XFS_DAS_DONE)
828 case XFS_DAS_LEAF_REPLACE:
829 case XFS_DAS_NODE_REPLACE:
831 * We must "flip" the incomplete flags on the "new" and "old"
832 * attribute/value pairs so that one disappears and one appears
835 error = xfs_attr3_leaf_flipflags(args);
839 * We must commit the flag value change now to make it atomic
840 * and then we can start the next trans in series at REMOVE_OLD.
842 attr->xattri_dela_state++;
845 case XFS_DAS_LEAF_REMOVE_OLD:
846 case XFS_DAS_NODE_REMOVE_OLD:
848 * If we have a remote attr, start the process of removing it
849 * by invalidating any cached buffers.
851 * If we don't have a remote attr, we skip the remote block
852 * removal state altogether with a second state increment.
854 xfs_attr_restore_rmt_blk(args);
855 if (args->rmtblkno) {
856 error = xfs_attr_rmtval_invalidate(args);
860 attr->xattri_dela_state++;
863 attr->xattri_dela_state++;
866 case XFS_DAS_LEAF_REMOVE_RMT:
867 case XFS_DAS_NODE_REMOVE_RMT:
868 error = xfs_attr_rmtval_remove(attr);
869 if (error == -EAGAIN) {
877 * We've finished removing the remote attr blocks, so commit the
878 * transaction and move on to removing the attr name from the
879 * leaf/node block. Removing the attr might require a full
880 * transaction reservation for btree block freeing, so we
881 * can't do that in the same transaction where we removed the
882 * remote attr blocks.
884 attr->xattri_dela_state++;
887 case XFS_DAS_LEAF_REMOVE_ATTR:
888 error = xfs_attr_leaf_remove_attr(attr);
889 attr->xattri_dela_state = xfs_attr_complete_op(attr,
890 xfs_attr_init_add_state(args));
893 case XFS_DAS_NODE_REMOVE_ATTR:
894 error = xfs_attr_node_remove_attr(attr);
896 error = xfs_attr_leaf_shrink(args);
897 attr->xattri_dela_state = xfs_attr_complete_op(attr,
898 xfs_attr_init_add_state(args));
905 trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
911 * Return EEXIST if attr is found, or ENOATTR if not
915 struct xfs_da_args *args)
917 struct xfs_inode *dp = args->dp;
918 struct xfs_buf *bp = NULL;
919 struct xfs_da_state *state;
922 if (!xfs_inode_hasattr(dp))
925 if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL) {
926 if (xfs_attr_sf_findname(args))
931 /* Prerequisite for xfs_attr_is_leaf */
932 error = xfs_iread_extents(args->trans, args->dp, XFS_ATTR_FORK);
936 if (xfs_attr_is_leaf(dp)) {
937 error = xfs_attr_leaf_hasname(args, &bp);
940 xfs_trans_brelse(args->trans, bp);
945 state = xfs_da_state_alloc(args);
946 error = xfs_attr_node_lookup(args, state);
947 xfs_da_state_free(state);
953 struct xfs_inode *ip, /* incore inode pointer */
954 int size, /* space new attribute needs */
955 int rsvd) /* xact may use reserved blks */
957 struct xfs_mount *mp = ip->i_mount;
958 struct xfs_trans *tp; /* transaction pointer */
959 unsigned int blks; /* space reservation */
960 int error; /* error return value */
962 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
964 blks = XFS_ADDAFORK_SPACE_RES(mp);
966 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_addafork, blks, 0,
971 if (xfs_inode_has_attr_fork(ip))
974 error = xfs_bmap_add_attrfork(tp, ip, size, rsvd);
978 error = xfs_trans_commit(tp);
979 xfs_iunlock(ip, XFS_ILOCK_EXCL);
983 xfs_trans_cancel(tp);
984 xfs_iunlock(ip, XFS_ILOCK_EXCL);
989 * Make a change to the xattr structure.
991 * The caller must have initialized @args, attached dquots, and must not hold
992 * any ILOCKs. Reserved data blocks may be used if @rsvd is set.
994 * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists.
995 * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist.
996 * Returns 0 on success, or a negative errno if something else went wrong.
1000 struct xfs_da_args *args,
1001 enum xfs_attr_update op,
1004 struct xfs_inode *dp = args->dp;
1005 struct xfs_mount *mp = dp->i_mount;
1006 struct xfs_trans_res tres;
1011 ASSERT(!args->trans);
1014 case XFS_ATTRUPDATE_UPSERT:
1015 case XFS_ATTRUPDATE_CREATE:
1016 case XFS_ATTRUPDATE_REPLACE:
1017 XFS_STATS_INC(mp, xs_attr_set);
1018 args->total = xfs_attr_calc_size(args, &local);
1021 * If the inode doesn't have an attribute fork, add one.
1022 * (inode must not be locked when we call this routine)
1024 if (xfs_inode_has_attr_fork(dp) == 0) {
1025 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1026 xfs_attr_sf_entsize_byname(args->namelen,
1029 error = xfs_attr_add_fork(dp, sf_size, rsvd);
1035 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1037 case XFS_ATTRUPDATE_REMOVE:
1038 XFS_STATS_INC(mp, xs_attr_remove);
1039 rmt_blks = xfs_attr3_max_rmt_blocks(mp);
1044 * Root fork attributes can use reserved data blocks for this
1045 * operation if necessary
1047 xfs_init_attr_trans(args, &tres, &total);
1048 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
1052 if (op != XFS_ATTRUPDATE_REMOVE || xfs_inode_hasattr(dp)) {
1053 error = xfs_iext_count_extend(args->trans, dp, XFS_ATTR_FORK,
1054 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1056 goto out_trans_cancel;
1059 error = xfs_attr_lookup(args);
1062 if (op == XFS_ATTRUPDATE_REMOVE) {
1063 /* if no value, we are performing a remove operation */
1064 xfs_attr_defer_add(args, XFS_ATTR_DEFER_REMOVE);
1068 /* Pure create fails if the attr already exists */
1069 if (op == XFS_ATTRUPDATE_CREATE)
1070 goto out_trans_cancel;
1071 xfs_attr_defer_add(args, XFS_ATTR_DEFER_REPLACE);
1074 /* Can't remove what isn't there. */
1075 if (op == XFS_ATTRUPDATE_REMOVE)
1076 goto out_trans_cancel;
1078 /* Pure replace fails if no existing attr to replace. */
1079 if (op == XFS_ATTRUPDATE_REPLACE)
1080 goto out_trans_cancel;
1081 xfs_attr_defer_add(args, XFS_ATTR_DEFER_SET);
1084 goto out_trans_cancel;
1088 * If this is a synchronous mount, make sure that the
1089 * transaction goes to disk before returning to the user.
1091 if (xfs_has_wsync(mp))
1092 xfs_trans_set_sync(args->trans);
1094 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
1097 * Commit the last in the sequence of transactions.
1099 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1100 error = xfs_trans_commit(args->trans);
1102 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1108 xfs_trans_cancel(args->trans);
1112 /*========================================================================
1113 * External routines when attribute list is inside the inode
1114 *========================================================================*/
1116 int xfs_attr_sf_totsize(struct xfs_inode *dp)
1118 struct xfs_attr_sf_hdr *sf = dp->i_af.if_data;
1120 return be16_to_cpu(sf->totsize);
1124 * Add a name to the shortform attribute list structure
1125 * This is the external routine.
1128 xfs_attr_shortform_addname(
1129 struct xfs_da_args *args)
1131 int newsize, forkoff;
1133 trace_xfs_attr_sf_addname(args);
1135 if (xfs_attr_sf_findname(args)) {
1138 ASSERT(args->op_flags & XFS_DA_OP_REPLACE);
1140 error = xfs_attr_sf_removename(args);
1145 * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1146 * so that the new attr doesn't fit in shortform format, the
1147 * leaf format add routine won't trip over the attr not being
1150 args->op_flags &= ~XFS_DA_OP_REPLACE;
1152 ASSERT(!(args->op_flags & XFS_DA_OP_REPLACE));
1155 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1156 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1159 newsize = xfs_attr_sf_totsize(args->dp);
1160 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1162 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1166 xfs_attr_shortform_add(args, forkoff);
1171 /*========================================================================
1172 * External routines when attribute list is one block
1173 *========================================================================*/
1175 /* Save the current remote block info and clear the current pointers. */
1177 xfs_attr_save_rmt_blk(
1178 struct xfs_da_args *args)
1180 args->blkno2 = args->blkno;
1181 args->index2 = args->index;
1182 args->rmtblkno2 = args->rmtblkno;
1183 args->rmtblkcnt2 = args->rmtblkcnt;
1184 args->rmtvaluelen2 = args->rmtvaluelen;
1186 args->rmtblkcnt = 0;
1187 args->rmtvaluelen = 0;
1190 /* Set stored info about a remote block */
1192 xfs_attr_restore_rmt_blk(
1193 struct xfs_da_args *args)
1195 args->blkno = args->blkno2;
1196 args->index = args->index2;
1197 args->rmtblkno = args->rmtblkno2;
1198 args->rmtblkcnt = args->rmtblkcnt2;
1199 args->rmtvaluelen = args->rmtvaluelen2;
1203 * Tries to add an attribute to an inode in leaf form
1205 * This function is meant to execute as part of a delayed operation and leaves
1206 * the transaction handling to the caller. On success the attribute is added
1207 * and the inode and transaction are left dirty. If there is not enough space,
1208 * the attr data is converted to node format and -ENOSPC is returned. Caller is
1209 * responsible for handling the dirty inode and transaction or adding the attr
1213 xfs_attr_leaf_try_add(
1214 struct xfs_da_args *args)
1219 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, &bp);
1224 * Look up the xattr name to set the insertion point for the new xattr.
1226 error = xfs_attr3_leaf_lookup_int(bp, args);
1229 if (args->op_flags & XFS_DA_OP_REPLACE)
1233 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1236 trace_xfs_attr_leaf_replace(args);
1238 * Save the existing remote attr state so that the current
1239 * values reflect the state of the new attribute we are about to
1240 * add, not the attribute we just found and will remove later.
1242 xfs_attr_save_rmt_blk(args);
1250 return xfs_attr3_leaf_add(bp, args);
1253 xfs_trans_brelse(args->trans, bp);
1258 * Return EEXIST if attr is found, or ENOATTR if not
1261 xfs_attr_leaf_hasname(
1262 struct xfs_da_args *args,
1263 struct xfs_buf **bp)
1267 error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner, 0, bp);
1271 error = xfs_attr3_leaf_lookup_int(*bp, args);
1272 if (error != -ENOATTR && error != -EEXIST)
1273 xfs_trans_brelse(args->trans, *bp);
1279 * Remove a name from the leaf attribute list structure
1281 * This leaf block cannot have a "remote" value, we only call this routine
1282 * if bmap_one_block() says there is only one block (ie: no remote blks).
1285 xfs_attr_leaf_removename(
1286 struct xfs_da_args *args)
1288 struct xfs_inode *dp;
1292 trace_xfs_attr_leaf_removename(args);
1295 * Remove the attribute.
1299 error = xfs_attr_leaf_hasname(args, &bp);
1300 if (error == -ENOATTR) {
1301 xfs_trans_brelse(args->trans, bp);
1302 if (args->op_flags & XFS_DA_OP_RECOVERY)
1305 } else if (error != -EEXIST)
1308 xfs_attr3_leaf_remove(bp, args);
1311 * If the result is small enough, shrink it all into the inode.
1313 forkoff = xfs_attr_shortform_allfit(bp, dp);
1315 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1316 /* bp is gone due to xfs_da_shrink_inode */
1322 * Look up a name in a leaf attribute list structure.
1324 * This leaf block cannot have a "remote" value, we only call this routine
1325 * if bmap_one_block() says there is only one block (ie: no remote blks).
1327 * Returns 0 on successful retrieval, otherwise an error.
1330 xfs_attr_leaf_get(xfs_da_args_t *args)
1335 trace_xfs_attr_leaf_get(args);
1337 error = xfs_attr_leaf_hasname(args, &bp);
1339 if (error == -ENOATTR) {
1340 xfs_trans_brelse(args->trans, bp);
1342 } else if (error != -EEXIST)
1346 error = xfs_attr3_leaf_getvalue(bp, args);
1347 xfs_trans_brelse(args->trans, bp);
1351 /* Return EEXIST if attr is found, or ENOATTR if not. */
1353 xfs_attr_node_lookup(
1354 struct xfs_da_args *args,
1355 struct xfs_da_state *state)
1360 * Search to see if name exists, and get back a pointer to it.
1362 error = xfs_da3_node_lookup_int(state, &retval);
1369 /*========================================================================
1370 * External routines when attribute list size > geo->blksize
1371 *========================================================================*/
1374 xfs_attr_node_addname_find_attr(
1375 struct xfs_attr_intent *attr)
1377 struct xfs_da_args *args = attr->xattri_da_args;
1381 * Search to see if name already exists, and get back a pointer
1382 * to where it should go.
1384 xfs_attr_item_init_da_state(attr);
1385 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1388 if (args->op_flags & XFS_DA_OP_REPLACE)
1392 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1396 trace_xfs_attr_node_replace(args);
1398 * Save the existing remote attr state so that the current
1399 * values reflect the state of the new attribute we are about to
1400 * add, not the attribute we just found and will remove later.
1402 xfs_attr_save_rmt_blk(args);
1412 if (attr->xattri_da_state) {
1413 xfs_da_state_free(attr->xattri_da_state);
1414 attr->xattri_da_state = NULL;
1420 * Add a name to a Btree-format attribute list.
1422 * This will involve walking down the Btree, and may involve splitting
1423 * leaf nodes and even splitting intermediate nodes up to and including
1424 * the root node (a special case of an intermediate node).
1427 xfs_attr_node_try_addname(
1428 struct xfs_attr_intent *attr)
1430 struct xfs_da_state *state = attr->xattri_da_state;
1431 struct xfs_da_state_blk *blk;
1434 trace_xfs_attr_node_addname(state->args);
1436 blk = &state->path.blk[state->path.active-1];
1437 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1439 error = xfs_attr3_leaf_add(blk->bp, state->args);
1440 if (error == -ENOSPC) {
1441 if (state->path.active == 1) {
1443 * Its really a single leaf node, but it had
1444 * out-of-line values so it looked like it *might*
1445 * have been a b-tree. Let the caller deal with this.
1451 * Split as many Btree elements as required.
1452 * This code tracks the new and old attr's location
1453 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1454 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1456 error = xfs_da3_split(state);
1461 * Addition succeeded, update Btree hashvals.
1463 xfs_da3_fixhashpath(state, &state->path);
1467 xfs_da_state_free(state);
1468 attr->xattri_da_state = NULL;
1473 xfs_attr_node_removename(
1474 struct xfs_da_args *args,
1475 struct xfs_da_state *state)
1477 struct xfs_da_state_blk *blk;
1481 * Remove the name and update the hashvals in the tree.
1483 blk = &state->path.blk[state->path.active-1];
1484 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1485 retval = xfs_attr3_leaf_remove(blk->bp, args);
1486 xfs_da3_fixhashpath(state, &state->path);
1492 xfs_attr_node_remove_attr(
1493 struct xfs_attr_intent *attr)
1495 struct xfs_da_args *args = attr->xattri_da_args;
1496 struct xfs_da_state *state = xfs_da_state_alloc(args);
1501 * The attr we are removing has already been marked incomplete, so
1502 * we need to set the filter appropriately to re-find the "old"
1503 * attribute entry after any split ops.
1505 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1506 error = xfs_da3_node_lookup_int(state, &retval);
1510 error = xfs_attr_node_removename(args, state);
1513 * Check to see if the tree needs to be collapsed.
1515 if (retval && (state->path.active > 1)) {
1516 error = xfs_da3_join(state);
1523 xfs_da_state_free(state);
1530 * Retrieve the attribute data from a node attribute list.
1532 * This routine gets called for any attribute fork that has more than one
1533 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1534 * "remote" values taking up more blocks.
1536 * Returns 0 on successful retrieval, otherwise an error.
1540 struct xfs_da_args *args)
1542 struct xfs_da_state *state;
1543 struct xfs_da_state_blk *blk;
1547 trace_xfs_attr_node_get(args);
1550 * Search to see if name exists, and get back a pointer to it.
1552 state = xfs_da_state_alloc(args);
1553 error = xfs_attr_node_lookup(args, state);
1554 if (error != -EEXIST)
1558 * Get the value, local or "remote"
1560 blk = &state->path.blk[state->path.active - 1];
1561 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1564 * If not in a transaction, we have to release all the buffers.
1567 for (i = 0; i < state->path.active; i++) {
1568 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1569 state->path.blk[i].bp = NULL;
1572 xfs_da_state_free(state);
1576 /* Enforce that there is at most one namespace bit per attr. */
1577 inline bool xfs_attr_check_namespace(unsigned int attr_flags)
1579 return hweight32(attr_flags & XFS_ATTR_NSP_ONDISK_MASK) < 2;
1582 /* Returns true if the attribute entry name is valid. */
1585 unsigned int attr_flags,
1589 /* Only one namespace bit allowed. */
1590 if (!xfs_attr_check_namespace(attr_flags))
1594 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1595 * out, so use >= for the length check.
1597 if (length >= MAXNAMELEN)
1600 /* Parent pointers have their own validation. */
1601 if (attr_flags & XFS_ATTR_PARENT)
1602 return xfs_parent_namecheck(attr_flags, name, length);
1604 /* There shouldn't be any nulls here */
1605 return !memchr(name, 0, length);
1609 xfs_attr_intent_init_cache(void)
1611 xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1612 sizeof(struct xfs_attr_intent),
1615 return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1619 xfs_attr_intent_destroy_cache(void)
1621 kmem_cache_destroy(xfs_attr_intent_cache);
1622 xfs_attr_intent_cache = NULL;