From: liubaolin <liubaolin12138@163.com>
To: Ernest Borowski <ernest.borowski.git@proton.me>
Cc: Zhan Xusheng <zhanxusheng1024@gmail.com>,
linkinjeon@kernel.org, hyc.lee@gmail.com, ntfs@lists.linux.dev,
linux-kernel@vger.kernel.org, zhanxusheng@xiaomi.com,
stable@vger.kernel.org
Subject: Re: [PATCH v9] ntfs: trim the sizes after a short write
Date: Wed, 25 Nov 2026 11:25:09 +0800 [thread overview]
Message-ID: <4c9826bf-a0c1-4d6b-9b6b-a2e2bc88a8ce@163.com> (raw)
In-Reply-To: <179552385440.2421003.16146153406926819876.b4-reply@b4>
在 2026/11/24 20:37, Ernest Borowski 写道:
> 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
Hi Ernest,
Thanks for reproducing this and for the fix and thorough testing.
I agree that folding this into v10 makes sense.
Thanks,
Baolin
prev parent reply other threads:[~2026-11-25 3:25 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 2:59 Zhan Xusheng
2026-11-24 9:10 ` liubaolin
2026-11-24 12:37 ` Ernest Borowski
2026-11-25 3:25 ` liubaolin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4c9826bf-a0c1-4d6b-9b6b-a2e2bc88a8ce@163.com \
--to=liubaolin12138@163.com \
--cc=ernest.borowski.git@proton.me \
--cc=hyc.lee@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ntfs@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=zhanxusheng1024@gmail.com \
--cc=zhanxusheng@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®