168252eb5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e6a6d379SPhillip Lougher /*
3e6a6d379SPhillip Lougher * Squashfs - a compressed read only filesystem for Linux
4e6a6d379SPhillip Lougher *
5e6a6d379SPhillip Lougher * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6d7f2ff67SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
7e6a6d379SPhillip Lougher *
8e6a6d379SPhillip Lougher * zlib_wrapper.c
9e6a6d379SPhillip Lougher */
10e6a6d379SPhillip Lougher
11e6a6d379SPhillip Lougher
12e6a6d379SPhillip Lougher #include <linux/mutex.h>
1393e72b3cSPhilippe Liard #include <linux/bio.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15e6a6d379SPhillip Lougher #include <linux/zlib.h>
16117a91e0SPhillip Lougher #include <linux/vmalloc.h>
17e6a6d379SPhillip Lougher
18e6a6d379SPhillip Lougher #include "squashfs_fs.h"
19e6a6d379SPhillip Lougher #include "squashfs_fs_sb.h"
20e6a6d379SPhillip Lougher #include "squashfs.h"
214c0f0bb2SPhillip Lougher #include "decompressor.h"
22846b730eSPhillip Lougher #include "page_actor.h"
23e6a6d379SPhillip Lougher
zlib_init(struct squashfs_sb_info * dummy,void * buff)249508c6b9SPhillip Lougher static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
25f1a40359SPhillip Lougher {
26f1a40359SPhillip Lougher z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
27f1a40359SPhillip Lougher if (stream == NULL)
28f1a40359SPhillip Lougher goto failed;
29117a91e0SPhillip Lougher stream->workspace = vmalloc(zlib_inflate_workspacesize());
30f1a40359SPhillip Lougher if (stream->workspace == NULL)
31f1a40359SPhillip Lougher goto failed;
32f1a40359SPhillip Lougher
33f1a40359SPhillip Lougher return stream;
34f1a40359SPhillip Lougher
35f1a40359SPhillip Lougher failed:
36f1a40359SPhillip Lougher ERROR("Failed to allocate zlib workspace\n");
37f1a40359SPhillip Lougher kfree(stream);
38b7fc0ff0SPhillip Lougher return ERR_PTR(-ENOMEM);
39f1a40359SPhillip Lougher }
40f1a40359SPhillip Lougher
41f1a40359SPhillip Lougher
zlib_free(void * strm)424c0f0bb2SPhillip Lougher static void zlib_free(void *strm)
43f1a40359SPhillip Lougher {
44f1a40359SPhillip Lougher z_stream *stream = strm;
45f1a40359SPhillip Lougher
46f1a40359SPhillip Lougher if (stream)
47117a91e0SPhillip Lougher vfree(stream->workspace);
48f1a40359SPhillip Lougher kfree(stream);
49f1a40359SPhillip Lougher }
50f1a40359SPhillip Lougher
51f1a40359SPhillip Lougher
zlib_uncompress(struct squashfs_sb_info * msblk,void * strm,struct bio * bio,int offset,int length,struct squashfs_page_actor * output)529508c6b9SPhillip Lougher static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
5393e72b3cSPhilippe Liard struct bio *bio, int offset, int length,
54846b730eSPhillip Lougher struct squashfs_page_actor *output)
55e6a6d379SPhillip Lougher {
5693e72b3cSPhilippe Liard struct bvec_iter_all iter_all = {};
5793e72b3cSPhilippe Liard struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
5893e72b3cSPhilippe Liard int zlib_init = 0, error = 0;
599508c6b9SPhillip Lougher z_stream *stream = strm;
60e6a6d379SPhillip Lougher
6109cbfeafSKirill A. Shutemov stream->avail_out = PAGE_SIZE;
62846b730eSPhillip Lougher stream->next_out = squashfs_first_page(output);
63f1a40359SPhillip Lougher stream->avail_in = 0;
64e6a6d379SPhillip Lougher
65*f268eeddSPhillip Lougher if (IS_ERR(stream->next_out)) {
66*f268eeddSPhillip Lougher error = PTR_ERR(stream->next_out);
67*f268eeddSPhillip Lougher goto finish;
68*f268eeddSPhillip Lougher }
69*f268eeddSPhillip Lougher
7093e72b3cSPhilippe Liard for (;;) {
7193e72b3cSPhilippe Liard int zlib_err;
7293e72b3cSPhilippe Liard
7393e72b3cSPhilippe Liard if (stream->avail_in == 0) {
7493e72b3cSPhilippe Liard const void *data;
7593e72b3cSPhilippe Liard int avail;
7693e72b3cSPhilippe Liard
7793e72b3cSPhilippe Liard if (!bio_next_segment(bio, &iter_all)) {
7893e72b3cSPhilippe Liard /* Z_STREAM_END must be reached. */
7993e72b3cSPhilippe Liard error = -EIO;
8093e72b3cSPhilippe Liard break;
8193e72b3cSPhilippe Liard }
8293e72b3cSPhilippe Liard
8393e72b3cSPhilippe Liard avail = min(length, ((int)bvec->bv_len) - offset);
84fbc27241SChristoph Hellwig data = bvec_virt(bvec);
85170cf021SPhillip Lougher length -= avail;
8693e72b3cSPhilippe Liard stream->next_in = data + offset;
87f1a40359SPhillip Lougher stream->avail_in = avail;
88e6a6d379SPhillip Lougher offset = 0;
89e6a6d379SPhillip Lougher }
90e6a6d379SPhillip Lougher
91846b730eSPhillip Lougher if (stream->avail_out == 0) {
92846b730eSPhillip Lougher stream->next_out = squashfs_next_page(output);
93*f268eeddSPhillip Lougher if (IS_ERR(stream->next_out)) {
94*f268eeddSPhillip Lougher error = PTR_ERR(stream->next_out);
95*f268eeddSPhillip Lougher break;
96*f268eeddSPhillip Lougher } else if (stream->next_out != NULL)
9709cbfeafSKirill A. Shutemov stream->avail_out = PAGE_SIZE;
98e6a6d379SPhillip Lougher }
99e6a6d379SPhillip Lougher
100e6a6d379SPhillip Lougher if (!zlib_init) {
101f1a40359SPhillip Lougher zlib_err = zlib_inflateInit(stream);
102846b730eSPhillip Lougher if (zlib_err != Z_OK) {
10393e72b3cSPhilippe Liard error = -EIO;
10493e72b3cSPhilippe Liard break;
105846b730eSPhillip Lougher }
106e6a6d379SPhillip Lougher zlib_init = 1;
107e6a6d379SPhillip Lougher }
108e6a6d379SPhillip Lougher
109f1a40359SPhillip Lougher zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
11093e72b3cSPhilippe Liard if (zlib_err == Z_STREAM_END)
11193e72b3cSPhilippe Liard break;
11293e72b3cSPhilippe Liard if (zlib_err != Z_OK) {
11393e72b3cSPhilippe Liard error = -EIO;
11493e72b3cSPhilippe Liard break;
11593e72b3cSPhilippe Liard }
11693e72b3cSPhilippe Liard }
117e6a6d379SPhillip Lougher
118*f268eeddSPhillip Lougher finish:
119846b730eSPhillip Lougher squashfs_finish_page(output);
120846b730eSPhillip Lougher
12193e72b3cSPhilippe Liard if (!error)
12293e72b3cSPhilippe Liard if (zlib_inflateEnd(stream) != Z_OK)
12393e72b3cSPhilippe Liard error = -EIO;
124e6a6d379SPhillip Lougher
12593e72b3cSPhilippe Liard return error ? error : stream->total_out;
126e6a6d379SPhillip Lougher }
1274c0f0bb2SPhillip Lougher
1284c0f0bb2SPhillip Lougher const struct squashfs_decompressor squashfs_zlib_comp_ops = {
1294c0f0bb2SPhillip Lougher .init = zlib_init,
1304c0f0bb2SPhillip Lougher .free = zlib_free,
1314c0f0bb2SPhillip Lougher .decompress = zlib_uncompress,
1324c0f0bb2SPhillip Lougher .id = ZLIB_COMPRESSION,
1334c0f0bb2SPhillip Lougher .name = "zlib",
134*f268eeddSPhillip Lougher .alloc_buffer = 1,
1354c0f0bb2SPhillip Lougher .supported = 1
1364c0f0bb2SPhillip Lougher };
1374c0f0bb2SPhillip Lougher
138