2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 * Phillip Lougher <phillip@squashfs.org.uk>
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.
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.
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.
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).
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.
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
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 #include <linux/mm_inline.h>
52 #include "squashfs_fs.h"
53 #include "squashfs_fs_sb.h"
54 #include "squashfs_fs_i.h"
57 // Backported from 4.5
58 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
61 * Locate cache slot in range [offset, index] for specified inode. If
62 * there's more than one return the slot closest to index.
64 static struct meta_index *locate_meta_index(struct inode *inode, int offset,
67 struct meta_index *meta = NULL;
68 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
71 mutex_lock(&msblk->meta_index_mutex);
73 TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
75 if (msblk->meta_index == NULL)
78 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
79 if (msblk->meta_index[i].inode_number == inode->i_ino &&
80 msblk->meta_index[i].offset >= offset &&
81 msblk->meta_index[i].offset <= index &&
82 msblk->meta_index[i].locked == 0) {
83 TRACE("locate_meta_index: entry %d, offset %d\n", i,
84 msblk->meta_index[i].offset);
85 meta = &msblk->meta_index[i];
86 offset = meta->offset;
94 mutex_unlock(&msblk->meta_index_mutex);
101 * Find and initialise an empty cache slot for index offset.
103 static struct meta_index *empty_meta_index(struct inode *inode, int offset,
106 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
107 struct meta_index *meta = NULL;
110 mutex_lock(&msblk->meta_index_mutex);
112 TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
114 if (msblk->meta_index == NULL) {
116 * First time cache index has been used, allocate and
117 * initialise. The cache index could be allocated at
118 * mount time but doing it here means it is allocated only
119 * if a 'large' file is read.
121 msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
122 sizeof(*(msblk->meta_index)), GFP_KERNEL);
123 if (msblk->meta_index == NULL) {
124 ERROR("Failed to allocate meta_index\n");
127 for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
128 msblk->meta_index[i].inode_number = 0;
129 msblk->meta_index[i].locked = 0;
131 msblk->next_meta_index = 0;
134 for (i = SQUASHFS_META_SLOTS; i &&
135 msblk->meta_index[msblk->next_meta_index].locked; i--)
136 msblk->next_meta_index = (msblk->next_meta_index + 1) %
140 TRACE("empty_meta_index: failed!\n");
144 TRACE("empty_meta_index: returned meta entry %d, %p\n",
145 msblk->next_meta_index,
146 &msblk->meta_index[msblk->next_meta_index]);
148 meta = &msblk->meta_index[msblk->next_meta_index];
149 msblk->next_meta_index = (msblk->next_meta_index + 1) %
152 meta->inode_number = inode->i_ino;
153 meta->offset = offset;
159 mutex_unlock(&msblk->meta_index_mutex);
164 static void release_meta_index(struct inode *inode, struct meta_index *meta)
166 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
167 mutex_lock(&msblk->meta_index_mutex);
169 mutex_unlock(&msblk->meta_index_mutex);
174 * Read the next n blocks from the block list, starting from
175 * metadata block <start_block, offset>.
177 static long long read_indexes(struct super_block *sb, int n,
178 u64 *start_block, int *offset)
182 __le32 *blist = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
185 ERROR("read_indexes: Failed to allocate block_list\n");
190 int blocks = min_t(int, n, PAGE_CACHE_SIZE >> 2);
192 err = squashfs_read_metadata(sb, blist, start_block,
193 offset, blocks << 2);
195 ERROR("read_indexes: reading block [%llx:%x]\n",
196 *start_block, *offset);
200 for (i = 0; i < blocks; i++) {
201 int size = le32_to_cpu(blist[i]);
202 block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
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
227 static inline int calculate_skip(int blocks)
229 int skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
230 * SQUASHFS_META_INDEXES);
231 return min(SQUASHFS_CACHED_BLKS - 1, skip + 1);
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).
240 static int fill_meta_index(struct inode *inode, int index,
241 u64 *index_block, int *index_offset, u64 *data_block)
243 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
244 int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
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;
254 * Scale index to cache index (cache slot entry)
256 index /= SQUASHFS_META_INDEXES * skip;
258 while (offset < index) {
259 meta = locate_meta_index(inode, offset + 1, index);
262 meta = empty_meta_index(inode, offset + 1, skip);
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 +
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,
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);
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.
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);
293 if (meta->entries == 0)
295 * Don't leave an empty slot on read
296 * error allocated to this inode...
298 meta->inode_number = 0;
303 cur_data_block += res;
304 meta_entry = &meta->meta_entry[i - meta->offset];
305 meta_entry->index_block = cur_index_block -
307 meta_entry->offset = cur_offset;
308 meta_entry->data_block = cur_data_block;
313 TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
314 meta->offset, meta->entries);
316 release_meta_index(inode, meta);
320 *index_block = cur_index_block;
321 *index_offset = cur_offset;
322 *data_block = cur_data_block;
325 * Scale cache index (cache slot entry) to index
327 return offset * SQUASHFS_META_INDEXES * skip;
330 release_meta_index(inode, meta);
336 * Get the on-disk location and compressed size of the datablock
337 * specified by index. Fill_meta_index() does most of the work.
339 static int read_blocklist(struct inode *inode, int index, u64 *block)
345 int res = fill_meta_index(inode, index, &start, &offset, block);
347 TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
348 " 0x%x, block 0x%llx\n", res, index, start, offset,
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.
361 blks = read_indexes(inode->i_sb, index - res, &start, &offset);
368 * Read length of block specified by index.
370 res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
374 return le32_to_cpu(size);
377 /* Copy data into page cache */
378 void squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
379 int bytes, int offset)
381 struct inode *inode = page->mapping->host;
382 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
384 int i, mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
385 int start_index = page->index & ~mask, end_index = start_index | mask;
388 * Loop copying datablock into pages. As the datablock likely covers
389 * many PAGE_CACHE_SIZE pages (default block size is 128 KiB) explicitly
390 * grab the pages from the page cache, except for the page that we've
391 * been called to fill.
393 for (i = start_index; i <= end_index && bytes > 0; i++,
394 bytes -= PAGE_CACHE_SIZE, offset += PAGE_CACHE_SIZE) {
395 struct page *push_page;
396 int avail = buffer ? min_t(int, bytes, PAGE_CACHE_SIZE) : 0;
398 TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
400 push_page = (i == page->index) ? page :
401 grab_cache_page_nowait(page->mapping, i);
406 if (PageUptodate(push_page))
409 pageaddr = kmap_atomic(push_page);
410 squashfs_copy_data(pageaddr, buffer, offset, avail);
411 memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
412 kunmap_atomic(pageaddr);
413 flush_dcache_page(push_page);
414 SetPageUptodate(push_page);
416 unlock_page(push_page);
417 if (i != page->index)
418 page_cache_release(push_page);
422 /* Read datablock stored packed inside a fragment (tail-end packed block) */
423 static int squashfs_readpage_fragment(struct page *page)
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;
433 ERROR("Unable to read page, block %llx, size %x\n",
434 squashfs_i(inode)->fragment_block,
435 squashfs_i(inode)->fragment_size);
437 squashfs_copy_cache(page, buffer, i_size_read(inode) &
438 (msblk->block_size - 1),
439 squashfs_i(inode)->fragment_offset);
441 squashfs_cache_put(buffer);
445 static int squashfs_readpages_fragment(struct page *page,
446 struct list_head *readahead_pages, struct address_space *mapping)
449 page = lru_to_page(readahead_pages);
450 list_del(&page->lru);
451 if (add_to_page_cache_lru(page, mapping, page->index,
452 mapping_gfp_constraint(mapping, GFP_KERNEL))) {
457 return squashfs_readpage_fragment(page);
460 static int squashfs_readpage_sparse(struct page *page, int index, int file_end)
462 struct inode *inode = page->mapping->host;
463 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
464 int bytes = index == file_end ?
465 (i_size_read(inode) & (msblk->block_size - 1)) :
468 squashfs_copy_cache(page, NULL, bytes, 0);
472 static int squashfs_readpages_sparse(struct page *page,
473 struct list_head *readahead_pages, int index, int file_end,
474 struct address_space *mapping)
477 page = lru_to_page(readahead_pages);
478 list_del(&page->lru);
479 if (add_to_page_cache_lru(page, mapping, page->index,
480 mapping_gfp_constraint(mapping, GFP_KERNEL))) {
485 return squashfs_readpage_sparse(page, index, file_end);
488 static int __squashfs_readpages(struct file *file, struct page *page,
489 struct list_head *readahead_pages, unsigned int nr_pages,
490 struct address_space *mapping)
492 struct inode *inode = mapping->host;
493 struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
494 int file_end = i_size_read(inode) >> msblk->block_log;
498 struct page *cur_page = page ? page
499 : lru_to_page(readahead_pages);
500 int page_index = cur_page->index;
501 int index = page_index >> (msblk->block_log - PAGE_CACHE_SHIFT);
503 if (page_index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
507 if (index < file_end || squashfs_i(inode)->fragment_block ==
508 SQUASHFS_INVALID_BLK) {
510 int bsize = read_blocklist(inode, index, &block);
516 res = squashfs_readpages_sparse(page,
517 readahead_pages, index, file_end,
520 res = squashfs_readpages_block(page,
521 readahead_pages, &nr_pages, mapping,
522 page_index, block, bsize);
525 res = squashfs_readpages_fragment(page,
526 readahead_pages, mapping);
531 } while (readahead_pages && !list_empty(readahead_pages));
536 static int squashfs_readpage(struct file *file, struct page *page)
540 TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
541 page->index, squashfs_i(page->mapping->host)->start);
545 ret = __squashfs_readpages(file, page, NULL, 1, page->mapping);
547 flush_dcache_page(page);
551 SetPageUptodate(page);
552 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
560 static int squashfs_readpages(struct file *file, struct address_space *mapping,
561 struct list_head *pages, unsigned int nr_pages)
563 TRACE("Entered squashfs_readpages, %u pages, first page index %lx\n",
564 nr_pages, lru_to_page(pages)->index);
565 __squashfs_readpages(file, NULL, pages, nr_pages, mapping);
570 const struct address_space_operations squashfs_aops = {
571 .readpage = squashfs_readpage,
572 .readpages = squashfs_readpages,