Commit 93e72b3c authored by Philippe Liard's avatar Philippe Liard Committed by Linus Torvalds

squashfs: migrate from ll_rw_block usage to BIO

ll_rw_block() function has been deprecated in favor of BIO which appears
to come with large performance improvements.

This patch decreases boot time by close to 40% when using squashfs for
the root file-system.  This is observed at least in the context of
starting an Android VM on Chrome OS using crosvm.  The patch was tested
on 4.19 as well as master.

This patch is largely based on Adrien Schildknecht's patch that was
originally sent as https://lkml.org/lkml/2017/9/22/814 though with some
significant changes and simplifications while also taking Phillip
Lougher's feedback into account, around preserving support for
FILE_CACHE in particular.

[akpm@linux-foundation.org: fix build error reported by Randy]
  Link: http://lkml.kernel.org/r/319997c2-5fc8-f889-2ea3-d913308a7c1f@infradead.orgSigned-off-by: default avatarPhilippe Liard <pliard@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Cc: Adrien Schildknecht <adrien+dev@schischi.me>
Cc: Phillip Lougher <phillip@squashfs.org.uk>
Cc: Guenter Roeck <groeck@chromium.org>
Cc: Daniel Rosenberg <drosen@google.com>
Link: https://chromium.googlesource.com/chromiumos/platform/crosvm
Link: http://lkml.kernel.org/r/20191106074238.186023-1-pliard@google.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 9bf9511e
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* datablocks and metadata blocks. * datablocks and metadata blocks.
*/ */
#include <linux/blkdev.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/vfs.h> #include <linux/vfs.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -27,44 +28,103 @@ ...@@ -27,44 +28,103 @@
#include "page_actor.h" #include "page_actor.h"
/* /*
* Read the metadata block length, this is stored in the first two * Returns the amount of bytes copied to the page actor.
* bytes of the metadata block.
*/ */
static struct buffer_head *get_block_length(struct super_block *sb, static int copy_bio_to_actor(struct bio *bio,
u64 *cur_index, int *offset, int *length) 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)
{ {
struct squashfs_sb_info *msblk = sb->s_fs_info; struct squashfs_sb_info *msblk = sb->s_fs_info;
struct buffer_head *bh; const u64 read_start = round_down(index, msblk->devblksize);
const sector_t block = read_start >> msblk->devblksize_log2;
bh = sb_bread(sb, *cur_index); const u64 read_end = round_up(index + length, msblk->devblksize);
if (bh == NULL) const sector_t block_end = read_end >> msblk->devblksize_log2;
return NULL; int offset = read_start - round_down(index, PAGE_SIZE);
int total_len = (block_end - block) << msblk->devblksize_log2;
if (msblk->devblksize - *offset == 1) { const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
*length = (unsigned char) bh->b_data[*offset]; int error, i;
put_bh(bh); struct bio *bio;
bh = sb_bread(sb, ++(*cur_index));
if (bh == NULL) bio = bio_alloc(GFP_NOIO, page_count);
return NULL; if (!bio)
*length |= (unsigned char) bh->b_data[0] << 8; return -ENOMEM;
*offset = 1;
} else { bio_set_dev(bio, sb->s_bdev);
*length = (unsigned char) bh->b_data[*offset] | bio->bi_opf = READ;
(unsigned char) bh->b_data[*offset + 1] << 8; bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
*offset += 2;
for (i = 0; i < page_count; ++i) {
if (*offset == msblk->devblksize) { unsigned int len =
put_bh(bh); min_t(unsigned int, PAGE_SIZE - offset, total_len);
bh = sb_bread(sb, ++(*cur_index)); struct page *page = alloc_page(GFP_NOIO);
if (bh == NULL)
return NULL; if (!page) {
*offset = 0; error = -ENOMEM;
goto out_free_bio;
}
if (!bio_add_page(bio, page, len, offset)) {
error = -EIO;
goto out_free_bio;
} }
offset = 0;
total_len -= len;
} }
return bh; error = submit_bio_wait(bio);
} if (error)
goto out_free_bio;
*biop = bio;
*block_offset = index & ((1 << msblk->devblksize_log2) - 1);
return 0;
out_free_bio:
bio_free_pages(bio);
bio_put(bio);
return error;
}
/* /*
* Read and decompress a metadata block or datablock. Length is non-zero * Read and decompress a metadata block or datablock. Length is non-zero
...@@ -76,129 +136,88 @@ static struct buffer_head *get_block_length(struct super_block *sb, ...@@ -76,129 +136,88 @@ static struct buffer_head *get_block_length(struct super_block *sb,
* algorithms). * algorithms).
*/ */
int squashfs_read_data(struct super_block *sb, u64 index, int length, int squashfs_read_data(struct super_block *sb, u64 index, int length,
u64 *next_index, struct squashfs_page_actor *output) u64 *next_index, struct squashfs_page_actor *output)
{ {
struct squashfs_sb_info *msblk = sb->s_fs_info; struct squashfs_sb_info *msblk = sb->s_fs_info;
struct buffer_head **bh; struct bio *bio = NULL;
int offset = index & ((1 << msblk->devblksize_log2) - 1); int compressed;
u64 cur_index = index >> msblk->devblksize_log2; int res;
int bytes, compressed, b = 0, k = 0, avail, i; int offset;
bh = kcalloc(((output->length + msblk->devblksize - 1)
>> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
if (bh == NULL)
return -ENOMEM;
if (length) { if (length) {
/* /*
* Datablock. * Datablock.
*/ */
bytes = -offset;
compressed = SQUASHFS_COMPRESSED_BLOCK(length); compressed = SQUASHFS_COMPRESSED_BLOCK(length);
length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length); length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
if (next_index)
*next_index = index + length;
TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n", TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
index, compressed ? "" : "un", length, output->length); index, compressed ? "" : "un", length, output->length);
if (length < 0 || length > output->length ||
(index + length) > msblk->bytes_used)
goto read_failure;
for (b = 0; bytes < length; b++, cur_index++) {
bh[b] = sb_getblk(sb, cur_index);
if (bh[b] == NULL)
goto block_release;
bytes += msblk->devblksize;
}
ll_rw_block(REQ_OP_READ, 0, b, bh);
} else { } else {
/* /*
* Metadata block. * Metadata block.
*/ */
if ((index + 2) > msblk->bytes_used) const u8 *data;
goto read_failure; struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
bh[0] = get_block_length(sb, &cur_index, &offset, &length); if (index + 2 > msblk->bytes_used) {
if (bh[0] == NULL) res = -EIO;
goto read_failure; goto out;
b = 1; }
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];
if (offset <= bvec->bv_len - 1) {
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);
bytes = msblk->devblksize - offset;
compressed = SQUASHFS_COMPRESSED(length); compressed = SQUASHFS_COMPRESSED(length);
length = SQUASHFS_COMPRESSED_SIZE(length); length = SQUASHFS_COMPRESSED_SIZE(length);
if (next_index) index += 2;
*next_index = index + length + 2;
TRACE("Block @ 0x%llx, %scompressed size %d\n", index, TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
compressed ? "" : "un", length); compressed ? "" : "un", length);
if (length < 0 || length > output->length ||
(index + length) > msblk->bytes_used)
goto block_release;
for (; bytes < length; b++) {
bh[b] = sb_getblk(sb, ++cur_index);
if (bh[b] == NULL)
goto block_release;
bytes += msblk->devblksize;
}
ll_rw_block(REQ_OP_READ, 0, b - 1, bh + 1);
} }
if (next_index)
*next_index = index + length;
for (i = 0; i < b; i++) { res = squashfs_bio_read(sb, index, length, &bio, &offset);
wait_on_buffer(bh[i]); if (res)
if (!buffer_uptodate(bh[i])) goto out;
goto block_release;
}
if (compressed) { if (compressed) {
if (!msblk->stream) if (!msblk->stream) {
goto read_failure; res = -EIO;
length = squashfs_decompress(msblk, bh, b, offset, length, goto out_free_bio;
output);
if (length < 0)
goto read_failure;
} else {
/*
* Block is uncompressed.
*/
int in, pg_offset = 0;
void *data = squashfs_first_page(output);
for (bytes = length; k < b; k++) {
in = min(bytes, msblk->devblksize - offset);
bytes -= in;
while (in) {
if (pg_offset == PAGE_SIZE) {
data = squashfs_next_page(output);
pg_offset = 0;
}
avail = min_t(int, in, PAGE_SIZE -
pg_offset);
memcpy(data + pg_offset, bh[k]->b_data + offset,
avail);
in -= avail;
pg_offset += avail;
offset += avail;
}
offset = 0;
put_bh(bh[k]);
} }
squashfs_finish_page(output); res = squashfs_decompress(msblk, bio, offset, length, output);
} else {
res = copy_bio_to_actor(bio, output, offset, length);
} }
kfree(bh); out_free_bio:
return length; bio_free_pages(bio);
bio_put(bio);
block_release: out:
for (; k < b; k++) if (res < 0)
put_bh(bh[k]); ERROR("Failed to read block 0x%llx: %d\n", index, res);
read_failure: return res;
ERROR("squashfs_read_data failed to read block 0x%llx\n",
(unsigned long long) index);
kfree(bh);
return -EIO;
} }
...@@ -10,13 +10,14 @@ ...@@ -10,13 +10,14 @@
* decompressor.h * decompressor.h
*/ */
#include <linux/bio.h>
struct squashfs_decompressor { struct squashfs_decompressor {
void *(*init)(struct squashfs_sb_info *, void *); void *(*init)(struct squashfs_sb_info *, void *);
void *(*comp_opts)(struct squashfs_sb_info *, void *, int); void *(*comp_opts)(struct squashfs_sb_info *, void *, int);
void (*free)(void *); void (*free)(void *);
int (*decompress)(struct squashfs_sb_info *, void *, int (*decompress)(struct squashfs_sb_info *, void *,
struct buffer_head **, int, int, int, struct bio *, int, int, struct squashfs_page_actor *);
struct squashfs_page_actor *);
int id; int id;
char *name; char *name;
int supported; int supported;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/wait.h> #include <linux/wait.h>
#include <linux/cpumask.h> #include <linux/cpumask.h>
...@@ -180,14 +180,15 @@ static struct decomp_stream *get_decomp_stream(struct squashfs_sb_info *msblk, ...@@ -180,14 +180,15 @@ static struct decomp_stream *get_decomp_stream(struct squashfs_sb_info *msblk,
} }
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh, int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
int b, int offset, int length, struct squashfs_page_actor *output) int offset, int length,
struct squashfs_page_actor *output)
{ {
int res; int res;
struct squashfs_stream *stream = msblk->stream; struct squashfs_stream *stream = msblk->stream;
struct decomp_stream *decomp_stream = get_decomp_stream(msblk, stream); struct decomp_stream *decomp_stream = get_decomp_stream(msblk, stream);
res = msblk->decompressor->decompress(msblk, decomp_stream->stream, res = msblk->decompressor->decompress(msblk, decomp_stream->stream,
bh, b, offset, length, output); bio, offset, length, output);
put_decomp_stream(decomp_stream, stream); put_decomp_stream(decomp_stream, stream);
if (res < 0) if (res < 0)
ERROR("%s decompression failed, data probably corrupt\n", ERROR("%s decompression failed, data probably corrupt\n",
......
...@@ -72,14 +72,17 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk) ...@@ -72,14 +72,17 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
} }
} }
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh, int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
int b, int offset, int length, struct squashfs_page_actor *output) int offset, int length, struct squashfs_page_actor *output)
{ {
struct squashfs_stream __percpu *percpu = struct squashfs_stream __percpu *percpu;
(struct squashfs_stream __percpu *) msblk->stream; struct squashfs_stream *stream;
struct squashfs_stream *stream = get_cpu_ptr(percpu); int res;
int res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
offset, length, output); percpu = (struct squashfs_stream __percpu *)msblk->stream;
stream = get_cpu_ptr(percpu);
res = msblk->decompressor->decompress(msblk, stream->stream, bio,
offset, length, output);
put_cpu_ptr(stream); put_cpu_ptr(stream);
if (res < 0) if (res < 0)
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include "squashfs_fs.h" #include "squashfs_fs.h"
#include "squashfs_fs_sb.h" #include "squashfs_fs_sb.h"
...@@ -59,14 +59,15 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk) ...@@ -59,14 +59,15 @@ void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
} }
} }
int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh, int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
int b, int offset, int length, struct squashfs_page_actor *output) int offset, int length,
struct squashfs_page_actor *output)
{ {
int res; int res;
struct squashfs_stream *stream = msblk->stream; struct squashfs_stream *stream = msblk->stream;
mutex_lock(&stream->mutex); mutex_lock(&stream->mutex);
res = msblk->decompressor->decompress(msblk, stream->stream, bh, b, res = msblk->decompressor->decompress(msblk, stream->stream, bio,
offset, length, output); offset, length, output);
mutex_unlock(&stream->mutex); mutex_unlock(&stream->mutex);
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Phillip Lougher <phillip@squashfs.org.uk> * Phillip Lougher <phillip@squashfs.org.uk>
*/ */
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
...@@ -89,20 +89,23 @@ static void lz4_free(void *strm) ...@@ -89,20 +89,23 @@ static void lz4_free(void *strm)
static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm, static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct buffer_head **bh, int b, int offset, int length, struct bio *bio, int offset, int length,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
struct squashfs_lz4 *stream = strm; struct squashfs_lz4 *stream = strm;
void *buff = stream->input, *data; void *buff = stream->input, *data;
int avail, i, bytes = length, res; int bytes = length, res;
for (i = 0; i < b; i++) { while (bio_next_segment(bio, &iter_all)) {
avail = min(bytes, msblk->devblksize - offset); int avail = min(bytes, ((int)bvec->bv_len) - offset);
memcpy(buff, bh[i]->b_data + offset, avail);
data = page_address(bvec->bv_page) + bvec->bv_offset;
memcpy(buff, data + offset, avail);
buff += avail; buff += avail;
bytes -= avail; bytes -= avail;
offset = 0; offset = 0;
put_bh(bh[i]);
} }
res = LZ4_decompress_safe(stream->input, stream->output, res = LZ4_decompress_safe(stream->input, stream->output,
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
*/ */
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/lzo.h> #include <linux/lzo.h>
...@@ -63,21 +63,24 @@ static void lzo_free(void *strm) ...@@ -63,21 +63,24 @@ static void lzo_free(void *strm)
static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm, static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct buffer_head **bh, int b, int offset, int length, struct bio *bio, int offset, int length,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
struct squashfs_lzo *stream = strm; struct squashfs_lzo *stream = strm;
void *buff = stream->input, *data; void *buff = stream->input, *data;
int avail, i, bytes = length, res; int bytes = length, res;
size_t out_len = output->length; size_t out_len = output->length;
for (i = 0; i < b; i++) { while (bio_next_segment(bio, &iter_all)) {
avail = min(bytes, msblk->devblksize - offset); int avail = min(bytes, ((int)bvec->bv_len) - offset);
memcpy(buff, bh[i]->b_data + offset, avail);
data = page_address(bvec->bv_page) + bvec->bv_offset;
memcpy(buff, data + offset, avail);
buff += avail; buff += avail;
bytes -= avail; bytes -= avail;
offset = 0; offset = 0;
put_bh(bh[i]);
} }
res = lzo1x_decompress_safe(stream->input, (size_t)length, res = lzo1x_decompress_safe(stream->input, (size_t)length,
......
...@@ -40,8 +40,8 @@ extern void *squashfs_decompressor_setup(struct super_block *, unsigned short); ...@@ -40,8 +40,8 @@ extern void *squashfs_decompressor_setup(struct super_block *, unsigned short);
/* decompressor_xxx.c */ /* decompressor_xxx.c */
extern void *squashfs_decompressor_create(struct squashfs_sb_info *, void *); extern void *squashfs_decompressor_create(struct squashfs_sb_info *, void *);
extern void squashfs_decompressor_destroy(struct squashfs_sb_info *); extern void squashfs_decompressor_destroy(struct squashfs_sb_info *);
extern int squashfs_decompress(struct squashfs_sb_info *, struct buffer_head **, extern int squashfs_decompress(struct squashfs_sb_info *, struct bio *,
int, int, int, struct squashfs_page_actor *); int, int, struct squashfs_page_actor *);
extern int squashfs_max_decompressors(void); extern int squashfs_max_decompressors(void);
/* export.c */ /* export.c */
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/xz.h> #include <linux/xz.h>
#include <linux/bitops.h> #include <linux/bitops.h>
...@@ -117,11 +117,12 @@ static void squashfs_xz_free(void *strm) ...@@ -117,11 +117,12 @@ static void squashfs_xz_free(void *strm)
static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct buffer_head **bh, int b, int offset, int length, struct bio *bio, int offset, int length,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
enum xz_ret xz_err; struct bvec_iter_all iter_all = {};
int avail, total = 0, k = 0; struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
int total = 0, error = 0;
struct squashfs_xz *stream = strm; struct squashfs_xz *stream = strm;
xz_dec_reset(stream->state); xz_dec_reset(stream->state);
...@@ -131,11 +132,23 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, ...@@ -131,11 +132,23 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
stream->buf.out_size = PAGE_SIZE; stream->buf.out_size = PAGE_SIZE;
stream->buf.out = squashfs_first_page(output); stream->buf.out = squashfs_first_page(output);
do { for (;;) {
if (stream->buf.in_pos == stream->buf.in_size && k < b) { enum xz_ret xz_err;
avail = min(length, msblk->devblksize - offset);
if (stream->buf.in_pos == stream->buf.in_size) {
const void *data;
int avail;
if (!bio_next_segment(bio, &iter_all)) {
/* XZ_STREAM_END must be reached. */
error = -EIO;
break;
}
avail = min(length, ((int)bvec->bv_len) - offset);
data = page_address(bvec->bv_page) + bvec->bv_offset;
length -= avail; length -= avail;
stream->buf.in = bh[k]->b_data + offset; stream->buf.in = data + offset;
stream->buf.in_size = avail; stream->buf.in_size = avail;
stream->buf.in_pos = 0; stream->buf.in_pos = 0;
offset = 0; offset = 0;
...@@ -150,23 +163,17 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm, ...@@ -150,23 +163,17 @@ static int squashfs_xz_uncompress(struct squashfs_sb_info *msblk, void *strm,
} }
xz_err = xz_dec_run(stream->state, &stream->buf); xz_err = xz_dec_run(stream->state, &stream->buf);
if (xz_err == XZ_STREAM_END)
if (stream->buf.in_pos == stream->buf.in_size && k < b) break;
put_bh(bh[k++]); if (xz_err != XZ_OK) {
} while (xz_err == XZ_OK); error = -EIO;
break;
}
}
squashfs_finish_page(output); squashfs_finish_page(output);
if (xz_err != XZ_STREAM_END || k < b) return error ? error : total + stream->buf.out_pos;
goto out;
return total + stream->buf.out_pos;
out:
for (; k < b; k++)
put_bh(bh[k]);
return -EIO;
} }
const struct squashfs_decompressor squashfs_xz_comp_ops = { const struct squashfs_decompressor squashfs_xz_comp_ops = {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/zlib.h> #include <linux/zlib.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
...@@ -50,21 +50,35 @@ static void zlib_free(void *strm) ...@@ -50,21 +50,35 @@ static void zlib_free(void *strm)
static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct buffer_head **bh, int b, int offset, int length, struct bio *bio, int offset, int length,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
int zlib_err, zlib_init = 0, k = 0; struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
int zlib_init = 0, error = 0;
z_stream *stream = strm; z_stream *stream = strm;
stream->avail_out = PAGE_SIZE; stream->avail_out = PAGE_SIZE;
stream->next_out = squashfs_first_page(output); stream->next_out = squashfs_first_page(output);
stream->avail_in = 0; stream->avail_in = 0;
do { for (;;) {
if (stream->avail_in == 0 && k < b) { int zlib_err;
int avail = min(length, msblk->devblksize - offset);
if (stream->avail_in == 0) {
const void *data;
int avail;
if (!bio_next_segment(bio, &iter_all)) {
/* Z_STREAM_END must be reached. */
error = -EIO;
break;
}
avail = min(length, ((int)bvec->bv_len) - offset);
data = page_address(bvec->bv_page) + bvec->bv_offset;
length -= avail; length -= avail;
stream->next_in = bh[k]->b_data + offset; stream->next_in = data + offset;
stream->avail_in = avail; stream->avail_in = avail;
offset = 0; offset = 0;
} }
...@@ -78,37 +92,28 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, ...@@ -78,37 +92,28 @@ static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
if (!zlib_init) { if (!zlib_init) {
zlib_err = zlib_inflateInit(stream); zlib_err = zlib_inflateInit(stream);
if (zlib_err != Z_OK) { if (zlib_err != Z_OK) {
squashfs_finish_page(output); error = -EIO;
goto out; break;
} }
zlib_init = 1; zlib_init = 1;
} }
zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
if (zlib_err == Z_STREAM_END)
if (stream->avail_in == 0 && k < b) break;
put_bh(bh[k++]); if (zlib_err != Z_OK) {
} while (zlib_err == Z_OK); error = -EIO;
break;
}
}
squashfs_finish_page(output); squashfs_finish_page(output);
if (zlib_err != Z_STREAM_END) if (!error)
goto out; if (zlib_inflateEnd(stream) != Z_OK)
error = -EIO;
zlib_err = zlib_inflateEnd(stream);
if (zlib_err != Z_OK)
goto out;
if (k < b)
goto out;
return stream->total_out;
out:
for (; k < b; k++)
put_bh(bh[k]);
return -EIO; return error ? error : stream->total_out;
} }
const struct squashfs_decompressor squashfs_zlib_comp_ops = { const struct squashfs_decompressor squashfs_zlib_comp_ops = {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
*/ */
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/buffer_head.h> #include <linux/bio.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/zstd.h> #include <linux/zstd.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
...@@ -59,33 +59,44 @@ static void zstd_free(void *strm) ...@@ -59,33 +59,44 @@ static void zstd_free(void *strm)
static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct buffer_head **bh, int b, int offset, int length, struct bio *bio, int offset, int length,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
struct workspace *wksp = strm; struct workspace *wksp = strm;
ZSTD_DStream *stream; ZSTD_DStream *stream;
size_t total_out = 0; size_t total_out = 0;
size_t zstd_err; int error = 0;
int k = 0;
ZSTD_inBuffer in_buf = { NULL, 0, 0 }; ZSTD_inBuffer in_buf = { NULL, 0, 0 };
ZSTD_outBuffer out_buf = { NULL, 0, 0 }; ZSTD_outBuffer out_buf = { NULL, 0, 0 };
struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size); stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size);
if (!stream) { if (!stream) {
ERROR("Failed to initialize zstd decompressor\n"); ERROR("Failed to initialize zstd decompressor\n");
goto out; return -EIO;
} }
out_buf.size = PAGE_SIZE; out_buf.size = PAGE_SIZE;
out_buf.dst = squashfs_first_page(output); out_buf.dst = squashfs_first_page(output);
do { for (;;) {
if (in_buf.pos == in_buf.size && k < b) { size_t zstd_err;
int avail = min(length, msblk->devblksize - offset);
if (in_buf.pos == in_buf.size) {
const void *data;
int avail;
if (!bio_next_segment(bio, &iter_all)) {
error = -EIO;
break;
}
avail = min(length, ((int)bvec->bv_len) - offset);
data = page_address(bvec->bv_page) + bvec->bv_offset;
length -= avail; length -= avail;
in_buf.src = bh[k]->b_data + offset; in_buf.src = data + offset;
in_buf.size = avail; in_buf.size = avail;
in_buf.pos = 0; in_buf.pos = 0;
offset = 0; offset = 0;
...@@ -97,8 +108,8 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, ...@@ -97,8 +108,8 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
/* Shouldn't run out of pages /* Shouldn't run out of pages
* before stream is done. * before stream is done.
*/ */
squashfs_finish_page(output); error = -EIO;
goto out; break;
} }
out_buf.pos = 0; out_buf.pos = 0;
out_buf.size = PAGE_SIZE; out_buf.size = PAGE_SIZE;
...@@ -107,29 +118,20 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, ...@@ -107,29 +118,20 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
total_out -= out_buf.pos; total_out -= out_buf.pos;
zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf); zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf);
total_out += out_buf.pos; /* add the additional data produced */ total_out += out_buf.pos; /* add the additional data produced */
if (zstd_err == 0)
if (in_buf.pos == in_buf.size && k < b) break;
put_bh(bh[k++]);
} while (zstd_err != 0 && !ZSTD_isError(zstd_err)); if (ZSTD_isError(zstd_err)) {
ERROR("zstd decompression error: %d\n",
squashfs_finish_page(output); (int)ZSTD_getErrorCode(zstd_err));
error = -EIO;
if (ZSTD_isError(zstd_err)) { break;
ERROR("zstd decompression error: %d\n", }
(int)ZSTD_getErrorCode(zstd_err));
goto out;
} }
if (k < b) squashfs_finish_page(output);
goto out;
return (int)total_out;
out:
for (; k < b; k++)
put_bh(bh[k]);
return -EIO; return error ? error : total_out;
} }
const struct squashfs_decompressor squashfs_zstd_comp_ops = { const struct squashfs_decompressor squashfs_zstd_comp_ops = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment