btrfs: Factor out common delayed refs init code
[sfrench/cifs-2.6.git] / fs / btrfs / delayed-ref.c
index e1b0651686f7c4e988766d927c669306560c468d..a0dc255792c7f5e1aa09f94540bf6c604f9439f7 100644 (file)
@@ -286,10 +286,10 @@ static bool merge_ref(struct btrfs_trans_handle *trans,
 }
 
 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
-                             struct btrfs_fs_info *fs_info,
                              struct btrfs_delayed_ref_root *delayed_refs,
                              struct btrfs_delayed_ref_head *head)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_delayed_ref_node *ref;
        struct rb_node *node;
        u64 seq = 0;
@@ -323,9 +323,7 @@ again:
        }
 }
 
-int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
-                           struct btrfs_delayed_ref_root *delayed_refs,
-                           u64 seq)
+int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
 {
        struct seq_list *elem;
        int ret = 0;
@@ -336,10 +334,9 @@ int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
                                        struct seq_list, list);
                if (seq >= elem->seq) {
                        btrfs_debug(fs_info,
-                               "holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)",
+                               "holding back delayed_ref %#x.%x, lowest is %#x.%x",
                                (u32)(seq >> 32), (u32)seq,
-                               (u32)(elem->seq >> 32), (u32)elem->seq,
-                               delayed_refs);
+                               (u32)(elem->seq >> 32), (u32)elem->seq);
                        ret = 1;
                }
        }
@@ -535,8 +532,7 @@ update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
  * overall modification count.
  */
 static noinline struct btrfs_delayed_ref_head *
