Re: [PATCH v9] ntfs: trim the sizes after a short write
From: Ernest Borowski
Date: Tue Nov 24 2026 - 07:37:55 EST
Hi Baolin, Xusheng,
On Tue, Nov 24, 2026 at 05:10:32PM +0800, liubaolin wrote:
> Could an async direct write still miss this cleanup if it fails after
> submitting some BIOs?
Yes. I tested a 6 MiB O_DIRECT write at offset 0 into a file with
6 MiB reserved by FALLOC_FL_KEEP_SIZE (volume free space filled with
0xee, source buffer unmapped after 4 MiB). Through io_submit(), the
write fails with -EFAULT, the file stays at 6 MiB, and the last 2 MiB
read back as 0xee, even after a remount. The same write through
pwritev() returns -EFAULT to ntfs_file_write_iter(), where the rollback
that predates v9 resets the size to 0.
> Would using IOMAP_DIO_FORCE_WAIT for writes that advance
> initialized_size let us handle this through the existing rollback?
Yes. With the forced wait, the async write reaches the rollback and
behaves like the sync one. ext4 also waits for extending direct writes
and fails non-blocking ones (IOCB_NOWAIT, e.g. from io_uring) with
-EAGAIN before changing anything, so the caller retries them in
blocking mode. ntfs needs the same -EAGAIN return so that the forced
wait does not block an io_uring submitter. Resident attributes need it
too, since the write makes them non-resident first. On top of v9:
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index f3bdc54595..86297a1633 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -762,12 +762,13 @@ static const struct iomap_dio_ops ntfs_write_dio_ops = {
.end_io = ntfs_file_write_dio_end_io,
};
-static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
+static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
+ unsigned int dio_flags)
{
ssize_t ret;
ret = iomap_dio_rw(iocb, from, ntfs_dio_iomap_next,
- &ntfs_write_dio_ops, 0, NULL, 0);
+ &ntfs_write_dio_ops, dio_flags, NULL, 0);
if (ret == -ENOTBLK)
ret = 0;
else if (ret < 0)
@@ -955,6 +956,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (ret <= 0)
goto out_lock;
+ /*
+ * A direct write that moves the initialized size waits for its I/O
+ * (see ntfs_dio_write_iter() below), which a non-blocking one must
+ * not do, and a resident attribute is written through the page cache
+ * or made non-resident first. Have the caller retry such a write
+ * blocking before anything is changed, as ext4 does for extending
+ * direct writes.
+ */
+ if (iocb->ki_flags & IOCB_NOWAIT && iocb->ki_flags & IOCB_DIRECT &&
+ (!NInoNonResident(ni) ||
+ iocb->ki_pos + ret > ni->initialized_size)) {
+ ret = -EAGAIN;
+ goto out_lock;
+ }
+
err = ntfs_file_modified(iocb);
if (err) {
ret = err;
@@ -1001,8 +1017,17 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
goto out;
}
+ /*
+ * The sizes were moved to end before the I/O, and only the rollback
+ * at out: takes them back if the write fails. An asynchronous write
+ * returns -EIOCBQUEUED once it has submitted bios and reports a later
+ * failure, such as a fault on its source or an I/O error, only on
+ * completion, which would skip the rollback. Wait for such a write
+ * to complete.
+ */
if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT)
- ret = ntfs_dio_write_iter(iocb, from);
+ ret = ntfs_dio_write_iter(iocb, from, end > old_init_size ?
+ IOMAP_DIO_FORCE_WAIT : 0);
else
ret = iomap_file_buffered_write(iocb, from, ntfs_write_iomap_next,
&ntfs_iomap_folio_ops, NULL);
Results on ntfs-next f674e4b6c315 with 512 B, 4 KiB, 16 KiB and 64 KiB
clusters, on Ubuntu 26.04 and Arch Linux. The async and sync cases
write three files per cluster size; the RWF_NOWAIT case is a fully
mapped 4 MiB pwritev2() into a zero-size file, either reserved or new
and empty ("-": not run):
ntfs-next + v9 + v9 + diff
async write, old data readable 24 of 24 24 of 24 0 of 24
sync write, old data readable 0 of 24 0 of 24 0 of 24
RWF_NOWAIT, reserved file, -EAGAIN - 0 of 8 8 of 8
RWF_NOWAIT, new empty file, -EAGAIN - 0 of 8 8 of 8
killed_write_mmap FAIL pass pass
short_write_size FAIL pass pass
keep_size_short_write FAIL pass pass
killed_short_write pass pass pass
ntfs-next: https://github.com/xerrni/linux-ntfs/actions/runs/45384651132
ntfs-next + v9: https://github.com/xerrni/linux-ntfs/actions/runs/45387762427
ntfs-next + v9 + diff: https://github.com/xerrni/linux-ntfs/actions/runs/45387767567
The test is tests/aio_dio_partial.sh (with tests/aio_dio_partial.c) and
the diff is patches/v9-dio-force-wait.diff in
https://github.com/xerrni/linux-ntfs.
The bug predates v9, but the fix is small and only extends this
patch's rollback to async writes, so I think it belongs in this patch
rather than in a follow-up. Xusheng, could you fold it into v10? My
Co-developed-by and Signed-off-by on v9 cover it too.
Thanks,
Ernest