]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/xfs_attr_leaf.c
xfs: move local to extent inode logging into bmap helper
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_attr_leaf.c
index 90937d6f2adc924ad5bc7f0ae6ab8081400349ac..0249a0a96c39e97a92481616fd2e5506fee6f861 100644 (file)
@@ -1,23 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  * Copyright (c) 2013 Red Hat, Inc.
  * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms 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.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
+#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_sb.h"
+#include "xfs_mount.h"
+#include "xfs_da_format.h"
+#include "xfs_inode.h"
+#include "xfs_trans.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_bmap.h"
+#include "xfs_attr_sf.h"
+#include "xfs_attr_remote.h"
+#include "xfs_attr.h"
+#include "xfs_attr_leaf.h"
+#include "xfs_trace.h"
+#include "xfs_dir2.h"
 
-#include <xfs.h>
 
 /*
  * xfs_attr_leaf.c
@@ -54,15 +60,91 @@ STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
 /*
  * Utility routines.
  */
-STATIC void xfs_attr3_leaf_moveents(struct xfs_attr_leafblock *src_leaf,
+STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
+                       struct xfs_attr_leafblock *src_leaf,
                        struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
                        struct xfs_attr_leafblock *dst_leaf,
                        struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
-                       int move_count, struct xfs_mount *mp);
+                       int move_count);
 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
 
+/*
+ * attr3 block 'firstused' conversion helpers.
+ *
+ * firstused refers to the offset of the first used byte of the nameval region
+ * of an attr leaf block. The region starts at the tail of the block and expands
+ * backwards towards the middle. As such, firstused is initialized to the block
+ * size for an empty leaf block and is reduced from there.
+ *
+ * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
+ * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
+ * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
+ * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
+ * the attr block size. The following helpers manage the conversion between the
+ * in-core and on-disk formats.
+ */
+
+static void
+xfs_attr3_leaf_firstused_from_disk(
+       struct xfs_da_geometry          *geo,
+       struct xfs_attr3_icleaf_hdr     *to,
+       struct xfs_attr_leafblock       *from)
+{
+       struct xfs_attr3_leaf_hdr       *hdr3;
+
+       if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
+               hdr3 = (struct xfs_attr3_leaf_hdr *) from;
+               to->firstused = be16_to_cpu(hdr3->firstused);
+       } else {
+               to->firstused = be16_to_cpu(from->hdr.firstused);
+       }
+
+       /*
+        * Convert from the magic fsb size value to actual blocksize. This
+        * should only occur for empty blocks when the block size overflows
+        * 16-bits.
+        */
+       if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
+               ASSERT(!to->count && !to->usedbytes);
+               ASSERT(geo->blksize > USHRT_MAX);
+               to->firstused = geo->blksize;
+       }
+}
+
+static void
+xfs_attr3_leaf_firstused_to_disk(
+       struct xfs_da_geometry          *geo,
+       struct xfs_attr_leafblock       *to,
+       struct xfs_attr3_icleaf_hdr     *from)
+{
+       struct xfs_attr3_leaf_hdr       *hdr3;
+       uint32_t                        firstused;
+
+       /* magic value should only be seen on disk */
+       ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
+
+       /*
+        * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
+        * value. This only overflows at the max supported value of 64k. Use the
+        * magic on-disk value to represent block size in this case.
+        */
+       firstused = from->firstused;
+       if (firstused > USHRT_MAX) {
+               ASSERT(from->firstused == geo->blksize);
+               firstused = XFS_ATTR3_LEAF_NULLOFF;
+       }
+
+       if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
+               hdr3 = (struct xfs_attr3_leaf_hdr *) to;
+               hdr3->firstused = cpu_to_be16(firstused);
+       } else {
+               to->hdr.firstused = cpu_to_be16(firstused);
+       }
+}
+
 void
 xfs_attr3_leaf_hdr_from_disk(
+       struct xfs_da_geometry          *geo,
        struct xfs_attr3_icleaf_hdr     *to,
        struct xfs_attr_leafblock       *from)
 {
@@ -79,7 +161,7 @@ xfs_attr3_leaf_hdr_from_disk(
                to->magic = be16_to_cpu(hdr3->info.hdr.magic);
                to->count = be16_to_cpu(hdr3->count);
                to->usedbytes = be16_to_cpu(hdr3->usedbytes);
-               to->firstused = be16_to_cpu(hdr3->firstused);
+               xfs_attr3_leaf_firstused_from_disk(geo, to, from);
                to->holes = hdr3->holes;
 
                for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
@@ -93,7 +175,7 @@ xfs_attr3_leaf_hdr_from_disk(
        to->magic = be16_to_cpu(from->hdr.info.magic);
        to->count = be16_to_cpu(from->hdr.count);
        to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
-       to->firstused = be16_to_cpu(from->hdr.firstused);
+       xfs_attr3_leaf_firstused_from_disk(geo, to, from);
        to->holes = from->hdr.holes;
 
        for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
@@ -104,10 +186,11 @@ xfs_attr3_leaf_hdr_from_disk(
 
 void
 xfs_attr3_leaf_hdr_to_disk(
+       struct xfs_da_geometry          *geo,
        struct xfs_attr_leafblock       *to,
        struct xfs_attr3_icleaf_hdr     *from)
 {
-       int     i;
+       int                             i;
 
        ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
               from->magic == XFS_ATTR3_LEAF_MAGIC);
@@ -120,7 +203,7 @@ xfs_attr3_leaf_hdr_to_disk(
                hdr3->info.hdr.magic = cpu_to_be16(from->magic);
                hdr3->count = cpu_to_be16(from->count);
                hdr3->usedbytes = cpu_to_be16(from->usedbytes);
-               hdr3->firstused = cpu_to_be16(from->firstused);
+               xfs_attr3_leaf_firstused_to_disk(geo, to, from);
                hdr3->holes = from->holes;
                hdr3->pad1 = 0;
 
@@ -135,7 +218,7 @@ xfs_attr3_leaf_hdr_to_disk(
        to->hdr.info.magic = cpu_to_be16(from->magic);
        to->hdr.count = cpu_to_be16(from->count);
        to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
-       to->hdr.firstused = cpu_to_be16(from->firstused);
+       xfs_attr3_leaf_firstused_to_disk(geo, to, from);
        to->hdr.holes = from->holes;
        to->hdr.pad1 = 0;
 
@@ -145,50 +228,92 @@ xfs_attr3_leaf_hdr_to_disk(
        }
 }
 
-static bool
+static xfs_failaddr_t
 xfs_attr3_leaf_verify(
-       struct xfs_buf          *bp)
+       struct xfs_buf                  *bp)
 {
-       struct xfs_mount        *mp = bp->b_target->bt_mount;
-       struct xfs_attr_leafblock *leaf = bp->b_addr;
-       struct xfs_attr3_icleaf_hdr ichdr;
+       struct xfs_attr3_icleaf_hdr     ichdr;
+       struct xfs_mount                *mp = bp->b_mount;
+       struct xfs_attr_leafblock       *leaf = bp->b_addr;
+       struct xfs_attr_leaf_entry      *entries;
+       uint32_t                        end;    /* must be 32bit - see below */
+       int                             i;
+       xfs_failaddr_t                  fa;
 
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
 
-       if (xfs_sb_version_hascrc(&mp->m_sb)) {
-               struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
+       fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
+       if (fa)
+               return fa;
 
-               if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
-                       return false;
+       /*
+        * In recovery there is a transient state where count == 0 is valid
+        * because we may have transitioned an empty shortform attr to a leaf
+        * if the attr didn't fit in shortform.
+        */
+       if (!xfs_log_in_recovery(mp) && ichdr.count == 0)
+               return __this_address;
 
-               if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid))
-                       return false;
-               if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
-                       return false;
-       } else {
-               if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
-                       return false;
-       }
-       if (ichdr.count == 0)
-               return false;
+       /*
+        * firstused is the block offset of the first name info structure.
+        * Make sure it doesn't go off the block or crash into the header.
+        */
+       if (ichdr.firstused > mp->m_attr_geo->blksize)
+               return __this_address;
+       if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
+               return __this_address;
+
+       /* Make sure the entries array doesn't crash into the name info. */
+       entries = xfs_attr3_leaf_entryp(bp->b_addr);
+       if ((char *)&entries[ichdr.count] >
+           (char *)bp->b_addr + ichdr.firstused)
+               return __this_address;
 
        /* XXX: need to range check rest of attr header values */
        /* XXX: hash order check? */
 
-       return true;
+       /*
+        * Quickly check the freemap information.  Attribute data has to be
+        * aligned to 4-byte boundaries, and likewise for the free space.
+        *
+        * Note that for 64k block size filesystems, the freemap entries cannot
+        * overflow as they are only be16 fields. However, when checking end
+        * pointer of the freemap, we have to be careful to detect overflows and
+        * so use uint32_t for those checks.
+        */
+       for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
+               if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
+                       return __this_address;
+               if (ichdr.freemap[i].base & 0x3)
+                       return __this_address;
+               if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
+                       return __this_address;
+               if (ichdr.freemap[i].size & 0x3)
+                       return __this_address;
+
+               /* be care of 16 bit overflows here */
+               end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size;
+               if (end < ichdr.freemap[i].base)
+                       return __this_address;
+               if (end > mp->m_attr_geo->blksize)
+                       return __this_address;
+       }
+
+       return NULL;
 }
 
 static void
 xfs_attr3_leaf_write_verify(
        struct xfs_buf  *bp)
 {
-       struct xfs_mount        *mp = bp->b_target->bt_mount;
-       struct xfs_buf_log_item *bip = bp->b_fspriv;
+       struct xfs_mount        *mp = bp->b_mount;
+       struct xfs_buf_log_item *bip = bp->b_log_item;
        struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
+       xfs_failaddr_t          fa;
 
-       if (!xfs_attr3_leaf_verify(bp)) {
-               XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
-               xfs_buf_ioerror(bp, EFSCORRUPTED);
+       fa = xfs_attr3_leaf_verify(bp);
+       if (fa) {
+               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                return;
        }
 
@@ -211,19 +336,26 @@ static void
 xfs_attr3_leaf_read_verify(
        struct xfs_buf          *bp)
 {
-       struct xfs_mount        *mp = bp->b_target->bt_mount;
+       struct xfs_mount        *mp = bp->b_mount;
+       xfs_failaddr_t          fa;
 
-       if ((xfs_sb_version_hascrc(&mp->m_sb) &&
-            !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF)) ||
-           !xfs_attr3_leaf_verify(bp)) {
-               XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
-               xfs_buf_ioerror(bp, EFSCORRUPTED);
+       if (xfs_sb_version_hascrc(&mp->m_sb) &&
+            !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
+               xfs_verifier_error(bp, -EFSBADCRC, __this_address);
+       else {
+               fa = xfs_attr3_leaf_verify(bp);
+               if (fa)
+                       xfs_verifier_error(bp, -EFSCORRUPTED, fa);
        }
 }
 
 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
+       .name = "xfs_attr3_leaf",
+       .magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC),
+                    cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) },
        .verify_read = xfs_attr3_leaf_read_verify,
        .verify_write = xfs_attr3_leaf_write_verify,
+       .verify_struct = xfs_attr3_leaf_verify,
 };
 
 int
