• Zhang Yi's avatar
    xfs: convert delayed extents to unwritten when zeroing post eof blocks · 5ce56741
    Zhang Yi authored
    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: default avatarDave Chinner <david@fromorbit.com>
    Signed-off-by: default avatarZhang Yi <yi.zhang@huawei.com>
    Reviewed-by: default avatar"Darrick J. Wong" <djwong@kernel.org>
    Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
    Signed-off-by: default avatarChandan Babu R <chandanbabu@kernel.org>
    5ce56741
xfs_iomap.c 39.6 KB