]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/xfs_attr.c
xfs: remove unnecessary dfops init calls in xattr code
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_attr.c
index 303fa7833cc14674ddfcdf363834563483fe2efb..c14e8a346fcea41da713123b422b54fed027af58 100644 (file)
@@ -1,36 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
- * Copyright (c) 2005 Silicon Graphics, Inc.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * Copyright (c) 2000-2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  */
-
-#include <xfs.h>
+#include "libxfs_priv.h"
+#include "xfs_fs.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_log_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_bit.h"
+#include "xfs_mount.h"
+#include "xfs_defer.h"
+#include "xfs_da_format.h"
+#include "xfs_da_btree.h"
+#include "xfs_attr_sf.h"
+#include "xfs_inode.h"
+#include "xfs_alloc.h"
+#include "xfs_trans.h"
+#include "xfs_bmap.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_attr_leaf.h"
+#include "xfs_attr_remote.h"
+#include "xfs_trans_space.h"
+#include "xfs_trace.h"
 
 /*
  * xfs_attr.c
@@ -53,7 +46,6 @@ STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
-STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
 
 /*
  * Internal routines when attribute list is more than one block.
@@ -61,141 +53,218 @@ STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
-STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
 
-/*
- * Routines to manipulate out-of-line attribute values.
- */
-STATIC int xfs_attr_rmtval_get(xfs_da_args_t *args);
-STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
-STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
 
-#define ATTR_RMTVALUE_MAPSIZE  1       /* # of map entries at once */
+STATIC int
+xfs_attr_args_init(
+       struct xfs_da_args      *args,
+       struct xfs_inode        *dp,
+       const unsigned char     *name,
+       int                     flags)
+{
+
+       if (!name)
+               return -EINVAL;
+
+       memset(args, 0, sizeof(*args));
+       args->geo = dp->i_mount->m_attr_geo;
+       args->whichfork = XFS_ATTR_FORK;
+       args->dp = dp;
+       args->flags = flags;
+       args->name = name;
+       args->namelen = strlen((const char *)name);
+       if (args->namelen >= MAXNAMELEN)
+               return -EFAULT;         /* match IRIX behaviour */
+
+       args->hashval = xfs_da_hashname(args->name, args->namelen);
+       return 0;
+}
+
+int
+xfs_inode_hasattr(
+       struct xfs_inode        *ip)
+{
+       if (!XFS_IFORK_Q(ip) ||
+           (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
+            ip->i_d.di_anextents == 0))
+               return 0;
+       return 1;
+}
 
 /*========================================================================
  * Overall external interface routines.
  *========================================================================*/
 
+/* Retrieve an extended attribute and its value.  Must have ilock. */
 int
-xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
-                char *value, int valuelen, int flags)
+xfs_attr_get_ilocked(
+       struct xfs_inode        *ip,
+       struct xfs_da_args      *args)
 {
-       xfs_da_args_t   args;
-       xfs_fsblock_t   firstblock;
-       xfs_bmap_free_t flist;
-       int             error, err2, committed;
-       int             local, size;
-       uint            nblks;
-       xfs_mount_t     *mp = dp->i_mount;
-       int             rsvd = (flags & ATTR_ROOT) != 0;
+       ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
+
+       if (!xfs_inode_hasattr(ip))
+               return -ENOATTR;
+       else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
+               return xfs_attr_shortform_getvalue(args);
+       else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
+               return xfs_attr_leaf_get(args);
+       else
+               return xfs_attr_node_get(args);
+}
 
-       /*
-        * Attach the dquots to the inode.
-        */
-       if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
-               return (error);
+/* Retrieve an extended attribute by name, and its value. */
+int
+xfs_attr_get(
+       struct xfs_inode        *ip,
+       const unsigned char     *name,
+       unsigned char           *value,
+       int                     *valuelenp,
+       int                     flags)
+{
+       struct xfs_da_args      args;
+       uint                    lock_mode;
+       int                     error;
 
-       /*
-        * Determine space new attribute will use, and if it would be
-        * "local" or "remote" (note: local != inline).
-        */
-       size = xfs_attr_leaf_newentsize(namelen, valuelen,
-                                       mp->m_sb.sb_blocksize, &local);
+       XFS_STATS_INC(ip->i_mount, xs_attr_get);
 
-       /*
-        * If the inode doesn't have an attribute fork, add one.
-        * (inode must not be locked when we call this routine)
-        */
-       if (XFS_IFORK_Q(dp) == 0) {
-               if ((error = xfs_bmap_add_attrfork(dp, size, rsvd)))
-                       return(error);
-       }
+       if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+               return -EIO;
+
+       error = xfs_attr_args_init(&args, ip, name, flags);
+       if (error)
+               return error;
 
-       /*
-        * Fill in the arg structure for this request.
-        */
-       memset((char *)&args, 0, sizeof(args));
-       args.name = name;
-       args.namelen = namelen;
        args.value = value;
-       args.valuelen = valuelen;
-       args.flags = flags;
-       args.hashval = xfs_da_hashname(args.name, args.namelen);
-       args.dp = dp;
-       args.firstblock = &firstblock;
-       args.flist = &flist;
-       args.whichfork = XFS_ATTR_FORK;
-       args.addname = 1;
-       args.oknoent = 1;
+       args.valuelen = *valuelenp;
+       /* Entirely possible to look up a name which doesn't exist */
+       args.op_flags = XFS_DA_OP_OKNOENT;
+
+       lock_mode = xfs_ilock_attr_map_shared(ip);
+       error = xfs_attr_get_ilocked(ip, &args);
+       xfs_iunlock(ip, lock_mode);
+
+       *valuelenp = args.valuelen;
+       return error == -EEXIST ? 0 : error;
+}
+
+/*
+ * Calculate how many blocks we need for the new attribute,
+ */
+STATIC int
+xfs_attr_calc_size(
+       struct xfs_da_args      *args,
+       int                     *local)
+{
+       struct xfs_mount        *mp = args->dp->i_mount;
+       int                     size;
+       int                     nblks;
 
+       /*
+        * Determine space new attribute will use, and if it would be
+        * "local" or "remote" (note: local != inline).
+        */
+       size = xfs_attr_leaf_newentsize(args, local);
        nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
-       if (local) {
-               if (size > (mp->m_sb.sb_blocksize >> 1)) {
+       if (*local) {
+               if (size > (args->geo->blksize / 2)) {
                        /* Double split possible */
-                       nblks <<= 1;
+                       nblks *= 2;
                }
        } else {
-               uint    dblocks = XFS_B_TO_FSB(mp, valuelen);
-               /* Out of line attribute, cannot double split, but make
-                * room for the attribute value itself.
+               /*
+                * Out of line attribute, cannot double split, but
+                * make room for the attribute value itself.
                 */
+               uint    dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
                nblks += dblocks;
                nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
        }
 
-       /* Size is now blocks for attribute data */
-       args.total = nblks;
+       return nblks;
+}
+
+int
+xfs_attr_set(
+       struct xfs_inode        *dp,
+       const unsigned char     *name,
+       unsigned char           *value,
+       int                     valuelen,
+       int                     flags)
+{
+       struct xfs_mount        *mp = dp->i_mount;
+       struct xfs_buf          *leaf_bp = NULL;
+       struct xfs_da_args      args;
+       struct xfs_trans_res    tres;
+       int                     rsvd = (flags & ATTR_ROOT) != 0;
+       int                     error, err2, local;
+
+       XFS_STATS_INC(mp, xs_attr_set);
+
+       if (XFS_FORCED_SHUTDOWN(dp->i_mount))
+               return -EIO;
+
+       error = xfs_attr_args_init(&args, dp, name, flags);
+       if (error)
+               return error;
+
+       args.value = value;
+       args.valuelen = valuelen;
+       args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
+       args.total = xfs_attr_calc_size(&args, &local);
+
+       error = xfs_qm_dqattach(dp);
+       if (error)
+               return error;
 
        /*
-        * Start our first transaction of the day.
-        *
-        * All future transactions during this code must be "chained" off
-        * this one via the trans_dup() call.  All transactions will contain
-        * the inode, and the inode will always be marked with trans_ihold().
-        * Since the inode will be locked in all transactions, we must log
-        * the inode in every transaction to let it float upward through
-        * the log.
+        * If the inode doesn't have an attribute fork, add one.
+        * (inode must not be locked when we call this routine)
         */
-       args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
+       if (XFS_IFORK_Q(dp) == 0) {
+               int sf_size = sizeof(xfs_attr_sf_hdr_t) +
+                       XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
+
+               error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
+               if (error)
+                       return error;
+       }
+
+       tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
+                        M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
+       tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
+       tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
 
        /*
         * Root fork attributes can use reserved data blocks for this
         * operation if necessary
         */
+       error = xfs_trans_alloc(mp, &tres, args.total, 0,
+                       rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
+       if (error)
+               return error;
 
-       if (rsvd)
-               args.trans->t_flags |= XFS_TRANS_RESERVE;
-
-       if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
-                                     XFS_ATTRSET_LOG_RES(mp, nblks),
-                                     0, XFS_TRANS_PERM_LOG_RES,
-                                     XFS_ATTRSET_LOG_COUNT))) {
-               xfs_trans_cancel(args.trans, 0);
-               return(error);
-       }
        xfs_ilock(dp, XFS_ILOCK_EXCL);
-
-       error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
-                        rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
-                               XFS_QMOPT_RES_REGBLKS);
+       error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
+                               rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
+                                      XFS_QMOPT_RES_REGBLKS);
        if (error) {
                xfs_iunlock(dp, XFS_ILOCK_EXCL);
-               xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
-               return (error);
+               xfs_trans_cancel(args.trans);
+               return error;
        }
 
