]>
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"
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_trans_space.h"
25 #include "xfs_trace.h"
30 * Provide the external interfaces to manage attribute lists.
33 /*========================================================================
34 * Function prototypes for the kernel.
35 *========================================================================*/
38 * Internal routines when attribute list fits inside the inode.
40 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
43 * Internal routines when attribute list is one block.
45 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
46 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
47 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
50 * Internal routines when attribute list is more than one block.
52 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
53 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
54 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
55 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
56 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
61 struct xfs_da_args
*args
,
63 const unsigned char *name
,
70 memset(args
, 0, sizeof(*args
));
71 args
->geo
= dp
->i_mount
->m_attr_geo
;
72 args
->whichfork
= XFS_ATTR_FORK
;
76 args
->namelen
= strlen((const char *)name
);
77 if (args
->namelen
>= MAXNAMELEN
)
78 return -EFAULT
; /* match IRIX behaviour */
80 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
88 if (!XFS_IFORK_Q(ip
) ||
89 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
90 ip
->i_d
.di_anextents
== 0))
95 /*========================================================================
96 * Overall external interface routines.
97 *========================================================================*/
99 /* Retrieve an extended attribute and its value. Must have ilock. */
101 xfs_attr_get_ilocked(
102 struct xfs_inode
*ip
,
103 struct xfs_da_args
*args
)
105 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
));
107 if (!xfs_inode_hasattr(ip
))
109 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
110 return xfs_attr_shortform_getvalue(args
);
111 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
112 return xfs_attr_leaf_get(args
);
114 return xfs_attr_node_get(args
);
117 /* Retrieve an extended attribute by name, and its value. */
120 struct xfs_inode
*ip
,
121 const unsigned char *name
,
122 unsigned char *value
,
126 struct xfs_da_args args
;
130 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
132 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
135 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
140 args
.valuelen
= *valuelenp
;
141 /* Entirely possible to look up a name which doesn't exist */
142 args
.op_flags
= XFS_DA_OP_OKNOENT
;
144 lock_mode
= xfs_ilock_attr_map_shared(ip
);
145 error
= xfs_attr_get_ilocked(ip
, &args
);
146 xfs_iunlock(ip
, lock_mode
);
148 *valuelenp
= args
.valuelen
;
149 return error
== -EEXIST
? 0 : error
;
153 * Calculate how many blocks we need for the new attribute,
157 struct xfs_da_args
*args
,
160 struct xfs_mount
*mp
= args
->dp
->i_mount
;
165 * Determine space new attribute will use, and if it would be
166 * "local" or "remote" (note: local != inline).
168 size
= xfs_attr_leaf_newentsize(args
, local
);
169 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
171 if (size
> (args
->geo
->blksize
/ 2)) {
172 /* Double split possible */
177 * Out of line attribute, cannot double split, but
178 * make room for the attribute value itself.
180 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
182 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
189 xfs_attr_try_sf_addname(
190 struct xfs_inode
*dp
,
191 struct xfs_da_args
*args
)
194 struct xfs_mount
*mp
= dp
->i_mount
;
197 error
= xfs_attr_shortform_addname(args
);
198 if (error
== -ENOSPC
)
202 * Commit the shortform mods, and we're done.
203 * NOTE: this is also the error path (EEXIST, etc).
205 if (!error
&& (args
->flags
& ATTR_KERNOTIME
) == 0)
206 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
208 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
209 xfs_trans_set_sync(args
->trans
);
211 error2
= xfs_trans_commit(args
->trans
);
213 return error
? error
: error2
;
217 * Set the attribute specified in @args.
221 struct xfs_da_args
*args
)
223 struct xfs_inode
*dp
= args
->dp
;
224 struct xfs_buf
*leaf_bp
= NULL
;
228 * If the attribute list is non-existent or a shortform list,
229 * upgrade it to a single-leaf-block attribute list.
231 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
232 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
233 dp
->i_d
.di_anextents
== 0)) {
236 * Build initial attribute list (if required).
238 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
239 xfs_attr_shortform_create(args
);
242 * Try to add the attr to the attribute list in the inode.
244 error
= xfs_attr_try_sf_addname(dp
, args
);
245 if (error
!= -ENOSPC
)
249 * It won't fit in the shortform, transform to a leaf block.
250 * GROT: another possible req'mt for a double-split btree op.
252 error
= xfs_attr_shortform_to_leaf(args
, &leaf_bp
);
257 * Prevent the leaf buffer from being unlocked so that a
258 * concurrent AIL push cannot grab the half-baked leaf
259 * buffer and run into problems with the write verifier.
260 * Once we're done rolling the transaction we can release
261 * the hold and add the attr to the leaf.
263 xfs_trans_bhold(args
->trans
, leaf_bp
);
264 error
= xfs_defer_finish(&args
->trans
);
265 xfs_trans_bhold_release(args
->trans
, leaf_bp
);
267 xfs_trans_brelse(args
->trans
, leaf_bp
);
272 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
273 error
= xfs_attr_leaf_addname(args
);
275 error
= xfs_attr_node_addname(args
);
280 * Remove the attribute specified in @args.
283 xfs_attr_remove_args(
284 struct xfs_da_args
*args
)
286 struct xfs_inode
*dp
= args
->dp
;
289 if (!xfs_inode_hasattr(dp
)) {
291 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
292 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
293 error
= xfs_attr_shortform_remove(args
);
294 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
295 error
= xfs_attr_leaf_removename(args
);
297 error
= xfs_attr_node_removename(args
);
305 struct xfs_inode
*dp
,
306 const unsigned char *name
,
307 unsigned char *value
,
311 struct xfs_mount
*mp
= dp
->i_mount
;
312 struct xfs_da_args args
;
313 struct xfs_trans_res tres
;
314 int rsvd
= (flags
& ATTR_ROOT
) != 0;
317 XFS_STATS_INC(mp
, xs_attr_set
);
319 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
322 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
327 args
.valuelen
= valuelen
;
328 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
329 args
.total
= xfs_attr_calc_size(&args
, &local
);
331 error
= xfs_qm_dqattach(dp
);
336 * If the inode doesn't have an attribute fork, add one.
337 * (inode must not be locked when we call this routine)
339 if (XFS_IFORK_Q(dp
) == 0) {
340 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
341 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
343 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
348 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
349 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
350 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
351 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
354 * Root fork attributes can use reserved data blocks for this
355 * operation if necessary
357 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
358 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
362 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
363 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
364 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
365 XFS_QMOPT_RES_REGBLKS
);
367 goto out_trans_cancel
;
369 xfs_trans_ijoin(args
.trans
, dp
, 0);
370 error
= xfs_attr_set_args(&args
);
372 goto out_trans_cancel
;
374 /* shortform attribute has already been committed */
379 * If this is a synchronous mount, make sure that the
380 * transaction goes to disk before returning to the user.
382 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
383 xfs_trans_set_sync(args
.trans
);
385 if ((flags
& ATTR_KERNOTIME
) == 0)
386 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
389 * Commit the last in the sequence of transactions.
391 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
392 error
= xfs_trans_commit(args
.trans
);
394 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
399 xfs_trans_cancel(args
.trans
);
404 * Generic handler routine to remove a name from an attribute list.
405 * Transitions attribute list from Btree to shortform as necessary.
409 struct xfs_inode
*dp
,
410 const unsigned char *name
,
413 struct xfs_mount
*mp
= dp
->i_mount
;
414 struct xfs_da_args args
;
417 XFS_STATS_INC(mp
, xs_attr_remove
);
419 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
422 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
427 * we have no control over the attribute names that userspace passes us
428 * to remove, so we have to allow the name lookup prior to attribute
431 args
.op_flags
= XFS_DA_OP_OKNOENT
;
433 error
= xfs_qm_dqattach(dp
);
438 * Root fork attributes can use reserved data blocks for this
439 * operation if necessary
441 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
442 XFS_ATTRRM_SPACE_RES(mp
), 0,
443 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
448 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
450 * No need to make quota reservations here. We expect to release some
451 * blocks not allocate in the common case.
453 xfs_trans_ijoin(args
.trans
, dp
, 0);
455 error
= xfs_attr_remove_args(&args
);
460 * If this is a synchronous mount, make sure that the
461 * transaction goes to disk before returning to the user.
463 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
464 xfs_trans_set_sync(args
.trans
);
466 if ((flags
& ATTR_KERNOTIME
) == 0)
467 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
470 * Commit the last in the sequence of transactions.
472 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
473 error
= xfs_trans_commit(args
.trans
);
474 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
480 xfs_trans_cancel(args
.trans
);
481 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
485 /*========================================================================
486 * External routines when attribute list is inside the inode
487 *========================================================================*/
490 * Add a name to the shortform attribute list structure
491 * This is the external routine.
494 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
496 int newsize
, forkoff
, retval
;
498 trace_xfs_attr_sf_addname(args
);
500 retval
= xfs_attr_shortform_lookup(args
);
501 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
503 } else if (retval
== -EEXIST
) {
504 if (args
->flags
& ATTR_CREATE
)
506 retval
= xfs_attr_shortform_remove(args
);
510 * Since we have removed the old attr, clear ATTR_REPLACE so
511 * that the leaf format add routine won't trip over the attr
514 args
->flags
&= ~ATTR_REPLACE
;
517 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
518 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
521 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
522 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
524 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
528 xfs_attr_shortform_add(args
, forkoff
);
533 /*========================================================================
534 * External routines when attribute list is one block
535 *========================================================================*/
538 * Add a name to the leaf attribute list structure
540 * This leaf block cannot have a "remote" value, we only call this routine
541 * if bmap_one_block() says there is only one block (ie: no remote blks).
544 xfs_attr_leaf_addname(
545 struct xfs_da_args
*args
)
547 struct xfs_inode
*dp
;
549 int retval
, error
, forkoff
;
551 trace_xfs_attr_leaf_addname(args
);
554 * Read the (only) block in the attribute list in.
558 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
563 * Look up the given attribute in the leaf block. Figure out if
564 * the given flags produce an error or call for an atomic rename.
566 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
567 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
568 xfs_trans_brelse(args
->trans
, bp
);
570 } else if (retval
== -EEXIST
) {
571 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
572 xfs_trans_brelse(args
->trans
, bp
);
576 trace_xfs_attr_leaf_replace(args
);
578 /* save the attribute state for later removal*/
579 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
580 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
581 args
->index2
= args
->index
;
582 args
->rmtblkno2
= args
->rmtblkno
;
583 args
->rmtblkcnt2
= args
->rmtblkcnt
;
584 args
->rmtvaluelen2
= args
->rmtvaluelen
;
587 * clear the remote attr state now that it is saved so that the
588 * values reflect the state of the attribute we are about to
589 * add, not the attribute we just found and will remove later.
593 args
->rmtvaluelen
= 0;
597 * Add the attribute to the leaf block, transitioning to a Btree
600 retval
= xfs_attr3_leaf_add(bp
, args
);
601 if (retval
== -ENOSPC
) {
603 * Promote the attribute list to the Btree format, then
604 * Commit that transaction so that the node_addname() call
605 * can manage its own transactions.
607 error
= xfs_attr3_leaf_to_node(args
);
610 error
= xfs_defer_finish(&args
->trans
);
615 * Commit the current trans (including the inode) and start
618 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
623 * Fob the whole rest of the problem off on the Btree code.
625 error
= xfs_attr_node_addname(args
);
630 * Commit the transaction that added the attr name so that
631 * later routines can manage their own transactions.
633 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
638 * If there was an out-of-line value, allocate the blocks we
639 * identified for its storage and copy the value. This is done
640 * after we create the attribute so that we don't overflow the
641 * maximum size of a transaction and/or hit a deadlock.
643 if (args
->rmtblkno
> 0) {
644 error
= xfs_attr_rmtval_set(args
);
650 * If this is an atomic rename operation, we must "flip" the
651 * incomplete flags on the "new" and "old" attribute/value pairs
652 * so that one disappears and one appears atomically. Then we
653 * must remove the "old" attribute/value pair.
655 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
657 * In a separate transaction, set the incomplete flag on the
658 * "old" attr and clear the incomplete flag on the "new" attr.
660 error
= xfs_attr3_leaf_flipflags(args
);
665 * Dismantle the "old" attribute/value pair by removing
666 * a "remote" value (if it exists).
668 args
->index
= args
->index2
;
669 args
->blkno
= args
->blkno2
;
670 args
->rmtblkno
= args
->rmtblkno2
;
671 args
->rmtblkcnt
= args
->rmtblkcnt2
;
672 args
->rmtvaluelen
= args
->rmtvaluelen2
;
673 if (args
->rmtblkno
) {
674 error
= xfs_attr_rmtval_remove(args
);
680 * Read in the block containing the "old" attr, then
681 * remove the "old" attr from that block (neat, huh!)
683 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
688 xfs_attr3_leaf_remove(bp
, args
);
691 * If the result is small enough, shrink it all into the inode.
693 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
694 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
695 /* bp is gone due to xfs_da_shrink_inode */
698 error
= xfs_defer_finish(&args
->trans
);
704 * Commit the remove and start the next trans in series.
706 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
708 } else if (args
->rmtblkno
> 0) {
710 * Added a "remote" value, just clear the incomplete flag.
712 error
= xfs_attr3_leaf_clearflag(args
);
718 * Remove a name from the leaf attribute list structure
720 * This leaf block cannot have a "remote" value, we only call this routine
721 * if bmap_one_block() says there is only one block (ie: no remote blks).
724 xfs_attr_leaf_removename(
725 struct xfs_da_args
*args
)
727 struct xfs_inode
*dp
;
731 trace_xfs_attr_leaf_removename(args
);
734 * Remove the attribute.
738 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
742 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
743 if (error
== -ENOATTR
) {
744 xfs_trans_brelse(args
->trans
, bp
);
748 xfs_attr3_leaf_remove(bp
, args
);
751 * If the result is small enough, shrink it all into the inode.
753 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
754 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
755 /* bp is gone due to xfs_da_shrink_inode */
758 error
= xfs_defer_finish(&args
->trans
);
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(
813 struct xfs_da_args
*args
)
815 struct xfs_da_state
*state
;
816 struct xfs_da_state_blk
*blk
;
817 struct xfs_inode
*dp
;
818 struct xfs_mount
*mp
;
821 trace_xfs_attr_node_addname(args
);
824 * Fill in bucket of arguments/results/context to carry around.
829 state
= xfs_da_state_alloc();
834 * Search to see if name already exists, and get back a pointer
835 * to where it should go.
837 error
= xfs_da3_node_lookup_int(state
, &retval
);
840 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
841 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
842 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
844 } else if (retval
== -EEXIST
) {
845 if (args
->flags
& ATTR_CREATE
)
848 trace_xfs_attr_node_replace(args
);
850 /* save the attribute state for later removal*/
851 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
852 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
853 args
->index2
= args
->index
;
854 args
->rmtblkno2
= args
->rmtblkno
;
855 args
->rmtblkcnt2
= args
->rmtblkcnt
;
856 args
->rmtvaluelen2
= args
->rmtvaluelen
;
859 * clear the remote attr state now that it is saved so that the
860 * values reflect the state of the attribute we are about to
861 * add, not the attribute we just found and will remove later.
865 args
->rmtvaluelen
= 0;
868 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
869 if (retval
== -ENOSPC
) {
870 if (state
->path
.active
== 1) {
872 * Its really a single leaf node, but it had
873 * out-of-line values so it looked like it *might*
874 * have been a b-tree.
876 xfs_da_state_free(state
);
878 error
= xfs_attr3_leaf_to_node(args
);
881 error
= xfs_defer_finish(&args
->trans
);
886 * Commit the node conversion and start the next
887 * trans in the chain.
889 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
897 * Split as many Btree elements as required.
898 * This code tracks the new and old attr's location
899 * in the index/blkno/rmtblkno/rmtblkcnt fields and
900 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
902 error
= xfs_da3_split(state
);
905 error
= xfs_defer_finish(&args
->trans
);
910 * Addition succeeded, update Btree hashvals.
912 xfs_da3_fixhashpath(state
, &state
->path
);
916 * Kill the state structure, we're done with it and need to
917 * allow the buffers to come back later.
919 xfs_da_state_free(state
);
923 * Commit the leaf addition or btree split and start the next
924 * trans in the chain.
926 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
931 * If there was an out-of-line value, allocate the blocks we
932 * identified for its storage and copy the value. This is done
933 * after we create the attribute so that we don't overflow the
934 * maximum size of a transaction and/or hit a deadlock.
936 if (args
->rmtblkno
> 0) {
937 error
= xfs_attr_rmtval_set(args
);
943 * If this is an atomic rename operation, we must "flip" the
944 * incomplete flags on the "new" and "old" attribute/value pairs
945 * so that one disappears and one appears atomically. Then we
946 * must remove the "old" attribute/value pair.
948 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
950 * In a separate transaction, set the incomplete flag on the
951 * "old" attr and clear the incomplete flag on the "new" attr.
953 error
= xfs_attr3_leaf_flipflags(args
);
958 * Dismantle the "old" attribute/value pair by removing
959 * a "remote" value (if it exists).
961 args
->index
= args
->index2
;
962 args
->blkno
= args
->blkno2
;
963 args
->rmtblkno
= args
->rmtblkno2
;
964 args
->rmtblkcnt
= args
->rmtblkcnt2
;
965 args
->rmtvaluelen
= args
->rmtvaluelen2
;
966 if (args
->rmtblkno
) {
967 error
= xfs_attr_rmtval_remove(args
);
973 * Re-find the "old" attribute entry after any split ops.
974 * The INCOMPLETE flag means that we will find the "old"
975 * attr, not the "new" one.
977 args
->flags
|= XFS_ATTR_INCOMPLETE
;
978 state
= xfs_da_state_alloc();
982 error
= xfs_da3_node_lookup_int(state
, &retval
);
987 * Remove the name and update the hashvals in the tree.
989 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
990 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
991 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
992 xfs_da3_fixhashpath(state
, &state
->path
);
995 * Check to see if the tree needs to be collapsed.
997 if (retval
&& (state
->path
.active
> 1)) {
998 error
= xfs_da3_join(state
);
1001 error
= xfs_defer_finish(&args
->trans
);
1007 * Commit and start the next trans in the chain.
1009 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1013 } else if (args
->rmtblkno
> 0) {
1015 * Added a "remote" value, just clear the incomplete flag.
1017 error
= xfs_attr3_leaf_clearflag(args
);
1025 xfs_da_state_free(state
);
1032 * Remove a name from a B-tree attribute list.
1034 * This will involve walking down the Btree, and may involve joining
1035 * leaf nodes and even joining intermediate nodes up to and including
1036 * the root node (a special case of an intermediate node).
1039 xfs_attr_node_removename(
1040 struct xfs_da_args
*args
)
1042 struct xfs_da_state
*state
;
1043 struct xfs_da_state_blk
*blk
;
1044 struct xfs_inode
*dp
;
1046 int retval
, error
, forkoff
;
1048 trace_xfs_attr_node_removename(args
);
1051 * Tie a string around our finger to remind us where we are.
1054 state
= xfs_da_state_alloc();
1056 state
->mp
= dp
->i_mount
;
1059 * Search to see if name exists, and get back a pointer to it.
1061 error
= xfs_da3_node_lookup_int(state
, &retval
);
1062 if (error
|| (retval
!= -EEXIST
)) {
1069 * If there is an out-of-line value, de-allocate the blocks.
1070 * This is done before we remove the attribute so that we don't
1071 * overflow the maximum size of a transaction and/or hit a deadlock.
1073 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1074 ASSERT(blk
->bp
!= NULL
);
1075 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1076 if (args
->rmtblkno
> 0) {
1078 * Fill in disk block numbers in the state structure
1079 * so that we can get the buffers back after we commit
1080 * several transactions in the following calls.
1082 error
= xfs_attr_fillstate(state
);
1087 * Mark the attribute as INCOMPLETE, then bunmapi() the
1090 error
= xfs_attr3_leaf_setflag(args
);
1093 error
= xfs_attr_rmtval_remove(args
);
1098 * Refill the state structure with buffers, the prior calls
1099 * released our buffers.
1101 error
= xfs_attr_refillstate(state
);
1107 * Remove the name and update the hashvals in the tree.
1109 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1110 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1111 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1112 xfs_da3_fixhashpath(state
, &state
->path
);
1115 * Check to see if the tree needs to be collapsed.
1117 if (retval
&& (state
->path
.active
> 1)) {
1118 error
= xfs_da3_join(state
);
1121 error
= xfs_defer_finish(&args
->trans
);
1125 * Commit the Btree join operation and start a new trans.
1127 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1133 * If the result is small enough, push it all into the inode.
1135 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1137 * Have to get rid of the copy of this dabuf in the state.
1139 ASSERT(state
->path
.active
== 1);
1140 ASSERT(state
->path
.blk
[0].bp
);
1141 state
->path
.blk
[0].bp
= NULL
;
1143 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1147 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1148 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1149 /* bp is gone due to xfs_da_shrink_inode */
1152 error
= xfs_defer_finish(&args
->trans
);
1156 xfs_trans_brelse(args
->trans
, bp
);
1161 xfs_da_state_free(state
);
1166 * Fill in the disk block numbers in the state structure for the buffers
1167 * that are attached to the state structure.
1168 * This is done so that we can quickly reattach ourselves to those buffers
1169 * after some set of transaction commits have released these buffers.
1172 xfs_attr_fillstate(xfs_da_state_t
*state
)
1174 xfs_da_state_path_t
*path
;
1175 xfs_da_state_blk_t
*blk
;
1178 trace_xfs_attr_fillstate(state
->args
);
1181 * Roll down the "path" in the state structure, storing the on-disk
1182 * block number for those buffers in the "path".
1184 path
= &state
->path
;
1185 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1186 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1188 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1191 blk
->disk_blkno
= 0;
1196 * Roll down the "altpath" in the state structure, storing the on-disk
1197 * block number for those buffers in the "altpath".
1199 path
= &state
->altpath
;
1200 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1201 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1203 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1206 blk
->disk_blkno
= 0;
1214 * Reattach the buffers to the state structure based on the disk block
1215 * numbers stored in the state structure.
1216 * This is done after some set of transaction commits have released those
1217 * buffers from our grip.
1220 xfs_attr_refillstate(xfs_da_state_t
*state
)
1222 xfs_da_state_path_t
*path
;
1223 xfs_da_state_blk_t
*blk
;
1226 trace_xfs_attr_refillstate(state
->args
);
1229 * Roll down the "path" in the state structure, storing the on-disk
1230 * block number for those buffers in the "path".
1232 path
= &state
->path
;
1233 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1234 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1235 if (blk
->disk_blkno
) {
1236 error
= xfs_da3_node_read(state
->args
->trans
,
1238 blk
->blkno
, blk
->disk_blkno
,
1239 &blk
->bp
, XFS_ATTR_FORK
);
1248 * Roll down the "altpath" in the state structure, storing the on-disk
1249 * block number for those buffers in the "altpath".
1251 path
= &state
->altpath
;
1252 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1253 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1254 if (blk
->disk_blkno
) {
1255 error
= xfs_da3_node_read(state
->args
->trans
,
1257 blk
->blkno
, blk
->disk_blkno
,
1258 &blk
->bp
, XFS_ATTR_FORK
);
1270 * Look up a filename in a node attribute list.
1272 * This routine gets called for any attribute fork that has more than one
1273 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1274 * "remote" values taking up more blocks.
1277 xfs_attr_node_get(xfs_da_args_t
*args
)
1279 xfs_da_state_t
*state
;
1280 xfs_da_state_blk_t
*blk
;
1284 trace_xfs_attr_node_get(args
);
1286 state
= xfs_da_state_alloc();
1288 state
->mp
= args
->dp
->i_mount
;
1291 * Search to see if name exists, and get back a pointer to it.
1293 error
= xfs_da3_node_lookup_int(state
, &retval
);
1296 } else if (retval
== -EEXIST
) {
1297 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1298 ASSERT(blk
->bp
!= NULL
);
1299 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1302 * Get the value, local or "remote"
1304 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1305 if (!retval
&& (args
->rmtblkno
> 0)
1306 && !(args
->flags
& ATTR_KERNOVAL
)) {
1307 retval
= xfs_attr_rmtval_get(args
);
1312 * If not in a transaction, we have to release all the buffers.
1314 for (i
= 0; i
< state
->path
.active
; i
++) {
1315 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1316 state
->path
.blk
[i
].bp
= NULL
;
1319 xfs_da_state_free(state
);
1323 /* Returns true if the attribute entry name is valid. */
1330 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1331 * out, so use >= for the length check.
1333 if (length
>= MAXNAMELEN
)
1336 /* There shouldn't be any nulls here */
1337 return !memchr(name
, 0, length
);