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#include <linux/fs.h>
44#include <linux/vfs.h>
45#include <linux/kernel.h>
46#include <linux/slab.h>
47#include <linux/string.h>
48#include <linux/pagemap.h>
49#include <linux/mutex.h>
50
51#include "squashfs_fs.h"
52#include "squashfs_fs_sb.h"
53#include "squashfs_fs_i.h"
54#include "squashfs.h"
55
56
57
58
59
60static struct meta_index *locate_meta_index(struct inode *inode, int offset,
61 int index)
62{
63 struct meta_index *meta = NULL;
64 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
65 int i;
66
67 mutex_lock(&msblk->meta_index_mutex);
68
69 TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
70
71 if (msblk->meta_index == NULL)
72 goto not_allocated;
73
74 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
75 if (msblk->meta_index[i].inode_number == inode->i_ino &&
76 msblk->meta_index[i].offset >= offset &&
77 msblk->meta_index[i].offset <= index &&
78 msblk->meta_index[i].locked == 0) {
79 TRACE("locate_meta_index: entry %d, offset %d\n", i,
80 msblk->meta_index[i].offset);
81 meta = &msblk->meta_index[i];
82 offset = meta->offset;
83 }
84 }
85
86 if (meta)
87 meta->locked = 1;
88
89not_allocated:
90 mutex_unlock(&msblk->meta_index_mutex);
91
92 return meta;
93}
94
95
96
97
98
99static struct meta_index *empty_meta_index(struct inode *inode, int offset,
100 int skip)
101{
102 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
103 struct meta_index *meta = NULL;
104 int i;
105
106 mutex_lock(&msblk->meta_index_mutex);
107
108 TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
109
110 if (msblk->meta_index == NULL) {
111
112
113
114
115
116
117 msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
118 sizeof(*(msblk->meta_index)), GFP_KERNEL);
119 if (msblk->meta_index == NULL) {
120 ERROR("Failed to allocate meta_index\n");
121 goto failed;
122 }
123 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
124 msblk->meta_index[i].inode_number = 0;
125 msblk->meta_index[i].locked = 0;
126 }
127 msblk->next_meta_index = 0;
128 }
129
130 for (i = SQUASHFS_META_SLOTS; i &&
131 msblk->meta_index[msblk->next_meta_index].locked; i--)
132 msblk->next_meta_index = (msblk->next_meta_index + 1) %
133 SQUASHFS_META_SLOTS;
134
135 if (i == 0) {
136 TRACE("empty_meta_index: failed!\n");
137 goto failed;
138 }
139
140 TRACE("empty_meta_index: returned meta entry %d, %p\n",
141 msblk->next_meta_index,
142 &msblk->meta_index[msblk->next_meta_index]);
143
144 meta = &msblk->meta_index[msblk->next_meta_index];
145 msblk->next_meta_index = (msblk->next_meta_index + 1) %
146 SQUASHFS_META_SLOTS;
147
148 meta->inode_number = inode->i_ino;
149 meta->offset = offset;
150 meta->skip = skip;
151 meta->entries = 0;
152 meta->locked = 1;
153
154failed:
155 mutex_unlock(&msblk->meta_index_mutex);
156 return meta;
157}
158
159
160static void release_meta_index(struct inode *inode, struct meta_index *meta)
161{
162 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
163 mutex_lock(&msblk->meta_index_mutex);
164 meta->locked = 0;
165 mutex_unlock(&msblk->meta_index_mutex);
166}
167
168
169
170
171
172
173static long long read_indexes(struct super_block *sb, int n,
174 u64 *start_block, int *offset)
175{
176 int err, i;
177 long long block = 0;
178 __le32 *blist = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
179
180 if (blist == NULL) {
181 ERROR("read_indexes: Failed to allocate block_list\n");
182 return -ENOMEM;
183 }
184
185 while (n) {
186 int blocks = min_t(int, n, PAGE_CACHE_SIZE >> 2);
187
188 err = squashfs_read_metadata(sb, blist, start_block,
189 offset, blocks << 2);
190 if (err < 0) {
191 ERROR("read_indexes: reading block [%llx:%x]\n",
192 *start_block, *offset);
193 goto failure;
194 }
195
196 for (i = 0; i < blocks; i++) {
197 int size = le32_to_cpu(blist[i]);
198 block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
199 }
200 n -= blocks;
201 }
202
203 kfree(blist);
204 return block;
205
206failure:
207 kfree(blist);
208 return err;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223static inline int calculate_skip(int blocks)
224{
225 int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
226 * SQUASHFS_META_INDEXES);
227 return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
228}
229
230
231
232
233
234
235
236static int fill_meta_index(struct inode *inode, int index,
237 u64 *index_block, int *index_offset, u64 *data_block)
238{
239 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
240 int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
241 int offset = 0;
242 struct meta_index *meta;
243 struct meta_entry *meta_entry;
244 u64 cur_index_block = squashfs_i(inode)->block_list_start;
245 int cur_offset = squashfs_i(inode)->offset;
246 u64 cur_data_block = squashfs_i(inode)->start;
247 int err, i;
248
249
250
251
252 index /= SQUASHFS_META_INDEXES * skip;
253
254 while (offset < index) {
255 meta = locate_meta_index(inode, offset + 1, index);
256
257 if (meta == NULL) {
258 meta = empty_meta_index(inode, offset + 1, skip);
259 if (meta == NULL)
260 goto all_done;
261 } else {
262 offset = index < meta->offset + meta->entries ? index :
263 meta->offset + meta->entries - 1;
264 meta_entry = &meta->meta_entry[offset - meta->offset];
265 cur_index_block = meta_entry->index_block +
266 msblk->inode_table;
267 cur_offset = meta_entry->offset;
268 cur_data_block = meta_entry->data_block;
269 TRACE("get_meta_index: offset %d, meta->offset %d, "
270 "meta->entries %d\n", offset, meta->offset,
271 meta->entries);
272 TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
273 " data_block 0x%llx\n", cur_index_block,
274 cur_offset, cur_data_block);
275 }
276
277
278
279
280
281
282 for (i = meta->offset + meta->entries; i <= index &&
283 i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
284 int blocks = skip * SQUASHFS_META_INDEXES;
285 long long res = read_indexes(inode->i_sb, blocks,
286 &cur_index_block, &cur_offset);
287
288 if (res < 0) {
289 if (meta->entries == 0)
290
291
292
293
294 meta->inode_number = 0;
295 err = res;
296 goto failed;
297 }
298
299 cur_data_block += res;
300 meta_entry = &meta->meta_entry[i - meta->offset];
301 meta_entry->index_block = cur_index_block -
302 msblk->inode_table;
303 meta_entry->offset = cur_offset;
304 meta_entry->data_block = cur_data_block;
305 meta->entries++;
306 offset++;
307 }
308
309 TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
310 meta->offset, meta->entries);
311
312 release_meta_index(inode, meta);
313 }
314
315all_done:
316 *index_block = cur_index_block;
317 *index_offset = cur_offset;
318 *data_block = cur_data_block;
319
320
321
322
323 return offset * SQUASHFS_META_INDEXES * skip;
324
325failed:
326 release_meta_index(inode, meta);
327 return err;
328}
329
330
331
332
333
334
335static int read_blocklist(struct inode *inode, int index, u64 *block)
336{
337 u64 start;
338 long long blks;
339 int offset;
340 __le32 size;
341 int res = fill_meta_index(inode, index, &start, &offset, block);
342
343 TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
344 " 0x%x, block 0x%llx\n", res, index, start, offset,
345 *block);
346
347 if (res < 0)
348 return res;
349
350
351
352
353
354
355
356 if (res < index) {
357 blks = read_indexes(inode->i_sb, index - res, &start, &offset);
358 if (blks < 0)
359 return (int) blks;
360 *block += blks;
361 }
362
363
364
365
366 res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
367 sizeof(size));
368 if (res < 0)
369 return res;
370 return le32_to_cpu(size);
371}
372
373
374void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
375 int bytes, int offset)
376{
377 struct inode *inode = page->mapping->host;
378 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
379 void *pageaddr;
380 int i, mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
381 int start_index = page->index & ~mask, end_index = start_index | mask;
382
383
384
385
386
387
388
389 for (i = start_index; i <= end_index && bytes > 0; i++,
390 bytes -= PAGE_CACHE_SIZE, offset += PAGE_CACHE_SIZE) {
391 struct page *push_page;
392 int avail = buffer ? min_t(int, bytes, PAGE_CACHE_SIZE) : 0;
393
394 TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
395
396 push_page = (i == page->index) ? page :
397 grab_cache_page_nowait(page->mapping, i);
398
399 if (!push_page)
400 continue;
401
402 if (PageUptodate(push_page))
403 goto skip_page;
404
405 pageaddr = kmap_atomic(push_page);
406 squashfs_copy_data(pageaddr, buffer, offset, avail);
407 memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
408 kunmap_atomic(pageaddr);
409 flush_dcache_page(push_page);
410 SetPageUptodate(push_page);
411skip_page:
412 unlock_page(push_page);
413 if (i != page->index)
414 page_cache_release(push_page);
415 }
416}
417
418
419static int squashfs_readpage_fragment(struct page *page)
420{
421 struct inode *inode = page->mapping->host;
422 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
423 struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
424 squashfs_i(inode)->fragment_block,
425 squashfs_i(inode)->fragment_size);
426 int res = buffer->error;
427
428 if (res)
429 ERROR("Unable to read page, block %llx, size %x\n",
430 squashfs_i(inode)->fragment_block,
431 squashfs_i(inode)->fragment_size);
432 else
433 squashfs_copy_cache(page, buffer, i_size_read(inode) &
434 (msblk->block_size - 1),
435 squashfs_i(inode)->fragment_offset);
436
437 squashfs_cache_put(buffer);
438 return res;
439}
440
441static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
442{
443 struct inode *inode = page->mapping->host;
444 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
445 int bytes = index == file_end ?
446 (i_size_read(inode) & (msblk->block_size - 1)) :
447 msblk->block_size;
448
449 squashfs_copy_cache(page, NULL, bytes, 0);
450 return 0;
451}
452
453static int squashfs_readpage(struct file *file, struct page *page)
454{
455 struct inode *inode = page->mapping->host;
456 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
457 int index = page->index >> (msblk->block_log - PAGE_CACHE_SHIFT);
458 int file_end = i_size_read(inode) >> msblk->block_log;
459 int res;
460 void *pageaddr;
461
462 TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
463 page->index, squashfs_i(inode)->start);
464
465 if (page->index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
466 PAGE_CACHE_SHIFT))
467 goto out;
468
469 if (index < file_end || squashfs_i(inode)->fragment_block ==
470 SQUASHFS_INVALID_BLK) {
471 u64 block = 0;
472 int bsize = read_blocklist(inode, index, &block);
473 if (bsize < 0)
474 goto error_out;
475
476 if (bsize == 0)
477 res = squashfs_readpage_sparse(page, index, file_end);
478 else
479 res = squashfs_readpage_block(page, block, bsize);
480 } else
481 res = squashfs_readpage_fragment(page);
482
483 if (!res)
484 return 0;
485
486error_out:
487 SetPageError(page);
488out:
489 pageaddr = kmap_atomic(page);
490 memset(pageaddr, 0, PAGE_CACHE_SIZE);
491 kunmap_atomic(pageaddr);
492 flush_dcache_page(page);
493 if (!PageError(page))
494 SetPageUptodate(page);
495 unlock_page(page);
496
497 return 0;
498}
499
500
501const struct address_space_operations squashfs_aops = {
502 .readpage = squashfs_readpage
503};
504