]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "libxfs_priv.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_attr_leaf.h"
36 #include "xfs_attr_remote.h"
37 #include "xfs_trans_space.h"
38 #include "xfs_trace.h"
43 * Provide the external interfaces to manage attribute lists.
46 /*========================================================================
47 * Function prototypes for the kernel.
48 *========================================================================*/
51 * Internal routines when attribute list fits inside the inode.
53 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
56 * Internal routines when attribute list is one block.
58 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
59 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
60 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
63 * Internal routines when attribute list is more than one block.
65 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
66 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
67 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
68 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
69 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
74 struct xfs_da_args
*args
,
76 const unsigned char *name
,
83 memset(args
, 0, sizeof(*args
));
84 args
->geo
= dp
->i_mount
->m_attr_geo
;
85 args
->whichfork
= XFS_ATTR_FORK
;
89 args
->namelen
= strlen((const char *)name
);
90 if (args
->namelen
>= MAXNAMELEN
)
91 return -EFAULT
; /* match IRIX behaviour */
93 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
101 if (!XFS_IFORK_Q(ip
) ||
102 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
103 ip
->i_d
.di_anextents
== 0))
108 /*========================================================================
109 * Overall external interface routines.
110 *========================================================================*/
114 struct xfs_inode
*ip
,
115 const unsigned char *name
,
116 unsigned char *value
,
120 struct xfs_da_args args
;
124 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
126 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
129 if (!xfs_inode_hasattr(ip
))
132 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
137 args
.valuelen
= *valuelenp
;
139 lock_mode
= xfs_ilock_attr_map_shared(ip
);
140 if (!xfs_inode_hasattr(ip
))
142 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
143 error
= xfs_attr_shortform_getvalue(&args
);
144 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
145 error
= xfs_attr_leaf_get(&args
);
147 error
= xfs_attr_node_get(&args
);
148 xfs_iunlock(ip
, lock_mode
);
150 *valuelenp
= args
.valuelen
;
151 return error
== -EEXIST
? 0 : error
;
155 * Calculate how many blocks we need for the new attribute,
159 struct xfs_da_args
*args
,
162 struct xfs_mount
*mp
= args
->dp
->i_mount
;
167 * Determine space new attribute will use, and if it would be
168 * "local" or "remote" (note: local != inline).
170 size
= xfs_attr_leaf_newentsize(args
, local
);
171 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
173 if (size
> (args
->geo
->blksize
/ 2)) {
174 /* Double split possible */
179 * Out of line attribute, cannot double split, but
180 * make room for the attribute value itself.
182 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
184 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
192 struct xfs_inode
*dp
,
193 const unsigned char *name
,
194 unsigned char *value
,
198 struct xfs_mount
*mp
= dp
->i_mount
;
199 struct xfs_da_args args
;
200 struct xfs_defer_ops flist
;
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
, 0);
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
.flist
, args
.firstblock
);
314 error
= xfs_attr_shortform_to_leaf(&args
);
316 error
= xfs_defer_finish(&args
.trans
, args
.flist
, dp
);
319 xfs_defer_cancel(&flist
);
324 * Commit the leaf transformation. We'll need another (linked)
325 * transaction to add the new attribute to the leaf.
328 error
= xfs_trans_roll(&args
.trans
, dp
);
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_cancel(args
.trans
);
363 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
368 * Generic handler routine to remove a name from an attribute list.
369 * Transitions attribute list from Btree to shortform as necessary.
373 struct xfs_inode
*dp
,
374 const unsigned char *name
,
377 struct xfs_mount
*mp
= dp
->i_mount
;
378 struct xfs_da_args args
;
379 struct xfs_defer_ops flist
;
380 xfs_fsblock_t firstblock
;
383 XFS_STATS_INC(mp
, xs_attr_remove
);
385 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
388 if (!xfs_inode_hasattr(dp
))
391 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
395 args
.firstblock
= &firstblock
;
399 * we have no control over the attribute names that userspace passes us
400 * to remove, so we have to allow the name lookup prior to attribute
403 args
.op_flags
= XFS_DA_OP_OKNOENT
;
405 error
= xfs_qm_dqattach(dp
, 0);
410 * Root fork attributes can use reserved data blocks for this
411 * operation if necessary
413 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
414 XFS_ATTRRM_SPACE_RES(mp
), 0,
415 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
420 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
422 * No need to make quota reservations here. We expect to release some
423 * blocks not allocate in the common case.
425 xfs_trans_ijoin(args
.trans
, dp
, 0);
427 if (!xfs_inode_hasattr(dp
)) {
429 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
430 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
431 error
= xfs_attr_shortform_remove(&args
);
432 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
433 error
= xfs_attr_leaf_removename(&args
);
435 error
= xfs_attr_node_removename(&args
);
442 * If this is a synchronous mount, make sure that the
443 * transaction goes to disk before returning to the user.
445 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
446 xfs_trans_set_sync(args
.trans
);
448 if ((flags
& ATTR_KERNOTIME
) == 0)
449 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
452 * Commit the last in the sequence of transactions.
454 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
455 error
= xfs_trans_commit(args
.trans
);
456 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
462 xfs_trans_cancel(args
.trans
);
463 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
467 /*========================================================================
468 * External routines when attribute list is inside the inode
469 *========================================================================*/
472 * Add a name to the shortform attribute list structure
473 * This is the external routine.
476 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
478 int newsize
, forkoff
, retval
;
480 trace_xfs_attr_sf_addname(args
);
482 retval
= xfs_attr_shortform_lookup(args
);
483 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
485 } else if (retval
== -EEXIST
) {
486 if (args
->flags
& ATTR_CREATE
)
488 retval
= xfs_attr_shortform_remove(args
);
492 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
493 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
496 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
497 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
499 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
503 xfs_attr_shortform_add(args
, forkoff
);
508 /*========================================================================
509 * External routines when attribute list is one block
510 *========================================================================*/
513 * Add a name to the leaf attribute list structure
515 * This leaf block cannot have a "remote" value, we only call this routine
516 * if bmap_one_block() says there is only one block (ie: no remote blks).
519 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
523 int retval
, error
, forkoff
;
525 trace_xfs_attr_leaf_addname(args
);
528 * Read the (only) block in the attribute list in.
532 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
537 * Look up the given attribute in the leaf block. Figure out if
538 * the given flags produce an error or call for an atomic rename.
540 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
541 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
542 xfs_trans_brelse(args
->trans
, bp
);
544 } else if (retval
== -EEXIST
) {
545 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
546 xfs_trans_brelse(args
->trans
, bp
);
550 trace_xfs_attr_leaf_replace(args
);
552 /* save the attribute state for later removal*/
553 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
554 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
555 args
->index2
= args
->index
;
556 args
->rmtblkno2
= args
->rmtblkno
;
557 args
->rmtblkcnt2
= args
->rmtblkcnt
;
558 args
->rmtvaluelen2
= args
->rmtvaluelen
;
561 * clear the remote attr state now that it is saved so that the
562 * values reflect the state of the attribute we are about to
563 * add, not the attribute we just found and will remove later.
567 args
->rmtvaluelen
= 0;
571 * Add the attribute to the leaf block, transitioning to a Btree
574 retval
= xfs_attr3_leaf_add(bp
, args
);
575 if (retval
== -ENOSPC
) {
577 * Promote the attribute list to the Btree format, then
578 * Commit that transaction so that the node_addname() call
579 * can manage its own transactions.
581 xfs_defer_init(args
->flist
, args
->firstblock
);
582 error
= xfs_attr3_leaf_to_node(args
);
584 error
= xfs_defer_finish(&args
->trans
, args
->flist
, dp
);
587 xfs_defer_cancel(args
->flist
);
592 * Commit the current trans (including the inode) and start
595 error
= xfs_trans_roll(&args
->trans
, dp
);
600 * Fob the whole rest of the problem off on the Btree code.
602 error
= xfs_attr_node_addname(args
);
607 * Commit the transaction that added the attr name so that
608 * later routines can manage their own transactions.
610 error
= xfs_trans_roll(&args
->trans
, dp
);
615 * If there was an out-of-line value, allocate the blocks we
616 * identified for its storage and copy the value. This is done
617 * after we create the attribute so that we don't overflow the
618 * maximum size of a transaction and/or hit a deadlock.
620 if (args
->rmtblkno
> 0) {
621 error
= xfs_attr_rmtval_set(args
);
627 * If this is an atomic rename operation, we must "flip" the
628 * incomplete flags on the "new" and "old" attribute/value pairs
629 * so that one disappears and one appears atomically. Then we
630 * must remove the "old" attribute/value pair.
632 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
634 * In a separate transaction, set the incomplete flag on the
635 * "old" attr and clear the incomplete flag on the "new" attr.
637 error
= xfs_attr3_leaf_flipflags(args
);
642 * Dismantle the "old" attribute/value pair by removing
643 * a "remote" value (if it exists).
645 args
->index
= args
->index2
;
646 args
->blkno
= args
->blkno2
;
647 args
->rmtblkno
= args
->rmtblkno2
;
648 args
->rmtblkcnt
= args
->rmtblkcnt2
;
649 args
->rmtvaluelen
= args
->rmtvaluelen2
;
650 if (args
->rmtblkno
) {
651 error
= xfs_attr_rmtval_remove(args
);
657 * Read in the block containing the "old" attr, then
658 * remove the "old" attr from that block (neat, huh!)
660 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
665 xfs_attr3_leaf_remove(bp
, args
);
668 * If the result is small enough, shrink it all into the inode.
670 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
671 xfs_defer_init(args
->flist
, args
->firstblock
);
672 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
673 /* bp is gone due to xfs_da_shrink_inode */
675 error
= xfs_defer_finish(&args
->trans
,
679 xfs_defer_cancel(args
->flist
);
685 * Commit the remove and start the next trans in series.
687 error
= xfs_trans_roll(&args
->trans
, dp
);
689 } else if (args
->rmtblkno
> 0) {
691 * Added a "remote" value, just clear the incomplete flag.
693 error
= xfs_attr3_leaf_clearflag(args
);
699 * Remove a name from the leaf attribute list structure
701 * This leaf block cannot have a "remote" value, we only call this routine
702 * if bmap_one_block() says there is only one block (ie: no remote blks).
705 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
711 trace_xfs_attr_leaf_removename(args
);
714 * Remove the attribute.
718 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
722 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
723 if (error
== -ENOATTR
) {
724 xfs_trans_brelse(args
->trans
, bp
);
728 xfs_attr3_leaf_remove(bp
, args
);
731 * If the result is small enough, shrink it all into the inode.
733 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
734 xfs_defer_init(args
->flist
, args
->firstblock
);
735 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
736 /* bp is gone due to xfs_da_shrink_inode */
738 error
= xfs_defer_finish(&args
->trans
, args
->flist
, dp
);
741 xfs_defer_cancel(args
->flist
);
749 * Look up a name in a leaf attribute list structure.
751 * This leaf block cannot have a "remote" value, we only call this routine
752 * if bmap_one_block() says there is only one block (ie: no remote blks).
755 xfs_attr_leaf_get(xfs_da_args_t
*args
)
760 trace_xfs_attr_leaf_get(args
);
763 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
767 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
768 if (error
!= -EEXIST
) {
769 xfs_trans_brelse(args
->trans
, bp
);
772 error
= xfs_attr3_leaf_getvalue(bp
, args
);
773 xfs_trans_brelse(args
->trans
, bp
);
774 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
775 error
= xfs_attr_rmtval_get(args
);
780 /*========================================================================
781 * External routines when attribute list size > geo->blksize
782 *========================================================================*/
785 * Add a name to a Btree-format attribute list.
787 * This will involve walking down the Btree, and may involve splitting
788 * leaf nodes and even splitting intermediate nodes up to and including
789 * the root node (a special case of an intermediate node).
791 * "Remote" attribute values confuse the issue and atomic rename operations
792 * add a whole extra layer of confusion on top of that.
795 xfs_attr_node_addname(xfs_da_args_t
*args
)
797 xfs_da_state_t
*state
;
798 xfs_da_state_blk_t
*blk
;
803 trace_xfs_attr_node_addname(args
);
806 * Fill in bucket of arguments/results/context to carry around.
811 state
= xfs_da_state_alloc();
816 * Search to see if name already exists, and get back a pointer
817 * to where it should go.
819 error
= xfs_da3_node_lookup_int(state
, &retval
);
822 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
823 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
824 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
826 } else if (retval
== -EEXIST
) {
827 if (args
->flags
& ATTR_CREATE
)
830 trace_xfs_attr_node_replace(args
);
832 /* save the attribute state for later removal*/
833 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
834 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
835 args
->index2
= args
->index
;
836 args
->rmtblkno2
= args
->rmtblkno
;
837 args
->rmtblkcnt2
= args
->rmtblkcnt
;
838 args
->rmtvaluelen2
= args
->rmtvaluelen
;
841 * clear the remote attr state now that it is saved so that the
842 * values reflect the state of the attribute we are about to
843 * add, not the attribute we just found and will remove later.
847 args
->rmtvaluelen
= 0;
850 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
851 if (retval
== -ENOSPC
) {
852 if (state
->path
.active
== 1) {
854 * Its really a single leaf node, but it had
855 * out-of-line values so it looked like it *might*
856 * have been a b-tree.
858 xfs_da_state_free(state
);
860 xfs_defer_init(args
->flist
, args
->firstblock
);
861 error
= xfs_attr3_leaf_to_node(args
);
863 error
= xfs_defer_finish(&args
->trans
,
867 xfs_defer_cancel(args
->flist
);
872 * Commit the node conversion and start the next
873 * trans in the chain.
875 error
= xfs_trans_roll(&args
->trans
, dp
);
883 * Split as many Btree elements as required.
884 * This code tracks the new and old attr's location
885 * in the index/blkno/rmtblkno/rmtblkcnt fields and
886 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
888 xfs_defer_init(args
->flist
, args
->firstblock
);
889 error
= xfs_da3_split(state
);
891 error
= xfs_defer_finish(&args
->trans
, args
->flist
, dp
);
894 xfs_defer_cancel(args
->flist
);
899 * Addition succeeded, update Btree hashvals.
901 xfs_da3_fixhashpath(state
, &state
->path
);
905 * Kill the state structure, we're done with it and need to
906 * allow the buffers to come back later.
908 xfs_da_state_free(state
);
912 * Commit the leaf addition or btree split and start the next
913 * trans in the chain.
915 error
= xfs_trans_roll(&args
->trans
, dp
);
920 * If there was an out-of-line value, allocate the blocks we
921 * identified for its storage and copy the value. This is done
922 * after we create the attribute so that we don't overflow the
923 * maximum size of a transaction and/or hit a deadlock.
925 if (args
->rmtblkno
> 0) {
926 error
= xfs_attr_rmtval_set(args
);
932 * If this is an atomic rename operation, we must "flip" the
933 * incomplete flags on the "new" and "old" attribute/value pairs
934 * so that one disappears and one appears atomically. Then we
935 * must remove the "old" attribute/value pair.
937 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
939 * In a separate transaction, set the incomplete flag on the
940 * "old" attr and clear the incomplete flag on the "new" attr.
942 error
= xfs_attr3_leaf_flipflags(args
);
947 * Dismantle the "old" attribute/value pair by removing
948 * a "remote" value (if it exists).
950 args
->index
= args
->index2
;
951 args
->blkno
= args
->blkno2
;
952 args
->rmtblkno
= args
->rmtblkno2
;
953 args
->rmtblkcnt
= args
->rmtblkcnt2
;
954 args
->rmtvaluelen
= args
->rmtvaluelen2
;
955 if (args
->rmtblkno
) {
956 error
= xfs_attr_rmtval_remove(args
);
962 * Re-find the "old" attribute entry after any split ops.
963 * The INCOMPLETE flag means that we will find the "old"
964 * attr, not the "new" one.
966 args
->flags
|= XFS_ATTR_INCOMPLETE
;
967 state
= xfs_da_state_alloc();
971 error
= xfs_da3_node_lookup_int(state
, &retval
);
976 * Remove the name and update the hashvals in the tree.
978 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
979 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
980 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
981 xfs_da3_fixhashpath(state
, &state
->path
);
984 * Check to see if the tree needs to be collapsed.
986 if (retval
&& (state
->path
.active
> 1)) {
987 xfs_defer_init(args
->flist
, args
->firstblock
);
988 error
= xfs_da3_join(state
);
990 error
= xfs_defer_finish(&args
->trans
,
994 xfs_defer_cancel(args
->flist
);
1000 * Commit and start the next trans in the chain.
1002 error
= xfs_trans_roll(&args
->trans
, dp
);
1006 } else if (args
->rmtblkno
> 0) {
1008 * Added a "remote" value, just clear the incomplete flag.
1010 error
= xfs_attr3_leaf_clearflag(args
);
1018 xfs_da_state_free(state
);
1025 * Remove a name from a B-tree attribute list.
1027 * This will involve walking down the Btree, and may involve joining
1028 * leaf nodes and even joining intermediate nodes up to and including
1029 * the root node (a special case of an intermediate node).
1032 xfs_attr_node_removename(xfs_da_args_t
*args
)
1034 xfs_da_state_t
*state
;
1035 xfs_da_state_blk_t
*blk
;
1038 int retval
, error
, forkoff
;
1040 trace_xfs_attr_node_removename(args
);
1043 * Tie a string around our finger to remind us where we are.
1046 state
= xfs_da_state_alloc();
1048 state
->mp
= dp
->i_mount
;
1051 * Search to see if name exists, and get back a pointer to it.
1053 error
= xfs_da3_node_lookup_int(state
, &retval
);
1054 if (error
|| (retval
!= -EEXIST
)) {
1061 * If there is an out-of-line value, de-allocate the blocks.
1062 * This is done before we remove the attribute so that we don't
1063 * overflow the maximum size of a transaction and/or hit a deadlock.
1065 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1066 ASSERT(blk
->bp
!= NULL
);
1067 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1068 if (args
->rmtblkno
> 0) {
1070 * Fill in disk block numbers in the state structure
1071 * so that we can get the buffers back after we commit
1072 * several transactions in the following calls.
1074 error
= xfs_attr_fillstate(state
);
1079 * Mark the attribute as INCOMPLETE, then bunmapi() the
1082 error
= xfs_attr3_leaf_setflag(args
);
1085 error
= xfs_attr_rmtval_remove(args
);
1090 * Refill the state structure with buffers, the prior calls
1091 * released our buffers.
1093 error
= xfs_attr_refillstate(state
);
1099 * Remove the name and update the hashvals in the tree.
1101 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1102 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1103 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1104 xfs_da3_fixhashpath(state
, &state
->path
);
1107 * Check to see if the tree needs to be collapsed.
1109 if (retval
&& (state
->path
.active
> 1)) {
1110 xfs_defer_init(args
->flist
, args
->firstblock
);
1111 error
= xfs_da3_join(state
);
1113 error
= xfs_defer_finish(&args
->trans
, args
->flist
, dp
);
1116 xfs_defer_cancel(args
->flist
);
1120 * Commit the Btree join operation and start a new trans.
1122 error
= xfs_trans_roll(&args
->trans
, dp
);
1128 * If the result is small enough, push it all into the inode.
1130 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1132 * Have to get rid of the copy of this dabuf in the state.
1134 ASSERT(state
->path
.active
== 1);
1135 ASSERT(state
->path
.blk
[0].bp
);
1136 state
->path
.blk
[0].bp
= NULL
;
1138 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1142 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1143 xfs_defer_init(args
->flist
, args
->firstblock
);
1144 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1145 /* bp is gone due to xfs_da_shrink_inode */
1147 error
= xfs_defer_finish(&args
->trans
,
1151 xfs_defer_cancel(args
->flist
);
1155 xfs_trans_brelse(args
->trans
, bp
);
1160 xfs_da_state_free(state
);
1165 * Fill in the disk block numbers in the state structure for the buffers
1166 * that are attached to the state structure.
1167 * This is done so that we can quickly reattach ourselves to those buffers
1168 * after some set of transaction commits have released these buffers.
1171 xfs_attr_fillstate(xfs_da_state_t
*state
)
1173 xfs_da_state_path_t
*path
;
1174 xfs_da_state_blk_t
*blk
;
1177 trace_xfs_attr_fillstate(state
->args
);
1180 * Roll down the "path" in the state structure, storing the on-disk
1181 * block number for those buffers in the "path".
1183 path
= &state
->path
;
1184 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1185 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1187 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1190 blk
->disk_blkno
= 0;
1195 * Roll down the "altpath" in the state structure, storing the on-disk
1196 * block number for those buffers in the "altpath".
1198 path
= &state
->altpath
;
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;
1213 * Reattach the buffers to the state structure based on the disk block
1214 * numbers stored in the state structure.
1215 * This is done after some set of transaction commits have released those
1216 * buffers from our grip.
1219 xfs_attr_refillstate(xfs_da_state_t
*state
)
1221 xfs_da_state_path_t
*path
;
1222 xfs_da_state_blk_t
*blk
;
1225 trace_xfs_attr_refillstate(state
->args
);
1228 * Roll down the "path" in the state structure, storing the on-disk
1229 * block number for those buffers in the "path".
1231 path
= &state
->path
;
1232 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1233 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1234 if (blk
->disk_blkno
) {
1235 error
= xfs_da3_node_read(state
->args
->trans
,
1237 blk
->blkno
, blk
->disk_blkno
,
1238 &blk
->bp
, XFS_ATTR_FORK
);
1247 * Roll down the "altpath" in the state structure, storing the on-disk
1248 * block number for those buffers in the "altpath".
1250 path
= &state
->altpath
;
1251 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1252 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1253 if (blk
->disk_blkno
) {
1254 error
= xfs_da3_node_read(state
->args
->trans
,
1256 blk
->blkno
, blk
->disk_blkno
,
1257 &blk
->bp
, XFS_ATTR_FORK
);
1269 * Look up a filename in a node attribute list.
1271 * This routine gets called for any attribute fork that has more than one
1272 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1273 * "remote" values taking up more blocks.
1276 xfs_attr_node_get(xfs_da_args_t
*args
)
1278 xfs_da_state_t
*state
;
1279 xfs_da_state_blk_t
*blk
;
1283 trace_xfs_attr_node_get(args
);
1285 state
= xfs_da_state_alloc();
1287 state
->mp
= args
->dp
->i_mount
;
1290 * Search to see if name exists, and get back a pointer to it.
1292 error
= xfs_da3_node_lookup_int(state
, &retval
);
1295 } else if (retval
== -EEXIST
) {
1296 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1297 ASSERT(blk
->bp
!= NULL
);
1298 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1301 * Get the value, local or "remote"
1303 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1304 if (!retval
&& (args
->rmtblkno
> 0)
1305 && !(args
->flags
& ATTR_KERNOVAL
)) {
1306 retval
= xfs_attr_rmtval_get(args
);
1311 * If not in a transaction, we have to release all the buffers.
1313 for (i
= 0; i
< state
->path
.active
; i
++) {
1314 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1315 state
->path
.blk
[i
].bp
= NULL
;
1318 xfs_da_state_free(state
);