-       xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
-       xfs_trans_ihold(args.trans, dp);
+       xfs_trans_ijoin(args.trans, dp, 0);
 
        /*
-        * If the attribute list is non-existant or a shortform list,
+        * If the attribute list is non-existent or a shortform list,
         * upgrade it to a single-leaf-block attribute list.
         */
-       if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
-           ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
-            (dp->i_d.di_anextents == 0))) {
+       if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
+           (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
+            dp->i_d.di_anextents == 0)) {
 
                /*
                 * Build initial attribute list (if required).
@@ -208,7 +277,7 @@ xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
                 * the inode.
                 */
                error = xfs_attr_shortform_addname(&args);
-               if (error != ENOSPC) {
+               if (error != -ENOSPC) {
                        /*
                         * Commit the shortform mods, and we're done.
                         * NOTE: this is also the error path (EEXIST, etc).
@@ -220,224 +289,177 @@ xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
                         * the transaction goes to disk before returning
                         * to the user.
                         */
-                       if (mp->m_flags & XFS_MOUNT_WSYNC) {
+                       if (mp->m_flags & XFS_MOUNT_WSYNC)
                                xfs_trans_set_sync(args.trans);
-                       }
-                       err2 = xfs_trans_commit(args.trans,
-                                                XFS_TRANS_RELEASE_LOG_RES,
-                                                NULL);
-                       xfs_iunlock(dp, XFS_ILOCK_EXCL);
 
-                       /*
-                        * Hit the inode change time.
-                        */
                        if (!error && (flags & ATTR_KERNOTIME) == 0) {
-                               xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
+                               xfs_trans_ichgtime(args.trans, dp,
+                                                       XFS_ICHGTIME_CHG);
                        }
-                       return(error == 0 ? err2 : error);
+                       err2 = xfs_trans_commit(args.trans);
+                       xfs_iunlock(dp, XFS_ILOCK_EXCL);
+
+                       return error ? error : err2;
                }
 
                /*
                 * It won't fit in the shortform, transform to a leaf block.
                 * GROT: another possible req'mt for a double-split btree op.
                 */
-               XFS_BMAP_INIT(args.flist, args.firstblock);
-               error = xfs_attr_shortform_to_leaf(&args);
-               if (!error) {
-                       error = xfs_bmap_finish(&args.trans, args.flist,
-                                               *args.firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args.trans = NULL;
-                       xfs_bmap_cancel(&flist);
+               error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
+               if (error)
                        goto out;
-               }
-
                /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
+                * Prevent the leaf buffer from being unlocked so that a
+                * concurrent AIL push cannot grab the half-baked leaf
+                * buffer and run into problems with the write verifier.
                 */
-               if (committed) {
-                       xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args.trans, dp);
-               }
+               xfs_trans_bhold(args.trans, leaf_bp);
+               xfs_defer_bjoin(args.trans->t_dfops, leaf_bp);
+               xfs_defer_ijoin(args.trans->t_dfops, dp);
+               error = xfs_defer_finish(&args.trans, args.trans->t_dfops);
+               if (error)
+                       goto out;
 
                /*
                 * Commit the leaf transformation.  We'll need another (linked)
-                * transaction to add the new attribute to the leaf.
+                * transaction to add the new attribute to the leaf, which
+                * means that we have to hold & join the leaf buffer here too.
                 */
-               if ((error = xfs_attr_rolltrans(&args.trans, dp)))
+               error = xfs_trans_roll_inode(&args.trans, dp);
+               if (error)
                        goto out;
-
+               xfs_trans_bjoin(args.trans, leaf_bp);
+               leaf_bp = NULL;
        }
 
-       if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
+       if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
                error = xfs_attr_leaf_addname(&args);
-       } else {
+       else
                error = xfs_attr_node_addname(&args);
-       }
-       if (error) {
+       if (error)
                goto out;
-       }
 
        /*
         * If this is a synchronous mount, make sure that the
         * transaction goes to disk before returning to the user.
         */
-       if (mp->m_flags & XFS_MOUNT_WSYNC) {
+       if (mp->m_flags & XFS_MOUNT_WSYNC)
                xfs_trans_set_sync(args.trans);
-       }
+
+       if ((flags & ATTR_KERNOTIME) == 0)
+               xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
 
        /*
         * Commit the last in the sequence of transactions.
         */
        xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
-       error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
-                                NULL);
+       error = xfs_trans_commit(args.trans);
        xfs_iunlock(dp, XFS_ILOCK_EXCL);
 
-       /*
-        * Hit the inode change time.
-        */
-       if (!error && (flags & ATTR_KERNOTIME) == 0) {
-               xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
-       }
-
-       return(error);
+       return error;
 
 out:
+       if (leaf_bp)
+               xfs_trans_brelse(args.trans, leaf_bp);
        if (args.trans)
-               xfs_trans_cancel(args.trans,
-                       XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
+               xfs_trans_cancel(args.trans);
        xfs_iunlock(dp, XFS_ILOCK_EXCL);
-       return(error);
+       return error;
 }
 
-STATIC int
-xfs_attr_remove_int(xfs_inode_t *dp, const char *name, int namelen, int flags)
+/*
+ * Generic handler routine to remove a name from an attribute list.
+ * Transitions attribute list from Btree to shortform as necessary.
+ */
+int
+xfs_attr_remove(
+       struct xfs_inode        *dp,
+       const unsigned char     *name,
+       int                     flags)
 {
-       xfs_da_args_t   args;
-       xfs_fsblock_t   firstblock;
-       xfs_bmap_free_t flist;
-       int             error;
-       xfs_mount_t     *mp = dp->i_mount;
+       struct xfs_mount        *mp = dp->i_mount;
+       struct xfs_da_args      args;
+       int                     error;
 
-       /*
-        * Fill in the arg structure for this request.
-        */
-       memset((char *)&args, 0, sizeof(args));
-       args.name = name;
-       args.namelen = namelen;
-       args.flags = flags;
-       args.hashval = xfs_da_hashname(args.name, args.namelen);
-       args.dp = dp;
-       args.firstblock = &firstblock;
-       args.flist = &flist;
-       args.total = 0;
-       args.whichfork = XFS_ATTR_FORK;
+       XFS_STATS_INC(mp, xs_attr_remove);
 
-       /*
-        * Attach the dquots to the inode.
-        */
-       if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
-               return (error);
+       if (XFS_FORCED_SHUTDOWN(dp->i_mount))
+               return -EIO;
+
+       error = xfs_attr_args_init(&args, dp, name, flags);
+       if (error)
+               return error;
 
        /*
-        * Start our first transaction of the day.
-        *
-        * All future transactions during this code must be "chained" off
-        * this one via the trans_dup() call.  All transactions will contain
-        * the inode, and the inode will always be marked with trans_ihold().
-        * Since the inode will be locked in all transactions, we must log
-        * the inode in every transaction to let it float upward through
-        * the log.
+        * we have no control over the attribute names that userspace passes us
+        * to remove, so we have to allow the name lookup prior to attribute
+        * removal to fail.
         */
-       args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
+       args.op_flags = XFS_DA_OP_OKNOENT;
+
+       error = xfs_qm_dqattach(dp);
+       if (error)
+               return error;
 
        /*
         * Root fork attributes can use reserved data blocks for this
         * operation if necessary
         */
-
-       if (flags & ATTR_ROOT)
-               args.trans->t_flags |= XFS_TRANS_RESERVE;
-
-       if ((error = xfs_trans_reserve(args.trans,
-                                     XFS_ATTRRM_SPACE_RES(mp),
-                                     XFS_ATTRRM_LOG_RES(mp),
-                                     0, XFS_TRANS_PERM_LOG_RES,
-                                     XFS_ATTRRM_LOG_COUNT))) {
-               xfs_trans_cancel(args.trans, 0);
-               return(error);
-       }
+       error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
+                       XFS_ATTRRM_SPACE_RES(mp), 0,
+                       (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
+                       &args.trans);
+       if (error)
+               return error;
 
        xfs_ilock(dp, XFS_ILOCK_EXCL);
        /*
         * No need to make quota reservations here. We expect to release some
         * blocks not allocate in the common case.
         */
-       xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
-       xfs_trans_ihold(args.trans, dp);
+       xfs_trans_ijoin(args.trans, dp, 0);
 
-       /*
-        * Decide on what work routines to call based on the inode size.
-        */
-       if (XFS_IFORK_Q(dp) == 0 ||
-           (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
-            dp->i_d.di_anextents == 0)) {
-               error = XFS_ERROR(ENOATTR);
-               goto out;
-       }
-       if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
+       if (!xfs_inode_hasattr(dp)) {
+               error = -ENOATTR;
+       } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
                ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
                error = xfs_attr_shortform_remove(&args);
-               if (error) {
-                       goto out;
-               }
        } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
                error = xfs_attr_leaf_removename(&args);
        } else {
                error = xfs_attr_node_removename(&args);
        }
-       if (error) {
+
+       if (error)
                goto out;
-       }
 
        /*
         * If this is a synchronous mount, make sure that the
         * transaction goes to disk before returning to the user.
         */
-       if (mp->m_flags & XFS_MOUNT_WSYNC) {
+       if (mp->m_flags & XFS_MOUNT_WSYNC)
                xfs_trans_set_sync(args.trans);
-       }
+
+       if ((flags & ATTR_KERNOTIME) == 0)
+               xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
 
        /*
         * Commit the last in the sequence of transactions.
         */
        xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
-       error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
-                                NULL);
+       error = xfs_trans_commit(args.trans);
        xfs_iunlock(dp, XFS_ILOCK_EXCL);
 
-       /*
-        * Hit the inode change time.
-        */
-       if (!error && (flags & ATTR_KERNOTIME) == 0) {
-               xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
-       }
-
-       return(error);
+       return error;
 
 out:
        if (args.trans)
-               xfs_trans_cancel(args.trans,
-                       XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
+               xfs_trans_cancel(args.trans);
        xfs_iunlock(dp, XFS_ILOCK_EXCL);
-       return(error);
+       return error;
 }
 
-
 /*========================================================================
  * External routines when attribute list is inside the inode
  *========================================================================*/
@@ -451,29 +473,38 @@ xfs_attr_shortform_addname(xfs_da_args_t *args)
 {
        int newsize, forkoff, retval;
 
+       trace_xfs_attr_sf_addname(args);
+
        retval = xfs_attr_shortform_lookup(args);
-       if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
-               return(retval);
-       } else if (retval == EEXIST) {
+       if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
+               return retval;
+       } else if (retval == -EEXIST) {
                if (args->flags & ATTR_CREATE)
-                       return(retval);
+                       return retval;
                retval = xfs_attr_shortform_remove(args);
-               ASSERT(retval == 0);
+               if (retval)
+                       return retval;
+               /*
+                * Since we have removed the old attr, clear ATTR_REPLACE so
+                * that the leaf format add routine won't trip over the attr
+                * not being around.
+                */
+               args->flags &= ~ATTR_REPLACE;
        }
 
        if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
            args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
-               return(XFS_ERROR(ENOSPC));
+               return -ENOSPC;
 
        newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
        newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
 
        forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
        if (!forkoff)
-               return(XFS_ERROR(ENOSPC));
+               return -ENOSPC;
 
        xfs_attr_shortform_add(args, forkoff);
-       return(0);
+       return 0;
 }
 
 
@@ -487,98 +518,100 @@ xfs_attr_shortform_addname(xfs_da_args_t *args)
  * This leaf block cannot have a "remote" value, we only call this routine
  * if bmap_one_block() says there is only one block (ie: no remote blks).
  */
