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"
27 #include "xfs_mount.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
36 #include "xfs_bmap_util.h"
37 #include "xfs_bmap_btree.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_attr_remote.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_trace.h"
45 #include "xfs_dinode.h"
50 * Provide the external interfaces to manage attribute lists.
53 /*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
58 * Internal routines when attribute list fits inside the inode.
60 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
63 * Internal routines when attribute list is one block.
65 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
66 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
67 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
70 * Internal routines when attribute list is more than one block.
72 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
74 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
75 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
76 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
81 struct xfs_da_args
*args
,
83 const unsigned char *name
,
90 memset(args
, 0, sizeof(*args
));
91 args
->geo
= dp
->i_mount
->m_attr_geo
;
92 args
->whichfork
= XFS_ATTR_FORK
;
96 args
->namelen
= strlen((const char *)name
);
97 if (args
->namelen
>= MAXNAMELEN
)
98 return EFAULT
; /* match IRIX behaviour */
100 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
106 struct xfs_inode
*ip
)
108 if (!XFS_IFORK_Q(ip
) ||
109 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
110 ip
->i_d
.di_anextents
== 0))
115 /*========================================================================
116 * Overall external interface routines.
117 *========================================================================*/
121 struct xfs_inode
*ip
,
122 const unsigned char *name
,
123 unsigned char *value
,
127 struct xfs_da_args args
;
131 XFS_STATS_INC(xs_attr_get
);
133 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
136 if (!xfs_inode_hasattr(ip
))
139 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
144 args
.valuelen
= *valuelenp
;
146 lock_mode
= xfs_ilock_attr_map_shared(ip
);
147 if (!xfs_inode_hasattr(ip
))
149 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
150 error
= xfs_attr_shortform_getvalue(&args
);
151 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
152 error
= xfs_attr_leaf_get(&args
);
154 error
= xfs_attr_node_get(&args
);
155 xfs_iunlock(ip
, lock_mode
);
157 *valuelenp
= args
.valuelen
;
158 return error
== EEXIST
? 0 : error
;
162 * Calculate how many blocks we need for the new attribute,
166 struct xfs_da_args
*args
,
169 struct xfs_mount
*mp
= args
->dp
->i_mount
;
174 * Determine space new attribute will use, and if it would be
175 * "local" or "remote" (note: local != inline).
177 size
= xfs_attr_leaf_newentsize(args
, local
);
178 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
180 if (size
> (args
->geo
->blksize
/ 2)) {
181 /* Double split possible */
186 * Out of line attribute, cannot double split, but
187 * make room for the attribute value itself.
189 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
191 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
199 struct xfs_inode
*dp
,
200 const unsigned char *name
,
201 unsigned char *value
,
205 struct xfs_mount
*mp
= dp
->i_mount
;
206 struct xfs_da_args args
;
207 struct xfs_bmap_free flist
;
208 struct xfs_trans_res tres
;
209 xfs_fsblock_t firstblock
;
210 int rsvd
= (flags
& ATTR_ROOT
) != 0;
211 int error
, err2
, committed
, local
;
213 XFS_STATS_INC(xs_attr_set
);
215 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
218 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
223 args
.valuelen
= valuelen
;
224 args
.firstblock
= &firstblock
;
226 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
227 args
.total
= xfs_attr_calc_size(&args
, &local
);
229 error
= xfs_qm_dqattach(dp
, 0);
234 * If the inode doesn't have an attribute fork, add one.
235 * (inode must not be locked when we call this routine)
237 if (XFS_IFORK_Q(dp
) == 0) {
238 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
239 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
241 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
247 * Start our first transaction of the day.
249 * All future transactions during this code must be "chained" off
250 * this one via the trans_dup() call. All transactions will contain
251 * the inode, and the inode will always be marked with trans_ihold().
252 * Since the inode will be locked in all transactions, we must log
253 * the inode in every transaction to let it float upward through
256 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
259 * Root fork attributes can use reserved data blocks for this
260 * operation if necessary
264 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
266 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
267 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
268 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
269 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
270 error
= xfs_trans_reserve(args
.trans
, &tres
, args
.total
, 0);
272 xfs_trans_cancel(args
.trans
, 0);
275 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
277 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
278 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
279 XFS_QMOPT_RES_REGBLKS
);
281 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
282 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
286 xfs_trans_ijoin(args
.trans
, dp
, 0);
289 * If the attribute list is non-existent or a shortform list,
290 * upgrade it to a single-leaf-block attribute list.
292 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
293 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
294 dp
->i_d
.di_anextents
== 0)) {
297 * Build initial attribute list (if required).
299 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
300 xfs_attr_shortform_create(&args
);
303 * Try to add the attr to the attribute list in
306 error
= xfs_attr_shortform_addname(&args
);
307 if (error
!= ENOSPC
) {
309 * Commit the shortform mods, and we're done.
310 * NOTE: this is also the error path (EEXIST, etc).
312 ASSERT(args
.trans
!= NULL
);
315 * If this is a synchronous mount, make sure that
316 * the transaction goes to disk before returning
319 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
320 xfs_trans_set_sync(args
.trans
);
322 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
323 xfs_trans_ichgtime(args
.trans
, dp
,
326 err2
= xfs_trans_commit(args
.trans
,
327 XFS_TRANS_RELEASE_LOG_RES
);
328 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
330 return error
? error
: err2
;
334 * It won't fit in the shortform, transform to a leaf block.
335 * GROT: another possible req'mt for a double-split btree op.
337 xfs_bmap_init(args
.flist
, args
.firstblock
);
338 error
= xfs_attr_shortform_to_leaf(&args
);
340 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
346 xfs_bmap_cancel(&flist
);
351 * bmap_finish() may have committed the last trans and started
352 * a new one. We need the inode to be in all transactions.
355 xfs_trans_ijoin(args
.trans
, dp
, 0);
358 * Commit the leaf transformation. We'll need another (linked)
359 * transaction to add the new attribute to the leaf.
362 error
= xfs_trans_roll(&args
.trans
, dp
);
368 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
369 error
= xfs_attr_leaf_addname(&args
);
371 error
= xfs_attr_node_addname(&args
);
376 * If this is a synchronous mount, make sure that the
377 * transaction goes to disk before returning to the user.
379 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
380 xfs_trans_set_sync(args
.trans
);
382 if ((flags
& ATTR_KERNOTIME
) == 0)
383 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
386 * Commit the last in the sequence of transactions.
388 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
389 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
390 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
396 xfs_trans_cancel(args
.trans
,
397 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
399 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
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
;
415 struct xfs_bmap_free flist
;
416 xfs_fsblock_t firstblock
;
419 XFS_STATS_INC(xs_attr_remove
);
421 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
424 if (!xfs_inode_hasattr(dp
))
427 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
431 args
.firstblock
= &firstblock
;
435 * we have no control over the attribute names that userspace passes us
436 * to remove, so we have to allow the name lookup prior to attribute
439 args
.op_flags
= XFS_DA_OP_OKNOENT
;
441 error
= xfs_qm_dqattach(dp
, 0);
446 * Start our first transaction of the day.
448 * All future transactions during this code must be "chained" off
449 * this one via the trans_dup() call. All transactions will contain
450 * the inode, and the inode will always be marked with trans_ihold().
451 * Since the inode will be locked in all transactions, we must log
452 * the inode in every transaction to let it float upward through
455 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
458 * Root fork attributes can use reserved data blocks for this
459 * operation if necessary
462 if (flags
& ATTR_ROOT
)
463 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
465 error
= xfs_trans_reserve(args
.trans
, &M_RES(mp
)->tr_attrrm
,
466 XFS_ATTRRM_SPACE_RES(mp
), 0);
468 xfs_trans_cancel(args
.trans
, 0);
472 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
474 * No need to make quota reservations here. We expect to release some
475 * blocks not allocate in the common case.
477 xfs_trans_ijoin(args
.trans
, dp
, 0);
479 if (!xfs_inode_hasattr(dp
)) {
480 error
= XFS_ERROR(ENOATTR
);
481 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
482 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
483 error
= xfs_attr_shortform_remove(&args
);
484 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
485 error
= xfs_attr_leaf_removename(&args
);
487 error
= xfs_attr_node_removename(&args
);
494 * If this is a synchronous mount, make sure that the
495 * transaction goes to disk before returning to the user.
497 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
498 xfs_trans_set_sync(args
.trans
);
500 if ((flags
& ATTR_KERNOTIME
) == 0)
501 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
504 * Commit the last in the sequence of transactions.
506 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
507 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
508 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
514 xfs_trans_cancel(args
.trans
,
515 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
517 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
521 /*========================================================================
522 * External routines when attribute list is inside the inode
523 *========================================================================*/
526 * Add a name to the shortform attribute list structure
527 * This is the external routine.
530 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
532 int newsize
, forkoff
, retval
;
534 trace_xfs_attr_sf_addname(args
);
536 retval
= xfs_attr_shortform_lookup(args
);
537 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
539 } else if (retval
== EEXIST
) {
540 if (args
->flags
& ATTR_CREATE
)
542 retval
= xfs_attr_shortform_remove(args
);
546 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
547 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
548 return(XFS_ERROR(ENOSPC
));
550 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
551 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
553 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
555 return(XFS_ERROR(ENOSPC
));
557 xfs_attr_shortform_add(args
, forkoff
);
562 /*========================================================================
563 * External routines when attribute list is one block
564 *========================================================================*/
567 * Add a name to the leaf attribute list structure
569 * This leaf block cannot have a "remote" value, we only call this routine
570 * if bmap_one_block() says there is only one block (ie: no remote blks).
573 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
577 int retval
, error
, committed
, forkoff
;
579 trace_xfs_attr_leaf_addname(args
);
582 * Read the (only) block in the attribute list in.
586 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
591 * Look up the given attribute in the leaf block. Figure out if
592 * the given flags produce an error or call for an atomic rename.
594 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
595 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
596 xfs_trans_brelse(args
->trans
, bp
);
598 } else if (retval
== EEXIST
) {
599 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
600 xfs_trans_brelse(args
->trans
, bp
);
604 trace_xfs_attr_leaf_replace(args
);
606 /* save the attribute state for later removal*/
607 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
608 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
609 args
->index2
= args
->index
;
610 args
->rmtblkno2
= args
->rmtblkno
;
611 args
->rmtblkcnt2
= args
->rmtblkcnt
;
612 args
->rmtvaluelen2
= args
->rmtvaluelen
;
615 * clear the remote attr state now that it is saved so that the
616 * values reflect the state of the attribute we are about to
617 * add, not the attribute we just found and will remove later.
621 args
->rmtvaluelen
= 0;
625 * Add the attribute to the leaf block, transitioning to a Btree
628 retval
= xfs_attr3_leaf_add(bp
, args
);
629 if (retval
== ENOSPC
) {
631 * Promote the attribute list to the Btree format, then
632 * Commit that transaction so that the node_addname() call
633 * can manage its own transactions.
635 xfs_bmap_init(args
->flist
, args
->firstblock
);
636 error
= xfs_attr3_leaf_to_node(args
);
638 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
644 xfs_bmap_cancel(args
->flist
);
649 * bmap_finish() may have committed the last trans and started
650 * a new one. We need the inode to be in all transactions.
653 xfs_trans_ijoin(args
->trans
, dp
, 0);
656 * Commit the current trans (including the inode) and start
659 error
= xfs_trans_roll(&args
->trans
, dp
);
664 * Fob the whole rest of the problem off on the Btree code.
666 error
= xfs_attr_node_addname(args
);
671 * Commit the transaction that added the attr name so that
672 * later routines can manage their own transactions.
674 error
= xfs_trans_roll(&args
->trans
, dp
);
679 * If there was an out-of-line value, allocate the blocks we
680 * identified for its storage and copy the value. This is done
681 * after we create the attribute so that we don't overflow the
682 * maximum size of a transaction and/or hit a deadlock.
684 if (args
->rmtblkno
> 0) {
685 error
= xfs_attr_rmtval_set(args
);
691 * If this is an atomic rename operation, we must "flip" the
692 * incomplete flags on the "new" and "old" attribute/value pairs
693 * so that one disappears and one appears atomically. Then we
694 * must remove the "old" attribute/value pair.
696 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
698 * In a separate transaction, set the incomplete flag on the
699 * "old" attr and clear the incomplete flag on the "new" attr.
701 error
= xfs_attr3_leaf_flipflags(args
);
706 * Dismantle the "old" attribute/value pair by removing
707 * a "remote" value (if it exists).
709 args
->index
= args
->index2
;
710 args
->blkno
= args
->blkno2
;
711 args
->rmtblkno
= args
->rmtblkno2
;
712 args
->rmtblkcnt
= args
->rmtblkcnt2
;
713 args
->rmtvaluelen
= args
->rmtvaluelen2
;
714 if (args
->rmtblkno
) {
715 error
= xfs_attr_rmtval_remove(args
);
721 * Read in the block containing the "old" attr, then
722 * remove the "old" attr from that block (neat, huh!)
724 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
729 xfs_attr3_leaf_remove(bp
, args
);
732 * If the result is small enough, shrink it all into the inode.
734 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
735 xfs_bmap_init(args
->flist
, args
->firstblock
);
736 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
737 /* bp is gone due to xfs_da_shrink_inode */
739 error
= xfs_bmap_finish(&args
->trans
,
746 xfs_bmap_cancel(args
->flist
);
751 * bmap_finish() may have committed the last trans
752 * and started a new one. We need the inode to be
753 * in all transactions.
756 xfs_trans_ijoin(args
->trans
, dp
, 0);
760 * Commit the remove and start the next trans in series.
762 error
= xfs_trans_roll(&args
->trans
, dp
);
764 } else if (args
->rmtblkno
> 0) {
766 * Added a "remote" value, just clear the incomplete flag.
768 error
= xfs_attr3_leaf_clearflag(args
);
774 * Remove a name from the leaf attribute list structure
776 * This leaf block cannot have a "remote" value, we only call this routine
777 * if bmap_one_block() says there is only one block (ie: no remote blks).
780 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
784 int error
, committed
, forkoff
;
786 trace_xfs_attr_leaf_removename(args
);
789 * Remove the attribute.
793 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
797 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
798 if (error
== ENOATTR
) {
799 xfs_trans_brelse(args
->trans
, bp
);
803 xfs_attr3_leaf_remove(bp
, args
);
806 * If the result is small enough, shrink it all into the inode.
808 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
809 xfs_bmap_init(args
->flist
, args
->firstblock
);
810 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
811 /* bp is gone due to xfs_da_shrink_inode */
813 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
819 xfs_bmap_cancel(args
->flist
);
824 * bmap_finish() may have committed the last trans and started
825 * a new one. We need the inode to be in all transactions.
828 xfs_trans_ijoin(args
->trans
, dp
, 0);
834 * Look up a name in a leaf attribute list structure.
836 * This leaf block cannot have a "remote" value, we only call this routine
837 * if bmap_one_block() says there is only one block (ie: no remote blks).
840 xfs_attr_leaf_get(xfs_da_args_t
*args
)
845 trace_xfs_attr_leaf_get(args
);
848 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
852 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
853 if (error
!= EEXIST
) {
854 xfs_trans_brelse(args
->trans
, bp
);
857 error
= xfs_attr3_leaf_getvalue(bp
, args
);
858 xfs_trans_brelse(args
->trans
, bp
);
859 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
860 error
= xfs_attr_rmtval_get(args
);
865 /*========================================================================
866 * External routines when attribute list size > geo->blksize
867 *========================================================================*/
870 * Add a name to a Btree-format attribute list.
872 * This will involve walking down the Btree, and may involve splitting
873 * leaf nodes and even splitting intermediate nodes up to and including
874 * the root node (a special case of an intermediate node).
876 * "Remote" attribute values confuse the issue and atomic rename operations
877 * add a whole extra layer of confusion on top of that.
880 xfs_attr_node_addname(xfs_da_args_t
*args
)
882 xfs_da_state_t
*state
;
883 xfs_da_state_blk_t
*blk
;
886 int committed
, retval
, error
;
888 trace_xfs_attr_node_addname(args
);
891 * Fill in bucket of arguments/results/context to carry around.
896 state
= xfs_da_state_alloc();
901 * Search to see if name already exists, and get back a pointer
902 * to where it should go.
904 error
= xfs_da3_node_lookup_int(state
, &retval
);
907 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
908 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
909 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
911 } else if (retval
== EEXIST
) {
912 if (args
->flags
& ATTR_CREATE
)
915 trace_xfs_attr_node_replace(args
);
917 /* save the attribute state for later removal*/
918 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
919 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
920 args
->index2
= args
->index
;
921 args
->rmtblkno2
= args
->rmtblkno
;
922 args
->rmtblkcnt2
= args
->rmtblkcnt
;
923 args
->rmtvaluelen2
= args
->rmtvaluelen
;
926 * clear the remote attr state now that it is saved so that the
927 * values reflect the state of the attribute we are about to
928 * add, not the attribute we just found and will remove later.
932 args
->rmtvaluelen
= 0;
935 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
936 if (retval
== ENOSPC
) {
937 if (state
->path
.active
== 1) {
939 * Its really a single leaf node, but it had
940 * out-of-line values so it looked like it *might*
941 * have been a b-tree.
943 xfs_da_state_free(state
);
945 xfs_bmap_init(args
->flist
, args
->firstblock
);
946 error
= xfs_attr3_leaf_to_node(args
);
948 error
= xfs_bmap_finish(&args
->trans
,
955 xfs_bmap_cancel(args
->flist
);
960 * bmap_finish() may have committed the last trans
961 * and started a new one. We need the inode to be
962 * in all transactions.
965 xfs_trans_ijoin(args
->trans
, dp
, 0);
968 * Commit the node conversion and start the next
969 * trans in the chain.
971 error
= xfs_trans_roll(&args
->trans
, dp
);
979 * Split as many Btree elements as required.
980 * This code tracks the new and old attr's location
981 * in the index/blkno/rmtblkno/rmtblkcnt fields and
982 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
984 xfs_bmap_init(args
->flist
, args
->firstblock
);
985 error
= xfs_da3_split(state
);
987 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
993 xfs_bmap_cancel(args
->flist
);
998 * bmap_finish() may have committed the last trans and started
999 * a new one. We need the inode to be in all transactions.
1002 xfs_trans_ijoin(args
->trans
, dp
, 0);
1005 * Addition succeeded, update Btree hashvals.
1007 xfs_da3_fixhashpath(state
, &state
->path
);
1011 * Kill the state structure, we're done with it and need to
1012 * allow the buffers to come back later.
1014 xfs_da_state_free(state
);
1018 * Commit the leaf addition or btree split and start the next
1019 * trans in the chain.
1021 error
= xfs_trans_roll(&args
->trans
, dp
);
1026 * If there was an out-of-line value, allocate the blocks we
1027 * identified for its storage and copy the value. This is done
1028 * after we create the attribute so that we don't overflow the
1029 * maximum size of a transaction and/or hit a deadlock.
1031 if (args
->rmtblkno
> 0) {
1032 error
= xfs_attr_rmtval_set(args
);
1038 * If this is an atomic rename operation, we must "flip" the
1039 * incomplete flags on the "new" and "old" attribute/value pairs
1040 * so that one disappears and one appears atomically. Then we
1041 * must remove the "old" attribute/value pair.
1043 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1045 * In a separate transaction, set the incomplete flag on the
1046 * "old" attr and clear the incomplete flag on the "new" attr.
1048 error
= xfs_attr3_leaf_flipflags(args
);
1053 * Dismantle the "old" attribute/value pair by removing
1054 * a "remote" value (if it exists).
1056 args
->index
= args
->index2
;
1057 args
->blkno
= args
->blkno2
;
1058 args
->rmtblkno
= args
->rmtblkno2
;
1059 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1060 args
->rmtvaluelen
= args
->rmtvaluelen2
;
1061 if (args
->rmtblkno
) {
1062 error
= xfs_attr_rmtval_remove(args
);
1068 * Re-find the "old" attribute entry after any split ops.
1069 * The INCOMPLETE flag means that we will find the "old"
1070 * attr, not the "new" one.
1072 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1073 state
= xfs_da_state_alloc();
1077 error
= xfs_da3_node_lookup_int(state
, &retval
);
1082 * Remove the name and update the hashvals in the tree.
1084 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1085 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1086 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1087 xfs_da3_fixhashpath(state
, &state
->path
);
1090 * Check to see if the tree needs to be collapsed.
1092 if (retval
&& (state
->path
.active
> 1)) {
1093 xfs_bmap_init(args
->flist
, args
->firstblock
);
1094 error
= xfs_da3_join(state
);
1096 error
= xfs_bmap_finish(&args
->trans
,
1103 xfs_bmap_cancel(args
->flist
);
1108 * bmap_finish() may have committed the last trans
1109 * and started a new one. We need the inode to be
1110 * in all transactions.
1113 xfs_trans_ijoin(args
->trans
, dp
, 0);
1117 * Commit and start the next trans in the chain.
1119 error
= xfs_trans_roll(&args
->trans
, dp
);
1123 } else if (args
->rmtblkno
> 0) {
1125 * Added a "remote" value, just clear the incomplete flag.
1127 error
= xfs_attr3_leaf_clearflag(args
);
1135 xfs_da_state_free(state
);
1142 * Remove a name from a B-tree attribute list.
1144 * This will involve walking down the Btree, and may involve joining
1145 * leaf nodes and even joining intermediate nodes up to and including
1146 * the root node (a special case of an intermediate node).
1149 xfs_attr_node_removename(xfs_da_args_t
*args
)
1151 xfs_da_state_t
*state
;
1152 xfs_da_state_blk_t
*blk
;
1155 int retval
, error
, committed
, forkoff
;
1157 trace_xfs_attr_node_removename(args
);
1160 * Tie a string around our finger to remind us where we are.
1163 state
= xfs_da_state_alloc();
1165 state
->mp
= dp
->i_mount
;
1168 * Search to see if name exists, and get back a pointer to it.
1170 error
= xfs_da3_node_lookup_int(state
, &retval
);
1171 if (error
|| (retval
!= EEXIST
)) {
1178 * If there is an out-of-line value, de-allocate the blocks.
1179 * This is done before we remove the attribute so that we don't
1180 * overflow the maximum size of a transaction and/or hit a deadlock.
1182 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1183 ASSERT(blk
->bp
!= NULL
);
1184 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1185 if (args
->rmtblkno
> 0) {
1187 * Fill in disk block numbers in the state structure
1188 * so that we can get the buffers back after we commit
1189 * several transactions in the following calls.
1191 error
= xfs_attr_fillstate(state
);
1196 * Mark the attribute as INCOMPLETE, then bunmapi() the
1199 error
= xfs_attr3_leaf_setflag(args
);
1202 error
= xfs_attr_rmtval_remove(args
);
1207 * Refill the state structure with buffers, the prior calls
1208 * released our buffers.
1210 error
= xfs_attr_refillstate(state
);
1216 * Remove the name and update the hashvals in the tree.
1218 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1219 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1220 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1221 xfs_da3_fixhashpath(state
, &state
->path
);
1224 * Check to see if the tree needs to be collapsed.
1226 if (retval
&& (state
->path
.active
> 1)) {
1227 xfs_bmap_init(args
->flist
, args
->firstblock
);
1228 error
= xfs_da3_join(state
);
1230 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1236 xfs_bmap_cancel(args
->flist
);
1241 * bmap_finish() may have committed the last trans and started
1242 * a new one. We need the inode to be in all transactions.
1245 xfs_trans_ijoin(args
->trans
, dp
, 0);
1248 * Commit the Btree join operation and start a new trans.
1250 error
= xfs_trans_roll(&args
->trans
, dp
);
1256 * If the result is small enough, push it all into the inode.
1258 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1260 * Have to get rid of the copy of this dabuf in the state.
1262 ASSERT(state
->path
.active
== 1);
1263 ASSERT(state
->path
.blk
[0].bp
);
1264 state
->path
.blk
[0].bp
= NULL
;
1266 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1270 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1271 xfs_bmap_init(args
->flist
, args
->firstblock
);
1272 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1273 /* bp is gone due to xfs_da_shrink_inode */
1275 error
= xfs_bmap_finish(&args
->trans
,
1282 xfs_bmap_cancel(args
->flist
);
1287 * bmap_finish() may have committed the last trans
1288 * and started a new one. We need the inode to be
1289 * in all transactions.
1292 xfs_trans_ijoin(args
->trans
, dp
, 0);
1294 xfs_trans_brelse(args
->trans
, bp
);
1299 xfs_da_state_free(state
);
1304 * Fill in the disk block numbers in the state structure for the buffers
1305 * that are attached to the state structure.
1306 * This is done so that we can quickly reattach ourselves to those buffers
1307 * after some set of transaction commits have released these buffers.
1310 xfs_attr_fillstate(xfs_da_state_t
*state
)
1312 xfs_da_state_path_t
*path
;
1313 xfs_da_state_blk_t
*blk
;
1316 trace_xfs_attr_fillstate(state
->args
);
1319 * Roll down the "path" in the state structure, storing the on-disk
1320 * block number for those buffers in the "path".
1322 path
= &state
->path
;
1323 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1324 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1326 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1329 blk
->disk_blkno
= 0;
1334 * Roll down the "altpath" in the state structure, storing the on-disk
1335 * block number for those buffers in the "altpath".
1337 path
= &state
->altpath
;
1338 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1339 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1341 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1344 blk
->disk_blkno
= 0;
1352 * Reattach the buffers to the state structure based on the disk block
1353 * numbers stored in the state structure.
1354 * This is done after some set of transaction commits have released those
1355 * buffers from our grip.
1358 xfs_attr_refillstate(xfs_da_state_t
*state
)
1360 xfs_da_state_path_t
*path
;
1361 xfs_da_state_blk_t
*blk
;
1364 trace_xfs_attr_refillstate(state
->args
);
1367 * Roll down the "path" in the state structure, storing the on-disk
1368 * block number for those buffers in the "path".
1370 path
= &state
->path
;
1371 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1372 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1373 if (blk
->disk_blkno
) {
1374 error
= xfs_da3_node_read(state
->args
->trans
,
1376 blk
->blkno
, blk
->disk_blkno
,
1377 &blk
->bp
, XFS_ATTR_FORK
);
1386 * Roll down the "altpath" in the state structure, storing the on-disk
1387 * block number for those buffers in the "altpath".
1389 path
= &state
->altpath
;
1390 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1391 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1392 if (blk
->disk_blkno
) {
1393 error
= xfs_da3_node_read(state
->args
->trans
,
1395 blk
->blkno
, blk
->disk_blkno
,
1396 &blk
->bp
, XFS_ATTR_FORK
);
1408 * Look up a filename in a node attribute list.
1410 * This routine gets called for any attribute fork that has more than one
1411 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1412 * "remote" values taking up more blocks.
1415 xfs_attr_node_get(xfs_da_args_t
*args
)
1417 xfs_da_state_t
*state
;
1418 xfs_da_state_blk_t
*blk
;
1422 trace_xfs_attr_node_get(args
);
1424 state
= xfs_da_state_alloc();
1426 state
->mp
= args
->dp
->i_mount
;
1429 * Search to see if name exists, and get back a pointer to it.
1431 error
= xfs_da3_node_lookup_int(state
, &retval
);
1434 } else if (retval
== EEXIST
) {
1435 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1436 ASSERT(blk
->bp
!= NULL
);
1437 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1440 * Get the value, local or "remote"
1442 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1443 if (!retval
&& (args
->rmtblkno
> 0)
1444 && !(args
->flags
& ATTR_KERNOVAL
)) {
1445 retval
= xfs_attr_rmtval_get(args
);
1450 * If not in a transaction, we have to release all the buffers.
1452 for (i
= 0; i
< state
->path
.active
; i
++) {
1453 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1454 state
->path
.blk
[i
].bp
= NULL
;
1457 xfs_da_state_free(state
);