tree-wide: fix assorted typos all over the place
[safe/jmp/linux-2.6] / fs / ocfs2 / alloc.c
index ace27d1..7c7198a 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/highmem.h>
 #include <linux/swap.h>
+#include <linux/quotaops.h>
 
 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
 #include <cluster/masklog.h>
@@ -36,6 +37,7 @@
 
 #include "alloc.h"
 #include "aops.h"
+#include "blockcheck.h"
 #include "dlmglue.h"
 #include "extent_map.h"
 #include "inode.h"
 #include "file.h"
 #include "super.h"
 #include "uptodate.h"
+#include "xattr.h"
+#include "refcounttree.h"
 
 #include "buffer_head_io.h"
 
+enum ocfs2_contig_type {
+       CONTIG_NONE = 0,
+       CONTIG_LEFT,
+       CONTIG_RIGHT,
+       CONTIG_LEFTRIGHT,
+};
+
+static enum ocfs2_contig_type
+       ocfs2_extent_rec_contig(struct super_block *sb,
+                               struct ocfs2_extent_rec *ext,
+                               struct ocfs2_extent_rec *insert_rec);
 /*
- * 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 ocfs2_extent_tree *et,
+                                  u32 new_clusters);
+
+       /*
+        * If this extent tree is supported by an extent map, insert
+        * a record into the map.
+        */
+       void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
+                                    struct ocfs2_extent_rec *rec);
+
+       /*
+        * If this extent tree is supported by an extent map, truncate the
+        * map to clusters,
+        */
+       void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
+                                      u32 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 ocfs2_extent_tree *et,
+                              struct ocfs2_extent_rec *rec);
+       int (*eo_sanity_check)(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 ocfs2_extent_tree *et);
+
+       /*
+        * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
+        * are contiguous or not. Optional. Don't need to set it if use
+        * ocfs2_extent_rec as the tree leaf.
+        */
+       enum ocfs2_contig_type
+               (*eo_extent_contig)(struct ocfs2_extent_tree *et,
+                                   struct ocfs2_extent_rec *ext,
+                                   struct ocfs2_extent_rec *insert_rec);
 };
 
-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 ocfs2_extent_tree *et,
+                                        u32 clusters);
+static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
+                                          struct ocfs2_extent_rec *rec);
+static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
+                                            u32 clusters);
+static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
+                                    struct ocfs2_extent_rec *rec);
+static int ocfs2_dinode_sanity_check(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_extent_map_insert   = ocfs2_dinode_extent_map_insert,
+       .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
+       .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);
 }
 
-static void ocfs2_dinode_update_clusters(struct inode *inode,
-                                        struct ocfs2_extent_tree *et,
+static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
                                         u32 clusters)
 {
-       struct ocfs2_dinode *di =
-                       (struct ocfs2_dinode *)et->root_bh->b_data;
+       struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
+       struct ocfs2_dinode *di = et->et_object;
 
        le32_add_cpu(&di->i_clusters, clusters);
-       spin_lock(&OCFS2_I(inode)->ip_lock);
-       OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
-       spin_unlock(&OCFS2_I(inode)->ip_lock);
+       spin_lock(&oi->ip_lock);
+       oi->ip_clusters = le32_to_cpu(di->i_clusters);
+       spin_unlock(&oi->ip_lock);
 }
 
-static int ocfs2_dinode_sanity_check(struct inode *inode,
-                                    struct ocfs2_extent_tree *et)
+static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
+                                          struct ocfs2_extent_rec *rec)
 {
-       int ret = 0;
-       struct ocfs2_dinode *di;
+       struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
 
-       BUG_ON(et->type != OCFS2_DINODE_EXTENT);
+       ocfs2_extent_map_insert_rec(inode, rec);
+}
 
-       di = (struct ocfs2_dinode *)et->root_bh->b_data;
-       if (!OCFS2_IS_VALID_DINODE(di)) {
-               ret = -EIO;
-               ocfs2_error(inode->i_sb,
-                       "Inode %llu has invalid path root",
-                       (unsigned long long)OCFS2_I(inode)->ip_blkno);
-       }
+static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
+                                            u32 clusters)
+{
+       struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
 
-       return ret;
+       ocfs2_extent_map_trunc(inode, clusters);
 }
 
-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 int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
+                                    struct ocfs2_extent_rec *rec)
+{
+       struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
+       struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
+
+       BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
+       mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
+                       (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
+                       "Device %s, asking for sparse allocation: inode %llu, "
+                       "cpos %u, clusters %u\n",
+                       osb->dev_str,
+                       (unsigned long long)oi->ip_blkno,
+                       rec->e_cpos, oi->ip_clusters);
+
+       return 0;
+}
+
+static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_dinode *di = et->et_object;
+
+       BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
+       BUG_ON(!OCFS2_IS_VALID_DINODE(di));
+
+       return 0;
+}
+
+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_buf *vb = et->et_object;
+
+       et->et_root_el = &vb->vb_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_buf *vb = et->et_object;
 
-       xv->xr_last_eb_blk = cpu_to_le64(blkno);
+       vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
 }
 
 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_buf *vb = et->et_object;
 
-       return le64_to_cpu(xv->xr_last_eb_blk);
+       return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
 }
 
-static void ocfs2_xattr_value_update_clusters(struct inode *inode,
-                                             struct ocfs2_extent_tree *et,
+static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
                                              u32 clusters)
 {
-       struct ocfs2_xattr_value_root *xv =
-               (struct ocfs2_xattr_value_root *)et->private;
+       struct ocfs2_xattr_value_buf *vb = et->et_object;
 
-       le32_add_cpu(&xv->xr_clusters, clusters);
+       le32_add_cpu(&vb->vb_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 ocfs2_extent_tree *et)
+{
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
+       et->et_max_leaf_clusters =
+               ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
+}
 
-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_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
+                                            u64 blkno)
 {
-       struct ocfs2_extent_tree *et;
+       struct ocfs2_xattr_block *xb = et->et_object;
+       struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
 
-       et = kzalloc(sizeof(*et), GFP_NOFS);
-       if (!et)
-               return NULL;
+       xt->xt_last_eb_blk = cpu_to_le64(blkno);
+}
 
-       et->type = et_type;
-       get_bh(bh);
-       et->root_bh = bh;
-       et->private = private;
+static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_xattr_block *xb = et->et_object;
+       struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
 
-       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;
-       }
+       return le64_to_cpu(xt->xt_last_eb_blk);
+}
+
+static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
+                                            u32 clusters)
+{
+       struct ocfs2_xattr_block *xb = et->et_object;
 
-       return et;
+       le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
 }
 
-static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et)
+static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
+       .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 void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
+                                         u64 blkno)
 {
-       if (et) {
-               brelse(et->root_bh);
-               kfree(et);
-       }
+       struct ocfs2_dx_root_block *dx_root = et->et_object;
+
+       dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
 }
 
-static inline void ocfs2_set_last_eb_blk(struct ocfs2_extent_tree *et,
-                                        u64 new_last_eb_blk)
+static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
 {
-       et->eops->set_last_eb_blk(et, new_last_eb_blk);
+       struct ocfs2_dx_root_block *dx_root = et->et_object;
+
+       return le64_to_cpu(dx_root->dr_last_eb_blk);
 }
 
-static inline u64 ocfs2_get_last_eb_blk(struct ocfs2_extent_tree *et)
+static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
+                                         u32 clusters)
 {
-       return et->eops->get_last_eb_blk(et);
+       struct ocfs2_dx_root_block *dx_root = et->et_object;
+
+       le32_add_cpu(&dx_root->dr_clusters, clusters);
 }
 
-static inline void ocfs2_update_clusters(struct inode *inode,
-                                        struct ocfs2_extent_tree *et,
-                                        u32 clusters)
+static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
 {
-       et->eops->update_clusters(inode, et, clusters);
+       struct ocfs2_dx_root_block *dx_root = et->et_object;
+
+       BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
+
+       return 0;
 }
 
-static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
-static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
-                                        struct ocfs2_extent_block *eb);
+static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_dx_root_block *dx_root = et->et_object;
 
-/*
- * Structures which describe a path through a btree, and functions to
- * manipulate them.
- *
- * The idea here is to be as generic as possible with the tree
- * manipulation code.
- */
-struct ocfs2_path_item {
-       struct buffer_head              *bh;
-       struct ocfs2_extent_list        *el;
+       et->et_root_el = &dx_root->dr_list;
+}
+
+static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
+       .eo_set_last_eb_blk     = ocfs2_dx_root_set_last_eb_blk,
+       .eo_get_last_eb_blk     = ocfs2_dx_root_get_last_eb_blk,
+       .eo_update_clusters     = ocfs2_dx_root_update_clusters,
+       .eo_sanity_check        = ocfs2_dx_root_sanity_check,
+       .eo_fill_root_el        = ocfs2_dx_root_fill_root_el,
 };
 
-#define OCFS2_MAX_PATH_DEPTH   5
+static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_refcount_block *rb = et->et_object;
+
+       et->et_root_el = &rb->rf_list;
+}
+
+static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
+                                               u64 blkno)
+{
+       struct ocfs2_refcount_block *rb = et->et_object;
 
-struct ocfs2_path {
-       int                     p_tree_depth;
-       struct ocfs2_path_item  p_node[OCFS2_MAX_PATH_DEPTH];
+       rb->rf_last_eb_blk = cpu_to_le64(blkno);
+}
+
+static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
+{
+       struct ocfs2_refcount_block *rb = et->et_object;
+
+       return le64_to_cpu(rb->rf_last_eb_blk);
+}
+
+static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
+                                               u32 clusters)
+{
+       struct ocfs2_refcount_block *rb = et->et_object;
+
+       le32_add_cpu(&rb->rf_clusters, clusters);
+}
+
+static enum ocfs2_contig_type
+ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
+                                 struct ocfs2_extent_rec *ext,
+                                 struct ocfs2_extent_rec *insert_rec)
+{
+       return CONTIG_NONE;
+}
+
+static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
+       .eo_set_last_eb_blk     = ocfs2_refcount_tree_set_last_eb_blk,
+       .eo_get_last_eb_blk     = ocfs2_refcount_tree_get_last_eb_blk,
+       .eo_update_clusters     = ocfs2_refcount_tree_update_clusters,
+       .eo_fill_root_el        = ocfs2_refcount_tree_fill_root_el,
+       .eo_extent_contig       = ocfs2_refcount_tree_extent_contig,
 };
 
-#define path_root_bh(_path) ((_path)->p_node[0].bh)
-#define path_root_el(_path) ((_path)->p_node[0].el)
-#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
-#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
-#define path_num_items(_path) ((_path)->p_tree_depth + 1)
+static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
+                                    struct ocfs2_caching_info *ci,
+                                    struct buffer_head *bh,
+                                    ocfs2_journal_access_func access,
+                                    void *obj,
+                                    struct ocfs2_extent_tree_operations *ops)
+{
+       et->et_ops = ops;
+       et->et_root_bh = bh;
+       et->et_ci = ci;
+       et->et_root_journal_access = access;
+       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(et);
+}
+
+void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
+                                  struct ocfs2_caching_info *ci,
+                                  struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
+                                NULL, &ocfs2_dinode_et_ops);
+}
+
+void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
+                                      struct ocfs2_caching_info *ci,
+                                      struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
+                                NULL, &ocfs2_xattr_tree_et_ops);
+}
 
+void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
+                                       struct ocfs2_caching_info *ci,
+                                       struct ocfs2_xattr_value_buf *vb)
+{
+       __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
+                                &ocfs2_xattr_value_et_ops);
+}
+
+void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
+                                   struct ocfs2_caching_info *ci,
+                                   struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
+                                NULL, &ocfs2_dx_root_et_ops);
+}
+
+void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
+                                    struct ocfs2_caching_info *ci,
+                                    struct buffer_head *bh)
+{
+       __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
+                                NULL, &ocfs2_refcount_tree_et_ops);
+}
+
+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 inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
+{
+       return et->et_ops->eo_get_last_eb_blk(et);
+}
+
+static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
+                                           u32 clusters)
+{
+       et->et_ops->eo_update_clusters(et, clusters);
+}
+
+static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
+                                             struct ocfs2_extent_rec *rec)
+{
+       if (et->et_ops->eo_extent_map_insert)
+               et->et_ops->eo_extent_map_insert(et, rec);
+}
+
+static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
+                                               u32 clusters)
+{
+       if (et->et_ops->eo_extent_map_truncate)
+               et->et_ops->eo_extent_map_truncate(et, clusters);
+}
+
+static inline int ocfs2_et_root_journal_access(handle_t *handle,
+                                              struct ocfs2_extent_tree *et,
+                                              int type)
+{
+       return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
+                                         type);
+}
+
+static inline enum ocfs2_contig_type
+       ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
+                              struct ocfs2_extent_rec *rec,
+                              struct ocfs2_extent_rec *insert_rec)
+{
+       if (et->et_ops->eo_extent_contig)
+               return et->et_ops->eo_extent_contig(et, rec, insert_rec);
+
+       return ocfs2_extent_rec_contig(
+                               ocfs2_metadata_cache_get_super(et->et_ci),
+                               rec, insert_rec);
+}
+
+static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
+                                       struct ocfs2_extent_rec *rec)
+{
+       int ret = 0;
+
+       if (et->et_ops->eo_insert_check)
+               ret = et->et_ops->eo_insert_check(et, rec);
+       return ret;
+}
+
+static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
+{
+       int ret = 0;
+
+       if (et->et_ops->eo_sanity_check)
+               ret = et->et_ops->eo_sanity_check(et);
+       return ret;
+}
+
+static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
+static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
+                                        struct ocfs2_extent_block *eb);
+static void ocfs2_adjust_rightmost_records(handle_t *handle,
+                                          struct ocfs2_extent_tree *et,
+                                          struct ocfs2_path *path,
+                                          struct ocfs2_extent_rec *insert_rec);
 /*
  * Reset the actual path elements so that we can re-use the structure
  * to build another path. Generally, this involves freeing the buffer
  * heads.
  */
-static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
+void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
 {
        int i, start = 0, depth = 0;
        struct ocfs2_path_item *node;
@@ -289,11 +600,13 @@ static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
         */
        if (keep_root)
                depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
+       else
+               path_root_access(path) = NULL;
 
        path->p_tree_depth = depth;
 }
 
-static void ocfs2_free_path(struct ocfs2_path *path)
+void ocfs2_free_path(struct ocfs2_path *path)
 {
        if (path) {
                ocfs2_reinit_path(path, 0);
@@ -314,6 +627,7 @@ static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
 
        BUG_ON(path_root_bh(dest) != path_root_bh(src));
        BUG_ON(path_root_el(dest) != path_root_el(src));
+       BUG_ON(path_root_access(dest) != path_root_access(src));
 
        ocfs2_reinit_path(dest, 1);
 
@@ -335,6 +649,7 @@ static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
        int i;
 
        BUG_ON(path_root_bh(dest) != path_root_bh(src));
+       BUG_ON(path_root_access(dest) != path_root_access(src));
 
        for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
                brelse(dest->p_node[i].bh);
@@ -370,7 +685,8 @@ static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
 }
 
 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
-                                        struct ocfs2_extent_list *root_el)
+                                        struct ocfs2_extent_list *root_el,
+                                        ocfs2_journal_access_func access)
 {
        struct ocfs2_path *path;
 
@@ -382,16 +698,54 @@ static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
                get_bh(root_bh);
                path_root_bh(path) = root_bh;
                path_root_el(path) = root_el;
+               path_root_access(path) = access;
        }
 
        return path;
 }
 
+struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
+{
+       return ocfs2_new_path(path_root_bh(path), path_root_el(path),
+                             path_root_access(path));
+}
+
+struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
+{
+       return ocfs2_new_path(et->et_root_bh, et->et_root_el,
+                             et->et_root_journal_access);
+}
+
+/*
+ * Journal the buffer at depth idx.  All idx>0 are extent_blocks,
+ * otherwise it's the root_access function.
+ *
+ * I don't like the way this function's name looks next to
+ * ocfs2_journal_access_path(), but I don't have a better one.
+ */
+int ocfs2_path_bh_journal_access(handle_t *handle,
+                                struct ocfs2_caching_info *ci,
+                                struct ocfs2_path *path,
+                                int idx)
+{
+       ocfs2_journal_access_func access = path_root_access(path);
+
+       if (!access)
+               access = ocfs2_journal_access;
+
+       if (idx)
+               access = ocfs2_journal_access_eb;
+
+       return access(handle, ci, path->p_node[idx].bh,
+                     OCFS2_JOURNAL_ACCESS_WRITE);
+}
+
 /*
  * Convenience function to journal all components in a path.
  */
-static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
-                                    struct ocfs2_path *path)
+int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
+                             handle_t *handle,
+                             struct ocfs2_path *path)
 {
        int i, ret = 0;
 
@@ -399,8 +753,7 @@ static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
                goto out;
 
        for(i = 0; i < path_num_items(path); i++) {
-               ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -441,17 +794,9 @@ int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
        return ret;
 }
 
-enum ocfs2_contig_type {
-       CONTIG_NONE = 0,
-       CONTIG_LEFT,
-       CONTIG_RIGHT,
-       CONTIG_LEFTRIGHT,
-};
-
-
 /*
  * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
- * ocfs2_extent_contig only work properly against leaf nodes!
+ * ocfs2_extent_rec_contig only work properly against leaf nodes!
  */
 static int ocfs2_block_extent_contig(struct super_block *sb,
                                     struct ocfs2_extent_rec *ext,
@@ -477,9 +822,9 @@ static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
 }
 
 static enum ocfs2_contig_type
-       ocfs2_extent_contig(struct inode *inode,
-                           struct ocfs2_extent_rec *ext,
-                           struct ocfs2_extent_rec *insert_rec)
+       ocfs2_extent_rec_contig(struct super_block *sb,
+                               struct ocfs2_extent_rec *ext,
+                               struct ocfs2_extent_rec *insert_rec)
 {
        u64 blkno = le64_to_cpu(insert_rec->e_blkno);
 
@@ -492,12 +837,12 @@ static enum ocfs2_contig_type
                return CONTIG_NONE;
 
        if (ocfs2_extents_adjacent(ext, insert_rec) &&
-           ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
+           ocfs2_block_extent_contig(sb, ext, blkno))
                        return CONTIG_RIGHT;
 
        blkno = le64_to_cpu(ext->e_blkno);
        if (ocfs2_extents_adjacent(insert_rec, ext) &&
-           ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
+           ocfs2_block_extent_contig(sb, insert_rec, blkno))
                return CONTIG_LEFT;
 
        return CONTIG_NONE;
@@ -535,14 +880,85 @@ struct ocfs2_merge_ctxt {
        int                     c_split_covers_rec;
 };
 
+static int ocfs2_validate_extent_block(struct super_block *sb,
+                                      struct buffer_head *bh)
+{
+       int rc;
+       struct ocfs2_extent_block *eb =
+               (struct ocfs2_extent_block *)bh->b_data;
+
+       mlog(0, "Validating extent block %llu\n",
+            (unsigned long long)bh->b_blocknr);
+
+       BUG_ON(!buffer_uptodate(bh));
+
+       /*
+        * If the ecc fails, we return the error but otherwise
+        * leave the filesystem running.  We know any error is
+        * local to this block.
+        */
+       rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
+       if (rc) {
+               mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
+                    (unsigned long long)bh->b_blocknr);
+               return rc;
+       }
+
+       /*
+        * Errors after here are fatal.
+        */
+
+       if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+               ocfs2_error(sb,
+                           "Extent block #%llu has bad signature %.*s",
+                           (unsigned long long)bh->b_blocknr, 7,
+                           eb->h_signature);
+               return -EINVAL;
+       }
+
+       if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
+               ocfs2_error(sb,
+                           "Extent block #%llu has an invalid h_blkno "
+                           "of %llu",
+                           (unsigned long long)bh->b_blocknr,
+                           (unsigned long long)le64_to_cpu(eb->h_blkno));
+               return -EINVAL;
+       }
+
+       if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
+               ocfs2_error(sb,
+                           "Extent block #%llu has an invalid "
+                           "h_fs_generation of #%u",
+                           (unsigned long long)bh->b_blocknr,
+                           le32_to_cpu(eb->h_fs_generation));
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
+                           struct buffer_head **bh)
+{
+       int rc;
+       struct buffer_head *tmp = *bh;
+
+       rc = ocfs2_read_block(ci, eb_blkno, &tmp,
+                             ocfs2_validate_extent_block);
+
+       /* If ocfs2_read_block() got us a new bh, pass it up. */
+       if (!rc && !*bh)
+               *bh = tmp;
+
+       return rc;
+}
+
+
 /*
  * How many free extents have we got before we need more meta data?
  */
 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;
@@ -552,29 +968,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;
-       }
+       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_extent_block(et->et_ci, last_eb_blk,
+                                                &eb_bh);
                if (retval < 0) {
                        mlog_errno(retval);
                        goto bail;
@@ -587,8 +986,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;
@@ -599,9 +997,8 @@ bail:
  * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  * l_count for you
  */
-static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
-                                    handle_t *handle,
-                                    struct inode *inode,
+static int ocfs2_create_new_meta_bhs(handle_t *handle,
+                                    struct ocfs2_extent_tree *et,
                                     int wanted,
                                     struct ocfs2_alloc_context *meta_ac,
                                     struct buffer_head *bhs[])
@@ -610,6 +1007,8 @@ static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
        u16 suballoc_bit_start;
        u32 num_got;
        u64 first_blkno;
+       struct ocfs2_super *osb =
+               OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
        struct ocfs2_extent_block *eb;
 
        mlog_entry_void();
@@ -635,10 +1034,11 @@ static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
                                mlog_errno(status);
                                goto bail;
                        }
-                       ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
+                       ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
 
-                       status = ocfs2_journal_access(handle, inode, bhs[i],
-                                                     OCFS2_JOURNAL_ACCESS_CREATE);
+                       status = ocfs2_journal_access_eb(handle, et->et_ci,
+                                                        bhs[i],
+                                                        OCFS2_JOURNAL_ACCESS_CREATE);
                        if (status < 0) {
                                mlog_errno(status);
                                goto bail;
@@ -674,8 +1074,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;
                }
        }
@@ -699,15 +1098,62 @@ static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
 {
        int i;
 
-       i = le16_to_cpu(el->l_next_free_rec) - 1;
+       i = le16_to_cpu(el->l_next_free_rec) - 1;
+
+       return le32_to_cpu(el->l_recs[i].e_cpos) +
+               ocfs2_rec_clusters(el, &el->l_recs[i]);
+}
+
+/*
+ * Change range of the branches in the right most path according to the leaf
+ * extent block's rightmost record.
+ */
+static int ocfs2_adjust_rightmost_branch(handle_t *handle,
+                                        struct ocfs2_extent_tree *et)
+{
+       int status;
+       struct ocfs2_path *path = NULL;
+       struct ocfs2_extent_list *el;
+       struct ocfs2_extent_rec *rec;
+
+       path = ocfs2_new_path_from_et(et);
+       if (!path) {
+               status = -ENOMEM;
+               return status;
+       }
+
+       status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
+       if (status < 0) {
+               mlog_errno(status);
+               goto out;
+       }
+
+       status = ocfs2_extend_trans(handle, path_num_items(path) +
+                                   handle->h_buffer_credits);
+       if (status < 0) {
+               mlog_errno(status);
+               goto out;
+       }
+
+       status = ocfs2_journal_access_path(et->et_ci, handle, path);
+       if (status < 0) {
+               mlog_errno(status);
+               goto out;
+       }
 
-       return le32_to_cpu(el->l_recs[i].e_cpos) +
-               ocfs2_rec_clusters(el, &el->l_recs[i]);
+       el = path_leaf_el(path);
+       rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
+
+       ocfs2_adjust_rightmost_records(handle, et, path, rec);
+
+out:
+       ocfs2_free_path(path);
+       return status;
 }
 
 /*
  * Add an entire tree branch to our inode. eb_bh is the extent block
- * to start at, if we don't want to start the branch at the dinode
+ * to start at, if we don't want to start the branch at the root
  * structure.
  *
  * last_eb_bh is required as we have to update it's next_leaf pointer
@@ -716,9 +1162,7 @@ static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list  *el)
  * the new branch will be 'empty' in the sense that every block will
  * contain a single record with cluster count == 0.
  */
-static int ocfs2_add_branch(struct ocfs2_super *osb,
-                           handle_t *handle,
-                           struct inode *inode,
+static int ocfs2_add_branch(handle_t *handle,
                            struct ocfs2_extent_tree *et,
                            struct buffer_head *eb_bh,
                            struct buffer_head **last_eb_bh,
@@ -731,7 +1175,7 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
        struct ocfs2_extent_block *eb;
        struct ocfs2_extent_list  *eb_el;
        struct ocfs2_extent_list  *el;
-       u32 new_cpos;
+       u32 new_cpos, root_end;
 
        mlog_entry_void();
 
@@ -741,13 +1185,34 @@ 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);
 
        new_blocks = le16_to_cpu(el->l_tree_depth);
 
+       eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
+       new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
+       root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
+
+       /*
+        * If there is a gap before the root end and the real end
+        * of the righmost leaf block, we need to remove the gap
+        * between new_cpos and root_end first so that the tree
+        * is consistent after we add a new branch(it will start
+        * from new_cpos).
+        */
+       if (root_end > new_cpos) {
+               mlog(0, "adjust the cluster end from %u to %u\n",
+                    root_end, new_cpos);
+               status = ocfs2_adjust_rightmost_branch(handle, et);
+               if (status) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
        /* allocate the number of new eb blocks we need */
        new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
                             GFP_KERNEL);
@@ -757,16 +1222,13 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
                goto bail;
        }
 
-       status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
+       status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
                                           meta_ac, new_eb_bhs);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
 
-       eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
-       new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
-
        /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
         * linked with the rest of the tree.
         * conversly, new_eb_bhs[0] is the new bottommost leaf.
@@ -778,15 +1240,12 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
        for(i = 0; i < new_blocks; i++) {
                bh = new_eb_bhs[i];
                eb = (struct ocfs2_extent_block *) bh->b_data;
-               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-                       status = -EIO;
-                       goto bail;
-               }
+               /* ocfs2_create_new_meta_bhs() should create it right! */
+               BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
                eb_el = &eb->h_list;
 
-               status = ocfs2_journal_access(handle, inode, bh,
-                                             OCFS2_JOURNAL_ACCESS_CREATE);
+               status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
+                                                OCFS2_JOURNAL_ACCESS_CREATE);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -825,21 +1284,21 @@ static int ocfs2_add_branch(struct ocfs2_super *osb,
         * journal_dirty erroring as it won't unless we've aborted the
         * handle (in which case we would never be here) so reserving
         * the write with journal_access is all we need to do. */
-       status = ocfs2_journal_access(handle, inode, *last_eb_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
-       status = ocfs2_journal_access(handle, inode, et->root_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_et_root_journal_access(handle, et,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
        if (eb_bh) {
-               status = ocfs2_journal_access(handle, inode, eb_bh,
-                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
+                                                OCFS2_JOURNAL_ACCESS_WRITE);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -856,7 +1315,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);
@@ -864,7 +1323,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) {
@@ -885,8 +1344,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);
        }
 
@@ -899,9 +1357,7 @@ bail:
  * returns back the new extent block so you can add a branch to it
  * after this call.
  */
-static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
-                                 handle_t *handle,
-                                 struct inode *inode,
+static int ocfs2_shift_tree_depth(handle_t *handle,
                                  struct ocfs2_extent_tree *et,
                                  struct ocfs2_alloc_context *meta_ac,
                                  struct buffer_head **ret_new_eb_bh)
@@ -915,7 +1371,7 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
 
        mlog_entry_void();
 
-       status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
+       status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
                                           &new_eb_bh);
        if (status < 0) {
                mlog_errno(status);
@@ -923,17 +1379,14 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
        }
 
        eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
-       if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-               status = -EIO;
-               goto bail;
-       }
+       /* ocfs2_create_new_meta_bhs() should create it right! */
+       BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
 
        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);
+       status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
+                                        OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -951,8 +1404,8 @@ static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
                goto bail;
        }
 
-       status = ocfs2_journal_access(handle, inode, et->root_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_et_root_journal_access(handle, et,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -972,9 +1425,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;
@@ -984,8 +1437,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;
@@ -1008,9 +1460,7 @@ bail:
  *
  * return status < 0 indicates an error.
  */
-static int ocfs2_find_branch_target(struct ocfs2_super *osb,
-                                   struct inode *inode,
-                                   struct ocfs2_extent_tree *et,
+static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
                                    struct buffer_head **target_bh)
 {
        int status = 0, i;
@@ -1024,51 +1474,44 @@ 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) {
-                       ocfs2_error(inode->i_sb, "Dinode %llu has empty "
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu has empty "
                                    "extent list (next_free_rec == 0)",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno);
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
                        status = -EIO;
                        goto bail;
                }
                i = le16_to_cpu(el->l_next_free_rec) - 1;
                blkno = le64_to_cpu(el->l_recs[i].e_blkno);
                if (!blkno) {
-                       ocfs2_error(inode->i_sb, "Dinode %llu has extent "
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu has extent "
                                    "list where extent # %d has no physical "
                                    "block start",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
                        status = -EIO;
                        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_extent_block(et->et_ci, blkno, &bh);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
                }
 
                eb = (struct ocfs2_extent_block *) bh->b_data;
-               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-                       status = -EIO;
-                       goto bail;
-               }
                el = &eb->h_list;
 
                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);
                }
@@ -1076,14 +1519,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;
@@ -1099,20 +1541,18 @@ bail:
  *
  * *last_eb_bh will be updated by ocfs2_add_branch().
  */
-static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
-                          struct ocfs2_extent_tree *et, int *final_depth,
-                          struct buffer_head **last_eb_bh,
+static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
+                          int *final_depth, struct buffer_head **last_eb_bh,
                           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;
 
        BUG_ON(meta_ac == NULL);
 
-       shift = ocfs2_find_branch_target(osb, inode, et, &bh);
+       shift = ocfs2_find_branch_target(et, &bh);
        if (shift < 0) {
                ret = shift;
                mlog_errno(ret);
@@ -1129,8 +1569,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
                /* ocfs2_shift_tree_depth will return us a buffer with
                 * the new extent block (so we can pass that to
                 * ocfs2_add_branch). */
-               ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
-                                            meta_ac, &bh);
+               ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -1156,7 +1595,7 @@ static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
        /* call ocfs2_add_branch to add the final part of the tree with
         * the new data. */
        mlog(0, "add branch. bh = %p\n", bh);