-int
-xfs_attr_leaf_addname(xfs_da_args_t *args)
+STATIC int
+xfs_attr_leaf_addname(
+       struct xfs_da_args      *args)
 {
-       xfs_inode_t *dp;
-       xfs_dabuf_t *bp;
-       int retval, error, committed, forkoff;
+       struct xfs_inode        *dp;
+       struct xfs_buf          *bp;
+       int                     retval, error, forkoff;
+
+       trace_xfs_attr_leaf_addname(args);
 
        /*
         * Read the (only) block in the attribute list in.
         */
        dp = args->dp;
        args->blkno = 0;
-       error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
-                                            XFS_ATTR_FORK);
+       error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
        if (error)
-               return(error);
-       ASSERT(bp != NULL);
+               return error;
 
        /*
         * Look up the given attribute in the leaf block.  Figure out if
         * the given flags produce an error or call for an atomic rename.
         */
-       retval = xfs_attr_leaf_lookup_int(bp, args);
-       if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
-               xfs_da_brelse(args->trans, bp);
-               return(retval);
-       } else if (retval == EEXIST) {
+       retval = xfs_attr3_leaf_lookup_int(bp, args);
+       if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
+               xfs_trans_brelse(args->trans, bp);
+               return retval;
+       } else if (retval == -EEXIST) {
                if (args->flags & ATTR_CREATE) {        /* pure create op */
-                       xfs_da_brelse(args->trans, bp);
-                       return(retval);
+                       xfs_trans_brelse(args->trans, bp);
+                       return retval;
                }
-               args->rename = 1;                       /* an atomic rename */
+
+               trace_xfs_attr_leaf_replace(args);
+
+               /* save the attribute state for later removal*/
+               args->op_flags |= XFS_DA_OP_RENAME;     /* an atomic rename */
                args->blkno2 = args->blkno;             /* set 2nd entry info*/
                args->index2 = args->index;
                args->rmtblkno2 = args->rmtblkno;
                args->rmtblkcnt2 = args->rmtblkcnt;
+               args->rmtvaluelen2 = args->rmtvaluelen;
+
+               /*
+                * clear the remote attr state now that it is saved so that the
+                * values reflect the state of the attribute we are about to
+                * add, not the attribute we just found and will remove later.
+                */
+               args->rmtblkno = 0;
+               args->rmtblkcnt = 0;
+               args->rmtvaluelen = 0;
        }
 
        /*
         * Add the attribute to the leaf block, transitioning to a Btree
         * if required.
         */
-       retval = xfs_attr_leaf_add(bp, args);
-       xfs_da_buf_done(bp);
-       if (retval == ENOSPC) {
+       retval = xfs_attr3_leaf_add(bp, args);
+       if (retval == -ENOSPC) {
                /*
                 * Promote the attribute list to the Btree format, then
                 * Commit that transaction so that the node_addname() call
                 * can manage its own transactions.
                 */
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               error = xfs_attr_leaf_to_node(args);
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       return(error);
-               }
-
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, dp);
-               }
+               error = xfs_attr3_leaf_to_node(args);
+               if (error)
+                       goto out_defer_cancel;
+               xfs_defer_ijoin(args->trans->t_dfops, dp);
+               error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+               if (error)
+                       goto out_defer_cancel;
 
                /*
                 * Commit the current trans (including the inode) and start
                 * a new one.
                 */
-               if ((error = xfs_attr_rolltrans(&args->trans, dp)))
-                       return (error);
+               error = xfs_trans_roll_inode(&args->trans, dp);
+               if (error)
+                       return error;
 
                /*
                 * Fob the whole rest of the problem off on the Btree code.
                 */
                error = xfs_attr_node_addname(args);
-               return(error);
+               return error;
        }
 
        /*
         * Commit the transaction that added the attr name so that
         * later routines can manage their own transactions.
         */
-       if ((error = xfs_attr_rolltrans(&args->trans, dp)))
-               return (error);
+       error = xfs_trans_roll_inode(&args->trans, dp);
+       if (error)
+               return error;
 
        /*
         * If there was an out-of-line value, allocate the blocks we
@@ -589,7 +622,7 @@ xfs_attr_leaf_addname(xfs_da_args_t *args)
        if (args->rmtblkno > 0) {
                error = xfs_attr_rmtval_set(args);
                if (error)
-                       return(error);
+                       return error;
        }
 
        /*
@@ -598,14 +631,14 @@ xfs_attr_leaf_addname(xfs_da_args_t *args)
         * so that one disappears and one appears atomically.  Then we
         * must remove the "old" attribute/value pair.
         */
-       if (args->rename) {
+       if (args->op_flags & XFS_DA_OP_RENAME) {
                /*
                 * In a separate transaction, set the incomplete flag on the
                 * "old" attr and clear the incomplete flag on the "new" attr.
                 */
-               error = xfs_attr_leaf_flipflags(args);
+               error = xfs_attr3_leaf_flipflags(args);
                if (error)
-                       return(error);
+                       return error;
 
                /*
                 * Dismantle the "old" attribute/value pair by removing
@@ -615,67 +648,53 @@ xfs_attr_leaf_addname(xfs_da_args_t *args)
                args->blkno = args->blkno2;
                args->rmtblkno = args->rmtblkno2;
                args->rmtblkcnt = args->rmtblkcnt2;
+               args->rmtvaluelen = args->rmtvaluelen2;
                if (args->rmtblkno) {
                        error = xfs_attr_rmtval_remove(args);
                        if (error)
-                               return(error);
+                               return error;
                }
 
                /*
                 * Read in the block containing the "old" attr, then
                 * remove the "old" attr from that block (neat, huh!)
                 */
-               error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
-                                                    &bp, XFS_ATTR_FORK);
+               error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
+                                          -1, &bp);
                if (error)
-                       return(error);
-               ASSERT(bp != NULL);
-               (void)xfs_attr_leaf_remove(bp, args);
+                       return error;
+
+               xfs_attr3_leaf_remove(bp, args);
 
                /*
                 * If the result is small enough, shrink it all into the inode.
                 */
                if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
-                       XFS_BMAP_INIT(args->flist, args->firstblock);
-                       error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
+                       error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
                        /* bp is gone due to xfs_da_shrink_inode */
-                       if (!error) {
-                               error = xfs_bmap_finish(&args->trans,
-                                                       args->flist,
-                                                       *args->firstblock,
-                                                       &committed);
-                       }
-                       if (error) {
-                               ASSERT(committed);
-                               args->trans = NULL;
-                               xfs_bmap_cancel(args->flist);
-                               return(error);
-                       }
-
-                       /*
-                        * bmap_finish() may have committed the last trans
-                        * and started a new one.  We need the inode to be
-                        * in all transactions.
-                        */
-                       if (committed) {
-                               xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                               xfs_trans_ihold(args->trans, dp);
-                       }
-               } else
-                       xfs_da_buf_done(bp);
+                       if (error)
+                               goto out_defer_cancel;
+                       xfs_defer_ijoin(args->trans->t_dfops, dp);
+                       error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+                       if (error)
+                               goto out_defer_cancel;
+               }
 
                /*
                 * Commit the remove and start the next trans in series.
                 */
