1 // SPDX-License-Identifier: GPL-2.0-only
4 * Phillip Lougher <phillip@squashfs.org.uk>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/pagemap.h>
13 #include <linux/mutex.h>
15 #include "squashfs_fs.h"
16 #include "squashfs_fs_sb.h"
17 #include "squashfs_fs_i.h"
19 #include "page_actor.h"
21 /* Read separately compressed datablock directly into page cache */
22 int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
26 struct inode *inode = target_page->mapping->host;
27 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
29 int file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
30 int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
31 int start_index = target_page->index & ~mask;
32 int end_index = start_index | mask;
33 int i, n, pages, bytes, res = -ENOMEM;
35 struct squashfs_page_actor *actor;
38 if (end_index > file_end)
41 pages = end_index - start_index + 1;
43 page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
47 /* Try to grab all the pages covered by the Squashfs block */
48 for (i = 0, n = start_index; n <= end_index; n++) {
49 page[i] = (n == target_page->index) ? target_page :
50 grab_cache_page_nowait(target_page->mapping, n);
55 if (PageUptodate(page[i])) {
67 * Create a "page actor" which will kmap and kunmap the
68 * page cache pages appropriately within the decompressor
70 actor = squashfs_page_actor_init_special(msblk, page, pages, expected);
74 /* Decompress directly into the page cache buffers */
75 res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
77 squashfs_page_actor_free(actor);
82 if (res != expected) {
87 /* Last page (if present) may have trailing bytes not filled */
88 bytes = res % PAGE_SIZE;
89 if (page[pages - 1]->index == end_index && bytes) {
90 pageaddr = kmap_local_page(page[pages - 1]);
91 memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
92 kunmap_local(pageaddr);
95 /* Mark pages as uptodate, unlock and release */
96 for (i = 0; i < pages; i++) {
97 flush_dcache_page(page[i]);
98 SetPageUptodate(page[i]);
100 if (page[i] != target_page)
109 /* Decompression failed, mark pages as errored. Target_page is
110 * dealt with by the caller
112 for (i = 0; i < pages; i++) {
113 if (page[i] == NULL || page[i] == target_page)
115 flush_dcache_page(page[i]);
116 SetPageError(page[i]);
117 unlock_page(page[i]);