[XFS] remove remaining VN_HOLD calls
[safe/jmp/linux-2.6] / fs / xfs / xfs_bmap.c
index d384e48..3d6a2ce 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2005 Silicon Graphics, Inc.
+ * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  * All Rights Reserved.
  *
  * This program is free software; you can redistribute it and/or
 #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"
+#include "xfs_vnodeops.h"
 
 
 #ifdef DEBUG
@@ -101,6 +103,7 @@ xfs_bmap_add_extent(
        xfs_fsblock_t           *first, /* pointer to firstblock variable */
        xfs_bmap_free_t         *flist, /* list of extents to be freed */
        int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     whichfork, /* data or attr fork */
        int                     rsvd);  /* OK to allocate reserved blocks */
 
@@ -118,6 +121,7 @@ xfs_bmap_add_extent_delay_real(
        xfs_fsblock_t           *first, /* pointer to firstblock variable */
        xfs_bmap_free_t         *flist, /* list of extents to be freed */
        int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     rsvd);  /* OK to allocate reserved blocks */
 
 /*
@@ -128,9 +132,9 @@ 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 */
 
 /*
@@ -144,6 +148,7 @@ xfs_bmap_add_extent_hole_real(
        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                     whichfork); /* data or attr fork */
 
 /*
@@ -156,7 +161,8 @@ xfs_bmap_add_extent_unwritten_real(
        xfs_extnum_t            idx,    /* extent number to update/insert */
        xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
        xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
-       int                     *logflagsp); /* inode logging flags */
+       int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta); /* Change made to incore extents */
 
 /*
  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
@@ -180,16 +186,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).
@@ -203,6 +199,7 @@ xfs_bmap_del_extent(
        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                     whichfork, /* data or attr fork */
        int                     rsvd);   /* OK to allocate reserved blocks */
 
@@ -252,7 +249,7 @@ xfs_bmap_local_to_extents(
  * Else, *lastxp will be set to the index of the found
  * entry; *gotp will contain the entry.
  */
-STATIC xfs_bmbt_rec_t *                        /* pointer to found extent entry */
+STATIC xfs_bmbt_rec_host_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 */
@@ -277,26 +274,11 @@ xfs_bmap_isaeof(
 
 #ifdef XFS_BMAP_TRACE
 /*
- * Add a bmap trace buffer entry.  Base routine for the others.
- */
-STATIC void
-xfs_bmap_trace_addentry(
-       int             opcode,         /* operation */
-       char            *fname,         /* function name */
-       char            *desc,          /* operation description */
-       xfs_inode_t     *ip,            /* incore inode pointer */
-       xfs_extnum_t    idx,            /* index of entry(ies) */
-       xfs_extnum_t    cnt,            /* count of entries, 1 or 2 */
-       xfs_bmbt_rec_t  *r1,            /* first record */
-       xfs_bmbt_rec_t  *r2,            /* second record or null */
-       int             whichfork);     /* data or attr fork */
-
-/*
  * Add bmap trace entry prior to a call to xfs_iext_remove.
  */
 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 */
@@ -309,7 +291,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 */
@@ -323,7 +305,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 */
@@ -334,17 +316,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(__func__,d,ip,i,c,w)
+#define        XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
+       xfs_bmap_trace_insert(__func__,d,ip,i,c,r1,r2,w)
+#define        XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)    \
+       xfs_bmap_trace_post_update(__func__,d,ip,i,w)
+#define        XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)     \
+       xfs_bmap_trace_pre_update(__func__,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 */
 
 /*
@@ -403,8 +393,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,
@@ -440,7 +428,8 @@ xfs_bmap_add_attrfork_btree(
                cur->bc_private.b.firstblock = *firstblock;
                if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
                        goto error0;
-               ASSERT(stat == 1);      /* must be at least one entry */
+               /* must be at least one entry */
+               XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
                if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
                        goto error0;
                if (stat == 0) {
@@ -510,7 +499,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);
@@ -530,6 +519,7 @@ xfs_bmap_add_extent(
        xfs_fsblock_t           *first, /* pointer to firstblock variable */
        xfs_bmap_free_t         *flist, /* list of extents to be freed */
        int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     whichfork, /* data or attr fork */
        int                     rsvd)   /* OK to use reserved data blocks */
 {
@@ -537,9 +527,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 */
@@ -557,8 +544,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;
@@ -567,6 +554,15 @@ xfs_bmap_add_extent(
                        logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
                } else
                        logflags = 0;
+               /* DELTA: single new extent */
+               if (delta) {
+                       if (delta->xed_startoff > new->br_startoff)
+                               delta->xed_startoff = new->br_startoff;
+                       if (delta->xed_blockcount <
+                                       new->br_startoff + new->br_blockcount)
+                               delta->xed_blockcount = new->br_startoff +
+                                               new->br_blockcount;
+               }
        }
        /*
         * Any kind of new delayed allocation goes here.
@@ -575,8 +571,8 @@ 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,
-                               &logflags, rsvd)))
+               if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
+                               &logflags, delta, rsvd)))
                        goto done;
        }
        /*
@@ -587,7 +583,7 @@ xfs_bmap_add_extent(
                        ASSERT((cur->bc_private.b.flags &
                                XFS_BTCUR_BPRV_WASDEL) == 0);
                if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
-                               &logflags, whichfork)))
+                               &logflags, delta, whichfork)))
                        goto done;
        } else {
                xfs_bmbt_irec_t prev;   /* old extent at offset idx */
@@ -612,17 +608,17 @@ xfs_bmap_add_extent(
                                                XFS_BTCUR_BPRV_WASDEL);
                                if ((error = xfs_bmap_add_extent_delay_real(ip,
                                        idx, &cur, new, &da_new, first, flist,
-                                       &logflags, rsvd)))
+                                       &logflags, delta, rsvd)))
                                        goto done;
                        } else if (new->br_state == XFS_EXT_NORM) {
                                ASSERT(new->br_state == XFS_EXT_NORM);
                                if ((error = xfs_bmap_add_extent_unwritten_real(
-                                       ip, idx, &cur, new, &logflags)))
+                                       ip, idx, &cur, new, &logflags, delta)))
                                        goto done;
                        } else {
                                ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
                                if ((error = xfs_bmap_add_extent_unwritten_real(
-                                       ip, idx, &cur, new, &logflags)))
+                                       ip, idx, &cur, new, &logflags, delta)))
                                        goto done;
                        }
                        ASSERT(*curp == cur || *curp == NULL);
@@ -635,7 +631,7 @@ xfs_bmap_add_extent(
                                ASSERT((cur->bc_private.b.flags &
                                        XFS_BTCUR_BPRV_WASDEL) == 0);
                        if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
-                                       new, &logflags, whichfork)))
+                                       new, &logflags, delta, whichfork)))
                                goto done;
                }
        }
