btrfs: return EUCLEAN if extent_inline_ref type is invalid
[sfrench/cifs-2.6.git] / fs / btrfs / extent-tree.c
index 3d9fe58c0080db89f4933f162f8bf7f8d6df1c85..b28fa3d20ef72f62633311aef3efe558bb56ba0b 100644 (file)
@@ -52,24 +52,21 @@ enum {
 };
 
 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
-                              struct btrfs_fs_info *fs_info,
-                               struct btrfs_delayed_ref_node *node, u64 parent,
-                               u64 root_objectid, u64 owner_objectid,
-                               u64 owner_offset, int refs_to_drop,
-                               struct btrfs_delayed_extent_op *extra_op);
+                              struct btrfs_delayed_ref_node *node, u64 parent,
+                              u64 root_objectid, u64 owner_objectid,
+                              u64 owner_offset, int refs_to_drop,
+                              struct btrfs_delayed_extent_op *extra_op);
 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
                                    struct extent_buffer *leaf,
                                    struct btrfs_extent_item *ei);
 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
-                                     struct btrfs_fs_info *fs_info,
                                      u64 parent, u64 root_objectid,
                                      u64 flags, u64 owner, u64 offset,
                                      struct btrfs_key *ins, int ref_mod);
 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
                                     struct btrfs_delayed_ref_node *node,
                                     struct btrfs_delayed_extent_op *extent_op);
-static int do_chunk_alloc(struct btrfs_trans_handle *trans,
-                         struct btrfs_fs_info *fs_info, u64 flags,
+static int do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
                          int force);
 static int find_next_key(struct btrfs_path *path, int level,
                         struct btrfs_key *key);
@@ -220,9 +217,9 @@ static int add_excluded_extent(struct btrfs_fs_info *fs_info,
        return 0;
 }
 
-static void free_excluded_extents(struct btrfs_fs_info *fs_info,
-                                 struct btrfs_block_group_cache *cache)
+static void free_excluded_extents(struct btrfs_block_group_cache *cache)
 {
+       struct btrfs_fs_info *fs_info = cache->fs_info;
        u64 start, end;
 
        start = cache->key.objectid;
@@ -234,9 +231,9 @@ static void free_excluded_extents(struct btrfs_fs_info *fs_info,
                          start, end, EXTENT_UPTODATE);
 }
 
-static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
-                                struct btrfs_block_group_cache *cache)
+static int exclude_super_stripes(struct btrfs_block_group_cache *cache)
 {
+       struct btrfs_fs_info *fs_info = cache->fs_info;
        u64 bytenr;
        u64 *logical;
        int stripe_len;
@@ -558,7 +555,7 @@ static noinline void caching_thread(struct btrfs_work *work)
        caching_ctl->progress = (u64)-1;
 
        up_read(&fs_info->commit_root_sem);
-       free_excluded_extents(fs_info, block_group);
+       free_excluded_extents(block_group);
        mutex_unlock(&caching_ctl->mutex);
 
        wake_up(&caching_ctl->wait);
@@ -666,7 +663,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
                wake_up(&caching_ctl->wait);
                if (ret == 1) {
                        put_caching_control(caching_ctl);
-                       free_excluded_extents(fs_info, cache);
+                       free_excluded_extents(cache);
                        return 0;
                }
        } else {
@@ -1216,13 +1213,12 @@ static int match_extent_data_ref(struct extent_buffer *leaf,
 }
 
 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
-                                          struct btrfs_fs_info *fs_info,
                                           struct btrfs_path *path,
                                           u64 bytenr, u64 parent,
                                           u64 root_objectid,
                                           u64 owner, u64 offset)
 {
-       struct btrfs_root *root = fs_info->extent_root;
+       struct btrfs_root *root = trans->fs_info->extent_root;
        struct btrfs_key key;
        struct btrfs_extent_data_ref *ref;
        struct extent_buffer *leaf;
@@ -1304,13 +1300,12 @@ fail:
 }
 
 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
-                                          struct btrfs_fs_info *fs_info,
                                           struct btrfs_path *path,
                                           u64 bytenr, u64 parent,
                                           u64 root_objectid, u64 owner,
                                           u64 offset, int refs_to_add)
 {
-       struct btrfs_root *root = fs_info->extent_root;
+       struct btrfs_root *root = trans->fs_info->extent_root;
        struct btrfs_key key;
        struct extent_buffer *leaf;
        u32 size;
@@ -1384,7 +1379,6 @@ fail:
 }
 
 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
