]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/xfs_da_btree.c
xfs: explicitly pass buffer size to xfs_corruption_error
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_da_btree.c
index 5db94dba783321bee1932410a2b6a2c9c8b10a83..467e96dc9e350522b3435fe6cf84991cb3415946 100644 (file)
  * along with this program; if not, write the Free Software Foundation,
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
-
-#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_da_format.h"
+#include "xfs_da_btree.h"
+#include "xfs_dir2.h"
+#include "xfs_dir2_priv.h"
+#include "xfs_inode.h"
+#include "xfs_trans.h"
+#include "xfs_alloc.h"
+#include "xfs_bmap.h"
+#include "xfs_attr_leaf.h"
+#include "xfs_trace.h"
+#include "xfs_cksum.h"
 
 /*
  * xfs_da_btree.c
@@ -106,98 +123,53 @@ xfs_da_state_free(xfs_da_state_t *state)
        kmem_zone_free(xfs_da_state_zone, state);
 }
 
-void
-xfs_da3_node_hdr_from_disk(
-       struct xfs_da3_icnode_hdr       *to,
-       struct xfs_da_intnode           *from)
-{
-       ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
-              from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
-
-       if (from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
-               struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)from;
-
-               to->forw = be32_to_cpu(hdr3->info.hdr.forw);
-               to->back = be32_to_cpu(hdr3->info.hdr.back);
-               to->magic = be16_to_cpu(hdr3->info.hdr.magic);
-               to->count = be16_to_cpu(hdr3->__count);
-               to->level = be16_to_cpu(hdr3->__level);
-               return;
-       }
-       to->forw = be32_to_cpu(from->hdr.info.forw);
-       to->back = be32_to_cpu(from->hdr.info.back);
-       to->magic = be16_to_cpu(from->hdr.info.magic);
-       to->count = be16_to_cpu(from->hdr.__count);
-       to->level = be16_to_cpu(from->hdr.__level);
-}
-
-void
-xfs_da3_node_hdr_to_disk(
-       struct xfs_da_intnode           *to,
-       struct xfs_da3_icnode_hdr       *from)
-{
-       ASSERT(from->magic == XFS_DA_NODE_MAGIC ||
-              from->magic == XFS_DA3_NODE_MAGIC);
-
-       if (from->magic == XFS_DA3_NODE_MAGIC) {
-               struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)to;
-
-               hdr3->info.hdr.forw = cpu_to_be32(from->forw);
-               hdr3->info.hdr.back = cpu_to_be32(from->back);
-               hdr3->info.hdr.magic = cpu_to_be16(from->magic);
-               hdr3->__count = cpu_to_be16(from->count);
-               hdr3->__level = cpu_to_be16(from->level);
-               return;
-       }
-       to->hdr.info.forw = cpu_to_be32(from->forw);
-       to->hdr.info.back = cpu_to_be32(from->back);
-       to->hdr.info.magic = cpu_to_be16(from->magic);
-       to->hdr.__count = cpu_to_be16(from->count);
-       to->hdr.__level = cpu_to_be16(from->level);
-}
-
-static bool
+static xfs_failaddr_t
 xfs_da3_node_verify(
        struct xfs_buf          *bp)
 {
        struct xfs_mount        *mp = bp->b_target->bt_mount;
        struct xfs_da_intnode   *hdr = bp->b_addr;
        struct xfs_da3_icnode_hdr ichdr;
+       const struct xfs_dir_ops *ops;
+
+       ops = xfs_dir_get_ops(mp, NULL);
 
-       xfs_da3_node_hdr_from_disk(&ichdr, hdr);
+       ops->node_hdr_from_disk(&ichdr, hdr);
 
        if (xfs_sb_version_hascrc(&mp->m_sb)) {
                struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
 
                if (ichdr.magic != XFS_DA3_NODE_MAGIC)
-                       return false;
+                       return __this_address;
 
-               if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid))
-                       return false;
+               if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
+                       return __this_address;
                if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
-                       return false;
+                       return __this_address;
+               if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
+                       return __this_address;
        } else {
                if (ichdr.magic != XFS_DA_NODE_MAGIC)
-                       return false;
+                       return __this_address;
        }
        if (ichdr.level == 0)
-               return false;
+               return __this_address;
        if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
-               return false;
+               return __this_address;
        if (ichdr.count == 0)
-               return false;
+               return __this_address;
 
        /*
         * we don't know if the node is for and attribute or directory tree,
         * so only fail if the count is outside both bounds
         */
-       if (ichdr.count > mp->m_dir_node_ents &&
-           ichdr.count > mp->m_attr_node_ents)
-               return false;
+       if (ichdr.count > mp->m_dir_geo->node_ents &&
+           ichdr.count > mp->m_attr_geo->node_ents)
+               return __this_address;
 
        /* XXX: hash order check? */
 
-       return true;
+       return NULL;
 }
 
 static void
@@ -205,12 +177,13 @@ xfs_da3_node_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_buf_log_item *bip = bp->b_log_item;
        struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
+       xfs_failaddr_t          fa;
 
-       if (!xfs_da3_node_verify(bp)) {
-               XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
-               xfs_buf_ioerror(bp, EFSCORRUPTED);
+       fa = xfs_da3_node_verify(bp);
+       if (fa) {
+               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                return;
        }
 
@@ -220,7 +193,7 @@ xfs_da3_node_write_verify(
        if (bip)
                hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
 
-       xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DA3_NODE_CRC_OFF);
+       xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
 }
 
 /*
@@ -233,18 +206,21 @@ static void
 xfs_da3_node_read_verify(
        struct xfs_buf          *bp)
 {
-       struct xfs_mount        *mp = bp->b_target->bt_mount;
        struct xfs_da_blkinfo   *info = bp->b_addr;
+       xfs_failaddr_t          fa;
 
        switch (be16_to_cpu(info->magic)) {
                case XFS_DA3_NODE_MAGIC:
-                       if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
-                                             XFS_DA3_NODE_CRC_OFF))
+                       if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
+                               xfs_verifier_error(bp, -EFSBADCRC,
+                                               __this_address);
                                break;
+                       }
                        /* fall through */
                case XFS_DA_NODE_MAGIC:
-                       if (!xfs_da3_node_verify(bp))
-                               break;
+                       fa = xfs_da3_node_verify(bp);
+                       if (fa)
+                               xfs_verifier_error(bp, -EFSCORRUPTED, fa);
                        return;
                case XFS_ATTR_LEAF_MAGIC:
                case XFS_ATTR3_LEAF_MAGIC:
@@ -257,20 +233,42 @@ xfs_da3_node_read_verify(
                        bp->b_ops->verify_read(bp);
                        return;
                default:
+                       xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
                        break;
        }
+}
+
+/* Verify the structure of a da3 block. */
+static xfs_failaddr_t
+xfs_da3_node_verify_struct(
+       struct xfs_buf          *bp)
+{
+       struct xfs_da_blkinfo   *info = bp->b_addr;
 
-       /* corrupt block */
-       XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
-       xfs_buf_ioerror(bp, EFSCORRUPTED);
+       switch (be16_to_cpu(info->magic)) {
+       case XFS_DA3_NODE_MAGIC:
+       case XFS_DA_NODE_MAGIC:
+               return xfs_da3_node_verify(bp);
+       case XFS_ATTR_LEAF_MAGIC:
+       case XFS_ATTR3_LEAF_MAGIC:
+               bp->b_ops = &xfs_attr3_leaf_buf_ops;
+               return bp->b_ops->verify_struct(bp);
+       case XFS_DIR2_LEAFN_MAGIC:
+       case XFS_DIR3_LEAFN_MAGIC:
+               bp->b_ops = &xfs_dir3_leafn_buf_ops;
+               return bp->b_ops->verify_struct(bp);
+       default:
+               return __this_address;
+       }
 }
 
 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
+       .name = "xfs_da3_node",
        .verify_read = xfs_da3_node_read_verify,
        .verify_write = xfs_da3_node_write_verify,
+       .verify_struct = xfs_da3_node_verify_struct,
 };
 
