mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] ntfs: write back the truncated range before dropping it
@ 2026-09-15  6:27 Hongling Zeng
  2026-09-15  6:27 ` [PATCH 2/2] ntfs: roll back the in-memory attributes if the metadata update fails Hongling Zeng
  2026-09-15  9:03 ` [PATCH 1/2] ntfs: write back the truncated range before dropping it liubaolin
  0 siblings, 2 replies; 4+ messages in thread
From: Hongling Zeng @ 2026-09-15  6:27 UTC (permalink / raw)
  To: linkinjeon, hyc.lee
  Cc: ntfs, linux-kernel, zhongling0719, Hongling Zeng, stable

On a truncate, ntfs_setattr_size() drops the page cache beyond the new
size with truncate_setsize() and only then updates the attribute on
disk.  If that update fails, the size is reverted and the data beyond
the new size becomes visible again -- but the pages were already
dropped, and dirty data in them is lost, even though the truncate, the
operation the user asked for, never happened.

Write the range back before dropping it.  If the writeback fails, fail
the truncate with that error and keep the page cache intact; if the
on-disk truncation fails afterwards, the reverted size can be re-read
from disk.

Fixes: 9c87959601e8 ("ntfs: update file operations")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
 fs/ntfs/file.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 007d1614b9ac..1417a76788ed 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -283,12 +283,27 @@ static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
 		i_size_write(vi, attr->ia_size);
 		pagecache_isize_extended(vi, old_size, attr->ia_size);
 	} else {
+		/*
+		 * The on-disk truncation below can fail, in which case
+		 * the size is reverted and the data beyond the new size
+		 * becomes visible again.  Write that range back first:
+		 * truncate_setsize() drops those pages and dirty data
+		 * cannot be recovered afterwards.
+		 */
+		if (attr->ia_size < old_size) {
+			err = filemap_write_and_wait_range(vi->i_mapping,
+					attr->ia_size, old_size - 1);
+			if (err)
+				goto out_unlock;
+		}
+
 		truncate_setsize(vi, attr->ia_size);
 	}
 
 	err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
 	if (err)
 		i_size_write(vi, old_size);
+out_unlock:
 	filemap_invalidate_unlock(vi->i_mapping);
 
 	return err;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-15  9:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-15  6:27 [PATCH 1/2] ntfs: write back the truncated range before dropping it Hongling Zeng
2026-09-15  6:27 ` [PATCH 2/2] ntfs: roll back the in-memory attributes if the metadata update fails Hongling Zeng
2026-09-15  9:33   ` liubaolin
2026-09-15  9:03 ` [PATCH 1/2] ntfs: write back the truncated range before dropping it liubaolin

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®