blob: 5698dae5d92dd5f7c3b8db3674348ae80fea45f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by David Woodhouse <dwmw2@infradead.org>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#if !defined(__KERNEL__) && !defined(__ECOS)
14#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
15#endif
16
Joe Perches5a528952012-02-15 15:56:45 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/zlib.h>
21#include <linux/zutil.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "nodelist.h"
23#include "compr.h"
24
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000025 /* Plan: call deflate() with avail_in == *sourcelen,
26 avail_out = *dstlen - 12 and flush == Z_FINISH.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 If it doesn't manage to finish, call it again with
28 avail_in == 0 and avail_out set to the remaining 12
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000029 bytes for it to clean up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 Q: Is 12 bytes sufficient?
31 */
32#define STREAM_END_SPACE 12
33
Ingo Molnar353ab6e2006-03-26 01:37:12 -080034static DEFINE_MUTEX(deflate_mutex);
35static DEFINE_MUTEX(inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static z_stream inf_strm, def_strm;
37
38#ifdef __KERNEL__ /* Linux-only */
39#include <linux/vmalloc.h>
40#include <linux/init.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080041#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static int __init alloc_workspaces(void)
44{
Jim Keniston565d76c2011-03-22 16:35:12 -070045 def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
46 MAX_MEM_LEVEL));
Joe Perches045ead32012-01-31 14:42:08 -080047 if (!def_strm.workspace)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return -ENOMEM;
Joe Perches045ead32012-01-31 14:42:08 -080049
Joe Perches9c261b32012-02-15 15:56:43 -080050 jffs2_dbg(1, "Allocated %d bytes for deflate workspace\n",
51 zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
53 if (!inf_strm.workspace) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 vfree(def_strm.workspace);
55 return -ENOMEM;
56 }
Joe Perches9c261b32012-02-15 15:56:43 -080057 jffs2_dbg(1, "Allocated %d bytes for inflate workspace\n",
58 zlib_inflate_workspacesize());
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
60}
61
62static void free_workspaces(void)
63{
64 vfree(def_strm.workspace);
65 vfree(inf_strm.workspace);
66}
67#else
68#define alloc_workspaces() (0)
69#define free_workspaces() do { } while(0)
70#endif /* __KERNEL__ */
71
Adrian Bunk75c96f82005-05-05 16:16:09 -070072static int jffs2_zlib_compress(unsigned char *data_in,
73 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -040074 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int ret;
77
78 if (*dstlen <= STREAM_END_SPACE)
79 return -1;
80
Ingo Molnar353ab6e2006-03-26 01:37:12 -080081 mutex_lock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
Joe Perchesda320f02012-02-15 15:56:44 -080084 pr_warn("deflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -080085 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -1;
87 }
88
89 def_strm.next_in = data_in;
90 def_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 def_strm.next_out = cpage_out;
93 def_strm.total_out = 0;
94
95 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
96 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
Yinghai Lud97b07c2014-08-08 14:23:14 -070097 def_strm.avail_in = min_t(unsigned long,
98 (*sourcelen-def_strm.total_in), def_strm.avail_out);
99 jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld\n",
Joe Perches9c261b32012-02-15 15:56:43 -0800100 def_strm.avail_in, def_strm.avail_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
Yinghai Lud97b07c2014-08-08 14:23:14 -0700102 jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld\n",
Joe Perches9c261b32012-02-15 15:56:43 -0800103 def_strm.avail_in, def_strm.avail_out,
104 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (ret != Z_OK) {
Joe Perches9c261b32012-02-15 15:56:43 -0800106 jffs2_dbg(1, "deflate in loop returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 zlib_deflateEnd(&def_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800108 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -1;
110 }
111 }
112 def_strm.avail_out += STREAM_END_SPACE;
113 def_strm.avail_in = 0;
114 ret = zlib_deflate(&def_strm, Z_FINISH);
115 zlib_deflateEnd(&def_strm);
116
117 if (ret != Z_STREAM_END) {
Joe Perches9c261b32012-02-15 15:56:43 -0800118 jffs2_dbg(1, "final deflate returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ret = -1;
120 goto out;
121 }
122
123 if (def_strm.total_out >= def_strm.total_in) {
Joe Perches9c261b32012-02-15 15:56:43 -0800124 jffs2_dbg(1, "zlib compressed %ld bytes into %ld; failing\n",
125 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ret = -1;
127 goto out;
128 }
129
Joe Perches9c261b32012-02-15 15:56:43 -0800130 jffs2_dbg(1, "zlib compressed %ld bytes into %ld\n",
131 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 *dstlen = def_strm.total_out;
134 *sourcelen = def_strm.total_in;
135 ret = 0;
136 out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800137 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return ret;
139}
140
Adrian Bunk75c96f82005-05-05 16:16:09 -0700141static int jffs2_zlib_decompress(unsigned char *data_in,
142 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400143 uint32_t srclen, uint32_t destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int ret;
146 int wbits = MAX_WBITS;
147
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800148 mutex_lock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 inf_strm.next_in = data_in;
151 inf_strm.avail_in = srclen;
152 inf_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 inf_strm.next_out = cpage_out;
155 inf_strm.avail_out = destlen;
156 inf_strm.total_out = 0;
157
158 /* If it's deflate, and it's got no preset dictionary, then
159 we can tell zlib to skip the adler32 check. */
160 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
161 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
162 !(((data_in[0]<<8) + data_in[1]) % 31)) {
163
Joe Perches9c261b32012-02-15 15:56:43 -0800164 jffs2_dbg(2, "inflate skipping adler32\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 wbits = -((data_in[0] >> 4) + 8);
166 inf_strm.next_in += 2;
167 inf_strm.avail_in -= 2;
168 } else {
169 /* Let this remain D1 for now -- it should never happen */
Joe Perches9c261b32012-02-15 15:56:43 -0800170 jffs2_dbg(1, "inflate not skipping adler32\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
173
174 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800175 pr_warn("inflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800176 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 1;
178 }
179
180 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
181 ;
182 if (ret != Z_STREAM_END) {
Joe Perchesda320f02012-02-15 15:56:44 -0800183 pr_notice("inflate returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 zlib_inflateEnd(&inf_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800186 mutex_unlock(&inflate_mutex);
David Woodhouseef53cb02007-07-10 10:01:22 +0100187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190static struct jffs2_compressor jffs2_zlib_comp = {
191 .priority = JFFS2_ZLIB_PRIORITY,
192 .name = "zlib",
193 .compr = JFFS2_COMPR_ZLIB,
194 .compress = &jffs2_zlib_compress,
195 .decompress = &jffs2_zlib_decompress,
196#ifdef JFFS2_ZLIB_DISABLED
197 .disabled = 1,
198#else
199 .disabled = 0,
200#endif
201};
202
203int __init jffs2_zlib_init(void)
204{
205 int ret;
206
207 ret = alloc_workspaces();
208 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 ret = jffs2_register_compressor(&jffs2_zlib_comp);
212 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100213 free_workspaces();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 return ret;
216}
217
218void jffs2_zlib_exit(void)
219{
220 jffs2_unregister_compressor(&jffs2_zlib_comp);
221 free_workspaces();
222}