]>
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
);
190 xfs_attr_try_sf_addname(
191 struct xfs_inode
*dp
,
192 struct xfs_da_args
*args
)
195 struct xfs_mount
*mp
= dp
->i_mount
;
198 error
= xfs_attr_shortform_addname(args
);
199 if (error
== -ENOSPC
)
203 * Commit the shortform mods, and we're done.
204 * NOTE: this is also the error path (EEXIST, etc).
206 if (!error
&& (args
->flags
& ATTR_KERNOTIME
) == 0)
207 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
209 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
210 xfs_trans_set_sync(args
->trans
);
212 error2
= xfs_trans_commit(args
->trans
);
214 return error
? error
: error2
;
218 * Set the attribute specified in @args.
222 struct xfs_da_args
*args
,
223 struct xfs_buf
**leaf_bp
)
225 struct xfs_inode
*dp
= args
->dp
;
229 * If the attribute list is non-existent or a shortform list,
230 * upgrade it to a single-leaf-block attribute list.
232 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
233 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
234 dp
->i_d
.di_anextents
== 0)) {
237 * Build initial attribute list (if required).
239 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
240 xfs_attr_shortform_create(args
);
243 * Try to add the attr to the attribute list in the inode.
245 error
= xfs_attr_try_sf_addname(dp
, args
);
246 if (error
!= -ENOSPC
)
250 * It won't fit in the shortform, transform to a leaf block.
251 * GROT: another possible req'mt for a double-split btree op.
253 error
= xfs_attr_shortform_to_leaf(args
, leaf_bp
);
258 * Prevent the leaf buffer from being unlocked so that a
259 * concurrent AIL push cannot grab the half-baked leaf
260 * buffer and run into problems with the write verifier.
262 xfs_trans_bhold(args
->trans
, *leaf_bp
);
264 error
= xfs_defer_finish(&args
->trans
);
269 * Commit the leaf transformation. We'll need another
270 * (linked) transaction to add the new attribute to the
273 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
276 xfs_trans_bjoin(args
->trans
, *leaf_bp
);
280 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
281 error
= xfs_attr_leaf_addname(args
);
283 error
= xfs_attr_node_addname(args
);
288 * Remove the attribute specified in @args.
291 xfs_attr_remove_args(
292 struct xfs_da_args
*args
)
294 struct xfs_inode
*dp
= args
->dp
;
297 if (!xfs_inode_hasattr(dp
)) {
299 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
300 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
301 error
= xfs_attr_shortform_remove(args
);
302 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
303 error
= xfs_attr_leaf_removename(args
);
305 error
= xfs_attr_node_removename(args
);
313 struct xfs_inode
*dp
,
314 const unsigned char *name
,
315 unsigned char *value
,
319 struct xfs_mount
*mp
= dp
->i_mount
;
320 struct xfs_buf
*leaf_bp
= NULL
;
321 struct xfs_da_args args
;
322 struct xfs_trans_res tres
;
323 int rsvd
= (flags
& ATTR_ROOT
) != 0;
326 XFS_STATS_INC(mp
, xs_attr_set
);
328 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
331 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
336 args
.valuelen
= valuelen
;
337 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
338 args
.total
= xfs_attr_calc_size(&args
, &local
);
340 error
= xfs_qm_dqattach(dp
);
345 * If the inode doesn't have an attribute fork, add one.
346 * (inode must not be locked when we call this routine)
348 if (XFS_IFORK_Q(dp
) == 0) {
349 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
350 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
352 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
357 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
358 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
359 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
360 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
363 * Root fork attributes can use reserved data blocks for this
364 * operation if necessary
366 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
367 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
371 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
372 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
373 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
374 XFS_QMOPT_RES_REGBLKS
);
376 goto out_trans_cancel
;
378 xfs_trans_ijoin(args
.trans
, dp
, 0);
379 error
= xfs_attr_set_args(&args
, &leaf_bp
);
381 goto out_release_leaf
;
383 /* shortform attribute has already been committed */
388 * If this is a synchronous mount, make sure that the
389 * transaction goes to disk before returning to the user.
391 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
392 xfs_trans_set_sync(args
.trans
);
394 if ((flags
& ATTR_KERNOTIME
) == 0)
395 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
398 * Commit the last in the sequence of transactions.
400 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
401 error
= xfs_trans_commit(args
.trans
);
403 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
408 xfs_trans_brelse(args
.trans
, leaf_bp
);
411 xfs_trans_cancel(args
.trans
);
416 * Generic handler routine to remove a name from an attribute list.
417 * Transitions attribute list from Btree to shortform as necessary.
421 struct xfs_inode
*dp
,
422 const unsigned char *name
,
425 struct xfs_mount
*mp
= dp
->i_mount
;
426 struct xfs_da_args args
;
429 XFS_STATS_INC(mp
, xs_attr_remove
);
431 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
434 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
439 * we have no control over the attribute names that userspace passes us
440 * to remove, so we have to allow the name lookup prior to attribute
443 args
.op_flags
= XFS_DA_OP_OKNOENT
;
445 error
= xfs_qm_dqattach(dp
);
450 * Root fork attributes can use reserved data blocks for this
451 * operation if necessary
453 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
454 XFS_ATTRRM_SPACE_RES(mp
), 0,
455 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
460 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
462 * No need to make quota reservations here. We expect to release some
463 * blocks not allocate in the common case.
465 xfs_trans_ijoin(args
.trans
, dp
, 0);
467 error
= xfs_attr_remove_args(&args
);
472 * If this is a synchronous mount, make sure that the
473 * transaction goes to disk before returning to the user.
475 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
476 xfs_trans_set_sync(args
.trans
);
478 if ((flags
& ATTR_KERNOTIME
) == 0)
479 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
482 * Commit the last in the sequence of transactions.
484 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
485 error
= xfs_trans_commit(args
.trans
);
486 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
492 xfs_trans_cancel(args
.trans
);
493 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
497 /*========================================================================
498 * External routines when attribute list is inside the inode
499 *========================================================================*/
502 * Add a name to the shortform attribute list structure
503 * This is the external routine.
506 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
508 int newsize
, forkoff
, retval
;
510 trace_xfs_attr_sf_addname(args
);
512 retval
= xfs_attr_shortform_lookup(args
);
513 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
515 } else if (retval
== -EEXIST
) {
516 if (args
->flags
& ATTR_CREATE
)
518 retval
= xfs_attr_shortform_remove(args
);
522 * Since we have removed the old attr, clear ATTR_REPLACE so
523 * that the leaf format add routine won't trip over the attr
526 args
->flags
&= ~ATTR_REPLACE
;
529 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
530 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
533 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
534 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
536 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
540 xfs_attr_shortform_add(args
, forkoff
);
545 /*========================================================================
546 * External routines when attribute list is one block
547 *========================================================================*/
550 * Add a name to the leaf attribute list structure
552 * This leaf block cannot have a "remote" value, we only call this routine
553 * if bmap_one_block() says there is only one block (ie: no remote blks).
556 xfs_attr_leaf_addname(
557 struct xfs_da_args
*args
)
559 struct xfs_inode
*dp
;
561 int retval
, error
, forkoff
;
563 trace_xfs_attr_leaf_addname(args
);
566 * Read the (only) block in the attribute list in.
570 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
575 * Look up the given attribute in the leaf block. Figure out if
576 * the given flags produce an error or call for an atomic rename.
578 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
579 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
580 xfs_trans_brelse(args
->trans
, bp
);
582 } else if (retval
== -EEXIST
) {
583 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
584 xfs_trans_brelse(args
->trans
, bp
);
588 trace_xfs_attr_leaf_replace(args
);
590 /* save the attribute state for later removal*/
591 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
592 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
593 args
->index2
= args
->index
;
594 args
->rmtblkno2
= args
->rmtblkno
;
595 args
->rmtblkcnt2
= args
->rmtblkcnt
;
596 args
->rmtvaluelen2
= args
->rmtvaluelen
;
599 * clear the remote attr state now that it is saved so that the
600 * values reflect the state of the attribute we are about to
601 * add, not the attribute we just found and will remove later.
605 args
->rmtvaluelen
= 0;
609 * Add the attribute to the leaf block, transitioning to a Btree
612 retval
= xfs_attr3_leaf_add(bp
, args
);
613 if (retval
== -ENOSPC
) {
615 * Promote the attribute list to the Btree format, then
616 * Commit that transaction so that the node_addname() call
617 * can manage its own transactions.
619 error
= xfs_attr3_leaf_to_node(args
);
622 error
= xfs_defer_finish(&args
->trans
);
627 * Commit the current trans (including the inode) and start
630 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
635 * Fob the whole rest of the problem off on the Btree code.
637 error
= xfs_attr_node_addname(args
);
642 * Commit the transaction that added the attr name so that
643 * later routines can manage their own transactions.
645 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
650 * If there was an out-of-line value, allocate the blocks we
651 * identified for its storage and copy the value. This is done
652 * after we create the attribute so that we don't overflow the
653 * maximum size of a transaction and/or hit a deadlock.
655 if (args
->rmtblkno
> 0) {
656 error
= xfs_attr_rmtval_set(args
);
662 * If this is an atomic rename operation, we must "flip" the
663 * incomplete flags on the "new" and "old" attribute/value pairs
664 * so that one disappears and one appears atomically. Then we
665 * must remove the "old" attribute/value pair.
667 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
669 * In a separate transaction, set the incomplete flag on the
670 * "old" attr and clear the incomplete flag on the "new" attr.
672 error
= xfs_attr3_leaf_flipflags(args
);
677 * Dismantle the "old" attribute/value pair by removing
678 * a "remote" value (if it exists).
680 args
->index
= args
->index2
;
681 args
->blkno
= args
->blkno2
;
682 args
->rmtblkno
= args
->rmtblkno2
;
683 args
->rmtblkcnt
= args
->rmtblkcnt2
;
684 args
->rmtvaluelen
= args
->rmtvaluelen2
;
685 if (args
->rmtblkno
) {
686 error
= xfs_attr_rmtval_remove(args
);
692 * Read in the block containing the "old" attr, then
693 * remove the "old" attr from that block (neat, huh!)
695 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
700 xfs_attr3_leaf_remove(bp
, args
);
703 * If the result is small enough, shrink it all into the inode.
705 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
706 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
707 /* bp is gone due to xfs_da_shrink_inode */
710 error
= xfs_defer_finish(&args
->trans
);
716 * Commit the remove and start the next trans in series.
718 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
720 } else if (args
->rmtblkno
> 0) {
722 * Added a "remote" value, just clear the incomplete flag.
724 error
= xfs_attr3_leaf_clearflag(args
);
730 * Remove a name from the leaf attribute list structure
732 * This leaf block cannot have a "remote" value, we only call this routine
733 * if bmap_one_block() says there is only one block (ie: no remote blks).
736 xfs_attr_leaf_removename(
737 struct xfs_da_args
*args
)
739 struct xfs_inode
*dp
;
743 trace_xfs_attr_leaf_removename(args
);
746 * Remove the attribute.
750 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
754 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
755 if (error
== -ENOATTR
) {
756 xfs_trans_brelse(args
->trans
, bp
);
760 xfs_attr3_leaf_remove(bp
, args
);
763 * If the result is small enough, shrink it all into the inode.
765 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
766 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
767 /* bp is gone due to xfs_da_shrink_inode */
770 error
= xfs_defer_finish(&args
->trans
);
778 * Look up a name in a leaf attribute list structure.
780 * This leaf block cannot have a "remote" value, we only call this routine
781 * if bmap_one_block() says there is only one block (ie: no remote blks).
784 xfs_attr_leaf_get(xfs_da_args_t
*args
)
789 trace_xfs_attr_leaf_get(args
);
792 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
796 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
797 if (error
!= -EEXIST
) {
798 xfs_trans_brelse(args
->trans
, bp
);
801 error
= xfs_attr3_leaf_getvalue(bp
, args
);
802 xfs_trans_brelse(args
->trans
, bp
);
803 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
804 error
= xfs_attr_rmtval_get(args
);
809 /*========================================================================
810 * External routines when attribute list size > geo->blksize
811 *========================================================================*/
814 * Add a name to a Btree-format attribute list.
816 * This will involve walking down the Btree, and may involve splitting
817 * leaf nodes and even splitting intermediate nodes up to and including
818 * the root node (a special case of an intermediate node).
820 * "Remote" attribute values confuse the issue and atomic rename operations
821 * add a whole extra layer of confusion on top of that.
824 xfs_attr_node_addname(
825 struct xfs_da_args
*args
)
827 struct xfs_da_state
*state
;
828 struct xfs_da_state_blk
*blk
;
829 struct xfs_inode
*dp
;
830 struct xfs_mount
*mp
;
833 trace_xfs_attr_node_addname(args
);
836 * Fill in bucket of arguments/results/context to carry around.
841 state
= xfs_da_state_alloc();
846 * Search to see if name already exists, and get back a pointer
847 * to where it should go.
849 error
= xfs_da3_node_lookup_int(state
, &retval
);
852 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
853 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
854 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
856 } else if (retval
== -EEXIST
) {
857 if (args
->flags
& ATTR_CREATE
)
860 trace_xfs_attr_node_replace(args
);
862 /* save the attribute state for later removal*/
863 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
864 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
865 args
->index2
= args
->index
;
866 args
->rmtblkno2
= args
->rmtblkno
;
867 args
->rmtblkcnt2
= args
->rmtblkcnt
;
868 args
->rmtvaluelen2
= args
->rmtvaluelen
;
871 * clear the remote attr state now that it is saved so that the
872 * values reflect the state of the attribute we are about to
873 * add, not the attribute we just found and will remove later.
877 args
->rmtvaluelen
= 0;
880 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
881 if (retval
== -ENOSPC
) {
882 if (state
->path
.active
== 1) {
884 * Its really a single leaf node, but it had
885 * out-of-line values so it looked like it *might*
886 * have been a b-tree.
888 xfs_da_state_free(state
);
890 error
= xfs_attr3_leaf_to_node(args
);
893 error
= xfs_defer_finish(&args
->trans
);
898 * Commit the node conversion and start the next
899 * trans in the chain.
901 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
909 * Split as many Btree elements as required.
910 * This code tracks the new and old attr's location
911 * in the index/blkno/rmtblkno/rmtblkcnt fields and
912 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
914 error
= xfs_da3_split(state
);
917 error
= xfs_defer_finish(&args
->trans
);
922 * Addition succeeded, update Btree hashvals.
924 xfs_da3_fixhashpath(state
, &state
->path
);
928 * Kill the state structure, we're done with it and need to
929 * allow the buffers to come back later.
931 xfs_da_state_free(state
);
935 * Commit the leaf addition or btree split and start the next
936 * trans in the chain.
938 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
943 * If there was an out-of-line value, allocate the blocks we
944 * identified for its storage and copy the value. This is done
945 * after we create the attribute so that we don't overflow the
946 * maximum size of a transaction and/or hit a deadlock.
948 if (args
->rmtblkno
> 0) {
949 error
= xfs_attr_rmtval_set(args
);
955 * If this is an atomic rename operation, we must "flip" the
956 * incomplete flags on the "new" and "old" attribute/value pairs
957 * so that one disappears and one appears atomically. Then we
958 * must remove the "old" attribute/value pair.
960 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
962 * In a separate transaction, set the incomplete flag on the
963 * "old" attr and clear the incomplete flag on the "new" attr.
965 error
= xfs_attr3_leaf_flipflags(args
);
970 * Dismantle the "old" attribute/value pair by removing
971 * a "remote" value (if it exists).
973 args
->index
= args
->index2
;
974 args
->blkno
= args
->blkno2
;
975 args
->rmtblkno
= args
->rmtblkno2
;
976 args
->rmtblkcnt
= args
->rmtblkcnt2
;
977 args
->rmtvaluelen
= args
->rmtvaluelen2
;
978 if (args
->rmtblkno
) {
979 error
= xfs_attr_rmtval_remove(args
);
985 * Re-find the "old" attribute entry after any split ops.
986 * The INCOMPLETE flag means that we will find the "old"
987 * attr, not the "new" one.
989 args
->flags
|= XFS_ATTR_INCOMPLETE
;
990 state
= xfs_da_state_alloc();
994 error
= xfs_da3_node_lookup_int(state
, &retval
);
999 * Remove the name and update the hashvals in the tree.
1001 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1002 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1003 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1004 xfs_da3_fixhashpath(state
, &state
->path
);
1007 * Check to see if the tree needs to be collapsed.
1009 if (retval
&& (state
->path
.active
> 1)) {
1010 error
= xfs_da3_join(state
);
1013 error
= xfs_defer_finish(&args
->trans
);
1019 * Commit and start the next trans in the chain.
1021 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1025 } else if (args
->rmtblkno
> 0) {
1027 * Added a "remote" value, just clear the incomplete flag.
1029 error
= xfs_attr3_leaf_clearflag(args
);
1037 xfs_da_state_free(state
);
1044 * Remove a name from a B-tree attribute list.
1046 * This will involve walking down the Btree, and may involve joining
1047 * leaf nodes and even joining intermediate nodes up to and including
1048 * the root node (a special case of an intermediate node).
1051 xfs_attr_node_removename(
1052 struct xfs_da_args
*args
)
1054 struct xfs_da_state
*state
;
1055 struct xfs_da_state_blk
*blk
;
1056 struct xfs_inode
*dp
;
1058 int retval
, error
, forkoff
;
1060 trace_xfs_attr_node_removename(args
);
1063 * Tie a string around our finger to remind us where we are.
1066 state
= xfs_da_state_alloc();
1068 state
->mp
= dp
->i_mount
;
1071 * Search to see if name exists, and get back a pointer to it.
1073 error
= xfs_da3_node_lookup_int(state
, &retval
);
1074 if (error
|| (retval
!= -EEXIST
)) {
1081 * If there is an out-of-line value, de-allocate the blocks.
1082 * This is done before we remove the attribute so that we don't
1083 * overflow the maximum size of a transaction and/or hit a deadlock.
1085 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1086 ASSERT(blk
->bp
!= NULL
);
1087 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1088 if (args
->rmtblkno
> 0) {
1090 * Fill in disk block numbers in the state structure
1091 * so that we can get the buffers back after we commit
1092 * several transactions in the following calls.
1094 error
= xfs_attr_fillstate(state
);
1099 * Mark the attribute as INCOMPLETE, then bunmapi() the
1102 error
= xfs_attr3_leaf_setflag(args
);
1105 error
= xfs_attr_rmtval_remove(args
);
1110 * Refill the state structure with buffers, the prior calls
1111 * released our buffers.
1113 error
= xfs_attr_refillstate(state
);
1119 * Remove the name and update the hashvals in the tree.
1121 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1122 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1123 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1124 xfs_da3_fixhashpath(state
, &state
->path
);
1127 * Check to see if the tree needs to be collapsed.
1129 if (retval
&& (state
->path
.active
> 1)) {
1130 error
= xfs_da3_join(state
);
1133 error
= xfs_defer_finish(&args
->trans
);
1137 * Commit the Btree join operation and start a new trans.
1139 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1145 * If the result is small enough, push it all into the inode.
1147 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1149 * Have to get rid of the copy of this dabuf in the state.
1151 ASSERT(state
->path
.active
== 1);
1152 ASSERT(state
->path
.blk
[0].bp
);
1153 state
->path
.blk
[0].bp
= NULL
;
1155 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1159 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1160 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1161 /* bp is gone due to xfs_da_shrink_inode */
1164 error
= xfs_defer_finish(&args
->trans
);
1168 xfs_trans_brelse(args
->trans
, bp
);
1173 xfs_da_state_free(state
);
1178 * Fill in the disk block numbers in the state structure for the buffers
1179 * that are attached to the state structure.
1180 * This is done so that we can quickly reattach ourselves to those buffers
1181 * after some set of transaction commits have released these buffers.
1184 xfs_attr_fillstate(xfs_da_state_t
*state
)
1186 xfs_da_state_path_t
*path
;
1187 xfs_da_state_blk_t
*blk
;
1190 trace_xfs_attr_fillstate(state
->args
);
1193 * Roll down the "path" in the state structure, storing the on-disk
1194 * block number for those buffers in the "path".
1196 path
= &state
->path
;
1197 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1198 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1200 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1203 blk
->disk_blkno
= 0;
1208 * Roll down the "altpath" in the state structure, storing the on-disk
1209 * block number for those buffers in the "altpath".
1211 path
= &state
->altpath
;
1212 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1213 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1215 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1218 blk
->disk_blkno
= 0;
1226 * Reattach the buffers to the state structure based on the disk block
1227 * numbers stored in the state structure.
1228 * This is done after some set of transaction commits have released those
1229 * buffers from our grip.
1232 xfs_attr_refillstate(xfs_da_state_t
*state
)
1234 xfs_da_state_path_t
*path
;
1235 xfs_da_state_blk_t
*blk
;
1238 trace_xfs_attr_refillstate(state
->args
);
1241 * Roll down the "path" in the state structure, storing the on-disk
1242 * block number for those buffers in the "path".
1244 path
= &state
->path
;
1245 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1246 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1247 if (blk
->disk_blkno
) {
1248 error
= xfs_da3_node_read(state
->args
->trans
,
1250 blk
->blkno
, blk
->disk_blkno
,
1251 &blk
->bp
, XFS_ATTR_FORK
);
1260 * Roll down the "altpath" in the state structure, storing the on-disk
1261 * block number for those buffers in the "altpath".
1263 path
= &state
->altpath
;
1264 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1265 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1266 if (blk
->disk_blkno
) {
1267 error
= xfs_da3_node_read(state
->args
->trans
,
1269 blk
->blkno
, blk
->disk_blkno
,
1270 &blk
->bp
, XFS_ATTR_FORK
);
1282 * Look up a filename in a node attribute list.
1284 * This routine gets called for any attribute fork that has more than one
1285 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1286 * "remote" values taking up more blocks.
1289 xfs_attr_node_get(xfs_da_args_t
*args
)
1291 xfs_da_state_t
*state
;
1292 xfs_da_state_blk_t
*blk
;
1296 trace_xfs_attr_node_get(args
);
1298 state
= xfs_da_state_alloc();
1300 state
->mp
= args
->dp
->i_mount
;
1303 * Search to see if name exists, and get back a pointer to it.
1305 error
= xfs_da3_node_lookup_int(state
, &retval
);
1308 } else if (retval
== -EEXIST
) {
1309 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1310 ASSERT(blk
->bp
!= NULL
);
1311 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1314 * Get the value, local or "remote"
1316 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1317 if (!retval
&& (args
->rmtblkno
> 0)
1318 && !(args
->flags
& ATTR_KERNOVAL
)) {
1319 retval
= xfs_attr_rmtval_get(args
);
1324 * If not in a transaction, we have to release all the buffers.
1326 for (i
= 0; i
< state
->path
.active
; i
++) {
1327 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1328 state
->path
.blk
[i
].bp
= NULL
;
1331 xfs_da_state_free(state
);