Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame^] | 33 | #include "decompressor.h" |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 34 | |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame^] | 35 | static void *zlib_init(struct squashfs_sb_info *dummy) |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 36 | { |
| 37 | z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL); |
| 38 | if (stream == NULL) |
| 39 | goto failed; |
| 40 | stream->workspace = kmalloc(zlib_inflate_workspacesize(), |
| 41 | GFP_KERNEL); |
| 42 | if (stream->workspace == NULL) |
| 43 | goto failed; |
| 44 | |
| 45 | return stream; |
| 46 | |
| 47 | failed: |
| 48 | ERROR("Failed to allocate zlib workspace\n"); |
| 49 | kfree(stream); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame^] | 54 | static void zlib_free(void *strm) |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 55 | { |
| 56 | z_stream *stream = strm; |
| 57 | |
| 58 | if (stream) |
| 59 | kfree(stream->workspace); |
| 60 | kfree(stream); |
| 61 | } |
| 62 | |
| 63 | |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame^] | 64 | static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer, |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 65 | struct buffer_head **bh, int b, int offset, int length, int srclength, |
| 66 | int pages) |
| 67 | { |
| 68 | int zlib_err = 0, zlib_init = 0; |
| 69 | int avail, bytes, k = 0, page = 0; |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 70 | z_stream *stream = msblk->stream; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 71 | |
| 72 | mutex_lock(&msblk->read_data_mutex); |
| 73 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 74 | stream->avail_out = 0; |
| 75 | stream->avail_in = 0; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 76 | |
| 77 | bytes = length; |
| 78 | do { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 79 | if (stream->avail_in == 0 && k < b) { |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 80 | avail = min(bytes, msblk->devblksize - offset); |
| 81 | bytes -= avail; |
| 82 | wait_on_buffer(bh[k]); |
| 83 | if (!buffer_uptodate(bh[k])) |
| 84 | goto release_mutex; |
| 85 | |
| 86 | if (avail == 0) { |
| 87 | offset = 0; |
| 88 | put_bh(bh[k++]); |
| 89 | continue; |
| 90 | } |
| 91 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 92 | stream->next_in = bh[k]->b_data + offset; |
| 93 | stream->avail_in = avail; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 94 | offset = 0; |
| 95 | } |
| 96 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 97 | if (stream->avail_out == 0 && page < pages) { |
| 98 | stream->next_out = buffer[page++]; |
| 99 | stream->avail_out = PAGE_CACHE_SIZE; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (!zlib_init) { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 103 | zlib_err = zlib_inflateInit(stream); |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 104 | if (zlib_err != Z_OK) { |
| 105 | ERROR("zlib_inflateInit returned unexpected " |
| 106 | "result 0x%x, srclength %d\n", |
| 107 | zlib_err, srclength); |
| 108 | goto release_mutex; |
| 109 | } |
| 110 | zlib_init = 1; |
| 111 | } |
| 112 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 113 | zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 114 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 115 | if (stream->avail_in == 0 && k < b) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 116 | put_bh(bh[k++]); |
| 117 | } while (zlib_err == Z_OK); |
| 118 | |
| 119 | if (zlib_err != Z_STREAM_END) { |
| 120 | ERROR("zlib_inflate error, data probably corrupt\n"); |
| 121 | goto release_mutex; |
| 122 | } |
| 123 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 124 | zlib_err = zlib_inflateEnd(stream); |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 125 | if (zlib_err != Z_OK) { |
| 126 | ERROR("zlib_inflate error, data probably corrupt\n"); |
| 127 | goto release_mutex; |
| 128 | } |
| 129 | |
| 130 | mutex_unlock(&msblk->read_data_mutex); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 131 | return stream->total_out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 132 | |
| 133 | release_mutex: |
| 134 | mutex_unlock(&msblk->read_data_mutex); |
| 135 | |
| 136 | for (; k < b; k++) |
| 137 | put_bh(bh[k]); |
| 138 | |
| 139 | return -EIO; |
| 140 | } |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame^] | 141 | |
| 142 | const struct squashfs_decompressor squashfs_zlib_comp_ops = { |
| 143 | .init = zlib_init, |
| 144 | .free = zlib_free, |
| 145 | .decompress = zlib_uncompress, |
| 146 | .id = ZLIB_COMPRESSION, |
| 147 | .name = "zlib", |
| 148 | .supported = 1 |
| 149 | }; |
| 150 | |