]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/squashfs/zlib_wrapper.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / squashfs / zlib_wrapper.c
CommitLineData
e6a6d379
PL
1/*
2 * Squashfs - a compressed read only filesystem for Linux
3 *
4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
d7f2ff67 5 * Phillip Lougher <phillip@squashfs.org.uk>
e6a6d379
PL
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * zlib_wrapper.c
22 */
23
24
25#include <linux/mutex.h>
26#include <linux/buffer_head.h>
5a0e3ad6 27#include <linux/slab.h>
e6a6d379 28#include <linux/zlib.h>
117a91e0 29#include <linux/vmalloc.h>
e6a6d379
PL
30
31#include "squashfs_fs.h"
32#include "squashfs_fs_sb.h"
e6a6d379 33#include "squashfs.h"
4c0f0bb2 34#include "decompressor.h"
846b730e 35#include "page_actor.h"
e6a6d379 36
9508c6b9 37static void *zlib_init(struct squashfs_sb_info *dummy, void *buff)
f1a40359
PL
38{
39 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL);
40 if (stream == NULL)
41 goto failed;
117a91e0 42 stream->workspace = vmalloc(zlib_inflate_workspacesize());
f1a40359
PL
43 if (stream->workspace == NULL)
44 goto failed;
45
46 return stream;
47
48failed:
49 ERROR("Failed to allocate zlib workspace\n");
50 kfree(stream);
b7fc0ff0 51 return ERR_PTR(-ENOMEM);
f1a40359
PL
52}
53
54
4c0f0bb2 55static void zlib_free(void *strm)
f1a40359
PL
56{
57 z_stream *stream = strm;
58
59 if (stream)
117a91e0 60 vfree(stream->workspace);
f1a40359
PL
61 kfree(stream);
62}
63
64
9508c6b9 65static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
846b730e
PL
66 struct buffer_head **bh, int b, int offset, int length,
67 struct squashfs_page_actor *output)
e6a6d379 68{
846b730e 69 int zlib_err, zlib_init = 0, k = 0;
9508c6b9 70 z_stream *stream = strm;
e6a6d379 71
09cbfeaf 72 stream->avail_out = PAGE_SIZE;
846b730e 73 stream->next_out = squashfs_first_page(output);
f1a40359 74 stream->avail_in = 0;
e6a6d379 75
e6a6d379 76 do {
f1a40359 77 if (stream->avail_in == 0 && k < b) {
170cf021
PL
78 int avail = min(length, msblk->devblksize - offset);
79 length -= avail;
f1a40359
PL
80 stream->next_in = bh[k]->b_data + offset;
81 stream->avail_in = avail;
e6a6d379
PL
82 offset = 0;
83 }
84
846b730e
PL
85 if (stream->avail_out == 0) {
86 stream->next_out = squashfs_next_page(output);
87 if (stream->next_out != NULL)
09cbfeaf 88 stream->avail_out = PAGE_SIZE;
e6a6d379
PL
89 }
90
91 if (!zlib_init) {
f1a40359 92 zlib_err = zlib_inflateInit(stream);
846b730e
PL
93 if (zlib_err != Z_OK) {
94 squashfs_finish_page(output);
9508c6b9 95 goto out;
846b730e 96 }
e6a6d379
PL
97 zlib_init = 1;
98 }
99
f1a40359 100 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);
e6a6d379 101
f1a40359 102 if (stream->avail_in == 0 && k < b)
e6a6d379
PL
103 put_bh(bh[k++]);
104 } while (zlib_err == Z_OK);
105
846b730e
PL
106 squashfs_finish_page(output);
107
9508c6b9
PL
108 if (zlib_err != Z_STREAM_END)
109 goto out;
e6a6d379 110
f1a40359 111 zlib_err = zlib_inflateEnd(stream);
9508c6b9
PL
112 if (zlib_err != Z_OK)
113 goto out;
e7ee11f0 114
9508c6b9
PL
115 if (k < b)
116 goto out;
e6a6d379 117
9508c6b9 118 return stream->total_out;
e6a6d379 119
9508c6b9 120out:
e6a6d379
PL
121 for (; k < b; k++)
122 put_bh(bh[k]);
123
124 return -EIO;
125}
4c0f0bb2
PL
126
127const struct squashfs_decompressor squashfs_zlib_comp_ops = {
128 .init = zlib_init,
129 .free = zlib_free,
130 .decompress = zlib_uncompress,
131 .id = ZLIB_COMPRESSION,
132 .name = "zlib",
133 .supported = 1
134};
135