blob: bcb881ec47f226e1147b961e722543ef1ae17bbf [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6 * Phillip Lougher <phillip@squashfs.org.uk>
7 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 * zlib_wrapper.c
9 */
10
11
12#include <linux/mutex.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/bio.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/slab.h>
15#include <linux/zlib.h>
16#include <linux/vmalloc.h>
17
18#include "squashfs_fs.h"
19#include "squashfs_fs_sb.h"
20#include "squashfs.h"
21#include "decompressor.h"
22#include "page_actor.h"
23
24static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
25{
26 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
27 if (stream == NULL)
28 goto failed;
29 stream->workspace = vmalloc(zlib_inflate_workspacesize());
30 if (stream->workspace == NULL)
31 goto failed;
32
33 return stream;
34
35failed:
36 ERROR("Failed to allocate zlib workspace\n");
37 kfree(stream);
38 return ERR_PTR(-ENOMEM);
39}
40
41
42static void zlib_free(void *strm)
43{
44 z_stream *stream = strm;
45
46 if (stream)
47 vfree(stream->workspace);
48 kfree(stream);
49}
50
51
52static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
Olivier Deprez157378f2022-04-04 15:47:50 +020053 struct bio *bio, int offset, int length,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 struct squashfs_page_actor *output)
55{
Olivier Deprez157378f2022-04-04 15:47:50 +020056 struct bvec_iter_all iter_all = {};
57 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
58 int zlib_init = 0, error = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 z_stream *stream = strm;
60
61 stream->avail_out = PAGE_SIZE;
62 stream->next_out = squashfs_first_page(output);
63 stream->avail_in = 0;
64
Olivier Deprez157378f2022-04-04 15:47:50 +020065 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);
79 data = page_address(bvec->bv_page) + bvec->bv_offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 length -= avail;
Olivier Deprez157378f2022-04-04 15:47:50 +020081 stream->next_in = data + offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082 stream->avail_in = avail;
83 offset = 0;
84 }
85
86 if (stream->avail_out == 0) {
87 stream->next_out = squashfs_next_page(output);
88 if (stream->next_out != NULL)
89 stream->avail_out = PAGE_SIZE;
90 }
91
92 if (!zlib_init) {
93 zlib_err = zlib_inflateInit(stream);
94 if (zlib_err != Z_OK) {
Olivier Deprez157378f2022-04-04 15:47:50 +020095 error = -EIO;
96 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 }
98 zlib_init = 1;
99 }
100
101 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
Olivier Deprez157378f2022-04-04 15:47:50 +0200102 if (zlib_err == Z_STREAM_END)
103 break;
104 if (zlib_err != Z_OK) {
105 error = -EIO;
106 break;
107 }
108 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 squashfs_finish_page(output);
111
Olivier Deprez157378f2022-04-04 15:47:50 +0200112 if (!error)
113 if (zlib_inflateEnd(stream) != Z_OK)
114 error = -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
Olivier Deprez157378f2022-04-04 15:47:50 +0200116 return error ? error : stream->total_out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117}
118
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