]>
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
;
138 /* Entirely possible to look up a name which doesn't exist */
139 args
.op_flags
= XFS_DA_OP_OKNOENT
;
141 lock_mode
= xfs_ilock_attr_map_shared(ip
);
142 if (!xfs_inode_hasattr(ip
))
144 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
145 error
= xfs_attr_shortform_getvalue(&args
);
146 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
147 error
= xfs_attr_leaf_get(&args
);
149 error
= xfs_attr_node_get(&args
);
150 xfs_iunlock(ip
, lock_mode
);
152 *valuelenp
= args
.valuelen
;
153 return error
== -EEXIST
? 0 : error
;
157 * Calculate how many blocks we need for the new attribute,
161 struct xfs_da_args
*args
,
164 struct xfs_mount
*mp
= args
->dp
->i_mount
;
169 * Determine space new attribute will use, and if it would be
170 * "local" or "remote" (note: local != inline).
172 size
= xfs_attr_leaf_newentsize(args
, local
);
173 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
175 if (size
> (args
->geo
->blksize
/ 2)) {
176 /* Double split possible */
181 * Out of line attribute, cannot double split, but
182 * make room for the attribute value itself.
184 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
186 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
194 struct xfs_inode
*dp
,
195 const unsigned char *name
,
196 unsigned char *value
,
200 struct xfs_mount
*mp
= dp
->i_mount
;
201 struct xfs_da_args args
;
202 struct xfs_defer_ops dfops
;
203 struct xfs_trans_res tres
;
204 xfs_fsblock_t firstblock
;
205 int rsvd
= (flags
& ATTR_ROOT
) != 0;
206 int error
, err2
, local
;
208 XFS_STATS_INC(mp
, xs_attr_set
);
210 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
213 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
218 args
.valuelen
= valuelen
;
219 args
.firstblock
= &firstblock
;
221 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
222 args
.total
= xfs_attr_calc_size(&args
, &local
);
224 error
= xfs_qm_dqattach(dp
, 0);
229 * If the inode doesn't have an attribute fork, add one.
230 * (inode must not be locked when we call this routine)
232 if (XFS_IFORK_Q(dp
) == 0) {
233 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
234 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
236 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
241 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
242 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
243 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
244 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
247 * Root fork attributes can use reserved data blocks for this
248 * operation if necessary
250 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
251 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
255 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
256 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
257 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
258 XFS_QMOPT_RES_REGBLKS
);
260 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
261 xfs_trans_cancel(args
.trans
);
265 xfs_trans_ijoin(args
.trans
, dp
, 0);
268 * If the attribute list is non-existent or a shortform list,
269 * upgrade it to a single-leaf-block attribute list.
271 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
272 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
273 dp
->i_d
.di_anextents
== 0)) {
276 * Build initial attribute list (if required).
278 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
279 xfs_attr_shortform_create(&args
);
282 * Try to add the attr to the attribute list in
285 error
= xfs_attr_shortform_addname(&args
);
286 if (error
!= -ENOSPC
) {
288 * Commit the shortform mods, and we're done.
289 * NOTE: this is also the error path (EEXIST, etc).
291 ASSERT(args
.trans
!= NULL
);
294 * If this is a synchronous mount, make sure that
295 * the transaction goes to disk before returning
298 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
299 xfs_trans_set_sync(args
.trans
);
301 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
302 xfs_trans_ichgtime(args
.trans
, dp
,
305 err2
= xfs_trans_commit(args
.trans
);
306 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
308 return error
? error
: err2
;
312 * It won't fit in the shortform, transform to a leaf block.
313 * GROT: another possible req'mt for a double-split btree op.
315 xfs_defer_init(args
.dfops
, args
.firstblock
);
316 error
= xfs_attr_shortform_to_leaf(&args
);
318 error
= xfs_defer_finish(&args
.trans
, args
.dfops
, dp
);
321 xfs_defer_cancel(&dfops
);
326 * Commit the leaf transformation. We'll need another (linked)
327 * transaction to add the new attribute to the leaf.
330 error
= xfs_trans_roll(&args
.trans
, dp
);
336 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
337 error
= xfs_attr_leaf_addname(&args
);
339 error
= xfs_attr_node_addname(&args
);
344 * If this is a synchronous mount, make sure that the
345 * transaction goes to disk before returning to the user.
347 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
348 xfs_trans_set_sync(args
.trans
);
350 if ((flags
& ATTR_KERNOTIME
) == 0)
351 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
354 * Commit the last in the sequence of transactions.
356 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
357 error
= xfs_trans_commit(args
.trans
);
358 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
364 xfs_trans_cancel(args
.trans
);
365 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
370 * Generic handler routine to remove a name from an attribute list.
371 * Transitions attribute list from Btree to shortform as necessary.
375 struct xfs_inode
*dp
,
376 const unsigned char *name
,
379 struct xfs_mount
*mp
= dp
->i_mount
;
380 struct xfs_da_args args
;
381 struct xfs_defer_ops dfops
;
382 xfs_fsblock_t firstblock
;
385 XFS_STATS_INC(mp
, xs_attr_remove
);
387 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
390 if (!xfs_inode_hasattr(dp
))
393 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
397 args
.firstblock
= &firstblock
;
401 * we have no control over the attribute names that userspace passes us
402 * to remove, so we have to allow the name lookup prior to attribute
405 args
.op_flags
= XFS_DA_OP_OKNOENT
;
407 error
= xfs_qm_dqattach(dp
, 0);
412 * Root fork attributes can use reserved data blocks for this
413 * operation if necessary
415 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
416 XFS_ATTRRM_SPACE_RES(mp
), 0,
417 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
422 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
424 * No need to make quota reservations here. We expect to release some
425 * blocks not allocate in the common case.
427 xfs_trans_ijoin(args
.trans
, dp
, 0);
429 if (!xfs_inode_hasattr(dp
)) {
431 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
432 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
433 error
= xfs_attr_shortform_remove(&args
);
434 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
435 error
= xfs_attr_leaf_removename(&args
);
437 error
= xfs_attr_node_removename(&args
);
444 * If this is a synchronous mount, make sure that the
445 * transaction goes to disk before returning to the user.
447 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
448 xfs_trans_set_sync(args
.trans
);
450 if ((flags
& ATTR_KERNOTIME
) == 0)
451 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
454 * Commit the last in the sequence of transactions.
456 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
457 error
= xfs_trans_commit(args
.trans
);
458 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
464 xfs_trans_cancel(args
.trans
);
465 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
469 /*========================================================================
470 * External routines when attribute list is inside the inode
471 *========================================================================*/
474 * Add a name to the shortform attribute list structure
475 * This is the external routine.
478 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
480 int newsize
, forkoff
, retval
;
482 trace_xfs_attr_sf_addname(args
);
484 retval
= xfs_attr_shortform_lookup(args
);
485 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
487 } else if (retval
== -EEXIST
) {
488 if (args
->flags
& ATTR_CREATE
)
490 retval
= xfs_attr_shortform_remove(args
);
494 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
495 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
498 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
499 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
501 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
505 xfs_attr_shortform_add(args
, forkoff
);
510 /*========================================================================
511 * External routines when attribute list is one block
512 *========================================================================*/
515 * Add a name to the leaf attribute list structure
517 * This leaf block cannot have a "remote" value, we only call this routine
518 * if bmap_one_block() says there is only one block (ie: no remote blks).
521 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
525 int retval
, error
, forkoff
;
527 trace_xfs_attr_leaf_addname(args
);
530 * Read the (only) block in the attribute list in.
534 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
539 * Look up the given attribute in the leaf block. Figure out if
540 * the given flags produce an error or call for an atomic rename.
542 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
543 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
544 xfs_trans_brelse(args
->trans
, bp
);
546 } else if (retval
== -EEXIST
) {
547 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
548 xfs_trans_brelse(args
->trans
, bp
);
552 trace_xfs_attr_leaf_replace(args
);
554 /* save the attribute state for later removal*/
555 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
556 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
557 args
->index2
= args
->index
;
558 args
->rmtblkno2
= args
->rmtblkno
;
559 args
->rmtblkcnt2
= args
->rmtblkcnt
;
560 args
->rmtvaluelen2
= args
->rmtvaluelen
;
563 * clear the remote attr state now that it is saved so that the
564 * values reflect the state of the attribute we are about to
565 * add, not the attribute we just found and will remove later.
569 args
->rmtvaluelen
= 0;
573 * Add the attribute to the leaf block, transitioning to a Btree
576 retval
= xfs_attr3_leaf_add(bp
, args
);
577 if (retval
== -ENOSPC
) {
579 * Promote the attribute list to the Btree format, then
580 * Commit that transaction so that the node_addname() call
581 * can manage its own transactions.
583 xfs_defer_init(args
->dfops
, args
->firstblock
);
584 error
= xfs_attr3_leaf_to_node(args
);
586 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
589 xfs_defer_cancel(args
->dfops
);
594 * Commit the current trans (including the inode) and start
597 error
= xfs_trans_roll(&args
->trans
, dp
);
602 * Fob the whole rest of the problem off on the Btree code.
604 error
= xfs_attr_node_addname(args
);
609 * Commit the transaction that added the attr name so that
610 * later routines can manage their own transactions.
612 error
= xfs_trans_roll(&args
->trans
, dp
);
617 * If there was an out-of-line value, allocate the blocks we
618 * identified for its storage and copy the value. This is done
619 * after we create the attribute so that we don't overflow the
620 * maximum size of a transaction and/or hit a deadlock.
622 if (args
->rmtblkno
> 0) {
623 error
= xfs_attr_rmtval_set(args
);
629 * If this is an atomic rename operation, we must "flip" the
630 * incomplete flags on the "new" and "old" attribute/value pairs
631 * so that one disappears and one appears atomically. Then we
632 * must remove the "old" attribute/value pair.
634 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
636 * In a separate transaction, set the incomplete flag on the
637 * "old" attr and clear the incomplete flag on the "new" attr.
639 error
= xfs_attr3_leaf_flipflags(args
);
644 * Dismantle the "old" attribute/value pair by removing
645 * a "remote" value (if it exists).
647 args
->index
= args
->index2
;
648 args
->blkno
= args
->blkno2
;
649 args
->rmtblkno
= args
->rmtblkno2
;
650 args
->rmtblkcnt
= args
->rmtblkcnt2
;
651 args
->rmtvaluelen
= args
->rmtvaluelen2
;
652 if (args
->rmtblkno
) {
653 error
= xfs_attr_rmtval_remove(args
);
659 * Read in the block containing the "old" attr, then
660 * remove the "old" attr from that block (neat, huh!)
662 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
667 xfs_attr3_leaf_remove(bp
, args
);
670 * If the result is small enough, shrink it all into the inode.
672 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
673 xfs_defer_init(args
->dfops
, args
->firstblock
);
674 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
675 /* bp is gone due to xfs_da_shrink_inode */
677 error
= xfs_defer_finish(&args
->trans
,
681 xfs_defer_cancel(args
->dfops
);
687 * Commit the remove and start the next trans in series.
689 error
= xfs_trans_roll(&args
->trans
, dp
);
691 } else if (args
->rmtblkno
> 0) {
693 * Added a "remote" value, just clear the incomplete flag.
695 error
= xfs_attr3_leaf_clearflag(args
);
701 * Remove a name from the leaf attribute list structure
703 * This leaf block cannot have a "remote" value, we only call this routine
704 * if bmap_one_block() says there is only one block (ie: no remote blks).
707 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
713 trace_xfs_attr_leaf_removename(args
);
716 * Remove the attribute.
720 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
724 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
725 if (error
== -ENOATTR
) {
726 xfs_trans_brelse(args
->trans
, bp
);
730 xfs_attr3_leaf_remove(bp
, args
);
733 * If the result is small enough, shrink it all into the inode.
735 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
736 xfs_defer_init(args
->dfops
, args
->firstblock
);
737 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
738 /* bp is gone due to xfs_da_shrink_inode */
740 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
743 xfs_defer_cancel(args
->dfops
);
751 * Look up a name in a leaf attribute list structure.
753 * This leaf block cannot have a "remote" value, we only call this routine
754 * if bmap_one_block() says there is only one block (ie: no remote blks).
757 xfs_attr_leaf_get(xfs_da_args_t
*args
)
762 trace_xfs_attr_leaf_get(args
);
765 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
769 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
770 if (error
!= -EEXIST
) {
771 xfs_trans_brelse(args
->trans
, bp
);
774 error
= xfs_attr3_leaf_getvalue(bp
, args
);
775 xfs_trans_brelse(args
->trans
, bp
);
776 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
777 error
= xfs_attr_rmtval_get(args
);
782 /*========================================================================
783 * External routines when attribute list size > geo->blksize
784 *========================================================================*/
787 * Add a name to a Btree-format attribute list.
789 * This will involve walking down the Btree, and may involve splitting
790 * leaf nodes and even splitting intermediate nodes up to and including
791 * the root node (a special case of an intermediate node).
793 * "Remote" attribute values confuse the issue and atomic rename operations
794 * add a whole extra layer of confusion on top of that.
797 xfs_attr_node_addname(xfs_da_args_t
*args
)
799 xfs_da_state_t
*state
;
800 xfs_da_state_blk_t
*blk
;
805 trace_xfs_attr_node_addname(args
);
808 * Fill in bucket of arguments/results/context to carry around.
813 state
= xfs_da_state_alloc();
818 * Search to see if name already exists, and get back a pointer
819 * to where it should go.
821 error
= xfs_da3_node_lookup_int(state
, &retval
);
824 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
825 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
826 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
828 } else if (retval
== -EEXIST
) {
829 if (args
->flags
& ATTR_CREATE
)
832 trace_xfs_attr_node_replace(args
);
834 /* save the attribute state for later removal*/
835 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
836 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
837 args
->index2
= args
->index
;
838 args
->rmtblkno2
= args
->rmtblkno
;
839 args
->rmtblkcnt2
= args
->rmtblkcnt
;
840 args
->rmtvaluelen2
= args
->rmtvaluelen
;
843 * clear the remote attr state now that it is saved so that the
844 * values reflect the state of the attribute we are about to
845 * add, not the attribute we just found and will remove later.
849 args
->rmtvaluelen
= 0;
852 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
853 if (retval
== -ENOSPC
) {
854 if (state
->path
.active
== 1) {
856 * Its really a single leaf node, but it had
857 * out-of-line values so it looked like it *might*
858 * have been a b-tree.
860 xfs_da_state_free(state
);
862 xfs_defer_init(args
->dfops
, args
->firstblock
);
863 error
= xfs_attr3_leaf_to_node(args
);
865 error
= xfs_defer_finish(&args
->trans
,
869 xfs_defer_cancel(args
->dfops
);
874 * Commit the node conversion and start the next
875 * trans in the chain.
877 error
= xfs_trans_roll(&args
->trans
, dp
);
885 * Split as many Btree elements as required.
886 * This code tracks the new and old attr's location
887 * in the index/blkno/rmtblkno/rmtblkcnt fields and
888 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
890 xfs_defer_init(args
->dfops
, args
->firstblock
);
891 error
= xfs_da3_split(state
);
893 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
896 xfs_defer_cancel(args
->dfops
);
901 * Addition succeeded, update Btree hashvals.
903 xfs_da3_fixhashpath(state
, &state
->path
);
907 * Kill the state structure, we're done with it and need to
908 * allow the buffers to come back later.
910 xfs_da_state_free(state
);
914 * Commit the leaf addition or btree split and start the next
915 * trans in the chain.
917 error
= xfs_trans_roll(&args
->trans
, dp
);
922 * If there was an out-of-line value, allocate the blocks we
923 * identified for its storage and copy the value. This is done
924 * after we create the attribute so that we don't overflow the
925 * maximum size of a transaction and/or hit a deadlock.
927 if (args
->rmtblkno
> 0) {
928 error
= xfs_attr_rmtval_set(args
);
934 * If this is an atomic rename operation, we must "flip" the
935 * incomplete flags on the "new" and "old" attribute/value pairs
936 * so that one disappears and one appears atomically. Then we
937 * must remove the "old" attribute/value pair.
939 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
941 * In a separate transaction, set the incomplete flag on the
942 * "old" attr and clear the incomplete flag on the "new" attr.
944 error
= xfs_attr3_leaf_flipflags(args
);
949 * Dismantle the "old" attribute/value pair by removing
950 * a "remote" value (if it exists).
952 args
->index
= args
->index2
;
953 args
->blkno
= args
->blkno2
;
954 args
->rmtblkno
= args
->rmtblkno2
;
955 args
->rmtblkcnt
= args
->rmtblkcnt2
;
956 args
->rmtvaluelen
= args
->rmtvaluelen2
;
957 if (args
->rmtblkno
) {
958 error
= xfs_attr_rmtval_remove(args
);
964 * Re-find the "old" attribute entry after any split ops.
965 * The INCOMPLETE flag means that we will find the "old"
966 * attr, not the "new" one.
968 args
->flags
|= XFS_ATTR_INCOMPLETE
;
969 state
= xfs_da_state_alloc();
973 error
= xfs_da3_node_lookup_int(state
, &retval
);
978 * Remove the name and update the hashvals in the tree.
980 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
981 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
982 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
983 xfs_da3_fixhashpath(state
, &state
->path
);
986 * Check to see if the tree needs to be collapsed.
988 if (retval
&& (state
->path
.active
> 1)) {
989 xfs_defer_init(args
->dfops
, args
->firstblock
);
990 error
= xfs_da3_join(state
);
992 error
= xfs_defer_finish(&args
->trans
,
996 xfs_defer_cancel(args
->dfops
);
1002 * Commit and start the next trans in the chain.
1004 error
= xfs_trans_roll(&args
->trans
, dp
);
1008 } else if (args
->rmtblkno
> 0) {
1010 * Added a "remote" value, just clear the incomplete flag.
1012 error
= xfs_attr3_leaf_clearflag(args
);
1020 xfs_da_state_free(state
);
1027 * Remove a name from a B-tree attribute list.
1029 * This will involve walking down the Btree, and may involve joining
1030 * leaf nodes and even joining intermediate nodes up to and including
1031 * the root node (a special case of an intermediate node).
1034 xfs_attr_node_removename(xfs_da_args_t
*args
)
1036 xfs_da_state_t
*state
;
1037 xfs_da_state_blk_t
*blk
;
1040 int retval
, error
, forkoff
;
1042 trace_xfs_attr_node_removename(args
);
1045 * Tie a string around our finger to remind us where we are.
1048 state
= xfs_da_state_alloc();
1050 state
->mp
= dp
->i_mount
;
1053 * Search to see if name exists, and get back a pointer to it.
1055 error
= xfs_da3_node_lookup_int(state
, &retval
);
1056 if (error
|| (retval
!= -EEXIST
)) {
1063 * If there is an out-of-line value, de-allocate the blocks.
1064 * This is done before we remove the attribute so that we don't
1065 * overflow the maximum size of a transaction and/or hit a deadlock.
1067 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1068 ASSERT(blk
->bp
!= NULL
);
1069 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1070 if (args
->rmtblkno
> 0) {
1072 * Fill in disk block numbers in the state structure
1073 * so that we can get the buffers back after we commit
1074 * several transactions in the following calls.
1076 error
= xfs_attr_fillstate(state
);
1081 * Mark the attribute as INCOMPLETE, then bunmapi() the
1084 error
= xfs_attr3_leaf_setflag(args
);
1087 error
= xfs_attr_rmtval_remove(args
);
1092 * Refill the state structure with buffers, the prior calls
1093 * released our buffers.
1095 error
= xfs_attr_refillstate(state
);
1101 * Remove the name and update the hashvals in the tree.
1103 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1104 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1105 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1106 xfs_da3_fixhashpath(state
, &state
->path
);
1109 * Check to see if the tree needs to be collapsed.
1111 if (retval
&& (state
->path
.active
> 1)) {
1112 xfs_defer_init(args
->dfops
, args
->firstblock
);
1113 error
= xfs_da3_join(state
);
1115 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
1118 xfs_defer_cancel(args
->dfops
);
1122 * Commit the Btree join operation and start a new trans.
1124 error
= xfs_trans_roll(&args
->trans
, dp
);
1130 * If the result is small enough, push it all into the inode.
1132 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1134 * Have to get rid of the copy of this dabuf in the state.
1136 ASSERT(state
->path
.active
== 1);
1137 ASSERT(state
->path
.blk
[0].bp
);
1138 state
->path
.blk
[0].bp
= NULL
;
1140 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1144 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1145 xfs_defer_init(args
->dfops
, args
->firstblock
);
1146 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1147 /* bp is gone due to xfs_da_shrink_inode */
1149 error
= xfs_defer_finish(&args
->trans
,
1153 xfs_defer_cancel(args
->dfops
);
1157 xfs_trans_brelse(args
->trans
, bp
);
1162 xfs_da_state_free(state
);
1167 * Fill in the disk block numbers in the state structure for the buffers
1168 * that are attached to the state structure.
1169 * This is done so that we can quickly reattach ourselves to those buffers
1170 * after some set of transaction commits have released these buffers.
1173 xfs_attr_fillstate(xfs_da_state_t
*state
)
1175 xfs_da_state_path_t
*path
;
1176 xfs_da_state_blk_t
*blk
;
1179 trace_xfs_attr_fillstate(state
->args
);
1182 * Roll down the "path" in the state structure, storing the on-disk
1183 * block number for those buffers in the "path".
1185 path
= &state
->path
;
1186 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1187 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1189 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1192 blk
->disk_blkno
= 0;
1197 * Roll down the "altpath" in the state structure, storing the on-disk
1198 * block number for those buffers in the "altpath".
1200 path
= &state
->altpath
;
1201 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1202 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1204 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1207 blk
->disk_blkno
= 0;
1215 * Reattach the buffers to the state structure based on the disk block
1216 * numbers stored in the state structure.
1217 * This is done after some set of transaction commits have released those
1218 * buffers from our grip.
1221 xfs_attr_refillstate(xfs_da_state_t
*state
)
1223 xfs_da_state_path_t
*path
;
1224 xfs_da_state_blk_t
*blk
;
1227 trace_xfs_attr_refillstate(state
->args
);
1230 * Roll down the "path" in the state structure, storing the on-disk
1231 * block number for those buffers in the "path".
1233 path
= &state
->path
;
1234 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1235 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1236 if (blk
->disk_blkno
) {
1237 error
= xfs_da3_node_read(state
->args
->trans
,
1239 blk
->blkno
, blk
->disk_blkno
,
1240 &blk
->bp
, XFS_ATTR_FORK
);
1249 * Roll down the "altpath" in the state structure, storing the on-disk
1250 * block number for those buffers in the "altpath".
1252 path
= &state
->altpath
;
1253 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1254 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1255 if (blk
->disk_blkno
) {
1256 error
= xfs_da3_node_read(state
->args
->trans
,
1258 blk
->blkno
, blk
->disk_blkno
,
1259 &blk
->bp
, XFS_ATTR_FORK
);
1271 * Look up a filename in a node attribute list.
1273 * This routine gets called for any attribute fork that has more than one
1274 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1275 * "remote" values taking up more blocks.
1278 xfs_attr_node_get(xfs_da_args_t
*args
)
1280 xfs_da_state_t
*state
;
1281 xfs_da_state_blk_t
*blk
;
1285 trace_xfs_attr_node_get(args
);
1287 state
= xfs_da_state_alloc();
1289 state
->mp
= args
->dp
->i_mount
;
1292 * Search to see if name exists, and get back a pointer to it.
1294 error
= xfs_da3_node_lookup_int(state
, &retval
);
1297 } else if (retval
== -EEXIST
) {
1298 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1299 ASSERT(blk
->bp
!= NULL
);
1300 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1303 * Get the value, local or "remote"
1305 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1306 if (!retval
&& (args
->rmtblkno
> 0)
1307 && !(args
->flags
& ATTR_KERNOVAL
)) {
1308 retval
= xfs_attr_rmtval_get(args
);
1313 * If not in a transaction, we have to release all the buffers.
1315 for (i
= 0; i
< state
->path
.active
; i
++) {
1316 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1317 state
->path
.blk
[i
].bp
= NULL
;
1320 xfs_da_state_free(state
);