@@ -238,7 +370,7 @@ xfs_attr3_leaf_read(
 
        err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
                                XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
-       if (!err && tp)
+       if (!err && tp && *bpp)
                xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
        return err;
 }
@@ -257,6 +389,50 @@ xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
        return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
 }
 
+static int
+xfs_attr_copy_value(
+       struct xfs_da_args      *args,
+       unsigned char           *value,
+       int                     valuelen)
+{
+       /*
+        * No copy if all we have to do is get the length
+        */
+       if (args->flags & ATTR_KERNOVAL) {
+               args->valuelen = valuelen;
+               return 0;
+       }
+
+       /*
+        * No copy if the length of the existing buffer is too small
+        */
+       if (args->valuelen < valuelen) {
+               args->valuelen = valuelen;
+               return -ERANGE;
+       }
+
+       if (args->op_flags & XFS_DA_OP_ALLOCVAL) {
+               args->value = kmem_alloc_large(valuelen, 0);
+               if (!args->value)
+                       return -ENOMEM;
+       }
+       args->valuelen = valuelen;
+
+       /* remote block xattr requires IO for copy-in */
+       if (args->rmtblkno)
+               return xfs_attr_rmtval_get(args);
+
+       /*
+        * This is to prevent a GCC warning because the remote xattr case
+        * doesn't have a value to pass in. In that case, we never reach here,
+        * but GCC can't work that out and so throws a "passing NULL to
+        * memcpy" warning.
+        */
+       if (!value)
+               return -EINVAL;
+       memcpy(args->value, value, valuelen);
+       return 0;
+}
 
 /*========================================================================
  * External routines when attribute fork size < XFS_LITINO(mp).
@@ -284,13 +460,9 @@ xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
        /* rounded down */
        offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
 
-       switch (dp->i_d.di_format) {
-       case XFS_DINODE_FMT_DEV:
+       if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
                minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
                return (offset >= minforkoff) ? minforkoff : 0;
-       case XFS_DINODE_FMT_UUID:
-               minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
-               return (offset >= minforkoff) ? minforkoff : 0;
        }
 
        /*
@@ -319,7 +491,7 @@ xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
        switch (dp->i_d.di_format) {
        case XFS_DINODE_FMT_EXTENTS:
                /*
-                * If there is no attr fork and the data fork is extents, 
+                * If there is no attr fork and the data fork is extents,
                 * determine if creating the default attr fork will result
                 * in the extents form migrating to btree. If so, the
                 * minimum offset only needs to be the space required for
@@ -349,7 +521,7 @@ xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
         * A data fork btree root must have space for at least
         * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
         */
-       minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
+       minforkoff = max(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
        minforkoff = roundup(minforkoff, 8) >> 3;
 
        /* attr fork btree root can have at least this many key/ptr pairs */
@@ -376,7 +548,7 @@ xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
                if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
                        xfs_sb_version_addattr2(&mp->m_sb);
                        spin_unlock(&mp->m_sb_lock);
-                       xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
+                       xfs_log_sb(tp);
                } else
                        spin_unlock(&mp->m_sb_lock);
        }
