Commit 71d18b53 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: add inode to truncate control

In the future we're going to want to use btrfs_truncate_inode_items
without looking up the associated inode.  In order to accommodate this
add the inode to btrfs_truncate_control and handle the case where
control->inode is NULL appropriately.  This is fairly straightforward,
we simply need to add a helper for the trace points, as the file extent
map update is controlled by a flag on btrfs_truncate_control.
Reviewed-by: default avatarFilipe Manana <fdmanana@suse.com>
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 487e81d2
...@@ -292,6 +292,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, ...@@ -292,6 +292,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
struct inode *vfs_inode) struct inode *vfs_inode)
{ {
struct btrfs_truncate_control control = { struct btrfs_truncate_control control = {
.inode = BTRFS_I(vfs_inode),
.new_size = 0, .new_size = 0,
.ino = btrfs_ino(BTRFS_I(vfs_inode)), .ino = btrfs_ino(BTRFS_I(vfs_inode)),
.min_type = BTRFS_EXTENT_DATA_KEY, .min_type = BTRFS_EXTENT_DATA_KEY,
...@@ -339,7 +340,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, ...@@ -339,7 +340,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
* We skip the throttling logic for free space cache inodes, so we don't * We skip the throttling logic for free space cache inodes, so we don't
* need to check for -EAGAIN. * need to check for -EAGAIN.
*/ */
ret = btrfs_truncate_inode_items(trans, root, inode, &control); ret = btrfs_truncate_inode_items(trans, root, &control);
inode_sub_bytes(&inode->vfs_inode, control.sub_bytes); inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
btrfs_inode_safe_disk_i_size_write(inode, control.last_size); btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
......
...@@ -419,6 +419,20 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -419,6 +419,20 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
return ret; return ret;
} }
static inline void btrfs_trace_truncate(struct btrfs_inode *inode,
struct extent_buffer *leaf,
struct btrfs_file_extent_item *fi,
u64 offset, int extent_type, int slot)
{
if (!inode)
return;
if (extent_type == BTRFS_FILE_EXTENT_INLINE)
trace_btrfs_truncate_show_fi_inline(inode, leaf, fi, slot,
offset);
else
trace_btrfs_truncate_show_fi_regular(inode, leaf, fi, offset);
}
/* /*
* Remove inode items from a given root. * Remove inode items from a given root.
* *
...@@ -439,7 +453,6 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root ...@@ -439,7 +453,6 @@ int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
*/ */
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,
struct btrfs_truncate_control *control) struct btrfs_truncate_control *control)
{ {
struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_fs_info *fs_info = root->fs_info;
...@@ -462,6 +475,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -462,6 +475,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
bool be_nice = false; bool be_nice = false;
bool should_throttle = false; bool should_throttle = false;
ASSERT(control->inode || !control->clear_extent_range);
BUG_ON(new_size > 0 && control->min_type != BTRFS_EXTENT_DATA_KEY); BUG_ON(new_size > 0 && control->min_type != BTRFS_EXTENT_DATA_KEY);
control->last_size = new_size; control->last_size = new_size;
...@@ -526,19 +540,15 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -526,19 +540,15 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
fi = btrfs_item_ptr(leaf, path->slots[0], fi = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
extent_type = btrfs_file_extent_type(leaf, fi); extent_type = btrfs_file_extent_type(leaf, fi);
if (extent_type != BTRFS_FILE_EXTENT_INLINE) { if (extent_type != BTRFS_FILE_EXTENT_INLINE)
item_end += item_end +=
btrfs_file_extent_num_bytes(leaf, fi); btrfs_file_extent_num_bytes(leaf, fi);
else if (extent_type == BTRFS_FILE_EXTENT_INLINE)
trace_btrfs_truncate_show_fi_regular(
inode, leaf, fi, found_key.offset);
} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
item_end += btrfs_file_extent_ram_bytes(leaf, fi); item_end += btrfs_file_extent_ram_bytes(leaf, fi);
trace_btrfs_truncate_show_fi_inline( btrfs_trace_truncate(control->inode, leaf, fi,
inode, leaf, fi, path->slots[0], found_key.offset, extent_type,
found_key.offset); path->slots[0]);
}
item_end--; item_end--;
} }
if (found_type > control->min_type) { if (found_type > control->min_type) {
...@@ -628,7 +638,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -628,7 +638,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
* normal truncate path. * normal truncate path.
*/ */
if (control->clear_extent_range) { if (control->clear_extent_range) {
ret = btrfs_inode_clear_file_extent_range(inode, ret = btrfs_inode_clear_file_extent_range(control->inode,
clear_start, clear_len); clear_start, clear_len);
if (ret) { if (ret) {
btrfs_abort_transaction(trans, ret); btrfs_abort_transaction(trans, ret);
......
...@@ -20,6 +20,12 @@ struct extent_buffer; ...@@ -20,6 +20,12 @@ struct extent_buffer;
#define BTRFS_NEED_TRUNCATE_BLOCK 1 #define BTRFS_NEED_TRUNCATE_BLOCK 1
struct btrfs_truncate_control { struct btrfs_truncate_control {
/*
* IN: the inode we're operating on, this can be NULL if
* ->clear_extent_range is false.
*/
struct btrfs_inode *inode;
/* IN: the size we're truncating to. */ /* IN: the size we're truncating to. */
u64 new_size; u64 new_size;
...@@ -56,7 +62,6 @@ struct btrfs_truncate_control { ...@@ -56,7 +62,6 @@ struct btrfs_truncate_control {
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,
struct btrfs_truncate_control *control); struct btrfs_truncate_control *control);
int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
......
...@@ -5252,6 +5252,7 @@ void btrfs_evict_inode(struct inode *inode) ...@@ -5252,6 +5252,7 @@ void btrfs_evict_inode(struct inode *inode)
while (1) { while (1) {
struct btrfs_truncate_control control = { struct btrfs_truncate_control control = {
.inode = BTRFS_I(inode),
.ino = btrfs_ino(BTRFS_I(inode)), .ino = btrfs_ino(BTRFS_I(inode)),
.new_size = 0, .new_size = 0,
.min_type = 0, .min_type = 0,
...@@ -5263,8 +5264,7 @@ void btrfs_evict_inode(struct inode *inode) ...@@ -5263,8 +5264,7 @@ void btrfs_evict_inode(struct inode *inode)
trans->block_rsv = rsv; trans->block_rsv = rsv;
ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), ret = btrfs_truncate_inode_items(trans, root, &control);
&control);
trans->block_rsv = &fs_info->trans_block_rsv; trans->block_rsv = &fs_info->trans_block_rsv;
btrfs_end_transaction(trans); btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info); btrfs_btree_balance_dirty(fs_info);
...@@ -8534,6 +8534,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) ...@@ -8534,6 +8534,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
static int btrfs_truncate(struct inode *inode, bool skip_writeback) static int btrfs_truncate(struct inode *inode, bool skip_writeback)
{ {
struct btrfs_truncate_control control = { struct btrfs_truncate_control control = {
.inode = BTRFS_I(inode),
.ino = btrfs_ino(BTRFS_I(inode)), .ino = btrfs_ino(BTRFS_I(inode)),
.min_type = BTRFS_EXTENT_DATA_KEY, .min_type = BTRFS_EXTENT_DATA_KEY,
.clear_extent_range = true, .clear_extent_range = true,
...@@ -8621,8 +8622,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback) ...@@ -8621,8 +8622,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
ALIGN(new_size, fs_info->sectorsize), ALIGN(new_size, fs_info->sectorsize),
(u64)-1, 0); (u64)-1, 0);
ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode), ret = btrfs_truncate_inode_items(trans, root, &control);
&control);
inode_sub_bytes(inode, control.sub_bytes); inode_sub_bytes(inode, control.sub_bytes);
btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size); btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size);
......
...@@ -4107,8 +4107,7 @@ static int truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4107,8 +4107,7 @@ static int truncate_inode_items(struct btrfs_trans_handle *trans,
int ret; int ret;
do { do {
ret = btrfs_truncate_inode_items(trans, log_root, inode, ret = btrfs_truncate_inode_items(trans, log_root, &control);
&control);
} while (ret == -EAGAIN); } while (ret == -EAGAIN);
return ret; return ret;
......
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