blob: d917c728422b5120c4eddecebcd847735c38b8cd [file] [log] [blame]
Phillip Loughere6a6d372009-09-22 19:25:24 +01001/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01005 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Loughere6a6d372009-09-22 19:25:24 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * zlib_wrapper.c
22 */
23
24
25#include <linux/mutex.h>
26#include <linux/buffer_head.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010028#include <linux/zlib.h>
Phillip Lougher117a91e2011-03-22 23:01:26 +000029#include <linux/vmalloc.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010030
31#include "squashfs_fs.h"
32#include "squashfs_fs_sb.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010033#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010034#include "decompressor.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000035#include "page_actor.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010036
Phillip Lougher9508c6b2013-11-13 02:56:26 +000037static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
Phillip Lougherf1a40352009-09-23 19:04:49 +010038{
39 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
40 if (stream == NULL)
41 goto failed;
Phillip Lougher117a91e2011-03-22 23:01:26 +000042 stream->workspace = vmalloc(zlib_inflate_workspacesize());
Phillip Lougherf1a40352009-09-23 19:04:49 +010043 if (stream->workspace == NULL)
44 goto failed;
45
46 return stream;
47
48failed:
49 ERROR("Failed to allocate zlib workspace\n");
50 kfree(stream);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000051 return ERR_PTR(-ENOMEM);
Phillip Lougherf1a40352009-09-23 19:04:49 +010052}
53
54
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010055static void zlib_free(void *strm)
Phillip Lougherf1a40352009-09-23 19:04:49 +010056{
57 z_stream *stream = strm;
58
59 if (stream)
Phillip Lougher117a91e2011-03-22 23:01:26 +000060 vfree(stream->workspace);
Phillip Lougherf1a40352009-09-23 19:04:49 +010061 kfree(stream);
62}
63
64
Phillip Lougher9508c6b2013-11-13 02:56:26 +000065static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
Phillip Lougher846b7302013-11-18 02:59:12 +000066 struct buffer_head **bh, int b, int offset, int length,
67 struct squashfs_page_actor *output)
Phillip Loughere6a6d372009-09-22 19:25:24 +010068{
Adrien Schildknecht38840af2016-09-28 13:59:18 -070069 void *buf = NULL;
Phillip Lougher846b7302013-11-18 02:59:12 +000070 int zlib_err, zlib_init = 0, k = 0;
Phillip Lougher9508c6b2013-11-13 02:56:26 +000071 z_stream *stream = strm;
Phillip Loughere6a6d372009-09-22 19:25:24 +010072
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030073 stream->avail_out = PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +000074 stream->next_out = squashfs_first_page(output);
Phillip Lougherf1a40352009-09-23 19:04:49 +010075 stream->avail_in = 0;
Phillip Loughere6a6d372009-09-22 19:25:24 +010076
Phillip Loughere6a6d372009-09-22 19:25:24 +010077 do {
Phillip Lougherf1a40352009-09-23 19:04:49 +010078 if (stream->avail_in == 0 && k < b) {
Phillip Lougher170cf022011-01-05 17:52:26 +000079 int avail = min(length, msblk->devblksize - offset);
80 length -= avail;
Phillip Lougherf1a40352009-09-23 19:04:49 +010081 stream->next_in = bh[k]->b_data + offset;
82 stream->avail_in = avail;
Phillip Loughere6a6d372009-09-22 19:25:24 +010083 offset = 0;
84 }
85
Phillip Lougher846b7302013-11-18 02:59:12 +000086 if (stream->avail_out == 0) {
87 stream->next_out = squashfs_next_page(output);
Adrien Schildknecht38840af2016-09-28 13:59:18 -070088 if (!IS_ERR(stream->next_out))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030089 stream->avail_out = PAGE_SIZE;
Phillip Loughere6a6d372009-09-22 19:25:24 +010090 }
91
Adrien Schildknecht38840af2016-09-28 13:59:18 -070092 if (!stream->next_out) {
93 if (!buf) {
94 buf = kmalloc(PAGE_SIZE, GFP_ATOMIC);
95 if (!buf)
96 goto out;
97 }
98 stream->next_out = buf;
99 }
100
Phillip Loughere6a6d372009-09-22 19:25:24 +0100101 if (!zlib_init) {
Phillip Lougherf1a40352009-09-23 19:04:49 +0100102 zlib_err = zlib_inflateInit(stream);
Phillip Lougher846b7302013-11-18 02:59:12 +0000103 if (zlib_err != Z_OK) {
104 squashfs_finish_page(output);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000105 goto out;
Phillip Lougher846b7302013-11-18 02:59:12 +0000106 }
Phillip Loughere6a6d372009-09-22 19:25:24 +0100107 zlib_init = 1;
108 }
109
Phillip Lougherf1a40352009-09-23 19:04:49 +0100110 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100111
Phillip Lougherf1a40352009-09-23 19:04:49 +0100112 if (stream->avail_in == 0 && k < b)
Phillip Loughere6a6d372009-09-22 19:25:24 +0100113 put_bh(bh[k++]);
114 } while (zlib_err == Z_OK);
115
Phillip Lougher846b7302013-11-18 02:59:12 +0000116 squashfs_finish_page(output);
117
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000118 if (zlib_err != Z_STREAM_END)
119 goto out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100120
Phillip Lougherf1a40352009-09-23 19:04:49 +0100121 zlib_err = zlib_inflateEnd(stream);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000122 if (zlib_err != Z_OK)
123 goto out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100124
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000125 if (k < b)
126 goto out;
Phillip Loughere7ee11f2011-01-05 18:02:37 +0000127
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700128 kfree(buf);
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000129 return stream->total_out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100130
Phillip Lougher9508c6b2013-11-13 02:56:26 +0000131out:
Phillip Loughere6a6d372009-09-22 19:25:24 +0100132 for (; k < b; k++)
133 put_bh(bh[k]);
Adrien Schildknecht38840af2016-09-28 13:59:18 -0700134 kfree(buf);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100135
136 return -EIO;
137}
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100138
139const struct squashfs_decompressor squashfs_zlib_comp_ops = {
140 .init = zlib_init,
141 .free = zlib_free,
142 .decompress = zlib_uncompress,
143 .id = ZLIB_COMPRESSION,
144 .name = "zlib",
145 .supported = 1
146};
147