Skip to content

Commit

Permalink
ocfs2: fix data corruption after failed write
Browse files Browse the repository at this point in the history
When buffered write fails to copy data into underlying page cache page,
ocfs2_write_end_nolock() just zeroes out and dirties the page.  This can
leave dirty page beyond EOF and if page writeback tries to write this page
before write succeeds and expands i_size, page gets into inconsistent
state where page dirty bit is clear but buffer dirty bits stay set
resulting in page data never getting written and so data copied to the
page is lost.  Fix the problem by invalidating page beyond EOF after
failed write.

Link: https://lkml.kernel.org/r/[email protected]
Fixes: 6dbf7bb ("fs: Don't invalidate page buffers in block_write_full_page()")
Signed-off-by: Jan Kara <[email protected]>
Reviewed-by: Joseph Qi <[email protected]>
Cc: Mark Fasheh <[email protected]>
Cc: Joel Becker <[email protected]>
Cc: Junxiao Bi <[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]>
  • Loading branch information
Jan Kara via Ocfs2-devel authored and akpm00 committed Mar 8, 2023
1 parent 2ef7dbb commit 90410bc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions fs/ocfs2/aops.c
Original file line number Diff line number Diff line change
Expand Up @@ -1977,11 +1977,26 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
}

if (unlikely(copied < len) && wc->w_target_page) {
loff_t new_isize;

if (!PageUptodate(wc->w_target_page))
copied = 0;

ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
start+len);
new_isize = max_t(loff_t, i_size_read(inode), pos + copied);
if (new_isize > page_offset(wc->w_target_page))
ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
start+len);
else {
/*
* When page is fully beyond new isize (data copy
* failed), do not bother zeroing the page. Invalidate
* it instead so that writeback does not get confused
* put page & buffer dirty bits into inconsistent
* state.
*/
block_invalidate_folio(page_folio(wc->w_target_page),
0, PAGE_SIZE);
}
}
if (wc->w_target_page)
flush_dcache_page(wc->w_target_page);
Expand Down

0 comments on commit 90410bc

Please sign in to comment.