blob: a20e9042146bd6f7424104ba66a26b6c8cbde9c0 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Loughere6a6d372009-09-22 19:25:24 +01002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Loughere6a6d372009-09-22 19:25:24 +01007 *
Phillip Loughere6a6d372009-09-22 19:25:24 +01008 * zlib_wrapper.c
9 */
10
11
12#include <linux/mutex.h>
Philippe Liard93e72b32020-06-01 21:45:23 -070013#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010015#include <linux/zlib.h>
Phillip Lougher117a91e2011-03-22 23:01:26 +000016#include <linux/vmalloc.h>
Phillip Loughere6a6d372009-09-22 19:25:24 +010017
18#include "squashfs_fs.h"
19#include "squashfs_fs_sb.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010020#include "squashfs.h"
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010021#include "decompressor.h"
Phillip Lougher846b7302013-11-18 02:59:12 +000022#include "page_actor.h"
Phillip Loughere6a6d372009-09-22 19:25:24 +010023
Phillip Lougher9508c6b2013-11-13 02:56:26 +000024static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
Phillip Lougherf1a40352009-09-23 19:04:49 +010025{
26 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
27 if (stream == NULL)
28 goto failed;
Phillip Lougher117a91e2011-03-22 23:01:26 +000029 stream->workspace = vmalloc(zlib_inflate_workspacesize());
Phillip Lougherf1a40352009-09-23 19:04:49 +010030 if (stream->workspace == NULL)
31 goto failed;
32
33 return stream;
34
35failed:
36 ERROR("Failed to allocate zlib workspace\n");
37 kfree(stream);
Phillip Lougherb7fc0ff2011-02-28 01:45:42 +000038 return ERR_PTR(-ENOMEM);
Phillip Lougherf1a40352009-09-23 19:04:49 +010039}
40
41
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +010042static void zlib_free(void *strm)
Phillip Lougherf1a40352009-09-23 19:04:49 +010043{
44 z_stream *stream = strm;
45
46 if (stream)
Phillip Lougher117a91e2011-03-22 23:01:26 +000047 vfree(stream->workspace);
Phillip Lougherf1a40352009-09-23 19:04:49 +010048 kfree(stream);
49}
50
51
Phillip Lougher9508c6b2013-11-13 02:56:26 +000052static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
Philippe Liard93e72b32020-06-01 21:45:23 -070053 struct bio *bio, int offset, int length,
Phillip Lougher846b7302013-11-18 02:59:12 +000054 struct squashfs_page_actor *output)
Phillip Loughere6a6d372009-09-22 19:25:24 +010055{
Philippe Liard93e72b32020-06-01 21:45:23 -070056 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 Lougher9508c6b2013-11-13 02:56:26 +000059 z_stream *stream = strm;
Phillip Loughere6a6d372009-09-22 19:25:24 +010060
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030061 stream->avail_out = PAGE_SIZE;
Phillip Lougher846b7302013-11-18 02:59:12 +000062 stream->next_out = squashfs_first_page(output);
Phillip Lougherf1a40352009-09-23 19:04:49 +010063 stream->avail_in = 0;
Phillip Loughere6a6d372009-09-22 19:25:24 +010064
Philippe Liard93e72b32020-06-01 21:45:23 -070065 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 Hellwigfbc272412021-08-04 11:56:25 +020079 data = bvec_virt(bvec);
Phillip Lougher170cf022011-01-05 17:52:26 +000080 length -= avail;
Philippe Liard93e72b32020-06-01 21:45:23 -070081 stream->next_in = data + offset;
Phillip Lougherf1a40352009-09-23 19:04:49 +010082 stream->avail_in = avail;
Phillip Loughere6a6d372009-09-22 19:25:24 +010083 offset = 0;
84 }
85
Phillip Lougher846b7302013-11-18 02:59:12 +000086 if (stream->avail_out == 0) {
87 stream->next_out = squashfs_next_page(output);
88 if (stream->next_out != NULL)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030089 stream->avail_out = PAGE_SIZE;
Phillip Loughere6a6d372009-09-22 19:25:24 +010090 }
91
92 if (!zlib_init) {
Phillip Lougherf1a40352009-09-23 19:04:49 +010093 zlib_err = zlib_inflateInit(stream);
Phillip Lougher846b7302013-11-18 02:59:12 +000094 if (zlib_err != Z_OK) {
Philippe Liard93e72b32020-06-01 21:45:23 -070095 error = -EIO;
96 break;
Phillip Lougher846b7302013-11-18 02:59:12 +000097 }
Phillip Loughere6a6d372009-09-22 19:25:24 +010098 zlib_init = 1;
99 }
100
Phillip Lougherf1a40352009-09-23 19:04:49 +0100101 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Philippe Liard93e72b32020-06-01 21:45:23 -0700102 if (zlib_err == Z_STREAM_END)
103 break;
104 if (zlib_err != Z_OK) {
105 error = -EIO;
106 break;
107 }
108 }
Phillip Loughere6a6d372009-09-22 19:25:24 +0100109
Phillip Lougher846b7302013-11-18 02:59:12 +0000110 squashfs_finish_page(output);
111
Philippe Liard93e72b32020-06-01 21:45:23 -0700112 if (!error)
113 if (zlib_inflateEnd(stream) != Z_OK)
114 error = -EIO;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100115
Philippe Liard93e72b32020-06-01 21:45:23 -0700116 return error ? error : stream->total_out;
Phillip Loughere6a6d372009-09-22 19:25:24 +0100117}
Phillip Lougher4c0f0bb2009-10-06 04:04:15 +0100118
119const 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