]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/ocfs2/alloc.c
Merge branch 'drm-next' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / alloc.c
index 06ea7913c134f3fbe2f110ddae80757ce6d95ef6..0cc2deb9394c02e1a961c0a1366ae5737b8f32bc 100644 (file)
 
 #include "buffer_head_io.h"
 
+
 /*
- * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
- * the b-tree operations in ocfs2. Now all the b-tree operations are not
- * limited to ocfs2_dinode only. Any data which need to allocate clusters
- * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
- * and operation.
+ * Operations for a specific extent tree type.
  *
- * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
- * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
- * functions.
- * ocfs2_extent_tree_operations abstract the normal operations we do for
- * the root of extent b-tree.
+ * To implement an on-disk btree (extent tree) type in ocfs2, add
+ * an ocfs2_extent_tree_operations structure and the matching
+ * ocfs2_init_<thingy>_extent_tree() function.  That's pretty much it
+ * for the allocation portion of the extent tree.
  */
-struct ocfs2_extent_tree;
-
 struct ocfs2_extent_tree_operations {
-       void (*set_last_eb_blk) (struct ocfs2_extent_tree *et, u64 blkno);
-       u64 (*get_last_eb_blk) (struct ocfs2_extent_tree *et);
-       void (*update_clusters) (struct inode *inode,
-                                struct ocfs2_extent_tree *et,
-                                u32 new_clusters);
-       int (*sanity_check) (struct inode *inode, struct ocfs2_extent_tree *et);
+       /*
+        * last_eb_blk is the block number of the right most leaf extent
+        * block.  Most on-disk structures containing an extent tree store
+        * this value for fast access.  The ->eo_set_last_eb_blk() and
+        * ->eo_get_last_eb_blk() operations access this value.  They are
+        *  both required.
+        */
+       void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
+                                  u64 blkno);
+       u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
+
+       /*
+        * The on-disk structure usually keeps track of how many total
+        * clusters are stored in this extent tree.  This function updates
+        * that value.  new_clusters is the delta, and must be
+        * added to the total.  Required.
+        */
+       void (*eo_update_clusters)(struct inode *inode,
+                                  struct ocfs2_extent_tree *et,
+                                  u32 new_clusters);
+
+       /*
+        * If ->eo_insert_check() exists, it is called before rec is
+        * inserted into the extent tree.  It is optional.
+        */
+       int (*eo_insert_check)(struct inode *inode,
+                              struct ocfs2_extent_tree *et,
+                              struct ocfs2_extent_rec *rec);
+       int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
+
+       /*
+        * --------------------------------------------------------------
+        * The remaining are internal to ocfs2_extent_tree and don't have
+        * accessor functions
+        */
+
+       /*
+        * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
+        * It is required.
+        */
+       void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
+
+       /*
+        * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
+        * it exists.  If it does not, et->et_max_leaf_clusters is set
+        * to 0 (unlimited).  Optional.
+        */
+       void (*eo_fill_max_leaf_clusters)(struct inode *inode,
+                                         struct ocfs2_extent_tree *et);
 };
 
-struct ocfs2_extent_tree {
-       enum ocfs2_extent_tree_type type;
-       struct ocfs2_extent_tree_operations *eops;
-       struct buffer_head *root_bh;
-       struct ocfs2_extent_list *root_el;
-       void *private;
+
+/*
+ * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
+ * in the methods.
+ */
+static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
+static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
+                                        u64 blkno);
+static void ocfs2_dinode_update_clusters(struct inode *inode,
+                                        struct ocfs2_extent_tree *et,
+                                        u32 clusters);
+static int ocfs2_dinode_insert_check(struct inode *inode,
+                                    struct ocfs2_extent_tree *et,
+                                    struct ocfs2_extent_rec *rec);
+static int ocfs2_dinode_sanity_check(struct inode *inode,
+                                    struct ocfs2_extent_tree *et);
+static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
+static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
+       .eo_set_last_eb_blk     = ocfs2_dinode_set_last_eb_blk,
+       .eo_get_last_eb_blk     = ocfs2_dinode_get_last_eb_blk,
+       .eo_update_clusters     = ocfs2_dinode_update_clusters,
+       .eo_insert_check        = ocfs2_dinode_insert_check,
+       .eo_sanity_check        = ocfs2_dinode_sanity_check,
+       .eo_fill_root_el        = ocfs2_dinode_fill_root_el,
 };
 
 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
                                         u64 blkno)
 {
-       struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data;
+       struct ocfs2_dinode *di = et->et_object;
 
-       BUG_ON(et->type != OCFS2_DINODE_EXTENT);
+       BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
        di->i_last_eb_blk = cpu_to_le64(blkno);
 }
 
 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
 {
-       struct ocfs2_dinode *di = (struct ocfs2_dinode *)et->root_bh->b_data;
+       struct ocfs2_dinode *di = et->et_object;
 
-       BUG_ON(et->type != OCFS2_DINODE_EXTENT);
+       BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
        return le64_to_cpu(di->i_last_eb_blk);
 }
 
