Skip to content

Commit c7d2cbc

Browse files
biger410torvalds
authored andcommitted
ocfs2: update inode size after zeroing the hole
fs-writeback will release the dirty pages without page lock whose offset are over inode size, the release happens at block_write_full_page_endio(). If not update, dirty pages in file holes may be released before flushed to the disk, then file holes will contain some non-zero data, this will cause sparse file md5sum error. To reproduce the bug, find a big sparse file with many holes, like vm image file, its actual size should be bigger than available mem size to make writeback work more frequently, tar it with -S option, then keep untar it and check its md5sum again and again until you get a wrong md5sum. Signed-off-by: Junxiao Bi <[email protected]> Cc: Younger Liu <[email protected]> Reviewed-by: Mark Fasheh <[email protected]> Cc: Joel Becker <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent d62e74b commit c7d2cbc

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

fs/ocfs2/file.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
716716
* While a write will already be ordering the data, a truncate will not.
717717
* Thus, we need to explicitly order the zeroed pages.
718718
*/
719-
static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
719+
static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
720+
struct buffer_head *di_bh)
720721
{
721722
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
722723
handle_t *handle = NULL;
@@ -733,7 +734,14 @@ static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
733734
}
734735

735736
ret = ocfs2_jbd2_file_inode(handle, inode);
736-
if (ret < 0)
737+
if (ret < 0) {
738+
mlog_errno(ret);
739+
goto out;
740+
}
741+
742+
ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
743+
OCFS2_JOURNAL_ACCESS_WRITE);
744+
if (ret)
737745
mlog_errno(ret);
738746

739747
out:
@@ -749,14 +757,15 @@ static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode)
749757
* to be too fragile to do exactly what we need without us having to
750758
* worry about recursive locking in ->write_begin() and ->write_end(). */
751759
static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
752-
u64 abs_to)
760+
u64 abs_to, struct buffer_head *di_bh)
753761
{
754762
struct address_space *mapping = inode->i_mapping;
755763
struct page *page;
756764
unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
757765
handle_t *handle = NULL;
758766
int ret = 0;
759767
unsigned zero_from, zero_to, block_start, block_end;
768+
struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
760769

761770
BUG_ON(abs_from >= abs_to);
762771
BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
@@ -799,7 +808,8 @@ static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
799808
}
800809

801810
if (!handle) {
802-
handle = ocfs2_zero_start_ordered_transaction(inode);
811+
handle = ocfs2_zero_start_ordered_transaction(inode,
812+
di_bh);
803813
if (IS_ERR(handle)) {
804814
ret = PTR_ERR(handle);
805815
handle = NULL;
@@ -816,8 +826,22 @@ static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
816826
ret = 0;
817827
}
818828

819-
if (handle)
829+
if (handle) {
830+
/*
831+
* fs-writeback will release the dirty pages without page lock
832+
* whose offset are over inode size, the release happens at
833+
* block_write_full_page_endio().
834+
*/
835+
i_size_write(inode, abs_to);
836+
inode->i_blocks = ocfs2_inode_sector_count(inode);
837+
di->i_size = cpu_to_le64((u64)i_size_read(inode));
838+
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
839+
di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
840+
di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
841+
di->i_mtime_nsec = di->i_ctime_nsec;
842+
ocfs2_journal_dirty(handle, di_bh);
820843
ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
844+
}
821845

822846
out_unlock:
823847
unlock_page(page);
@@ -913,7 +937,7 @@ static int ocfs2_zero_extend_get_range(struct inode *inode,
913937
* has made sure that the entire range needs zeroing.
914938
*/
915939
static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
916-
u64 range_end)
940+
u64 range_end, struct buffer_head *di_bh)
917941
{
918942
int rc = 0;
919943
u64 next_pos;
@@ -929,7 +953,7 @@ static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
929953
next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
930954
if (next_pos > range_end)
931955
next_pos = range_end;
932-
rc = ocfs2_write_zero_page(inode, zero_pos, next_pos);
956+
rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
933957
if (rc < 0) {
934958
mlog_errno(rc);
935959
break;
@@ -975,7 +999,7 @@ int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
975999
range_end = zero_to_size;
9761000

9771001
ret = ocfs2_zero_extend_range(inode, range_start,
978-
range_end);
1002+
range_end, di_bh);
9791003
if (ret) {
9801004
mlog_errno(ret);
9811005
break;

0 commit comments

Comments
 (0)