blob: 88529e4924c69408c9c1f52e0acee529cd523d5a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/zlib.h>
19#include <linux/zutil.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "nodelist.h"
21#include "compr.h"
22
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000023 /* Plan: call deflate() with avail_in == *sourcelen,
24 avail_out = *dstlen - 12 and flush == Z_FINISH.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 If it doesn't manage to finish, call it again with
26 avail_in == 0 and avail_out set to the remaining 12
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000027 bytes for it to clean up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 Q: Is 12 bytes sufficient?
29 */
30#define STREAM_END_SPACE 12
31
Ingo Molnar353ab6e2006-03-26 01:37:12 -080032static DEFINE_MUTEX(deflate_mutex);
33static DEFINE_MUTEX(inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static z_stream inf_strm, def_strm;
35
36#ifdef __KERNEL__ /* Linux-only */
37#include <linux/vmalloc.h>
38#include <linux/init.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static int __init alloc_workspaces(void)
42{
Jim Keniston565d76c2011-03-22 16:35:12 -070043 def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
44 MAX_MEM_LEVEL));
Joe Perches045ead32012-01-31 14:42:08 -080045 if (!def_strm.workspace)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -ENOMEM;
Joe Perches045ead32012-01-31 14:42:08 -080047
Joe Perches9c261b32012-02-15 15:56:43 -080048 jffs2_dbg(1, "Allocated %d bytes for deflate workspace\n",
49 zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
51 if (!inf_strm.workspace) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 vfree(def_strm.workspace);
53 return -ENOMEM;
54 }
Joe Perches9c261b32012-02-15 15:56:43 -080055 jffs2_dbg(1, "Allocated %d bytes for inflate workspace\n",
56 zlib_inflate_workspacesize());
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return 0;
58}
59
60static void free_workspaces(void)
61{
62 vfree(def_strm.workspace);
63 vfree(inf_strm.workspace);
64}
65#else
66#define alloc_workspaces() (0)
67#define free_workspaces() do { } while(0)
68#endif /* __KERNEL__ */
69
Adrian Bunk75c96f82005-05-05 16:16:09 -070070static int jffs2_zlib_compress(unsigned char *data_in,
71 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -040072 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 int ret;
75
76 if (*dstlen <= STREAM_END_SPACE)
77 return -1;
78
Ingo Molnar353ab6e2006-03-26 01:37:12 -080079 mutex_lock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
Joe Perchesda320f02012-02-15 15:56:44 -080082 pr_warn("deflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -080083 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -1;
85 }
86
87 def_strm.next_in = data_in;
88 def_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 def_strm.next_out = cpage_out;
91 def_strm.total_out = 0;
92
93 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
94 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
95 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
Joe Perches9c261b32012-02-15 15:56:43 -080096 jffs2_dbg(1, "calling deflate with avail_in %d, avail_out %d\n",
97 def_strm.avail_in, def_strm.avail_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
Joe Perches9c261b32012-02-15 15:56:43 -080099 jffs2_dbg(1, "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
100 def_strm.avail_in, def_strm.avail_out,
101 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (ret != Z_OK) {
Joe Perches9c261b32012-02-15 15:56:43 -0800103 jffs2_dbg(1, "deflate in loop returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 zlib_deflateEnd(&def_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800105 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -1;
107 }
108 }
109 def_strm.avail_out += STREAM_END_SPACE;
110 def_strm.avail_in = 0;
111 ret = zlib_deflate(&def_strm, Z_FINISH);
112 zlib_deflateEnd(&def_strm);
113
114 if (ret != Z_STREAM_END) {
Joe Perches9c261b32012-02-15 15:56:43 -0800115 jffs2_dbg(1, "final deflate returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ret = -1;
117 goto out;
118 }
119
120 if (def_strm.total_out >= def_strm.total_in) {
Joe Perches9c261b32012-02-15 15:56:43 -0800121 jffs2_dbg(1, "zlib compressed %ld bytes into %ld; failing\n",
122 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ret = -1;
124 goto out;
125 }
126
Joe Perches9c261b32012-02-15 15:56:43 -0800127 jffs2_dbg(1, "zlib compressed %ld bytes into %ld\n",
128 def_strm.total_in, def_strm.total_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 *dstlen = def_strm.total_out;
131 *sourcelen = def_strm.total_in;
132 ret = 0;
133 out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800134 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return ret;
136}
137
Adrian Bunk75c96f82005-05-05 16:16:09 -0700138static int jffs2_zlib_decompress(unsigned char *data_in,
139 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400140 uint32_t srclen, uint32_t destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int ret;
143 int wbits = MAX_WBITS;
144
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800145 mutex_lock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 inf_strm.next_in = data_in;
148 inf_strm.avail_in = srclen;
149 inf_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 inf_strm.next_out = cpage_out;
152 inf_strm.avail_out = destlen;
153 inf_strm.total_out = 0;
154
155 /* If it's deflate, and it's got no preset dictionary, then
156 we can tell zlib to skip the adler32 check. */
157 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
158 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
159 !(((data_in[0]<<8) + data_in[1]) % 31)) {
160
Joe Perches9c261b32012-02-15 15:56:43 -0800161 jffs2_dbg(2, "inflate skipping adler32\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 wbits = -((data_in[0] >> 4) + 8);
163 inf_strm.next_in += 2;
164 inf_strm.avail_in -= 2;
165 } else {
166 /* Let this remain D1 for now -- it should never happen */
Joe Perches9c261b32012-02-15 15:56:43 -0800167 jffs2_dbg(1, "inflate not skipping adler32\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
170
171 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800172 pr_warn("inflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800173 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 1;
175 }
176
177 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
178 ;
179 if (ret != Z_STREAM_END) {
Joe Perchesda320f02012-02-15 15:56:44 -0800180 pr_notice("inflate returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 zlib_inflateEnd(&inf_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800183 mutex_unlock(&inflate_mutex);
David Woodhouseef53cb02007-07-10 10:01:22 +0100184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static struct jffs2_compressor jffs2_zlib_comp = {
188 .priority = JFFS2_ZLIB_PRIORITY,
189 .name = "zlib",
190 .compr = JFFS2_COMPR_ZLIB,
191 .compress = &jffs2_zlib_compress,
192 .decompress = &jffs2_zlib_decompress,
193#ifdef JFFS2_ZLIB_DISABLED
194 .disabled = 1,
195#else
196 .disabled = 0,
197#endif
198};
199
200int __init jffs2_zlib_init(void)
201{
202 int ret;
203
204 ret = alloc_workspaces();
205 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 ret = jffs2_register_compressor(&jffs2_zlib_comp);
209 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100210 free_workspaces();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 return ret;
213}
214
215void jffs2_zlib_exit(void)
216{
217 jffs2_unregister_compressor(&jffs2_zlib_comp);
218 free_workspaces();
219}