-
 int
 xfs_da3_node_read(
        struct xfs_trans        *tp,
@@ -280,8 +278,37 @@ xfs_da3_node_read(
        struct xfs_buf          **bpp,
        int                     which_fork)
 {
-       return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
+       int                     err;
+
+       err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
                                        which_fork, &xfs_da3_node_buf_ops);
+       if (!err && tp && *bpp) {
+               struct xfs_da_blkinfo   *info = (*bpp)->b_addr;
+               int                     type;
+
+               switch (be16_to_cpu(info->magic)) {
+               case XFS_DA_NODE_MAGIC:
+               case XFS_DA3_NODE_MAGIC:
+                       type = XFS_BLFT_DA_NODE_BUF;
+                       break;
+               case XFS_ATTR_LEAF_MAGIC:
+               case XFS_ATTR3_LEAF_MAGIC:
+                       type = XFS_BLFT_ATTR_LEAF_BUF;
+                       break;
+               case XFS_DIR2_LEAFN_MAGIC:
+               case XFS_DIR3_LEAFN_MAGIC:
+                       type = XFS_BLFT_DIR_LEAFN_BUF;
+                       break;
+               default:
+                       XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
+                                       tp->t_mountp, info, sizeof(*info));
+                       xfs_trans_brelse(tp, *bpp);
+                       *bpp = NULL;
+                       return -EFSCORRUPTED;
+               }
+               xfs_trans_buf_set_type(tp, *bpp, type);
+       }
+       return err;
 }
 
 /*========================================================================
@@ -305,34 +332,37 @@ xfs_da3_node_create(
        struct xfs_da3_icnode_hdr ichdr = {0};
        struct xfs_buf          *bp;
        int                     error;
+       struct xfs_inode        *dp = args->dp;
 
        trace_xfs_da_node_create(args);
        ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
 
-       error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
+       error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
        if (error)
-               return(error);
+               return error;
+       bp->b_ops = &xfs_da3_node_buf_ops;
+       xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
        node = bp->b_addr;
 
        if (xfs_sb_version_hascrc(&mp->m_sb)) {
                struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
 
+               memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
                ichdr.magic = XFS_DA3_NODE_MAGIC;
                hdr3->info.blkno = cpu_to_be64(bp->b_bn);
                hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
-               uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_uuid);
+               uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
        } else {
                ichdr.magic = XFS_DA_NODE_MAGIC;
        }
        ichdr.level = level;
 
-       xfs_da3_node_hdr_to_disk(node, &ichdr);
+       dp->d_ops->node_hdr_to_disk(node, &ichdr);
        xfs_trans_log_buf(tp, bp,
-               XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
+               XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
 
-       bp->b_ops = &xfs_da3_node_buf_ops;
        *bpp = bp;
-       return(0);
+       return 0;
 }
 
 /*
@@ -347,9 +377,8 @@ xfs_da3_split(
        struct xfs_da_state_blk *newblk;
        struct xfs_da_state_blk *addblk;
        struct xfs_da_intnode   *node;
-       struct xfs_buf          *bp;
        int                     max;
-       int                     action;
+       int                     action = 0;
        int                     error;
        int                     i;
 
@@ -380,15 +409,17 @@ xfs_da3_split(
                switch (oldblk->magic) {
                case XFS_ATTR_LEAF_MAGIC:
                        error = xfs_attr3_leaf_split(state, oldblk, newblk);
-                       if ((error != 0) && (error != ENOSPC)) {
-                               return(error);  /* GROT: attr is inconsistent */
+                       if ((error != 0) && (error != -ENOSPC)) {
+                               return error;   /* GROT: attr is inconsistent */
                        }
                        if (!error) {
                                addblk = newblk;
                                break;
                        }
                        /*
-                        * Entry wouldn't fit, split the leaf again.
+                        * Entry wouldn't fit, split the leaf again. The new
+                        * extrablk will be consumed by xfs_da3_node_split if
+                        * the node is split.
                         */
                        state->extravalid = 1;
                        if (state->inleaf) {
@@ -403,7 +434,7 @@ xfs_da3_split(
                                                            &state->extrablk);
                        }
                        if (error)
-                               return(error);  /* GROT: attr inconsistent */
+                               return error;   /* GROT: attr inconsistent */
                        addblk = newblk;
                        break;
                case XFS_DIR2_LEAFN_MAGIC:
@@ -417,7 +448,7 @@ xfs_da3_split(
                                                         max - i, &action);
                        addblk->bp = NULL;
                        if (error)
-                               return(error);  /* GROT: dir is inconsistent */
+                               return error;   /* GROT: dir is inconsistent */
                        /*
                         * Record the newly split block for the next time thru?
                         */
@@ -434,7 +465,15 @@ xfs_da3_split(
                xfs_da3_fixhashpath(state, &state->path);
        }
        if (!addblk)
-               return(0);
+               return 0;
+
+       /*
+        * xfs_da3_node_split() should have consumed any extra blocks we added
+        * during a double leaf split in the attr fork. This is guaranteed as
+        * we can't be here if the attr fork only has a single leaf block.
+        */
+       ASSERT(state->extravalid == 0 ||
+              state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
 
        /*
         * Split the root node.
@@ -444,48 +483,40 @@ xfs_da3_split(
        error = xfs_da3_root_split(state, oldblk, addblk);
        if (error) {
                addblk->bp = NULL;
-               return(error);  /* GROT: dir is inconsistent */
+               return error;   /* GROT: dir is inconsistent */
        }
 
        /*
-        * Update pointers to the node which used to be block 0 and
-        * just got bumped because of the addition of a new root node.
-        * There might be three blocks involved if a double split occurred,
-        * and the original block 0 could be at any position in the list.
+        * Update pointers to the node which used to be block 0 and just got
+        * bumped because of the addition of a new root node.  Note that the
+        * original block 0 could be at any position in the list of blocks in
+        * the tree.
         *
-        * Note: the info structures being modified here for both v2 and v3 da
-        * headers, so we can do this linkage just using the v2 structures.
+        * Note: the magic numbers and sibling pointers are in the same physical
+        * place for both v2 and v3 headers (by design). Hence it doesn't matter
+        * which version of the xfs_da_intnode structure we use here as the
+        * result will be the same using either structure.
         */
        node = oldblk->bp->b_addr;
        if (node->hdr.info.forw) {
-               if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
-                       bp = addblk->bp;
-               } else {
-                       ASSERT(state->extravalid);
-                       bp = state->extrablk.bp;
-               }
-               node = bp->b_addr;
+               ASSERT(be32_to_cpu(node->hdr.info.forw) == addblk->blkno);
+               node = addblk->bp->b_addr;
                node->hdr.info.back = cpu_to_be32(oldblk->blkno);
-               xfs_trans_log_buf(state->args->trans, bp,
-                   XFS_DA_LOGRANGE(node, &node->hdr.info,
-                   sizeof(node->hdr.info)));
+               xfs_trans_log_buf(state->args->trans, addblk->bp,
+                                 XFS_DA_LOGRANGE(node, &node->hdr.info,
+                                 sizeof(node->hdr.info)));
        }
        node = oldblk->bp->b_addr;
        if (node->hdr.info.back) {
-               if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
-                       bp = addblk->bp;
-               } else {
-                       ASSERT(state->extravalid);
-                       bp = state->extrablk.bp;
-               }
-               node = bp->b_addr;
+               ASSERT(be32_to_cpu(node->hdr.info.back) == addblk->blkno);
+               node = addblk->bp->b_addr;
                node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
-               xfs_trans_log_buf(state->args->trans, bp,
-                   XFS_DA_LOGRANGE(node, &node->hdr.info,
-                   sizeof(node->hdr.info)));
+               xfs_trans_log_buf(state->args->trans, addblk->bp,
+                                 XFS_DA_LOGRANGE(node, &node->hdr.info,
+                                 sizeof(node->hdr.info)));
        }
        addblk->bp = NULL;