-               error = xfs_attr_rolltrans(&args->trans, dp);
+               error = xfs_trans_roll_inode(&args->trans, dp);
 
        } else if (args->rmtblkno > 0) {
                /*
                 * Added a "remote" value, just clear the incomplete flag.
                 */
-               error = xfs_attr_leaf_clearflag(args);
+               error = xfs_attr3_leaf_clearflag(args);
        }
-       return(error);
+       return error;
+out_defer_cancel:
+       xfs_defer_cancel(args->trans->t_dfops);
+       return error;
 }
 
 /*
@@ -685,65 +704,85 @@ xfs_attr_leaf_addname(xfs_da_args_t *args)
  * if bmap_one_block() says there is only one block (ie: no remote blks).
  */
 STATIC int
-xfs_attr_leaf_removename(xfs_da_args_t *args)
+xfs_attr_leaf_removename(
+       struct xfs_da_args      *args)
 {
-       xfs_inode_t *dp;
-       xfs_dabuf_t *bp;
-       int error, committed, forkoff;
+       struct xfs_inode        *dp;
+       struct xfs_buf          *bp;
+       int                     error, forkoff;
+
+       trace_xfs_attr_leaf_removename(args);
 
        /*
         * Remove the attribute.
         */
        dp = args->dp;
        args->blkno = 0;
-       error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
-                                            XFS_ATTR_FORK);
-       if (error) {
-               return(error);
-       }
+       error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
+       if (error)
+               return error;
 
-       ASSERT(bp != NULL);
-       error = xfs_attr_leaf_lookup_int(bp, args);
-       if (error == ENOATTR) {
-               xfs_da_brelse(args->trans, bp);
-               return(error);
+       error = xfs_attr3_leaf_lookup_int(bp, args);
+       if (error == -ENOATTR) {
+               xfs_trans_brelse(args->trans, bp);
+               return error;
        }
 
-       (void)xfs_attr_leaf_remove(bp, args);
+       xfs_attr3_leaf_remove(bp, args);
 
        /*
         * If the result is small enough, shrink it all into the inode.
         */
        if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
+               error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
                /* bp is gone due to xfs_da_shrink_inode */
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       return(error);
-               }
+               if (error)
+                       goto out_defer_cancel;
+               xfs_defer_ijoin(args->trans->t_dfops, dp);
+               error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+               if (error)
+                       goto out_defer_cancel;
+       }
+       return 0;
+out_defer_cancel:
+       xfs_defer_cancel(args->trans->t_dfops);
+       return error;
+}
 
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, dp);
-               }
-       } else
-               xfs_da_buf_done(bp);
-       return(0);
+/*
+ * Look up a name in a leaf attribute list structure.
+ *
+ * This leaf block cannot have a "remote" value, we only call this routine
+ * if bmap_one_block() says there is only one block (ie: no remote blks).
+ */
+STATIC int
+xfs_attr_leaf_get(xfs_da_args_t *args)
+{
+       struct xfs_buf *bp;
+       int error;
+
+       trace_xfs_attr_leaf_get(args);
+
+       args->blkno = 0;
+       error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
+       if (error)
+               return error;
+
+       error = xfs_attr3_leaf_lookup_int(bp, args);
+       if (error != -EEXIST)  {
+               xfs_trans_brelse(args->trans, bp);
+               return error;
+       }
+       error = xfs_attr3_leaf_getvalue(bp, args);
+       xfs_trans_brelse(args->trans, bp);
+       if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
+               error = xfs_attr_rmtval_get(args);
+       }
+       return error;
 }
 
 /*========================================================================
- * External routines when attribute list size > XFS_LBSIZE(mp).
+ * External routines when attribute list size > geo->blksize
  *========================================================================*/
 
 /*
@@ -757,13 +796,16 @@ xfs_attr_leaf_removename(xfs_da_args_t *args)
  * add a whole extra layer of confusion on top of that.
  */
 STATIC int
-xfs_attr_node_addname(xfs_da_args_t *args)
+xfs_attr_node_addname(
+       struct xfs_da_args      *args)
 {
-       xfs_da_state_t *state;
-       xfs_da_state_blk_t *blk;
-       xfs_inode_t *dp;
-       xfs_mount_t *mp;
-       int committed, retval, error;
+       struct xfs_da_state     *state;
+       struct xfs_da_state_blk *blk;
+       struct xfs_inode        *dp;
+       struct xfs_mount        *mp;
+       int                     retval, error;
+
+       trace_xfs_attr_node_addname(args);
 
        /*
         * Fill in bucket of arguments/results/context to carry around.
@@ -774,34 +816,44 @@ restart:
        state = xfs_da_state_alloc();
        state->args = args;
        state->mp = mp;
-       state->blocksize = state->mp->m_sb.sb_blocksize;
-       state->node_ents = state->mp->m_attr_node_ents;
 
        /*
         * Search to see if name already exists, and get back a pointer
         * to where it should go.
         */
-       error = xfs_da_node_lookup_int(state, &retval);
+       error = xfs_da3_node_lookup_int(state, &retval);
        if (error)
                goto out;
        blk = &state->path.blk[ state->path.active-1 ];
        ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
-       if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
+       if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
                goto out;
-       } else if (retval == EEXIST) {
+       } else if (retval == -EEXIST) {
                if (args->flags & ATTR_CREATE)
                        goto out;
-               args->rename = 1;                       /* atomic rename op */
+
+               trace_xfs_attr_node_replace(args);
+
+               /* save the attribute state for later removal*/
+               args->op_flags |= XFS_DA_OP_RENAME;     /* atomic rename op */
                args->blkno2 = args->blkno;             /* set 2nd entry info*/
                args->index2 = args->index;
                args->rmtblkno2 = args->rmtblkno;
                args->rmtblkcnt2 = args->rmtblkcnt;
+               args->rmtvaluelen2 = args->rmtvaluelen;
+
+               /*
+                * clear the remote attr state now that it is saved so that the
+                * values reflect the state of the attribute we are about to
+                * add, not the attribute we just found and will remove later.
+                */
                args->rmtblkno = 0;
                args->rmtblkcnt = 0;
+               args->rmtvaluelen = 0;
        }
 
