Commit 53d9981c authored by Christoph Hellwig's avatar Christoph Hellwig Committed by David Sterba

btrfs: split btrfs_alloc_ordered_extent to allocation and insertion helpers

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: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent b0307e28
...@@ -146,35 +146,11 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree, ...@@ -146,35 +146,11 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
return ret; return ret;
} }
/* static struct btrfs_ordered_extent *alloc_ordered_extent(
* Add an ordered extent to the per-inode tree. struct btrfs_inode *inode, u64 file_offset, u64 num_bytes,
* u64 ram_bytes, u64 disk_bytenr, u64 disk_num_bytes,
* @inode: Inode that this extent is for. u64 offset, unsigned long flags, int compress_type)
* @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_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; struct btrfs_ordered_extent *entry;
int ret; int ret;
...@@ -184,7 +160,6 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( ...@@ -184,7 +160,6 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes); ret = btrfs_qgroup_free_data(inode, NULL, file_offset, num_bytes);
if (ret < 0) if (ret < 0)
return ERR_PTR(ret); return ERR_PTR(ret);
ret = 0;
} else { } else {
/* /*
* The ordered extent has reserved qgroup space, release now * The ordered extent has reserved qgroup space, release now
...@@ -209,14 +184,7 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( ...@@ -209,14 +184,7 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
entry->compress_type = compress_type; entry->compress_type = compress_type;
entry->truncated_len = (u64)-1; entry->truncated_len = (u64)-1;
entry->qgroup_rsv = ret; entry->qgroup_rsv = ret;
ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
entry->flags = flags; 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); refcount_set(&entry->refs, 1);
init_waitqueue_head(&entry->wait); init_waitqueue_head(&entry->wait);
INIT_LIST_HEAD(&entry->list); INIT_LIST_HEAD(&entry->list);
...@@ -225,15 +193,40 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( ...@@ -225,15 +193,40 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
INIT_LIST_HEAD(&entry->work_list); INIT_LIST_HEAD(&entry->work_list);
init_completion(&entry->completion); 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); 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); spin_lock_irq(&tree->lock);
node = tree_insert(&tree->tree, file_offset, node = tree_insert(&tree->tree, entry->file_offset, &entry->rb_node);
&entry->rb_node);
if (node) if (node)
btrfs_panic(fs_info, -EEXIST, btrfs_panic(fs_info, -EEXIST,
"inconsistency in ordered tree at offset %llu", "inconsistency in ordered tree at offset %llu",
file_offset); entry->file_offset);
spin_unlock_irq(&tree->lock); spin_unlock_irq(&tree->lock);
spin_lock(&root->ordered_extent_lock); spin_lock(&root->ordered_extent_lock);
...@@ -247,19 +240,42 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( ...@@ -247,19 +240,42 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
spin_unlock(&fs_info->ordered_root_lock); spin_unlock(&fs_info->ordered_root_lock);
} }
spin_unlock(&root->ordered_extent_lock); spin_unlock(&root->ordered_extent_lock);
}
/* /*
* We don't need the count_max_extents here, we can assume that all of * Add an ordered extent to the per-inode tree.
* that work has been done at higher layers, so this is truly the *
* smallest the extent is going to get. * @inode: Inode that this extent is for.
*/ * @file_offset: Logical offset in file where the extent starts.
spin_lock(&inode->lock); * @num_bytes: Logical length of extent in file.
btrfs_mod_outstanding_extents(inode, 1); * @ram_bytes: Full length of unencoded data.
spin_unlock(&inode->lock); * @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. */ ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
refcount_inc(&entry->refs);
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; return entry;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment