[DLM] block dlm_recv in recovery transition
[safe/jmp/linux-2.6] / fs / xfs / xfs_bmap.c
index 890ad35..94b5c5f 100644 (file)
 #include "xfs_trans.h"
 #include "xfs_sb.h"
 #include "xfs_ag.h"
-#include "xfs_dir.h"
 #include "xfs_dir2.h"
 #include "xfs_da_btree.h"
 #include "xfs_bmap_btree.h"
 #include "xfs_alloc_btree.h"
 #include "xfs_ialloc_btree.h"
-#include "xfs_dir_sf.h"
 #include "xfs_dir2_sf.h"
 #include "xfs_attr_sf.h"
 #include "xfs_dinode.h"
 #include "xfs_mount.h"
 #include "xfs_ialloc.h"
 #include "xfs_itable.h"
+#include "xfs_dir2_data.h"
+#include "xfs_dir2_leaf.h"
+#include "xfs_dir2_block.h"
 #include "xfs_inode_item.h"
 #include "xfs_extfree_item.h"
 #include "xfs_alloc.h"
 #include "xfs_bmap.h"
 #include "xfs_rtalloc.h"
 #include "xfs_error.h"
-#include "xfs_dir_leaf.h"
 #include "xfs_attr_leaf.h"
 #include "xfs_rw.h"
 #include "xfs_quota.h"
 #include "xfs_trans_space.h"
 #include "xfs_buf_item.h"
+#include "xfs_filestream.h"
 
 
 #ifdef DEBUG
@@ -130,7 +131,6 @@ STATIC int                          /* error */
 xfs_bmap_add_extent_hole_delay(
        xfs_inode_t             *ip,    /* incore inode pointer */
        xfs_extnum_t            idx,    /* extent number to update/insert */
-       xfs_btree_cur_t         *cur,   /* if null, not a btree */
        xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
        int                     *logflagsp,/* inode logging flags */
        xfs_extdelta_t          *delta, /* Change made to incore extents */
@@ -185,16 +185,6 @@ xfs_bmap_btree_to_extents(
        int                     *logflagsp, /* inode logging flags */
        int                     whichfork); /* data or attr fork */
 
-#ifdef DEBUG
-/*
- * Check that the extents list for the inode ip is in the right order.
- */
-STATIC void
-xfs_bmap_check_extents(
-       xfs_inode_t             *ip,            /* incore inode pointer */
-       int                     whichfork);     /* data or attr fork */
-#endif
-
 /*
  * Called by xfs_bmapi to update file extent records and the btree
  * after removing space (or undoing a delayed allocation).
@@ -288,7 +278,7 @@ xfs_bmap_isaeof(
 STATIC void
 xfs_bmap_trace_addentry(
        int             opcode,         /* operation */
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(ies) */
@@ -302,7 +292,7 @@ xfs_bmap_trace_addentry(
  */
 STATIC void
 xfs_bmap_trace_delete(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(entries) deleted */
@@ -315,7 +305,7 @@ xfs_bmap_trace_delete(
  */
 STATIC void
 xfs_bmap_trace_insert(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(entries) inserted */
@@ -329,7 +319,7 @@ xfs_bmap_trace_insert(
  */
 STATIC void
 xfs_bmap_trace_post_update(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry updated */
@@ -340,17 +330,25 @@ xfs_bmap_trace_post_update(
  */
 STATIC void
 xfs_bmap_trace_pre_update(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry to be updated */
        int             whichfork);     /* data or attr fork */
 
+#define        XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)       \
+       xfs_bmap_trace_delete(__FUNCTION__,d,ip,i,c,w)
+#define        XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
+       xfs_bmap_trace_insert(__FUNCTION__,d,ip,i,c,r1,r2,w)
+#define        XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)    \
+       xfs_bmap_trace_post_update(__FUNCTION__,d,ip,i,w)
+#define        XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)     \
+       xfs_bmap_trace_pre_update(__FUNCTION__,d,ip,i,w)
 #else
-#define        xfs_bmap_trace_delete(f,d,ip,i,c,w)
-#define        xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
-#define        xfs_bmap_trace_post_update(f,d,ip,i,w)
-#define        xfs_bmap_trace_pre_update(f,d,ip,i,w)
+#define        XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
+#define        XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
+#define        XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
+#define        XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
 #endif /* XFS_BMAP_TRACE */
 
 /*
@@ -409,8 +407,6 @@ xfs_bmap_count_leaves(
 
 STATIC int
 xfs_bmap_disk_count_leaves(
-       xfs_ifork_t             *ifp,
-       xfs_mount_t             *mp,
        xfs_extnum_t            idx,
        xfs_bmbt_block_t        *block,
        int                     numrecs,
@@ -516,7 +512,7 @@ xfs_bmap_add_attrfork_local(
                dargs.total = mp->m_dirblkfsbs;
                dargs.whichfork = XFS_DATA_FORK;
                dargs.trans = tp;
-               error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
+               error = xfs_dir2_sf_to_block(&dargs);
        } else
                error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
                        XFS_DATA_FORK);
@@ -544,9 +540,6 @@ xfs_bmap_add_extent(
        xfs_filblks_t           da_new; /* new count del alloc blocks used */
        xfs_filblks_t           da_old; /* old count del alloc blocks used */
        int                     error;  /* error return value */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_add_extent";
-#endif
        xfs_ifork_t             *ifp;   /* inode fork ptr */
        int                     logflags; /* returned value */
        xfs_extnum_t            nextents; /* number of extents in file now */
@@ -564,8 +557,8 @@ xfs_bmap_add_extent(
         * already extents in the list.
         */
        if (nextents == 0) {
-               xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
-                       NULL, whichfork);
+               XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
+                       whichfork);
                xfs_iext_insert(ifp, 0, 1, new);
                ASSERT(cur == NULL);
                ifp->if_lastex = 0;
@@ -591,7 +584,7 @@ xfs_bmap_add_extent(
                if (cur)
                        ASSERT((cur->bc_private.b.flags &
                                XFS_BTCUR_BPRV_WASDEL) == 0);
-               if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
+               if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
                                &logflags, delta, rsvd)))
                        goto done;
        }
@@ -684,7 +677,7 @@ xfs_bmap_add_extent(
                ASSERT(nblks <= da_old);
                if (nblks < da_old)
                        xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
-                               (int)(da_old - nblks), rsvd);
+                               (int64_t)(da_old - nblks), rsvd);
        }
        /*
         * Clear out the allocated field, done with it now in any case.
@@ -723,9 +716,6 @@ xfs_bmap_add_extent_delay_real(
        int                     diff;   /* temp value */
        xfs_bmbt_rec_t          *ep;    /* extent entry for idx */
        int                     error;  /* error return value */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_add_extent_delay_real";
-#endif
        int                     i;      /* temp state */
        xfs_ifork_t             *ifp;   /* inode fork pointer */
        xfs_fileoff_t           new_endoff;     /* end offset of new entry */
@@ -821,15 +811,14 @@ xfs_bmap_add_extent_delay_real(
                 * Filling in all of a previously delayed allocation extent.
                 * The left and right neighbors are both contiguous with new.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + PREV.br_blockcount +
                        RIGHT.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
-                       XFS_DATA_FORK);
-               xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx, 2);
                ip->i_df.if_lastex = idx - 1;
                ip->i_d.di_nextents--;
@@ -868,15 +857,14 @@ xfs_bmap_add_extent_delay_real(
                 * Filling in all of a previously delayed allocation extent.
                 * The left neighbor is contiguous, the right is not.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + PREV.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx - 1;
-               xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx, 1);
                if (cur == NULL)
                        rval = XFS_ILOG_DEXT;
@@ -905,16 +893,13 @@ xfs_bmap_add_extent_delay_real(
                 * Filling in all of a previously delayed allocation extent.
                 * The right neighbor is contiguous, the left is not.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_startblock(ep, new->br_startblock);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount + RIGHT.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
                ip->i_df.if_lastex = idx;
-               xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx + 1, 1);
                if (cur == NULL)
                        rval = XFS_ILOG_DEXT;
@@ -944,11 +929,9 @@ xfs_bmap_add_extent_delay_real(
                 * Neither the left nor right neighbors are contiguous with
                 * the new one.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_startblock(ep, new->br_startblock);
-               xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
                ip->i_df.if_lastex = idx;
                ip->i_d.di_nextents++;
                if (cur == NULL)
@@ -976,17 +959,14 @@ xfs_bmap_add_extent_delay_real(
                 * Filling in the first part of a previous delayed allocation.
                 * The left neighbor is contiguous.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + new->br_blockcount);
                xfs_bmbt_set_startoff(ep,
                        PREV.br_startoff + new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
                temp = PREV.br_blockcount - new->br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep, temp);
                ip->i_df.if_lastex = idx - 1;
                if (cur == NULL)
@@ -1008,8 +988,7 @@ xfs_bmap_add_extent_delay_real(
                temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
                        STARTBLOCKVAL(PREV.br_startblock));
                xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-               xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
                *dnew = temp;
                /* DELTA: The boundary between two in-core extents moved. */
                temp = LEFT.br_startoff;
@@ -1022,11 +1001,11 @@ xfs_bmap_add_extent_delay_real(
                 * Filling in the first part of a previous delayed allocation.
                 * The left neighbor is not contiguous.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_startoff(ep, new_endoff);
                temp = PREV.br_blockcount - new->br_blockcount;
                xfs_bmbt_set_blockcount(ep, temp);
-               xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
+               XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
                        XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx, 1, new);
                ip->i_df.if_lastex = idx;
@@ -1059,8 +1038,7 @@ xfs_bmap_add_extent_delay_real(
                        (cur ? cur->bc_private.b.allocated : 0));
                ep = xfs_iext_get_ext(ifp, idx + 1);
                xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-               xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
                *dnew = temp;
                /* DELTA: One in-core extent is split in two. */
                temp = PREV.br_startoff;
@@ -1073,17 +1051,14 @@ xfs_bmap_add_extent_delay_real(
                 * The right neighbor is contiguous with the new allocation.
                 */
                temp = PREV.br_blockcount - new->br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
-                       XFS_DATA_FORK);
-               xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep, temp);
                xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
                        new->br_startoff, new->br_startblock,
                        new->br_blockcount + RIGHT.br_blockcount,
                        RIGHT.br_state);
-               xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
                ip->i_df.if_lastex = idx + 1;
                if (cur == NULL)
                        rval = XFS_ILOG_DEXT;
@@ -1104,8 +1079,7 @@ xfs_bmap_add_extent_delay_real(
                temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
                        STARTBLOCKVAL(PREV.br_startblock));
                xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-               xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
                *dnew = temp;
                /* DELTA: The boundary between two in-core extents moved. */
                temp = PREV.br_startoff;
@@ -1119,10 +1093,10 @@ xfs_bmap_add_extent_delay_real(
                 * The right neighbor is not contiguous.
                 */
                temp = PREV.br_blockcount - new->br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep, temp);
-               xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
-                       new, NULL, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
+                       XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx + 1, 1, new);
                ip->i_df.if_lastex = idx + 1;
                ip->i_d.di_nextents++;
