blob: 5c63e0cdcf4c76cb98cbc86e8fae3720023f2439 [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
18#include <linux/config.h>
19#include <linux/kernel.h>
Thomas Gleixnerc84441c2005-05-20 20:30:09 +010020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/zlib.h>
23#include <linux/zutil.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "nodelist.h"
25#include "compr.h"
26
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000027 /* Plan: call deflate() with avail_in == *sourcelen,
28 avail_out = *dstlen - 12 and flush == Z_FINISH.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 If it doesn't manage to finish, call it again with
30 avail_in == 0 and avail_out set to the remaining 12
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000031 bytes for it to clean up.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 Q: Is 12 bytes sufficient?
33 */
34#define STREAM_END_SPACE 12
35
Ingo Molnar353ab6e2006-03-26 01:37:12 -080036static DEFINE_MUTEX(deflate_mutex);
37static DEFINE_MUTEX(inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static z_stream inf_strm, def_strm;
39
40#ifdef __KERNEL__ /* Linux-only */
41#include <linux/vmalloc.h>
42#include <linux/init.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080043#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static int __init alloc_workspaces(void)
46{
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());
50 return -ENOMEM;
51 }
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);
57 return -ENOMEM;
58 }
59 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
60 return 0;
61}
62
63static void free_workspaces(void)
64{
65 vfree(def_strm.workspace);
66 vfree(inf_strm.workspace);
67}
68#else
69#define alloc_workspaces() (0)
70#define free_workspaces() do { } while(0)
71#endif /* __KERNEL__ */
72
Adrian Bunk75c96f82005-05-05 16:16:09 -070073static int jffs2_zlib_compress(unsigned char *data_in,
74 unsigned char *cpage_out,
75 uint32_t *sourcelen, uint32_t *dstlen,
76 void *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int ret;
79
80 if (*dstlen <= STREAM_END_SPACE)
81 return -1;
82
Ingo Molnar353ab6e2006-03-26 01:37:12 -080083 mutex_lock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
86 printk(KERN_WARNING "deflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -080087 mutex_unlock(&deflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return -1;
89 }
90
91 def_strm.next_in = data_in;
92 def_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 def_strm.next_out = cpage_out;
95 def_strm.total_out = 0;
96
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);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000103 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 -0700104 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
105 if (ret != Z_OK) {
106 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
107 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) {
118 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
119 ret = -1;
120 goto out;
121 }
122
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));
126 ret = -1;
127 goto out;
128 }
129
130 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
131 def_strm.total_in, def_strm.total_out));
132
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,
143 uint32_t srclen, uint32_t destlen,
144 void *model)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int ret;
147 int wbits = MAX_WBITS;
148
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800149 mutex_lock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 inf_strm.next_in = data_in;
152 inf_strm.avail_in = srclen;
153 inf_strm.total_in = 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 inf_strm.next_out = cpage_out;
156 inf_strm.avail_out = destlen;
157 inf_strm.total_out = 0;
158
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)) {
164
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;
169 } else {
170 /* Let this remain D1 for now -- it should never happen */
171 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
172 }
173
174
175 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
176 printk(KERN_WARNING "inflateInit failed\n");
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800177 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 1;
179 }
180
181 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
182 ;
183 if (ret != Z_STREAM_END) {
184 printk(KERN_NOTICE "inflate returned %d\n", ret);
185 }
186 zlib_inflateEnd(&inf_strm);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800187 mutex_unlock(&inflate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return 0;
189}
190
191static struct jffs2_compressor jffs2_zlib_comp = {
192 .priority = JFFS2_ZLIB_PRIORITY,
193 .name = "zlib",
194 .compr = JFFS2_COMPR_ZLIB,
195 .compress = &jffs2_zlib_compress,
196 .decompress = &jffs2_zlib_decompress,
197#ifdef JFFS2_ZLIB_DISABLED
198 .disabled = 1,
199#else
200 .disabled = 0,
201#endif
202};
203
204int __init jffs2_zlib_init(void)
205{
206 int ret;
207
208 ret = alloc_workspaces();
209 if (ret)
210 return ret;
211
212 ret = jffs2_register_compressor(&jffs2_zlib_comp);
213 if (ret)
214 free_workspaces();
215
216 return ret;
217}
218
219void jffs2_zlib_exit(void)
220{
221 jffs2_unregister_compressor(&jffs2_zlib_comp);
222 free_workspaces();
223}