Re: [PATCH v8] ntfs: trim the sizes after a short write
From: Ernest Borowski
Date: Sun Nov 22 2026 - 13:21:42 EST
Hi Zhan,
I tested v8 on ntfs-next (f674e4b6c315) in QEMU, on a kernel with KASAN,
lockdep and UBSAN, at 512 B, 4 KiB, 16 KiB and 64 KiB clusters. It fixes
the file size after an ENOSPC short write, keeps FALLOC_FL_KEEP_SIZE
space, and fixes a short copy into allocated clusters past the initialized
size, which on ntfs-next leaves the uncopied part of the write reading the
old contents of those clusters.
One case loses data. It needs a writer killed during its copy, and a
shared writable mapping created while the write is stalled:
> + if (zend > zstart) {
> + err = iomap_zero_range(vi, zstart,
> + zend - zstart, NULL,
> + ntfs_seek_iomap_next,
> + &ntfs_iomap_folio_ops, NULL);
iomap_write_begin() returns -EINTR at once when fatal_signal_pending(), so
for a writer that got SIGKILL this zeroing always fails, and the fallback
then does
> + new_init_size = min3(ni->initialized_size,
> + zstart, new_data_size);
followed by truncate_pagecache_range(vi, new_init_size, ...) up to i_size.
Meanwhile ntfs_file_mmap_prepare() can have extended initialized_size to
i_size for a MAP_SHARED, PROT_WRITE mapping (it does not take the inode
lock, so it does not wait for the stalled writer), and the mapping can
have dirtied a page far past the write. That page is dropped from the
page cache and the initialized size is moved below it, so it reads back
as zeroes, also after fsync.
Reproducer: a file with old data in allocated clusters past its
initialized size (as Windows leaves it after SetFileValidData(); the test
sets the field on the image), pwrite() at offset 0 whose copy blocks on a
userfaultfd page after 16 KiB, mmap() of the file and a store at
i_size / 2, then SIGKILL to the writer. With v8 the 4 KiB page written
through the mapping is lost at every cluster size; without v8 the
mapping keeps its data but [16 KiB, 64 KiB) shows the old contents.
Zeroing from a worker when the caller has a fatal signal pending fixes
it. Something like this on top of v8:
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 25034735d2..a65892bb08 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -22,6 +22,7 @@
#include <linux/overflow.h>
#include <linux/security.h>
#include <linux/slab.h>
+#include <linux/workqueue.h>
#include <uapi/linux/ntfs.h>
#include "lcnalloc.h"
@@ -871,6 +872,52 @@ static loff_t ntfs_allocated_end_on_disk(struct ntfs_inode *ni)
return end;
}
+struct ntfs_zero_work {
+ struct work_struct work;
+ struct inode *inode;
+ loff_t pos;
+ loff_t len;
+ int err;
+};
+
+static void ntfs_zero_workfn(struct work_struct *work)
+{
+ struct ntfs_zero_work *zw = container_of(work, struct ntfs_zero_work,
+ work);
+
+ zw->err = iomap_zero_range(zw->inode, zw->pos, zw->len, NULL,
+ ntfs_seek_iomap_next, &ntfs_iomap_folio_ops,
+ NULL);
+}
+
+/*
+ * iomap_zero_range() gives up with -EINTR when the caller has a fatal signal
+ * pending, and a writer killed during its copy is one whose short write must
+ * still be zeroed: zero from a worker then, which has no signal to see. The
+ * caller holds the invalidate lock, which keeps page faults out meanwhile.
+ */
+static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos,
+ loff_t len)
+{
+ struct ntfs_zero_work zw = { .inode = inode, .pos = pos, .len = len };
+ int err;
+
+ if (!fatal_signal_pending(current)) {
+ err = iomap_zero_range(inode, pos, len, NULL,
+ ntfs_seek_iomap_next,
+ &ntfs_iomap_folio_ops, NULL);
+ /* the signal may also come while it zeroes */
+ if (err != -EINTR || !fatal_signal_pending(current))
+ return err;
+ }
+
+ INIT_WORK_ONSTACK(&zw.work, ntfs_zero_workfn);
+ queue_work(system_unbound_wq, &zw.work);
+ flush_work(&zw.work);
+ destroy_work_on_stack(&zw.work);
+ return zw.err;
+}
+
ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
@@ -1000,10 +1047,8 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
zend = min(zend, new_data_size);
if (zend > zstart) {
- err = iomap_zero_range(vi, zstart,
- zend - zstart, NULL,
- ntfs_seek_iomap_next,
- &ntfs_iomap_folio_ops, NULL);
+ err = ntfs_zero_range_unkillable(vi, zstart,
+ zend - zstart);
/* A direct read must not see the old blocks. */
if (!err && iocb->ki_flags & IOCB_DIRECT)
err = filemap_write_and_wait_range(
With it the test passes at all four cluster sizes, and the tests for the
ENOSPC short write size, FALLOC_FL_KEEP_SIZE and a writer killed inside
a cluster that was a hole still pass. The same five tests on the same
ntfs-next commit, each run on Ubuntu 26.04 and on Arch Linux:
ntfs-next: https://github.com/xerrni/linux-ntfs/actions/runs/45025520955
ntfs-next + v8: https://github.com/xerrni/linux-ntfs/actions/runs/45025527902
ntfs-next + v8 + the diff: https://github.com/xerrni/linux-ntfs/actions/runs/45025534974
ntfs-next + v8 + v8 + diff
killed_write_mmap FAIL FAIL pass
short_write_size FAIL pass pass
keep_size_short_write FAIL pass pass
killed_short_write pass pass pass
short_copy_stale FAIL FAIL FAIL
short_copy_stale fails at 4 KiB clusters in all three: a short copy into
a hole still shows old data there, with or without v8. That is a
separate bug; patch 7 of the series I just posted [1] fixes it. With v8,
the diff above and that series, all five tests pass:
https://github.com/xerrni/linux-ntfs/actions/runs/45029305849
Patch 7 has the same kind of helper in fs/ntfs/iomap.c, so whichever
lands second can share one copy.
To run the test on your own tree, locally (needs KVM, QEMU, what a kernel
build needs, gcc with a static libc, mkntfs from ntfs-3g and python3, but
not root; the README lists the packages):
git clone https://github.com/xerrni/linux-ntfs && cd linux-ntfs
vm/build_kernel.sh /path/to/linux ../build
tests/run_reproducers.sh ../build ../work killed_write_mmap.sh
It prints one line per test with the PASS and FAIL counts and the first
FAIL line; the guest logs are in ../work/killed_write_mmap/. Or with
nothing installed: fork the repository, enable Actions in the fork and
run its test-kernel workflow, which takes a kernel repository and branch,
patches (lore links work, b4 fetches them) and the tests to run, e.g.
gh workflow run test-kernel.yml -R <you>/linux-ntfs \
-f repo=<your tree> -f ref=<branch> -f tests=killed_write_mmap
The diff above is patches/v8-zero-for-a-killed-writer.diff in that
repository. If v8 is already queued, I can send it as a separate patch
on top instead.
Thanks,
Ernest
[1] https://lore.kernel.org/all/20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@xxxxxxxxx/