Commit 3d7db6e8 authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba

btrfs: don't allocate file extent tree for non regular files

When not using the NO_HOLES feature we always allocate an io tree for an
inode's file_extent_tree. This is wasteful because that io tree is only
used for regular files, so we allocate more memory than needed for inodes
that represent directories or symlinks for example, or for inodes that
correspond to free space inodes.

So improve on this by allocating the io tree only for inodes of regular
files that are not free space inodes.
Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent d9891ae2
...@@ -45,13 +45,12 @@ ...@@ -45,13 +45,12 @@
*/ */
void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size) void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size)
{ {
struct btrfs_fs_info *fs_info = inode->root->fs_info;
u64 start, end, i_size; u64 start, end, i_size;
int ret; int ret;
spin_lock(&inode->lock); spin_lock(&inode->lock);
i_size = new_i_size ?: i_size_read(&inode->vfs_inode); i_size = new_i_size ?: i_size_read(&inode->vfs_inode);
if (btrfs_fs_incompat(fs_info, NO_HOLES)) { if (!inode->file_extent_tree) {
inode->disk_i_size = i_size; inode->disk_i_size = i_size;
goto out_unlock; goto out_unlock;
} }
...@@ -84,13 +83,14 @@ void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_siz ...@@ -84,13 +83,14 @@ void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_siz
int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
u64 len) u64 len)
{ {
if (!inode->file_extent_tree)
return 0;
if (len == 0) if (len == 0)
return 0; return 0;
ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize)); ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize));
if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
return 0;
return set_extent_bit(inode->file_extent_tree, start, start + len - 1, return set_extent_bit(inode->file_extent_tree, start, start + len - 1,
EXTENT_DIRTY, NULL); EXTENT_DIRTY, NULL);
} }
...@@ -112,14 +112,15 @@ int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start, ...@@ -112,14 +112,15 @@ int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start, int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
u64 len) u64 len)
{ {
if (!inode->file_extent_tree)
return 0;
if (len == 0) if (len == 0)
return 0; return 0;
ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) || ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) ||
len == (u64)-1); len == (u64)-1);
if (btrfs_fs_incompat(inode->root->fs_info, NO_HOLES))
return 0;
return clear_extent_bit(inode->file_extent_tree, start, return clear_extent_bit(inode->file_extent_tree, start,
start + len - 1, EXTENT_DIRTY, NULL); start + len - 1, EXTENT_DIRTY, NULL);
} }
......
...@@ -3781,6 +3781,30 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf, ...@@ -3781,6 +3781,30 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
return 1; return 1;
} }
static int btrfs_init_file_extent_tree(struct btrfs_inode *inode)
{
struct btrfs_fs_info *fs_info = inode->root->fs_info;
if (WARN_ON_ONCE(inode->file_extent_tree))
return 0;
if (btrfs_fs_incompat(fs_info, NO_HOLES))
return 0;
if (!S_ISREG(inode->vfs_inode.i_mode))
return 0;
if (btrfs_is_free_space_inode(inode))
return 0;
inode->file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL);
if (!inode->file_extent_tree)
return -ENOMEM;
extent_io_tree_init(fs_info, inode->file_extent_tree, IO_TREE_INODE_FILE_EXTENT);
/* Lockdep class is set only for the file extent tree. */
lockdep_set_class(&inode->file_extent_tree->lock, &file_extent_tree_class);
return 0;
}
/* /*
* read an inode from the btree into the in-memory inode * read an inode from the btree into the in-memory inode
*/ */
...@@ -3800,6 +3824,10 @@ static int btrfs_read_locked_inode(struct inode *inode, ...@@ -3800,6 +3824,10 @@ static int btrfs_read_locked_inode(struct inode *inode,
bool filled = false; bool filled = false;
int first_xattr_slot; int first_xattr_slot;
ret = btrfs_init_file_extent_tree(BTRFS_I(inode));
if (ret)
return ret;
ret = btrfs_fill_inode(inode, &rdev); ret = btrfs_fill_inode(inode, &rdev);
if (!ret) if (!ret)
filled = true; filled = true;
...@@ -6247,6 +6275,10 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans, ...@@ -6247,6 +6275,10 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root); BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root);
root = BTRFS_I(inode)->root; root = BTRFS_I(inode)->root;
ret = btrfs_init_file_extent_tree(BTRFS_I(inode));
if (ret)
goto out;
ret = btrfs_get_free_objectid(root, &objectid); ret = btrfs_get_free_objectid(root, &objectid);
if (ret) if (ret)
goto out; goto out;
...@@ -8413,20 +8445,10 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) ...@@ -8413,20 +8445,10 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
struct btrfs_fs_info *fs_info = btrfs_sb(sb); struct btrfs_fs_info *fs_info = btrfs_sb(sb);
struct btrfs_inode *ei; struct btrfs_inode *ei;
struct inode *inode; struct inode *inode;
struct extent_io_tree *file_extent_tree = NULL;
/* Self tests may pass a NULL fs_info. */
if (fs_info && !btrfs_fs_incompat(fs_info, NO_HOLES)) {
file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL);
if (!file_extent_tree)
return NULL;
}
ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL); ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
if (!ei) { if (!ei)
kfree(file_extent_tree);
return NULL; return NULL;
}
ei->root = NULL; ei->root = NULL;
ei->generation = 0; ei->generation = 0;
...@@ -8471,13 +8493,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) ...@@ -8471,13 +8493,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO); extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO);
ei->io_tree.inode = ei; ei->io_tree.inode = ei;
ei->file_extent_tree = file_extent_tree; ei->file_extent_tree = NULL;
if (file_extent_tree) {
extent_io_tree_init(fs_info, ei->file_extent_tree,
IO_TREE_INODE_FILE_EXTENT);
/* Lockdep class is set only for the file extent tree. */
lockdep_set_class(&ei->file_extent_tree->lock, &file_extent_tree_class);
}
mutex_init(&ei->log_mutex); mutex_init(&ei->log_mutex);
spin_lock_init(&ei->ordered_tree_lock); spin_lock_init(&ei->ordered_tree_lock);
ei->ordered_tree = RB_ROOT; ei->ordered_tree = RB_ROOT;
......
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