-       retval = xfs_attr_leaf_add(blk->bp, state->args);
-       if (retval == ENOSPC) {
+       retval = xfs_attr3_leaf_add(blk->bp, state->args);
+       if (retval == -ENOSPC) {
                if (state->path.active == 1) {
                        /*
                         * Its really a single leaf node, but it had
@@ -809,36 +861,22 @@ restart:
                         * have been a b-tree.
                         */
                        xfs_da_state_free(state);
-                       XFS_BMAP_INIT(args->flist, args->firstblock);
-                       error = xfs_attr_leaf_to_node(args);
-                       if (!error) {
-                               error = xfs_bmap_finish(&args->trans,
-                                                       args->flist,
-                                                       *args->firstblock,
-                                                       &committed);
-                       }
-                       if (error) {
-                               ASSERT(committed);
-                               args->trans = NULL;
-                               xfs_bmap_cancel(args->flist);
-                               goto out;
-                       }
-
-                       /*
-                        * bmap_finish() may have committed the last trans
-                        * and started a new one.  We need the inode to be
-                        * in all transactions.
-                        */
-                       if (committed) {
-                               xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                               xfs_trans_ihold(args->trans, dp);
-                       }
+                       state = NULL;
+                       error = xfs_attr3_leaf_to_node(args);
+                       if (error)
+                               goto out_defer_cancel;
+                       xfs_defer_ijoin(args->trans->t_dfops, dp);
+                       error = xfs_defer_finish(&args->trans,
+                                                args->trans->t_dfops);
+                       if (error)
+                               goto out_defer_cancel;
 
                        /*
                         * Commit the node conversion and start the next
                         * trans in the chain.
                         */
-                       if ((error = xfs_attr_rolltrans(&args->trans, dp)))
+                       error = xfs_trans_roll_inode(&args->trans, dp);
+                       if (error)
                                goto out;
 
                        goto restart;
@@ -850,32 +888,18 @@ restart:
                 * in the index/blkno/rmtblkno/rmtblkcnt fields and
                 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
                 */
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               error = xfs_da_split(state);
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       goto out;
-               }
-
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, dp);
-               }
+               error = xfs_da3_split(state);
+               if (error)
+                       goto out_defer_cancel;
+               xfs_defer_ijoin(args->trans->t_dfops, dp);
+               error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+               if (error)
+                       goto out_defer_cancel;
        } else {
                /*
                 * Addition succeeded, update Btree hashvals.
                 */
-               xfs_da_fixhashpath(state, &state->path);
+               xfs_da3_fixhashpath(state, &state->path);
        }
 
        /*
@@ -889,7 +913,8 @@ restart:
         * Commit the leaf addition or btree split and start the next
         * trans in the chain.
         */
-       if ((error = xfs_attr_rolltrans(&args->trans, dp)))
+       error = xfs_trans_roll_inode(&args->trans, dp);
+       if (error)
                goto out;
 
        /*
@@ -901,7 +926,7 @@ restart:
        if (args->rmtblkno > 0) {
                error = xfs_attr_rmtval_set(args);
                if (error)
-                       return(error);
+                       return error;
        }
 
        /*
@@ -910,12 +935,12 @@ restart:
         * so that one disappears and one appears atomically.  Then we
         * must remove the "old" attribute/value pair.
         */
-       if (args->rename) {
+       if (args->op_flags & XFS_DA_OP_RENAME) {
                /*
                 * In a separate transaction, set the incomplete flag on the
                 * "old" attr and clear the incomplete flag on the "new" attr.
                 */
-               error = xfs_attr_leaf_flipflags(args);
+               error = xfs_attr3_leaf_flipflags(args);
                if (error)
                        goto out;
 
@@ -927,10 +952,11 @@ restart:
                args->blkno = args->blkno2;
                args->rmtblkno = args->rmtblkno2;
                args->rmtblkcnt = args->rmtblkcnt2;
+               args->rmtvaluelen = args->rmtvaluelen2;
                if (args->rmtblkno) {
                        error = xfs_attr_rmtval_remove(args);
                        if (error)
-                               return(error);
+                               return error;
                }
 
                /*
@@ -942,10 +968,8 @@ restart:
                state = xfs_da_state_alloc();
                state->args = args;
                state->mp = mp;
-               state->blocksize = state->mp->m_sb.sb_blocksize;
-               state->node_ents = state->mp->m_attr_node_ents;
                state->inleaf = 0;
-               error = xfs_da_node_lookup_int(state, &retval);
+               error = xfs_da3_node_lookup_int(state, &retval);
                if (error)
                        goto out;
 
@@ -954,50 +978,34 @@ restart:
                 */
                blk = &state->path.blk[ state->path.active-1 ];
                ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
-               error = xfs_attr_leaf_remove(blk->bp, args);
-               xfs_da_fixhashpath(state, &state->path);
+               error = xfs_attr3_leaf_remove(blk->bp, args);
+               xfs_da3_fixhashpath(state, &state->path);
 
                /*
                 * Check to see if the tree needs to be collapsed.
                 */
                if (retval && (state->path.active > 1)) {
-                       XFS_BMAP_INIT(args->flist, args->firstblock);
-                       error = xfs_da_join(state);
-                       if (!error) {
-                               error = xfs_bmap_finish(&args->trans,
-                                                       args->flist,
-                                                       *args->firstblock,
-                                                       &committed);
-                       }
-                       if (error) {
-                               ASSERT(committed);
-                               args->trans = NULL;
-                               xfs_bmap_cancel(args->flist);
-                               goto out;
-                       }
-
-                       /*
-                        * bmap_finish() may have committed the last trans
-                        * and started a new one.  We need the inode to be
-                        * in all transactions.
-                        */
-                       if (committed) {
-                               xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                               xfs_trans_ihold(args->trans, dp);
-                       }
+                       error = xfs_da3_join(state);
+                       if (error)
+                               goto out_defer_cancel;
+                       xfs_defer_ijoin(args->trans->t_dfops, dp);
+                       error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+                       if (error)
+                               goto out_defer_cancel;
                }
 
                /*
                 * Commit and start the next trans in the chain.
                 */
-               if ((error = xfs_attr_rolltrans(&args->trans, dp)))
+               error = xfs_trans_roll_inode(&args->trans, dp);
+               if (error)
                        goto out;
 
        } else if (args->rmtblkno > 0) {
                /*
                 * Added a "remote" value, just clear the incomplete flag.
                 */
-               error = xfs_attr_leaf_clearflag(args);
+               error = xfs_attr3_leaf_clearflag(args);
                if (error)
                        goto out;
        }
@@ -1007,8 +1015,11 @@ out:
        if (state)
                xfs_da_state_free(state);
        if (error)
-               return(error);
-       return(retval);
+               return error;
+       return retval;
+out_defer_cancel:
+       xfs_defer_cancel(args->trans->t_dfops);
+       goto out;
 }
 
 /*
@@ -1019,13 +1030,16 @@ out:
  * the root node (a special case of an intermediate node).
  */
 STATIC int
