168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e2780ab1SPhillip Lougher /*
3e2780ab1SPhillip Lougher * Squashfs - a compressed read only filesystem for Linux
4e2780ab1SPhillip Lougher *
5e2780ab1SPhillip Lougher * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6d7f2ff67SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
7e2780ab1SPhillip Lougher *
8e2780ab1SPhillip Lougher * block.c
9e2780ab1SPhillip Lougher */
10e2780ab1SPhillip Lougher
11e2780ab1SPhillip Lougher /*
12e2780ab1SPhillip Lougher * This file implements the low-level routines to read and decompress
13e2780ab1SPhillip Lougher * datablocks and metadata blocks.
14e2780ab1SPhillip Lougher */
15e2780ab1SPhillip Lougher
1693e72b3cSPhilippe Liard #include <linux/blkdev.h>
17e2780ab1SPhillip Lougher #include <linux/fs.h>
18e2780ab1SPhillip Lougher #include <linux/vfs.h>
19e2780ab1SPhillip Lougher #include <linux/slab.h>
20e994f5b6SVincent Whitchurch #include <linux/pagemap.h>
21e2780ab1SPhillip Lougher #include <linux/string.h>
222f8b5444SChristoph Hellwig #include <linux/bio.h>
23e2780ab1SPhillip Lougher
24e2780ab1SPhillip Lougher #include "squashfs_fs.h"
25e2780ab1SPhillip Lougher #include "squashfs_fs_sb.h"
26e2780ab1SPhillip Lougher #include "squashfs.h"
274c0f0bb2SPhillip Lougher #include "decompressor.h"
28846b730eSPhillip Lougher #include "page_actor.h"
29e2780ab1SPhillip Lougher
30e2780ab1SPhillip Lougher /*
3193e72b3cSPhilippe Liard * Returns the amount of bytes copied to the page actor.
32e2780ab1SPhillip Lougher */
copy_bio_to_actor(struct bio * bio,struct squashfs_page_actor * actor,int offset,int req_length)3393e72b3cSPhilippe Liard static int copy_bio_to_actor(struct bio *bio,
3493e72b3cSPhilippe Liard struct squashfs_page_actor *actor,
3593e72b3cSPhilippe Liard int offset, int req_length)
3693e72b3cSPhilippe Liard {
37f268eeddSPhillip Lougher void *actor_addr;
3893e72b3cSPhilippe Liard struct bvec_iter_all iter_all = {};
3993e72b3cSPhilippe Liard struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
4093e72b3cSPhilippe Liard int copied_bytes = 0;
4193e72b3cSPhilippe Liard int actor_offset = 0;
4293e72b3cSPhilippe Liard
43f268eeddSPhillip Lougher squashfs_actor_nobuff(actor);
44f268eeddSPhillip Lougher actor_addr = squashfs_first_page(actor);
45f268eeddSPhillip Lougher
4693e72b3cSPhilippe Liard if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
4793e72b3cSPhilippe Liard return 0;
4893e72b3cSPhilippe Liard
4993e72b3cSPhilippe Liard while (copied_bytes < req_length) {
5093e72b3cSPhilippe Liard int bytes_to_copy = min_t(int, bvec->bv_len - offset,
5193e72b3cSPhilippe Liard PAGE_SIZE - actor_offset);
5293e72b3cSPhilippe Liard
5393e72b3cSPhilippe Liard bytes_to_copy = min_t(int, bytes_to_copy,
5493e72b3cSPhilippe Liard req_length - copied_bytes);
55f268eeddSPhillip Lougher if (!IS_ERR(actor_addr))
56f268eeddSPhillip Lougher memcpy(actor_addr + actor_offset, bvec_virt(bvec) +
57f268eeddSPhillip Lougher offset, bytes_to_copy);
5893e72b3cSPhilippe Liard
5993e72b3cSPhilippe Liard actor_offset += bytes_to_copy;
6093e72b3cSPhilippe Liard copied_bytes += bytes_to_copy;
6193e72b3cSPhilippe Liard offset += bytes_to_copy;
6293e72b3cSPhilippe Liard
6393e72b3cSPhilippe Liard if (actor_offset >= PAGE_SIZE) {
6493e72b3cSPhilippe Liard actor_addr = squashfs_next_page(actor);
6593e72b3cSPhilippe Liard if (!actor_addr)
6693e72b3cSPhilippe Liard break;
6793e72b3cSPhilippe Liard actor_offset = 0;
6893e72b3cSPhilippe Liard }
6993e72b3cSPhilippe Liard if (offset >= bvec->bv_len) {
7093e72b3cSPhilippe Liard if (!bio_next_segment(bio, &iter_all))
7193e72b3cSPhilippe Liard break;
7293e72b3cSPhilippe Liard offset = 0;
7393e72b3cSPhilippe Liard }
7493e72b3cSPhilippe Liard }
7593e72b3cSPhilippe Liard squashfs_finish_page(actor);
7693e72b3cSPhilippe Liard return copied_bytes;
7793e72b3cSPhilippe Liard }
7893e72b3cSPhilippe Liard
squashfs_bio_read_cached(struct bio * fullbio,struct address_space * cache_mapping,u64 index,int length,u64 read_start,u64 read_end,int page_count)79e994f5b6SVincent Whitchurch static int squashfs_bio_read_cached(struct bio *fullbio,
80e994f5b6SVincent Whitchurch struct address_space *cache_mapping, u64 index, int length,
81e994f5b6SVincent Whitchurch u64 read_start, u64 read_end, int page_count)
82e994f5b6SVincent Whitchurch {
83e994f5b6SVincent Whitchurch struct page *head_to_cache = NULL, *tail_to_cache = NULL;
84e994f5b6SVincent Whitchurch struct block_device *bdev = fullbio->bi_bdev;
85e994f5b6SVincent Whitchurch int start_idx = 0, end_idx = 0;
86e994f5b6SVincent Whitchurch struct bvec_iter_all iter_all;
87e994f5b6SVincent Whitchurch struct bio *bio = NULL;
88e994f5b6SVincent Whitchurch struct bio_vec *bv;
89e994f5b6SVincent Whitchurch int idx = 0;
90e994f5b6SVincent Whitchurch int err = 0;
91e994f5b6SVincent Whitchurch
92e994f5b6SVincent Whitchurch bio_for_each_segment_all(bv, fullbio, iter_all) {
93e994f5b6SVincent Whitchurch struct page *page = bv->bv_page;
94e994f5b6SVincent Whitchurch
95e994f5b6SVincent Whitchurch if (page->mapping == cache_mapping) {
96e994f5b6SVincent Whitchurch idx++;
97e994f5b6SVincent Whitchurch continue;
98e994f5b6SVincent Whitchurch }
99e994f5b6SVincent Whitchurch
100e994f5b6SVincent Whitchurch /*
101e994f5b6SVincent Whitchurch * We only use this when the device block size is the same as
102e994f5b6SVincent Whitchurch * the page size, so read_start and read_end cover full pages.
103e994f5b6SVincent Whitchurch *
104e994f5b6SVincent Whitchurch * Compare these to the original required index and length to
105e994f5b6SVincent Whitchurch * only cache pages which were requested partially, since these
106e994f5b6SVincent Whitchurch * are the ones which are likely to be needed when reading
107e994f5b6SVincent Whitchurch * adjacent blocks.
108e994f5b6SVincent Whitchurch */
109e994f5b6SVincent Whitchurch if (idx == 0 && index != read_start)
110e994f5b6SVincent Whitchurch head_to_cache = page;
111e994f5b6SVincent Whitchurch else if (idx == page_count - 1 && index + length != read_end)
112e994f5b6SVincent Whitchurch tail_to_cache = page;
113e994f5b6SVincent Whitchurch
114e994f5b6SVincent Whitchurch if (!bio || idx != end_idx) {
115e994f5b6SVincent Whitchurch struct bio *new = bio_alloc_clone(bdev, fullbio,
116e994f5b6SVincent Whitchurch GFP_NOIO, &fs_bio_set);
117e994f5b6SVincent Whitchurch
118e994f5b6SVincent Whitchurch if (bio) {
119e994f5b6SVincent Whitchurch bio_trim(bio, start_idx * PAGE_SECTORS,
120e994f5b6SVincent Whitchurch (end_idx - start_idx) * PAGE_SECTORS);
121e994f5b6SVincent Whitchurch bio_chain(bio, new);
122e994f5b6SVincent Whitchurch submit_bio(bio);
123e994f5b6SVincent Whitchurch }
124e994f5b6SVincent Whitchurch
125e994f5b6SVincent Whitchurch bio = new;
126e994f5b6SVincent Whitchurch start_idx = idx;
127e994f5b6SVincent Whitchurch }
128e994f5b6SVincent Whitchurch
129e994f5b6SVincent Whitchurch idx++;
130e994f5b6SVincent Whitchurch end_idx = idx;
131e994f5b6SVincent Whitchurch }
132e994f5b6SVincent Whitchurch
133e994f5b6SVincent Whitchurch if (bio) {
134e994f5b6SVincent Whitchurch bio_trim(bio, start_idx * PAGE_SECTORS,
135e994f5b6SVincent Whitchurch (end_idx - start_idx) * PAGE_SECTORS);
136e994f5b6SVincent Whitchurch err = submit_bio_wait(bio);
137e994f5b6SVincent Whitchurch bio_put(bio);
138e994f5b6SVincent Whitchurch }
139e994f5b6SVincent Whitchurch
140e994f5b6SVincent Whitchurch if (err)
141e994f5b6SVincent Whitchurch return err;
142e994f5b6SVincent Whitchurch
143e994f5b6SVincent Whitchurch if (head_to_cache) {
144e994f5b6SVincent Whitchurch int ret = add_to_page_cache_lru(head_to_cache, cache_mapping,
145e994f5b6SVincent Whitchurch read_start >> PAGE_SHIFT,
146e994f5b6SVincent Whitchurch GFP_NOIO);
147e994f5b6SVincent Whitchurch
148e994f5b6SVincent Whitchurch if (!ret) {
149e994f5b6SVincent Whitchurch SetPageUptodate(head_to_cache);
150e994f5b6SVincent Whitchurch unlock_page(head_to_cache);
151e994f5b6SVincent Whitchurch }
152e994f5b6SVincent Whitchurch
153e994f5b6SVincent Whitchurch }
154e994f5b6SVincent Whitchurch
155e994f5b6SVincent Whitchurch if (tail_to_cache) {
156e994f5b6SVincent Whitchurch int ret = add_to_page_cache_lru(tail_to_cache, cache_mapping,
157e994f5b6SVincent Whitchurch (read_end >> PAGE_SHIFT) - 1,
158e994f5b6SVincent Whitchurch GFP_NOIO);
159e994f5b6SVincent Whitchurch
160e994f5b6SVincent Whitchurch if (!ret) {
161e994f5b6SVincent Whitchurch SetPageUptodate(tail_to_cache);
162e994f5b6SVincent Whitchurch unlock_page(tail_to_cache);
163e994f5b6SVincent Whitchurch }
164e994f5b6SVincent Whitchurch }
165e994f5b6SVincent Whitchurch
166e994f5b6SVincent Whitchurch return 0;
167e994f5b6SVincent Whitchurch }
168e994f5b6SVincent Whitchurch
squashfs_get_cache_page(struct address_space * mapping,pgoff_t index)16908bab74aSVincent Whitchurch static struct page *squashfs_get_cache_page(struct address_space *mapping,
17008bab74aSVincent Whitchurch pgoff_t index)
17108bab74aSVincent Whitchurch {
17208bab74aSVincent Whitchurch struct page *page;
17308bab74aSVincent Whitchurch
17408bab74aSVincent Whitchurch if (!mapping)
17508bab74aSVincent Whitchurch return NULL;
17608bab74aSVincent Whitchurch
17708bab74aSVincent Whitchurch page = find_get_page(mapping, index);
17808bab74aSVincent Whitchurch if (!page)
17908bab74aSVincent Whitchurch return NULL;
18008bab74aSVincent Whitchurch
18108bab74aSVincent Whitchurch if (!PageUptodate(page)) {
18208bab74aSVincent Whitchurch put_page(page);
18308bab74aSVincent Whitchurch return NULL;
18408bab74aSVincent Whitchurch }
18508bab74aSVincent Whitchurch
18608bab74aSVincent Whitchurch return page;
18708bab74aSVincent Whitchurch }
18808bab74aSVincent Whitchurch
squashfs_bio_read(struct super_block * sb,u64 index,int length,struct bio ** biop,int * block_offset)18993e72b3cSPhilippe Liard static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
19093e72b3cSPhilippe Liard struct bio **biop, int *block_offset)
191e2780ab1SPhillip Lougher {
192e2780ab1SPhillip Lougher struct squashfs_sb_info *msblk = sb->s_fs_info;
193e994f5b6SVincent Whitchurch struct address_space *cache_mapping = msblk->cache_mapping;
19493e72b3cSPhilippe Liard const u64 read_start = round_down(index, msblk->devblksize);
19593e72b3cSPhilippe Liard const sector_t block = read_start >> msblk->devblksize_log2;
19693e72b3cSPhilippe Liard const u64 read_end = round_up(index + length, msblk->devblksize);
19793e72b3cSPhilippe Liard const sector_t block_end = read_end >> msblk->devblksize_log2;
19893e72b3cSPhilippe Liard int offset = read_start - round_down(index, PAGE_SIZE);
19993e72b3cSPhilippe Liard int total_len = (block_end - block) << msblk->devblksize_log2;
20093e72b3cSPhilippe Liard const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
20193e72b3cSPhilippe Liard int error, i;
20293e72b3cSPhilippe Liard struct bio *bio;
203e2780ab1SPhillip Lougher
204066ff571SChristoph Hellwig bio = bio_kmalloc(page_count, GFP_NOIO);
20593e72b3cSPhilippe Liard if (!bio)
20693e72b3cSPhilippe Liard return -ENOMEM;
207066ff571SChristoph Hellwig bio_init(bio, sb->s_bdev, bio->bi_inline_vecs, page_count, REQ_OP_READ);
20893e72b3cSPhilippe Liard bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
2093689456bSPhillip Lougher
21093e72b3cSPhilippe Liard for (i = 0; i < page_count; ++i) {
21193e72b3cSPhilippe Liard unsigned int len =
21293e72b3cSPhilippe Liard min_t(unsigned int, PAGE_SIZE - offset, total_len);
21308bab74aSVincent Whitchurch pgoff_t index = (read_start >> PAGE_SHIFT) + i;
21408bab74aSVincent Whitchurch struct page *page;
215e994f5b6SVincent Whitchurch
21608bab74aSVincent Whitchurch page = squashfs_get_cache_page(cache_mapping, index);
217e994f5b6SVincent Whitchurch if (!page)
218e994f5b6SVincent Whitchurch page = alloc_page(GFP_NOIO);
21993e72b3cSPhilippe Liard
22093e72b3cSPhilippe Liard if (!page) {
22193e72b3cSPhilippe Liard error = -ENOMEM;
22293e72b3cSPhilippe Liard goto out_free_bio;
2233689456bSPhillip Lougher }
224e994f5b6SVincent Whitchurch
225e994f5b6SVincent Whitchurch /*
226e994f5b6SVincent Whitchurch * Use the __ version to avoid merging since we need each page
227e994f5b6SVincent Whitchurch * to be separate when we check for and avoid cached pages.
228e994f5b6SVincent Whitchurch */
229e994f5b6SVincent Whitchurch __bio_add_page(bio, page, len, offset);
23093e72b3cSPhilippe Liard offset = 0;
23193e72b3cSPhilippe Liard total_len -= len;
232e2780ab1SPhillip Lougher }
233e2780ab1SPhillip Lougher
234e994f5b6SVincent Whitchurch if (cache_mapping)
235e994f5b6SVincent Whitchurch error = squashfs_bio_read_cached(bio, cache_mapping, index,
236e994f5b6SVincent Whitchurch length, read_start, read_end,
237e994f5b6SVincent Whitchurch page_count);
238e994f5b6SVincent Whitchurch else
23993e72b3cSPhilippe Liard error = submit_bio_wait(bio);
24093e72b3cSPhilippe Liard if (error)
24193e72b3cSPhilippe Liard goto out_free_bio;
242e2780ab1SPhillip Lougher
24393e72b3cSPhilippe Liard *biop = bio;
24493e72b3cSPhilippe Liard *block_offset = index & ((1 << msblk->devblksize_log2) - 1);
24593e72b3cSPhilippe Liard return 0;
24693e72b3cSPhilippe Liard
24793e72b3cSPhilippe Liard out_free_bio:
24893e72b3cSPhilippe Liard bio_free_pages(bio);
249066ff571SChristoph Hellwig bio_uninit(bio);
250066ff571SChristoph Hellwig kfree(bio);
25193e72b3cSPhilippe Liard return error;
25293e72b3cSPhilippe Liard }
253e2780ab1SPhillip Lougher
254e2780ab1SPhillip Lougher /*
255e2780ab1SPhillip Lougher * Read and decompress a metadata block or datablock. Length is non-zero
256e2780ab1SPhillip Lougher * if a datablock is being read (the size is stored elsewhere in the
257e2780ab1SPhillip Lougher * filesystem), otherwise the length is obtained from the first two bytes of
258e2780ab1SPhillip Lougher * the metadata block. A bit in the length field indicates if the block
259e2780ab1SPhillip Lougher * is stored uncompressed in the filesystem (usually because compression
260ec9267b6SPhillip Lougher * generated a larger block - this does occasionally happen with compression
261ec9267b6SPhillip Lougher * algorithms).
262e2780ab1SPhillip Lougher */
squashfs_read_data(struct super_block * sb,u64 index,int length,u64 * next_index,struct squashfs_page_actor * output)263846b730eSPhillip Lougher int squashfs_read_data(struct super_block *sb, u64 index, int length,
264846b730eSPhillip Lougher u64 *next_index, struct squashfs_page_actor *output)
265e2780ab1SPhillip Lougher {
266e2780ab1SPhillip Lougher struct squashfs_sb_info *msblk = sb->s_fs_info;
26793e72b3cSPhilippe Liard struct bio *bio = NULL;
26893e72b3cSPhilippe Liard int compressed;
26993e72b3cSPhilippe Liard int res;
27093e72b3cSPhilippe Liard int offset;
271e2780ab1SPhillip Lougher
272e2780ab1SPhillip Lougher if (length) {
273e2780ab1SPhillip Lougher /*
274e2780ab1SPhillip Lougher * Datablock.
275e2780ab1SPhillip Lougher */
276e2780ab1SPhillip Lougher compressed = SQUASHFS_COMPRESSED_BLOCK(length);
277e2780ab1SPhillip Lougher length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
278e2780ab1SPhillip Lougher TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
279846b730eSPhillip Lougher index, compressed ? "" : "un", length, output->length);
280e2780ab1SPhillip Lougher } else {
281e2780ab1SPhillip Lougher /*
282e2780ab1SPhillip Lougher * Metadata block.
283e2780ab1SPhillip Lougher */
28493e72b3cSPhilippe Liard const u8 *data;
28593e72b3cSPhilippe Liard struct bvec_iter_all iter_all = {};
28693e72b3cSPhilippe Liard struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
287e2780ab1SPhillip Lougher
28893e72b3cSPhilippe Liard if (index + 2 > msblk->bytes_used) {
28993e72b3cSPhilippe Liard res = -EIO;
29093e72b3cSPhilippe Liard goto out;
29193e72b3cSPhilippe Liard }
29293e72b3cSPhilippe Liard res = squashfs_bio_read(sb, index, 2, &bio, &offset);
29393e72b3cSPhilippe Liard if (res)
29493e72b3cSPhilippe Liard goto out;
295e2780ab1SPhillip Lougher
29693e72b3cSPhilippe Liard if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
29793e72b3cSPhilippe Liard res = -EIO;
29893e72b3cSPhilippe Liard goto out_free_bio;
29993e72b3cSPhilippe Liard }
30093e72b3cSPhilippe Liard /* Extract the length of the metadata block */
301fbc27241SChristoph Hellwig data = bvec_virt(bvec);
30293e72b3cSPhilippe Liard length = data[offset];
3032910c59fSPhillip Lougher if (offset < bvec->bv_len - 1) {
30493e72b3cSPhilippe Liard length |= data[offset + 1] << 8;
30593e72b3cSPhilippe Liard } else {
30693e72b3cSPhilippe Liard if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
30793e72b3cSPhilippe Liard res = -EIO;
30893e72b3cSPhilippe Liard goto out_free_bio;
30993e72b3cSPhilippe Liard }
310fbc27241SChristoph Hellwig data = bvec_virt(bvec);
31193e72b3cSPhilippe Liard length |= data[0] << 8;
31293e72b3cSPhilippe Liard }
31393e72b3cSPhilippe Liard bio_free_pages(bio);
314066ff571SChristoph Hellwig bio_uninit(bio);
315066ff571SChristoph Hellwig kfree(bio);
31693e72b3cSPhilippe Liard
317e2780ab1SPhillip Lougher compressed = SQUASHFS_COMPRESSED(length);
318e2780ab1SPhillip Lougher length = SQUASHFS_COMPRESSED_SIZE(length);
31993e72b3cSPhilippe Liard index += 2;
320e2780ab1SPhillip Lougher
321e812cbbbSPhillip Lougher TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
322e2780ab1SPhillip Lougher compressed ? "" : "un", length);
323e2780ab1SPhillip Lougher }
324*2dbaa757SLizhi Xu if (length <= 0 || length > output->length ||
325e812cbbbSPhillip Lougher (index + length) > msblk->bytes_used) {
326e812cbbbSPhillip Lougher res = -EIO;
327e812cbbbSPhillip Lougher goto out;
328e812cbbbSPhillip Lougher }
329e812cbbbSPhillip Lougher
33093e72b3cSPhilippe Liard if (next_index)
33193e72b3cSPhilippe Liard *next_index = index + length;
332e2780ab1SPhillip Lougher
33393e72b3cSPhilippe Liard res = squashfs_bio_read(sb, index, length, &bio, &offset);
33493e72b3cSPhilippe Liard if (res)
33593e72b3cSPhilippe Liard goto out;
3369508c6b9SPhillip Lougher
337e2780ab1SPhillip Lougher if (compressed) {
33893e72b3cSPhilippe Liard if (!msblk->stream) {
33993e72b3cSPhilippe Liard res = -EIO;
34093e72b3cSPhilippe Liard goto out_free_bio;
34193e72b3cSPhilippe Liard }
34280f78409SXiaoming Ni res = msblk->thread_ops->decompress(msblk, bio, offset, length, output);
343e2780ab1SPhillip Lougher } else {
34493e72b3cSPhilippe Liard res = copy_bio_to_actor(bio, output, offset, length);
345e2780ab1SPhillip Lougher }
346e2780ab1SPhillip Lougher
34793e72b3cSPhilippe Liard out_free_bio:
34893e72b3cSPhilippe Liard bio_free_pages(bio);
349066ff571SChristoph Hellwig bio_uninit(bio);
350066ff571SChristoph Hellwig kfree(bio);
35193e72b3cSPhilippe Liard out:
35210dde05bSVincent Whitchurch if (res < 0) {
35393e72b3cSPhilippe Liard ERROR("Failed to read block 0x%llx: %d\n", index, res);
35410dde05bSVincent Whitchurch if (msblk->panic_on_errors)
35510dde05bSVincent Whitchurch panic("squashfs read failed");
35610dde05bSVincent Whitchurch }
357e2780ab1SPhillip Lougher
35893e72b3cSPhilippe Liard return res;
359e2780ab1SPhillip Lougher }
360