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