Commit ee6e6504 authored by Chris Mason's avatar Chris Mason

Add a per-inode lock around btrfs_drop_extents

btrfs_drop_extents is always called with a range lock held on the inode.
But, it may operate on extents outside that range as it drops and splits
them.

This patch adds a per-inode mutex that is held while calling
btrfs_drop_extents and while inserting new extents into the tree.  It
prevents races from two procs working against adjacent ranges in the tree.
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent ba1da2f4
...@@ -32,6 +32,7 @@ struct btrfs_inode { ...@@ -32,6 +32,7 @@ struct btrfs_inode {
struct extent_io_tree io_tree; struct extent_io_tree io_tree;
struct extent_io_tree io_failure_tree; struct extent_io_tree io_failure_tree;
struct mutex csum_mutex; struct mutex csum_mutex;
struct mutex extent_mutex;
struct inode vfs_inode; struct inode vfs_inode;
struct btrfs_ordered_inode_tree ordered_tree; struct btrfs_ordered_inode_tree ordered_tree;
......
...@@ -1671,6 +1671,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, ...@@ -1671,6 +1671,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
ref_generation, owner_objectid, owner_offset, ref_generation, owner_objectid, owner_offset,
pin, pin == 0); pin, pin == 0);
finish_current_insert(trans, root->fs_info->extent_root);
pending_ret = del_pending_extents(trans, root->fs_info->extent_root); pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
return ret ? ret : pending_ret; return ret ? ret : pending_ret;
} }
......
...@@ -242,6 +242,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -242,6 +242,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
u64 end_of_last_block; u64 end_of_last_block;
u64 end_pos = pos + write_bytes; u64 end_pos = pos + write_bytes;
u64 inline_size; u64 inline_size;
int did_inline = 0;
loff_t isize = i_size_read(inode); loff_t isize = i_size_read(inode);
start_pos = pos & ~((u64)root->sectorsize - 1); start_pos = pos & ~((u64)root->sectorsize - 1);
...@@ -275,6 +276,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -275,6 +276,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
if (hole_size > 0) { if (hole_size > 0) {
btrfs_wait_ordered_range(inode, last_pos_in_file, btrfs_wait_ordered_range(inode, last_pos_in_file,
last_pos_in_file + hole_size); last_pos_in_file + hole_size);
mutex_lock(&BTRFS_I(inode)->extent_mutex);
err = btrfs_drop_extents(trans, root, inode, err = btrfs_drop_extents(trans, root, inode,
last_pos_in_file, last_pos_in_file,
last_pos_in_file + hole_size, last_pos_in_file + hole_size,
...@@ -289,6 +291,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -289,6 +291,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
0, 0, hole_size, 0); 0, 0, hole_size, 0);
btrfs_drop_extent_cache(inode, last_pos_in_file, btrfs_drop_extent_cache(inode, last_pos_in_file,
last_pos_in_file + hole_size -1); last_pos_in_file + hole_size -1);
mutex_unlock(&BTRFS_I(inode)->extent_mutex);
btrfs_check_file(root, inode); btrfs_check_file(root, inode);
} }
if (err) if (err)
...@@ -321,6 +324,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -321,6 +324,7 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
/* step one, delete the existing extents in this range */ /* step one, delete the existing extents in this range */
aligned_end = (pos + write_bytes + root->sectorsize - 1) & aligned_end = (pos + write_bytes + root->sectorsize - 1) &
~((u64)root->sectorsize - 1); ~((u64)root->sectorsize - 1);
mutex_lock(&BTRFS_I(inode)->extent_mutex);
err = btrfs_drop_extents(trans, root, inode, start_pos, err = btrfs_drop_extents(trans, root, inode, start_pos,
aligned_end, aligned_end, &hint_byte); aligned_end, aligned_end, &hint_byte);
if (err) if (err)
...@@ -332,9 +336,13 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -332,9 +336,13 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
inline_size, pages, 0, num_pages); inline_size, pages, 0, num_pages);
btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1); btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
BUG_ON(err); BUG_ON(err);
mutex_unlock(&BTRFS_I(inode)->extent_mutex);
did_inline = 1;
} }
if (end_pos > isize) { if (end_pos > isize) {
i_size_write(inode, end_pos); i_size_write(inode, end_pos);
if (did_inline)
BTRFS_I(inode)->disk_i_size = end_pos;
btrfs_update_inode(trans, root, inode); btrfs_update_inode(trans, root, inode);
} }
failed: failed:
......
...@@ -516,6 +516,8 @@ int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, ...@@ -516,6 +516,8 @@ int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
trans->transid, inode->i_ino, trans->transid, inode->i_ino,
ordered_extent->file_offset, &ins); ordered_extent->file_offset, &ins);
BUG_ON(ret); BUG_ON(ret);
mutex_lock(&BTRFS_I(inode)->extent_mutex);
ret = btrfs_drop_extents(trans, root, inode, ret = btrfs_drop_extents(trans, root, inode,
ordered_extent->file_offset, ordered_extent->file_offset,
ordered_extent->file_offset + ordered_extent->file_offset +
...@@ -531,6 +533,8 @@ int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, ...@@ -531,6 +533,8 @@ int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
btrfs_drop_extent_cache(inode, ordered_extent->file_offset, btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
ordered_extent->file_offset + ordered_extent->file_offset +
ordered_extent->len - 1); ordered_extent->len - 1);
mutex_unlock(&BTRFS_I(inode)->extent_mutex);
inode->i_blocks += ordered_extent->len >> 9; inode->i_blocks += ordered_extent->len >> 9;
unlock_extent(io_tree, ordered_extent->file_offset, unlock_extent(io_tree, ordered_extent->file_offset,
ordered_extent->file_offset + ordered_extent->len - 1, ordered_extent->file_offset + ordered_extent->len - 1,
...@@ -1399,6 +1403,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1399,6 +1403,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
btrfs_set_trans_block_group(trans, inode); btrfs_set_trans_block_group(trans, inode);
mutex_lock(&BTRFS_I(inode)->extent_mutex);
err = btrfs_drop_extents(trans, root, inode, err = btrfs_drop_extents(trans, root, inode,
hole_start, block_end, hole_start, hole_start, block_end, hole_start,
&alloc_hint); &alloc_hint);
...@@ -1412,6 +1417,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1412,6 +1417,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
(u64)-1); (u64)-1);
btrfs_check_file(root, inode); btrfs_check_file(root, inode);
} }
mutex_unlock(&BTRFS_I(inode)->extent_mutex);
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
if (err) if (err)
...@@ -1578,6 +1584,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p) ...@@ -1578,6 +1584,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
inode->i_mapping, GFP_NOFS); inode->i_mapping, GFP_NOFS);
btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
mutex_init(&BTRFS_I(inode)->csum_mutex); mutex_init(&BTRFS_I(inode)->csum_mutex);
mutex_init(&BTRFS_I(inode)->extent_mutex);
return 0; return 0;
} }
...@@ -1880,6 +1887,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, ...@@ -1880,6 +1887,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
inode->i_mapping, GFP_NOFS); inode->i_mapping, GFP_NOFS);
btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
mutex_init(&BTRFS_I(inode)->csum_mutex); mutex_init(&BTRFS_I(inode)->csum_mutex);
mutex_init(&BTRFS_I(inode)->extent_mutex);
BTRFS_I(inode)->delalloc_bytes = 0; BTRFS_I(inode)->delalloc_bytes = 0;
BTRFS_I(inode)->disk_i_size = 0; BTRFS_I(inode)->disk_i_size = 0;
BTRFS_I(inode)->root = root; BTRFS_I(inode)->root = root;
...@@ -2105,6 +2113,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry, ...@@ -2105,6 +2113,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
inode->i_mapping, GFP_NOFS); inode->i_mapping, GFP_NOFS);
mutex_init(&BTRFS_I(inode)->csum_mutex); mutex_init(&BTRFS_I(inode)->csum_mutex);
mutex_init(&BTRFS_I(inode)->extent_mutex);
BTRFS_I(inode)->delalloc_bytes = 0; BTRFS_I(inode)->delalloc_bytes = 0;
BTRFS_I(inode)->disk_i_size = 0; BTRFS_I(inode)->disk_i_size = 0;
BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
...@@ -3079,6 +3088,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -3079,6 +3088,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
inode->i_mapping, GFP_NOFS); inode->i_mapping, GFP_NOFS);
mutex_init(&BTRFS_I(inode)->csum_mutex); mutex_init(&BTRFS_I(inode)->csum_mutex);
mutex_init(&BTRFS_I(inode)->extent_mutex);
BTRFS_I(inode)->delalloc_bytes = 0; BTRFS_I(inode)->delalloc_bytes = 0;
BTRFS_I(inode)->disk_i_size = 0; BTRFS_I(inode)->disk_i_size = 0;
BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
......
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