[PATCH v1 10/13] ntfs: complete compressed reads at folio granularity

From: Baolin Liu

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


From: Baolin Liu <liubaolin@xxxxxxxxxx>

An empty file can retain its folio policy and existing VMAs when native
compression is enabled. The compression reader must therefore handle a
target folio spanning several compression units without unlocking it
after the first base page.

Read the units intersecting the target and count completed base pages,
bounded by i_size. Complete neighbouring order-0 pages independently and
leave neighbouring large folios to their own reads. Mark the target
uptodate only after all required pages complete, zero its uninitialized
tail, and unlock it once. Use common error cleanup to release neighbour
references and leave an incomplete target non-uptodate.

Signed-off-by: Baolin Liu <liubaolin@xxxxxxxxxx>
---
fs/ntfs/compress.c | 220 +++++++++++++++++--------------------------
fs/ntfs/ntfs_codec.h | 2 +-
2 files changed, 89 insertions(+), 133 deletions(-)

diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
index 01582f64d4c2..0f41591db1b5 100644
--- a/fs/ntfs/compress.c
+++ b/fs/ntfs/compress.c
@@ -124,6 +124,24 @@ static inline void handle_bounds_compressed_page(struct page *page,
}
}

+/* Complete neighbouring base pages, but keep the target folio locked. */
+static void ntfs_end_compressed_page(struct page **pages, unsigned int index,
+ struct folio *target, unsigned int *nr_completed,
+ loff_t i_size, s64 initialized_size)
+{
+ struct page *page = pages[index];
+
+ handle_bounds_compressed_page(page, i_size, initialized_size);
+ if (page_folio(page) == target) {
+ (*nr_completed)++;
+ } else {
+ SetPageUptodate(page);
+ unlock_page(page);
+ put_page(page);
+ }
+ pages[index] = NULL;
+}
+
/*
* ntfs_decompress - decompress a compression block into an array of pages
* @dest_pages: destination array of pages
@@ -132,39 +150,36 @@ static inline void handle_bounds_compressed_page(struct page *page,
* @dest_ofs: current offset within @dest_pages[@dest_index] (IN/OUT)
* @dest_max_index: maximum index into @dest_pages (IN)
* @dest_max_ofs: maximum offset within @dest_pages[@dest_max_index] (IN)
- * @xpage: the target page (-1 if none) (IN)
- * @xpage_done: set to 1 if xpage was completed successfully (IN/OUT)
+ * @target: locked target folio (IN)
+ * @nr_completed: number of completed pages in @target (IN/OUT)
* @cb_start: compression block to decompress (IN)
* @cb_size: size of compression block @cb_start in bytes (IN)
* @i_size: file size when we started the read (IN)
* @initialized_size: initialized file size when we started the read (IN)
*
- * The caller must have disabled preemption. ntfs_decompress() reenables it when
- * the critical section is finished.
+ * The caller must hold ntfs_cb_lock. ntfs_decompress() releases it after
+ * it has finished accessing the shared compression buffer.
*
* This decompresses the compression block @cb_start into the array of
* destination pages @dest_pages starting at index @dest_index into @dest_pages
* and at offset @dest_pos into the page @dest_pages[@dest_index].
*
- * When the page @dest_pages[@xpage] is completed, @xpage_done is set to 1.
- * If xpage is -1 or @xpage has not been completed, @xpage_done is not modified.
+ * Completed pages in @target are counted in @nr_completed. The caller
+ * marks @target uptodate and unlocks it once all of its pages are filled.
*
* @cb_start is a pointer to the compression block which needs decompressing
* and @cb_size is the size of @cb_start in bytes (8-64kiB).
*
* Return 0 if success or -EOVERFLOW on error in the compressed stream.
- * @xpage_done indicates whether the target page (@dest_pages[@xpage]) was
- * completed during the decompression of the compression block (@cb_start).
*
* Warning: This function *REQUIRES* PAGE_SIZE >= 4096 or it will blow up
* unpredicatbly! You have been warned!
*
- * Note to hackers: This function may not sleep until it has finished accessing
- * the compression block @cb_start as it is a per-CPU buffer.
*/
static int ntfs_decompress(struct page *dest_pages[], int completed_pages[],
int *dest_index, int *dest_ofs, const int dest_max_index,
- const int dest_max_ofs, const int xpage, char *xpage_done,
+ const int dest_max_ofs, struct folio *target,
+ unsigned int *nr_completed,
u8 *const cb_start, const u32 cb_size, const loff_t i_size,
const s64 initialized_size)
{
@@ -220,20 +235,9 @@ return_error:
for (i = 0; i < nr_completed_pages; i++) {
int di = completed_pages[i];

- dp = dest_pages[di];
- /*
- * If we are outside the initialized size, zero
- * the out of bounds page range.
- */
- handle_bounds_compressed_page(dp, i_size,
- initialized_size);
- SetPageUptodate(dp);
- unlock_page(dp);
- if (di == xpage)
- *xpage_done = 1;
- else
- put_page(dp);
- dest_pages[di] = NULL;
+ ntfs_end_compressed_page(dest_pages, di, target,
+ nr_completed, i_size,
+ initialized_size);
}
}
return err;
@@ -450,7 +454,7 @@ return_overflow:
* When we are called the page has already been verified to be locked and the
* attribute is known to be non-resident, not encrypted, but compressed.
*
- * 1. Determine which compression block(s) @page is in.
+ * 1. Determine which compression block(s) @folio spans.
* 2. Get hold of all pages corresponding to this/these compression block(s).
* 3. Read the (first) compression block.
* 4. Decompress it into the corresponding pages.
@@ -485,7 +489,7 @@ int ntfs_read_compressed_block(struct folio *folio)
* The first vcn after the last wanted vcn (minimum alignment is again
* PAGE_SIZE.
*/
- s64 end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1)
+ s64 end_vcn = ((folio_next_pos(folio) + cb_size - 1)
& ~cb_size_mask) >> vol->cluster_size_bits;
/* Number of compression blocks (cbs) in the wanted vcn range. */
unsigned int nr_cbs = ntfs_cluster_to_bytes(vol, end_vcn - start_vcn) >>
@@ -501,7 +505,7 @@ int ntfs_read_compressed_block(struct folio *folio)
int cb_max_page, err = 0;
struct page **pages;
int *completed_pages;
- unsigned char xpage_done = 0;
+ unsigned int nr_completed = 0, nr_target_pages;
unsigned int nofs_flags;

ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = %i.",
@@ -527,12 +531,11 @@ int ntfs_read_compressed_block(struct folio *folio)
}

