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"
30 struct kmem_cache *xfs_attr_intent_cache;
35 * Provide the external interfaces to manage attribute lists.
38 /*========================================================================
39 * Function prototypes for the kernel.
40 *========================================================================*/
43 * Internal routines when attribute list fits inside the inode.
45 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
48 * Internal routines when attribute list is one block.
50 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
51 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
53 STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args);
56 * Internal routines when attribute list is more than one block.
58 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
59 STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
60 static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
61 STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
62 STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
63 STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
64 struct xfs_da_state *state);
74 if (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
75 ip->i_afp->if_nextents == 0)
81 * Returns true if the there is exactly only block in the attr fork, in which
82 * case the attribute fork consists of a single leaf block entry.
88 struct xfs_ifork *ifp = ip->i_afp;
89 struct xfs_iext_cursor icur;
90 struct xfs_bmbt_irec imap;
92 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
95 xfs_iext_first(ifp, &icur);
96 xfs_iext_get_extent(ifp, &icur, &imap);
97 return imap.br_startoff == 0 && imap.br_blockcount == 1;
101 * XXX (dchinner): name path state saving and refilling is an optimisation to
102 * avoid needing to look up name entries after rolling transactions removing
103 * remote xattr blocks between the name entry lookup and name entry removal.
104 * This optimisation got sidelined when combining the set and remove state
105 * machines, but the code has been left in place because it is worthwhile to
106 * restore the optimisation once the combined state machine paths have settled.
108 * This comment is a public service announcement to remind Future Dave that he
109 * still needs to restore this code to working order.
113 * Fill in the disk block numbers in the state structure for the buffers
114 * that are attached to the state structure.
115 * This is done so that we can quickly reattach ourselves to those buffers
116 * after some set of transaction commits have released these buffers.
119 xfs_attr_fillstate(xfs_da_state_t *state)
121 xfs_da_state_path_t *path;
122 xfs_da_state_blk_t *blk;
125 trace_xfs_attr_fillstate(state->args);
128 * Roll down the "path" in the state structure, storing the on-disk
129 * block number for those buffers in the "path".
132 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
133 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
135 blk->disk_blkno = xfs_buf_daddr(blk->bp);
143 * Roll down the "altpath" in the state structure, storing the on-disk
144 * block number for those buffers in the "altpath".
146 path = &state->altpath;
147 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
148 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
150 blk->disk_blkno = xfs_buf_daddr(blk->bp);
161 * Reattach the buffers to the state structure based on the disk block
162 * numbers stored in the state structure.
163 * This is done after some set of transaction commits have released those
164 * buffers from our grip.
167 xfs_attr_refillstate(xfs_da_state_t *state)
169 xfs_da_state_path_t *path;
170 xfs_da_state_blk_t *blk;
173 trace_xfs_attr_refillstate(state->args);
176 * Roll down the "path" in the state structure, storing the on-disk
177 * block number for those buffers in the "path".
180 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
181 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
182 if (blk->disk_blkno) {
183 error = xfs_da3_node_read_mapped(state->args->trans,
184 state->args->dp, blk->disk_blkno,
185 &blk->bp, XFS_ATTR_FORK);
194 * Roll down the "altpath" in the state structure, storing the on-disk
195 * block number for those buffers in the "altpath".
197 path = &state->altpath;
198 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
199 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
200 if (blk->disk_blkno) {
201 error = xfs_da3_node_read_mapped(state->args->trans,
202 state->args->dp, blk->disk_blkno,
203 &blk->bp, XFS_ATTR_FORK);
214 static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
217 /*========================================================================
218 * Overall external interface routines.
219 *========================================================================*/
222 * Retrieve an extended attribute and its value. Must have ilock.
223 * Returns 0 on successful retrieval, otherwise an error.
226 xfs_attr_get_ilocked(
227 struct xfs_da_args *args)
229 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
231 if (!xfs_inode_hasattr(args->dp))
234 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
235 return xfs_attr_shortform_getvalue(args);
236 if (xfs_attr_is_leaf(args->dp))
237 return xfs_attr_leaf_get(args);
238 return xfs_attr_node_get(args);
242 * Retrieve an extended attribute by name, and its value if requested.
244 * If args->valuelen is zero, then the caller does not want the value, just an
245 * indication whether the attribute exists and the size of the value if it
246 * exists. The size is returned in args.valuelen.
248 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
249 * for the value after existence of the attribute has been determined. The
250 * caller always has to free args->value if it is set, no matter if this
251 * function was successful or not.
253 * If the attribute is found, but exceeds the size limit set by the caller in
254 * args->valuelen, return -ERANGE with the size of the attribute that was found
259 struct xfs_da_args *args)
264 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
266 if (xfs_is_shutdown(args->dp->i_mount))
269 args->geo = args->dp->i_mount->m_attr_geo;
270 args->whichfork = XFS_ATTR_FORK;
271 args->hashval = xfs_da_hashname(args->name, args->namelen);
273 /* Entirely possible to look up a name which doesn't exist */
274 args->op_flags = XFS_DA_OP_OKNOENT;
276 lock_mode = xfs_ilock_attr_map_shared(args->dp);
277 error = xfs_attr_get_ilocked(args);
278 xfs_iunlock(args->dp, lock_mode);
284 * Calculate how many blocks we need for the new attribute,
288 struct xfs_da_args *args,
291 struct xfs_mount *mp = args->dp->i_mount;
296 * Determine space new attribute will use, and if it would be
297 * "local" or "remote" (note: local != inline).
299 size = xfs_attr_leaf_newentsize(args, local);
300 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
302 if (size > (args->geo->blksize / 2)) {
303 /* Double split possible */
308 * Out of line attribute, cannot double split, but
309 * make room for the attribute value itself.
311 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
313 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
319 /* Initialize transaction reservation for attr operations */
322 struct xfs_da_args *args,
323 struct xfs_trans_res *tres,
326 struct xfs_mount *mp = args->dp->i_mount;
329 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
330 M_RES(mp)->tr_attrsetrt.tr_logres *
332 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
333 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
334 *total = args->total;
336 *tres = M_RES(mp)->tr_attrrm;
337 *total = XFS_ATTRRM_SPACE_RES(mp);
342 * Add an attr to a shortform fork. If there is no space,
343 * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
347 xfs_attr_try_sf_addname(
348 struct xfs_inode *dp,
349 struct xfs_da_args *args)
355 * Build initial attribute list (if required).
357 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
358 xfs_attr_shortform_create(args);
360 error = xfs_attr_shortform_addname(args);
361 if (error == -ENOSPC)
365 * Commit the shortform mods, and we're done.
366 * NOTE: this is also the error path (EEXIST, etc).
368 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
369 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
371 if (xfs_has_wsync(dp->i_mount))
372 xfs_trans_set_sync(args->trans);
379 struct xfs_attr_intent *attr)
381 struct xfs_da_args *args = attr->xattri_da_args;
382 struct xfs_inode *dp = args->dp;
385 error = xfs_attr_try_sf_addname(dp, args);
386 if (error != -ENOSPC) {
387 ASSERT(!error || error == -EEXIST);
388 attr->xattri_dela_state = XFS_DAS_DONE;
393 * It won't fit in the shortform, transform to a leaf block. GROT:
394 * another possible req'mt for a double-split btree op.
396 error = xfs_attr_shortform_to_leaf(args);
400 attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
402 trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
407 * Handle the state change on completion of a multi-state attr operation.
409 * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
410 * modification in a attr replace operation and we still have to do the second
411 * state, indicated by @replace_state.
413 * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
414 * completion of the second half of the attr replace operation we correctly
415 * signal that it is done.
417 static enum xfs_delattr_state
418 xfs_attr_complete_op(
419 struct xfs_attr_intent *attr,
420 enum xfs_delattr_state replace_state)
422 struct xfs_da_args *args = attr->xattri_da_args;
423 bool do_replace = args->op_flags & XFS_DA_OP_REPLACE;
425 args->op_flags &= ~XFS_DA_OP_REPLACE;
427 args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
428 return replace_state;
434 xfs_attr_leaf_addname(
435 struct xfs_attr_intent *attr)
437 struct xfs_da_args *args = attr->xattri_da_args;
440 ASSERT(xfs_attr_is_leaf(args->dp));
443 * Use the leaf buffer we may already hold locked as a result of
444 * a sf-to-leaf conversion.
446 error = xfs_attr_leaf_try_add(args);
448 if (error == -ENOSPC) {
449 error = xfs_attr3_leaf_to_node(args);
454 * We're not in leaf format anymore, so roll the transaction and
455 * retry the add to the newly allocated node block.
457 attr->xattri_dela_state = XFS_DAS_NODE_ADD;
464 * We need to commit and roll if we need to allocate remote xattr blocks
465 * or perform more xattr manipulations. Otherwise there is nothing more
466 * to do and we can return success.
469 attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
471 attr->xattri_dela_state = xfs_attr_complete_op(attr,
472 XFS_DAS_LEAF_REPLACE);
474 trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
479 * Add an entry to a node format attr tree.
481 * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
482 * the difference between leaf + remote attr blocks and a node format tree,
483 * so we may still end up having to convert from leaf to node format here.
486 xfs_attr_node_addname(
487 struct xfs_attr_intent *attr)
489 struct xfs_da_args *args = attr->xattri_da_args;
492 error = xfs_attr_node_addname_find_attr(attr);
496 error = xfs_attr_node_try_addname(attr);
497 if (error == -ENOSPC) {
498 error = xfs_attr3_leaf_to_node(args);
502 * No state change, we really are in node form now
503 * but we need the transaction rolled to continue.
511 attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
513 attr->xattri_dela_state = xfs_attr_complete_op(attr,
514 XFS_DAS_NODE_REPLACE);
516 trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
521 xfs_attr_rmtval_alloc(
522 struct xfs_attr_intent *attr)
524 struct xfs_da_args *args = attr->xattri_da_args;
528 * If there was an out-of-line value, allocate the blocks we
529 * identified for its storage and copy the value. This is done
530 * after we create the attribute so that we don't overflow the
531 * maximum size of a transaction and/or hit a deadlock.
533 if (attr->xattri_blkcnt > 0) {
534 error = xfs_attr_rmtval_set_blk(attr);
537 /* Roll the transaction only if there is more to allocate. */
538 if (attr->xattri_blkcnt > 0)
542 error = xfs_attr_rmtval_set_value(args);
546 attr->xattri_dela_state = xfs_attr_complete_op(attr,
547 ++attr->xattri_dela_state);
549 * If we are not doing a rename, we've finished the operation but still
550 * have to clear the incomplete flag protecting the new attr from
551 * exposing partially initialised state if we crash during creation.
553 if (attr->xattri_dela_state == XFS_DAS_DONE)
554 error = xfs_attr3_leaf_clearflag(args);
556 trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
561 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
562 * for later deletion of the entry.
565 xfs_attr_leaf_mark_incomplete(
566 struct xfs_da_args *args,
567 struct xfs_da_state *state)
572 * Fill in disk block numbers in the state structure
573 * so that we can get the buffers back after we commit
574 * several transactions in the following calls.
576 error = xfs_attr_fillstate(state);
581 * Mark the attribute as INCOMPLETE
583 return xfs_attr3_leaf_setflag(args);
586 /* Ensure the da state of an xattr deferred work item is ready to go. */
588 xfs_attr_item_init_da_state(
589 struct xfs_attr_intent *attr)
591 struct xfs_da_args *args = attr->xattri_da_args;
593 if (!attr->xattri_da_state)
594 attr->xattri_da_state = xfs_da_state_alloc(args);
596 xfs_da_state_reset(attr->xattri_da_state, args);
600 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
601 * the blocks are valid. Attr keys with remote blocks will be marked
605 int xfs_attr_node_removename_setup(
606 struct xfs_attr_intent *attr)
608 struct xfs_da_args *args = attr->xattri_da_args;
609 struct xfs_da_state *state;
612 xfs_attr_item_init_da_state(attr);
613 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
614 if (error != -EEXIST)
618 state = attr->xattri_da_state;
619 ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
620 ASSERT(state->path.blk[state->path.active - 1].magic ==
621 XFS_ATTR_LEAF_MAGIC);
623 error = xfs_attr_leaf_mark_incomplete(args, state);
626 if (args->rmtblkno > 0)
627 error = xfs_attr_rmtval_invalidate(args);
630 xfs_da_state_free(attr->xattri_da_state);
631 attr->xattri_da_state = NULL;
638 * Remove the original attr we have just replaced. This is dependent on the
639 * original lookup and insert placing the old attr in args->blkno/args->index
640 * and the new attr in args->blkno2/args->index2.
643 xfs_attr_leaf_remove_attr(
644 struct xfs_attr_intent *attr)
646 struct xfs_da_args *args = attr->xattri_da_args;
647 struct xfs_inode *dp = args->dp;
648 struct xfs_buf *bp = NULL;
652 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
657 xfs_attr3_leaf_remove(bp, args);
659 forkoff = xfs_attr_shortform_allfit(bp, dp);
661 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
662 /* bp is gone due to xfs_da_shrink_inode */
668 * Shrink an attribute from leaf to shortform. Used by the node format remove
669 * path when the node format collapses to a single block and so we have to check
670 * if it can be collapsed further.
673 xfs_attr_leaf_shrink(
674 struct xfs_da_args *args)
676 struct xfs_inode *dp = args->dp;
681 if (!xfs_attr_is_leaf(dp))
684 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
688 forkoff = xfs_attr_shortform_allfit(bp, dp);
690 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
691 /* bp is gone due to xfs_da_shrink_inode */
693 xfs_trans_brelse(args->trans, bp);
700 * Run the attribute operation specified in @attr.
702 * This routine is meant to function as a delayed operation and will set the
703 * state to XFS_DAS_DONE when the operation is complete. Calling functions will
704 * need to handle this, and recall the function until either an error or
705 * XFS_DAS_DONE is detected.
709 struct xfs_attr_intent *attr)
711 struct xfs_da_args *args = attr->xattri_da_args;
714 /* State machine switch */
716 switch (attr->xattri_dela_state) {
719 return -EFSCORRUPTED;
721 return xfs_attr_sf_addname(attr);
722 case XFS_DAS_LEAF_ADD:
723 return xfs_attr_leaf_addname(attr);
724 case XFS_DAS_NODE_ADD:
725 return xfs_attr_node_addname(attr);
727 case XFS_DAS_SF_REMOVE:
728 error = xfs_attr_sf_removename(args);
729 attr->xattri_dela_state = xfs_attr_complete_op(attr,
730 xfs_attr_init_add_state(args));
732 case XFS_DAS_LEAF_REMOVE:
733 error = xfs_attr_leaf_removename(args);
734 attr->xattri_dela_state = xfs_attr_complete_op(attr,
735 xfs_attr_init_add_state(args));
737 case XFS_DAS_NODE_REMOVE:
738 error = xfs_attr_node_removename_setup(attr);
739 if (error == -ENOATTR &&
740 (args->op_flags & XFS_DA_OP_RECOVERY)) {
741 attr->xattri_dela_state = xfs_attr_complete_op(attr,
742 xfs_attr_init_add_state(args));
748 attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
749 if (args->rmtblkno == 0)
750 attr->xattri_dela_state++;
753 case XFS_DAS_LEAF_SET_RMT:
754 case XFS_DAS_NODE_SET_RMT:
755 error = xfs_attr_rmtval_find_space(attr);
758 attr->xattri_dela_state++;
761 case XFS_DAS_LEAF_ALLOC_RMT:
762 case XFS_DAS_NODE_ALLOC_RMT:
763 error = xfs_attr_rmtval_alloc(attr);
766 if (attr->xattri_dela_state == XFS_DAS_DONE)
770 case XFS_DAS_LEAF_REPLACE:
771 case XFS_DAS_NODE_REPLACE:
773 * We must "flip" the incomplete flags on the "new" and "old"
774 * attribute/value pairs so that one disappears and one appears
777 error = xfs_attr3_leaf_flipflags(args);
781 * We must commit the flag value change now to make it atomic
782 * and then we can start the next trans in series at REMOVE_OLD.
784 attr->xattri_dela_state++;
787 case XFS_DAS_LEAF_REMOVE_OLD:
788 case XFS_DAS_NODE_REMOVE_OLD:
790 * If we have a remote attr, start the process of removing it
791 * by invalidating any cached buffers.
793 * If we don't have a remote attr, we skip the remote block
794 * removal state altogether with a second state increment.
796 xfs_attr_restore_rmt_blk(args);
797 if (args->rmtblkno) {
798 error = xfs_attr_rmtval_invalidate(args);
802 attr->xattri_dela_state++;
805 attr->xattri_dela_state++;
808 case XFS_DAS_LEAF_REMOVE_RMT:
809 case XFS_DAS_NODE_REMOVE_RMT:
810 error = xfs_attr_rmtval_remove(attr);
811 if (error == -EAGAIN) {
819 * We've finished removing the remote attr blocks, so commit the
820 * transaction and move on to removing the attr name from the
821 * leaf/node block. Removing the attr might require a full
822 * transaction reservation for btree block freeing, so we
823 * can't do that in the same transaction where we removed the
824 * remote attr blocks.
826 attr->xattri_dela_state++;
829 case XFS_DAS_LEAF_REMOVE_ATTR:
830 error = xfs_attr_leaf_remove_attr(attr);
831 attr->xattri_dela_state = xfs_attr_complete_op(attr,
832 xfs_attr_init_add_state(args));
835 case XFS_DAS_NODE_REMOVE_ATTR:
836 error = xfs_attr_node_remove_attr(attr);
838 error = xfs_attr_leaf_shrink(args);
839 attr->xattri_dela_state = xfs_attr_complete_op(attr,
840 xfs_attr_init_add_state(args));
847 trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
853 * Return EEXIST if attr is found, or ENOATTR if not
857 struct xfs_da_args *args)
859 struct xfs_inode *dp = args->dp;
860 struct xfs_buf *bp = NULL;
861 struct xfs_da_state *state;
864 if (!xfs_inode_hasattr(dp))
867 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
868 return xfs_attr_sf_findname(args, NULL, NULL);
870 if (xfs_attr_is_leaf(dp)) {
871 error = xfs_attr_leaf_hasname(args, &bp);
874 xfs_trans_brelse(args->trans, bp);
879 state = xfs_da_state_alloc(args);
880 error = xfs_attr_node_lookup(args, state);
881 xfs_da_state_free(state);
886 xfs_attr_intent_init(
887 struct xfs_da_args *args,
888 unsigned int op_flags, /* op flag (set or remove) */
889 struct xfs_attr_intent **attr) /* new xfs_attr_intent */
892 struct xfs_attr_intent *new;
894 new = kmem_cache_zalloc(xfs_attr_intent_cache, GFP_NOFS | __GFP_NOFAIL);
895 new->xattri_op_flags = op_flags;
896 new->xattri_da_args = args;
902 /* Sets an attribute for an inode as a deferred operation */
905 struct xfs_da_args *args)
907 struct xfs_attr_intent *new;
910 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_SET, &new);
914 new->xattri_dela_state = xfs_attr_init_add_state(args);
915 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
916 trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
921 /* Sets an attribute for an inode as a deferred operation */
923 xfs_attr_defer_replace(
924 struct xfs_da_args *args)
926 struct xfs_attr_intent *new;
929 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REPLACE, &new);
933 new->xattri_dela_state = xfs_attr_init_replace_state(args);
934 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
935 trace_xfs_attr_defer_replace(new->xattri_dela_state, args->dp);
940 /* Removes an attribute for an inode as a deferred operation */
942 xfs_attr_defer_remove(
943 struct xfs_da_args *args)
946 struct xfs_attr_intent *new;
949 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REMOVE, &new);
953 new->xattri_dela_state = xfs_attr_init_remove_state(args);
954 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
955 trace_xfs_attr_defer_remove(new->xattri_dela_state, args->dp);
961 * Note: If args->value is NULL the attribute will be removed, just like the
962 * Linux ->setattr API.
966 struct xfs_da_args *args)
968 struct xfs_inode *dp = args->dp;
969 struct xfs_mount *mp = dp->i_mount;
970 struct xfs_trans_res tres;
971 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
976 if (xfs_is_shutdown(dp->i_mount))
979 error = xfs_qm_dqattach(dp);
983 args->geo = mp->m_attr_geo;
984 args->whichfork = XFS_ATTR_FORK;
985 args->hashval = xfs_da_hashname(args->name, args->namelen);
988 * We have no control over the attribute names that userspace passes us
989 * to remove, so we have to allow the name lookup prior to attribute
990 * removal to fail as well. Preserve the logged flag, since we need
991 * to pass that through to the logging code.
993 args->op_flags = XFS_DA_OP_OKNOENT |
994 (args->op_flags & XFS_DA_OP_LOGGED);
997 XFS_STATS_INC(mp, xs_attr_set);
998 args->total = xfs_attr_calc_size(args, &local);
1001 * If the inode doesn't have an attribute fork, add one.
1002 * (inode must not be locked when we call this routine)
1004 if (XFS_IFORK_Q(dp) == 0) {
1005 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1006 xfs_attr_sf_entsize_byname(args->namelen,
1009 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
1015 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1017 XFS_STATS_INC(mp, xs_attr_remove);
1018 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
1022 * Root fork attributes can use reserved data blocks for this
1023 * operation if necessary
1025 xfs_init_attr_trans(args, &tres, &total);
1026 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
1030 if (args->value || xfs_inode_hasattr(dp)) {
1031 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
1032 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1033 if (error == -EFBIG)
1034 error = xfs_iext_count_upgrade(args->trans, dp,
1035 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1037 goto out_trans_cancel;
1040 error = xfs_attr_lookup(args);
1043 /* if no value, we are performing a remove operation */
1045 error = xfs_attr_defer_remove(args);
1048 /* Pure create fails if the attr already exists */
1049 if (args->attr_flags & XATTR_CREATE)
1050 goto out_trans_cancel;
1052 error = xfs_attr_defer_replace(args);
1055 /* Can't remove what isn't there. */
1057 goto out_trans_cancel;
1059 /* Pure replace fails if no existing attr to replace. */
1060 if (args->attr_flags & XATTR_REPLACE)
1061 goto out_trans_cancel;
1063 error = xfs_attr_defer_add(args);
1066 goto out_trans_cancel;
1069 goto out_trans_cancel;
1072 * If this is a synchronous mount, make sure that the
1073 * transaction goes to disk before returning to the user.
1075 if (xfs_has_wsync(mp))
1076 xfs_trans_set_sync(args->trans);
1078 if (!(args->op_flags & XFS_DA_OP_NOTIME))
1079 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
1082 * Commit the last in the sequence of transactions.
1084 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1085 error = xfs_trans_commit(args->trans);
1087 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1092 xfs_trans_cancel(args->trans);
1096 /*========================================================================
1097 * External routines when attribute list is inside the inode
1098 *========================================================================*/
1100 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
1102 struct xfs_attr_shortform *sf;
1104 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
1105 return be16_to_cpu(sf->hdr.totsize);
1109 * Add a name to the shortform attribute list structure
1110 * This is the external routine.
1113 xfs_attr_shortform_addname(
1114 struct xfs_da_args *args)
1116 int newsize, forkoff;
1119 trace_xfs_attr_sf_addname(args);
1121 error = xfs_attr_shortform_lookup(args);
1124 if (args->op_flags & XFS_DA_OP_REPLACE)
1128 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1131 error = xfs_attr_sf_removename(args);
1136 * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1137 * so that the new attr doesn't fit in shortform format, the
1138 * leaf format add routine won't trip over the attr not being
1141 args->op_flags &= ~XFS_DA_OP_REPLACE;
1149 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1150 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1153 newsize = xfs_attr_sf_totsize(args->dp);
1154 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1156 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1160 xfs_attr_shortform_add(args, forkoff);
1165 /*========================================================================
1166 * External routines when attribute list is one block
1167 *========================================================================*/
1169 /* Save the current remote block info and clear the current pointers. */
1171 xfs_attr_save_rmt_blk(
1172 struct xfs_da_args *args)
1174 args->blkno2 = args->blkno;
1175 args->index2 = args->index;
1176 args->rmtblkno2 = args->rmtblkno;
1177 args->rmtblkcnt2 = args->rmtblkcnt;
1178 args->rmtvaluelen2 = args->rmtvaluelen;
1180 args->rmtblkcnt = 0;
1181 args->rmtvaluelen = 0;
1184 /* Set stored info about a remote block */
1186 xfs_attr_restore_rmt_blk(
1187 struct xfs_da_args *args)
1189 args->blkno = args->blkno2;
1190 args->index = args->index2;
1191 args->rmtblkno = args->rmtblkno2;
1192 args->rmtblkcnt = args->rmtblkcnt2;
1193 args->rmtvaluelen = args->rmtvaluelen2;
1197 * Tries to add an attribute to an inode in leaf form
1199 * This function is meant to execute as part of a delayed operation and leaves
1200 * the transaction handling to the caller. On success the attribute is added
1201 * and the inode and transaction are left dirty. If there is not enough space,
1202 * the attr data is converted to node format and -ENOSPC is returned. Caller is
1203 * responsible for handling the dirty inode and transaction or adding the attr
1207 xfs_attr_leaf_try_add(
1208 struct xfs_da_args *args)
1213 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1218 * Look up the xattr name to set the insertion point for the new xattr.
1220 error = xfs_attr3_leaf_lookup_int(bp, args);
1223 if (args->op_flags & XFS_DA_OP_REPLACE)
1227 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1230 trace_xfs_attr_leaf_replace(args);
1232 * Save the existing remote attr state so that the current
1233 * values reflect the state of the new attribute we are about to
1234 * add, not the attribute we just found and will remove later.
1236 xfs_attr_save_rmt_blk(args);
1244 return xfs_attr3_leaf_add(bp, args);
1247 xfs_trans_brelse(args->trans, bp);
1252 * Return EEXIST if attr is found, or ENOATTR if not
1255 xfs_attr_leaf_hasname(
1256 struct xfs_da_args *args,
1257 struct xfs_buf **bp)
1261 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
1265 error = xfs_attr3_leaf_lookup_int(*bp, args);
1266 if (error != -ENOATTR && error != -EEXIST)
1267 xfs_trans_brelse(args->trans, *bp);
1273 * Remove a name from the leaf attribute list structure
1275 * This leaf block cannot have a "remote" value, we only call this routine
1276 * if bmap_one_block() says there is only one block (ie: no remote blks).
1279 xfs_attr_leaf_removename(
1280 struct xfs_da_args *args)
1282 struct xfs_inode *dp;
1286 trace_xfs_attr_leaf_removename(args);
1289 * Remove the attribute.
1293 error = xfs_attr_leaf_hasname(args, &bp);
1294 if (error == -ENOATTR) {
1295 xfs_trans_brelse(args->trans, bp);
1296 if (args->op_flags & XFS_DA_OP_RECOVERY)
1299 } else if (error != -EEXIST)
1302 xfs_attr3_leaf_remove(bp, args);
1305 * If the result is small enough, shrink it all into the inode.
1307 forkoff = xfs_attr_shortform_allfit(bp, dp);
1309 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1310 /* bp is gone due to xfs_da_shrink_inode */
1316 * Look up a name in a leaf attribute list structure.
1318 * This leaf block cannot have a "remote" value, we only call this routine
1319 * if bmap_one_block() says there is only one block (ie: no remote blks).
1321 * Returns 0 on successful retrieval, otherwise an error.
1324 xfs_attr_leaf_get(xfs_da_args_t *args)
1329 trace_xfs_attr_leaf_get(args);
1331 error = xfs_attr_leaf_hasname(args, &bp);
1333 if (error == -ENOATTR) {
1334 xfs_trans_brelse(args->trans, bp);
1336 } else if (error != -EEXIST)
1340 error = xfs_attr3_leaf_getvalue(bp, args);
1341 xfs_trans_brelse(args->trans, bp);
1345 /* Return EEXIST if attr is found, or ENOATTR if not. */
1347 xfs_attr_node_lookup(
1348 struct xfs_da_args *args,
1349 struct xfs_da_state *state)
1354 * Search to see if name exists, and get back a pointer to it.
1356 error = xfs_da3_node_lookup_int(state, &retval);
1363 /*========================================================================
1364 * External routines when attribute list size > geo->blksize
1365 *========================================================================*/
1368 xfs_attr_node_addname_find_attr(
1369 struct xfs_attr_intent *attr)
1371 struct xfs_da_args *args = attr->xattri_da_args;
1375 * Search to see if name already exists, and get back a pointer
1376 * to where it should go.
1378 xfs_attr_item_init_da_state(attr);
1379 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1382 if (args->op_flags & XFS_DA_OP_REPLACE)
1386 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1390 trace_xfs_attr_node_replace(args);
1392 * Save the existing remote attr state so that the current
1393 * values reflect the state of the new attribute we are about to
1394 * add, not the attribute we just found and will remove later.
1396 xfs_attr_save_rmt_blk(args);
1406 if (attr->xattri_da_state) {
1407 xfs_da_state_free(attr->xattri_da_state);
1408 attr->xattri_da_state = NULL;
1414 * Add a name to a Btree-format attribute list.
1416 * This will involve walking down the Btree, and may involve splitting
1417 * leaf nodes and even splitting intermediate nodes up to and including
1418 * the root node (a special case of an intermediate node).
1421 xfs_attr_node_try_addname(
1422 struct xfs_attr_intent *attr)
1424 struct xfs_da_state *state = attr->xattri_da_state;
1425 struct xfs_da_state_blk *blk;
1428 trace_xfs_attr_node_addname(state->args);
1430 blk = &state->path.blk[state->path.active-1];
1431 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1433 error = xfs_attr3_leaf_add(blk->bp, state->args);
1434 if (error == -ENOSPC) {
1435 if (state->path.active == 1) {
1437 * Its really a single leaf node, but it had
1438 * out-of-line values so it looked like it *might*
1439 * have been a b-tree. Let the caller deal with this.
1445 * Split as many Btree elements as required.
1446 * This code tracks the new and old attr's location
1447 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1448 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1450 error = xfs_da3_split(state);
1455 * Addition succeeded, update Btree hashvals.
1457 xfs_da3_fixhashpath(state, &state->path);
1461 xfs_da_state_free(state);
1462 attr->xattri_da_state = NULL;
1467 xfs_attr_node_removename(
1468 struct xfs_da_args *args,
1469 struct xfs_da_state *state)
1471 struct xfs_da_state_blk *blk;
1475 * Remove the name and update the hashvals in the tree.
1477 blk = &state->path.blk[state->path.active-1];
1478 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1479 retval = xfs_attr3_leaf_remove(blk->bp, args);
1480 xfs_da3_fixhashpath(state, &state->path);
1486 xfs_attr_node_remove_attr(
1487 struct xfs_attr_intent *attr)
1489 struct xfs_da_args *args = attr->xattri_da_args;
1490 struct xfs_da_state *state = xfs_da_state_alloc(args);
1495 * The attr we are removing has already been marked incomplete, so
1496 * we need to set the filter appropriately to re-find the "old"
1497 * attribute entry after any split ops.
1499 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1500 error = xfs_da3_node_lookup_int(state, &retval);
1504 error = xfs_attr_node_removename(args, state);
1507 * Check to see if the tree needs to be collapsed.
1509 if (retval && (state->path.active > 1)) {
1510 error = xfs_da3_join(state);
1517 xfs_da_state_free(state);
1524 * Retrieve the attribute data from a node attribute list.
1526 * This routine gets called for any attribute fork that has more than one
1527 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1528 * "remote" values taking up more blocks.
1530 * Returns 0 on successful retrieval, otherwise an error.
1534 struct xfs_da_args *args)
1536 struct xfs_da_state *state;
1537 struct xfs_da_state_blk *blk;
1541 trace_xfs_attr_node_get(args);
1544 * Search to see if name exists, and get back a pointer to it.
1546 state = xfs_da_state_alloc(args);
1547 error = xfs_attr_node_lookup(args, state);
1548 if (error != -EEXIST)
1552 * Get the value, local or "remote"
1554 blk = &state->path.blk[state->path.active - 1];
1555 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1558 * If not in a transaction, we have to release all the buffers.
1561 for (i = 0; state != NULL && i < state->path.active; i++) {
1562 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1563 state->path.blk[i].bp = NULL;
1566 xfs_da_state_free(state);
1570 /* Returns true if the attribute entry name is valid. */
1577 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1578 * out, so use >= for the length check.
1580 if (length >= MAXNAMELEN)
1583 /* There shouldn't be any nulls here */
1584 return !memchr(name, 0, length);
1588 xfs_attr_intent_init_cache(void)
1590 xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1591 sizeof(struct xfs_attr_intent),
1594 return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1598 xfs_attr_intent_destroy_cache(void)
1600 kmem_cache_destroy(xfs_attr_intent_cache);
1601 xfs_attr_intent_cache = NULL;