blob: dc87f77ce11e4a08cb6423b06e79d450a6cafacf [file] [log] [blame]
Phillip Lougher0d455c12013-11-13 02:04:19 +00001/*
2 * Copyright (c) 2013
3 * Phillip Lougher <phillip@squashfs.org.uk>
4 *
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
7 */
8
9#include <linux/fs.h>
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>
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070016#include <linux/mm_inline.h>
Phillip Lougher0d455c12013-11-13 02:04:19 +000017
18#include "squashfs_fs.h"
19#include "squashfs_fs_sb.h"
20#include "squashfs_fs_i.h"
21#include "squashfs.h"
22#include "page_actor.h"
23
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070024static void release_actor_pages(struct page **page, int pages, int error)
25{
26 int i;
27
28 for (i = 0; i < pages; i++) {
29 if (!page[i])
30 continue;
31 flush_dcache_page(page[i]);
32 if (!error)
33 SetPageUptodate(page[i]);
34 else {
35 SetPageError(page[i]);
36 zero_user_segment(page[i], 0, PAGE_SIZE);
37 }
38 unlock_page(page[i]);
39 put_page(page[i]);
40 }
41 kfree(page);
42}
43
44/*
45 * Create a "page actor" which will kmap and kunmap the
46 * page cache pages appropriately within the decompressor
47 */
48static struct squashfs_page_actor *actor_from_page_cache(
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070049 unsigned int actor_pages, struct page *target_page,
50 struct list_head *rpages, unsigned int *nr_pages, int start_index,
51 struct address_space *mapping)
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070052{
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070053 struct page **page;
54 struct squashfs_page_actor *actor;
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070055 int i, n;
56 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070057
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070058 page = kmalloc_array(actor_pages, sizeof(void *), GFP_KERNEL);
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070059 if (!page)
60 return NULL;
61
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070062 for (i = 0, n = start_index; i < actor_pages; i++, n++) {
63 if (target_page == NULL && rpages && !list_empty(rpages)) {
64 struct page *cur_page = lru_to_page(rpages);
65
66 if (cur_page->index < start_index + actor_pages) {
67 list_del(&cur_page->lru);
68 --(*nr_pages);
69 if (add_to_page_cache_lru(cur_page, mapping,
70 cur_page->index, gfp))
71 put_page(cur_page);
72 else
73 target_page = cur_page;
74 } else
75 rpages = NULL;
76 }
77
78 if (target_page && target_page->index == n) {
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070079 page[i] = target_page;
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070080 target_page = NULL;
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070081 } else {
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070082 page[i] = grab_cache_page_nowait(mapping, n);
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070083 if (page[i] == NULL)
84 continue;
85 }
86
87 if (PageUptodate(page[i])) {
88 unlock_page(page[i]);
89 put_page(page[i]);
90 page[i] = NULL;
91 }
92 }
93
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070094 actor = squashfs_page_actor_init(page, actor_pages, 0,
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070095 release_actor_pages);
96 if (!actor) {
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070097 release_actor_pages(page, actor_pages, -ENOMEM);
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -070098 kfree(page);
99 return NULL;
100 }
101 return actor;
102}
Phillip Lougher0d455c12013-11-13 02:04:19 +0000103
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700104int squashfs_readpages_block(struct page *target_page,
105 struct list_head *readahead_pages,
106 unsigned int *nr_pages,
107 struct address_space *mapping,
108 int page_index, u64 block, int bsize)
Phillip Lougher0d455c12013-11-13 02:04:19 +0000109
110{
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700111 struct squashfs_page_actor *actor;
112 struct inode *inode = mapping->host;
Phillip Lougher0d455c12013-11-13 02:04:19 +0000113 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
Adrien Schildknecht60cc09a2016-09-29 15:25:30 -0700114 int start_index, end_index, file_end, actor_pages, res;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300115 int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
Phillip Lougher0d455c12013-11-13 02:04:19 +0000116
Adrien Schildknecht60cc09a2016-09-29 15:25:30 -0700117 /*
118 * If readpage() is called on an uncompressed datablock, we can just
119 * read the pages instead of fetching the whole block.
120 * This greatly improves the performance when a process keep doing
121 * random reads because we only fetch the necessary data.
122 * The readahead algorithm will take care of doing speculative reads
123 * if necessary.
124 * We can't read more than 1 block even if readahead provides use more
125 * pages because we don't know yet if the next block is compressed or
126 * not.
127 */
128 if (bsize && !SQUASHFS_COMPRESSED_BLOCK(bsize)) {
129 u64 block_end = block + msblk->block_size;
130
131 block += (page_index & mask) * PAGE_SIZE;
132 actor_pages = (block_end - block) / PAGE_SIZE;
133 if (*nr_pages < actor_pages)
134 actor_pages = *nr_pages;
135 start_index = page_index;
136 bsize = min_t(int, bsize, (PAGE_SIZE * actor_pages)
137 | SQUASHFS_COMPRESSED_BIT_BLOCK);
138 } else {
139 file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
140 start_index = page_index & ~mask;
141 end_index = start_index | mask;
142 if (end_index > file_end)
143 end_index = file_end;
144 actor_pages = end_index - start_index + 1;
145 }
Phillip Lougher0d455c12013-11-13 02:04:19 +0000146
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700147 actor = actor_from_page_cache(actor_pages, target_page,
148 readahead_pages, nr_pages, start_index,
149 mapping);
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -0700150 if (!actor)
151 return -ENOMEM;
Phillip Lougher0d455c12013-11-13 02:04:19 +0000152
Adrien Schildknecht6c8928f2016-10-17 18:22:32 -0700153 res = squashfs_read_data_async(inode->i_sb, block, bsize, NULL,
154 actor);
155 return res < 0 ? res : 0;
Phillip Lougher0d455c12013-11-13 02:04:19 +0000156}