blob: 55d918fd2d862605beb85dae54c3a177b776381d [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 Loughere6a6d372009-09-22 19:25:24 +010035
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000036static void *zlib_init(struct squashfs_sb_info *dummy, void *buff, int len)
Phillip Lougherf1a40352009-09-23 19:04:49 +010037{
38 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
39 if (stream == NULL)
40 goto failed;
Phillip Lougher117a91e2011-03-22 23:01:26 +000041 stream->workspace = vmalloc(zlib_inflate_workspacesize());
Phillip Lougherf1a40352009-09-23 19:04:49 +010042 if (stream->workspace == NULL)
43 goto failed;
44
45 return stream;
46
47failed:
48 ERROR("Failed to allocate zlib workspace\n");
49 kfree(stream);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000050 return ERR_PTR(-ENOMEM);
Phillip Lougherf1a40352009-09-23 19:04:49 +010051}
52
53
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010054static void zlib_free(void *strm)
Phillip Lougherf1a40352009-09-23 19:04:49 +010055{
56 z_stream *stream = strm;
57
58 if (stream)
Phillip Lougher117a91e2011-03-22 23:01:26 +000059 vfree(stream->workspace);
Phillip Lougherf1a40352009-09-23 19:04:49 +010060 kfree(stream);
61}
62
63
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010064static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
Phillip Loughere6a6d372009-09-22 19:25:24 +010065 struct buffer_head **bh, int b, int offset, int length, int srclength,
66 int pages)
67{
Phillip Lougher170cf022011-01-05 17:52:26 +000068 int zlib_err, zlib_init = 0;
69 int k = 0, page = 0;
Phillip Lougherf1a40352009-09-23 19:04:49 +010070 z_stream *stream = msblk->stream;
Phillip Loughere6a6d372009-09-22 19:25:24 +010071
72 mutex_lock(&msblk->read_data_mutex);
73
Phillip Lougherf1a40352009-09-23 19:04:49 +010074 stream->avail_out = 0;
75 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 Loughere6a6d372009-09-22 19:25:24 +010081 wait_on_buffer(bh[k]);
82 if (!buffer_uptodate(bh[k]))
83 goto release_mutex;
84
Phillip Lougherf1a40352009-09-23 19:04:49 +010085 stream->next_in = bh[k]->b_data + offset;
86 stream->avail_in = avail;
Phillip Loughere6a6d372009-09-22 19:25:24 +010087 offset = 0;
88 }
89
Phillip Lougherf1a40352009-09-23 19:04:49 +010090 if (stream->avail_out == 0 && page < pages) {
91 stream->next_out = buffer[page++];
92 stream->avail_out = PAGE_CACHE_SIZE;
Phillip Loughere6a6d372009-09-22 19:25:24 +010093 }
94
95 if (!zlib_init) {
Phillip Lougherf1a40352009-09-23 19:04:49 +010096 zlib_err = zlib_inflateInit(stream);
Phillip Loughere6a6d372009-09-22 19:25:24 +010097 if (zlib_err != Z_OK) {
98 ERROR("zlib_inflateInit returned unexpected "
99 "result 0x%x, srclength %d\n",
100 zlib_err, srclength);
101 goto release_mutex;
102 }
103 zlib_init = 1;
104 }
105
Phillip Lougherf1a40352009-09-23 19:04:49 +0100106 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100107
Phillip Lougherf1a40352009-09-23 19:04:49 +0100108 if (stream->avail_in == 0 && k < b)
Phillip Loughere6a6d372009-09-22 19:25:24 +0100109 put_bh(bh[k++]);
110 } while (zlib_err == Z_OK);
111
112 if (zlib_err != Z_STREAM_END) {
113 ERROR("zlib_inflate error, data probably corrupt\n");
114 goto release_mutex;
115 }
116
Phillip Lougherf1a40352009-09-23 19:04:49 +0100117 zlib_err = zlib_inflateEnd(stream);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100118 if (zlib_err != Z_OK) {
119 ERROR("zlib_inflate error, data probably corrupt\n");
120 goto release_mutex;
121 }
122
Phillip Loughere7ee11f2011-01-05 18:02:37 +0000123 if (k < b) {
124 ERROR("zlib_uncompress error, data remaining\n");
125 goto release_mutex;
126 }
127
Phillip Lougher792590c2010-04-04 22:20:58 +0100128 length = stream->total_out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100129 mutex_unlock(&msblk->read_data_mutex);
Phillip Lougher792590c2010-04-04 22:20:58 +0100130 return length;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100131
132release_mutex:
133 mutex_unlock(&msblk->read_data_mutex);
134
135 for (; k < b; k++)
136 put_bh(bh[k]);
137
138 return -EIO;
139}
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100140
141const struct squashfs_decompressor squashfs_zlib_comp_ops = {
142 .init = zlib_init,
143 .free = zlib_free,
144 .decompress = zlib_uncompress,
145 .id = ZLIB_COMPRESSION,
146 .name = "zlib",
147 .supported = 1
148};
149