@@ -390,7 +562,7 @@ xfs_attr_shortform_create(xfs_da_args_t *args)
 {
        xfs_attr_sf_hdr_t *hdr;
        xfs_inode_t *dp;
-       xfs_ifork_t *ifp;
+       struct xfs_ifork *ifp;
 
        trace_xfs_attr_sf_create(args);
 
@@ -425,7 +597,7 @@ xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
        int i, offset, size;
        xfs_mount_t *mp;
        xfs_inode_t *dp;
-       xfs_ifork_t *ifp;
+       struct xfs_ifork *ifp;
 
        trace_xfs_attr_sf_add(args);
 
@@ -471,8 +643,8 @@ xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
  * After the last attribute is removed revert to original inode format,
  * making all literal area available to the data fork once more.
  */
-STATIC void
-xfs_attr_fork_reset(
+void
+xfs_attr_fork_remove(
        struct xfs_inode        *ip,
        struct xfs_trans        *tp)
 {
@@ -518,7 +690,7 @@ xfs_attr_shortform_remove(xfs_da_args_t *args)
                break;
        }
        if (i == end)
-               return(XFS_ERROR(ENOATTR));
+               return -ENOATTR;
 
        /*
         * Fix up the attribute fork data, covering the hole
@@ -538,7 +710,7 @@ xfs_attr_shortform_remove(xfs_da_args_t *args)
            (mp->m_flags & XFS_MOUNT_ATTR2) &&
            (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
            !(args->op_flags & XFS_DA_OP_ADDNAME)) {
-               xfs_attr_fork_reset(dp, args->trans);
+               xfs_attr_fork_remove(dp, args->trans);
        } else {
                xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
                dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
@@ -553,7 +725,7 @@ xfs_attr_shortform_remove(xfs_da_args_t *args)
 
        xfs_sbversion_add_attr2(mp, args->trans);
 
-       return(0);
+       return 0;
 }
 
 /*
@@ -566,7 +738,7 @@ xfs_attr_shortform_lookup(xfs_da_args_t *args)
        xfs_attr_shortform_t *sf;
        xfs_attr_sf_entry_t *sfe;
        int i;
-       xfs_ifork_t *ifp;
+       struct xfs_ifork *ifp;
 
        trace_xfs_attr_sf_lookup(args);
 
@@ -582,21 +754,25 @@ xfs_attr_shortform_lookup(xfs_da_args_t *args)
                        continue;
                if (!xfs_attr_namesp_match(args->flags, sfe->flags))
                        continue;
-               return(XFS_ERROR(EEXIST));
+               return -EEXIST;
        }
-       return(XFS_ERROR(ENOATTR));
+       return -ENOATTR;
 }
 
 /*
- * Look up a name in a shortform attribute list structure.
+ * Retreive the attribute value and length.
+ *
+ * If ATTR_KERNOVAL is specified, only the length needs to be returned.
+ * Unlike a lookup, we only return an error if the attribute does not
+ * exist or we can't retrieve the value.
  */
-/*ARGSUSED*/
 int
-xfs_attr_shortform_getvalue(xfs_da_args_t *args)
+xfs_attr_shortform_getvalue(
+       struct xfs_da_args      *args)
 {
-       xfs_attr_shortform_t *sf;
-       xfs_attr_sf_entry_t *sfe;
-       int i;
+       struct xfs_attr_shortform *sf;
+       struct xfs_attr_sf_entry *sfe;
+       int                     i;
 
        ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
        sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
@@ -609,37 +785,30 @@ xfs_attr_shortform_getvalue(xfs_da_args_t *args)
                        continue;
                if (!xfs_attr_namesp_match(args->flags, sfe->flags))
                        continue;
-               if (args->flags & ATTR_KERNOVAL) {
-                       args->valuelen = sfe->valuelen;
-                       return(XFS_ERROR(EEXIST));
-               }
-               if (args->valuelen < sfe->valuelen) {
-                       args->valuelen = sfe->valuelen;
-                       return(XFS_ERROR(ERANGE));
-               }
-               args->valuelen = sfe->valuelen;
-               memcpy(args->value, &sfe->nameval[args->namelen],
-                                                   args->valuelen);
-               return(XFS_ERROR(EEXIST));
+               return xfs_attr_copy_value(args, &sfe->nameval[args->namelen],
+                                               sfe->valuelen);
        }
-       return(XFS_ERROR(ENOATTR));
+       return -ENOATTR;
 }
 
 /*
- * Convert from using the shortform to the leaf.
+ * Convert from using the shortform to the leaf.  On success, return the
+ * buffer so that we can keep it locked until we're totally done with it.
  */
 int
-xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
+xfs_attr_shortform_to_leaf(
+       struct xfs_da_args              *args,
+       struct xfs_buf                  **leaf_bp)
 {
-       xfs_inode_t *dp;
-       xfs_attr_shortform_t *sf;
-       xfs_attr_sf_entry_t *sfe;
-       xfs_da_args_t nargs;
-       char *tmpbuffer;
-       int error, i, size;
-       xfs_dablk_t blkno;
-       struct xfs_buf *bp;
-       xfs_ifork_t *ifp;
+       struct xfs_inode                *dp;
+       struct xfs_attr_shortform       *sf;
+       struct xfs_attr_sf_entry        *sfe;
+       struct xfs_da_args              nargs;
+       char                            *tmpbuffer;
+       int                             error, i, size;
+       xfs_dablk_t                     blkno;
+       struct xfs_buf                  *bp;
+       struct xfs_ifork                *ifp;
 
        trace_xfs_attr_sf_to_leaf(args);
 
@@ -647,44 +816,27 @@ xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
        ifp = dp->i_afp;
        sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
        size = be16_to_cpu(sf->hdr.totsize);
-       tmpbuffer = kmem_alloc(size, KM_SLEEP);
+       tmpbuffer = kmem_alloc(size, 0);
        ASSERT(tmpbuffer != NULL);
        memcpy(tmpbuffer, ifp->if_u1.if_data, size);
        sf = (xfs_attr_shortform_t *)tmpbuffer;
 
        xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
-       xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
+       xfs_bmap_local_to_extents_empty(args->trans, dp, XFS_ATTR_FORK);
 
        bp = NULL;
        error = xfs_da_grow_inode(args, &blkno);
-       if (error) {
-               /*
-                * If we hit an IO error middle of the transaction inside
-                * grow_inode(), we may have inconsistent data. Bail out.
-                */
-               if (error == EIO)
-                       goto out;
-               xfs_idata_realloc(dp, size, XFS_ATTR_FORK);     /* try to put */
-               memcpy(ifp->if_u1.if_data, tmpbuffer, size);    /* it back */
+       if (error)
                goto out;
-       }
 
        ASSERT(blkno == 0);
        error = xfs_attr3_leaf_create(args, blkno, &bp);
-       if (error) {
-               error = xfs_da_shrink_inode(args, 0, bp);
-               bp = NULL;
-               if (error)
-                       goto out;
-               xfs_idata_realloc(dp, size, XFS_ATTR_FORK);     /* try to put */
-               memcpy(ifp->if_u1.if_data, tmpbuffer, size);    /* it back */
+       if (error)
                goto out;
-       }
 
        memset((char *)&nargs, 0, sizeof(nargs));
        nargs.dp = dp;
-       nargs.firstblock = args->firstblock;
-       nargs.flist = args->flist;
+       nargs.geo = args->geo;
        nargs.total = args->total;
        nargs.whichfork = XFS_ATTR_FORK;
        nargs.trans = args->trans;
@@ -700,18 +852,18 @@ xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
                                                sfe->namelen);
                nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
                error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
-               ASSERT(error == ENOATTR);
+               ASSERT(error == -ENOATTR);
                error = xfs_attr3_leaf_add(bp, &nargs);
-               ASSERT(error != ENOSPC);
+               ASSERT(error != -ENOSPC);
                if (error)
                        goto out;
                sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
        }
        error = 0;
