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(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
;
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_bmap_free flist
;
205 struct xfs_trans_res tres
;
206 xfs_fsblock_t firstblock
;
207 int rsvd
= (flags
& ATTR_ROOT
) != 0;
208 int error
, err2
, committed
, local
;
210 XFS_STATS_INC(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
);
244 * Start our first transaction of the day.
246 * All future transactions during this code must be "chained" off
247 * this one via the trans_dup() call. All transactions will contain
248 * the inode, and the inode will always be marked with trans_ihold().
249 * Since the inode will be locked in all transactions, we must log
250 * the inode in every transaction to let it float upward through
253 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
256 * Root fork attributes can use reserved data blocks for this
257 * operation if necessary
261 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
263 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
264 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
265 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
266 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
267 error
= xfs_trans_reserve(args
.trans
, &tres
, args
.total
, 0);
269 xfs_trans_cancel(args
.trans
, 0);
272 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
274 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
275 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
276 XFS_QMOPT_RES_REGBLKS
);
278 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
279 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
283 xfs_trans_ijoin(args
.trans
, dp
, 0);
286 * If the attribute list is non-existent or a shortform list,
287 * upgrade it to a single-leaf-block attribute list.
289 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
290 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
291 dp
->i_d
.di_anextents
== 0)) {
294 * Build initial attribute list (if required).
296 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
297 xfs_attr_shortform_create(&args
);
300 * Try to add the attr to the attribute list in
303 error
= xfs_attr_shortform_addname(&args
);
304 if (error
!= -ENOSPC
) {
306 * Commit the shortform mods, and we're done.
307 * NOTE: this is also the error path (EEXIST, etc).
309 ASSERT(args
.trans
!= NULL
);
312 * If this is a synchronous mount, make sure that
313 * the transaction goes to disk before returning
316 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
317 xfs_trans_set_sync(args
.trans
);
319 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
320 xfs_trans_ichgtime(args
.trans
, dp
,
323 err2
= xfs_trans_commit(args
.trans
,
324 XFS_TRANS_RELEASE_LOG_RES
);
325 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
327 return error
? error
: err2
;
331 * It won't fit in the shortform, transform to a leaf block.
332 * GROT: another possible req'mt for a double-split btree op.
334 xfs_bmap_init(args
.flist
, args
.firstblock
);
335 error
= xfs_attr_shortform_to_leaf(&args
);
337 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
343 xfs_bmap_cancel(&flist
);
348 * bmap_finish() may have committed the last trans and started
349 * a new one. We need the inode to be in all transactions.
352 xfs_trans_ijoin(args
.trans
, dp
, 0);
355 * Commit the leaf transformation. We'll need another (linked)
356 * transaction to add the new attribute to the leaf.
359 error
= xfs_trans_roll(&args
.trans
, dp
);
365 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
366 error
= xfs_attr_leaf_addname(&args
);
368 error
= xfs_attr_node_addname(&args
);
373 * If this is a synchronous mount, make sure that the
374 * transaction goes to disk before returning to the user.
376 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
377 xfs_trans_set_sync(args
.trans
);
379 if ((flags
& ATTR_KERNOTIME
) == 0)
380 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
383 * Commit the last in the sequence of transactions.
385 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
386 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
387 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
393 xfs_trans_cancel(args
.trans
,
394 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
396 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
401 * Generic handler routine to remove a name from an attribute list.
402 * Transitions attribute list from Btree to shortform as necessary.
406 struct xfs_inode
*dp
,
407 const unsigned char *name
,
410 struct xfs_mount
*mp
= dp
->i_mount
;
411 struct xfs_da_args args
;
412 struct xfs_bmap_free flist
;
413 xfs_fsblock_t firstblock
;
416 XFS_STATS_INC(xs_attr_remove
);
418 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
421 if (!xfs_inode_hasattr(dp
))
424 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
428 args
.firstblock
= &firstblock
;
432 * we have no control over the attribute names that userspace passes us
433 * to remove, so we have to allow the name lookup prior to attribute
436 args
.op_flags
= XFS_DA_OP_OKNOENT
;
438 error
= xfs_qm_dqattach(dp
, 0);
443 * Start our first transaction of the day.
445 * All future transactions during this code must be "chained" off
446 * this one via the trans_dup() call. All transactions will contain
447 * the inode, and the inode will always be marked with trans_ihold().
448 * Since the inode will be locked in all transactions, we must log
449 * the inode in every transaction to let it float upward through
452 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
455 * Root fork attributes can use reserved data blocks for this
456 * operation if necessary
459 if (flags
& ATTR_ROOT
)
460 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
462 error
= xfs_trans_reserve(args
.trans
, &M_RES(mp
)->tr_attrrm
,
463 XFS_ATTRRM_SPACE_RES(mp
), 0);
465 xfs_trans_cancel(args
.trans
, 0);
469 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
471 * No need to make quota reservations here. We expect to release some
472 * blocks not allocate in the common case.
474 xfs_trans_ijoin(args
.trans
, dp
, 0);
476 if (!xfs_inode_hasattr(dp
)) {
478 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
479 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
480 error
= xfs_attr_shortform_remove(&args
);
481 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
482 error
= xfs_attr_leaf_removename(&args
);
484 error
= xfs_attr_node_removename(&args
);
491 * If this is a synchronous mount, make sure that the
492 * transaction goes to disk before returning to the user.
494 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
495 xfs_trans_set_sync(args
.trans
);
497 if ((flags
& ATTR_KERNOTIME
) == 0)
498 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
501 * Commit the last in the sequence of transactions.
503 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
504 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
505 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
511 xfs_trans_cancel(args
.trans
,
512 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
514 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
518 /*========================================================================
519 * External routines when attribute list is inside the inode
520 *========================================================================*/
523 * Add a name to the shortform attribute list structure
524 * This is the external routine.
527 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
529 int newsize
, forkoff
, retval
;
531 trace_xfs_attr_sf_addname(args
);
533 retval
= xfs_attr_shortform_lookup(args
);
534 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
536 } else if (retval
== -EEXIST
) {
537 if (args
->flags
& ATTR_CREATE
)
539 retval
= xfs_attr_shortform_remove(args
);
543 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
544 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
547 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
548 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
550 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
554 xfs_attr_shortform_add(args
, forkoff
);
559 /*========================================================================
560 * External routines when attribute list is one block
561 *========================================================================*/
564 * Add a name to the leaf attribute list structure
566 * This leaf block cannot have a "remote" value, we only call this routine
567 * if bmap_one_block() says there is only one block (ie: no remote blks).
570 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
574 int retval
, error
, committed
, forkoff
;
576 trace_xfs_attr_leaf_addname(args
);
579 * Read the (only) block in the attribute list in.
583 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
588 * Look up the given attribute in the leaf block. Figure out if
589 * the given flags produce an error or call for an atomic rename.
591 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
592 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
593 xfs_trans_brelse(args
->trans
, bp
);
595 } else if (retval
== -EEXIST
) {
596 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
597 xfs_trans_brelse(args
->trans
, bp
);
601 trace_xfs_attr_leaf_replace(args
);
603 /* save the attribute state for later removal*/
604 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
605 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
606 args
->index2
= args
->index
;
607 args
->rmtblkno2
= args
->rmtblkno
;
608 args
->rmtblkcnt2
= args
->rmtblkcnt
;
609 args
->rmtvaluelen2
= args
->rmtvaluelen
;
612 * clear the remote attr state now that it is saved so that the
613 * values reflect the state of the attribute we are about to
614 * add, not the attribute we just found and will remove later.
618 args
->rmtvaluelen
= 0;
622 * Add the attribute to the leaf block, transitioning to a Btree
625 retval
= xfs_attr3_leaf_add(bp
, args
);
626 if (retval
== -ENOSPC
) {
628 * Promote the attribute list to the Btree format, then
629 * Commit that transaction so that the node_addname() call
630 * can manage its own transactions.
632 xfs_bmap_init(args
->flist
, args
->firstblock
);
633 error
= xfs_attr3_leaf_to_node(args
);
635 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
641 xfs_bmap_cancel(args
->flist
);
646 * bmap_finish() may have committed the last trans and started
647 * a new one. We need the inode to be in all transactions.
650 xfs_trans_ijoin(args
->trans
, dp
, 0);
653 * Commit the current trans (including the inode) and start
656 error
= xfs_trans_roll(&args
->trans
, dp
);
661 * Fob the whole rest of the problem off on the Btree code.
663 error
= xfs_attr_node_addname(args
);
668 * Commit the transaction that added the attr name so that
669 * later routines can manage their own transactions.
671 error
= xfs_trans_roll(&args
->trans
, dp
);
676 * If there was an out-of-line value, allocate the blocks we
677 * identified for its storage and copy the value. This is done
678 * after we create the attribute so that we don't overflow the
679 * maximum size of a transaction and/or hit a deadlock.
681 if (args
->rmtblkno
> 0) {
682 error
= xfs_attr_rmtval_set(args
);
688 * If this is an atomic rename operation, we must "flip" the
689 * incomplete flags on the "new" and "old" attribute/value pairs
690 * so that one disappears and one appears atomically. Then we
691 * must remove the "old" attribute/value pair.
693 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
695 * In a separate transaction, set the incomplete flag on the
696 * "old" attr and clear the incomplete flag on the "new" attr.
698 error
= xfs_attr3_leaf_flipflags(args
);
703 * Dismantle the "old" attribute/value pair by removing
704 * a "remote" value (if it exists).
706 args
->index
= args
->index2
;
707 args
->blkno
= args
->blkno2
;
708 args
->rmtblkno
= args
->rmtblkno2
;
709 args
->rmtblkcnt
= args
->rmtblkcnt2
;
710 args
->rmtvaluelen
= args
->rmtvaluelen2
;
711 if (args
->rmtblkno
) {
712 error
= xfs_attr_rmtval_remove(args
);
718 * Read in the block containing the "old" attr, then
719 * remove the "old" attr from that block (neat, huh!)
721 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
726 xfs_attr3_leaf_remove(bp
, args
);
729 * If the result is small enough, shrink it all into the inode.
731 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
732 xfs_bmap_init(args
->flist
, args
->firstblock
);
733 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
734 /* bp is gone due to xfs_da_shrink_inode */
736 error
= xfs_bmap_finish(&args
->trans
,
743 xfs_bmap_cancel(args
->flist
);
748 * bmap_finish() may have committed the last trans
749 * and started a new one. We need the inode to be
750 * in all transactions.
753 xfs_trans_ijoin(args
->trans
, dp
, 0);
757 * Commit the remove and start the next trans in series.
759 error
= xfs_trans_roll(&args
->trans
, dp
);
761 } else if (args
->rmtblkno
> 0) {
763 * Added a "remote" value, just clear the incomplete flag.
765 error
= xfs_attr3_leaf_clearflag(args
);
771 * Remove a name from the leaf attribute list structure
773 * This leaf block cannot have a "remote" value, we only call this routine
774 * if bmap_one_block() says there is only one block (ie: no remote blks).
777 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
781 int error
, committed
, forkoff
;
783 trace_xfs_attr_leaf_removename(args
);
786 * Remove the attribute.
790 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
794 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
795 if (error
== -ENOATTR
) {
796 xfs_trans_brelse(args
->trans
, bp
);
800 xfs_attr3_leaf_remove(bp
, args
);
803 * If the result is small enough, shrink it all into the inode.
805 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
806 xfs_bmap_init(args
->flist
, args
->firstblock
);
807 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
808 /* bp is gone due to xfs_da_shrink_inode */
810 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
816 xfs_bmap_cancel(args
->flist
);
821 * bmap_finish() may have committed the last trans and started
822 * a new one. We need the inode to be in all transactions.
825 xfs_trans_ijoin(args
->trans
, dp
, 0);
831 * Look up a name in a leaf attribute list structure.
833 * This leaf block cannot have a "remote" value, we only call this routine
834 * if bmap_one_block() says there is only one block (ie: no remote blks).
837 xfs_attr_leaf_get(xfs_da_args_t
*args
)
842 trace_xfs_attr_leaf_get(args
);
845 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
849 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
850 if (error
!= -EEXIST
) {
851 xfs_trans_brelse(args
->trans
, bp
);
854 error
= xfs_attr3_leaf_getvalue(bp
, args
);
855 xfs_trans_brelse(args
->trans
, bp
);
856 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
857 error
= xfs_attr_rmtval_get(args
);
862 /*========================================================================
863 * External routines when attribute list size > geo->blksize
864 *========================================================================*/
867 * Add a name to a Btree-format attribute list.
869 * This will involve walking down the Btree, and may involve splitting
870 * leaf nodes and even splitting intermediate nodes up to and including
871 * the root node (a special case of an intermediate node).
873 * "Remote" attribute values confuse the issue and atomic rename operations
874 * add a whole extra layer of confusion on top of that.
877 xfs_attr_node_addname(xfs_da_args_t
*args
)
879 xfs_da_state_t
*state
;
880 xfs_da_state_blk_t
*blk
;
883 int committed
, retval
, error
;
885 trace_xfs_attr_node_addname(args
);
888 * Fill in bucket of arguments/results/context to carry around.
893 state
= xfs_da_state_alloc();
898 * Search to see if name already exists, and get back a pointer
899 * to where it should go.
901 error
= xfs_da3_node_lookup_int(state
, &retval
);
904 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
905 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
906 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
908 } else if (retval
== -EEXIST
) {
909 if (args
->flags
& ATTR_CREATE
)
912 trace_xfs_attr_node_replace(args
);
914 /* save the attribute state for later removal*/
915 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
916 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
917 args
->index2
= args
->index
;
918 args
->rmtblkno2
= args
->rmtblkno
;
919 args
->rmtblkcnt2
= args
->rmtblkcnt
;
920 args
->rmtvaluelen2
= args
->rmtvaluelen
;
923 * clear the remote attr state now that it is saved so that the
924 * values reflect the state of the attribute we are about to
925 * add, not the attribute we just found and will remove later.
929 args
->rmtvaluelen
= 0;
932 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
933 if (retval
== -ENOSPC
) {
934 if (state
->path
.active
== 1) {
936 * Its really a single leaf node, but it had
937 * out-of-line values so it looked like it *might*
938 * have been a b-tree.
940 xfs_da_state_free(state
);
942 xfs_bmap_init(args
->flist
, args
->firstblock
);
943 error
= xfs_attr3_leaf_to_node(args
);
945 error
= xfs_bmap_finish(&args
->trans
,
952 xfs_bmap_cancel(args
->flist
);
957 * bmap_finish() may have committed the last trans
958 * and started a new one. We need the inode to be
959 * in all transactions.
962 xfs_trans_ijoin(args
->trans
, dp
, 0);
965 * Commit the node conversion and start the next
966 * trans in the chain.
968 error
= xfs_trans_roll(&args
->trans
, dp
);
976 * Split as many Btree elements as required.
977 * This code tracks the new and old attr's location
978 * in the index/blkno/rmtblkno/rmtblkcnt fields and
979 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
981 xfs_bmap_init(args
->flist
, args
->firstblock
);
982 error
= xfs_da3_split(state
);
984 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
990 xfs_bmap_cancel(args
->flist
);
995 * bmap_finish() may have committed the last trans and started
996 * a new one. We need the inode to be in all transactions.
999 xfs_trans_ijoin(args
->trans
, dp
, 0);
1002 * Addition succeeded, update Btree hashvals.
1004 xfs_da3_fixhashpath(state
, &state
->path
);
1008 * Kill the state structure, we're done with it and need to
1009 * allow the buffers to come back later.
1011 xfs_da_state_free(state
);
1015 * Commit the leaf addition or btree split and start the next
1016 * trans in the chain.
1018 error
= xfs_trans_roll(&args
->trans
, dp
);
1023 * If there was an out-of-line value, allocate the blocks we
1024 * identified for its storage and copy the value. This is done
1025 * after we create the attribute so that we don't overflow the
1026 * maximum size of a transaction and/or hit a deadlock.
1028 if (args
->rmtblkno
> 0) {
1029 error
= xfs_attr_rmtval_set(args
);
1035 * If this is an atomic rename operation, we must "flip" the
1036 * incomplete flags on the "new" and "old" attribute/value pairs
1037 * so that one disappears and one appears atomically. Then we
1038 * must remove the "old" attribute/value pair.
1040 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1042 * In a separate transaction, set the incomplete flag on the
1043 * "old" attr and clear the incomplete flag on the "new" attr.
1045 error
= xfs_attr3_leaf_flipflags(args
);
1050 * Dismantle the "old" attribute/value pair by removing
1051 * a "remote" value (if it exists).
1053 args
->index
= args
->index2
;
1054 args
->blkno
= args
->blkno2
;
1055 args
->rmtblkno
= args
->rmtblkno2
;
1056 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1057 args
->rmtvaluelen
= args
->rmtvaluelen2
;
1058 if (args
->rmtblkno
) {
1059 error
= xfs_attr_rmtval_remove(args
);
1065 * Re-find the "old" attribute entry after any split ops.
1066 * The INCOMPLETE flag means that we will find the "old"
1067 * attr, not the "new" one.
1069 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1070 state
= xfs_da_state_alloc();
1074 error
= xfs_da3_node_lookup_int(state
, &retval
);
1079 * Remove the name and update the hashvals in the tree.
1081 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1082 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1083 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1084 xfs_da3_fixhashpath(state
, &state
->path
);
1087 * Check to see if the tree needs to be collapsed.
1089 if (retval
&& (state
->path
.active
> 1)) {
1090 xfs_bmap_init(args
->flist
, args
->firstblock
);
1091 error
= xfs_da3_join(state
);
1093 error
= xfs_bmap_finish(&args
->trans
,
1100 xfs_bmap_cancel(args
->flist
);
1105 * bmap_finish() may have committed the last trans
1106 * and started a new one. We need the inode to be
1107 * in all transactions.
1110 xfs_trans_ijoin(args
->trans
, dp
, 0);
1114 * Commit and start the next trans in the chain.
1116 error
= xfs_trans_roll(&args
->trans
, dp
);
1120 } else if (args
->rmtblkno
> 0) {
1122 * Added a "remote" value, just clear the incomplete flag.
1124 error
= xfs_attr3_leaf_clearflag(args
);
1132 xfs_da_state_free(state
);
1139 * Remove a name from a B-tree attribute list.
1141 * This will involve walking down the Btree, and may involve joining
1142 * leaf nodes and even joining intermediate nodes up to and including
1143 * the root node (a special case of an intermediate node).
1146 xfs_attr_node_removename(xfs_da_args_t
*args
)
1148 xfs_da_state_t
*state
;
1149 xfs_da_state_blk_t
*blk
;
1152 int retval
, error
, committed
, forkoff
;
1154 trace_xfs_attr_node_removename(args
);
1157 * Tie a string around our finger to remind us where we are.
1160 state
= xfs_da_state_alloc();
1162 state
->mp
= dp
->i_mount
;
1165 * Search to see if name exists, and get back a pointer to it.
1167 error
= xfs_da3_node_lookup_int(state
, &retval
);
1168 if (error
|| (retval
!= -EEXIST
)) {
1175 * If there is an out-of-line value, de-allocate the blocks.
1176 * This is done before we remove the attribute so that we don't
1177 * overflow the maximum size of a transaction and/or hit a deadlock.
1179 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1180 ASSERT(blk
->bp
!= NULL
);
1181 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1182 if (args
->rmtblkno
> 0) {
1184 * Fill in disk block numbers in the state structure
1185 * so that we can get the buffers back after we commit
1186 * several transactions in the following calls.
1188 error
= xfs_attr_fillstate(state
);
1193 * Mark the attribute as INCOMPLETE, then bunmapi() the
1196 error
= xfs_attr3_leaf_setflag(args
);
1199 error
= xfs_attr_rmtval_remove(args
);
1204 * Refill the state structure with buffers, the prior calls
1205 * released our buffers.
1207 error
= xfs_attr_refillstate(state
);
1213 * Remove the name and update the hashvals in the tree.
1215 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1216 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1217 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1218 xfs_da3_fixhashpath(state
, &state
->path
);
1221 * Check to see if the tree needs to be collapsed.
1223 if (retval
&& (state
->path
.active
> 1)) {
1224 xfs_bmap_init(args
->flist
, args
->firstblock
);
1225 error
= xfs_da3_join(state
);
1227 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1233 xfs_bmap_cancel(args
->flist
);
1238 * bmap_finish() may have committed the last trans and started
1239 * a new one. We need the inode to be in all transactions.
1242 xfs_trans_ijoin(args
->trans
, dp
, 0);
1245 * Commit the Btree join operation and start a new trans.
1247 error
= xfs_trans_roll(&args
->trans
, dp
);
1253 * If the result is small enough, push it all into the inode.
1255 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1257 * Have to get rid of the copy of this dabuf in the state.
1259 ASSERT(state
->path
.active
== 1);
1260 ASSERT(state
->path
.blk
[0].bp
);
1261 state
->path
.blk
[0].bp
= NULL
;
1263 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1267 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1268 xfs_bmap_init(args
->flist
, args
->firstblock
);
1269 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1270 /* bp is gone due to xfs_da_shrink_inode */
1272 error
= xfs_bmap_finish(&args
->trans
,
1279 xfs_bmap_cancel(args
->flist
);
1284 * bmap_finish() may have committed the last trans
1285 * and started a new one. We need the inode to be
1286 * in all transactions.
1289 xfs_trans_ijoin(args
->trans
, dp
, 0);
1291 xfs_trans_brelse(args
->trans
, bp
);
1296 xfs_da_state_free(state
);
1301 * Fill in the disk block numbers in the state structure for the buffers
1302 * that are attached to the state structure.
1303 * This is done so that we can quickly reattach ourselves to those buffers
1304 * after some set of transaction commits have released these buffers.
1307 xfs_attr_fillstate(xfs_da_state_t
*state
)
1309 xfs_da_state_path_t
*path
;
1310 xfs_da_state_blk_t
*blk
;
1313 trace_xfs_attr_fillstate(state
->args
);
1316 * Roll down the "path" in the state structure, storing the on-disk
1317 * block number for those buffers in the "path".
1319 path
= &state
->path
;
1320 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1321 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1323 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1326 blk
->disk_blkno
= 0;
1331 * Roll down the "altpath" in the state structure, storing the on-disk
1332 * block number for those buffers in the "altpath".
1334 path
= &state
->altpath
;
1335 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1336 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1338 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1341 blk
->disk_blkno
= 0;
1349 * Reattach the buffers to the state structure based on the disk block
1350 * numbers stored in the state structure.
1351 * This is done after some set of transaction commits have released those
1352 * buffers from our grip.
1355 xfs_attr_refillstate(xfs_da_state_t
*state
)
1357 xfs_da_state_path_t
*path
;
1358 xfs_da_state_blk_t
*blk
;
1361 trace_xfs_attr_refillstate(state
->args
);
1364 * Roll down the "path" in the state structure, storing the on-disk
1365 * block number for those buffers in the "path".
1367 path
= &state
->path
;
1368 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1369 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1370 if (blk
->disk_blkno
) {
1371 error
= xfs_da3_node_read(state
->args
->trans
,
1373 blk
->blkno
, blk
->disk_blkno
,
1374 &blk
->bp
, XFS_ATTR_FORK
);
1383 * Roll down the "altpath" in the state structure, storing the on-disk
1384 * block number for those buffers in the "altpath".
1386 path
= &state
->altpath
;
1387 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1388 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1389 if (blk
->disk_blkno
) {
1390 error
= xfs_da3_node_read(state
->args
->trans
,
1392 blk
->blkno
, blk
->disk_blkno
,
1393 &blk
->bp
, XFS_ATTR_FORK
);
1405 * Look up a filename in a node attribute list.
1407 * This routine gets called for any attribute fork that has more than one
1408 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1409 * "remote" values taking up more blocks.
1412 xfs_attr_node_get(xfs_da_args_t
*args
)
1414 xfs_da_state_t
*state
;
1415 xfs_da_state_blk_t
*blk
;
1419 trace_xfs_attr_node_get(args
);
1421 state
= xfs_da_state_alloc();
1423 state
->mp
= args
->dp
->i_mount
;
1426 * Search to see if name exists, and get back a pointer to it.
1428 error
= xfs_da3_node_lookup_int(state
, &retval
);
1431 } else if (retval
== -EEXIST
) {
1432 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1433 ASSERT(blk
->bp
!= NULL
);
1434 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1437 * Get the value, local or "remote"
1439 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1440 if (!retval
&& (args
->rmtblkno
> 0)
1441 && !(args
->flags
& ATTR_KERNOVAL
)) {
1442 retval
= xfs_attr_rmtval_get(args
);
1447 * If not in a transaction, we have to release all the buffers.
1449 for (i
= 0; i
< state
->path
.active
; i
++) {
1450 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1451 state
->path
.blk
[i
].bp
= NULL
;
1454 xfs_da_state_free(state
);