1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <linux/fs.h>
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>
55
56#include "squashfs_fs.h"
57#include "squashfs_fs_sb.h"
58#include "squashfs.h"
59#include "page_actor.h"
60
61
62
63
64
65struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb,
66 struct squashfs_cache *cache, u64 block, int length)
67{
68 int i, n;
69 struct squashfs_cache_entry *entry;
70
71 spin_lock(&cache->lock);
72
73 while (1) {
74 for (i = cache->curr_blk, n = 0; n < cache->entries; n++) {
75 if (cache->entry[i].block == block) {
76 cache->curr_blk = i;
77 break;
78 }
79 i = (i + 1) % cache->entries;
80 }
81
82 if (n == cache->entries) {
83
84
85
86
87 if (cache->unused == 0) {
88 cache->num_waiters++;
89 spin_unlock(&cache->lock);
90 wait_event(cache->wait_queue, cache->unused);
91 spin_lock(&cache->lock);
92 cache->num_waiters--;
93 continue;
94 }
95
96
97
98
99
100
101 i = cache->next_blk;
102 for (n = 0; n < cache->entries; n++) {
103 if (cache->entry[i].refcount == 0)
104 break;
105 i = (i + 1) % cache->entries;
106 }
107
108 cache->next_blk = (i + 1) % cache->entries;
109 entry = &cache->entry[i];
110
111
112
113
114
115 cache->unused--;
116 entry->block = block;
117 entry->refcount = 1;
118 entry->pending = 1;
119 entry->num_waiters = 0;
120 entry->error = 0;
121 spin_unlock(&cache->lock);
122
123 entry->length = squashfs_read_data(sb, block, length,
124 &entry->next_index, entry->actor);
125
126 spin_lock(&cache->lock);
127
128 if (entry->length < 0)
129 entry->error = entry->length;
130
131 entry->pending = 0;
132
133
134
135
136
137
138 if (entry->num_waiters) {
139 spin_unlock(&cache->lock);
140 wake_up_all(&entry->wait_queue);
141 } else
142 spin_unlock(&cache->lock);
143
144 goto out;
145 }
146
147
148
149
150
151
152
153 entry = &cache->entry[i];
154 if (entry->refcount == 0)
155 cache->unused--;
156 entry->refcount++;
157
158
159
160
161
162 if (entry->pending) {
163 entry->num_waiters++;
164 spin_unlock(&cache->lock);
165 wait_event(entry->wait_queue, !entry->pending);
166 } else
167 spin_unlock(&cache->lock);
168
169 goto out;
170 }
171
172out:
173 TRACE("Got %s %d, start block %lld, refcount %d, error %d\n",
174 cache->name, i, entry->block, entry->refcount, entry->error);
175
176 if (entry->error)
177 ERROR("Unable to read %s cache entry [%llx]\n", cache->name,
178 block);
179 return entry;
180}
181
182
183
184
185
186void squashfs_cache_put(struct squashfs_cache_entry *entry)
187{
188 struct squashfs_cache *cache = entry->cache;
189
190 spin_lock(&cache->lock);
191 entry->refcount--;
192 if (entry->refcount == 0) {
193 cache->unused++;
194
195
196
197
198 if (cache->num_waiters) {
199 spin_unlock(&cache->lock);
200 wake_up(&cache->wait_queue);
201 return;
202 }
203 }
204 spin_unlock(&cache->lock);
205}
206
207
208
209
210void squashfs_cache_delete(struct squashfs_cache *cache)
211{
212 int i, j;
213
214 if (cache == NULL)
215 return;
216
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);
222 }
223 kfree(cache->entry[i].actor);
224 }
225
226 kfree(cache->entry);
227 kfree(cache);
228}
229
230
231
232
233
234
235
236struct squashfs_cache *squashfs_cache_init(char *name, int entries,
237 int block_size)
238{
239 int i, j;
240 struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL);
241
242 if (cache == NULL) {
243 ERROR("Failed to allocate %s cache\n", name);
244 return NULL;
245 }
246
247 cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL);
248 if (cache->entry == NULL) {
249 ERROR("Failed to allocate %s cache\n", name);
250 goto cleanup;
251 }
252
253 cache->curr_blk = 0;
254 cache->next_blk = 0;
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;
260 cache->name = name;
261 cache->num_waiters = 0;
262 spin_lock_init(&cache->lock);
263 init_waitqueue_head(&cache->wait_queue);
264
265 for (i = 0; i < entries; i++) {
266 struct squashfs_cache_entry *entry = &cache->entry[i];
267
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);
274 goto cleanup;
275 }
276
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);
281 goto cleanup;
282 }
283 }
284
285 entry->actor = squashfs_page_actor_init(entry->data,
286 cache->pages, 0);
287 if (entry->actor == NULL) {
288 ERROR("Failed to allocate %s cache entry\n", name);
289 goto cleanup;
290 }
291 }
292
293 return cache;
294
295cleanup:
296 squashfs_cache_delete(cache);
297 return NULL;
298}
299
300
301
302
303
304
305
306int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry,
307 int offset, int length)
308{
309 int remaining = length;
310
311 if (length == 0)
312 return 0;
313 else if (buffer == NULL)
314 return min(length, entry->length - offset);
315
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));
321
322 if (bytes >= remaining) {
323 memcpy(buffer, buff, remaining);
324 remaining = 0;
325 break;
326 }
327
328 memcpy(buffer, buff, bytes);
329 buffer += bytes;
330 remaining -= bytes;
331 offset += bytes;
332 }
333
334 return length - remaining;
335}
336
337
338
339
340
341
342
343
344int squashfs_read_metadata(struct super_block *sb, void *buffer,
345 u64 *block, int *offset, int length)
346{
347 struct squashfs_sb_info *msblk = sb->s_fs_info;
348 int bytes, res = length;
349 struct squashfs_cache_entry *entry;
350
351 TRACE("Entered squashfs_read_metadata [%llx:%x]\n", *block, *offset);
352
353 if (unlikely(length < 0))
354 return -EIO;
355
356 while (length) {
357 entry = squashfs_cache_get(sb, msblk->block_cache, *block, 0);
358 if (entry->error) {
359 res = entry->error;
360 goto error;
361 } else if (*offset >= entry->length) {
362 res = -EIO;
363 goto error;
364 }
365
366 bytes = squashfs_copy_data(buffer, entry, *offset, length);
367 if (buffer)
368 buffer += bytes;
369 length -= bytes;
370 *offset += bytes;
371
372 if (*offset == entry->length) {
373 *block = entry->next_index;
374 *offset = 0;
375 }
376
377 squashfs_cache_put(entry);
378 }
379
380 return res;
381
382error:
383 squashfs_cache_put(entry);
384 return res;
385}
386
387
388
389
390
391
392struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *sb,
393 u64 start_block, int length)
394{
395 struct squashfs_sb_info *msblk = sb->s_fs_info;
396
397 return squashfs_cache_get(sb, msblk->fragment_cache, start_block,
398 length);
399}
400
401
402
403
404
405
406
407struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *sb,
408 u64 start_block, int length)
409{
410 struct squashfs_sb_info *msblk = sb->s_fs_info;
411
412 return squashfs_cache_get(sb, msblk->read_page, start_block, length);
413}
414
415
416
417
418
419void *squashfs_read_table(struct super_block *sb, u64 block, int length)
420{
421 int pages = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
422 int i, res;
423 void *table, *buffer, **data;
424 struct squashfs_page_actor *actor;
425
426 table = buffer = kmalloc(length, GFP_KERNEL);
427 if (table == NULL)
428 return ERR_PTR(-ENOMEM);
429
430 data = kcalloc(pages, sizeof(void *), GFP_KERNEL);
431 if (data == NULL) {
432 res = -ENOMEM;
433 goto failed;
434 }
435
436 actor = squashfs_page_actor_init(data, pages, length);
437 if (actor == NULL) {
438 res = -ENOMEM;
439 goto failed2;
440 }
441
442 for (i = 0; i < pages; i++, buffer += PAGE_SIZE)
443 data[i] = buffer;
444
445 res = squashfs_read_data(sb, block, length |
446 SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, actor);
447
448 kfree(data);
449 kfree(actor);
450
451 if (res < 0)
452 goto failed;
453
454 return table;
455
456failed2:
457 kfree(data);
458failed:
459 kfree(table);
460 return ERR_PTR(res);
461}
462