-
+       *leaf_bp = bp;
 out:
        kmem_free(tmpbuffer);
-       return(error);
+       return error;
 }
 
 /*
@@ -729,9 +881,10 @@ xfs_attr_shortform_allfit(
        struct xfs_attr3_icleaf_hdr leafhdr;
        int                     bytes;
        int                     i;
+       struct xfs_mount        *mp = bp->b_mount;
 
        leaf = bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
        entry = xfs_attr3_leaf_entryp(leaf);
 
        bytes = sizeof(struct xfs_attr_sf_hdr);
@@ -739,12 +892,12 @@ xfs_attr_shortform_allfit(
                if (entry->flags & XFS_ATTR_INCOMPLETE)
                        continue;               /* don't copy partial entries */
                if (!(entry->flags & XFS_ATTR_LOCAL))
-                       return(0);
+                       return 0;
                name_loc = xfs_attr3_leaf_name_local(leaf, i);
                if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
-                       return(0);
+                       return 0;
                if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
-                       return(0);
+                       return 0;
                bytes += sizeof(struct xfs_attr_sf_entry) - 1
                                + name_loc->namelen
                                + be16_to_cpu(name_loc->valuelen);
@@ -756,6 +909,80 @@ xfs_attr_shortform_allfit(
        return xfs_attr_shortform_bytesfit(dp, bytes);
 }
 
+/* Verify the consistency of an inline attribute fork. */
+xfs_failaddr_t
+xfs_attr_shortform_verify(
+       struct xfs_inode                *ip)
+{
+       struct xfs_attr_shortform       *sfp;
+       struct xfs_attr_sf_entry        *sfep;
+       struct xfs_attr_sf_entry        *next_sfep;
+       char                            *endp;
+       struct xfs_ifork                *ifp;
+       int                             i;
+       int                             size;
+
+       ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
+       ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
+       sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
+       size = ifp->if_bytes;
+
+       /*
+        * Give up if the attribute is way too short.
+        */
+       if (size < sizeof(struct xfs_attr_sf_hdr))
+               return __this_address;
+
+       endp = (char *)sfp + size;
+
+       /* Check all reported entries */
+       sfep = &sfp->list[0];
+       for (i = 0; i < sfp->hdr.count; i++) {
+               /*
+                * struct xfs_attr_sf_entry has a variable length.
+                * Check the fixed-offset parts of the structure are
+                * within the data buffer.
+                */
+               if (((char *)sfep + sizeof(*sfep)) >= endp)
+                       return __this_address;
+
+               /* Don't allow names with known bad length. */
+               if (sfep->namelen == 0)
+                       return __this_address;
+
+               /*
+                * Check that the variable-length part of the structure is
+                * within the data buffer.  The next entry starts after the
+                * name component, so nextentry is an acceptable test.
+                */
+               next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
+               if ((char *)next_sfep > endp)
+                       return __this_address;
+
+               /*
+                * Check for unknown flags.  Short form doesn't support
+                * the incomplete or local bits, so we can use the namespace
+                * mask here.
+                */
+               if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
+                       return __this_address;
+
+               /*
+                * Check for invalid namespace combinations.  We only allow
+                * one namespace flag per xattr, so we can just count the
+                * bits (i.e. hweight) here.
+                */
+               if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
+                       return __this_address;
+
+               sfep = next_sfep;
+       }
+       if ((void *)sfep != (void *)endp)
+               return __this_address;
+
+       return NULL;
+}
+
 /*
  * Convert a leaf attribute list to shortform attribute list
  */
@@ -777,18 +1004,18 @@ xfs_attr3_leaf_to_shortform(
 
        trace_xfs_attr_leaf_to_sf(args);
 
-       tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
+       tmpbuffer = kmem_alloc(args->geo->blksize, 0);
        if (!tmpbuffer)
-               return ENOMEM;
+               return -ENOMEM;
 
-       memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
+       memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
 
        leaf = (xfs_attr_leafblock_t *)tmpbuffer;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
        entry = xfs_attr3_leaf_entryp(leaf);
 
        /* XXX (dgc): buffer is about to be marked stale - why zero it? */
-       memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
+       memset(bp->b_addr, 0, args->geo->blksize);
 
        /*
         * Clean out the prior contents of the attribute list.
@@ -800,7 +1027,7 @@ xfs_attr3_leaf_to_shortform(
        if (forkoff == -1) {
                ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
                ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
-               xfs_attr_fork_reset(dp, args->trans);
+               xfs_attr_fork_remove(dp, args->trans);
                goto out;
        }
 
@@ -810,9 +1037,8 @@ xfs_attr3_leaf_to_shortform(
         * Copy the attributes
         */
        memset((char *)&nargs, 0, sizeof(nargs));
+       nargs.geo = args->geo;
        nargs.dp = dp;
-       nargs.firstblock = args->firstblock;
-       nargs.flist = args->flist;
        nargs.total = args->total;
        nargs.whichfork = XFS_ATTR_FORK;
        nargs.trans = args->trans;
@@ -876,12 +1102,12 @@ xfs_attr3_leaf_to_node(
        /* copy leaf to new buffer, update identifiers */
        xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
        bp2->b_ops = bp1->b_ops;
-       memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(mp));
+       memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
        if (xfs_sb_version_hascrc(&mp->m_sb)) {
                struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
                hdr3->blkno = cpu_to_be64(bp2->b_bn);
        }
-       xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(mp) - 1);
+       xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
 
        /*
         * Set up the new root node.
@@ -890,19 +1116,19 @@ xfs_attr3_leaf_to_node(
        if (error)
                goto out;
        node = bp1->b_addr;
-       xfs_da3_node_hdr_from_disk(&icnodehdr, node);
-       btree = xfs_da3_node_tree_p(node);
+       dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
+       btree = dp->d_ops->node_tree_p(node);
 
        leaf = bp2->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&icleafhdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
        entries = xfs_attr3_leaf_entryp(leaf);
 
        /* both on-disk, don't endian-flip twice */
        btree[0].hashval = entries[icleafhdr.count - 1].hashval;
        btree[0].before = cpu_to_be32(blkno);
        icnodehdr.count = 1;
-       xfs_da3_node_hdr_to_disk(node, &icnodehdr);
-       xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(mp) - 1);
+       dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
+       xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
        error = 0;
 out:
        return error;
