2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@lougher.demon.co.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 isssues with vmalloc the cache
33 * uses sequences of kmalloced PAGE_CACHE_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"
61 * Look-up block in cache, and increment usage count. If not in cache, read
62 * and decompress it from disk.
64 struct squashfs_cache_entry
*squashfs_cache_get(struct super_block
*sb
,
65 struct squashfs_cache
*cache
, u64 block
, int length
)
68 struct squashfs_cache_entry
*entry
;
70 spin_lock(&cache
->lock
);
73 for (i
= 0; i
< cache
->entries
; i
++)
74 if (cache
->entry
[i
].block
== block
)
77 if (i
== cache
->entries
) {
79 * Block not in cache, if all cache entries are used
80 * go to sleep waiting for one to become available.
82 if (cache
->unused
== 0) {
84 spin_unlock(&cache
->lock
);
85 wait_event(cache
->wait_queue
, cache
->unused
);
86 spin_lock(&cache
->lock
);
92 * At least one unused cache entry. A simple
93 * round-robin strategy is used to choose the entry to
94 * be evicted from the cache.
97 for (n
= 0; n
< cache
->entries
; n
++) {
98 if (cache
->entry
[i
].refcount
== 0)
100 i
= (i
+ 1) % cache
->entries
;
103 cache
->next_blk
= (i
+ 1) % cache
->entries
;
104 entry
= &cache
->entry
[i
];
107 * Initialise chosen cache entry, and fill it in from
111 entry
->block
= block
;
114 entry
->num_waiters
= 0;
116 spin_unlock(&cache
->lock
);
118 entry
->length
= squashfs_read_data(sb
, entry
->data
,
119 block
, length
, &entry
->next_index
,
120 cache
->block_size
, cache
->pages
);
122 spin_lock(&cache
->lock
);
124 if (entry
->length
< 0)
125 entry
->error
= entry
->length
;
130 * While filling this entry one or more other processes
131 * have looked it up in the cache, and have slept
132 * waiting for it to become available.
134 if (entry
->num_waiters
) {
135 spin_unlock(&cache
->lock
);
136 wake_up_all(&entry
->wait_queue
);
138 spin_unlock(&cache
->lock
);
144 * Block already in cache. Increment refcount so it doesn't
145 * get reused until we're finished with it, if it was
146 * previously unused there's one less cache entry available
149 entry
= &cache
->entry
[i
];
150 if (entry
->refcount
== 0)
155 * If the entry is currently being filled in by another process
156 * go to sleep waiting for it to become available.
158 if (entry
->pending
) {
159 entry
->num_waiters
++;
160 spin_unlock(&cache
->lock
);
161 wait_event(entry
->wait_queue
, !entry
->pending
);
163 spin_unlock(&cache
->lock
);
169 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
170 cache
->name
, i
, entry
->block
, entry
->refcount
, entry
->error
);
173 ERROR("Unable to read %s cache entry [%llx]\n", cache
->name
,
180 * Release cache entry, once usage count is zero it can be reused.
182 void squashfs_cache_put(struct squashfs_cache_entry
*entry
)
184 struct squashfs_cache
*cache
= entry
->cache
;
186 spin_lock(&cache
->lock
);
188 if (entry
->refcount
== 0) {
191 * If there's any processes waiting for a block to become
192 * available, wake one up.
194 if (cache
->num_waiters
) {
195 spin_unlock(&cache
->lock
);
196 wake_up(&cache
->wait_queue
);
200 spin_unlock(&cache
->lock
);
204 * Delete cache reclaiming all kmalloced buffers.
206 void squashfs_cache_delete(struct squashfs_cache
*cache
)
213 for (i
= 0; i
< cache
->entries
; i
++) {
214 if (cache
->entry
[i
].data
) {
215 for (j
= 0; j
< cache
->pages
; j
++)
216 kfree(cache
->entry
[i
].data
[j
]);
217 kfree(cache
->entry
[i
].data
);
227 * Initialise cache allocating the specified number of entries, each of
228 * size block_size. To avoid vmalloc fragmentation issues each entry
229 * is allocated as a sequence of kmalloced PAGE_CACHE_SIZE buffers.
231 struct squashfs_cache
*squashfs_cache_init(char *name
, int entries
,
235 struct squashfs_cache
*cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
238 ERROR("Failed to allocate %s cache\n", name
);
242 cache
->entry
= kcalloc(entries
, sizeof(*(cache
->entry
)), GFP_KERNEL
);
243 if (cache
->entry
== NULL
) {
244 ERROR("Failed to allocate %s cache\n", name
);
249 cache
->unused
= entries
;
250 cache
->entries
= entries
;
251 cache
->block_size
= block_size
;
252 cache
->pages
= block_size
>> PAGE_CACHE_SHIFT
;
253 cache
->pages
= cache
->pages
? cache
->pages
: 1;
255 cache
->num_waiters
= 0;
256 spin_lock_init(&cache
->lock
);
257 init_waitqueue_head(&cache
->wait_queue
);
259 for (i
= 0; i
< entries
; i
++) {
260 struct squashfs_cache_entry
*entry
= &cache
->entry
[i
];
262 init_waitqueue_head(&cache
->entry
[i
].wait_queue
);
263 entry
->cache
= cache
;
264 entry
->block
= SQUASHFS_INVALID_BLK
;
265 entry
->data
= kcalloc(cache
->pages
, sizeof(void *), GFP_KERNEL
);
266 if (entry
->data
== NULL
) {
267 ERROR("Failed to allocate %s cache entry\n", name
);
271 for (j
= 0; j
< cache
->pages
; j
++) {
272 entry
->data
[j
] = kmalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
273 if (entry
->data
[j
] == NULL
) {
274 ERROR("Failed to allocate %s buffer\n", name
);
283 squashfs_cache_delete(cache
);
289 * Copy up to length bytes from cache entry to buffer starting at offset bytes
290 * into the cache entry. If there's not length bytes then copy the number of
291 * bytes available. In all cases return the number of bytes copied.
293 int squashfs_copy_data(void *buffer
, struct squashfs_cache_entry
*entry
,
294 int offset
, int length
)
296 int remaining
= length
;
300 else if (buffer
== NULL
)
301 return min(length
, entry
->length
- offset
);
303 while (offset
< entry
->length
) {
304 void *buff
= entry
->data
[offset
/ PAGE_CACHE_SIZE
]
305 + (offset
% PAGE_CACHE_SIZE
);
306 int bytes
= min_t(int, entry
->length
- offset
,
307 PAGE_CACHE_SIZE
- (offset
% PAGE_CACHE_SIZE
));
309 if (bytes
>= remaining
) {
310 memcpy(buffer
, buff
, remaining
);
315 memcpy(buffer
, buff
, bytes
);
321 return length
- remaining
;
326 * Read length bytes from metadata position <block, offset> (block is the
327 * start of the compressed block on disk, and offset is the offset into
328 * the block once decompressed). Data is packed into consecutive blocks,
329 * and length bytes may require reading more than one block.
331 int squashfs_read_metadata(struct super_block
*sb
, void *buffer
,
332 u64
*block
, int *offset
, int length
)
334 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
335 int bytes
, copied
= length
;
336 struct squashfs_cache_entry
*entry
;
338 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block
, *offset
);
341 entry
= squashfs_cache_get(sb
, msblk
->block_cache
, *block
, 0);
344 else if (*offset
>= entry
->length
)
347 bytes
= squashfs_copy_data(buffer
, entry
, *offset
, length
);
353 if (*offset
== entry
->length
) {
354 *block
= entry
->next_index
;
358 squashfs_cache_put(entry
);
366 * Look-up in the fragmment cache the fragment located at <start_block> in the
367 * filesystem. If necessary read and decompress it from disk.
369 struct squashfs_cache_entry
*squashfs_get_fragment(struct super_block
*sb
,
370 u64 start_block
, int length
)
372 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
374 return squashfs_cache_get(sb
, msblk
->fragment_cache
, start_block
,
380 * Read and decompress the datablock located at <start_block> in the
381 * filesystem. The cache is used here to avoid duplicating locking and
382 * read/decompress code.
384 struct squashfs_cache_entry
*squashfs_get_datablock(struct super_block
*sb
,
385 u64 start_block
, int length
)
387 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
389 return squashfs_cache_get(sb
, msblk
->read_page
, start_block
, length
);
394 * Read a filesystem table (uncompressed sequence of bytes) from disk
396 int squashfs_read_table(struct super_block
*sb
, void *buffer
, u64 block
,
399 int pages
= (length
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
401 void **data
= kcalloc(pages
, sizeof(void *), GFP_KERNEL
);
405 for (i
= 0; i
< pages
; i
++, buffer
+= PAGE_CACHE_SIZE
)
407 res
= squashfs_read_data(sb
, data
, block
, length
|
408 SQUASHFS_COMPRESSED_BIT_BLOCK
, NULL
, length
, pages
);