3 * Phillip Lougher <phillip@squashfs.org.uk>
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
10 #include <linux/vfs.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/pagemap.h>
15 #include <linux/mutex.h>
16 #include <linux/mm_inline.h>
18 #include "squashfs_fs.h"
19 #include "squashfs_fs_sb.h"
20 #include "squashfs_fs_i.h"
22 #include "page_actor.h"
24 // Backported from 4.5
25 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
27 static void release_actor_pages(struct page **page, int pages, int error)
31 for (i = 0; i < pages; i++) {
34 flush_dcache_page(page[i]);
36 SetPageUptodate(page[i]);
38 SetPageError(page[i]);
39 zero_user_segment(page[i], 0, PAGE_CACHE_SIZE);
48 * Create a "page actor" which will kmap and kunmap the
49 * page cache pages appropriately within the decompressor
51 static struct squashfs_page_actor *actor_from_page_cache(
52 unsigned int actor_pages, struct page *target_page,
53 struct list_head *rpages, unsigned int *nr_pages, int start_index,
54 struct address_space *mapping)
57 struct squashfs_page_actor *actor;
59 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
61 page = kmalloc_array(actor_pages, sizeof(void *), GFP_KERNEL);
65 for (i = 0, n = start_index; i < actor_pages; i++, n++) {
66 if (target_page == NULL && rpages && !list_empty(rpages)) {
67 struct page *cur_page = lru_to_page(rpages);
69 if (cur_page->index < start_index + actor_pages) {
70 list_del(&cur_page->lru);
72 if (add_to_page_cache_lru(cur_page, mapping,
73 cur_page->index, gfp))
76 target_page = cur_page;
81 if (target_page && target_page->index == n) {
82 page[i] = target_page;
85 page[i] = grab_cache_page_nowait(mapping, n);
90 if (PageUptodate(page[i])) {
97 actor = squashfs_page_actor_init(page, actor_pages, 0,
100 release_actor_pages(page, actor_pages, -ENOMEM);
107 int squashfs_readpages_block(struct page *target_page,
108 struct list_head *readahead_pages,
109 unsigned int *nr_pages,
110 struct address_space *mapping,
111 int page_index, u64 block, int bsize)
114 struct squashfs_page_actor *actor;
115 struct inode *inode = mapping->host;
116 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
117 int start_index, end_index, file_end, actor_pages, res;
118 int mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
121 * If readpage() is called on an uncompressed datablock, we can just
122 * read the pages instead of fetching the whole block.
123 * This greatly improves the performance when a process keep doing
124 * random reads because we only fetch the necessary data.
125 * The readahead algorithm will take care of doing speculative reads
127 * We can't read more than 1 block even if readahead provides use more
128 * pages because we don't know yet if the next block is compressed or
131 if (bsize && !SQUASHFS_COMPRESSED_BLOCK(bsize)) {
132 u64 block_end = block + msblk->block_size;
134 block += (page_index & mask) * PAGE_CACHE_SIZE;
135 actor_pages = (block_end - block) / PAGE_CACHE_SIZE;
136 if (*nr_pages < actor_pages)
137 actor_pages = *nr_pages;
138 start_index = page_index;
139 bsize = min_t(int, bsize, (PAGE_CACHE_SIZE * actor_pages)
140 | SQUASHFS_COMPRESSED_BIT_BLOCK);
142 file_end = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
143 start_index = page_index & ~mask;
144 end_index = start_index | mask;
145 if (end_index > file_end)
146 end_index = file_end;
147 actor_pages = end_index - start_index + 1;
150 actor = actor_from_page_cache(actor_pages, target_page,
151 readahead_pages, nr_pages, start_index,
156 res = squashfs_read_data_async(inode->i_sb, block, bsize, NULL,
158 return res < 0 ? res : 0;