@@ -102,8 +157,7 @@ static void ocfs2_dinode_update_clusters(struct inode *inode,
                                         struct ocfs2_extent_tree *et,
                                         u32 clusters)
 {
-       struct ocfs2_dinode *di =
-                       (struct ocfs2_dinode *)et->root_bh->b_data;
+       struct ocfs2_dinode *di = et->et_object;
 
        le32_add_cpu(&di->i_clusters, clusters);
        spin_lock(&OCFS2_I(inode)->ip_lock);
@@ -111,15 +165,34 @@ static void ocfs2_dinode_update_clusters(struct inode *inode,
        spin_unlock(&OCFS2_I(inode)->ip_lock);
 }
 
+static int ocfs2_dinode_insert_check(struct inode *inode,
+                                    struct ocfs2_extent_tree *et,
+                                    struct ocfs2_extent_rec *rec)
+{
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+
+       BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
+       mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
+                       (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
+                       "Device %s, asking for sparse allocation: inode %llu, "
+                       "cpos %u, clusters %u\n",
+                       osb->dev_str,
+                       (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                       rec->e_cpos,
+                       OCFS2_I(inode)->ip_clusters);
+
+       return 0;
+}
+
 static int ocfs2_dinode_sanity_check(struct inode *inode,
                                     struct ocfs2_extent_tree *et)
 {
        int ret = 0;
        struct ocfs2_dinode *di;
 
-       BUG_ON(et->type != OCFS2_DINODE_EXTENT);
+       BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
 
-       di = (struct ocfs2_dinode *)et->root_bh->b_data;
+       di = et->et_object;
        if (!OCFS2_IS_VALID_DINODE(di)) {
                ret = -EIO;
                ocfs2_error(inode->i_sb,
@@ -130,18 +203,26 @@ static int ocfs2_dinode_sanity_check(struct inode *inode,
        return ret;
 }
 
-static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
-       .set_last_eb_blk        = ocfs2_dinode_set_last_eb_blk,
-       .get_last_eb_blk        = ocfs2_dinode_get_last_eb_blk,
-       .update_clusters        = ocfs2_dinode_update_clusters,
-       .sanity_check           = ocfs2_dinode_sanity_check,
-};
+static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_dinode *di = et->et_object;
+
+       et->et_root_el = &di->id2.i_list;
+}
+
+
+static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_xattr_value_root *xv = et->et_object;
+
+       et->et_root_el = &xv->xr_list;
+}
 
 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
                                              u64 blkno)
 {
        struct ocfs2_xattr_value_root *xv =
-               (struct ocfs2_xattr_value_root *)et->private;
+               (struct ocfs2_xattr_value_root *)et->et_object;
 
        xv->xr_last_eb_blk = cpu_to_le64(blkno);
 }
@@ -149,7 +230,7 @@ static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
 {
        struct ocfs2_xattr_value_root *xv =
-               (struct ocfs2_xattr_value_root *) et->private;
+               (struct ocfs2_xattr_value_root *) et->et_object;
 
        return le64_to_cpu(xv->xr_last_eb_blk);
 }
@@ -159,29 +240,37 @@ static void ocfs2_xattr_value_update_clusters(struct inode *inode,
                                              u32 clusters)
 {
        struct ocfs2_xattr_value_root *xv =
-               (struct ocfs2_xattr_value_root *)et->private;
+               (struct ocfs2_xattr_value_root *)et->et_object;
 
        le32_add_cpu(&xv->xr_clusters, clusters);
 }
 
-static int ocfs2_xattr_value_sanity_check(struct inode *inode,
-                                         struct ocfs2_extent_tree *et)
+static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
+       .eo_set_last_eb_blk     = ocfs2_xattr_value_set_last_eb_blk,
+       .eo_get_last_eb_blk     = ocfs2_xattr_value_get_last_eb_blk,
+       .eo_update_clusters     = ocfs2_xattr_value_update_clusters,
+       .eo_fill_root_el        = ocfs2_xattr_value_fill_root_el,
+};
+
+static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
 {
-       return 0;
+       struct ocfs2_xattr_block *xb = et->et_object;
+
+       et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
 }
 
-static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
-       .set_last_eb_blk        = ocfs2_xattr_value_set_last_eb_blk,
-       .get_last_eb_blk        = ocfs2_xattr_value_get_last_eb_blk,
-       .update_clusters        = ocfs2_xattr_value_update_clusters,
-       .sanity_check           = ocfs2_xattr_value_sanity_check,
-};
+static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
+                                                   struct ocfs2_extent_tree *et)
+{
+       et->et_max_leaf_clusters =
+               ocfs2_clusters_for_bytes(inode->i_sb,
+                                        OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
+}
 
 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
                                             u64 blkno)
 {
-       struct ocfs2_xattr_block *xb =
-               (struct ocfs2_xattr_block *) et->root_bh->b_data;
+       struct ocfs2_xattr_block *xb = et->et_object;
        struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
 
        xt->xt_last_eb_blk = cpu_to_le64(blkno);
@@ -189,8 +278,7 @@ static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
 
 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
 {
-       struct ocfs2_xattr_block *xb =
-               (struct ocfs2_xattr_block *) et->root_bh->b_data;
+       struct ocfs2_xattr_block *xb = et->et_object;
        struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
 
        return le64_to_cpu(xt->xt_last_eb_blk);
@@ -200,83 +288,99 @@ static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
                                             struct ocfs2_extent_tree *et,
                                             u32 clusters)
 {
-       struct ocfs2_xattr_block *xb =
-                       (struct ocfs2_xattr_block *)et->root_bh->b_data;
+       struct ocfs2_xattr_block *xb = et->et_object;
 
        le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
 }
 
-static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
-                                        struct ocfs2_extent_tree *et)
-{
-       return 0;
-}
-
 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
-       .set_last_eb_blk        = ocfs2_xattr_tree_set_last_eb_blk,
-       .get_last_eb_blk        = ocfs2_xattr_tree_get_last_eb_blk,
-       .update_clusters        = ocfs2_xattr_tree_update_clusters,
-       .sanity_check           = ocfs2_xattr_tree_sanity_check,
+       .eo_set_last_eb_blk     = ocfs2_xattr_tree_set_last_eb_blk,
+       .eo_get_last_eb_blk     = ocfs2_xattr_tree_get_last_eb_blk,
+       .eo_update_clusters     = ocfs2_xattr_tree_update_clusters,
+       .eo_fill_root_el        = ocfs2_xattr_tree_fill_root_el,
+       .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
 };
 