@@ -938,10 +1164,10 @@ xfs_attr3_leaf_create(
        bp->b_ops = &xfs_attr3_leaf_buf_ops;
        xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
        leaf = bp->b_addr;
-       memset(leaf, 0, XFS_LBSIZE(mp));
+       memset(leaf, 0, args->geo->blksize);
 
        memset(&ichdr, 0, sizeof(ichdr));
-       ichdr.firstused = XFS_LBSIZE(mp);
+       ichdr.firstused = args->geo->blksize;
 
        if (xfs_sb_version_hascrc(&mp->m_sb)) {
                struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
@@ -950,7 +1176,7 @@ xfs_attr3_leaf_create(
 
                hdr3->blkno = cpu_to_be64(bp->b_bn);
                hdr3->owner = cpu_to_be64(dp->i_ino);
-               uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
+               uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
 
                ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
        } else {
@@ -959,8 +1185,8 @@ xfs_attr3_leaf_create(
        }
        ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
 
-       xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
-       xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(mp) - 1);
+       xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
+       xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
 
        *bpp = bp;
        return 0;
@@ -986,10 +1212,10 @@ xfs_attr3_leaf_split(
        ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
        error = xfs_da_grow_inode(state->args, &blkno);
        if (error)
-               return(error);
+               return error;
        error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
        if (error)
-               return(error);
+               return error;
        newblk->blkno = blkno;
        newblk->magic = XFS_ATTR_LEAF_MAGIC;
 
@@ -1000,7 +1226,7 @@ xfs_attr3_leaf_split(
        xfs_attr3_leaf_rebalance(state, oldblk, newblk);
        error = xfs_da3_blk_link(state, oldblk, newblk);
        if (error)
-               return(error);
+               return error;
 
        /*
         * Save info on "old" attribute for "atomic rename" ops, leaf_add()
@@ -1022,7 +1248,7 @@ xfs_attr3_leaf_split(
         */
        oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
        newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
-       return(error);
+       return error;
 }
 
 /*
@@ -1044,10 +1270,9 @@ xfs_attr3_leaf_add(
        trace_xfs_attr_leaf_add(args);
 
        leaf = bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
        ASSERT(args->index >= 0 && args->index <= ichdr.count);
-       entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
-                          args->trans->t_mountp->m_sb.sb_blocksize, NULL);
+       entsize = xfs_attr_leaf_newentsize(args, NULL);
 
        /*
         * Search through freemap for first-fit on new name length.
@@ -1078,7 +1303,7 @@ xfs_attr3_leaf_add(
         * no good and we should just give up.
         */
        if (!ichdr.holes && sum < entsize)
-               return XFS_ERROR(ENOSPC);
+               return -ENOSPC;
 
        /*
         * Compact the entries to coalesce free space.
@@ -1091,14 +1316,14 @@ xfs_attr3_leaf_add(
         * free region, in freemap[0].  If it is not big enough, give up.
         */
        if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
-               tmp = ENOSPC;
+               tmp = -ENOSPC;
                goto out_log_hdr;
        }
 
        tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
 
 out_log_hdr:
-       xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
+       xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
        xfs_trans_log_buf(args->trans, bp,
                XFS_DA_LOGRANGE(leaf, &leaf->hdr,
                                xfs_attr3_leaf_hdr_size(leaf)));
@@ -1146,17 +1371,14 @@ xfs_attr3_leaf_add_work(
         * Allocate space for the new string (at the end of the run).
         */
        mp = args->trans->t_mountp;
-       ASSERT(ichdr->freemap[mapindex].base < XFS_LBSIZE(mp));
+       ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
        ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
        ASSERT(ichdr->freemap[mapindex].size >=
-               xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
-                                        mp->m_sb.sb_blocksize, NULL));
-       ASSERT(ichdr->freemap[mapindex].size < XFS_LBSIZE(mp));
+               xfs_attr_leaf_newentsize(args, NULL));
+       ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
        ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
 
-       ichdr->freemap[mapindex].size -=
-                       xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
-                                                mp->m_sb.sb_blocksize, &tmp);
+       ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
 
        entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
                                     ichdr->freemap[mapindex].size);
