Skip to content

Commit d0c2204

Browse files
Brian Fosterdjwong
authored andcommitted
xfs: stabilize insert range start boundary to avoid COW writeback race
generic/522 (fsx) occasionally fails with a file corruption due to an insert range operation. The primary characteristic of the corruption is a misplaced insert range operation that differs from the requested target offset. The reason for this behavior is a race between the extent shift sequence of an insert range and a COW writeback completion that causes a front merge with the first extent in the shift. The shift preparation function flushes and unmaps from the target offset of the operation to the end of the file to ensure no modifications can be made and page cache is invalidated before file data is shifted. An insert range operation then splits the extent at the target offset, if necessary, and begins to shift the start offset of each extent starting from the end of the file to the start offset. The shift sequence operates at extent level and so depends on the preparation sequence to guarantee no changes can be made to the target range during the shift. If the block immediately prior to the target offset was dirty and shared, however, it can undergo writeback and move from the COW fork to the data fork at any point during the shift. If the block is contiguous with the block at the start offset of the insert range, it can front merge and alter the start offset of the extent. Once the shift sequence reaches the target offset, it shifts based on the latest start offset and silently changes the target offset of the operation and corrupts the file. To address this problem, update the shift preparation code to stabilize the start boundary along with the full range of the insert. Also update the existing corruption check to fail if any extent is shifted with a start offset behind the target offset of the insert range. This prevents insert from racing with COW writeback completion and fails loudly in the event of an unexpected extent shift. Signed-off-by: Brian Foster <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]>
1 parent 99528ef commit d0c2204

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,8 +5972,7 @@ xfs_bmap_insert_extents(
59725972
goto del_cursor;
59735973
}
59745974

5975-
if (XFS_IS_CORRUPT(mp,
5976-
stop_fsb >= got.br_startoff + got.br_blockcount)) {
5975+
if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
59775976
error = -EFSCORRUPTED;
59785977
goto del_cursor;
59795978
}

fs/xfs/xfs_bmap_util.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ xfs_prepare_shift(
992992
struct xfs_inode *ip,
993993
loff_t offset)
994994
{
995+
struct xfs_mount *mp = ip->i_mount;
995996
int error;
996997

997998
/*
@@ -1004,6 +1005,17 @@ xfs_prepare_shift(
10041005
return error;
10051006
}
10061007

1008+
/*
1009+
* Shift operations must stabilize the start block offset boundary along
1010+
* with the full range of the operation. If we don't, a COW writeback
1011+
* completion could race with an insert, front merge with the start
1012+
* extent (after split) during the shift and corrupt the file. Start
1013+
* with the block just prior to the start to stabilize the boundary.
1014+
*/
1015+
offset = round_down(offset, 1 << mp->m_sb.sb_blocklog);
1016+
if (offset)
1017+
offset -= (1 << mp->m_sb.sb_blocklog);
1018+
10071019
/*
10081020
* Writeback and invalidate cache for the remainder of the file as we're
10091021
* about to shift down every extent from offset to EOF.

0 commit comments

Comments
 (0)