* [PATCH v8] ntfs: trim the sizes after a short write
@ 2026-11-20 2:35 Zhan Xusheng
2026-11-20 7:19 ` liubaolin
2026-11-22 18:21 ` Ernest Borowski
0 siblings, 2 replies; 4+ messages in thread
From: Zhan Xusheng @ 2026-11-20 2:35 UTC (permalink / raw)
To: linkinjeon, hyc.lee
Cc: liubaolin12138, ntfs, linux-kernel, stable, zhanxusheng
From: Zhan Xusheng <zhanxusheng@xiaomi.com>
ntfs_file_write_iter() moves the data size and, for a non-resident
uncompressed file, the initialized size to pos + count before the copy.
__ntfs_extend_initialized_size() only zeroes [old initialized size, pos),
so the range [pos, pos + count) is initialized only by the copy itself.
When iomap_file_buffered_write() copies less than count, it returns a
positive short count and the error rollback at out: does not run. The
file keeps the size pos + count, and [pos + ret, pos + count) reads back
whatever the freed clusters held before, also after a remount. A local
user can read the old contents of freed clusters this way.
Run the existing rollback for a short write as well, and roll the data
size back to the end of what was written, keeping the clusters that were
allocated on disk before the write, such as space reserved with
FALLOC_FL_KEEP_SIZE. Keep the initialized size, as a page fault through
a shared mapping may rely on it while the write is stalled on its
source, and zero what the write did not reach, up to the end of the
block holding pos + count. Only if that fails, take the initialized
size back to where the zeroing started, if it is above that.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
v8: when taking the data size back without truncating, mark the file
name dirty as ntfs_non_resident_attr_shrink() does. If the inode was
written while the write waited on its source, the size in the directory
index otherwise stays at the extended value (Baolin).
v7: when taking the data size back, keep the clusters allocated on disk
before the write instead of truncating, which freed space reserved
with FALLOC_FL_KEEP_SIZE; a failed write already did so before this
patch (Hyunchul). Holes and delayed allocations past the data size,
such as the preallocation of an earlier extending write, are freed as
before. Baolin's Reviewed-by on v6 is not carried over, as this
changes the data size rollback he reviewed.
v6: cap that fallback at the current initialized size, which a failed
extension before the write can leave below pos (Baolin).
v5 (Baolin):
- start the zeroing at pos, not at the old initialized size, which
overwrote data stored through a shared mapping below pos
- do the same for a direct write, instead of truncating the page cache
and taking the initialized size back
- if the zeroing fails, take the initialized size back to where it
started, instead of skipping both rollbacks
v4: for a buffered write, zero the unwritten range instead of
truncating the page cache and taking the initialized size back, which
dropped data stored through a shared mapping during the copy (Baolin).
v3: clear the cache up to EOF, not up to the extended initialized
size, which left the rest of the block holding pos + count (Baolin).
v2: also clear the cached tail of the last block, which v1 left in
place when the write stayed inside the old data size (Baolin).
v7: https://lore.kernel.org/all/20261119022715.3636699-1-zhanxusheng1024@gmail.com/
v6: https://lore.kernel.org/all/20261117082021.2947603-1-zhanxusheng1024@gmail.com/
v5: https://lore.kernel.org/all/20261116091121.2547497-1-zhanxusheng1024@gmail.com/
v4: https://lore.kernel.org/all/20261113050148.1466682-1-zhanxusheng1024@gmail.com/
v3: https://lore.kernel.org/all/20261112093233.1118469-1-zhanxusheng1024@gmail.com/
v2: https://lore.kernel.org/all/20261112030815.982026-1-zhanxusheng1024@gmail.com/
v1: https://lore.kernel.org/all/20261111093459.660484-1-zhanxusheng1024@gmail.com/
Tested in a VM on ntfs-next 4862a81576e3, with all free space filled
with a known pattern first. Short buffered writes of 4M, old data
readable after drop_caches and after remount, 3 runs each:
unfixed v8
fallocated, ends block-aligned 3137536 0
fallocated, ends inside a block 3141332 0
fallocated, unaligned start 3137536 0
same, pos + count in that page - 0
append 3137536 0
Marker stored through a shared mapping at 64K while pwritev() is
stalled on a userfaultfd source page, state after remount, with and
without fallocate():
unfixed v8
buffered 4K at 128K, EFAULT lost kept
O_DIRECT 12K at 0, EFAULT lost kept
buffered 12K at 0, short 8192 kept kept
1M reserved with FALLOC_FL_KEEP_SIZE, then a 12K write at 0, 512-byte
blocks after remount:
unfixed v8
short, 8192 written 2048 2048
failed, EFAULT 0 2048
A short write past 8K reserved this way frees what it allocated beyond
the 8K and keeps the 8K. A store into [pos + ret, pos + count) itself
during the write is zeroed, as that range belongs to the write. With
iomap_zero_range() failing (injected -ENOMEM), a 32K write to a 16K
file that returns 12288 leaves the size at 28K with no old data, also
after remount.
In the 8192 case above, with syncfs() while the write waits on its
source, the data size in the directory index after remount is 12288
with v7 and 8192 with v8.
fsx (buffered, no-mmap, O_DIRECT and mixed), compressed files,
fallocate, insert/collapse range and punch hole give the same results
with v8 as with v7.
---
fs/ntfs/attrib.c | 37 +++++++++++++++
fs/ntfs/attrib.h | 1 +
fs/ntfs/file.c | 121 +++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 151 insertions(+), 8 deletions(-)
diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
index 29eae1eb3d02..ccaeb484aaa0 100644
--- a/fs/ntfs/attrib.c
+++ b/fs/ntfs/attrib.c
@@ -2456,6 +2456,43 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
return err;
}
+/*
+ * ntfs_attr_set_data_size - set the data size of a non-resident attribute
+ * without changing its allocation
+ *
+ * The caller holds mrec_lock. @new_size must not be below the initialized
+ * size or above the allocated size.
+ */
+int ntfs_attr_set_data_size(struct ntfs_inode *ni, loff_t new_size)
+{
+ struct ntfs_attr_search_ctx *ctx;
+ struct ntfs_inode *base_ni = ntfs_base_inode(ni);
+ int err = 0;
+
+ if (!NInoNonResident(ni) || new_size < ni->initialized_size ||
+ new_size > ni->allocated_size)
+ return -EINVAL;
+
+ ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
+ if (!ctx)
+ return -ENOMEM;
+
+ err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
+ CASE_SENSITIVE, 0, NULL, 0, ctx);
+ if (err)
+ goto out_ctx;
+
+ ctx->attr->data.non_resident.data_size = cpu_to_le64(new_size);
+ ni->data_size = new_size;
+ /* Update data size in the index. */
+ if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
+ NInoSetFileNameDirty(ni);
+ mark_mft_record_dirty(ctx->ntfs_ino);
+out_ctx:
+ ntfs_attr_put_search_ctx(ctx);
+ return err;
+}
+
/*
* ntfs_make_room_for_attr - make room for an attribute inside an mft record
* @m: mft record
diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
index bee91c9f6f81..edb62826a6ab 100644
--- a/fs/ntfs/attrib.h
+++ b/fs/ntfs/attrib.h
@@ -102,6 +102,7 @@ int ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size);
int ntfs_attr_set(struct ntfs_inode *ni, const s64 ofs, const s64 cnt,
const u8 val);
int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size);
+int ntfs_attr_set_data_size(struct ntfs_inode *ni, loff_t new_size);
int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
__le16 *name, u32 name_len);
void ntfs_attr_close(struct ntfs_inode *n);
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index a52483d16c6e..25034735d249 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -843,6 +843,34 @@ static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
return err;
}
+/*
+ * End of the last run of clusters allocated on disk. Holes and delayed
+ * allocations past it, such as the preallocation of an extending write,
+ * do not count. Falls back to the allocated size if the runlist is not
+ * fully mapped.
+ */
+static loff_t ntfs_allocated_end_on_disk(struct ntfs_inode *ni)
+{
+ loff_t end = ni->allocated_size;
+ ssize_t i;
+
+ down_read(&ni->runlist.lock);
+ if (NInoFullyMapped(ni) && ni->runlist.rl) {
+ end = 0;
+ for (i = ni->runlist.count - 1; i >= 0; i--) {
+ struct runlist_element *rl = &ni->runlist.rl[i];
+
+ if (rl->length && rl->lcn >= 0) {
+ end = ntfs_cluster_to_bytes(ni->vol,
+ rl->vcn + rl->length);
+ break;
+ }
+ }
+ }
+ up_read(&ni->runlist.lock);
+ return end;
+}
+
ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
@@ -853,7 +881,7 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
ssize_t count;
loff_t pos, end;
int err;
- loff_t old_data_size, old_init_size;
+ loff_t old_data_size, old_init_size, old_alloc_size;
if (NVolShutdown(vol))
return -EIO;
@@ -901,6 +929,10 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
old_data_size = ni->data_size;
old_init_size = ni->initialized_size;
+ old_alloc_size = ni->allocated_size;
+ if (NInoNonResident(ni) && !NInoCompressed(ni) &&
+ old_alloc_size > round_up(old_data_size, vol->cluster_size))
+ old_alloc_size = ntfs_allocated_end_on_disk(ni);
if (end > old_data_size) {
ret = ntfs_expand_for_write(ni, end);
@@ -928,10 +960,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
ret = iomap_file_buffered_write(iocb, from, ntfs_write_iomap_next,
&ntfs_iomap_folio_ops, NULL);
out:
- if (ret < 0 && ret != -EIOCBQUEUED) {
+ /*
+ * Both sizes were moved to end before the copy, so a short write,
+ * like a failed one, must take back what it did not write.
+ */
+ if (ret < count && ret != -EIOCBQUEUED) {
+ loff_t new_init_size = old_init_size;
+ loff_t new_data_size = old_data_size;
int init_err = 0;
int data_err = 0;
+ if (ret > 0) {
+ new_init_size = max(old_init_size, pos + ret);
+ new_data_size = max(old_data_size, pos + ret);
+ }
+
/*
* Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs()
* expects the invalidate lock held across the size
@@ -939,14 +982,59 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
*/
filemap_invalidate_lock(vi->i_mapping);
+ /*
+ * A page fault through a shared mapping may have used the
+ * extended initialized size while the write was stalled on
+ * its source, so keep that size and zero what the write did
+ * not reach instead. [old_init_size, pos) was zeroed before
+ * the write. Past end, zero the rest of the block that a
+ * buffered copy may have read from disk, unless such a fault
+ * already moved the initialized size past end and zeroed it.
+ */
+ if (NInoNonResident(ni) && !NInoCompressed(ni) &&
+ ni->initialized_size > new_init_size) {
+ loff_t zstart = max(pos, new_init_size);
+ loff_t zend = ni->initialized_size > end ? end :
+ round_up(end, i_blocksize(vi));
+ int err = 0;
+
+ 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);
+ /* A direct read must not see the old blocks. */
+ if (!err && iocb->ki_flags & IOCB_DIRECT)
+ err = filemap_write_and_wait_range(
+ vi->i_mapping, zstart, zend - 1);
+ }
+ if (!err)
+ new_init_size = min(ni->initialized_size,
+ new_data_size);
+ else
+ new_init_size = min3(ni->initialized_size,
+ zstart, new_data_size);
+ }
+
/* ntfs_attr_set_initialized_size() rejects a resident one. */
- if (ni->initialized_size != old_init_size &&
+ if (ni->initialized_size != new_init_size &&
NInoNonResident(ni)) {
struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
+ /*
+ * Only reached when the zeroing above failed or did
+ * not apply. Drop what was cached past the new
+ * initialized size, as __ntfs_extend_initialized_size()
+ * does.
+ */
+ if (ni->initialized_size > new_init_size)
+ truncate_pagecache_range(vi, new_init_size,
+ max_t(loff_t, i_size_read(vi),
+ ni->initialized_size) - 1);
mutex_lock(&mrec_ni->mrec_lock);
init_err = ntfs_attr_set_initialized_size(ni,
- old_init_size);
+ new_init_size);
mutex_unlock(&mrec_ni->mrec_lock);
}
/*
@@ -955,13 +1043,30 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
* the uninitialized tail, so skip it unless the rollback
* above restored that size.
*/
- if (ni->data_size != old_data_size && !init_err) {
+ if (ni->data_size != new_data_size && !init_err) {
struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
- truncate_setsize(vi, old_data_size);
+ truncate_setsize(vi, new_data_size);
mutex_lock(&mrec_ni->mrec_lock);
- data_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
- old_data_size);
+ if (NInoNonResident(ni) && !NInoCompressed(ni)) {
+ /*
+ * Only free what the write allocated, not
+ * clusters that were there before, such as
+ * space reserved with FALLOC_FL_KEEP_SIZE.
+ */
+ loff_t keep = max(new_data_size,
+ old_alloc_size);
+
+ if (ni->allocated_size > keep)
+ data_err = __ntfs_attr_truncate_vfs(ni,
+ keep, keep);
+ if (!data_err && ni->data_size != new_data_size)
+ data_err = ntfs_attr_set_data_size(ni,
+ new_data_size);
+ } else {
+ data_err = __ntfs_attr_truncate_vfs(ni,
+ new_data_size, new_data_size);
+ }
mutex_unlock(&mrec_ni->mrec_lock);
/* Like ntfs_setattr_size(): stay on the durable size. */
if (data_err)
base-commit: 4862a81576e3b20ba40cb94a38c5df0450047ffe
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v8] ntfs: trim the sizes after a short write
2026-11-20 2:35 [PATCH v8] ntfs: trim the sizes after a short write Zhan Xusheng
@ 2026-11-20 7:19 ` liubaolin
2026-11-22 18:21 ` Ernest Borowski
1 sibling, 0 replies; 4+ messages in thread
From: liubaolin @ 2026-11-20 7:19 UTC (permalink / raw)
To: Zhan Xusheng, linkinjeon, hyc.lee; +Cc: ntfs, linux-kernel, stable, zhanxusheng
在 2026/11/20 10:35, Zhan Xusheng 写道:
> From: Zhan Xusheng <zhanxusheng@xiaomi.com>
>
> ntfs_file_write_iter() moves the data size and, for a non-resident
> uncompressed file, the initialized size to pos + count before the copy.
> __ntfs_extend_initialized_size() only zeroes [old initialized size, pos),
> so the range [pos, pos + count) is initialized only by the copy itself.
> When iomap_file_buffered_write() copies less than count, it returns a
> positive short count and the error rollback at out: does not run. The
> file keeps the size pos + count, and [pos + ret, pos + count) reads back
> whatever the freed clusters held before, also after a remount. A local
> user can read the old contents of freed clusters this way.
>
> Run the existing rollback for a short write as well, and roll the data
> size back to the end of what was written, keeping the clusters that were
> allocated on disk before the write, such as space reserved with
> FALLOC_FL_KEEP_SIZE. Keep the initialized size, as a page fault through
> a shared mapping may rely on it while the write is stalled on its
> source, and zero what the write did not reach, up to the end of the
> block holding pos + count. Only if that fails, take the initialized
> size back to where the zeroing started, if it is above that.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
> ---
> v8: when taking the data size back without truncating, mark the file
> name dirty as ntfs_non_resident_attr_shrink() does. If the inode was
> written while the write waited on its source, the size in the directory
> index otherwise stays at the extended value (Baolin).
> v7: when taking the data size back, keep the clusters allocated on disk
> before the write instead of truncating, which freed space reserved
> with FALLOC_FL_KEEP_SIZE; a failed write already did so before this
> patch (Hyunchul). Holes and delayed allocations past the data size,
> such as the preallocation of an earlier extending write, are freed as
> before. Baolin's Reviewed-by on v6 is not carried over, as this
> changes the data size rollback he reviewed.
> v6: cap that fallback at the current initialized size, which a failed
> extension before the write can leave below pos (Baolin).
> v5 (Baolin):
> - start the zeroing at pos, not at the old initialized size, which
> overwrote data stored through a shared mapping below pos
> - do the same for a direct write, instead of truncating the page cache
> and taking the initialized size back
> - if the zeroing fails, take the initialized size back to where it
> started, instead of skipping both rollbacks
> v4: for a buffered write, zero the unwritten range instead of
> truncating the page cache and taking the initialized size back, which
> dropped data stored through a shared mapping during the copy (Baolin).
> v3: clear the cache up to EOF, not up to the extended initialized
> size, which left the rest of the block holding pos + count (Baolin).
> v2: also clear the cached tail of the last block, which v1 left in
> place when the write stayed inside the old data size (Baolin).
>
> v7: https://lore.kernel.org/all/20261119022715.3636699-1-zhanxusheng1024@gmail.com/
> v6: https://lore.kernel.org/all/20261117082021.2947603-1-zhanxusheng1024@gmail.com/
> v5: https://lore.kernel.org/all/20261116091121.2547497-1-zhanxusheng1024@gmail.com/
> v4: https://lore.kernel.org/all/20261113050148.1466682-1-zhanxusheng1024@gmail.com/
> v3: https://lore.kernel.org/all/20261112093233.1118469-1-zhanxusheng1024@gmail.com/
> v2: https://lore.kernel.org/all/20261112030815.982026-1-zhanxusheng1024@gmail.com/
> v1: https://lore.kernel.org/all/20261111093459.660484-1-zhanxusheng1024@gmail.com/
>
> Tested in a VM on ntfs-next 4862a81576e3, with all free space filled
> with a known pattern first. Short buffered writes of 4M, old data
> readable after drop_caches and after remount, 3 runs each:
>
> unfixed v8
> fallocated, ends block-aligned 3137536 0
> fallocated, ends inside a block 3141332 0
> fallocated, unaligned start 3137536 0
> same, pos + count in that page - 0
> append 3137536 0
>
> Marker stored through a shared mapping at 64K while pwritev() is
> stalled on a userfaultfd source page, state after remount, with and
> without fallocate():
>
> unfixed v8
> buffered 4K at 128K, EFAULT lost kept
> O_DIRECT 12K at 0, EFAULT lost kept
> buffered 12K at 0, short 8192 kept kept
>
> 1M reserved with FALLOC_FL_KEEP_SIZE, then a 12K write at 0, 512-byte
> blocks after remount:
>
> unfixed v8
> short, 8192 written 2048 2048
> failed, EFAULT 0 2048
>
> A short write past 8K reserved this way frees what it allocated beyond
> the 8K and keeps the 8K. A store into [pos + ret, pos + count) itself
> during the write is zeroed, as that range belongs to the write. With
> iomap_zero_range() failing (injected -ENOMEM), a 32K write to a 16K
> file that returns 12288 leaves the size at 28K with no old data, also
> after remount.
>
> In the 8192 case above, with syncfs() while the write waits on its
> source, the data size in the directory index after remount is 12288
> with v7 and 8192 with v8.
>
> fsx (buffered, no-mmap, O_DIRECT and mixed), compressed files,
> fallocate, insert/collapse range and punch hole give the same results
> with v8 as with v7.
> ---
> fs/ntfs/attrib.c | 37 +++++++++++++++
> fs/ntfs/attrib.h | 1 +
> fs/ntfs/file.c | 121 +++++++++++++++++++++++++++++++++++++++++++----
> 3 files changed, 151 insertions(+), 8 deletions(-)
>
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index 29eae1eb3d02..ccaeb484aaa0 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2456,6 +2456,43 @@ int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size)
> return err;
> }
>
> +/*
> + * ntfs_attr_set_data_size - set the data size of a non-resident attribute
> + * without changing its allocation
> + *
> + * The caller holds mrec_lock. @new_size must not be below the initialized
> + * size or above the allocated size.
> + */
> +int ntfs_attr_set_data_size(struct ntfs_inode *ni, loff_t new_size)
> +{
> + struct ntfs_attr_search_ctx *ctx;
> + struct ntfs_inode *base_ni = ntfs_base_inode(ni);
> + int err = 0;
> +
> + if (!NInoNonResident(ni) || new_size < ni->initialized_size ||
> + new_size > ni->allocated_size)
> + return -EINVAL;
> +
> + ctx = ntfs_attr_get_search_ctx(base_ni, NULL);
> + if (!ctx)
> + return -ENOMEM;
> +
> + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
> + CASE_SENSITIVE, 0, NULL, 0, ctx);
> + if (err)
> + goto out_ctx;
> +
> + ctx->attr->data.non_resident.data_size = cpu_to_le64(new_size);
> + ni->data_size = new_size;
> + /* Update data size in the index. */
> + if (ni->type == AT_DATA && ni->name == AT_UNNAMED)
> + NInoSetFileNameDirty(ni);
> + mark_mft_record_dirty(ctx->ntfs_ino);
> +out_ctx:
> + ntfs_attr_put_search_ctx(ctx);
> + return err;
> +}
> +
> /*
> * ntfs_make_room_for_attr - make room for an attribute inside an mft record
> * @m: mft record
> diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h
> index bee91c9f6f81..edb62826a6ab 100644
> --- a/fs/ntfs/attrib.h
> +++ b/fs/ntfs/attrib.h
> @@ -102,6 +102,7 @@ int ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size);
> int ntfs_attr_set(struct ntfs_inode *ni, const s64 ofs, const s64 cnt,
> const u8 val);
> int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size);
> +int ntfs_attr_set_data_size(struct ntfs_inode *ni, loff_t new_size);
> int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type,
> __le16 *name, u32 name_len);
> void ntfs_attr_close(struct ntfs_inode *n);
> diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
> index a52483d16c6e..25034735d249 100644
> --- a/fs/ntfs/file.c
> +++ b/fs/ntfs/file.c
> @@ -843,6 +843,34 @@ static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end)
> return err;
> }
>
> +/*
> + * End of the last run of clusters allocated on disk. Holes and delayed
> + * allocations past it, such as the preallocation of an extending write,
> + * do not count. Falls back to the allocated size if the runlist is not
> + * fully mapped.
> + */
> +static loff_t ntfs_allocated_end_on_disk(struct ntfs_inode *ni)
> +{
> + loff_t end = ni->allocated_size;
> + ssize_t i;
> +
> + down_read(&ni->runlist.lock);
> + if (NInoFullyMapped(ni) && ni->runlist.rl) {
> + end = 0;
> + for (i = ni->runlist.count - 1; i >= 0; i--) {
> + struct runlist_element *rl = &ni->runlist.rl[i];
> +
> + if (rl->length && rl->lcn >= 0) {
> + end = ntfs_cluster_to_bytes(ni->vol,
> + rl->vcn + rl->length);
> + break;
> + }
> + }
> + }
> + up_read(&ni->runlist.lock);
> + return end;
> +}
> +
> ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> {
> struct file *file = iocb->ki_filp;
> @@ -853,7 +881,7 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> ssize_t count;
> loff_t pos, end;
> int err;
> - loff_t old_data_size, old_init_size;
> + loff_t old_data_size, old_init_size, old_alloc_size;
>
> if (NVolShutdown(vol))
> return -EIO;
> @@ -901,6 +929,10 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
>
> old_data_size = ni->data_size;
> old_init_size = ni->initialized_size;
> + old_alloc_size = ni->allocated_size;
> + if (NInoNonResident(ni) && !NInoCompressed(ni) &&
> + old_alloc_size > round_up(old_data_size, vol->cluster_size))
> + old_alloc_size = ntfs_allocated_end_on_disk(ni);
>
> if (end > old_data_size) {
> ret = ntfs_expand_for_write(ni, end);
> @@ -928,10 +960,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> ret = iomap_file_buffered_write(iocb, from, ntfs_write_iomap_next,
> &ntfs_iomap_folio_ops, NULL);
> out:
> - if (ret < 0 && ret != -EIOCBQUEUED) {
> + /*
> + * Both sizes were moved to end before the copy, so a short write,
> + * like a failed one, must take back what it did not write.
> + */
> + if (ret < count && ret != -EIOCBQUEUED) {
> + loff_t new_init_size = old_init_size;
> + loff_t new_data_size = old_data_size;
> int init_err = 0;
> int data_err = 0;
>
> + if (ret > 0) {
> + new_init_size = max(old_init_size, pos + ret);
> + new_data_size = max(old_data_size, pos + ret);
> + }
> +
> /*
> * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs()
> * expects the invalidate lock held across the size
> @@ -939,14 +982,59 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> */
> filemap_invalidate_lock(vi->i_mapping);
>
> + /*
> + * A page fault through a shared mapping may have used the
> + * extended initialized size while the write was stalled on
> + * its source, so keep that size and zero what the write did
> + * not reach instead. [old_init_size, pos) was zeroed before
> + * the write. Past end, zero the rest of the block that a
> + * buffered copy may have read from disk, unless such a fault
> + * already moved the initialized size past end and zeroed it.
> + */
> + if (NInoNonResident(ni) && !NInoCompressed(ni) &&
> + ni->initialized_size > new_init_size) {
> + loff_t zstart = max(pos, new_init_size);
> + loff_t zend = ni->initialized_size > end ? end :
> + round_up(end, i_blocksize(vi));
> + int err = 0;
> +
> + 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);
> + /* A direct read must not see the old blocks. */
> + if (!err && iocb->ki_flags & IOCB_DIRECT)
> + err = filemap_write_and_wait_range(
> + vi->i_mapping, zstart, zend - 1);
> + }
> + if (!err)
> + new_init_size = min(ni->initialized_size,
> + new_data_size);
> + else
> + new_init_size = min3(ni->initialized_size,
> + zstart, new_data_size);
> + }
> +
> /* ntfs_attr_set_initialized_size() rejects a resident one. */
> - if (ni->initialized_size != old_init_size &&
> + if (ni->initialized_size != new_init_size &&
> NInoNonResident(ni)) {
> struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
>
> + /*
> + * Only reached when the zeroing above failed or did
> + * not apply. Drop what was cached past the new
> + * initialized size, as __ntfs_extend_initialized_size()
> + * does.
> + */
> + if (ni->initialized_size > new_init_size)
> + truncate_pagecache_range(vi, new_init_size,
> + max_t(loff_t, i_size_read(vi),
> + ni->initialized_size) - 1);
> mutex_lock(&mrec_ni->mrec_lock);
> init_err = ntfs_attr_set_initialized_size(ni,
> - old_init_size);
> + new_init_size);
> mutex_unlock(&mrec_ni->mrec_lock);
> }
> /*
> @@ -955,13 +1043,30 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> * the uninitialized tail, so skip it unless the rollback
> * above restored that size.
> */
> - if (ni->data_size != old_data_size && !init_err) {
> + if (ni->data_size != new_data_size && !init_err) {
> struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
>
> - truncate_setsize(vi, old_data_size);
> + truncate_setsize(vi, new_data_size);
> mutex_lock(&mrec_ni->mrec_lock);
> - data_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
> - old_data_size);
> + if (NInoNonResident(ni) && !NInoCompressed(ni)) {
> + /*
> + * Only free what the write allocated, not
> + * clusters that were there before, such as
> + * space reserved with FALLOC_FL_KEEP_SIZE.
> + */
> + loff_t keep = max(new_data_size,
> + old_alloc_size);
> +
> + if (ni->allocated_size > keep)
> + data_err = __ntfs_attr_truncate_vfs(ni,
> + keep, keep);
> + if (!data_err && ni->data_size != new_data_size)
> + data_err = ntfs_attr_set_data_size(ni,
> + new_data_size);
> + } else {
> + data_err = __ntfs_attr_truncate_vfs(ni,
> + new_data_size, new_data_size);
> + }
> mutex_unlock(&mrec_ni->mrec_lock);
> /* Like ntfs_setattr_size(): stay on the durable size. */
> if (data_err)
>
> base-commit: 4862a81576e3b20ba40cb94a38c5df0450047ffe
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v8] ntfs: trim the sizes after a short write
2026-11-20 2:35 [PATCH v8] ntfs: trim the sizes after a short write Zhan Xusheng
2026-11-20 7:19 ` liubaolin
@ 2026-11-22 18:21 ` Ernest Borowski
2026-11-23 3:09 ` Zhan Xusheng
1 sibling, 1 reply; 4+ messages in thread
From: Ernest Borowski @ 2026-11-22 18:21 UTC (permalink / raw)
To: Zhan Xusheng
Cc: linkinjeon, hyc.lee, liubaolin12138, ntfs, linux-kernel, stable,
zhanxusheng
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@proton.me/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v8] ntfs: trim the sizes after a short write
2026-11-22 18:21 ` Ernest Borowski
@ 2026-11-23 3:09 ` Zhan Xusheng
0 siblings, 0 replies; 4+ messages in thread
From: Zhan Xusheng @ 2026-11-23 3:09 UTC (permalink / raw)
To: ernest.borowski.git
Cc: linkinjeon, hyc.lee, liubaolin12138, ntfs, linux-kernel, zhanxusheng
Hi Ernest,
On Sun, Nov 22, 2026 at 06:21:27PM +0000, Ernest Borowski wrote:
> Zeroing from a worker when the caller has a fatal signal pending fixes
> it. Something like this on top of v8:
Thanks. I reproduced it on ntfs-next f674e4b6c315 at 4K and 64K
clusters: with v8 the page stored through the mapping reads back as
zeroes, with your diff it is kept, and v8's other tests give the same
results.
I'd like to fold the diff into v9 as it is. Can I add your
Co-developed-by and Signed-off-by for it?
> Patch 7 has the same kind of helper in fs/ntfs/iomap.c, so whichever
> lands second can share one copy.
They zero through different paths: the one in patch 7 calls
ntfs_zero_range(), whose ntfs_iomap_valid() only accepts delalloc
mappings, while v8 zeroes allocated clusters past the initialized
size. A shared helper would need the zeroing function passed in.
Thanks,
Zhan Xusheng
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 3:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 2:35 [PATCH v8] ntfs: trim the sizes after a short write Zhan Xusheng
2026-11-20 7:19 ` liubaolin
2026-11-22 18:21 ` Ernest Borowski
2026-11-23 3:09 ` Zhan Xusheng
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®