-static struct ocfs2_extent_tree*
-        ocfs2_new_extent_tree(struct buffer_head *bh,
-                              enum ocfs2_extent_tree_type et_type,
-                              void *private)
+static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
+                                    struct inode *inode,
+                                    struct buffer_head *bh,
+                                    void *obj,
+                                    struct ocfs2_extent_tree_operations *ops)
 {
-       struct ocfs2_extent_tree *et;
+       et->et_ops = ops;
+       et->et_root_bh = bh;
+       if (!obj)
+               obj = (void *)bh->b_data;
+       et->et_object = obj;
+
+       et->et_ops->eo_fill_root_el(et);
+       if (!et->et_ops->eo_fill_max_leaf_clusters)
+               et->et_max_leaf_clusters = 0;
+       else
+               et->et_ops->eo_fill_max_leaf_clusters(inode, et);
+}
 
-       et = kzalloc(sizeof(*et), GFP_NOFS);
-       if (!et)
-               return NULL;
+void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
+                                  struct inode *inode,
+                                  struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
+}
 
-       et->type = et_type;
-       get_bh(bh);
-       et->root_bh = bh;
-       et->private = private;
+void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
+                                      struct inode *inode,
+                                      struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, inode, bh, NULL,
+                                &ocfs2_xattr_tree_et_ops);
+}
 
-       if (et_type == OCFS2_DINODE_EXTENT) {
-               et->root_el = &((struct ocfs2_dinode *)bh->b_data)->id2.i_list;
-               et->eops = &ocfs2_dinode_et_ops;
-       } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
-               struct ocfs2_xattr_value_root *xv =
-                       (struct ocfs2_xattr_value_root *) private;
-               et->root_el = &xv->xr_list;
-               et->eops = &ocfs2_xattr_et_ops;
-       } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
-               struct ocfs2_xattr_block *xb =
-                       (struct ocfs2_xattr_block *)bh->b_data;
-               et->root_el = &xb->xb_attrs.xb_root.xt_list;
-               et->eops = &ocfs2_xattr_tree_et_ops;
-       }
+void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
+                                       struct inode *inode,
+                                       struct buffer_head *bh,
+                                       struct ocfs2_xattr_value_root *xv)
+{
+       __ocfs2_init_extent_tree(et, inode, bh, xv,
+                                &ocfs2_xattr_value_et_ops);
+}
 
-       return et;
+static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
+                                           u64 new_last_eb_blk)
+{
+       et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
 }
 
-static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et)
+static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
 {
-       if (et) {
-               brelse(et->root_bh);
-               kfree(et);
-       }
+       return et->et_ops->eo_get_last_eb_blk(et);
 }
 
-static inline void ocfs2_set_last_eb_blk(struct ocfs2_extent_tree *et,
-                                        u64 new_last_eb_blk)
+static inline void ocfs2_et_update_clusters(struct inode *inode,
+                                           struct ocfs2_extent_tree *et,
+                                           u32 clusters)
 {
-       et->eops->set_last_eb_blk(et, new_last_eb_blk);
+       et->et_ops->eo_update_clusters(inode, et, clusters);
 }
 
-static inline u64 ocfs2_get_last_eb_blk(struct ocfs2_extent_tree *et)
+static inline int ocfs2_et_insert_check(struct inode *inode,
+                                       struct ocfs2_extent_tree *et,
+                                       struct ocfs2_extent_rec *rec)
 {
-       return et->eops->get_last_eb_blk(et);
+       int ret = 0;
+
+       if (et->et_ops->eo_insert_check)
+               ret = et->et_ops->eo_insert_check(inode, et, rec);
+       return ret;
 }
 
-static inline void ocfs2_update_clusters(struct inode *inode,
-                                        struct ocfs2_extent_tree *et,
-                                        u32 clusters)
+static inline int ocfs2_et_sanity_check(struct inode *inode,
+                                       struct ocfs2_extent_tree *et)
 {
-       et->eops->update_clusters(inode, et, clusters);
+       int ret = 0;
+
+       if (et->et_ops->eo_sanity_check)
+               ret = et->et_ops->eo_sanity_check(inode, et);
+       return ret;
 }
 
 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