@@ -668,7 +664,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.
@@ -700,15 +696,13 @@ xfs_bmap_add_extent_delay_real(
        xfs_fsblock_t           *first, /* pointer to firstblock variable */
        xfs_bmap_free_t         *flist, /* list of extents to be freed */
        int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     rsvd)   /* OK to use reserved data block allocation */
 {
        xfs_btree_cur_t         *cur;   /* btree cursor */
        int                     diff;   /* temp value */
-       xfs_bmbt_rec_t          *ep;    /* extent entry for idx */
+       xfs_bmbt_rec_host_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 */
@@ -716,8 +710,8 @@ xfs_bmap_add_extent_delay_real(
                                        /* left is 0, right is 1, prev is 2 */
        int                     rval=0; /* return value (logging flags) */
        int                     state = 0;/* state bits, accessed thru macros */
-       xfs_filblks_t           temp;   /* value for dnew calculations */
-       xfs_filblks_t           temp2;  /* value for dnew calculations */
+       xfs_filblks_t           temp=0; /* value for dnew calculations */
+       xfs_filblks_t           temp2=0;/* value for dnew calculations */
        int                     tmp_rval;       /* partial logging flags */
        enum {                          /* bit number definitions for state */
                LEFT_CONTIG,    RIGHT_CONTIG,
@@ -804,15 +798,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--;
@@ -824,13 +817,13 @@ xfs_bmap_add_extent_delay_real(
                                        RIGHT.br_startblock,
                                        RIGHT.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_delete(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_decrement(cur, 0, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
                                        LEFT.br_startblock,
                                        LEFT.br_blockcount +
@@ -839,6 +832,11 @@ xfs_bmap_add_extent_delay_real(
                                goto done;
                }
                *dnew = 0;
+               /* DELTA: Three in-core extents are replaced by one. */
+               temp = LEFT.br_startoff;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
@@ -846,15 +844,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;
@@ -864,7 +861,7 @@ xfs_bmap_add_extent_delay_real(
                                        LEFT.br_startblock, LEFT.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
                                        LEFT.br_startblock,
                                        LEFT.br_blockcount +
@@ -872,6 +869,10 @@ xfs_bmap_add_extent_delay_real(
                                goto done;
                }
                *dnew = 0;
+               /* DELTA: Two in-core extents are replaced by one. */
+               temp = LEFT.br_startoff;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
@@ -879,16 +880,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;
@@ -898,7 +896,7 @@ xfs_bmap_add_extent_delay_real(
                                        RIGHT.br_startblock,
                                        RIGHT.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
                                        new->br_startblock,
                                        PREV.br_blockcount +
@@ -906,6 +904,10 @@ xfs_bmap_add_extent_delay_real(
                                goto done;
                }
                *dnew = 0;
+               /* DELTA: Two in-core extents are replaced by one. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK2(LEFT_FILLING, RIGHT_FILLING):
@@ -914,11 +916,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)
@@ -929,13 +929,16 @@ xfs_bmap_add_extent_delay_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 0);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        cur->bc_rec.b.br_state = XFS_EXT_NORM;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
                *dnew = 0;
+               /* DELTA: The in-core extent described by new changed type. */
+               temp = new->br_startoff;
+               temp2 = new->br_blockcount;
                break;
 
        case MASK2(LEFT_FILLING, LEFT_CONTIG):
@@ -943,17 +946,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)
@@ -964,7 +964,7 @@ xfs_bmap_add_extent_delay_real(
                                        LEFT.br_startblock, LEFT.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
                                        LEFT.br_startblock,
                                        LEFT.br_blockcount +
@@ -975,9 +975,12 @@ 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;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount;
                break;
 
        case MASK(LEFT_FILLING):
@@ -985,11 +988,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;
@@ -1002,11 +1005,11 @@ xfs_bmap_add_extent_delay_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 0);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        cur->bc_rec.b.br_state = XFS_EXT_NORM;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
                if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
                    ip->i_d.di_nextents > ip->i_df.if_ext_max) {
@@ -1022,9 +1025,11 @@ 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;
+               temp2 = PREV.br_blockcount;
                break;
 
        case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
@@ -1033,17 +1038,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;
@@ -1053,7 +1055,7 @@ xfs_bmap_add_extent_delay_real(
                                        RIGHT.br_startblock,
                                        RIGHT.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, new->br_startoff,
                                        new->br_startblock,
                                        new->br_blockcount +
@@ -1064,9 +1066,12 @@ 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;
+               temp2 = PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK(RIGHT_FILLING):
@@ -1075,10 +1080,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++;
@@ -1090,11 +1095,11 @@ xfs_bmap_add_extent_delay_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 0);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        cur->bc_rec.b.br_state = XFS_EXT_NORM;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
                if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
                    ip->i_d.di_nextents > ip->i_df.if_ext_max) {
@@ -1110,8 +1115,11 @@ 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;
+               temp2 = PREV.br_blockcount;
                break;
 
        case 0:
@@ -1121,13 +1129,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;
@@ -1140,11 +1150,11 @@ xfs_bmap_add_extent_delay_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 0);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        cur->bc_rec.b.br_state = XFS_EXT_NORM;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
                if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
                    ip->i_d.di_nextents > ip->i_df.if_ext_max) {
@@ -1160,7 +1170,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.
                         */
@@ -1171,7 +1181,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) {
@@ -1179,21 +1189,22 @@ 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;
+               temp2 = PREV.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
@@ -1209,6 +1220,13 @@ xfs_bmap_add_extent_delay_real(
                ASSERT(0);
        }
        *curp = cur;
+       if (delta) {
+               temp2 += temp;
+               if (delta->xed_startoff > temp)
+                       delta->xed_startoff = temp;
+               if (delta->xed_blockcount < temp2)
+                       delta->xed_blockcount = temp2;
+       }
 done:
        *logflagsp = rval;
        return error;
@@ -1235,14 +1253,12 @@ xfs_bmap_add_extent_unwritten_real(
        xfs_extnum_t            idx,    /* extent number to update/insert */
        xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
        xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
-       int                     *logflagsp) /* inode logging flags */
+       int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta) /* Change made to incore extents */
 {
        xfs_btree_cur_t         *cur;   /* btree cursor */
-       xfs_bmbt_rec_t          *ep;    /* extent entry for idx */
+       xfs_bmbt_rec_host_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 */
@@ -1252,6 +1268,8 @@ xfs_bmap_add_extent_unwritten_real(
                                        /* left is 0, right is 1, prev is 2 */
        int                     rval=0; /* return value (logging flags) */
        int                     state = 0;/* state bits, accessed thru macros */
+       xfs_filblks_t           temp=0;
+       xfs_filblks_t           temp2=0;
        enum {                          /* bit number definitions for state */
                LEFT_CONTIG,    RIGHT_CONTIG,
                LEFT_FILLING,   RIGHT_FILLING,
@@ -1341,15 +1359,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;
@@ -1361,25 +1378,30 @@ xfs_bmap_add_extent_unwritten_real(
                                        RIGHT.br_startblock,
                                        RIGHT.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_delete(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_decrement(cur, 0, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_delete(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_decrement(cur, 0, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
                                LEFT.br_startblock,
                                LEFT.br_blockcount + PREV.br_blockcount +
                                RIGHT.br_blockcount, LEFT.br_state)))
                                goto done;
                }
+               /* DELTA: Three in-core extents are replaced by one. */
+               temp = LEFT.br_startoff;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
@@ -1387,15 +1409,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)
@@ -1406,19 +1427,23 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock, PREV.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_delete(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_decrement(cur, 0, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
                                LEFT.br_startblock,
                                LEFT.br_blockcount + PREV.br_blockcount,
                                LEFT.br_state)))
                                goto done;
                }
+               /* DELTA: Two in-core extents are replaced by one. */
+               temp = LEFT.br_startoff;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
@@ -1426,16 +1451,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)
@@ -1446,19 +1470,23 @@ xfs_bmap_add_extent_unwritten_real(
                                        RIGHT.br_startblock,
                                        RIGHT.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_delete(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_decrement(cur, 0, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, new->br_startoff,
                                new->br_startblock,
                                new->br_blockcount + RIGHT.br_blockcount,
                                newext)))
                                goto done;
                }
+               /* DELTA: Two in-core extents are replaced by one. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK2(LEFT_FILLING, RIGHT_FILLING):
@@ -1467,10 +1495,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)
@@ -1481,12 +1509,15 @@ xfs_bmap_add_extent_unwritten_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, new->br_startoff,
                                new->br_startblock, new->br_blockcount,
                                newext)))
                                goto done;
                }
+               /* DELTA: The in-core extent described by new changed type. */
+               temp = new->br_startoff;
+               temp2 = new->br_blockcount;
                break;
 
        case MASK2(LEFT_FILLING, LEFT_CONTIG):
@@ -1494,21 +1525,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)
@@ -1519,7 +1550,7 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock, PREV.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur,
                                PREV.br_startoff + new->br_blockcount,
                                PREV.br_startblock + new->br_blockcount,
@@ -1534,6 +1565,10 @@ xfs_bmap_add_extent_unwritten_real(
                                LEFT.br_state))
                                goto done;
                }
+               /* DELTA: The boundary between two in-core extents moved. */
+               temp = LEFT.br_startoff;
+               temp2 = LEFT.br_blockcount +
+                       PREV.br_blockcount;
                break;
 
        case MASK(LEFT_FILLING):
@@ -1541,15 +1576,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;
@@ -1562,7 +1597,7 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock, PREV.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur,
                                PREV.br_startoff + new->br_blockcount,
                                PREV.br_startblock + new->br_blockcount,
@@ -1572,8 +1607,11 @@ xfs_bmap_add_extent_unwritten_real(
                        cur->bc_rec.b = *new;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
+               /* DELTA: One in-core extent is split in two. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount;
                break;
 
        case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
@@ -1581,18 +1619,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)
@@ -1603,7 +1641,7 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock,
                                        PREV.br_blockcount, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
                                PREV.br_startblock,
                                PREV.br_blockcount - new->br_blockcount,
@@ -1617,6 +1655,10 @@ xfs_bmap_add_extent_unwritten_real(
                                newext)))
                                goto done;
                }
+               /* DELTA: The boundary between two in-core extents moved. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount +
+                       RIGHT.br_blockcount;
                break;
 
        case MASK(RIGHT_FILLING):
@@ -1624,12 +1666,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++;
@@ -1641,7 +1683,7 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock, PREV.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
                                PREV.br_startblock,
                                PREV.br_blockcount - new->br_blockcount,
@@ -1651,12 +1693,15 @@ xfs_bmap_add_extent_unwritten_real(
                                        new->br_startblock, new->br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 0);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        cur->bc_rec.b.br_state = XFS_EXT_NORM;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
+               /* DELTA: One in-core extent is split in two. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount;
                break;
 
        case 0:
@@ -1665,17 +1710,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;
@@ -1688,28 +1733,38 @@ xfs_bmap_add_extent_unwritten_real(
                                        PREV.br_startblock, PREV.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        /* new right extent - oldext */
                        if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
                                r[1].br_startblock, r[1].br_blockcount,
                                r[1].br_state)))
                                goto done;
                        /* new left extent - oldext */