/*
- * We have already been given one page, this is the one we must do.
- * Once again, the alignment guarantees keep it simple.
+ * The target folio stays locked until all of its pages have been read.
+ * Neighbouring base pages can be completed independently.
*/
offset = ntfs_cluster_to_pidx(vol, start_vcn);
xpage = index - offset;
- pages[xpage] = page;
/*
* The remaining pages need to be allocated and inserted into the page
* cache, alignment guarantees keep all the below much simpler. (-8
@@ -541,34 +544,39 @@ int ntfs_read_compressed_block(struct folio *folio)
i_size = i_size_read(VFS_I(ni));
initialized_size = ni->initialized_size;
read_unlock_irqrestore(&ni->size_lock, flags);
- max_page = ((i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) -
- offset;
- /* Is the page fully outside i_size? (truncate in progress) */
- if (xpage >= max_page) {
+ /* Is the folio fully outside i_size? (truncate in progress) */
+ if (folio_pos(folio) >= i_size) {
kfree(pages);
kfree(completed_pages);
- zero_user_segments(page, 0, PAGE_SIZE, 0, 0);
+ folio_zero_segment(folio, 0, folio_size(folio));
ntfs_debug("Compressed read outside i_size - truncated?");
- SetPageUptodate(page);
- unlock_page(page);
+ folio_mark_uptodate(folio);
+ folio_unlock(folio);
return 0;
}
+ max_page = ((i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) - offset;
if (nr_pages < max_page)
max_page = nr_pages;
+ nr_target_pages = min_t(unsigned int, folio_nr_pages(folio),
+ max_page - xpage);

for (i = 0; i < max_page; i++, offset++) {
- if (i != xpage)
- pages[i] = grab_cache_page_nowait(mapping, offset);
+ if (folio_contains(folio, offset)) {
+ pages[i] = folio_file_page(folio, offset);
+ continue;
+ }
+ pages[i] = grab_cache_page_nowait(mapping, offset);
page = pages[i];
if (page) {
/*
* We only (re)read the page if it isn't already read
* in and/or dirty or we would be losing data or at
- * least wasting our time.
+ * least wasting our time. A neighbouring large folio needs
+ * its own complete read.
*/
- if (!PageDirty(page) && (!PageUptodate(page))) {
+ if (!folio_test_large(page_folio(page)) &&
+ !PageDirty(page) && !PageUptodate(page))
continue;
- }
unlock_page(page);
put_page(page);
pages[i] = NULL;
@@ -677,32 +685,25 @@ lock_retry_remap:
cb_max_page >>= PAGE_SHIFT;

/* Catch end of file inside a compression block. */
- if (cb_max_page > max_page)
+ if (cb_max_page > max_page) {
cb_max_page = max_page;
+ cb_max_ofs = 0;
+ }

if (vcn == start_vcn - cb_clusters) {
/* Sparse cb, zero out page range overlapping the cb. */
ntfs_debug("Found sparse compression block.");
/* We can sleep from now on, so we drop lock. */
mutex_unlock(&ntfs_cb_lock);
- if (cb_max_ofs)
- cb_max_page--;
for (; cur_page < cb_max_page; cur_page++) {
page = pages[cur_page];
if (page) {
memzero_page(page, cur_ofs, PAGE_SIZE - cur_ofs);
- SetPageUptodate(page);
- unlock_page(page);
- if (cur_page == xpage)
- xpage_done = 1;
- else
- put_page(page);
- pages[cur_page] = NULL;
+ ntfs_end_compressed_page(pages, cur_page, folio,
+ &nr_completed, i_size, initialized_size);
}
cb_pos += PAGE_SIZE - cur_ofs;
cur_ofs = 0;
- if (cb_pos >= cb_end)
- break;
}
/* If we have a partial final page, deal with it now. */
if (cb_max_ofs && cb_pos < cb_end) {
@@ -718,13 +719,9 @@ lock_retry_remap:
} else if (vcn == start_vcn) {
/* We can't sleep so we need two stages. */
unsigned int cur2_page = cur_page;
- unsigned int cur_ofs2 = cur_ofs;
- u8 *cb_pos2 = cb_pos;

ntfs_debug("Found uncompressed compression block.");
/* Uncompressed cb, copy it to the destination pages. */
- if (cb_max_ofs)
- cb_max_page--;
/* First stage: copy data into destination pages. */
for (; cur_page < cb_max_page; cur_page++) {
page = pages[cur_page];
@@ -733,8 +730,6 @@ lock_retry_remap:
PAGE_SIZE - cur_ofs);
cb_pos += PAGE_SIZE - cur_ofs;
cur_ofs = 0;
- if (cb_pos >= cb_end)
- break;
}
/* If we have a partial final page, deal with it now. */
if (cb_max_ofs && cb_pos < cb_end) {
@@ -750,34 +745,16 @@ lock_retry_remap:
/* Second stage: finalize pages. */
for (; cur2_page < cb_max_page; cur2_page++) {
page = pages[cur2_page];
- if (page) {
- /*
- * If we are outside the initialized size, zero
- * the out of bounds page range.
- */
- handle_bounds_compressed_page(page, i_size,
- initialized_size);
- SetPageUptodate(page);
- unlock_page(page);
- if (cur2_page == xpage)
- xpage_done = 1;
- else
- put_page(page);
- pages[cur2_page] = NULL;
- }
- cb_pos2 += PAGE_SIZE - cur_ofs2;
- cur_ofs2 = 0;
- if (cb_pos2 >= cb_end)
- break;
+ if (page)
+ ntfs_end_compressed_page(pages, cur2_page, folio,
+ &nr_completed, i_size, initialized_size);
}
} else {
/* Compressed cb, decompress it into the destination page(s). */
- unsigned int prev_cur_page = cur_page;
-
ntfs_debug("Found compressed compression block.");
err = ntfs_lznt1_codec_ops.decompress_pages(pages, completed_pages, &cur_page,
- &cur_ofs, cb_max_page, cb_max_ofs, xpage,
- &xpage_done, cb_pos, cb_size - (cb_pos - cb),
+ &cur_ofs, cb_max_page, cb_max_ofs, folio,
+ &nr_completed, cb_pos, cb_size - (cb_pos - cb),
i_size, initialized_size);
/*
* We can sleep from now on, lock already dropped by
@@ -785,54 +762,42 @@ lock_retry_remap:
*/
if (err) {
ntfs_error(vol->sb,
- "ntfs_decompress() failed in inode 0x%llx with error code %i. Skipping this compression block.",
+ "ntfs_decompress() failed in inode 0x%llx with error code %i.",
ni->mft_no, -err);
- /* Release the unfinished pages. */
- for (; prev_cur_page < cur_page; prev_cur_page++) {
- page = pages[prev_cur_page];
- if (page) {
- flush_dcache_page(page);
- unlock_page(page);
- if (prev_cur_page != xpage)
- put_page(page);
- pages[prev_cur_page] = NULL;
- }
- }
+ goto out;
}
}

- /* Do we have more work to do? */
- if (nr_cbs)
+ /* Stop at EOF even if the target folio spans more compression units. */
+ if (nr_cbs && cur_page < max_page)
goto do_next_cb;

- /* Clean up if we have any pages left. Should never happen. */
- for (cur_page = 0; cur_page < max_page; cur_page++) {
- page = pages[cur_page];
- if (page) {
- folio = page_folio(page);
-
- ntfs_error(vol->sb,
- "Still have pages left! Terminating them with extreme prejudice. Inode 0x%llx, page index 0x%lx.",
- ni->mft_no, folio->index);
- flush_dcache_folio(folio);
- folio_unlock(folio);
- if (cur_page != xpage)
- folio_put(folio);
- pages[cur_page] = NULL;
+out:
+ /* Only neighbouring pages hold references acquired by this read. */
+ for (i = 0; i < max_page; i++) {
+ page = pages[i];
+ if (page && page_folio(page) != folio) {
+ unlock_page(page);
+ put_page(page);
}
}
-
- /* We no longer need the list of pages. */
kfree(pages);
kfree(completed_pages);

- /* If we have completed the requested page, we return success. */
- if (likely(xpage_done))
- return 0;
+ if (nr_completed == nr_target_pages) {
+ loff_t end = min_t(loff_t, i_size, initialized_size);
+ size_t valid = clamp_t(loff_t, end - folio_pos(folio),
+ 0, folio_size(folio));

- ntfs_debug("Failed. Returning error code %s.", err == -EOVERFLOW ?
- "EOVERFLOW" : (!err ? "EIO" : "unknown error"));
- return err < 0 ? err : -EIO;
+ folio_zero_segment(folio, valid, folio_size(folio));
+ flush_dcache_folio(folio);
+ folio_mark_uptodate(folio);
+ err = 0;
+ } else if (!err) {
+ err = -EIO;
+ }
+ folio_unlock(folio);
+ return err;

map_rl_err:
ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read compression block.");
@@ -848,18 +813,9 @@ read_err:
ntfs_error(vol->sb, "IO error while reading compressed data.");

err_out:
- for (i = cur_page; i < max_page; i++) {
- page = pages[i];
- if (page) {
- flush_dcache_page(page);
- unlock_page(page);
- if (i != xpage)
- put_page(page);
- }
- }
- kfree(pages);
- kfree(completed_pages);
- return -EIO;
+ if (!err)
+ err = -EIO;
+ goto out;
}

/*
diff --git a/fs/ntfs/ntfs_codec.h b/fs/ntfs/ntfs_codec.h
index 6030055fb0c1..df52aeb23f95 100644
--- a/fs/ntfs/ntfs_codec.h
+++ b/fs/ntfs/ntfs_codec.h
@@ -36,7 +36,7 @@ struct ntfs_codec_ops {
int completed_pages[],
int *dest_index, int *dest_ofs,
int dest_max_index, int dest_max_ofs,
- int xpage, char *xpage_done,
+ struct folio *target, unsigned int *nr_completed,
u8 *cb_start, u32 cb_size,
loff_t i_size, s64 initialized_size);
int (*compress_subblock)(struct compress_context *pctx,