@@ -1201,6 +1423,7 @@ xfs_attr3_leaf_add_work(
                name_rmt->valueblk = 0;
                args->rmtblkno = 1;
                args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
+               args->rmtvaluelen = args->valuelen;
        }
        xfs_trans_log_buf(args->trans, bp,
             XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
@@ -1240,14 +1463,13 @@ xfs_attr3_leaf_compact(
        struct xfs_attr_leafblock *leaf_dst;
        struct xfs_attr3_icleaf_hdr ichdr_src;
        struct xfs_trans        *trans = args->trans;
-       struct xfs_mount        *mp = trans->t_mountp;
        char                    *tmpbuffer;
 
        trace_xfs_attr_leaf_compact(args);
 
-       tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
-       memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
-       memset(bp->b_addr, 0, XFS_LBSIZE(mp));
+       tmpbuffer = kmem_alloc(args->geo->blksize, 0);
+       memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
+       memset(bp->b_addr, 0, args->geo->blksize);
        leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
        leaf_dst = bp->b_addr;
 
@@ -1260,7 +1482,7 @@ xfs_attr3_leaf_compact(
 
        /* Initialise the incore headers */
        ichdr_src = *ichdr_dst; /* struct copy */
-       ichdr_dst->firstused = XFS_LBSIZE(mp);
+       ichdr_dst->firstused = args->geo->blksize;
        ichdr_dst->usedbytes = 0;
        ichdr_dst->count = 0;
        ichdr_dst->holes = 0;
@@ -1269,19 +1491,19 @@ xfs_attr3_leaf_compact(
                                                ichdr_dst->freemap[0].base;
 
        /* write the header back to initialise the underlying buffer */
-       xfs_attr3_leaf_hdr_to_disk(leaf_dst, ichdr_dst);
+       xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
 
        /*
         * Copy all entry's in the same (sorted) order,
         * but allocate name/value pairs packed and in sequence.
         */
-       xfs_attr3_leaf_moveents(leaf_src, &ichdr_src, 0, leaf_dst, ichdr_dst, 0,
-                               ichdr_src.count, mp);
+       xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
+                               leaf_dst, ichdr_dst, 0, ichdr_src.count);
        /*
         * this logs the entire buffer, but the caller must write the header
         * back to the buffer when it is finished modifying it.
         */
-       xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
+       xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
 
        kmem_free(tmpbuffer);
 }
@@ -1319,9 +1541,10 @@ xfs_attr_leaf_order(
 {
        struct xfs_attr3_icleaf_hdr ichdr1;
        struct xfs_attr3_icleaf_hdr ichdr2;
+       struct xfs_mount *mp = leaf1_bp->b_mount;
 
-       xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1_bp->b_addr);
-       xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2_bp->b_addr);
+       xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
+       xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
        return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
 }
 
@@ -1363,8 +1586,8 @@ xfs_attr3_leaf_rebalance(
        ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
        leaf1 = blk1->bp->b_addr;
        leaf2 = blk2->bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
-       xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
+       xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
+       xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
        ASSERT(ichdr2.count == 0);
        args = state->args;
 
@@ -1378,17 +1601,10 @@ xfs_attr3_leaf_rebalance(
         */
        swap = 0;
        if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
-               struct xfs_da_state_blk *tmp_blk;
-               struct xfs_attr3_icleaf_hdr tmp_ichdr;
-
-               tmp_blk = blk1;
-               blk1 = blk2;
-               blk2 = tmp_blk;
+               swap(blk1, blk2);
 
-               /* struct copies to swap them rather than reconverting */
-               tmp_ichdr = ichdr1;
-               ichdr1 = ichdr2;
-               ichdr2 = tmp_ichdr;
+               /* swap structures rather than reconverting them */
+               swap(ichdr1, ichdr2);
 
                leaf1 = blk1->bp->b_addr;
                leaf2 = blk2->bp->b_addr;
@@ -1432,8 +1648,8 @@ xfs_attr3_leaf_rebalance(
                /*
                 * Move high entries from leaf1 to low end of leaf2.
                 */
-               xfs_attr3_leaf_moveents(leaf1, &ichdr1, ichdr1.count - count,
-                               leaf2, &ichdr2, 0, count, state->mp);
+               xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
+                               ichdr1.count - count, leaf2, &ichdr2, 0, count);
 
        } else if (count > ichdr1.count) {
                /*
@@ -1461,14 +1677,14 @@ xfs_attr3_leaf_rebalance(
                /*
                 * Move low entries from leaf2 to high end of leaf1.
                 */
-               xfs_attr3_leaf_moveents(leaf2, &ichdr2, 0, leaf1, &ichdr1,
-                                       ichdr1.count, count, state->mp);
+               xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
+                                       ichdr1.count, count);
        }
 
-       xfs_attr3_leaf_hdr_to_disk(leaf1, &ichdr1);
-       xfs_attr3_leaf_hdr_to_disk(leaf2, &ichdr2);
-       xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
-       xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
+       xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
+       xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
+       xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
+       xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
 
        /*
         * Copy out last hashval in each block for B-tree code.
@@ -1563,11 +1779,9 @@ xfs_attr3_leaf_figure_balance(
        max = ichdr1->count + ichdr2->count;
        half = (max + 1) * sizeof(*entry);
        half += ichdr1->usedbytes + ichdr2->usedbytes +
-                       xfs_attr_leaf_newentsize(state->args->namelen,
-                                                state->args->valuelen,
-                                                state->blocksize, NULL);
+                       xfs_attr_leaf_newentsize(state->args, NULL);
        half /= 2;
-       lastdelta = state->blocksize;
+       lastdelta = state->args->geo->blksize;
        entry = xfs_attr3_leaf_entryp(leaf1);
        for (count = index = 0; count < max; entry++, index++, count++) {
 
@@ -1577,10 +1791,7 @@ xfs_attr3_leaf_figure_balance(
                 */
                if (count == blk1->index) {
                        tmp = totallen + sizeof(*entry) +
-                               xfs_attr_leaf_newentsize(
-                                               state->args->namelen,
-                                               state->args->valuelen,
-                                               state->blocksize, NULL);
+                               xfs_attr_leaf_newentsize(state->args, NULL);
                        if (XFS_ATTR_ABS(half - tmp) > lastdelta)
                                break;
                        lastdelta = XFS_ATTR_ABS(half - tmp);
@@ -1616,10 +1827,7 @@ xfs_attr3_leaf_figure_balance(
        totallen -= count * sizeof(*entry);
        if (foundit) {
                totallen -= sizeof(*entry) +
-                               xfs_attr_leaf_newentsize(
-                                               state->args->namelen,
-                                               state->args->valuelen,
-                                               state->blocksize, NULL);
+                               xfs_attr_leaf_newentsize(state->args, NULL);
        }
 
        *countarg = count;
@@ -1667,13 +1875,13 @@ xfs_attr3_leaf_toosmall(
         */
        blk = &state->path.blk[ state->path.active-1 ];
        leaf = blk->bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
        bytes = xfs_attr3_leaf_hdr_size(leaf) +
                ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
                ichdr.usedbytes;
-       if (bytes > (state->blocksize >> 1)) {
+       if (bytes > (state->args->geo->blksize >> 1)) {
                *action = 0;    /* blk over 50%, don't try to join */
-               return(0);
+               return 0;
        }
 
        /*
@@ -1692,7 +1900,7 @@ xfs_attr3_leaf_toosmall(
                error = xfs_da3_path_shift(state, &state->altpath, forward,
                                                 0, &retval);
                if (error)
-                       return(error);
+                       return error;
                if (retval) {
                        *action = 0;
                } else {
@@ -1721,11 +1929,12 @@ xfs_attr3_leaf_toosmall(
                error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
                                        blkno, -1, &bp);
                if (error)
-                       return(error);
+                       return error;
 
-               xfs_attr3_leaf_hdr_from_disk(&ichdr2, bp->b_addr);
+               xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
 
-               bytes = state->blocksize - (state->blocksize >> 2) -
+               bytes = state->args->geo->blksize -
+                       (state->args->geo->blksize >> 2) -
                        ichdr.usedbytes - ichdr2.usedbytes -
                        ((ichdr.count + ichdr2.count) *
                                        sizeof(xfs_attr_leaf_entry_t)) -
@@ -1737,7 +1946,7 @@ xfs_attr3_leaf_toosmall(
        }
        if (i >= 2) {
                *action = 0;
-               return(0);
+               return 0;
        }
 
        /*
@@ -1753,13 +1962,13 @@ xfs_attr3_leaf_toosmall(
                                                 0, &retval);
        }
        if (error)
-               return(error);
+               return error;
        if (retval) {
                *action = 0;
        } else {
                *action = 1;
        }
-       return(0);
+       return 0;
 }
 
 /*
@@ -1776,7 +1985,6 @@ xfs_attr3_leaf_remove(
        struct xfs_attr_leafblock *leaf;
        struct xfs_attr3_icleaf_hdr ichdr;
        struct xfs_attr_leaf_entry *entry;
-       struct xfs_mount        *mp = args->trans->t_mountp;
        int                     before;
        int                     after;
        int                     smallest;
@@ -1788,9 +1996,9 @@ xfs_attr3_leaf_remove(
        trace_xfs_attr_leaf_remove(args);
 
        leaf = bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
 
-       ASSERT(ichdr.count > 0 && ichdr.count < XFS_LBSIZE(mp) / 8);
+       ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
        ASSERT(args->index >= 0 && args->index < ichdr.count);
        ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
                                        xfs_attr3_leaf_hdr_size(leaf));
@@ -1798,7 +2006,7 @@ xfs_attr3_leaf_remove(
        entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
 
        ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
-       ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
+       ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
 
        /*
         * Scan through free region table:
@@ -1813,8 +2021,8 @@ xfs_attr3_leaf_remove(
        smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
        entsize = xfs_attr_leaf_entsize(leaf, args->index);
        for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
-               ASSERT(ichdr.freemap[i].base < XFS_LBSIZE(mp));
-               ASSERT(ichdr.freemap[i].size < XFS_LBSIZE(mp));
+               ASSERT(ichdr.freemap[i].base < args->geo->blksize);
+               ASSERT(ichdr.freemap[i].size < args->geo->blksize);
                if (ichdr.freemap[i].base == tablesize) {
                        ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
                        ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
@@ -1891,22 +2099,21 @@ xfs_attr3_leaf_remove(
         * removing the name.
         */
        if (smallest) {
-               tmp = XFS_LBSIZE(mp);
+               tmp = args->geo->blksize;
                entry = xfs_attr3_leaf_entryp(leaf);
                for (i = ichdr.count - 1; i >= 0; entry++, i--) {
                        ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
-                       ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
+                       ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
 
                        if (be16_to_cpu(entry->nameidx) < tmp)
                                tmp = be16_to_cpu(entry->nameidx);
                }
                ichdr.firstused = tmp;
-               if (!ichdr.firstused)
-                       ichdr.firstused = tmp - XFS_ATTR_LEAF_NAME_ALIGN;
+               ASSERT(ichdr.firstused != 0);
        } else {
                ichdr.holes = 1;        /* mark as needing compaction */
        }
-       xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
+       xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
        xfs_trans_log_buf(args->trans, bp,
                          XFS_DA_LOGRANGE(leaf, &leaf->hdr,
                                          xfs_attr3_leaf_hdr_size(leaf)));
@@ -1918,7 +2125,7 @@ xfs_attr3_leaf_remove(
        tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
              ichdr.count * sizeof(xfs_attr_leaf_entry_t);
 
-       return tmp < mp->m_attr_magicpct; /* leaf is < 37% full */
+       return tmp < args->geo->magicpct; /* leaf is < 37% full */
 }
 
 /*
@@ -1935,14 +2142,13 @@ xfs_attr3_leaf_unbalance(
        struct xfs_attr3_icleaf_hdr drophdr;
        struct xfs_attr3_icleaf_hdr savehdr;
        struct xfs_attr_leaf_entry *entry;
-       struct xfs_mount        *mp = state->mp;
 
        trace_xfs_attr_leaf_unbalance(state->args);
 
        drop_leaf = drop_blk->bp->b_addr;
        save_leaf = save_blk->bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&drophdr, drop_leaf);
-       xfs_attr3_leaf_hdr_from_disk(&savehdr, save_leaf);
+       xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
+       xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
        entry = xfs_attr3_leaf_entryp(drop_leaf);
 
        /*
@@ -1962,13 +2168,15 @@ xfs_attr3_leaf_unbalance(
                 */
                if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
                                         drop_blk->bp, &drophdr)) {
-                       xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
+                       xfs_attr3_leaf_moveents(state->args,
+                                               drop_leaf, &drophdr, 0,
                                                save_leaf, &savehdr, 0,
-                                               drophdr.count, mp);
+                                               drophdr.count);
                } else {
-                       xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
+                       xfs_attr3_leaf_moveents(state->args,
+                                               drop_leaf, &drophdr, 0,
                                                save_leaf, &savehdr,
-                                               savehdr.count, drophdr.count, mp);
+                                               savehdr.count, drophdr.count);
                }
        } else {
                /*
@@ -1978,7 +2186,7 @@ xfs_attr3_leaf_unbalance(
                struct xfs_attr_leafblock *tmp_leaf;
                struct xfs_attr3_icleaf_hdr tmphdr;
 
-               tmp_leaf = kmem_zalloc(state->blocksize, KM_SLEEP);
+               tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
 
                /*
                 * Copy the header into the temp leaf so that all the stuff
@@ -1991,35 +2199,39 @@ xfs_attr3_leaf_unbalance(
                tmphdr.magic = savehdr.magic;
                tmphdr.forw = savehdr.forw;
                tmphdr.back = savehdr.back;
-               tmphdr.firstused = state->blocksize;
+               tmphdr.firstused = state->args->geo->blksize;
 
                /* write the header to the temp buffer to initialise it */
-               xfs_attr3_leaf_hdr_to_disk(tmp_leaf, &tmphdr);
+               xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
 
                if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
                                         drop_blk->bp, &drophdr)) {
-                       xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
+                       xfs_attr3_leaf_moveents(state->args,
+                                               drop_leaf, &drophdr, 0,
                                                tmp_leaf, &tmphdr, 0,
-                                               drophdr.count, mp);
-                       xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
+                                               drophdr.count);
+                       xfs_attr3_leaf_moveents(state->args,
+                                               save_leaf, &savehdr, 0,
                                                tmp_leaf, &tmphdr, tmphdr.count,
-                                               savehdr.count, mp);
+                                               savehdr.count);
                } else {
-                       xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
+                       xfs_attr3_leaf_moveents(state->args,
+                                               save_leaf, &savehdr, 0,
                                                tmp_leaf, &tmphdr, 0,
-                                               savehdr.count, mp);
-                       xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
+                                               savehdr.count);
+                       xfs_attr3_leaf_moveents(state->args,
+                                               drop_leaf, &drophdr, 0,
                                                tmp_leaf, &tmphdr, tmphdr.count,
-                                               drophdr.count, mp);
+                                               drophdr.count);
                }
