blob: 05e42441d1065df13838c0fc4af5db085e4c1570 [file] [log] [blame]
Phillip Lougherf400e122009-01-05 08:46:26 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01005 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougherf400e122009-01-05 08:46:26 +00006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * cache.c
22 */
23
24/*
25 * Blocks in Squashfs are compressed. To avoid repeatedly decompressing
26 * recently accessed data Squashfs uses two small metadata and fragment caches.
27 *
28 * This file implements a generic cache implementation used for both caches,
29 * plus functions layered ontop of the generic cache implementation to
30 * access the metadata and fragment caches.
31 *
Justin P. Mattock70f23fd2011-05-10 10:16:21 +020032 * To avoid out of memory and fragmentation issues with vmalloc the cache
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030033 * uses sequences of kmalloced PAGE_SIZE buffers.
Phillip Lougherf400e122009-01-05 08:46:26 +000034 *
35 * It should be noted that the cache is not used for file datablocks, these
36 * are decompressed and cached in the page-cache in the normal way. The
37 * cache is only used to temporarily cache fragment and metadata blocks
38 * which have been read as as a result of a metadata (i.e. inode or
39 * directory) or fragment access. Because metadata and fragments are packed
40 * together into blocks (to gain greater compression) the read of a particular
41 * piece of metadata or fragment will retrieve other metadata/fragments which
42 * have been packed with it, these because of locality-of-reference may be read
43 * in the near future. Temporarily caching them ensures they are available for
44 * near future access without requiring an additional read and decompress.
45 */
46
47#include <linux/fs.h>
48#include <linux/vfs.h>
49#include <linux/slab.h>
50#include <linux/vmalloc.h>
51#include <linux/sched.h>
52#include <linux/spinlock.h>
53#include <linux/wait.h>
Phillip Lougherf400e122009-01-05 08:46:26 +000054#include <linux/pagemap.h>
55
56#include "squashfs_fs.h"
57#include "squashfs_fs_sb.h"
Phillip Lougherf400e122009-01-05 08:46:26 +000058#include "squashfs.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000059#include "page_actor.h"
Phillip Lougherf400e122009-01-05 08:46:26 +000060
61/*
62 * Look-up block in cache, and increment usage count. If not in cache, read
63 * and decompress it from disk.
64 */
65struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
66 struct squashfs_cache *cache, u64 block, int length)
67{
68 int i, n;
69 struct squashfs_cache_entry *entry;
70
71 spin_lock(&cache->lock);
72
73 while (1) {
Ajeet Yadavd7fbd892011-12-27 15:10:04 +053074 for (i = cache->curr_blk, n = 0; n < cache->entries; n++) {
75 if (cache->entry[i].block == block) {
76 cache->curr_blk = i;
Phillip Lougherf400e122009-01-05 08:46:26 +000077 break;
Ajeet Yadavd7fbd892011-12-27 15:10:04 +053078 }
79 i = (i + 1) % cache->entries;
80 }
Phillip Lougherf400e122009-01-05 08:46:26 +000081
Ajeet Yadavd7fbd892011-12-27 15:10:04 +053082 if (n == cache->entries) {
Phillip Lougherf400e122009-01-05 08:46:26 +000083 /*
84 * Block not in cache, if all cache entries are used
85 * go to sleep waiting for one to become available.
86 */
87 if (cache->unused == 0) {
88 cache->num_waiters++;
89 spin_unlock(&cache->lock);
90 wait_event(cache->wait_queue, cache->unused);
91 spin_lock(&cache->lock);
92 cache->num_waiters--;
93 continue;
94 }
95
96 /*
97 * At least one unused cache entry. A simple
98 * round-robin strategy is used to choose the entry to
99 * be evicted from the cache.
100 */
101 i = cache->next_blk;
102 for (n = 0; n < cache->entries; n++) {
103 if (cache->entry[i].refcount == 0)
104 break;
105 i = (i + 1) % cache->entries;
106 }
107
108 cache->next_blk = (i + 1) % cache->entries;
109 entry = &cache->entry[i];
110
111 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300112 * Initialise chosen cache entry, and fill it in from
Phillip Lougherf400e122009-01-05 08:46:26 +0000113 * disk.
114 */
115 cache->unused--;
116 entry->block = block;
117 entry->refcount = 1;
118 entry->pending = 1;
119 entry->num_waiters = 0;
120 entry->error = 0;
121 spin_unlock(&cache->lock);
122
Phillip Lougher846b7302013-11-18 02:59:12 +0000123 entry->length = squashfs_read_data(sb, block, length,
124 &entry->next_index, entry->actor);
Phillip Lougherf400e122009-01-05 08:46:26 +0000125
126 spin_lock(&cache->lock);
127
128 if (entry->length < 0)
129 entry->error = entry->length;
130
131 entry->pending = 0;
132
133 /*
134 * While filling this entry one or more other processes
135 * have looked it up in the cache, and have slept
136 * waiting for it to become available.
137 */
138 if (entry->num_waiters) {
139 spin_unlock(&cache->lock);
140 wake_up_all(&entry->wait_queue);
141 } else
142 spin_unlock(&cache->lock);
143
144 goto out;
145 }
146
147 /*
148 * Block already in cache. Increment refcount so it doesn't
149 * get reused until we're finished with it, if it was
150 * previously unused there's one less cache entry available
151 * for reuse.
152 */
153 entry = &cache->entry[i];
154 if (entry->refcount == 0)
155 cache->unused--;
156 entry->refcount++;
157
158 /*
159 * If the entry is currently being filled in by another process
160 * go to sleep waiting for it to become available.
161 */
162 if (entry->pending) {
163 entry->num_waiters++;
164 spin_unlock(&cache->lock);
165 wait_event(entry->wait_queue, !entry->pending);
166 } else
167 spin_unlock(&cache->lock);
168
169 goto out;
170 }
171
172out:
173 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
174 cache->name, i, entry->block, entry->refcount, entry->error);
175
176 if (entry->error)
177 ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
178 block);
179 return entry;
180}
181
182
183/*
184 * Release cache entry, once usage count is zero it can be reused.
185 */
186void squashfs_cache_put(struct squashfs_cache_entry *entry)
187{
188 struct squashfs_cache *cache = entry->cache;
189
190 spin_lock(&cache->lock);
191 entry->refcount--;
192 if (entry->refcount == 0) {
193 cache->unused++;
194 /*
195 * If there's any processes waiting for a block to become
196 * available, wake one up.
197 */
198 if (cache->num_waiters) {
199 spin_unlock(&cache->lock);
200 wake_up(&cache->wait_queue);
201 return;
202 }
203 }
204 spin_unlock(&cache->lock);
205}
206
207/*
208 * Delete cache reclaiming all kmalloced buffers.
209 */
210void squashfs_cache_delete(struct squashfs_cache *cache)
211{
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700212 int i;
Phillip Lougherf400e122009-01-05 08:46:26 +0000213
214 if (cache == NULL)
215 return;
216
217 for (i = 0; i < cache->entries; i++) {
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700218 if (cache->entry[i].page)
219 free_page_array(cache->entry[i].page, cache->pages);
Phillip Lougher846b7302013-11-18 02:59:12 +0000220 kfree(cache->entry[i].actor);
Phillip Lougherf400e122009-01-05 08:46:26 +0000221 }
222
223 kfree(cache->entry);
224 kfree(cache);
225}
226
227
228/*
229 * Initialise cache allocating the specified number of entries, each of
230 * size block_size. To avoid vmalloc fragmentation issues each entry
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300231 * is allocated as a sequence of kmalloced PAGE_SIZE buffers.
Phillip Lougherf400e122009-01-05 08:46:26 +0000232 */
233struct squashfs_cache *squashfs_cache_init(char *name, int entries,
234 int block_size)
235{
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700236 int i;
Phillip Lougherf400e122009-01-05 08:46:26 +0000237 struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL);
238
239 if (cache == NULL) {
240 ERROR("Failed to allocate %s cache\n", name);
241 return NULL;
242 }
243
244 cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL);
245 if (cache->entry == NULL) {
246 ERROR("Failed to allocate %s cache\n", name);
247 goto cleanup;
248 }
249
Ajeet Yadavd7fbd892011-12-27 15:10:04 +0530250 cache->curr_blk = 0;
Phillip Lougherf400e122009-01-05 08:46:26 +0000251 cache->next_blk = 0;
252 cache->unused = entries;
253 cache->entries = entries;
254 cache->block_size = block_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 cache->pages = block_size >> PAGE_SHIFT;
Doug Chapmana37b06d2009-05-13 02:56:39 +0100256 cache->pages = cache->pages ? cache->pages : 1;
Phillip Lougherf400e122009-01-05 08:46:26 +0000257 cache->name = name;
258 cache->num_waiters = 0;
259 spin_lock_init(&cache->lock);
260 init_waitqueue_head(&cache->wait_queue);
261
262 for (i = 0; i < entries; i++) {
263 struct squashfs_cache_entry *entry = &cache->entry[i];
264
265 init_waitqueue_head(&cache->entry[i].wait_queue);
266 entry->cache = cache;
267 entry->block = SQUASHFS_INVALID_BLK;
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700268 entry->page = alloc_page_array(cache->pages, GFP_KERNEL);
269 if (!entry->page) {
Phillip Lougherf400e122009-01-05 08:46:26 +0000270 ERROR("Failed to allocate %s cache entry\n", name);
271 goto cleanup;
272 }
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700273 entry->actor = squashfs_page_actor_init(entry->page,
274 cache->pages, 0, NULL);
Phillip Lougher846b7302013-11-18 02:59:12 +0000275 if (entry->actor == NULL) {
276 ERROR("Failed to allocate %s cache entry\n", name);
277 goto cleanup;
278 }
Phillip Lougherf400e122009-01-05 08:46:26 +0000279 }
280
281 return cache;
282
283cleanup:
284 squashfs_cache_delete(cache);
285 return NULL;
286}
287
288
289/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300290 * Copy up to length bytes from cache entry to buffer starting at offset bytes
Phillip Lougherf400e122009-01-05 08:46:26 +0000291 * into the cache entry. If there's not length bytes then copy the number of
292 * bytes available. In all cases return the number of bytes copied.
293 */
294int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry,
295 int offset, int length)
296{
297 int remaining = length;
298
299 if (length == 0)
300 return 0;
301 else if (buffer == NULL)
302 return min(length, entry->length - offset);
303
304 while (offset < entry->length) {
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700305 void *buff = kmap_atomic(entry->page[offset / PAGE_SIZE])
306 + (offset % PAGE_SIZE);
Phillip Lougherf400e122009-01-05 08:46:26 +0000307 int bytes = min_t(int, entry->length - offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300308 PAGE_SIZE - (offset % PAGE_SIZE));
Phillip Lougherf400e122009-01-05 08:46:26 +0000309
310 if (bytes >= remaining) {
311 memcpy(buffer, buff, remaining);
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700312 kunmap_atomic(buff);
Phillip Lougherf400e122009-01-05 08:46:26 +0000313 remaining = 0;
314 break;
315 }
316
317 memcpy(buffer, buff, bytes);
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700318 kunmap_atomic(buff);
Phillip Lougherf400e122009-01-05 08:46:26 +0000319 buffer += bytes;
320 remaining -= bytes;
321 offset += bytes;
322 }
323
324 return length - remaining;
325}
326
327
328/*
329 * Read length bytes from metadata position <block, offset> (block is the
330 * start of the compressed block on disk, and offset is the offset into
331 * the block once decompressed). Data is packed into consecutive blocks,
332 * and length bytes may require reading more than one block.
333 */
334int squashfs_read_metadata(struct super_block *sb, void *buffer,
335 u64 *block, int *offset, int length)
336{
337 struct squashfs_sb_info *msblk = sb->s_fs_info;
Phillip Loughere552a592011-12-29 03:50:20 +0000338 int bytes, res = length;
Phillip Lougherf400e122009-01-05 08:46:26 +0000339 struct squashfs_cache_entry *entry;
340
341 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
342
343 while (length) {
344 entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
Phillip Loughere552a592011-12-29 03:50:20 +0000345 if (entry->error) {
346 res = entry->error;
347 goto error;
348 } else if (*offset >= entry->length) {
349 res = -EIO;
350 goto error;
351 }
Phillip Lougherf400e122009-01-05 08:46:26 +0000352
353 bytes = squashfs_copy_data(buffer, entry, *offset, length);
354 if (buffer)
355 buffer += bytes;
356 length -= bytes;
357 *offset += bytes;
358
359 if (*offset == entry->length) {
360 *block = entry->next_index;
361 *offset = 0;
362 }
363
364 squashfs_cache_put(entry);
365 }
366
Phillip Loughere552a592011-12-29 03:50:20 +0000367 return res;
368
369error:
370 squashfs_cache_put(entry);
371 return res;
Phillip Lougherf400e122009-01-05 08:46:26 +0000372}
373
374
375/*
376 * Look-up in the fragmment cache the fragment located at <start_block> in the
377 * filesystem. If necessary read and decompress it from disk.
378 */
379struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
380 u64 start_block, int length)
381{
382 struct squashfs_sb_info *msblk = sb->s_fs_info;
383
384 return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
385 length);
386}
387
388
389/*
390 * Read and decompress the datablock located at <start_block> in the
391 * filesystem. The cache is used here to avoid duplicating locking and
392 * read/decompress code.
393 */
394struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
395 u64 start_block, int length)
396{
397 struct squashfs_sb_info *msblk = sb->s_fs_info;
398
399 return squashfs_cache_get(sb, msblk->read_page, start_block, length);
400}
401
402
403/*
404 * Read a filesystem table (uncompressed sequence of bytes) from disk
405 */
Phillip Lougher82de6472011-05-20 02:26:43 +0100406void *squashfs_read_table(struct super_block *sb, u64 block, int length)
Phillip Lougherf400e122009-01-05 08:46:26 +0000407{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300408 int pages = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700409 struct page **page;
410 void *buff;
411 int res;
Phillip Lougher846b7302013-11-18 02:59:12 +0000412 struct squashfs_page_actor *actor;
Phillip Lougher82de6472011-05-20 02:26:43 +0100413
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700414 page = alloc_page_array(pages, GFP_KERNEL);
415 if (!page)
Phillip Lougher82de6472011-05-20 02:26:43 +0100416 return ERR_PTR(-ENOMEM);
417
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700418 actor = squashfs_page_actor_init(page, pages, length, NULL);
419 if (actor == NULL) {
Phillip Lougher82de6472011-05-20 02:26:43 +0100420 res = -ENOMEM;
421 goto failed;
422 }
Phillip Lougherf400e122009-01-05 08:46:26 +0000423
Phillip Lougher846b7302013-11-18 02:59:12 +0000424 res = squashfs_read_data(sb, block, length |
425 SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);
Phillip Lougher82de6472011-05-20 02:26:43 +0100426
Phillip Lougher82de6472011-05-20 02:26:43 +0100427 if (res < 0)
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700428 goto failed2;
Phillip Lougher82de6472011-05-20 02:26:43 +0100429
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700430 buff = kmalloc(length, GFP_KERNEL);
431 if (!buff)
432 goto failed2;
433 squashfs_actor_to_buf(actor, buff, length);
434 squashfs_page_actor_free(actor, 0);
435 free_page_array(page, pages);
436 return buff;
Phillip Lougher82de6472011-05-20 02:26:43 +0100437
Phillip Lougher846b7302013-11-18 02:59:12 +0000438failed2:
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700439 squashfs_page_actor_free(actor, 0);
Phillip Lougher82de6472011-05-20 02:26:43 +0100440failed:
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700441 free_page_array(page, pages);
Phillip Lougher82de6472011-05-20 02:26:43 +0100442 return ERR_PTR(res);
Phillip Lougherf400e122009-01-05 08:46:26 +0000443}