2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: compr_zlib.c,v 1.32 2005/11/07 11:14:38 gleixner Exp $
14 #if !defined(__KERNEL__) && !defined(__ECOS)
15 #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/zlib.h>
23 #include <linux/zutil.h>
27 /* Plan: call deflate() with avail_in == *sourcelen,
28 avail_out = *dstlen - 12 and flush == Z_FINISH.
29 If it doesn't manage to finish, call it again with
30 avail_in == 0 and avail_out set to the remaining 12
31 bytes for it to clean up.
32 Q: Is 12 bytes sufficient?
34 #define STREAM_END_SPACE 12
36 static DEFINE_MUTEX(deflate_mutex);
37 static DEFINE_MUTEX(inflate_mutex);
38 static z_stream inf_strm, def_strm;
40 #ifdef __KERNEL__ /* Linux-only */
41 #include <linux/vmalloc.h>
42 #include <linux/init.h>
43 #include <linux/mutex.h>
45 static int __init alloc_workspaces(void)
47 def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
48 if (!def_strm.workspace) {
49 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
52 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
53 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
54 if (!inf_strm.workspace) {
55 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
56 vfree(def_strm.workspace);
59 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
63 static void free_workspaces(void)
65 vfree(def_strm.workspace);
66 vfree(inf_strm.workspace);
69 #define alloc_workspaces() (0)
70 #define free_workspaces() do { } while(0)
71 #endif /* __KERNEL__ */
73 static int jffs2_zlib_compress(unsigned char *data_in,
74 unsigned char *cpage_out,
75 uint32_t *sourcelen, uint32_t *dstlen,
80 if (*dstlen <= STREAM_END_SPACE)
83 mutex_lock(&deflate_mutex);
85 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
86 printk(KERN_WARNING "deflateInit failed\n");
87 mutex_unlock(&deflate_mutex);
91 def_strm.next_in = data_in;
92 def_strm.total_in = 0;
94 def_strm.next_out = cpage_out;
95 def_strm.total_out = 0;
97 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
98 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
99 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
100 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
101 def_strm.avail_in, def_strm.avail_out));
102 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
103 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
104 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
106 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
107 zlib_deflateEnd(&def_strm);
108 mutex_unlock(&deflate_mutex);
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);
117 if (ret != Z_STREAM_END) {
118 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
123 if (def_strm.total_out >= def_strm.total_in) {
124 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
125 def_strm.total_in, def_strm.total_out));
130 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
131 def_strm.total_in, def_strm.total_out));
133 *dstlen = def_strm.total_out;
134 *sourcelen = def_strm.total_in;
137 mutex_unlock(&deflate_mutex);
141 static int jffs2_zlib_decompress(unsigned char *data_in,
142 unsigned char *cpage_out,
143 uint32_t srclen, uint32_t destlen,
147 int wbits = MAX_WBITS;
149 mutex_lock(&inflate_mutex);
151 inf_strm.next_in = data_in;
152 inf_strm.avail_in = srclen;
153 inf_strm.total_in = 0;
155 inf_strm.next_out = cpage_out;
156 inf_strm.avail_out = destlen;
157 inf_strm.total_out = 0;
159 /* If it's deflate, and it's got no preset dictionary, then
160 we can tell zlib to skip the adler32 check. */
161 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
162 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
163 !(((data_in[0]<<8) + data_in[1]) % 31)) {
165 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
166 wbits = -((data_in[0] >> 4) + 8);
167 inf_strm.next_in += 2;
168 inf_strm.avail_in -= 2;
170 /* Let this remain D1 for now -- it should never happen */
171 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
175 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
176 printk(KERN_WARNING "inflateInit failed\n");
177 mutex_unlock(&inflate_mutex);
181 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
183 if (ret != Z_STREAM_END) {
184 printk(KERN_NOTICE "inflate returned %d\n", ret);
186 zlib_inflateEnd(&inf_strm);
187 mutex_unlock(&inflate_mutex);
191 static struct jffs2_compressor jffs2_zlib_comp = {
192 .priority = JFFS2_ZLIB_PRIORITY,
194 .compr = JFFS2_COMPR_ZLIB,
195 .compress = &jffs2_zlib_compress,
196 .decompress = &jffs2_zlib_decompress,
197 #ifdef JFFS2_ZLIB_DISABLED
204 int __init jffs2_zlib_init(void)
208 ret = alloc_workspaces();
212 ret = jffs2_register_compressor(&jffs2_zlib_comp);
219 void jffs2_zlib_exit(void)
221 jffs2_unregister_compressor(&jffs2_zlib_comp);