btrfs: split btrfs_alloc_ordered_extent to allocation and insertion helpers
authorChristoph Hellwig <hch@lst.de>
Wed, 24 May 2023 15:03:13 +0000 (17:03 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Jun 2023 11:59:33 +0000 (13:59 +0200)
Split two low-level helpers out of btrfs_alloc_ordered_extent to allocate
and insert the logic extent.  The pure alloc helper will be used to
improve btrfs_split_ordered_extent.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ordered-data.c

index 15a410b..ea1c2ec 100644 (file)
@@ -146,35 +146,11 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
        return ret;
 }
 
-/*
- * Add an ordered extent to the per-inode tree.
- *
- * @inode:           Inode that this extent is for.
- * @file_offset:     Logical offset in file where the extent starts.
- * @num_bytes:       Logical length of extent in file.
- * @ram_bytes:       Full length of unencoded data.
- * @disk_bytenr:     Offset of extent on disk.
- * @disk_num_bytes:  Size of extent on disk.
- * @offset:          Offset into unencoded data where file data starts.
- * @flags:           Flags specifying type of extent (1 << BTRFS_ORDERED_*).
- * @compress_type:   Compression algorithm used for data.
- *
- * Most of these parameters correspond to &struct btrfs_file_extent_item. The
- * tree is given a single reference on the ordered extent that was inserted, and
- * the returned pointer is given a second reference.
- *
- * Return: the new ordered extent or error pointer.
- */
-struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
-                       struct btrfs_inode *inode, u64 file_offset,
-                       u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
-                       u64 disk_num_bytes, u64 offset, unsigned long flags,
-                       int compress_type)
+static struct btrfs_ordered_extent *alloc_ordered_extent(
+                       struct btrfs_inode *inode, u64 file_offset, u64 num_bytes,
+                       u64 ram_bytes, u64 disk_bytenr, u64 disk_num_bytes,
+                       u64 offset, unsigned long flags, int compress_type)
 {
-       struct btrfs_root *root = inode->root;
-       struct btrfs_fs_info *fs_info = root->fs_info;
-       struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
-       struct rb_node *node;
        struct btrfs_ordered_extent *entry;
        int ret;
 
@@ -184,7 +160,6 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
                ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
                if (ret < 0)
                        return ERR_PTR(ret);
-               ret = 0;
        } else {
                /*
                 * The ordered extent has reserved qgroup space, release now
@@ -209,14 +184,7 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
        entry->compress_type = compress_type;
        entry->truncated_len = (u64)-1;
        entry->qgroup_rsv = ret;
-
-       ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
        entry->flags = flags;
-
-       percpu_counter_add_batch(&fs_info->ordered_bytes, num_bytes,
-                                fs_info->delalloc_batch);
-
-       /* one ref for the tree */
        refcount_set(&entry->refs, 1);
        init_waitqueue_head(&entry->wait);
        INIT_LIST_HEAD(&entry->list);
@@ -225,15 +193,40 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
        INIT_LIST_HEAD(&entry->work_list);
        init_completion(&entry->completion);
 
+       /*
+        * We don't need the count_max_extents here, we can assume that all of
+        * that work has been done at higher layers, so this is truly the
+        * smallest the extent is going to get.
+        */
+       spin_lock(&inode->lock);
+       btrfs_mod_outstanding_extents(inode, 1);
+       spin_unlock(&inode->lock);
+
+       return entry;
+}
+
+static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
+{
+       struct btrfs_inode *inode = BTRFS_I(entry->inode);
+       struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
+       struct btrfs_root *root = inode->root;
+       struct btrfs_fs_info *fs_info = root->fs_info;
+       struct rb_node *node;
+
        trace_btrfs_ordered_extent_add(inode, entry);
 
+       percpu_counter_add_batch(&fs_info->ordered_bytes, entry->num_bytes,
+                                fs_info->delalloc_batch);
+
+       /* One ref for the tree. */
+       refcount_inc(&entry->refs);
+
        spin_lock_irq(&tree->lock);
-       node = tree_insert(&tree->tree, file_offset,
-                          &entry->rb_node);
+       node = tree_insert(&tree->tree, entry->file_offset, &entry->rb_node);
        if (node)
                btrfs_panic(fs_info, -EEXIST,
                                "inconsistency in ordered tree at offset %llu",
-                               file_offset);
+                               entry->file_offset);
        spin_unlock_irq(&tree->lock);
 
        spin_lock(&root->ordered_extent_lock);
@@ -247,19 +240,42 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
                spin_unlock(&fs_info->ordered_root_lock);
        }
        spin_unlock(&root->ordered_extent_lock);
+}
 
-       /*
-        * We don't need the count_max_extents here, we can assume that all of
-        * that work has been done at higher layers, so this is truly the
-        * smallest the extent is going to get.
-        */
-       spin_lock(&inode->lock);
-       btrfs_mod_outstanding_extents(inode, 1);
-       spin_unlock(&inode->lock);
+/*
+ * Add an ordered extent to the per-inode tree.
+ *
+ * @inode:           Inode that this extent is for.
+ * @file_offset:     Logical offset in file where the extent starts.
+ * @num_bytes:       Logical length of extent in file.
+ * @ram_bytes:       Full length of unencoded data.
+ * @disk_bytenr:     Offset of extent on disk.
+ * @disk_num_bytes:  Size of extent on disk.
+ * @offset:          Offset into unencoded data where file data starts.
+ * @flags:           Flags specifying type of extent (1 << BTRFS_ORDERED_*).
+ * @compress_type:   Compression algorithm used for data.
+ *
+ * Most of these parameters correspond to &struct btrfs_file_extent_item. The
+ * tree is given a single reference on the ordered extent that was inserted, and
+ * the returned pointer is given a second reference.
+ *
+ * Return: the new ordered extent or error pointer.
+ */
+struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
+                       struct btrfs_inode *inode, u64 file_offset,
+                       u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
+                       u64 disk_num_bytes, u64 offset, unsigned long flags,
+                       int compress_type)
+{
+       struct btrfs_ordered_extent *entry;
 
-       /* One ref for the returned entry to match semantics of lookup. */
-       refcount_inc(&entry->refs);
+       ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
 
+       entry = alloc_ordered_extent(inode, file_offset, num_bytes, ram_bytes,
+                                    disk_bytenr, disk_num_bytes, offset, flags,
+                                    compress_type);
+       if (!IS_ERR(entry))
+               insert_ordered_extent(entry);
        return entry;
 }