-                       PREV.br_blockcount =
-                               new->br_startoff - PREV.br_startoff;
                        cur->bc_rec.b = PREV;
+                       cur->bc_rec.b.br_blockcount =
+                               new->br_startoff - PREV.br_startoff;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
-                       if ((error = xfs_bmbt_increment(cur, 0, &i)))
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       /*
+                        * Reset the cursor to the position of the new extent
+                        * we are about to insert as we can't trust it after
+                        * the previous insert.
+                        */
+                       if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
+                                       new->br_startblock, new->br_blockcount,
+                                       &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
                        /* new middle extent - newext */
-                       cur->bc_rec.b = *new;
+                       cur->bc_rec.b.br_state = new->br_state;
                        if ((error = xfs_bmbt_insert(cur, &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
+               /* DELTA: One in-core extent is split in three. */
+               temp = PREV.br_startoff;
+               temp2 = PREV.br_blockcount;
                break;
 
        case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
@@ -1725,6 +1780,13 @@ xfs_bmap_add_extent_unwritten_real(
                ASSERT(0);
        }
        *curp = cur;
+       if (delta) {
+               temp2 += temp;
+               if (delta->xed_startoff > temp)
+                       delta->xed_startoff = temp;
+               if (delta->xed_blockcount < temp2)
+                       delta->xed_blockcount = temp2;
+       }
 done:
        *logflagsp = rval;
        return error;
@@ -1750,22 +1812,20 @@ 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_bmbt_rec_host_t     *ep;    /* extent record for idx */
        xfs_ifork_t             *ifp;   /* inode fork pointer */
        xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
        xfs_filblks_t           newlen=0;       /* new indirect size */
        xfs_filblks_t           oldlen=0;       /* old indirect size */
        xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
        int                     state;  /* state bits, accessed thru macros */
-       xfs_filblks_t           temp;   /* temp for indirect calculations */
+       xfs_filblks_t           temp=0; /* temp for indirect calculations */
+       xfs_filblks_t           temp2=0;
        enum {                          /* bit number definitions for state */
                LEFT_CONTIG,    RIGHT_CONTIG,
                LEFT_DELAY,     RIGHT_DELAY,
@@ -1829,7 +1889,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) +
@@ -1838,12 +1898,14 @@ 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. */
+               temp2 = temp;
+               temp = left.br_startoff;
                break;
 
        case MASK(LEFT_CONTIG):
@@ -1853,7 +1915,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) +
@@ -1861,9 +1923,12 @@ 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. */
+               temp2 = temp;
+               temp = left.br_startoff;
                break;
 
        case MASK(RIGHT_CONTIG):
@@ -1872,15 +1937,18 @@ 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;
+               temp = new->br_startoff;
                break;
 
        case 0:
@@ -1890,20 +1958,30 @@ 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;
+               /* DELTA: A new in-core extent was added in a hole. */
+               temp2 = new->br_blockcount;
+               temp = new->br_startoff;
                break;
        }
        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.
                 */
        }
+       if (delta) {
+               temp2 += temp;
+               if (delta->xed_startoff > temp)
+                       delta->xed_startoff = temp;
+               if (delta->xed_blockcount < temp2)
+                       delta->xed_blockcount = temp2;
+       }
        *logflagsp = 0;
        return 0;
 #undef MASK
