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 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 5 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 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> |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 29 | #include <linux/vmalloc.h> |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 30 | |
| 31 | #include "squashfs_fs.h" |
| 32 | #include "squashfs_fs_sb.h" |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 33 | #include "squashfs.h" |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 34 | #include "decompressor.h" |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 35 | #include "page_actor.h" |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 36 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 37 | static void *zlib_init(struct squashfs_sb_info *dummy, void *buff) |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 38 | { |
| 39 | z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL); |
| 40 | if (stream == NULL) |
| 41 | goto failed; |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 42 | stream->workspace = vmalloc(zlib_inflate_workspacesize()); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 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); |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 51 | return ERR_PTR(-ENOMEM); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 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) |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 60 | vfree(stream->workspace); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 61 | kfree(stream); |
| 62 | } |
| 63 | |
| 64 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 65 | static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 66 | struct buffer_head **bh, int b, int offset, int length, |
| 67 | struct squashfs_page_actor *output) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 68 | { |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 69 | int zlib_err, zlib_init = 0, k = 0; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 70 | z_stream *stream = strm; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 71 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 72 | stream->avail_out = PAGE_CACHE_SIZE; |
| 73 | stream->next_out = squashfs_first_page(output); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 74 | stream->avail_in = 0; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 75 | |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 76 | do { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 77 | if (stream->avail_in == 0 && k < b) { |
Phillip Lougher | 170cf02 | 2011-01-05 17:52:26 +0000 | [diff] [blame] | 78 | int avail = min(length, msblk->devblksize - offset); |
| 79 | length -= avail; |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 80 | stream->next_in = bh[k]->b_data + offset; |
| 81 | stream->avail_in = avail; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 82 | offset = 0; |
| 83 | } |
| 84 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 85 | if (stream->avail_out == 0) { |
| 86 | stream->next_out = squashfs_next_page(output); |
| 87 | if (stream->next_out != NULL) |
| 88 | stream->avail_out = PAGE_CACHE_SIZE; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (!zlib_init) { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 92 | zlib_err = zlib_inflateInit(stream); |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 93 | if (zlib_err != Z_OK) { |
| 94 | squashfs_finish_page(output); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 95 | goto out; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 96 | } |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 97 | zlib_init = 1; |
| 98 | } |
| 99 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 100 | zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 101 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 102 | if (stream->avail_in == 0 && k < b) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 103 | put_bh(bh[k++]); |
| 104 | } while (zlib_err == Z_OK); |
| 105 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 106 | squashfs_finish_page(output); |
| 107 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 108 | if (zlib_err != Z_STREAM_END) |
| 109 | goto out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 110 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 111 | zlib_err = zlib_inflateEnd(stream); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 112 | if (zlib_err != Z_OK) |
| 113 | goto out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 114 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 115 | if (k < b) |
| 116 | goto out; |
Phillip Lougher | e7ee11f | 2011-01-05 18:02:37 +0000 | [diff] [blame] | 117 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 118 | return stream->total_out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 119 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 120 | out: |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 121 | for (; k < b; k++) |
| 122 | put_bh(bh[k]); |
| 123 | |
| 124 | return -EIO; |
| 125 | } |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 126 | |
| 127 | const struct squashfs_decompressor squashfs_zlib_comp_ops = { |
| 128 | .init = zlib_init, |
| 129 | .free = zlib_free, |
| 130 | .decompress = zlib_uncompress, |
| 131 | .id = ZLIB_COMPRESSION, |
| 132 | .name = "zlib", |
| 133 | .supported = 1 |
| 134 | }; |
| 135 | |