@@ -1154,7 +1128,7 @@ xfs_bmap_add_extent_delay_real(
                        (cur ? cur->bc_private.b.allocated : 0));
                ep = xfs_iext_get_ext(ifp, idx);
                xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-               xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
                *dnew = temp;
                /* DELTA: One in-core extent is split in two. */
                temp = PREV.br_startoff;
@@ -1168,13 +1142,15 @@ xfs_bmap_add_extent_delay_real(
                 * This case is avoided almost all the time.
                 */
                temp = new->br_startoff - PREV.br_startoff;
-               xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep, temp);
                r[0] = *new;
+               r[1].br_state = PREV.br_state;
+               r[1].br_startblock = 0;
                r[1].br_startoff = new_endoff;
                temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
                r[1].br_blockcount = temp2;
-               xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
+               XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
                        XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
                ip->i_df.if_lastex = idx + 1;
@@ -1207,7 +1183,7 @@ xfs_bmap_add_extent_delay_real(
                diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
                        (cur ? cur->bc_private.b.allocated : 0));
                if (diff > 0 &&
-                   xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
+                   xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
                        /*
                         * Ick gross gag me with a spoon.
                         */
@@ -1218,7 +1194,7 @@ xfs_bmap_add_extent_delay_real(
                                        diff--;
                                        if (!diff ||
                                            !xfs_mod_incore_sb(ip->i_mount,
-                                                   XFS_SBS_FDBLOCKS, -diff, rsvd))
+                                                   XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
                                                break;
                                }
                                if (temp2) {
@@ -1226,20 +1202,18 @@ xfs_bmap_add_extent_delay_real(
                                        diff--;
                                        if (!diff ||
                                            !xfs_mod_incore_sb(ip->i_mount,
-                                                   XFS_SBS_FDBLOCKS, -diff, rsvd))
+                                                   XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
                                                break;
                                }
                        }
                }
                ep = xfs_iext_get_ext(ifp, idx);
                xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-               xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
-               xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
                xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
                        NULLSTARTBLOCK((int)temp2));
-               xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
                *dnew = temp + temp2;
                /* DELTA: One in-core extent is split in three. */
                temp = PREV.br_startoff;
@@ -1298,9 +1272,6 @@ xfs_bmap_add_extent_unwritten_real(
        xfs_btree_cur_t         *cur;   /* btree cursor */
        xfs_bmbt_rec_t          *ep;    /* extent entry for idx */
        int                     error;  /* error return value */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_add_extent_unwritten_real";
-#endif
        int                     i;      /* temp state */
        xfs_ifork_t             *ifp;   /* inode fork pointer */
        xfs_fileoff_t           new_endoff;     /* end offset of new entry */
@@ -1401,15 +1372,14 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting all of a previous oldext extent to newext.
                 * The left and right neighbors are both contiguous with new.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + PREV.br_blockcount +
                        RIGHT.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
-                       XFS_DATA_FORK);
-               xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx, 2);
                ip->i_df.if_lastex = idx - 1;
                ip->i_d.di_nextents -= 2;
