blob: fcff2e0487fef11f89724a72221090e529f0a775 [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>
Phillip Lougher1701aec2009-01-05 08:46:24 +000050
51#include "squashfs_fs.h"
52#include "squashfs_fs_sb.h"
53#include "squashfs_fs_i.h"
54#include "squashfs.h"
55
56/*
57 * Locate cache slot in range [offset, index] for specified inode. If
58 * there's more than one return the slot closest to index.
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 * Find and initialise an empty cache slot for index offset.
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 * First time cache index has been used, allocate and
113 * initialise. The cache index could be allocated at
114 * mount time but doing it here means it is allocated only
115 * if a 'large' file is read.
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 * Read the next n blocks from the block list, starting from
171 * metadata block <start_block, offset>.
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;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300178 __le32 *blist = kmalloc(PAGE_SIZE, GFP_KERNEL);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000179
180 if (blist == NULL) {
181 ERROR("read_indexes: Failed to allocate block_list\n");
182 return -ENOMEM;
183 }
184
185 while (n) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300186 int blocks = min_t(int, n, PAGE_SIZE >> 2);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000187
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++) {
Linus Torvalds1aecbe42018-07-29 12:44:46 -0700197 int size = squashfs_block_size(blist[i]);
198 if (size < 0) {
199 err = size;
200 goto failure;
201 }
Phillip Lougher1701aec2009-01-05 08:46:24 +0000202 block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
203 }
204 n -= blocks;
205 }
206
207 kfree(blist);
208 return block;
209
210failure:
211 kfree(blist);
212 return err;
213}
214
215
216/*
217 * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
218 * can cache one index -> datablock/blocklist-block mapping. We wish
219 * to distribute these over the length of the file, entry[0] maps index x,
220 * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
221 * The larger the file, the greater the skip factor. The skip factor is
222 * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
223 * the number of metadata blocks that need to be read fits into the cache.
224 * If the skip factor is limited in this way then the file will use multiple
225 * slots.
226 */
227static inline int calculate_skip(int blocks)
228{
229 int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
230 * SQUASHFS_META_INDEXES);
231 return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
232}
233
234
235/*
236 * Search and grow the index cache for the specified inode, returning the
237 * on-disk locations of the datablock and block list metadata block
238 * <index_block, index_offset> for index (scaled to nearest cache index).
239 */
240static int fill_meta_index(struct inode *inode, int index,
241 u64 *index_block, int *index_offset, u64 *data_block)
242{
243 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
244 int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
245 int offset = 0;
246 struct meta_index *meta;
247 struct meta_entry *meta_entry;
248 u64 cur_index_block = squashfs_i(inode)->block_list_start;
249 int cur_offset = squashfs_i(inode)->offset;
250 u64 cur_data_block = squashfs_i(inode)->start;
251 int err, i;
252
253 /*
254 * Scale index to cache index (cache slot entry)
255 */
256 index /= SQUASHFS_META_INDEXES * skip;
257
258 while (offset < index) {
259 meta = locate_meta_index(inode, offset + 1, index);
260
261 if (meta == NULL) {
262 meta = empty_meta_index(inode, offset + 1, skip);
263 if (meta == NULL)
264 goto all_done;
265 } else {
266 offset = index < meta->offset + meta->entries ? index :
267 meta->offset + meta->entries - 1;
268 meta_entry = &meta->meta_entry[offset - meta->offset];
269 cur_index_block = meta_entry->index_block +
270 msblk->inode_table;
271 cur_offset = meta_entry->offset;
272 cur_data_block = meta_entry->data_block;
273 TRACE("get_meta_index: offset %d, meta->offset %d, "
274 "meta->entries %d\n", offset, meta->offset,
275 meta->entries);
276 TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
277 " data_block 0x%llx\n", cur_index_block,
278 cur_offset, cur_data_block);
279 }
280
281 /*
282 * If necessary grow cache slot by reading block list. Cache
283 * slot is extended up to index or to the end of the slot, in
284 * which case further slots will be used.
285 */
286 for (i = meta->offset + meta->entries; i <= index &&
287 i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
288 int blocks = skip * SQUASHFS_META_INDEXES;
289 long long res = read_indexes(inode->i_sb, blocks,
290 &cur_index_block, &cur_offset);
291
292 if (res < 0) {
293 if (meta->entries == 0)
294 /*
295 * Don't leave an empty slot on read
296 * error allocated to this inode...
297 */
298 meta->inode_number = 0;
299 err = res;
300 goto failed;
301 }
302
303 cur_data_block += res;
304 meta_entry = &meta->meta_entry[i - meta->offset];
305 meta_entry->index_block = cur_index_block -
306 msblk->inode_table;
307 meta_entry->offset = cur_offset;
308 meta_entry->data_block = cur_data_block;
309 meta->entries++;
310 offset++;
311 }
312
313 TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
314 meta->offset, meta->entries);
315
316 release_meta_index(inode, meta);
317 }
318
319all_done:
320 *index_block = cur_index_block;
321 *index_offset = cur_offset;
322 *data_block = cur_data_block;
323
324 /*
325 * Scale cache index (cache slot entry) to index
326 */
327 return offset * SQUASHFS_META_INDEXES * skip;
328
329failed:
330 release_meta_index(inode, meta);
331 return err;
332}
333
334
335/*
336 * Get the on-disk location and compressed size of the datablock
337 * specified by index. Fill_meta_index() does most of the work.
338 */
339static int read_blocklist(struct inode *inode, int index, u64 *block)
340{
341 u64 start;
342 long long blks;
343 int offset;
344 __le32 size;
345 int res = fill_meta_index(inode, index, &start, &offset, block);
346
347 TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
348 " 0x%x, block 0x%llx\n", res, index, start, offset,
349 *block);
350
351 if (res < 0)
352 return res;
353
354 /*
355 * res contains the index of the mapping returned by fill_meta_index(),
356 * this will likely be less than the desired index (because the
357 * meta_index cache works at a higher granularity). Read any
358 * extra block indexes needed.
359 */
360 if (res < index) {
361 blks = read_indexes(inode->i_sb, index - res, &start, &offset);
362 if (blks < 0)
363 return (int) blks;
364 *block += blks;
365 }
366
367 /*
368 * Read length of block specified by index.
369 */
370 res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
371 sizeof(size));
372 if (res < 0)
373 return res;
Linus Torvalds1aecbe42018-07-29 12:44:46 -0700374 return squashfs_block_size(size);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000375}
376
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000377/* Copy data into page cache */
378void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
379 int bytes, int offset)
Phillip Lougher1701aec2009-01-05 08:46:24 +0000380{
381 struct inode *inode = page->mapping->host;
382 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000383 void *pageaddr;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300384 int i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000385 int start_index = page->index & ~mask, end_index = start_index | mask;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000386
387 /*
388 * Loop copying datablock into pages. As the datablock likely covers
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +0300389 * many PAGE_SIZE pages (default block size is 128 KiB) explicitly
Phillip Lougher1701aec2009-01-05 08:46:24 +0000390 * grab the pages from the page cache, except for the page that we've
391 * been called to fill.
392 */
393 for (i = start_index; i <= end_index && bytes > 0; i++,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300394 bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
Phillip Lougher1701aec2009-01-05 08:46:24 +0000395 struct page *push_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300396 int avail = buffer ? min_t(int, bytes, PAGE_SIZE) : 0;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000397
398 TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
399
400 push_page = (i == page->index) ? page :
401 grab_cache_page_nowait(page->mapping, i);
402
403 if (!push_page)
404 continue;
405
406 if (PageUptodate(push_page))
407 goto skip_page;
408
Cong Wang53b55e52011-11-25 23:14:36 +0800409 pageaddr = kmap_atomic(push_page);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000410 squashfs_copy_data(pageaddr, buffer, offset, avail);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300411 memset(pageaddr + avail, 0, PAGE_SIZE - avail);
Cong Wang53b55e52011-11-25 23:14:36 +0800412 kunmap_atomic(pageaddr);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000413 flush_dcache_page(push_page);
414 SetPageUptodate(push_page);
415skip_page:
416 unlock_page(push_page);
417 if (i != page->index)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300418 put_page(push_page);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000419 }
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000420}
Phillip Lougher1701aec2009-01-05 08:46:24 +0000421
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000422/* Read datablock stored packed inside a fragment (tail-end packed block) */
423static int squashfs_readpage_fragment(struct page *page)
424{
425 struct inode *inode = page->mapping->host;
426 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
427 struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
428 squashfs_i(inode)->fragment_block,
429 squashfs_i(inode)->fragment_size);
430 int res = buffer->error;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000431
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000432 if (res)
433 ERROR("Unable to read page, block %llx, size %x\n",
434 squashfs_i(inode)->fragment_block,
435 squashfs_i(inode)->fragment_size);
436 else
437 squashfs_copy_cache(page, buffer, i_size_read(inode) &
438 (msblk->block_size - 1),
439 squashfs_i(inode)->fragment_offset);
440
441 squashfs_cache_put(buffer);
442 return res;
443}
444
445static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
446{
447 struct inode *inode = page->mapping->host;
448 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
449 int bytes = index == file_end ?
450 (i_size_read(inode) & (msblk->block_size - 1)) :
451 msblk->block_size;
452
453 squashfs_copy_cache(page, NULL, bytes, 0);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000454 return 0;
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000455}
456
457static int squashfs_readpage(struct file *file, struct page *page)
458{
459 struct inode *inode = page->mapping->host;
460 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300461 int index = page->index >> (msblk->block_log - PAGE_SHIFT);
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000462 int file_end = i_size_read(inode) >> msblk->block_log;
463 int res;
464 void *pageaddr;
465
466 TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
467 page->index, squashfs_i(inode)->start);
468
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300469 if (page->index >= ((i_size_read(inode) + PAGE_SIZE - 1) >>
470 PAGE_SHIFT))
Phillip Lougher5f55dbc2013-10-31 19:24:27 +0000471 goto out;
472
473 if (index < file_end || squashfs_i(inode)->fragment_block ==
474 SQUASHFS_INVALID_BLK) {
475 u64 block = 0;
476 int bsize = read_blocklist(inode, index, &block);
477 if (bsize < 0)
478 goto error_out;
479
480 if (bsize == 0)
481 res = squashfs_readpage_sparse(page, index, file_end);
482 else
483 res = squashfs_readpage_block(page, block, bsize);
484 } else
485 res = squashfs_readpage_fragment(page);
486
487 if (!res)
488 return 0;
Phillip Lougher1701aec2009-01-05 08:46:24 +0000489
490error_out:
491 SetPageError(page);
492out:
Cong Wang53b55e52011-11-25 23:14:36 +0800493 pageaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300494 memset(pageaddr, 0, PAGE_SIZE);
Cong Wang53b55e52011-11-25 23:14:36 +0800495 kunmap_atomic(pageaddr);
Phillip Lougher1701aec2009-01-05 08:46:24 +0000496 flush_dcache_page(page);
497 if (!PageError(page))
498 SetPageUptodate(page);
499 unlock_page(page);
500
501 return 0;
502}
503
504
505const struct address_space_operations squashfs_aops = {
506 .readpage = squashfs_readpage
507};