blob: 4e7a138745ec5d06a1d77ffbc79f76d7e3d51b2d [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
Jim Keniston565d76c2011-03-22 16:35:12 -070048 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
50 if (!inf_strm.workspace) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 vfree(def_strm.workspace);
52 return -ENOMEM;
53 }
54 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
55 return 0;
56}
57
58static void free_workspaces(void)
59{
60 vfree(def_strm.workspace);
61 vfree(inf_strm.workspace);
62}
63#else
64#define alloc_workspaces() (0)
65#define free_workspaces() do { } while(0)
66#endif /* __KERNEL__ */
67
Adrian Bunk75c96f82005-05-05 16:16:09 -070068static int jffs2_zlib_compress(unsigned char *data_in,
69 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -040070 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 int ret;
73
74 if (*dstlen <= STREAM_END_SPACE)
75 return -1;
76
Ingo Molnar353ab6e2006-03-26 01:37:12 -080077 mutex_lock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
80 printk(KERN_WARNING "deflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -080081 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -1;
83 }
84
85 def_strm.next_in = data_in;
86 def_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 def_strm.next_out = cpage_out;
89 def_strm.total_out = 0;
90
91 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
92 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
93 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
94 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
95 def_strm.avail_in, def_strm.avail_out));
96 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000097 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
99 if (ret != Z_OK) {
100 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
101 zlib_deflateEnd(&def_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800102 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -1;
104 }
105 }
106 def_strm.avail_out += STREAM_END_SPACE;
107 def_strm.avail_in = 0;
108 ret = zlib_deflate(&def_strm, Z_FINISH);
109 zlib_deflateEnd(&def_strm);
110
111 if (ret != Z_STREAM_END) {
112 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
113 ret = -1;
114 goto out;
115 }
116
117 if (def_strm.total_out >= def_strm.total_in) {
118 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
119 def_strm.total_in, def_strm.total_out));
120 ret = -1;
121 goto out;
122 }
123
124 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
125 def_strm.total_in, def_strm.total_out));
126
127 *dstlen = def_strm.total_out;
128 *sourcelen = def_strm.total_in;
129 ret = 0;
130 out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800131 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return ret;
133}
134
Adrian Bunk75c96f82005-05-05 16:16:09 -0700135static int jffs2_zlib_decompress(unsigned char *data_in,
136 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -0400137 uint32_t srclen, uint32_t destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int ret;
140 int wbits = MAX_WBITS;
141
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800142 mutex_lock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 inf_strm.next_in = data_in;
145 inf_strm.avail_in = srclen;
146 inf_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 inf_strm.next_out = cpage_out;
149 inf_strm.avail_out = destlen;
150 inf_strm.total_out = 0;
151
152 /* If it's deflate, and it's got no preset dictionary, then
153 we can tell zlib to skip the adler32 check. */
154 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
155 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
156 !(((data_in[0]<<8) + data_in[1]) % 31)) {
157
158 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
159 wbits = -((data_in[0] >> 4) + 8);
160 inf_strm.next_in += 2;
161 inf_strm.avail_in -= 2;
162 } else {
163 /* Let this remain D1 for now -- it should never happen */
164 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
165 }
166
167
168 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
169 printk(KERN_WARNING "inflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800170 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 1;
172 }
173
174 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
175 ;
176 if (ret != Z_STREAM_END) {
177 printk(KERN_NOTICE "inflate returned %d\n", ret);
178 }
179 zlib_inflateEnd(&inf_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800180 mutex_unlock(&inflate_mutex);
David Woodhouseef53cb02007-07-10 10:01:22 +0100181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static struct jffs2_compressor jffs2_zlib_comp = {
185 .priority = JFFS2_ZLIB_PRIORITY,
186 .name = "zlib",
187 .compr = JFFS2_COMPR_ZLIB,
188 .compress = &jffs2_zlib_compress,
189 .decompress = &jffs2_zlib_decompress,
190#ifdef JFFS2_ZLIB_DISABLED
191 .disabled = 1,
192#else
193 .disabled = 0,
194#endif
195};
196
197int __init jffs2_zlib_init(void)
198{
199 int ret;
200
201 ret = alloc_workspaces();
202 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100203 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 ret = jffs2_register_compressor(&jffs2_zlib_comp);
206 if (ret)
David Woodhouseef53cb02007-07-10 10:01:22 +0100207 free_workspaces();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return ret;
210}
211
212void jffs2_zlib_exit(void)
213{
214 jffs2_unregister_compressor(&jffs2_zlib_comp);
215 free_workspaces();
216}