blob: cd3c5c8211a5fc21960ea4fe4a8b249c8ff737a1 [file] [log] [blame]
Phillip Lougher1701aec2009-01-05 08:46:24 +00001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01005 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougher1701aec2009-01-05 08:46:24 +00006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * file.c
22 */
23
24/*
25 * This file contains code for handling regular files. A regular file
26 * consists of a sequence of contiguous compressed blocks, and/or a
27 * compressed fragment block (tail-end packed block). The compressed size
28 * of each datablock is stored in a block list contained within the
29 * file inode (itself stored in one or more compressed metadata blocks).
30 *
31 * To speed up access to datablocks when reading 'large' files (256 Mbytes or
32 * larger), the code implements an index cache that caches the mapping from
33 * block index to datablock location on disk.
34 *
35 * The index cache allows Squashfs to handle large files (up to 1.75 TiB) while
36 * retaining a simple and space-efficient block list on disk. The cache
37 * is split into slots, caching up to eight 224 GiB files (128 KiB blocks).
38 * Larger files use multiple slots, with 1.75 TiB files using all 8 slots.
39 * The index cache is designed to be memory efficient, and by default uses
40 * 16 KiB.
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>
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -070050#include <linux/mm_inline.h>
Phillip Lougher1701aec2009-01-05 08:46:24 +000051
52#include "squashfs_fs.h"
53#include "squashfs_fs_sb.h"
54#include "squashfs_fs_i.h"
55#include "squashfs.h"
56
57/*
58 * Locate cache slot in range [offset, index] for specified inode. If
59 * there's more than one return the slot closest to index.
60 */
61static struct meta_index *locate_meta_index(struct inode *inode, int offset,
62 int index)
63{
64 struct meta_index *meta = NULL;
65 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
66 int i;
67
68 mutex_lock(&msblk->meta_index_mutex);
69
70 TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
71
72 if (msblk->meta_index == NULL)
73 goto not_allocated;
74
75 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
76 if (msblk->meta_index[i].inode_number == inode->i_ino &&
77 msblk->meta_index[i].offset >= offset &&
78 msblk->meta_index[i].offset <= index &&
79 msblk->meta_index[i].locked == 0) {
80 TRACE("locate_meta_index: entry %d, offset %d\n", i,
81 msblk->meta_index[i].offset);
82 meta = &msblk->meta_index[i];
83 offset = meta->offset;
84 }
85 }
86
87 if (meta)
88 meta->locked = 1;
89
90not_allocated:
91 mutex_unlock(&msblk->meta_index_mutex);
92
93 return meta;
94}
95
96
97/*
98 * Find and initialise an empty cache slot for index offset.
99 */
100static struct meta_index *empty_meta_index(struct inode *inode, int offset,
101 int skip)
102{
103 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
104 struct meta_index *meta = NULL;
105 int i;
106
107 mutex_lock(&msblk->meta_index_mutex);
108
109 TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
110
111 if (msblk->meta_index == NULL) {
112 /*
113 * First time cache index has been used, allocate and
114 * initialise. The cache index could be allocated at
115 * mount time but doing it here means it is allocated only
116 * if a 'large' file is read.
117 */
118 msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
119 sizeof(*(msblk->meta_index)), GFP_KERNEL);
120 if (msblk->meta_index == NULL) {
121 ERROR("Failed to allocate meta_index\n");
122 goto failed;
123 }
124 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
125 msblk->meta_index[i].inode_number = 0;
126 msblk->meta_index[i].locked = 0;
127 }
128 msblk->next_meta_index = 0;
129 }
130
131 for (i = SQUASHFS_META_SLOTS; i &&
132 msblk->meta_index[msblk->next_meta_index].locked; i--)
133 msblk->next_meta_index = (msblk->next_meta_index + 1) %
134 SQUASHFS_META_SLOTS;
135
136 if (i == 0) {
137 TRACE("empty_meta_index: failed!\n");
138 goto failed;
139 }
140
141 TRACE("empty_meta_index: returned meta entry %d, %p\n",
142 msblk->next_meta_index,
143 &msblk->meta_index[msblk->next_meta_index]);
144
145 meta = &msblk->meta_index[msblk->next_meta_index];
146 msblk->next_meta_index = (msblk->next_meta_index + 1) %
147 SQUASHFS_META_SLOTS;
148
149 meta->inode_number = inode->i_ino;
150 meta->offset = offset;
151 meta->skip = skip;
152 meta->entries = 0;
153 meta->locked = 1;
154
155failed:
156 mutex_unlock(&msblk->meta_index_mutex);
157 return meta;
158}
159
160
161static void release_meta_index(struct inode *inode, struct meta_index *meta)
162{
163 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
164 mutex_lock(&msblk->meta_index_mutex);
165 meta->locked = 0;
166 mutex_unlock(&msblk->meta_index_mutex);
167}
168
169
170/*
171 * Read the next n blocks from the block list, starting from
172 * metadata block <start_block, offset>.
173 */
174static long long read_indexes(struct super_block *sb, int n,
175 u64 *start_block, int *offset)
176{
177 int err, i;
178 long long block = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300179 __le32 *blist = kmalloc(PAGE_SIZE, GFP_KERNEL);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000180
181 if (blist == NULL) {
182 ERROR("read_indexes: Failed to allocate block_list\n");
183 return -ENOMEM;
184 }
185
186 while (n) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300187 int blocks = min_t(int, n, PAGE_SIZE >> 2);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000188
189 err = squashfs_read_metadata(sb, blist, start_block,
190 offset, blocks << 2);
191 if (err < 0) {
192 ERROR("read_indexes: reading block [%llx:%x]\n",
193 *start_block, *offset);
194 goto failure;
195 }
196
197 for (i = 0; i < blocks; i++) {
Linus Torvalds1aecbe42018-07-29 12:44:46 -0700198 int size = squashfs_block_size(blist[i]);
199 if (size < 0) {
200 err = size;
201 goto failure;
202 }
Phillip Lougher1701aec2009-01-05 08:46:24 +0000203 block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
204 }
205 n -= blocks;
206 }
207
208 kfree(blist);
209 return block;
210
211failure:
212 kfree(blist);
213 return err;
214}
215
216
217/*
218 * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
219 * can cache one index -> datablock/blocklist-block mapping. We wish
220 * to distribute these over the length of the file, entry[0] maps index x,
221 * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
222 * The larger the file, the greater the skip factor. The skip factor is
223 * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
224 * the number of metadata blocks that need to be read fits into the cache.
225 * If the skip factor is limited in this way then the file will use multiple
226 * slots.
227 */
228static inline int calculate_skip(int blocks)
229{
230 int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
231 * SQUASHFS_META_INDEXES);
232 return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
233}
234
235
236/*
237 * Search and grow the index cache for the specified inode, returning the
238 * on-disk locations of the datablock and block list metadata block
239 * <index_block, index_offset> for index (scaled to nearest cache index).
240 */
241static int fill_meta_index(struct inode *inode, int index,
242 u64 *index_block, int *index_offset, u64 *data_block)
243{
244 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
245 int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
246 int offset = 0;
247 struct meta_index *meta;
248 struct meta_entry *meta_entry;
249 u64 cur_index_block = squashfs_i(inode)->block_list_start;
250 int cur_offset = squashfs_i(inode)->offset;
251 u64 cur_data_block = squashfs_i(inode)->start;
252 int err, i;
253
254 /*
255 * Scale index to cache index (cache slot entry)
256 */
257 index /= SQUASHFS_META_INDEXES * skip;
258
259 while (offset < index) {
260 meta = locate_meta_index(inode, offset + 1, index);
261
262 if (meta == NULL) {
263 meta = empty_meta_index(inode, offset + 1, skip);
264 if (meta == NULL)
265 goto all_done;
266 } else {
267 offset = index < meta->offset + meta->entries ? index :
268 meta->offset + meta->entries - 1;
269 meta_entry = &meta->meta_entry[offset - meta->offset];
270 cur_index_block = meta_entry->index_block +
271 msblk->inode_table;
272 cur_offset = meta_entry->offset;
273 cur_data_block = meta_entry->data_block;
274 TRACE("get_meta_index: offset %d, meta->offset %d, "
275 "meta->entries %d\n", offset, meta->offset,
276 meta->entries);
277 TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
278 " data_block 0x%llx\n", cur_index_block,
279 cur_offset, cur_data_block);
280 }
281
282 /*
283 * If necessary grow cache slot by reading block list. Cache
284 * slot is extended up to index or to the end of the slot, in
285 * which case further slots will be used.
286 */
287 for (i = meta->offset + meta->entries; i <= index &&
288 i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
289 int blocks = skip * SQUASHFS_META_INDEXES;
290 long long res = read_indexes(inode->i_sb, blocks,
291 &cur_index_block, &cur_offset);
292
293 if (res < 0) {
294 if (meta->entries == 0)
295 /*
296 * Don't leave an empty slot on read
297 * error allocated to this inode...
298 */
299 meta->inode_number = 0;
300 err = res;
301 goto failed;
302 }
303
304 cur_data_block += res;
305 meta_entry = &meta->meta_entry[i - meta->offset];
306 meta_entry->index_block = cur_index_block -
307 msblk->inode_table;
308 meta_entry->offset = cur_offset;
309 meta_entry->data_block = cur_data_block;
310 meta->entries++;
311 offset++;
312 }
313
314 TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
315 meta->offset, meta->entries);
316
317 release_meta_index(inode, meta);
318 }
319
320all_done:
321 *index_block = cur_index_block;
322 *index_offset = cur_offset;
323 *data_block = cur_data_block;
324
325 /*
326 * Scale cache index (cache slot entry) to index
327 */
328 return offset * SQUASHFS_META_INDEXES * skip;
329
330failed:
331 release_meta_index(inode, meta);
332 return err;
333}
334
335
336/*
337 * Get the on-disk location and compressed size of the datablock
338 * specified by index. Fill_meta_index() does most of the work.
339 */
340static int read_blocklist(struct inode *inode, int index, u64 *block)
341{
342 u64 start;
343 long long blks;
344 int offset;
345 __le32 size;
346 int res = fill_meta_index(inode, index, &start, &offset, block);
347
348 TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
349 " 0x%x, block 0x%llx\n", res, index, start, offset,
350 *block);
351
352 if (res < 0)
353 return res;
354
355 /*
356 * res contains the index of the mapping returned by fill_meta_index(),
357 * this will likely be less than the desired index (because the
358 * meta_index cache works at a higher granularity). Read any
359 * extra block indexes needed.
360 */
361 if (res < index) {
362 blks = read_indexes(inode->i_sb, index - res, &start, &offset);
363 if (blks < 0)
364 return (int) blks;
365 *block += blks;
366 }
367
368 /*
369 * Read length of block specified by index.
370 */
371 res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
372 sizeof(size));
373 if (res < 0)
374 return res;
Linus Torvalds1aecbe42018-07-29 12:44:46 -0700375 return squashfs_block_size(size);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000376}
377
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000378/* Copy data into page cache */
379void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
380 int bytes, int offset)
Phillip Lougher1701aec2009-01-05 08:46:24 +0000381{
382 struct inode *inode = page->mapping->host;
383 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000384 void *pageaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300385 int i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000386 int start_index = page->index & ~mask, end_index = start_index | mask;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000387
388 /*
389 * Loop copying datablock into pages. As the datablock likely covers
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300390 * many PAGE_SIZE pages (default block size is 128 KiB) explicitly
Phillip Lougher1701aec2009-01-05 08:46:24 +0000391 * grab the pages from the page cache, except for the page that we've
392 * been called to fill.
393 */
394 for (i = start_index; i <= end_index && bytes > 0; i++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300395 bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
Phillip Lougher1701aec2009-01-05 08:46:24 +0000396 struct page *push_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300397 int avail = buffer ? min_t(int, bytes, PAGE_SIZE) : 0;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000398
399 TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
400
401 push_page = (i == page->index) ? page :
402 grab_cache_page_nowait(page->mapping, i);
403
404 if (!push_page)
405 continue;
406
407 if (PageUptodate(push_page))
408 goto skip_page;
409
Cong Wang53b55e52011-11-25 23:14:36 +0800410 pageaddr = kmap_atomic(push_page);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000411 squashfs_copy_data(pageaddr, buffer, offset, avail);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300412 memset(pageaddr + avail, 0, PAGE_SIZE - avail);
Cong Wang53b55e52011-11-25 23:14:36 +0800413 kunmap_atomic(pageaddr);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000414 flush_dcache_page(push_page);
415 SetPageUptodate(push_page);
416skip_page:
417 unlock_page(push_page);
418 if (i != page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300419 put_page(push_page);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000420 }
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000421}
Phillip Lougher1701aec2009-01-05 08:46:24 +0000422
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000423/* Read datablock stored packed inside a fragment (tail-end packed block) */
424static int squashfs_readpage_fragment(struct page *page)
425{
426 struct inode *inode = page->mapping->host;
427 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
428 struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
429 squashfs_i(inode)->fragment_block,
430 squashfs_i(inode)->fragment_size);
431 int res = buffer->error;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000432
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000433 if (res)
434 ERROR("Unable to read page, block %llx, size %x\n",
435 squashfs_i(inode)->fragment_block,
436 squashfs_i(inode)->fragment_size);
437 else
438 squashfs_copy_cache(page, buffer, i_size_read(inode) &
439 (msblk->block_size - 1),
440 squashfs_i(inode)->fragment_offset);
441
442 squashfs_cache_put(buffer);
443 return res;
444}
445
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700446static int squashfs_readpages_fragment(struct page *page,
447 struct list_head *readahead_pages, struct address_space *mapping)
448{
449 if (!page) {
450 page = lru_to_page(readahead_pages);
451 list_del(&page->lru);
452 if (add_to_page_cache_lru(page, mapping, page->index,
453 mapping_gfp_constraint(mapping, GFP_KERNEL))) {
454 put_page(page);
455 return 0;
456 }
457 }
458 return squashfs_readpage_fragment(page);
459}
460
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000461static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
462{
463 struct inode *inode = page->mapping->host;
464 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
465 int bytes = index == file_end ?
466 (i_size_read(inode) & (msblk->block_size - 1)) :
467 msblk->block_size;
468
469 squashfs_copy_cache(page, NULL, bytes, 0);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000470 return 0;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000471}
472
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700473static int squashfs_readpages_sparse(struct page *page,
474 struct list_head *readahead_pages, int index, int file_end,
475 struct address_space *mapping)
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000476{
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700477 if (!page) {
478 page = lru_to_page(readahead_pages);
479 list_del(&page->lru);
480 if (add_to_page_cache_lru(page, mapping, page->index,
481 mapping_gfp_constraint(mapping, GFP_KERNEL))) {
482 put_page(page);
483 return 0;
484 }
485 }
486 return squashfs_readpage_sparse(page, index, file_end);
487}
488
489static int __squashfs_readpages(struct file *file, struct page *page,
490 struct list_head *readahead_pages, unsigned int nr_pages,
491 struct address_space *mapping)
492{
493 struct inode *inode = mapping->host;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000494 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000495 int file_end = i_size_read(inode) >> msblk->block_log;
496 int res;
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700497
498 do {
499 struct page *cur_page = page ? page
500 : lru_to_page(readahead_pages);
501 int page_index = cur_page->index;
502 int index = page_index >> (msblk->block_log - PAGE_SHIFT);
503
504 if (page_index >= ((i_size_read(inode) + PAGE_SIZE - 1) >>
505 PAGE_SHIFT))
506 return 1;
507
508 if (index < file_end || squashfs_i(inode)->fragment_block ==
509 SQUASHFS_INVALID_BLK) {
510 u64 block = 0;
511 int bsize = read_blocklist(inode, index, &block);
512
513 if (bsize < 0)
514 return -1;
515
516 if (bsize == 0) {
517 res = squashfs_readpages_sparse(page,
518 readahead_pages, index, file_end,
519 mapping);
520 } else {
521 res = squashfs_readpages_block(page,
522 readahead_pages, &nr_pages, mapping,
523 page_index, block, bsize);
524 }
525 } else {
526 res = squashfs_readpages_fragment(page,
527 readahead_pages, mapping);
528 }
529 if (res)
530 return 0;
531 page = NULL;
532 } while (readahead_pages && !list_empty(readahead_pages));
533
534 return 0;
535}
536
537static int squashfs_readpage(struct file *file, struct page *page)
538{
539 int ret;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000540
541 TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700542 page->index, squashfs_i(page->mapping->host)->start);
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000543
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700544 get_page(page);
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000545
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700546 ret = __squashfs_readpages(file, page, NULL, 1, page->mapping);
547 if (ret) {
548 flush_dcache_page(page);
549 if (ret < 0)
550 SetPageError(page);
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000551 else
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700552 SetPageUptodate(page);
553 zero_user_segment(page, 0, PAGE_SIZE);
554 unlock_page(page);
555 put_page(page);
556 }
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000557
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700558 return 0;
559}
Phillip Lougher1701aec2009-01-05 08:46:24 +0000560
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700561static int squashfs_readpages(struct file *file, struct address_space *mapping,
562 struct list_head *pages, unsigned int nr_pages)
563{
564 TRACE("Entered squashfs_readpages, %u pages, first page index %lx\n",
565 nr_pages, lru_to_page(pages)->index);
566 __squashfs_readpages(file, NULL, pages, nr_pages, mapping);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000567 return 0;
568}
569
570
571const struct address_space_operations squashfs_aops = {
Adrien Schildknechtd840c1d2016-10-14 21:03:54 -0700572 .readpage = squashfs_readpage,
573 .readpages = squashfs_readpages,
Phillip Lougher1701aec2009-01-05 08:46:24 +0000574};