blob: c814594d522eebb180a9c38500cb818899df709c [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
5 * Phillip Lougher <phillip@lougher.demon.co.uk>
6 *
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>
27#include <linux/zlib.h>
28
29#include "squashfs_fs.h"
30#include "squashfs_fs_sb.h"
31#include "squashfs_fs_i.h"
32#include "squashfs.h"
33
Phillip Lougherf1a40352009-09-23 19:04:49 +010034void *squashfs_zlib_init()
35{
36 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
37 if (stream == NULL)
38 goto failed;
39 stream->workspace = kmalloc(zlib_inflate_workspacesize(),
40 GFP_KERNEL);
41 if (stream->workspace == NULL)
42 goto failed;
43
44 return stream;
45
46failed:
47 ERROR("Failed to allocate zlib workspace\n");
48 kfree(stream);
49 return NULL;
50}
51
52
53void squashfs_zlib_free(void *strm)
54{
55 z_stream *stream = strm;
56
57 if (stream)
58 kfree(stream->workspace);
59 kfree(stream);
60}
61
62
Phillip Loughere6a6d372009-09-22 19:25:24 +010063int squashfs_zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
64 struct buffer_head **bh, int b, int offset, int length, int srclength,
65 int pages)
66{
67 int zlib_err = 0, zlib_init = 0;
68 int avail, bytes, k = 0, page = 0;
Phillip Lougherf1a40352009-09-23 19:04:49 +010069 z_stream *stream = msblk->stream;
Phillip Loughere6a6d372009-09-22 19:25:24 +010070
71 mutex_lock(&msblk->read_data_mutex);
72
Phillip Lougherf1a40352009-09-23 19:04:49 +010073 stream->avail_out = 0;
74 stream->avail_in = 0;
Phillip Loughere6a6d372009-09-22 19:25:24 +010075
76 bytes = length;
77 do {
Phillip Lougherf1a40352009-09-23 19:04:49 +010078 if (stream->avail_in == 0 && k < b) {
Phillip Loughere6a6d372009-09-22 19:25:24 +010079 avail = min(bytes, msblk->devblksize - offset);
80 bytes -= avail;
81 wait_on_buffer(bh[k]);
82 if (!buffer_uptodate(bh[k]))
83 goto release_mutex;
84
85 if (avail == 0) {
86 offset = 0;
87 put_bh(bh[k++]);
88 continue;
89 }
90
Phillip Lougherf1a40352009-09-23 19:04:49 +010091 stream->next_in = bh[k]->b_data + offset;
92 stream->avail_in = avail;
Phillip Loughere6a6d372009-09-22 19:25:24 +010093 offset = 0;
94 }
95
Phillip Lougherf1a40352009-09-23 19:04:49 +010096 if (stream->avail_out == 0 && page < pages) {
97 stream->next_out = buffer[page++];
98 stream->avail_out = PAGE_CACHE_SIZE;
Phillip Loughere6a6d372009-09-22 19:25:24 +010099 }
100
101 if (!zlib_init) {
Phillip Lougherf1a40352009-09-23 19:04:49 +0100102 zlib_err = zlib_inflateInit(stream);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100103 if (zlib_err != Z_OK) {
104 ERROR("zlib_inflateInit returned unexpected "
105 "result 0x%x, srclength %d\n",
106 zlib_err, srclength);
107 goto release_mutex;
108 }
109 zlib_init = 1;
110 }
111
Phillip Lougherf1a40352009-09-23 19:04:49 +0100112 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100113
Phillip Lougherf1a40352009-09-23 19:04:49 +0100114 if (stream->avail_in == 0 && k < b)
Phillip Loughere6a6d372009-09-22 19:25:24 +0100115 put_bh(bh[k++]);
116 } while (zlib_err == Z_OK);
117
118 if (zlib_err != Z_STREAM_END) {
119 ERROR("zlib_inflate error, data probably corrupt\n");
120 goto release_mutex;
121 }
122
Phillip Lougherf1a40352009-09-23 19:04:49 +0100123 zlib_err = zlib_inflateEnd(stream);
Phillip Loughere6a6d372009-09-22 19:25:24 +0100124 if (zlib_err != Z_OK) {
125 ERROR("zlib_inflate error, data probably corrupt\n");
126 goto release_mutex;
127 }
128
129 mutex_unlock(&msblk->read_data_mutex);
Phillip Lougherf1a40352009-09-23 19:04:49 +0100130 return stream->total_out;
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}