[PATCH v1 09/13] ntfs: serialize compression changes with file access

From: Baolin Liu

Date: Wed Dec 02 2026 - 23:07:34 EST


From: Baolin Liu <liubaolin@xxxxxxxxxx>

An empty file can change native compression through system.ntfs_attrib
or system.ntfs_attrib_be while a VMA still references its page cache.
Serialize the attribute update with faults using the mapping invalidate
lock, and discard the old cache after a successful compression change.

Check direct-I/O eligibility under the inode lock so compression cannot
change between the check and the operation. Return SIGBUS for writes
through shared writable VMAs retained across compression enablement;
iomap must not write compressed data. Retained VMAs can still read, while
new mmap requests for compressed files remain unsupported.

Keep the mapping's folio-order limits unchanged after initialization.

Fixes: fc053f05ca28 ("ntfs: add reparse and ea operations")
Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/ea.c | 7 +++++++
fs/ntfs/file.c | 23 +++++++++++++++--------
2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/fs/ntfs/ea.c b/fs/ntfs/ea.c
index 08a053be3e30..3f88baa8ff15 100644
--- a/fs/ntfs/ea.c
+++ b/fs/ntfs/ea.c
@@ -950,9 +950,16 @@ static int ntfs_setxattr(const struct xattr_handler *handler,
goto out;

if (S_ISREG(inode->i_mode)) {
+ bool compression_changed = (ni->flags ^ fattr) &
+ FILE_ATTR_COMPRESSED;
+
+ filemap_invalidate_lock(inode->i_mapping);
mutex_lock(&ni->mrec_lock);
err = ntfs_new_attr_flags(ni, fattr);
mutex_unlock(&ni->mrec_lock);
+ if (!err && compression_changed)
+ truncate_pagecache(inode, 0);
+ filemap_invalidate_unlock(inode->i_mapping);
if (err)
goto out;
}
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 4a837c599c82..f78405bd373f 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -708,11 +708,12 @@ ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
if (NVolShutdown(NTFS_SB(sb)))
return -EIO;

- if ((NInoCompressed(NTFS_I(vi)) || NInoWofCompressed(NTFS_I(vi)) ||
- NInoEncrypted(NTFS_I(vi))) && iocb->ki_flags & IOCB_DIRECT)
- return -EOPNOTSUPP;
-
inode_lock_shared(vi);
+ if ((NInoCompressed(NTFS_I(vi)) || NInoWofCompressed(NTFS_I(vi)) ||
+ NInoEncrypted(NTFS_I(vi))) && iocb->ki_flags & IOCB_DIRECT) {
+ ret = -EOPNOTSUPP;
+ goto inode_unlock;
+ }

if (iocb->ki_flags & IOCB_DIRECT) {
size_t count = iov_iter_count(to);
@@ -867,15 +868,17 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
return -EOPNOTSUPP;
}

- if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT)
- return -EOPNOTSUPP;
-
if (iocb->ki_flags & IOCB_NOWAIT) {
if (!inode_trylock(vi))
return -EAGAIN;
} else
inode_lock(vi);

+ if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT) {
+ ret = -EOPNOTSUPP;
+ goto out_lock;
+ }
+
ret = generic_write_checks(iocb, from);
if (ret <= 0)
goto out_lock;
@@ -1012,7 +1015,11 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
* exclusively while invalidating pagecache and changing extents.
*/
filemap_invalidate_lock_shared(mapping);
- ret = iomap_page_mkwrite(vmf, ntfs_page_mkwrite_iomap_next, NULL);
+ /* A mapping created before an empty file was made compressed survives. */
+ if (NInoCompressed(NTFS_I(inode)))
+ ret = VM_FAULT_SIGBUS;
+ else
+ ret = iomap_page_mkwrite(vmf, ntfs_page_mkwrite_iomap_next, NULL);
filemap_invalidate_unlock_shared(mapping);
sb_end_pagefault(inode->i_sb);
return ret;