Skip to content

Commit 48ca7d7

Browse files
dgchinnersashalevin
authored andcommitted
xfs: ensure truncate forces zeroed blocks to disk
[ Upstream commit 5885ebd ] A new fsync vs power fail test in xfstests indicated that XFS can have unreliable data consistency when doing extending truncates that require block zeroing. The blocks beyond EOF get zeroed in memory, but we never force those changes to disk before we run the transaction that extends the file size and exposes those blocks to userspace. This can result in the blocks not being correctly zeroed after a crash. Because in-memory behaviour is correct, tools like fsx don't pick up any coherency problems - it's not until the filesystem is shutdown or the system crashes after writing the truncate transaction to the journal but before the zeroed data in the page cache is flushed that the issue is exposed. Fix this by also flushing the dirty data in memory region between the old size and new size when we've found blocks that need zeroing in the truncate process. Reported-by: Liu Bo <bo.li.liu@oracle.com> cc: <stable@vger.kernel.org> Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Brian Foster <bfoster@redhat.com> Signed-off-by: Dave Chinner <david@fromorbit.com> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent a699573 commit 48ca7d7

3 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎fs/xfs/xfs_file.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ STATIC int /* error (positive) */
363363
xfs_zero_last_block(
364364
struct xfs_inode *ip,
365365
xfs_fsize_t offset,
366-
xfs_fsize_t isize)
366+
xfs_fsize_t isize,
367+
bool *did_zeroing)
367368
{
368369
struct xfs_mount *mp = ip->i_mount;
369370
xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
@@ -391,6 +392,7 @@ xfs_zero_last_block(
391392
zero_len = mp->m_sb.sb_blocksize - zero_offset;
392393
if (isize + zero_len > offset)
393394
zero_len = offset - isize;
395+
*did_zeroing = true;
394396
return xfs_iozero(ip, isize, zero_len);
395397
}
396398

@@ -409,7 +411,8 @@ int /* error (positive) */
409411
xfs_zero_eof(
410412
struct xfs_inode *ip,
411413
xfs_off_t offset, /* starting I/O offset */
412-
xfs_fsize_t isize) /* current inode size */
414+
xfs_fsize_t isize, /* current inode size */
415+
bool *did_zeroing)
413416
{
414417
struct xfs_mount *mp = ip->i_mount;
415418
xfs_fileoff_t start_zero_fsb;
@@ -431,7 +434,7 @@ xfs_zero_eof(
431434
* We only zero a part of that block so it is handled specially.
432435
*/
433436
if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
434-
error = xfs_zero_last_block(ip, offset, isize);
437+
error = xfs_zero_last_block(ip, offset, isize, did_zeroing);
435438
if (error)
436439
return error;
437440
}
@@ -491,6 +494,7 @@ xfs_zero_eof(
491494
if (error)
492495
return error;
493496

497+
*did_zeroing = true;
494498
start_zero_fsb = imap.br_startoff + imap.br_blockcount;
495499
ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
496500
}
@@ -529,13 +533,15 @@ xfs_file_aio_write_checks(
529533
* having to redo all checks before.
530534
*/
531535
if (*pos > i_size_read(inode)) {
536+
bool zero = false;
537+
532538
if (*iolock == XFS_IOLOCK_SHARED) {
533539
xfs_rw_iunlock(ip, *iolock);
534540
*iolock = XFS_IOLOCK_EXCL;
535541
xfs_rw_ilock(ip, *iolock);
536542
goto restart;
537543
}
538-
error = xfs_zero_eof(ip, *pos, i_size_read(inode));
544+
error = xfs_zero_eof(ip, *pos, i_size_read(inode), &zero);
539545
if (error)
540546
return error;
541547
}

‎fs/xfs/xfs_inode.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,9 @@ int xfs_droplink(struct xfs_trans *, struct xfs_inode *);
379379
int xfs_bumplink(struct xfs_trans *, struct xfs_inode *);
380380

381381
/* from xfs_file.c */
382-
int xfs_zero_eof(struct xfs_inode *, xfs_off_t, xfs_fsize_t);
383-
int xfs_iozero(struct xfs_inode *, loff_t, size_t);
382+
int xfs_zero_eof(struct xfs_inode *ip, xfs_off_t offset,
383+
xfs_fsize_t isize, bool *did_zeroing);
384+
int xfs_iozero(struct xfs_inode *ip, loff_t pos, size_t count);
384385

385386

386387
#define IHOLD(ip) \

‎fs/xfs/xfs_iops.c‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ xfs_setattr_size(
744744
int error;
745745
uint lock_flags = 0;
746746
uint commit_flags = 0;
747+
bool did_zeroing = false;
747748

748749
trace_xfs_setattr(ip);
749750

@@ -787,20 +788,16 @@ xfs_setattr_size(
787788
return error;
788789

789790
/*
790-
* Now we can make the changes. Before we join the inode to the
791-
* transaction, take care of the part of the truncation that must be
792-
* done without the inode lock. This needs to be done before joining
793-
* the inode to the transaction, because the inode cannot be unlocked
794-
* once it is a part of the transaction.
791+
* File data changes must be complete before we start the transaction to
792+
* modify the inode. This needs to be done before joining the inode to
793+
* the transaction because the inode cannot be unlocked once it is a
794+
* part of the transaction.
795+
*
796+
* Start with zeroing any data block beyond EOF that we may expose on
797+
* file extension.
795798
*/
796799
if (newsize > oldsize) {
797-
/*
798-
* Do the first part of growing a file: zero any data in the
799-
* last block that is beyond the old EOF. We need to do this
800-
* before the inode is joined to the transaction to modify
801-
* i_size.
802-
*/
803-
error = xfs_zero_eof(ip, newsize, oldsize);
800+
error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
804801
if (error)
805802
return error;
806803
}
@@ -810,23 +807,18 @@ xfs_setattr_size(
810807
* any previous writes that are beyond the on disk EOF and the new
811808
* EOF that have not been written out need to be written here. If we
812809
* do not write the data out, we expose ourselves to the null files
813-
* problem.
814-
*
815-
* Only flush from the on disk size to the smaller of the in memory
816-
* file size or the new size as that's the range we really care about
817-
* here and prevents waiting for other data not within the range we
818-
* care about here.
810+
* problem. Note that this includes any block zeroing we did above;
811+
* otherwise those blocks may not be zeroed after a crash.
819812
*/
820-
if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
813+
if (newsize > ip->i_d.di_size &&
814+
(oldsize != ip->i_d.di_size || did_zeroing)) {
821815
error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
822816
ip->i_d.di_size, newsize);
823817
if (error)
824818
return error;
825819
}
826820

827-
/*
828-
* Wait for all direct I/O to complete.
829-
*/
821+
/* Now wait for all direct I/O to complete. */
830822
inode_dio_wait(inode);
831823

832824
/*

0 commit comments

Comments
 (0)