xref: /openbmc/linux/fs/squashfs/cache.c (revision 68252eb5)
168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f400e126SPhillip Lougher /*
3f400e126SPhillip Lougher  * Squashfs - a compressed read only filesystem for Linux
4f400e126SPhillip Lougher  *
5f400e126SPhillip Lougher  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher  * Phillip Lougher <phillip@squashfs.org.uk>
7f400e126SPhillip Lougher  *
8f400e126SPhillip Lougher  * cache.c
9f400e126SPhillip Lougher  */
10f400e126SPhillip Lougher 
11f400e126SPhillip Lougher /*
12f400e126SPhillip Lougher  * Blocks in Squashfs are compressed.  To avoid repeatedly decompressing
13f400e126SPhillip Lougher  * recently accessed data Squashfs uses two small metadata and fragment caches.
14f400e126SPhillip Lougher  *
15f400e126SPhillip Lougher  * This file implements a generic cache implementation used for both caches,
16f400e126SPhillip Lougher  * plus functions layered ontop of the generic cache implementation to
17f400e126SPhillip Lougher  * access the metadata and fragment caches.
18f400e126SPhillip Lougher  *
1970f23fd6SJustin P. Mattock  * To avoid out of memory and fragmentation issues with vmalloc the cache
20ea1754a0SKirill A. Shutemov  * uses sequences of kmalloced PAGE_SIZE buffers.
21f400e126SPhillip Lougher  *
22f400e126SPhillip Lougher  * It should be noted that the cache is not used for file datablocks, these
23f400e126SPhillip Lougher  * are decompressed and cached in the page-cache in the normal way.  The
24f400e126SPhillip Lougher  * cache is only used to temporarily cache fragment and metadata blocks
25f400e126SPhillip Lougher  * which have been read as as a result of a metadata (i.e. inode or
26f400e126SPhillip Lougher  * directory) or fragment access.  Because metadata and fragments are packed
27f400e126SPhillip Lougher  * together into blocks (to gain greater compression) the read of a particular
28f400e126SPhillip Lougher  * piece of metadata or fragment will retrieve other metadata/fragments which
29f400e126SPhillip Lougher  * have been packed with it, these because of locality-of-reference may be read
30f400e126SPhillip Lougher  * in the near future. Temporarily caching them ensures they are available for
31f400e126SPhillip Lougher  * near future access without requiring an additional read and decompress.
32f400e126SPhillip Lougher  */
33f400e126SPhillip Lougher 
34f400e126SPhillip Lougher #include <linux/fs.h>
35f400e126SPhillip Lougher #include <linux/vfs.h>
36f400e126SPhillip Lougher #include <linux/slab.h>
37f400e126SPhillip Lougher #include <linux/vmalloc.h>
38f400e126SPhillip Lougher #include <linux/sched.h>
39f400e126SPhillip Lougher #include <linux/spinlock.h>
40f400e126SPhillip Lougher #include <linux/wait.h>
41f400e126SPhillip Lougher #include <linux/pagemap.h>
42f400e126SPhillip Lougher 
43f400e126SPhillip Lougher #include "squashfs_fs.h"
44f400e126SPhillip Lougher #include "squashfs_fs_sb.h"
45f400e126SPhillip Lougher #include "squashfs.h"
46846b730eSPhillip Lougher #include "page_actor.h"
47f400e126SPhillip Lougher 
48f400e126SPhillip Lougher /*
49f400e126SPhillip Lougher  * Look-up block in cache, and increment usage count.  If not in cache, read
50f400e126SPhillip Lougher  * and decompress it from disk.
51f400e126SPhillip Lougher  */
squashfs_cache_get(struct super_block * sb,struct squashfs_cache * cache,u64 block,int length)52f400e126SPhillip Lougher struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
53f400e126SPhillip Lougher 	struct squashfs_cache *cache, u64 block, int length)
54f400e126SPhillip Lougher {
55f400e126SPhillip Lougher 	int i, n;
56f400e126SPhillip Lougher 	struct squashfs_cache_entry *entry;
57f400e126SPhillip Lougher 
58f400e126SPhillip Lougher 	spin_lock(&cache->lock);
59f400e126SPhillip Lougher 
60f400e126SPhillip Lougher 	while (1) {
61d7fbd893SAjeet Yadav 		for (i = cache->curr_blk, n = 0; n < cache->entries; n++) {
62d7fbd893SAjeet Yadav 			if (cache->entry[i].block == block) {
63d7fbd893SAjeet Yadav 				cache->curr_blk = i;
64f400e126SPhillip Lougher 				break;
65d7fbd893SAjeet Yadav 			}
66d7fbd893SAjeet Yadav 			i = (i + 1) % cache->entries;
67d7fbd893SAjeet Yadav 		}
68f400e126SPhillip Lougher 
69d7fbd893SAjeet Yadav 		if (n == cache->entries) {
70f400e126SPhillip Lougher 			/*
71f400e126SPhillip Lougher 			 * Block not in cache, if all cache entries are used
72f400e126SPhillip Lougher 			 * go to sleep waiting for one to become available.
73f400e126SPhillip Lougher 			 */
74f400e126SPhillip Lougher 			if (cache->unused == 0) {
75f400e126SPhillip Lougher 				cache->num_waiters++;
76f400e126SPhillip Lougher 				spin_unlock(&cache->lock);
77f400e126SPhillip Lougher 				wait_event(cache->wait_queue, cache->unused);
78f400e126SPhillip Lougher 				spin_lock(&cache->lock);
79f400e126SPhillip Lougher 				cache->num_waiters--;
80f400e126SPhillip Lougher 				continue;
81f400e126SPhillip Lougher 			}
82f400e126SPhillip Lougher 
83f400e126SPhillip Lougher 			/*
84f400e126SPhillip Lougher 			 * At least one unused cache entry.  A simple
85f400e126SPhillip Lougher 			 * round-robin strategy is used to choose the entry to
86f400e126SPhillip Lougher 			 * be evicted from the cache.
87f400e126SPhillip Lougher 			 */
88f400e126SPhillip Lougher 			i = cache->next_blk;
89f400e126SPhillip Lougher 			for (n = 0; n < cache->entries; n++) {
90f400e126SPhillip Lougher 				if (cache->entry[i].refcount == 0)
91f400e126SPhillip Lougher 					break;
92f400e126SPhillip Lougher 				i = (i + 1) % cache->entries;
93f400e126SPhillip Lougher 			}
94f400e126SPhillip Lougher 
95f400e126SPhillip Lougher 			cache->next_blk = (i + 1) % cache->entries;
96f400e126SPhillip Lougher 			entry = &cache->entry[i];
97f400e126SPhillip Lougher 
98f400e126SPhillip Lougher 			/*
9925985edcSLucas De Marchi 			 * Initialise chosen cache entry, and fill it in from
100f400e126SPhillip Lougher 			 * disk.
101f400e126SPhillip Lougher 			 */
102f400e126SPhillip Lougher 			cache->unused--;
103f400e126SPhillip Lougher 			entry->block = block;
104f400e126SPhillip Lougher 			entry->refcount = 1;
105f400e126SPhillip Lougher 			entry->pending = 1;
106f400e126SPhillip Lougher 			entry->num_waiters = 0;
107f400e126SPhillip Lougher 			entry->error = 0;
108f400e126SPhillip Lougher 			spin_unlock(&cache->lock);
109f400e126SPhillip Lougher 
110846b730eSPhillip Lougher 			entry->length = squashfs_read_data(sb, block, length,
111846b730eSPhillip Lougher 				&entry->next_index, entry->actor);
112f400e126SPhillip Lougher 
113f400e126SPhillip Lougher 			spin_lock(&cache->lock);
114f400e126SPhillip Lougher 
115f400e126SPhillip Lougher 			if (entry->length < 0)
116f400e126SPhillip Lougher 				entry->error = entry->length;
117f400e126SPhillip Lougher 
118f400e126SPhillip Lougher 			entry->pending = 0;
119f400e126SPhillip Lougher 
120f400e126SPhillip Lougher 			/*
121f400e126SPhillip Lougher 			 * While filling this entry one or more other processes
122f400e126SPhillip Lougher 			 * have looked it up in the cache, and have slept
123f400e126SPhillip Lougher 			 * waiting for it to become available.
124f400e126SPhillip Lougher 			 */
125f400e126SPhillip Lougher 			if (entry->num_waiters) {
126f400e126SPhillip Lougher 				spin_unlock(&cache->lock);
127f400e126SPhillip Lougher 				wake_up_all(&entry->wait_queue);
128f400e126SPhillip Lougher 			} else
129f400e126SPhillip Lougher 				spin_unlock(&cache->lock);
130f400e126SPhillip Lougher 
131f400e126SPhillip Lougher 			goto out;
132f400e126SPhillip Lougher 		}
133f400e126SPhillip Lougher 
134f400e126SPhillip Lougher 		/*
135f400e126SPhillip Lougher 		 * Block already in cache.  Increment refcount so it doesn't
136f400e126SPhillip Lougher 		 * get reused until we're finished with it, if it was
137f400e126SPhillip Lougher 		 * previously unused there's one less cache entry available
138f400e126SPhillip Lougher 		 * for reuse.
139f400e126SPhillip Lougher 		 */
140f400e126SPhillip Lougher 		entry = &cache->entry[i];
141f400e126SPhillip Lougher 		if (entry->refcount == 0)
142f400e126SPhillip Lougher 			cache->unused--;
143f400e126SPhillip Lougher 		entry->refcount++;
144f400e126SPhillip Lougher 
145f400e126SPhillip Lougher 		/*
146f400e126SPhillip Lougher 		 * If the entry is currently being filled in by another process
147f400e126SPhillip Lougher 		 * go to sleep waiting for it to become available.
148f400e126SPhillip Lougher 		 */
149f400e126SPhillip Lougher 		if (entry->pending) {
150f400e126SPhillip Lougher 			entry->num_waiters++;
151f400e126SPhillip Lougher 			spin_unlock(&cache->lock);
152f400e126SPhillip Lougher 			wait_event(entry->wait_queue, !entry->pending);
153f400e126SPhillip Lougher 		} else
154f400e126SPhillip Lougher 			spin_unlock(&cache->lock);
155f400e126SPhillip Lougher 
156f400e126SPhillip Lougher 		goto out;
157f400e126SPhillip Lougher 	}
158f400e126SPhillip Lougher 
159f400e126SPhillip Lougher out:
160f400e126SPhillip Lougher 	TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
161f400e126SPhillip Lougher 		cache->name, i, entry->block, entry->refcount, entry->error);
162f400e126SPhillip Lougher 
163f400e126SPhillip Lougher 	if (entry->error)
164f400e126SPhillip Lougher 		ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
165f400e126SPhillip Lougher 							block);
166f400e126SPhillip Lougher 	return entry;
167f400e126SPhillip Lougher }
168f400e126SPhillip Lougher 
169f400e126SPhillip Lougher 
170f400e126SPhillip Lougher /*
171f400e126SPhillip Lougher  * Release cache entry, once usage count is zero it can be reused.
172f400e126SPhillip Lougher  */
squashfs_cache_put(struct squashfs_cache_entry * entry)173f400e126SPhillip Lougher void squashfs_cache_put(struct squashfs_cache_entry *entry)
174f400e126SPhillip Lougher {
175f400e126SPhillip Lougher 	struct squashfs_cache *cache = entry->cache;
176f400e126SPhillip Lougher 
177f400e126SPhillip Lougher 	spin_lock(&cache->lock);
178f400e126SPhillip Lougher 	entry->refcount--;
179f400e126SPhillip Lougher 	if (entry->refcount == 0) {
180f400e126SPhillip Lougher 		cache->unused++;
181f400e126SPhillip Lougher 		/*
182f400e126SPhillip Lougher 		 * If there's any processes waiting for a block to become
183f400e126SPhillip Lougher 		 * available, wake one up.
184f400e126SPhillip Lougher 		 */
185f400e126SPhillip Lougher 		if (cache->num_waiters) {
186f400e126SPhillip Lougher 			spin_unlock(&cache->lock);
187f400e126SPhillip Lougher 			wake_up(&cache->wait_queue);
188f400e126SPhillip Lougher 			return;
189f400e126SPhillip Lougher 		}
190f400e126SPhillip Lougher 	}
191f400e126SPhillip Lougher 	spin_unlock(&cache->lock);
192f400e126SPhillip Lougher }
193f400e126SPhillip Lougher 
194f400e126SPhillip Lougher /*
195f400e126SPhillip Lougher  * Delete cache reclaiming all kmalloced buffers.
196f400e126SPhillip Lougher  */
squashfs_cache_delete(struct squashfs_cache * cache)197f400e126SPhillip Lougher void squashfs_cache_delete(struct squashfs_cache *cache)
198f400e126SPhillip Lougher {
199f400e126SPhillip Lougher 	int i, j;
200f400e126SPhillip Lougher 
201f400e126SPhillip Lougher 	if (cache == NULL)
202f400e126SPhillip Lougher 		return;
203f400e126SPhillip Lougher 
204f400e126SPhillip Lougher 	for (i = 0; i < cache->entries; i++) {
205f400e126SPhillip Lougher 		if (cache->entry[i].data) {
206f400e126SPhillip Lougher 			for (j = 0; j < cache->pages; j++)
207f400e126SPhillip Lougher 				kfree(cache->entry[i].data[j]);
208f400e126SPhillip Lougher 			kfree(cache->entry[i].data);
209f400e126SPhillip Lougher 		}
210846b730eSPhillip Lougher 		kfree(cache->entry[i].actor);
211f400e126SPhillip Lougher 	}
212f400e126SPhillip Lougher 
213f400e126SPhillip Lougher 	kfree(cache->entry);
214f400e126SPhillip Lougher 	kfree(cache);
215f400e126SPhillip Lougher }
216f400e126SPhillip Lougher 
217f400e126SPhillip Lougher 
218f400e126SPhillip Lougher /*
219f400e126SPhillip Lougher  * Initialise cache allocating the specified number of entries, each of
220f400e126SPhillip Lougher  * size block_size.  To avoid vmalloc fragmentation issues each entry
221ea1754a0SKirill A. Shutemov  * is allocated as a sequence of kmalloced PAGE_SIZE buffers.
222f400e126SPhillip Lougher  */
squashfs_cache_init(char * name,int entries,int block_size)223f400e126SPhillip Lougher struct squashfs_cache *squashfs_cache_init(char *name, int entries,
224f400e126SPhillip Lougher 	int block_size)
225f400e126SPhillip Lougher {
226f400e126SPhillip Lougher 	int i, j;
227f400e126SPhillip Lougher 	struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL);
228f400e126SPhillip Lougher 
229f400e126SPhillip Lougher 	if (cache == NULL) {
230f400e126SPhillip Lougher 		ERROR("Failed to allocate %s cache\n", name);
231f400e126SPhillip Lougher 		return NULL;
232f400e126SPhillip Lougher 	}
233f400e126SPhillip Lougher 
234f400e126SPhillip Lougher 	cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL);
235f400e126SPhillip Lougher 	if (cache->entry == NULL) {
236f400e126SPhillip Lougher 		ERROR("Failed to allocate %s cache\n", name);
237f400e126SPhillip Lougher 		goto cleanup;
238f400e126SPhillip Lougher 	}
239f400e126SPhillip Lougher 
240d7fbd893SAjeet Yadav 	cache->curr_blk = 0;
241f400e126SPhillip Lougher 	cache->next_blk = 0;
242f400e126SPhillip Lougher 	cache->unused = entries;
243f400e126SPhillip Lougher 	cache->entries = entries;
244f400e126SPhillip Lougher 	cache->block_size = block_size;
24509cbfeafSKirill A. Shutemov 	cache->pages = block_size >> PAGE_SHIFT;
246a37b06d5SDoug Chapman 	cache->pages = cache->pages ? cache->pages : 1;
247f400e126SPhillip Lougher 	cache->name = name;
248f400e126SPhillip Lougher 	cache->num_waiters = 0;
249f400e126SPhillip Lougher 	spin_lock_init(&cache->lock);
250f400e126SPhillip Lougher 	init_waitqueue_head(&cache->wait_queue);
251f400e126SPhillip Lougher 
252f400e126SPhillip Lougher 	for (i = 0; i < entries; i++) {
253f400e126SPhillip Lougher 		struct squashfs_cache_entry *entry = &cache->entry[i];
254f400e126SPhillip Lougher 
255f400e126SPhillip Lougher 		init_waitqueue_head(&cache->entry[i].wait_queue);
256f400e126SPhillip Lougher 		entry->cache = cache;
257f400e126SPhillip Lougher 		entry->block = SQUASHFS_INVALID_BLK;
258f400e126SPhillip Lougher 		entry->data = kcalloc(cache->pages, sizeof(void *), GFP_KERNEL);
259f400e126SPhillip Lougher 		if (entry->data == NULL) {
260f400e126SPhillip Lougher 			ERROR("Failed to allocate %s cache entry\n", name);
261f400e126SPhillip Lougher 			goto cleanup;
262f400e126SPhillip Lougher 		}
263f400e126SPhillip Lougher 
264f400e126SPhillip Lougher 		for (j = 0; j < cache->pages; j++) {
26509cbfeafSKirill A. Shutemov 			entry->data[j] = kmalloc(PAGE_SIZE, GFP_KERNEL);
266f400e126SPhillip Lougher 			if (entry->data[j] == NULL) {
267f400e126SPhillip Lougher 				ERROR("Failed to allocate %s buffer\n", name);
268f400e126SPhillip Lougher 				goto cleanup;
269f400e126SPhillip Lougher 			}
270f400e126SPhillip Lougher 		}
271846b730eSPhillip Lougher 
272846b730eSPhillip Lougher 		entry->actor = squashfs_page_actor_init(entry->data,
273846b730eSPhillip Lougher 						cache->pages, 0);
274846b730eSPhillip Lougher 		if (entry->actor == NULL) {
275846b730eSPhillip Lougher 			ERROR("Failed to allocate %s cache entry\n", name);
276846b730eSPhillip Lougher 			goto cleanup;
277846b730eSPhillip Lougher 		}
278f400e126SPhillip Lougher 	}
279f400e126SPhillip Lougher 
280f400e126SPhillip Lougher 	return cache;
281f400e126SPhillip Lougher 
282f400e126SPhillip Lougher cleanup:
283f400e126SPhillip Lougher 	squashfs_cache_delete(cache);
284f400e126SPhillip Lougher 	return NULL;
285f400e126SPhillip Lougher }
286f400e126SPhillip Lougher 
287f400e126SPhillip Lougher 
288f400e126SPhillip Lougher /*
289f400e126SPhillip Lougher  * Copy up to length bytes from cache entry to buffer starting at offset bytes
290f400e126SPhillip Lougher  * into the cache entry.  If there's not length bytes then copy the number of
291f400e126SPhillip Lougher  * bytes available.  In all cases return the number of bytes copied.
292f400e126SPhillip Lougher  */
squashfs_copy_data(void * buffer,struct squashfs_cache_entry * entry,int offset,int length)293f400e126SPhillip Lougher int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry,
294f400e126SPhillip Lougher 		int offset, int length)
295f400e126SPhillip Lougher {
296f400e126SPhillip Lougher 	int remaining = length;
297f400e126SPhillip Lougher 
298f400e126SPhillip Lougher 	if (length == 0)
299f400e126SPhillip Lougher 		return 0;
300f400e126SPhillip Lougher 	else if (buffer == NULL)
301f400e126SPhillip Lougher 		return min(length, entry->length - offset);
302f400e126SPhillip Lougher 
303f400e126SPhillip Lougher 	while (offset < entry->length) {
30409cbfeafSKirill A. Shutemov 		void *buff = entry->data[offset / PAGE_SIZE]
30509cbfeafSKirill A. Shutemov 				+ (offset % PAGE_SIZE);
306f400e126SPhillip Lougher 		int bytes = min_t(int, entry->length - offset,
30709cbfeafSKirill A. Shutemov 				PAGE_SIZE - (offset % PAGE_SIZE));
308f400e126SPhillip Lougher 
309f400e126SPhillip Lougher 		if (bytes >= remaining) {
310f400e126SPhillip Lougher 			memcpy(buffer, buff, remaining);
311f400e126SPhillip Lougher 			remaining = 0;
312f400e126SPhillip Lougher 			break;
313f400e126SPhillip Lougher 		}
314f400e126SPhillip Lougher 
315f400e126SPhillip Lougher 		memcpy(buffer, buff, bytes);
316f400e126SPhillip Lougher 		buffer += bytes;
317f400e126SPhillip Lougher 		remaining -= bytes;
318f400e126SPhillip Lougher 		offset += bytes;
319f400e126SPhillip Lougher 	}
320f400e126SPhillip Lougher 
321f400e126SPhillip Lougher 	return length - remaining;
322f400e126SPhillip Lougher }
323f400e126SPhillip Lougher 
324f400e126SPhillip Lougher 
325f400e126SPhillip Lougher /*
326f400e126SPhillip Lougher  * Read length bytes from metadata position <block, offset> (block is the
327f400e126SPhillip Lougher  * start of the compressed block on disk, and offset is the offset into
328f400e126SPhillip Lougher  * the block once decompressed).  Data is packed into consecutive blocks,
329f400e126SPhillip Lougher  * and length bytes may require reading more than one block.
330f400e126SPhillip Lougher  */
squashfs_read_metadata(struct super_block * sb,void * buffer,u64 * block,int * offset,int length)331f400e126SPhillip Lougher int squashfs_read_metadata(struct super_block *sb, void *buffer,
332f400e126SPhillip Lougher 		u64 *block, int *offset, int length)
333f400e126SPhillip Lougher {
334f400e126SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
335e552a596SPhillip Lougher 	int bytes, res = length;
336f400e126SPhillip Lougher 	struct squashfs_cache_entry *entry;
337f400e126SPhillip Lougher 
338f400e126SPhillip Lougher 	TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
339f400e126SPhillip Lougher 
34001cfb793SLinus Torvalds 	if (unlikely(length < 0))
34101cfb793SLinus Torvalds 		return -EIO;
34201cfb793SLinus Torvalds 
343f400e126SPhillip Lougher 	while (length) {
344f400e126SPhillip Lougher 		entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
345e552a596SPhillip Lougher 		if (entry->error) {
346e552a596SPhillip Lougher 			res = entry->error;
347e552a596SPhillip Lougher 			goto error;
348e552a596SPhillip Lougher 		} else if (*offset >= entry->length) {
349e552a596SPhillip Lougher 			res = -EIO;
350e552a596SPhillip Lougher 			goto error;
351e552a596SPhillip Lougher 		}
352f400e126SPhillip Lougher 
353f400e126SPhillip Lougher 		bytes = squashfs_copy_data(buffer, entry, *offset, length);
354f400e126SPhillip Lougher 		if (buffer)
355f400e126SPhillip Lougher 			buffer += bytes;
356f400e126SPhillip Lougher 		length -= bytes;
357f400e126SPhillip Lougher 		*offset += bytes;
358f400e126SPhillip Lougher 
359f400e126SPhillip Lougher 		if (*offset == entry->length) {
360f400e126SPhillip Lougher 			*block = entry->next_index;
361f400e126SPhillip Lougher 			*offset = 0;
362f400e126SPhillip Lougher 		}
363f400e126SPhillip Lougher 
364f400e126SPhillip Lougher 		squashfs_cache_put(entry);
365f400e126SPhillip Lougher 	}
366f400e126SPhillip Lougher 
367e552a596SPhillip Lougher 	return res;
368e552a596SPhillip Lougher 
369e552a596SPhillip Lougher error:
370e552a596SPhillip Lougher 	squashfs_cache_put(entry);
371e552a596SPhillip Lougher 	return res;
372f400e126SPhillip Lougher }
373f400e126SPhillip Lougher 
374f400e126SPhillip Lougher 
375f400e126SPhillip Lougher /*
376f400e126SPhillip Lougher  * Look-up in the fragmment cache the fragment located at <start_block> in the
377f400e126SPhillip Lougher  * filesystem.  If necessary read and decompress it from disk.
378f400e126SPhillip Lougher  */
squashfs_get_fragment(struct super_block * sb,u64 start_block,int length)379f400e126SPhillip Lougher struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
380f400e126SPhillip Lougher 				u64 start_block, int length)
381f400e126SPhillip Lougher {
382f400e126SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
383f400e126SPhillip Lougher 
384f400e126SPhillip Lougher 	return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
385f400e126SPhillip Lougher 		length);
386f400e126SPhillip Lougher }
387f400e126SPhillip Lougher 
388f400e126SPhillip Lougher 
389f400e126SPhillip Lougher /*
390f400e126SPhillip Lougher  * Read and decompress the datablock located at <start_block> in the
391f400e126SPhillip Lougher  * filesystem.  The cache is used here to avoid duplicating locking and
392f400e126SPhillip Lougher  * read/decompress code.
393f400e126SPhillip Lougher  */
squashfs_get_datablock(struct super_block * sb,u64 start_block,int length)394f400e126SPhillip Lougher struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
395f400e126SPhillip Lougher 				u64 start_block, int length)
396f400e126SPhillip Lougher {
397f400e126SPhillip Lougher 	struct squashfs_sb_info *msblk = sb->s_fs_info;
398f400e126SPhillip Lougher 
399f400e126SPhillip Lougher 	return squashfs_cache_get(sb, msblk->read_page, start_block, length);
400f400e126SPhillip Lougher }
401f400e126SPhillip Lougher 
402f400e126SPhillip Lougher 
403f400e126SPhillip Lougher /*
404f400e126SPhillip Lougher  * Read a filesystem table (uncompressed sequence of bytes) from disk
405f400e126SPhillip Lougher  */
squashfs_read_table(struct super_block * sb,u64 block,int length)40682de647eSPhillip Lougher void *squashfs_read_table(struct super_block *sb, u64 block, int length)
407f400e126SPhillip Lougher {
40809cbfeafSKirill A. Shutemov 	int pages = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
409f400e126SPhillip Lougher 	int i, res;
41082de647eSPhillip Lougher 	void *table, *buffer, **data;
411846b730eSPhillip Lougher 	struct squashfs_page_actor *actor;
41282de647eSPhillip Lougher 
41382de647eSPhillip Lougher 	table = buffer = kmalloc(length, GFP_KERNEL);
41482de647eSPhillip Lougher 	if (table == NULL)
41582de647eSPhillip Lougher 		return ERR_PTR(-ENOMEM);
41682de647eSPhillip Lougher 
41782de647eSPhillip Lougher 	data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
41882de647eSPhillip Lougher 	if (data == NULL) {
41982de647eSPhillip Lougher 		res = -ENOMEM;
42082de647eSPhillip Lougher 		goto failed;
42182de647eSPhillip Lougher 	}
422f400e126SPhillip Lougher 
423846b730eSPhillip Lougher 	actor = squashfs_page_actor_init(data, pages, length);
424846b730eSPhillip Lougher 	if (actor == NULL) {
425846b730eSPhillip Lougher 		res = -ENOMEM;
426846b730eSPhillip Lougher 		goto failed2;
427846b730eSPhillip Lougher 	}
428846b730eSPhillip Lougher 
42909cbfeafSKirill A. Shutemov 	for (i = 0; i < pages; i++, buffer += PAGE_SIZE)
430f400e126SPhillip Lougher 		data[i] = buffer;
43182de647eSPhillip Lougher 
432846b730eSPhillip Lougher 	res = squashfs_read_data(sb, block, length |
433846b730eSPhillip Lougher 		SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);
43482de647eSPhillip Lougher 
435f400e126SPhillip Lougher 	kfree(data);
436846b730eSPhillip Lougher 	kfree(actor);
43782de647eSPhillip Lougher 
43882de647eSPhillip Lougher 	if (res < 0)
43982de647eSPhillip Lougher 		goto failed;
44082de647eSPhillip Lougher 
44182de647eSPhillip Lougher 	return table;
44282de647eSPhillip Lougher 
443846b730eSPhillip Lougher failed2:
444846b730eSPhillip Lougher 	kfree(data);
44582de647eSPhillip Lougher failed:
44682de647eSPhillip Lougher 	kfree(table);
44782de647eSPhillip Lougher 	return ERR_PTR(res);
448f400e126SPhillip Lougher }
449