blob: 0c1fc6e20b4321af24ecb92a22b4cdce926caaa6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: compr_zlib.c,v 1.32 2005/11/07 11:14:38 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
14#if !defined(__KERNEL__) && !defined(__ECOS)
15#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
16#endif
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/slab.h>
20#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{
45 def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
46 if (!def_strm.workspace) {
47 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
48 return -ENOMEM;
49 }
50 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
51 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
52 if (!inf_strm.workspace) {
53 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
54 vfree(def_strm.workspace);
55 return -ENOMEM;
56 }
57 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
58 return 0;
59}
60
61static void free_workspaces(void)
62{
63 vfree(def_strm.workspace);
64 vfree(inf_strm.workspace);
65}
66#else
67#define alloc_workspaces() (0)
68#define free_workspaces() do { } while(0)
69#endif /* __KERNEL__ */
70
Adrian Bunk75c96f82005-05-05 16:16:09 -070071static int jffs2_zlib_compress(unsigned char *data_in,
72 unsigned char *cpage_out,
73 uint32_t *sourcelen, uint32_t *dstlen,
74 void *model)
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)) {
84 printk(KERN_WARNING "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);
97 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
98 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
99 def_strm.avail_in, def_strm.avail_out));
100 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000101 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 -0700102 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
103 if (ret != Z_OK) {
104 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
105 zlib_deflateEnd(&def_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800106 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -1;
108 }
109 }
110 def_strm.avail_out += STREAM_END_SPACE;
111 def_strm.avail_in = 0;
112 ret = zlib_deflate(&def_strm, Z_FINISH);
113 zlib_deflateEnd(&def_strm);
114
115 if (ret != Z_STREAM_END) {
116 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
117 ret = -1;
118 goto out;
119 }
120
121 if (def_strm.total_out >= def_strm.total_in) {
122 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
123 def_strm.total_in, def_strm.total_out));
124 ret = -1;
125 goto out;
126 }
127
128 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
129 def_strm.total_in, def_strm.total_out));
130
131 *dstlen = def_strm.total_out;
132 *sourcelen = def_strm.total_in;
133 ret = 0;
134 out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800135 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return ret;
137}
138
Adrian Bunk75c96f82005-05-05 16:16:09 -0700139static int jffs2_zlib_decompress(unsigned char *data_in,
140 unsigned char *cpage_out,
141 uint32_t srclen, uint32_t destlen,
142 void *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 int ret;
145 int wbits = MAX_WBITS;
146
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800147 mutex_lock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 inf_strm.next_in = data_in;
150 inf_strm.avail_in = srclen;
151 inf_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 inf_strm.next_out = cpage_out;
154 inf_strm.avail_out = destlen;
155 inf_strm.total_out = 0;
156
157 /* If it's deflate, and it's got no preset dictionary, then
158 we can tell zlib to skip the adler32 check. */
159 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
160 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
161 !(((data_in[0]<<8) + data_in[1]) % 31)) {
162
163 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
164 wbits = -((data_in[0] >> 4) + 8);
165 inf_strm.next_in += 2;
166 inf_strm.avail_in -= 2;
167 } else {
168 /* Let this remain D1 for now -- it should never happen */
169 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
170 }
171
172
173 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
174 printk(KERN_WARNING "inflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800175 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 1;
177 }
178
179 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
180 ;
181 if (ret != Z_STREAM_END) {
182 printk(KERN_NOTICE "inflate returned %d\n", ret);
183 }
184 zlib_inflateEnd(&inf_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800185 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
189static struct jffs2_compressor jffs2_zlib_comp = {
190 .priority = JFFS2_ZLIB_PRIORITY,
191 .name = "zlib",
192 .compr = JFFS2_COMPR_ZLIB,
193 .compress = &jffs2_zlib_compress,
194 .decompress = &jffs2_zlib_decompress,
195#ifdef JFFS2_ZLIB_DISABLED
196 .disabled = 1,
197#else
198 .disabled = 0,
199#endif
200};
201
202int __init jffs2_zlib_init(void)
203{
204 int ret;
205
206 ret = alloc_workspaces();
207 if (ret)
208 return ret;
209
210 ret = jffs2_register_compressor(&jffs2_zlib_comp);
211 if (ret)
212 free_workspaces();
213
214 return ret;
215}
216
217void jffs2_zlib_exit(void)
218{
219 jffs2_unregister_compressor(&jffs2_zlib_comp);
220 free_workspaces();
221}