1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6 * Phillip Lougher <phillip@squashfs.org.uk>
12 * Blocks in Squashfs are compressed. To avoid repeatedly decompressing
13 * recently accessed data Squashfs uses two small metadata and fragment caches.
15 * This file implements a generic cache implementation used for both caches,
16 * plus functions layered ontop of the generic cache implementation to
17 * access the metadata and fragment caches.
19 * To avoid out of memory and fragmentation issues with vmalloc the cache
20 * uses sequences of kmalloced PAGE_SIZE buffers.
22 * It should be noted that the cache is not used for file datablocks, these
23 * are decompressed and cached in the page-cache in the normal way. The
24 * cache is only used to temporarily cache fragment and metadata blocks
25 * which have been read as as a result of a metadata (i.e. inode or
26 * directory) or fragment access. Because metadata and fragments are packed
27 * together into blocks (to gain greater compression) the read of a particular
28 * piece of metadata or fragment will retrieve other metadata/fragments which
29 * have been packed with it, these because of locality-of-reference may be read
30 * in the near future. Temporarily caching them ensures they are available for
31 * near future access without requiring an additional read and decompress.
35 #include <linux/vfs.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/sched.h>
39 #include <linux/spinlock.h>
40 #include <linux/wait.h>
41 #include <linux/pagemap.h>
43 #include "squashfs_fs.h"
44 #include "squashfs_fs_sb.h"
46 #include "page_actor.h"
49 * Look-up block in cache, and increment usage count. If not in cache, read
50 * and decompress it from disk.
52 struct squashfs_cache_entry
*squashfs_cache_get(struct super_block
*sb
,
53 struct squashfs_cache
*cache
, u64 block
, int length
)
56 struct squashfs_cache_entry
*entry
;
58 spin_lock(&cache
->lock
);
61 for (i
= cache
->curr_blk
, n
= 0; n
< cache
->entries
; n
++) {
62 if (cache
->entry
[i
].block
== block
) {
66 i
= (i
+ 1) % cache
->entries
;
69 if (n
== cache
->entries
) {
71 * Block not in cache, if all cache entries are used
72 * go to sleep waiting for one to become available.
74 if (cache
->unused
== 0) {
76 spin_unlock(&cache
->lock
);
77 wait_event(cache
->wait_queue
, cache
->unused
);
78 spin_lock(&cache
->lock
);
84 * At least one unused cache entry. A simple
85 * round-robin strategy is used to choose the entry to
86 * be evicted from the cache.
89 for (n
= 0; n
< cache
->entries
; n
++) {
90 if (cache
->entry
[i
].refcount
== 0)
92 i
= (i
+ 1) % cache
->entries
;
95 cache
->next_blk
= (i
+ 1) % cache
->entries
;
96 entry
= &cache
->entry
[i
];
99 * Initialise chosen cache entry, and fill it in from
103 entry
->block
= block
;
106 entry
->num_waiters
= 0;
108 spin_unlock(&cache
->lock
);
110 entry
->length
= squashfs_read_data(sb
, block
, length
,
111 &entry
->next_index
, entry
->actor
);
113 spin_lock(&cache
->lock
);
115 if (entry
->length
< 0)
116 entry
->error
= entry
->length
;
121 * While filling this entry one or more other processes
122 * have looked it up in the cache, and have slept
123 * waiting for it to become available.
125 if (entry
->num_waiters
) {
126 spin_unlock(&cache
->lock
);
127 wake_up_all(&entry
->wait_queue
);
129 spin_unlock(&cache
->lock
);
135 * Block already in cache. Increment refcount so it doesn't
136 * get reused until we're finished with it, if it was
137 * previously unused there's one less cache entry available
140 entry
= &cache
->entry
[i
];
141 if (entry
->refcount
== 0)
146 * If the entry is currently being filled in by another process
147 * go to sleep waiting for it to become available.
149 if (entry
->pending
) {
150 entry
->num_waiters
++;
151 spin_unlock(&cache
->lock
);
152 wait_event(entry
->wait_queue
, !entry
->pending
);
154 spin_unlock(&cache
->lock
);
160 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
161 cache
->name
, i
, entry
->block
, entry
->refcount
, entry
->error
);
164 ERROR("Unable to read %s cache entry [%llx]\n", cache
->name
,
171 * Release cache entry, once usage count is zero it can be reused.
173 void squashfs_cache_put(struct squashfs_cache_entry
*entry
)
175 struct squashfs_cache
*cache
= entry
->cache
;
177 spin_lock(&cache
->lock
);
179 if (entry
->refcount
== 0) {
182 * If there's any processes waiting for a block to become
183 * available, wake one up.
185 if (cache
->num_waiters
) {
186 spin_unlock(&cache
->lock
);
187 wake_up(&cache
->wait_queue
);
191 spin_unlock(&cache
->lock
);
195 * Delete cache reclaiming all kmalloced buffers.
197 void squashfs_cache_delete(struct squashfs_cache
*cache
)
204 for (i
= 0; i
< cache
->entries
; i
++) {
205 if (cache
->entry
[i
].data
) {
206 for (j
= 0; j
< cache
->pages
; j
++)
207 kfree(cache
->entry
[i
].data
[j
]);
208 kfree(cache
->entry
[i
].data
);
210 kfree(cache
->entry
[i
].actor
);
219 * Initialise cache allocating the specified number of entries, each of
220 * size block_size. To avoid vmalloc fragmentation issues each entry
221 * is allocated as a sequence of kmalloced PAGE_SIZE buffers.
223 struct squashfs_cache
*squashfs_cache_init(char *name
, int entries
,
227 struct squashfs_cache
*cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
230 ERROR("Failed to allocate %s cache\n", name
);
234 cache
->entry
= kcalloc(entries
, sizeof(*(cache
->entry
)), GFP_KERNEL
);
235 if (cache
->entry
== NULL
) {
236 ERROR("Failed to allocate %s cache\n", name
);
242 cache
->unused
= entries
;
243 cache
->entries
= entries
;
244 cache
->block_size
= block_size
;
245 cache
->pages
= block_size
>> PAGE_SHIFT
;
246 cache
->pages
= cache
->pages
? cache
->pages
: 1;
248 cache
->num_waiters
= 0;
249 spin_lock_init(&cache
->lock
);
250 init_waitqueue_head(&cache
->wait_queue
);
252 for (i
= 0; i
< entries
; i
++) {
253 struct squashfs_cache_entry
*entry
= &cache
->entry
[i
];
255 init_waitqueue_head(&cache
->entry
[i
].wait_queue
);
256 entry
->cache
= cache
;
257 entry
->block
= SQUASHFS_INVALID_BLK
;
258 entry
->data
= kcalloc(cache
->pages
, sizeof(void *), GFP_KERNEL
);
259 if (entry
->data
== NULL
) {
260 ERROR("Failed to allocate %s cache entry\n", name
);
264 for (j
= 0; j
< cache
->pages
; j
++) {
265 entry
->data
[j
] = kmalloc(PAGE_SIZE
, GFP_KERNEL
);
266 if (entry
->data
[j
] == NULL
) {
267 ERROR("Failed to allocate %s buffer\n", name
);
272 entry
->actor
= squashfs_page_actor_init(entry
->data
,
274 if (entry
->actor
== NULL
) {
275 ERROR("Failed to allocate %s cache entry\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_SIZE
]
305 + (offset
% PAGE_SIZE
);
306 int bytes
= min_t(int, entry
->length
- offset
,
307 PAGE_SIZE
- (offset
% PAGE_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
, res
= length
;
336 struct squashfs_cache_entry
*entry
;
338 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block
, *offset
);
340 if (unlikely(length
< 0))
344 entry
= squashfs_cache_get(sb
, msblk
->block_cache
, *block
, 0);
348 } else if (*offset
>= entry
->length
) {
353 bytes
= squashfs_copy_data(buffer
, entry
, *offset
, length
);
359 if (*offset
== entry
->length
) {
360 *block
= entry
->next_index
;
364 squashfs_cache_put(entry
);
370 squashfs_cache_put(entry
);
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.
379 struct squashfs_cache_entry
*squashfs_get_fragment(struct super_block
*sb
,
380 u64 start_block
, int length
)
382 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
384 return squashfs_cache_get(sb
, msblk
->fragment_cache
, start_block
,
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.
394 struct squashfs_cache_entry
*squashfs_get_datablock(struct super_block
*sb
,
395 u64 start_block
, int length
)
397 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
399 return squashfs_cache_get(sb
, msblk
->read_page
, start_block
, length
);
404 * Read a filesystem table (uncompressed sequence of bytes) from disk
406 void *squashfs_read_table(struct super_block
*sb
, u64 block
, int length
)
408 int pages
= (length
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
410 void *table
, *buffer
, **data
;
411 struct squashfs_page_actor
*actor
;
413 table
= buffer
= kmalloc(length
, GFP_KERNEL
);
415 return ERR_PTR(-ENOMEM
);
417 data
= kcalloc(pages
, sizeof(void *), GFP_KERNEL
);
423 actor
= squashfs_page_actor_init(data
, pages
, length
);
429 for (i
= 0; i
< pages
; i
++, buffer
+= PAGE_SIZE
)
432 res
= squashfs_read_data(sb
, block
, length
|
433 SQUASHFS_COMPRESSED_BIT_BLOCK
, NULL
, actor
);