@@ -1925,18 +2003,19 @@ xfs_bmap_add_extent_hole_real(
        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                     whichfork) /* data or attr fork */
 {
-       xfs_bmbt_rec_t          *ep;    /* pointer to extent entry ins. point */
+       xfs_bmbt_rec_host_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 */
        xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
+       int                     rval=0; /* return value (logging flags) */
        int                     state;  /* state bits, accessed thru macros */
+       xfs_filblks_t           temp=0;
+       xfs_filblks_t           temp2=0;
        enum {                          /* bit number definitions for state */
                LEFT_CONTIG,    RIGHT_CONTIG,
                LEFT_DELAY,     RIGHT_DELAY,
@@ -1993,6 +2072,7 @@ xfs_bmap_add_extent_hole_real(
                 left.br_blockcount + new->br_blockcount +
                     right.br_blockcount <= MAXEXTLEN));
 
+       error = 0;
        /*
         * Select which case we're in here, and implement it.
         */
@@ -2004,39 +2084,48 @@ 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,
                        XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
                if (cur == NULL) {
-                       *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
-                       return 0;
+                       rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
+               } else {
+                       rval = XFS_ILOG_CORE;
+                       if ((error = xfs_bmbt_lookup_eq(cur,
+                                       right.br_startoff,
+                                       right.br_startblock,
+                                       right.br_blockcount, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       if ((error = xfs_bmbt_delete(cur, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       if ((error = xfs_bmbt_decrement(cur, 0, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       if ((error = xfs_bmbt_update(cur, left.br_startoff,
+                                       left.br_startblock,
+                                       left.br_blockcount +
+                                               new->br_blockcount +
+                                               right.br_blockcount,
+                                       left.br_state)))
+                               goto done;
                }
-               *logflagsp = XFS_ILOG_CORE;
-               if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
-                               right.br_startblock, right.br_blockcount, &i)))
-                       return error;
-               ASSERT(i == 1);
-               if ((error = xfs_bmbt_delete(cur, &i)))
-                       return error;
-               ASSERT(i == 1);
-               if ((error = xfs_bmbt_decrement(cur, 0, &i)))
-                       return error;
-               ASSERT(i == 1);
-               error = xfs_bmbt_update(cur, left.br_startoff,
-                               left.br_startblock,
-                               left.br_blockcount + new->br_blockcount +
-                               right.br_blockcount, left.br_state);
-               return error;
+               /* DELTA: Two in-core extents were replaced by one. */
+               temp = left.br_startoff;
+               temp2 = left.br_blockcount +
+                       new->br_blockcount +
+                       right.br_blockcount;
+               break;
 
        case MASK(LEFT_CONTIG):
                /*
@@ -2044,25 +2133,33 @@ 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) {
-                       *logflagsp = XFS_ILOG_FEXT(whichfork);
-                       return 0;
+                       rval = XFS_ILOG_FEXT(whichfork);
+               } else {
+                       rval = 0;
+                       if ((error = xfs_bmbt_lookup_eq(cur,
+                                       left.br_startoff,
+                                       left.br_startblock,
+                                       left.br_blockcount, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       if ((error = xfs_bmbt_update(cur, left.br_startoff,
+                                       left.br_startblock,
+                                       left.br_blockcount +
+                                               new->br_blockcount,
+                                       left.br_state)))
+                               goto done;
                }
-               *logflagsp = 0;
-               if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
-                               left.br_startblock, left.br_blockcount, &i)))
-                       return error;
-               ASSERT(i == 1);
-               error = xfs_bmbt_update(cur, left.br_startoff,
-                               left.br_startblock,
-                               left.br_blockcount + new->br_blockcount,
-                               left.br_state);
-               return error;
+               /* DELTA: One in-core extent grew. */
+               temp = left.br_startoff;
+               temp2 = left.br_blockcount +
+                       new->br_blockcount;
+               break;
 
        case MASK(RIGHT_CONTIG):
                /*
@@ -2070,26 +2167,34 @@ 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) {
-                       *logflagsp = XFS_ILOG_FEXT(whichfork);
-                       return 0;
+                       rval = XFS_ILOG_FEXT(whichfork);
+               } else {
+                       rval = 0;
+                       if ((error = xfs_bmbt_lookup_eq(cur,
+                                       right.br_startoff,
+                                       right.br_startblock,
+                                       right.br_blockcount, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
+                       if ((error = xfs_bmbt_update(cur, new->br_startoff,
+                                       new->br_startblock,
+                                       new->br_blockcount +
+                                               right.br_blockcount,
+                                       right.br_state)))
+                               goto done;
                }
-               *logflagsp = 0;
-               if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
-                               right.br_startblock, right.br_blockcount, &i)))
-                       return error;
-               ASSERT(i == 1);
-               error = xfs_bmbt_update(cur, new->br_startoff,
-                               new->br_startblock,
-                               new->br_blockcount + right.br_blockcount,
-                               right.br_state);
-               return error;
+               /* DELTA: One in-core extent grew. */
+               temp = new->br_startoff;
+               temp2 = new->br_blockcount +
+                       right.br_blockcount;
+               break;
 
        case 0:
                /*
@@ -2097,36 +2202,47 @@ 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,
                        XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
                if (cur == NULL) {
-                       *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
-                       return 0;
+                       rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
+               } else {
+                       rval = XFS_ILOG_CORE;
+                       if ((error = xfs_bmbt_lookup_eq(cur,
+                                       new->br_startoff,
+                                       new->br_startblock,
+                                       new->br_blockcount, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 0, done);
+                       cur->bc_rec.b.br_state = new->br_state;
+                       if ((error = xfs_bmbt_insert(cur, &i)))
+                               goto done;
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
-               *logflagsp = XFS_ILOG_CORE;
-               if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
-                               new->br_startblock, new->br_blockcount, &i)))
-                       return error;
-               ASSERT(i == 0);
-               cur->bc_rec.b.br_state = new->br_state;
-               if ((error = xfs_bmbt_insert(cur, &i)))
-                       return error;
-               ASSERT(i == 1);
-               return 0;
+               /* DELTA: A new extent was added in a hole. */
+               temp = new->br_startoff;
+               temp2 = new->br_blockcount;
+               break;
        }
+       if (delta) {
+               temp2 += temp;
+               if (delta->xed_startoff > temp)
+                       delta->xed_startoff = temp;
+               if (delta->xed_blockcount < temp2)
+                       delta->xed_blockcount = temp2;
+       }
+done:
+       *logflagsp = rval;
+       return error;
 #undef MASK
 #undef MASK2
 #undef STATE_SET
 #undef STATE_TEST
 #undef STATE_SET_TEST
 #undef SWITCH_STATE
-       /* NOTREACHED */
-       ASSERT(0);
-       return 0; /* keep gcc quite */
 }
 
 /*
@@ -2294,7 +2410,7 @@ xfs_bmap_extsize_align(
 
 #define XFS_ALLOC_GAP_UNITS    4
 
-STATIC int
+STATIC void
 xfs_bmap_adjacent(
        xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
 {
@@ -2440,7 +2556,6 @@ xfs_bmap_adjacent(
                        ap->rval = gotbno;
        }
 #undef ISVALID
-       return 0;
 }
 
 STATIC int
@@ -2453,12 +2568,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,
@@ -2492,6 +2605,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;
@@ -2563,9 +2678,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,
@@ -2575,9 +2688,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);
@@ -2598,15 +2717,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) {
@@ -2632,6 +2761,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)
@@ -2656,24 +2814,34 @@ 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 >= PAGE_CACHE_SIZE) {
                args.prod = 1;
                args.mod = 0;
        } else {
-               args.prod = NBPP >> mp->m_sb.sb_blocklog;
+               args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
                if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
                        args.mod = (xfs_extlen_t)(args.prod - args.mod);
        }
@@ -2808,7 +2976,7 @@ STATIC int
 xfs_bmap_alloc(
        xfs_bmalloca_t  *ap)            /* bmap alloc argument struct */
 {
-       if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
+       if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
                return xfs_bmap_rtalloc(ap);
        return xfs_bmap_btalloc(ap);
 }
@@ -2834,7 +3002,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);
@@ -2846,12 +3014,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;
@@ -2885,6 +3053,7 @@ xfs_bmap_del_extent(
        xfs_btree_cur_t         *cur,   /* if null, not a btree */
        xfs_bmbt_irec_t         *del,   /* data to remove from extents */
        int                     *logflagsp, /* inode logging flags */
+       xfs_extdelta_t          *delta, /* Change made to incore extents */
        int                     whichfork, /* data or attr fork */
        int                     rsvd)   /* OK to allocate reserved blocks */
 {
@@ -2894,12 +3063,9 @@ xfs_bmap_del_extent(
        xfs_fileoff_t           del_endoff;     /* first offset past del */
        int                     delay;  /* current block is delayed allocated */
        int                     do_fx;  /* free extent at end of routine */
-       xfs_bmbt_rec_t          *ep;    /* current extent entry pointer */
+       xfs_bmbt_rec_host_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 */
@@ -2937,8 +3103,7 @@ xfs_bmap_del_extent(
                /*
                 * Realtime allocation.  Free it and record di_nblocks update.
                 */
-               if (whichfork == XFS_DATA_FORK &&
-                   (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
+               if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
                        xfs_fsblock_t   bno;
                        xfs_filblks_t   len;
 
@@ -2974,7 +3139,7 @@ xfs_bmap_del_extent(
                                        got.br_startblock, got.br_blockcount,
                                        &i)))
                                goto done;
-                       ASSERT(i == 1);
+                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                }
                da_old = da_new = 0;
        } else {
@@ -2993,7 +3158,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)
@@ -3007,14 +3172,14 @@ xfs_bmap_del_extent(
                }
                if ((error = xfs_bmbt_delete(cur, &i)))
                        goto done;
-               ASSERT(i == 1);
+               XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                break;
 
        case 2:
                /*
                 * 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);
@@ -3023,13 +3188,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;
@@ -3045,19 +3210,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;
@@ -3074,7 +3239,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;
@@ -3111,7 +3276,7 @@ xfs_bmap_del_extent(
                                                        got.br_startblock,
                                                        temp, &i)))
                                                goto done;
-                                       ASSERT(i == 1);
+                                       XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                                        /*
                                         * Update the btree record back
                                         * to the original value.
@@ -3132,7 +3297,7 @@ xfs_bmap_del_extent(
                                        error = XFS_ERROR(ENOSPC);
                                        goto done;
                                }
-                               ASSERT(i == 1);
+                               XFS_WANT_CORRUPTED_GOTO(i == 1, done);
                        } else
                                flags |= XFS_ILOG_FEXT(whichfork);
                        XFS_IFORK_NEXT_SET(ip, whichfork,
@@ -3161,8 +3326,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;
@@ -3191,8 +3356,16 @@ 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. */
+               if (delta->xed_startoff > got.br_startoff)
+                       delta->xed_startoff = got.br_startoff;
+               if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
+                       delta->xed_blockcount = got.br_startoff +
+                                                       got.br_blockcount;
+       }
 done:
        *logflagsp = flags;
        return error;
@@ -3237,7 +3410,7 @@ xfs_bmap_extents_to_btree(
        xfs_bmbt_rec_t          *arp;           /* child record pointer */
        xfs_bmbt_block_t        *block;         /* btree root block */
        xfs_btree_cur_t         *cur;           /* bmap btree cursor */
-       xfs_bmbt_rec_t          *ep;            /* extent record pointer */
+       xfs_bmbt_rec_host_t     *ep;            /* extent record pointer */
        int                     error;          /* error return value */
        xfs_extnum_t            i, cnt;         /* extent record index */
        xfs_ifork_t             *ifp;           /* inode fork pointer */
@@ -3279,6 +3452,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);
@@ -3325,8 +3499,8 @@ xfs_bmap_extents_to_btree(
        for (cnt = i = 0; i < nextents; i++) {
                ep = xfs_iext_get_ext(ifp, i);
                if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
-                       arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
-                       arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
+                       arp->l0 = cpu_to_be64(ep->l0);
+                       arp->l1 = cpu_to_be64(ep->l1);
                        arp++; cnt++;
                }
        }
@@ -3337,9 +3511,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.
@@ -3366,6 +3540,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) /
@@ -3392,9 +3567,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 */
 
        /*
@@ -3410,10 +3582,11 @@ xfs_bmap_local_to_extents(
        if (ifp->if_bytes) {
                xfs_alloc_arg_t args;   /* allocation arguments */
                xfs_buf_t       *bp;    /* buffer for extent block */
-               xfs_bmbt_rec_t  *ep;    /* extent record pointer */
+               xfs_bmbt_rec_host_t *ep;/* extent record pointer */
 
                args.tp = tp;
                args.mp = ip->i_mount;
+               args.firstblock = *firstblock;
                ASSERT((ifp->if_flags &
                        (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
                /*
@@ -3448,7 +3621,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,
@@ -3474,7 +3647,7 @@ done:
  * entry (null if none).  Else, *lastxp will be set to the index
  * of the found entry; *gotp will contain the entry.
  */
-xfs_bmbt_rec_t *                       /* pointer to found extent entry */
+xfs_bmbt_rec_host_t *                  /* pointer to found extent entry */
 xfs_bmap_search_multi_extents(
        xfs_ifork_t     *ifp,           /* inode fork pointer */
        xfs_fileoff_t   bno,            /* block number searched for */
@@ -3483,7 +3656,7 @@ xfs_bmap_search_multi_extents(
        xfs_bmbt_irec_t *gotp,          /* out: extent entry found */
        xfs_bmbt_irec_t *prevp)         /* out: previous extent entry found */
 {
-       xfs_bmbt_rec_t  *ep;            /* extent record pointer */
+       xfs_bmbt_rec_host_t *ep;                /* extent record pointer */
        xfs_extnum_t    lastx;          /* last extent index */
 
        /*
@@ -3525,37 +3698,40 @@ xfs_bmap_search_multi_extents(
  * Else, *lastxp will be set to the index of the found
  * entry; *gotp will contain the entry.
  */
-STATIC xfs_bmbt_rec_t *                 /* pointer to found extent entry */
+STATIC xfs_bmbt_rec_host_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 */
        xfs_bmbt_irec_t *prevp)         /* out: previous extent entry found */
 {
        xfs_ifork_t     *ifp;           /* inode fork pointer */
-       xfs_bmbt_rec_t  *ep;            /* extent record pointer */
-       int             rt;             /* realtime flag    */
+       xfs_bmbt_rec_host_t  *ep;            /* extent record pointer */
 
        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;
 }
 
 
@@ -3568,16 +3744,16 @@ 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) */
        xfs_extnum_t    cnt,            /* count of entries, 1 or 2 */
-       xfs_bmbt_rec_t  *r1,            /* first record */
-       xfs_bmbt_rec_t  *r2,            /* second record or null */
+       xfs_bmbt_rec_host_t *r1,        /* first record */
+       xfs_bmbt_rec_host_t *r2,        /* second record or null */
        int             whichfork)      /* data or attr fork */
 {
-       xfs_bmbt_rec_ tr2;
+       xfs_bmbt_rec_host_t tr2;
 
        ASSERT(cnt == 1 || cnt == 2);
        ASSERT(r1 != NULL);
@@ -3627,7 +3803,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 */
@@ -3649,7 +3825,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 */
@@ -3658,8 +3834,8 @@ xfs_bmap_trace_insert(
        xfs_bmbt_irec_t *r2,            /* inserted record 2 or null */
        int             whichfork)      /* data or attr fork */
 {
-       xfs_bmbt_rec_t  tr1;            /* compressed record 1 */
-       xfs_bmbt_rec_t  tr2;            /* compressed record 2 if needed */
+       xfs_bmbt_rec_host_t tr1;        /* compressed record 1 */
+       xfs_bmbt_rec_host_t tr2;        /* compressed record 2 if needed */
 
        xfs_bmbt_set_all(&tr1, r1);
        if (cnt == 2) {
@@ -3678,7 +3854,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 */
@@ -3696,7 +3872,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 */
@@ -3753,7 +3929,7 @@ xfs_bunmap_trace(
        if (ip->i_rwtrace == NULL)
                return;
        ktrace_enter(ip->i_rwtrace,
-               (void *)(__psint_t)XFS_BUNMAPI,
+               (void *)(__psint_t)XFS_BUNMAP,
                (void *)ip,
                (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
                (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
@@ -3786,7 +3962,6 @@ xfs_bmap_add_attrfork(
        xfs_bmap_free_t         flist;          /* freed extent records */
        xfs_mount_t             *mp;            /* mount structure */
        xfs_trans_t             *tp;            /* transaction pointer */
-       unsigned long           s;              /* spinlock spl value */
        int                     blks;           /* space reservation */
        int                     version = 1;    /* superblock attr version */
        int                     committed;      /* xaction was committed */
@@ -3825,7 +4000,7 @@ xfs_bmap_add_attrfork(
                ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
        }
        ASSERT(ip->i_d.di_anextents == 0);
-       VN_HOLD(XFS_ITOV(ip));
+       IHOLD(ip);
        xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
        xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
        switch (ip->i_d.di_format) {
@@ -3879,35 +4054,34 @@ xfs_bmap_add_attrfork(
                xfs_trans_log_inode(tp, ip, logflags);
        if (error)
                goto error2;
-       if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
-          (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
+       if (!xfs_sb_version_hasattr(&mp->m_sb) ||
+          (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
                __int64_t sbfields = 0;
 
-               s = XFS_SB_LOCK(mp);
-               if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
-                       XFS_SB_VERSION_ADDATTR(&mp->m_sb);
+               spin_lock(&mp->m_sb_lock);
+               if (!xfs_sb_version_hasattr(&mp->m_sb)) {
+                       xfs_sb_version_addattr(&mp->m_sb);
                        sbfields |= XFS_SB_VERSIONNUM;
                }
-               if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
-                       XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
+               if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
+                       xfs_sb_version_addattr2(&mp->m_sb);
                        sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
                }
                if (sbfields) {
-                       XFS_SB_UNLOCK(mp, s);
+                       spin_unlock(&mp->m_sb_lock);
                        xfs_mod_sb(tp, sbfields);
                } else
-                       XFS_SB_UNLOCK(mp, s);
+                       spin_unlock(&mp->m_sb_lock);
        }
-       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;
 error2:
        xfs_bmap_cancel(&flist);
 error1:
-       ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
        xfs_iunlock(ip, XFS_ILOCK_EXCL);
 error0:
        xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
@@ -3986,16 +4160,21 @@ xfs_bmap_compute_maxlevels(
         * number of leaf entries, is controlled by the type of di_nextents
         * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
         * (a signed 16-bit number, xfs_aextnum_t).
+        *
+        * Note that we can no longer assume that if we are in ATTR1 that
+        * the fork offset of all the inodes will be (m_attroffset >> 3)
+        * because we could have mounted with ATTR2 and then mounted back
+        * with ATTR1, keeping the di_forkoff's fixed but probably at
+        * various positions. Therefore, for both ATTR1 and ATTR2
+        * we have to assume the worst case scenario of a minimum size
+        * available.
         */
        if (whichfork == XFS_DATA_FORK) {
                maxleafents = MAXEXTNUM;
-               sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
-                       XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
+               sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
        } else {
                maxleafents = MAXAEXTNUM;
-               sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
-                       XFS_BMDR_SPACE_CALC(MINABTPTRS) :
-                       mp->m_sb.sb_inodesize - mp->m_attroffset;
+               sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
        }
        maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
        minleafrecs = mp->m_bmap_dmnr[0];
@@ -4031,7 +4210,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 */
@@ -4057,7 +4235,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;
        /*
@@ -4087,8 +4265,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,
@@ -4133,7 +4311,6 @@ xfs_bmap_first_unused(
        xfs_fileoff_t   *first_unused,          /* unused block */
        int             whichfork)              /* data or attr fork */
 {
-       xfs_bmbt_rec_t  *ep;                    /* pointer to an extent entry */
        int             error;                  /* error return value */
        int             idx;                    /* extent record index */
        xfs_ifork_t     *ifp;                   /* inode fork pointer */
@@ -4157,7 +4334,7 @@ xfs_bmap_first_unused(
        lowest = *first_unused;
        nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
        for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
-               ep = xfs_iext_get_ext(ifp, idx);
+               xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
                off = xfs_bmbt_get_startoff(ep);
                /*
                 * See if the hole before this extent will work.
@@ -4188,7 +4365,7 @@ xfs_bmap_last_before(
 {
        xfs_fileoff_t   bno;                    /* input file offset */
        int             eof;                    /* hit end of file */
-       xfs_bmbt_rec_t  *ep;                    /* pointer to last extent */
+       xfs_bmbt_rec_host_t *ep;                /* pointer to last extent */
        int             error;                  /* error return value */
        xfs_bmbt_irec_t got;                    /* current extent value */
        xfs_ifork_t     *ifp;                   /* inode fork pointer */
@@ -4234,7 +4411,7 @@ xfs_bmap_last_offset(
        xfs_fileoff_t   *last_block,            /* last block */
        int             whichfork)              /* data or attr fork */
 {
-       xfs_bmbt_rec_t  *ep;                    /* pointer to last extent */
+       xfs_bmbt_rec_host_t *ep;                /* pointer to last extent */
        int             error;                  /* error return value */
        xfs_ifork_t     *ifp;                   /* inode fork pointer */
        xfs_extnum_t    nextents;               /* number of extent entries */
@@ -4271,14 +4448,17 @@ xfs_bmap_one_block(
        xfs_inode_t     *ip,            /* incore inode */
        int             whichfork)      /* data or attr fork */
 {
-       xfs_bmbt_rec_t  *ep;            /* ptr to fork's extent */
+       xfs_bmbt_rec_host_t *ep;        /* ptr to fork's extent */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
        int             rval;           /* return value */
        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;
@@ -4290,7 +4470,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;
 }
 
@@ -4311,14 +4491,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 */
 
@@ -4334,10 +4511,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.
@@ -4352,12 +4529,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);
        }
        /*
@@ -4369,7 +4543,7 @@ xfs_bmap_read_extents(
         * Loop over all leaf nodes.  Copy information to the extent records.
         */
        for (;;) {
-               xfs_bmbt_rec_t  *frp, *trp;
+               xfs_bmbt_rec_t  *frp;
                xfs_fsblock_t   nextbno;
                xfs_extnum_t    num_recs;
                xfs_extnum_t    start;
@@ -4398,13 +4572,12 @@ 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);
-                       trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
-                       trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
+                       xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
+                       trp->l0 = be64_to_cpu(frp->l0);
+                       trp->l1 = be64_to_cpu(frp->l1);
                }
                if (exntf == XFS_EXTFMT_NOSTATE) {
                        /*
@@ -4434,7 +4607,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);
@@ -4447,12 +4620,12 @@ 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 */
 {
-       xfs_bmbt_rec_t  *ep;            /* current extent record */
+       xfs_bmbt_rec_host_t *ep;        /* current extent record */
        xfs_extnum_t    idx;            /* extent record index */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
        xfs_bmbt_irec_t s;              /* file extent record */
@@ -4462,7 +4635,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);
        }
 }
@@ -4538,7 +4711,8 @@ xfs_bmapi(
        xfs_extlen_t    total,          /* total blocks needed */
        xfs_bmbt_irec_t *mval,          /* output: map values */
        int             *nmap,          /* i/o: mval size/count */
-       xfs_bmap_free_t *flist)         /* i/o: list extents to free */
+       xfs_bmap_free_t *flist,         /* i/o: list extents to free */
+       xfs_extdelta_t  *delta)         /* o: change made to incore extents */
 {
        xfs_fsblock_t   abno;           /* allocated block number */
        xfs_extlen_t    alen;           /* allocated extent length */
@@ -4547,7 +4721,7 @@ xfs_bmapi(
        xfs_btree_cur_t *cur;           /* bmap btree cursor */
        xfs_fileoff_t   end;            /* end of mapped file region */
        int             eof;            /* we've hit the end of extents */
-       xfs_bmbt_rec_t  *ep;            /* extent record pointer */
+       xfs_bmbt_rec_host_t *ep;        /* extent record pointer */
        int             error;          /* error return */
        xfs_bmbt_irec_t got;            /* current file extent record */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
@@ -4650,6 +4824,10 @@ xfs_bmapi(
        end = bno + len;
        obno = bno;
        bma.ip = NULL;
+       if (delta) {
+               delta->xed_startoff = NULLFILEOFF;
+               delta->xed_blockcount = 0;
+       }
        while (bno < end && n < *nmap) {
                /*
                 * Reading past eof, act as though there's a hole
@@ -4697,12 +4875,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,
@@ -4719,18 +4892,17 @@ xfs_bmapi(
                                /*
                                 * Make a transaction-less quota reservation for
                                 * delayed allocation blocks. This number gets
-                                * adjusted later.
-                                * We return EDQUOT if we haven't allocated
-                                * blks already inside this loop;
+                                * adjusted later.  We return if we haven't
+                                * allocated blocks already inside this loop.
                                 */
-                               if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
+                               if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
                                                mp, NULL, ip, (long)alen, 0,
                                                rt ? XFS_QMOPT_RES_RTBLKS :
-                                                    XFS_QMOPT_RES_REGBLKS)) {
+                                                    XFS_QMOPT_RES_REGBLKS))) {
                                        if (n == 0) {
                                                *nmap = 0;
                                                ASSERT(cur == NULL);
-                                               return XFS_ERROR(EDQUOT);
+                                               return error;
                                        }
                                        break;
                                }
@@ -4746,28 +4918,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));
                                }
 
@@ -4813,7 +4985,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;
@@ -4882,13 +5054,13 @@ xfs_bmapi(
                         * A wasdelay extent has been initialized, so
                         * shouldn't be flagged as unwritten.
                         */
-                       if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
+                       if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
                                if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
                                        got.br_state = XFS_EXT_UNWRITTEN;
                        }
                        error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
-                               firstblock, flist, &tmp_logflags, whichfork,
-                               (flags & XFS_BMAPI_RSVBLOCKS));
+                               firstblock, flist, &tmp_logflags, delta,
+                               whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
                        logflags |= tmp_logflags;
                        if (error)
                                goto error0;
@@ -4984,8 +5156,8 @@ xfs_bmapi(
                        }
                        mval->br_state = XFS_EXT_NORM;
                        error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
-                               firstblock, flist, &tmp_logflags, whichfork,
-                               (flags & XFS_BMAPI_RSVBLOCKS));
+                               firstblock, flist, &tmp_logflags, delta,
+                               whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
                        logflags |= tmp_logflags;
                        if (error)
                                goto error0;
@@ -5049,10 +5221,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;
@@ -5074,7 +5246,14 @@ xfs_bmapi(
        ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
               XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
        error = 0;
-
+       if (delta && delta->xed_startoff != NULLFILEOFF) {
+               /* A change was actually made.
+                * Note that delta->xed_blockount is an offset at this
+                * point and needs to be converted to a block count.
+                */
+               ASSERT(delta->xed_blockcount > delta->xed_startoff);
+               delta->xed_blockcount -= delta->xed_startoff;
+       }
 error0:
        /*
         * Log everything.  Do this after conversion, there's no point in
@@ -5186,12 +5365,14 @@ xfs_bunmapi(
        xfs_fsblock_t           *firstblock,    /* first allocated block
                                                   controls a.g. for allocs */
        xfs_bmap_free_t         *flist,         /* i/o: list extents to free */
+       xfs_extdelta_t          *delta,         /* o: change made to incore
+                                                  extents */
        int                     *done)          /* set if not done yet */
 {
        xfs_btree_cur_t         *cur;           /* bmap btree cursor */
        xfs_bmbt_irec_t         del;            /* extent being deleted */
        int                     eof;            /* is deleting at eof */
-       xfs_bmbt_rec_t          *ep;            /* extent record pointer */
+       xfs_bmbt_rec_host_t     *ep;            /* extent record pointer */
        int                     error;          /* error return value */
        xfs_extnum_t            extno;          /* extent number in list */
        xfs_bmbt_irec_t         got;            /* current extent record */
@@ -5243,6 +5424,10 @@ xfs_bunmapi(
        bno = start + len - 1;
        ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
                &prev);
+       if (delta) {
+               delta->xed_startoff = NULLFILEOFF;
+               delta->xed_blockcount = 0;
+       }
        /*
         * Check to see if the given block number is past the end of the
         * file, back up to the last block if so...
@@ -5309,7 +5494,7 @@ xfs_bunmapi(
                         * get rid of part of a realtime extent.
                         */
                        if (del.br_state == XFS_EXT_UNWRITTEN ||
-                           !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
+                           !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
                                /*
                                 * This piece is unwritten, or we're not
                                 * using unwritten extents.  Skip over it.
@@ -5341,7 +5526,8 @@ xfs_bunmapi(
                        }
                        del.br_state = XFS_EXT_UNWRITTEN;
                        error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
-                               firstblock, flist, &logflags, XFS_DATA_FORK, 0);
+                               firstblock, flist, &logflags, delta,
+                               XFS_DATA_FORK, 0);
                        if (error)
                                goto error0;
                        goto nodelete;
@@ -5360,7 +5546,7 @@ xfs_bunmapi(
                        } else if ((del.br_startoff == start &&
                                    (del.br_state == XFS_EXT_UNWRITTEN ||
                                     xfs_trans_get_block_res(tp) == 0)) ||
-                                  !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
+                                  !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
                                /*
                                 * Can't make it unwritten.  There isn't
                                 * a full extent here so just skip it.
@@ -5395,7 +5581,7 @@ xfs_bunmapi(
                                prev.br_state = XFS_EXT_UNWRITTEN;
                                error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
                                        &prev, firstblock, flist, &logflags,
-                                       XFS_DATA_FORK, 0);
+                                       delta, XFS_DATA_FORK, 0);
                                if (error)
                                        goto error0;
                                goto nodelete;
@@ -5404,7 +5590,7 @@ xfs_bunmapi(
                                del.br_state = XFS_EXT_UNWRITTEN;
                                error = xfs_bmap_add_extent(ip, lastx, &cur,
                                        &del, firstblock, flist, &logflags,
-                                       XFS_DATA_FORK, 0);
+                                       delta, XFS_DATA_FORK, 0);
                                if (error)
                                        goto error0;
                                goto nodelete;
@@ -5419,13 +5605,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);
@@ -5457,7 +5643,7 @@ xfs_bunmapi(
                        goto error0;
                }
                error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
-                       &tmp_logflags, whichfork, rsvd);
+                               &tmp_logflags, delta, whichfork, rsvd);
                logflags |= tmp_logflags;
                if (error)
                        goto error0;
@@ -5514,6 +5700,14 @@ nodelete:
        ASSERT(ifp->if_ext_max ==
               XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
        error = 0;
+       if (delta && delta->xed_startoff != NULLFILEOFF) {
+               /* A change was actually made.
+                * Note that delta->xed_blockount is an offset at this
+                * point and needs to be converted to a block count.
+                */
+               ASSERT(delta->xed_blockcount > delta->xed_startoff);
+               delta->xed_blockcount -= delta->xed_startoff;
+       }
 error0:
        /*
         * Log everything.  Do this after conversion, there's no point in
@@ -5543,11 +5737,44 @@ error0:
 }
 
 /*
+ * returns 1 for success, 0 if we failed to map the extent.
+ */
+STATIC int
+xfs_getbmapx_fix_eof_hole(
+       xfs_inode_t             *ip,            /* xfs incore inode pointer */
+       struct getbmap          *out,           /* output structure */
+       int                     prealloced,     /* this is a file with
+                                               * preallocated data space */
+       __int64_t               end,            /* last block requested */
+       xfs_fsblock_t           startblock)
+{
+       __int64_t               fixlen;
+       xfs_mount_t             *mp;            /* file system mount point */
+
+       if (startblock == HOLESTARTBLOCK) {
+               mp = ip->i_mount;
+               out->bmv_block = -1;
+               fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
+               fixlen -= out->bmv_offset;
+               if (prealloced && out->bmv_offset + out->bmv_length == end) {
+                       /* Came to hole at EOF. Trim it. */
+                       if (fixlen <= 0)
+                               return 0;
+                       out->bmv_length = fixlen;
+               }
+       } else {
+               out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
+       }
+
+       return 1;
+}
+
+/*
  * Fcntl interface to xfs_bmapi.
  */
 int                                            /* error code */
 xfs_getbmap(
-       bhv_desc_t              *bdp,           /* XFS behavior descriptor*/
+       xfs_inode_t             *ip,
        struct getbmap          *bmv,           /* user bmap structure */
        void                    __user *ap,     /* pointer to user's array */
        int                     interface)      /* interface flags */
@@ -5556,8 +5783,6 @@ xfs_getbmap(
        int                     error;          /* return value */
        __int64_t               fixlen;         /* length for -1 case */
        int                     i;              /* extent number */
-       xfs_inode_t             *ip;            /* xfs incore inode pointer */
-       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 */
@@ -5574,8 +5799,6 @@ xfs_getbmap(
        int                     bmapi_flags;    /* flags for xfs_bmapi */
        __int32_t               oflags;         /* getbmapx bmv_oflags field */
 
-       vp = BHV_TO_VNODE(bdp);
-       ip = XFS_BHVTOI(bdp);
        mp = ip->i_mount;
 
        whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
@@ -5594,11 +5817,10 @@ xfs_getbmap(
         *      could misinterpret holes in a DMAPI file as true holes,
         *      when in fact they may represent offline user data.
         */
-       if (   (interface & BMV_IF_NO_DMAPI_READ) == 0
-           && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
-           && whichfork == XFS_DATA_FORK) {
-
-               error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
+       if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
+           DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
+           whichfork == XFS_DATA_FORK) {
+               error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
                if (error)
                        return XFS_ERROR(error);
        }
@@ -5621,14 +5843,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;
@@ -5652,9 +5873,15 @@ 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 = xfs_flush_pages(ip, (xfs_off_t)0,
+                                              -1, 0, FI_REMAPF);
+               if (error) {
+                       xfs_iunlock(ip, XFS_IOLOCK_SHARED);
+               return error;
+               }
        }
 
        ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
@@ -5690,7 +5917,8 @@ xfs_getbmap(
                nmap = (nexleft > subnex) ? subnex : nexleft;
                error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
                                  XFS_BB_TO_FSB(mp, bmv->bmv_length),
-                                 bmapi_flags, NULL, 0, map, &nmap, NULL);
+                                 bmapi_flags, NULL, 0, map, &nmap,
+                                 NULL, NULL);
                if (error)
                        goto unlock_and_return;
                ASSERT(nmap <= subnex);
@@ -5703,18 +5931,15 @@ xfs_getbmap(
                        out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
                        ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
                         if (map[i].br_startblock == HOLESTARTBLOCK &&
-                           ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
-                             whichfork == XFS_ATTR_FORK )) {
-                                /*
-                                 * came to hole at end of file or the end of
-                                   attribute fork
-                                 */
+                           whichfork == XFS_ATTR_FORK) {
+                               /* came to the end of attribute fork */
                                goto unlock_and_return;
                        } else {
-                               out.bmv_block =
-                                   (map[i].br_startblock == HOLESTARTBLOCK) ?
-                                       -1 :
-                                       XFS_FSB_TO_DB(ip, map[i].br_startblock);
+                               if (!xfs_getbmapx_fix_eof_hole(ip, &out,
+                                                       prealloced, bmvend,
+                                                       map[i].br_startblock)) {
+                                       goto unlock_and_return;
+                               }
 
                                /* return either getbmap/getbmapx structure. */
                                if (interface & BMV_IF_EXTENDED) {
@@ -5753,7 +5978,7 @@ unlock_and_return:
        xfs_iunlock_map_shared(ip, lock);
        xfs_iunlock(ip, XFS_IOLOCK_SHARED);
 
-       kmem_free(map, subnex * sizeof(*map));
+       kmem_free(map);
 
        return error;
 }
@@ -5773,7 +5998,7 @@ xfs_bmap_isaeof(
 {
        int             error;          /* error return value */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
-       xfs_bmbt_rec_t  *lastrec;       /* extent record pointer */
+       xfs_bmbt_rec_host_t *lastrec;   /* extent record pointer */
        xfs_extnum_t    nextents;       /* number of file extents */
        xfs_bmbt_irec_t s;              /* expanded extent record */
 
@@ -5817,7 +6042,7 @@ xfs_bmap_eof(
        xfs_fsblock_t   blockcount;     /* extent block count */
        int             error;          /* error return value */
        xfs_ifork_t     *ifp;           /* inode fork pointer */
-       xfs_bmbt_rec_t  *lastrec;       /* extent record pointer */
+       xfs_bmbt_rec_host_t *lastrec;   /* extent record pointer */
        xfs_extnum_t    nextents;       /* number of file extents */
        xfs_fileoff_t   startoff;       /* extent starting file offset */
 
@@ -5842,32 +6067,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(
@@ -5897,7 +6096,7 @@ xfs_bmap_get_bp(
                tp = cur->bc_tp;
                licp = &tp->t_items;
                while (!bp && licp != NULL) {
-                       if (XFS_LIC_ARE_ALL_FREE(licp)) {
+                       if (xfs_lic_are_all_free(licp)) {
                                licp = licp->lic_next;
                                continue;
                        }
@@ -5907,11 +6106,11 @@ xfs_bmap_get_bp(
                                xfs_buf_log_item_t      *bip;
                                xfs_buf_t               *lbp;
 
-                               if (XFS_LIC_ISFREE(licp, i)) {
+                               if (xfs_lic_isfree(licp, i)) {
                                        continue;
                                }
 
-                               lidp = XFS_LIC_SLOT(licp, i);
+                               lidp = xfs_lic_slot(licp, i);
                                lip = lidp->lid_item;
                                if (lip->li_type != XFS_LI_BUF)
                                        continue;
@@ -5938,7 +6137,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);
@@ -5950,8 +6149,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) {
@@ -5966,23 +6164,21 @@ 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));
+                                       __func__, j, i,
+                                       (unsigned long long)be64_to_cpu(*thispa));
                                panic("%s: ptrs are equal in node\n",
-                                       __FUNCTION__);
+                                       __func__);
                        }
                }
        }
@@ -6007,9 +6203,9 @@ 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          last = {0, 0}; /* last extent in prev block */
        xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
        int                     bp_release = 0;
 
@@ -6028,10 +6224,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.
@@ -6060,10 +6258,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);
@@ -6078,7 +6275,6 @@ xfs_bmap_check_leaf_extents(
        /*
         * Loop over all leaf nodes checking that all extents are in the right order.
         */
-       lastp = NULL;
        for (;;) {
                xfs_fsblock_t   nextbno;
                xfs_extnum_t    num_recs;
@@ -6098,21 +6294,17 @@ 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);
+               if (i) {
+                       xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
+               }
                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]);
-                       if (lastp) {
-                               xfs_btree_check_rec(XFS_BTNUM_BMAP,
-                                       (void *)lastp, (void *)ep);
-                       }
-                       xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
-                               (void *)(nextp));
-                       lastp = ep;
+                       nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
+                       xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
                        ep = nextp;
                }
 
+               last = *ep;
                i += num_recs;
                if (bp_release) {
                        bp_release = 0;
@@ -6143,13 +6335,13 @@ xfs_bmap_check_leaf_extents(
        return;
 
 error0:
-       cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
+       cmn_err(CE_WARN, "%s: at error0", __func__);
        if (bp_release)
                xfs_trans_brelse(NULL, bp);
 error_norelse:
        cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
-               __FUNCTION__, i);
-       panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
+               __func__, i);
+       panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
        return;
 }
 #endif
@@ -6169,7 +6361,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;
@@ -6192,10 +6384,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,
@@ -6210,7 +6402,7 @@ xfs_bmap_count_blocks(
  * Recursively walks each level of a btree
  * to count total fsblocks is use.
  */
-int                                     /* error */
+STATIC int                                     /* error */
 xfs_bmap_count_tree(
        xfs_mount_t     *mp,            /* file system mount point */
        xfs_trans_t     *tp,            /* transaction pointer */
@@ -6222,7 +6414,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;
@@ -6247,9 +6439,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);
@@ -6263,8 +6454,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);
@@ -6287,7 +6478,7 @@ xfs_bmap_count_tree(
 /*
  * Count leaf blocks given a range of extent records.
  */
-int
+STATIC int
 xfs_bmap_count_leaves(
        xfs_ifork_t             *ifp,
        xfs_extnum_t            idx,
@@ -6295,10 +6486,9 @@ xfs_bmap_count_leaves(
        int                     *count)
 {
        int             b;
-       xfs_bmbt_rec_t  *frp;
 
        for (b = 0; b < numrecs; b++) {
-               frp = xfs_iext_get_ext(ifp, idx + b);
+               xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
                *count += xfs_bmbt_get_blockcount(frp);
        }
        return 0;
@@ -6308,10 +6498,8 @@ xfs_bmap_count_leaves(
  * Count leaf blocks given a range of extent records originally
  * in btree format.
  */
-int
+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,
@@ -6321,8 +6509,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;