-               memcpy(save_leaf, tmp_leaf, state->blocksize);
+               memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
                savehdr = tmphdr; /* struct copy */
                kmem_free(tmp_leaf);
        }
 
-       xfs_attr3_leaf_hdr_to_disk(save_leaf, &savehdr);
+       xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
        xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
-                                          state->blocksize - 1);
+                                          state->args->geo->blksize - 1);
 
        /*
         * Copy out last hashval in each block for B-tree code.
@@ -2063,9 +2275,10 @@ xfs_attr3_leaf_lookup_int(
        trace_xfs_attr_leaf_lookup(args);
 
        leaf = bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
        entries = xfs_attr3_leaf_entryp(leaf);
-       ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
+       if (ichdr.count >= args->geo->blksize / 8)
+               return -EFSCORRUPTED;
 
        /*
         * Binary search.  (note: small blocks will skip this loop)
@@ -2081,8 +2294,10 @@ xfs_attr3_leaf_lookup_int(
                else
                        break;
        }
-       ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count));
-       ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval);
+       if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
+               return -EFSCORRUPTED;
+       if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
+               return -EFSCORRUPTED;
 
        /*
         * Since we may have duplicate hashval's, find the first matching
@@ -2099,7 +2314,7 @@ xfs_attr3_leaf_lookup_int(
        }
        if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
                args->index = probe;
-               return XFS_ERROR(ENOATTR);
+               return -ENOATTR;
        }
 
        /*
@@ -2128,7 +2343,7 @@ xfs_attr3_leaf_lookup_int(
                        if (!xfs_attr_namesp_match(args->flags, entry->flags))
                                continue;
                        args->index = probe;
-                       return XFS_ERROR(EEXIST);
+                       return -EEXIST;
                } else {
                        name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
                        if (name_rmt->namelen != args->namelen)
@@ -2139,21 +2354,25 @@ xfs_attr3_leaf_lookup_int(
                        if (!xfs_attr_namesp_match(args->flags, entry->flags))
                                continue;
                        args->index = probe;
-                       args->valuelen = be32_to_cpu(name_rmt->valuelen);
+                       args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
                        args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
                        args->rmtblkcnt = xfs_attr3_rmt_blocks(
                                                        args->dp->i_mount,
-                                                       args->valuelen);
-                       return XFS_ERROR(EEXIST);
+                                                       args->rmtvaluelen);
+                       return -EEXIST;
                }
        }
        args->index = probe;
-       return XFS_ERROR(ENOATTR);
+       return -ENOATTR;
 }
 
 /*
  * Get the value associated with an attribute name from a leaf attribute
  * list structure.
+ *
+ * If ATTR_KERNOVAL is specified, only the length needs to be returned.
+ * Unlike a lookup, we only return an error if the attribute does not
+ * exist or we can't retrieve the value.
  */
 int
 xfs_attr3_leaf_getvalue(
@@ -2165,11 +2384,10 @@ xfs_attr3_leaf_getvalue(
        struct xfs_attr_leaf_entry *entry;
        struct xfs_attr_leaf_name_local *name_loc;
        struct xfs_attr_leaf_name_remote *name_rmt;
-       int                     valuelen;
 
        leaf = bp->b_addr;
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
-       ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
+       ASSERT(ichdr.count < args->geo->blksize / 8);
        ASSERT(args->index < ichdr.count);
 
        entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
@@ -2177,36 +2395,19 @@ xfs_attr3_leaf_getvalue(
                name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
                ASSERT(name_loc->namelen == args->namelen);
                ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
-               valuelen = be16_to_cpu(name_loc->valuelen);
-               if (args->flags & ATTR_KERNOVAL) {
-                       args->valuelen = valuelen;
-                       return 0;
-               }
-               if (args->valuelen < valuelen) {
-                       args->valuelen = valuelen;
-                       return XFS_ERROR(ERANGE);
-               }
-               args->valuelen = valuelen;
-               memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
-       } else {
-               name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
-               ASSERT(name_rmt->namelen == args->namelen);
-               ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
-               valuelen = be32_to_cpu(name_rmt->valuelen);
-               args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
-               args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
-                                                      valuelen);
-               if (args->flags & ATTR_KERNOVAL) {
-                       args->valuelen = valuelen;
-                       return 0;
-               }
-               if (args->valuelen < valuelen) {
-                       args->valuelen = valuelen;
-                       return XFS_ERROR(ERANGE);
-               }
-               args->valuelen = valuelen;
-       }
-       return 0;
+               return xfs_attr_copy_value(args,
+                                       &name_loc->nameval[args->namelen],
+                                       be16_to_cpu(name_loc->valuelen));
+       }
+
+       name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
+       ASSERT(name_rmt->namelen == args->namelen);
+       ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
+       args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
+       args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
+       args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
+                                              args->rmtvaluelen);
+       return xfs_attr_copy_value(args, NULL, args->rmtvaluelen);
 }
 
 /*========================================================================
@@ -2220,14 +2421,14 @@ xfs_attr3_leaf_getvalue(
 /*ARGSUSED*/
 STATIC void
 xfs_attr3_leaf_moveents(
+       struct xfs_da_args              *args,
        struct xfs_attr_leafblock       *leaf_s,
        struct xfs_attr3_icleaf_hdr     *ichdr_s,
        int                             start_s,
        struct xfs_attr_leafblock       *leaf_d,
        struct xfs_attr3_icleaf_hdr     *ichdr_d,
        int                             start_d,
-       int                             count,
-       struct xfs_mount                *mp)
+       int                             count)
 {
        struct xfs_attr_leaf_entry      *entry_s;
        struct xfs_attr_leaf_entry      *entry_d;
@@ -2247,10 +2448,10 @@ xfs_attr3_leaf_moveents(
        ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
               ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
        ASSERT(ichdr_s->magic == ichdr_d->magic);
-       ASSERT(ichdr_s->count > 0 && ichdr_s->count < XFS_LBSIZE(mp) / 8);
+       ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
        ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
                                        + xfs_attr3_leaf_hdr_size(leaf_s));
-       ASSERT(ichdr_d->count < XFS_LBSIZE(mp) / 8);
+       ASSERT(ichdr_d->count < args->geo->blksize / 8);
        ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
                                        + xfs_attr3_leaf_hdr_size(leaf_d));
 
