Skip to content

Commit 5885ebd

Browse files
dgchinnerdchinner
authored andcommitted
xfs: ensure truncate forces zeroed blocks to disk
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>
1 parent dfcc70a commit 5885ebd

3 files changed

Lines changed: 29 additions & 30 deletions

File tree

‎fs/xfs/xfs_file.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ STATIC int /* error (positive) */
397397
xfs_zero_last_block(
398398
struct xfs_inode *ip,
399399
xfs_fsize_t offset,
400-
xfs_fsize_t isize)
400+
xfs_fsize_t isize,
401+
bool *did_zeroing)
401402
{
402403
struct xfs_mount *mp = ip->i_mount;
403404
xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
@@ -425,6 +426,7 @@ xfs_zero_last_block(
425426
zero_len = mp->m_sb.sb_blocksize - zero_offset;
426427
if (isize + zero_len > offset)
427428
zero_len = offset - isize;
429+
*did_zeroing = true;
428430
return xfs_iozero(ip, isize, zero_len);
429431
}
430432

@@ -443,7 +445,8 @@ int /* error (positive) */
443445
xfs_zero_eof(
444446
struct xfs_inode *ip,
445447
xfs_off_t offset, /* starting I/O offset */
446-
xfs_fsize_t isize) /* current inode size */
448+
xfs_fsize_t isize, /* current inode size */
449+
bool *did_zeroing)
447450
{
448451
struct xfs_mount *mp = ip->i_mount;
449452
xfs_fileoff_t start_zero_fsb;
@@ -465,7 +468,7 @@ xfs_zero_eof(
465468
* We only zero a part of that block so it is handled specially.
466469
*/
467470
if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
468-
error = xfs_zero_last_block(ip, offset, isize);
471+
error = xfs_zero_last_block(ip, offset, isize, did_zeroing);
469472
if (error)
470473
return error;
471474
}
@@ -525,6 +528,7 @@ xfs_zero_eof(
525528
if (error)
526529
return error;
527530

531+
*did_zeroing = true;
528532
start_zero_fsb = imap.br_startoff + imap.br_blockcount;
529533
ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
530534
}
@@ -567,13 +571,15 @@ xfs_file_aio_write_checks(
567571
* having to redo all checks before.
568572
*/
569573
if (*pos > i_size_read(inode)) {
574+
bool zero = false;
575+
570576
if (*iolock == XFS_IOLOCK_SHARED) {
571577
xfs_rw_iunlock(ip, *iolock);
572578
*iolock = XFS_IOLOCK_EXCL;
573579
xfs_rw_ilock(ip, *iolock);
574580
goto restart;
575581
}
576-
error = xfs_zero_eof(ip, *pos, i_size_read(inode));
582+
error = xfs_zero_eof(ip, *pos, i_size_read(inode), &zero);
577583
if (error)
578584
return error;
579585
}

‎fs/xfs/xfs_inode.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,11 @@ enum xfs_prealloc_flags {
384384
XFS_PREALLOC_INVISIBLE = (1 << 4),
385385
};
386386

387-
int xfs_update_prealloc_flags(struct xfs_inode *,
388-
enum xfs_prealloc_flags);
389-
int xfs_zero_eof(struct xfs_inode *, xfs_off_t, xfs_fsize_t);
390-
int xfs_iozero(struct xfs_inode *, loff_t, size_t);
387+
int xfs_update_prealloc_flags(struct xfs_inode *ip,
388+
enum xfs_prealloc_flags flags);
389+
int xfs_zero_eof(struct xfs_inode *ip, xfs_off_t offset,
390+
xfs_fsize_t isize, bool *did_zeroing);
391+
int xfs_iozero(struct xfs_inode *ip, loff_t pos, size_t count);
391392

392393

393394
#define IHOLD(ip) \

‎fs/xfs/xfs_iops.c‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ xfs_setattr_size(
751751
int error;
752752
uint lock_flags = 0;
753753
uint commit_flags = 0;
754+
bool did_zeroing = false;
754755

755756
trace_xfs_setattr(ip);
756757

@@ -794,20 +795,16 @@ xfs_setattr_size(
794795
return error;
795796

796797
/*
797-
* Now we can make the changes. Before we join the inode to the
798-
* transaction, take care of the part of the truncation that must be
799-
* done without the inode lock. This needs to be done before joining
800-
* the inode to the transaction, because the inode cannot be unlocked
801-
* once it is a part of the transaction.
798+
* File data changes must be complete before we start the transaction to
799+
* modify the inode. This needs to be done before joining the inode to
800+
* the transaction because the inode cannot be unlocked once it is a
801+
* part of the transaction.
802+
*
803+
* Start with zeroing any data block beyond EOF that we may expose on
804+
* file extension.
802805
*/
803806
if (newsize > oldsize) {
804-
/*
805-
* Do the first part of growing a file: zero any data in the
806-
* last block that is beyond the old EOF. We need to do this
807-
* before the inode is joined to the transaction to modify
808-
* i_size.
809-
*/
810-
error = xfs_zero_eof(ip, newsize, oldsize);
807+
error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
811808
if (error)
812809
return error;
813810
}
@@ -817,23 +814,18 @@ xfs_setattr_size(
817814
* any previous writes that are beyond the on disk EOF and the new
818815
* EOF that have not been written out need to be written here. If we
819816
* do not write the data out, we expose ourselves to the null files
820-
* problem.
821-
*
822-
* Only flush from the on disk size to the smaller of the in memory
823-
* file size or the new size as that's the range we really care about
824-
* here and prevents waiting for other data not within the range we
825-
* care about here.
817+
* problem. Note that this includes any block zeroing we did above;
818+
* otherwise those blocks may not be zeroed after a crash.
826819
*/
827-
if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
820+
if (newsize > ip->i_d.di_size &&
821+
(oldsize != ip->i_d.di_size || did_zeroing)) {
828822
error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
829823
ip->i_d.di_size, newsize);
830824
if (error)
831825
return error;
832826
}
833827

834-
/*
835-
* Wait for all direct I/O to complete.
836-
*/
828+
/* Now wait for all direct I/O to complete. */
837829
inode_dio_wait(inode);
838830

839831
/*

0 commit comments

Comments
 (0)