]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_attr_sf.h"
18 #include "xfs_inode.h"
19 #include "xfs_alloc.h"
20 #include "xfs_trans.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_attr_leaf.h"
24 #include "xfs_attr_remote.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
31 * Provide the external interfaces to manage attribute lists.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Internal routines when attribute list fits inside the inode.
41 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
44 * Internal routines when attribute list is one block.
46 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
47 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
48 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
51 * Internal routines when attribute list is more than one block.
53 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
54 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
55 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
56 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
57 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
62 struct xfs_da_args
*args
,
64 const unsigned char *name
,
71 memset(args
, 0, sizeof(*args
));
72 args
->geo
= dp
->i_mount
->m_attr_geo
;
73 args
->whichfork
= XFS_ATTR_FORK
;
77 args
->namelen
= strlen((const char *)name
);
78 if (args
->namelen
>= MAXNAMELEN
)
79 return -EFAULT
; /* match IRIX behaviour */
81 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
89 if (!XFS_IFORK_Q(ip
) ||
90 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
91 ip
->i_d
.di_anextents
== 0))
96 /*========================================================================
97 * Overall external interface routines.
98 *========================================================================*/
100 /* Retrieve an extended attribute and its value. Must have ilock. */
102 xfs_attr_get_ilocked(
103 struct xfs_inode
*ip
,
104 struct xfs_da_args
*args
)
106 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
));
108 if (!xfs_inode_hasattr(ip
))
110 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
111 return xfs_attr_shortform_getvalue(args
);
112 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
113 return xfs_attr_leaf_get(args
);
115 return xfs_attr_node_get(args
);
118 /* Retrieve an extended attribute by name, and its value. */
121 struct xfs_inode
*ip
,
122 const unsigned char *name
,
123 unsigned char *value
,
127 struct xfs_da_args args
;
131 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
133 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
136 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
141 args
.valuelen
= *valuelenp
;
142 /* Entirely possible to look up a name which doesn't exist */
143 args
.op_flags
= XFS_DA_OP_OKNOENT
;
145 lock_mode
= xfs_ilock_attr_map_shared(ip
);
146 error
= xfs_attr_get_ilocked(ip
, &args
);
147 xfs_iunlock(ip
, lock_mode
);
149 *valuelenp
= args
.valuelen
;
150 return error
== -EEXIST
? 0 : error
;
154 * Calculate how many blocks we need for the new attribute,
158 struct xfs_da_args
*args
,
161 struct xfs_mount
*mp
= args
->dp
->i_mount
;
166 * Determine space new attribute will use, and if it would be
167 * "local" or "remote" (note: local != inline).
169 size
= xfs_attr_leaf_newentsize(args
, local
);
170 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
172 if (size
> (args
->geo
->blksize
/ 2)) {
173 /* Double split possible */
178 * Out of line attribute, cannot double split, but
179 * make room for the attribute value itself.
181 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
183 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
191 struct xfs_inode
*dp
,
192 const unsigned char *name
,
193 unsigned char *value
,
197 struct xfs_mount
*mp
= dp
->i_mount
;
198 struct xfs_buf
*leaf_bp
= NULL
;
199 struct xfs_da_args args
;
200 struct xfs_trans_res tres
;
201 int rsvd
= (flags
& ATTR_ROOT
) != 0;
202 int error
, err2
, local
;
204 XFS_STATS_INC(mp
, xs_attr_set
);
206 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
209 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
214 args
.valuelen
= valuelen
;
215 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
216 args
.total
= xfs_attr_calc_size(&args
, &local
);
218 error
= xfs_qm_dqattach(dp
);
223 * If the inode doesn't have an attribute fork, add one.
224 * (inode must not be locked when we call this routine)
226 if (XFS_IFORK_Q(dp
) == 0) {
227 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
228 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
230 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
235 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
236 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
237 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
238 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
241 * Root fork attributes can use reserved data blocks for this
242 * operation if necessary
244 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
245 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
249 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
250 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
251 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
252 XFS_QMOPT_RES_REGBLKS
);
254 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
255 xfs_trans_cancel(args
.trans
);
259 xfs_trans_ijoin(args
.trans
, dp
, 0);
262 * If the attribute list is non-existent or a shortform list,
263 * upgrade it to a single-leaf-block attribute list.
265 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
266 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
267 dp
->i_d
.di_anextents
== 0)) {
270 * Build initial attribute list (if required).
272 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
273 xfs_attr_shortform_create(&args
);
276 * Try to add the attr to the attribute list in
279 error
= xfs_attr_shortform_addname(&args
);
280 if (error
!= -ENOSPC
) {
282 * Commit the shortform mods, and we're done.
283 * NOTE: this is also the error path (EEXIST, etc).
285 ASSERT(args
.trans
!= NULL
);
288 * If this is a synchronous mount, make sure that
289 * the transaction goes to disk before returning
292 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
293 xfs_trans_set_sync(args
.trans
);
295 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
296 xfs_trans_ichgtime(args
.trans
, dp
,
299 err2
= xfs_trans_commit(args
.trans
);
300 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
302 return error
? error
: err2
;
306 * It won't fit in the shortform, transform to a leaf block.
307 * GROT: another possible req'mt for a double-split btree op.
309 error
= xfs_attr_shortform_to_leaf(&args
, &leaf_bp
);
313 * Prevent the leaf buffer from being unlocked so that a
314 * concurrent AIL push cannot grab the half-baked leaf
315 * buffer and run into problems with the write verifier.
317 xfs_trans_bhold(args
.trans
, leaf_bp
);
318 error
= xfs_defer_finish(&args
.trans
);
323 * Commit the leaf transformation. We'll need another (linked)
324 * transaction to add the new attribute to the leaf, which
325 * means that we have to hold & join the leaf buffer here too.
327 error
= xfs_trans_roll_inode(&args
.trans
, dp
);
330 xfs_trans_bjoin(args
.trans
, leaf_bp
);
334 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
335 error
= xfs_attr_leaf_addname(&args
);
337 error
= xfs_attr_node_addname(&args
);
342 * If this is a synchronous mount, make sure that the
343 * transaction goes to disk before returning to the user.
345 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
346 xfs_trans_set_sync(args
.trans
);
348 if ((flags
& ATTR_KERNOTIME
) == 0)
349 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
352 * Commit the last in the sequence of transactions.
354 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
355 error
= xfs_trans_commit(args
.trans
);
356 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
362 xfs_trans_brelse(args
.trans
, leaf_bp
);
364 xfs_trans_cancel(args
.trans
);
365 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
370 * Generic handler routine to remove a name from an attribute list.
371 * Transitions attribute list from Btree to shortform as necessary.
375 struct xfs_inode
*dp
,
376 const unsigned char *name
,
379 struct xfs_mount
*mp
= dp
->i_mount
;
380 struct xfs_da_args args
;
383 XFS_STATS_INC(mp
, xs_attr_remove
);
385 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
388 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
393 * we have no control over the attribute names that userspace passes us
394 * to remove, so we have to allow the name lookup prior to attribute
397 args
.op_flags
= XFS_DA_OP_OKNOENT
;
399 error
= xfs_qm_dqattach(dp
);
404 * Root fork attributes can use reserved data blocks for this
405 * operation if necessary
407 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
408 XFS_ATTRRM_SPACE_RES(mp
), 0,
409 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
414 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
416 * No need to make quota reservations here. We expect to release some
417 * blocks not allocate in the common case.
419 xfs_trans_ijoin(args
.trans
, dp
, 0);
421 if (!xfs_inode_hasattr(dp
)) {
423 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
424 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
425 error
= xfs_attr_shortform_remove(&args
);
426 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
427 error
= xfs_attr_leaf_removename(&args
);
429 error
= xfs_attr_node_removename(&args
);
436 * If this is a synchronous mount, make sure that the
437 * transaction goes to disk before returning to the user.
439 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
440 xfs_trans_set_sync(args
.trans
);
442 if ((flags
& ATTR_KERNOTIME
) == 0)
443 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
446 * Commit the last in the sequence of transactions.
448 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
449 error
= xfs_trans_commit(args
.trans
);
450 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
456 xfs_trans_cancel(args
.trans
);
457 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
461 /*========================================================================
462 * External routines when attribute list is inside the inode
463 *========================================================================*/
466 * Add a name to the shortform attribute list structure
467 * This is the external routine.
470 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
472 int newsize
, forkoff
, retval
;
474 trace_xfs_attr_sf_addname(args
);
476 retval
= xfs_attr_shortform_lookup(args
);
477 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
479 } else if (retval
== -EEXIST
) {
480 if (args
->flags
& ATTR_CREATE
)
482 retval
= xfs_attr_shortform_remove(args
);
486 * Since we have removed the old attr, clear ATTR_REPLACE so
487 * that the leaf format add routine won't trip over the attr
490 args
->flags
&= ~ATTR_REPLACE
;
493 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
494 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
497 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
498 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
500 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
504 xfs_attr_shortform_add(args
, forkoff
);
509 /*========================================================================
510 * External routines when attribute list is one block
511 *========================================================================*/
514 * Add a name to the leaf attribute list structure
516 * This leaf block cannot have a "remote" value, we only call this routine
517 * if bmap_one_block() says there is only one block (ie: no remote blks).
520 xfs_attr_leaf_addname(
521 struct xfs_da_args
*args
)
523 struct xfs_inode
*dp
;
525 int retval
, error
, forkoff
;
527 trace_xfs_attr_leaf_addname(args
);
530 * Read the (only) block in the attribute list in.
534 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
539 * Look up the given attribute in the leaf block. Figure out if
540 * the given flags produce an error or call for an atomic rename.
542 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
543 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
544 xfs_trans_brelse(args
->trans
, bp
);
546 } else if (retval
== -EEXIST
) {
547 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
548 xfs_trans_brelse(args
->trans
, bp
);
552 trace_xfs_attr_leaf_replace(args
);
554 /* save the attribute state for later removal*/
555 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
556 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
557 args
->index2
= args
->index
;
558 args
->rmtblkno2
= args
->rmtblkno
;
559 args
->rmtblkcnt2
= args
->rmtblkcnt
;
560 args
->rmtvaluelen2
= args
->rmtvaluelen
;
563 * clear the remote attr state now that it is saved so that the
564 * values reflect the state of the attribute we are about to
565 * add, not the attribute we just found and will remove later.
569 args
->rmtvaluelen
= 0;
573 * Add the attribute to the leaf block, transitioning to a Btree
576 retval
= xfs_attr3_leaf_add(bp
, args
);
577 if (retval
== -ENOSPC
) {
579 * Promote the attribute list to the Btree format, then
580 * Commit that transaction so that the node_addname() call
581 * can manage its own transactions.
583 error
= xfs_attr3_leaf_to_node(args
);
586 error
= xfs_defer_finish(&args
->trans
);
591 * Commit the current trans (including the inode) and start
594 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
599 * Fob the whole rest of the problem off on the Btree code.
601 error
= xfs_attr_node_addname(args
);
606 * Commit the transaction that added the attr name so that
607 * later routines can manage their own transactions.
609 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
614 * If there was an out-of-line value, allocate the blocks we
615 * identified for its storage and copy the value. This is done
616 * after we create the attribute so that we don't overflow the
617 * maximum size of a transaction and/or hit a deadlock.
619 if (args
->rmtblkno
> 0) {
620 error
= xfs_attr_rmtval_set(args
);
626 * If this is an atomic rename operation, we must "flip" the
627 * incomplete flags on the "new" and "old" attribute/value pairs
628 * so that one disappears and one appears atomically. Then we
629 * must remove the "old" attribute/value pair.
631 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
633 * In a separate transaction, set the incomplete flag on the
634 * "old" attr and clear the incomplete flag on the "new" attr.
636 error
= xfs_attr3_leaf_flipflags(args
);
641 * Dismantle the "old" attribute/value pair by removing
642 * a "remote" value (if it exists).
644 args
->index
= args
->index2
;
645 args
->blkno
= args
->blkno2
;
646 args
->rmtblkno
= args
->rmtblkno2
;
647 args
->rmtblkcnt
= args
->rmtblkcnt2
;
648 args
->rmtvaluelen
= args
->rmtvaluelen2
;
649 if (args
->rmtblkno
) {
650 error
= xfs_attr_rmtval_remove(args
);
656 * Read in the block containing the "old" attr, then
657 * remove the "old" attr from that block (neat, huh!)
659 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
664 xfs_attr3_leaf_remove(bp
, args
);
667 * If the result is small enough, shrink it all into the inode.
669 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
670 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
671 /* bp is gone due to xfs_da_shrink_inode */
674 error
= xfs_defer_finish(&args
->trans
);
680 * Commit the remove and start the next trans in series.
682 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
684 } else if (args
->rmtblkno
> 0) {
686 * Added a "remote" value, just clear the incomplete flag.
688 error
= xfs_attr3_leaf_clearflag(args
);
694 * Remove a name from the leaf attribute list structure
696 * This leaf block cannot have a "remote" value, we only call this routine
697 * if bmap_one_block() says there is only one block (ie: no remote blks).
700 xfs_attr_leaf_removename(
701 struct xfs_da_args
*args
)
703 struct xfs_inode
*dp
;
707 trace_xfs_attr_leaf_removename(args
);
710 * Remove the attribute.
714 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
718 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
719 if (error
== -ENOATTR
) {
720 xfs_trans_brelse(args
->trans
, bp
);
724 xfs_attr3_leaf_remove(bp
, args
);
727 * If the result is small enough, shrink it all into the inode.
729 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
730 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
731 /* bp is gone due to xfs_da_shrink_inode */
734 error
= xfs_defer_finish(&args
->trans
);
742 * Look up a name in a leaf attribute list structure.
744 * This leaf block cannot have a "remote" value, we only call this routine
745 * if bmap_one_block() says there is only one block (ie: no remote blks).
748 xfs_attr_leaf_get(xfs_da_args_t
*args
)
753 trace_xfs_attr_leaf_get(args
);
756 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
760 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
761 if (error
!= -EEXIST
) {
762 xfs_trans_brelse(args
->trans
, bp
);
765 error
= xfs_attr3_leaf_getvalue(bp
, args
);
766 xfs_trans_brelse(args
->trans
, bp
);
767 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
768 error
= xfs_attr_rmtval_get(args
);
773 /*========================================================================
774 * External routines when attribute list size > geo->blksize
775 *========================================================================*/
778 * Add a name to a Btree-format attribute list.
780 * This will involve walking down the Btree, and may involve splitting
781 * leaf nodes and even splitting intermediate nodes up to and including
782 * the root node (a special case of an intermediate node).
784 * "Remote" attribute values confuse the issue and atomic rename operations
785 * add a whole extra layer of confusion on top of that.
788 xfs_attr_node_addname(
789 struct xfs_da_args
*args
)
791 struct xfs_da_state
*state
;
792 struct xfs_da_state_blk
*blk
;
793 struct xfs_inode
*dp
;
794 struct xfs_mount
*mp
;
797 trace_xfs_attr_node_addname(args
);
800 * Fill in bucket of arguments/results/context to carry around.
805 state
= xfs_da_state_alloc();
810 * Search to see if name already exists, and get back a pointer
811 * to where it should go.
813 error
= xfs_da3_node_lookup_int(state
, &retval
);
816 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
817 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
818 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
820 } else if (retval
== -EEXIST
) {
821 if (args
->flags
& ATTR_CREATE
)
824 trace_xfs_attr_node_replace(args
);
826 /* save the attribute state for later removal*/
827 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
828 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
829 args
->index2
= args
->index
;
830 args
->rmtblkno2
= args
->rmtblkno
;
831 args
->rmtblkcnt2
= args
->rmtblkcnt
;
832 args
->rmtvaluelen2
= args
->rmtvaluelen
;
835 * clear the remote attr state now that it is saved so that the
836 * values reflect the state of the attribute we are about to
837 * add, not the attribute we just found and will remove later.
841 args
->rmtvaluelen
= 0;
844 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
845 if (retval
== -ENOSPC
) {
846 if (state
->path
.active
== 1) {
848 * Its really a single leaf node, but it had
849 * out-of-line values so it looked like it *might*
850 * have been a b-tree.
852 xfs_da_state_free(state
);
854 error
= xfs_attr3_leaf_to_node(args
);
857 error
= xfs_defer_finish(&args
->trans
);
862 * Commit the node conversion and start the next
863 * trans in the chain.
865 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
873 * Split as many Btree elements as required.
874 * This code tracks the new and old attr's location
875 * in the index/blkno/rmtblkno/rmtblkcnt fields and
876 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
878 error
= xfs_da3_split(state
);
881 error
= xfs_defer_finish(&args
->trans
);
886 * Addition succeeded, update Btree hashvals.
888 xfs_da3_fixhashpath(state
, &state
->path
);
892 * Kill the state structure, we're done with it and need to
893 * allow the buffers to come back later.
895 xfs_da_state_free(state
);
899 * Commit the leaf addition or btree split and start the next
900 * trans in the chain.
902 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
907 * If there was an out-of-line value, allocate the blocks we
908 * identified for its storage and copy the value. This is done
909 * after we create the attribute so that we don't overflow the
910 * maximum size of a transaction and/or hit a deadlock.
912 if (args
->rmtblkno
> 0) {
913 error
= xfs_attr_rmtval_set(args
);
919 * If this is an atomic rename operation, we must "flip" the
920 * incomplete flags on the "new" and "old" attribute/value pairs
921 * so that one disappears and one appears atomically. Then we
922 * must remove the "old" attribute/value pair.
924 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
926 * In a separate transaction, set the incomplete flag on the
927 * "old" attr and clear the incomplete flag on the "new" attr.
929 error
= xfs_attr3_leaf_flipflags(args
);
934 * Dismantle the "old" attribute/value pair by removing
935 * a "remote" value (if it exists).
937 args
->index
= args
->index2
;
938 args
->blkno
= args
->blkno2
;
939 args
->rmtblkno
= args
->rmtblkno2
;
940 args
->rmtblkcnt
= args
->rmtblkcnt2
;
941 args
->rmtvaluelen
= args
->rmtvaluelen2
;
942 if (args
->rmtblkno
) {
943 error
= xfs_attr_rmtval_remove(args
);
949 * Re-find the "old" attribute entry after any split ops.
950 * The INCOMPLETE flag means that we will find the "old"
951 * attr, not the "new" one.
953 args
->flags
|= XFS_ATTR_INCOMPLETE
;
954 state
= xfs_da_state_alloc();
958 error
= xfs_da3_node_lookup_int(state
, &retval
);
963 * Remove the name and update the hashvals in the tree.
965 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
966 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
967 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
968 xfs_da3_fixhashpath(state
, &state
->path
);
971 * Check to see if the tree needs to be collapsed.
973 if (retval
&& (state
->path
.active
> 1)) {
974 error
= xfs_da3_join(state
);
977 error
= xfs_defer_finish(&args
->trans
);
983 * Commit and start the next trans in the chain.
985 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
989 } else if (args
->rmtblkno
> 0) {
991 * Added a "remote" value, just clear the incomplete flag.
993 error
= xfs_attr3_leaf_clearflag(args
);
1001 xfs_da_state_free(state
);
1008 * Remove a name from a B-tree attribute list.
1010 * This will involve walking down the Btree, and may involve joining
1011 * leaf nodes and even joining intermediate nodes up to and including
1012 * the root node (a special case of an intermediate node).
1015 xfs_attr_node_removename(
1016 struct xfs_da_args
*args
)
1018 struct xfs_da_state
*state
;
1019 struct xfs_da_state_blk
*blk
;
1020 struct xfs_inode
*dp
;
1022 int retval
, error
, forkoff
;
1024 trace_xfs_attr_node_removename(args
);
1027 * Tie a string around our finger to remind us where we are.
1030 state
= xfs_da_state_alloc();
1032 state
->mp
= dp
->i_mount
;
1035 * Search to see if name exists, and get back a pointer to it.
1037 error
= xfs_da3_node_lookup_int(state
, &retval
);
1038 if (error
|| (retval
!= -EEXIST
)) {
1045 * If there is an out-of-line value, de-allocate the blocks.
1046 * This is done before we remove the attribute so that we don't
1047 * overflow the maximum size of a transaction and/or hit a deadlock.
1049 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1050 ASSERT(blk
->bp
!= NULL
);
1051 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1052 if (args
->rmtblkno
> 0) {
1054 * Fill in disk block numbers in the state structure
1055 * so that we can get the buffers back after we commit
1056 * several transactions in the following calls.
1058 error
= xfs_attr_fillstate(state
);
1063 * Mark the attribute as INCOMPLETE, then bunmapi() the
1066 error
= xfs_attr3_leaf_setflag(args
);
1069 error
= xfs_attr_rmtval_remove(args
);
1074 * Refill the state structure with buffers, the prior calls
1075 * released our buffers.
1077 error
= xfs_attr_refillstate(state
);
1083 * Remove the name and update the hashvals in the tree.
1085 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1086 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1087 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1088 xfs_da3_fixhashpath(state
, &state
->path
);
1091 * Check to see if the tree needs to be collapsed.
1093 if (retval
&& (state
->path
.active
> 1)) {
1094 error
= xfs_da3_join(state
);
1097 error
= xfs_defer_finish(&args
->trans
);
1101 * Commit the Btree join operation and start a new trans.
1103 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1109 * If the result is small enough, push it all into the inode.
1111 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1113 * Have to get rid of the copy of this dabuf in the state.
1115 ASSERT(state
->path
.active
== 1);
1116 ASSERT(state
->path
.blk
[0].bp
);
1117 state
->path
.blk
[0].bp
= NULL
;
1119 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1123 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1124 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1125 /* bp is gone due to xfs_da_shrink_inode */
1128 error
= xfs_defer_finish(&args
->trans
);
1132 xfs_trans_brelse(args
->trans
, bp
);
1137 xfs_da_state_free(state
);
1142 * Fill in the disk block numbers in the state structure for the buffers
1143 * that are attached to the state structure.
1144 * This is done so that we can quickly reattach ourselves to those buffers
1145 * after some set of transaction commits have released these buffers.
1148 xfs_attr_fillstate(xfs_da_state_t
*state
)
1150 xfs_da_state_path_t
*path
;
1151 xfs_da_state_blk_t
*blk
;
1154 trace_xfs_attr_fillstate(state
->args
);
1157 * Roll down the "path" in the state structure, storing the on-disk
1158 * block number for those buffers in the "path".
1160 path
= &state
->path
;
1161 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1162 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1164 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1167 blk
->disk_blkno
= 0;
1172 * Roll down the "altpath" in the state structure, storing the on-disk
1173 * block number for those buffers in the "altpath".
1175 path
= &state
->altpath
;
1176 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1177 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1179 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1182 blk
->disk_blkno
= 0;
1190 * Reattach the buffers to the state structure based on the disk block
1191 * numbers stored in the state structure.
1192 * This is done after some set of transaction commits have released those
1193 * buffers from our grip.
1196 xfs_attr_refillstate(xfs_da_state_t
*state
)
1198 xfs_da_state_path_t
*path
;
1199 xfs_da_state_blk_t
*blk
;
1202 trace_xfs_attr_refillstate(state
->args
);
1205 * Roll down the "path" in the state structure, storing the on-disk
1206 * block number for those buffers in the "path".
1208 path
= &state
->path
;
1209 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1210 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1211 if (blk
->disk_blkno
) {
1212 error
= xfs_da3_node_read(state
->args
->trans
,
1214 blk
->blkno
, blk
->disk_blkno
,
1215 &blk
->bp
, XFS_ATTR_FORK
);
1224 * Roll down the "altpath" in the state structure, storing the on-disk
1225 * block number for those buffers in the "altpath".
1227 path
= &state
->altpath
;
1228 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1229 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1230 if (blk
->disk_blkno
) {
1231 error
= xfs_da3_node_read(state
->args
->trans
,
1233 blk
->blkno
, blk
->disk_blkno
,
1234 &blk
->bp
, XFS_ATTR_FORK
);
1246 * Look up a filename in a node attribute list.
1248 * This routine gets called for any attribute fork that has more than one
1249 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1250 * "remote" values taking up more blocks.
1253 xfs_attr_node_get(xfs_da_args_t
*args
)
1255 xfs_da_state_t
*state
;
1256 xfs_da_state_blk_t
*blk
;
1260 trace_xfs_attr_node_get(args
);
1262 state
= xfs_da_state_alloc();
1264 state
->mp
= args
->dp
->i_mount
;
1267 * Search to see if name exists, and get back a pointer to it.
1269 error
= xfs_da3_node_lookup_int(state
, &retval
);
1272 } else if (retval
== -EEXIST
) {
1273 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1274 ASSERT(blk
->bp
!= NULL
);
1275 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1278 * Get the value, local or "remote"
1280 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1281 if (!retval
&& (args
->rmtblkno
> 0)
1282 && !(args
->flags
& ATTR_KERNOVAL
)) {
1283 retval
= xfs_attr_rmtval_get(args
);
1288 * If not in a transaction, we have to release all the buffers.
1290 for (i
= 0; i
< state
->path
.active
; i
++) {
1291 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1292 state
->path
.blk
[i
].bp
= NULL
;
1295 xfs_da_state_free(state
);