@@ -1452,15 +1422,14 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting all of a previous oldext extent to newext.
                 * The left neighbor is contiguous, the right is not.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + PREV.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx - 1;
-               xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx, 1);
                ip->i_d.di_nextents--;
                if (cur == NULL)
@@ -1495,16 +1464,15 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting all of a previous oldext extent to newext.
                 * The right neighbor is contiguous, the left is not.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount + RIGHT.br_blockcount);
                xfs_bmbt_set_state(ep, newext);
-               xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx;
-               xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
-                       XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx + 1, 1);
                ip->i_d.di_nextents--;
                if (cur == NULL)
@@ -1540,10 +1508,10 @@ xfs_bmap_add_extent_unwritten_real(
                 * Neither the left nor right neighbors are contiguous with
                 * the new one.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
                        XFS_DATA_FORK);
                xfs_bmbt_set_state(ep, newext);
-               xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx;
                if (cur == NULL)
@@ -1570,21 +1538,21 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting the first part of a previous oldext extent to newext.
                 * The left neighbor is contiguous.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        LEFT.br_blockcount + new->br_blockcount);
                xfs_bmbt_set_startoff(ep,
                        PREV.br_startoff + new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
                        XFS_DATA_FORK);
-               xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
+               XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
                        XFS_DATA_FORK);
                xfs_bmbt_set_startblock(ep,
                        new->br_startblock + new->br_blockcount);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount - new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
+               XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx - 1;
                if (cur == NULL)
@@ -1621,15 +1589,15 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting the first part of a previous oldext extent to newext.
                 * The left neighbor is not contiguous.
                 */
-               xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
                ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
                xfs_bmbt_set_startoff(ep, new_endoff);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount - new->br_blockcount);
                xfs_bmbt_set_startblock(ep,
                        new->br_startblock + new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
-               xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
+               XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
                        XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx, 1, new);
                ip->i_df.if_lastex = idx;
@@ -1664,18 +1632,18 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting the last part of a previous oldext extent to newext.
                 * The right neighbor is contiguous with the new allocation.
                 */
-               xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
+               XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
                        XFS_DATA_FORK);
-               xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount - new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
+               XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
                        XFS_DATA_FORK);
                xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
                        new->br_startoff, new->br_startblock,
                        new->br_blockcount + RIGHT.br_blockcount, newext);
-               xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
+               XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx + 1;
                if (cur == NULL)
@@ -1711,12 +1679,12 @@ xfs_bmap_add_extent_unwritten_real(
                 * Setting the last part of a previous oldext extent to newext.
                 * The right neighbor is not contiguous.
                 */
-               xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep,
                        PREV.br_blockcount - new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
-               xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
-                       new, NULL, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
+                       XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx + 1, 1, new);
                ip->i_df.if_lastex = idx + 1;
                ip->i_d.di_nextents++;
@@ -1755,17 +1723,17 @@ xfs_bmap_add_extent_unwritten_real(
                 * newext.  Contiguity is impossible here.
                 * One extent becomes three extents.
                 */
-               xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(ep,
                        new->br_startoff - PREV.br_startoff);
-               xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
                r[0] = *new;
                r[1].br_startoff = new_endoff;
                r[1].br_blockcount =
                        PREV.br_startoff + PREV.br_blockcount - new_endoff;
                r[1].br_startblock = new->br_startblock + new->br_blockcount;
                r[1].br_state = oldext;
-               xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
+               XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
                        XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
                ip->i_df.if_lastex = idx + 1;
@@ -1850,16 +1818,12 @@ STATIC int                              /* error */
 xfs_bmap_add_extent_hole_delay(
        xfs_inode_t             *ip,    /* incore inode pointer */
        xfs_extnum_t            idx,    /* extent number to update/insert */
-       xfs_btree_cur_t         *cur,   /* if null, not a btree */
        xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
        int                     *logflagsp, /* inode logging flags */
        xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     rsvd)           /* OK to allocate reserved blocks */
 {
        xfs_bmbt_rec_t          *ep;    /* extent record for idx */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_add_extent_hole_delay";
-#endif
        xfs_ifork_t             *ifp;   /* inode fork pointer */
        xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
        xfs_filblks_t           newlen=0;       /* new indirect size */
@@ -1931,7 +1895,7 @@ xfs_bmap_add_extent_hole_delay(
                 */
                temp = left.br_blockcount + new->br_blockcount +
                        right.br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
                oldlen = STARTBLOCKVAL(left.br_startblock) +
@@ -1940,10 +1904,9 @@ xfs_bmap_add_extent_hole_delay(
                newlen = xfs_bmap_worst_indlen(ip, temp);
                xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
                        NULLSTARTBLOCK((int)newlen));
-               xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
-                       XFS_DATA_FORK);
-               xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
                        XFS_DATA_FORK);
+               XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
                xfs_iext_remove(ifp, idx, 1);
                ip->i_df.if_lastex = idx - 1;
                /* DELTA: Two in-core extents were replaced by one. */
@@ -1958,7 +1921,7 @@ xfs_bmap_add_extent_hole_delay(
                 * Merge the new allocation with the left neighbor.
                 */
                temp = left.br_blockcount + new->br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
                        XFS_DATA_FORK);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
                oldlen = STARTBLOCKVAL(left.br_startblock) +
@@ -1966,7 +1929,7 @@ xfs_bmap_add_extent_hole_delay(
                newlen = xfs_bmap_worst_indlen(ip, temp);
                xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
                        NULLSTARTBLOCK((int)newlen));
-               xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
                        XFS_DATA_FORK);
                ip->i_df.if_lastex = idx - 1;
                /* DELTA: One in-core extent grew into a hole. */
