blob: f2226afa1625e982c55a9e2ec469340625bd5c10 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Loughere6a6d372009-09-22 19:25:24 +01002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Loughere6a6d372009-09-22 19:25:24 +01007 *
Phillip Loughere6a6d372009-09-22 19:25:24 +01008 * zlib_wrapper.c
9 */
10
11
12#include <linux/mutex.h>
13#include <linux/buffer_head.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010015#include <linux/zlib.h>
Phillip Lougher117a91e2011-03-22 23:01:26 +000016#include <linux/vmalloc.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010017
18#include "squashfs_fs.h"
19#include "squashfs_fs_sb.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010020#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010021#include "decompressor.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000022#include "page_actor.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010023
Phillip Lougher9508c6b2013-11-13 02:56:26 +000024static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
Phillip Lougherf1a40352009-09-23 19:04:49 +010025{
26 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
27 if (stream == NULL)
28 goto failed;
Phillip Lougher117a91e2011-03-22 23:01:26 +000029 stream->workspace = vmalloc(zlib_inflate_workspacesize());
Phillip Lougherf1a40352009-09-23 19:04:49 +010030 if (stream->workspace == NULL)
31 goto failed;
32
33 return stream;
34
35failed:
36 ERROR("Failed to allocate zlib workspace\n");
37 kfree(stream);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000038 return ERR_PTR(-ENOMEM);
Phillip Lougherf1a40352009-09-23 19:04:49 +010039}
40
41
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010042static void zlib_free(void *strm)
Phillip Lougherf1a40352009-09-23 19:04:49 +010043{
44 z_stream *stream = strm;
45
46 if (stream)
Phillip Lougher117a91e2011-03-22 23:01:26 +000047 vfree(stream->workspace);
Phillip Lougherf1a40352009-09-23 19:04:49 +010048 kfree(stream);
49}
50
51
Phillip Lougher9508c6b2013-11-13 02:56:26 +000052static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
Phillip Lougher846b7302013-11-18 02:59:12 +000053 struct buffer_head **bh, int b, int offset, int length,
54 struct squashfs_page_actor *output)
Phillip Loughere6a6d372009-09-22 19:25:24 +010055{
Phillip Lougher846b7302013-11-18 02:59:12 +000056 int zlib_err, zlib_init = 0, k = 0;
Phillip Lougher9508c6b2013-11-13 02:56:26 +000057 z_stream *stream = strm;
Phillip Loughere6a6d372009-09-22 19:25:24 +010058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030059 stream->avail_out = PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +000060 stream->next_out = squashfs_first_page(output);
Phillip Lougherf1a40352009-09-23 19:04:49 +010061 stream->avail_in = 0;
Phillip Loughere6a6d372009-09-22 19:25:24 +010062
Phillip Loughere6a6d372009-09-22 19:25:24 +010063 do {
Phillip Lougherf1a40352009-09-23 19:04:49 +010064 if (stream->avail_in == 0 && k < b) {
Phillip Lougher170cf022011-01-05 17:52:26 +000065 int avail = min(length, msblk->devblksize - offset);
66 length -= avail;
Phillip Lougherf1a40352009-09-23 19:04:49 +010067 stream->next_in = bh[k]->b_data + offset;
68 stream->avail_in = avail;
Phillip Loughere6a6d372009-09-22 19:25:24 +010069 offset = 0;
70 }
71
Phillip Lougher846b7302013-11-18 02:59:12 +000072 if (stream->avail_out == 0) {
73 stream->next_out = squashfs_next_page(output);
74 if (stream->next_out != NULL)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030075 stream->avail_out = PAGE_SIZE;
Phillip Loughere6a6d372009-09-22 19:25:24 +010076 }
77
78 if (!zlib_init) {
Phillip Lougherf1a40352009-09-23 19:04:49 +010079 zlib_err = zlib_inflateInit(stream);
Phillip Lougher846b7302013-11-18 02:59:12 +000080 if (zlib_err != Z_OK) {
81 squashfs_finish_page(output);
Phillip Lougher9508c6b2013-11-13 02:56:26 +000082 goto out;
Phillip Lougher846b7302013-11-18 02:59:12 +000083 }
Phillip Loughere6a6d372009-09-22 19:25:24 +010084 zlib_init = 1;
85 }
86
Phillip Lougherf1a40352009-09-23 19:04:49 +010087 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Phillip Loughere6a6d372009-09-22 19:25:24 +010088
Phillip Lougherf1a40352009-09-23 19:04:49 +010089 if (stream->avail_in == 0 && k < b)
Phillip Loughere6a6d372009-09-22 19:25:24 +010090 put_bh(bh[k++]);
91 } while (zlib_err == Z_OK);
92
Phillip Lougher846b7302013-11-18 02:59:12 +000093 squashfs_finish_page(output);
94
Phillip Lougher9508c6b2013-11-13 02:56:26 +000095 if (zlib_err != Z_STREAM_END)
96 goto out;
Phillip Loughere6a6d372009-09-22 19:25:24 +010097
Phillip Lougherf1a40352009-09-23 19:04:49 +010098 zlib_err = zlib_inflateEnd(stream);
Phillip Lougher9508c6b2013-11-13 02:56:26 +000099 if (zlib_err != Z_OK)
100 goto out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100101
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000102 if (k < b)
103 goto out;
Phillip Loughere7ee11f2011-01-05 18:02:37 +0000104
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000105 return stream->total_out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100106
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000107out:
Phillip Loughere6a6d372009-09-22 19:25:24 +0100108 for (; k < b; k++)
109 put_bh(bh[k]);
110
111 return -EIO;
112}
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100113
114const struct squashfs_decompressor squashfs_zlib_comp_ops = {
115 .init = zlib_init,
116 .free = zlib_free,
117 .decompress = zlib_uncompress,
118 .id = ZLIB_COMPRESSION,
119 .name = "zlib",
120 .supported = 1
121};
122