18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
68c2ecf20Sopenharmony_ci * Phillip Lougher <phillip@squashfs.org.uk>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * file.c
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file contains code for handling regular files.  A regular file
138c2ecf20Sopenharmony_ci * consists of a sequence of contiguous compressed blocks, and/or a
148c2ecf20Sopenharmony_ci * compressed fragment block (tail-end packed block).   The compressed size
158c2ecf20Sopenharmony_ci * of each datablock is stored in a block list contained within the
168c2ecf20Sopenharmony_ci * file inode (itself stored in one or more compressed metadata blocks).
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * To speed up access to datablocks when reading 'large' files (256 Mbytes or
198c2ecf20Sopenharmony_ci * larger), the code implements an index cache that caches the mapping from
208c2ecf20Sopenharmony_ci * block index to datablock location on disk.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * The index cache allows Squashfs to handle large files (up to 1.75 TiB) while
238c2ecf20Sopenharmony_ci * retaining a simple and space-efficient block list on disk.  The cache
248c2ecf20Sopenharmony_ci * is split into slots, caching up to eight 224 GiB files (128 KiB blocks).
258c2ecf20Sopenharmony_ci * Larger files use multiple slots, with 1.75 TiB files using all 8 slots.
268c2ecf20Sopenharmony_ci * The index cache is designed to be memory efficient, and by default uses
278c2ecf20Sopenharmony_ci * 16 KiB.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/fs.h>
318c2ecf20Sopenharmony_ci#include <linux/vfs.h>
328c2ecf20Sopenharmony_ci#include <linux/kernel.h>
338c2ecf20Sopenharmony_ci#include <linux/slab.h>
348c2ecf20Sopenharmony_ci#include <linux/string.h>
358c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
368c2ecf20Sopenharmony_ci#include <linux/mutex.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include "squashfs_fs.h"
398c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h"
408c2ecf20Sopenharmony_ci#include "squashfs_fs_i.h"
418c2ecf20Sopenharmony_ci#include "squashfs.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * Locate cache slot in range [offset, index] for specified inode.  If
458c2ecf20Sopenharmony_ci * there's more than one return the slot closest to index.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic struct meta_index *locate_meta_index(struct inode *inode, int offset,
488c2ecf20Sopenharmony_ci				int index)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct meta_index *meta = NULL;
518c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
528c2ecf20Sopenharmony_ci	int i;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	mutex_lock(&msblk->meta_index_mutex);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (msblk->meta_index == NULL)
598c2ecf20Sopenharmony_ci		goto not_allocated;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
628c2ecf20Sopenharmony_ci		if (msblk->meta_index[i].inode_number == inode->i_ino &&
638c2ecf20Sopenharmony_ci				msblk->meta_index[i].offset >= offset &&
648c2ecf20Sopenharmony_ci				msblk->meta_index[i].offset <= index &&
658c2ecf20Sopenharmony_ci				msblk->meta_index[i].locked == 0) {
668c2ecf20Sopenharmony_ci			TRACE("locate_meta_index: entry %d, offset %d\n", i,
678c2ecf20Sopenharmony_ci					msblk->meta_index[i].offset);
688c2ecf20Sopenharmony_ci			meta = &msblk->meta_index[i];
698c2ecf20Sopenharmony_ci			offset = meta->offset;
708c2ecf20Sopenharmony_ci		}
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (meta)
748c2ecf20Sopenharmony_ci		meta->locked = 1;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cinot_allocated:
778c2ecf20Sopenharmony_ci	mutex_unlock(&msblk->meta_index_mutex);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return meta;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * Find and initialise an empty cache slot for index offset.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic struct meta_index *empty_meta_index(struct inode *inode, int offset,
878c2ecf20Sopenharmony_ci				int skip)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
908c2ecf20Sopenharmony_ci	struct meta_index *meta = NULL;
918c2ecf20Sopenharmony_ci	int i;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	mutex_lock(&msblk->meta_index_mutex);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (msblk->meta_index == NULL) {
988c2ecf20Sopenharmony_ci		/*
998c2ecf20Sopenharmony_ci		 * First time cache index has been used, allocate and
1008c2ecf20Sopenharmony_ci		 * initialise.  The cache index could be allocated at
1018c2ecf20Sopenharmony_ci		 * mount time but doing it here means it is allocated only
1028c2ecf20Sopenharmony_ci		 * if a 'large' file is read.
1038c2ecf20Sopenharmony_ci		 */
1048c2ecf20Sopenharmony_ci		msblk->meta_index = kcalloc(SQUASHFS_META_SLOTS,
1058c2ecf20Sopenharmony_ci			sizeof(*(msblk->meta_index)), GFP_KERNEL);
1068c2ecf20Sopenharmony_ci		if (msblk->meta_index == NULL) {
1078c2ecf20Sopenharmony_ci			ERROR("Failed to allocate meta_index\n");
1088c2ecf20Sopenharmony_ci			goto failed;
1098c2ecf20Sopenharmony_ci		}
1108c2ecf20Sopenharmony_ci		for (i = 0; i < SQUASHFS_META_SLOTS; i++) {
1118c2ecf20Sopenharmony_ci			msblk->meta_index[i].inode_number = 0;
1128c2ecf20Sopenharmony_ci			msblk->meta_index[i].locked = 0;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci		msblk->next_meta_index = 0;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	for (i = SQUASHFS_META_SLOTS; i &&
1188c2ecf20Sopenharmony_ci			msblk->meta_index[msblk->next_meta_index].locked; i--)
1198c2ecf20Sopenharmony_ci		msblk->next_meta_index = (msblk->next_meta_index + 1) %
1208c2ecf20Sopenharmony_ci			SQUASHFS_META_SLOTS;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (i == 0) {
1238c2ecf20Sopenharmony_ci		TRACE("empty_meta_index: failed!\n");
1248c2ecf20Sopenharmony_ci		goto failed;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	TRACE("empty_meta_index: returned meta entry %d, %p\n",
1288c2ecf20Sopenharmony_ci			msblk->next_meta_index,
1298c2ecf20Sopenharmony_ci			&msblk->meta_index[msblk->next_meta_index]);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	meta = &msblk->meta_index[msblk->next_meta_index];
1328c2ecf20Sopenharmony_ci	msblk->next_meta_index = (msblk->next_meta_index + 1) %
1338c2ecf20Sopenharmony_ci			SQUASHFS_META_SLOTS;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	meta->inode_number = inode->i_ino;
1368c2ecf20Sopenharmony_ci	meta->offset = offset;
1378c2ecf20Sopenharmony_ci	meta->skip = skip;
1388c2ecf20Sopenharmony_ci	meta->entries = 0;
1398c2ecf20Sopenharmony_ci	meta->locked = 1;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cifailed:
1428c2ecf20Sopenharmony_ci	mutex_unlock(&msblk->meta_index_mutex);
1438c2ecf20Sopenharmony_ci	return meta;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic void release_meta_index(struct inode *inode, struct meta_index *meta)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
1508c2ecf20Sopenharmony_ci	mutex_lock(&msblk->meta_index_mutex);
1518c2ecf20Sopenharmony_ci	meta->locked = 0;
1528c2ecf20Sopenharmony_ci	mutex_unlock(&msblk->meta_index_mutex);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/*
1578c2ecf20Sopenharmony_ci * Read the next n blocks from the block list, starting from
1588c2ecf20Sopenharmony_ci * metadata block <start_block, offset>.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic long long read_indexes(struct super_block *sb, int n,
1618c2ecf20Sopenharmony_ci				u64 *start_block, int *offset)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int err, i;
1648c2ecf20Sopenharmony_ci	long long block = 0;
1658c2ecf20Sopenharmony_ci	__le32 *blist = kmalloc(PAGE_SIZE, GFP_KERNEL);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (blist == NULL) {
1688c2ecf20Sopenharmony_ci		ERROR("read_indexes: Failed to allocate block_list\n");
1698c2ecf20Sopenharmony_ci		return -ENOMEM;
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	while (n) {
1738c2ecf20Sopenharmony_ci		int blocks = min_t(int, n, PAGE_SIZE >> 2);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		err = squashfs_read_metadata(sb, blist, start_block,
1768c2ecf20Sopenharmony_ci				offset, blocks << 2);
1778c2ecf20Sopenharmony_ci		if (err < 0) {
1788c2ecf20Sopenharmony_ci			ERROR("read_indexes: reading block [%llx:%x]\n",
1798c2ecf20Sopenharmony_ci				*start_block, *offset);
1808c2ecf20Sopenharmony_ci			goto failure;
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		for (i = 0; i < blocks; i++) {
1848c2ecf20Sopenharmony_ci			int size = squashfs_block_size(blist[i]);
1858c2ecf20Sopenharmony_ci			if (size < 0) {
1868c2ecf20Sopenharmony_ci				err = size;
1878c2ecf20Sopenharmony_ci				goto failure;
1888c2ecf20Sopenharmony_ci			}
1898c2ecf20Sopenharmony_ci			block += SQUASHFS_COMPRESSED_SIZE_BLOCK(size);
1908c2ecf20Sopenharmony_ci		}
1918c2ecf20Sopenharmony_ci		n -= blocks;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	kfree(blist);
1958c2ecf20Sopenharmony_ci	return block;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cifailure:
1988c2ecf20Sopenharmony_ci	kfree(blist);
1998c2ecf20Sopenharmony_ci	return err;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/*
2048c2ecf20Sopenharmony_ci * Each cache index slot has SQUASHFS_META_ENTRIES, each of which
2058c2ecf20Sopenharmony_ci * can cache one index -> datablock/blocklist-block mapping.  We wish
2068c2ecf20Sopenharmony_ci * to distribute these over the length of the file, entry[0] maps index x,
2078c2ecf20Sopenharmony_ci * entry[1] maps index x + skip, entry[2] maps index x + 2 * skip, and so on.
2088c2ecf20Sopenharmony_ci * The larger the file, the greater the skip factor.  The skip factor is
2098c2ecf20Sopenharmony_ci * limited to the size of the metadata cache (SQUASHFS_CACHED_BLKS) to ensure
2108c2ecf20Sopenharmony_ci * the number of metadata blocks that need to be read fits into the cache.
2118c2ecf20Sopenharmony_ci * If the skip factor is limited in this way then the file will use multiple
2128c2ecf20Sopenharmony_ci * slots.
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_cistatic inline int calculate_skip(u64 blocks)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	u64 skip = blocks / ((SQUASHFS_META_ENTRIES + 1)
2178c2ecf20Sopenharmony_ci		 * SQUASHFS_META_INDEXES);
2188c2ecf20Sopenharmony_ci	return min((u64) SQUASHFS_CACHED_BLKS - 1, skip + 1);
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/*
2238c2ecf20Sopenharmony_ci * Search and grow the index cache for the specified inode, returning the
2248c2ecf20Sopenharmony_ci * on-disk locations of the datablock and block list metadata block
2258c2ecf20Sopenharmony_ci * <index_block, index_offset> for index (scaled to nearest cache index).
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_cistatic int fill_meta_index(struct inode *inode, int index,
2288c2ecf20Sopenharmony_ci		u64 *index_block, int *index_offset, u64 *data_block)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
2318c2ecf20Sopenharmony_ci	int skip = calculate_skip(i_size_read(inode) >> msblk->block_log);
2328c2ecf20Sopenharmony_ci	int offset = 0;
2338c2ecf20Sopenharmony_ci	struct meta_index *meta;
2348c2ecf20Sopenharmony_ci	struct meta_entry *meta_entry;
2358c2ecf20Sopenharmony_ci	u64 cur_index_block = squashfs_i(inode)->block_list_start;
2368c2ecf20Sopenharmony_ci	int cur_offset = squashfs_i(inode)->offset;
2378c2ecf20Sopenharmony_ci	u64 cur_data_block = squashfs_i(inode)->start;
2388c2ecf20Sopenharmony_ci	int err, i;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/*
2418c2ecf20Sopenharmony_ci	 * Scale index to cache index (cache slot entry)
2428c2ecf20Sopenharmony_ci	 */
2438c2ecf20Sopenharmony_ci	index /= SQUASHFS_META_INDEXES * skip;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	while (offset < index) {
2468c2ecf20Sopenharmony_ci		meta = locate_meta_index(inode, offset + 1, index);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci		if (meta == NULL) {
2498c2ecf20Sopenharmony_ci			meta = empty_meta_index(inode, offset + 1, skip);
2508c2ecf20Sopenharmony_ci			if (meta == NULL)
2518c2ecf20Sopenharmony_ci				goto all_done;
2528c2ecf20Sopenharmony_ci		} else {
2538c2ecf20Sopenharmony_ci			offset = index < meta->offset + meta->entries ? index :
2548c2ecf20Sopenharmony_ci				meta->offset + meta->entries - 1;
2558c2ecf20Sopenharmony_ci			meta_entry = &meta->meta_entry[offset - meta->offset];
2568c2ecf20Sopenharmony_ci			cur_index_block = meta_entry->index_block +
2578c2ecf20Sopenharmony_ci				msblk->inode_table;
2588c2ecf20Sopenharmony_ci			cur_offset = meta_entry->offset;
2598c2ecf20Sopenharmony_ci			cur_data_block = meta_entry->data_block;
2608c2ecf20Sopenharmony_ci			TRACE("get_meta_index: offset %d, meta->offset %d, "
2618c2ecf20Sopenharmony_ci				"meta->entries %d\n", offset, meta->offset,
2628c2ecf20Sopenharmony_ci				meta->entries);
2638c2ecf20Sopenharmony_ci			TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
2648c2ecf20Sopenharmony_ci				" data_block 0x%llx\n", cur_index_block,
2658c2ecf20Sopenharmony_ci				cur_offset, cur_data_block);
2668c2ecf20Sopenharmony_ci		}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		/*
2698c2ecf20Sopenharmony_ci		 * If necessary grow cache slot by reading block list.  Cache
2708c2ecf20Sopenharmony_ci		 * slot is extended up to index or to the end of the slot, in
2718c2ecf20Sopenharmony_ci		 * which case further slots will be used.
2728c2ecf20Sopenharmony_ci		 */
2738c2ecf20Sopenharmony_ci		for (i = meta->offset + meta->entries; i <= index &&
2748c2ecf20Sopenharmony_ci				i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
2758c2ecf20Sopenharmony_ci			int blocks = skip * SQUASHFS_META_INDEXES;
2768c2ecf20Sopenharmony_ci			long long res = read_indexes(inode->i_sb, blocks,
2778c2ecf20Sopenharmony_ci					&cur_index_block, &cur_offset);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci			if (res < 0) {
2808c2ecf20Sopenharmony_ci				if (meta->entries == 0)
2818c2ecf20Sopenharmony_ci					/*
2828c2ecf20Sopenharmony_ci					 * Don't leave an empty slot on read
2838c2ecf20Sopenharmony_ci					 * error allocated to this inode...
2848c2ecf20Sopenharmony_ci					 */
2858c2ecf20Sopenharmony_ci					meta->inode_number = 0;
2868c2ecf20Sopenharmony_ci				err = res;
2878c2ecf20Sopenharmony_ci				goto failed;
2888c2ecf20Sopenharmony_ci			}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci			cur_data_block += res;
2918c2ecf20Sopenharmony_ci			meta_entry = &meta->meta_entry[i - meta->offset];
2928c2ecf20Sopenharmony_ci			meta_entry->index_block = cur_index_block -
2938c2ecf20Sopenharmony_ci				msblk->inode_table;
2948c2ecf20Sopenharmony_ci			meta_entry->offset = cur_offset;
2958c2ecf20Sopenharmony_ci			meta_entry->data_block = cur_data_block;
2968c2ecf20Sopenharmony_ci			meta->entries++;
2978c2ecf20Sopenharmony_ci			offset++;
2988c2ecf20Sopenharmony_ci		}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
3018c2ecf20Sopenharmony_ci				meta->offset, meta->entries);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci		release_meta_index(inode, meta);
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ciall_done:
3078c2ecf20Sopenharmony_ci	*index_block = cur_index_block;
3088c2ecf20Sopenharmony_ci	*index_offset = cur_offset;
3098c2ecf20Sopenharmony_ci	*data_block = cur_data_block;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/*
3128c2ecf20Sopenharmony_ci	 * Scale cache index (cache slot entry) to index
3138c2ecf20Sopenharmony_ci	 */
3148c2ecf20Sopenharmony_ci	return offset * SQUASHFS_META_INDEXES * skip;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cifailed:
3178c2ecf20Sopenharmony_ci	release_meta_index(inode, meta);
3188c2ecf20Sopenharmony_ci	return err;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci/*
3238c2ecf20Sopenharmony_ci * Get the on-disk location and compressed size of the datablock
3248c2ecf20Sopenharmony_ci * specified by index.  Fill_meta_index() does most of the work.
3258c2ecf20Sopenharmony_ci */
3268c2ecf20Sopenharmony_cistatic int read_blocklist(struct inode *inode, int index, u64 *block)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	u64 start;
3298c2ecf20Sopenharmony_ci	long long blks;
3308c2ecf20Sopenharmony_ci	int offset;
3318c2ecf20Sopenharmony_ci	__le32 size;
3328c2ecf20Sopenharmony_ci	int res = fill_meta_index(inode, index, &start, &offset, block);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	TRACE("read_blocklist: res %d, index %d, start 0x%llx, offset"
3358c2ecf20Sopenharmony_ci		       " 0x%x, block 0x%llx\n", res, index, start, offset,
3368c2ecf20Sopenharmony_ci			*block);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (res < 0)
3398c2ecf20Sopenharmony_ci		return res;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/*
3428c2ecf20Sopenharmony_ci	 * res contains the index of the mapping returned by fill_meta_index(),
3438c2ecf20Sopenharmony_ci	 * this will likely be less than the desired index (because the
3448c2ecf20Sopenharmony_ci	 * meta_index cache works at a higher granularity).  Read any
3458c2ecf20Sopenharmony_ci	 * extra block indexes needed.
3468c2ecf20Sopenharmony_ci	 */
3478c2ecf20Sopenharmony_ci	if (res < index) {
3488c2ecf20Sopenharmony_ci		blks = read_indexes(inode->i_sb, index - res, &start, &offset);
3498c2ecf20Sopenharmony_ci		if (blks < 0)
3508c2ecf20Sopenharmony_ci			return (int) blks;
3518c2ecf20Sopenharmony_ci		*block += blks;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/*
3558c2ecf20Sopenharmony_ci	 * Read length of block specified by index.
3568c2ecf20Sopenharmony_ci	 */
3578c2ecf20Sopenharmony_ci	res = squashfs_read_metadata(inode->i_sb, &size, &start, &offset,
3588c2ecf20Sopenharmony_ci			sizeof(size));
3598c2ecf20Sopenharmony_ci	if (res < 0)
3608c2ecf20Sopenharmony_ci		return res;
3618c2ecf20Sopenharmony_ci	return squashfs_block_size(size);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_civoid squashfs_fill_page(struct page *page, struct squashfs_cache_entry *buffer, int offset, int avail)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	int copied;
3678c2ecf20Sopenharmony_ci	void *pageaddr;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	pageaddr = kmap_atomic(page);
3708c2ecf20Sopenharmony_ci	copied = squashfs_copy_data(pageaddr, buffer, offset, avail);
3718c2ecf20Sopenharmony_ci	memset(pageaddr + copied, 0, PAGE_SIZE - copied);
3728c2ecf20Sopenharmony_ci	kunmap_atomic(pageaddr);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	flush_dcache_page(page);
3758c2ecf20Sopenharmony_ci	if (copied == avail)
3768c2ecf20Sopenharmony_ci		SetPageUptodate(page);
3778c2ecf20Sopenharmony_ci	else
3788c2ecf20Sopenharmony_ci		SetPageError(page);
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/* Copy data into page cache  */
3828c2ecf20Sopenharmony_civoid squashfs_copy_cache(struct page *page, struct squashfs_cache_entry *buffer,
3838c2ecf20Sopenharmony_ci	int bytes, int offset)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	struct inode *inode = page->mapping->host;
3868c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
3878c2ecf20Sopenharmony_ci	int i, mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
3888c2ecf20Sopenharmony_ci	int start_index = page->index & ~mask, end_index = start_index | mask;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/*
3918c2ecf20Sopenharmony_ci	 * Loop copying datablock into pages.  As the datablock likely covers
3928c2ecf20Sopenharmony_ci	 * many PAGE_SIZE pages (default block size is 128 KiB) explicitly
3938c2ecf20Sopenharmony_ci	 * grab the pages from the page cache, except for the page that we've
3948c2ecf20Sopenharmony_ci	 * been called to fill.
3958c2ecf20Sopenharmony_ci	 */
3968c2ecf20Sopenharmony_ci	for (i = start_index; i <= end_index && bytes > 0; i++,
3978c2ecf20Sopenharmony_ci			bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
3988c2ecf20Sopenharmony_ci		struct page *push_page;
3998c2ecf20Sopenharmony_ci		int avail = buffer ? min_t(int, bytes, PAGE_SIZE) : 0;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci		TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci		push_page = (i == page->index) ? page :
4048c2ecf20Sopenharmony_ci			grab_cache_page_nowait(page->mapping, i);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		if (!push_page)
4078c2ecf20Sopenharmony_ci			continue;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		if (PageUptodate(push_page))
4108c2ecf20Sopenharmony_ci			goto skip_page;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci		squashfs_fill_page(push_page, buffer, offset, avail);
4138c2ecf20Sopenharmony_ciskip_page:
4148c2ecf20Sopenharmony_ci		unlock_page(push_page);
4158c2ecf20Sopenharmony_ci		if (i != page->index)
4168c2ecf20Sopenharmony_ci			put_page(push_page);
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci/* Read datablock stored packed inside a fragment (tail-end packed block) */
4218c2ecf20Sopenharmony_cistatic int squashfs_readpage_fragment(struct page *page, int expected)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	struct inode *inode = page->mapping->host;
4248c2ecf20Sopenharmony_ci	struct squashfs_cache_entry *buffer = squashfs_get_fragment(inode->i_sb,
4258c2ecf20Sopenharmony_ci		squashfs_i(inode)->fragment_block,
4268c2ecf20Sopenharmony_ci		squashfs_i(inode)->fragment_size);
4278c2ecf20Sopenharmony_ci	int res = buffer->error;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if (res)
4308c2ecf20Sopenharmony_ci		ERROR("Unable to read page, block %llx, size %x\n",
4318c2ecf20Sopenharmony_ci			squashfs_i(inode)->fragment_block,
4328c2ecf20Sopenharmony_ci			squashfs_i(inode)->fragment_size);
4338c2ecf20Sopenharmony_ci	else
4348c2ecf20Sopenharmony_ci		squashfs_copy_cache(page, buffer, expected,
4358c2ecf20Sopenharmony_ci			squashfs_i(inode)->fragment_offset);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	squashfs_cache_put(buffer);
4388c2ecf20Sopenharmony_ci	return res;
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic int squashfs_readpage_sparse(struct page *page, int expected)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	squashfs_copy_cache(page, NULL, expected, 0);
4448c2ecf20Sopenharmony_ci	return 0;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int squashfs_readpage(struct file *file, struct page *page)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct inode *inode = page->mapping->host;
4508c2ecf20Sopenharmony_ci	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
4518c2ecf20Sopenharmony_ci	int index = page->index >> (msblk->block_log - PAGE_SHIFT);
4528c2ecf20Sopenharmony_ci	int file_end = i_size_read(inode) >> msblk->block_log;
4538c2ecf20Sopenharmony_ci	int expected = index == file_end ?
4548c2ecf20Sopenharmony_ci			(i_size_read(inode) & (msblk->block_size - 1)) :
4558c2ecf20Sopenharmony_ci			 msblk->block_size;
4568c2ecf20Sopenharmony_ci	int res;
4578c2ecf20Sopenharmony_ci	void *pageaddr;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
4608c2ecf20Sopenharmony_ci				page->index, squashfs_i(inode)->start);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	if (page->index >= ((i_size_read(inode) + PAGE_SIZE - 1) >>
4638c2ecf20Sopenharmony_ci					PAGE_SHIFT))
4648c2ecf20Sopenharmony_ci		goto out;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (index < file_end || squashfs_i(inode)->fragment_block ==
4678c2ecf20Sopenharmony_ci					SQUASHFS_INVALID_BLK) {
4688c2ecf20Sopenharmony_ci		u64 block = 0;
4698c2ecf20Sopenharmony_ci		int bsize = read_blocklist(inode, index, &block);
4708c2ecf20Sopenharmony_ci		if (bsize < 0)
4718c2ecf20Sopenharmony_ci			goto error_out;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci		if (bsize == 0)
4748c2ecf20Sopenharmony_ci			res = squashfs_readpage_sparse(page, expected);
4758c2ecf20Sopenharmony_ci		else
4768c2ecf20Sopenharmony_ci			res = squashfs_readpage_block(page, block, bsize, expected);
4778c2ecf20Sopenharmony_ci	} else
4788c2ecf20Sopenharmony_ci		res = squashfs_readpage_fragment(page, expected);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	if (!res)
4818c2ecf20Sopenharmony_ci		return 0;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cierror_out:
4848c2ecf20Sopenharmony_ci	SetPageError(page);
4858c2ecf20Sopenharmony_ciout:
4868c2ecf20Sopenharmony_ci	pageaddr = kmap_atomic(page);
4878c2ecf20Sopenharmony_ci	memset(pageaddr, 0, PAGE_SIZE);
4888c2ecf20Sopenharmony_ci	kunmap_atomic(pageaddr);
4898c2ecf20Sopenharmony_ci	flush_dcache_page(page);
4908c2ecf20Sopenharmony_ci	if (!PageError(page))
4918c2ecf20Sopenharmony_ci		SetPageUptodate(page);
4928c2ecf20Sopenharmony_ci	unlock_page(page);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	return 0;
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ciconst struct address_space_operations squashfs_aops = {
4998c2ecf20Sopenharmony_ci	.readpage = squashfs_readpage
5008c2ecf20Sopenharmony_ci};
501