@@ -587,9 +691,7 @@ struct ocfs2_merge_ctxt {
  */
 int ocfs2_num_free_extents(struct ocfs2_super *osb,
                           struct inode *inode,
-                          struct buffer_head *root_bh,
-                          enum ocfs2_extent_tree_type type,
-                          void *private)
+                          struct ocfs2_extent_tree *et)
 {
        int retval;
        struct ocfs2_extent_list *el = NULL;
@@ -599,35 +701,12 @@ int ocfs2_num_free_extents(struct ocfs2_super *osb,
 
        mlog_entry_void();
 
-       if (type == OCFS2_DINODE_EXTENT) {
-               struct ocfs2_dinode *fe =
-                               (struct ocfs2_dinode *)root_bh->b_data;
-               if (!OCFS2_IS_VALID_DINODE(fe)) {
-                       OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
-                       retval = -EIO;
-                       goto bail;
-               }
-
-               if (fe->i_last_eb_blk)
-                       last_eb_blk = le64_to_cpu(fe->i_last_eb_blk);
-               el = &fe->id2.i_list;
-       } else if (type == OCFS2_XATTR_VALUE_EXTENT) {
-               struct ocfs2_xattr_value_root *xv =
-                       (struct ocfs2_xattr_value_root *) private;
-
-               last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk);
-               el = &xv->xr_list;
-       } else if (type == OCFS2_XATTR_TREE_EXTENT) {
-               struct ocfs2_xattr_block *xb =
-                       (struct ocfs2_xattr_block *)root_bh->b_data;
-
-               last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk);
-               el = &xb->xb_attrs.xb_root.xt_list;
-       }
+       el = et->et_root_el;
+       last_eb_blk = ocfs2_et_get_last_eb_blk(et);
 
        if (last_eb_blk) {
-               retval = ocfs2_read_block(osb, last_eb_blk,
-                                         &eb_bh, OCFS2_BH_CACHED, inode);
+               retval = ocfs2_read_block(inode, last_eb_blk,
+                                         &eb_bh);
                if (retval < 0) {
                        mlog_errno(retval);
                        goto bail;
@@ -640,8 +719,7 @@ int ocfs2_num_free_extents(struct ocfs2_super *osb,
 
        retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
 bail:
-       if (eb_bh)
-               brelse(eb_bh);
+       brelse(eb_bh);
 
        mlog_exit(retval);
        return retval;
@@ -727,8 +805,7 @@ static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
 bail:
        if (status < 0) {
                for(i = 0; i < wanted; i++) {
-                       if (bhs[i])
-                               brelse(bhs[i]);
+                       brelse(bhs[i]);
                        bhs[i] = NULL;
                }
        }
@@ -794,7 +871,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
                eb = (struct ocfs2_extent_block *) eb_bh->b_data;
                el = &eb->h_list;
        } else
-               el = et->root_el;
+               el = et->et_root_el;
 
        /* we never add a branch to a leaf. */
        BUG_ON(!el->l_tree_depth);
@@ -884,7 +961,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
                mlog_errno(status);
                goto bail;
        }
-       status = ocfs2_journal_access(handle, inode, et->root_bh,
+       status = ocfs2_journal_access(handle, inode, et->et_root_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
@@ -909,7 +986,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
 
        /* fe needs a new last extent block pointer, as does the
         * next_leaf on the previously last-extent-block. */
-       ocfs2_set_last_eb_blk(et, new_last_eb_blk);
+       ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
 
        eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
        eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
@@ -917,7 +994,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
        status = ocfs2_journal_dirty(handle, *last_eb_bh);
        if (status < 0)
                mlog_errno(status);
-       status = ocfs2_journal_dirty(handle, et->root_bh);
+       status = ocfs2_journal_dirty(handle, et->et_root_bh);
        if (status < 0)
                mlog_errno(status);
        if (eb_bh) {
@@ -938,8 +1015,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
 bail:
        if (new_eb_bhs) {
                for (i = 0; i < new_blocks; i++)
-                       if (new_eb_bhs[i])
-                               brelse(new_eb_bhs[i]);
+                       brelse(new_eb_bhs[i]);
                kfree(new_eb_bhs);
        }
 
@@ -983,7 +1059,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
        }
 
        eb_el = &eb->h_list;
-       root_el = et->root_el;
+       root_el = et->et_root_el;
 
        status = ocfs2_journal_access(handle, inode, new_eb_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
@@ -1004,7 +1080,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
                goto bail;
        }
 
-       status = ocfs2_journal_access(handle, inode, et->root_bh,
+       status = ocfs2_journal_access(handle, inode, et->et_root_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
@@ -1025,9 +1101,9 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
        /* If this is our 1st tree depth shift, then last_eb_blk
         * becomes the allocated extent block */
        if (root_el->l_tree_depth == cpu_to_le16(1))
-               ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
+               ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
 
-       status = ocfs2_journal_dirty(handle, et->root_bh);
+       status = ocfs2_journal_dirty(handle, et->et_root_bh);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -1037,8 +1113,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
        new_eb_bh = NULL;
        status = 0;
 bail:
-       if (new_eb_bh)
-               brelse(new_eb_bh);
+       brelse(new_eb_bh);
 
        mlog_exit(status);
        return status;
@@ -1077,7 +1152,7 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
 
        *target_bh = NULL;
 
-       el = et->root_el;
+       el = et->et_root_el;
 
        while(le16_to_cpu(el->l_tree_depth) > 1) {
                if (le16_to_cpu(el->l_next_free_rec) == 0) {
@@ -1098,13 +1173,10 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
                        goto bail;
                }
 
-               if (bh) {
-                       brelse(bh);
-                       bh = NULL;
-               }
+               brelse(bh);
+               bh = NULL;
 
-               status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
-                                         inode);
+               status = ocfs2_read_block(inode, blkno, &bh);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -1120,8 +1192,7 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
 
                if (le16_to_cpu(el->l_next_free_rec) <
                    le16_to_cpu(el->l_count)) {
-                       if (lowest_bh)
-                               brelse(lowest_bh);
+                       brelse(lowest_bh);
                        lowest_bh = bh;
                        get_bh(lowest_bh);
                }
@@ -1129,14 +1200,13 @@ static int ocfs2_find_branch_target(struct ocfs2_super *osb,
 
        /* If we didn't find one and the fe doesn't have any room,
         * then return '1' */
-       el = et->root_el;
+       el = et->et_root_el;
        if (!lowest_bh && (el->l_next_free_rec == el->l_count))
                status = 1;
 
        *target_bh = lowest_bh;
 bail:
-       if (bh)
-               brelse(bh);
+       brelse(bh);
 
        mlog_exit(status);
        return status;
@@ -1158,7 +1228,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
                           struct ocfs2_alloc_context *meta_ac)
 {
        int ret, shift;
-       struct ocfs2_extent_list *el = et->root_el;
+       struct ocfs2_extent_list *el = et->et_root_el;
        int depth = le16_to_cpu(el->l_tree_depth);
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct buffer_head *bh = NULL;
@@ -1470,8 +1540,7 @@ static int __ocfs2_find_path(struct inode *inode,
 
                brelse(bh);
                bh = NULL;
-               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
-                                      &bh, OCFS2_BH_CACHED, inode);
+               ret = ocfs2_read_block(inode, blkno, &bh);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2423,7 +2492,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
                ocfs2_update_edge_lengths(inode, handle, left_path);
 
                eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
-               ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
+               ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
 
                /*
                 * Removal of the extent in the left leaf was skipped
@@ -2684,7 +2753,7 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
        struct ocfs2_extent_list *el;
 
 
-       ret = et->eops->sanity_check(inode, et);
+       ret = ocfs2_et_sanity_check(inode, et);
        if (ret)
                goto out;
        /*
@@ -2743,7 +2812,7 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
                ocfs2_update_edge_lengths(inode, handle, left_path);
 
                eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
-               ocfs2_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
+               ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
        } else {
                /*
                 * 'path' is also the leftmost path which
@@ -2754,12 +2823,12 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
                 */
                ocfs2_unlink_path(inode, handle, dealloc, path, 1);
 
-               el = et->root_el;
+               el = et->et_root_el;
                el->l_tree_depth = 0;
                el->l_next_free_rec = 0;
                memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
 
-               ocfs2_set_last_eb_blk(et, 0);
+               ocfs2_et_set_last_eb_blk(et, 0);
        }
 
        ocfs2_journal_dirty(handle, path_root_bh(path));
@@ -3887,9 +3956,9 @@ static int ocfs2_do_insert_extent(struct inode *inode,
        struct ocfs2_path *left_path = NULL;
        struct ocfs2_extent_list *el;
 
-       el = et->root_el;
+       el = et->et_root_el;
 
-       ret = ocfs2_journal_access(handle, inode, et->root_bh,
+       ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
                                   OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -3901,7 +3970,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
                goto out_update_clusters;
        }
 
-       right_path = ocfs2_new_path(et->root_bh, et->root_el);
+       right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
        if (!right_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -3951,7 +4020,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
                 * ocfs2_rotate_tree_right() might have extended the
                 * transaction without re-journaling our tree root.
                 */
-               ret = ocfs2_journal_access(handle, inode, et->root_bh,
+               ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
                                           OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
                        mlog_errno(ret);
@@ -3976,10 +4045,10 @@ static int ocfs2_do_insert_extent(struct inode *inode,
 
 out_update_clusters:
        if (type->ins_split == SPLIT_NONE)
-               ocfs2_update_clusters(inode, et,
-                                     le16_to_cpu(insert_rec->e_leaf_clusters));
+               ocfs2_et_update_clusters(inode, et,
+                                        le16_to_cpu(insert_rec->e_leaf_clusters));
 
-       ret = ocfs2_journal_dirty(handle, et->root_bh);
+       ret = ocfs2_journal_dirty(handle, et->et_root_bh);
        if (ret)
                mlog_errno(ret);
 
@@ -4109,7 +4178,8 @@ out:
 static void ocfs2_figure_contig_type(struct inode *inode,
                                     struct ocfs2_insert_type *insert,
                                     struct ocfs2_extent_list *el,
-                                    struct ocfs2_extent_rec *insert_rec)
+                                    struct ocfs2_extent_rec *insert_rec,
+                                    struct ocfs2_extent_tree *et)
 {
        int i;
        enum ocfs2_contig_type contig_type = CONTIG_NONE;
@@ -4125,6 +4195,21 @@ static void ocfs2_figure_contig_type(struct inode *inode,
                }
        }
        insert->ins_contig = contig_type;
+
+       if (insert->ins_contig != CONTIG_NONE) {
+               struct ocfs2_extent_rec *rec =
+                               &el->l_recs[insert->ins_contig_index];
+               unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
+                                  le16_to_cpu(insert_rec->e_leaf_clusters);
+
+               /*
+                * Caller might want us to limit the size of extents, don't
+                * calculate contiguousness if we might exceed that limit.
+                */
+               if (et->et_max_leaf_clusters &&
+                   (len > et->et_max_leaf_clusters))
+                       insert->ins_contig = CONTIG_NONE;
+       }
 }
 
 /*
@@ -4199,7 +4284,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
 
        insert->ins_split = SPLIT_NONE;
 
-       el = et->root_el;
+       el = et->et_root_el;
        insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
 
        if (el->l_tree_depth) {
@@ -4209,9 +4294,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
                 * ocfs2_figure_insert_type() and ocfs2_add_branch()
                 * may want it later.
                 */
-               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
-                                      ocfs2_get_last_eb_blk(et), &bh,
-                                      OCFS2_BH_CACHED, inode);
+               ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et), &bh);
                if (ret) {
                        mlog_exit(ret);
                        goto out;
@@ -4232,12 +4315,12 @@ static int ocfs2_figure_insert_type(struct inode *inode,
                le16_to_cpu(el->l_next_free_rec);
 
        if (!insert->ins_tree_depth) {
-               ocfs2_figure_contig_type(inode, insert, el, insert_rec);
+               ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
                ocfs2_figure_appending_type(insert, el, insert_rec);
                return 0;
        }
 
-       path = ocfs2_new_path(et->root_bh, et->root_el);
+       path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
        if (!path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -4266,7 +4349,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
          *     into two types of appends: simple record append, or a
          *     rotate inside the tail leaf.
         */
-       ocfs2_figure_contig_type(inode, insert, el, insert_rec);
+       ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
 
        /*
         * The insert code isn't quite ready to deal with all cases of
@@ -4287,7 +4370,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
         * the case that we're doing a tail append, so maybe we can
         * take advantage of that information somehow.
         */
-       if (ocfs2_get_last_eb_blk(et) ==
+       if (ocfs2_et_get_last_eb_blk(et) ==
            path_leaf_bh(path)->b_blocknr) {
                /*
                 * Ok, ocfs2_find_path() returned us the rightmost
@@ -4315,16 +4398,15 @@ out:
  *
  * The caller needs to update fe->i_clusters
  */
-static int ocfs2_insert_extent(struct ocfs2_super *osb,
-                              handle_t *handle,
-                              struct inode *inode,
-                              struct buffer_head *root_bh,
-                              u32 cpos,
-                              u64 start_blk,
-                              u32 new_clusters,
-                              u8 flags,
-                              struct ocfs2_alloc_context *meta_ac,
-                              struct ocfs2_extent_tree *et)
+int ocfs2_insert_extent(struct ocfs2_super *osb,
+                       handle_t *handle,
+                       struct inode *inode,
+                       struct ocfs2_extent_tree *et,
+                       u32 cpos,
+                       u64 start_blk,
+                       u32 new_clusters,
+                       u8 flags,
+                       struct ocfs2_alloc_context *meta_ac)
 {
        int status;
        int uninitialized_var(free_records);
@@ -4332,24 +4414,19 @@ static int ocfs2_insert_extent(struct ocfs2_super *osb,
        struct ocfs2_insert_type insert = {0, };
        struct ocfs2_extent_rec rec;
 
-       BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
-
        mlog(0, "add %u clusters at position %u to inode %llu\n",
             new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
 
-       mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
-                       (OCFS2_I(inode)->ip_clusters != cpos),
-                       "Device %s, asking for sparse allocation: inode %llu, "
-                       "cpos %u, clusters %u\n",
-                       osb->dev_str,
-                       (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
-                       OCFS2_I(inode)->ip_clusters);
-
        memset(&rec, 0, sizeof(rec));
        rec.e_cpos = cpu_to_le32(cpos);
        rec.e_blkno = cpu_to_le64(start_blk);
        rec.e_leaf_clusters = cpu_to_le16(new_clusters);
        rec.e_flags = flags;
+       status = ocfs2_et_insert_check(inode, et, &rec);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
 
        status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
                                          &free_records, &insert);
@@ -4378,112 +4455,20 @@ static int ocfs2_insert_extent(struct ocfs2_super *osb,
        status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
        if (status < 0)
                mlog_errno(status);
-       else if (et->type == OCFS2_DINODE_EXTENT)
+       else if (et->et_ops == &ocfs2_dinode_et_ops)
                ocfs2_extent_map_insert_rec(inode, &rec);
 
 bail:
-       if (last_eb_bh)
-               brelse(last_eb_bh);
+       brelse(last_eb_bh);
 
        mlog_exit(status);
        return status;
 }
 
-int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
-                              handle_t *handle,
-                              struct inode *inode,
-                              struct buffer_head *root_bh,
-                              u32 cpos,
-                              u64 start_blk,
-                              u32 new_clusters,
-                              u8 flags,
-                              struct ocfs2_alloc_context *meta_ac)
-{
-       int status;
-       struct ocfs2_extent_tree *et = NULL;
-
-       et = ocfs2_new_extent_tree(root_bh, OCFS2_DINODE_EXTENT, NULL);
-       if (!et) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
-       status = ocfs2_insert_extent(osb, handle, inode, root_bh,
-                                    cpos, start_blk, new_clusters,
-                                    flags, meta_ac, et);
-
-       if (et)
-               ocfs2_free_extent_tree(et);
-bail:
-       return status;
-}
-
-int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
-                                   handle_t *handle,
-                                   struct inode *inode,
-                                   struct buffer_head *root_bh,
-                                   u32 cpos,
-                                   u64 start_blk,
-                                   u32 new_clusters,
-                                   u8 flags,
-                                   struct ocfs2_alloc_context *meta_ac,
-                                   void *private)
-{
-       int status;
-       struct ocfs2_extent_tree *et = NULL;
-
-       et = ocfs2_new_extent_tree(root_bh, OCFS2_XATTR_VALUE_EXTENT, private);
-       if (!et) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
-       status = ocfs2_insert_extent(osb, handle, inode, root_bh,
-                                    cpos, start_blk, new_clusters,
-                                    flags, meta_ac, et);
-
-       if (et)
-               ocfs2_free_extent_tree(et);
-bail:
-       return status;
-}
-
-int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
-                                  handle_t *handle,
-                                  struct inode *inode,
-                                  struct buffer_head *root_bh,
-                                  u32 cpos,
-                                  u64 start_blk,
-                                  u32 new_clusters,
-                                  u8 flags,
-                                  struct ocfs2_alloc_context *meta_ac)
-{
-       int status;
-       struct ocfs2_extent_tree *et = NULL;
-
-       et = ocfs2_new_extent_tree(root_bh, OCFS2_XATTR_TREE_EXTENT, NULL);
-       if (!et) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
-       status = ocfs2_insert_extent(osb, handle, inode, root_bh,
-                                    cpos, start_blk, new_clusters,
-                                    flags, meta_ac, et);
-
-       if (et)
-               ocfs2_free_extent_tree(et);
-bail:
-       return status;
-}
-
 /*
  * Allcate and add clusters into the extent b-tree.
  * The new clusters(clusters_to_add) will be inserted at logical_offset.
- * The extent b-tree's root is root_el and it should be in root_bh, and
+ * The extent b-tree's root is specified by et, and
  * it is not limited to the file storage. Any extent tree can use this
  * function if it implements the proper ocfs2_extent_tree.
  */
@@ -4492,14 +4477,11 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
                                u32 *logical_offset,
                                u32 clusters_to_add,
                                int mark_unwritten,
-                               struct buffer_head *root_bh,
-                               struct ocfs2_extent_list *root_el,
+                               struct ocfs2_extent_tree *et,
                                handle_t *handle,
                                struct ocfs2_alloc_context *data_ac,
                                struct ocfs2_alloc_context *meta_ac,
-                               enum ocfs2_alloc_restarted *reason_ret,
-                               enum ocfs2_extent_tree_type type,
-                               void *private)
+                               enum ocfs2_alloc_restarted *reason_ret)
 {
        int status = 0;
        int free_extents;
@@ -4513,8 +4495,7 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
        if (mark_unwritten)
                flags = OCFS2_EXT_UNWRITTEN;
 
-       free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
-                                             private);
+       free_extents = ocfs2_num_free_extents(osb, inode, et);
        if (free_extents < 0) {
                status = free_extents;
                mlog_errno(status);
@@ -4533,7 +4514,7 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
                goto leave;
        } else if ((!free_extents)
                   && (ocfs2_alloc_context_bits_left(meta_ac)
-                      < ocfs2_extend_meta_needed(root_el))) {
+                      < ocfs2_extend_meta_needed(et->et_root_el))) {
                mlog(0, "filesystem is really fragmented...\n");
                status = -EAGAIN;
                reason = RESTART_META;
@@ -4551,7 +4532,7 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
        BUG_ON(num_bits > clusters_to_add);
 
        /* reserve our write early -- insert_extent may update the inode */
-       status = ocfs2_journal_access(handle, inode, root_bh,
+       status = ocfs2_journal_access(handle, inode, et->et_root_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
@@ -4561,28 +4542,15 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
        block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
        mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
             num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
-       if (type == OCFS2_DINODE_EXTENT)
-               status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
-                                                   *logical_offset, block,
-                                                   num_bits, flags, meta_ac);
-       else if (type == OCFS2_XATTR_TREE_EXTENT)
-               status = ocfs2_xattr_tree_insert_extent(osb, handle,
-                                                       inode, root_bh,
-                                                       *logical_offset,
-                                                       block, num_bits, flags,
-                                                       meta_ac);
-       else
-               status = ocfs2_xattr_value_insert_extent(osb, handle,
-                                                        inode, root_bh,
-                                                        *logical_offset,
-                                                        block, num_bits, flags,
-                                                        meta_ac, private);
+       status = ocfs2_insert_extent(osb, handle, inode, et,
+                                    *logical_offset, block,
+                                    num_bits, flags, meta_ac);
        if (status < 0) {
                mlog_errno(status);
                goto leave;
        }
 
-       status = ocfs2_journal_dirty(handle, root_bh);
+       status = ocfs2_journal_dirty(handle, et->et_root_bh);
        if (status < 0) {
                mlog_errno(status);
                goto leave;
@@ -4650,7 +4618,7 @@ leftright:
         */
        rec = path_leaf_el(path)->l_recs[split_index];
 
-       rightmost_el = et->root_el;
+       rightmost_el = et->et_root_el;
 
        depth = le16_to_cpu(rightmost_el->l_tree_depth);
        if (depth) {
@@ -4792,9 +4760,8 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
        if (path->p_tree_depth) {
                struct ocfs2_extent_block *eb;
 
-               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
-                                      ocfs2_get_last_eb_blk(et),
-                                      &last_eb_bh, OCFS2_BH_CACHED, inode);
+               ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et),
+                                      &last_eb_bh);
                if (ret) {
                        mlog_exit(ret);
                        goto out;
@@ -4853,19 +4820,17 @@ out:
  *
  * The caller is responsible for passing down meta_ac if we'll need it.
  */
-int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
+int ocfs2_mark_extent_written(struct inode *inode,
+                             struct ocfs2_extent_tree *et,
                              handle_t *handle, u32 cpos, u32 len, u32 phys,
                              struct ocfs2_alloc_context *meta_ac,
-                             struct ocfs2_cached_dealloc_ctxt *dealloc,
-                             enum ocfs2_extent_tree_type et_type,
-                             void *private)
+                             struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret, index;
        u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
        struct ocfs2_extent_rec split_rec;
        struct ocfs2_path *left_path = NULL;
        struct ocfs2_extent_list *el;
-       struct ocfs2_extent_tree *et = NULL;
 
        mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
             inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
@@ -4879,21 +4844,17 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
                goto out;
        }
 
-       et = ocfs2_new_extent_tree(root_bh, et_type, private);
-       if (!et) {
-               ret = -ENOMEM;
-               mlog_errno(ret);
-               goto out;
-       }
-
        /*
         * XXX: This should be fixed up so that we just re-insert the
         * next extent records.
+        *
+        * XXX: This is a hack on the extent tree, maybe it should be
+        * an op?
         */
-       if (et_type == OCFS2_DINODE_EXTENT)
+       if (et->et_ops == &ocfs2_dinode_et_ops)
                ocfs2_extent_map_trunc(inode, 0);
 
-       left_path = ocfs2_new_path(et->root_bh, et->root_el);
+       left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
        if (!left_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -4932,8 +4893,6 @@ int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
 
 out:
        ocfs2_free_path(left_path);
-       if (et)
-               ocfs2_free_extent_tree(et);
        return ret;
 }
 
@@ -4959,9 +4918,8 @@ static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
 
        depth = path->p_tree_depth;
        if (depth > 0) {
-               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
-                                      ocfs2_get_last_eb_blk(et),
-                                      &last_eb_bh, OCFS2_BH_CACHED, inode);
+               ret = ocfs2_read_block(inode, ocfs2_et_get_last_eb_blk(et),
+                                      &last_eb_bh);
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -4973,7 +4931,7 @@ static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
                rightmost_el = path_leaf_el(path);
 
        credits += path->p_tree_depth +
-                  ocfs2_extend_meta_needed(et->root_el);
+                  ocfs2_extend_meta_needed(et->et_root_el);
        ret = ocfs2_extend_trans(handle, credits);
        if (ret) {
                mlog_errno(ret);
@@ -5163,30 +5121,21 @@ out:
        return ret;
 }
 
-int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
+int ocfs2_remove_extent(struct inode *inode,
+                       struct ocfs2_extent_tree *et,
                        u32 cpos, u32 len, handle_t *handle,
                        struct ocfs2_alloc_context *meta_ac,
-                       struct ocfs2_cached_dealloc_ctxt *dealloc,
-                       enum ocfs2_extent_tree_type et_type,
-                       void *private)
+                       struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret, index;
        u32 rec_range, trunc_range;
        struct ocfs2_extent_rec *rec;
        struct ocfs2_extent_list *el;
        struct ocfs2_path *path = NULL;
-       struct ocfs2_extent_tree *et = NULL;
-
-       et = ocfs2_new_extent_tree(root_bh, et_type, private);
-       if (!et) {
-               ret = -ENOMEM;
-               mlog_errno(ret);
-               goto out;
-       }
 
        ocfs2_extent_map_trunc(inode, 0);
 
-       path = ocfs2_new_path(et->root_bh, et->root_el);
+       path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
        if (!path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -5303,8 +5252,6 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
 
 out:
        ocfs2_free_path(path);
-       if (et)
-               ocfs2_free_extent_tree(et);
        return ret;
 }
 
@@ -5639,8 +5586,7 @@ static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
                goto bail;
        }
 
-       status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
-                                 OCFS2_BH_CACHED, inode);
+       status = ocfs2_read_block(inode, OCFS2_I(inode)->ip_blkno, &bh);
        if (status < 0) {
                iput(inode);
                mlog_errno(status);
@@ -5715,8 +5661,7 @@ int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
 bail:
        if (tl_inode)
                iput(tl_inode);
-       if (tl_bh)
-               brelse(tl_bh);
+       brelse(tl_bh);
 
        if (status < 0 && (*tl_copy)) {
                kfree(*tl_copy);
@@ -6459,20 +6404,13 @@ bail:
        return status;
 }
 
-static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
+static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
 {
        set_buffer_uptodate(bh);
        mark_buffer_dirty(bh);
        return 0;
 }
 
-static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
-{
-       set_buffer_uptodate(bh);
-       mark_buffer_dirty(bh);
-       return ocfs2_journal_dirty_data(handle, bh);
-}
-
 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
                                     unsigned int from, unsigned int to,
                                     struct page *page, int zero, u64 *phys)
@@ -6491,17 +6429,18 @@ static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
         * here if they aren't - ocfs2_map_page_blocks()
         * might've skipped some
         */
-       if (ocfs2_should_order_data(inode)) {
-               ret = walk_page_buffers(handle,
-                                       page_buffers(page),
-                                       from, to, &partial,
-                                       ocfs2_ordered_zero_func);
-               if (ret < 0)
-                       mlog_errno(ret);
-       } else {
+       ret = walk_page_buffers(handle, page_buffers(page),
+                               from, to, &partial,
+                               ocfs2_zero_func);
+       if (ret < 0)
+               mlog_errno(ret);
+       else if (ocfs2_should_order_data(inode)) {
+               ret = ocfs2_jbd2_file_inode(handle, inode);
+#ifdef CONFIG_OCFS2_COMPAT_JBD
                ret = walk_page_buffers(handle, page_buffers(page),
                                        from, to, &partial,
-                                       ocfs2_writeback_zero_func);
+                                       ocfs2_journal_dirty_data);
+#endif
                if (ret < 0)
                        mlog_errno(ret);
        }
@@ -6723,6 +6662,7 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
        struct ocfs2_alloc_context *data_ac = NULL;
        struct page **pages = NULL;
        loff_t end = osb->s_clustersize;
+       struct ocfs2_extent_tree et;
 
        has_data = i_size_read(inode) ? 1 : 0;
 
@@ -6822,8 +6762,9 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
                 * this proves to be false, we could always re-build
                 * the in-inode data from our pages.
                 */
-               ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
-                                                0, block, 1, 0, NULL);
+               ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
+               ret = ocfs2_insert_extent(osb, handle, inode, &et,
+                                         0, block, 1, 0, NULL);
                if (ret) {
                        mlog_errno(ret);
                        goto out_commit;
@@ -7043,8 +6984,8 @@ int ocfs2_prepare_truncate(struct ocfs2_super *osb,
        ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
 
        if (fe->id2.i_list.l_tree_depth) {
-               status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
-                                         &last_eb_bh, OCFS2_BH_CACHED, inode);
+               status = ocfs2_read_block(inode, le64_to_cpu(fe->i_last_eb_blk),
+                                         &last_eb_bh);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -7157,8 +7098,7 @@ static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
                mlog(ML_NOTICE,
                     "Truncate completion has non-empty dealloc context\n");
 
-       if (tc->tc_last_eb_bh)
-               brelse(tc->tc_last_eb_bh);
+       brelse(tc->tc_last_eb_bh);
 
        kfree(tc);
 }