-xfs_attr_node_removename(xfs_da_args_t *args)
+xfs_attr_node_removename(
+       struct xfs_da_args      *args)
 {
-       xfs_da_state_t *state;
-       xfs_da_state_blk_t *blk;
-       xfs_inode_t *dp;
-       xfs_dabuf_t *bp;
-       int retval, error, committed, forkoff;
+       struct xfs_da_state     *state;
+       struct xfs_da_state_blk *blk;
+       struct xfs_inode        *dp;
+       struct xfs_buf          *bp;
+       int                     retval, error, forkoff;
+
+       trace_xfs_attr_node_removename(args);
 
        /*
         * Tie a string around our finger to remind us where we are.
@@ -1034,14 +1048,12 @@ xfs_attr_node_removename(xfs_da_args_t *args)
        state = xfs_da_state_alloc();
        state->args = args;
        state->mp = dp->i_mount;
-       state->blocksize = state->mp->m_sb.sb_blocksize;
-       state->node_ents = state->mp->m_attr_node_ents;
 
        /*
         * Search to see if name exists, and get back a pointer to it.
         */
-       error = xfs_da_node_lookup_int(state, &retval);
-       if (error || (retval != EEXIST)) {
+       error = xfs_da3_node_lookup_int(state, &retval);
+       if (error || (retval != -EEXIST)) {
                if (error == 0)
                        error = retval;
                goto out;
@@ -1069,7 +1081,7 @@ xfs_attr_node_removename(xfs_da_args_t *args)
                 * Mark the attribute as INCOMPLETE, then bunmapi() the
                 * remote value.
                 */
-               error = xfs_attr_leaf_setflag(args);
+               error = xfs_attr3_leaf_setflag(args);
                if (error)
                        goto out;
                error = xfs_attr_rmtval_remove(args);
@@ -1090,39 +1102,25 @@ xfs_attr_node_removename(xfs_da_args_t *args)
         */
        blk = &state->path.blk[ state->path.active-1 ];
        ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
-       retval = xfs_attr_leaf_remove(blk->bp, args);
-       xfs_da_fixhashpath(state, &state->path);
+       retval = xfs_attr3_leaf_remove(blk->bp, args);
+       xfs_da3_fixhashpath(state, &state->path);
 
        /*
         * Check to see if the tree needs to be collapsed.
         */
        if (retval && (state->path.active > 1)) {
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               error = xfs_da_join(state);
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       goto out;
-               }
-
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, dp);
-               }
-
+               error = xfs_da3_join(state);
+               if (error)
+                       goto out_defer_cancel;
+               xfs_defer_ijoin(args->trans->t_dfops, dp);
+               error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+               if (error)
+                       goto out_defer_cancel;
                /*
                 * Commit the Btree join operation and start a new trans.
                 */
-               if ((error = xfs_attr_rolltrans(&args->trans, dp)))
+               error = xfs_trans_roll_inode(&args->trans, dp);
+               if (error)
                        goto out;
        }
 
@@ -1135,58 +1133,39 @@ xfs_attr_node_removename(xfs_da_args_t *args)
                 */
                ASSERT(state->path.active == 1);
                ASSERT(state->path.blk[0].bp);
-               xfs_da_buf_done(state->path.blk[0].bp);
                state->path.blk[0].bp = NULL;
 
-               error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
-                                                    XFS_ATTR_FORK);
+               error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
                if (error)
                        goto out;
-               ASSERT(INT_GET(((xfs_attr_leafblock_t *)
-                                     bp->data)->hdr.info.magic, ARCH_CONVERT)
-                                                      == XFS_ATTR_LEAF_MAGIC);
 
                if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
-                       XFS_BMAP_INIT(args->flist, args->firstblock);
-                       error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
+                       error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
                        /* bp is gone due to xfs_da_shrink_inode */
-                       if (!error) {
-                               error = xfs_bmap_finish(&args->trans,
-                                                       args->flist,
-                                                       *args->firstblock,
-                                                       &committed);
-                       }
-                       if (error) {
-                               ASSERT(committed);
-                               args->trans = NULL;
-                               xfs_bmap_cancel(args->flist);
-                               goto out;
-                       }
-
-                       /*
-                        * bmap_finish() may have committed the last trans
-                        * and started a new one.  We need the inode to be
-                        * in all transactions.
-                        */
-                       if (committed) {
-                               xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                               xfs_trans_ihold(args->trans, dp);
-                       }
+                       if (error)
+                               goto out_defer_cancel;
+                       xfs_defer_ijoin(args->trans->t_dfops, dp);
+                       error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
+                       if (error)
+                               goto out_defer_cancel;
                } else
-                       xfs_da_brelse(args->trans, bp);
+                       xfs_trans_brelse(args->trans, bp);
        }
        error = 0;
 
 out:
        xfs_da_state_free(state);
-       return(error);
+       return error;
+out_defer_cancel:
+       xfs_defer_cancel(args->trans->t_dfops);
+       goto out;
 }
 
 /*
  * Fill in the disk block numbers in the state structure for the buffers
  * that are attached to the state structure.
  * This is done so that we can quickly reattach ourselves to those buffers
- * after some set of transaction commit's has released these buffers.
+ * after some set of transaction commits have released these buffers.
  */
 STATIC int
 xfs_attr_fillstate(xfs_da_state_t *state)
@@ -1195,6 +1174,8 @@ xfs_attr_fillstate(xfs_da_state_t *state)
        xfs_da_state_blk_t *blk;
        int level;
 
+       trace_xfs_attr_fillstate(state->args);
+
        /*
         * Roll down the "path" in the state structure, storing the on-disk
         * block number for those buffers in the "path".
@@ -1203,8 +1184,7 @@ xfs_attr_fillstate(xfs_da_state_t *state)
        ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
        for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
                if (blk->bp) {
-                       blk->disk_blkno = xfs_da_blkno(blk->bp);
-                       xfs_da_buf_done(blk->bp);
+                       blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
                        blk->bp = NULL;
                } else {
                        blk->disk_blkno = 0;
@@ -1219,21 +1199,20 @@ xfs_attr_fillstate(xfs_da_state_t *state)
        ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
        for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
                if (blk->bp) {
-                       blk->disk_blkno = xfs_da_blkno(blk->bp);
-                       xfs_da_buf_done(blk->bp);
+                       blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
                        blk->bp = NULL;
                } else {
                        blk->disk_blkno = 0;
                }
        }
 
-       return(0);
+       return 0;
 }
 
 /*
  * Reattach the buffers to the state structure based on the disk block
  * numbers stored in the state structure.
- * This is done after some set of transaction commit's has released those
+ * This is done after some set of transaction commits have released those
  * buffers from our grip.
  */
 STATIC int
@@ -1243,6 +1222,8 @@ xfs_attr_refillstate(xfs_da_state_t *state)
        xfs_da_state_blk_t *blk;
        int level, error;
 
+       trace_xfs_attr_refillstate(state->args);
+
        /*
         * Roll down the "path" in the state structure, storing the on-disk
         * block number for those buffers in the "path".
@@ -1251,12 +1232,12 @@ xfs_attr_refillstate(xfs_da_state_t *state)
        ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
        for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
                if (blk->disk_blkno) {
-                       error = xfs_da_read_buf(state->args->trans,
+                       error = xfs_da3_node_read(state->args->trans,
                                                state->args->dp,
                                                blk->blkno, blk->disk_blkno,
                                                &blk->bp, XFS_ATTR_FORK);
                        if (error)
-                               return(error);
+                               return error;
                } else {
                        blk->bp = NULL;
                }
@@ -1270,249 +1251,70 @@ xfs_attr_refillstate(xfs_da_state_t *state)
        ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
        for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
                if (blk->disk_blkno) {
-                       error = xfs_da_read_buf(state->args->trans,
+                       error = xfs_da3_node_read(state->args->trans,
                                                state->args->dp,
                                                blk->blkno, blk->disk_blkno,
                                                &blk->bp, XFS_ATTR_FORK);
                        if (error)
-                               return(error);
+                               return error;
                } else {
                        blk->bp = NULL;
                }
        }
 
-       return(0);
+       return 0;
 }
 
 /*
- * Write the value associated with an attribute into the out-of-line buffer
- * that we have defined for it.
+ * Look up a filename in a node attribute list.
+ *
+ * This routine gets called for any attribute fork that has more than one
+ * block, ie: both true Btree attr lists and for single-leaf-blocks with
+ * "remote" values taking up more blocks.
  */
 STATIC int
