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
);
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
);
70 if (!xfs_inode_has_attr_fork(ip
))
72 if (ip
->i_af
.if_format
== XFS_DINODE_FMT_EXTENTS
&&
73 ip
->i_af
.if_nextents
== 0)
79 * Returns true if the there is exactly only block in the attr fork, in which
80 * case the attribute fork consists of a single leaf block entry.
86 struct xfs_ifork
*ifp
= &ip
->i_af
;
87 struct xfs_iext_cursor icur
;
88 struct xfs_bmbt_irec imap
;
90 ASSERT(!xfs_need_iread_extents(ifp
));
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
)
231 xfs_assert_ilocked(args
->dp
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
);
233 if (!xfs_inode_hasattr(args
->dp
))
237 * The incore attr fork iext tree must be loaded for xfs_attr_is_leaf
240 error
= xfs_iread_extents(args
->trans
, args
->dp
, XFS_ATTR_FORK
);
244 if (args
->dp
->i_af
.if_format
== XFS_DINODE_FMT_LOCAL
)
245 return xfs_attr_shortform_getvalue(args
);
246 if (xfs_attr_is_leaf(args
->dp
))
247 return xfs_attr_leaf_get(args
);
248 return xfs_attr_node_get(args
);
252 * Retrieve an extended attribute by name, and its value if requested.
254 * If args->valuelen is zero, then the caller does not want the value, just an
255 * indication whether the attribute exists and the size of the value if it
256 * exists. The size is returned in args.valuelen.
258 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
259 * for the value after existence of the attribute has been determined. The
260 * caller always has to free args->value if it is set, no matter if this
261 * function was successful or not.
263 * If the attribute is found, but exceeds the size limit set by the caller in
264 * args->valuelen, return -ERANGE with the size of the attribute that was found
269 struct xfs_da_args
*args
)
274 XFS_STATS_INC(args
->dp
->i_mount
, xs_attr_get
);
276 if (xfs_is_shutdown(args
->dp
->i_mount
))
280 args
->owner
= args
->dp
->i_ino
;
281 args
->geo
= args
->dp
->i_mount
->m_attr_geo
;
282 args
->whichfork
= XFS_ATTR_FORK
;
283 xfs_attr_sethash(args
);
285 /* Entirely possible to look up a name which doesn't exist */
286 args
->op_flags
= XFS_DA_OP_OKNOENT
;
288 lock_mode
= xfs_ilock_attr_map_shared(args
->dp
);
289 error
= xfs_attr_get_ilocked(args
);
290 xfs_iunlock(args
->dp
, lock_mode
);
296 * Calculate how many blocks we need for the new attribute,
300 struct xfs_da_args
*args
,
303 struct xfs_mount
*mp
= args
->dp
->i_mount
;
308 * Determine space new attribute will use, and if it would be
309 * "local" or "remote" (note: local != inline).
311 size
= xfs_attr_leaf_newentsize(args
, local
);
312 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
314 if (size
> (args
->geo
->blksize
/ 2)) {
315 /* Double split possible */
320 * Out of line attribute, cannot double split, but
321 * make room for the attribute value itself.
323 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
325 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
331 /* Initialize transaction reservation for an xattr set/replace/upsert */
332 inline struct xfs_trans_res
334 const struct xfs_da_args
*args
)
336 struct xfs_mount
*mp
= args
->dp
->i_mount
;
337 struct xfs_trans_res ret
= {
338 .tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
339 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
->total
,
340 .tr_logcount
= XFS_ATTRSET_LOG_COUNT
,
341 .tr_logflags
= XFS_TRANS_PERM_LOG_RES
,
348 * Add an attr to a shortform fork. If there is no space,
349 * xfs_attr_shortform_addname() will convert to leaf format and return -ENOSPC.
353 xfs_attr_try_sf_addname(
354 struct xfs_inode
*dp
,
355 struct xfs_da_args
*args
)
361 * Build initial attribute list (if required).
363 if (dp
->i_af
.if_format
== XFS_DINODE_FMT_EXTENTS
)
364 xfs_attr_shortform_create(args
);
366 error
= xfs_attr_shortform_addname(args
);
367 if (error
== -ENOSPC
)
371 * Commit the shortform mods, and we're done.
372 * NOTE: this is also the error path (EEXIST, etc).
375 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
377 if (xfs_has_wsync(dp
->i_mount
))
378 xfs_trans_set_sync(args
->trans
);
385 struct xfs_attr_intent
*attr
)
387 struct xfs_da_args
*args
= attr
->xattri_da_args
;
388 struct xfs_inode
*dp
= args
->dp
;
391 error
= xfs_attr_try_sf_addname(dp
, args
);
392 if (error
!= -ENOSPC
) {
393 ASSERT(!error
|| error
== -EEXIST
);
394 attr
->xattri_dela_state
= XFS_DAS_DONE
;
399 * It won't fit in the shortform, transform to a leaf block. GROT:
400 * another possible req'mt for a double-split btree op.
402 error
= xfs_attr_shortform_to_leaf(args
);
406 attr
->xattri_dela_state
= XFS_DAS_LEAF_ADD
;
408 trace_xfs_attr_sf_addname_return(attr
->xattri_dela_state
, args
->dp
);
412 /* Compute the hash value for a user/root/secure extended attribute */
418 return xfs_da_hashname(name
, namelen
);
421 /* Compute the hash value for any extended attribute from any namespace. */
424 struct xfs_mount
*mp
,
425 unsigned int attr_flags
,
431 ASSERT(xfs_attr_check_namespace(attr_flags
));
433 if (attr_flags
& XFS_ATTR_PARENT
)
434 return xfs_parent_hashattr(mp
, name
, namelen
, value
, valuelen
);
436 return xfs_attr_hashname(name
, namelen
);
439 /* Save the current remote block info and clear the current pointers. */
441 xfs_attr_save_rmt_blk(
442 struct xfs_da_args
*args
)
444 args
->blkno2
= args
->blkno
;
445 args
->index2
= args
->index
;
446 args
->rmtblkno2
= args
->rmtblkno
;
447 args
->rmtblkcnt2
= args
->rmtblkcnt
;
448 args
->rmtvaluelen2
= args
->rmtvaluelen
;
451 args
->rmtvaluelen
= 0;
454 /* Set stored info about a remote block */
456 xfs_attr_restore_rmt_blk(
457 struct xfs_da_args
*args
)
459 args
->blkno
= args
->blkno2
;
460 args
->index
= args
->index2
;
461 args
->rmtblkno
= args
->rmtblkno2
;
462 args
->rmtblkcnt
= args
->rmtblkcnt2
;
463 args
->rmtvaluelen
= args
->rmtvaluelen2
;
467 * PPTR_REPLACE operations require the caller to set the old and new names and
468 * values explicitly. Update the canonical fields to the new name and value
469 * here now that the removal phase has finished.
472 xfs_attr_update_pptr_replace_args(
473 struct xfs_da_args
*args
)
475 ASSERT(args
->new_namelen
> 0);
476 args
->name
= args
->new_name
;
477 args
->namelen
= args
->new_namelen
;
478 args
->value
= args
->new_value
;
479 args
->valuelen
= args
->new_valuelen
;
480 xfs_attr_sethash(args
);
484 * Handle the state change on completion of a multi-state attr operation.
486 * If the XFS_DA_OP_REPLACE flag is set, this means the operation was the first
487 * modification in a attr replace operation and we still have to do the second
488 * state, indicated by @replace_state.
490 * We consume the XFS_DA_OP_REPLACE flag so that when we are called again on
491 * completion of the second half of the attr replace operation we correctly
492 * signal that it is done.
494 static enum xfs_delattr_state
495 xfs_attr_complete_op(
496 struct xfs_attr_intent
*attr
,
497 enum xfs_delattr_state replace_state
)
499 struct xfs_da_args
*args
= attr
->xattri_da_args
;
501 if (!(args
->op_flags
& XFS_DA_OP_REPLACE
))
502 replace_state
= XFS_DAS_DONE
;
503 else if (xfs_attr_intent_op(attr
) == XFS_ATTRI_OP_FLAGS_PPTR_REPLACE
)
504 xfs_attr_update_pptr_replace_args(args
);
506 args
->op_flags
&= ~XFS_DA_OP_REPLACE
;
507 args
->attr_filter
&= ~XFS_ATTR_INCOMPLETE
;
508 return replace_state
;
512 * Try to add an attribute to an inode in leaf form.
515 xfs_attr_leaf_addname(
516 struct xfs_attr_intent
*attr
)
518 struct xfs_da_args
*args
= attr
->xattri_da_args
;
522 ASSERT(xfs_attr_is_leaf(args
->dp
));
524 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->owner
, 0, &bp
);
529 * Look up the xattr name to set the insertion point for the new xattr.
531 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
534 if (args
->op_flags
& XFS_DA_OP_REPLACE
)
538 if (!(args
->op_flags
& XFS_DA_OP_REPLACE
))
541 trace_xfs_attr_leaf_replace(args
);
543 * Save the existing remote attr state so that the current
544 * values reflect the state of the new attribute we are about to
545 * add, not the attribute we just found and will remove later.
547 xfs_attr_save_rmt_blk(args
);
556 * We need to commit and roll if we need to allocate remote xattr blocks
557 * or perform more xattr manipulations. Otherwise there is nothing more
558 * to do and we can return success.
560 if (!xfs_attr3_leaf_add(bp
, args
)) {
561 error
= xfs_attr3_leaf_to_node(args
);
565 attr
->xattri_dela_state
= XFS_DAS_NODE_ADD
;
566 } else if (args
->rmtblkno
) {
567 attr
->xattri_dela_state
= XFS_DAS_LEAF_SET_RMT
;
569 attr
->xattri_dela_state
=
570 xfs_attr_complete_op(attr
, XFS_DAS_LEAF_REPLACE
);
573 trace_xfs_attr_leaf_addname_return(attr
->xattri_dela_state
, args
->dp
);
577 xfs_trans_brelse(args
->trans
, bp
);
582 * Add an entry to a node format attr tree.
584 * Note that we might still have a leaf here - xfs_attr_is_leaf() cannot tell
585 * the difference between leaf + remote attr blocks and a node format tree,
586 * so we may still end up having to convert from leaf to node format here.
589 xfs_attr_node_addname(
590 struct xfs_attr_intent
*attr
)
592 struct xfs_da_args
*args
= attr
->xattri_da_args
;
595 error
= xfs_attr_node_addname_find_attr(attr
);
599 error
= xfs_attr_node_try_addname(attr
);
601 error
= xfs_attr3_leaf_to_node(args
);
605 * No state change, we really are in node form now
606 * but we need the transaction rolled to continue.
614 attr
->xattri_dela_state
= XFS_DAS_NODE_SET_RMT
;
616 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
617 XFS_DAS_NODE_REPLACE
);
619 trace_xfs_attr_node_addname_return(attr
->xattri_dela_state
, args
->dp
);
624 xfs_attr_rmtval_alloc(
625 struct xfs_attr_intent
*attr
)
627 struct xfs_da_args
*args
= attr
->xattri_da_args
;
631 * If there was an out-of-line value, allocate the blocks we
632 * identified for its storage and copy the value. This is done
633 * after we create the attribute so that we don't overflow the
634 * maximum size of a transaction and/or hit a deadlock.
636 if (attr
->xattri_blkcnt
> 0) {
637 error
= xfs_attr_rmtval_set_blk(attr
);
640 /* Roll the transaction only if there is more to allocate. */
641 if (attr
->xattri_blkcnt
> 0)
645 error
= xfs_attr_rmtval_set_value(args
);
649 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
650 ++attr
->xattri_dela_state
);
652 * If we are not doing a rename, we've finished the operation but still
653 * have to clear the incomplete flag protecting the new attr from
654 * exposing partially initialised state if we crash during creation.
656 if (attr
->xattri_dela_state
== XFS_DAS_DONE
)
657 error
= xfs_attr3_leaf_clearflag(args
);
659 trace_xfs_attr_rmtval_alloc(attr
->xattri_dela_state
, args
->dp
);
664 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
665 * for later deletion of the entry.
668 xfs_attr_leaf_mark_incomplete(
669 struct xfs_da_args
*args
,
670 struct xfs_da_state
*state
)
675 * Fill in disk block numbers in the state structure
676 * so that we can get the buffers back after we commit
677 * several transactions in the following calls.
679 error
= xfs_attr_fillstate(state
);
684 * Mark the attribute as INCOMPLETE
686 return xfs_attr3_leaf_setflag(args
);
689 /* Ensure the da state of an xattr deferred work item is ready to go. */
691 xfs_attr_item_init_da_state(
692 struct xfs_attr_intent
*attr
)
694 struct xfs_da_args
*args
= attr
->xattri_da_args
;
696 if (!attr
->xattri_da_state
)
697 attr
->xattri_da_state
= xfs_da_state_alloc(args
);
699 xfs_da_state_reset(attr
->xattri_da_state
, args
);
703 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
704 * the blocks are valid. Attr keys with remote blocks will be marked
708 int xfs_attr_node_removename_setup(
709 struct xfs_attr_intent
*attr
)
711 struct xfs_da_args
*args
= attr
->xattri_da_args
;
712 struct xfs_da_state
*state
;
715 xfs_attr_item_init_da_state(attr
);
716 error
= xfs_attr_node_lookup(args
, attr
->xattri_da_state
);
717 if (error
!= -EEXIST
)
721 state
= attr
->xattri_da_state
;
722 ASSERT(state
->path
.blk
[state
->path
.active
- 1].bp
!= NULL
);
723 ASSERT(state
->path
.blk
[state
->path
.active
- 1].magic
==
724 XFS_ATTR_LEAF_MAGIC
);
726 error
= xfs_attr_leaf_mark_incomplete(args
, state
);
729 if (args
->rmtblkno
> 0)
730 error
= xfs_attr_rmtval_invalidate(args
);
733 xfs_da_state_free(attr
->xattri_da_state
);
734 attr
->xattri_da_state
= NULL
;
741 * Remove the original attr we have just replaced. This is dependent on the
742 * original lookup and insert placing the old attr in args->blkno/args->index
743 * and the new attr in args->blkno2/args->index2.
746 xfs_attr_leaf_remove_attr(
747 struct xfs_attr_intent
*attr
)
749 struct xfs_da_args
*args
= attr
->xattri_da_args
;
750 struct xfs_inode
*dp
= args
->dp
;
751 struct xfs_buf
*bp
= NULL
;
755 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->owner
,
760 xfs_attr3_leaf_remove(bp
, args
);
762 forkoff
= xfs_attr_shortform_allfit(bp
, dp
);
764 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
765 /* bp is gone due to xfs_da_shrink_inode */
771 * Shrink an attribute from leaf to shortform. Used by the node format remove
772 * path when the node format collapses to a single block and so we have to check
773 * if it can be collapsed further.
776 xfs_attr_leaf_shrink(
777 struct xfs_da_args
*args
)
779 struct xfs_inode
*dp
= args
->dp
;
784 if (!xfs_attr_is_leaf(dp
))
787 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->owner
, 0, &bp
);
791 forkoff
= xfs_attr_shortform_allfit(bp
, dp
);
793 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
794 /* bp is gone due to xfs_da_shrink_inode */
796 xfs_trans_brelse(args
->trans
, bp
);
803 * Run the attribute operation specified in @attr.
805 * This routine is meant to function as a delayed operation and will set the
806 * state to XFS_DAS_DONE when the operation is complete. Calling functions will
807 * need to handle this, and recall the function until either an error or
808 * XFS_DAS_DONE is detected.
812 struct xfs_attr_intent
*attr
)
814 struct xfs_da_args
*args
= attr
->xattri_da_args
;
817 /* State machine switch */
819 switch (attr
->xattri_dela_state
) {
822 return -EFSCORRUPTED
;
824 return xfs_attr_sf_addname(attr
);
825 case XFS_DAS_LEAF_ADD
:
826 return xfs_attr_leaf_addname(attr
);
827 case XFS_DAS_NODE_ADD
:
828 return xfs_attr_node_addname(attr
);
830 case XFS_DAS_SF_REMOVE
:
831 error
= xfs_attr_sf_removename(args
);
832 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
833 xfs_attr_init_add_state(args
));
835 case XFS_DAS_LEAF_REMOVE
:
836 error
= xfs_attr_leaf_removename(args
);
837 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
838 xfs_attr_init_add_state(args
));
840 case XFS_DAS_NODE_REMOVE
:
841 error
= xfs_attr_node_removename_setup(attr
);
842 if (error
== -ENOATTR
&&
843 (args
->op_flags
& XFS_DA_OP_RECOVERY
)) {
844 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
845 xfs_attr_init_add_state(args
));
851 attr
->xattri_dela_state
= XFS_DAS_NODE_REMOVE_RMT
;
852 if (args
->rmtblkno
== 0)
853 attr
->xattri_dela_state
++;
856 case XFS_DAS_LEAF_SET_RMT
:
857 case XFS_DAS_NODE_SET_RMT
:
858 error
= xfs_attr_rmtval_find_space(attr
);
861 attr
->xattri_dela_state
++;
864 case XFS_DAS_LEAF_ALLOC_RMT
:
865 case XFS_DAS_NODE_ALLOC_RMT
:
866 error
= xfs_attr_rmtval_alloc(attr
);
869 if (attr
->xattri_dela_state
== XFS_DAS_DONE
)
873 case XFS_DAS_LEAF_REPLACE
:
874 case XFS_DAS_NODE_REPLACE
:
876 * We must "flip" the incomplete flags on the "new" and "old"
877 * attribute/value pairs so that one disappears and one appears
880 error
= xfs_attr3_leaf_flipflags(args
);
884 * We must commit the flag value change now to make it atomic
885 * and then we can start the next trans in series at REMOVE_OLD.
887 attr
->xattri_dela_state
++;
890 case XFS_DAS_LEAF_REMOVE_OLD
:
891 case XFS_DAS_NODE_REMOVE_OLD
:
893 * If we have a remote attr, start the process of removing it
894 * by invalidating any cached buffers.
896 * If we don't have a remote attr, we skip the remote block
897 * removal state altogether with a second state increment.
899 xfs_attr_restore_rmt_blk(args
);
900 if (args
->rmtblkno
) {
901 error
= xfs_attr_rmtval_invalidate(args
);
905 attr
->xattri_dela_state
++;
908 attr
->xattri_dela_state
++;
911 case XFS_DAS_LEAF_REMOVE_RMT
:
912 case XFS_DAS_NODE_REMOVE_RMT
:
913 error
= xfs_attr_rmtval_remove(attr
);
914 if (error
== -EAGAIN
) {
922 * We've finished removing the remote attr blocks, so commit the
923 * transaction and move on to removing the attr name from the
924 * leaf/node block. Removing the attr might require a full
925 * transaction reservation for btree block freeing, so we
926 * can't do that in the same transaction where we removed the
927 * remote attr blocks.
929 attr
->xattri_dela_state
++;
932 case XFS_DAS_LEAF_REMOVE_ATTR
:
933 error
= xfs_attr_leaf_remove_attr(attr
);
934 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
935 xfs_attr_init_add_state(args
));
938 case XFS_DAS_NODE_REMOVE_ATTR
:
939 error
= xfs_attr_node_remove_attr(attr
);
941 error
= xfs_attr_leaf_shrink(args
);
942 attr
->xattri_dela_state
= xfs_attr_complete_op(attr
,
943 xfs_attr_init_add_state(args
));
950 trace_xfs_attr_set_iter_return(attr
->xattri_dela_state
, args
->dp
);
956 * Return EEXIST if attr is found, or ENOATTR if not
960 struct xfs_da_args
*args
)
962 struct xfs_inode
*dp
= args
->dp
;
963 struct xfs_buf
*bp
= NULL
;
964 struct xfs_da_state
*state
;
967 if (!xfs_inode_hasattr(dp
))
970 if (dp
->i_af
.if_format
== XFS_DINODE_FMT_LOCAL
) {
971 if (xfs_attr_sf_findname(args
))
976 /* Prerequisite for xfs_attr_is_leaf */
977 error
= xfs_iread_extents(args
->trans
, args
->dp
, XFS_ATTR_FORK
);
981 if (xfs_attr_is_leaf(dp
)) {
982 error
= xfs_attr_leaf_hasname(args
, &bp
);
985 xfs_trans_brelse(args
->trans
, bp
);
990 state
= xfs_da_state_alloc(args
);
991 error
= xfs_attr_node_lookup(args
, state
);
992 xfs_da_state_free(state
);
998 struct xfs_inode
*ip
, /* incore inode pointer */
999 int size
, /* space new attribute needs */
1000 int rsvd
) /* xact may use reserved blks */
1002 struct xfs_mount
*mp
= ip
->i_mount
;
1003 struct xfs_trans
*tp
; /* transaction pointer */
1004 unsigned int blks
; /* space reservation */
1005 int error
; /* error return value */
1007 if (xfs_is_metadir_inode(ip
))
1008 ASSERT(XFS_IS_DQDETACHED(ip
));
1010 ASSERT(!XFS_NOT_DQATTACHED(mp
, ip
));
1012 blks
= XFS_ADDAFORK_SPACE_RES(mp
);
1014 error
= xfs_trans_alloc_inode(ip
, &M_RES(mp
)->tr_addafork
, blks
, 0,
1019 if (xfs_inode_has_attr_fork(ip
))
1022 error
= xfs_bmap_add_attrfork(tp
, ip
, size
, rsvd
);
1026 error
= xfs_trans_commit(tp
);
1027 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1031 xfs_trans_cancel(tp
);
1032 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1037 * Make a change to the xattr structure.
1039 * The caller must have initialized @args, attached dquots, and must not hold
1040 * any ILOCKs. Reserved data blocks may be used if @rsvd is set.
1042 * Returns -EEXIST for XFS_ATTRUPDATE_CREATE if the name already exists.
1043 * Returns -ENOATTR for XFS_ATTRUPDATE_REMOVE if the name does not exist.
1044 * Returns 0 on success, or a negative errno if something else went wrong.
1048 struct xfs_da_args
*args
,
1049 enum xfs_attr_update op
,
1052 struct xfs_inode
*dp
= args
->dp
;
1053 struct xfs_mount
*mp
= dp
->i_mount
;
1054 struct xfs_trans_res tres
;
1057 unsigned int total
= 0;
1059 ASSERT(!args
->trans
);
1062 case XFS_ATTRUPDATE_UPSERT
:
1063 case XFS_ATTRUPDATE_CREATE
:
1064 case XFS_ATTRUPDATE_REPLACE
:
1065 XFS_STATS_INC(mp
, xs_attr_set
);
1066 args
->total
= xfs_attr_calc_size(args
, &local
);
1069 * If the inode doesn't have an attribute fork, add one.
1070 * (inode must not be locked when we call this routine)
1072 if (xfs_inode_has_attr_fork(dp
) == 0) {
1073 int sf_size
= sizeof(struct xfs_attr_sf_hdr
) +
1074 xfs_attr_sf_entsize_byname(args
->namelen
,
1077 error
= xfs_attr_add_fork(dp
, sf_size
, rsvd
);
1083 rmt_blks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1085 tres
= xfs_attr_set_resv(args
);
1086 total
= args
->total
;
1088 case XFS_ATTRUPDATE_REMOVE
:
1089 XFS_STATS_INC(mp
, xs_attr_remove
);
1090 rmt_blks
= xfs_attr3_max_rmt_blocks(mp
);
1091 tres
= M_RES(mp
)->tr_attrrm
;
1092 total
= XFS_ATTRRM_SPACE_RES(mp
);
1097 * Root fork attributes can use reserved data blocks for this
1098 * operation if necessary
1100 error
= xfs_trans_alloc_inode(dp
, &tres
, total
, 0, rsvd
, &args
->trans
);
1104 if (op
!= XFS_ATTRUPDATE_REMOVE
|| xfs_inode_hasattr(dp
)) {
1105 error
= xfs_iext_count_extend(args
->trans
, dp
, XFS_ATTR_FORK
,
1106 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks
));
1108 goto out_trans_cancel
;
1111 error
= xfs_attr_lookup(args
);
1114 if (op
== XFS_ATTRUPDATE_REMOVE
) {
1115 /* if no value, we are performing a remove operation */
1116 xfs_attr_defer_add(args
, XFS_ATTR_DEFER_REMOVE
);
1120 /* Pure create fails if the attr already exists */
1121 if (op
== XFS_ATTRUPDATE_CREATE
)
1122 goto out_trans_cancel
;
1123 xfs_attr_defer_add(args
, XFS_ATTR_DEFER_REPLACE
);
1126 /* Can't remove what isn't there. */
1127 if (op
== XFS_ATTRUPDATE_REMOVE
)
1128 goto out_trans_cancel
;
1130 /* Pure replace fails if no existing attr to replace. */
1131 if (op
== XFS_ATTRUPDATE_REPLACE
)
1132 goto out_trans_cancel
;
1133 xfs_attr_defer_add(args
, XFS_ATTR_DEFER_SET
);
1136 goto out_trans_cancel
;
1140 * If this is a synchronous mount, make sure that the
1141 * transaction goes to disk before returning to the user.
1143 if (xfs_has_wsync(mp
))
1144 xfs_trans_set_sync(args
->trans
);
1146 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
1149 * Commit the last in the sequence of transactions.
1151 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
1152 error
= xfs_trans_commit(args
->trans
);
1154 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1160 xfs_trans_cancel(args
->trans
);
1164 /*========================================================================
1165 * External routines when attribute list is inside the inode
1166 *========================================================================*/
1168 int xfs_attr_sf_totsize(struct xfs_inode
*dp
)
1170 struct xfs_attr_sf_hdr
*sf
= dp
->i_af
.if_data
;
1172 return be16_to_cpu(sf
->totsize
);
1176 * Add a name to the shortform attribute list structure
1177 * This is the external routine.
1180 xfs_attr_shortform_addname(
1181 struct xfs_da_args
*args
)
1183 int newsize
, forkoff
;
1185 trace_xfs_attr_sf_addname(args
);
1187 if (xfs_attr_sf_findname(args
)) {
1190 ASSERT(args
->op_flags
& XFS_DA_OP_REPLACE
);
1192 error
= xfs_attr_sf_removename(args
);
1197 * Since we have removed the old attr, clear XFS_DA_OP_REPLACE
1198 * so that the new attr doesn't fit in shortform format, the
1199 * leaf format add routine won't trip over the attr not being
1202 args
->op_flags
&= ~XFS_DA_OP_REPLACE
;
1204 ASSERT(!(args
->op_flags
& XFS_DA_OP_REPLACE
));
1207 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
1208 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
1211 newsize
= xfs_attr_sf_totsize(args
->dp
);
1212 newsize
+= xfs_attr_sf_entsize_byname(args
->namelen
, args
->valuelen
);
1214 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
1218 xfs_attr_shortform_add(args
, forkoff
);
1223 /*========================================================================
1224 * External routines when attribute list is one block
1225 *========================================================================*/
1228 * Return EEXIST if attr is found, or ENOATTR if not
1231 xfs_attr_leaf_hasname(
1232 struct xfs_da_args
*args
,
1233 struct xfs_buf
**bp
)
1237 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->owner
, 0, bp
);
1241 error
= xfs_attr3_leaf_lookup_int(*bp
, args
);
1242 if (error
!= -ENOATTR
&& error
!= -EEXIST
)
1243 xfs_trans_brelse(args
->trans
, *bp
);
1249 * Remove a name from the leaf attribute list structure
1251 * This leaf block cannot have a "remote" value, we only call this routine
1252 * if bmap_one_block() says there is only one block (ie: no remote blks).
1255 xfs_attr_leaf_removename(
1256 struct xfs_da_args
*args
)
1258 struct xfs_inode
*dp
;
1262 trace_xfs_attr_leaf_removename(args
);
1265 * Remove the attribute.
1269 error
= xfs_attr_leaf_hasname(args
, &bp
);
1270 if (error
== -ENOATTR
) {
1271 xfs_trans_brelse(args
->trans
, bp
);
1272 if (args
->op_flags
& XFS_DA_OP_RECOVERY
)
1275 } else if (error
!= -EEXIST
)
1278 xfs_attr3_leaf_remove(bp
, args
);
1281 * If the result is small enough, shrink it all into the inode.
1283 forkoff
= xfs_attr_shortform_allfit(bp
, dp
);
1285 return xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1286 /* bp is gone due to xfs_da_shrink_inode */
1292 * Look up a name in a leaf attribute list structure.
1294 * This leaf block cannot have a "remote" value, we only call this routine
1295 * if bmap_one_block() says there is only one block (ie: no remote blks).
1297 * Returns 0 on successful retrieval, otherwise an error.
1300 xfs_attr_leaf_get(xfs_da_args_t
*args
)
1305 trace_xfs_attr_leaf_get(args
);
1307 error
= xfs_attr_leaf_hasname(args
, &bp
);
1309 if (error
== -ENOATTR
) {
1310 xfs_trans_brelse(args
->trans
, bp
);
1312 } else if (error
!= -EEXIST
)
1316 error
= xfs_attr3_leaf_getvalue(bp
, args
);
1317 xfs_trans_brelse(args
->trans
, bp
);
1321 /* Return EEXIST if attr is found, or ENOATTR if not. */
1323 xfs_attr_node_lookup(
1324 struct xfs_da_args
*args
,
1325 struct xfs_da_state
*state
)
1330 * Search to see if name exists, and get back a pointer to it.
1332 error
= xfs_da3_node_lookup_int(state
, &retval
);
1339 /*========================================================================
1340 * External routines when attribute list size > geo->blksize
1341 *========================================================================*/
1344 xfs_attr_node_addname_find_attr(
1345 struct xfs_attr_intent
*attr
)
1347 struct xfs_da_args
*args
= attr
->xattri_da_args
;
1351 * Search to see if name already exists, and get back a pointer
1352 * to where it should go.
1354 xfs_attr_item_init_da_state(attr
);
1355 error
= xfs_attr_node_lookup(args
, attr
->xattri_da_state
);
1358 if (args
->op_flags
& XFS_DA_OP_REPLACE
)
1362 if (!(args
->op_flags
& XFS_DA_OP_REPLACE
))
1366 trace_xfs_attr_node_replace(args
);
1368 * Save the existing remote attr state so that the current
1369 * values reflect the state of the new attribute we are about to
1370 * add, not the attribute we just found and will remove later.
1372 xfs_attr_save_rmt_blk(args
);
1382 if (attr
->xattri_da_state
) {
1383 xfs_da_state_free(attr
->xattri_da_state
);
1384 attr
->xattri_da_state
= NULL
;
1390 * Add a name to a Btree-format attribute list.
1392 * This will involve walking down the Btree, and may involve splitting leaf
1393 * nodes and even splitting intermediate nodes up to and including the root
1394 * node (a special case of an intermediate node).
1396 * If the tree was still in single leaf format and needs to converted to
1397 * real node format return 1 and let the caller handle that.
1400 xfs_attr_node_try_addname(
1401 struct xfs_attr_intent
*attr
)
1403 struct xfs_da_state
*state
= attr
->xattri_da_state
;
1404 struct xfs_da_state_blk
*blk
;
1407 trace_xfs_attr_node_addname(state
->args
);
1409 blk
= &state
->path
.blk
[state
->path
.active
-1];
1410 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1412 if (!xfs_attr3_leaf_add(blk
->bp
, state
->args
)) {
1413 if (state
->path
.active
== 1) {
1415 * Its really a single leaf node, but it had
1416 * out-of-line values so it looked like it *might*
1417 * have been a b-tree. Let the caller deal with this.
1424 * Split as many Btree elements as required.
1425 * This code tracks the new and old attr's location
1426 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1427 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1429 error
= xfs_da3_split(state
);
1434 * Addition succeeded, update Btree hashvals.
1436 xfs_da3_fixhashpath(state
, &state
->path
);
1440 xfs_da_state_free(state
);
1441 attr
->xattri_da_state
= NULL
;
1446 xfs_attr_node_removename(
1447 struct xfs_da_args
*args
,
1448 struct xfs_da_state
*state
)
1450 struct xfs_da_state_blk
*blk
;
1454 * Remove the name and update the hashvals in the tree.
1456 blk
= &state
->path
.blk
[state
->path
.active
-1];
1457 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1458 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1459 xfs_da3_fixhashpath(state
, &state
->path
);
1465 xfs_attr_node_remove_attr(
1466 struct xfs_attr_intent
*attr
)
1468 struct xfs_da_args
*args
= attr
->xattri_da_args
;
1469 struct xfs_da_state
*state
= xfs_da_state_alloc(args
);
1474 * The attr we are removing has already been marked incomplete, so
1475 * we need to set the filter appropriately to re-find the "old"
1476 * attribute entry after any split ops.
1478 args
->attr_filter
|= XFS_ATTR_INCOMPLETE
;
1479 error
= xfs_da3_node_lookup_int(state
, &retval
);
1483 error
= xfs_attr_node_removename(args
, state
);
1486 * Check to see if the tree needs to be collapsed.
1488 if (retval
&& (state
->path
.active
> 1)) {
1489 error
= xfs_da3_join(state
);
1496 xfs_da_state_free(state
);
1503 * Retrieve the attribute data from a node attribute list.
1505 * This routine gets called for any attribute fork that has more than one
1506 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1507 * "remote" values taking up more blocks.
1509 * Returns 0 on successful retrieval, otherwise an error.
1513 struct xfs_da_args
*args
)
1515 struct xfs_da_state
*state
;
1516 struct xfs_da_state_blk
*blk
;
1520 trace_xfs_attr_node_get(args
);
1523 * Search to see if name exists, and get back a pointer to it.
1525 state
= xfs_da_state_alloc(args
);
1526 error
= xfs_attr_node_lookup(args
, state
);
1527 if (error
!= -EEXIST
)
1531 * Get the value, local or "remote"
1533 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1534 error
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1537 * If not in a transaction, we have to release all the buffers.
1540 for (i
= 0; i
< state
->path
.active
; i
++) {
1541 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1542 state
->path
.blk
[i
].bp
= NULL
;
1545 xfs_da_state_free(state
);
1549 /* Enforce that there is at most one namespace bit per attr. */
1550 inline bool xfs_attr_check_namespace(unsigned int attr_flags
)
1552 return hweight32(attr_flags
& XFS_ATTR_NSP_ONDISK_MASK
) < 2;
1555 /* Returns true if the attribute entry name is valid. */
1558 unsigned int attr_flags
,
1562 /* Only one namespace bit allowed. */
1563 if (!xfs_attr_check_namespace(attr_flags
))
1567 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1568 * out, so use >= for the length check.
1570 if (length
>= MAXNAMELEN
)
1573 /* Parent pointers have their own validation. */
1574 if (attr_flags
& XFS_ATTR_PARENT
)
1575 return xfs_parent_namecheck(attr_flags
, name
, length
);
1577 /* There shouldn't be any nulls here */
1578 return !memchr(name
, 0, length
);
1582 xfs_attr_intent_init_cache(void)
1584 xfs_attr_intent_cache
= kmem_cache_create("xfs_attr_intent",
1585 sizeof(struct xfs_attr_intent
),
1588 return xfs_attr_intent_cache
!= NULL
? 0 : -ENOMEM
;
1592 xfs_attr_intent_destroy_cache(void)
1594 kmem_cache_destroy(xfs_attr_intent_cache
);
1595 xfs_attr_intent_cache
= NULL
;