]>
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_defer_ops dfops
;
201 struct xfs_trans_res tres
;
202 xfs_fsblock_t firstblock
;
203 int rsvd
= (flags
& ATTR_ROOT
) != 0;
204 int error
, err2
, local
;
206 XFS_STATS_INC(mp
, xs_attr_set
);
208 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
211 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
216 args
.valuelen
= valuelen
;
217 args
.firstblock
= &firstblock
;
219 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
220 args
.total
= xfs_attr_calc_size(&args
, &local
);
222 error
= xfs_qm_dqattach(dp
);
227 * If the inode doesn't have an attribute fork, add one.
228 * (inode must not be locked when we call this routine)
230 if (XFS_IFORK_Q(dp
) == 0) {
231 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
232 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
234 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
239 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
240 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
241 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
242 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
245 * Root fork attributes can use reserved data blocks for this
246 * operation if necessary
248 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
249 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
253 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
254 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
255 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
256 XFS_QMOPT_RES_REGBLKS
);
258 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
259 xfs_trans_cancel(args
.trans
);
263 xfs_trans_ijoin(args
.trans
, dp
, 0);
266 * If the attribute list is non-existent or a shortform list,
267 * upgrade it to a single-leaf-block attribute list.
269 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
270 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
271 dp
->i_d
.di_anextents
== 0)) {
274 * Build initial attribute list (if required).
276 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
277 xfs_attr_shortform_create(&args
);
280 * Try to add the attr to the attribute list in
283 error
= xfs_attr_shortform_addname(&args
);
284 if (error
!= -ENOSPC
) {
286 * Commit the shortform mods, and we're done.
287 * NOTE: this is also the error path (EEXIST, etc).
289 ASSERT(args
.trans
!= NULL
);
292 * If this is a synchronous mount, make sure that
293 * the transaction goes to disk before returning
296 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
297 xfs_trans_set_sync(args
.trans
);
299 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
300 xfs_trans_ichgtime(args
.trans
, dp
,
303 err2
= xfs_trans_commit(args
.trans
);
304 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
306 return error
? error
: err2
;
310 * It won't fit in the shortform, transform to a leaf block.
311 * GROT: another possible req'mt for a double-split btree op.
313 xfs_defer_init(args
.dfops
, args
.firstblock
);
314 error
= xfs_attr_shortform_to_leaf(&args
, &leaf_bp
);
316 goto out_defer_cancel
;
318 * Prevent the leaf buffer from being unlocked so that a
319 * concurrent AIL push cannot grab the half-baked leaf
320 * buffer and run into problems with the write verifier.
322 xfs_trans_bhold(args
.trans
, leaf_bp
);
323 xfs_defer_bjoin(args
.dfops
, leaf_bp
);
324 xfs_defer_ijoin(args
.dfops
, dp
);
325 error
= xfs_defer_finish(&args
.trans
, args
.dfops
);
327 goto out_defer_cancel
;
330 * Commit the leaf transformation. We'll need another (linked)
331 * transaction to add the new attribute to the leaf, which
332 * means that we have to hold & join the leaf buffer here too.
334 error
= xfs_trans_roll_inode(&args
.trans
, dp
);
337 xfs_trans_bjoin(args
.trans
, leaf_bp
);
341 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
342 error
= xfs_attr_leaf_addname(&args
);
344 error
= xfs_attr_node_addname(&args
);
349 * If this is a synchronous mount, make sure that the
350 * transaction goes to disk before returning to the user.
352 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
353 xfs_trans_set_sync(args
.trans
);
355 if ((flags
& ATTR_KERNOTIME
) == 0)
356 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
359 * Commit the last in the sequence of transactions.
361 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
362 error
= xfs_trans_commit(args
.trans
);
363 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
368 xfs_defer_cancel(&dfops
);
371 xfs_trans_brelse(args
.trans
, leaf_bp
);
373 xfs_trans_cancel(args
.trans
);
374 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
379 * Generic handler routine to remove a name from an attribute list.
380 * Transitions attribute list from Btree to shortform as necessary.
384 struct xfs_inode
*dp
,
385 const unsigned char *name
,
388 struct xfs_mount
*mp
= dp
->i_mount
;
389 struct xfs_da_args args
;
390 struct xfs_defer_ops dfops
;
391 xfs_fsblock_t firstblock
;
394 XFS_STATS_INC(mp
, xs_attr_remove
);
396 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
399 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
403 args
.firstblock
= &firstblock
;
407 * we have no control over the attribute names that userspace passes us
408 * to remove, so we have to allow the name lookup prior to attribute
411 args
.op_flags
= XFS_DA_OP_OKNOENT
;
413 error
= xfs_qm_dqattach(dp
);
418 * Root fork attributes can use reserved data blocks for this
419 * operation if necessary
421 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
422 XFS_ATTRRM_SPACE_RES(mp
), 0,
423 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
428 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
430 * No need to make quota reservations here. We expect to release some
431 * blocks not allocate in the common case.
433 xfs_trans_ijoin(args
.trans
, dp
, 0);
435 if (!xfs_inode_hasattr(dp
)) {
437 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
438 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
439 error
= xfs_attr_shortform_remove(&args
);
440 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
441 error
= xfs_attr_leaf_removename(&args
);
443 error
= xfs_attr_node_removename(&args
);
450 * If this is a synchronous mount, make sure that the
451 * transaction goes to disk before returning to the user.
453 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
454 xfs_trans_set_sync(args
.trans
);
456 if ((flags
& ATTR_KERNOTIME
) == 0)
457 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
460 * Commit the last in the sequence of transactions.
462 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
463 error
= xfs_trans_commit(args
.trans
);
464 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
470 xfs_trans_cancel(args
.trans
);
471 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
475 /*========================================================================
476 * External routines when attribute list is inside the inode
477 *========================================================================*/
480 * Add a name to the shortform attribute list structure
481 * This is the external routine.
484 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
486 int newsize
, forkoff
, retval
;
488 trace_xfs_attr_sf_addname(args
);
490 retval
= xfs_attr_shortform_lookup(args
);
491 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
493 } else if (retval
== -EEXIST
) {
494 if (args
->flags
& ATTR_CREATE
)
496 retval
= xfs_attr_shortform_remove(args
);
500 * Since we have removed the old attr, clear ATTR_REPLACE so
501 * that the leaf format add routine won't trip over the attr
504 args
->flags
&= ~ATTR_REPLACE
;
507 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
508 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
511 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
512 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
514 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
518 xfs_attr_shortform_add(args
, forkoff
);
523 /*========================================================================
524 * External routines when attribute list is one block
525 *========================================================================*/
528 * Add a name to the leaf attribute list structure
530 * This leaf block cannot have a "remote" value, we only call this routine
531 * if bmap_one_block() says there is only one block (ie: no remote blks).
534 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
538 int retval
, error
, forkoff
;
540 trace_xfs_attr_leaf_addname(args
);
543 * Read the (only) block in the attribute list in.
547 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
552 * Look up the given attribute in the leaf block. Figure out if
553 * the given flags produce an error or call for an atomic rename.
555 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
556 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
557 xfs_trans_brelse(args
->trans
, bp
);
559 } else if (retval
== -EEXIST
) {
560 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
561 xfs_trans_brelse(args
->trans
, bp
);
565 trace_xfs_attr_leaf_replace(args
);
567 /* save the attribute state for later removal*/
568 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
569 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
570 args
->index2
= args
->index
;
571 args
->rmtblkno2
= args
->rmtblkno
;
572 args
->rmtblkcnt2
= args
->rmtblkcnt
;
573 args
->rmtvaluelen2
= args
->rmtvaluelen
;
576 * clear the remote attr state now that it is saved so that the
577 * values reflect the state of the attribute we are about to
578 * add, not the attribute we just found and will remove later.
582 args
->rmtvaluelen
= 0;
586 * Add the attribute to the leaf block, transitioning to a Btree
589 retval
= xfs_attr3_leaf_add(bp
, args
);
590 if (retval
== -ENOSPC
) {
592 * Promote the attribute list to the Btree format, then
593 * Commit that transaction so that the node_addname() call
594 * can manage its own transactions.
596 xfs_defer_init(args
->dfops
, args
->firstblock
);
597 error
= xfs_attr3_leaf_to_node(args
);
599 goto out_defer_cancel
;
600 xfs_defer_ijoin(args
->dfops
, dp
);
601 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
603 goto out_defer_cancel
;
606 * Commit the current trans (including the inode) and start
609 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
614 * Fob the whole rest of the problem off on the Btree code.
616 error
= xfs_attr_node_addname(args
);
621 * Commit the transaction that added the attr name so that
622 * later routines can manage their own transactions.
624 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
629 * If there was an out-of-line value, allocate the blocks we
630 * identified for its storage and copy the value. This is done
631 * after we create the attribute so that we don't overflow the
632 * maximum size of a transaction and/or hit a deadlock.
634 if (args
->rmtblkno
> 0) {
635 error
= xfs_attr_rmtval_set(args
);
641 * If this is an atomic rename operation, we must "flip" the
642 * incomplete flags on the "new" and "old" attribute/value pairs
643 * so that one disappears and one appears atomically. Then we
644 * must remove the "old" attribute/value pair.
646 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
648 * In a separate transaction, set the incomplete flag on the
649 * "old" attr and clear the incomplete flag on the "new" attr.
651 error
= xfs_attr3_leaf_flipflags(args
);
656 * Dismantle the "old" attribute/value pair by removing
657 * a "remote" value (if it exists).
659 args
->index
= args
->index2
;
660 args
->blkno
= args
->blkno2
;
661 args
->rmtblkno
= args
->rmtblkno2
;
662 args
->rmtblkcnt
= args
->rmtblkcnt2
;
663 args
->rmtvaluelen
= args
->rmtvaluelen2
;
664 if (args
->rmtblkno
) {
665 error
= xfs_attr_rmtval_remove(args
);
671 * Read in the block containing the "old" attr, then
672 * remove the "old" attr from that block (neat, huh!)
674 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
679 xfs_attr3_leaf_remove(bp
, args
);
682 * If the result is small enough, shrink it all into the inode.
684 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
685 xfs_defer_init(args
->dfops
, args
->firstblock
);
686 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
687 /* bp is gone due to xfs_da_shrink_inode */
689 goto out_defer_cancel
;
690 xfs_defer_ijoin(args
->dfops
, dp
);
691 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
693 goto out_defer_cancel
;
697 * Commit the remove and start the next trans in series.
699 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
701 } else if (args
->rmtblkno
> 0) {
703 * Added a "remote" value, just clear the incomplete flag.
705 error
= xfs_attr3_leaf_clearflag(args
);
709 xfs_defer_cancel(args
->dfops
);
714 * Remove a name from the leaf attribute list structure
716 * This leaf block cannot have a "remote" value, we only call this routine
717 * if bmap_one_block() says there is only one block (ie: no remote blks).
720 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
726 trace_xfs_attr_leaf_removename(args
);
729 * Remove the attribute.
733 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
737 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
738 if (error
== -ENOATTR
) {
739 xfs_trans_brelse(args
->trans
, bp
);
743 xfs_attr3_leaf_remove(bp
, args
);
746 * If the result is small enough, shrink it all into the inode.
748 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
749 xfs_defer_init(args
->dfops
, args
->firstblock
);
750 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
751 /* bp is gone due to xfs_da_shrink_inode */
753 goto out_defer_cancel
;
754 xfs_defer_ijoin(args
->dfops
, dp
);
755 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
757 goto out_defer_cancel
;
761 xfs_defer_cancel(args
->dfops
);
766 * Look up a name in a leaf attribute list structure.
768 * This leaf block cannot have a "remote" value, we only call this routine
769 * if bmap_one_block() says there is only one block (ie: no remote blks).
772 xfs_attr_leaf_get(xfs_da_args_t
*args
)
777 trace_xfs_attr_leaf_get(args
);
780 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
784 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
785 if (error
!= -EEXIST
) {
786 xfs_trans_brelse(args
->trans
, bp
);
789 error
= xfs_attr3_leaf_getvalue(bp
, args
);
790 xfs_trans_brelse(args
->trans
, bp
);
791 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
792 error
= xfs_attr_rmtval_get(args
);
797 /*========================================================================
798 * External routines when attribute list size > geo->blksize
799 *========================================================================*/
802 * Add a name to a Btree-format attribute list.
804 * This will involve walking down the Btree, and may involve splitting
805 * leaf nodes and even splitting intermediate nodes up to and including
806 * the root node (a special case of an intermediate node).
808 * "Remote" attribute values confuse the issue and atomic rename operations
809 * add a whole extra layer of confusion on top of that.
812 xfs_attr_node_addname(xfs_da_args_t
*args
)
814 xfs_da_state_t
*state
;
815 xfs_da_state_blk_t
*blk
;
820 trace_xfs_attr_node_addname(args
);
823 * Fill in bucket of arguments/results/context to carry around.
828 state
= xfs_da_state_alloc();
833 * Search to see if name already exists, and get back a pointer
834 * to where it should go.
836 error
= xfs_da3_node_lookup_int(state
, &retval
);
839 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
840 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
841 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
843 } else if (retval
== -EEXIST
) {
844 if (args
->flags
& ATTR_CREATE
)
847 trace_xfs_attr_node_replace(args
);
849 /* save the attribute state for later removal*/
850 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
851 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
852 args
->index2
= args
->index
;
853 args
->rmtblkno2
= args
->rmtblkno
;
854 args
->rmtblkcnt2
= args
->rmtblkcnt
;
855 args
->rmtvaluelen2
= args
->rmtvaluelen
;
858 * clear the remote attr state now that it is saved so that the
859 * values reflect the state of the attribute we are about to
860 * add, not the attribute we just found and will remove later.
864 args
->rmtvaluelen
= 0;
867 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
868 if (retval
== -ENOSPC
) {
869 if (state
->path
.active
== 1) {
871 * Its really a single leaf node, but it had
872 * out-of-line values so it looked like it *might*
873 * have been a b-tree.
875 xfs_da_state_free(state
);
877 xfs_defer_init(args
->dfops
, args
->firstblock
);
878 error
= xfs_attr3_leaf_to_node(args
);
880 goto out_defer_cancel
;
881 xfs_defer_ijoin(args
->dfops
, dp
);
882 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
884 goto out_defer_cancel
;
887 * Commit the node conversion and start the next
888 * trans in the chain.
890 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
898 * Split as many Btree elements as required.
899 * This code tracks the new and old attr's location
900 * in the index/blkno/rmtblkno/rmtblkcnt fields and
901 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
903 xfs_defer_init(args
->dfops
, args
->firstblock
);
904 error
= xfs_da3_split(state
);
906 goto out_defer_cancel
;
907 xfs_defer_ijoin(args
->dfops
, dp
);
908 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
910 goto out_defer_cancel
;
913 * Addition succeeded, update Btree hashvals.
915 xfs_da3_fixhashpath(state
, &state
->path
);
919 * Kill the state structure, we're done with it and need to
920 * allow the buffers to come back later.
922 xfs_da_state_free(state
);
926 * Commit the leaf addition or btree split and start the next
927 * trans in the chain.
929 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
934 * If there was an out-of-line value, allocate the blocks we
935 * identified for its storage and copy the value. This is done
936 * after we create the attribute so that we don't overflow the
937 * maximum size of a transaction and/or hit a deadlock.
939 if (args
->rmtblkno
> 0) {
940 error
= xfs_attr_rmtval_set(args
);
946 * If this is an atomic rename operation, we must "flip" the
947 * incomplete flags on the "new" and "old" attribute/value pairs
948 * so that one disappears and one appears atomically. Then we
949 * must remove the "old" attribute/value pair.
951 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
953 * In a separate transaction, set the incomplete flag on the
954 * "old" attr and clear the incomplete flag on the "new" attr.
956 error
= xfs_attr3_leaf_flipflags(args
);
961 * Dismantle the "old" attribute/value pair by removing
962 * a "remote" value (if it exists).
964 args
->index
= args
->index2
;
965 args
->blkno
= args
->blkno2
;
966 args
->rmtblkno
= args
->rmtblkno2
;
967 args
->rmtblkcnt
= args
->rmtblkcnt2
;
968 args
->rmtvaluelen
= args
->rmtvaluelen2
;
969 if (args
->rmtblkno
) {
970 error
= xfs_attr_rmtval_remove(args
);
976 * Re-find the "old" attribute entry after any split ops.
977 * The INCOMPLETE flag means that we will find the "old"
978 * attr, not the "new" one.
980 args
->flags
|= XFS_ATTR_INCOMPLETE
;
981 state
= xfs_da_state_alloc();
985 error
= xfs_da3_node_lookup_int(state
, &retval
);
990 * Remove the name and update the hashvals in the tree.
992 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
993 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
994 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
995 xfs_da3_fixhashpath(state
, &state
->path
);
998 * Check to see if the tree needs to be collapsed.
1000 if (retval
&& (state
->path
.active
> 1)) {
1001 xfs_defer_init(args
->dfops
, args
->firstblock
);
1002 error
= xfs_da3_join(state
);
1004 goto out_defer_cancel
;
1005 xfs_defer_ijoin(args
->dfops
, dp
);
1006 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1008 goto out_defer_cancel
;
1012 * Commit and start the next trans in the chain.
1014 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1018 } else if (args
->rmtblkno
> 0) {
1020 * Added a "remote" value, just clear the incomplete flag.
1022 error
= xfs_attr3_leaf_clearflag(args
);
1030 xfs_da_state_free(state
);
1035 xfs_defer_cancel(args
->dfops
);
1040 * Remove a name from a B-tree attribute list.
1042 * This will involve walking down the Btree, and may involve joining
1043 * leaf nodes and even joining intermediate nodes up to and including
1044 * the root node (a special case of an intermediate node).
1047 xfs_attr_node_removename(xfs_da_args_t
*args
)
1049 xfs_da_state_t
*state
;
1050 xfs_da_state_blk_t
*blk
;
1053 int retval
, error
, forkoff
;
1055 trace_xfs_attr_node_removename(args
);
1058 * Tie a string around our finger to remind us where we are.
1061 state
= xfs_da_state_alloc();
1063 state
->mp
= dp
->i_mount
;
1066 * Search to see if name exists, and get back a pointer to it.
1068 error
= xfs_da3_node_lookup_int(state
, &retval
);
1069 if (error
|| (retval
!= -EEXIST
)) {
1076 * If there is an out-of-line value, de-allocate the blocks.
1077 * This is done before we remove the attribute so that we don't
1078 * overflow the maximum size of a transaction and/or hit a deadlock.
1080 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1081 ASSERT(blk
->bp
!= NULL
);
1082 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1083 if (args
->rmtblkno
> 0) {
1085 * Fill in disk block numbers in the state structure
1086 * so that we can get the buffers back after we commit
1087 * several transactions in the following calls.
1089 error
= xfs_attr_fillstate(state
);
1094 * Mark the attribute as INCOMPLETE, then bunmapi() the
1097 error
= xfs_attr3_leaf_setflag(args
);
1100 error
= xfs_attr_rmtval_remove(args
);
1105 * Refill the state structure with buffers, the prior calls
1106 * released our buffers.
1108 error
= xfs_attr_refillstate(state
);
1114 * Remove the name and update the hashvals in the tree.
1116 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1117 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1118 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1119 xfs_da3_fixhashpath(state
, &state
->path
);
1122 * Check to see if the tree needs to be collapsed.
1124 if (retval
&& (state
->path
.active
> 1)) {
1125 xfs_defer_init(args
->dfops
, args
->firstblock
);
1126 error
= xfs_da3_join(state
);
1128 goto out_defer_cancel
;
1129 xfs_defer_ijoin(args
->dfops
, dp
);
1130 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1132 goto out_defer_cancel
;
1134 * Commit the Btree join operation and start a new trans.
1136 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1142 * If the result is small enough, push it all into the inode.
1144 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1146 * Have to get rid of the copy of this dabuf in the state.
1148 ASSERT(state
->path
.active
== 1);
1149 ASSERT(state
->path
.blk
[0].bp
);
1150 state
->path
.blk
[0].bp
= NULL
;
1152 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1156 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1157 xfs_defer_init(args
->dfops
, args
->firstblock
);
1158 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1159 /* bp is gone due to xfs_da_shrink_inode */
1161 goto out_defer_cancel
;
1162 xfs_defer_ijoin(args
->dfops
, dp
);
1163 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1165 goto out_defer_cancel
;
1167 xfs_trans_brelse(args
->trans
, bp
);
1172 xfs_da_state_free(state
);
1175 xfs_defer_cancel(args
->dfops
);
1180 * Fill in the disk block numbers in the state structure for the buffers
1181 * that are attached to the state structure.
1182 * This is done so that we can quickly reattach ourselves to those buffers
1183 * after some set of transaction commits have released these buffers.
1186 xfs_attr_fillstate(xfs_da_state_t
*state
)
1188 xfs_da_state_path_t
*path
;
1189 xfs_da_state_blk_t
*blk
;
1192 trace_xfs_attr_fillstate(state
->args
);
1195 * Roll down the "path" in the state structure, storing the on-disk
1196 * block number for those buffers in the "path".
1198 path
= &state
->path
;
1199 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1200 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1202 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1205 blk
->disk_blkno
= 0;
1210 * Roll down the "altpath" in the state structure, storing the on-disk
1211 * block number for those buffers in the "altpath".
1213 path
= &state
->altpath
;
1214 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1215 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1217 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1220 blk
->disk_blkno
= 0;
1228 * Reattach the buffers to the state structure based on the disk block
1229 * numbers stored in the state structure.
1230 * This is done after some set of transaction commits have released those
1231 * buffers from our grip.
1234 xfs_attr_refillstate(xfs_da_state_t
*state
)
1236 xfs_da_state_path_t
*path
;
1237 xfs_da_state_blk_t
*blk
;
1240 trace_xfs_attr_refillstate(state
->args
);
1243 * Roll down the "path" in the state structure, storing the on-disk
1244 * block number for those buffers in the "path".
1246 path
= &state
->path
;
1247 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1248 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1249 if (blk
->disk_blkno
) {
1250 error
= xfs_da3_node_read(state
->args
->trans
,
1252 blk
->blkno
, blk
->disk_blkno
,
1253 &blk
->bp
, XFS_ATTR_FORK
);
1262 * Roll down the "altpath" in the state structure, storing the on-disk
1263 * block number for those buffers in the "altpath".
1265 path
= &state
->altpath
;
1266 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1267 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1268 if (blk
->disk_blkno
) {
1269 error
= xfs_da3_node_read(state
->args
->trans
,
1271 blk
->blkno
, blk
->disk_blkno
,
1272 &blk
->bp
, XFS_ATTR_FORK
);
1284 * Look up a filename in a node attribute list.
1286 * This routine gets called for any attribute fork that has more than one
1287 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1288 * "remote" values taking up more blocks.
1291 xfs_attr_node_get(xfs_da_args_t
*args
)
1293 xfs_da_state_t
*state
;
1294 xfs_da_state_blk_t
*blk
;
1298 trace_xfs_attr_node_get(args
);
1300 state
= xfs_da_state_alloc();
1302 state
->mp
= args
->dp
->i_mount
;
1305 * Search to see if name exists, and get back a pointer to it.
1307 error
= xfs_da3_node_lookup_int(state
, &retval
);
1310 } else if (retval
== -EEXIST
) {
1311 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1312 ASSERT(blk
->bp
!= NULL
);
1313 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1316 * Get the value, local or "remote"
1318 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1319 if (!retval
&& (args
->rmtblkno
> 0)
1320 && !(args
->flags
& ATTR_KERNOVAL
)) {
1321 retval
= xfs_attr_rmtval_get(args
);
1326 * If not in a transaction, we have to release all the buffers.
1328 for (i
= 0; i
< state
->path
.active
; i
++) {
1329 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1330 state
->path
.blk
[i
].bp
= NULL
;
1333 xfs_da_state_free(state
);