Skip to content

Commit

Permalink
Btrfs: clean up error handling in btrfs_truncate()
Browse files Browse the repository at this point in the history
btrfs_truncate() uses two variables for error handling, ret and err (if
this sounds familiar, it's because btrfs_truncate_inode_items() did
something similar). This is error prone, as was made evident by "Btrfs:
fix error handling in btrfs_truncate()". We only have err because we
don't want to mask an error if we call btrfs_update_inode() and
btrfs_end_transaction(), so let's make that its own scoped return
variable and use ret everywhere else.

Reviewed-by: Nikolay Borisov <[email protected]>
Signed-off-by: Omar Sandoval <[email protected]>
Signed-off-by: David Sterba <[email protected]>
  • Loading branch information
osandov authored and kdave committed May 30, 2018
1 parent c5794e5 commit ad7e1a7
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -9030,8 +9030,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_block_rsv *rsv;
int ret = 0;
int err = 0;
int ret;
struct btrfs_trans_handle *trans;
u64 mask = fs_info->sectorsize - 1;
u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Expand Down Expand Up @@ -9083,7 +9082,7 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
*/
trans = btrfs_start_transaction(root, 2);
if (IS_ERR(trans)) {
err = PTR_ERR(trans);
ret = PTR_ERR(trans);
goto out;
}

Expand All @@ -9107,24 +9106,19 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
inode->i_size,
BTRFS_EXTENT_DATA_KEY);
trans->block_rsv = &fs_info->trans_block_rsv;
if (ret != -ENOSPC && ret != -EAGAIN) {
if (ret < 0)
err = ret;
if (ret != -ENOSPC && ret != -EAGAIN)
break;
}

ret = btrfs_update_inode(trans, root, inode);
if (ret) {
err = ret;
if (ret)
break;
}

btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info);

trans = btrfs_start_transaction(root, 2);
if (IS_ERR(trans)) {
ret = err = PTR_ERR(trans);
ret = PTR_ERR(trans);
trans = NULL;
break;
}
Expand Down Expand Up @@ -9158,21 +9152,22 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
}

if (trans) {
int ret2;

trans->block_rsv = &fs_info->trans_block_rsv;
ret = btrfs_update_inode(trans, root, inode);
if (ret && !err)
err = ret;
ret2 = btrfs_update_inode(trans, root, inode);
if (ret2 && !ret)
ret = ret2;

ret = btrfs_end_transaction(trans);
ret2 = btrfs_end_transaction(trans);
if (ret2 && !ret)
ret = ret2;
btrfs_btree_balance_dirty(fs_info);
}
out:
btrfs_free_block_rsv(fs_info, rsv);

if (ret && !err)
err = ret;

return err;
return ret;
}

/*
Expand Down

0 comments on commit ad7e1a7

Please sign in to comment.