-                                          struct btrfs_fs_info *fs_info,
                                           struct btrfs_path *path,
                                           int refs_to_drop, int *last_ref)
 {
@@ -1421,7 +1415,7 @@ static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
        num_refs -= refs_to_drop;
 
        if (num_refs == 0) {
-               ret = btrfs_del_item(trans, fs_info->extent_root, path);
+               ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
                *last_ref = 1;
        } else {
                if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
@@ -1489,12 +1483,11 @@ static noinline u32 extent_data_ref_count(struct btrfs_path *path,
 }
 
 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
-                                         struct btrfs_fs_info *fs_info,
                                          struct btrfs_path *path,
                                          u64 bytenr, u64 parent,
                                          u64 root_objectid)
 {
-       struct btrfs_root *root = fs_info->extent_root;
+       struct btrfs_root *root = trans->fs_info->extent_root;
        struct btrfs_key key;
        int ret;
 
@@ -1523,7 +1516,6 @@ static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
 }
 
 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
-                                         struct btrfs_fs_info *fs_info,
                                          struct btrfs_path *path,
                                          u64 bytenr, u64 parent,
                                          u64 root_objectid)
@@ -1540,7 +1532,7 @@ static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
                key.offset = root_objectid;
        }
 
-       ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
+       ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
                                      path, &key, 0);
        btrfs_release_path(path);
        return ret;
@@ -1599,13 +1591,13 @@ static int find_next_key(struct btrfs_path *path, int level,
  */
 static noinline_for_stack
 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_path *path,
                                 struct btrfs_extent_inline_ref **ref_ret,
                                 u64 bytenr, u64 num_bytes,
                                 u64 parent, u64 root_objectid,
                                 u64 owner, u64 offset, int insert)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_root *root = fs_info->extent_root;
        struct btrfs_key key;
        struct extent_buffer *leaf;
@@ -1635,8 +1627,8 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
                extra_size = -1;
 
        /*
-        * Owner is our parent level, so we can just add one to get the level
-        * for the block we are interested in.
+        * Owner is our level, so we can just add one to get the level for the
+        * block we are interested in.
         */
        if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
                key.type = BTRFS_METADATA_ITEM_KEY;
@@ -1727,7 +1719,7 @@ again:
                iref = (struct btrfs_extent_inline_ref *)ptr;
                type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
                if (type == BTRFS_REF_TYPE_INVALID) {
-                       err = -EINVAL;
+                       err = -EUCLEAN;
                        goto out;
                }
 
@@ -1863,7 +1855,6 @@ void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
 }
 
 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_path *path,
                                 struct btrfs_extent_inline_ref **ref_ret,
                                 u64 bytenr, u64 num_bytes, u64 parent,
@@ -1871,9 +1862,9 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans,
 {
        int ret;
 
-       ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
-                                          bytenr, num_bytes, parent,
-                                          root_objectid, owner, offset, 0);
+       ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
+                                          num_bytes, parent, root_objectid,
+                                          owner, offset, 0);
        if (ret != -ENOENT)
                return ret;
 
@@ -1881,12 +1872,11 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans,
        *ref_ret = NULL;
 
        if (owner < BTRFS_FIRST_FREE_OBJECTID) {
-               ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
-                                           parent, root_objectid);
+               ret = lookup_tree_block_ref(trans, path, bytenr, parent,
+                                           root_objectid);
        } else {
-               ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
-                                            parent, root_objectid, owner,
-                                            offset);
+               ret = lookup_extent_data_ref(trans, path, bytenr, parent,
+                                            root_objectid, owner, offset);
        }
        return ret;
 }
@@ -1895,14 +1885,14 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  * helper to update/remove inline back ref
  */
 static noinline_for_stack
-void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
-                                 struct btrfs_path *path,
+void update_inline_extent_backref(struct btrfs_path *path,
                                  struct btrfs_extent_inline_ref *iref,
                                  int refs_to_mod,
                                  struct btrfs_delayed_extent_op *extent_op,
                                  int *last_ref)
 {
-       struct extent_buffer *leaf;
+       struct extent_buffer *leaf = path->nodes[0];
+       struct btrfs_fs_info *fs_info = leaf->fs_info;
        struct btrfs_extent_item *ei;
        struct btrfs_extent_data_ref *dref = NULL;
        struct btrfs_shared_data_ref *sref = NULL;
@@ -1913,7 +1903,6 @@ void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
        int type;
        u64 refs;
 
-       leaf = path->nodes[0];
        ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
        refs = btrfs_extent_refs(leaf, ei);
        WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
@@ -1965,7 +1954,6 @@ void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
 
 static noinline_for_stack
 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_path *path,
                                 u64 bytenr, u64 num_bytes, u64 parent,
                                 u64 root_objectid, u64 owner,
@@ -1975,15 +1963,15 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
        struct btrfs_extent_inline_ref *iref;
        int ret;
 
-       ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
-                                          bytenr, num_bytes, parent,
-                                          root_objectid, owner, offset, 1);
+       ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
+                                          num_bytes, parent, root_objectid,
+                                          owner, offset, 1);
        if (ret == 0) {
                BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
-               update_inline_extent_backref(fs_info, path, iref,
-                                            refs_to_add, extent_op, NULL);
+               update_inline_extent_backref(path, iref, refs_to_add,
+                                            extent_op, NULL);
        } else if (ret == -ENOENT) {
-               setup_inline_extent_backref(fs_info, path, iref, parent,
+               setup_inline_extent_backref(trans->fs_info, path, iref, parent,
                                            root_objectid, owner, offset,
                                            refs_to_add, extent_op);
                ret = 0;
@@ -1992,7 +1980,6 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
 }
 
 static int insert_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_path *path,
                                 u64 bytenr, u64 parent, u64 root_objectid,
                                 u64 owner, u64 offset, int refs_to_add)
