2019-05-20 17:08:00 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-01-05 08:46:27 +00:00
|
|
|
/*
|
|
|
|
* Squashfs - a compressed read only filesystem for Linux
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
|
2011-05-26 09:39:56 +00:00
|
|
|
* Phillip Lougher <phillip@squashfs.org.uk>
|
2009-01-05 08:46:27 +00:00
|
|
|
*
|
|
|
|
* block.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file implements the low-level routines to read and decompress
|
|
|
|
* datablocks and metadata blocks.
|
|
|
|
*/
|
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
#include <linux/blkdev.h>
|
2009-01-05 08:46:27 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/vfs.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/buffer_head.h>
|
2016-11-01 13:40:13 +00:00
|
|
|
#include <linux/bio.h>
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
#include "squashfs_fs.h"
|
|
|
|
#include "squashfs_fs_sb.h"
|
|
|
|
#include "squashfs.h"
|
2009-10-06 03:04:15 +00:00
|
|
|
#include "decompressor.h"
|
2013-11-18 02:59:12 +00:00
|
|
|
#include "page_actor.h"
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
/*
|
2020-06-02 04:45:23 +00:00
|
|
|
* Returns the amount of bytes copied to the page actor.
|
2009-01-05 08:46:27 +00:00
|
|
|
*/
|
2020-06-02 04:45:23 +00:00
|
|
|
static int copy_bio_to_actor(struct bio *bio,
|
|
|
|
struct squashfs_page_actor *actor,
|
|
|
|
int offset, int req_length)
|
|
|
|
{
|
|
|
|
void *actor_addr = squashfs_first_page(actor);
|
|
|
|
struct bvec_iter_all iter_all = {};
|
|
|
|
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
|
|
|
|
int copied_bytes = 0;
|
|
|
|
int actor_offset = 0;
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (copied_bytes < req_length) {
|
|
|
|
int bytes_to_copy = min_t(int, bvec->bv_len - offset,
|
|
|
|
PAGE_SIZE - actor_offset);
|
|
|
|
|
|
|
|
bytes_to_copy = min_t(int, bytes_to_copy,
|
|
|
|
req_length - copied_bytes);
|
|
|
|
memcpy(actor_addr + actor_offset,
|
|
|
|
page_address(bvec->bv_page) + bvec->bv_offset + offset,
|
|
|
|
bytes_to_copy);
|
|
|
|
|
|
|
|
actor_offset += bytes_to_copy;
|
|
|
|
copied_bytes += bytes_to_copy;
|
|
|
|
offset += bytes_to_copy;
|
|
|
|
|
|
|
|
if (actor_offset >= PAGE_SIZE) {
|
|
|
|
actor_addr = squashfs_next_page(actor);
|
|
|
|
if (!actor_addr)
|
|
|
|
break;
|
|
|
|
actor_offset = 0;
|
|
|
|
}
|
|
|
|
if (offset >= bvec->bv_len) {
|
|
|
|
if (!bio_next_segment(bio, &iter_all))
|
|
|
|
break;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
squashfs_finish_page(actor);
|
|
|
|
return copied_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
|
|
|
|
struct bio **biop, int *block_offset)
|
2009-01-05 08:46:27 +00:00
|
|
|
{
|
|
|
|
struct squashfs_sb_info *msblk = sb->s_fs_info;
|
2020-06-02 04:45:23 +00:00
|
|
|
const u64 read_start = round_down(index, msblk->devblksize);
|
|
|
|
const sector_t block = read_start >> msblk->devblksize_log2;
|
|
|
|
const u64 read_end = round_up(index + length, msblk->devblksize);
|
|
|
|
const sector_t block_end = read_end >> msblk->devblksize_log2;
|
|
|
|
int offset = read_start - round_down(index, PAGE_SIZE);
|
|
|
|
int total_len = (block_end - block) << msblk->devblksize_log2;
|
|
|
|
const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
|
|
|
|
int error, i;
|
|
|
|
struct bio *bio;
|
|
|
|
|
2020-08-21 00:42:21 +00:00
|
|
|
if (page_count <= BIO_MAX_PAGES)
|
|
|
|
bio = bio_alloc(GFP_NOIO, page_count);
|
|
|
|
else
|
|
|
|
bio = bio_kmalloc(GFP_NOIO, page_count);
|
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
if (!bio)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
bio_set_dev(bio, sb->s_bdev);
|
|
|
|
bio->bi_opf = READ;
|
|
|
|
bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
|
|
|
|
|
|
|
|
for (i = 0; i < page_count; ++i) {
|
|
|
|
unsigned int len =
|
|
|
|
min_t(unsigned int, PAGE_SIZE - offset, total_len);
|
|
|
|
struct page *page = alloc_page(GFP_NOIO);
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out_free_bio;
|
|
|
|
}
|
|
|
|
if (!bio_add_page(bio, page, len, offset)) {
|
|
|
|
error = -EIO;
|
|
|
|
goto out_free_bio;
|
squashfs: fix use of uninitialised variable in zlib & xz decompressors
Fix potential use of uninitialised variable caused by recent
decompressor code optimisations.
In zlib_uncompress (zlib_wrapper.c) we have
int zlib_err, zlib_init = 0;
...
do {
...
if (avail == 0) {
offset = 0;
put_bh(bh[k++]);
continue;
}
...
zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
...
} while (zlib_err == Z_OK);
If continue is executed (avail == 0) then the while condition will be
evaluated testing zlib_err, which is uninitialised first time around the
loop.
Fix this by getting rid of the 'if (avail == 0)' condition test, this
edge condition should not be being handled in the decompressor code, and
instead handle it generically in the caller code.
Similarly for xz_wrapper.c.
Incidentally, on most architectures (bar Mips and Parisc), no
uninitialised variable warning is generated by gcc, this is because the
while condition test on continue is optimised out and not performed
(when executing continue zlib_err has not been changed since entering
the loop, and logically if the while condition was true previously, then
it's still true).
Signed-off-by: Phillip Lougher <phillip@lougher.demon.co.uk>
Reported-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-01-25 23:07:34 +00:00
|
|
|
}
|
2020-06-02 04:45:23 +00:00
|
|
|
offset = 0;
|
|
|
|
total_len -= len;
|
2009-01-05 08:46:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
error = submit_bio_wait(bio);
|
|
|
|
if (error)
|
|
|
|
goto out_free_bio;
|
2009-01-05 08:46:27 +00:00
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
*biop = bio;
|
|
|
|
*block_offset = index & ((1 << msblk->devblksize_log2) - 1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_free_bio:
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
return error;
|
|
|
|
}
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and decompress a metadata block or datablock. Length is non-zero
|
|
|
|
* if a datablock is being read (the size is stored elsewhere in the
|
|
|
|
* filesystem), otherwise the length is obtained from the first two bytes of
|
|
|
|
* the metadata block. A bit in the length field indicates if the block
|
|
|
|
* is stored uncompressed in the filesystem (usually because compression
|
2012-03-06 01:18:49 +00:00
|
|
|
* generated a larger block - this does occasionally happen with compression
|
|
|
|
* algorithms).
|
2009-01-05 08:46:27 +00:00
|
|
|
*/
|
2013-11-18 02:59:12 +00:00
|
|
|
int squashfs_read_data(struct super_block *sb, u64 index, int length,
|
2020-06-02 04:45:23 +00:00
|
|
|
u64 *next_index, struct squashfs_page_actor *output)
|
2009-01-05 08:46:27 +00:00
|
|
|
{
|
|
|
|
struct squashfs_sb_info *msblk = sb->s_fs_info;
|
2020-06-02 04:45:23 +00:00
|
|
|
struct bio *bio = NULL;
|
|
|
|
int compressed;
|
|
|
|
int res;
|
|
|
|
int offset;
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
if (length) {
|
|
|
|
/*
|
|
|
|
* Datablock.
|
|
|
|
*/
|
|
|
|
compressed = SQUASHFS_COMPRESSED_BLOCK(length);
|
|
|
|
length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
|
|
|
|
TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
|
2013-11-18 02:59:12 +00:00
|
|
|
index, compressed ? "" : "un", length, output->length);
|
2009-01-05 08:46:27 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Metadata block.
|
|
|
|
*/
|
2020-06-02 04:45:23 +00:00
|
|
|
const u8 *data;
|
|
|
|
struct bvec_iter_all iter_all = {};
|
|
|
|
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
|
2009-01-05 08:46:27 +00:00
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
if (index + 2 > msblk->bytes_used) {
|
|
|
|
res = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
res = squashfs_bio_read(sb, index, 2, &bio, &offset);
|
|
|
|
if (res)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
|
|
|
|
res = -EIO;
|
|
|
|
goto out_free_bio;
|
|
|
|
}
|
|
|
|
/* Extract the length of the metadata block */
|
|
|
|
data = page_address(bvec->bv_page) + bvec->bv_offset;
|
|
|
|
length = data[offset];
|
2020-07-24 04:15:40 +00:00
|
|
|
if (offset < bvec->bv_len - 1) {
|
2020-06-02 04:45:23 +00:00
|
|
|
length |= data[offset + 1] << 8;
|
|
|
|
} else {
|
|
|
|
if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
|
|
|
|
res = -EIO;
|
|
|
|
goto out_free_bio;
|
|
|
|
}
|
|
|
|
data = page_address(bvec->bv_page) + bvec->bv_offset;
|
|
|
|
length |= data[0] << 8;
|
|
|
|
}
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
compressed = SQUASHFS_COMPRESSED(length);
|
|
|
|
length = SQUASHFS_COMPRESSED_SIZE(length);
|
2020-06-02 04:45:23 +00:00
|
|
|
index += 2;
|
2009-01-05 08:46:27 +00:00
|
|
|
|
|
|
|
TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
|
2020-06-02 04:45:23 +00:00
|
|
|
compressed ? "" : "un", length);
|
2009-01-05 08:46:27 +00:00
|
|
|
}
|
2020-06-02 04:45:23 +00:00
|
|
|
if (next_index)
|
|
|
|
*next_index = index + length;
|
2009-01-05 08:46:27 +00:00
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
res = squashfs_bio_read(sb, index, length, &bio, &offset);
|
|
|
|
if (res)
|
|
|
|
goto out;
|
2013-11-13 02:56:26 +00:00
|
|
|
|
2009-01-05 08:46:27 +00:00
|
|
|
if (compressed) {
|
2020-06-02 04:45:23 +00:00
|
|
|
if (!msblk->stream) {
|
|
|
|
res = -EIO;
|
|
|
|
goto out_free_bio;
|
2009-01-05 08:46:27 +00:00
|
|
|
}
|
2020-06-02 04:45:23 +00:00
|
|
|
res = squashfs_decompress(msblk, bio, offset, length, output);
|
|
|
|
} else {
|
|
|
|
res = copy_bio_to_actor(bio, output, offset, length);
|
2009-01-05 08:46:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
out_free_bio:
|
|
|
|
bio_free_pages(bio);
|
|
|
|
bio_put(bio);
|
|
|
|
out:
|
|
|
|
if (res < 0)
|
|
|
|
ERROR("Failed to read block 0x%llx: %d\n", index, res);
|
2009-01-05 08:46:27 +00:00
|
|
|
|
2020-06-02 04:45:23 +00:00
|
|
|
return res;
|
2009-01-05 08:46:27 +00:00
|
|
|
}
|