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> |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 13 | #include <linux/bio.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, |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 53 | struct bio *bio, int offset, int length, |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 54 | struct squashfs_page_actor *output) |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 55 | { |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 56 | struct bvec_iter_all iter_all = {}; |
| 57 | struct bio_vec *bvec = bvec_init_iter_all(&iter_all); |
| 58 | int zlib_init = 0, error = 0; |
Phillip Lougher | 9508c6b | 2013-11-13 02:56:26 +0000 | [diff] [blame] | 59 | z_stream *stream = strm; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 60 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 61 | stream->avail_out = PAGE_SIZE; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 62 | stream->next_out = squashfs_first_page(output); |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 63 | stream->avail_in = 0; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 64 | |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 65 | for (;;) { |
| 66 | int zlib_err; |
| 67 | |
| 68 | if (stream->avail_in == 0) { |
| 69 | const void *data; |
| 70 | int avail; |
| 71 | |
| 72 | if (!bio_next_segment(bio, &iter_all)) { |
| 73 | /* Z_STREAM_END must be reached. */ |
| 74 | error = -EIO; |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | avail = min(length, ((int)bvec->bv_len) - offset); |
Christoph Hellwig | fbc27241 | 2021-08-04 11:56:25 +0200 | [diff] [blame] | 79 | data = bvec_virt(bvec); |
Phillip Lougher | 170cf02 | 2011-01-05 17:52:26 +0000 | [diff] [blame] | 80 | length -= avail; |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 81 | stream->next_in = data + offset; |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 82 | stream->avail_in = avail; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 83 | offset = 0; |
| 84 | } |
| 85 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 86 | if (stream->avail_out == 0) { |
| 87 | stream->next_out = squashfs_next_page(output); |
| 88 | if (stream->next_out != NULL) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 89 | stream->avail_out = PAGE_SIZE; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (!zlib_init) { |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 93 | zlib_err = zlib_inflateInit(stream); |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 94 | if (zlib_err != Z_OK) { |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 95 | error = -EIO; |
| 96 | break; |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 97 | } |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 98 | zlib_init = 1; |
| 99 | } |
| 100 | |
Phillip Lougher | f1a4035 | 2009-09-23 19:04:49 +0100 | [diff] [blame] | 101 | zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 102 | if (zlib_err == Z_STREAM_END) |
| 103 | break; |
| 104 | if (zlib_err != Z_OK) { |
| 105 | error = -EIO; |
| 106 | break; |
| 107 | } |
| 108 | } |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 109 | |
Phillip Lougher | 846b730 | 2013-11-18 02:59:12 +0000 | [diff] [blame] | 110 | squashfs_finish_page(output); |
| 111 | |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 112 | if (!error) |
| 113 | if (zlib_inflateEnd(stream) != Z_OK) |
| 114 | error = -EIO; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 115 | |
Philippe Liard | 93e72b3 | 2020-06-01 21:45:23 -0700 | [diff] [blame] | 116 | return error ? error : stream->total_out; |
Phillip Lougher | e6a6d37 | 2009-09-22 19:25:24 +0100 | [diff] [blame] | 117 | } |
Phillip Lougher | 4c0f0bb | 2009-10-06 04:04:15 +0100 | [diff] [blame] | 118 | |
| 119 | const struct squashfs_decompressor squashfs_zlib_comp_ops = { |
| 120 | .init = zlib_init, |
| 121 | .free = zlib_free, |
| 122 | .decompress = zlib_uncompress, |
| 123 | .id = ZLIB_COMPRESSION, |
| 124 | .name = "zlib", |
| 125 | .supported = 1 |
| 126 | }; |
| 127 | |