-xfs_attr_rmtval_set(xfs_da_args_t *args)
+xfs_attr_node_get(xfs_da_args_t *args)
 {
-       xfs_mount_t *mp;
-       xfs_fileoff_t lfileoff;
-       xfs_inode_t *dp;
-       xfs_bmbt_irec_t map;
-       xfs_daddr_t dblkno;
-       xfs_caddr_t src;
-       xfs_buf_t *bp;
-       xfs_dablk_t lblkno;
-       int blkcnt, valuelen, nmap, error, tmp, committed;
+       xfs_da_state_t *state;
+       xfs_da_state_blk_t *blk;
+       int error, retval;
+       int i;
 
-       dp = args->dp;
-       mp = dp->i_mount;
-       src = args->value;
+       trace_xfs_attr_node_get(args);
 
-       /*
-        * Find a "hole" in the attribute address space large enough for
-        * us to drop the new attribute's value into.
-        */
-       blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
-       lfileoff = 0;
-       error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
-                                                  XFS_ATTR_FORK);
-       if (error) {
-               return(error);
-       }
-       args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
-       args->rmtblkcnt = blkcnt;
+       state = xfs_da_state_alloc();
+       state->args = args;
+       state->mp = args->dp->i_mount;
 
        /*
-        * Roll through the "value", allocating blocks on disk as required.
+        * Search to see if name exists, and get back a pointer to it.
         */
-       while (blkcnt > 0) {
-               /*
-                * Allocate a single extent, up to the size of the value.
-                */
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               nmap = 1;
-               error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
-                                 blkcnt,
-                                 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
-                                                       XFS_BMAPI_WRITE,
-                                 args->firstblock, args->total, &map, &nmap,
-                                 args->flist);
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       return(error);
-               }
-
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, dp);
-               }
-
-               ASSERT(nmap == 1);
-               ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
-                      (map.br_startblock != HOLESTARTBLOCK));
-               lblkno += map.br_blockcount;
-               blkcnt -= map.br_blockcount;
-
-               /*
-                * Start the next trans in the chain.
-                */
-               if ((error = xfs_attr_rolltrans(&args->trans, dp)))
-                       return (error);
-       }
+       error = xfs_da3_node_lookup_int(state, &retval);
+       if (error) {
+               retval = error;
+       } else if (retval == -EEXIST) {
+               blk = &state->path.blk[ state->path.active-1 ];
+               ASSERT(blk->bp != NULL);
+               ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
 
-       /*
-        * Roll through the "value", copying the attribute value to the
-        * already-allocated blocks.  Blocks are written synchronously
-        * so that we can know they are all on disk before we turn off
-        * the INCOMPLETE flag.
-        */
-       lblkno = args->rmtblkno;
-       valuelen = args->valuelen;
-       while (valuelen > 0) {
                /*
-                * Try to remember where we decided to put the value.
+                * Get the value, local or "remote"
                 */
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               nmap = 1;
-               error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
-                                 args->rmtblkcnt,
-                                 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
-                                 args->firstblock, 0, &map, &nmap, NULL);
-               if (error) {
-                       return(error);
+               retval = xfs_attr3_leaf_getvalue(blk->bp, args);
+               if (!retval && (args->rmtblkno > 0)
+                   && !(args->flags & ATTR_KERNOVAL)) {
+                       retval = xfs_attr_rmtval_get(args);
                }
-               ASSERT(nmap == 1);
-               ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
-                      (map.br_startblock != HOLESTARTBLOCK));
-
-               dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
-               blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
-
-               bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
-                                                       blkcnt, XFS_BUF_LOCK);
-               ASSERT(bp);
-               ASSERT(!XFS_BUF_GETERROR(bp));
-
-               tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
-                                                       XFS_BUF_SIZE(bp);
-               xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
-               if (tmp < XFS_BUF_SIZE(bp))
-                       xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
-               if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
-                       return (error);
-               }
-               src += tmp;
-               valuelen -= tmp;
-
-               lblkno += map.br_blockcount;
        }
-       ASSERT(valuelen == 0);
-       return(0);
-}
-
-/*
- * Remove the value associated with an attribute by deleting the
- * out-of-line buffer that it is stored on.
- */
-STATIC int
-xfs_attr_rmtval_remove(xfs_da_args_t *args)
-{
-       xfs_mount_t *mp;
-       xfs_bmbt_irec_t map;
-       xfs_buf_t *bp;
-       xfs_daddr_t dblkno;
-       xfs_dablk_t lblkno;
-       int valuelen, blkcnt, nmap, error, done, committed;
-
-       mp = args->dp->i_mount;
 
        /*
-        * Roll through the "value", invalidating the attribute value's
-        * blocks.
+        * If not in a transaction, we have to release all the buffers.
         */
-       lblkno = args->rmtblkno;
-       valuelen = args->rmtblkcnt;
-       while (valuelen > 0) {
-               /*
-                * Try to remember where we decided to put the value.
-                */
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               nmap = 1;
-               error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
-                                       args->rmtblkcnt,
-                                       XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
-                                       args->firstblock, 0, &map, &nmap,
-                                       args->flist);
-               if (error) {
-                       return(error);
-               }
-               ASSERT(nmap == 1);
-               ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
-                      (map.br_startblock != HOLESTARTBLOCK));
-
-               dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
-               blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
-
-               /*
-                * If the "remote" value is in the cache, remove it.
-                */
-               bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
-                               XFS_INCORE_TRYLOCK);
-               if (bp) {
-                       XFS_BUF_STALE(bp);
-                       XFS_BUF_UNDELAYWRITE(bp);
-                       xfs_buf_relse(bp);
-                       bp = NULL;
-               }
-
-               valuelen -= map.br_blockcount;
-
-               lblkno += map.br_blockcount;
+       for (i = 0; i < state->path.active; i++) {
+               xfs_trans_brelse(args->trans, state->path.blk[i].bp);
+               state->path.blk[i].bp = NULL;
        }
 
-       /*
-        * Keep de-allocating extents until the remote-value region is gone.
-        */
-       lblkno = args->rmtblkno;
-       blkcnt = args->rmtblkcnt;
-       done = 0;
-       while (!done) {
-               XFS_BMAP_INIT(args->flist, args->firstblock);
-               error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
-                                   XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
-                                   1, args->firstblock, args->flist, &done);
-               if (!error) {
-                       error = xfs_bmap_finish(&args->trans, args->flist,
-                                               *args->firstblock, &committed);
-               }
-               if (error) {
-                       ASSERT(committed);
-                       args->trans = NULL;
-                       xfs_bmap_cancel(args->flist);
-                       return(error);
-               }
-
-               /*
-                * bmap_finish() may have committed the last trans and started
-                * a new one.  We need the inode to be in all transactions.
-                */
-               if (committed) {
-                       xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
-                       xfs_trans_ihold(args->trans, args->dp);
-               }
-
-               /*
-                * Close out trans and start the next one in the chain.
-                */
-               if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
-                       return (error);
-       }
-       return(0);
+       xfs_da_state_free(state);
+       return retval;
 }