Skip to content

Commit 8d7bba1

Browse files
Darrick J. Wongcmaiolino
authored andcommitted
xfs: fix various problems in xfs_atomic_write_cow_iomap_begin
I think there are several things wrong with this function: A) xfs_bmapi_write can return a much larger unwritten mapping than what the caller asked for. We convert part of that range to written, but return the entire written mapping to iomap even though that's inaccurate. B) The arguments to xfs_reflink_convert_cow_locked are wrong -- an unwritten mapping could be *smaller* than the write range (or even the hole range). In this case, we convert too much file range to written state because we then return a smaller mapping to iomap. C) It doesn't handle delalloc mappings. This I covered in the patch that I already sent to the list. D) Reassigning count_fsb to handle the hole means that if the second cmap lookup attempt succeeds (due to racing with someone else) we trim the mapping more than is strictly necessary. The changing meaning of count_fsb makes this harder to notice. E) The tracepoint is kinda wrong because @Length is mutated. That makes it harder to chase the data flows through this function because you can't just grep on the pos/bytecount strings. F) We don't actually check that the br_state = XFS_EXT_NORM assignment is accurate, i.e that the cow fork actually contains a written mapping for the range we're interested in G) Somewhat inadequate documentation of why we need to xfs_trim_extent so aggressively in this function. H) Not sure why xfs_iomap_end_fsb is used here, the vfs already clamped the write range to s_maxbytes. Fix these issues, and then the atomic writes regressions in generic/760, generic/617, generic/091, generic/263, and generic/521 all go away for me. Cc: stable@vger.kernel.org # v6.16 Fixes: bd1d2c2 ("xfs: add xfs_atomic_write_cow_iomap_begin()") Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: John Garry <john.g.garry@oracle.com> Signed-off-by: Carlos Maiolino <cem@kernel.org>
1 parent 8d54eac commit 8d7bba1

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

fs/xfs/xfs_iomap.c

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,29 @@ const struct iomap_ops xfs_zoned_direct_write_iomap_ops = {
10911091
};
10921092
#endif /* CONFIG_XFS_RT */
10931093

1094+
#ifdef DEBUG
1095+
static void
1096+
xfs_check_atomic_cow_conversion(
1097+
struct xfs_inode *ip,
1098+
xfs_fileoff_t offset_fsb,
1099+
xfs_filblks_t count_fsb,
1100+
const struct xfs_bmbt_irec *cmap)
1101+
{
1102+
struct xfs_iext_cursor icur;
1103+
struct xfs_bmbt_irec cmap2 = { };
1104+
1105+
if (xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap2))
1106+
xfs_trim_extent(&cmap2, offset_fsb, count_fsb);
1107+
1108+
ASSERT(cmap2.br_startoff == cmap->br_startoff);
1109+
ASSERT(cmap2.br_blockcount == cmap->br_blockcount);
1110+
ASSERT(cmap2.br_startblock == cmap->br_startblock);
1111+
ASSERT(cmap2.br_state == cmap->br_state);
1112+
}
1113+
#else
1114+
# define xfs_check_atomic_cow_conversion(...) ((void)0)
1115+
#endif
1116+
10941117
static int
10951118
xfs_atomic_write_cow_iomap_begin(
10961119
struct inode *inode,
@@ -1102,9 +1125,10 @@ xfs_atomic_write_cow_iomap_begin(
11021125
{
11031126
struct xfs_inode *ip = XFS_I(inode);
11041127
struct xfs_mount *mp = ip->i_mount;
1105-
const xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1106-
xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1107-
xfs_filblks_t count_fsb = end_fsb - offset_fsb;
1128+
const xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1129+
const xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1130+
const xfs_filblks_t count_fsb = end_fsb - offset_fsb;
1131+
xfs_filblks_t hole_count_fsb;
11081132
int nmaps = 1;
11091133
xfs_filblks_t resaligned;
11101134
struct xfs_bmbt_irec cmap;
@@ -1143,14 +1167,20 @@ xfs_atomic_write_cow_iomap_begin(
11431167
if (cmap.br_startoff <= offset_fsb) {
11441168
if (isnullstartblock(cmap.br_startblock))
11451169
goto convert_delay;
1170+
1171+
/*
1172+
* cmap could extend outside the write range due to previous
1173+
* speculative preallocations. We must trim cmap to the write
1174+
* range because the cow fork treats written mappings to mean
1175+
* "write in progress".
1176+
*/
11461177
xfs_trim_extent(&cmap, offset_fsb, count_fsb);
11471178
goto found;
11481179
}
11491180

1150-
end_fsb = cmap.br_startoff;
1151-
count_fsb = end_fsb - offset_fsb;
1181+
hole_count_fsb = cmap.br_startoff - offset_fsb;
11521182

1153-
resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
1183+
resaligned = xfs_aligned_fsb_count(offset_fsb, hole_count_fsb,
11541184
xfs_get_cowextsz_hint(ip));
11551185
xfs_iunlock(ip, XFS_ILOCK_EXCL);
11561186

@@ -1186,7 +1216,7 @@ xfs_atomic_write_cow_iomap_begin(
11861216
* atomic writes to that same range will be aligned (and don't require
11871217
* this COW-based method).
11881218
*/
1189-
error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
1219+
error = xfs_bmapi_write(tp, ip, offset_fsb, hole_count_fsb,
11901220
XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC |
11911221
XFS_BMAPI_EXTSZALIGN, 0, &cmap, &nmaps);
11921222
if (error) {
@@ -1199,17 +1229,26 @@ xfs_atomic_write_cow_iomap_begin(
11991229
if (error)
12001230
goto out_unlock;
12011231

1232+
/*
1233+
* cmap could map more blocks than the range we passed into bmapi_write
1234+
* because of EXTSZALIGN or adjacent pre-existing unwritten mappings
1235+
* that were merged. Trim cmap to the original write range so that we
1236+
* don't convert more than we were asked to do for this write.
1237+
*/
1238+
xfs_trim_extent(&cmap, offset_fsb, count_fsb);
1239+
12021240
found:
12031241
if (cmap.br_state != XFS_EXT_NORM) {
1204-
error = xfs_reflink_convert_cow_locked(ip, offset_fsb,
1205-
count_fsb);
1242+
error = xfs_reflink_convert_cow_locked(ip, cmap.br_startoff,
1243+
cmap.br_blockcount);
12061244
if (error)
12071245
goto out_unlock;
12081246
cmap.br_state = XFS_EXT_NORM;
1247+
xfs_check_atomic_cow_conversion(ip, offset_fsb, count_fsb,
1248+
&cmap);
12091249
}
12101250

1211-
length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
1212-
trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
1251+
trace_xfs_iomap_found(ip, offset, length, XFS_COW_FORK, &cmap);
12131252
seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
12141253
xfs_iunlock(ip, XFS_ILOCK_EXCL);
12151254
return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq);

0 commit comments

Comments
 (0)