-       ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
+       ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
                               meta_ac);
        if (ret < 0) {
                mlog_errno(ret);
@@ -1326,7 +1765,7 @@ set_and_inc:
  *
  * The array index of the subtree root is passed back.
  */
-static int ocfs2_find_subtree_root(struct inode *inode,
+static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
                                   struct ocfs2_path *left,
                                   struct ocfs2_path *right)
 {
@@ -1344,10 +1783,10 @@ static int ocfs2_find_subtree_root(struct inode *inode,
                 * The caller didn't pass two adjacent paths.
                 */
                mlog_bug_on_msg(i > left->p_tree_depth,
-                               "Inode %lu, left depth %u, right depth %u\n"
+                               "Owner %llu, left depth %u, right depth %u\n"
                                "left leaf blk %llu, right leaf blk %llu\n",
-                               inode->i_ino, left->p_tree_depth,
-                               right->p_tree_depth,
+                               (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+                               left->p_tree_depth, right->p_tree_depth,
                                (unsigned long long)path_leaf_bh(left)->b_blocknr,
                                (unsigned long long)path_leaf_bh(right)->b_blocknr);
        } while (left->p_node[i].bh->b_blocknr ==
@@ -1364,7 +1803,7 @@ typedef void (path_insert_t)(void *, struct buffer_head *);
  * This code can be called with a cpos larger than the tree, in which
  * case it will return the rightmost path.
  */
-static int __ocfs2_find_path(struct inode *inode,
+static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
                             struct ocfs2_extent_list *root_el, u32 cpos,
                             path_insert_t *func, void *data)
 {
@@ -1375,15 +1814,14 @@ static int __ocfs2_find_path(struct inode *inode,
        struct ocfs2_extent_block *eb;
        struct ocfs2_extent_list *el;
        struct ocfs2_extent_rec *rec;
-       struct ocfs2_inode_info *oi = OCFS2_I(inode);
 
        el = root_el;
        while (el->l_tree_depth) {
                if (le16_to_cpu(el->l_next_free_rec) == 0) {
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu has empty extent list at "
+                       ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+                                   "Owner %llu has empty extent list at "
                                    "depth %u\n",
-                                   (unsigned long long)oi->ip_blkno,
+                                   (unsigned long long)ocfs2_metadata_cache_owner(ci),
                                    le16_to_cpu(el->l_tree_depth));
                        ret = -EROFS;
                        goto out;
@@ -1406,10 +1844,10 @@ static int __ocfs2_find_path(struct inode *inode,
 
                blkno = le64_to_cpu(el->l_recs[i].e_blkno);
                if (blkno == 0) {
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu has bad blkno in extent list "
+                       ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+                                   "Owner %llu has bad blkno in extent list "
                                    "at depth %u (index %d)\n",
-                                   (unsigned long long)oi->ip_blkno,
+                                   (unsigned long long)ocfs2_metadata_cache_owner(ci),
                                    le16_to_cpu(el->l_tree_depth), i);
                        ret = -EROFS;
                        goto out;
@@ -1417,8 +1855,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_extent_block(ci, blkno, &bh);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -1426,18 +1863,13 @@ static int __ocfs2_find_path(struct inode *inode,
 
                eb = (struct ocfs2_extent_block *) bh->b_data;
                el = &eb->h_list;
-               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-                       ret = -EIO;
-                       goto out;
-               }
 
                if (le16_to_cpu(el->l_next_free_rec) >
                    le16_to_cpu(el->l_count)) {
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu has bad count in extent list "
+                       ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+                                   "Owner %llu has bad count in extent list "
                                    "at block %llu (next free=%u, count=%u)\n",
-                                   (unsigned long long)oi->ip_blkno,
+                                   (unsigned long long)ocfs2_metadata_cache_owner(ci),
                                    (unsigned long long)bh->b_blocknr,
                                    le16_to_cpu(el->l_next_free_rec),
                                    le16_to_cpu(el->l_count));
@@ -1481,14 +1913,14 @@ static void find_path_ins(void *data, struct buffer_head *bh)
        ocfs2_path_insert_eb(fp->path, fp->index, bh);
        fp->index++;
 }
-static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
-                          u32 cpos)
+int ocfs2_find_path(struct ocfs2_caching_info *ci,
+                   struct ocfs2_path *path, u32 cpos)
 {
        struct find_path_data data;
 
        data.index = 1;
        data.path = path;
-       return __ocfs2_find_path(inode, path_root_el(path), cpos,
+       return __ocfs2_find_path(ci, path_root_el(path), cpos,
                                 find_path_ins, &data);
 }
 
@@ -1513,13 +1945,14 @@ static void find_leaf_ins(void *data, struct buffer_head *bh)
  *
  * This function doesn't handle non btree extent lists.
  */
-int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
-                   u32 cpos, struct buffer_head **leaf_bh)
+int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
+                   struct ocfs2_extent_list *root_el, u32 cpos,
+                   struct buffer_head **leaf_bh)
 {
        int ret;
        struct buffer_head *bh = NULL;
 
-       ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
+       ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -1559,7 +1992,8 @@ static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
         * immediately to their right.
         */
        left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
-       if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
+       if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
+               BUG_ON(right_child_el->l_tree_depth);
                BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
                left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
        }
@@ -1624,7 +2058,7 @@ static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  *   - When we've adjusted the last extent record in the left path leaf and the
  *     1st extent record in the right path leaf during cross extent block merge.
  */
-static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
+static void ocfs2_complete_edge_insert(handle_t *handle,
                                       struct ocfs2_path *left_path,
                                       struct ocfs2_path *right_path,
                                       int subtree_index)
@@ -1702,8 +2136,8 @@ static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
                mlog_errno(ret);
 }
 
-static int ocfs2_rotate_subtree_right(struct inode *inode,
-                                     handle_t *handle,
+static int ocfs2_rotate_subtree_right(handle_t *handle,
+                                     struct ocfs2_extent_tree *et,
                                      struct ocfs2_path *left_path,
                                      struct ocfs2_path *right_path,
                                      int subtree_index)
@@ -1719,10 +2153,10 @@ static int ocfs2_rotate_subtree_right(struct inode *inode,
        left_el = path_leaf_el(left_path);
 
        if (left_el->l_next_free_rec != left_el->l_count) {
-               ocfs2_error(inode->i_sb,
+               ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
                            "Inode %llu has non-full interior leaf node %llu"
                            "(next free = %u)",
-                           (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                           (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                            (unsigned long long)left_leaf_bh->b_blocknr,
                            le16_to_cpu(left_el->l_next_free_rec));
                return -EROFS;
@@ -1738,25 +2172,23 @@ static int ocfs2_rotate_subtree_right(struct inode *inode,
        root_bh = left_path->p_node[subtree_index].bh;
        BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
 
-       ret = ocfs2_journal_access(handle, inode, root_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
+                                          subtree_index);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
        for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
-               ret = ocfs2_journal_access(handle, inode,
-                                          right_path->p_node[i].bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                  right_path, i);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_journal_access(handle, inode,
-                                          left_path->p_node[i].bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                  left_path, i);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -1769,7 +2201,7 @@ static int ocfs2_rotate_subtree_right(struct inode *inode,
        /* This is a code error, not a disk corruption. */
        mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
                        "because rightmost leaf block %llu is empty\n",
-                       (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                       (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                        (unsigned long long)right_leaf_bh->b_blocknr);
 
        ocfs2_create_empty_extent(right_el);
@@ -1803,8 +2235,8 @@ static int ocfs2_rotate_subtree_right(struct inode *inode,
                goto out;
        }
 
-       ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
-                               subtree_index);
+       ocfs2_complete_edge_insert(handle, left_path, right_path,
+                                  subtree_index);
 
 out:
        return ret;
@@ -1894,10 +2326,18 @@ static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
                                           int op_credits,
                                           struct ocfs2_path *path)
 {
+       int ret;
        int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
 
-       if (handle->h_buffer_credits < credits)
-               return ocfs2_extend_trans(handle, credits);
+       if (handle->h_buffer_credits < credits) {
+               ret = ocfs2_extend_trans(handle,
+                                        credits - handle->h_buffer_credits);
+               if (ret)
+                       return ret;
+
+               if (unlikely(handle->h_buffer_credits < credits))
+                       return ocfs2_extend_trans(handle, credits);
+       }
 
        return 0;
 }
@@ -1958,7 +2398,7 @@ static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  *
  * The array is assumed to be large enough to hold an entire path (tree depth).
  *
- * Upon succesful return from this function:
+ * Upon successful return from this function:
  *
  * - The 'right_path' array will contain a path to the leaf block
  *   whose range contains e_cpos.
@@ -1967,8 +2407,8 @@ static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  *   *ret_left_path will contain a valid path which can be passed to
  *   ocfs2_insert_path().
  */
-static int ocfs2_rotate_tree_right(struct inode *inode,
-                                  handle_t *handle,
+static int ocfs2_rotate_tree_right(handle_t *handle,
+                                  struct ocfs2_extent_tree *et,
                                   enum ocfs2_split_type split,
                                   u32 insert_cpos,
                                   struct ocfs2_path *right_path,
@@ -1977,18 +2417,18 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
        int ret, start, orig_credits = handle->h_buffer_credits;
        u32 cpos;
        struct ocfs2_path *left_path = NULL;
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
 
        *ret_left_path = NULL;
 
-       left_path = ocfs2_new_path(path_root_bh(right_path),
-                                  path_root_el(right_path));
+       left_path = ocfs2_new_path_from_path(right_path);
        if (!left_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
+       ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2026,7 +2466,7 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
                mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
                     insert_cpos, cpos);
 
-               ret = ocfs2_find_path(inode, left_path, cpos);
+               ret = ocfs2_find_path(et->et_ci, left_path, cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2034,10 +2474,11 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
 
                mlog_bug_on_msg(path_leaf_bh(left_path) ==
                                path_leaf_bh(right_path),
-                               "Inode %lu: error during insert of %u "
+                               "Owner %llu: error during insert of %u "
                                "(left path cpos %u) results in two identical "
                                "paths ending at %llu\n",
-                               inode->i_ino, insert_cpos, cpos,
+                               (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+                               insert_cpos, cpos,
                                (unsigned long long)
                                path_leaf_bh(left_path)->b_blocknr);
 
@@ -2063,7 +2504,7 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
                        goto out_ret_path;
                }
 
-               start = ocfs2_find_subtree_root(inode, left_path, right_path);
+               start = ocfs2_find_subtree_root(et, left_path, right_path);
 
                mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
                     start,
@@ -2077,7 +2518,7 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
                        goto out;
                }
 
-               ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
+               ret = ocfs2_rotate_subtree_right(handle, et, left_path,
                                                 right_path, start);
                if (ret) {
                        mlog_errno(ret);
@@ -2109,8 +2550,7 @@ static int ocfs2_rotate_tree_right(struct inode *inode,
                 */
                ocfs2_mv_path(right_path, left_path);
 
-               ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
-                                                   &cpos);
+               ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2124,15 +2564,38 @@ out_ret_path:
        return ret;
 }
 
-static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
-                                     struct ocfs2_path *path)
+static int ocfs2_update_edge_lengths(handle_t *handle,
+                                    struct ocfs2_extent_tree *et,
+                                    int subtree_index, struct ocfs2_path *path)
 {
-       int i, idx;
+       int i, idx, ret;
        struct ocfs2_extent_rec *rec;
        struct ocfs2_extent_list *el;
        struct ocfs2_extent_block *eb;
        u32 range;
 
+       /*
+        * In normal tree rotation process, we will never touch the
+        * tree branch above subtree_index and ocfs2_extend_rotate_transaction
+        * doesn't reserve the credits for them either.
+        *
+        * But we do have a special case here which will update the rightmost
+        * records for all the bh in the path.
+        * So we have to allocate extra credits and access them.
+        */
+       ret = ocfs2_extend_trans(handle,
+                                handle->h_buffer_credits + subtree_index);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       ret = ocfs2_journal_access_path(et->et_ci, handle, path);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
        /* Path should always be rightmost. */
        eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
        BUG_ON(eb->h_next_leaf_blk != 0ULL);
@@ -2153,9 +2616,12 @@ static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
 
                ocfs2_journal_dirty(handle, path->p_node[i].bh);
        }
+out:
+       return ret;
 }
 
-static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
+static void ocfs2_unlink_path(handle_t *handle,
+                             struct ocfs2_extent_tree *et,
                              struct ocfs2_cached_dealloc_ctxt *dealloc,
                              struct ocfs2_path *path, int unlink_start)
 {
@@ -2177,12 +2643,12 @@ static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
                        mlog(ML_ERROR,
                             "Inode %llu, attempted to remove extent block "
                             "%llu with %u records\n",
-                            (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                            (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                             (unsigned long long)le64_to_cpu(eb->h_blkno),
                             le16_to_cpu(el->l_next_free_rec));
 
                        ocfs2_journal_dirty(handle, bh);
-                       ocfs2_remove_from_cache(inode, bh);
+                       ocfs2_remove_from_cache(et->et_ci, bh);
                        continue;
                }
 
@@ -2195,11 +2661,12 @@ static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
                if (ret)
                        mlog_errno(ret);
 
-               ocfs2_remove_from_cache(inode, bh);
+               ocfs2_remove_from_cache(et->et_ci, bh);
        }
 }
 
-static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
+static void ocfs2_unlink_subtree(handle_t *handle,
+                                struct ocfs2_extent_tree *et,
                                 struct ocfs2_path *left_path,
                                 struct ocfs2_path *right_path,
                                 int subtree_index,
@@ -2230,17 +2697,17 @@ static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
        ocfs2_journal_dirty(handle, root_bh);
        ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
 
-       ocfs2_unlink_path(inode, handle, dealloc, right_path,
+       ocfs2_unlink_path(handle, et, dealloc, right_path,
                          subtree_index + 1);
 }
 
-static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
+static int ocfs2_rotate_subtree_left(handle_t *handle,
+                                    struct ocfs2_extent_tree *et,
                                     struct ocfs2_path *left_path,
                                     struct ocfs2_path *right_path,
                                     int subtree_index,
                                     struct ocfs2_cached_dealloc_ctxt *dealloc,
-                                    int *deleted,
-                                    struct ocfs2_extent_tree *et)
+                                    int *deleted)
 {
        int ret, i, del_right_subtree = 0, right_has_empty = 0;
        struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
@@ -2276,9 +2743,9 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
                        return -EAGAIN;
 
                if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
-                       ret = ocfs2_journal_access(handle, inode,
-                                                  path_leaf_bh(right_path),
-                                                  OCFS2_JOURNAL_ACCESS_WRITE);
+                       ret = ocfs2_journal_access_eb(handle, et->et_ci,
+                                                     path_leaf_bh(right_path),
+                                                     OCFS2_JOURNAL_ACCESS_WRITE);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -2295,8 +2762,8 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
                 * We have to update i_last_eb_blk during the meta
                 * data delete.
                 */
-               ret = ocfs2_journal_access(handle, inode, et_root_bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_et_root_journal_access(handle, et,
+                                                  OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2311,25 +2778,23 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
         */
        BUG_ON(right_has_empty && !del_right_subtree);
 
-       ret = ocfs2_journal_access(handle, inode, root_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
+                                          subtree_index);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
        for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
-               ret = ocfs2_journal_access(handle, inode,
-                                          right_path->p_node[i].bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                  right_path, i);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_journal_access(handle, inode,
-                                          left_path->p_node[i].bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                  left_path, i);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2365,12 +2830,17 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
                mlog_errno(ret);
 
        if (del_right_subtree) {
-               ocfs2_unlink_subtree(inode, handle, left_path, right_path,
+               ocfs2_unlink_subtree(handle, et, left_path, right_path,
                                     subtree_index, dealloc);
-               ocfs2_update_edge_lengths(inode, handle, left_path);
+               ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
+                                               left_path);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
 
                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
@@ -2386,7 +2856,7 @@ static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
 
                *deleted = 1;
        } else
-               ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
+               ocfs2_complete_edge_insert(handle, left_path, right_path,
                                           subtree_index);
 
 out:
@@ -2472,18 +2942,19 @@ out:
        return ret;
 }
 
-static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
-                                           handle_t *handle,
-                                           struct buffer_head *bh,
-                                           struct ocfs2_extent_list *el)
+static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
+                                           struct ocfs2_extent_tree *et,
+                                           struct ocfs2_path *path)
 {
        int ret;
+       struct buffer_head *bh = path_leaf_bh(path);
+       struct ocfs2_extent_list *el = path_leaf_el(path);
 
        if (!ocfs2_is_empty_extent(&el->l_recs[0]))
                return 0;
 
-       ret = ocfs2_journal_access(handle, inode, bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
+                                          path_num_items(path) - 1);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2499,31 +2970,30 @@ out:
        return ret;
 }
 
-static int __ocfs2_rotate_tree_left(struct inode *inode,
-                                   handle_t *handle, int orig_credits,
+static int __ocfs2_rotate_tree_left(handle_t *handle,
+                                   struct ocfs2_extent_tree *et,
+                                   int orig_credits,
                                    struct ocfs2_path *path,
                                    struct ocfs2_cached_dealloc_ctxt *dealloc,
-                                   struct ocfs2_path **empty_extent_path,
-                                   struct ocfs2_extent_tree *et)
+                                   struct ocfs2_path **empty_extent_path)
 {
        int ret, subtree_root, deleted;
        u32 right_cpos;
        struct ocfs2_path *left_path = NULL;
        struct ocfs2_path *right_path = NULL;
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
 
        BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
 
        *empty_extent_path = NULL;
 
-       ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
-                                            &right_cpos);
+       ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       left_path = ocfs2_new_path(path_root_bh(path),
-                                  path_root_el(path));
+       left_path = ocfs2_new_path_from_path(path);
        if (!left_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -2532,8 +3002,7 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
 
        ocfs2_cp_path(left_path, path);
 
-       right_path = ocfs2_new_path(path_root_bh(path),
-                                   path_root_el(path));
+       right_path = ocfs2_new_path_from_path(path);
        if (!right_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -2541,13 +3010,13 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
        }
 
        while (right_cpos) {
-               ret = ocfs2_find_path(inode, right_path, right_cpos);
+               ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               subtree_root = ocfs2_find_subtree_root(inode, left_path,
+               subtree_root = ocfs2_find_subtree_root(et, left_path,
                                                       right_path);
 
                mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
@@ -2567,17 +3036,16 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
                 * Caller might still want to make changes to the
                 * tree root, so re-add it to the journal here.
                 */
-               ret = ocfs2_journal_access(handle, inode,
-                                          path_root_bh(left_path),
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                  left_path, 0);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
+               ret = ocfs2_rotate_subtree_left(handle, et, left_path,
                                                right_path, subtree_root,
-                                               dealloc, &deleted, et);
+                                               dealloc, &deleted);
                if (ret == -EAGAIN) {
                        /*
                         * The rotation has to temporarily stop due to
@@ -2604,7 +3072,7 @@ static int __ocfs2_rotate_tree_left(struct inode *inode,
 
                ocfs2_mv_path(left_path, right_path);
 
-               ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
+               ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
                                                     &right_cpos);
                if (ret) {
                        mlog_errno(ret);
@@ -2619,10 +3087,10 @@ out:
        return ret;
 }
 
-static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
+static int ocfs2_remove_rightmost_path(handle_t *handle,
+                               struct ocfs2_extent_tree *et,
                                struct ocfs2_path *path,
-                               struct ocfs2_cached_dealloc_ctxt *dealloc,
-                               struct ocfs2_extent_tree *et)
+                               struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret, subtree_index;
        u32 cpos;
@@ -2631,7 +3099,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(et);
        if (ret)
                goto out;
        /*
@@ -2646,13 +3114,14 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
                goto out;
        }
 
-       ret = ocfs2_journal_access_path(inode, handle, path);
+       ret = ocfs2_journal_access_path(et->et_ci, handle, path);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
+       ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
+                                           path, &cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2663,50 +3132,54 @@ static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
                 * We have a path to the left of this one - it needs
                 * an update too.
                 */
-               left_path = ocfs2_new_path(path_root_bh(path),
-                                          path_root_el(path));
+               left_path = ocfs2_new_path_from_path(path);
                if (!left_path) {
                        ret = -ENOMEM;
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_find_path(inode, left_path, cpos);
+               ret = ocfs2_find_path(et->et_ci, left_path, cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_journal_access_path(inode, handle, left_path);
+               ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
+               subtree_index = ocfs2_find_subtree_root(et, left_path, path);
 
-               ocfs2_unlink_subtree(inode, handle, left_path, path,
+               ocfs2_unlink_subtree(handle, et, left_path, path,
                                     subtree_index, dealloc);
-               ocfs2_update_edge_lengths(inode, handle, left_path);
+               ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
+                                               left_path);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
 
                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
                 * means it must be the only one. This gets
                 * handled differently because we want to
-                * revert the inode back to having extents
+                * revert the root back to having extents
                 * in-line.
                 */
-               ocfs2_unlink_path(inode, handle, dealloc, path, 1);
+               ocfs2_unlink_path(handle, et, 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));
@@ -2732,10 +3205,10 @@ out:
  * the rightmost tree leaf record is removed so the caller is
  * responsible for detecting and correcting that.
  */
-static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
+static int ocfs2_rotate_tree_left(handle_t *handle,
+                                 struct ocfs2_extent_tree *et,
                                  struct ocfs2_path *path,
-                                 struct ocfs2_cached_dealloc_ctxt *dealloc,
-                                 struct ocfs2_extent_tree *et)
+                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret, orig_credits = handle->h_buffer_credits;
        struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
@@ -2752,9 +3225,7 @@ rightmost_no_delete:
                 * Inline extents. This is trivially handled, so do
                 * it up front.
                 */
-               ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
-                                                      path_leaf_bh(path),
-                                                      path_leaf_el(path));
+               ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
                if (ret)
                        mlog_errno(ret);
                goto out;
@@ -2770,7 +3241,7 @@ rightmost_no_delete:
         *
         *  1) is handled via ocfs2_rotate_rightmost_leaf_left()
         *  2a) we need the left branch so that we can update it with the unlink
-        *  2b) we need to bring the inode back to inline extents.
+        *  2b) we need to bring the root back to inline extents.
         */
 
        eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
@@ -2786,9 +3257,9 @@ rightmost_no_delete:
 
                if (le16_to_cpu(el->l_next_free_rec) == 0) {
                        ret = -EIO;
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu has empty extent block at %llu",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu has empty extent block at %llu",
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                                    (unsigned long long)le64_to_cpu(eb->h_blkno));
                        goto out;
                }
@@ -2802,8 +3273,8 @@ rightmost_no_delete:
                 * nonempty list.
                 */
 
-               ret = ocfs2_remove_rightmost_path(inode, handle, path,
-                                                 dealloc, et);
+               ret = ocfs2_remove_rightmost_path(handle, et, path,
+                                                 dealloc);
                if (ret)
                        mlog_errno(ret);
                goto out;
@@ -2814,8 +3285,8 @@ rightmost_no_delete:
         * and restarting from there.
         */
 try_rotate:
-       ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
-                                      dealloc, &restart_path, et);
+       ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
+                                      dealloc, &restart_path);
        if (ret && ret != -EAGAIN) {
                mlog_errno(ret);
                goto out;
@@ -2825,9 +3296,9 @@ try_rotate:
                tmp_path = restart_path;
                restart_path = NULL;
 
-               ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
+               ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
                                               tmp_path, dealloc,
-                                              &restart_path, et);
+                                              &restart_path);
                if (ret && ret != -EAGAIN) {
                        mlog_errno(ret);
                        goto out;
@@ -2878,7 +3349,7 @@ static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
        }
 }
 
-static int ocfs2_get_right_path(struct inode *inode,
+static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
                                struct ocfs2_path *left_path,
                                struct ocfs2_path **ret_right_path)
 {
@@ -2895,8 +3366,8 @@ static int ocfs2_get_right_path(struct inode *inode,
        left_el = path_leaf_el(left_path);
        BUG_ON(left_el->l_next_free_rec != left_el->l_count);
 
-       ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
-                                            &right_cpos);
+       ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
+                                            left_path, &right_cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2905,15 +3376,14 @@ static int ocfs2_get_right_path(struct inode *inode,
        /* This function shouldn't be called for the rightmost leaf. */
        BUG_ON(right_cpos == 0);
 
-       right_path = ocfs2_new_path(path_root_bh(left_path),
-                                   path_root_el(left_path));
+       right_path = ocfs2_new_path_from_path(left_path);
        if (!right_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_find_path(inode, right_path, right_cpos);
+       ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2933,9 +3403,9 @@ out:
  * For index == l_count - 1, the "next" means the 1st extent rec of the
  * next extent block.
  */
-static int ocfs2_merge_rec_right(struct inode *inode,
-                                struct ocfs2_path *left_path,
+static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
                                 handle_t *handle,
+                                struct ocfs2_extent_tree *et,
                                 struct ocfs2_extent_rec *split_rec,
                                 int index)
 {
@@ -2956,7 +3426,7 @@ static int ocfs2_merge_rec_right(struct inode *inode,
        if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
            le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
                /* we meet with a cross extent block merge. */
-               ret = ocfs2_get_right_path(inode, left_path, &right_path);
+               ret = ocfs2_get_right_path(et, left_path, &right_path);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2975,8 +3445,8 @@ static int ocfs2_merge_rec_right(struct inode *inode,
                       le16_to_cpu(left_rec->e_leaf_clusters) !=
                       le32_to_cpu(right_rec->e_cpos));
 
-               subtree_index = ocfs2_find_subtree_root(inode,
-                                                       left_path, right_path);
+               subtree_index = ocfs2_find_subtree_root(et, left_path,
+                                                       right_path);
 
                ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
                                                      handle->h_buffer_credits,
@@ -2989,8 +3459,8 @@ static int ocfs2_merge_rec_right(struct inode *inode,
                root_bh = left_path->p_node[subtree_index].bh;
                BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
 
-               ret = ocfs2_journal_access(handle, inode, root_bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
+                                                  subtree_index);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -2998,17 +3468,15 @@ static int ocfs2_merge_rec_right(struct inode *inode,
 
                for (i = subtree_index + 1;
                     i < path_num_items(right_path); i++) {
-                       ret = ocfs2_journal_access(handle, inode,
-                                                  right_path->p_node[i].bh,
-                                                  OCFS2_JOURNAL_ACCESS_WRITE);
+                       ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                          right_path, i);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
                        }
 
-                       ret = ocfs2_journal_access(handle, inode,
-                                                  left_path->p_node[i].bh,
-                                                  OCFS2_JOURNAL_ACCESS_WRITE);
+                       ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                          left_path, i);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -3020,8 +3488,8 @@ static int ocfs2_merge_rec_right(struct inode *inode,
                right_rec = &el->l_recs[index + 1];
        }
 
-       ret = ocfs2_journal_access(handle, inode, bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
+                                          path_num_items(left_path) - 1);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -3031,7 +3499,8 @@ static int ocfs2_merge_rec_right(struct inode *inode,
 
        le32_add_cpu(&right_rec->e_cpos, -split_clusters);
        le64_add_cpu(&right_rec->e_blkno,
-                    -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
+                    -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
+                                              split_clusters));
        le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
 
        ocfs2_cleanup_merge(el, index);
@@ -3045,8 +3514,8 @@ static int ocfs2_merge_rec_right(struct inode *inode,
                if (ret)
                        mlog_errno(ret);
 
-               ocfs2_complete_edge_insert(inode, handle, left_path,
-                                          right_path, subtree_index);
+               ocfs2_complete_edge_insert(handle, left_path, right_path,
+                                          subtree_index);
        }
 out:
        if (right_path)
@@ -3054,7 +3523,7 @@ out:
        return ret;
 }
 
-static int ocfs2_get_left_path(struct inode *inode,
+static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
                               struct ocfs2_path *right_path,
                               struct ocfs2_path **ret_left_path)
 {
@@ -3067,7 +3536,7 @@ static int ocfs2_get_left_path(struct inode *inode,
        /* This function shouldn't be called for non-trees. */
        BUG_ON(right_path->p_tree_depth == 0);
 
-       ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
+       ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
                                            right_path, &left_cpos);
        if (ret) {
                mlog_errno(ret);
@@ -3077,15 +3546,14 @@ static int ocfs2_get_left_path(struct inode *inode,
        /* This function shouldn't be called for the leftmost leaf. */
        BUG_ON(left_cpos == 0);
 
-       left_path = ocfs2_new_path(path_root_bh(right_path),
-                                  path_root_el(right_path));
+       left_path = ocfs2_new_path_from_path(right_path);
        if (!left_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_find_path(inode, left_path, left_cpos);
+       ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -3108,12 +3576,11 @@ out:
  * remove the rightmost leaf extent block in the right_path and change
  * the right path to indicate the new rightmost path.
  */
-static int ocfs2_merge_rec_left(struct inode *inode,
-                               struct ocfs2_path *right_path,
+static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
                                handle_t *handle,
+                               struct ocfs2_extent_tree *et,
                                struct ocfs2_extent_rec *split_rec,
                                struct ocfs2_cached_dealloc_ctxt *dealloc,
-                               struct ocfs2_extent_tree *et,
                                int index)
 {
        int ret, i, subtree_index = 0, has_empty_extent = 0;
@@ -3131,7 +3598,7 @@ static int ocfs2_merge_rec_left(struct inode *inode,
        right_rec = &el->l_recs[index];
        if (index == 0) {
                /* we meet with a cross extent block merge. */
-               ret = ocfs2_get_left_path(inode, right_path, &left_path);
+               ret = ocfs2_get_left_path(et, right_path, &left_path);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -3147,8 +3614,8 @@ static int ocfs2_merge_rec_left(struct inode *inode,
                       le16_to_cpu(left_rec->e_leaf_clusters) !=
                       le32_to_cpu(split_rec->e_cpos));
 
-               subtree_index = ocfs2_find_subtree_root(inode,
-                                                       left_path, right_path);
+               subtree_index = ocfs2_find_subtree_root(et, left_path,
+                                                       right_path);
 
                ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
                                                      handle->h_buffer_credits,
@@ -3161,8 +3628,8 @@ static int ocfs2_merge_rec_left(struct inode *inode,
                root_bh = left_path->p_node[subtree_index].bh;
                BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
 
-               ret = ocfs2_journal_access(handle, inode, root_bh,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
+                                                  subtree_index);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -3170,17 +3637,15 @@ static int ocfs2_merge_rec_left(struct inode *inode,
 
                for (i = subtree_index + 1;
                     i < path_num_items(right_path); i++) {
-                       ret = ocfs2_journal_access(handle, inode,
-                                                  right_path->p_node[i].bh,
-                                                  OCFS2_JOURNAL_ACCESS_WRITE);
+                       ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                          right_path, i);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
                        }
 
-                       ret = ocfs2_journal_access(handle, inode,
-                                                  left_path->p_node[i].bh,
-                                                  OCFS2_JOURNAL_ACCESS_WRITE);
+                       ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
+                                                          left_path, i);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -3192,8 +3657,8 @@ static int ocfs2_merge_rec_left(struct inode *inode,
                        has_empty_extent = 1;
        }
 
-       ret = ocfs2_journal_access(handle, inode, bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
+                                          path_num_items(right_path) - 1);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -3211,7 +3676,8 @@ static int ocfs2_merge_rec_left(struct inode *inode,
 
        le32_add_cpu(&right_rec->e_cpos, split_clusters);
        le64_add_cpu(&right_rec->e_blkno,
-                    ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
+                    ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
+                                             split_clusters));
        le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
 
        ocfs2_cleanup_merge(el, index);
@@ -3233,9 +3699,9 @@ static int ocfs2_merge_rec_left(struct inode *inode,
                if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
                    le16_to_cpu(el->l_next_free_rec) == 1) {
 
-                       ret = ocfs2_remove_rightmost_path(inode, handle,
+                       ret = ocfs2_remove_rightmost_path(handle, et,
                                                          right_path,
-                                                         dealloc, et);
+                                                         dealloc);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -3247,7 +3713,7 @@ static int ocfs2_merge_rec_left(struct inode *inode,
                        ocfs2_mv_path(right_path, left_path);
                        left_path = NULL;
                } else
-                       ocfs2_complete_edge_insert(inode, handle, left_path,
+                       ocfs2_complete_edge_insert(handle, left_path,
                                                   right_path, subtree_index);
        }
 out:
@@ -3256,15 +3722,13 @@ out:
        return ret;
 }
 
-static int ocfs2_try_to_merge_extent(struct inode *inode,
-                                    handle_t *handle,
+static int ocfs2_try_to_merge_extent(handle_t *handle,
+                                    struct ocfs2_extent_tree *et,
                                     struct ocfs2_path *path,
                                     int split_index,
                                     struct ocfs2_extent_rec *split_rec,
                                     struct ocfs2_cached_dealloc_ctxt *dealloc,
-                                    struct ocfs2_merge_ctxt *ctxt,
-                                    struct ocfs2_extent_tree *et)
-
+                                    struct ocfs2_merge_ctxt *ctxt)
 {
        int ret = 0;
        struct ocfs2_extent_list *el = path_leaf_el(path);
@@ -3280,8 +3744,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                 * extents - having more than one in a leaf is
                 * illegal.
                 */
-               ret = ocfs2_rotate_tree_left(inode, handle, path,
-                                            dealloc, et);
+               ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -3310,8 +3773,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                 * prevoius extent block. It is more efficient and easier
                 * if we do merge_right first and merge_left later.
                 */
-               ret = ocfs2_merge_rec_right(inode, path,
-                                           handle, split_rec,
+               ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
                                            split_index);
                if (ret) {
                        mlog_errno(ret);
@@ -3324,8 +3786,7 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
 
                /* The merge left us with an empty extent, remove it. */
-               ret = ocfs2_rotate_tree_left(inode, handle, path,
-                                            dealloc, et);
+               ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -3337,18 +3798,15 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                 * Note that we don't pass split_rec here on purpose -
                 * we've merged it into the rec already.
                 */
-               ret = ocfs2_merge_rec_left(inode, path,
-                                          handle, rec,
-                                          dealloc, et,
-                                          split_index);
+               ret = ocfs2_merge_rec_left(path, handle, et, rec,
+                                          dealloc, split_index);
 
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               ret = ocfs2_rotate_tree_left(inode, handle, path,
-                                            dealloc, et);
+               ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
                /*
                 * Error from this last rotate is not critical, so
                 * print but don't bubble it up.
@@ -3365,19 +3823,16 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                 * the record on the left (hence the left merge).
                 */
                if (ctxt->c_contig_type == CONTIG_RIGHT) {
-                       ret = ocfs2_merge_rec_left(inode,
-                                                  path,
-                                                  handle, split_rec,
-                                                  dealloc, et,
+                       ret = ocfs2_merge_rec_left(path, handle, et,
+                                                  split_rec, dealloc,
                                                   split_index);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
                        }
                } else {
-                       ret = ocfs2_merge_rec_right(inode,
-                                                   path,
-                                                   handle, split_rec,
+                       ret = ocfs2_merge_rec_right(path, handle,
+                                                   et, split_rec,
                                                    split_index);
                        if (ret) {
                                mlog_errno(ret);
@@ -3390,8 +3845,8 @@ static int ocfs2_try_to_merge_extent(struct inode *inode,
                         * The merge may have left an empty extent in
                         * our leaf. Try to rotate it away.
                         */
-                       ret = ocfs2_rotate_tree_left(inode, handle, path,
-                                                    dealloc, et);
+                       ret = ocfs2_rotate_tree_left(handle, et, path,
+                                                    dealloc);
                        if (ret)
                                mlog_errno(ret);
                        ret = 0;
@@ -3437,10 +3892,10 @@ static void ocfs2_subtract_from_rec(struct super_block *sb,
  * list. If this leaf is part of an allocation tree, it is assumed
  * that the tree above has been prepared.
  */
-static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
+static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
+                                struct ocfs2_extent_rec *insert_rec,
                                 struct ocfs2_extent_list *el,
-                                struct ocfs2_insert_type *insert,
-                                struct inode *inode)
+                                struct ocfs2_insert_type *insert)
 {
        int i = insert->ins_contig_index;
        unsigned int range;
@@ -3452,7 +3907,8 @@ static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
                i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
                BUG_ON(i == -1);
                rec = &el->l_recs[i];
-               ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
+               ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
+                                       insert->ins_split, rec,
                                        insert_rec);
                goto rotate;
        }
@@ -3494,10 +3950,10 @@ static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
 
                mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
                                le16_to_cpu(el->l_count),
-                               "inode %lu, depth %u, count %u, next free %u, "
+                               "owner %llu, depth %u, count %u, next free %u, "
                                "rec.cpos %u, rec.clusters %u, "
                                "insert.cpos %u, insert.clusters %u\n",
-                               inode->i_ino,
+                               ocfs2_metadata_cache_owner(et->et_ci),
                                le16_to_cpu(el->l_tree_depth),
                                le16_to_cpu(el->l_count),
                                le16_to_cpu(el->l_next_free_rec),
@@ -3525,8 +3981,8 @@ rotate:
        ocfs2_rotate_leaf(el, insert_rec);
 }
 
-static void ocfs2_adjust_rightmost_records(struct inode *inode,
-                                          handle_t *handle,
+static void ocfs2_adjust_rightmost_records(handle_t *handle,
+                                          struct ocfs2_extent_tree *et,
                                           struct ocfs2_path *path,
                                           struct ocfs2_extent_rec *insert_rec)
 {
@@ -3544,9 +4000,9 @@ static void ocfs2_adjust_rightmost_records(struct inode *inode,
 
                next_free = le16_to_cpu(el->l_next_free_rec);
                if (next_free == 0) {
-                       ocfs2_error(inode->i_sb,
-                                   "Dinode %llu has a bad extent list",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno);
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu has a bad extent list",
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
                        ret = -EIO;
                        return;
                }
@@ -3566,7 +4022,8 @@ static void ocfs2_adjust_rightmost_records(struct inode *inode,
        }
 }
 
-static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
+static int ocfs2_append_rec_to_path(handle_t *handle,
+                                   struct ocfs2_extent_tree *et,
                                    struct ocfs2_extent_rec *insert_rec,
                                    struct ocfs2_path *right_path,
                                    struct ocfs2_path **ret_left_path)
@@ -3594,8 +4051,8 @@ static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
            (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
                u32 left_cpos;
 
-               ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
-                                                   &left_cpos);
+               ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
+                                                   right_path, &left_cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -3610,15 +4067,15 @@ static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
                 * leftmost leaf.
                 */
                if (left_cpos) {
-                       left_path = ocfs2_new_path(path_root_bh(right_path),
-                                                  path_root_el(right_path));
+                       left_path = ocfs2_new_path_from_path(right_path);
                        if (!left_path) {
                                ret = -ENOMEM;
                                mlog_errno(ret);
                                goto out;
                        }
 
-                       ret = ocfs2_find_path(inode, left_path, left_cpos);
+                       ret = ocfs2_find_path(et->et_ci, left_path,
+                                             left_cpos);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -3631,13 +4088,13 @@ static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
                }
        }
 
-       ret = ocfs2_journal_access_path(inode, handle, right_path);
+       ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
+       ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
 
        *ret_left_path = left_path;
        ret = 0;
@@ -3648,7 +4105,7 @@ out:
        return ret;
 }
 
-static void ocfs2_split_record(struct inode *inode,
+static void ocfs2_split_record(struct ocfs2_extent_tree *et,
                               struct ocfs2_path *left_path,
                               struct ocfs2_path *right_path,
                               struct ocfs2_extent_rec *split_rec,
@@ -3659,7 +4116,7 @@ static void ocfs2_split_record(struct inode *inode,
        struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
        struct ocfs2_extent_rec *rec, *tmprec;
 
-       right_el = path_leaf_el(right_path);;
+       right_el = path_leaf_el(right_path);
        if (left_path)
                left_el = path_leaf_el(left_path);
 
@@ -3721,7 +4178,8 @@ static void ocfs2_split_record(struct inode *inode,
        }
 
        rec = &el->l_recs[index];
-       ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
+       ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
+                               split, rec, split_rec);
        ocfs2_rotate_leaf(insert_el, split_rec);
 }
 
@@ -3733,8 +4191,8 @@ static void ocfs2_split_record(struct inode *inode,
  * in. left_path should only be passed in if we need to update that
  * portion of the tree after an edge insert.
  */
-static int ocfs2_insert_path(struct inode *inode,
-                            handle_t *handle,
+static int ocfs2_insert_path(handle_t *handle,
+                            struct ocfs2_extent_tree *et,
                             struct ocfs2_path *left_path,
                             struct ocfs2_path *right_path,
                             struct ocfs2_extent_rec *insert_rec,
@@ -3760,7 +4218,7 @@ static int ocfs2_insert_path(struct inode *inode,
                        goto out;
                }
 
-               ret = ocfs2_journal_access_path(inode, handle, left_path);
+               ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -3771,7 +4229,7 @@ static int ocfs2_insert_path(struct inode *inode,
         * Pass both paths to the journal. The majority of inserts
         * will be touching all components anyway.
         */
-       ret = ocfs2_journal_access_path(inode, handle, right_path);
+       ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
        if (ret < 0) {
                mlog_errno(ret);
                goto out;
@@ -3783,7 +4241,7 @@ static int ocfs2_insert_path(struct inode *inode,
                 * of splits, but it's easier to just let one separate
                 * function sort it all out.
                 */
-               ocfs2_split_record(inode, left_path, right_path,
+               ocfs2_split_record(et, left_path, right_path,
                                   insert_rec, insert->ins_split);
 
                /*
@@ -3797,8 +4255,8 @@ static int ocfs2_insert_path(struct inode *inode,
                        if (ret)
                                mlog_errno(ret);
        } else
-               ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
-                                    insert, inode);
+               ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
+                                    insert);
 
        ret = ocfs2_journal_dirty(handle, leaf_bh);
        if (ret)
@@ -3811,10 +4269,10 @@ static int ocfs2_insert_path(struct inode *inode,
                 *
                 * XXX: Should we extend the transaction here?
                 */
-               subtree_index = ocfs2_find_subtree_root(inode, left_path,
+               subtree_index = ocfs2_find_subtree_root(et, left_path,
                                                        right_path);
-               ocfs2_complete_edge_insert(inode, handle, left_path,
-                                          right_path, subtree_index);
+               ocfs2_complete_edge_insert(handle, left_path, right_path,
+                                          subtree_index);
        }
 
        ret = 0;
@@ -3822,8 +4280,7 @@ out:
        return ret;
 }
 
-static int ocfs2_do_insert_extent(struct inode *inode,
-                                 handle_t *handle,
+static int ocfs2_do_insert_extent(handle_t *handle,
                                  struct ocfs2_extent_tree *et,
                                  struct ocfs2_extent_rec *insert_rec,
                                  struct ocfs2_insert_type *type)
@@ -3834,21 +4291,21 @@ 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,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_et_root_journal_access(handle, et,
+                                          OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
        if (le16_to_cpu(el->l_tree_depth) == 0) {
-               ocfs2_insert_at_leaf(insert_rec, el, type, inode);
+               ocfs2_insert_at_leaf(et, insert_rec, el, type);
                goto out_update_clusters;
        }
 
-       right_path = ocfs2_new_path(et->root_bh, et->root_el);
+       right_path = ocfs2_new_path_from_et(et);
        if (!right_path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -3867,7 +4324,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
                cpos = UINT_MAX;
        }
 
-       ret = ocfs2_find_path(inode, right_path, cpos);
+       ret = ocfs2_find_path(et->et_ci, right_path, cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -3886,7 +4343,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
         * can wind up skipping both of these two special cases...
         */
        if (rotate) {
-               ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
+               ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
                                              le32_to_cpu(insert_rec->e_cpos),
                                              right_path, &left_path);
                if (ret) {
@@ -3898,15 +4355,15 @@ 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,
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               ret = ocfs2_et_root_journal_access(handle, et,
+                                                  OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
        } else if (type->ins_appending == APPEND_TAIL
                   && type->ins_contig != CONTIG_LEFT) {
-               ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
+               ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
                                               right_path, &left_path);
                if (ret) {
                        mlog_errno(ret);
@@ -3914,7 +4371,7 @@ static int ocfs2_do_insert_extent(struct inode *inode,
                }
        }
 
-       ret = ocfs2_insert_path(inode, handle, left_path, right_path,
+       ret = ocfs2_insert_path(handle, et, left_path, right_path,
                                insert_rec, type);
        if (ret) {
                mlog_errno(ret);
@@ -3923,10 +4380,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(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);
 
@@ -3938,7 +4395,8 @@ out:
 }
 
 static enum ocfs2_contig_type
-ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
+ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
+                              struct ocfs2_path *path,
                               struct ocfs2_extent_list *el, int index,
                               struct ocfs2_extent_rec *split_rec)
 {
@@ -3950,22 +4408,22 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
        struct ocfs2_path *left_path = NULL, *right_path = NULL;
        struct buffer_head *bh;
        struct ocfs2_extent_block *eb;
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
 
        if (index > 0) {
                rec = &el->l_recs[index - 1];
        } else if (path->p_tree_depth > 0) {
-               status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
-                                                      path, &left_cpos);
+               status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
                if (status)
                        goto out;
 
                if (left_cpos != 0) {
-                       left_path = ocfs2_new_path(path_root_bh(path),
-                                                  path_root_el(path));
+                       left_path = ocfs2_new_path_from_path(path);
                        if (!left_path)
                                goto out;
 
-                       status = ocfs2_find_path(inode, left_path, left_cpos);
+                       status = ocfs2_find_path(et->et_ci, left_path,
+                                                left_cpos);
                        if (status)
                                goto out;
 
@@ -3975,8 +4433,15 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
                            le16_to_cpu(new_el->l_count)) {
                                bh = path_leaf_bh(left_path);
                                eb = (struct ocfs2_extent_block *)bh->b_data;
-                               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
-                                                                eb);
+                               ocfs2_error(sb,
+                                           "Extent block #%llu has an "
+                                           "invalid l_next_free_rec of "
+                                           "%d.  It should have "
+                                           "matched the l_count of %d",
+                                           (unsigned long long)le64_to_cpu(eb->h_blkno),
+                                           le16_to_cpu(new_el->l_next_free_rec),
+                                           le16_to_cpu(new_el->l_count));
+                               status = -EINVAL;
                                goto out;
                        }
                        rec = &new_el->l_recs[
@@ -3993,7 +4458,7 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
                        if (split_rec->e_cpos == el->l_recs[index].e_cpos)
                                ret = CONTIG_RIGHT;
                } else {
-                       ret = ocfs2_extent_contig(inode, rec, split_rec);
+                       ret = ocfs2_et_extent_contig(et, rec, split_rec);
                }
        }
 
@@ -4002,20 +4467,18 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
                rec = &el->l_recs[index + 1];
        else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
                 path->p_tree_depth > 0) {
-               status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
-                                                       path, &right_cpos);
+               status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
                if (status)
                        goto out;
 
                if (right_cpos == 0)
                        goto out;
 
-               right_path = ocfs2_new_path(path_root_bh(path),
-                                           path_root_el(path));
+               right_path = ocfs2_new_path_from_path(path);
                if (!right_path)
                        goto out;
 
-               status = ocfs2_find_path(inode, right_path, right_cpos);
+               status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
                if (status)
                        goto out;
 
@@ -4025,8 +4488,12 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
                        if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
                                bh = path_leaf_bh(right_path);
                                eb = (struct ocfs2_extent_block *)bh->b_data;
-                               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
-                                                                eb);
+                               ocfs2_error(sb,
+                                           "Extent block #%llu has an "
+                                           "invalid l_next_free_rec of %d",
+                                           (unsigned long long)le64_to_cpu(eb->h_blkno),
+                                           le16_to_cpu(new_el->l_next_free_rec));
+                               status = -EINVAL;
                                goto out;
                        }
                        rec = &new_el->l_recs[1];
@@ -4036,7 +4503,7 @@ ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
        if (rec) {
                enum ocfs2_contig_type contig_type;
 
-               contig_type = ocfs2_extent_contig(inode, rec, split_rec);
+               contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
 
                if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
                        ret = CONTIG_LEFTRIGHT;
@@ -4053,7 +4520,7 @@ out:
        return ret;
 }
 
-static void ocfs2_figure_contig_type(struct inode *inode,
+static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
                                     struct ocfs2_insert_type *insert,
                                     struct ocfs2_extent_list *el,
                                     struct ocfs2_extent_rec *insert_rec)
@@ -4064,14 +4531,29 @@ static void ocfs2_figure_contig_type(struct inode *inode,
        BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
 
        for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
-               contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
-                                                 insert_rec);
+               contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
+                                                    insert_rec);
                if (contig_type != CONTIG_NONE) {
                        insert->ins_contig_index = i;
                        break;
                }
        }
        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;
+       }
 }
 
 /*
@@ -4131,8 +4613,7 @@ set_tail_append:
  * All of the information is stored on the ocfs2_insert_type
  * structure.
  */
-static int ocfs2_figure_insert_type(struct inode *inode,
-                                   struct ocfs2_extent_tree *et,
+static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
                                    struct buffer_head **last_eb_bh,
                                    struct ocfs2_extent_rec *insert_rec,
                                    int *free_records,
@@ -4146,7 +4627,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) {
@@ -4156,9 +4637,9 @@ 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_extent_block(et->et_ci,
+                                             ocfs2_et_get_last_eb_blk(et),
+                                             &bh);
                if (ret) {
                        mlog_exit(ret);
                        goto out;
@@ -4179,12 +4660,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(et, insert, el, insert_rec);
                ocfs2_figure_appending_type(insert, el, insert_rec);
                return 0;
        }
 
-       path = ocfs2_new_path(et->root_bh, et->root_el);
+       path = ocfs2_new_path_from_et(et);
        if (!path) {
                ret = -ENOMEM;
                mlog_errno(ret);
@@ -4197,7 +4678,7 @@ static int ocfs2_figure_insert_type(struct inode *inode,
         * us the rightmost tree path. This is accounted for below in
         * the appending code.
         */
-       ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
+       ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -4213,7 +4694,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(et, insert, el, insert_rec);
 
        /*
         * The insert code isn't quite ready to deal with all cases of
@@ -4234,7 +4715,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
@@ -4258,20 +4739,17 @@ out:
 }
 
 /*
- * Insert an extent into an inode btree.
+ * Insert an extent into a btree.
  *
- * The caller needs to update fe->i_clusters
+ * The caller needs to update the owning btree's cluster count.
  */
-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(handle_t *handle,
+                       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);
@@ -4279,26 +4757,22 @@ 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);
+       mlog(0, "add %u clusters at position %u to owner %llu\n",
+            new_clusters, cpos,
+            (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
 
        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(et, &rec);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
 
-       status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
+       status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
                                          &free_records, &insert);
        if (status < 0) {
                mlog_errno(status);
@@ -4312,7 +4786,7 @@ static int ocfs2_insert_extent(struct ocfs2_super *osb,
             free_records, insert.ins_tree_depth);
 
        if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
-               status = ocfs2_grow_tree(inode, handle, et,
+               status = ocfs2_grow_tree(handle, et,
                                         &insert.ins_tree_depth, &last_eb_bh,
                                         meta_ac);
                if (status) {
@@ -4322,101 +4796,34 @@ static int ocfs2_insert_extent(struct ocfs2_super *osb,
        }
 
        /* Finally, we can add clusters. This might rotate the tree for us. */
-       status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
+       status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
        if (status < 0)
                mlog_errno(status);
-       else if (et->type == OCFS2_DINODE_EXTENT)
-               ocfs2_extent_map_insert_rec(inode, &rec);
+       else
+               ocfs2_et_extent_map_insert(et, &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;
-}
-
 /*
  * 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.
  */
-int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
-                               struct inode *inode,
+int ocfs2_add_clusters_in_btree(handle_t *handle,
+                               struct ocfs2_extent_tree *et,
                                u32 *logical_offset,
                                u32 clusters_to_add,
                                int mark_unwritten,
-                               struct buffer_head *root_bh,
-                               struct ocfs2_extent_list *root_el,
-                               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;
@@ -4424,14 +4831,15 @@ int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
        u32 bit_off, num_bits;
        u64 block;
        u8 flags = 0;
+       struct ocfs2_super *osb =
+               OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
 
        BUG_ON(!clusters_to_add);
 
        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, et);
        if (free_extents < 0) {
                status = free_extents;
                mlog_errno(status);
@@ -4450,7 +4858,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;
@@ -4467,33 +4875,26 @@ 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,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       /* reserve our write early -- insert_extent may update the tree root */
+       status = ocfs2_et_root_journal_access(handle, et,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto leave;
        }
 
        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
-               status = ocfs2_xattr_value_insert_extent(osb, handle,
-                                                        inode, root_bh,
-                                                        *logical_offset,
-                                                        block, num_bits, flags,
-                                                        meta_ac, private);
+       mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
+            num_bits, bit_off,
+            (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
+       status = ocfs2_insert_extent(handle, 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;
@@ -4536,10 +4937,9 @@ static void ocfs2_make_right_split_rec(struct super_block *sb,
        split_rec->e_flags = rec->e_flags;
 }
 
-static int ocfs2_split_and_insert(struct inode *inode,
-                                 handle_t *handle,
-                                 struct ocfs2_path *path,
+static int ocfs2_split_and_insert(handle_t *handle,
                                  struct ocfs2_extent_tree *et,
+                                 struct ocfs2_path *path,
                                  struct buffer_head **last_eb_bh,
                                  int split_index,
                                  struct ocfs2_extent_rec *orig_split_rec,
@@ -4561,7 +4961,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) {
@@ -4572,7 +4972,7 @@ leftright:
 
        if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
            le16_to_cpu(rightmost_el->l_count)) {
-               ret = ocfs2_grow_tree(inode, handle, et,
+               ret = ocfs2_grow_tree(handle, et,
                                      &depth, last_eb_bh, meta_ac);
                if (ret) {
                        mlog_errno(ret);
@@ -4601,8 +5001,8 @@ leftright:
                 */
                insert.ins_split = SPLIT_RIGHT;
 
-               ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
-                                          &rec);
+               ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
+                                          &tmprec, insert_range, &rec);
 
                split_rec = tmprec;
 
@@ -4610,7 +5010,7 @@ leftright:
                do_leftright = 1;
        }
 
-       ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
+       ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -4626,7 +5026,7 @@ leftright:
                ocfs2_reinit_path(path, 1);
 
                cpos = le32_to_cpu(split_rec.e_cpos);
-               ret = ocfs2_find_path(inode, path, cpos);
+               ret = ocfs2_find_path(et->et_ci, path, cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -4641,10 +5041,32 @@ out:
        return ret;
 }
 
+static int ocfs2_replace_extent_rec(handle_t *handle,
+                                   struct ocfs2_extent_tree *et,
+                                   struct ocfs2_path *path,
+                                   struct ocfs2_extent_list *el,
+                                   int split_index,
+                                   struct ocfs2_extent_rec *split_rec)
+{
+       int ret;
+
+       ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
+                                          path_num_items(path) - 1);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       el->l_recs[split_index] = *split_rec;
+
+       ocfs2_journal_dirty(handle, path_leaf_bh(path));
+out:
+       return ret;
+}
+
 /*
- * Mark part or all of the extent record at split_index in the leaf
- * pointed to by path as written. This removes the unwritten
- * extent flag.
+ * Split part or all of the extent record at split_index in the leaf
+ * pointed to by path. Merge with the contiguous extent record if needed.
  *
  * Care is taken to handle contiguousness so as to not grow the tree.
  *
@@ -4661,14 +5083,13 @@ out:
  * have been brought into cache (and pinned via the journal), so the
  * extra overhead is not expressed in terms of disk reads.
  */
-static int __ocfs2_mark_extent_written(struct inode *inode,
-                                      struct ocfs2_extent_tree *et,
-                                      handle_t *handle,
-                                      struct ocfs2_path *path,
-                                      int split_index,
-                                      struct ocfs2_extent_rec *split_rec,
-                                      struct ocfs2_alloc_context *meta_ac,
-                                      struct ocfs2_cached_dealloc_ctxt *dealloc)
+int ocfs2_split_extent(handle_t *handle,
+                      struct ocfs2_extent_tree *et,
+                      struct ocfs2_path *path,
+                      int split_index,
+                      struct ocfs2_extent_rec *split_rec,
+                      struct ocfs2_alloc_context *meta_ac,
+                      struct ocfs2_cached_dealloc_ctxt *dealloc)
 {
        int ret = 0;
        struct ocfs2_extent_list *el = path_leaf_el(path);
@@ -4677,12 +5098,6 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
        struct ocfs2_merge_ctxt ctxt;
        struct ocfs2_extent_list *rightmost_el;
 
-       if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
-               ret = -EIO;
-               mlog_errno(ret);
-               goto out;
-       }
-
        if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
            ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
             (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
@@ -4691,33 +5106,27 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
                goto out;
        }
 
-       ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
+       ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
                                                            split_index,
                                                            split_rec);
 
        /*
         * The core merge / split code wants to know how much room is
-        * left in this inodes allocation tree, so we pass the
+        * left in this allocation tree, so we pass the
         * rightmost extent list.
         */
        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_extent_block(et->et_ci,
+                                             ocfs2_et_get_last_eb_blk(et),
+                                             &last_eb_bh);
                if (ret) {
                        mlog_exit(ret);
                        goto out;
                }
 
                eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
-               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-                       ret = -EROFS;
-                       goto out;
-               }
-
                rightmost_el = &eb->h_list;
        } else
                rightmost_el = path_root_el(path);
@@ -4736,50 +5145,138 @@ static int __ocfs2_mark_extent_written(struct inode *inode,
 
        if (ctxt.c_contig_type == CONTIG_NONE) {
                if (ctxt.c_split_covers_rec)
-                       el->l_recs[split_index] = *split_rec;
+                       ret = ocfs2_replace_extent_rec(handle, et, path, el,
+                                                      split_index, split_rec);
                else
-                       ret = ocfs2_split_and_insert(inode, handle, path, et,
+                       ret = ocfs2_split_and_insert(handle, et, path,
                                                     &last_eb_bh, split_index,
                                                     split_rec, meta_ac);
                if (ret)
                        mlog_errno(ret);
        } else {
-               ret = ocfs2_try_to_merge_extent(inode, handle, path,
+               ret = ocfs2_try_to_merge_extent(handle, et, path,
                                                split_index, split_rec,
-                                               dealloc, &ctxt, et);
+                                               dealloc, &ctxt);
                if (ret)
                        mlog_errno(ret);
        }
 
 out:
-       brelse(last_eb_bh);
+       brelse(last_eb_bh);
+       return ret;
+}
+
+/*
+ * Change the flags of the already-existing extent at cpos for len clusters.
+ *
+ * new_flags: the flags we want to set.
+ * clear_flags: the flags we want to clear.
+ * phys: the new physical offset we want this new extent starts from.
+ *
+ * If the existing extent is larger than the request, initiate a
+ * split. An attempt will be made at merging with adjacent extents.
+ *
+ * The caller is responsible for passing down meta_ac if we'll need it.
+ */
+int ocfs2_change_extent_flag(handle_t *handle,
+                            struct ocfs2_extent_tree *et,
+                            u32 cpos, u32 len, u32 phys,
+                            struct ocfs2_alloc_context *meta_ac,
+                            struct ocfs2_cached_dealloc_ctxt *dealloc,
+                            int new_flags, int clear_flags)
+{
+       int ret, index;
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
+       u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
+       struct ocfs2_extent_rec split_rec;
+       struct ocfs2_path *left_path = NULL;
+       struct ocfs2_extent_list *el;
+       struct ocfs2_extent_rec *rec;
+
+       left_path = ocfs2_new_path_from_et(et);
+       if (!left_path) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
+
+       ret = ocfs2_find_path(et->et_ci, left_path, cpos);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+       el = path_leaf_el(left_path);
+
+       index = ocfs2_search_extent_list(el, cpos);
+       if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
+               ocfs2_error(sb,
+                           "Owner %llu has an extent at cpos %u which can no "
+                           "longer be found.\n",
+                            (unsigned long long)
+                            ocfs2_metadata_cache_owner(et->et_ci), cpos);
+               ret = -EROFS;
+               goto out;
+       }
+
+       ret = -EIO;
+       rec = &el->l_recs[index];
+       if (new_flags && (rec->e_flags & new_flags)) {
+               mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
+                    "extent that already had them",
+                    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+                    new_flags);
+               goto out;
+       }
+
+       if (clear_flags && !(rec->e_flags & clear_flags)) {
+               mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
+                    "extent that didn't have them",
+                    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+                    clear_flags);
+               goto out;
+       }
+
+       memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
+       split_rec.e_cpos = cpu_to_le32(cpos);
+       split_rec.e_leaf_clusters = cpu_to_le16(len);
+       split_rec.e_blkno = cpu_to_le64(start_blkno);
+       split_rec.e_flags = rec->e_flags;
+       if (new_flags)
+               split_rec.e_flags |= new_flags;
+       if (clear_flags)
+               split_rec.e_flags &= ~clear_flags;
+
+       ret = ocfs2_split_extent(handle, et, left_path,
+                                index, &split_rec, meta_ac,
+                                dealloc);
+       if (ret)
+               mlog_errno(ret);
+
+out:
+       ocfs2_free_path(left_path);
        return ret;
+
 }
 
 /*
  * Mark the already-existing extent at cpos as written for len clusters.
+ * This removes the unwritten extent flag.
  *
  * If the existing extent is larger than the request, initiate a
  * split. An attempt will be made at merging with adjacent extents.
  *
  * 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;
+       int ret;
 
-       mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
-            inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
+       mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
+            inode->i_ino, cpos, len, phys);
 
        if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
                ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
@@ -4790,66 +5287,24 @@ 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.
         */
-       if (et_type == OCFS2_DINODE_EXTENT)
-               ocfs2_extent_map_trunc(inode, 0);
-
-       left_path = ocfs2_new_path(et->root_bh, et->root_el);
-       if (!left_path) {
-               ret = -ENOMEM;
-               mlog_errno(ret);
-               goto out;
-       }
-
-       ret = ocfs2_find_path(inode, left_path, cpos);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-       el = path_leaf_el(left_path);
-
-       index = ocfs2_search_extent_list(el, cpos);
-       if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
-               ocfs2_error(inode->i_sb,
-                           "Inode %llu has an extent at cpos %u which can no "
-                           "longer be found.\n",
-                           (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
-               ret = -EROFS;
-               goto out;
-       }
-
-       memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
-       split_rec.e_cpos = cpu_to_le32(cpos);
-       split_rec.e_leaf_clusters = cpu_to_le16(len);
-       split_rec.e_blkno = cpu_to_le64(start_blkno);
-       split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
-       split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
+       ocfs2_et_extent_map_truncate(et, 0);
 
-       ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
-                                         index, &split_rec, meta_ac,
-                                         dealloc);
+       ret = ocfs2_change_extent_flag(handle, et, cpos,
+                                      len, phys, meta_ac, dealloc,
+                                      0, OCFS2_EXT_UNWRITTEN);
        if (ret)
                mlog_errno(ret);
 
 out:
-       ocfs2_free_path(left_path);
-       if (et)
-               ocfs2_free_extent_tree(et);
        return ret;
 }
 
-static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
-                           handle_t *handle, struct ocfs2_path *path,
+static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
+                           struct ocfs2_path *path,
                            int index, u32 new_range,
                            struct ocfs2_alloc_context *meta_ac)
 {
@@ -4866,13 +5321,14 @@ static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
         */
        el = path_leaf_el(path);
        rec = &el->l_recs[index];
-       ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
+       ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
+                                  &split_rec, new_range, rec);
 
        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_extent_block(et->et_ci,
+                                             ocfs2_et_get_last_eb_blk(et),
+                                             &last_eb_bh);
                if (ret < 0) {
                        mlog_errno(ret);
                        goto out;
@@ -4884,7 +5340,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);
@@ -4893,7 +5349,7 @@ static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
 
        if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
            le16_to_cpu(rightmost_el->l_count)) {
-               ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
+               ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
                                      meta_ac);
                if (ret) {
                        mlog_errno(ret);
@@ -4907,7 +5363,7 @@ static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
        insert.ins_split = SPLIT_RIGHT;
        insert.ins_tree_depth = depth;
 
-       ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
+       ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
        if (ret)
                mlog_errno(ret);
 
@@ -4916,23 +5372,23 @@ out:
        return ret;
 }
 
-static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
+static int ocfs2_truncate_rec(handle_t *handle,
+                             struct ocfs2_extent_tree *et,
                              struct ocfs2_path *path, int index,
                              struct ocfs2_cached_dealloc_ctxt *dealloc,
-                             u32 cpos, u32 len,
-                             struct ocfs2_extent_tree *et)
+                             u32 cpos, u32 len)
 {
        int ret;
        u32 left_cpos, rec_range, trunc_range;
        int wants_rotate = 0, is_rightmost_tree_rec = 0;
-       struct super_block *sb = inode->i_sb;
+       struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
        struct ocfs2_path *left_path = NULL;
        struct ocfs2_extent_list *el = path_leaf_el(path);
        struct ocfs2_extent_rec *rec;
        struct ocfs2_extent_block *eb;
 
        if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
-               ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
+               ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -4964,29 +5420,28 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
                 * by this leaf and the one to it's left.
                 *
                 * There are two cases we can skip:
-                *   1) Path is the leftmost one in our inode tree.
+                *   1) Path is the leftmost one in our btree.
                 *   2) The leaf is rightmost and will be empty after
                 *      we remove the extent record - the rotate code
                 *      knows how to update the newly formed edge.
                 */
 
-               ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
-                                                   &left_cpos);
+               ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
                if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
-                       left_path = ocfs2_new_path(path_root_bh(path),
-                                                  path_root_el(path));
+                       left_path = ocfs2_new_path_from_path(path);
                        if (!left_path) {
                                ret = -ENOMEM;
                                mlog_errno(ret);
                                goto out;
                        }
 
-                       ret = ocfs2_find_path(inode, left_path, left_cpos);
+                       ret = ocfs2_find_path(et->et_ci, left_path,
+                                             left_cpos);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -5002,13 +5457,13 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
                goto out;
        }
 
-       ret = ocfs2_journal_access_path(inode, handle, path);
+       ret = ocfs2_journal_access_path(et->et_ci, handle, path);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_journal_access_path(inode, handle, left_path);
+       ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -5031,7 +5486,7 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
                         * be deleted by the rotate code.
                         */
                        rec = &el->l_recs[next_free - 1];
-                       ocfs2_adjust_rightmost_records(inode, handle, path,
+                       ocfs2_adjust_rightmost_records(handle, et, path,
                                                       rec);
                }
        } else if (le32_to_cpu(rec->e_cpos) == cpos) {
@@ -5043,11 +5498,12 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
                /* Remove rightmost portion of the record */
                le16_add_cpu(&rec->e_leaf_clusters, -len);
                if (is_rightmost_tree_rec)
-                       ocfs2_adjust_rightmost_records(inode, handle, path, rec);
+                       ocfs2_adjust_rightmost_records(handle, et, path, rec);
        } else {
                /* Caller should have trapped this. */
-               mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
-                    "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
+               mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
+                    "(%u, %u)\n",
+                    (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                     le32_to_cpu(rec->e_cpos),
                     le16_to_cpu(rec->e_leaf_clusters), cpos, len);
                BUG();
@@ -5056,14 +5512,14 @@ static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
        if (left_path) {
                int subtree_index;
 
-               subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
-               ocfs2_complete_edge_insert(inode, handle, left_path, path,
+               subtree_index = ocfs2_find_subtree_root(et, left_path, path);
+               ocfs2_complete_edge_insert(handle, left_path, path,
                                           subtree_index);
        }
 
        ocfs2_journal_dirty(handle, path_leaf_bh(path));
 
-       ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
+       ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -5074,37 +5530,32 @@ out:
        return ret;
 }
 
-int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
-                       u32 cpos, u32 len, handle_t *handle,
+int ocfs2_remove_extent(handle_t *handle,
+                       struct ocfs2_extent_tree *et,
+                       u32 cpos, u32 len,
                        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);
+       /*
+        * XXX: Why are we truncating to 0 instead of wherever this
+        * affects us?
+        */
+       ocfs2_et_extent_map_truncate(et, 0);
 
-       path = ocfs2_new_path(et->root_bh, et->root_el);
+       path = ocfs2_new_path_from_et(et);
        if (!path) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_find_path(inode, path, cpos);
+       ret = ocfs2_find_path(et->et_ci, path, cpos);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -5113,10 +5564,11 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
        el = path_leaf_el(path);
        index = ocfs2_search_extent_list(el, cpos);
        if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
-               ocfs2_error(inode->i_sb,
-                           "Inode %llu has an extent at cpos %u which can no "
+               ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                           "Owner %llu has an extent at cpos %u which can no "
                            "longer be found.\n",
-                           (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
+                           (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+                           cpos);
                ret = -EROFS;
                goto out;
        }
@@ -5143,20 +5595,21 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
 
        BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
 
-       mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
+       mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
             "(cpos %u, len %u)\n",
-            (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
+            (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
+            cpos, len, index,
             le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
 
        if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
-               ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
-                                        cpos, len, et);
+               ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
+                                        cpos, len);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
        } else {
-               ret = ocfs2_split_tree(inode, et, handle, path, index,
+               ret = ocfs2_split_tree(handle, et, path, index,
                                       trunc_range, meta_ac);
                if (ret) {
                        mlog_errno(ret);
@@ -5169,7 +5622,7 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
                 */
                ocfs2_reinit_path(path, 1);
 
-               ret = ocfs2_find_path(inode, path, cpos);
+               ret = ocfs2_find_path(et->et_ci, path, cpos);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -5178,9 +5631,9 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
                el = path_leaf_el(path);
                index = ocfs2_search_extent_list(el, cpos);
                if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu: split at cpos %u lost record.",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu: split at cpos %u lost record.",
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                                    cpos);
                        ret = -EROFS;
                        goto out;
@@ -5194,18 +5647,18 @@ int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
                rec_range = le32_to_cpu(rec->e_cpos) +
                        ocfs2_rec_clusters(el, rec);
                if (rec_range != trunc_range) {
-                       ocfs2_error(inode->i_sb,
-                                   "Inode %llu: error after split at cpos %u"
+                       ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
+                                   "Owner %llu: error after split at cpos %u"
                                    "trunc len %u, existing record is (%u,%u)",
-                                   (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                                   (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
                                    cpos, len, le32_to_cpu(rec->e_cpos),
                                    ocfs2_rec_clusters(el, rec));
                        ret = -EROFS;
                        goto out;
                }
 
-               ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
-                                        cpos, len, et);
+               ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
+                                        cpos, len);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -5214,8 +5667,80 @@ 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;
+}
+
+int ocfs2_remove_btree_range(struct inode *inode,
+                            struct ocfs2_extent_tree *et,
+                            u32 cpos, u32 phys_cpos, u32 len,
+                            struct ocfs2_cached_dealloc_ctxt *dealloc)
+{
+       int ret;
+       u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct inode *tl_inode = osb->osb_tl_inode;
+       handle_t *handle;
+       struct ocfs2_alloc_context *meta_ac = NULL;
+
+       ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
+       if (ret) {
+               mlog_errno(ret);
+               return ret;
+       }
+
+       mutex_lock(&tl_inode->i_mutex);
+
+       if (ocfs2_truncate_log_needs_flush(osb)) {
+               ret = __ocfs2_flush_truncate_log(osb);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+       }
+
+       handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
+       if (IS_ERR(handle)) {
+               ret = PTR_ERR(handle);
+               mlog_errno(ret);
+               goto out;
+       }
+
+       ret = ocfs2_et_root_journal_access(handle, et,
+                                          OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       vfs_dq_free_space_nodirty(inode,
+                                 ocfs2_clusters_to_bytes(inode->i_sb, len));
+
+       ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
+       if (ret) {
+               mlog_errno(ret);
+               goto out_commit;
+       }
+
+       ocfs2_et_update_clusters(et, -len);
+
+       ret = ocfs2_journal_dirty(handle, et->et_root_bh);
+       if (ret) {
+               mlog_errno(ret);
+               goto out_commit;
+       }
+
+       ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
+       if (ret)
+               mlog_errno(ret);
+
+out_commit:
+       ocfs2_commit_trans(osb, handle);
+out:
+       mutex_unlock(&tl_inode->i_mutex);
+
+       if (meta_ac)
+               ocfs2_free_alloc_context(meta_ac);
+
        return ret;
 }
 
@@ -5272,13 +5797,13 @@ int ocfs2_truncate_log_append(struct ocfs2_super *osb,
        start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
 
        di = (struct ocfs2_dinode *) tl_bh->b_data;
-       tl = &di->id2.i_dealloc;
-       if (!OCFS2_IS_VALID_DINODE(di)) {
-               OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
-               status = -EIO;
-               goto bail;
-       }
 
+       /* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
+        * by the underlying call to ocfs2_read_inode_block(), so any
+        * corruption is a code bug */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(di));
+
+       tl = &di->id2.i_dealloc;
        tl_count = le16_to_cpu(tl->tl_count);
        mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
                        tl_count == 0,
@@ -5296,8 +5821,8 @@ int ocfs2_truncate_log_append(struct ocfs2_super *osb,
                goto bail;
        }
 
-       status = ocfs2_journal_access(handle, tl_inode, tl_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -5358,8 +5883,8 @@ static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
        while (i >= 0) {
                /* Caller has given us at least enough credits to
                 * update the truncate log dinode */
-               status = ocfs2_journal_access(handle, tl_inode, tl_bh,
-                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
+                                                OCFS2_JOURNAL_ACCESS_WRITE);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -5428,13 +5953,13 @@ int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
        BUG_ON(mutex_trylock(&tl_inode->i_mutex));
 
        di = (struct ocfs2_dinode *) tl_bh->b_data;
-       tl = &di->id2.i_dealloc;
-       if (!OCFS2_IS_VALID_DINODE(di)) {
-               OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
-               status = -EIO;
-               goto out;
-       }
 
+       /* tl_bh is loaded from ocfs2_truncate_log_init().  It's validated
+        * by the underlying call to ocfs2_read_inode_block(), so any
+        * corruption is a code bug */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(di));
+
+       tl = &di->id2.i_dealloc;
        num_to_flush = le16_to_cpu(tl->tl_used);
        mlog(0, "Flush %u records from truncate log #%llu\n",
             num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
@@ -5550,8 +6075,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_inode_block(inode, &bh);
        if (status < 0) {
                iput(inode);
                mlog_errno(status);
@@ -5590,13 +6114,13 @@ int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
        }
 
        di = (struct ocfs2_dinode *) tl_bh->b_data;
-       tl = &di->id2.i_dealloc;
-       if (!OCFS2_IS_VALID_DINODE(di)) {
-               OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
-               status = -EIO;
-               goto bail;
-       }
 
+       /* tl_bh is loaded from ocfs2_get_truncate_log_info().  It's
+        * validated by the underlying call to ocfs2_read_inode_block(),
+        * so any corruption is a code bug */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(di));
+
+       tl = &di->id2.i_dealloc;
        if (le16_to_cpu(tl->tl_used)) {
                mlog(0, "We'll have %u logs to recover\n",
                     le16_to_cpu(tl->tl_used));
@@ -5616,7 +6140,8 @@ int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
                 * tl_used. */
                tl->tl_used = 0;
 
-               status = ocfs2_write_block(osb, tl_bh, tl_inode);
+               ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
+               status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -5626,8 +6151,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);
@@ -5766,7 +6290,10 @@ int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  */
 
 /*
- * Describes a single block free from a suballocator
+ * Describe a single bit freed from a suballocator.  For the block
+ * suballocators, it represents one block.  For the global cluster
+ * allocator, it represents some clusters and free_bit indicates
+ * clusters number.
  */
 struct ocfs2_cached_block_free {
        struct ocfs2_cached_block_free          *free_next;
@@ -5781,10 +6308,10 @@ struct ocfs2_per_slot_free_list {
        struct ocfs2_cached_block_free          *f_first;
 };
 
-static int ocfs2_free_cached_items(struct ocfs2_super *osb,
-                                  int sysfile_type,
-                                  int slot,
-                                  struct ocfs2_cached_block_free *head)
+static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
+                                   int sysfile_type,
+                                   int slot,
+                                   struct ocfs2_cached_block_free *head)
 {
        int ret;
        u64 bg_blkno;
@@ -5859,6 +6386,82 @@ out:
        return ret;
 }
 
+int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
+                               u64 blkno, unsigned int bit)
+{
+       int ret = 0;
+       struct ocfs2_cached_block_free *item;
+
+       item = kmalloc(sizeof(*item), GFP_NOFS);
+       if (item == NULL) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               return ret;
+       }
+
+       mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
+            bit, (unsigned long long)blkno);
+
+       item->free_blk = blkno;
+       item->free_bit = bit;
+       item->free_next = ctxt->c_global_allocator;
+
+       ctxt->c_global_allocator = item;
+       return ret;
+}
+
+static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
+                                     struct ocfs2_cached_block_free *head)
+{
+       struct ocfs2_cached_block_free *tmp;
+       struct inode *tl_inode = osb->osb_tl_inode;
+       handle_t *handle;
+       int ret = 0;
+
+       mutex_lock(&tl_inode->i_mutex);
+
+       while (head) {
+               if (ocfs2_truncate_log_needs_flush(osb)) {
+                       ret = __ocfs2_flush_truncate_log(osb);
+                       if (ret < 0) {
+                               mlog_errno(ret);
+                               break;
+                       }
+               }
+
+               handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
+               if (IS_ERR(handle)) {
+                       ret = PTR_ERR(handle);
+                       mlog_errno(ret);
+                       break;
+               }
+
+               ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
+                                               head->free_bit);
+
+               ocfs2_commit_trans(osb, handle);
+               tmp = head;
+               head = head->free_next;
+               kfree(tmp);
+
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       break;
+               }
+       }
+
+       mutex_unlock(&tl_inode->i_mutex);
+
+       while (head) {
+               /* Premature exit may have left some dangling items. */
+               tmp = head;
+               head = head->free_next;
+               kfree(tmp);
+       }
+
+       return ret;
+}
+
 int ocfs2_run_deallocs(struct ocfs2_super *osb,
                       struct ocfs2_cached_dealloc_ctxt *ctxt)
 {
@@ -5874,8 +6477,10 @@ int ocfs2_run_deallocs(struct ocfs2_super *osb,
                if (fl->f_first) {
                        mlog(0, "Free items: (type %u, slot %d)\n",
                             fl->f_inode_type, fl->f_slot);
-                       ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
-                                                      fl->f_slot, fl->f_first);
+                       ret2 = ocfs2_free_cached_blocks(osb,
+                                                       fl->f_inode_type,
+                                                       fl->f_slot,
+                                                       fl->f_first);
                        if (ret2)
                                mlog_errno(ret2);
                        if (!ret)
@@ -5886,6 +6491,17 @@ int ocfs2_run_deallocs(struct ocfs2_super *osb,
                kfree(fl);
        }
 
+       if (ctxt->c_global_allocator) {
+               ret2 = ocfs2_free_cached_clusters(osb,
+                                                 ctxt->c_global_allocator);
+               if (ret2)
+                       mlog_errno(ret2);
+               if (!ret)
+                       ret = ret2;
+
+               ctxt->c_global_allocator = NULL;
+       }
+
        return ret;
 }
 
@@ -5915,9 +6531,9 @@ ocfs2_find_per_slot_free_list(int type,
        return fl;
 }
 
-static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
-                                    int type, int slot, u64 blkno,
-                                    unsigned int bit)
+int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
+                             int type, int slot, u64 blkno,
+                             unsigned int bit)
 {
        int ret;
        struct ocfs2_per_slot_free_list *fl;
@@ -6033,7 +6649,7 @@ static int ocfs2_find_new_last_ext_blk(struct inode *inode,
                goto out;
        }
 
-       ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
+       ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -6041,11 +6657,10 @@ static int ocfs2_find_new_last_ext_blk(struct inode *inode,
 
        eb = (struct ocfs2_extent_block *) bh->b_data;
        el = &eb->h_list;
-       if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-               ret = -EROFS;
-               goto out;
-       }
+
+       /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
+        * Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
 
        *new_last_eb = bh;
        get_bh(*new_last_eb);
@@ -6067,7 +6682,7 @@ out:
  */
 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
                           handle_t *handle, struct ocfs2_truncate_context *tc,
-                          u32 clusters_to_del, u64 *delete_start)
+                          u32 clusters_to_del, u64 *delete_start, u8 *flags)
 {
        int ret, i, index = path->p_tree_depth;
        u32 new_edge = 0;
@@ -6077,6 +6692,7 @@ static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
        struct ocfs2_extent_rec *rec;
 
        *delete_start = 0;
+       *flags = 0;
 
        while (index >= 0) {
                bh = path->p_node[index].bh;
@@ -6164,6 +6780,7 @@ find_tail_record:
                        *delete_start = le64_to_cpu(rec->e_blkno)
                                + ocfs2_clusters_to_blocks(inode->i_sb,
                                        le16_to_cpu(rec->e_leaf_clusters));
+                       *flags = rec->e_flags;
 
                        /*
                         * If it's now empty, remove this record.
@@ -6235,7 +6852,7 @@ delete:
 
                        mlog(0, "deleting this extent block.\n");
 
-                       ocfs2_remove_from_cache(inode, bh);
+                       ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
 
                        BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
                        BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
@@ -6263,7 +6880,8 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
                             struct buffer_head *fe_bh,
                             handle_t *handle,
                             struct ocfs2_truncate_context *tc,
-                            struct ocfs2_path *path)
+                            struct ocfs2_path *path,
+                            struct ocfs2_alloc_context *meta_ac)
 {
        int status;
        struct ocfs2_dinode *fe;
@@ -6271,6 +6889,7 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        struct ocfs2_extent_list *el;
        struct buffer_head *last_eb_bh = NULL;
        u64 delete_blk = 0;
+       u8 rec_flags;
 
        fe = (struct ocfs2_dinode *) fe_bh->b_data;
 
@@ -6285,15 +6904,15 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
         * Each component will be touched, so we might as well journal
         * here to avoid having to handle errors later.
         */
-       status = ocfs2_journal_access_path(inode, handle, path);
+       status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
 
        if (last_eb_bh) {
-               status = ocfs2_journal_access(handle, inode, last_eb_bh,
-                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
+                                                OCFS2_JOURNAL_ACCESS_WRITE);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -6316,6 +6935,8 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
                goto bail;
        }
 
+       vfs_dq_free_space_nodirty(inode,
+                       ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
        spin_lock(&OCFS2_I(inode)->ip_lock);
        OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
                                      clusters_to_del;
@@ -6324,7 +6945,7 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        inode->i_blocks = ocfs2_inode_sector_count(inode);
 
        status = ocfs2_trim_tree(inode, path, handle, tc,
-                                clusters_to_del, &delete_blk);
+                                clusters_to_del, &delete_blk, &rec_flags);
        if (status) {
                mlog_errno(status);
                goto bail;
@@ -6356,8 +6977,16 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        }
 
        if (delete_blk) {
-               status = ocfs2_truncate_log_append(osb, handle, delete_blk,
-                                                  clusters_to_del);
+               if (rec_flags & OCFS2_EXT_REFCOUNTED)
+                       status = ocfs2_decrease_refcount(inode, handle,
+                                       ocfs2_blocks_to_clusters(osb->sb,
+                                                                delete_blk),
+                                       clusters_to_del, meta_ac,
+                                       &tc->tc_dealloc, 1);
+               else
+                       status = ocfs2_truncate_log_append(osb, handle,
+                                                          delete_blk,
+                                                          clusters_to_del);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -6365,28 +6994,21 @@ static int ocfs2_do_truncate(struct ocfs2_super *osb,
        }
        status = 0;
 bail:
-
+       brelse(last_eb_bh);
        mlog_exit(status);
        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)
+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)
 {
        int ret, partial = 0;
 
@@ -6402,17 +7024,13 @@ 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_writeback_zero_func);
+       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);
                if (ret < 0)
                        mlog_errno(ret);
        }
@@ -6458,20 +7076,16 @@ out:
                ocfs2_unlock_and_free_pages(pages, numpages);
 }
 
-static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
-                               struct page **pages, int *num)
+int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
+                    struct page **pages, int *num)
 {
        int numpages, ret = 0;
-       struct super_block *sb = inode->i_sb;
        struct address_space *mapping = inode->i_mapping;
        unsigned long index;
        loff_t last_page_bytes;
 
        BUG_ON(start > end);
 
-       BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
-              (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
-
        numpages = 0;
        last_page_bytes = PAGE_ALIGN(end);
        index = start >> PAGE_CACHE_SHIFT;
@@ -6499,6 +7113,17 @@ out:
        return ret;
 }
 
+static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
+                               struct page **pages, int *num)
+{
+       struct super_block *sb = inode->i_sb;
+
+       BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
+              (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
+
+       return ocfs2_grab_pages(inode, start, end, pages, num);
+}
+
 /*
  * Zero the area past i_size but still within an allocated
  * cluster. This avoids exposing nonzero data on subsequent file
@@ -6634,6 +7259,8 @@ 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;
+       int did_quota = 0;
 
        has_data = i_size_read(inode) ? 1 : 0;
 
@@ -6653,15 +7280,16 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
                }
        }
 
-       handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
+       handle = ocfs2_start_trans(osb,
+                                  ocfs2_inline_to_extents_credits(osb->sb));
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
                mlog_errno(ret);
                goto out_unlock;
        }
 
-       ret = ocfs2_journal_access(handle, inode, di_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -6672,6 +7300,13 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
                unsigned int page_end;
                u64 phys;
 
+               if (vfs_dq_alloc_space_nodirty(inode,
+                                      ocfs2_clusters_to_bytes(osb->sb, 1))) {
+                       ret = -EDQUOT;
+                       goto out_commit;
+               }
+               did_quota = 1;
+
                ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
                                           &num);
                if (ret) {
@@ -6733,8 +7368,8 @@ 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_CACHE(inode), di_bh);
+               ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
                if (ret) {
                        mlog_errno(ret);
                        goto out_commit;
@@ -6744,6 +7379,10 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
        }
 
 out_commit:
+       if (ret < 0 && did_quota)
+               vfs_dq_free_space_nodirty(inode,
+                                         ocfs2_clusters_to_bytes(osb->sb, 1));
+
        ocfs2_commit_trans(osb, handle);
 
 out_unlock:
@@ -6772,18 +7411,22 @@ int ocfs2_commit_truncate(struct ocfs2_super *osb,
 {
        int status, i, credits, tl_sem = 0;
        u32 clusters_to_del, new_highest_cpos, range;
+       u64 blkno = 0;
        struct ocfs2_extent_list *el;
        handle_t *handle = NULL;
        struct inode *tl_inode = osb->osb_tl_inode;
        struct ocfs2_path *path = NULL;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
+       struct ocfs2_alloc_context *meta_ac = NULL;
+       struct ocfs2_refcount_tree *ref_tree = NULL;
 
        mlog_entry_void();
 
        new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
                                                     i_size_read(inode));
 
-       path = ocfs2_new_path(fe_bh, &di->id2.i_list);
+       path = ocfs2_new_path(fe_bh, &di->id2.i_list,
+                             ocfs2_journal_access_di);
        if (!path) {
                status = -ENOMEM;
                mlog_errno(status);
@@ -6801,10 +7444,12 @@ start:
                goto bail;
        }
 
+       credits = 0;
+
        /*
         * Truncate always works against the rightmost tree branch.
         */
-       status = ocfs2_find_path(inode, path, UINT_MAX);
+       status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
        if (status) {
                mlog_errno(status);
                goto bail;
@@ -6841,10 +7486,15 @@ start:
                clusters_to_del = 0;
        } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
                clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
+               blkno = le64_to_cpu(el->l_recs[i].e_blkno);
        } else if (range > new_highest_cpos) {
                clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
                                   le32_to_cpu(el->l_recs[i].e_cpos)) -
                                  new_highest_cpos;
+               blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
+                       ocfs2_clusters_to_blocks(inode->i_sb,
+                               ocfs2_rec_clusters(el, &el->l_recs[i]) -
+                               clusters_to_del);
        } else {
                status = 0;
                goto bail;
@@ -6853,6 +7503,29 @@ start:
        mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
             clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
 
+       if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
+               BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
+                        OCFS2_HAS_REFCOUNT_FL));
+
+               status = ocfs2_lock_refcount_tree(osb,
+                                               le64_to_cpu(di->i_refcount_loc),
+                                               1, &ref_tree, NULL);
+               if (status) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
+                                                              blkno,
+                                                              clusters_to_del,
+                                                              &credits,
+                                                              &meta_ac);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
        mutex_lock(&tl_inode->i_mutex);
        tl_sem = 1;
        /* ocfs2_truncate_log_needs_flush guarantees us at least one
@@ -6866,7 +7539,7 @@ start:
                }
        }
 
-       credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
+       credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
                                                (struct ocfs2_dinode *)fe_bh->b_data,
                                                el);
        handle = ocfs2_start_trans(osb, credits);
@@ -6878,7 +7551,7 @@ start:
        }
 
        status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
-                                  tc, path);
+                                  tc, path, meta_ac);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -6892,6 +7565,16 @@ start:
 
        ocfs2_reinit_path(path, 1);
 
+       if (meta_ac) {
+               ocfs2_free_alloc_context(meta_ac);
+               meta_ac = NULL;
+       }
+
+       if (ref_tree) {
+               ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
+               ref_tree = NULL;
+       }
+
        /*
         * The check above will catch the case where we've truncated
         * away all allocation.
@@ -6908,6 +7591,12 @@ bail:
        if (handle)
                ocfs2_commit_trans(osb, handle);
 
+       if (meta_ac)
+               ocfs2_free_alloc_context(meta_ac);
+
+       if (ref_tree)
+               ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
+
        ocfs2_run_deallocs(osb, &tc->tc_dealloc);
 
        ocfs2_free_path(path);
@@ -6954,20 +7643,14 @@ 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_extent_block(INODE_CACHE(inode),
+                                                le64_to_cpu(fe->i_last_eb_blk),
+                                                &last_eb_bh);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
                }
                eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
-               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
-                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
-
-                       brelse(last_eb_bh);
-                       status = -EIO;
-                       goto bail;
-               }
        }
 
        (*tc)->tc_last_eb_bh = last_eb_bh;
@@ -7022,8 +7705,8 @@ int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
                goto out;
        }
 
-       ret = ocfs2_journal_access(handle, inode, di_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -7068,8 +7751,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);
 }