1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6 * Phillip Lougher <phillip@squashfs.org.uk>
12 #include <linux/mutex.h>
13 #include <linux/bio.h>
14 #include <linux/slab.h>
15 #include <linux/zlib.h>
16 #include <linux/vmalloc.h>
18 #include "squashfs_fs.h"
19 #include "squashfs_fs_sb.h"
21 #include "decompressor.h"
22 #include "page_actor.h"
24 static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
26 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
29 stream->workspace = vmalloc(zlib_inflate_workspacesize());
30 if (stream->workspace == NULL)
36 ERROR("Failed to allocate zlib workspace\n");
38 return ERR_PTR(-ENOMEM);
42 static void zlib_free(void *strm)
44 z_stream *stream = strm;
47 vfree(stream->workspace);
52 static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
53 struct bio *bio, int offset, int length,
54 struct squashfs_page_actor *output)
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;
59 z_stream *stream = strm;
61 stream->avail_out = PAGE_SIZE;
62 stream->next_out = squashfs_first_page(output);
65 if (IS_ERR(stream->next_out)) {
66 error = PTR_ERR(stream->next_out);
73 if (stream->avail_in == 0) {
77 if (!bio_next_segment(bio, &iter_all)) {
78 /* Z_STREAM_END must be reached. */
83 avail = min(length, ((int)bvec->bv_len) - offset);
84 data = bvec_virt(bvec);
86 stream->next_in = data + offset;
87 stream->avail_in = avail;
91 if (stream->avail_out == 0) {
92 stream->next_out = squashfs_next_page(output);
93 if (IS_ERR(stream->next_out)) {
94 error = PTR_ERR(stream->next_out);
96 } else if (stream->next_out != NULL)
97 stream->avail_out = PAGE_SIZE;
101 zlib_err = zlib_inflateInit(stream);
102 if (zlib_err != Z_OK) {
109 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
110 if (zlib_err == Z_STREAM_END)
112 if (zlib_err != Z_OK) {
119 squashfs_finish_page(output);
122 if (zlib_inflateEnd(stream) != Z_OK)
125 return error ? error : stream->total_out;
128 const struct squashfs_decompressor squashfs_zlib_comp_ops = {
131 .decompress = zlib_uncompress,
132 .id = ZLIB_COMPRESSION,