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