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_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_attr_sf.h"
29 #include "xfs_inode.h"
30 #include "xfs_alloc.h"
31 #include "xfs_trans.h"
32 #include "xfs_inode_item.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_bmap_btree.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_attr_remote.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_trans_space.h"
42 #include "xfs_trace.h"
47 * Provide the external interfaces to manage attribute lists.
50 /*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
55 * Internal routines when attribute list fits inside the inode.
57 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
60 * Internal routines when attribute list is one block.
62 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
63 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
64 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
67 * Internal routines when attribute list is more than one block.
69 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
70 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
73 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
78 struct xfs_da_args
*args
,
80 const unsigned char *name
,
87 memset(args
, 0, sizeof(*args
));
88 args
->geo
= dp
->i_mount
->m_attr_geo
;
89 args
->whichfork
= XFS_ATTR_FORK
;
93 args
->namelen
= strlen((const char *)name
);
94 if (args
->namelen
>= MAXNAMELEN
)
95 return -EFAULT
; /* match IRIX behaviour */
97 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
103 struct xfs_inode
*ip
)
105 if (!XFS_IFORK_Q(ip
) ||
106 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
107 ip
->i_d
.di_anextents
== 0))
112 /*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
118 struct xfs_inode
*ip
,
119 const unsigned char *name
,
120 unsigned char *value
,
124 struct xfs_da_args args
;
128 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
130 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
133 if (!xfs_inode_hasattr(ip
))
136 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
141 args
.valuelen
= *valuelenp
;
142 /* Entirely possible to look up a name which doesn't exist */
143 args
.op_flags
= XFS_DA_OP_OKNOENT
;
145 lock_mode
= xfs_ilock_attr_map_shared(ip
);
146 if (!xfs_inode_hasattr(ip
))
148 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
149 error
= xfs_attr_shortform_getvalue(&args
);
150 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
151 error
= xfs_attr_leaf_get(&args
);
153 error
= xfs_attr_node_get(&args
);
154 xfs_iunlock(ip
, lock_mode
);
156 *valuelenp
= args
.valuelen
;
157 return error
== -EEXIST
? 0 : error
;
161 * Calculate how many blocks we need for the new attribute,
165 struct xfs_da_args
*args
,
168 struct xfs_mount
*mp
= args
->dp
->i_mount
;
173 * Determine space new attribute will use, and if it would be
174 * "local" or "remote" (note: local != inline).
176 size
= xfs_attr_leaf_newentsize(args
, local
);
177 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
179 if (size
> (args
->geo
->blksize
/ 2)) {
180 /* Double split possible */
185 * Out of line attribute, cannot double split, but
186 * make room for the attribute value itself.
188 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
190 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
198 struct xfs_inode
*dp
,
199 const unsigned char *name
,
200 unsigned char *value
,
204 struct xfs_mount
*mp
= dp
->i_mount
;
205 struct xfs_da_args args
;
206 struct xfs_bmap_free flist
;
207 struct xfs_trans_res tres
;
208 xfs_fsblock_t firstblock
;
209 int rsvd
= (flags
& ATTR_ROOT
) != 0;
210 int error
, err2
, committed
, local
;
212 XFS_STATS_INC(mp
, xs_attr_set
);
214 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
217 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
222 args
.valuelen
= valuelen
;
223 args
.firstblock
= &firstblock
;
225 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
226 args
.total
= xfs_attr_calc_size(&args
, &local
);
228 error
= xfs_qm_dqattach(dp
, 0);
233 * If the inode doesn't have an attribute fork, add one.
234 * (inode must not be locked when we call this routine)
236 if (XFS_IFORK_Q(dp
) == 0) {
237 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
238 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
240 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
246 * Start our first transaction of the day.
248 * All future transactions during this code must be "chained" off
249 * this one via the trans_dup() call. All transactions will contain
250 * the inode, and the inode will always be marked with trans_ihold().
251 * Since the inode will be locked in all transactions, we must log
252 * the inode in every transaction to let it float upward through
255 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
258 * Root fork attributes can use reserved data blocks for this
259 * operation if necessary
263 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
265 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
266 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
267 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
268 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
269 error
= xfs_trans_reserve(args
.trans
, &tres
, args
.total
, 0);
271 xfs_trans_cancel(args
.trans
);
274 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
276 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
277 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
278 XFS_QMOPT_RES_REGBLKS
);
280 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
281 xfs_trans_cancel(args
.trans
);
285 xfs_trans_ijoin(args
.trans
, dp
, 0);
288 * If the attribute list is non-existent or a shortform list,
289 * upgrade it to a single-leaf-block attribute list.
291 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
292 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
293 dp
->i_d
.di_anextents
== 0)) {
296 * Build initial attribute list (if required).
298 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
299 xfs_attr_shortform_create(&args
);
302 * Try to add the attr to the attribute list in
305 error
= xfs_attr_shortform_addname(&args
);
306 if (error
!= -ENOSPC
) {
308 * Commit the shortform mods, and we're done.
309 * NOTE: this is also the error path (EEXIST, etc).
311 ASSERT(args
.trans
!= NULL
);
314 * If this is a synchronous mount, make sure that
315 * the transaction goes to disk before returning
318 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
319 xfs_trans_set_sync(args
.trans
);
321 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
322 xfs_trans_ichgtime(args
.trans
, dp
,
325 err2
= xfs_trans_commit(args
.trans
);
326 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
328 return error
? error
: err2
;
332 * It won't fit in the shortform, transform to a leaf block.
333 * GROT: another possible req'mt for a double-split btree op.
335 xfs_bmap_init(args
.flist
, args
.firstblock
);
336 error
= xfs_attr_shortform_to_leaf(&args
);
338 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
344 xfs_bmap_cancel(&flist
);
349 * bmap_finish() may have committed the last trans and started
350 * a new one. We need the inode to be in all transactions.
353 xfs_trans_ijoin(args
.trans
, dp
, 0);
356 * Commit the leaf transformation. We'll need another (linked)
357 * transaction to add the new attribute to the leaf.
360 error
= xfs_trans_roll(&args
.trans
, dp
);
366 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
367 error
= xfs_attr_leaf_addname(&args
);
369 error
= xfs_attr_node_addname(&args
);
374 * If this is a synchronous mount, make sure that the
375 * transaction goes to disk before returning to the user.
377 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
378 xfs_trans_set_sync(args
.trans
);
380 if ((flags
& ATTR_KERNOTIME
) == 0)
381 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
384 * Commit the last in the sequence of transactions.
386 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
387 error
= xfs_trans_commit(args
.trans
);
388 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
394 xfs_trans_cancel(args
.trans
);
395 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
400 * Generic handler routine to remove a name from an attribute list.
401 * Transitions attribute list from Btree to shortform as necessary.
405 struct xfs_inode
*dp
,
406 const unsigned char *name
,
409 struct xfs_mount
*mp
= dp
->i_mount
;
410 struct xfs_da_args args
;
411 struct xfs_bmap_free flist
;
412 xfs_fsblock_t firstblock
;
415 XFS_STATS_INC(mp
, xs_attr_remove
);
417 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
420 if (!xfs_inode_hasattr(dp
))
423 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
427 args
.firstblock
= &firstblock
;
431 * we have no control over the attribute names that userspace passes us
432 * to remove, so we have to allow the name lookup prior to attribute
435 args
.op_flags
= XFS_DA_OP_OKNOENT
;
437 error
= xfs_qm_dqattach(dp
, 0);
442 * Start our first transaction of the day.
444 * All future transactions during this code must be "chained" off
445 * this one via the trans_dup() call. All transactions will contain
446 * the inode, and the inode will always be marked with trans_ihold().
447 * Since the inode will be locked in all transactions, we must log
448 * the inode in every transaction to let it float upward through
451 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
454 * Root fork attributes can use reserved data blocks for this
455 * operation if necessary
458 if (flags
& ATTR_ROOT
)
459 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
461 error
= xfs_trans_reserve(args
.trans
, &M_RES(mp
)->tr_attrrm
,
462 XFS_ATTRRM_SPACE_RES(mp
), 0);
464 xfs_trans_cancel(args
.trans
);
468 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
470 * No need to make quota reservations here. We expect to release some
471 * blocks not allocate in the common case.
473 xfs_trans_ijoin(args
.trans
, dp
, 0);
475 if (!xfs_inode_hasattr(dp
)) {
477 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
478 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
479 error
= xfs_attr_shortform_remove(&args
);
480 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
481 error
= xfs_attr_leaf_removename(&args
);
483 error
= xfs_attr_node_removename(&args
);
490 * If this is a synchronous mount, make sure that the
491 * transaction goes to disk before returning to the user.
493 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
494 xfs_trans_set_sync(args
.trans
);
496 if ((flags
& ATTR_KERNOTIME
) == 0)
497 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
500 * Commit the last in the sequence of transactions.
502 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
503 error
= xfs_trans_commit(args
.trans
);
504 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
510 xfs_trans_cancel(args
.trans
);
511 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
515 /*========================================================================
516 * External routines when attribute list is inside the inode
517 *========================================================================*/
520 * Add a name to the shortform attribute list structure
521 * This is the external routine.
524 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
526 int newsize
, forkoff
, retval
;
528 trace_xfs_attr_sf_addname(args
);
530 retval
= xfs_attr_shortform_lookup(args
);
531 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
533 } else if (retval
== -EEXIST
) {
534 if (args
->flags
& ATTR_CREATE
)
536 retval
= xfs_attr_shortform_remove(args
);
540 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
541 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
544 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
545 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
547 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
551 xfs_attr_shortform_add(args
, forkoff
);
556 /*========================================================================
557 * External routines when attribute list is one block
558 *========================================================================*/
561 * Add a name to the leaf attribute list structure
563 * This leaf block cannot have a "remote" value, we only call this routine
564 * if bmap_one_block() says there is only one block (ie: no remote blks).
567 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
571 int retval
, error
, committed
, forkoff
;
573 trace_xfs_attr_leaf_addname(args
);
576 * Read the (only) block in the attribute list in.
580 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
585 * Look up the given attribute in the leaf block. Figure out if
586 * the given flags produce an error or call for an atomic rename.
588 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
589 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
590 xfs_trans_brelse(args
->trans
, bp
);
592 } else if (retval
== -EEXIST
) {
593 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
594 xfs_trans_brelse(args
->trans
, bp
);
598 trace_xfs_attr_leaf_replace(args
);
600 /* save the attribute state for later removal*/
601 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
602 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
603 args
->index2
= args
->index
;
604 args
->rmtblkno2
= args
->rmtblkno
;
605 args
->rmtblkcnt2
= args
->rmtblkcnt
;
606 args
->rmtvaluelen2
= args
->rmtvaluelen
;
609 * clear the remote attr state now that it is saved so that the
610 * values reflect the state of the attribute we are about to
611 * add, not the attribute we just found and will remove later.
615 args
->rmtvaluelen
= 0;
619 * Add the attribute to the leaf block, transitioning to a Btree
622 retval
= xfs_attr3_leaf_add(bp
, args
);
623 if (retval
== -ENOSPC
) {
625 * Promote the attribute list to the Btree format, then
626 * Commit that transaction so that the node_addname() call
627 * can manage its own transactions.
629 xfs_bmap_init(args
->flist
, args
->firstblock
);
630 error
= xfs_attr3_leaf_to_node(args
);
632 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
638 xfs_bmap_cancel(args
->flist
);
643 * bmap_finish() may have committed the last trans and started
644 * a new one. We need the inode to be in all transactions.
647 xfs_trans_ijoin(args
->trans
, dp
, 0);
650 * Commit the current trans (including the inode) and start
653 error
= xfs_trans_roll(&args
->trans
, dp
);
658 * Fob the whole rest of the problem off on the Btree code.
660 error
= xfs_attr_node_addname(args
);
665 * Commit the transaction that added the attr name so that
666 * later routines can manage their own transactions.
668 error
= xfs_trans_roll(&args
->trans
, dp
);
673 * If there was an out-of-line value, allocate the blocks we
674 * identified for its storage and copy the value. This is done
675 * after we create the attribute so that we don't overflow the
676 * maximum size of a transaction and/or hit a deadlock.
678 if (args
->rmtblkno
> 0) {
679 error
= xfs_attr_rmtval_set(args
);
685 * If this is an atomic rename operation, we must "flip" the
686 * incomplete flags on the "new" and "old" attribute/value pairs
687 * so that one disappears and one appears atomically. Then we
688 * must remove the "old" attribute/value pair.
690 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
692 * In a separate transaction, set the incomplete flag on the
693 * "old" attr and clear the incomplete flag on the "new" attr.
695 error
= xfs_attr3_leaf_flipflags(args
);
700 * Dismantle the "old" attribute/value pair by removing
701 * a "remote" value (if it exists).
703 args
->index
= args
->index2
;
704 args
->blkno
= args
->blkno2
;
705 args
->rmtblkno
= args
->rmtblkno2
;
706 args
->rmtblkcnt
= args
->rmtblkcnt2
;
707 args
->rmtvaluelen
= args
->rmtvaluelen2
;
708 if (args
->rmtblkno
) {
709 error
= xfs_attr_rmtval_remove(args
);
715 * Read in the block containing the "old" attr, then
716 * remove the "old" attr from that block (neat, huh!)
718 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
723 xfs_attr3_leaf_remove(bp
, args
);
726 * If the result is small enough, shrink it all into the inode.
728 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
729 xfs_bmap_init(args
->flist
, args
->firstblock
);
730 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
731 /* bp is gone due to xfs_da_shrink_inode */
733 error
= xfs_bmap_finish(&args
->trans
,
740 xfs_bmap_cancel(args
->flist
);
745 * bmap_finish() may have committed the last trans
746 * and started a new one. We need the inode to be
747 * in all transactions.
750 xfs_trans_ijoin(args
->trans
, dp
, 0);
754 * Commit the remove and start the next trans in series.
756 error
= xfs_trans_roll(&args
->trans
, dp
);
758 } else if (args
->rmtblkno
> 0) {
760 * Added a "remote" value, just clear the incomplete flag.
762 error
= xfs_attr3_leaf_clearflag(args
);
768 * Remove a name from the leaf attribute list structure
770 * This leaf block cannot have a "remote" value, we only call this routine
771 * if bmap_one_block() says there is only one block (ie: no remote blks).
774 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
778 int error
, committed
, forkoff
;
780 trace_xfs_attr_leaf_removename(args
);
783 * Remove the attribute.
787 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
791 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
792 if (error
== -ENOATTR
) {
793 xfs_trans_brelse(args
->trans
, bp
);
797 xfs_attr3_leaf_remove(bp
, args
);
800 * If the result is small enough, shrink it all into the inode.
802 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
803 xfs_bmap_init(args
->flist
, args
->firstblock
);
804 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
805 /* bp is gone due to xfs_da_shrink_inode */
807 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
813 xfs_bmap_cancel(args
->flist
);
818 * bmap_finish() may have committed the last trans and started
819 * a new one. We need the inode to be in all transactions.
822 xfs_trans_ijoin(args
->trans
, dp
, 0);
828 * Look up a name in a leaf attribute list structure.
830 * This leaf block cannot have a "remote" value, we only call this routine
831 * if bmap_one_block() says there is only one block (ie: no remote blks).
834 xfs_attr_leaf_get(xfs_da_args_t
*args
)
839 trace_xfs_attr_leaf_get(args
);
842 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
846 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
847 if (error
!= -EEXIST
) {
848 xfs_trans_brelse(args
->trans
, bp
);
851 error
= xfs_attr3_leaf_getvalue(bp
, args
);
852 xfs_trans_brelse(args
->trans
, bp
);
853 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
854 error
= xfs_attr_rmtval_get(args
);
859 /*========================================================================
860 * External routines when attribute list size > geo->blksize
861 *========================================================================*/
864 * Add a name to a Btree-format attribute list.
866 * This will involve walking down the Btree, and may involve splitting
867 * leaf nodes and even splitting intermediate nodes up to and including
868 * the root node (a special case of an intermediate node).
870 * "Remote" attribute values confuse the issue and atomic rename operations
871 * add a whole extra layer of confusion on top of that.
874 xfs_attr_node_addname(xfs_da_args_t
*args
)
876 xfs_da_state_t
*state
;
877 xfs_da_state_blk_t
*blk
;
880 int committed
, retval
, error
;
882 trace_xfs_attr_node_addname(args
);
885 * Fill in bucket of arguments/results/context to carry around.
890 state
= xfs_da_state_alloc();
895 * Search to see if name already exists, and get back a pointer
896 * to where it should go.
898 error
= xfs_da3_node_lookup_int(state
, &retval
);
901 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
902 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
903 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
905 } else if (retval
== -EEXIST
) {
906 if (args
->flags
& ATTR_CREATE
)
909 trace_xfs_attr_node_replace(args
);
911 /* save the attribute state for later removal*/
912 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
913 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
914 args
->index2
= args
->index
;
915 args
->rmtblkno2
= args
->rmtblkno
;
916 args
->rmtblkcnt2
= args
->rmtblkcnt
;
917 args
->rmtvaluelen2
= args
->rmtvaluelen
;
920 * clear the remote attr state now that it is saved so that the
921 * values reflect the state of the attribute we are about to
922 * add, not the attribute we just found and will remove later.
926 args
->rmtvaluelen
= 0;
929 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
930 if (retval
== -ENOSPC
) {
931 if (state
->path
.active
== 1) {
933 * Its really a single leaf node, but it had
934 * out-of-line values so it looked like it *might*
935 * have been a b-tree.
937 xfs_da_state_free(state
);
939 xfs_bmap_init(args
->flist
, args
->firstblock
);
940 error
= xfs_attr3_leaf_to_node(args
);
942 error
= xfs_bmap_finish(&args
->trans
,
949 xfs_bmap_cancel(args
->flist
);
954 * bmap_finish() may have committed the last trans
955 * and started a new one. We need the inode to be
956 * in all transactions.
959 xfs_trans_ijoin(args
->trans
, dp
, 0);
962 * Commit the node conversion and start the next
963 * trans in the chain.
965 error
= xfs_trans_roll(&args
->trans
, dp
);
973 * Split as many Btree elements as required.
974 * This code tracks the new and old attr's location
975 * in the index/blkno/rmtblkno/rmtblkcnt fields and
976 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
978 xfs_bmap_init(args
->flist
, args
->firstblock
);
979 error
= xfs_da3_split(state
);
981 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
987 xfs_bmap_cancel(args
->flist
);
992 * bmap_finish() may have committed the last trans and started
993 * a new one. We need the inode to be in all transactions.
996 xfs_trans_ijoin(args
->trans
, dp
, 0);
999 * Addition succeeded, update Btree hashvals.
1001 xfs_da3_fixhashpath(state
, &state
->path
);
1005 * Kill the state structure, we're done with it and need to
1006 * allow the buffers to come back later.
1008 xfs_da_state_free(state
);
1012 * Commit the leaf addition or btree split and start the next
1013 * trans in the chain.
1015 error
= xfs_trans_roll(&args
->trans
, dp
);
1020 * If there was an out-of-line value, allocate the blocks we
1021 * identified for its storage and copy the value. This is done
1022 * after we create the attribute so that we don't overflow the
1023 * maximum size of a transaction and/or hit a deadlock.
1025 if (args
->rmtblkno
> 0) {
1026 error
= xfs_attr_rmtval_set(args
);
1032 * If this is an atomic rename operation, we must "flip" the
1033 * incomplete flags on the "new" and "old" attribute/value pairs
1034 * so that one disappears and one appears atomically. Then we
1035 * must remove the "old" attribute/value pair.
1037 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1039 * In a separate transaction, set the incomplete flag on the
1040 * "old" attr and clear the incomplete flag on the "new" attr.
1042 error
= xfs_attr3_leaf_flipflags(args
);
1047 * Dismantle the "old" attribute/value pair by removing
1048 * a "remote" value (if it exists).
1050 args
->index
= args
->index2
;
1051 args
->blkno
= args
->blkno2
;
1052 args
->rmtblkno
= args
->rmtblkno2
;
1053 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1054 args
->rmtvaluelen
= args
->rmtvaluelen2
;
1055 if (args
->rmtblkno
) {
1056 error
= xfs_attr_rmtval_remove(args
);
1062 * Re-find the "old" attribute entry after any split ops.
1063 * The INCOMPLETE flag means that we will find the "old"
1064 * attr, not the "new" one.
1066 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1067 state
= xfs_da_state_alloc();
1071 error
= xfs_da3_node_lookup_int(state
, &retval
);
1076 * Remove the name and update the hashvals in the tree.
1078 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1079 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1080 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1081 xfs_da3_fixhashpath(state
, &state
->path
);
1084 * Check to see if the tree needs to be collapsed.
1086 if (retval
&& (state
->path
.active
> 1)) {
1087 xfs_bmap_init(args
->flist
, args
->firstblock
);
1088 error
= xfs_da3_join(state
);
1090 error
= xfs_bmap_finish(&args
->trans
,
1097 xfs_bmap_cancel(args
->flist
);
1102 * bmap_finish() may have committed the last trans
1103 * and started a new one. We need the inode to be
1104 * in all transactions.
1107 xfs_trans_ijoin(args
->trans
, dp
, 0);
1111 * Commit and start the next trans in the chain.
1113 error
= xfs_trans_roll(&args
->trans
, dp
);
1117 } else if (args
->rmtblkno
> 0) {
1119 * Added a "remote" value, just clear the incomplete flag.
1121 error
= xfs_attr3_leaf_clearflag(args
);
1129 xfs_da_state_free(state
);
1136 * Remove a name from a B-tree attribute list.
1138 * This will involve walking down the Btree, and may involve joining
1139 * leaf nodes and even joining intermediate nodes up to and including
1140 * the root node (a special case of an intermediate node).
1143 xfs_attr_node_removename(xfs_da_args_t
*args
)
1145 xfs_da_state_t
*state
;
1146 xfs_da_state_blk_t
*blk
;
1149 int retval
, error
, committed
, forkoff
;
1151 trace_xfs_attr_node_removename(args
);
1154 * Tie a string around our finger to remind us where we are.
1157 state
= xfs_da_state_alloc();
1159 state
->mp
= dp
->i_mount
;
1162 * Search to see if name exists, and get back a pointer to it.
1164 error
= xfs_da3_node_lookup_int(state
, &retval
);
1165 if (error
|| (retval
!= -EEXIST
)) {
1172 * If there is an out-of-line value, de-allocate the blocks.
1173 * This is done before we remove the attribute so that we don't
1174 * overflow the maximum size of a transaction and/or hit a deadlock.
1176 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1177 ASSERT(blk
->bp
!= NULL
);
1178 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1179 if (args
->rmtblkno
> 0) {
1181 * Fill in disk block numbers in the state structure
1182 * so that we can get the buffers back after we commit
1183 * several transactions in the following calls.
1185 error
= xfs_attr_fillstate(state
);
1190 * Mark the attribute as INCOMPLETE, then bunmapi() the
1193 error
= xfs_attr3_leaf_setflag(args
);
1196 error
= xfs_attr_rmtval_remove(args
);
1201 * Refill the state structure with buffers, the prior calls
1202 * released our buffers.
1204 error
= xfs_attr_refillstate(state
);
1210 * Remove the name and update the hashvals in the tree.
1212 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1213 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1214 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1215 xfs_da3_fixhashpath(state
, &state
->path
);
1218 * Check to see if the tree needs to be collapsed.
1220 if (retval
&& (state
->path
.active
> 1)) {
1221 xfs_bmap_init(args
->flist
, args
->firstblock
);
1222 error
= xfs_da3_join(state
);
1224 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1230 xfs_bmap_cancel(args
->flist
);
1235 * bmap_finish() may have committed the last trans and started
1236 * a new one. We need the inode to be in all transactions.
1239 xfs_trans_ijoin(args
->trans
, dp
, 0);
1242 * Commit the Btree join operation and start a new trans.
1244 error
= xfs_trans_roll(&args
->trans
, dp
);
1250 * If the result is small enough, push it all into the inode.
1252 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1254 * Have to get rid of the copy of this dabuf in the state.
1256 ASSERT(state
->path
.active
== 1);
1257 ASSERT(state
->path
.blk
[0].bp
);
1258 state
->path
.blk
[0].bp
= NULL
;
1260 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1264 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1265 xfs_bmap_init(args
->flist
, args
->firstblock
);
1266 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1267 /* bp is gone due to xfs_da_shrink_inode */
1269 error
= xfs_bmap_finish(&args
->trans
,
1276 xfs_bmap_cancel(args
->flist
);
1281 * bmap_finish() may have committed the last trans
1282 * and started a new one. We need the inode to be
1283 * in all transactions.
1286 xfs_trans_ijoin(args
->trans
, dp
, 0);
1288 xfs_trans_brelse(args
->trans
, bp
);
1293 xfs_da_state_free(state
);
1298 * Fill in the disk block numbers in the state structure for the buffers
1299 * that are attached to the state structure.
1300 * This is done so that we can quickly reattach ourselves to those buffers
1301 * after some set of transaction commits have released these buffers.
1304 xfs_attr_fillstate(xfs_da_state_t
*state
)
1306 xfs_da_state_path_t
*path
;
1307 xfs_da_state_blk_t
*blk
;
1310 trace_xfs_attr_fillstate(state
->args
);
1313 * Roll down the "path" in the state structure, storing the on-disk
1314 * block number for those buffers in the "path".
1316 path
= &state
->path
;
1317 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1318 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1320 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1323 blk
->disk_blkno
= 0;
1328 * Roll down the "altpath" in the state structure, storing the on-disk
1329 * block number for those buffers in the "altpath".
1331 path
= &state
->altpath
;
1332 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1333 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1335 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1338 blk
->disk_blkno
= 0;
1346 * Reattach the buffers to the state structure based on the disk block
1347 * numbers stored in the state structure.
1348 * This is done after some set of transaction commits have released those
1349 * buffers from our grip.
1352 xfs_attr_refillstate(xfs_da_state_t
*state
)
1354 xfs_da_state_path_t
*path
;
1355 xfs_da_state_blk_t
*blk
;
1358 trace_xfs_attr_refillstate(state
->args
);
1361 * Roll down the "path" in the state structure, storing the on-disk
1362 * block number for those buffers in the "path".
1364 path
= &state
->path
;
1365 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1366 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1367 if (blk
->disk_blkno
) {
1368 error
= xfs_da3_node_read(state
->args
->trans
,
1370 blk
->blkno
, blk
->disk_blkno
,
1371 &blk
->bp
, XFS_ATTR_FORK
);
1380 * Roll down the "altpath" in the state structure, storing the on-disk
1381 * block number for those buffers in the "altpath".
1383 path
= &state
->altpath
;
1384 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1385 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1386 if (blk
->disk_blkno
) {
1387 error
= xfs_da3_node_read(state
->args
->trans
,
1389 blk
->blkno
, blk
->disk_blkno
,
1390 &blk
->bp
, XFS_ATTR_FORK
);
1402 * Look up a filename in a node attribute list.
1404 * This routine gets called for any attribute fork that has more than one
1405 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1406 * "remote" values taking up more blocks.
1409 xfs_attr_node_get(xfs_da_args_t
*args
)
1411 xfs_da_state_t
*state
;
1412 xfs_da_state_blk_t
*blk
;
1416 trace_xfs_attr_node_get(args
);
1418 state
= xfs_da_state_alloc();
1420 state
->mp
= args
->dp
->i_mount
;
1423 * Search to see if name exists, and get back a pointer to it.
1425 error
= xfs_da3_node_lookup_int(state
, &retval
);
1428 } else if (retval
== -EEXIST
) {
1429 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1430 ASSERT(blk
->bp
!= NULL
);
1431 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1434 * Get the value, local or "remote"
1436 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1437 if (!retval
&& (args
->rmtblkno
> 0)
1438 && !(args
->flags
& ATTR_KERNOVAL
)) {
1439 retval
= xfs_attr_rmtval_get(args
);
1444 * If not in a transaction, we have to release all the buffers.
1446 for (i
= 0; i
< state
->path
.active
; i
++) {
1447 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1448 state
->path
.blk
[i
].bp
= NULL
;
1451 xfs_da_state_free(state
);