-add_delayed_ref_head(struct btrfs_fs_info *fs_info,
-                    struct btrfs_trans_handle *trans,
+add_delayed_ref_head(struct btrfs_trans_handle *trans,
                     struct btrfs_delayed_ref_head *head_ref,
                     struct btrfs_qgroup_extent_record *qrecord,
                     u64 bytenr, u64 num_bytes, u64 ref_root, u64 reserved,
@@ -609,14 +605,14 @@ add_delayed_ref_head(struct btrfs_fs_info *fs_info,
                qrecord->num_bytes = num_bytes;
                qrecord->old_roots = NULL;
 
-               if(btrfs_qgroup_trace_extent_nolock(fs_info,
+               if(btrfs_qgroup_trace_extent_nolock(trans->fs_info,
                                        delayed_refs, qrecord))
                        kfree(qrecord);
                else
                        qrecord_inserted = 1;
        }
 
-       trace_add_delayed_ref_head(fs_info, head_ref, action);
+       trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
 
        existing = htree_insert(&delayed_refs->href_root,
                                &head_ref->href_node);
@@ -648,6 +644,57 @@ add_delayed_ref_head(struct btrfs_fs_info *fs_info,
        return head_ref;
 }
 
+/*
+ * init_delayed_ref_common - Initialize the structure which represents a
+ *                          modification to a an extent.
+ *
+ * @fs_info:    Internal to the mounted filesystem mount structure.
+ *
+ * @ref:       The structure which is going to be initialized.
+ *
+ * @bytenr:    The logical address of the extent for which a modification is
+ *             going to be recorded.
+ *
+ * @num_bytes:  Size of the extent whose modification is being recorded.
+ *
+ * @ref_root:  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.
+ *
+ * @action:    Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
+ *             BTRFS_ADD_DELAYED_EXTENT
+ *
+ * @ref_type:  Holds the type of the extent which is being recorded, can be
+ *             one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
+ *             when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
+ *             BTRFS_EXTENT_DATA_REF_KEY when recording data extent
+ */
+static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
+                                   struct btrfs_delayed_ref_node *ref,
+                                   u64 bytenr, u64 num_bytes, u64 ref_root,
+                                   int action, u8 ref_type)
+{
+       u64 seq = 0;
+
+       if (action == BTRFS_ADD_DELAYED_EXTENT)
+               action = BTRFS_ADD_DELAYED_REF;
+
+       if (is_fstree(ref_root))
+               seq = atomic64_read(&fs_info->tree_mod_seq);
+
+       refcount_set(&ref->refs, 1);
+       ref->bytenr = bytenr;
+       ref->num_bytes = num_bytes;
+       ref->ref_mod = 1;
+       ref->action = action;
+       ref->is_head = 0;
+       ref->in_tree = 1;
+       ref->seq = seq;
+       ref->type = ref_type;
+       RB_CLEAR_NODE(&ref->ref_node);
+       INIT_LIST_HEAD(&ref->add_list);
+}
+
 /*
  * helper to insert a delayed tree ref into the rbtree.
  */
@@ -708,8 +755,7 @@ add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  * helper to insert a delayed data ref into the rbtree.
  */
 static noinline void
-add_delayed_data_ref(struct btrfs_fs_info *fs_info,
-                    struct btrfs_trans_handle *trans,
+add_delayed_data_ref(struct btrfs_trans_handle *trans,
                     struct btrfs_delayed_ref_head *head_ref,
                     struct btrfs_delayed_ref_node *ref, u64 bytenr,
                     u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
@@ -726,7 +772,7 @@ add_delayed_data_ref(struct btrfs_fs_info *fs_info,
        delayed_refs = &trans->transaction->delayed_refs;
 
        if (is_fstree(ref_root))
-               seq = atomic64_read(&fs_info->tree_mod_seq);
+               seq = atomic64_read(&trans->fs_info->tree_mod_seq);
 
        /* first set the basic ref node struct up */
        refcount_set(&ref->refs, 1);
@@ -751,7 +797,7 @@ add_delayed_data_ref(struct btrfs_fs_info *fs_info,
        full_ref->objectid = owner;
        full_ref->offset = offset;
 
-       trace_add_delayed_data_ref(fs_info, ref, full_ref, action);
+       trace_add_delayed_data_ref(trans->fs_info, ref, full_ref, action);
 
        ret = insert_delayed_ref(trans, delayed_refs, head_ref, ref);
        if (ret > 0)
@@ -802,8 +848,8 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
         * insert both the head node and the new ref without dropping
         * the spin lock
         */
-       head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
-                                       bytenr, num_bytes, 0, 0, action, 0,
+       head_ref = add_delayed_ref_head(trans, head_ref, record, bytenr,
+                                       num_bytes, 0, 0, action, 0,
                                        is_system, &qrecord_inserted,
                                        old_ref_mod, new_ref_mod);
 
@@ -870,14 +916,13 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
         * insert both the head node and the new ref without dropping
         * the spin lock
         */
-       head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
-                                       bytenr, num_bytes, ref_root, reserved,
+       head_ref = add_delayed_ref_head(trans, head_ref, record, bytenr,
+                                       num_bytes, ref_root, reserved,
                                        action, 1, 0, &qrecord_inserted,
                                        old_ref_mod, new_ref_mod);
 
-       add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
-                                  num_bytes, parent, ref_root, owner, offset,
-                                  action);
+       add_delayed_data_ref(trans, head_ref, &ref->node, bytenr, num_bytes,
+                            parent, ref_root, owner, offset, action);
        spin_unlock(&delayed_refs->lock);
 
        if (qrecord_inserted)
@@ -907,9 +952,9 @@ int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
         * in ref count changes, hence it's safe to pass false/0 for is_system
         * argument
         */
-       add_delayed_ref_head(fs_info, trans, head_ref, NULL, bytenr,
-                            num_bytes, 0, 0, BTRFS_UPDATE_DELAYED_HEAD,
-                            extent_op->is_data, 0, NULL, NULL, NULL);
+       add_delayed_ref_head(trans, head_ref, NULL, bytenr, num_bytes, 0, 0,
+                            BTRFS_UPDATE_DELAYED_HEAD, extent_op->is_data,
+                            0, NULL, NULL, NULL);
 
        spin_unlock(&delayed_refs->lock);
        return 0;