* Re: [PATCH v12] ntfs: trim the sizes after a short write
2026-11-27 3:10 [PATCH v12] ntfs: trim the sizes after a short write Zhan Xusheng
@ 2026-11-27 3:58 ` liubaolin
2026-12-01 0:35 ` Hyunchul Lee
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: liubaolin @ 2026-11-27 3:58 UTC (permalink / raw)
To: Zhan Xusheng, linkinjeon, hyc.lee
Cc: ernest.borowski.git, ntfs, linux-kernel, zhanxusheng, stable
在 2026/11/27 11:10, 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. A writer
> killed during the copy also leaves a short write, but iomap_zero_range()
> then fails with -EINTR, so zero from a worker when a fatal signal is
> pending.
>
> An asynchronous direct write that fails after submitting bios returns
> -EIOCBQUEUED, and the rollback does not run for it either. Wait for a
> direct write that moves the initialized size, and fail a non-blocking
> one with -EAGAIN so that it is retried blocking, as ext4 does.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Co-developed-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
> ---
> v12: move the rollback into ntfs_write_rollback() (Hyunchul). No
> functional change.
> v11: ntfs_attr_get_search_ctx() returns an ERR_PTR() on failure, so
> check it with IS_ERR() in ntfs_attr_set_data_size() (Baolin).
> v10: an asynchronous direct write that fails after submitting bios
> returns -EIOCBQUEUED, so the rollback did not run and the file kept the
> size of the whole write. Wait for a direct write that moves the
> initialized size, and return -EAGAIN for a non-blocking one (Baolin,
> Ernest).
> v9: for a writer killed during the copy, iomap_zero_range() fails with
> -EINTR, and the fallback then took the initialized size back and lost
> what was stored meanwhile through a shared mapping. Zero from a worker
> when a fatal signal is pending (Ernest). Baolin's Reviewed-by on v8 is
> not carried over, as this changes the code he reviewed. After patch 6
> of Ernest's delalloc series, ntfs_zero_range() makes the same call as
> the new helper, so whichever of the two lands second can reuse the
> other.
> 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).
>
> v11: https://lore.kernel.org/all/20261126015731.3254689-1-zhanxusheng1024@gmail.com/
> v10: https://lore.kernel.org/all/20261125051247.2880928-1-zhanxusheng1024@gmail.com/
> v9: https://lore.kernel.org/all/20261124025932.2360848-1-zhanxusheng1024@gmail.com/
> v8: https://lore.kernel.org/all/20261120023504.4134966-1-zhanxusheng1024@gmail.com/
> 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/
>
> v8 was 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.
>
> A 32K pwritev() at 0 to a file with 1M fallocated, its copy stalled
> on a userfaultfd source page after 16K. A marker is stored at 512K
> through a shared mapping, then the writer gets SIGKILL. State after
> remount:
>
> unfixed v8 v9
> marker, 4K clusters kept lost kept
> marker, 64K clusters kept lost kept
> old data in [16K, 64K), 64K clusters 16384 0 0
>
> The tests above give the same results with v9 as with v8, on
> ntfs-next f674e4b6c315. No lockdep or KASAN reports.
>
> A 6M O_DIRECT write at 0 into a file with 6M reserved by
> FALLOC_FL_KEEP_SIZE, its source unmapped after 4M, on a volume whose
> free space held a known pattern, 3 files each, after remount:
>
> ntfs-next v9 v10
> io_submit(), 4K clusters size 6M, size 6M, size 0
> old data in no old data
> 1 of 3
> io_submit(), 64K clusters size 6M, size 6M, size 0
> 2M old data 2M old data
> pwritev() size 0 size 0 size 0
> RWF_NOWAIT 4M, reserved or written written -EAGAIN,
> empty file size 0
>
> The other tests above give the same results with v10 as with v9. No
> lockdep or KASAN reports.
> ---
>
> The short write, killed and stalled writer, KEEP_SIZE and partial
> O_DIRECT tests above give the same results with v11 as with v10, and
> all of the tests above with v12 as with v11, compressed files included.
>
> fs/ntfs/attrib.c | 37 ++++++
> fs/ntfs/attrib.h | 1 +
> fs/ntfs/file.c | 290 ++++++++++++++++++++++++++++++++++++++---------
> 3 files changed, 275 insertions(+), 53 deletions(-)
>
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index 6ee6b6bcac1d..c192333be83c 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2457,6 +2457,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 (IS_ERR(ctx))
> + return PTR_ERR(ctx);
> +
> + 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 9d6bf0320219..0415f817dcf5 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 7d93a520566e..39b49d36d069 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"
> @@ -761,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)
> @@ -843,6 +845,206 @@ 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;
> +}
> +
> +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;
> +}
> +
> +/*
> + * Both sizes were moved to the end of the write before the copy, so a short
> + * write, like a failed one, must take back what it did not write. @written
> + * is what the write returned, a negative error if it wrote nothing. The
> + * caller holds the inode lock.
> + */
> +static void ntfs_write_rollback(struct kiocb *iocb, ssize_t written,
> + loff_t pos, loff_t end, loff_t old_init_size,
> + loff_t old_data_size, loff_t old_alloc_size)
> +{
> + struct inode *vi = iocb->ki_filp->f_mapping->host;
> + struct ntfs_inode *ni = NTFS_I(vi);
> + struct ntfs_volume *vol = ni->vol;
> + 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 (written > 0) {
> + new_init_size = max(old_init_size, pos + written);
> + new_data_size = max(old_data_size, pos + written);
> + }
> +
> + /*
> + * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs() expects the
> + * invalidate lock held across the size updates.
> + */
> + 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 = 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(
> + 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 != 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,
> + new_init_size);
> + mutex_unlock(&mrec_ni->mrec_lock);
> + }
> + /*
> + * The shrink clamps the initialized size to the data size, which can
> + * sit above the old initialized one and expose the uninitialized tail,
> + * so skip it unless the rollback above restored that size.
> + */
> + if (ni->data_size != new_data_size && !init_err) {
> + struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> +
> + truncate_setsize(vi, new_data_size);
> + mutex_lock(&mrec_ni->mrec_lock);
> + 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)
> + i_size_write(vi, ni->data_size);
> + }
> + if (init_err || data_err) {
> + /* The failed sizes stay in the dirty MFT record. */
> + NVolSetErrors(vol);
> + ntfs_error(vi->i_sb,
> + "Failed to roll back the size update: %d",
> + init_err ?: data_err);
> + }
> +
> + filemap_invalidate_unlock(vi->i_mapping);
> +}
> +
> ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> {
> struct file *file = iocb->ki_filp;
> @@ -853,7 +1055,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;
> @@ -880,6 +1082,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;
> @@ -901,6 +1118,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);
> @@ -922,61 +1143,24 @@ 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);
> out:
> - if (ret < 0 && ret != -EIOCBQUEUED) {
> - int init_err = 0;
> - int data_err = 0;
> -
> - /*
> - * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs()
> - * expects the invalidate lock held across the size
> - * updates.
> - */
> - filemap_invalidate_lock(vi->i_mapping);
> -
> - /* ntfs_attr_set_initialized_size() rejects a resident one. */
> - if (ni->initialized_size != old_init_size &&
> - NInoNonResident(ni)) {
> - struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> -
> - mutex_lock(&mrec_ni->mrec_lock);
> - init_err = ntfs_attr_set_initialized_size(ni,
> - old_init_size);
> - mutex_unlock(&mrec_ni->mrec_lock);
> - }
> - /*
> - * The shrink clamps the initialized size to the data size,
> - * which can sit above the old initialized one and expose
> - * the uninitialized tail, so skip it unless the rollback
> - * above restored that size.
> - */
> - if (ni->data_size != old_data_size && !init_err) {
> - struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> -
> - truncate_setsize(vi, old_data_size);
> - mutex_lock(&mrec_ni->mrec_lock);
> - data_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
> - old_data_size);
> - mutex_unlock(&mrec_ni->mrec_lock);
> - /* Like ntfs_setattr_size(): stay on the durable size. */
> - if (data_err)
> - i_size_write(vi, ni->data_size);
> - }
> - if (init_err || data_err) {
> - /* The failed sizes stay in the dirty MFT record. */
> - NVolSetErrors(vol);
> - ntfs_error(vi->i_sb,
> - "Failed to roll back the size update: %d",
> - init_err ?: data_err);
> - }
> -
> - filemap_invalidate_unlock(vi->i_mapping);
> - }
> + if (ret < count && ret != -EIOCBQUEUED)
> + ntfs_write_rollback(iocb, ret, pos, end, old_init_size,
> + old_data_size, old_alloc_size);
> out_lock:
> inode_unlock(vi);
> if (ret > 0)
Looks good to me.
Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v12] ntfs: trim the sizes after a short write
2026-11-27 3:10 [PATCH v12] ntfs: trim the sizes after a short write Zhan Xusheng
2026-11-27 3:58 ` liubaolin
@ 2026-12-01 0:35 ` Hyunchul Lee
2026-12-01 9:34 ` Namjae Jeon
2026-12-03 13:39 ` [syzbot ci] " syzbot ci
3 siblings, 0 replies; 7+ messages in thread
From: Hyunchul Lee @ 2026-12-01 0:35 UTC (permalink / raw)
To: Zhan Xusheng
Cc: linkinjeon, ernest.borowski.git, liubaolin12138, ntfs,
linux-kernel, zhanxusheng, stable
On Fri, Nov 27, 2026 at 11:10:53AM +0800, Zhan Xusheng wrote:
> 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. A writer
> killed during the copy also leaves a short write, but iomap_zero_range()
> then fails with -EINTR, so zero from a worker when a fatal signal is
> pending.
>
> An asynchronous direct write that fails after submitting bios returns
> -EIOCBQUEUED, and the rollback does not run for it either. Wait for a
> direct write that moves the initialized size, and fail a non-blocking
> one with -EAGAIN so that it is retried blocking, as ext4 does.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Co-developed-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
Looks good to me.
Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com>
> ---
> v12: move the rollback into ntfs_write_rollback() (Hyunchul). No
> functional change.
> v11: ntfs_attr_get_search_ctx() returns an ERR_PTR() on failure, so
> check it with IS_ERR() in ntfs_attr_set_data_size() (Baolin).
> v10: an asynchronous direct write that fails after submitting bios
> returns -EIOCBQUEUED, so the rollback did not run and the file kept the
> size of the whole write. Wait for a direct write that moves the
> initialized size, and return -EAGAIN for a non-blocking one (Baolin,
> Ernest).
> v9: for a writer killed during the copy, iomap_zero_range() fails with
> -EINTR, and the fallback then took the initialized size back and lost
> what was stored meanwhile through a shared mapping. Zero from a worker
> when a fatal signal is pending (Ernest). Baolin's Reviewed-by on v8 is
> not carried over, as this changes the code he reviewed. After patch 6
> of Ernest's delalloc series, ntfs_zero_range() makes the same call as
> the new helper, so whichever of the two lands second can reuse the
> other.
> 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).
>
> v11: https://lore.kernel.org/all/20261126015731.3254689-1-zhanxusheng1024@gmail.com/
> v10: https://lore.kernel.org/all/20261125051247.2880928-1-zhanxusheng1024@gmail.com/
> v9: https://lore.kernel.org/all/20261124025932.2360848-1-zhanxusheng1024@gmail.com/
> v8: https://lore.kernel.org/all/20261120023504.4134966-1-zhanxusheng1024@gmail.com/
> 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/
>
> v8 was 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.
>
> A 32K pwritev() at 0 to a file with 1M fallocated, its copy stalled
> on a userfaultfd source page after 16K. A marker is stored at 512K
> through a shared mapping, then the writer gets SIGKILL. State after
> remount:
>
> unfixed v8 v9
> marker, 4K clusters kept lost kept
> marker, 64K clusters kept lost kept
> old data in [16K, 64K), 64K clusters 16384 0 0
>
> The tests above give the same results with v9 as with v8, on
> ntfs-next f674e4b6c315. No lockdep or KASAN reports.
>
> A 6M O_DIRECT write at 0 into a file with 6M reserved by
> FALLOC_FL_KEEP_SIZE, its source unmapped after 4M, on a volume whose
> free space held a known pattern, 3 files each, after remount:
>
> ntfs-next v9 v10
> io_submit(), 4K clusters size 6M, size 6M, size 0
> old data in no old data
> 1 of 3
> io_submit(), 64K clusters size 6M, size 6M, size 0
> 2M old data 2M old data
> pwritev() size 0 size 0 size 0
> RWF_NOWAIT 4M, reserved or written written -EAGAIN,
> empty file size 0
>
> The other tests above give the same results with v10 as with v9. No
> lockdep or KASAN reports.
> ---
>
> The short write, killed and stalled writer, KEEP_SIZE and partial
> O_DIRECT tests above give the same results with v11 as with v10, and
> all of the tests above with v12 as with v11, compressed files included.
>
> fs/ntfs/attrib.c | 37 ++++++
> fs/ntfs/attrib.h | 1 +
> fs/ntfs/file.c | 290 ++++++++++++++++++++++++++++++++++++++---------
> 3 files changed, 275 insertions(+), 53 deletions(-)
>
> diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c
> index 6ee6b6bcac1d..c192333be83c 100644
> --- a/fs/ntfs/attrib.c
> +++ b/fs/ntfs/attrib.c
> @@ -2457,6 +2457,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 (IS_ERR(ctx))
> + return PTR_ERR(ctx);
> +
> + 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 9d6bf0320219..0415f817dcf5 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 7d93a520566e..39b49d36d069 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"
> @@ -761,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)
> @@ -843,6 +845,206 @@ 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;
> +}
> +
> +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;
> +}
> +
> +/*
> + * Both sizes were moved to the end of the write before the copy, so a short
> + * write, like a failed one, must take back what it did not write. @written
> + * is what the write returned, a negative error if it wrote nothing. The
> + * caller holds the inode lock.
> + */
> +static void ntfs_write_rollback(struct kiocb *iocb, ssize_t written,
> + loff_t pos, loff_t end, loff_t old_init_size,
> + loff_t old_data_size, loff_t old_alloc_size)
> +{
> + struct inode *vi = iocb->ki_filp->f_mapping->host;
> + struct ntfs_inode *ni = NTFS_I(vi);
> + struct ntfs_volume *vol = ni->vol;
> + 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 (written > 0) {
> + new_init_size = max(old_init_size, pos + written);
> + new_data_size = max(old_data_size, pos + written);
> + }
> +
> + /*
> + * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs() expects the
> + * invalidate lock held across the size updates.
> + */
> + 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 = 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(
> + 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 != 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,
> + new_init_size);
> + mutex_unlock(&mrec_ni->mrec_lock);
> + }
> + /*
> + * The shrink clamps the initialized size to the data size, which can
> + * sit above the old initialized one and expose the uninitialized tail,
> + * so skip it unless the rollback above restored that size.
> + */
> + if (ni->data_size != new_data_size && !init_err) {
> + struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> +
> + truncate_setsize(vi, new_data_size);
> + mutex_lock(&mrec_ni->mrec_lock);
> + 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)
> + i_size_write(vi, ni->data_size);
> + }
> + if (init_err || data_err) {
> + /* The failed sizes stay in the dirty MFT record. */
> + NVolSetErrors(vol);
> + ntfs_error(vi->i_sb,
> + "Failed to roll back the size update: %d",
> + init_err ?: data_err);
> + }
> +
> + filemap_invalidate_unlock(vi->i_mapping);
> +}
> +
> ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
> {
> struct file *file = iocb->ki_filp;
> @@ -853,7 +1055,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;
> @@ -880,6 +1082,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;
> @@ -901,6 +1118,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);
> @@ -922,61 +1143,24 @@ 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);
> out:
> - if (ret < 0 && ret != -EIOCBQUEUED) {
> - int init_err = 0;
> - int data_err = 0;
> -
> - /*
> - * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs()
> - * expects the invalidate lock held across the size
> - * updates.
> - */
> - filemap_invalidate_lock(vi->i_mapping);
> -
> - /* ntfs_attr_set_initialized_size() rejects a resident one. */
> - if (ni->initialized_size != old_init_size &&
> - NInoNonResident(ni)) {
> - struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> -
> - mutex_lock(&mrec_ni->mrec_lock);
> - init_err = ntfs_attr_set_initialized_size(ni,
> - old_init_size);
> - mutex_unlock(&mrec_ni->mrec_lock);
> - }
> - /*
> - * The shrink clamps the initialized size to the data size,
> - * which can sit above the old initialized one and expose
> - * the uninitialized tail, so skip it unless the rollback
> - * above restored that size.
> - */
> - if (ni->data_size != old_data_size && !init_err) {
> - struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
> -
> - truncate_setsize(vi, old_data_size);
> - mutex_lock(&mrec_ni->mrec_lock);
> - data_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
> - old_data_size);
> - mutex_unlock(&mrec_ni->mrec_lock);
> - /* Like ntfs_setattr_size(): stay on the durable size. */
> - if (data_err)
> - i_size_write(vi, ni->data_size);
> - }
> - if (init_err || data_err) {
> - /* The failed sizes stay in the dirty MFT record. */
> - NVolSetErrors(vol);
> - ntfs_error(vi->i_sb,
> - "Failed to roll back the size update: %d",
> - init_err ?: data_err);
> - }
> -
> - filemap_invalidate_unlock(vi->i_mapping);
> - }
> + if (ret < count && ret != -EIOCBQUEUED)
> + ntfs_write_rollback(iocb, ret, pos, end, old_init_size,
> + old_data_size, old_alloc_size);
> out_lock:
> inode_unlock(vi);
> if (ret > 0)
> --
> 2.43.0
>
--
Thanks,
Hyunchul
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v12] ntfs: trim the sizes after a short write
2026-11-27 3:10 [PATCH v12] ntfs: trim the sizes after a short write Zhan Xusheng
2026-11-27 3:58 ` liubaolin
2026-12-01 0:35 ` Hyunchul Lee
@ 2026-12-01 9:34 ` Namjae Jeon
2026-12-03 13:39 ` [syzbot ci] " syzbot ci
3 siblings, 0 replies; 7+ messages in thread
From: Namjae Jeon @ 2026-12-01 9:34 UTC (permalink / raw)
To: Zhan Xusheng
Cc: hyc.lee, ernest.borowski.git, liubaolin12138, ntfs, linux-kernel,
zhanxusheng, stable
On Fri, Nov 27, 2026 at 12:11 PM Zhan Xusheng <zhanxusheng1024@gmail.com> wrote:
>
> 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. A writer
> killed during the copy also leaves a short write, but iomap_zero_range()
> then fails with -EINTR, so zero from a worker when a fatal signal is
> pending.
>
> An asynchronous direct write that fails after submitting bios returns
> -EIOCBQUEUED, and the rollback does not run for it either. Wait for a
> direct write that moves the initialized size, and fail a non-blocking
> one with -EAGAIN so that it is retried blocking, as ext4 does.
>
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> Co-developed-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Ernest Borowski <ernest.borowski.git@proton.me>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
Applied it to #ntfs-next.
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread* [syzbot ci] Re: ntfs: trim the sizes after a short write
2026-11-27 3:10 [PATCH v12] ntfs: trim the sizes after a short write Zhan Xusheng
` (2 preceding siblings ...)
2026-12-01 9:34 ` Namjae Jeon
@ 2026-12-03 13:39 ` syzbot ci
2026-12-03 21:54 ` Namjae Jeon
3 siblings, 1 reply; 7+ messages in thread
From: syzbot ci @ 2026-12-03 13:39 UTC (permalink / raw)
To: ernest.borowski.git, hyc.lee, linkinjeon, linux-kernel,
liubaolin12138, ntfs, stable, zhanxusheng1024, zhanxusheng
Cc: syzbot, syzkaller-bugs
syzbot ci has tested the following series
[v12] ntfs: trim the sizes after a short write
https://lore.kernel.org/all/20261127031053.3695758-1-zhanxusheng1024@gmail.com
* [PATCH v12] ntfs: trim the sizes after a short write
and found the following issue:
WARNING in iomap_iter
Full report is available here:
https://ci.syzbot.org/series/e9a896ef-5d0c-4800-ab40-719f4503c10a
***
WARNING in iomap_iter
tree: torvalds
URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux
base: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3
arch: amd64
compiler: Debian clang version 22.1.8 (1~deb13u5), Debian LLD 22.1.8
config: https://ci.syzbot.org/builds/a1ab6509-642b-4ccb-aaa7-76aab177bec7/config
syz repro: https://ci.syzbot.org/findings/87f90618-fd87-4cea-9211-3e100f96a1bd/syz_repro
------------[ cut here ]------------
iter->iomap.offset + iter->iomap.length <= iter->pos
WARNING: fs/iomap/iter.c:39 at iomap_iter_done fs/iomap/iter.c:39 [inline], CPU#1: syz.0.20/6344
WARNING: fs/iomap/iter.c:39 at iomap_iter+0x541/0xa80 fs/iomap/iter.c:125, CPU#1: syz.0.20/6344
Modules linked in:
CPU: 1 UID: 0 PID: 6344 Comm: syz.0.20 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:iomap_iter_done fs/iomap/iter.c:39 [inline]
RIP: 0010:iomap_iter+0x541/0xa80 fs/iomap/iter.c:125
Code: 0f 0b 90 e9 3b fb ff ff e8 2c 2c 4f ff 90 0f 0b 90 e9 30 fc ff ff e8 1e 2c 4f ff 90 0f 0b 90 e9 61 fc ff ff e8 10 2c 4f ff 90 <0f> 0b 90 e9 c9 fc ff ff e8 02 2c 4f ff 90 0f 0b 90 e9 fb fc ff ff
RSP: 0018:ffffc9000380eeb8 EFLAGS: 00010293
RAX: ffffffff827a33a0 RBX: ffffc9000380efa8 RCX: ffff888171c50000
RDX: 0000000000000000 RSI: 0000000000150000 RDI: 000000000013e000
RBP: 000000000013e000 R08: ffff88811d6f788f R09: 1ffff11023adef11
R10: dffffc0000000000 R11: ffffed1023adef12 R12: 1ffff92000701dfb
R13: 1ffff92000701df5 R14: ffffc9000380efa0 R15: 0000000000150000
FS: 00007fd630c3f6c0(0000) GS:ffff8882a77ed000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fbdec814440 CR3: 000000011f964000 CR4: 00000000000006f0
Call Trace:
<TASK>
iomap_readahead+0x3e9/0xf20 fs/iomap/buffered-io.c:751
ntfs_readahead+0x202/0x270 fs/ntfs/aops.c:251
read_pages+0x193/0x5a0 mm/readahead.c:174
page_cache_ra_unbounded+0x754/0x9d0 mm/readahead.c:315
do_page_cache_ra mm/readahead.c:348 [inline]
page_cache_ra_order+0xb47/0xee0 mm/readahead.c:558
do_sync_mmap_readahead+0x74b/0x960 mm/filemap.c:3466
filemap_fault+0x73d/0x12c0 mm/filemap.c:3620
__do_fault+0x138/0x2a0 mm/memory.c:5693
do_cow_fault mm/memory.c:6174 [inline]
do_fault mm/memory.c:6280 [inline]
do_pte_missing+0xfc9/0x3570 mm/memory.c:4800
handle_pte_fault mm/memory.c:6697 [inline]
__handle_mm_fault mm/memory.c:6849 [inline]
handle_mm_fault+0x1807/0x2f80 mm/memory.c:7016
faultin_page mm/gup.c:1140 [inline]
__get_user_pages+0x102b/0x1e90 mm/gup.c:1442
populate_vma_page_range+0x311/0x3f0 mm/gup.c:1878
__mm_populate+0x25f/0x390 mm/gup.c:1981
mm_populate include/linux/mm.h:4513 [inline]
vm_mmap_pgoff+0x36e/0x520 mm/util.c:586
ksys_mmap_pgoff+0x4dc/0x760 mm/mmap.c:619
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fd62fda9b59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fd630c3f028 EFLAGS: 00000246 ORIG_RAX: 0000000000000009
RAX: ffffffffffffffda RBX: 00007fd630035fa0 RCX: 00007fd62fda9b59
RDX: 000000000000000b RSI: 0000000000b36000 RDI: 0000200000000000
RBP: 00007fd62fe3a18b R08: 0000000000000005 R09: 0000000000000000
R10: 0000000000022852 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fd630036038 R14: 00007fd630035fa0 R15: 00007ffd16543888
</TASK>
***
If these findings have caused you to resend the series or submit a
separate fix, please add the following tag to your commit message:
Tested-by: syzbot@syzkaller.appspotmail.com
---
This report is generated by a bot. It may contain errors.
syzbot ci engineers can be reached at syzkaller@googlegroups.com.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [syzbot ci] Re: ntfs: trim the sizes after a short write
2026-12-03 13:39 ` [syzbot ci] " syzbot ci
@ 2026-12-03 21:54 ` Namjae Jeon
2026-12-03 21:55 ` syzbot ci
0 siblings, 1 reply; 7+ messages in thread
From: Namjae Jeon @ 2026-12-03 21:54 UTC (permalink / raw)
To: syzbot ci
Cc: ernest.borowski.git, hyc.lee, linux-kernel, liubaolin12138, ntfs,
stable, zhanxusheng1024, zhanxusheng, syzbot, syzkaller-bugs
#syz test: https://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git
ntfs-next
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Re: [syzbot ci] Re: ntfs: trim the sizes after a short write
2026-12-03 21:54 ` Namjae Jeon
@ 2026-12-03 21:55 ` syzbot ci
0 siblings, 0 replies; 7+ messages in thread
From: syzbot ci @ 2026-12-03 21:55 UTC (permalink / raw)
To: linkinjeon
Cc: ernest.borowski.git, hyc.lee, linkinjeon, linux-kernel,
liubaolin12138, ntfs, stable, syzbot, syzkaller-bugs,
zhanxusheng1024, zhanxusheng
> #syz test: https://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/ntfs.git
Patch testing via #syz test is temporarily disabled.
Sorry for the inconvenience.
> ntfs-next
^ permalink raw reply [flat|nested] 7+ messages in thread