Skip to content

Commit 6bba447

Browse files
biger410torvalds
authored andcommitted
ocfs2: fix data corruption by fallocate
When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size. Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily. qemu-img convert -p -t none -T none -f qcow2 $qcow_image \ -O qcow2 -o compat=1.1 $qcow_image.conv The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size. fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0 v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html v2: https://lore.kernel.org/linux-fsdevel/[email protected]/T/ Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Junxiao Bi <[email protected]> Reviewed-by: Joseph Qi <[email protected]> Cc: Jan Kara <[email protected]> Cc: Mark Fasheh <[email protected]> Cc: Joel Becker <[email protected]> Cc: Changwei Ge <[email protected]> Cc: Gang He <[email protected]> Cc: Jun Piao <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 415f0c8 commit 6bba447

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

fs/ocfs2/file.c

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
18551855
return ret;
18561856
}
18571857

1858+
/*
1859+
* zero out partial blocks of one cluster.
1860+
*
1861+
* start: file offset where zero starts, will be made upper block aligned.
1862+
* len: it will be trimmed to the end of current cluster if "start + len"
1863+
* is bigger than it.
1864+
*/
1865+
static int ocfs2_zeroout_partial_cluster(struct inode *inode,
1866+
u64 start, u64 len)
1867+
{
1868+
int ret;
1869+
u64 start_block, end_block, nr_blocks;
1870+
u64 p_block, offset;
1871+
u32 cluster, p_cluster, nr_clusters;
1872+
struct super_block *sb = inode->i_sb;
1873+
u64 end = ocfs2_align_bytes_to_clusters(sb, start);
1874+
1875+
if (start + len < end)
1876+
end = start + len;
1877+
1878+
start_block = ocfs2_blocks_for_bytes(sb, start);
1879+
end_block = ocfs2_blocks_for_bytes(sb, end);
1880+
nr_blocks = end_block - start_block;
1881+
if (!nr_blocks)
1882+
return 0;
1883+
1884+
cluster = ocfs2_bytes_to_clusters(sb, start);
1885+
ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
1886+
&nr_clusters, NULL);
1887+
if (ret)
1888+
return ret;
1889+
if (!p_cluster)
1890+
return 0;
1891+
1892+
offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
1893+
p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
1894+
return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
1895+
}
1896+
18581897
/*
18591898
* Parts of this function taken from xfs_change_file_space()
18601899
*/
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
18651904
{
18661905
int ret;
18671906
s64 llen;
1868-
loff_t size;
1907+
loff_t size, orig_isize;
18691908
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
18701909
struct buffer_head *di_bh = NULL;
18711910
handle_t *handle;
@@ -1896,14 +1935,15 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
18961935
goto out_inode_unlock;
18971936
}
18981937

1938+
orig_isize = i_size_read(inode);
18991939
switch (sr->l_whence) {
19001940
case 0: /*SEEK_SET*/
19011941
break;
19021942
case 1: /*SEEK_CUR*/
19031943
sr->l_start += f_pos;
19041944
break;
19051945
case 2: /*SEEK_END*/
1906-
sr->l_start += i_size_read(inode);
1946+
sr->l_start += orig_isize;
19071947
break;
19081948
default:
19091949
ret = -EINVAL;
@@ -1957,6 +1997,14 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
19571997
default:
19581998
ret = -EINVAL;
19591999
}
2000+
2001+
/* zeroout eof blocks in the cluster. */
2002+
if (!ret && change_size && orig_isize < size) {
2003+
ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
2004+
size - orig_isize);
2005+
if (!ret)
2006+
i_size_write(inode, size);
2007+
}
19602008
up_write(&OCFS2_I(inode)->ip_alloc_sem);
19612009
if (ret) {
19622010
mlog_errno(ret);
@@ -1973,9 +2021,6 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
19732021
goto out_inode_unlock;
19742022
}
19752023

1976-
if (change_size && i_size_read(inode) < size)
1977-
i_size_write(inode, size);
1978-
19792024
inode->i_ctime = inode->i_mtime = current_time(inode);
19802025
ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
19812026
if (ret < 0)

0 commit comments

Comments
 (0)