@@ -1980,14 +1943,14 @@ xfs_bmap_add_extent_hole_delay(
                 * on the right.
                 * Merge the new allocation with the right neighbor.
                 */
-               xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
                temp = new->br_blockcount + right.br_blockcount;
                oldlen = STARTBLOCKVAL(new->br_startblock) +
                        STARTBLOCKVAL(right.br_startblock);
                newlen = xfs_bmap_worst_indlen(ip, temp);
                xfs_bmbt_set_allf(ep, new->br_startoff,
                        NULLSTARTBLOCK((int)newlen), temp, right.br_state);
-               xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
+               XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
                ip->i_df.if_lastex = idx;
                /* DELTA: One in-core extent grew into a hole. */
                temp2 = temp;
@@ -2001,7 +1964,7 @@ xfs_bmap_add_extent_hole_delay(
                 * Insert a new entry.
                 */
                oldlen = newlen = 0;
-               xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
+               XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
                        XFS_DATA_FORK);
                xfs_iext_insert(ifp, idx, 1, new);
                ip->i_df.if_lastex = idx;
@@ -2013,7 +1976,7 @@ xfs_bmap_add_extent_hole_delay(
        if (oldlen != newlen) {
                ASSERT(oldlen > newlen);
                xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
-                       (int)(oldlen - newlen), rsvd);
+                       (int64_t)(oldlen - newlen), rsvd);
                /*
                 * Nothing to do for disk quota accounting here.
                 */
@@ -2051,9 +2014,6 @@ xfs_bmap_add_extent_hole_real(
 {
        xfs_bmbt_rec_t          *ep;    /* pointer to extent entry ins. point */
        int                     error;  /* error return value */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_add_extent_hole_real";
-#endif
        int                     i;      /* temp state */
        xfs_ifork_t             *ifp;   /* inode fork pointer */
        xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
@@ -2130,15 +2090,14 @@ xfs_bmap_add_extent_hole_real(
                 * left and on the right.
                 * Merge all three into a single extent record.
                 */
-               xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
+               XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
                        whichfork);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        left.br_blockcount + new->br_blockcount +
                        right.br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
+               XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
                        whichfork);
-               xfs_bmap_trace_delete(fname, "LC|RC", ip,
-                       idx, 1, whichfork);
+               XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
                xfs_iext_remove(ifp, idx, 1);
                ifp->if_lastex = idx - 1;
                XFS_IFORK_NEXT_SET(ip, whichfork,
@@ -2180,10 +2139,10 @@ xfs_bmap_add_extent_hole_real(
                 * on the left.
                 * Merge the new allocation with the left neighbor.
                 */
-               xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
+               XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
                xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
                        left.br_blockcount + new->br_blockcount);
-               xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
+               XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
                ifp->if_lastex = idx - 1;
                if (cur == NULL) {
                        rval = XFS_ILOG_FEXT(whichfork);
@@ -2214,11 +2173,11 @@ xfs_bmap_add_extent_hole_real(
                 * on the right.
                 * Merge the new allocation with the right neighbor.
                 */
-               xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
+               XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
                xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
                        new->br_blockcount + right.br_blockcount,
                        right.br_state);
-               xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
+               XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
                ifp->if_lastex = idx;
                if (cur == NULL) {
                        rval = XFS_ILOG_FEXT(whichfork);
@@ -2249,8 +2208,7 @@ xfs_bmap_add_extent_hole_real(
                 * real allocation.
                 * Insert a new entry.
                 */
-               xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
-                       whichfork);
+               XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
                xfs_iext_insert(ifp, idx, 1, new);
                ifp->if_lastex = idx;
                XFS_IFORK_NEXT_SET(ip, whichfork,
@@ -2617,12 +2575,10 @@ xfs_bmap_rtalloc(
        xfs_extlen_t    prod = 0;       /* product factor for allocators */
        xfs_extlen_t    ralen = 0;      /* realtime allocation length */
        xfs_extlen_t    align;          /* minimum allocation alignment */
-       xfs_rtblock_t   rtx;            /* realtime extent number */
        xfs_rtblock_t   rtb;
 
        mp = ap->ip->i_mount;
-       align = ap->ip->i_d.di_extsize ?
-               ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
+       align = xfs_get_extsz_hint(ap->ip);
        prod = align / mp->m_sb.sb_rextsize;
        error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
                                        align, 1, ap->eof, 0,
@@ -2656,6 +2612,8 @@ xfs_bmap_rtalloc(
         * pick an extent that will space things out in the rt area.
         */
        if (ap->eof && ap->off == 0) {
+               xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
+
                error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
                if (error)
                        return error;
@@ -2727,9 +2685,7 @@ xfs_bmap_btalloc(
        int             error;
 
        mp = ap->ip->i_mount;
-       align = (ap->userdata && ap->ip->i_d.di_extsize &&
-               (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
-               ap->ip->i_d.di_extsize : 0;
+       align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
        if (unlikely(align)) {
                error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
                                                align, 0, ap->eof, 0, ap->conv,
@@ -2739,9 +2695,15 @@ xfs_bmap_btalloc(
        }
        nullfb = ap->firstblock == NULLFSBLOCK;
        fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
-       if (nullfb)
-               ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
-       else
+       if (nullfb) {
+               if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
+                       ag = xfs_filestream_lookup_ag(ap->ip);
+                       ag = (ag != NULLAGNUMBER) ? ag : 0;
+                       ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
+               } else {
+                       ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
+               }
+       } else
                ap->rval = ap->firstblock;
 
        xfs_bmap_adjacent(ap);
@@ -2762,15 +2724,25 @@ xfs_bmap_btalloc(
        args.mp = mp;
        args.fsbno = ap->rval;
        args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
+       args.firstblock = ap->firstblock;
        blen = 0;
        if (nullfb) {
-               args.type = XFS_ALLOCTYPE_START_BNO;
+               if (ap->userdata && xfs_inode_is_filestream(ap->ip))
+                       args.type = XFS_ALLOCTYPE_NEAR_BNO;
+               else
+                       args.type = XFS_ALLOCTYPE_START_BNO;
                args.total = ap->total;
+
                /*
-                * Find the longest available space.
-                * We're going to try for the whole allocation at once.
+                * Search for an allocation group with a single extent
+                * large enough for the request.
+                *
+                * If one isn't found, then adjust the minimum allocation
+                * size to the largest space found.
                 */
                startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
+               if (startag == NULLAGNUMBER)
+                       startag = ag = 0;
                notinit = 0;
                down_read(&mp->m_peraglock);
                while (blen < ap->alen) {
@@ -2796,6 +2768,35 @@ xfs_bmap_btalloc(
                                        blen = longest;
                        } else
                                notinit = 1;
+
+                       if (xfs_inode_is_filestream(ap->ip)) {
+                               if (blen >= ap->alen)
+                                       break;
+
+                               if (ap->userdata) {
+                                       /*
+                                        * If startag is an invalid AG, we've
+                                        * come here once before and
+                                        * xfs_filestream_new_ag picked the
+                                        * best currently available.
+                                        *
+                                        * Don't continue looping, since we
+                                        * could loop forever.
+                                        */
+                                       if (startag == NULLAGNUMBER)
+                                               break;
+
+                                       error = xfs_filestream_new_ag(ap, &ag);
+                                       if (error) {
+                                               up_read(&mp->m_peraglock);
+                                               return error;
+                                       }
+
+                                       /* loop again to set 'blen'*/
+                                       startag = NULLAGNUMBER;
+                                       continue;
+                               }
+                       }
                        if (++ag == mp->m_sb.sb_agcount)
                                ag = 0;
                        if (ag == startag)
@@ -2820,20 +2821,30 @@ xfs_bmap_btalloc(
                 */
                else
                        args.minlen = ap->alen;
+
+               /*
+                * set the failure fallback case to look in the selected
+                * AG as the stream may have moved.
+                */
+               if (xfs_inode_is_filestream(ap->ip))
+                       ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
        } else if (ap->low) {
-               args.type = XFS_ALLOCTYPE_FIRST_AG;
+               if (xfs_inode_is_filestream(ap->ip))
+                       args.type = XFS_ALLOCTYPE_FIRST_AG;
+               else
+                       args.type = XFS_ALLOCTYPE_START_BNO;
                args.total = args.minlen = ap->minlen;
        } else {
                args.type = XFS_ALLOCTYPE_NEAR_BNO;
                args.total = ap->total;
                args.minlen = ap->minlen;
        }
-       if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
-                   (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
-               args.prod = ap->ip->i_d.di_extsize;
+       /* apply extent size hints if obtained earlier */
+       if (unlikely(align)) {
+               args.prod = align;
                if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
                        args.mod = (xfs_extlen_t)(args.prod - args.mod);
-       } else if (unlikely(mp->m_sb.sb_blocksize >= NBPP)) {
+       } else if (mp->m_sb.sb_blocksize >= NBPP) {
                args.prod = 1;
                args.mod = 0;
        } else {
@@ -2998,7 +3009,7 @@ xfs_bmap_btree_to_extents(
        int                     error;  /* error return value */
        xfs_ifork_t             *ifp;   /* inode fork data */
        xfs_mount_t             *mp;    /* mount point structure */
-       xfs_bmbt_ptr_t          *pp;    /* ptr to block address */
+       __be64                  *pp;    /* ptr to block address */
        xfs_bmbt_block_t        *rblock;/* root btree block */
 
        ifp = XFS_IFORK_PTR(ip, whichfork);
@@ -3010,12 +3021,12 @@ xfs_bmap_btree_to_extents(
        ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
        mp = ip->i_mount;
        pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
+       cbno = be64_to_cpu(*pp);
        *logflagsp = 0;
 #ifdef DEBUG
-       if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
+       if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
                return error;
 #endif
-       cbno = INT_GET(*pp, ARCH_CONVERT);
        if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
                        XFS_BMAP_BTREE_REF)))
                return error;
@@ -3062,9 +3073,6 @@ xfs_bmap_del_extent(
        xfs_bmbt_rec_t          *ep;    /* current extent entry pointer */
        int                     error;  /* error return value */
        int                     flags;  /* inode logging flags */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_del_extent";
-#endif
        xfs_bmbt_irec_t         got;    /* current extent entry */
        xfs_fileoff_t           got_endoff;     /* first offset past got */
        int                     i;      /* temp state */
@@ -3158,7 +3166,7 @@ xfs_bmap_del_extent(
                /*
                 * Matches the whole extent.  Delete the entry.
                 */
-               xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
+               XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
                xfs_iext_remove(ifp, idx, 1);
                ifp->if_lastex = idx;
                if (delay)
@@ -3179,7 +3187,7 @@ xfs_bmap_del_extent(
                /*
                 * Deleting the first part of the extent.
                 */
-               xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
+               XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
                xfs_bmbt_set_startoff(ep, del_endoff);
                temp = got.br_blockcount - del->br_blockcount;
                xfs_bmbt_set_blockcount(ep, temp);
@@ -3188,13 +3196,13 @@ xfs_bmap_del_extent(
                        temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
                                da_old);
                        xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-                       xfs_bmap_trace_post_update(fname, "2", ip, idx,
+                       XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
                                whichfork);
                        da_new = temp;
                        break;
                }
                xfs_bmbt_set_startblock(ep, del_endblock);
-               xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
+               XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
                if (!cur) {
                        flags |= XFS_ILOG_FEXT(whichfork);
                        break;
@@ -3210,19 +3218,19 @@ xfs_bmap_del_extent(
                 * Deleting the last part of the extent.
                 */
                temp = got.br_blockcount - del->br_blockcount;
-               xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
+               XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
                xfs_bmbt_set_blockcount(ep, temp);
                ifp->if_lastex = idx;
                if (delay) {
                        temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
                                da_old);
                        xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
-                       xfs_bmap_trace_post_update(fname, "1", ip, idx,
+                       XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
                                whichfork);
                        da_new = temp;
                        break;
                }
-               xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
+               XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
                if (!cur) {
                        flags |= XFS_ILOG_FEXT(whichfork);
                        break;
@@ -3239,7 +3247,7 @@ xfs_bmap_del_extent(
                 * Deleting the middle of the extent.
                 */
                temp = del->br_startoff - got.br_startoff;
-               xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
+               XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
                xfs_bmbt_set_blockcount(ep, temp);
                new.br_startoff = del_endoff;
                temp2 = got_endoff - del_endoff;
@@ -3326,8 +3334,8 @@ xfs_bmap_del_extent(
                                }
                        }
                }
-               xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
-               xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
+               XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
+               XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
                        whichfork);
                xfs_iext_insert(ifp, idx + 1, 1, &new);
                ifp->if_lastex = idx + 1;
@@ -3356,7 +3364,7 @@ xfs_bmap_del_extent(
         */
        ASSERT(da_old >= da_new);
        if (da_old > da_new)
-               xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
+               xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
                        rsvd);
        if (delta) {
                /* DELTA: report the original extent. */
@@ -3452,6 +3460,7 @@ xfs_bmap_extents_to_btree(
        XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
        args.tp = tp;
        args.mp = mp;
+       args.firstblock = *firstblock;
        if (*firstblock == NULLFSBLOCK) {
                args.type = XFS_ALLOCTYPE_START_BNO;
                args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
@@ -3510,9 +3519,9 @@ xfs_bmap_extents_to_btree(
         */
        kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
        arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
-       INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
+       kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
        pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
-       INT_SET(*pp, ARCH_CONVERT, args.fsbno);
+       *pp = cpu_to_be64(args.fsbno);
        /*
         * Do all this logging at the end so that
         * the root is at the right level.
@@ -3539,6 +3548,7 @@ xfs_bmap_forkoff_reset(
        if (whichfork == XFS_ATTR_FORK &&
            (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
            (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
+           (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
            ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
                ip->i_d.di_forkoff = mp->m_attroffset >> 3;
                ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
@@ -3565,9 +3575,6 @@ xfs_bmap_local_to_extents(
 {
        int             error;          /* error return value */
        int             flags;          /* logging flags returned */
-#ifdef XFS_BMAP_TRACE
-       static char     fname[] = "xfs_bmap_local_to_extents";
-#endif
        xfs_ifork_t     *ifp;           /* inode fork pointer */
 
        /*
@@ -3587,6 +3594,7 @@ xfs_bmap_local_to_extents(
 
                args.tp = tp;
                args.mp = ip->i_mount;
+               args.firstblock = *firstblock;
                ASSERT((ifp->if_flags &
                        (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
                /*
@@ -3621,7 +3629,7 @@ xfs_bmap_local_to_extents(
                xfs_iext_add(ifp, 0, 1);
                ep = xfs_iext_get_ext(ifp, 0);
                xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
-               xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
+               XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
                XFS_IFORK_NEXT_SET(ip, whichfork, 1);
                ip->i_d.di_nblocks = 1;
                XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
@@ -3702,7 +3710,7 @@ STATIC xfs_bmbt_rec_t *                 /* pointer to found extent entry */
 xfs_bmap_search_extents(
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_fileoff_t   bno,            /* block number searched for */
-       int             whichfork,      /* data or attr fork */
+       int             fork,           /* data or attr fork */
        int             *eofp,          /* out: end of file found */
        xfs_extnum_t    *lastxp,        /* out: last extent index */
        xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
@@ -3710,25 +3718,28 @@ xfs_bmap_search_extents(
 {
        xfs_ifork_t     *ifp;           /* inode fork pointer */
        xfs_bmbt_rec_t  *ep;            /* extent record pointer */
-       int             rt;             /* realtime flag    */
 
        XFS_STATS_INC(xs_look_exlist);
-       ifp = XFS_IFORK_PTR(ip, whichfork);
+       ifp = XFS_IFORK_PTR(ip, fork);
 
        ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
 
-       rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
-       if (unlikely(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM))) {
-                cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
-                       "start_block : %llx start_off : %llx blkcnt : %llx "
-                       "extent-state : %x \n",
-                       (ip->i_mount)->m_fsname, (long long)ip->i_ino,
+       if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
+                    !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
+               xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
+                               "Access to block zero in inode %llu "
+                               "start_block: %llx start_off: %llx "
+                               "blkcnt: %llx extent-state: %x lastx: %x\n",
+                       (unsigned long long)ip->i_ino,
                        (unsigned long long)gotp->br_startblock,
                        (unsigned long long)gotp->br_startoff,
                        (unsigned long long)gotp->br_blockcount,
-                       gotp->br_state);
-        }
-        return ep;
+                       gotp->br_state, *lastxp);
+               *lastxp = NULLEXTNUM;
+               *eofp = 1;
+               return NULL;
+       }
+       return ep;
 }
 
 
@@ -3741,7 +3752,7 @@ ktrace_t  *xfs_bmap_trace_buf;
 STATIC void
 xfs_bmap_trace_addentry(
        int             opcode,         /* operation */
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(ies) */
@@ -3800,7 +3811,7 @@ xfs_bmap_trace_addentry(
  */
 STATIC void
 xfs_bmap_trace_delete(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(entries) deleted */
@@ -3822,7 +3833,7 @@ xfs_bmap_trace_delete(
  */
 STATIC void
 xfs_bmap_trace_insert(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry(entries) inserted */
@@ -3851,7 +3862,7 @@ xfs_bmap_trace_insert(
  */
 STATIC void
 xfs_bmap_trace_post_update(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry updated */
@@ -3869,7 +3880,7 @@ xfs_bmap_trace_post_update(
  */
 STATIC void
 xfs_bmap_trace_pre_update(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        char            *desc,          /* operation description */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    idx,            /* index of entry to be updated */
@@ -4071,9 +4082,9 @@ xfs_bmap_add_attrfork(
                } else
                        XFS_SB_UNLOCK(mp, s);
        }
-       if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
+       if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
                goto error2;
-       error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
+       error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
        ASSERT(ip->i_df.if_ext_max ==
               XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
        return error;
@@ -4204,7 +4215,6 @@ int                                               /* error */
 xfs_bmap_finish(
        xfs_trans_t             **tp,           /* transaction pointer addr */
        xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
-       xfs_fsblock_t           firstblock,     /* controlled ag for allocs */
        int                     *committed)     /* xact committed or not */
 {
        xfs_efd_log_item_t      *efd;           /* extent free data */
@@ -4230,7 +4240,7 @@ xfs_bmap_finish(
        logres = ntp->t_log_res;
        logcount = ntp->t_log_count;
        ntp = xfs_trans_dup(*tp);
-       error = xfs_trans_commit(*tp, 0, NULL);
+       error = xfs_trans_commit(*tp, 0);
        *tp = ntp;
        *committed = 1;
        /*
@@ -4260,8 +4270,8 @@ xfs_bmap_finish(
                        if (!XFS_FORCED_SHUTDOWN(mp))
                                xfs_force_shutdown(mp,
                                                   (error == EFSCORRUPTED) ?
-                                                  XFS_CORRUPT_INCORE :
-                                                  XFS_METADATA_IO_ERROR);
+                                                  SHUTDOWN_CORRUPT_INCORE :
+                                                  SHUTDOWN_META_IO_ERROR);
                        return error;
                }
                xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
@@ -4450,8 +4460,11 @@ xfs_bmap_one_block(
        xfs_bmbt_irec_t s;              /* internal version of extent */
 
 #ifndef DEBUG
-       if (whichfork == XFS_DATA_FORK)
-               return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
+       if (whichfork == XFS_DATA_FORK) {
+               return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
+                       (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
+                       (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
+       }
 #endif /* !DEBUG */
        if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
                return 0;
@@ -4463,7 +4476,7 @@ xfs_bmap_one_block(
        xfs_bmbt_get_all(ep, &s);
        rval = s.br_startoff == 0 && s.br_blockcount == 1;
        if (rval && whichfork == XFS_DATA_FORK)
-               ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
+               ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
        return rval;
 }
 
@@ -4484,14 +4497,11 @@ xfs_bmap_read_extents(
        xfs_buf_t               *bp;    /* buffer for "block" */
        int                     error;  /* error return value */
        xfs_exntfmt_t           exntf;  /* XFS_EXTFMT_NOSTATE, if checking */
-#ifdef XFS_BMAP_TRACE
-       static char             fname[] = "xfs_bmap_read_extents";
-#endif
        xfs_extnum_t            i, j;   /* index into the extents list */
        xfs_ifork_t             *ifp;   /* fork structure */
        int                     level;  /* btree level, for checking */
        xfs_mount_t             *mp;    /* file system mount structure */
-       xfs_bmbt_ptr_t          *pp;    /* pointer to block address */
+       __be64                  *pp;    /* pointer to block address */
        /* REFERENCED */
        xfs_extnum_t            room;   /* number of entries there's room for */
 
@@ -4507,10 +4517,10 @@ xfs_bmap_read_extents(
        level = be16_to_cpu(block->bb_level);
        ASSERT(level > 0);
        pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
-       ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
-       ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
-       ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
-       bno = INT_GET(*pp, ARCH_CONVERT);
+       bno = be64_to_cpu(*pp);
+       ASSERT(bno != NULLDFSBNO);
+       ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
+       ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
        /*
         * Go down the tree until leaf level is reached, following the first
         * pointer (leftmost) at each level.
@@ -4525,12 +4535,9 @@ xfs_bmap_read_extents(
                        error0);
                if (level == 0)
                        break;
-               pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
-                       1, mp->m_bmap_dmxr[1]);
-               XFS_WANT_CORRUPTED_GOTO(
-                       XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
-                       error0);
-               bno = INT_GET(*pp, ARCH_CONVERT);
+               pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
+               bno = be64_to_cpu(*pp);
+               XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
                xfs_trans_brelse(tp, bp);
        }
        /*
@@ -4571,8 +4578,7 @@ xfs_bmap_read_extents(
                /*
                 * Copy records into the extent records.
                 */
-               frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
-                       block, 1, mp->m_bmap_dmxr[0]);
+               frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
                start = i;
                for (j = 0; j < num_recs; j++, i++, frp++) {
                        trp = xfs_iext_get_ext(ifp, i);
@@ -4607,7 +4613,7 @@ xfs_bmap_read_extents(
        }
        ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
        ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
-       xfs_bmap_trace_exlist(fname, ip, i, whichfork);
+       XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
        return 0;
 error0:
        xfs_trans_brelse(tp, bp);
@@ -4620,7 +4626,7 @@ error0:
  */
 void
 xfs_bmap_trace_exlist(
-       char            *fname,         /* function name */
+       const char      *fname,         /* function name */
        xfs_inode_t     *ip,            /* incore inode pointer */
        xfs_extnum_t    cnt,            /* count of entries in the list */
        int             whichfork)      /* data or attr fork */
@@ -4635,7 +4641,7 @@ xfs_bmap_trace_exlist(
        for (idx = 0; idx < cnt; idx++) {
                ep = xfs_iext_get_ext(ifp, idx);
                xfs_bmbt_get_all(ep, &s);
-               xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
+               XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
                        whichfork);
        }
 }
@@ -4875,12 +4881,7 @@ xfs_bmapi(
                                xfs_extlen_t    extsz;
 
                                /* Figure out the extent size, adjust alen */
-                               if (rt) {
-                                       if (!(extsz = ip->i_d.di_extsize))
-                                               extsz = mp->m_sb.sb_rextsize;
-                               } else {
-                                       extsz = ip->i_d.di_extsize;
-                               }
+                               extsz = xfs_get_extsz_hint(ip);
                                if (extsz) {
                                        error = xfs_bmap_extsize_align(mp,
                                                        &got, &prev, extsz,
@@ -4923,28 +4924,28 @@ xfs_bmapi(
                                if (rt) {
                                        error = xfs_mod_incore_sb(mp,
                                                        XFS_SBS_FREXTENTS,
-                                                       -(extsz), (flags &
+                                                       -((int64_t)extsz), (flags &
                                                        XFS_BMAPI_RSVBLOCKS));
                                } else {
                                        error = xfs_mod_incore_sb(mp,
                                                        XFS_SBS_FDBLOCKS,
-                                                       -(alen), (flags &
+                                                       -((int64_t)alen), (flags &
                                                        XFS_BMAPI_RSVBLOCKS));
                                }
                                if (!error) {
                                        error = xfs_mod_incore_sb(mp,
                                                        XFS_SBS_FDBLOCKS,
-                                                       -(indlen), (flags &
+                                                       -((int64_t)indlen), (flags &
                                                        XFS_BMAPI_RSVBLOCKS));
                                        if (error && rt)
                                                xfs_mod_incore_sb(mp,
                                                        XFS_SBS_FREXTENTS,
-                                                       extsz, (flags &
+                                                       (int64_t)extsz, (flags &
                                                        XFS_BMAPI_RSVBLOCKS));
                                        else if (error)
                                                xfs_mod_incore_sb(mp,
                                                        XFS_SBS_FDBLOCKS,
-                                                       alen, (flags &
+                                                       (int64_t)alen, (flags &
                                                        XFS_BMAPI_RSVBLOCKS));
                                }
 
@@ -4990,7 +4991,7 @@ xfs_bmapi(
                                bma.firstblock = *firstblock;
                                bma.alen = alen;
                                bma.off = aoff;
-                               bma.conv = (flags & XFS_BMAPI_CONVERT);
+                               bma.conv = !!(flags & XFS_BMAPI_CONVERT);
                                bma.wasdel = wasdelay;
                                bma.minlen = minlen;
                                bma.low = flist->xbf_low;
@@ -5226,10 +5227,10 @@ xfs_bmapi(
                 * Else go on to the next record.
                 */
                ep = xfs_iext_get_ext(ifp, ++lastx);
-               if (lastx >= nextents) {
+               prev = got;
+               if (lastx >= nextents)
                        eof = 1;
-                       prev = got;
-               } else
+               else
                        xfs_bmbt_get_all(ep, &got);
        }
        ifp->if_lastex = lastx;
@@ -5610,13 +5611,13 @@ xfs_bunmapi(
                                rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
                                do_div(rtexts, mp->m_sb.sb_rextsize);
                                xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
-                                               (int)rtexts, rsvd);
+                                               (int64_t)rtexts, rsvd);
                                (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
                                        NULL, ip, -((long)del.br_blockcount), 0,
                                        XFS_QMOPT_RES_RTBLKS);
                        } else {
                                xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
-                                               (int)del.br_blockcount, rsvd);
+                                               (int64_t)del.br_blockcount, rsvd);
                                (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
                                        NULL, ip, -((long)del.br_blockcount), 0,
                                        XFS_QMOPT_RES_REGBLKS);
@@ -5756,7 +5757,7 @@ xfs_getbmap(
        __int64_t               fixlen;         /* length for -1 case */
        int                     i;              /* extent number */
        xfs_inode_t             *ip;            /* xfs incore inode pointer */
-       vnode_t                 *vp;            /* corresponding vnode */
+       bhv_vnode_t             *vp;            /* corresponding vnode */
        int                     lock;           /* lock state */
        xfs_bmbt_irec_t         *map;           /* buffer for user's data */
        xfs_mount_t             *mp;            /* file system mount point */
@@ -5820,14 +5821,13 @@ xfs_getbmap(
                   ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
                return XFS_ERROR(EINVAL);
        if (whichfork == XFS_DATA_FORK) {
-               if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
-                               (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
+               if (xfs_get_extsz_hint(ip) ||
                    ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
                        prealloced = 1;
                        fixlen = XFS_MAXIOFFSET(mp);
                } else {
                        prealloced = 0;
-                       fixlen = ip->i_d.di_size;
+                       fixlen = ip->i_size;
                }
        } else {
                prealloced = 0;
@@ -5851,9 +5851,10 @@ xfs_getbmap(
 
        xfs_ilock(ip, XFS_IOLOCK_SHARED);
 
-       if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
+       if (whichfork == XFS_DATA_FORK &&
+               (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
                /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
-               VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
+               error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
        }
 
        ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
@@ -6042,32 +6043,6 @@ xfs_bmap_eof(
 }
 
 #ifdef DEBUG
-/*
- * Check that the extents list for the inode ip is in the right order.
- */
-STATIC void
-xfs_bmap_check_extents(
-       xfs_inode_t             *ip,            /* incore inode pointer */
-       int                     whichfork)      /* data or attr fork */
-{
-       xfs_bmbt_rec_t          *ep;            /* current extent entry */
-       xfs_extnum_t            idx;            /* extent record index */
-       xfs_ifork_t             *ifp;           /* inode fork pointer */
-       xfs_extnum_t            nextents;       /* number of extents in list */
-       xfs_bmbt_rec_t          *nextp;         /* next extent entry */
-
-       ifp = XFS_IFORK_PTR(ip, whichfork);
-       ASSERT(ifp->if_flags & XFS_IFEXTENTS);
-       nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
-       ep = xfs_iext_get_ext(ifp, 0);
-       for (idx = 0; idx < nextents - 1; idx++) {
-               nextp = xfs_iext_get_ext(ifp, idx + 1);
-               xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
-                       (void *)(nextp));
-               ep = nextp;
-       }
-}
-
 STATIC
 xfs_buf_t *
 xfs_bmap_get_bp(
@@ -6138,7 +6113,7 @@ xfs_check_block(
        short                   sz)
 {
        int                     i, j, dmxr;
-       xfs_bmbt_ptr_t          *pp, *thispa;   /* pointer to block address */
+       __be64                  *pp, *thispa;   /* pointer to block address */
        xfs_bmbt_key_t          *prevp, *keyp;
 
        ASSERT(be16_to_cpu(block->bb_level) > 0);
@@ -6150,8 +6125,7 @@ xfs_check_block(
                if (root) {
                        keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
                } else {
-                       keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
-                               xfs_bmbt, block, i, dmxr);
+                       keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
                }
 
                if (prevp) {
@@ -6166,21 +6140,19 @@ xfs_check_block(
                if (root) {
                        pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
                } else {
-                       pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
-                               xfs_bmbt, block, i, dmxr);
+                       pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
                }
                for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
                        if (root) {
                                thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
                        } else {
-                               thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
-                                       xfs_bmbt, block, j, dmxr);
+                               thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
+                                                           dmxr);
                        }
-                       if (INT_GET(*thispa, ARCH_CONVERT) ==
-                           INT_GET(*pp, ARCH_CONVERT)) {
+                       if (*thispa == *pp) {
                                cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
                                        __FUNCTION__, j, i,
-                                       INT_GET(*thispa, ARCH_CONVERT));
+                                       (unsigned long long)be64_to_cpu(*thispa));
                                panic("%s: ptrs are equal in node\n",
                                        __FUNCTION__);
                        }
@@ -6207,7 +6179,7 @@ xfs_bmap_check_leaf_extents(
        xfs_ifork_t             *ifp;   /* fork structure */
        int                     level;  /* btree level, for checking */
        xfs_mount_t             *mp;    /* file system mount structure */
-       xfs_bmbt_ptr_t          *pp;    /* pointer to block address */
+       __be64                  *pp;    /* pointer to block address */
        xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
        xfs_bmbt_rec_t          *lastp; /* pointer to previous extent */
        xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
@@ -6228,10 +6200,12 @@ xfs_bmap_check_leaf_extents(
        ASSERT(level > 0);
        xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
        pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
-       ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
-       ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
-       ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
-       bno = INT_GET(*pp, ARCH_CONVERT);
+       bno = be64_to_cpu(*pp);
+
+       ASSERT(bno != NULLDFSBNO);
+       ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
+       ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
+
        /*
         * Go down the tree until leaf level is reached, following the first
         * pointer (leftmost) at each level.
@@ -6260,10 +6234,9 @@ xfs_bmap_check_leaf_extents(
                 */
 
                xfs_check_block(block, mp, 0, 0);
-               pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
-                       1, mp->m_bmap_dmxr[1]);
-               XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
-               bno = INT_GET(*pp, ARCH_CONVERT);
+               pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
+               bno = be64_to_cpu(*pp);
+               XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
                if (bp_release) {
                        bp_release = 0;
                        xfs_trans_brelse(NULL, bp);
@@ -6298,11 +6271,9 @@ xfs_bmap_check_leaf_extents(
                 * conform with the first entry in this one.
                 */
 
-               ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
-                       block, 1, mp->m_bmap_dmxr[0]);
+               ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
                for (j = 1; j < num_recs; j++) {
-                       nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
-                               block, j + 1, mp->m_bmap_dmxr[0]);
+                       nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
                        if (lastp) {
                                xfs_btree_check_rec(XFS_BTNUM_BMAP,
                                        (void *)lastp, (void *)ep);
@@ -6369,7 +6340,7 @@ xfs_bmap_count_blocks(
        xfs_ifork_t             *ifp;   /* fork structure */
        int                     level;  /* btree level, for checking */
        xfs_mount_t             *mp;    /* file system mount structure */
-       xfs_bmbt_ptr_t          *pp;    /* pointer to block address */
+       __be64                  *pp;    /* pointer to block address */
 
        bno = NULLFSBLOCK;
        mp = ip->i_mount;
@@ -6392,10 +6363,10 @@ xfs_bmap_count_blocks(
        level = be16_to_cpu(block->bb_level);
        ASSERT(level > 0);
        pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
-       ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
-       ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
-       ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
-       bno = INT_GET(*pp, ARCH_CONVERT);
+       bno = be64_to_cpu(*pp);
+       ASSERT(bno != NULLDFSBNO);
+       ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
+       ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
 
        if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
                XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
@@ -6422,7 +6393,7 @@ xfs_bmap_count_tree(
        int                     error;
        xfs_buf_t               *bp, *nbp;
        int                     level = levelin;
-       xfs_bmbt_ptr_t          *pp;
+       __be64                  *pp;
        xfs_fsblock_t           bno = blockno;
        xfs_fsblock_t           nextbno;
        xfs_bmbt_block_t        *block, *nextblock;
@@ -6447,9 +6418,8 @@ xfs_bmap_count_tree(
                }
 
                /* Dive to the next level */
-               pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
-                       xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
-               bno = INT_GET(*pp, ARCH_CONVERT);
+               pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
+               bno = be64_to_cpu(*pp);
                if (unlikely((error =
                     xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
                        xfs_trans_brelse(tp, bp);
@@ -6463,8 +6433,8 @@ xfs_bmap_count_tree(
                for (;;) {
                        nextbno = be64_to_cpu(block->bb_rightsib);
                        numrecs = be16_to_cpu(block->bb_numrecs);
-                       if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
-                                       0, block, numrecs, count) < 0)) {
+                       if (unlikely(xfs_bmap_disk_count_leaves(0,
+                                       block, numrecs, count) < 0)) {
                                xfs_trans_brelse(tp, bp);
                                XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
                                                 XFS_ERRLEVEL_LOW, mp);
@@ -6510,8 +6480,6 @@ xfs_bmap_count_leaves(
  */
 int
 xfs_bmap_disk_count_leaves(
-       xfs_ifork_t             *ifp,
-       xfs_mount_t             *mp,
        xfs_extnum_t            idx,
        xfs_bmbt_block_t        *block,
        int                     numrecs,
@@ -6521,8 +6489,7 @@ xfs_bmap_disk_count_leaves(
        xfs_bmbt_rec_t  *frp;
 
        for (b = 1; b <= numrecs; b++) {
-               frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
-                       xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
+               frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
                *count += xfs_bmbt_disk_get_blockcount(frp);
        }
        return 0;