Skip to content

Commit a5949d3

Browse files
committed
xfs: force writes to delalloc regions to unwritten
When writing to a delalloc region in the data fork, commit the new allocations (of the da reservation) as unwritten so that the mappings are only marked written once writeback completes successfully. This fixes the problem of stale data exposure if the system goes down during targeted writeback of a specific region of a file, as tested by generic/042. Signed-off-by: Darrick J. Wong <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Brian Foster <[email protected]>
1 parent 590b165 commit a5949d3

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4145,17 +4145,7 @@ xfs_bmapi_allocate(
41454145
bma->got.br_blockcount = bma->length;
41464146
bma->got.br_state = XFS_EXT_NORM;
41474147

4148-
/*
4149-
* In the data fork, a wasdelay extent has been initialized, so
4150-
* shouldn't be flagged as unwritten.
4151-
*
4152-
* For the cow fork, however, we convert delalloc reservations
4153-
* (extents allocated for speculative preallocation) to
4154-
* allocated unwritten extents, and only convert the unwritten
4155-
* extents to real extents when we're about to write the data.
4156-
*/
4157-
if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4158-
(bma->flags & XFS_BMAPI_PREALLOC))
4148+
if (bma->flags & XFS_BMAPI_PREALLOC)
41594149
bma->got.br_state = XFS_EXT_UNWRITTEN;
41604150

41614151
if (bma->wasdel)
@@ -4563,8 +4553,23 @@ xfs_bmapi_convert_delalloc(
45634553
bma.offset = bma.got.br_startoff;
45644554
bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, MAXEXTLEN);
45654555
bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4556+
4557+
/*
4558+
* When we're converting the delalloc reservations backing dirty pages
4559+
* in the page cache, we must be careful about how we create the new
4560+
* extents:
4561+
*
4562+
* New CoW fork extents are created unwritten, turned into real extents
4563+
* when we're about to write the data to disk, and mapped into the data
4564+
* fork after the write finishes. End of story.
4565+
*
4566+
* New data fork extents must be mapped in as unwritten and converted
4567+
* to real extents after the write succeeds to avoid exposing stale
4568+
* disk contents if we crash.
4569+
*/
4570+
bma.flags = XFS_BMAPI_PREALLOC;
45664571
if (whichfork == XFS_COW_FORK)
4567-
bma.flags = XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
4572+
bma.flags |= XFS_BMAPI_COWFORK;
45684573

45694574
if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
45704575
bma.prev.br_startoff = NULLFILEOFF;

0 commit comments

Comments
 (0)