@@ -2000,18 +1987,17 @@ static int insert_extent_backref(struct btrfs_trans_handle *trans,
        int ret;
        if (owner < BTRFS_FIRST_FREE_OBJECTID) {
                BUG_ON(refs_to_add != 1);
-               ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
-                                           parent, root_objectid);
+               ret = insert_tree_block_ref(trans, path, bytenr, parent,
+                                           root_objectid);
        } else {
-               ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
-                                            parent, root_objectid,
-                                            owner, offset, refs_to_add);
+               ret = insert_extent_data_ref(trans, path, bytenr, parent,
+                                            root_objectid, owner, offset,
+                                            refs_to_add);
        }
        return ret;
 }
 
 static int remove_extent_backref(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_path *path,
                                 struct btrfs_extent_inline_ref *iref,
                                 int refs_to_drop, int is_data, int *last_ref)
@@ -2020,14 +2006,14 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,
 
        BUG_ON(!is_data && refs_to_drop != 1);
        if (iref) {
-               update_inline_extent_backref(fs_info, path, iref,
-                                            -refs_to_drop, NULL, last_ref);
+               update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
+                                            last_ref);
        } else if (is_data) {
-               ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
+               ret = remove_extent_data_ref(trans, path, refs_to_drop,
                                             last_ref);
        } else {
                *last_ref = 1;
-               ret = btrfs_del_item(trans, fs_info->extent_root, path);
+               ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
        }
        return ret;
 }
@@ -2185,13 +2171,13 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
                           owner, offset, BTRFS_ADD_DELAYED_REF);
 
        if (owner < BTRFS_FIRST_FREE_OBJECTID) {
-               ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
+               ret = btrfs_add_delayed_tree_ref(trans, bytenr,
                                                 num_bytes, parent,
                                                 root_objectid, (int)owner,
                                                 BTRFS_ADD_DELAYED_REF, NULL,
                                                 &old_ref_mod, &new_ref_mod);
        } else {
-               ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
+               ret = btrfs_add_delayed_data_ref(trans, bytenr,
                                                 num_bytes, parent,
                                                 root_objectid, owner, offset,
                                                 0, BTRFS_ADD_DELAYED_REF,
@@ -2207,8 +2193,41 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        return ret;
 }
 
+/*
+ * __btrfs_inc_extent_ref - insert backreference for a given extent
+ *
+ * @trans:         Handle of transaction
+ *
+ * @node:          The delayed ref node used to get the bytenr/length for
+ *                 extent whose references are incremented.
+ *
+ * @parent:        If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
+ *                 BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
+ *                 bytenr of the parent block. Since new extents are always
+ *                 created with indirect references, this will only be the case
+ *                 when relocating a shared extent. In that case, root_objectid
+ *                 will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
+ *                 be 0
+ *
+ * @root_objectid:  The id of the root where this modification has originated,
+ *                 this can be either one of the well-known metadata trees or
+ *                 the subvolume id which references this extent.
+ *
+ * @owner:         For data extents it is the inode number of the owning file.
+ *                 For metadata extents this parameter holds the level in the
+ *                 tree of the extent.
+ *
+ * @offset:        For metadata extents the offset is ignored and is currently
+ *                 always passed as 0. For data extents it is the fileoffset
+ *                 this extent belongs to.
+ *
+ * @refs_to_add     Number of references to add
+ *
+ * @extent_op       Pointer to a structure, holding information necessary when
+ *                  updating a tree block's flags
+ *
+ */
 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
-                                 struct btrfs_fs_info *fs_info,
                                  struct btrfs_delayed_ref_node *node,
                                  u64 parent, u64 root_objectid,
                                  u64 owner, u64 offset, int refs_to_add,
@@ -2230,10 +2249,9 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        path->reada = READA_FORWARD;
        path->leave_spinning = 1;
        /* this will setup the path even if it fails to insert the back ref */
-       ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
-                                          num_bytes, parent, root_objectid,
-                                          owner, offset,
-                                          refs_to_add, extent_op);
+       ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
+                                          parent, root_objectid, owner,
+                                          offset, refs_to_add, extent_op);
        if ((ret < 0 && ret != -EAGAIN) || !ret)
                goto out;
 
