xfs: improve metadata I/O merging in the elevator
[safe/jmp/linux-2.6] / fs / xfs / xfs_dir2_leaf.c
index 056f528..29f484c 100644 (file)
@@ -1,71 +1,45 @@
 /*
- * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
+ * All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
+ * 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.
+ * 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.
  *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
- */
-
-/*
- * xfs_dir2_leaf.c
- * XFS directory version 2 implementation - single leaf form
- * see xfs_dir2_leaf.h for data structures.
- * These directories have multiple XFS_DIR2_DATA blocks and one
- * XFS_DIR2_LEAF1 block containing the hash table and freespace map.
+ * 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 "xfs.h"
-
-#include "xfs_macros.h"
+#include "xfs_fs.h"
 #include "xfs_types.h"
-#include "xfs_inum.h"
+#include "xfs_bit.h"
 #include "xfs_log.h"
+#include "xfs_inum.h"
 #include "xfs_trans.h"
 #include "xfs_sb.h"
 #include "xfs_ag.h"
-#include "xfs_dir.h"
 #include "xfs_dir2.h"
 #include "xfs_dmapi.h"
 #include "xfs_mount.h"
+#include "xfs_da_btree.h"
 #include "xfs_bmap_btree.h"
 #include "xfs_attr_sf.h"
-#include "xfs_dir_sf.h"
 #include "xfs_dir2_sf.h"
 #include "xfs_dinode.h"
 #include "xfs_inode.h"
 #include "xfs_bmap.h"
-#include "xfs_da_btree.h"
 #include "xfs_dir2_data.h"
 #include "xfs_dir2_leaf.h"
 #include "xfs_dir2_block.h"
 #include "xfs_dir2_node.h"
-#include "xfs_dir2_trace.h"
 #include "xfs_error.h"
-#include "xfs_bit.h"
+#include "xfs_trace.h"
 
 /*
  * Local function declarations.
@@ -90,7 +64,7 @@ xfs_dir2_block_to_leaf(
        xfs_da_args_t           *args,          /* operation arguments */
        xfs_dabuf_t             *dbp)           /* input block's buffer */
 {
-       xfs_dir2_data_off_t     *bestsp;        /* leaf's bestsp entries */
+       __be16                  *bestsp;        /* leaf's bestsp entries */
        xfs_dablk_t             blkno;          /* leaf block's bno */
        xfs_dir2_block_t        *block;         /* block structure */
        xfs_dir2_leaf_entry_t   *blp;           /* block's leaf entries */
@@ -106,7 +80,8 @@ xfs_dir2_block_to_leaf(
        int                     needscan;       /* need to rescan bestfree */
        xfs_trans_t             *tp;            /* transaction pointer */
 
-       xfs_dir2_trace_args_b("block_to_leaf", args, dbp);
+       trace_xfs_dir2_block_to_leaf(args);
+
        dp = args->dp;
        mp = dp->i_mount;
        tp = args->trans;
@@ -118,7 +93,7 @@ xfs_dir2_block_to_leaf(
        if ((error = xfs_da_grow_inode(args, &blkno))) {
                return error;
        }
-       ldb = XFS_DIR2_DA_TO_DB(mp, blkno);
+       ldb = xfs_dir2_da_to_db(mp, blkno);
        ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
        /*
         * Initialize the leaf block, get a buffer for it.
@@ -130,19 +105,19 @@ xfs_dir2_block_to_leaf(
        leaf = lbp->data;
        block = dbp->data;
        xfs_dir2_data_check(dp, dbp);
-       btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
-       blp = XFS_DIR2_BLOCK_LEAF_P(btp);
+       btp = xfs_dir2_block_tail_p(mp, block);
+       blp = xfs_dir2_block_leaf_p(btp);
        /*
         * Set the counts in the leaf header.
         */
-       INT_COPY(leaf->hdr.count, btp->count, ARCH_CONVERT); /* INT_: type change */
-       INT_COPY(leaf->hdr.stale, btp->stale, ARCH_CONVERT); /* INT_: type change */
+       leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
+       leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
        /*
         * Could compact these but I think we always do the conversion
         * after squeezing out stale entries.
         */
-       memcpy(leaf->ents, blp, INT_GET(btp->count, ARCH_CONVERT) * sizeof(xfs_dir2_leaf_entry_t));
-       xfs_dir2_leaf_log_ents(tp, lbp, 0, INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1);
+       memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
+       xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
        needscan = 0;
        needlog = 1;
        /*
@@ -157,17 +132,16 @@ xfs_dir2_block_to_leaf(
        /*
         * Fix up the block header, make it a data block.
         */
-       INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC);
+       block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
        if (needscan)
-               xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
-                       NULL);
+               xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
        /*
         * Set up leaf tail and bests table.
         */
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
-       INT_SET(ltp->bestcount, ARCH_CONVERT, 1);
-       bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
-       INT_COPY(bestsp[0], block->hdr.bestfree[0].length, ARCH_CONVERT);
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
+       ltp->bestcount = cpu_to_be32(1);
+       bestsp = xfs_dir2_leaf_bests_p(ltp);
+       bestsp[0] =  block->hdr.bestfree[0].length;
        /*
         * Log the data header and leaf bests table.
         */
@@ -187,7 +161,7 @@ int                                         /* error */
 xfs_dir2_leaf_addname(
        xfs_da_args_t           *args)          /* operation arguments */
 {
-       xfs_dir2_data_off_t     *bestsp;        /* freespace table in leaf */
+       __be16                  *bestsp;        /* freespace table in leaf */
        int                     compact;        /* need to compact leaves */
        xfs_dir2_data_t         *data;          /* data block structure */
        xfs_dabuf_t             *dbp;           /* data block buffer */
@@ -211,11 +185,12 @@ xfs_dir2_leaf_addname(
        int                     needbytes;      /* leaf block bytes needed */
        int                     needlog;        /* need to log data header */
        int                     needscan;       /* need to rescan data free */
-       xfs_dir2_data_off_t     *tagp;          /* end of data entry */
+       __be16                  *tagp;          /* end of data entry */
        xfs_trans_t             *tp;            /* transaction pointer */
        xfs_dir2_db_t           use_block;      /* data block number */
 
-       xfs_dir2_trace_args("leaf_addname", args);
+       trace_xfs_dir2_leaf_addname(args);
+
        dp = args->dp;
        tp = args->trans;
        mp = dp->i_mount;
@@ -236,9 +211,9 @@ xfs_dir2_leaf_addname(
         */
        index = xfs_dir2_leaf_search_hash(args, lbp);
        leaf = lbp->data;
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
-       bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
-       length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
+       bestsp = xfs_dir2_leaf_bests_p(ltp);
+       length = xfs_dir2_data_entsize(args->namelen);
        /*
         * See if there are any entries with the same hash value
         * and space in their block for the new entry.
@@ -246,14 +221,14 @@ xfs_dir2_leaf_addname(
         * in a data block, improving the lookup of those entries.
         */
        for (use_block = -1, lep = &leaf->ents[index];
-            index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
+            index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
             index++, lep++) {
-               if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
+               if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
                        continue;
-               i = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
-               ASSERT(i < INT_GET(ltp->bestcount, ARCH_CONVERT));
-               ASSERT(INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF);
-               if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
+               i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
+               ASSERT(i < be32_to_cpu(ltp->bestcount));
+               ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
+               if (be16_to_cpu(bestsp[i]) >= length) {
                        use_block = i;
                        break;
                }
@@ -262,13 +237,13 @@ xfs_dir2_leaf_addname(
         * Didn't find a block yet, linear search all the data blocks.
         */
        if (use_block == -1) {
-               for (i = 0; i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++) {
+               for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
                        /*
                         * Remember a block we see that's missing.
                         */
-                       if (INT_GET(bestsp[i], ARCH_CONVERT) == NULLDATAOFF && use_block == -1)
+                       if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
                                use_block = i;
-                       else if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) {
+                       else if (be16_to_cpu(bestsp[i]) >= length) {
                                use_block = i;
                                break;
                        }
@@ -284,26 +259,27 @@ xfs_dir2_leaf_addname(
         * Now kill use_block if it refers to a missing block, so we
         * can use it as an indication of allocation needed.
         */
-       if (use_block != -1 && INT_GET(bestsp[use_block], ARCH_CONVERT) == NULLDATAOFF)
+       if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
                use_block = -1;
        /*
         * If we don't have enough free bytes but we can make enough
         * by compacting out stale entries, we'll do that.
         */
-       if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] < needbytes &&
-           INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1) {
+       if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
+                               needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
                compact = 1;
        }
        /*
         * Otherwise if we don't have enough free bytes we need to
         * convert to node form.
         */
-       else if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <
-                needbytes) {
+       else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
+                                               leaf->hdr.count)] < needbytes) {
                /*
                 * Just checking or no space reservation, give up.
                 */
-               if (args->justcheck || args->total == 0) {
+               if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
+                                                       args->total == 0) {
                        xfs_da_brelse(tp, lbp);
                        return XFS_ERROR(ENOSPC);
                }
@@ -328,7 +304,7 @@ xfs_dir2_leaf_addname(
         * If just checking, then it will fit unless we needed to allocate
         * a new data block.
         */
-       if (args->justcheck) {
+       if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
                xfs_da_brelse(tp, lbp);
                return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
        }
@@ -354,8 +330,8 @@ xfs_dir2_leaf_addname(
         * There are stale entries, so we'll need log-low and log-high
         * impossibly bad values later.
         */
-       else if (INT_GET(leaf->hdr.stale, ARCH_CONVERT)) {
-               lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
+       else if (be16_to_cpu(leaf->hdr.stale)) {
+               lfloglow = be16_to_cpu(leaf->hdr.count);
                lfloghigh = -1;
        }
        /*
@@ -382,13 +358,13 @@ xfs_dir2_leaf_addname(
                 * If we're adding a new data block on the end we need to
                 * extend the bests table.  Copy it up one entry.
                 */
-               if (use_block >= INT_GET(ltp->bestcount, ARCH_CONVERT)) {
+               if (use_block >= be32_to_cpu(ltp->bestcount)) {
                        bestsp--;
                        memmove(&bestsp[0], &bestsp[1],
-                               INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(bestsp[0]));
-                       INT_MOD(ltp->bestcount, ARCH_CONVERT, +1);
+                               be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
+                       be32_add_cpu(&ltp->bestcount, 1);
                        xfs_dir2_leaf_log_tail(tp, lbp);
-                       xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
+                       xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
                }
                /*
                 * If we're filling in a previously empty block just log it.
@@ -396,7 +372,7 @@ xfs_dir2_leaf_addname(
                else
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
                data = dbp->data;
-               INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
+               bestsp[use_block] = data->hdr.bestfree[0].length;
                grown = 1;
        }
        /*
@@ -405,7 +381,7 @@ xfs_dir2_leaf_addname(
         */
        else {
                if ((error =
-                   xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, use_block),
+                   xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
                            -1, &dbp, XFS_DATA_FORK))) {
                        xfs_da_brelse(tp, lbp);
                        return error;
@@ -418,8 +394,8 @@ xfs_dir2_leaf_addname(
         * Point to the biggest freespace in our data block.
         */
        dup = (xfs_dir2_data_unused_t *)
-             ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
-       ASSERT(INT_GET(dup->length, ARCH_CONVERT) >= length);
+             ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
+       ASSERT(be16_to_cpu(dup->length) >= length);
        needscan = needlog = 0;
        /*
         * Mark the initial part of our freespace in use for the new entry.
@@ -431,16 +407,16 @@ xfs_dir2_leaf_addname(
         * Initialize our new entry (at last).
         */
        dep = (xfs_dir2_data_entry_t *)dup;
-       INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
+       dep->inumber = cpu_to_be64(args->inumber);
        dep->namelen = args->namelen;
        memcpy(dep->name, args->name, dep->namelen);
-       tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
-       INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
+       tagp = xfs_dir2_data_entry_tag_p(dep);
+       *tagp = cpu_to_be16((char *)dep - (char *)data);
        /*
         * Need to scan fix up the bestfree table.
         */
        if (needscan)
-               xfs_dir2_data_freescan(mp, data, &needlog, NULL);
+               xfs_dir2_data_freescan(mp, data, &needlog);
        /*
         * Need to log the data block's header.
         */
@@ -451,8 +427,8 @@ xfs_dir2_leaf_addname(
         * If the bests table needs to be changed, do it.
         * Log the change unless we've already done that.
         */
-       if (INT_GET(bestsp[use_block], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
-               INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT);
+       if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
+               bestsp[use_block] = data->hdr.bestfree[0].length;
                if (!grown)
                        xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
        }
@@ -464,15 +440,15 @@ xfs_dir2_leaf_addname(
                /*
                 * lep is still good as the index leaf entry.
                 */
-               if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
+               if (index < be16_to_cpu(leaf->hdr.count))
                        memmove(lep + 1, lep,
-                               (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
+                               (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
                /*
                 * Record low and high logging indices for the leaf.
                 */
                lfloglow = index;
-               lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
-               INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
+               lfloghigh = be16_to_cpu(leaf->hdr.count);
+               be16_add_cpu(&leaf->hdr.count, 1);
        }
        /*
         * There are stale entries.
@@ -492,7 +468,7 @@ xfs_dir2_leaf_addname(
                         */
                        for (lowstale = index - 1;
                             lowstale >= 0 &&
-                               INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
+                               be32_to_cpu(leaf->ents[lowstale].address) !=
                                XFS_DIR2_NULL_DATAPTR;
                             lowstale--)
                                continue;
@@ -502,8 +478,8 @@ xfs_dir2_leaf_addname(
                         * lowstale entry would be better.
                         */
                        for (highstale = index;
-                            highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
-                               INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
+                            highstale < be16_to_cpu(leaf->hdr.count) &&
+                               be32_to_cpu(leaf->ents[highstale].address) !=
                                XFS_DIR2_NULL_DATAPTR &&
                                (lowstale < 0 ||
                                 index - lowstale - 1 >= highstale - index);
@@ -514,10 +490,10 @@ xfs_dir2_leaf_addname(
                 * If the low one is better, use it.
                 */
                if (lowstale >= 0 &&
-                   (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
+                   (highstale == be16_to_cpu(leaf->hdr.count) ||
                     index - lowstale - 1 < highstale - index)) {
                        ASSERT(index - lowstale - 1 >= 0);
-                       ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
+                       ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
                               XFS_DIR2_NULL_DATAPTR);
                        /*
                         * Copy entries up to cover the stale entry
@@ -536,10 +512,10 @@ xfs_dir2_leaf_addname(
                 */
                else {
                        ASSERT(highstale - index >= 0);
-                       ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
+                       ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
                               XFS_DIR2_NULL_DATAPTR);
                        /*
-                        * Copy entries down to copver the stale entry
+                        * Copy entries down to cover the stale entry
                         * and make room for the new entry.
                         */
                        if (highstale - index > 0)
@@ -550,13 +526,14 @@ xfs_dir2_leaf_addname(
                        lfloglow = MIN(index, lfloglow);
                        lfloghigh = MAX(highstale, lfloghigh);
                }
-               INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
+               be16_add_cpu(&leaf->hdr.stale, -1);
        }
        /*
         * Fill in the new leaf entry.
         */
-       INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
-       INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, use_block, INT_GET(*tagp, ARCH_CONVERT)));
+       lep->hashval = cpu_to_be32(args->hashval);
+       lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
+                               be16_to_cpu(*tagp)));
        /*
         * Log the leaf fields and give up the buffers.
         */
@@ -574,7 +551,7 @@ xfs_dir2_leaf_addname(
  * Check the internal consistency of a leaf1 block.
  * Pop an assert if something is wrong.
  */
-void
+STATIC void
 xfs_dir2_leaf_check(
        xfs_inode_t             *dp,            /* incore directory inode */
        xfs_dabuf_t             *bp)            /* leaf's buffer */
@@ -587,30 +564,30 @@ xfs_dir2_leaf_check(
 
        leaf = bp->data;
        mp = dp->i_mount;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
        /*
         * This value is not restrictive enough.
         * Should factor in the size of the bests table as well.
         * We can deduce a value for that from di_size.
         */
-       ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
+       ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
        /*
         * Leaves and bests don't overlap.
         */
-       ASSERT((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <=
-              (char *)XFS_DIR2_LEAF_BESTS_P(ltp));
+       ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
+              (char *)xfs_dir2_leaf_bests_p(ltp));
        /*
         * Check hash value order, count stale entries.
         */
-       for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
-               if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT))
-                       ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
-                              INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
-               if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
+       for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
+               if (i + 1 < be16_to_cpu(leaf->hdr.count))
+                       ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
+                              be32_to_cpu(leaf->ents[i + 1].hashval));
+               if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
                        stale++;
        }
-       ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
+       ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
 }
 #endif /* DEBUG */
 
@@ -635,8 +612,8 @@ xfs_dir2_leaf_compact(
        /*
         * Compress out the stale entries in place.
         */
-       for (from = to = 0, loglow = -1; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
-               if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
+       for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
+               if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
                        continue;
                /*
                 * Only actually copy the entries that are different.
@@ -651,8 +628,8 @@ xfs_dir2_leaf_compact(
        /*
         * Update and log the header, log the leaf entries.
         */
-       ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == from - to);
-       INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(INT_GET(leaf->hdr.stale, ARCH_CONVERT)));
+       ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
+       be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
        leaf->hdr.stale = 0;
        xfs_dir2_leaf_log_header(args->trans, bp);
        if (loglow != -1)
@@ -686,14 +663,14 @@ xfs_dir2_leaf_compact_x1(
        int             to;             /* destination copy index */
 
        leaf = bp->data;
-       ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1);
+       ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
        index = *indexp;
        /*
         * Find the first stale entry before our index, if any.
         */
        for (lowstale = index - 1;
             lowstale >= 0 &&
-               INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR;
+               be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
             lowstale--)
                continue;
        /*
@@ -701,8 +678,8 @@ xfs_dir2_leaf_compact_x1(
         * Stop if the answer would be worse than lowstale.
         */
        for (highstale = index;
-            highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
-               INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR &&
+            highstale < be16_to_cpu(leaf->hdr.count) &&
+               be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
                (lowstale < 0 || index - lowstale > highstale - index);
             highstale++)
                continue;
@@ -710,7 +687,7 @@ xfs_dir2_leaf_compact_x1(
         * Pick the better of lowstale and highstale.
         */
        if (lowstale >= 0 &&
-           (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
+           (highstale == be16_to_cpu(leaf->hdr.count) ||
             index - lowstale <= highstale - index))
                keepstale = lowstale;
        else
@@ -719,14 +696,14 @@ xfs_dir2_leaf_compact_x1(
         * Copy the entries in place, removing all the stale entries
         * except keepstale.
         */
-       for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) {
+       for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
                /*
                 * Notice the new value of index.
                 */
                if (index == from)
                        newindex = to;
                if (from != keepstale &&
-                   INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) {
+                   be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
                        if (from == to)
                                *lowlogp = to;
                        continue;
@@ -754,8 +731,8 @@ xfs_dir2_leaf_compact_x1(
        /*
         * Adjust the leaf header values.
         */
-       INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(from - to));
-       INT_SET(leaf->hdr.stale, ARCH_CONVERT, 1);
+       be16_add_cpu(&leaf->hdr.count, -(from - to));
+       leaf->hdr.stale = cpu_to_be16(1);
        /*
         * Remember the low/high stale value only in the "right"
         * direction.
@@ -763,8 +740,8 @@ xfs_dir2_leaf_compact_x1(
        if (lowstale >= newindex)
                lowstale = -1;
        else
-               highstale = INT_GET(leaf->hdr.count, ARCH_CONVERT);
-       *highlogp = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1;
+               highstale = be16_to_cpu(leaf->hdr.count);
+       *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
        *lowstalep = lowstale;
        *highstalep = highstale;
 }
@@ -775,12 +752,11 @@ xfs_dir2_leaf_compact_x1(
  */
 int                                            /* error */
 xfs_dir2_leaf_getdents(
-       xfs_trans_t             *tp,            /* transaction pointer */
        xfs_inode_t             *dp,            /* incore directory inode */
-       uio_t                   *uio,           /* I/O control & vectors */
-       int                     *eofp,          /* out: reached end of dir */
-       xfs_dirent_t            *dbp,           /* caller's buffer */
-       xfs_dir2_put_t          put)            /* ABI formatting routine */
+       void                    *dirent,
+       size_t                  bufsize,
+       xfs_off_t               *offset,
+       filldir_t               filldir)
 {
        xfs_dabuf_t             *bp;            /* data block buffer */
        int                     byteoff;        /* offset in current block */
@@ -789,8 +765,7 @@ xfs_dir2_leaf_getdents(
        xfs_dir2_data_t         *data;          /* data block structure */
        xfs_dir2_data_entry_t   *dep;           /* data entry */
        xfs_dir2_data_unused_t  *dup;           /* unused entry */
-       int                     eof;            /* reached end of directory */
-       int                     error=0;                /* error return value */
+       int                     error = 0;      /* error return value */
        int                     i;              /* temporary loop index */
        int                     j;              /* temporary loop index */
        int                     length;         /* temporary length value */
@@ -802,8 +777,7 @@ xfs_dir2_leaf_getdents(
        xfs_mount_t             *mp;            /* filesystem mount point */
        xfs_dir2_off_t          newoff;         /* new curoff after new blk */
        int                     nmap;           /* mappings to ask xfs_bmapi */
-       xfs_dir2_put_args_t     p;              /* formatting arg bundle */
-       char                    *ptr=NULL;              /* pointer to current data */
+       char                    *ptr = NULL;    /* pointer to current data */
        int                     ra_current;     /* number of read-ahead blks */
        int                     ra_index;       /* *map index for read-ahead */
        int                     ra_offset;      /* map entry offset for ra */
@@ -811,41 +785,34 @@ xfs_dir2_leaf_getdents(
 
        /*
         * If the offset is at or past the largest allowed value,
-        * give up right away, return eof.
+        * give up right away.
         */
-       if (uio->uio_offset >= XFS_DIR2_MAX_DATAPTR) {
-               *eofp = 1;
+       if (*offset >= XFS_DIR2_MAX_DATAPTR)
                return 0;
-       }
+
        mp = dp->i_mount;
-       /*
-        * Setup formatting arguments.
-        */
-       p.dbp = dbp;
-       p.put = put;
-       p.uio = uio;
+
        /*
         * Set up to bmap a number of blocks based on the caller's
         * buffer size, the directory block size, and the filesystem
         * block size.
         */
-       map_size =
-               howmany(uio->uio_resid + mp->m_dirblksize,
-                       mp->m_sb.sb_blocksize);
+       map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
        map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
        map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
        bp = NULL;
-       eof = 1;
+
        /*
         * Inside the loop we keep the main offset value as a byte offset
         * in the directory file.
         */
-       curoff = XFS_DIR2_DATAPTR_TO_BYTE(mp, uio->uio_offset);
+       curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
+
        /*
         * Force this conversion through db so we truncate the offset
         * down to get the start of the data block.
         */
-       map_off = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, curoff));
+       map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
        /*
         * Loop over directory entries until we reach the end offset.
         * Get more blocks and readahead as necessary.
@@ -861,7 +828,7 @@ xfs_dir2_leaf_getdents(
                         * take it out of the mapping.
                         */
                        if (bp) {
-                               xfs_da_brelse(tp, bp);
+                               xfs_da_brelse(NULL, bp);
                                bp = NULL;
                                map_blocks -= mp->m_dirblkfsbs;
                                /*
@@ -887,26 +854,28 @@ xfs_dir2_leaf_getdents(
                        /*
                         * Recalculate the readahead blocks wanted.
                         */
-                       ra_want = howmany(uio->uio_resid + mp->m_dirblksize,
+                       ra_want = howmany(bufsize + mp->m_dirblksize,
                                          mp->m_sb.sb_blocksize) - 1;
+                       ASSERT(ra_want >= 0);
+
                        /*
                         * If we don't have as many as we want, and we haven't
                         * run out of data blocks, get some more mappings.
                         */
                        if (1 + ra_want > map_blocks &&
                            map_off <
-                           XFS_DIR2_BYTE_TO_DA(mp, XFS_DIR2_LEAF_OFFSET)) {
+                           xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
                                /*
                                 * Get more bmaps, fill in after the ones
                                 * we already have in the table.
                                 */
                                nmap = map_size - map_valid;
-                               error = xfs_bmapi(tp, dp,
+                               error = xfs_bmapi(NULL, dp,
                                        map_off,
-                                       XFS_DIR2_BYTE_TO_DA(mp,
+                                       xfs_dir2_byte_to_da(mp,
                                                XFS_DIR2_LEAF_OFFSET) - map_off,
                                        XFS_BMAPI_METADATA, NULL, 0,
-                                       &map[map_valid], &nmap, NULL);
+                                       &map[map_valid], &nmap, NULL, NULL);
                                /*
                                 * Don't know if we should ignore this or
                                 * try to return an error.
@@ -928,7 +897,7 @@ xfs_dir2_leaf_getdents(
                                        map[map_valid + nmap - 1].br_blockcount;
                                else
                                        map_off =
-                                               XFS_DIR2_BYTE_TO_DA(mp,
+                                               xfs_dir2_byte_to_da(mp,
                                                        XFS_DIR2_LEAF_OFFSET);
                                /*
                                 * Look for holes in the mapping, and
@@ -956,15 +925,15 @@ xfs_dir2_leaf_getdents(
                         * No valid mappings, so no more data blocks.
                         */
                        if (!map_valid) {
-                               curoff = XFS_DIR2_DA_TO_BYTE(mp, map_off);
+                               curoff = xfs_dir2_da_to_byte(mp, map_off);
                                break;
                        }
                        /*
                         * Read the directory block starting at the first
                         * mapping.
                         */
-                       curdb = XFS_DIR2_DA_TO_DB(mp, map->br_startoff);
-                       error = xfs_da_read_buf(tp, dp, map->br_startoff,
+                       curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
+                       error = xfs_da_read_buf(NULL, dp, map->br_startoff,
                                map->br_blockcount >= mp->m_dirblkfsbs ?
                                    XFS_FSB_TO_DADDR(mp, map->br_startblock) :
                                    -1,
@@ -1007,7 +976,7 @@ xfs_dir2_leaf_getdents(
                                 * is a very rare case.
                                 */
                                else if (i > ra_current) {
-                                       (void)xfs_da_reada_buf(tp, dp,
+                                       (void)xfs_da_reada_buf(NULL, dp,
                                                map[ra_index].br_startoff +
                                                ra_offset, XFS_DATA_FORK);
                                        ra_current = i;
@@ -1039,7 +1008,7 @@ xfs_dir2_leaf_getdents(
                        /*
                         * Having done a read, we need to set a new offset.
                         */
-                       newoff = XFS_DIR2_DB_OFF_TO_BYTE(mp, curdb, 0);
+                       newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
                        /*
                         * Start of the current block.
                         */
@@ -1049,7 +1018,7 @@ xfs_dir2_leaf_getdents(
                         * Make sure we're in the right block.
                         */
                        else if (curoff > newoff)
-                               ASSERT(XFS_DIR2_BYTE_TO_DB(mp, curoff) ==
+                               ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
                                       curdb);
                        data = bp->data;
                        xfs_dir2_data_check(dp, bp);
@@ -1057,7 +1026,7 @@ xfs_dir2_leaf_getdents(
                         * Find our position in the block.
                         */
                        ptr = (char *)&data->u;
-                       byteoff = XFS_DIR2_BYTE_TO_OFF(mp, curoff);
+                       byteoff = xfs_dir2_byte_to_off(mp, curoff);
                        /*
                         * Skip past the header.
                         */
@@ -1070,25 +1039,24 @@ xfs_dir2_leaf_getdents(
                                while ((char *)ptr - (char *)data < byteoff) {
                                        dup = (xfs_dir2_data_unused_t *)ptr;
 
-                                       if (INT_GET(dup->freetag, ARCH_CONVERT)
+                                       if (be16_to_cpu(dup->freetag)
                                                  == XFS_DIR2_DATA_FREE_TAG) {
 
-                                               length = INT_GET(dup->length,
-                                                                ARCH_CONVERT);
+                                               length = be16_to_cpu(dup->length);
                                                ptr += length;
                                                continue;
                                        }
                                        dep = (xfs_dir2_data_entry_t *)ptr;
                                        length =
-                                          XFS_DIR2_DATA_ENTSIZE(dep->namelen);
+                                          xfs_dir2_data_entsize(dep->namelen);
                                        ptr += length;
                                }
                                /*
                                 * Now set our real offset.
                                 */
                                curoff =
-                                       XFS_DIR2_DB_OFF_TO_BYTE(mp,
-                                           XFS_DIR2_BYTE_TO_DB(mp, curoff),
+                                       xfs_dir2_db_off_to_byte(mp,
+                                           xfs_dir2_byte_to_db(mp, curoff),
                                            (char *)ptr - (char *)data);
                                if (ptr >= (char *)data + mp->m_dirblksize) {
                                        continue;
@@ -1103,58 +1071,40 @@ xfs_dir2_leaf_getdents(
                /*
                 * No, it's unused, skip over it.
                 */
-               if (INT_GET(dup->freetag, ARCH_CONVERT)
-                                               == XFS_DIR2_DATA_FREE_TAG) {
-                       length = INT_GET(dup->length, ARCH_CONVERT);
+               if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
+                       length = be16_to_cpu(dup->length);
                        ptr += length;
                        curoff += length;
                        continue;
                }
 
-               /*
-                * Copy the entry into the putargs, and try formatting it.
-                */
                dep = (xfs_dir2_data_entry_t *)ptr;
+               length = xfs_dir2_data_entsize(dep->namelen);
 
-               p.namelen = dep->namelen;
-
-               length = XFS_DIR2_DATA_ENTSIZE(p.namelen);
-
-               p.cook = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff + length);
-
-               p.ino = INT_GET(dep->inumber, ARCH_CONVERT);
-#if XFS_BIG_INUMS
-               p.ino += mp->m_inoadd;
-#endif
-               p.name = (char *)dep->name;
-
-               error = p.put(&p);
-
-               /*
-                * Won't fit.  Return to caller.
-                */
-               if (!p.done) {
-                       eof = 0;
+               if (filldir(dirent, dep->name, dep->namelen,
+                           xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
+                           be64_to_cpu(dep->inumber), DT_UNKNOWN))
                        break;
-               }
+
                /*
                 * Advance to next entry in the block.
                 */
                ptr += length;
                curoff += length;
+               /* bufsize may have just been a guess; don't go negative */
+               bufsize = bufsize > length ? bufsize - length : 0;
        }
 
        /*
         * All done.  Set output offset value to current offset.
         */
-       *eofp = eof;
-       if (curoff > XFS_DIR2_DATAPTR_TO_BYTE(mp, XFS_DIR2_MAX_DATAPTR))
-               uio->uio_offset = XFS_DIR2_MAX_DATAPTR;
+       if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
+               *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
        else
-               uio->uio_offset = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff);
-       kmem_free(map, map_size * sizeof(*map));
+               *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
+       kmem_free(map);
        if (bp)
-               xfs_da_brelse(tp, bp);
+               xfs_da_brelse(NULL, bp);
        return error;
 }
 
@@ -1185,7 +1135,7 @@ xfs_dir2_leaf_init(
        /*
         * Get the buffer for the block.
         */
-       error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, bno), -1, &bp,
+       error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
                XFS_DATA_FORK);
        if (error) {
                return error;
@@ -1195,7 +1145,7 @@ xfs_dir2_leaf_init(
        /*
         * Initialize the header.
         */
-       INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, magic);
+       leaf->hdr.info.magic = cpu_to_be16(magic);
        leaf->hdr.info.forw = 0;
        leaf->hdr.info.back = 0;
        leaf->hdr.count = 0;
@@ -1207,7 +1157,7 @@ xfs_dir2_leaf_init(
         * the block.
         */
        if (magic == XFS_DIR2_LEAF1_MAGIC) {
-               ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
+               ltp = xfs_dir2_leaf_tail_p(mp, leaf);
                ltp->bestcount = 0;
                xfs_dir2_leaf_log_tail(tp, bp);
        }
@@ -1225,16 +1175,16 @@ xfs_dir2_leaf_log_bests(
        int                     first,          /* first entry to log */
        int                     last)           /* last entry to log */
 {
-       xfs_dir2_data_off_t     *firstb;        /* pointer to first entry */
-       xfs_dir2_data_off_t     *lastb;         /* pointer to last entry */
+       __be16                  *firstb;        /* pointer to first entry */
+       __be16                  *lastb;         /* pointer to last entry */
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
        xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
 
        leaf = bp->data;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
-       ltp = XFS_DIR2_LEAF_TAIL_P(tp->t_mountp, leaf);
-       firstb = XFS_DIR2_LEAF_BESTS_P(ltp) + first;
-       lastb = XFS_DIR2_LEAF_BESTS_P(ltp) + last;
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
+       ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
+       firstb = xfs_dir2_leaf_bests_p(ltp) + first;
+       lastb = xfs_dir2_leaf_bests_p(ltp) + last;
        xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
                (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
 }
@@ -1254,8 +1204,8 @@ xfs_dir2_leaf_log_ents(
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
 
        leaf = bp->data;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
-              INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
+              be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
        firstlep = &leaf->ents[first];
        lastlep = &leaf->ents[last];
        xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
@@ -1273,8 +1223,8 @@ xfs_dir2_leaf_log_header(
        xfs_dir2_leaf_t         *leaf;          /* leaf structure */
 
        leaf = bp->data;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC ||
-              INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
+              be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
        xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
                (uint)(sizeof(leaf->hdr) - 1));
 }
@@ -1293,8 +1243,8 @@ xfs_dir2_leaf_log_tail(
 
        mp = tp->t_mountp;
        leaf = bp->data;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC);
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
        xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
                (uint)(mp->m_dirblksize - 1));
 }
@@ -1318,7 +1268,8 @@ xfs_dir2_leaf_lookup(
        xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
        xfs_trans_t             *tp;            /* transaction pointer */
 
-       xfs_dir2_trace_args("leaf_lookup", args);
+       trace_xfs_dir2_leaf_lookup(args);
+
        /*
         * Look up name in the leaf block, returning both buffers and index.
         */
@@ -1338,14 +1289,15 @@ xfs_dir2_leaf_lookup(
         */
        dep = (xfs_dir2_data_entry_t *)
              ((char *)dbp->data +
-              XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
+              xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
        /*
-        * Return the found inode number.
+        * Return the found inode number & CI name if appropriate
         */
-       args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
+       args->inumber = be64_to_cpu(dep->inumber);
+       error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
        xfs_da_brelse(tp, dbp);
        xfs_da_brelse(tp, lbp);
-       return XFS_ERROR(EEXIST);
+       return XFS_ERROR(error);
 }
 
 /*
@@ -1361,8 +1313,8 @@ xfs_dir2_leaf_lookup_int(
        int                     *indexp,        /* out: index in leaf block */
        xfs_dabuf_t             **dbpp)         /* out: data buffer */
 {
-       xfs_dir2_db_t           curdb;          /* current data block number */
-       xfs_dabuf_t             *dbp;           /* data buffer */
+       xfs_dir2_db_t           curdb = -1;     /* current data block number */
+       xfs_dabuf_t             *dbp = NULL;    /* data buffer */
        xfs_dir2_data_entry_t   *dep;           /* data entry */
        xfs_inode_t             *dp;            /* incore directory inode */
        int                     error;          /* error return code */
@@ -1373,6 +1325,8 @@ xfs_dir2_leaf_lookup_int(
        xfs_mount_t             *mp;            /* filesystem mount point */
        xfs_dir2_db_t           newdb;          /* new data block number */
        xfs_trans_t             *tp;            /* transaction pointer */
+       xfs_dir2_db_t           cidb = -1;      /* case match data block no. */
+       enum xfs_dacmp          cmp;            /* name compare result */
 
        dp = args->dp;
        tp = args->trans;
@@ -1380,11 +1334,10 @@ xfs_dir2_leaf_lookup_int(
        /*
         * Read the leaf block into the buffer.
         */
-       if ((error =
-           xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
-                   XFS_DATA_FORK))) {
+       error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
+                                                       XFS_DATA_FORK);
+       if (error)
                return error;
-       }
        *lbpp = lbp;
        leaf = lbp->data;
        xfs_dir2_leaf_check(dp, lbp);
@@ -1396,18 +1349,18 @@ xfs_dir2_leaf_lookup_int(
         * Loop over all the entries with the right hash value
         * looking to match the name.
         */
-       for (lep = &leaf->ents[index], dbp = NULL, curdb = -1;
-            index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
-            lep++, index++) {
+       for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
+                               be32_to_cpu(lep->hashval) == args->hashval;
+                               lep++, index++) {
                /*
                 * Skip over stale leaf entries.
                 */
-               if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
+               if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
                        continue;
                /*
                 * Get the new data block number.
                 */
-               newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
+               newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
                /*
                 * If it's not the same as the old data block number,
                 * need to pitch the old one and read the new one.
@@ -1415,10 +1368,10 @@ xfs_dir2_leaf_lookup_int(
                if (newdb != curdb) {
                        if (dbp)
                                xfs_da_brelse(tp, dbp);
-                       if ((error =
-                           xfs_da_read_buf(tp, dp,
-                                   XFS_DIR2_DB_TO_DA(mp, newdb), -1, &dbp,
-                                   XFS_DATA_FORK))) {
+                       error = xfs_da_read_buf(tp, dp,
+                                               xfs_dir2_db_to_da(mp, newdb),
+                                               -1, &dbp, XFS_DATA_FORK);
+                       if (error) {
                                xfs_da_brelse(tp, lbp);
                                return error;
                        }
@@ -1428,24 +1381,50 @@ xfs_dir2_leaf_lookup_int(
                /*
                 * Point to the data entry.
                 */
-               dep = (xfs_dir2_data_entry_t *)
-                     ((char *)dbp->data +
-                      XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
+               dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
+                       xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
                /*
-                * If it matches then return it.
+                * Compare name and if it's an exact match, return the index
+                * and buffer. If it's the first case-insensitive match, store
+                * the index and buffer and continue looking for an exact match.
                 */
-               if (dep->namelen == args->namelen &&
-                   dep->name[0] == args->name[0] &&
-                   memcmp(dep->name, args->name, args->namelen) == 0) {
-                       *dbpp = dbp;
+               cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
+               if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
+                       args->cmpresult = cmp;
                        *indexp = index;
-                       return 0;
+                       /* case exact match: return the current buffer. */
+                       if (cmp == XFS_CMP_EXACT) {
+                               *dbpp = dbp;
+                               return 0;
+                       }
+                       cidb = curdb;
+               }
+       }
+       ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
+       /*
+        * Here, we can only be doing a lookup (not a rename or remove).
+        * If a case-insensitive match was found earlier, re-read the
+        * appropriate data block if required and return it.
+        */
+       if (args->cmpresult == XFS_CMP_CASE) {
+               ASSERT(cidb != -1);
+               if (cidb != curdb) {
+                       xfs_da_brelse(tp, dbp);
+                       error = xfs_da_read_buf(tp, dp,
+                                               xfs_dir2_db_to_da(mp, cidb),
+                                               -1, &dbp, XFS_DATA_FORK);
+                       if (error) {
+                               xfs_da_brelse(tp, lbp);
+                               return error;
+                       }
                }
+               *dbpp = dbp;
+               return 0;
        }
        /*
         * No match found, return ENOENT.
         */
-       ASSERT(args->oknoent);
+       ASSERT(cidb == -1);
        if (dbp)
                xfs_da_brelse(tp, dbp);
        xfs_da_brelse(tp, lbp);
@@ -1459,7 +1438,7 @@ int                                               /* error */
 xfs_dir2_leaf_removename(
        xfs_da_args_t           *args)          /* operation arguments */
 {
-       xfs_dir2_data_off_t     *bestsp;        /* leaf block best freespace */
+       __be16                  *bestsp;        /* leaf block best freespace */
        xfs_dir2_data_t         *data;          /* data block structure */
        xfs_dir2_db_t           db;             /* data block number */
        xfs_dabuf_t             *dbp;           /* data block buffer */
@@ -1478,7 +1457,8 @@ xfs_dir2_leaf_removename(
        xfs_dir2_data_off_t     oldbest;        /* old value of best free */
        xfs_trans_t             *tp;            /* transaction pointer */
 
-       xfs_dir2_trace_args("leaf_removename", args);
+       trace_xfs_dir2_leaf_removename(args);
+
        /*
         * Lookup the leaf entry, get the leaf and data blocks read in.
         */
@@ -1495,48 +1475,48 @@ xfs_dir2_leaf_removename(
         * Point to the leaf entry, use that to point to the data entry.
         */
        lep = &leaf->ents[index];
-       db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
+       db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
        dep = (xfs_dir2_data_entry_t *)
-             ((char *)data + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
+             ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
        needscan = needlog = 0;
-       oldbest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
-       bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
-       ASSERT(INT_GET(bestsp[db], ARCH_CONVERT) == oldbest);
+       oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
+       bestsp = xfs_dir2_leaf_bests_p(ltp);
+       ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
        /*
         * Mark the former data entry unused.
         */
        xfs_dir2_data_make_free(tp, dbp,
                (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
-               XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
+               xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
        /*
         * We just mark the leaf entry stale by putting a null in it.
         */
-       INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
+       be16_add_cpu(&leaf->hdr.stale, 1);
        xfs_dir2_leaf_log_header(tp, lbp);
-       INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
+       lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
        xfs_dir2_leaf_log_ents(tp, lbp, index, index);
        /*
         * Scan the freespace in the data block again if necessary,
         * log the data block header if necessary.
         */
        if (needscan)
-               xfs_dir2_data_freescan(mp, data, &needlog, NULL);
+               xfs_dir2_data_freescan(mp, data, &needlog);
        if (needlog)
                xfs_dir2_data_log_header(tp, dbp);
        /*
         * If the longest freespace in the data block has changed,
         * put the new value in the bests table and log that.
         */
-       if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) != oldbest) {
-               INT_COPY(bestsp[db], data->hdr.bestfree[0].length, ARCH_CONVERT);
+       if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
+               bestsp[db] = data->hdr.bestfree[0].length;
                xfs_dir2_leaf_log_bests(tp, lbp, db, db);
        }
        xfs_dir2_data_check(dp, dbp);
        /*
         * If the data block is now empty then get rid of the data block.
         */
-       if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
+       if (be16_to_cpu(data->hdr.bestfree[0].length) ==
            mp->m_dirblksize - (uint)sizeof(data->hdr)) {
                ASSERT(db != mp->m_dirdatablk);
                if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
@@ -1559,12 +1539,12 @@ xfs_dir2_leaf_removename(
                 * If this is the last data block then compact the
                 * bests table by getting rid of entries.
                 */
-               if (db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1) {
+               if (db == be32_to_cpu(ltp->bestcount) - 1) {
                        /*
                         * Look for the last active entry (i).
                         */
                        for (i = db - 1; i > 0; i--) {
-                               if (INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF)
+                               if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
                                        break;
                        }
                        /*
@@ -1572,12 +1552,12 @@ xfs_dir2_leaf_removename(
                         * end are removed.
                         */
                        memmove(&bestsp[db - i], bestsp,
-                               (INT_GET(ltp->bestcount, ARCH_CONVERT) - (db - i)) * sizeof(*bestsp));
-                       INT_MOD(ltp->bestcount, ARCH_CONVERT, -(db - i));
+                               (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
+                       be32_add_cpu(&ltp->bestcount, -(db - i));
                        xfs_dir2_leaf_log_tail(tp, lbp);
-                       xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
+                       xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
                } else
-                       INT_SET(bestsp[db], ARCH_CONVERT, NULLDATAOFF);
+                       bestsp[db] = cpu_to_be16(NULLDATAOFF);
        }
        /*
         * If the data block was not the first one, drop it.
@@ -1610,7 +1590,8 @@ xfs_dir2_leaf_replace(
        xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
        xfs_trans_t             *tp;            /* transaction pointer */
 
-       xfs_dir2_trace_args("leaf_replace", args);
+       trace_xfs_dir2_leaf_replace(args);
+
        /*
         * Look up the entry.
         */
@@ -1628,12 +1609,12 @@ xfs_dir2_leaf_replace(
         */
        dep = (xfs_dir2_data_entry_t *)
              ((char *)dbp->data +
-              XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT)));
-       ASSERT(args->inumber != INT_GET(dep->inumber, ARCH_CONVERT));
+              xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
+       ASSERT(args->inumber != be64_to_cpu(dep->inumber));
        /*
         * Put the new inode number in, log it.
         */
-       INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
+       dep->inumber = cpu_to_be64(args->inumber);
        tp = args->trans;
        xfs_dir2_data_log_entry(tp, dbp, dep);
        xfs_da_buf_done(dbp);
@@ -1669,11 +1650,11 @@ xfs_dir2_leaf_search_hash(
         * Note, the table cannot be empty, so we have to go through the loop.
         * Binary search the leaf entries looking for our hash value.
         */
-       for (lep = leaf->ents, low = 0, high = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1,
+       for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
                hashwant = args->hashval;
             low <= high; ) {
                mid = (low + high) >> 1;
-               if ((hash = INT_GET(lep[mid].hashval, ARCH_CONVERT)) == hashwant)
+               if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
                        break;
                if (hash < hashwant)
                        low = mid + 1;
@@ -1684,7 +1665,7 @@ xfs_dir2_leaf_search_hash(
         * Found one, back up through all the equal hash values.
         */
        if (hash == hashwant) {
-               while (mid > 0 && INT_GET(lep[mid - 1].hashval, ARCH_CONVERT) == hashwant) {
+               while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
                        mid--;
                }
        }
@@ -1706,7 +1687,7 @@ xfs_dir2_leaf_trim_data(
        xfs_dabuf_t             *lbp,           /* leaf buffer */
        xfs_dir2_db_t           db)             /* data block number */
 {
-       xfs_dir2_data_off_t     *bestsp;        /* leaf bests table */
+       __be16                  *bestsp;        /* leaf bests table */
 #ifdef DEBUG
        xfs_dir2_data_t         *data;          /* data block structure */
 #endif
@@ -1724,13 +1705,13 @@ xfs_dir2_leaf_trim_data(
        /*
         * Read the offending data block.  We need its buffer.
         */
-       if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, db), -1, &dbp,
+       if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
                        XFS_DATA_FORK))) {
                return error;
        }
 #ifdef DEBUG
        data = dbp->data;
-       ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
+       ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
 #endif
        /* this seems to be an error
         * data is only valid if DEBUG is defined?
@@ -1738,10 +1719,10 @@ xfs_dir2_leaf_trim_data(
         */
 
        leaf = lbp->data;
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
-       ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) ==
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
+       ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
               mp->m_dirblksize - (uint)sizeof(data->hdr));
-       ASSERT(db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
+       ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
        /*
         * Get rid of the data block.
         */
@@ -1753,11 +1734,11 @@ xfs_dir2_leaf_trim_data(
        /*
         * Eliminate the last bests entry from the table.
         */
-       bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
-       INT_MOD(ltp->bestcount, ARCH_CONVERT, -1);
-       memmove(&bestsp[1], &bestsp[0], INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(*bestsp));
+       bestsp = xfs_dir2_leaf_bests_p(ltp);
+       be32_add_cpu(&ltp->bestcount, -1);
+       memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
        xfs_dir2_leaf_log_tail(tp, lbp);
-       xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
+       xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
        return 0;
 }
 
@@ -1790,7 +1771,9 @@ xfs_dir2_node_to_leaf(
        if (state->path.active > 1)
                return 0;
        args = state->args;
-       xfs_dir2_trace_args("node_to_leaf", args);
+
+       trace_xfs_dir2_node_to_leaf(args);
+
        mp = state->mp;
        dp = args->dp;
        tp = args->trans;
@@ -1829,7 +1812,7 @@ xfs_dir2_node_to_leaf(
                return 0;
        lbp = state->path.blk[0].bp;
        leaf = lbp->data;
-       ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
+       ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
        /*
         * Read the freespace block.
         */
@@ -1838,15 +1821,15 @@ xfs_dir2_node_to_leaf(
                return error;
        }
        free = fbp->data;
-       ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
+       ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
        ASSERT(!free->hdr.firstdb);
        /*
         * Now see if the leafn and free data will fit in a leaf1.
         * If not, release the buffer and give up.
         */
        if ((uint)sizeof(leaf->hdr) +
-           (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT)) * (uint)sizeof(leaf->ents[0]) +
-           INT_GET(free->hdr.nvalid, ARCH_CONVERT) * (uint)sizeof(leaf->bests[0]) +
+           (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
+           be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
            (uint)sizeof(leaf->tail) >
            mp->m_dirblksize) {
                xfs_da_brelse(tp, fbp);
@@ -1856,22 +1839,22 @@ xfs_dir2_node_to_leaf(
         * If the leaf has any stale entries in it, compress them out.
         * The compact routine will log the header.
         */
-       if (INT_GET(leaf->hdr.stale, ARCH_CONVERT))
+       if (be16_to_cpu(leaf->hdr.stale))
                xfs_dir2_leaf_compact(args, lbp);
        else
                xfs_dir2_leaf_log_header(tp, lbp);
-       INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAF1_MAGIC);
+       leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
        /*
         * Set up the leaf tail from the freespace block.
         */
-       ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
-       INT_COPY(ltp->bestcount, free->hdr.nvalid, ARCH_CONVERT);
+       ltp = xfs_dir2_leaf_tail_p(mp, leaf);
+       ltp->bestcount = free->hdr.nvalid;
        /*
         * Set up the leaf bests table.
         */
-       memcpy(XFS_DIR2_LEAF_BESTS_P(ltp), free->bests,
-               INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(leaf->bests[0]));
-       xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1);
+       memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
+               be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
+       xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
        xfs_dir2_leaf_log_tail(tp, lbp);
        xfs_dir2_leaf_check(dp, lbp);
        /*