2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.uk>
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.
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.
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.
25 * Blocks in Squashfs are compressed. To avoid repeatedly decompressing
26 * recently accessed data Squashfs uses two small metadata and fragment caches.
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.
32 * To avoid out of memory and fragmentation issues with vmalloc the cache
33 * uses sequences of kmalloced PAGE_SIZE buffers.
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.
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>
54 #include <linux/pagemap.h>
56 #include "squashfs_fs.h"
57 #include "squashfs_fs_sb.h"
59 #include "page_actor.h"
62 * Look-up block in cache, and increment usage count. If not in cache, read
63 * and decompress it from disk.
65 struct squashfs_cache_entry
*squashfs_cache_get(struct super_block
*sb
,
66 struct squashfs_cache
*cache
, u64 block
, int length
)
69 struct squashfs_cache_entry
*entry
;
71 spin_lock(&cache
->lock
);
74 for (i
= cache
->curr_blk
, n
= 0; n
< cache
->entries
; n
++) {
75 if (cache
->entry
[i
].block
== block
) {
79 i
= (i
+ 1) % cache
->entries
;
82 if (n
== cache
->entries
) {
84 * Block not in cache, if all cache entries are used
85 * go to sleep waiting for one to become available.
87 if (cache
->unused
== 0) {
89 spin_unlock(&cache
->lock
);
90 wait_event(cache
->wait_queue
, cache
->unused
);
91 spin_lock(&cache
->lock
);
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.
102 for (n
= 0; n
< cache
->entries
; n
++) {
103 if (cache
->entry
[i
].refcount
== 0)
105 i
= (i
+ 1) % cache
->entries
;
108 cache
->next_blk
= (i
+ 1) % cache
->entries
;
109 entry
= &cache
->entry
[i
];
112 * Initialise chosen cache entry, and fill it in from
116 entry
->block
= block
;
119 entry
->num_waiters
= 0;
121 spin_unlock(&cache
->lock
);
123 entry
->length
= squashfs_read_data(sb
, block
, length
,
124 &entry
->next_index
, entry
->actor
);
126 spin_lock(&cache
->lock
);
128 if (entry
->length
< 0)
129 entry
->error
= entry
->length
;
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.
138 if (entry
->num_waiters
) {
139 spin_unlock(&cache
->lock
);
140 wake_up_all(&entry
->wait_queue
);
142 spin_unlock(&cache
->lock
);
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
153 entry
= &cache
->entry
[i
];
154 if (entry
->refcount
== 0)
159 * If the entry is currently being filled in by another process
160 * go to sleep waiting for it to become available.
162 if (entry
->pending
) {
163 entry
->num_waiters
++;
164 spin_unlock(&cache
->lock
);
165 wait_event(entry
->wait_queue
, !entry
->pending
);
167 spin_unlock(&cache
->lock
);
173 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
174 cache
->name
, i
, entry
->block
, entry
->refcount
, entry
->error
);
177 ERROR("Unable to read %s cache entry [%llx]\n", cache
->name
,
184 * Release cache entry, once usage count is zero it can be reused.
186 void squashfs_cache_put(struct squashfs_cache_entry
*entry
)
188 struct squashfs_cache
*cache
= entry
->cache
;
190 spin_lock(&cache
->lock
);
192 if (entry
->refcount
== 0) {
195 * If there's any processes waiting for a block to become
196 * available, wake one up.
198 if (cache
->num_waiters
) {
199 spin_unlock(&cache
->lock
);
200 wake_up(&cache
->wait_queue
);
204 spin_unlock(&cache
->lock
);
208 * Delete cache reclaiming all kmalloced buffers.
210 void squashfs_cache_delete(struct squashfs_cache
*cache
)
217 for (i
= 0; i
< cache
->entries
; i
++) {
218 if (cache
->entry
[i
].data
) {
219 for (j
= 0; j
< cache
->pages
; j
++)
220 kfree(cache
->entry
[i
].data
[j
]);
221 kfree(cache
->entry
[i
].data
);
223 kfree(cache
->entry
[i
].actor
);
232 * Initialise cache allocating the specified number of entries, each of
233 * size block_size. To avoid vmalloc fragmentation issues each entry
234 * is allocated as a sequence of kmalloced PAGE_SIZE buffers.
236 struct squashfs_cache
*squashfs_cache_init(char *name
, int entries
,
240 struct squashfs_cache
*cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
243 ERROR("Failed to allocate %s cache\n", name
);
247 cache
->entry
= kcalloc(entries
, sizeof(*(cache
->entry
)), GFP_KERNEL
);
248 if (cache
->entry
== NULL
) {
249 ERROR("Failed to allocate %s cache\n", name
);
255 cache
->unused
= entries
;
256 cache
->entries
= entries
;
257 cache
->block_size
= block_size
;
258 cache
->pages
= block_size
>> PAGE_SHIFT
;
259 cache
->pages
= cache
->pages
? cache
->pages
: 1;
261 cache
->num_waiters
= 0;
262 spin_lock_init(&cache
->lock
);
263 init_waitqueue_head(&cache
->wait_queue
);
265 for (i
= 0; i
< entries
; i
++) {
266 struct squashfs_cache_entry
*entry
= &cache
->entry
[i
];
268 init_waitqueue_head(&cache
->entry
[i
].wait_queue
);
269 entry
->cache
= cache
;
270 entry
->block
= SQUASHFS_INVALID_BLK
;
271 entry
->data
= kcalloc(cache
->pages
, sizeof(void *), GFP_KERNEL
);
272 if (entry
->data
== NULL
) {
273 ERROR("Failed to allocate %s cache entry\n", name
);
277 for (j
= 0; j
< cache
->pages
; j
++) {
278 entry
->data
[j
] = kmalloc(PAGE_SIZE
, GFP_KERNEL
);
279 if (entry
->data
[j
] == NULL
) {
280 ERROR("Failed to allocate %s buffer\n", name
);
285 entry
->actor
= squashfs_page_actor_init(entry
->data
,
287 if (entry
->actor
== NULL
) {
288 ERROR("Failed to allocate %s cache entry\n", name
);
296 squashfs_cache_delete(cache
);
302 * Copy up to length bytes from cache entry to buffer starting at offset bytes
303 * into the cache entry. If there's not length bytes then copy the number of
304 * bytes available. In all cases return the number of bytes copied.
306 int squashfs_copy_data(void *buffer
, struct squashfs_cache_entry
*entry
,
307 int offset
, int length
)
309 int remaining
= length
;
313 else if (buffer
== NULL
)
314 return min(length
, entry
->length
- offset
);
316 while (offset
< entry
->length
) {
317 void *buff
= entry
->data
[offset
/ PAGE_SIZE
]
318 + (offset
% PAGE_SIZE
);
319 int bytes
= min_t(int, entry
->length
- offset
,
320 PAGE_SIZE
- (offset
% PAGE_SIZE
));
322 if (bytes
>= remaining
) {
323 memcpy(buffer
, buff
, remaining
);
328 memcpy(buffer
, buff
, bytes
);
334 return length
- remaining
;
339 * Read length bytes from metadata position <block, offset> (block is the
340 * start of the compressed block on disk, and offset is the offset into
341 * the block once decompressed). Data is packed into consecutive blocks,
342 * and length bytes may require reading more than one block.
344 int squashfs_read_metadata(struct super_block
*sb
, void *buffer
,
345 u64
*block
, int *offset
, int length
)
347 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
348 int bytes
, res
= length
;
349 struct squashfs_cache_entry
*entry
;
351 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block
, *offset
);
353 if (unlikely(length
< 0))
357 entry
= squashfs_cache_get(sb
, msblk
->block_cache
, *block
, 0);
361 } else if (*offset
>= entry
->length
) {
366 bytes
= squashfs_copy_data(buffer
, entry
, *offset
, length
);
372 if (*offset
== entry
->length
) {
373 *block
= entry
->next_index
;
377 squashfs_cache_put(entry
);
383 squashfs_cache_put(entry
);
389 * Look-up in the fragmment cache the fragment located at <start_block> in the
390 * filesystem. If necessary read and decompress it from disk.
392 struct squashfs_cache_entry
*squashfs_get_fragment(struct super_block
*sb
,
393 u64 start_block
, int length
)
395 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
397 return squashfs_cache_get(sb
, msblk
->fragment_cache
, start_block
,
403 * Read and decompress the datablock located at <start_block> in the
404 * filesystem. The cache is used here to avoid duplicating locking and
405 * read/decompress code.
407 struct squashfs_cache_entry
*squashfs_get_datablock(struct super_block
*sb
,
408 u64 start_block
, int length
)
410 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
412 return squashfs_cache_get(sb
, msblk
->read_page
, start_block
, length
);
417 * Read a filesystem table (uncompressed sequence of bytes) from disk
419 void *squashfs_read_table(struct super_block
*sb
, u64 block
, int length
)
421 int pages
= (length
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
423 void *table
, *buffer
, **data
;
424 struct squashfs_page_actor
*actor
;
426 table
= buffer
= kmalloc(length
, GFP_KERNEL
);
428 return ERR_PTR(-ENOMEM
);
430 data
= kcalloc(pages
, sizeof(void *), GFP_KERNEL
);
436 actor
= squashfs_page_actor_init(data
, pages
, length
);
442 for (i
= 0; i
< pages
; i
++, buffer
+= PAGE_SIZE
)
445 res
= squashfs_read_data(sb
, block
, length
|
446 SQUASHFS_COMPRESSED_BIT_BLOCK
, NULL
, actor
);