@@ -2256,8 +2274,8 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
        path->reada = READA_FORWARD;
        path->leave_spinning = 1;
        /* now insert the actual backref */
-       ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
-                                   root_objectid, owner, offset, refs_to_add);
+       ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
+                                   owner, offset, refs_to_add);
        if (ret)
                btrfs_abort_transaction(trans, ret);
 out:
@@ -2266,7 +2284,6 @@ out:
 }
 
 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
-                               struct btrfs_fs_info *fs_info,
                                struct btrfs_delayed_ref_node *node,
                                struct btrfs_delayed_extent_op *extent_op,
                                int insert_reserved)
@@ -2283,7 +2300,7 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
        ins.type = BTRFS_EXTENT_ITEM_KEY;
 
        ref = btrfs_delayed_node_to_data_ref(node);
-       trace_run_delayed_data_ref(fs_info, node, ref, node->action);
+       trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
 
        if (node->type == BTRFS_SHARED_DATA_REF_KEY)
                parent = ref->parent;
@@ -2292,17 +2309,16 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
        if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
                if (extent_op)
                        flags |= extent_op->flags_to_set;
-               ret = alloc_reserved_file_extent(trans, fs_info,
-                                                parent, ref_root, flags,
-                                                ref->objectid, ref->offset,
-                                                &ins, node->ref_mod);
+               ret = alloc_reserved_file_extent(trans, parent, ref_root,
+                                                flags, ref->objectid,
+                                                ref->offset, &ins,
+                                                node->ref_mod);
        } else if (node->action == BTRFS_ADD_DELAYED_REF) {
-               ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
-                                            ref_root, ref->objectid,
-                                            ref->offset, node->ref_mod,
-                                            extent_op);
+               ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
+                                            ref->objectid, ref->offset,
+                                            node->ref_mod, extent_op);
        } else if (node->action == BTRFS_DROP_DELAYED_REF) {
-               ret = __btrfs_free_extent(trans, fs_info, node, parent,
+               ret = __btrfs_free_extent(trans, node, parent,
                                          ref_root, ref->objectid,
                                          ref->offset, node->ref_mod,
                                          extent_op);
@@ -2331,10 +2347,10 @@ static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
 }
 
 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
-                                struct btrfs_fs_info *fs_info,
                                 struct btrfs_delayed_ref_head *head,
                                 struct btrfs_delayed_extent_op *extent_op)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_key key;
        struct btrfs_path *path;
        struct btrfs_extent_item *ei;
@@ -2422,7 +2438,6 @@ out:
 }
 
 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
-                               struct btrfs_fs_info *fs_info,
                                struct btrfs_delayed_ref_node *node,
                                struct btrfs_delayed_extent_op *extent_op,
                                int insert_reserved)
@@ -2433,14 +2448,14 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
        u64 ref_root = 0;
 
        ref = btrfs_delayed_node_to_tree_ref(node);
-       trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
+       trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
 
        if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
                parent = ref->parent;
        ref_root = ref->root;
 
        if (node->ref_mod != 1) {
-               btrfs_err(fs_info,
+               btrfs_err(trans->fs_info,
        "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
                          node->bytenr, node->ref_mod, node->action, ref_root,
                          parent);
@@ -2450,13 +2465,10 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
                BUG_ON(!extent_op || !extent_op->update_flags);
                ret = alloc_reserved_tree_block(trans, node, extent_op);
        } else if (node->action == BTRFS_ADD_DELAYED_REF) {
-               ret = __btrfs_inc_extent_ref(trans, fs_info, node,
-                                            parent, ref_root,
-                                            ref->level, 0, 1,
-                                            extent_op);
+               ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
+                                            ref->level, 0, 1, extent_op);
        } else if (node->action == BTRFS_DROP_DELAYED_REF) {
-               ret = __btrfs_free_extent(trans, fs_info, node,
-                                         parent, ref_root,
+               ret = __btrfs_free_extent(trans, node, parent, ref_root,
                                          ref->level, 0, 1, extent_op);
        } else {
                BUG();
@@ -2466,7 +2478,6 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
 
 /* helper function to actually process a single delayed ref entry */
 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
-                              struct btrfs_fs_info *fs_info,
                               struct btrfs_delayed_ref_node *node,
                               struct btrfs_delayed_extent_op *extent_op,
                               int insert_reserved)
@@ -2475,18 +2486,18 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
 
        if (trans->aborted) {
                if (insert_reserved)
-                       btrfs_pin_extent(fs_info, node->bytenr,
+                       btrfs_pin_extent(trans->fs_info, node->bytenr,
                                         node->num_bytes, 1);
                return 0;
        }
 
        if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
            node->type == BTRFS_SHARED_BLOCK_REF_KEY)
-               ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
+               ret = run_delayed_tree_ref(trans, node, extent_op,
                                           insert_reserved);
        else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
                 node->type == BTRFS_SHARED_DATA_REF_KEY)
