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
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"
33 #include "xfs_inode_item.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
48 * Provide the external interfaces to manage attribute lists.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Internal routines when attribute list fits inside the inode.
58 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
61 * Internal routines when attribute list is one block.
63 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
64 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
65 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
68 * Internal routines when attribute list is more than one block.
70 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
74 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
79 struct xfs_da_args
*args
,
81 const unsigned char *name
,
88 memset(args
, 0, sizeof(*args
));
89 args
->geo
= dp
->i_mount
->m_attr_geo
;
90 args
->whichfork
= XFS_ATTR_FORK
;
94 args
->namelen
= strlen((const char *)name
);
95 if (args
->namelen
>= MAXNAMELEN
)
96 return -EFAULT
; /* match IRIX behaviour */
98 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
104 struct xfs_inode
*ip
)
106 if (!XFS_IFORK_Q(ip
) ||
107 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
108 ip
->i_d
.di_anextents
== 0))
113 /*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
117 /* Retrieve an extended attribute and its value. Must have ilock. */
119 xfs_attr_get_ilocked(
120 struct xfs_inode
*ip
,
121 struct xfs_da_args
*args
)
123 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
));
125 if (!xfs_inode_hasattr(ip
))
127 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
128 return xfs_attr_shortform_getvalue(args
);
129 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
130 return xfs_attr_leaf_get(args
);
132 return xfs_attr_node_get(args
);
135 /* Retrieve an extended attribute by name, and its value. */
138 struct xfs_inode
*ip
,
139 const unsigned char *name
,
140 unsigned char *value
,
144 struct xfs_da_args args
;
148 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
150 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
153 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
158 args
.valuelen
= *valuelenp
;
159 /* Entirely possible to look up a name which doesn't exist */
160 args
.op_flags
= XFS_DA_OP_OKNOENT
;
162 lock_mode
= xfs_ilock_attr_map_shared(ip
);
163 error
= xfs_attr_get_ilocked(ip
, &args
);
164 xfs_iunlock(ip
, lock_mode
);
166 *valuelenp
= args
.valuelen
;
167 return error
== -EEXIST
? 0 : error
;
171 * Calculate how many blocks we need for the new attribute,
175 struct xfs_da_args
*args
,
178 struct xfs_mount
*mp
= args
->dp
->i_mount
;
183 * Determine space new attribute will use, and if it would be
184 * "local" or "remote" (note: local != inline).
186 size
= xfs_attr_leaf_newentsize(args
, local
);
187 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
189 if (size
> (args
->geo
->blksize
/ 2)) {
190 /* Double split possible */
195 * Out of line attribute, cannot double split, but
196 * make room for the attribute value itself.
198 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
200 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
208 struct xfs_inode
*dp
,
209 const unsigned char *name
,
210 unsigned char *value
,
214 struct xfs_mount
*mp
= dp
->i_mount
;
215 struct xfs_da_args args
;
216 struct xfs_defer_ops dfops
;
217 struct xfs_trans_res tres
;
218 xfs_fsblock_t firstblock
;
219 int rsvd
= (flags
& ATTR_ROOT
) != 0;
220 int error
, err2
, local
;
222 XFS_STATS_INC(mp
, xs_attr_set
);
224 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
227 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
232 args
.valuelen
= valuelen
;
233 args
.firstblock
= &firstblock
;
235 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
236 args
.total
= xfs_attr_calc_size(&args
, &local
);
238 error
= xfs_qm_dqattach(dp
, 0);
243 * If the inode doesn't have an attribute fork, add one.
244 * (inode must not be locked when we call this routine)
246 if (XFS_IFORK_Q(dp
) == 0) {
247 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
248 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
250 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
255 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
256 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
257 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
258 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
261 * Root fork attributes can use reserved data blocks for this
262 * operation if necessary
264 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
265 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
269 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
270 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
271 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
272 XFS_QMOPT_RES_REGBLKS
);
274 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
275 xfs_trans_cancel(args
.trans
);
279 xfs_trans_ijoin(args
.trans
, dp
, 0);
282 * If the attribute list is non-existent or a shortform list,
283 * upgrade it to a single-leaf-block attribute list.
285 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
286 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
287 dp
->i_d
.di_anextents
== 0)) {
290 * Build initial attribute list (if required).
292 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
293 xfs_attr_shortform_create(&args
);
296 * Try to add the attr to the attribute list in
299 error
= xfs_attr_shortform_addname(&args
);
300 if (error
!= -ENOSPC
) {
302 * Commit the shortform mods, and we're done.
303 * NOTE: this is also the error path (EEXIST, etc).
305 ASSERT(args
.trans
!= NULL
);
308 * If this is a synchronous mount, make sure that
309 * the transaction goes to disk before returning
312 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
313 xfs_trans_set_sync(args
.trans
);
315 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
316 xfs_trans_ichgtime(args
.trans
, dp
,
319 err2
= xfs_trans_commit(args
.trans
);
320 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
322 return error
? error
: err2
;
326 * It won't fit in the shortform, transform to a leaf block.
327 * GROT: another possible req'mt for a double-split btree op.
329 xfs_defer_init(args
.dfops
, args
.firstblock
);
330 error
= xfs_attr_shortform_to_leaf(&args
);
332 error
= xfs_defer_finish(&args
.trans
, args
.dfops
, dp
);
335 xfs_defer_cancel(&dfops
);
340 * Commit the leaf transformation. We'll need another (linked)
341 * transaction to add the new attribute to the leaf.
344 error
= xfs_trans_roll(&args
.trans
, dp
);
350 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
351 error
= xfs_attr_leaf_addname(&args
);
353 error
= xfs_attr_node_addname(&args
);
358 * If this is a synchronous mount, make sure that the
359 * transaction goes to disk before returning to the user.
361 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
362 xfs_trans_set_sync(args
.trans
);
364 if ((flags
& ATTR_KERNOTIME
) == 0)
365 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
368 * Commit the last in the sequence of transactions.
370 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
371 error
= xfs_trans_commit(args
.trans
);
372 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
378 xfs_trans_cancel(args
.trans
);
379 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
384 * Generic handler routine to remove a name from an attribute list.
385 * Transitions attribute list from Btree to shortform as necessary.
389 struct xfs_inode
*dp
,
390 const unsigned char *name
,
393 struct xfs_mount
*mp
= dp
->i_mount
;
394 struct xfs_da_args args
;
395 struct xfs_defer_ops dfops
;
396 xfs_fsblock_t firstblock
;
399 XFS_STATS_INC(mp
, xs_attr_remove
);
401 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
404 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
408 args
.firstblock
= &firstblock
;
412 * we have no control over the attribute names that userspace passes us
413 * to remove, so we have to allow the name lookup prior to attribute
416 args
.op_flags
= XFS_DA_OP_OKNOENT
;
418 error
= xfs_qm_dqattach(dp
, 0);
423 * Root fork attributes can use reserved data blocks for this
424 * operation if necessary
426 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
427 XFS_ATTRRM_SPACE_RES(mp
), 0,
428 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
433 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
435 * No need to make quota reservations here. We expect to release some
436 * blocks not allocate in the common case.
438 xfs_trans_ijoin(args
.trans
, dp
, 0);
440 if (!xfs_inode_hasattr(dp
)) {
442 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
443 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
444 error
= xfs_attr_shortform_remove(&args
);
445 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
446 error
= xfs_attr_leaf_removename(&args
);
448 error
= xfs_attr_node_removename(&args
);
455 * If this is a synchronous mount, make sure that the
456 * transaction goes to disk before returning to the user.
458 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
459 xfs_trans_set_sync(args
.trans
);
461 if ((flags
& ATTR_KERNOTIME
) == 0)
462 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
465 * Commit the last in the sequence of transactions.
467 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
468 error
= xfs_trans_commit(args
.trans
);
469 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
475 xfs_trans_cancel(args
.trans
);
476 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
480 /*========================================================================
481 * External routines when attribute list is inside the inode
482 *========================================================================*/
485 * Add a name to the shortform attribute list structure
486 * This is the external routine.
489 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
491 int newsize
, forkoff
, retval
;
493 trace_xfs_attr_sf_addname(args
);
495 retval
= xfs_attr_shortform_lookup(args
);
496 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
498 } else if (retval
== -EEXIST
) {
499 if (args
->flags
& ATTR_CREATE
)
501 retval
= xfs_attr_shortform_remove(args
);
505 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
506 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
509 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
510 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
512 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
516 xfs_attr_shortform_add(args
, forkoff
);
521 /*========================================================================
522 * External routines when attribute list is one block
523 *========================================================================*/
526 * Add a name to the leaf attribute list structure
528 * This leaf block cannot have a "remote" value, we only call this routine
529 * if bmap_one_block() says there is only one block (ie: no remote blks).
532 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
536 int retval
, error
, forkoff
;
538 trace_xfs_attr_leaf_addname(args
);
541 * Read the (only) block in the attribute list in.
545 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
550 * Look up the given attribute in the leaf block. Figure out if
551 * the given flags produce an error or call for an atomic rename.
553 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
554 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
555 xfs_trans_brelse(args
->trans
, bp
);
557 } else if (retval
== -EEXIST
) {
558 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
559 xfs_trans_brelse(args
->trans
, bp
);
563 trace_xfs_attr_leaf_replace(args
);
565 /* save the attribute state for later removal*/
566 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
567 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
568 args
->index2
= args
->index
;
569 args
->rmtblkno2
= args
->rmtblkno
;
570 args
->rmtblkcnt2
= args
->rmtblkcnt
;
571 args
->rmtvaluelen2
= args
->rmtvaluelen
;
574 * clear the remote attr state now that it is saved so that the
575 * values reflect the state of the attribute we are about to
576 * add, not the attribute we just found and will remove later.
580 args
->rmtvaluelen
= 0;
584 * Add the attribute to the leaf block, transitioning to a Btree
587 retval
= xfs_attr3_leaf_add(bp
, args
);
588 if (retval
== -ENOSPC
) {
590 * Promote the attribute list to the Btree format, then
591 * Commit that transaction so that the node_addname() call
592 * can manage its own transactions.
594 xfs_defer_init(args
->dfops
, args
->firstblock
);
595 error
= xfs_attr3_leaf_to_node(args
);
597 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
600 xfs_defer_cancel(args
->dfops
);
605 * Commit the current trans (including the inode) and start
608 error
= xfs_trans_roll(&args
->trans
, dp
);
613 * Fob the whole rest of the problem off on the Btree code.
615 error
= xfs_attr_node_addname(args
);
620 * Commit the transaction that added the attr name so that
621 * later routines can manage their own transactions.
623 error
= xfs_trans_roll(&args
->trans
, dp
);
628 * If there was an out-of-line value, allocate the blocks we
629 * identified for its storage and copy the value. This is done
630 * after we create the attribute so that we don't overflow the
631 * maximum size of a transaction and/or hit a deadlock.
633 if (args
->rmtblkno
> 0) {
634 error
= xfs_attr_rmtval_set(args
);
640 * If this is an atomic rename operation, we must "flip" the
641 * incomplete flags on the "new" and "old" attribute/value pairs
642 * so that one disappears and one appears atomically. Then we
643 * must remove the "old" attribute/value pair.
645 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
647 * In a separate transaction, set the incomplete flag on the
648 * "old" attr and clear the incomplete flag on the "new" attr.
650 error
= xfs_attr3_leaf_flipflags(args
);
655 * Dismantle the "old" attribute/value pair by removing
656 * a "remote" value (if it exists).
658 args
->index
= args
->index2
;
659 args
->blkno
= args
->blkno2
;
660 args
->rmtblkno
= args
->rmtblkno2
;
661 args
->rmtblkcnt
= args
->rmtblkcnt2
;
662 args
->rmtvaluelen
= args
->rmtvaluelen2
;
663 if (args
->rmtblkno
) {
664 error
= xfs_attr_rmtval_remove(args
);
670 * Read in the block containing the "old" attr, then
671 * remove the "old" attr from that block (neat, huh!)
673 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
678 xfs_attr3_leaf_remove(bp
, args
);
681 * If the result is small enough, shrink it all into the inode.
683 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
684 xfs_defer_init(args
->dfops
, args
->firstblock
);
685 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
686 /* bp is gone due to xfs_da_shrink_inode */
688 error
= xfs_defer_finish(&args
->trans
,
692 xfs_defer_cancel(args
->dfops
);
698 * Commit the remove and start the next trans in series.
700 error
= xfs_trans_roll(&args
->trans
, dp
);
702 } else if (args
->rmtblkno
> 0) {
704 * Added a "remote" value, just clear the incomplete flag.
706 error
= xfs_attr3_leaf_clearflag(args
);
712 * Remove a name from the leaf attribute list structure
714 * This leaf block cannot have a "remote" value, we only call this routine
715 * if bmap_one_block() says there is only one block (ie: no remote blks).
718 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
724 trace_xfs_attr_leaf_removename(args
);
727 * Remove the attribute.
731 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
735 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
736 if (error
== -ENOATTR
) {
737 xfs_trans_brelse(args
->trans
, bp
);
741 xfs_attr3_leaf_remove(bp
, args
);
744 * If the result is small enough, shrink it all into the inode.
746 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
747 xfs_defer_init(args
->dfops
, args
->firstblock
);
748 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
749 /* bp is gone due to xfs_da_shrink_inode */
751 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
754 xfs_defer_cancel(args
->dfops
);
762 * Look up a name in a leaf attribute list structure.
764 * This leaf block cannot have a "remote" value, we only call this routine
765 * if bmap_one_block() says there is only one block (ie: no remote blks).
768 xfs_attr_leaf_get(xfs_da_args_t
*args
)
773 trace_xfs_attr_leaf_get(args
);
776 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
780 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
781 if (error
!= -EEXIST
) {
782 xfs_trans_brelse(args
->trans
, bp
);
785 error
= xfs_attr3_leaf_getvalue(bp
, args
);
786 xfs_trans_brelse(args
->trans
, bp
);
787 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
788 error
= xfs_attr_rmtval_get(args
);
793 /*========================================================================
794 * External routines when attribute list size > geo->blksize
795 *========================================================================*/
798 * Add a name to a Btree-format attribute list.
800 * This will involve walking down the Btree, and may involve splitting
801 * leaf nodes and even splitting intermediate nodes up to and including
802 * the root node (a special case of an intermediate node).
804 * "Remote" attribute values confuse the issue and atomic rename operations
805 * add a whole extra layer of confusion on top of that.
808 xfs_attr_node_addname(xfs_da_args_t
*args
)
810 xfs_da_state_t
*state
;
811 xfs_da_state_blk_t
*blk
;
816 trace_xfs_attr_node_addname(args
);
819 * Fill in bucket of arguments/results/context to carry around.
824 state
= xfs_da_state_alloc();
829 * Search to see if name already exists, and get back a pointer
830 * to where it should go.
832 error
= xfs_da3_node_lookup_int(state
, &retval
);
835 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
836 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
837 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
839 } else if (retval
== -EEXIST
) {
840 if (args
->flags
& ATTR_CREATE
)
843 trace_xfs_attr_node_replace(args
);
845 /* save the attribute state for later removal*/
846 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
847 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
848 args
->index2
= args
->index
;
849 args
->rmtblkno2
= args
->rmtblkno
;
850 args
->rmtblkcnt2
= args
->rmtblkcnt
;
851 args
->rmtvaluelen2
= args
->rmtvaluelen
;
854 * clear the remote attr state now that it is saved so that the
855 * values reflect the state of the attribute we are about to
856 * add, not the attribute we just found and will remove later.
860 args
->rmtvaluelen
= 0;
863 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
864 if (retval
== -ENOSPC
) {
865 if (state
->path
.active
== 1) {
867 * Its really a single leaf node, but it had
868 * out-of-line values so it looked like it *might*
869 * have been a b-tree.
871 xfs_da_state_free(state
);
873 xfs_defer_init(args
->dfops
, args
->firstblock
);
874 error
= xfs_attr3_leaf_to_node(args
);
876 error
= xfs_defer_finish(&args
->trans
,
880 xfs_defer_cancel(args
->dfops
);
885 * Commit the node conversion and start the next
886 * trans in the chain.
888 error
= xfs_trans_roll(&args
->trans
, dp
);
896 * Split as many Btree elements as required.
897 * This code tracks the new and old attr's location
898 * in the index/blkno/rmtblkno/rmtblkcnt fields and
899 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
901 xfs_defer_init(args
->dfops
, args
->firstblock
);
902 error
= xfs_da3_split(state
);
904 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
907 xfs_defer_cancel(args
->dfops
);
912 * Addition succeeded, update Btree hashvals.
914 xfs_da3_fixhashpath(state
, &state
->path
);
918 * Kill the state structure, we're done with it and need to
919 * allow the buffers to come back later.
921 xfs_da_state_free(state
);
925 * Commit the leaf addition or btree split and start the next
926 * trans in the chain.
928 error
= xfs_trans_roll(&args
->trans
, dp
);
933 * If there was an out-of-line value, allocate the blocks we
934 * identified for its storage and copy the value. This is done
935 * after we create the attribute so that we don't overflow the
936 * maximum size of a transaction and/or hit a deadlock.
938 if (args
->rmtblkno
> 0) {
939 error
= xfs_attr_rmtval_set(args
);
945 * If this is an atomic rename operation, we must "flip" the
946 * incomplete flags on the "new" and "old" attribute/value pairs
947 * so that one disappears and one appears atomically. Then we
948 * must remove the "old" attribute/value pair.
950 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
952 * In a separate transaction, set the incomplete flag on the
953 * "old" attr and clear the incomplete flag on the "new" attr.
955 error
= xfs_attr3_leaf_flipflags(args
);
960 * Dismantle the "old" attribute/value pair by removing
961 * a "remote" value (if it exists).
963 args
->index
= args
->index2
;
964 args
->blkno
= args
->blkno2
;
965 args
->rmtblkno
= args
->rmtblkno2
;
966 args
->rmtblkcnt
= args
->rmtblkcnt2
;
967 args
->rmtvaluelen
= args
->rmtvaluelen2
;
968 if (args
->rmtblkno
) {
969 error
= xfs_attr_rmtval_remove(args
);
975 * Re-find the "old" attribute entry after any split ops.
976 * The INCOMPLETE flag means that we will find the "old"
977 * attr, not the "new" one.
979 args
->flags
|= XFS_ATTR_INCOMPLETE
;
980 state
= xfs_da_state_alloc();
984 error
= xfs_da3_node_lookup_int(state
, &retval
);
989 * Remove the name and update the hashvals in the tree.
991 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
992 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
993 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
994 xfs_da3_fixhashpath(state
, &state
->path
);
997 * Check to see if the tree needs to be collapsed.
999 if (retval
&& (state
->path
.active
> 1)) {
1000 xfs_defer_init(args
->dfops
, args
->firstblock
);
1001 error
= xfs_da3_join(state
);
1003 error
= xfs_defer_finish(&args
->trans
,
1007 xfs_defer_cancel(args
->dfops
);
1013 * Commit and start the next trans in the chain.
1015 error
= xfs_trans_roll(&args
->trans
, dp
);
1019 } else if (args
->rmtblkno
> 0) {
1021 * Added a "remote" value, just clear the incomplete flag.
1023 error
= xfs_attr3_leaf_clearflag(args
);
1031 xfs_da_state_free(state
);
1038 * Remove a name from a B-tree attribute list.
1040 * This will involve walking down the Btree, and may involve joining
1041 * leaf nodes and even joining intermediate nodes up to and including
1042 * the root node (a special case of an intermediate node).
1045 xfs_attr_node_removename(xfs_da_args_t
*args
)
1047 xfs_da_state_t
*state
;
1048 xfs_da_state_blk_t
*blk
;
1051 int retval
, error
, forkoff
;
1053 trace_xfs_attr_node_removename(args
);
1056 * Tie a string around our finger to remind us where we are.
1059 state
= xfs_da_state_alloc();
1061 state
->mp
= dp
->i_mount
;
1064 * Search to see if name exists, and get back a pointer to it.
1066 error
= xfs_da3_node_lookup_int(state
, &retval
);
1067 if (error
|| (retval
!= -EEXIST
)) {
1074 * If there is an out-of-line value, de-allocate the blocks.
1075 * This is done before we remove the attribute so that we don't
1076 * overflow the maximum size of a transaction and/or hit a deadlock.
1078 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1079 ASSERT(blk
->bp
!= NULL
);
1080 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1081 if (args
->rmtblkno
> 0) {
1083 * Fill in disk block numbers in the state structure
1084 * so that we can get the buffers back after we commit
1085 * several transactions in the following calls.
1087 error
= xfs_attr_fillstate(state
);
1092 * Mark the attribute as INCOMPLETE, then bunmapi() the
1095 error
= xfs_attr3_leaf_setflag(args
);
1098 error
= xfs_attr_rmtval_remove(args
);
1103 * Refill the state structure with buffers, the prior calls
1104 * released our buffers.
1106 error
= xfs_attr_refillstate(state
);
1112 * Remove the name and update the hashvals in the tree.
1114 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1115 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1116 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1117 xfs_da3_fixhashpath(state
, &state
->path
);
1120 * Check to see if the tree needs to be collapsed.
1122 if (retval
&& (state
->path
.active
> 1)) {
1123 xfs_defer_init(args
->dfops
, args
->firstblock
);
1124 error
= xfs_da3_join(state
);
1126 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
1129 xfs_defer_cancel(args
->dfops
);
1133 * Commit the Btree join operation and start a new trans.
1135 error
= xfs_trans_roll(&args
->trans
, dp
);
1141 * If the result is small enough, push it all into the inode.
1143 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1145 * Have to get rid of the copy of this dabuf in the state.
1147 ASSERT(state
->path
.active
== 1);
1148 ASSERT(state
->path
.blk
[0].bp
);
1149 state
->path
.blk
[0].bp
= NULL
;
1151 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1155 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1156 xfs_defer_init(args
->dfops
, args
->firstblock
);
1157 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1158 /* bp is gone due to xfs_da_shrink_inode */
1160 error
= xfs_defer_finish(&args
->trans
,
1164 xfs_defer_cancel(args
->dfops
);
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
);