Thomas Gleixner | 68252eb | 2019-05-20 19:08:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Squashfs - a compressed read only filesystem for Linux |
| 4 | * |
| 5 | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 6 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 7 | * |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 8 | * zlib_wrapper.c |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/buffer_head.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 15 | #include <linux/zlib.h> |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 16 | #include <linux/vmalloc.h> |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 17 | |
| 18 | #include "squashfs_fs.h" |
| 19 | #include "squashfs_fs_sb.h" |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 20 | #include "squashfs.h" |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 21 | #include "decompressor.h" |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 22 | #include "page_actor.h" |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 23 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 24 | static void *zlib_init(struct squashfs_sb_info *dummy, void *buff) |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 25 | { |
| 26 | z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL); |
| 27 | if (stream == NULL) |
| 28 | goto failed; |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 29 | stream->workspace = vmalloc(zlib_inflate_workspacesize()); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 30 | if (stream->workspace == NULL) |
| 31 | goto failed; |
| 32 | |
| 33 | return stream; |
| 34 | |
| 35 | failed: |
| 36 | ERROR("Failed to allocate zlib workspace\n"); |
| 37 | kfree(stream); |
Phillip Lougher | b7fc0ff | 2011-02-28 01:45:42 +0000 | [diff] [blame] | 38 | return ERR_PTR(-ENOMEM); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 42 | static void zlib_free(void *strm) |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 43 | { |
| 44 | z_stream *stream = strm; |
| 45 | |
| 46 | if (stream) |
Phillip Lougher | 117a91e | 2011-03-22 23:01:26 +0000 | [diff] [blame] | 47 | vfree(stream->workspace); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 48 | kfree(stream); |
| 49 | } |
| 50 | |
| 51 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 52 | static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 53 | struct buffer_head **bh, int b, int offset, int length, |
| 54 | struct squashfs_page_actor *output) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 55 | { |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 56 | int zlib_err, zlib_init = 0, k = 0; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 57 | z_stream *stream = strm; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 58 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 59 | stream->avail_out = PAGE_SIZE; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 60 | stream->next_out = squashfs_first_page(output); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 61 | stream->avail_in = 0; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 62 | |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 63 | do { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 64 | if (stream->avail_in == 0 && k < b) { |
Phillip Lougher | 170cf02 | 2011-01-05 17:52:26 +0000 | [diff] [blame] | 65 | int avail = min(length, msblk->devblksize - offset); |
| 66 | length -= avail; |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 67 | stream->next_in = bh[k]->b_data + offset; |
| 68 | stream->avail_in = avail; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 69 | offset = 0; |
| 70 | } |
| 71 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 72 | if (stream->avail_out == 0) { |
| 73 | stream->next_out = squashfs_next_page(output); |
| 74 | if (stream->next_out != NULL) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 75 | stream->avail_out = PAGE_SIZE; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (!zlib_init) { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 79 | zlib_err = zlib_inflateInit(stream); |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 80 | if (zlib_err != Z_OK) { |
| 81 | squashfs_finish_page(output); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 82 | goto out; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 83 | } |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 84 | zlib_init = 1; |
| 85 | } |
| 86 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 87 | zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 88 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 89 | if (stream->avail_in == 0 && k < b) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 90 | put_bh(bh[k++]); |
| 91 | } while (zlib_err == Z_OK); |
| 92 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 93 | squashfs_finish_page(output); |
| 94 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 95 | if (zlib_err != Z_STREAM_END) |
| 96 | goto out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 97 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 98 | zlib_err = zlib_inflateEnd(stream); |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 99 | if (zlib_err != Z_OK) |
| 100 | goto out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 101 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 102 | if (k < b) |
| 103 | goto out; |
Phillip Lougher | e7ee11f | 2011-01-05 18:02:37 +0000 | [diff] [blame] | 104 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 105 | return stream->total_out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 106 | |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 107 | out: |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 108 | for (; k < b; k++) |
| 109 | put_bh(bh[k]); |
| 110 | |
| 111 | return -EIO; |
| 112 | } |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 113 | |
| 114 | const struct squashfs_decompressor squashfs_zlib_comp_ops = { |
| 115 | .init = zlib_init, |
| 116 | .free = zlib_free, |
| 117 | .decompress = zlib_uncompress, |
| 118 | .id = ZLIB_COMPRESSION, |
| 119 | .name = "zlib", |
| 120 | .supported = 1 |
| 121 | }; |
| 122 | |