-               ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
+               ret = run_delayed_data_ref(trans, node, extent_op,
                                           insert_reserved);
        else
                BUG();
@@ -2528,7 +2539,6 @@ static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_ref
 }
 
 static int cleanup_extent_op(struct btrfs_trans_handle *trans,
-                            struct btrfs_fs_info *fs_info,
                             struct btrfs_delayed_ref_head *head)
 {
        struct btrfs_delayed_extent_op *extent_op = head->extent_op;
@@ -2542,21 +2552,22 @@ static int cleanup_extent_op(struct btrfs_trans_handle *trans,
                return 0;
        }
        spin_unlock(&head->lock);
-       ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
+       ret = run_delayed_extent_op(trans, head, extent_op);
        btrfs_free_delayed_extent_op(extent_op);
        return ret ? ret : 1;
 }
 
 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
-                           struct btrfs_fs_info *fs_info,
                            struct btrfs_delayed_ref_head *head)
 {
+
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_delayed_ref_root *delayed_refs;
        int ret;
 
        delayed_refs = &trans->transaction->delayed_refs;
 
-       ret = cleanup_extent_op(trans, fs_info, head);
+       ret = cleanup_extent_op(trans, head);
        if (ret < 0) {
                unselect_delayed_ref_head(delayed_refs, head);
                btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
@@ -2705,7 +2716,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
                 * up and move on to the next ref_head.
                 */
                if (!ref) {
-                       ret = cleanup_ref_head(trans, fs_info, locked_ref);
+                       ret = cleanup_ref_head(trans, locked_ref);
                        if (ret > 0 ) {
                                /* We dropped our lock, we need to loop. */
                                ret = 0;
@@ -2752,7 +2763,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
                locked_ref->extent_op = NULL;
                spin_unlock(&locked_ref->lock);
 
-               ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
+               ret = run_one_delayed_ref(trans, ref, extent_op,
                                          must_insert_reserved);
 
                btrfs_free_delayed_extent_op(extent_op);
@@ -4289,7 +4300,7 @@ again:
                        if (IS_ERR(trans))
                                return PTR_ERR(trans);
 
-                       ret = do_chunk_alloc(trans, fs_info, alloc_target,
+                       ret = do_chunk_alloc(trans, alloc_target,
                                             CHUNK_ALLOC_NO_FORCE);
                        btrfs_end_transaction(trans);
                        if (ret < 0) {
@@ -4511,9 +4522,9 @@ static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
  * for allocating a chunk, otherwise if it's false, reserve space necessary for
  * removing a chunk.
  */
-void check_system_chunk(struct btrfs_trans_handle *trans,
-                       struct btrfs_fs_info *fs_info, u64 type)
+void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_space_info *info;
        u64 left;
        u64 thresh;
@@ -4552,7 +4563,7 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
                 * the paths we visit in the chunk tree (they were already COWed
                 * or created in the current transaction for example).
                 */
-               ret = btrfs_alloc_chunk(trans, fs_info, flags);
+               ret = btrfs_alloc_chunk(trans, flags);
        }
 
        if (!ret) {
@@ -4573,9 +4584,10 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
  *    - return 1 if it successfully allocates a chunk,
  *    - return errors including -ENOSPC otherwise.
  */
-static int do_chunk_alloc(struct btrfs_trans_handle *trans,
-                         struct btrfs_fs_info *fs_info, u64 flags, int force)
+static int do_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
+                         int force)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_space_info *space_info;
        int wait_for_alloc = 0;
        int ret = 0;
@@ -4651,9 +4663,9 @@ again:
         * Check if we have enough space in SYSTEM chunk because we may need
         * to update devices.
         */
-       check_system_chunk(trans, fs_info, flags);
+       check_system_chunk(trans, flags);
 
-       ret = btrfs_alloc_chunk(trans, fs_info, flags);
+       ret = btrfs_alloc_chunk(trans, flags);
        trans->allocating_chunk = false;
 
        spin_lock(&space_info->lock);
@@ -4984,7 +4996,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,
                        ret = PTR_ERR(trans);
                        break;
                }
-               ret = do_chunk_alloc(trans, fs_info,
+               ret = do_chunk_alloc(trans,
                                     btrfs_metadata_alloc_profile(fs_info),
                                     CHUNK_ALLOC_NO_FORCE);
                btrfs_end_transaction(trans);
@@ -6279,7 +6291,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
                if (list_empty(&cache->dirty_list)) {
                        list_add_tail(&cache->dirty_list,
                                      &trans->transaction->dirty_bgs);
-                               trans->transaction->num_dirty_bgs++;
+                       trans->transaction->num_dirty_bgs++;
                        btrfs_get_block_group(cache);
                }
                spin_unlock(&trans->transaction->dirty_bgs_lock);
@@ -6815,12 +6827,12 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
 }
 
 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
-                               struct btrfs_fs_info *info,
-                               struct btrfs_delayed_ref_node *node, u64 parent,
-                               u64 root_objectid, u64 owner_objectid,
-                               u64 owner_offset, int refs_to_drop,
-                               struct btrfs_delayed_extent_op *extent_op)
+                              struct btrfs_delayed_ref_node *node, u64 parent,
+                              u64 root_objectid, u64 owner_objectid,
+                              u64 owner_offset, int refs_to_drop,
+                              struct btrfs_delayed_extent_op *extent_op)
 {
+       struct btrfs_fs_info *info = trans->fs_info;
        struct btrfs_key key;
        struct btrfs_path *path;
        struct btrfs_root *extent_root = info->extent_root;
@@ -6852,9 +6864,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
        if (is_data)
                skinny_metadata = false;
 
-       ret = lookup_extent_backref(trans, info, path, &iref,
-                                   bytenr, num_bytes, parent,
-                                   root_objectid, owner_objectid,
+       ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
+                                   parent, root_objectid, owner_objectid,
                                    owner_offset);
        if (ret == 0) {
                extent_slot = path->slots[0];
@@ -6884,7 +6895,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
 #endif
                if (!found_extent) {
                        BUG_ON(iref);
-                       ret = remove_extent_backref(trans, info, path, NULL,
+                       ret = remove_extent_backref(trans, path, NULL,
                                                    refs_to_drop,
                                                    is_data, &last_ref);
                        if (ret) {
@@ -7028,9 +7039,9 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
                        btrfs_mark_buffer_dirty(leaf);
                }
                if (found_extent) {
-                       ret = remove_extent_backref(trans, info, path,
-                                                   iref, refs_to_drop,
-                                                   is_data, &last_ref);
+                       ret = remove_extent_backref(trans, path, iref,
+                                                   refs_to_drop, is_data,
+                                                   &last_ref);
                        if (ret) {
                                btrfs_abort_transaction(trans, ret);
                                goto out;
@@ -7172,7 +7183,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
                                   root->root_key.objectid,
                                   btrfs_header_level(buf), 0,
                                   BTRFS_DROP_DELAYED_REF);
-               ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
+               ret = btrfs_add_delayed_tree_ref(trans, buf->start,
                                                 buf->len, parent,
                                                 root->root_key.objectid,
                                                 btrfs_header_level(buf),
@@ -7251,13 +7262,13 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
                old_ref_mod = new_ref_mod = 0;
                ret = 0;
        } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
-               ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
+               ret = btrfs_add_delayed_tree_ref(trans, bytenr,
                                                 num_bytes, parent,
                                                 root_objectid, (int)owner,
                                                 BTRFS_DROP_DELAYED_REF, NULL,
                                                 &old_ref_mod, &new_ref_mod);
        } else {
-               ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
+               ret = btrfs_add_delayed_data_ref(trans, bytenr,
                                                 num_bytes, parent,
                                                 root_objectid, owner, offset,
                                                 0, BTRFS_DROP_DELAYED_REF,
@@ -7534,7 +7545,7 @@ search:
                 * for the proper type.
                 */
                if (!block_group_bits(block_group, flags)) {
-                   u64 extra = BTRFS_BLOCK_GROUP_DUP |
+                       u64 extra = BTRFS_BLOCK_GROUP_DUP |
                                BTRFS_BLOCK_GROUP_RAID1 |
                                BTRFS_BLOCK_GROUP_RAID5 |
                                BTRFS_BLOCK_GROUP_RAID6 |
@@ -7826,8 +7837,7 @@ loop:
                                goto out;
                        }
 
-                       ret = do_chunk_alloc(trans, fs_info, flags,
-                                            CHUNK_ALLOC_FORCE);
+                       ret = do_chunk_alloc(trans, flags, CHUNK_ALLOC_FORCE);
 
                        /*
                         * If we can't allocate a new chunk we've already looped
@@ -8053,11 +8063,11 @@ int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
 }
 
 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
-                                     struct btrfs_fs_info *fs_info,
                                      u64 parent, u64 root_objectid,
                                      u64 flags, u64 owner, u64 offset,
                                      struct btrfs_key *ins, int ref_mod)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        int ret;
        struct btrfs_extent_item *extent_item;
        struct btrfs_extent_inline_ref *iref;
@@ -8231,7 +8241,6 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
                                     u64 offset, u64 ram_bytes,
                                     struct btrfs_key *ins)
 {
-       struct btrfs_fs_info *fs_info = root->fs_info;
        int ret;
 
        BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
@@ -8240,7 +8249,7 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
                           root->root_key.objectid, owner, offset,
                           BTRFS_ADD_DELAYED_EXTENT);
 
-       ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
+       ret = btrfs_add_delayed_data_ref(trans, ins->objectid,
                                         ins->offset, 0,
                                         root->root_key.objectid, owner,
                                         offset, ram_bytes,
@@ -8254,10 +8263,10 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  * space cache bits as well
  */
 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
-                                  struct btrfs_fs_info *fs_info,
                                   u64 root_objectid, u64 owner, u64 offset,
                                   struct btrfs_key *ins)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        int ret;
        struct btrfs_block_group_cache *block_group;
        struct btrfs_space_info *space_info;
@@ -8285,15 +8294,15 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
        spin_unlock(&block_group->lock);
        spin_unlock(&space_info->lock);
 
-       ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
-                                        0, owner, offset, ins, 1);
+       ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
+                                        offset, ins, 1);
        btrfs_put_block_group(block_group);
        return ret;
 }
 
 static struct extent_buffer *
 btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
-                     u64 bytenr, int level)
+                     u64 bytenr, int level, u64 owner)
 {
        struct btrfs_fs_info *fs_info = root->fs_info;
        struct extent_buffer *buf;
@@ -8302,7 +8311,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
        if (IS_ERR(buf))
                return buf;
 
-       btrfs_set_header_generation(buf, trans->transid);
        btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
        btrfs_tree_lock(buf);
        clean_tree_block(fs_info, buf);
@@ -8311,6 +8319,14 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
        btrfs_set_lock_blocking(buf);
        set_extent_buffer_uptodate(buf);
 
+       memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
+       btrfs_set_header_level(buf, level);
+       btrfs_set_header_bytenr(buf, buf->start);
+       btrfs_set_header_generation(buf, trans->transid);
+       btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
+       btrfs_set_header_owner(buf, owner);
+       write_extent_buffer_fsid(buf, fs_info->fsid);
+       write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
        if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
                buf->log_index = root->log_transid % 2;
                /*
@@ -8419,7 +8435,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
        if (btrfs_is_testing(fs_info)) {
                buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
-                                           level);
+                                           level, root_objectid);
                if (!IS_ERR(buf))
                        root->alloc_bytenr += blocksize;
                return buf;
@@ -8435,7 +8451,8 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
        if (ret)
                goto out_unuse;
 
-       buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
+       buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
+                                   root_objectid);
        if (IS_ERR(buf)) {
                ret = PTR_ERR(buf);
                goto out_free_reserved;
@@ -8467,7 +8484,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
                btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
                                   root_objectid, level, 0,
                                   BTRFS_ADD_DELAYED_EXTENT);
-               ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
+               ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
                                                 ins.offset, parent,
                                                 root_objectid, level,
                                                 BTRFS_ADD_DELAYED_EXTENT,
@@ -9417,10 +9434,10 @@ out:
        return ret;
 }
 
-int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
-                            struct btrfs_block_group_cache *cache)
+int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache)
 
 {
+       struct btrfs_fs_info *fs_info = cache->fs_info;
        struct btrfs_trans_handle *trans;
        u64 alloc_flags;
        int ret;
@@ -9454,7 +9471,7 @@ again:
         */
        alloc_flags = update_block_group_flags(fs_info, cache->flags);
        if (alloc_flags != cache->flags) {
-               ret = do_chunk_alloc(trans, fs_info, alloc_flags,
+               ret = do_chunk_alloc(trans, alloc_flags,
                                     CHUNK_ALLOC_FORCE);
                /*
                 * ENOSPC is allowed here, we may have enough space
@@ -9471,8 +9488,7 @@ again:
        if (!ret)
                goto out;
        alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
-       ret = do_chunk_alloc(trans, fs_info, alloc_flags,
-                            CHUNK_ALLOC_FORCE);
+       ret = do_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
        if (ret < 0)
                goto out;
        ret = inc_block_group_ro(cache, 0);
@@ -9480,7 +9496,7 @@ out:
        if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
                alloc_flags = update_block_group_flags(fs_info, cache->flags);
                mutex_lock(&fs_info->chunk_mutex);
-               check_system_chunk(trans, fs_info, alloc_flags);
+               check_system_chunk(trans, alloc_flags);
                mutex_unlock(&fs_info->chunk_mutex);
        }
        mutex_unlock(&fs_info->ro_block_group_mutex);
@@ -9489,12 +9505,11 @@ out:
        return ret;
 }
 
-int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
-                           struct btrfs_fs_info *fs_info, u64 type)
+int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
 {
-       u64 alloc_flags = get_alloc_profile(fs_info, type);
+       u64 alloc_flags = get_alloc_profile(trans->fs_info, type);
 
-       return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
+       return do_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
 }
 
 /*
@@ -9847,7 +9862,7 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
                 */
                if (block_group->cached == BTRFS_CACHE_NO ||
                    block_group->cached == BTRFS_CACHE_ERROR)
-                       free_excluded_extents(info, block_group);
+                       free_excluded_extents(block_group);
 
                btrfs_remove_free_space_cache(block_group);
                ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
@@ -10089,13 +10104,13 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
                 * info has super bytes accounted for, otherwise we'll think
                 * we have more space than we actually do.
                 */
-               ret = exclude_super_stripes(info, cache);
+               ret = exclude_super_stripes(cache);
                if (ret) {
                        /*
                         * We may have excluded something, so call this just in
                         * case.
                         */
-                       free_excluded_extents(info, cache);
+                       free_excluded_extents(cache);
                        btrfs_put_block_group(cache);
                        goto error;
                }
@@ -10110,14 +10125,14 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
                if (found_key.offset == btrfs_block_group_used(&cache->item)) {
                        cache->last_byte_to_unpin = (u64)-1;
                        cache->cached = BTRFS_CACHE_FINISHED;
-                       free_excluded_extents(info, cache);
+                       free_excluded_extents(cache);
                } else if (btrfs_block_group_used(&cache->item) == 0) {
                        cache->last_byte_to_unpin = (u64)-1;
                        cache->cached = BTRFS_CACHE_FINISHED;
                        add_new_free_space(cache, found_key.objectid,
                                           found_key.objectid +
                                           found_key.offset);
-                       free_excluded_extents(info, cache);
+                       free_excluded_extents(cache);
                }
 
                ret = btrfs_add_block_group_cache(info, cache);
@@ -10218,10 +10233,10 @@ next:
        trans->can_flush_pending_bgs = can_flush_pending_bgs;
 }
 
-int btrfs_make_block_group(struct btrfs_trans_handle *trans,
-                          struct btrfs_fs_info *fs_info, u64 bytes_used,
+int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
                           u64 type, u64 chunk_offset, u64 size)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_block_group_cache *cache;
        int ret;
 
@@ -10240,20 +10255,20 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
        cache->last_byte_to_unpin = (u64)-1;
        cache->cached = BTRFS_CACHE_FINISHED;
        cache->needs_free_space = 1;
-       ret = exclude_super_stripes(fs_info, cache);
+       ret = exclude_super_stripes(cache);
        if (ret) {
                /*
                 * We may have excluded something, so call this just in
                 * case.
                 */
-               free_excluded_extents(fs_info, cache);
+               free_excluded_extents(cache);
                btrfs_put_block_group(cache);
                return ret;
        }
 
        add_new_free_space(cache, chunk_offset, chunk_offset + size);
 
-       free_excluded_extents(fs_info, cache);
+       free_excluded_extents(cache);
 
 #ifdef CONFIG_BTRFS_DEBUG
        if (btrfs_should_fragment_free_space(cache)) {
@@ -10311,9 +10326,9 @@ static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
 }
 
 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
-                            struct btrfs_fs_info *fs_info, u64 group_start,
-                            struct extent_map *em)
+                            u64 group_start, struct extent_map *em)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_root *root = fs_info->extent_root;
        struct btrfs_path *path;
        struct btrfs_block_group_cache *block_group;
@@ -10337,7 +10352,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
         * Free the reserved super bytes from this block group before
         * remove it.
         */
-       free_excluded_extents(fs_info, block_group);
+       free_excluded_extents(block_group);
        btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
                                  block_group->key.offset);
 
@@ -10687,7 +10702,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
                /* Don't want to race with allocators so take the groups_sem */
                down_write(&space_info->groups_sem);
                spin_lock(&block_group->lock);
-               if (block_group->reserved ||
+               if (block_group->reserved || block_group->pinned ||
                    btrfs_block_group_used(&block_group->item) ||
                    block_group->ro ||
                    list_is_singular(&block_group->list)) {