-       return(0);
+       return 0;
 }
 
 /*
@@ -507,7 +538,6 @@ xfs_da3_root_split(
        struct xfs_buf          *bp;
        struct xfs_inode        *dp;
        struct xfs_trans        *tp;
-       struct xfs_mount        *mp;
        struct xfs_dir2_leaf    *leaf;
        xfs_dablk_t             blkno;
        int                     level;
@@ -527,7 +557,6 @@ xfs_da3_root_split(
 
        dp = args->dp;
        tp = args->trans;
-       mp = state->mp;
        error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
        if (error)
                return error;
@@ -535,24 +564,36 @@ xfs_da3_root_split(
        oldroot = blk1->bp->b_addr;
        if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
            oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
-               struct xfs_da3_icnode_hdr nodehdr;
+               struct xfs_da3_icnode_hdr icnodehdr;
 
-               xfs_da3_node_hdr_from_disk(&nodehdr, oldroot);
-               btree = xfs_da3_node_tree_p(oldroot);
-               size = (int)((char *)&btree[nodehdr.count] - (char *)oldroot);
-               level = nodehdr.level;
+               dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot);
+               btree = dp->d_ops->node_tree_p(oldroot);
+               size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
+               level = icnodehdr.level;
+
+               /*
+                * we are about to copy oldroot to bp, so set up the type
+                * of bp while we know exactly what it will be.
+                */
+               xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
        } else {
                struct xfs_dir3_icleaf_hdr leafhdr;
                struct xfs_dir2_leaf_entry *ents;
 
                leaf = (xfs_dir2_leaf_t *)oldroot;
-               xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
-               ents = xfs_dir3_leaf_ents_p(leaf);
+               dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
+               ents = dp->d_ops->leaf_ents_p(leaf);
 
                ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
                       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
                size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
                level = 0;
+
+               /*
+                * we are about to copy oldroot to bp, so set up the type
+                * of bp while we know exactly what it will be.
+                */
+               xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
        }
 
        /*
@@ -571,6 +612,7 @@ xfs_da3_root_split(
        xfs_trans_log_buf(tp, bp, 0, size - 1);
 
        bp->b_ops = blk1->bp->b_ops;
+       xfs_trans_buf_copy_type(bp, blk1->bp);
        blk1->bp = bp;
        blk1->blkno = blkno;
 
@@ -578,28 +620,28 @@ xfs_da3_root_split(
         * Set up the new root node.
         */
        error = xfs_da3_node_create(args,
-               (args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,
+               (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
                level + 1, &bp, args->whichfork);
        if (error)
                return error;
 
        node = bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
-       btree = xfs_da3_node_tree_p(node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+       btree = dp->d_ops->node_tree_p(node);
        btree[0].hashval = cpu_to_be32(blk1->hashval);
        btree[0].before = cpu_to_be32(blk1->blkno);
        btree[1].hashval = cpu_to_be32(blk2->hashval);
        btree[1].before = cpu_to_be32(blk2->blkno);
        nodehdr.count = 2;
-       xfs_da3_node_hdr_to_disk(node, &nodehdr);
+       dp->d_ops->node_hdr_to_disk(node, &nodehdr);
 
 #ifdef DEBUG
        if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
            oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
-               ASSERT(blk1->blkno >= mp->m_dirleafblk &&
-                      blk1->blkno < mp->m_dirfreeblk);
-               ASSERT(blk2->blkno >= mp->m_dirleafblk &&
-                      blk2->blkno < mp->m_dirfreeblk);
+               ASSERT(blk1->blkno >= args->geo->leafblk &&
+                      blk1->blkno < args->geo->freeblk);
+               ASSERT(blk2->blkno >= args->geo->leafblk &&
+                      blk2->blkno < args->geo->freeblk);
        }
 #endif
 
@@ -628,11 +670,12 @@ xfs_da3_node_split(
        int                     newcount;
        int                     error;
        int                     useextra;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_split(state->args);
 
        node = oldblk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
 
        /*
         * With V2 dirs the extra block is data or freespace.
@@ -642,25 +685,25 @@ xfs_da3_node_split(
        /*
         * Do we have to split the node?
         */
-       if (nodehdr.count + newcount > state->node_ents) {
+       if (nodehdr.count + newcount > state->args->geo->node_ents) {
                /*
                 * Allocate a new node, add to the doubly linked chain of
                 * nodes, then move some of our excess entries into it.
                 */
                error = xfs_da_grow_inode(state->args, &blkno);
                if (error)
-                       return(error);  /* GROT: dir is inconsistent */
+                       return error;   /* GROT: dir is inconsistent */
 
                error = xfs_da3_node_create(state->args, blkno, treelevel,
                                           &newblk->bp, state->args->whichfork);
                if (error)
-                       return(error);  /* GROT: dir is inconsistent */
+                       return error;   /* GROT: dir is inconsistent */
                newblk->blkno = blkno;
                newblk->magic = XFS_DA_NODE_MAGIC;
                xfs_da3_node_rebalance(state, oldblk, newblk);
                error = xfs_da3_blk_link(state, oldblk, newblk);
                if (error)
-                       return(error);
+                       return error;
                *result = 1;
        } else {
                *result = 0;
@@ -679,7 +722,7 @@ xfs_da3_node_split(
         * If we had double-split op below us, then add the extra block too.
         */
        node = oldblk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
        if (oldblk->index <= nodehdr.count) {
                oldblk->index++;
                xfs_da3_node_add(state, oldblk, addblk);
@@ -700,7 +743,7 @@ xfs_da3_node_split(
                }
        }
 
-       return(0);
+       return 0;
 }
 
 /*
@@ -728,15 +771,16 @@ xfs_da3_node_rebalance(
        int                     count;
        int                     tmp;
        int                     swap = 0;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_rebalance(state->args);
 
        node1 = blk1->bp->b_addr;
        node2 = blk2->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
-       xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
-       btree1 = xfs_da3_node_tree_p(node1);
-       btree2 = xfs_da3_node_tree_p(node2);
+       dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
+       dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
+       btree1 = dp->d_ops->node_tree_p(node1);
+       btree2 = dp->d_ops->node_tree_p(node2);
 
        /*
         * Figure out how many entries need to move, and in which direction.
@@ -749,10 +793,10 @@ xfs_da3_node_rebalance(
                tmpnode = node1;
                node1 = node2;
                node2 = tmpnode;
-               xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
-               xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
-               btree1 = xfs_da3_node_tree_p(node1);
-               btree2 = xfs_da3_node_tree_p(node2);
+               dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
+               dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
+               btree1 = dp->d_ops->node_tree_p(node1);
+               btree2 = dp->d_ops->node_tree_p(node2);
                swap = 1;
        }
 
@@ -781,7 +825,7 @@ xfs_da3_node_rebalance(
                 */
                nodehdr2.count += count;
                tmp = count * (uint)sizeof(xfs_da_node_entry_t);
-               btree_s = &btree1[nodehdr1.count- count];
+               btree_s = &btree1[nodehdr1.count - count];
                btree_d = &btree2[0];
                memcpy(btree_d, btree_s, tmp);
                nodehdr1.count -= count;
@@ -814,15 +858,14 @@ xfs_da3_node_rebalance(
        /*
         * Log header of node 1 and all current bits of node 2.
         */
-       xfs_da3_node_hdr_to_disk(node1, &nodehdr1);
+       dp->d_ops->node_hdr_to_disk(node1, &nodehdr1);
        xfs_trans_log_buf(tp, blk1->bp,
-               XFS_DA_LOGRANGE(node1, &node1->hdr,
-                               xfs_da3_node_hdr_size(node1)));
+               XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
 
-       xfs_da3_node_hdr_to_disk(node2, &nodehdr2);
+       dp->d_ops->node_hdr_to_disk(node2, &nodehdr2);
        xfs_trans_log_buf(tp, blk2->bp,
                XFS_DA_LOGRANGE(node2, &node2->hdr,
-                               xfs_da3_node_hdr_size(node2) +
+                               dp->d_ops->node_hdr_size +
                                (sizeof(btree2[0]) * nodehdr2.count)));
 
        /*
@@ -832,10 +875,10 @@ xfs_da3_node_rebalance(
        if (swap) {
                node1 = blk1->bp->b_addr;
                node2 = blk2->bp->b_addr;
-               xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
-               xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
-               btree1 = xfs_da3_node_tree_p(node1);
-               btree2 = xfs_da3_node_tree_p(node2);
+               dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
+               dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
+               btree1 = dp->d_ops->node_tree_p(node1);
+               btree2 = dp->d_ops->node_tree_p(node2);
        }
        blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
        blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
@@ -862,18 +905,19 @@ xfs_da3_node_add(
        struct xfs_da3_icnode_hdr nodehdr;
        struct xfs_da_node_entry *btree;
        int                     tmp;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_add(state->args);
 
        node = oldblk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
-       btree = xfs_da3_node_tree_p(node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+       btree = dp->d_ops->node_tree_p(node);
 
        ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
        ASSERT(newblk->blkno != 0);
        if (state->args->whichfork == XFS_DATA_FORK)
-               ASSERT(newblk->blkno >= state->mp->m_dirleafblk &&
-                      newblk->blkno < state->mp->m_dirfreeblk);
+               ASSERT(newblk->blkno >= state->args->geo->leafblk &&
+                      newblk->blkno < state->args->geo->freeblk);
 
        /*
         * We may need to make some room before we insert the new node.
@@ -890,9 +934,9 @@ xfs_da3_node_add(
                                tmp + sizeof(*btree)));
 
        nodehdr.count += 1;
-       xfs_da3_node_hdr_to_disk(node, &nodehdr);
+       dp->d_ops->node_hdr_to_disk(node, &nodehdr);
        xfs_trans_log_buf(state->args->trans, oldblk->bp,
-               XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
+               XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
 
        /*
         * Copy the last hash value from the oldblk to propagate upwards.
@@ -941,9 +985,9 @@ xfs_da3_join(
                case XFS_ATTR_LEAF_MAGIC:
                        error = xfs_attr3_leaf_toosmall(state, &action);
                        if (error)
-                               return(error);
+                               return error;
                        if (action == 0)
-                               return(0);
+                               return 0;
                        xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
                        break;
                case XFS_DIR2_LEAFN_MAGIC:
@@ -963,7 +1007,7 @@ xfs_da3_join(
                        xfs_da3_fixhashpath(state, &state->path);
                        error = xfs_da3_node_toosmall(state, &action);
                        if (error)
-                               return(error);
+                               return error;
                        if (action == 0)
                                return 0;
                        xfs_da3_node_unbalance(state, drop_blk, save_blk);
@@ -973,12 +1017,12 @@ xfs_da3_join(
                error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
                xfs_da_state_kill_altpath(state);
                if (error)
-                       return(error);
+                       return error;
                error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
                                                         drop_blk->bp);
                drop_blk->bp = NULL;
                if (error)
-                       return(error);
+                       return error;
        }
        /*
         * We joined all the way to the top.  If it turns out that
@@ -988,7 +1032,7 @@ xfs_da3_join(
        xfs_da3_node_remove(state, drop_blk);
        xfs_da3_fixhashpath(state, &state->path);
        error = xfs_da3_root_join(state, &state->path.blk[0]);
-       return(error);
+       return error;
 }
 
 #ifdef DEBUG
@@ -1029,6 +1073,7 @@ xfs_da3_root_join(
        struct xfs_da3_icnode_hdr oldroothdr;
        struct xfs_da_node_entry *btree;
        int                     error;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_root_join(state->args);
 
@@ -1036,7 +1081,7 @@ xfs_da3_root_join(
 
        args = state->args;
        oldroot = root_blk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&oldroothdr, oldroot);
+       dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot);
        ASSERT(oldroothdr.forw == 0);
        ASSERT(oldroothdr.back == 0);
 
@@ -1050,10 +1095,10 @@ xfs_da3_root_join(
         * Read in the (only) child block, then copy those bytes into
         * the root block's buffer and free the original child block.
         */
-       btree = xfs_da3_node_tree_p(oldroot);
+       btree = dp->d_ops->node_tree_p(oldroot);
        child = be32_to_cpu(btree[0].before);
        ASSERT(child != 0);
-       error = xfs_da3_node_read(args->trans, args->dp, child, -1, &bp,
+       error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
                                             args->whichfork);
        if (error)
                return error;
@@ -1066,15 +1111,17 @@ xfs_da3_root_join(
         * that could occur. For dir3 blocks we also need to update the block
         * number in the buffer header.
         */
-       memcpy(root_blk->bp->b_addr, bp->b_addr, state->blocksize);
+       memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
        root_blk->bp->b_ops = bp->b_ops;
+       xfs_trans_buf_copy_type(root_blk->bp, bp);
        if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
                struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
                da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
        }
-       xfs_trans_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
+       xfs_trans_log_buf(args->trans, root_blk->bp, 0,
+                         args->geo->blksize - 1);
        error = xfs_da_shrink_inode(args, child, bp);
-       return(error);
+       return error;
 }
 
 /*
@@ -1102,6 +1149,7 @@ xfs_da3_node_toosmall(
        int                     error;
        int                     retval;
        int                     i;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_toosmall(state->args);
 
@@ -1113,10 +1161,10 @@ xfs_da3_node_toosmall(
        blk = &state->path.blk[ state->path.active-1 ];
        info = blk->bp->b_addr;
        node = (xfs_da_intnode_t *)info;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
-       if (nodehdr.count > (state->node_ents >> 1)) {
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+       if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
                *action = 0;    /* blk over 50%, don't try to join */
-               return(0);      /* blk over 50%, don't try to join */
+               return 0;       /* blk over 50%, don't try to join */
        }
 
        /*
@@ -1135,13 +1183,13 @@ xfs_da3_node_toosmall(
                error = xfs_da3_path_shift(state, &state->altpath, forward,
                                                 0, &retval);
                if (error)
-                       return(error);
+                       return error;
                if (retval) {
                        *action = 0;
                } else {
                        *action = 2;
                }
-               return(0);
+               return 0;
        }
 
        /*
@@ -1151,29 +1199,30 @@ xfs_da3_node_toosmall(
         * We prefer coalescing with the lower numbered sibling so as
         * to shrink a directory over time.
         */
-       count  = state->node_ents;
-       count -= state->node_ents >> 2;
+       count  = state->args->geo->node_ents;
+       count -= state->args->geo->node_ents >> 2;
        count -= nodehdr.count;
 
        /* start with smaller blk num */
        forward = nodehdr.forw < nodehdr.back;
        for (i = 0; i < 2; forward = !forward, i++) {
+               struct xfs_da3_icnode_hdr thdr;
                if (forward)
                        blkno = nodehdr.forw;
                else
                        blkno = nodehdr.back;
                if (blkno == 0)
                        continue;
-               error = xfs_da3_node_read(state->args->trans, state->args->dp,
+               error = xfs_da3_node_read(state->args->trans, dp,
                                        blkno, -1, &bp, state->args->whichfork);
                if (error)
-                       return(error);
+                       return error;
 
                node = bp->b_addr;
-               xfs_da3_node_hdr_from_disk(&nodehdr, node);
+               dp->d_ops->node_hdr_from_disk(&thdr, node);
                xfs_trans_brelse(state->args->trans, bp);
 
-               if (count - nodehdr.count >= 0)
+               if (count - thdr.count >= 0)
                        break;  /* fits with at least 25% to spare */
        }
        if (i >= 2) {
@@ -1208,6 +1257,7 @@ xfs_da3_node_toosmall(
  */
 STATIC uint
 xfs_da3_node_lasthash(
+       struct xfs_inode        *dp,
        struct xfs_buf          *bp,
        int                     *count)
 {
@@ -1216,12 +1266,12 @@ xfs_da3_node_lasthash(
        struct xfs_da3_icnode_hdr nodehdr;
 
        node = bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
        if (count)
                *count = nodehdr.count;
        if (!nodehdr.count)
                return 0;
-       btree = xfs_da3_node_tree_p(node);
+       btree = dp->d_ops->node_tree_p(node);
        return be32_to_cpu(btree[nodehdr.count - 1].hashval);
 }
 
@@ -1240,6 +1290,7 @@ xfs_da3_fixhashpath(
        xfs_dahash_t            lasthash=0;
        int                     level;
        int                     count;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_fixhashpath(state->args);
 
@@ -1252,12 +1303,12 @@ xfs_da3_fixhashpath(
                        return;
                break;
        case XFS_DIR2_LEAFN_MAGIC:
-               lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
+               lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
                if (count == 0)
                        return;
                break;
        case XFS_DA_NODE_MAGIC:
-               lasthash = xfs_da3_node_lasthash(blk->bp, &count);
+               lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
                if (count == 0)
                        return;
                break;
@@ -1266,9 +1317,9 @@ xfs_da3_fixhashpath(
                struct xfs_da3_icnode_hdr nodehdr;
 
                node = blk->bp->b_addr;
-               xfs_da3_node_hdr_from_disk(&nodehdr, node);
-               btree = xfs_da3_node_tree_p(node);
-               if (be32_to_cpu(btree->hashval) == lasthash)
+               dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+               btree = dp->d_ops->node_tree_p(node);
+               if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
                        break;
                blk->hashval = lasthash;
                btree[blk->index].hashval = cpu_to_be32(lasthash);
@@ -1293,11 +1344,12 @@ xfs_da3_node_remove(
        struct xfs_da_node_entry *btree;
        int                     index;
        int                     tmp;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_remove(state->args);
 
        node = drop_blk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&nodehdr, node);
+       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
        ASSERT(drop_blk->index < nodehdr.count);
        ASSERT(drop_blk->index >= 0);
 
@@ -1305,7 +1357,7 @@ xfs_da3_node_remove(
         * Copy over the offending entry, or just zero it out.
         */
        index = drop_blk->index;
-       btree = xfs_da3_node_tree_p(node);
+       btree = dp->d_ops->node_tree_p(node);
        if (index < nodehdr.count - 1) {
                tmp  = nodehdr.count - index - 1;
                tmp *= (uint)sizeof(xfs_da_node_entry_t);
@@ -1318,9 +1370,9 @@ xfs_da3_node_remove(
        xfs_trans_log_buf(state->args->trans, drop_blk->bp,
            XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
        nodehdr.count -= 1;
-       xfs_da3_node_hdr_to_disk(node, &nodehdr);
+       dp->d_ops->node_hdr_to_disk(node, &nodehdr);
        xfs_trans_log_buf(state->args->trans, drop_blk->bp,
-           XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
+           XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
 
        /*
         * Copy the last hash value from the block to propagate upwards.
@@ -1340,63 +1392,65 @@ xfs_da3_node_unbalance(
 {
        struct xfs_da_intnode   *drop_node;
        struct xfs_da_intnode   *save_node;
-       struct xfs_da_node_entry *dbtree;
-       struct xfs_da_node_entry *sbtree;
-       struct xfs_da3_icnode_hdr dhdr;
-       struct xfs_da3_icnode_hdr shdr;
+       struct xfs_da_node_entry *drop_btree;
+       struct xfs_da_node_entry *save_btree;
+       struct xfs_da3_icnode_hdr drop_hdr;
+       struct xfs_da3_icnode_hdr save_hdr;
        struct xfs_trans        *tp;
        int                     sindex;
        int                     tmp;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_node_unbalance(state->args);
 
        drop_node = drop_blk->bp->b_addr;
        save_node = save_blk->bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&dhdr, drop_node);
-       xfs_da3_node_hdr_from_disk(&shdr, save_node);
-       dbtree = xfs_da3_node_tree_p(drop_node);
-       sbtree = xfs_da3_node_tree_p(save_node);
+       dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node);
+       dp->d_ops->node_hdr_from_disk(&save_hdr, save_node);
+       drop_btree = dp->d_ops->node_tree_p(drop_node);
+       save_btree = dp->d_ops->node_tree_p(save_node);
        tp = state->args->trans;
 
        /*
         * If the dying block has lower hashvals, then move all the
         * elements in the remaining block up to make a hole.
         */
-       if ((be32_to_cpu(dbtree[0].hashval) < be32_to_cpu(sbtree[ 0 ].hashval)) ||
-           (be32_to_cpu(dbtree[dhdr.count - 1].hashval) <
-                               be32_to_cpu(sbtree[shdr.count - 1].hashval))) {
+       if ((be32_to_cpu(drop_btree[0].hashval) <
+                       be32_to_cpu(save_btree[0].hashval)) ||
+           (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
+                       be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
                /* XXX: check this - is memmove dst correct? */
-               tmp = shdr.count * (uint)sizeof(xfs_da_node_entry_t);
-               memmove(&sbtree[dhdr.count], &sbtree[0], tmp);
+               tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
+               memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
 
                sindex = 0;
                xfs_trans_log_buf(tp, save_blk->bp,
-                       XFS_DA_LOGRANGE(save_node, &sbtree[0],
-                               (shdr.count + dhdr.count) *
+                       XFS_DA_LOGRANGE(save_node, &save_btree[0],
+                               (save_hdr.count + drop_hdr.count) *
                                                sizeof(xfs_da_node_entry_t)));
        } else {
-               sindex = shdr.count;
+               sindex = save_hdr.count;
                xfs_trans_log_buf(tp, save_blk->bp,
-                       XFS_DA_LOGRANGE(save_node, &sbtree[sindex],
-                               dhdr.count * sizeof(xfs_da_node_entry_t)));
+                       XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
+                               drop_hdr.count * sizeof(xfs_da_node_entry_t)));
        }
 
        /*
         * Move all the B-tree elements from drop_blk to save_blk.
         */
-       tmp = dhdr.count * (uint)sizeof(xfs_da_node_entry_t);
-       memcpy(&sbtree[sindex], &dbtree[0], tmp);
-       shdr.count += dhdr.count;
+       tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
+       memcpy(&save_btree[sindex], &drop_btree[0], tmp);
+       save_hdr.count += drop_hdr.count;
 
-       xfs_da3_node_hdr_to_disk(save_node, &shdr);
+       dp->d_ops->node_hdr_to_disk(save_node, &save_hdr);
        xfs_trans_log_buf(tp, save_blk->bp,
                XFS_DA_LOGRANGE(save_node, &save_node->hdr,
-                               xfs_da3_node_hdr_size(save_node)));
+                               dp->d_ops->node_hdr_size));
 
        /*
         * Save the last hashval in the remaining block for upward propagation.
         */
-       save_blk->hashval = be32_to_cpu(sbtree[shdr.count - 1].hashval);
+       save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
 }
 
 /*========================================================================
@@ -1433,6 +1487,8 @@ xfs_da3_node_lookup_int(
        int                     max;
        int                     error;
        int                     retval;
+       unsigned int            expected_level = 0;
+       struct xfs_inode        *dp = state->args->dp;
 
        args = state->args;
 
@@ -1440,7 +1496,7 @@ xfs_da3_node_lookup_int(
         * Descend thru the B-tree searching each level for the right
         * node to use, until the right hashval is found.
         */
-       blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
+       blkno = args->geo->leafblk;
        for (blk = &state->path.blk[0], state->path.active = 1;
                         state->path.active <= XFS_DA_NODE_MAXDEPTH;
                         blk++, state->path.active++) {
@@ -1453,7 +1509,7 @@ xfs_da3_node_lookup_int(
                if (error) {
                        blk->blkno = 0;
                        state->path.active--;
-                       return(error);
+                       return error;
                }
                curr = blk->bp->b_addr;
                blk->magic = be16_to_cpu(curr->magic);
@@ -1468,7 +1524,8 @@ xfs_da3_node_lookup_int(
                if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
                    blk->magic == XFS_DIR3_LEAFN_MAGIC) {
                        blk->magic = XFS_DIR2_LEAFN_MAGIC;
-                       blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
+                       blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
+                                                             blk->bp, NULL);
                        break;
                }
 
@@ -1479,8 +1536,20 @@ xfs_da3_node_lookup_int(
                 * Search an intermediate node for a match.
                 */
                node = blk->bp->b_addr;
-               xfs_da3_node_hdr_from_disk(&nodehdr, node);
-               btree = xfs_da3_node_tree_p(node);
+               dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+               btree = dp->d_ops->node_tree_p(node);
+
+               /* Tree taller than we can handle; bail out! */
+               if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
+                       return -EFSCORRUPTED;
+
+               /* Check the level from the root. */
+               if (blkno == args->geo->leafblk)
+                       expected_level = nodehdr.level - 1;
+               else if (expected_level != nodehdr.level)
+                       return -EFSCORRUPTED;
+               else
+                       expected_level--;
 
                max = nodehdr.count;
                blk->hashval = be32_to_cpu(btree[max - 1].hashval);
@@ -1527,8 +1596,15 @@ xfs_da3_node_lookup_int(
                        blk->index = probe;
                        blkno = be32_to_cpu(btree[probe].before);
                }
+
+               /* We can't point back to the root. */
+               if (blkno == args->geo->leafblk)
+                       return -EFSCORRUPTED;
        }
 
+       if (expected_level != 0)
+               return -EFSCORRUPTED;
+
        /*
         * A leaf block that ends in the hashval that we are interested in
         * (final hashval == search hashval) means that the next block may
@@ -1545,25 +1621,25 @@ xfs_da3_node_lookup_int(
                        args->blkno = blk->blkno;
                } else {
                        ASSERT(0);
-                       return XFS_ERROR(EFSCORRUPTED);
+                       return -EFSCORRUPTED;
                }
-               if (((retval == ENOENT) || (retval == ENOATTR)) &&
+               if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
                    (blk->hashval == args->hashval)) {
                        error = xfs_da3_path_shift(state, &state->path, 1, 1,
                                                         &retval);
                        if (error)
-                               return(error);
+                               return error;
                        if (retval == 0) {
                                continue;
                        } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
                                /* path_shift() gives ENOENT */
-                               retval = XFS_ERROR(ENOATTR);
+                               retval = -ENOATTR;
                        }
                }
                break;
        }
        *result = retval;
-       return(0);
+       return 0;
 }
 
 /*========================================================================
@@ -1575,6 +1651,7 @@ xfs_da3_node_lookup_int(
  */
 STATIC int
 xfs_da3_node_order(
+       struct xfs_inode *dp,
        struct xfs_buf  *node1_bp,
        struct xfs_buf  *node2_bp)
 {
@@ -1587,10 +1664,10 @@ xfs_da3_node_order(
 
        node1 = node1_bp->b_addr;
        node2 = node2_bp->b_addr;
-       xfs_da3_node_hdr_from_disk(&node1hdr, node1);
-       xfs_da3_node_hdr_from_disk(&node2hdr, node2);
-       btree1 = xfs_da3_node_tree_p(node1);
-       btree2 = xfs_da3_node_tree_p(node2);
+       dp->d_ops->node_hdr_from_disk(&node1hdr, node1);
+       dp->d_ops->node_hdr_from_disk(&node2hdr, node2);
+       btree1 = dp->d_ops->node_tree_p(node1);
+       btree2 = dp->d_ops->node_tree_p(node2);
 
        if (node1hdr.count > 0 && node2hdr.count > 0 &&
            ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
@@ -1617,6 +1694,7 @@ xfs_da3_blk_link(
        struct xfs_buf          *bp;
        int                     before = 0;
        int                     error;
+       struct xfs_inode        *dp = state->args->dp;
 
        /*
         * Set up environment.
@@ -1634,10 +1712,10 @@ xfs_da3_blk_link(
                before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
                break;
        case XFS_DIR2_LEAFN_MAGIC:
-               before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
+               before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
                break;
        case XFS_DA_NODE_MAGIC:
-               before = xfs_da3_node_order(old_blk->bp, new_blk->bp);
+               before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
                break;
        }
 
@@ -1652,11 +1730,11 @@ xfs_da3_blk_link(
                new_info->forw = cpu_to_be32(old_blk->blkno);
                new_info->back = old_info->back;
                if (old_info->back) {
-                       error = xfs_da3_node_read(args->trans, args->dp,
+                       error = xfs_da3_node_read(args->trans, dp,
                                                be32_to_cpu(old_info->back),
                                                -1, &bp, args->whichfork);
                        if (error)
-                               return(error);
+                               return error;
                        ASSERT(bp != NULL);
                        tmp_info = bp->b_addr;
                        ASSERT(tmp_info->magic == old_info->magic);
@@ -1673,11 +1751,11 @@ xfs_da3_blk_link(
                new_info->forw = old_info->forw;
                new_info->back = cpu_to_be32(old_blk->blkno);
                if (old_info->forw) {
-                       error = xfs_da3_node_read(args->trans, args->dp,
+                       error = xfs_da3_node_read(args->trans, dp,
                                                be32_to_cpu(old_info->forw),
                                                -1, &bp, args->whichfork);
                        if (error)
-                               return(error);
+                               return error;
                        ASSERT(bp != NULL);
                        tmp_info = bp->b_addr;
                        ASSERT(tmp_info->magic == old_info->magic);
@@ -1690,7 +1768,7 @@ xfs_da3_blk_link(
 
        xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
        xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
-       return(0);
+       return 0;
 }
 
 /*
@@ -1736,7 +1814,7 @@ xfs_da3_blk_unlink(
                                                be32_to_cpu(drop_info->back),
                                                -1, &bp, args->whichfork);
                        if (error)
-                               return(error);
+                               return error;
                        ASSERT(bp != NULL);
                        tmp_info = bp->b_addr;
                        ASSERT(tmp_info->magic == save_info->magic);
@@ -1753,7 +1831,7 @@ xfs_da3_blk_unlink(
                                                be32_to_cpu(drop_info->forw),
                                                -1, &bp, args->whichfork);
                        if (error)
-                               return(error);
+                               return error;
                        ASSERT(bp != NULL);
                        tmp_info = bp->b_addr;
                        ASSERT(tmp_info->magic == save_info->magic);
@@ -1765,7 +1843,7 @@ xfs_da3_blk_unlink(
        }
 
        xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
-       return(0);
+       return 0;
 }
 
 /*
@@ -1790,9 +1868,11 @@ xfs_da3_path_shift(
        struct xfs_da_args      *args;
        struct xfs_da_node_entry *btree;
        struct xfs_da3_icnode_hdr nodehdr;
+       struct xfs_buf          *bp;
        xfs_dablk_t             blkno = 0;
        int                     level;
        int                     error;
+       struct xfs_inode        *dp = state->args->dp;
 
        trace_xfs_da_path_shift(state->args);
 
@@ -1808,8 +1888,8 @@ xfs_da3_path_shift(
        level = (path->active-1) - 1;   /* skip bottom layer in path */
        for (blk = &path->blk[level]; level >= 0; blk--, level--) {
                node = blk->bp->b_addr;
-               xfs_da3_node_hdr_from_disk(&nodehdr, node);
-               btree = xfs_da3_node_tree_p(node);
+               dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+               btree = dp->d_ops->node_tree_p(node);
 
                if (forward && (blk->index < nodehdr.count - 1)) {
                        blk->index++;
@@ -1822,9 +1902,9 @@ xfs_da3_path_shift(
                }
        }
        if (level < 0) {
-               *result = XFS_ERROR(ENOENT);    /* we're out of our tree */
+               *result = -ENOENT;      /* we're out of our tree */
                ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
-               return(0);
+               return 0;
        }
 
        /*
@@ -1833,20 +1913,24 @@ xfs_da3_path_shift(
         */
        for (blk++, level++; level < path->active; blk++, level++) {
                /*
-                * Release the old block.
-                * (if it's dirty, trans won't actually let go)
+                * Read the next child block into a local buffer.
                 */
-               if (release)
-                       xfs_trans_brelse(args->trans, blk->bp);
+               error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
+                                         args->whichfork);
+               if (error)
+                       return error;
 
                /*
-                * Read the next child block.
+                * Release the old block (if it's dirty, the trans doesn't
+                * actually let go) and swap the local buffer into the path
+                * structure. This ensures failure of the above read doesn't set
+                * a NULL buffer in an active slot in the path.
                 */
+               if (release)
+                       xfs_trans_brelse(args->trans, blk->bp);
                blk->blkno = blkno;
-               error = xfs_da3_node_read(args->trans, args->dp, blkno, -1,
-                                       &blk->bp, args->whichfork);
-               if (error)
-                       return(error);
+               blk->bp = bp;
+
                info = blk->bp->b_addr;
                ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
                       info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
@@ -1865,8 +1949,8 @@ xfs_da3_path_shift(
                case XFS_DA3_NODE_MAGIC:
                        blk->magic = XFS_DA_NODE_MAGIC;
                        node = (xfs_da_intnode_t *)info;
-                       xfs_da3_node_hdr_from_disk(&nodehdr, node);
-                       btree = xfs_da3_node_tree_p(node);
+                       dp->d_ops->node_hdr_from_disk(&nodehdr, node);
+                       btree = dp->d_ops->node_tree_p(node);
                        blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
                        if (forward)
                                blk->index = 0;
@@ -1879,16 +1963,15 @@ xfs_da3_path_shift(
                        blk->magic = XFS_ATTR_LEAF_MAGIC;
                        ASSERT(level == path->active-1);
                        blk->index = 0;
-                       blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
-                                                             NULL);
+                       blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
                        break;
                case XFS_DIR2_LEAFN_MAGIC:
                case XFS_DIR3_LEAFN_MAGIC:
                        blk->magic = XFS_DIR2_LEAFN_MAGIC;
                        ASSERT(level == path->active-1);
                        blk->index = 0;
-                       blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
-                                                              NULL);
+                       blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
+                                                             blk->bp, NULL);
                        break;
                default:
                        ASSERT(0);
@@ -1910,7 +1993,7 @@ xfs_da3_path_shift(
  * This is implemented with some source-level loop unrolling.
  */
 xfs_dahash_t
-xfs_da_hashname(const __uint8_t *name, int namelen)
+xfs_da_hashname(const uint8_t *name, int namelen)
 {
        xfs_dahash_t hash;
 
@@ -1968,7 +2051,7 @@ xfs_da_grow_inode_int(
        struct xfs_trans        *tp = args->trans;
        struct xfs_inode        *dp = args->dp;
        int                     w = args->whichfork;
-       xfs_drfsbno_t           nblks = dp->i_d.di_nblocks;
+       xfs_rfsblock_t          nblks = dp->i_d.di_nblocks;
        struct xfs_bmbt_irec    map, *mapp;
        int                     nmap, error, got, i, mapi;
 
@@ -1987,7 +2070,7 @@ xfs_da_grow_inode_int(
        error = xfs_bmapi_write(tp, dp, *bno, count,
                        xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
                        args->firstblock, args->total, &map, &nmap,
-                       args->flist);
+                       args->dfops);
        if (error)
                return error;
 
@@ -2010,7 +2093,7 @@ xfs_da_grow_inode_int(
                        error = xfs_bmapi_write(tp, dp, b, c,
                                        xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
                                        args->firstblock, args->total,
-                                       &mapp[mapi], &nmap, args->flist);
+                                       &mapp[mapi], &nmap, args->dfops);
                        if (error)
                                goto out_free_map;
                        if (nmap < 1)
@@ -2032,7 +2115,7 @@ xfs_da_grow_inode_int(
        if (got != count || mapp[0].br_startoff != *bno ||
            mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
            *bno + count) {
-               error = XFS_ERROR(ENOSPC);
+               error = -ENOSPC;
                goto out_free_map;
        }
 
@@ -2055,20 +2138,12 @@ xfs_da_grow_inode(
        xfs_dablk_t             *new_blkno)
 {
        xfs_fileoff_t           bno;
-       int                     count;
        int                     error;
 
        trace_xfs_da_grow_inode(args);
 
-       if (args->whichfork == XFS_DATA_FORK) {
-               bno = args->dp->i_mount->m_dirleafblk;
-               count = args->dp->i_mount->m_dirblkfsbs;
-       } else {
-               bno = 0;
-               count = 1;
-       }
-
-       error = xfs_da_grow_inode_int(args, &bno, count);
+       bno = args->geo->leafblk;
+       error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
        if (!error)
                *new_blkno = (xfs_dablk_t)bno;
        return error;
@@ -2095,7 +2170,7 @@ xfs_da3_swap_lastblock(
        struct xfs_dir2_leaf    *dead_leaf2;
        struct xfs_da_node_entry *btree;
        struct xfs_da3_icnode_hdr par_hdr;
-       struct xfs_inode        *ip;
+       struct xfs_inode        *dp;
        struct xfs_trans        *tp;
        struct xfs_mount        *mp;
        struct xfs_buf          *dead_buf;
@@ -2119,31 +2194,31 @@ xfs_da3_swap_lastblock(
        dead_buf = *dead_bufp;
        dead_blkno = *dead_blknop;
        tp = args->trans;
-       ip = args->dp;
+       dp = args->dp;
        w = args->whichfork;
        ASSERT(w == XFS_DATA_FORK);
-       mp = ip->i_mount;
-       lastoff = mp->m_dirfreeblk;
-       error = xfs_bmap_last_before(tp, ip, &lastoff, w);
+       mp = dp->i_mount;
+       lastoff = args->geo->freeblk;
+       error = xfs_bmap_last_before(tp, dp, &lastoff, w);
        if (error)
                return error;
        if (unlikely(lastoff == 0)) {
                XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
                                 mp);
-               return XFS_ERROR(EFSCORRUPTED);
+               return -EFSCORRUPTED;
        }
        /*
         * Read the last block in the btree space.
         */
-       last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
-       error = xfs_da3_node_read(tp, ip, last_blkno, -1, &last_buf, w);
+       last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
+       error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
        if (error)
                return error;
        /*
         * Copy the last block into the dead buffer and log it.
         */
-       memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
-       xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
+       memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
+       xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
        dead_info = dead_buf->b_addr;
        /*
         * Get values from the moved block.
@@ -2154,18 +2229,16 @@ xfs_da3_swap_lastblock(
                struct xfs_dir2_leaf_entry *ents;
 
                dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
-               xfs_dir3_leaf_hdr_from_disk(&leafhdr, dead_leaf2);
-               ents = xfs_dir3_leaf_ents_p(dead_leaf2);
+               dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
+               ents = dp->d_ops->leaf_ents_p(dead_leaf2);
                dead_level = 0;
                dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
        } else {
                struct xfs_da3_icnode_hdr deadhdr;
 
-               ASSERT(dead_info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
-                      dead_info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
                dead_node = (xfs_da_intnode_t *)dead_info;
-               xfs_da3_node_hdr_from_disk(&deadhdr, dead_node);
-               btree = xfs_da3_node_tree_p(dead_node);
+               dp->d_ops->node_hdr_from_disk(&deadhdr, dead_node);
+               btree = dp->d_ops->node_tree_p(dead_node);
                dead_level = deadhdr.level;
                dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
        }
@@ -2174,7 +2247,7 @@ xfs_da3_swap_lastblock(
         * If the moved block has a left sibling, fix up the pointers.
         */
        if ((sib_blkno = be32_to_cpu(dead_info->back))) {
-               error = xfs_da3_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
+               error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
                if (error)
                        goto done;
                sib_info = sib_buf->b_addr;
@@ -2183,7 +2256,7 @@ xfs_da3_swap_lastblock(
                    sib_info->magic != dead_info->magic)) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
                sib_info->forw = cpu_to_be32(dead_blkno);
@@ -2196,7 +2269,7 @@ xfs_da3_swap_lastblock(
         * If the moved block has a right sibling, fix up the pointers.
         */
        if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
-               error = xfs_da3_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
+               error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
                if (error)
                        goto done;
                sib_info = sib_buf->b_addr;
@@ -2205,7 +2278,7 @@ xfs_da3_swap_lastblock(
                       sib_info->magic != dead_info->magic)) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
                sib_info->back = cpu_to_be32(dead_blkno);
@@ -2214,25 +2287,25 @@ xfs_da3_swap_lastblock(
                                        sizeof(sib_info->back)));
                sib_buf = NULL;
        }
-       par_blkno = mp->m_dirleafblk;
+       par_blkno = args->geo->leafblk;
        level = -1;
        /*
         * Walk down the tree looking for the parent of the moved block.
         */
        for (;;) {
-               error = xfs_da3_node_read(tp, ip, par_blkno, -1, &par_buf, w);
+               error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
                if (error)
                        goto done;
                par_node = par_buf->b_addr;
-               xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
+               dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
                if (level >= 0 && level != par_hdr.level + 1) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
                level = par_hdr.level;
-               btree = xfs_da3_node_tree_p(par_node);
+               btree = dp->d_ops->node_tree_p(par_node);
                for (entno = 0;
                     entno < par_hdr.count &&
                     be32_to_cpu(btree[entno].hashval) < dead_hash;
@@ -2241,7 +2314,7 @@ xfs_da3_swap_lastblock(
                if (entno == par_hdr.count) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
                par_blkno = be32_to_cpu(btree[entno].before);
@@ -2268,21 +2341,21 @@ xfs_da3_swap_lastblock(
                if (unlikely(par_blkno == 0)) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
-               error = xfs_da3_node_read(tp, ip, par_blkno, -1, &par_buf, w);
+               error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
                if (error)
                        goto done;
                par_node = par_buf->b_addr;
-               xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
+               dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
                if (par_hdr.level != level) {
                        XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
                                         XFS_ERRLEVEL_LOW, mp);
-                       error = XFS_ERROR(EFSCORRUPTED);
+                       error = -EFSCORRUPTED;
                        goto done;
                }
-               btree = xfs_da3_node_tree_p(par_node);
+               btree = dp->d_ops->node_tree_p(par_node);
                entno = 0;
        }
        /*
@@ -2316,27 +2389,22 @@ xfs_da_shrink_inode(
        xfs_inode_t *dp;
        int done, error, w, count;
        xfs_trans_t *tp;
-       xfs_mount_t *mp;
 
        trace_xfs_da_shrink_inode(args);
 
        dp = args->dp;
        w = args->whichfork;
        tp = args->trans;
-       mp = dp->i_mount;
-       if (w == XFS_DATA_FORK)
-               count = mp->m_dirblkfsbs;
-       else
-               count = 1;
+       count = args->geo->fsbcount;
        for (;;) {
                /*
                 * Remove extents.  If we get ENOSPC for a dir we have to move
                 * the last block to the place we want to kill.
                 */
                error = xfs_bunmapi(tp, dp, dead_blkno, count,
-                                   xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
-                                   0, args->firstblock, args->flist, &done);
-               if (error == ENOSPC) {
+                                   xfs_bmapi_aflag(w), 0, args->firstblock,
+                                   args->dfops, &done);
+               if (error == -ENOSPC) {
                        if (w != XFS_DATA_FORK)
                                break;
                        error = xfs_da3_swap_lastblock(args, &dead_blkno,
@@ -2390,9 +2458,9 @@ static int
 xfs_buf_map_from_irec(
        struct xfs_mount        *mp,
        struct xfs_buf_map      **mapp,
-       unsigned int            *nmaps,
+       int                     *nmaps,
        struct xfs_bmbt_irec    *irecs,
-       unsigned int            nirecs)
+       int                     nirecs)
 {
        struct xfs_buf_map      *map;
        int                     i;
@@ -2401,9 +2469,10 @@ xfs_buf_map_from_irec(
        ASSERT(nirecs >= 1);
 
        if (nirecs > 1) {
-               map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_SLEEP);
+               map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
+                                 KM_SLEEP | KM_NOFS);
                if (!map)
-                       return ENOMEM;
+                       return -ENOMEM;
                *mapp = map;
        }
 
@@ -2428,7 +2497,6 @@ xfs_buf_map_from_irec(
  */
 static int
 xfs_dabuf_map(
-       struct xfs_trans        *trans,
        struct xfs_inode        *dp,
        xfs_dablk_t             bno,
        xfs_daddr_t             mappedbno,
@@ -2446,7 +2514,10 @@ xfs_dabuf_map(
        ASSERT(map && *map);
        ASSERT(*nmaps == 1);
 
-       nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
+       if (whichfork == XFS_DATA_FORK)
+               nfsb = mp->m_dir_geo->fsbcount;
+       else
+               nfsb = mp->m_attr_geo->fsbcount;
 
        /*
         * Caller doesn't have a mapping.  -2 means don't complain
@@ -2457,7 +2528,8 @@ xfs_dabuf_map(
                 * Optimize the one-block case.
                 */
                if (nfsb != 1)
-                       irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP);
+                       irecs = kmem_zalloc(sizeof(irec) * nfsb,
+                                           KM_SLEEP | KM_NOFS);
 
                nirecs = nfsb;
                error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
@@ -2473,8 +2545,8 @@ xfs_dabuf_map(
        }
 
        if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
-               error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
-               if (unlikely(error == EFSCORRUPTED)) {
+               error = mappedbno == -2 ? -1 : -EFSCORRUPTED;
+               if (unlikely(error == -EFSCORRUPTED)) {
                        if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
                                int i;
                                xfs_alert(mp, "%s: bno %lld dir: inode %lld",
@@ -2523,7 +2595,7 @@ xfs_da_get_buf(
        *bpp = NULL;
        mapp = &map;
        nmap = 1;
-       error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
+       error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
                                &mapp, &nmap);
        if (error) {
                /* mapping a hole is not an error, but we don't continue */
@@ -2534,9 +2606,10 @@ xfs_da_get_buf(
 
        bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
                                    mapp, nmap, 0);
-       error = bp ? bp->b_error : XFS_ERROR(EIO);
+       error = bp ? bp->b_error : -EIO;
        if (error) {
-               xfs_trans_brelse(trans, bp);
+               if (bp)
+                       xfs_trans_brelse(trans, bp);
                goto out_free;
        }
 
@@ -2571,7 +2644,7 @@ xfs_da_read_buf(
        *bpp = NULL;
        mapp = &map;
        nmap = 1;
-       error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
+       error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
                                &mapp, &nmap);
        if (error) {
                /* mapping a hole is not an error, but we don't continue */
@@ -2590,47 +2663,6 @@ xfs_da_read_buf(
                xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
        else
                xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
-
-       /*
-        * This verification code will be moved to a CRC verification callback
-        * function so just leave it here unchanged until then.
-        */
-       {
-               xfs_dir2_data_hdr_t     *hdr = bp->b_addr;
-               xfs_dir2_free_t         *free = bp->b_addr;
-               xfs_da_blkinfo_t        *info = bp->b_addr;
-               uint                    magic, magic1;
-               struct xfs_mount        *mp = dp->i_mount;
-
-               magic = be16_to_cpu(info->magic);
-               magic1 = be32_to_cpu(hdr->magic);
-               if (unlikely(
-                   XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
-                                  (magic != XFS_DA3_NODE_MAGIC) &&
-                                  (magic != XFS_ATTR_LEAF_MAGIC) &&
-                                  (magic != XFS_ATTR3_LEAF_MAGIC) &&
-                                  (magic != XFS_DIR2_LEAF1_MAGIC) &&
-                                  (magic != XFS_DIR3_LEAF1_MAGIC) &&
-                                  (magic != XFS_DIR2_LEAFN_MAGIC) &&
-                                  (magic != XFS_DIR3_LEAFN_MAGIC) &&
-                                  (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
-                                  (magic1 != XFS_DIR3_BLOCK_MAGIC) &&
-                                  (magic1 != XFS_DIR2_DATA_MAGIC) &&
-                                  (magic1 != XFS_DIR3_DATA_MAGIC) &&
-                                  (free->hdr.magic !=
-                                       cpu_to_be32(XFS_DIR2_FREE_MAGIC)) &&
-                                  (free->hdr.magic !=
-                                       cpu_to_be32(XFS_DIR3_FREE_MAGIC)),
-                               mp, XFS_ERRTAG_DA_READ_BUF,
-                               XFS_RANDOM_DA_READ_BUF))) {
-                       trace_xfs_da_btree_corrupt(bp, _RET_IP_);
-                       XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
-                                            XFS_ERRLEVEL_LOW, mp, info);
-                       error = XFS_ERROR(EFSCORRUPTED);
-                       xfs_trans_brelse(trans, bp);
-                       goto out_free;
-               }
-       }
        *bpp = bp;
 out_free:
        if (mapp != &map)
@@ -2642,9 +2674,8 @@ out_free:
 /*
  * Readahead the dir/attr block.
  */
-xfs_daddr_t
+int
 xfs_da_reada_buf(
-       struct xfs_trans        *trans,
        struct xfs_inode        *dp,
        xfs_dablk_t             bno,
        xfs_daddr_t             mappedbno,
@@ -2658,7 +2689,7 @@ xfs_da_reada_buf(
 
        mapp = &map;
        nmap = 1;
-       error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
+       error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
                                &mapp, &nmap);
        if (error) {
                /* mapping a hole is not an error, but we don't continue */
@@ -2674,7 +2705,5 @@ out_free:
        if (mapp != &map)
                kmem_free(mapp);
 
-       if (error)
-               return -1;
-       return mappedbno;
+       return error;
 }