Commit 217f42eb authored by Nikolay Borisov's avatar Nikolay Borisov Committed by David Sterba

btrfs: make btrfs_truncate_block take btrfs_inode

Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: default avatarNikolay Borisov <nborisov@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 03fcb1ab
...@@ -3068,8 +3068,8 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, ...@@ -3068,8 +3068,8 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
struct btrfs_inode *parent_inode, struct btrfs_inode *inode, struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
const char *name, int name_len, int add_backref, u64 index); const char *name, int name_len, int add_backref, u64 index);
int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry); int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
int front); int front);
int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
struct btrfs_inode *inode, u64 new_size, struct btrfs_inode *inode, u64 new_size,
......
...@@ -2899,7 +2899,8 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) ...@@ -2899,7 +2899,8 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
if (same_block && len < fs_info->sectorsize) { if (same_block && len < fs_info->sectorsize) {
if (offset < ino_size) { if (offset < ino_size) {
truncated_block = true; truncated_block = true;
ret = btrfs_truncate_block(inode, offset, len, 0); ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
0);
} else { } else {
ret = 0; ret = 0;
} }
...@@ -2909,7 +2910,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) ...@@ -2909,7 +2910,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
/* zero back part of the first block */ /* zero back part of the first block */
if (offset < ino_size) { if (offset < ino_size) {
truncated_block = true; truncated_block = true;
ret = btrfs_truncate_block(inode, offset, 0, 0); ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
if (ret) { if (ret) {
inode_unlock(inode); inode_unlock(inode);
return ret; return ret;
...@@ -2945,7 +2946,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) ...@@ -2945,7 +2946,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
/* zero the front end of the last page */ /* zero the front end of the last page */
if (tail_start + tail_len < ino_size) { if (tail_start + tail_len < ino_size) {
truncated_block = true; truncated_block = true;
ret = btrfs_truncate_block(inode, ret = btrfs_truncate_block(BTRFS_I(inode),
tail_start + tail_len, tail_start + tail_len,
0, 1); 0, 1);
if (ret) if (ret)
...@@ -3187,7 +3188,8 @@ static int btrfs_zero_range(struct inode *inode, ...@@ -3187,7 +3188,8 @@ static int btrfs_zero_range(struct inode *inode,
} }
if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) { if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
free_extent_map(em); free_extent_map(em);
ret = btrfs_truncate_block(inode, offset, len, 0); ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
0);
if (!ret) if (!ret)
ret = btrfs_fallocate_update_isize(inode, ret = btrfs_fallocate_update_isize(inode,
offset + len, offset + len,
...@@ -3218,7 +3220,7 @@ static int btrfs_zero_range(struct inode *inode, ...@@ -3218,7 +3220,7 @@ static int btrfs_zero_range(struct inode *inode,
alloc_start = round_down(offset, sectorsize); alloc_start = round_down(offset, sectorsize);
ret = 0; ret = 0;
} else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
ret = btrfs_truncate_block(inode, offset, 0, 0); ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
if (ret) if (ret)
goto out; goto out;
} else { } else {
...@@ -3235,7 +3237,8 @@ static int btrfs_zero_range(struct inode *inode, ...@@ -3235,7 +3237,8 @@ static int btrfs_zero_range(struct inode *inode,
alloc_end = round_up(offset + len, sectorsize); alloc_end = round_up(offset + len, sectorsize);
ret = 0; ret = 0;
} else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) { } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
ret = btrfs_truncate_block(inode, offset + len, 0, 1); ret = btrfs_truncate_block(BTRFS_I(inode), offset + len,
0, 1);
if (ret) if (ret)
goto out; goto out;
} else { } else {
...@@ -3355,7 +3358,7 @@ static long btrfs_fallocate(struct file *file, int mode, ...@@ -3355,7 +3358,7 @@ static long btrfs_fallocate(struct file *file, int mode,
* need to zero out the end of the block if i_size lands in the * need to zero out the end of the block if i_size lands in the
* middle of a block. * middle of a block.
*/ */
ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
if (ret) if (ret)
goto out; goto out;
} }
......
...@@ -4655,12 +4655,12 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4655,12 +4655,12 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
* This will find the block for the "from" offset and cow the block and zero the * This will find the block for the "from" offset and cow the block and zero the
* part we want to zero. This is used with truncate and hole punching. * part we want to zero. This is used with truncate and hole punching.
*/ */
int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
int front) int front)
{ {
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); struct btrfs_fs_info *fs_info = inode->root->fs_info;
struct address_space *mapping = inode->i_mapping; struct address_space *mapping = inode->vfs_inode.i_mapping;
struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; struct extent_io_tree *io_tree = &inode->io_tree;
struct btrfs_ordered_extent *ordered; struct btrfs_ordered_extent *ordered;
struct extent_state *cached_state = NULL; struct extent_state *cached_state = NULL;
struct extent_changeset *data_reserved = NULL; struct extent_changeset *data_reserved = NULL;
...@@ -4683,30 +4683,29 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, ...@@ -4683,30 +4683,29 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
block_start = round_down(from, blocksize); block_start = round_down(from, blocksize);
block_end = block_start + blocksize - 1; block_end = block_start + blocksize - 1;
ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
block_start, blocksize); blocksize);
if (ret < 0) { if (ret < 0) {
if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start, if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
&write_bytes) > 0) {
/* For nocow case, no need to reserve data space */ /* For nocow case, no need to reserve data space */
only_release_metadata = true; only_release_metadata = true;
} else { } else {
goto out; goto out;
} }
} }
ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize); ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
if (ret < 0) { if (ret < 0) {
if (!only_release_metadata) if (!only_release_metadata)
btrfs_free_reserved_data_space(BTRFS_I(inode), btrfs_free_reserved_data_space(inode, data_reserved,
data_reserved, block_start, blocksize); block_start, blocksize);
goto out; goto out;
} }
again: again:
page = find_or_create_page(mapping, index, mask); page = find_or_create_page(mapping, index, mask);
if (!page) { if (!page) {
btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, btrfs_delalloc_release_space(inode, data_reserved, block_start,
block_start, blocksize, true); blocksize, true);
btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); btrfs_delalloc_release_extents(inode, blocksize);
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto out;
} }
...@@ -4729,7 +4728,7 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, ...@@ -4729,7 +4728,7 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
lock_extent_bits(io_tree, block_start, block_end, &cached_state); lock_extent_bits(io_tree, block_start, block_end, &cached_state);
set_page_extent_mapped(page); set_page_extent_mapped(page);
ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start); ordered = btrfs_lookup_ordered_extent(inode, block_start);
if (ordered) { if (ordered) {
unlock_extent_cached(io_tree, block_start, block_end, unlock_extent_cached(io_tree, block_start, block_end,
&cached_state); &cached_state);
...@@ -4740,11 +4739,11 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, ...@@ -4740,11 +4739,11 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
goto again; goto again;
} }
clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, clear_extent_bit(&inode->io_tree, block_start, block_end,
EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
0, 0, &cached_state); 0, 0, &cached_state);
ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0, ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
&cached_state); &cached_state);
if (ret) { if (ret) {
unlock_extent_cached(io_tree, block_start, block_end, unlock_extent_cached(io_tree, block_start, block_end,
...@@ -4770,24 +4769,23 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, ...@@ -4770,24 +4769,23 @@ int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
unlock_extent_cached(io_tree, block_start, block_end, &cached_state); unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
if (only_release_metadata) if (only_release_metadata)
set_extent_bit(&BTRFS_I(inode)->io_tree, block_start, set_extent_bit(&inode->io_tree, block_start, block_end,
block_end, EXTENT_NORESERVE, NULL, GFP_NOFS); EXTENT_NORESERVE, NULL, GFP_NOFS);
out_unlock: out_unlock:
if (ret) { if (ret) {
if (only_release_metadata) if (only_release_metadata)
btrfs_delalloc_release_metadata(BTRFS_I(inode), btrfs_delalloc_release_metadata(inode, blocksize, true);
blocksize, true);
else else
btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, btrfs_delalloc_release_space(inode, data_reserved,
block_start, blocksize, true); block_start, blocksize, true);
} }
btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize); btrfs_delalloc_release_extents(inode, blocksize);
unlock_page(page); unlock_page(page);
put_page(page); put_page(page);
out: out:
if (only_release_metadata) if (only_release_metadata)
btrfs_check_nocow_unlock(BTRFS_I(inode)); btrfs_check_nocow_unlock(inode);
extent_changeset_free(data_reserved); extent_changeset_free(data_reserved);
return ret; return ret;
} }
...@@ -4869,7 +4867,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) ...@@ -4869,7 +4867,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
* rest of the block before we expand the i_size, otherwise we could * rest of the block before we expand the i_size, otherwise we could
* expose stale data. * expose stale data.
*/ */
err = btrfs_truncate_block(inode, oldsize, 0, 0); err = btrfs_truncate_block(BTRFS_I(inode), oldsize, 0, 0);
if (err) if (err)
return err; return err;
...@@ -8553,7 +8551,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback) ...@@ -8553,7 +8551,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
btrfs_end_transaction(trans); btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info); btrfs_btree_balance_dirty(fs_info);
ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
if (ret) if (ret)
goto out; goto out;
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 1);
......
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