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 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
138 args
.valuelen
= *valuelenp
;
139 /* Entirely possible to look up a name which doesn't exist */
140 args
.op_flags
= XFS_DA_OP_OKNOENT
;
142 lock_mode
= xfs_ilock_attr_map_shared(ip
);
143 if (!xfs_inode_hasattr(ip
))
145 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
146 error
= xfs_attr_shortform_getvalue(&args
);
147 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
148 error
= xfs_attr_leaf_get(&args
);
150 error
= xfs_attr_node_get(&args
);
151 xfs_iunlock(ip
, lock_mode
);
153 *valuelenp
= args
.valuelen
;
154 return error
== -EEXIST
? 0 : error
;
158 * Calculate how many blocks we need for the new attribute,
162 struct xfs_da_args
*args
,
165 struct xfs_mount
*mp
= args
->dp
->i_mount
;
170 * Determine space new attribute will use, and if it would be
171 * "local" or "remote" (note: local != inline).
173 size
= xfs_attr_leaf_newentsize(args
, local
);
174 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
176 if (size
> (args
->geo
->blksize
/ 2)) {
177 /* Double split possible */
182 * Out of line attribute, cannot double split, but
183 * make room for the attribute value itself.
185 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
187 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
195 struct xfs_inode
*dp
,
196 const unsigned char *name
,
197 unsigned char *value
,
201 struct xfs_mount
*mp
= dp
->i_mount
;
202 struct xfs_da_args args
;
203 struct xfs_bmap_free flist
;
204 struct xfs_trans_res tres
;
205 xfs_fsblock_t firstblock
;
206 int rsvd
= (flags
& ATTR_ROOT
) != 0;
207 int error
, err2
, committed
, local
;
209 XFS_STATS_INC(mp
, xs_attr_set
);
211 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
214 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
219 args
.valuelen
= valuelen
;
220 args
.firstblock
= &firstblock
;
222 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
223 args
.total
= xfs_attr_calc_size(&args
, &local
);
225 error
= xfs_qm_dqattach(dp
, 0);
230 * If the inode doesn't have an attribute fork, add one.
231 * (inode must not be locked when we call this routine)
233 if (XFS_IFORK_Q(dp
) == 0) {
234 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
235 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
237 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
243 * Start our first transaction of the day.
245 * All future transactions during this code must be "chained" off
246 * this one via the trans_dup() call. All transactions will contain
247 * the inode, and the inode will always be marked with trans_ihold().
248 * Since the inode will be locked in all transactions, we must log
249 * the inode in every transaction to let it float upward through
252 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
255 * Root fork attributes can use reserved data blocks for this
256 * operation if necessary
260 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
262 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
263 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
264 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
265 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
266 error
= xfs_trans_reserve(args
.trans
, &tres
, args
.total
, 0);
268 xfs_trans_cancel(args
.trans
);
271 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
273 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
274 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
275 XFS_QMOPT_RES_REGBLKS
);
277 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
278 xfs_trans_cancel(args
.trans
);
282 xfs_trans_ijoin(args
.trans
, dp
, 0);
285 * If the attribute list is non-existent or a shortform list,
286 * upgrade it to a single-leaf-block attribute list.
288 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
289 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
290 dp
->i_d
.di_anextents
== 0)) {
293 * Build initial attribute list (if required).
295 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
296 xfs_attr_shortform_create(&args
);
299 * Try to add the attr to the attribute list in
302 error
= xfs_attr_shortform_addname(&args
);
303 if (error
!= -ENOSPC
) {
305 * Commit the shortform mods, and we're done.
306 * NOTE: this is also the error path (EEXIST, etc).
308 ASSERT(args
.trans
!= NULL
);
311 * If this is a synchronous mount, make sure that
312 * the transaction goes to disk before returning
315 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
316 xfs_trans_set_sync(args
.trans
);
318 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
319 xfs_trans_ichgtime(args
.trans
, dp
,
322 err2
= xfs_trans_commit(args
.trans
);
323 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
325 return error
? error
: err2
;
329 * It won't fit in the shortform, transform to a leaf block.
330 * GROT: another possible req'mt for a double-split btree op.
332 xfs_bmap_init(args
.flist
, args
.firstblock
);
333 error
= xfs_attr_shortform_to_leaf(&args
);
335 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
341 xfs_bmap_cancel(&flist
);
346 * bmap_finish() may have committed the last trans and started
347 * a new one. We need the inode to be in all transactions.
350 xfs_trans_ijoin(args
.trans
, dp
, 0);
353 * Commit the leaf transformation. We'll need another (linked)
354 * transaction to add the new attribute to the leaf.
357 error
= xfs_trans_roll(&args
.trans
, dp
);
363 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
364 error
= xfs_attr_leaf_addname(&args
);
366 error
= xfs_attr_node_addname(&args
);
371 * If this is a synchronous mount, make sure that the
372 * transaction goes to disk before returning to the user.
374 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
375 xfs_trans_set_sync(args
.trans
);
377 if ((flags
& ATTR_KERNOTIME
) == 0)
378 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
381 * Commit the last in the sequence of transactions.
383 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
384 error
= xfs_trans_commit(args
.trans
);
385 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
391 xfs_trans_cancel(args
.trans
);
392 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
397 * Generic handler routine to remove a name from an attribute list.
398 * Transitions attribute list from Btree to shortform as necessary.
402 struct xfs_inode
*dp
,
403 const unsigned char *name
,
406 struct xfs_mount
*mp
= dp
->i_mount
;
407 struct xfs_da_args args
;
408 struct xfs_bmap_free flist
;
409 xfs_fsblock_t firstblock
;
412 XFS_STATS_INC(mp
, xs_attr_remove
);
414 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
417 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
421 args
.firstblock
= &firstblock
;
425 * we have no control over the attribute names that userspace passes us
426 * to remove, so we have to allow the name lookup prior to attribute
429 args
.op_flags
= XFS_DA_OP_OKNOENT
;
431 error
= xfs_qm_dqattach(dp
, 0);
436 * Start our first transaction of the day.
438 * All future transactions during this code must be "chained" off
439 * this one via the trans_dup() call. All transactions will contain
440 * the inode, and the inode will always be marked with trans_ihold().
441 * Since the inode will be locked in all transactions, we must log
442 * the inode in every transaction to let it float upward through
445 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
448 * Root fork attributes can use reserved data blocks for this
449 * operation if necessary
452 if (flags
& ATTR_ROOT
)
453 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
455 error
= xfs_trans_reserve(args
.trans
, &M_RES(mp
)->tr_attrrm
,
456 XFS_ATTRRM_SPACE_RES(mp
), 0);
458 xfs_trans_cancel(args
.trans
);
462 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
464 * No need to make quota reservations here. We expect to release some
465 * blocks not allocate in the common case.
467 xfs_trans_ijoin(args
.trans
, dp
, 0);
469 if (!xfs_inode_hasattr(dp
)) {
471 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
472 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
473 error
= xfs_attr_shortform_remove(&args
);
474 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
475 error
= xfs_attr_leaf_removename(&args
);
477 error
= xfs_attr_node_removename(&args
);
484 * If this is a synchronous mount, make sure that the
485 * transaction goes to disk before returning to the user.
487 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
488 xfs_trans_set_sync(args
.trans
);
490 if ((flags
& ATTR_KERNOTIME
) == 0)
491 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
494 * Commit the last in the sequence of transactions.
496 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
497 error
= xfs_trans_commit(args
.trans
);
498 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
504 xfs_trans_cancel(args
.trans
);
505 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
509 /*========================================================================
510 * External routines when attribute list is inside the inode
511 *========================================================================*/
514 * Add a name to the shortform attribute list structure
515 * This is the external routine.
518 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
520 int newsize
, forkoff
, retval
;
522 trace_xfs_attr_sf_addname(args
);
524 retval
= xfs_attr_shortform_lookup(args
);
525 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
527 } else if (retval
== -EEXIST
) {
528 if (args
->flags
& ATTR_CREATE
)
530 retval
= xfs_attr_shortform_remove(args
);
534 * Since we have removed the old attr, clear ATTR_REPLACE so
535 * that the leaf format add routine won't trip over the attr
538 args
->flags
&= ~ATTR_REPLACE
;
541 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
542 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
545 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
546 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
548 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
552 xfs_attr_shortform_add(args
, forkoff
);
557 /*========================================================================
558 * External routines when attribute list is one block
559 *========================================================================*/
562 * Add a name to the leaf attribute list structure
564 * This leaf block cannot have a "remote" value, we only call this routine
565 * if bmap_one_block() says there is only one block (ie: no remote blks).
568 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
572 int retval
, error
, committed
, forkoff
;
574 trace_xfs_attr_leaf_addname(args
);
577 * Read the (only) block in the attribute list in.
581 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
586 * Look up the given attribute in the leaf block. Figure out if
587 * the given flags produce an error or call for an atomic rename.
589 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
590 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
591 xfs_trans_brelse(args
->trans
, bp
);
593 } else if (retval
== -EEXIST
) {
594 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
595 xfs_trans_brelse(args
->trans
, bp
);
599 trace_xfs_attr_leaf_replace(args
);
601 /* save the attribute state for later removal*/
602 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
603 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
604 args
->index2
= args
->index
;
605 args
->rmtblkno2
= args
->rmtblkno
;
606 args
->rmtblkcnt2
= args
->rmtblkcnt
;
607 args
->rmtvaluelen2
= args
->rmtvaluelen
;
610 * clear the remote attr state now that it is saved so that the
611 * values reflect the state of the attribute we are about to
612 * add, not the attribute we just found and will remove later.
616 args
->rmtvaluelen
= 0;
620 * Add the attribute to the leaf block, transitioning to a Btree
623 retval
= xfs_attr3_leaf_add(bp
, args
);
624 if (retval
== -ENOSPC
) {
626 * Promote the attribute list to the Btree format, then
627 * Commit that transaction so that the node_addname() call
628 * can manage its own transactions.
630 xfs_bmap_init(args
->flist
, args
->firstblock
);
631 error
= xfs_attr3_leaf_to_node(args
);
633 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
639 xfs_bmap_cancel(args
->flist
);
644 * bmap_finish() may have committed the last trans and started
645 * a new one. We need the inode to be in all transactions.
648 xfs_trans_ijoin(args
->trans
, dp
, 0);
651 * Commit the current trans (including the inode) and start
654 error
= xfs_trans_roll(&args
->trans
, dp
);
659 * Fob the whole rest of the problem off on the Btree code.
661 error
= xfs_attr_node_addname(args
);
666 * Commit the transaction that added the attr name so that
667 * later routines can manage their own transactions.
669 error
= xfs_trans_roll(&args
->trans
, dp
);
674 * If there was an out-of-line value, allocate the blocks we
675 * identified for its storage and copy the value. This is done
676 * after we create the attribute so that we don't overflow the
677 * maximum size of a transaction and/or hit a deadlock.
679 if (args
->rmtblkno
> 0) {
680 error
= xfs_attr_rmtval_set(args
);
686 * If this is an atomic rename operation, we must "flip" the
687 * incomplete flags on the "new" and "old" attribute/value pairs
688 * so that one disappears and one appears atomically. Then we
689 * must remove the "old" attribute/value pair.
691 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
693 * In a separate transaction, set the incomplete flag on the
694 * "old" attr and clear the incomplete flag on the "new" attr.
696 error
= xfs_attr3_leaf_flipflags(args
);
701 * Dismantle the "old" attribute/value pair by removing
702 * a "remote" value (if it exists).
704 args
->index
= args
->index2
;
705 args
->blkno
= args
->blkno2
;
706 args
->rmtblkno
= args
->rmtblkno2
;
707 args
->rmtblkcnt
= args
->rmtblkcnt2
;
708 args
->rmtvaluelen
= args
->rmtvaluelen2
;
709 if (args
->rmtblkno
) {
710 error
= xfs_attr_rmtval_remove(args
);
716 * Read in the block containing the "old" attr, then
717 * remove the "old" attr from that block (neat, huh!)
719 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
724 xfs_attr3_leaf_remove(bp
, args
);
727 * If the result is small enough, shrink it all into the inode.
729 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
730 xfs_bmap_init(args
->flist
, args
->firstblock
);
731 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
732 /* bp is gone due to xfs_da_shrink_inode */
734 error
= xfs_bmap_finish(&args
->trans
,
741 xfs_bmap_cancel(args
->flist
);
746 * bmap_finish() may have committed the last trans
747 * and started a new one. We need the inode to be
748 * in all transactions.
751 xfs_trans_ijoin(args
->trans
, dp
, 0);
755 * Commit the remove and start the next trans in series.
757 error
= xfs_trans_roll(&args
->trans
, dp
);
759 } else if (args
->rmtblkno
> 0) {
761 * Added a "remote" value, just clear the incomplete flag.
763 error
= xfs_attr3_leaf_clearflag(args
);
769 * Remove a name from the leaf attribute list structure
771 * This leaf block cannot have a "remote" value, we only call this routine
772 * if bmap_one_block() says there is only one block (ie: no remote blks).
775 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
779 int error
, committed
, forkoff
;
781 trace_xfs_attr_leaf_removename(args
);
784 * Remove the attribute.
788 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
792 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
793 if (error
== -ENOATTR
) {
794 xfs_trans_brelse(args
->trans
, bp
);
798 xfs_attr3_leaf_remove(bp
, args
);
801 * If the result is small enough, shrink it all into the inode.
803 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
804 xfs_bmap_init(args
->flist
, args
->firstblock
);
805 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
806 /* bp is gone due to xfs_da_shrink_inode */
808 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
814 xfs_bmap_cancel(args
->flist
);
819 * bmap_finish() may have committed the last trans and started
820 * a new one. We need the inode to be in all transactions.
823 xfs_trans_ijoin(args
->trans
, dp
, 0);
829 * Look up a name in a leaf attribute list structure.
831 * This leaf block cannot have a "remote" value, we only call this routine
832 * if bmap_one_block() says there is only one block (ie: no remote blks).
835 xfs_attr_leaf_get(xfs_da_args_t
*args
)
840 trace_xfs_attr_leaf_get(args
);
843 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
847 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
848 if (error
!= -EEXIST
) {
849 xfs_trans_brelse(args
->trans
, bp
);
852 error
= xfs_attr3_leaf_getvalue(bp
, args
);
853 xfs_trans_brelse(args
->trans
, bp
);
854 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
855 error
= xfs_attr_rmtval_get(args
);
860 /*========================================================================
861 * External routines when attribute list size > geo->blksize
862 *========================================================================*/
865 * Add a name to a Btree-format attribute list.
867 * This will involve walking down the Btree, and may involve splitting
868 * leaf nodes and even splitting intermediate nodes up to and including
869 * the root node (a special case of an intermediate node).
871 * "Remote" attribute values confuse the issue and atomic rename operations
872 * add a whole extra layer of confusion on top of that.
875 xfs_attr_node_addname(xfs_da_args_t
*args
)
877 xfs_da_state_t
*state
;
878 xfs_da_state_blk_t
*blk
;
881 int committed
, retval
, error
;
883 trace_xfs_attr_node_addname(args
);
886 * Fill in bucket of arguments/results/context to carry around.
891 state
= xfs_da_state_alloc();
896 * Search to see if name already exists, and get back a pointer
897 * to where it should go.
899 error
= xfs_da3_node_lookup_int(state
, &retval
);
902 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
903 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
904 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
906 } else if (retval
== -EEXIST
) {
907 if (args
->flags
& ATTR_CREATE
)
910 trace_xfs_attr_node_replace(args
);
912 /* save the attribute state for later removal*/
913 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
914 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
915 args
->index2
= args
->index
;
916 args
->rmtblkno2
= args
->rmtblkno
;
917 args
->rmtblkcnt2
= args
->rmtblkcnt
;
918 args
->rmtvaluelen2
= args
->rmtvaluelen
;
921 * clear the remote attr state now that it is saved so that the
922 * values reflect the state of the attribute we are about to
923 * add, not the attribute we just found and will remove later.
927 args
->rmtvaluelen
= 0;
930 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
931 if (retval
== -ENOSPC
) {
932 if (state
->path
.active
== 1) {
934 * Its really a single leaf node, but it had
935 * out-of-line values so it looked like it *might*
936 * have been a b-tree.
938 xfs_da_state_free(state
);
940 xfs_bmap_init(args
->flist
, args
->firstblock
);
941 error
= xfs_attr3_leaf_to_node(args
);
943 error
= xfs_bmap_finish(&args
->trans
,
950 xfs_bmap_cancel(args
->flist
);
955 * bmap_finish() may have committed the last trans
956 * and started a new one. We need the inode to be
957 * in all transactions.
960 xfs_trans_ijoin(args
->trans
, dp
, 0);
963 * Commit the node conversion and start the next
964 * trans in the chain.
966 error
= xfs_trans_roll(&args
->trans
, dp
);
974 * Split as many Btree elements as required.
975 * This code tracks the new and old attr's location
976 * in the index/blkno/rmtblkno/rmtblkcnt fields and
977 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
979 xfs_bmap_init(args
->flist
, args
->firstblock
);
980 error
= xfs_da3_split(state
);
982 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
988 xfs_bmap_cancel(args
->flist
);
993 * bmap_finish() may have committed the last trans and started
994 * a new one. We need the inode to be in all transactions.
997 xfs_trans_ijoin(args
->trans
, dp
, 0);
1000 * Addition succeeded, update Btree hashvals.
1002 xfs_da3_fixhashpath(state
, &state
->path
);
1006 * Kill the state structure, we're done with it and need to
1007 * allow the buffers to come back later.
1009 xfs_da_state_free(state
);
1013 * Commit the leaf addition or btree split and start the next
1014 * trans in the chain.
1016 error
= xfs_trans_roll(&args
->trans
, dp
);
1021 * If there was an out-of-line value, allocate the blocks we
1022 * identified for its storage and copy the value. This is done
1023 * after we create the attribute so that we don't overflow the
1024 * maximum size of a transaction and/or hit a deadlock.
1026 if (args
->rmtblkno
> 0) {
1027 error
= xfs_attr_rmtval_set(args
);
1033 * If this is an atomic rename operation, we must "flip" the
1034 * incomplete flags on the "new" and "old" attribute/value pairs
1035 * so that one disappears and one appears atomically. Then we
1036 * must remove the "old" attribute/value pair.
1038 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1040 * In a separate transaction, set the incomplete flag on the
1041 * "old" attr and clear the incomplete flag on the "new" attr.
1043 error
= xfs_attr3_leaf_flipflags(args
);
1048 * Dismantle the "old" attribute/value pair by removing
1049 * a "remote" value (if it exists).
1051 args
->index
= args
->index2
;
1052 args
->blkno
= args
->blkno2
;
1053 args
->rmtblkno
= args
->rmtblkno2
;
1054 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1055 args
->rmtvaluelen
= args
->rmtvaluelen2
;
1056 if (args
->rmtblkno
) {
1057 error
= xfs_attr_rmtval_remove(args
);
1063 * Re-find the "old" attribute entry after any split ops.
1064 * The INCOMPLETE flag means that we will find the "old"
1065 * attr, not the "new" one.
1067 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1068 state
= xfs_da_state_alloc();
1072 error
= xfs_da3_node_lookup_int(state
, &retval
);
1077 * Remove the name and update the hashvals in the tree.
1079 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1080 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1081 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1082 xfs_da3_fixhashpath(state
, &state
->path
);
1085 * Check to see if the tree needs to be collapsed.
1087 if (retval
&& (state
->path
.active
> 1)) {
1088 xfs_bmap_init(args
->flist
, args
->firstblock
);
1089 error
= xfs_da3_join(state
);
1091 error
= xfs_bmap_finish(&args
->trans
,
1098 xfs_bmap_cancel(args
->flist
);
1103 * bmap_finish() may have committed the last trans
1104 * and started a new one. We need the inode to be
1105 * in all transactions.
1108 xfs_trans_ijoin(args
->trans
, dp
, 0);
1112 * Commit and start the next trans in the chain.
1114 error
= xfs_trans_roll(&args
->trans
, dp
);
1118 } else if (args
->rmtblkno
> 0) {
1120 * Added a "remote" value, just clear the incomplete flag.
1122 error
= xfs_attr3_leaf_clearflag(args
);
1130 xfs_da_state_free(state
);
1137 * Remove a name from a B-tree attribute list.
1139 * This will involve walking down the Btree, and may involve joining
1140 * leaf nodes and even joining intermediate nodes up to and including
1141 * the root node (a special case of an intermediate node).
1144 xfs_attr_node_removename(xfs_da_args_t
*args
)
1146 xfs_da_state_t
*state
;
1147 xfs_da_state_blk_t
*blk
;
1150 int retval
, error
, committed
, forkoff
;
1152 trace_xfs_attr_node_removename(args
);
1155 * Tie a string around our finger to remind us where we are.
1158 state
= xfs_da_state_alloc();
1160 state
->mp
= dp
->i_mount
;
1163 * Search to see if name exists, and get back a pointer to it.
1165 error
= xfs_da3_node_lookup_int(state
, &retval
);
1166 if (error
|| (retval
!= -EEXIST
)) {
1173 * If there is an out-of-line value, de-allocate the blocks.
1174 * This is done before we remove the attribute so that we don't
1175 * overflow the maximum size of a transaction and/or hit a deadlock.
1177 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1178 ASSERT(blk
->bp
!= NULL
);
1179 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1180 if (args
->rmtblkno
> 0) {
1182 * Fill in disk block numbers in the state structure
1183 * so that we can get the buffers back after we commit
1184 * several transactions in the following calls.
1186 error
= xfs_attr_fillstate(state
);
1191 * Mark the attribute as INCOMPLETE, then bunmapi() the
1194 error
= xfs_attr3_leaf_setflag(args
);
1197 error
= xfs_attr_rmtval_remove(args
);
1202 * Refill the state structure with buffers, the prior calls
1203 * released our buffers.
1205 error
= xfs_attr_refillstate(state
);
1211 * Remove the name and update the hashvals in the tree.
1213 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1214 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1215 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1216 xfs_da3_fixhashpath(state
, &state
->path
);
1219 * Check to see if the tree needs to be collapsed.
1221 if (retval
&& (state
->path
.active
> 1)) {
1222 xfs_bmap_init(args
->flist
, args
->firstblock
);
1223 error
= xfs_da3_join(state
);
1225 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1231 xfs_bmap_cancel(args
->flist
);
1236 * bmap_finish() may have committed the last trans and started
1237 * a new one. We need the inode to be in all transactions.
1240 xfs_trans_ijoin(args
->trans
, dp
, 0);
1243 * Commit the Btree join operation and start a new trans.
1245 error
= xfs_trans_roll(&args
->trans
, dp
);
1251 * If the result is small enough, push it all into the inode.
1253 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1255 * Have to get rid of the copy of this dabuf in the state.
1257 ASSERT(state
->path
.active
== 1);
1258 ASSERT(state
->path
.blk
[0].bp
);
1259 state
->path
.blk
[0].bp
= NULL
;
1261 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1265 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1266 xfs_bmap_init(args
->flist
, args
->firstblock
);
1267 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1268 /* bp is gone due to xfs_da_shrink_inode */
1270 error
= xfs_bmap_finish(&args
->trans
,
1277 xfs_bmap_cancel(args
->flist
);
1282 * bmap_finish() may have committed the last trans
1283 * and started a new one. We need the inode to be
1284 * in all transactions.
1287 xfs_trans_ijoin(args
->trans
, dp
, 0);
1289 xfs_trans_brelse(args
->trans
, bp
);
1294 xfs_da_state_free(state
);
1299 * Fill in the disk block numbers in the state structure for the buffers
1300 * that are attached to the state structure.
1301 * This is done so that we can quickly reattach ourselves to those buffers
1302 * after some set of transaction commits have released these buffers.
1305 xfs_attr_fillstate(xfs_da_state_t
*state
)
1307 xfs_da_state_path_t
*path
;
1308 xfs_da_state_blk_t
*blk
;
1311 trace_xfs_attr_fillstate(state
->args
);
1314 * Roll down the "path" in the state structure, storing the on-disk
1315 * block number for those buffers in the "path".
1317 path
= &state
->path
;
1318 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1319 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1321 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1324 blk
->disk_blkno
= 0;
1329 * Roll down the "altpath" in the state structure, storing the on-disk
1330 * block number for those buffers in the "altpath".
1332 path
= &state
->altpath
;
1333 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1334 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1336 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1339 blk
->disk_blkno
= 0;
1347 * Reattach the buffers to the state structure based on the disk block
1348 * numbers stored in the state structure.
1349 * This is done after some set of transaction commits have released those
1350 * buffers from our grip.
1353 xfs_attr_refillstate(xfs_da_state_t
*state
)
1355 xfs_da_state_path_t
*path
;
1356 xfs_da_state_blk_t
*blk
;
1359 trace_xfs_attr_refillstate(state
->args
);
1362 * Roll down the "path" in the state structure, storing the on-disk
1363 * block number for those buffers in the "path".
1365 path
= &state
->path
;
1366 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1367 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1368 if (blk
->disk_blkno
) {
1369 error
= xfs_da3_node_read(state
->args
->trans
,
1371 blk
->blkno
, blk
->disk_blkno
,
1372 &blk
->bp
, XFS_ATTR_FORK
);
1381 * Roll down the "altpath" in the state structure, storing the on-disk
1382 * block number for those buffers in the "altpath".
1384 path
= &state
->altpath
;
1385 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1386 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1387 if (blk
->disk_blkno
) {
1388 error
= xfs_da3_node_read(state
->args
->trans
,
1390 blk
->blkno
, blk
->disk_blkno
,
1391 &blk
->bp
, XFS_ATTR_FORK
);
1403 * Look up a filename in a node attribute list.
1405 * This routine gets called for any attribute fork that has more than one
1406 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1407 * "remote" values taking up more blocks.
1410 xfs_attr_node_get(xfs_da_args_t
*args
)
1412 xfs_da_state_t
*state
;
1413 xfs_da_state_blk_t
*blk
;
1417 trace_xfs_attr_node_get(args
);
1419 state
= xfs_da_state_alloc();
1421 state
->mp
= args
->dp
->i_mount
;
1424 * Search to see if name exists, and get back a pointer to it.
1426 error
= xfs_da3_node_lookup_int(state
, &retval
);
1429 } else if (retval
== -EEXIST
) {
1430 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1431 ASSERT(blk
->bp
!= NULL
);
1432 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1435 * Get the value, local or "remote"
1437 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1438 if (!retval
&& (args
->rmtblkno
> 0)
1439 && !(args
->flags
& ATTR_KERNOVAL
)) {
1440 retval
= xfs_attr_rmtval_get(args
);
1445 * If not in a transaction, we have to release all the buffers.
1447 for (i
= 0; i
< state
->path
.active
; i
++) {
1448 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1449 state
->path
.blk
[i
].bp
= NULL
;
1452 xfs_da_state_free(state
);