• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
16 
17 #include "squashfs_fs.h"
18 #include "squashfs_fs_sb.h"
19 #include "squashfs_fs_i.h"
20 #include "squashfs.h"
21 #include "page_actor.h"
22 
23 // Backported from 4.5
24 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
25 
release_actor_pages(struct page ** page,int pages,int error)26 static void release_actor_pages(struct page **page, int pages, int error)
27 {
28 	int i;
29 
30 	for (i = 0; i < pages; i++) {
31 		if (!page[i])
32 			continue;
33 		flush_dcache_page(page[i]);
34 		if (!error)
35 			SetPageUptodate(page[i]);
36 		else {
37 			SetPageError(page[i]);
38 			zero_user_segment(page[i], 0, PAGE_CACHE_SIZE);
39 		}
40 		unlock_page(page[i]);
41 		put_page(page[i]);
42 	}
43 	kfree(page);
44 }
45 
46 /*
47  * Create a "page actor" which will kmap and kunmap the
48  * page cache pages appropriately within the decompressor
49  */
actor_from_page_cache(unsigned int actor_pages,struct page * target_page,struct list_head * rpages,unsigned int * nr_pages,int start_index,struct address_space * mapping)50 static struct squashfs_page_actor *actor_from_page_cache(
51 	unsigned int actor_pages, struct page *target_page,
52 	struct list_head *rpages, unsigned int *nr_pages, int start_index,
53 	struct address_space *mapping)
54 {
55 	struct page **page;
56 	struct squashfs_page_actor *actor;
57 	int i, n;
58 	gfp_t gfp = GFP_KERNEL;
59 
60 	page = kmalloc_array(actor_pages, sizeof(void *), GFP_KERNEL);
61 	if (!page)
62 		return NULL;
63 
64 	for (i = 0, n = start_index; i < actor_pages; i++, n++) {
65 		if (target_page == NULL && rpages && !list_empty(rpages)) {
66 			struct page *cur_page = lru_to_page(rpages);
67 
68 			if (cur_page->index < start_index + actor_pages) {
69 				list_del(&cur_page->lru);
70 				--(*nr_pages);
71 				if (add_to_page_cache_lru(cur_page, mapping,
72 							  cur_page->index, gfp))
73 					put_page(cur_page);
74 				else
75 					target_page = cur_page;
76 			} else
77 				rpages = NULL;
78 		}
79 
80 		if (target_page && target_page->index == n) {
81 			page[i] = target_page;
82 			target_page = NULL;
83 		} else {
84 			page[i] = grab_cache_page_nowait(mapping, n);
85 			if (page[i] == NULL)
86 				continue;
87 		}
88 
89 		if (PageUptodate(page[i])) {
90 			unlock_page(page[i]);
91 			page_cache_release(page[i]);
92 			page[i] = NULL;
93 		}
94 	}
95 
96 	actor = squashfs_page_actor_init(page, actor_pages, 0,
97 			release_actor_pages);
98 	if (!actor) {
99 		release_actor_pages(page, actor_pages, -ENOMEM);
100 		kfree(page);
101 		return NULL;
102 	}
103 	return actor;
104 }
105 
squashfs_readpages_block(struct page * target_page,struct list_head * readahead_pages,unsigned int * nr_pages,struct address_space * mapping,int page_index,u64 block,int bsize)106 int squashfs_readpages_block(struct page *target_page,
107 			     struct list_head *readahead_pages,
108 			     unsigned int *nr_pages,
109 			     struct address_space *mapping,
110 			     int page_index, u64 block, int bsize)
111 
112 {
113 	struct squashfs_page_actor *actor;
114 	struct inode *inode = mapping->host;
115 	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
116 	int start_index, end_index, file_end, actor_pages, res;
117 	int mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
118 
119 	/*
120 	 * If readpage() is called on an uncompressed datablock, we can just
121 	 * read the pages instead of fetching the whole block.
122 	 * This greatly improves the performance when a process keep doing
123 	 * random reads because we only fetch the necessary data.
124 	 * The readahead algorithm will take care of doing speculative reads
125 	 * if necessary.
126 	 * We can't read more than 1 block even if readahead provides use more
127 	 * pages because we don't know yet if the next block is compressed or
128 	 * not.
129 	 */
130 	if (bsize && !SQUASHFS_COMPRESSED_BLOCK(bsize)) {
131 		u64 block_end = block + msblk->block_size;
132 
133 		block += (page_index & mask) * PAGE_SIZE;
134 		actor_pages = (block_end - block) / PAGE_SIZE;
135 		if (*nr_pages < actor_pages)
136 			actor_pages = *nr_pages;
137 		start_index = page_index;
138 		bsize = min_t(int, bsize, (PAGE_SIZE * actor_pages)
139 					  | SQUASHFS_COMPRESSED_BIT_BLOCK);
140 	} else {
141 		file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
142 		start_index = page_index & ~mask;
143 		end_index = start_index | mask;
144 		if (end_index > file_end)
145 			end_index = file_end;
146 		actor_pages = end_index - start_index + 1;
147 	}
148 
149 	actor = actor_from_page_cache(actor_pages, target_page,
150 				      readahead_pages, nr_pages, start_index,
151 				      mapping);
152 	if (!actor)
153 		return -ENOMEM;
154 
155 	res = squashfs_read_data_async(inode->i_sb, block, bsize, NULL,
156 				       actor);
157 	return res < 0 ? res : 0;
158 }
159