Skip to content

Commit 5ce5674

Browse files
zhangyi089Chandan Babu R
authored andcommitted
xfs: convert delayed extents to unwritten when zeroing post eof blocks
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 <[email protected]> Signed-off-by: Zhang Yi <[email protected]> Reviewed-by: "Darrick J. Wong" <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Chandan Babu R <[email protected]>
1 parent 2e08371 commit 5ce5674

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
@@ -1039,6 +1039,24 @@ xfs_buffered_write_iomap_begin(
10391039
goto out_unlock;
10401040
}
10411041

1042+
/*
1043+
* For zeroing, trim a delalloc extent that extends beyond the EOF
1044+
* block. If it starts beyond the EOF block, convert it to an
1045+
* unwritten extent.
1046+
*/
1047+
if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb &&
1048+
isnullstartblock(imap.br_startblock)) {
1049+
xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
1050+
1051+
if (offset_fsb >= eof_fsb)
1052+
goto convert_delay;
1053+
if (end_fsb > eof_fsb) {
1054+
end_fsb = eof_fsb;
1055+
xfs_trim_extent(&imap, offset_fsb,
1056+
end_fsb - offset_fsb);
1057+
}
1058+
}
1059+
10421060
/*
10431061
* Search the COW fork extent list even if we did not find a data fork
10441062
* extent. This serves two purposes: first this implements the
@@ -1184,6 +1202,17 @@ xfs_buffered_write_iomap_begin(
11841202
xfs_iunlock(ip, lockmode);
11851203
return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq);
11861204

1205+
convert_delay:
1206+
xfs_iunlock(ip, lockmode);
1207+
truncate_pagecache(inode, offset);
1208+
error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset,
1209+
iomap, NULL);
1210+
if (error)
1211+
return error;
1212+
1213+
trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap);
1214+
return 0;
1215+
11871216
found_cow:
11881217
seq = xfs_iomap_inode_sequence(ip, 0);
11891218
if (imap.br_startoff <= offset_fsb) {

0 commit comments

Comments
 (0)