[PATCH v1 02/13] ntfs: read compression units without the block device page cache

From: Baolin Liu

Date: Wed Dec 02 2026 - 23:06:41 EST


From: Baolin Liu <liubaolin@xxxxxxxxxx>

Native compression writes use BIOs, but reads use the block device page
cache. After clusters are freed and reused, cached device pages can
still contain old data. A file-cache miss can then return stale contents,
as reproduced by rewriting compressed files and reusing their clusters
on an order-0 kernel.

Read compression units with ntfs_bdev_read(), bypassing the device page
cache. Read contiguous clusters within a unit together and use a NOFS
scope while holding the runlist and compression-buffer locks.

Fixes: 495e90fa3348 ("ntfs: update attrib operations")
Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/compress.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
index 82c7c7d29073..8a60f5e13202 100644
--- a/fs/ntfs/compress.c
+++ b/fs/ntfs/compress.c
@@ -19,6 +19,7 @@
#include <linux/blkdev.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
+#include <linux/sched/mm.h>

#include "attrib.h"
#include "ntfs_codec.h"
@@ -495,13 +496,13 @@ int ntfs_read_compressed_block(struct folio *folio)
* guarantees of start_vcn and end_vcn, no need to round up here.
*/
unsigned int nr_pages = ntfs_cluster_to_pidx(vol, end_vcn - start_vcn);
- unsigned int xpage, max_page, cur_page, cur_ofs, i, page_ofs, page_index;
+ unsigned int xpage, max_page, cur_page, cur_ofs, i;
unsigned int cb_clusters, cb_max_ofs;
int cb_max_page, err = 0;
struct page **pages;
int *completed_pages;
unsigned char xpage_done = 0;
- struct page *lpage;
+ unsigned int nofs_flags;

ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = %i.",
index, cb_size, nr_pages);
@@ -597,9 +598,10 @@ do_next_cb:
cb_end = cb + cb_size;

rl = NULL;
- for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;
- vcn++) {
+ for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn;) {
bool is_retry = false;
+ s64 clusters;
+ size_t bytes;

if (!rl) {
lock_retry_remap:
@@ -639,22 +641,20 @@ lock_retry_remap:
goto map_rl_err;
}

- page_ofs = ntfs_cluster_to_poff(vol, lcn);
- page_index = ntfs_cluster_to_pidx(vol, lcn);
-
- lpage = read_mapping_page(sb->s_bdev->bd_mapping,
- page_index, NULL);
- if (IS_ERR(lpage)) {
- err = PTR_ERR(lpage);
+ clusters = min(rl->length - (vcn - rl->vcn), start_vcn - vcn);
+ bytes = ntfs_cluster_to_bytes(vol, clusters);
+ /* Compression writes use BIOs and do not update the bdev cache. */
+ nofs_flags = memalloc_nofs_save();
+ err = ntfs_bdev_read(sb->s_bdev, cb_pos,
+ ntfs_cluster_to_bytes(vol, lcn),
+ bytes);
+ memalloc_nofs_restore(nofs_flags);
+ if (err) {
mutex_unlock(&ntfs_cb_lock);
goto read_err;
}
-
- lock_page(lpage);
- memcpy_from_page(cb_pos, lpage, page_ofs, vol->cluster_size);
- unlock_page(lpage);
- put_page(lpage);
- cb_pos += vol->cluster_size;
+ cb_pos += bytes;
+ vcn += clusters;
}

/* Release the lock if we took it. */