Skip to content

Commit fd4ffff

Browse files
zhangyi089smb49
authored andcommitted
xfs: convert delayed extents to unwritten when zeroing post eof blocks
BugLink: https://bugs.launchpad.net/bugs/2097575 commit 5ce5674187c345dc31534d2024c09ad8ef29b7ba upstream. Current clone operation could be non-atomic if the destination of a file is beyond EOF, user could get a file with corrupted (zeroed) data on crash. The problem is about preallocations. If you write some data into a file: [A...B) and XFS decides to preallocate some post-eof blocks, then it can create a delayed allocation reservation: [A.........D) The writeback path tries to convert delayed extents to real ones by allocating blocks. If there aren't enough contiguous free space, we can end up with two extents, the first real and the second still delalloc: [A....C)[C.D) After that, both the in-memory and the on-disk file sizes are still B. If we clone into the range [E...F) from another file: [A....C)[C.D) [E...F) then xfs_reflink_zero_posteof() calls iomap_zero_range() to zero out the range [B, E) beyond EOF and flush it. Since [C, D) is still a delalloc extent, its pagecache will be zeroed and both the in-memory and on-disk size will be updated to D after flushing but before cloning. This is wrong, because the user can see the size change and read the zeroes while the clone operation is ongoing. We need to keep the in-memory and on-disk size before the clone operation starts, so instead of writing zeroes through the page cache for delayed ranges beyond EOF, we convert these ranges to unwritten and invalidate any cached data over that range beyond EOF. Suggested-by: Dave Chinner <david@fromorbit.com> Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Chandan Babu R <chandanbabu@kernel.org> Signed-off-by: Catherine Hoang <catherine.hoang@oracle.com> Acked-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 721b870 commit fd4ffff

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

fs/xfs/xfs_iomap.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,24 @@ xfs_buffered_write_iomap_begin(
10051005
goto out_unlock;
10061006
}
10071007

1008+
/*
1009+
* For zeroing, trim a delalloc extent that extends beyond the EOF
1010+
* block. If it starts beyond the EOF block, convert it to an
1011+
* unwritten extent.
1012+
*/
1013+
if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb &&
1014+
isnullstartblock(imap.br_startblock)) {
1015+
xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
1016+
1017+
if (offset_fsb >= eof_fsb)
1018+
goto convert_delay;
1019+
if (end_fsb > eof_fsb) {
1020+
end_fsb = eof_fsb;
1021+
xfs_trim_extent(&imap, offset_fsb,
1022+
end_fsb - offset_fsb);
1023+
}
1024+
}
1025+
10081026
/*
10091027
* Search the COW fork extent list even if we did not find a data fork
10101028
* extent. This serves two purposes: first this implements the
@@ -1150,6 +1168,17 @@ xfs_buffered_write_iomap_begin(
11501168
xfs_iunlock(ip, lockmode);
11511169
return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq);
11521170

1171+
convert_delay:
1172+
xfs_iunlock(ip, lockmode);
1173+
truncate_pagecache(inode, offset);
1174+
error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset,
1175+
iomap, NULL);
1176+
if (error)
1177+
return error;
1178+
1179+
trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap);
1180+
return 0;
1181+
11531182
found_cow:
11541183
seq = xfs_iomap_inode_sequence(ip, 0);
11551184
if (imap.br_startoff <= offset_fsb) {

0 commit comments

Comments
 (0)