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 *========================================================================*/
119 struct xfs_inode
*ip
,
120 const unsigned char *name
,
121 unsigned char *value
,
125 struct xfs_da_args args
;
129 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
131 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
134 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
139 args
.valuelen
= *valuelenp
;
140 /* Entirely possible to look up a name which doesn't exist */
141 args
.op_flags
= XFS_DA_OP_OKNOENT
;
143 lock_mode
= xfs_ilock_attr_map_shared(ip
);
144 if (!xfs_inode_hasattr(ip
))
146 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
147 error
= xfs_attr_shortform_getvalue(&args
);
148 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
149 error
= xfs_attr_leaf_get(&args
);
151 error
= xfs_attr_node_get(&args
);
152 xfs_iunlock(ip
, lock_mode
);
154 *valuelenp
= args
.valuelen
;
155 return error
== -EEXIST
? 0 : error
;
159 * Calculate how many blocks we need for the new attribute,
163 struct xfs_da_args
*args
,
166 struct xfs_mount
*mp
= args
->dp
->i_mount
;
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
174 size
= xfs_attr_leaf_newentsize(args
, local
);
175 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
177 if (size
> (args
->geo
->blksize
/ 2)) {
178 /* Double split possible */
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
186 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
188 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
196 struct xfs_inode
*dp
,
197 const unsigned char *name
,
198 unsigned char *value
,
202 struct xfs_mount
*mp
= dp
->i_mount
;
203 struct xfs_da_args args
;
204 struct xfs_defer_ops dfops
;
205 struct xfs_trans_res tres
;
206 xfs_fsblock_t firstblock
;
207 int rsvd
= (flags
& ATTR_ROOT
) != 0;
208 int error
, err2
, local
;
210 XFS_STATS_INC(mp
, xs_attr_set
);
212 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
215 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
220 args
.valuelen
= valuelen
;
221 args
.firstblock
= &firstblock
;
223 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
224 args
.total
= xfs_attr_calc_size(&args
, &local
);
226 error
= xfs_qm_dqattach(dp
, 0);
231 * If the inode doesn't have an attribute fork, add one.
232 * (inode must not be locked when we call this routine)
234 if (XFS_IFORK_Q(dp
) == 0) {
235 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
236 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
238 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
243 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
244 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
245 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
246 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
249 * Root fork attributes can use reserved data blocks for this
250 * operation if necessary
252 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
253 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
257 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
258 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
259 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
260 XFS_QMOPT_RES_REGBLKS
);
262 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
263 xfs_trans_cancel(args
.trans
);
267 xfs_trans_ijoin(args
.trans
, dp
, 0);
270 * If the attribute list is non-existent or a shortform list,
271 * upgrade it to a single-leaf-block attribute list.
273 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
274 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
275 dp
->i_d
.di_anextents
== 0)) {
278 * Build initial attribute list (if required).
280 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
281 xfs_attr_shortform_create(&args
);
284 * Try to add the attr to the attribute list in
287 error
= xfs_attr_shortform_addname(&args
);
288 if (error
!= -ENOSPC
) {
290 * Commit the shortform mods, and we're done.
291 * NOTE: this is also the error path (EEXIST, etc).
293 ASSERT(args
.trans
!= NULL
);
296 * If this is a synchronous mount, make sure that
297 * the transaction goes to disk before returning
300 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
301 xfs_trans_set_sync(args
.trans
);
303 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
304 xfs_trans_ichgtime(args
.trans
, dp
,
307 err2
= xfs_trans_commit(args
.trans
);
308 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
310 return error
? error
: err2
;
314 * It won't fit in the shortform, transform to a leaf block.
315 * GROT: another possible req'mt for a double-split btree op.
317 xfs_defer_init(args
.dfops
, args
.firstblock
);
318 error
= xfs_attr_shortform_to_leaf(&args
);
320 error
= xfs_defer_finish(&args
.trans
, args
.dfops
, dp
);
323 xfs_defer_cancel(&dfops
);
328 * Commit the leaf transformation. We'll need another (linked)
329 * transaction to add the new attribute to the leaf.
332 error
= xfs_trans_roll(&args
.trans
, dp
);
338 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
339 error
= xfs_attr_leaf_addname(&args
);
341 error
= xfs_attr_node_addname(&args
);
346 * If this is a synchronous mount, make sure that the
347 * transaction goes to disk before returning to the user.
349 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
350 xfs_trans_set_sync(args
.trans
);
352 if ((flags
& ATTR_KERNOTIME
) == 0)
353 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
356 * Commit the last in the sequence of transactions.
358 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
359 error
= xfs_trans_commit(args
.trans
);
360 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
366 xfs_trans_cancel(args
.trans
);
367 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
372 * Generic handler routine to remove a name from an attribute list.
373 * Transitions attribute list from Btree to shortform as necessary.
377 struct xfs_inode
*dp
,
378 const unsigned char *name
,
381 struct xfs_mount
*mp
= dp
->i_mount
;
382 struct xfs_da_args args
;
383 struct xfs_defer_ops dfops
;
384 xfs_fsblock_t firstblock
;
387 XFS_STATS_INC(mp
, xs_attr_remove
);
389 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
392 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
396 args
.firstblock
= &firstblock
;
400 * we have no control over the attribute names that userspace passes us
401 * to remove, so we have to allow the name lookup prior to attribute
404 args
.op_flags
= XFS_DA_OP_OKNOENT
;
406 error
= xfs_qm_dqattach(dp
, 0);
411 * Root fork attributes can use reserved data blocks for this
412 * operation if necessary
414 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
415 XFS_ATTRRM_SPACE_RES(mp
), 0,
416 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
421 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
423 * No need to make quota reservations here. We expect to release some
424 * blocks not allocate in the common case.
426 xfs_trans_ijoin(args
.trans
, dp
, 0);
428 if (!xfs_inode_hasattr(dp
)) {
430 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
431 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
432 error
= xfs_attr_shortform_remove(&args
);
433 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
434 error
= xfs_attr_leaf_removename(&args
);
436 error
= xfs_attr_node_removename(&args
);
443 * If this is a synchronous mount, make sure that the
444 * transaction goes to disk before returning to the user.
446 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
447 xfs_trans_set_sync(args
.trans
);
449 if ((flags
& ATTR_KERNOTIME
) == 0)
450 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
453 * Commit the last in the sequence of transactions.
455 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
456 error
= xfs_trans_commit(args
.trans
);
457 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
463 xfs_trans_cancel(args
.trans
);
464 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
468 /*========================================================================
469 * External routines when attribute list is inside the inode
470 *========================================================================*/
473 * Add a name to the shortform attribute list structure
474 * This is the external routine.
477 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
479 int newsize
, forkoff
, retval
;
481 trace_xfs_attr_sf_addname(args
);
483 retval
= xfs_attr_shortform_lookup(args
);
484 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
486 } else if (retval
== -EEXIST
) {
487 if (args
->flags
& ATTR_CREATE
)
489 retval
= xfs_attr_shortform_remove(args
);
493 * Since we have removed the old attr, clear ATTR_REPLACE so
494 * that the leaf format add routine won't trip over the attr
497 args
->flags
&= ~ATTR_REPLACE
;
500 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
501 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
504 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
505 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
507 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
511 xfs_attr_shortform_add(args
, forkoff
);
516 /*========================================================================
517 * External routines when attribute list is one block
518 *========================================================================*/
521 * Add a name to the leaf attribute list structure
523 * This leaf block cannot have a "remote" value, we only call this routine
524 * if bmap_one_block() says there is only one block (ie: no remote blks).
527 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
531 int retval
, error
, forkoff
;
533 trace_xfs_attr_leaf_addname(args
);
536 * Read the (only) block in the attribute list in.
540 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
545 * Look up the given attribute in the leaf block. Figure out if
546 * the given flags produce an error or call for an atomic rename.
548 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
549 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
550 xfs_trans_brelse(args
->trans
, bp
);
552 } else if (retval
== -EEXIST
) {
553 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
554 xfs_trans_brelse(args
->trans
, bp
);
558 trace_xfs_attr_leaf_replace(args
);
560 /* save the attribute state for later removal*/
561 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
562 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
563 args
->index2
= args
->index
;
564 args
->rmtblkno2
= args
->rmtblkno
;
565 args
->rmtblkcnt2
= args
->rmtblkcnt
;
566 args
->rmtvaluelen2
= args
->rmtvaluelen
;
569 * clear the remote attr state now that it is saved so that the
570 * values reflect the state of the attribute we are about to
571 * add, not the attribute we just found and will remove later.
575 args
->rmtvaluelen
= 0;
579 * Add the attribute to the leaf block, transitioning to a Btree
582 retval
= xfs_attr3_leaf_add(bp
, args
);
583 if (retval
== -ENOSPC
) {
585 * Promote the attribute list to the Btree format, then
586 * Commit that transaction so that the node_addname() call
587 * can manage its own transactions.
589 xfs_defer_init(args
->dfops
, args
->firstblock
);
590 error
= xfs_attr3_leaf_to_node(args
);
592 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
595 xfs_defer_cancel(args
->dfops
);
600 * Commit the current trans (including the inode) and start
603 error
= xfs_trans_roll(&args
->trans
, dp
);
608 * Fob the whole rest of the problem off on the Btree code.
610 error
= xfs_attr_node_addname(args
);
615 * Commit the transaction that added the attr name so that
616 * later routines can manage their own transactions.
618 error
= xfs_trans_roll(&args
->trans
, dp
);
623 * If there was an out-of-line value, allocate the blocks we
624 * identified for its storage and copy the value. This is done
625 * after we create the attribute so that we don't overflow the
626 * maximum size of a transaction and/or hit a deadlock.
628 if (args
->rmtblkno
> 0) {
629 error
= xfs_attr_rmtval_set(args
);
635 * If this is an atomic rename operation, we must "flip" the
636 * incomplete flags on the "new" and "old" attribute/value pairs
637 * so that one disappears and one appears atomically. Then we
638 * must remove the "old" attribute/value pair.
640 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
642 * In a separate transaction, set the incomplete flag on the
643 * "old" attr and clear the incomplete flag on the "new" attr.
645 error
= xfs_attr3_leaf_flipflags(args
);
650 * Dismantle the "old" attribute/value pair by removing
651 * a "remote" value (if it exists).
653 args
->index
= args
->index2
;
654 args
->blkno
= args
->blkno2
;
655 args
->rmtblkno
= args
->rmtblkno2
;
656 args
->rmtblkcnt
= args
->rmtblkcnt2
;
657 args
->rmtvaluelen
= args
->rmtvaluelen2
;
658 if (args
->rmtblkno
) {
659 error
= xfs_attr_rmtval_remove(args
);
665 * Read in the block containing the "old" attr, then
666 * remove the "old" attr from that block (neat, huh!)
668 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
673 xfs_attr3_leaf_remove(bp
, args
);
676 * If the result is small enough, shrink it all into the inode.
678 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
679 xfs_defer_init(args
->dfops
, args
->firstblock
);
680 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
681 /* bp is gone due to xfs_da_shrink_inode */
683 error
= xfs_defer_finish(&args
->trans
,
687 xfs_defer_cancel(args
->dfops
);
693 * Commit the remove and start the next trans in series.
695 error
= xfs_trans_roll(&args
->trans
, dp
);
697 } else if (args
->rmtblkno
> 0) {
699 * Added a "remote" value, just clear the incomplete flag.
701 error
= xfs_attr3_leaf_clearflag(args
);
707 * Remove a name from the leaf attribute list structure
709 * This leaf block cannot have a "remote" value, we only call this routine
710 * if bmap_one_block() says there is only one block (ie: no remote blks).
713 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
719 trace_xfs_attr_leaf_removename(args
);
722 * Remove the attribute.
726 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
730 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
731 if (error
== -ENOATTR
) {
732 xfs_trans_brelse(args
->trans
, bp
);
736 xfs_attr3_leaf_remove(bp
, args
);
739 * If the result is small enough, shrink it all into the inode.
741 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
742 xfs_defer_init(args
->dfops
, args
->firstblock
);
743 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
744 /* bp is gone due to xfs_da_shrink_inode */
746 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
749 xfs_defer_cancel(args
->dfops
);
757 * Look up a name in a leaf attribute list structure.
759 * This leaf block cannot have a "remote" value, we only call this routine
760 * if bmap_one_block() says there is only one block (ie: no remote blks).
763 xfs_attr_leaf_get(xfs_da_args_t
*args
)
768 trace_xfs_attr_leaf_get(args
);
771 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
775 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
776 if (error
!= -EEXIST
) {
777 xfs_trans_brelse(args
->trans
, bp
);
780 error
= xfs_attr3_leaf_getvalue(bp
, args
);
781 xfs_trans_brelse(args
->trans
, bp
);
782 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
783 error
= xfs_attr_rmtval_get(args
);
788 /*========================================================================
789 * External routines when attribute list size > geo->blksize
790 *========================================================================*/
793 * Add a name to a Btree-format attribute list.
795 * This will involve walking down the Btree, and may involve splitting
796 * leaf nodes and even splitting intermediate nodes up to and including
797 * the root node (a special case of an intermediate node).
799 * "Remote" attribute values confuse the issue and atomic rename operations
800 * add a whole extra layer of confusion on top of that.
803 xfs_attr_node_addname(xfs_da_args_t
*args
)
805 xfs_da_state_t
*state
;
806 xfs_da_state_blk_t
*blk
;
811 trace_xfs_attr_node_addname(args
);
814 * Fill in bucket of arguments/results/context to carry around.
819 state
= xfs_da_state_alloc();
824 * Search to see if name already exists, and get back a pointer
825 * to where it should go.
827 error
= xfs_da3_node_lookup_int(state
, &retval
);
830 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
831 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
832 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
834 } else if (retval
== -EEXIST
) {
835 if (args
->flags
& ATTR_CREATE
)
838 trace_xfs_attr_node_replace(args
);
840 /* save the attribute state for later removal*/
841 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
842 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
843 args
->index2
= args
->index
;
844 args
->rmtblkno2
= args
->rmtblkno
;
845 args
->rmtblkcnt2
= args
->rmtblkcnt
;
846 args
->rmtvaluelen2
= args
->rmtvaluelen
;
849 * clear the remote attr state now that it is saved so that the
850 * values reflect the state of the attribute we are about to
851 * add, not the attribute we just found and will remove later.
855 args
->rmtvaluelen
= 0;
858 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
859 if (retval
== -ENOSPC
) {
860 if (state
->path
.active
== 1) {
862 * Its really a single leaf node, but it had
863 * out-of-line values so it looked like it *might*
864 * have been a b-tree.
866 xfs_da_state_free(state
);
868 xfs_defer_init(args
->dfops
, args
->firstblock
);
869 error
= xfs_attr3_leaf_to_node(args
);
871 error
= xfs_defer_finish(&args
->trans
,
875 xfs_defer_cancel(args
->dfops
);
880 * Commit the node conversion and start the next
881 * trans in the chain.
883 error
= xfs_trans_roll(&args
->trans
, dp
);
891 * Split as many Btree elements as required.
892 * This code tracks the new and old attr's location
893 * in the index/blkno/rmtblkno/rmtblkcnt fields and
894 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
896 xfs_defer_init(args
->dfops
, args
->firstblock
);
897 error
= xfs_da3_split(state
);
899 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
902 xfs_defer_cancel(args
->dfops
);
907 * Addition succeeded, update Btree hashvals.
909 xfs_da3_fixhashpath(state
, &state
->path
);
913 * Kill the state structure, we're done with it and need to
914 * allow the buffers to come back later.
916 xfs_da_state_free(state
);
920 * Commit the leaf addition or btree split and start the next
921 * trans in the chain.
923 error
= xfs_trans_roll(&args
->trans
, dp
);
928 * If there was an out-of-line value, allocate the blocks we
929 * identified for its storage and copy the value. This is done
930 * after we create the attribute so that we don't overflow the
931 * maximum size of a transaction and/or hit a deadlock.
933 if (args
->rmtblkno
> 0) {
934 error
= xfs_attr_rmtval_set(args
);
940 * If this is an atomic rename operation, we must "flip" the
941 * incomplete flags on the "new" and "old" attribute/value pairs
942 * so that one disappears and one appears atomically. Then we
943 * must remove the "old" attribute/value pair.
945 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
947 * In a separate transaction, set the incomplete flag on the
948 * "old" attr and clear the incomplete flag on the "new" attr.
950 error
= xfs_attr3_leaf_flipflags(args
);
955 * Dismantle the "old" attribute/value pair by removing
956 * a "remote" value (if it exists).
958 args
->index
= args
->index2
;
959 args
->blkno
= args
->blkno2
;
960 args
->rmtblkno
= args
->rmtblkno2
;
961 args
->rmtblkcnt
= args
->rmtblkcnt2
;
962 args
->rmtvaluelen
= args
->rmtvaluelen2
;
963 if (args
->rmtblkno
) {
964 error
= xfs_attr_rmtval_remove(args
);
970 * Re-find the "old" attribute entry after any split ops.
971 * The INCOMPLETE flag means that we will find the "old"
972 * attr, not the "new" one.
974 args
->flags
|= XFS_ATTR_INCOMPLETE
;
975 state
= xfs_da_state_alloc();
979 error
= xfs_da3_node_lookup_int(state
, &retval
);
984 * Remove the name and update the hashvals in the tree.
986 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
987 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
988 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
989 xfs_da3_fixhashpath(state
, &state
->path
);
992 * Check to see if the tree needs to be collapsed.
994 if (retval
&& (state
->path
.active
> 1)) {
995 xfs_defer_init(args
->dfops
, args
->firstblock
);
996 error
= xfs_da3_join(state
);
998 error
= xfs_defer_finish(&args
->trans
,
1002 xfs_defer_cancel(args
->dfops
);
1008 * Commit and start the next trans in the chain.
1010 error
= xfs_trans_roll(&args
->trans
, dp
);
1014 } else if (args
->rmtblkno
> 0) {
1016 * Added a "remote" value, just clear the incomplete flag.
1018 error
= xfs_attr3_leaf_clearflag(args
);
1026 xfs_da_state_free(state
);
1033 * Remove a name from a B-tree attribute list.
1035 * This will involve walking down the Btree, and may involve joining
1036 * leaf nodes and even joining intermediate nodes up to and including
1037 * the root node (a special case of an intermediate node).
1040 xfs_attr_node_removename(xfs_da_args_t
*args
)
1042 xfs_da_state_t
*state
;
1043 xfs_da_state_blk_t
*blk
;
1046 int retval
, error
, forkoff
;
1048 trace_xfs_attr_node_removename(args
);
1051 * Tie a string around our finger to remind us where we are.
1054 state
= xfs_da_state_alloc();
1056 state
->mp
= dp
->i_mount
;
1059 * Search to see if name exists, and get back a pointer to it.
1061 error
= xfs_da3_node_lookup_int(state
, &retval
);
1062 if (error
|| (retval
!= -EEXIST
)) {
1069 * If there is an out-of-line value, de-allocate the blocks.
1070 * This is done before we remove the attribute so that we don't
1071 * overflow the maximum size of a transaction and/or hit a deadlock.
1073 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1074 ASSERT(blk
->bp
!= NULL
);
1075 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1076 if (args
->rmtblkno
> 0) {
1078 * Fill in disk block numbers in the state structure
1079 * so that we can get the buffers back after we commit
1080 * several transactions in the following calls.
1082 error
= xfs_attr_fillstate(state
);
1087 * Mark the attribute as INCOMPLETE, then bunmapi() the
1090 error
= xfs_attr3_leaf_setflag(args
);
1093 error
= xfs_attr_rmtval_remove(args
);
1098 * Refill the state structure with buffers, the prior calls
1099 * released our buffers.
1101 error
= xfs_attr_refillstate(state
);
1107 * Remove the name and update the hashvals in the tree.
1109 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1110 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1111 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1112 xfs_da3_fixhashpath(state
, &state
->path
);
1115 * Check to see if the tree needs to be collapsed.
1117 if (retval
&& (state
->path
.active
> 1)) {
1118 xfs_defer_init(args
->dfops
, args
->firstblock
);
1119 error
= xfs_da3_join(state
);
1121 error
= xfs_defer_finish(&args
->trans
, args
->dfops
, dp
);
1124 xfs_defer_cancel(args
->dfops
);
1128 * Commit the Btree join operation and start a new trans.
1130 error
= xfs_trans_roll(&args
->trans
, dp
);
1136 * If the result is small enough, push it all into the inode.
1138 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1140 * Have to get rid of the copy of this dabuf in the state.
1142 ASSERT(state
->path
.active
== 1);
1143 ASSERT(state
->path
.blk
[0].bp
);
1144 state
->path
.blk
[0].bp
= NULL
;
1146 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1150 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1151 xfs_defer_init(args
->dfops
, args
->firstblock
);
1152 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1153 /* bp is gone due to xfs_da_shrink_inode */
1155 error
= xfs_defer_finish(&args
->trans
,
1159 xfs_defer_cancel(args
->dfops
);
1163 xfs_trans_brelse(args
->trans
, bp
);
1168 xfs_da_state_free(state
);
1173 * Fill in the disk block numbers in the state structure for the buffers
1174 * that are attached to the state structure.
1175 * This is done so that we can quickly reattach ourselves to those buffers
1176 * after some set of transaction commits have released these buffers.
1179 xfs_attr_fillstate(xfs_da_state_t
*state
)
1181 xfs_da_state_path_t
*path
;
1182 xfs_da_state_blk_t
*blk
;
1185 trace_xfs_attr_fillstate(state
->args
);
1188 * Roll down the "path" in the state structure, storing the on-disk
1189 * block number for those buffers in the "path".
1191 path
= &state
->path
;
1192 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1193 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1195 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1198 blk
->disk_blkno
= 0;
1203 * Roll down the "altpath" in the state structure, storing the on-disk
1204 * block number for those buffers in the "altpath".
1206 path
= &state
->altpath
;
1207 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1208 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1210 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1213 blk
->disk_blkno
= 0;
1221 * Reattach the buffers to the state structure based on the disk block
1222 * numbers stored in the state structure.
1223 * This is done after some set of transaction commits have released those
1224 * buffers from our grip.
1227 xfs_attr_refillstate(xfs_da_state_t
*state
)
1229 xfs_da_state_path_t
*path
;
1230 xfs_da_state_blk_t
*blk
;
1233 trace_xfs_attr_refillstate(state
->args
);
1236 * Roll down the "path" in the state structure, storing the on-disk
1237 * block number for those buffers in the "path".
1239 path
= &state
->path
;
1240 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1241 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1242 if (blk
->disk_blkno
) {
1243 error
= xfs_da3_node_read(state
->args
->trans
,
1245 blk
->blkno
, blk
->disk_blkno
,
1246 &blk
->bp
, XFS_ATTR_FORK
);
1255 * Roll down the "altpath" in the state structure, storing the on-disk
1256 * block number for those buffers in the "altpath".
1258 path
= &state
->altpath
;
1259 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1260 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1261 if (blk
->disk_blkno
) {
1262 error
= xfs_da3_node_read(state
->args
->trans
,
1264 blk
->blkno
, blk
->disk_blkno
,
1265 &blk
->bp
, XFS_ATTR_FORK
);
1277 * Look up a filename in a node attribute list.
1279 * This routine gets called for any attribute fork that has more than one
1280 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1281 * "remote" values taking up more blocks.
1284 xfs_attr_node_get(xfs_da_args_t
*args
)
1286 xfs_da_state_t
*state
;
1287 xfs_da_state_blk_t
*blk
;
1291 trace_xfs_attr_node_get(args
);
1293 state
= xfs_da_state_alloc();
1295 state
->mp
= args
->dp
->i_mount
;
1298 * Search to see if name exists, and get back a pointer to it.
1300 error
= xfs_da3_node_lookup_int(state
, &retval
);
1303 } else if (retval
== -EEXIST
) {
1304 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1305 ASSERT(blk
->bp
!= NULL
);
1306 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1309 * Get the value, local or "remote"
1311 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1312 if (!retval
&& (args
->rmtblkno
> 0)
1313 && !(args
->flags
& ATTR_KERNOVAL
)) {
1314 retval
= xfs_attr_rmtval_get(args
);
1319 * If not in a transaction, we have to release all the buffers.
1321 for (i
= 0; i
< state
->path
.active
; i
++) {
1322 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1323 state
->path
.blk
[i
].bp
= NULL
;
1326 xfs_da_state_free(state
);