@@ -2302,11 +2503,11 @@ xfs_attr3_leaf_moveents(
                        entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
                        entry_d->flags = entry_s->flags;
                        ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
-                                                       <= XFS_LBSIZE(mp));
+                                                       <= args->geo->blksize);
                        memmove(xfs_attr3_leaf_name(leaf_d, desti),
                                xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
                        ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
-                                                       <= XFS_LBSIZE(mp));
+                                                       <= args->geo->blksize);
                        memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
                        ichdr_s->usedbytes -= tmp;
                        ichdr_d->usedbytes += tmp;
@@ -2327,7 +2528,7 @@ xfs_attr3_leaf_moveents(
                tmp = count * sizeof(xfs_attr_leaf_entry_t);
                entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
                ASSERT(((char *)entry_s + tmp) <=
-                      ((char *)leaf_s + XFS_LBSIZE(mp)));
+                      ((char *)leaf_s + args->geo->blksize));
                memset(entry_s, 0, tmp);
        } else {
                /*
@@ -2342,7 +2543,7 @@ xfs_attr3_leaf_moveents(
                tmp = count * sizeof(xfs_attr_leaf_entry_t);
                entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
                ASSERT(((char *)entry_s + tmp) <=
-                      ((char *)leaf_s + XFS_LBSIZE(mp)));
+                      ((char *)leaf_s + args->geo->blksize));
                memset(entry_s, 0, tmp);
        }
 
@@ -2369,8 +2570,9 @@ xfs_attr_leaf_lasthash(
 {
        struct xfs_attr3_icleaf_hdr ichdr;
        struct xfs_attr_leaf_entry *entries;
+       struct xfs_mount *mp = bp->b_mount;
 
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, bp->b_addr);
+       xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
        entries = xfs_attr3_leaf_entryp(bp->b_addr);
        if (count)
                *count = ichdr.count;
@@ -2410,22 +2612,21 @@ xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
  * a "local" or a "remote" attribute.
  */
 int
-xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
+xfs_attr_leaf_newentsize(
+       struct xfs_da_args      *args,
+       int                     *local)
 {
-       int size;
+       int                     size;
 
-       size = xfs_attr_leaf_entsize_local(namelen, valuelen);
-       if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
-               if (local) {
+       size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
+       if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
+               if (local)
                        *local = 1;
-               }
-       } else {
-               size = xfs_attr_leaf_entsize_remote(namelen);
-               if (local) {
-                       *local = 0;
-               }
+               return size;
        }
-       return size;
+       if (local)
+               *local = 0;
+       return xfs_attr_leaf_entsize_remote(args->namelen);
 }
 
 
@@ -2458,14 +2659,14 @@ xfs_attr3_leaf_clearflag(
         */
        error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
        if (error)
-               return(error);
+               return error;
 
        leaf = bp->b_addr;
        entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
        ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
 
 #ifdef DEBUG
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
        ASSERT(args->index < ichdr.count);
        ASSERT(args->index >= 0);
 
@@ -2491,7 +2692,7 @@ xfs_attr3_leaf_clearflag(
                ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
                name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
                name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
-               name_rmt->valuelen = cpu_to_be32(args->valuelen);
+               name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
                xfs_trans_log_buf(args->trans, bp,
                         XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
        }
@@ -2499,7 +2700,7 @@ xfs_attr3_leaf_clearflag(
        /*
         * Commit the flag value change and start the next trans in series.
         */
-       return xfs_trans_roll(&args->trans, args->dp);
+       return xfs_trans_roll_inode(&args->trans, args->dp);
 }
 
 /*
@@ -2525,11 +2726,11 @@ xfs_attr3_leaf_setflag(
         */
        error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
        if (error)
-               return(error);
+               return error;
 
        leaf = bp->b_addr;
 #ifdef DEBUG
-       xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
        ASSERT(args->index < ichdr.count);
        ASSERT(args->index >= 0);
 #endif
@@ -2550,7 +2751,7 @@ xfs_attr3_leaf_setflag(
        /*
         * Commit the flag value change and start the next trans in series.
         */
-       return xfs_trans_roll(&args->trans, args->dp);
+       return xfs_trans_roll_inode(&args->trans, args->dp);
 }
 
 /*
@@ -2608,11 +2809,11 @@ xfs_attr3_leaf_flipflags(
        entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
 
 #ifdef DEBUG
-       xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
        ASSERT(args->index < ichdr1.count);
        ASSERT(args->index >= 0);
 
-       xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
+       xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
        ASSERT(args->index2 < ichdr2.count);
        ASSERT(args->index2 >= 0);
 
@@ -2649,7 +2850,7 @@ xfs_attr3_leaf_flipflags(
                ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
                name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
                name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
-               name_rmt->valuelen = cpu_to_be32(args->valuelen);
+               name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
                xfs_trans_log_buf(args->trans, bp1,
                         XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
        }
@@ -2668,7 +2869,7 @@ xfs_attr3_leaf_flipflags(
        /*
         * Commit the flag value change and start the next trans in series.
         */
-       error = xfs_trans_roll(&args->trans, args->dp);
+       error = xfs_trans_roll_inode(&args->trans, args->dp);
 
        return error;
 }