[PATCH v3 7/8] exfat: complete direct writes synchronously when they update metadata

From: Chi Zhiling

Date: Thu Oct 15 2026 - 23:35:54 EST


From: Chi Zhiling <chizhiling@xxxxxxxxxx>

The inode metadata for a direct write is updated in ->end_io(). That
handler runs without holding i_rwsem whenever the I/O completes
asynchronously. As a result, the i_size update can race with other
operations.

Whenever a write extends beyond the current valid_size, that is,
whenever ->end_io() will have metadata to update, pass
IOMAP_DIO_FORCE_WAIT to iomap_dio_rw(). This guarantees that the
metadata update happens under the inode lock held across the write.

Note that this forces extending O_DIRECT writes, the common append case
for AIO and io_uring, to complete synchronously. Pure overwrites still
complete asynchronously because ->end_io() has nothing to publish for
them.

Fixes: 867b9c96dc83 ("exfat: add iomap direct I/O support")
Signed-off-by: Chi Zhiling <chizhiling@xxxxxxxxxx>
---
fs/exfat/file.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/exfat/file.c b/fs/exfat/file.c
index 23b8a36acaf7..27050f0358ee 100644
--- a/fs/exfat/file.c
+++ b/fs/exfat/file.c
@@ -857,10 +857,16 @@ static ssize_t exfat_fallback_buffered_write(struct kiocb *iocb,

static ssize_t exfat_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
+ struct inode *inode = file_inode(iocb->ki_filp);
+ loff_t end = iocb->ki_pos + iov_iter_count(from);
+ unsigned int dio_flags = 0;
ssize_t ret;

+ if (end > exfat_get_valid_size(EXFAT_I(inode)))
+ dio_flags |= IOMAP_DIO_FORCE_WAIT;
+
ret = iomap_dio_rw(iocb, from, &exfat_write_iomap_ops,
- &exfat_write_dio_ops, 0, NULL, 0);
+ &exfat_write_dio_ops, dio_flags, NULL, 0);
if (ret == -ENOTBLK)
ret = 0;
else if (ret < 0)
--
2.53.0