[PATCH] cramfs: fix direct uncompressed full-page reads

Yichong Chen posted 1 patch 3 weeks, 2 days ago
fs/cramfs/inode.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] cramfs: fix direct uncompressed full-page reads
Posted by Yichong Chen 3 weeks, 2 days ago
For direct uncompressed cramfs blocks, the block size is PAGE_SIZE except
for the last block, where it is capped by the file size.

When the file size is exactly page-aligned, offset_in_page(i_size)
returns 0. The current last-block handling then turns the final full page
into a zero-length block, so cramfs_read_folio() treats it as a hole and
returns zero-filled data instead of the stored page.

Keep PAGE_SIZE for a page-aligned final block, and only shorten the block
when the last page is partial.

Fixes: fd4f6f2a78ae ("cramfs: implement uncompressed and arbitrary data block positioning")
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
---
 fs/cramfs/inode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index d4cd03f4f60d..371b4c421d86 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -853,9 +853,9 @@ static int cramfs_read_folio(struct file *file, struct folio *folio)
 			if (uncompressed) {
 				block_len = PAGE_SIZE;
 				/* if last block: cap to file length */
-				if (folio->index == maxblock - 1)
-					block_len =
-						offset_in_page(inode->i_size);
+				if (folio->index == maxblock - 1 &&
+				    offset_in_page(inode->i_size))
+					block_len = offset_in_page(inode->i_size);
 			} else {
 				block_len = *(u16 *)
 					cramfs_read(sb, block_start, 2);
-- 
2.51.0