Skip to content

Commit

Permalink
Btrfs: fix wrong i_size when truncating a file to a larger size
Browse files Browse the repository at this point in the history
Btrfsck report error 100 after the 83th case of xfstests was run, it means
the i_size of the file is wrong.

The reason of this bug is that:
Btrfs increased i_size of the file at the beginning, but it failed to expand
the file, and failed to update the i_size to the old size because there is no
enough space in the file system, so we found a wrong i_size.

This patch fixes this bug by updating the i_size just when we pass the file
expanding and get enough space to update i-node.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Miao Xie authored and Chris Mason committed Dec 15, 2011
1 parent 5dbc8fc commit f4a2f4c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3370,23 +3370,29 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)

static int btrfs_setsize(struct inode *inode, loff_t newsize)
{
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_trans_handle *trans;
loff_t oldsize = i_size_read(inode);
int ret;

if (newsize == oldsize)
return 0;

if (newsize > oldsize) {
i_size_write(inode, newsize);
btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
truncate_pagecache(inode, oldsize, newsize);
ret = btrfs_cont_expand(inode, oldsize, newsize);
if (ret) {
btrfs_setsize(inode, oldsize);
if (ret)
return ret;
}

mark_inode_dirty(inode);
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans))
return PTR_ERR(trans);

i_size_write(inode, newsize);
btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
ret = btrfs_update_inode(trans, root, inode);

btrfs_end_transaction_throttle(trans, root);
} else {

/*
Expand Down

0 comments on commit f4a2f4c

Please sign in to comment.