aboutsummaryrefslogtreecommitdiffstats
path: root/fs/jffs2/compr_zlib.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/jffs2/compr_zlib.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/jffs2/compr_zlib.c')
-rw-r--r--fs/jffs2/compr_zlib.c218
1 files changed, 218 insertions, 0 deletions
diff --git a/fs/jffs2/compr_zlib.c b/fs/jffs2/compr_zlib.c
new file mode 100644
index 000000000000..9f9932c22adb
--- /dev/null
+++ b/fs/jffs2/compr_zlib.c
@@ -0,0 +1,218 @@
1/*
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 *
10 * $Id: compr_zlib.c,v 1.29 2004/11/16 20:36:11 dwmw2 Exp $
11 *
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>
20#include <linux/slab.h>
21#include <linux/zlib.h>
22#include <linux/zutil.h>
23#include <asm/semaphore.h>
24#include "nodelist.h"
25#include "compr.h"
26
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?
33 */
34#define STREAM_END_SPACE 12
35
36static DECLARE_MUTEX(deflate_sem);
37static DECLARE_MUTEX(inflate_sem);
38static z_stream inf_strm, def_strm;
39
40#ifdef __KERNEL__ /* Linux-only */
41#include <linux/vmalloc.h>
42#include <linux/init.h>
43
44static int __init alloc_workspaces(void)
45{
46 def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
47 if (!def_strm.workspace) {
48 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
49 return -ENOMEM;
50 }
51 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
52 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
53 if (!inf_strm.workspace) {
54 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
55 vfree(def_strm.workspace);
56 return -ENOMEM;
57 }
58 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
59 return 0;
60}
61
62static void free_workspaces(void)
63{
64 vfree(def_strm.workspace);
65 vfree(inf_strm.workspace);
66}
67#else
68#define alloc_workspaces() (0)
69#define free_workspaces() do { } while(0)
70#endif /* __KERNEL__ */
71
72int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
73 uint32_t *sourcelen, uint32_t *dstlen, void *model)
74{
75 int ret;
76
77 if (*dstlen <= STREAM_END_SPACE)
78 return -1;
79
80 down(&deflate_sem);
81
82 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
83 printk(KERN_WARNING "deflateInit failed\n");
84 up(&deflate_sem);
85 return -1;
86 }
87
88 def_strm.next_in = data_in;
89 def_strm.total_in = 0;
90
91 def_strm.next_out = cpage_out;
92 def_strm.total_out = 0;
93
94 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
95 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
96 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
97 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
98 def_strm.avail_in, def_strm.avail_out));
99 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
100 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
101 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
102 if (ret != Z_OK) {
103 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
104 zlib_deflateEnd(&def_strm);
105 up(&deflate_sem);
106 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) {
115 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
116 ret = -1;
117 goto out;
118 }
119
120 if (def_strm.total_out >= def_strm.total_in) {
121 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
122 def_strm.total_in, def_strm.total_out));
123 ret = -1;
124 goto out;
125 }
126
127 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
128 def_strm.total_in, def_strm.total_out));
129
130 *dstlen = def_strm.total_out;
131 *sourcelen = def_strm.total_in;
132 ret = 0;
133 out:
134 up(&deflate_sem);
135 return ret;
136}
137
138int jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
139 uint32_t srclen, uint32_t destlen, void *model)
140{
141 int ret;
142 int wbits = MAX_WBITS;
143
144 down(&inflate_sem);
145
146 inf_strm.next_in = data_in;
147 inf_strm.avail_in = srclen;
148 inf_strm.total_in = 0;
149
150 inf_strm.next_out = cpage_out;
151 inf_strm.avail_out = destlen;
152 inf_strm.total_out = 0;
153
154 /* If it's deflate, and it's got no preset dictionary, then
155 we can tell zlib to skip the adler32 check. */
156 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
157 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
158 !(((data_in[0]<<8) + data_in[1]) % 31)) {
159
160 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
161 wbits = -((data_in[0] >> 4) + 8);
162 inf_strm.next_in += 2;
163 inf_strm.avail_in -= 2;
164 } else {
165 /* Let this remain D1 for now -- it should never happen */
166 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
167 }
168
169
170 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
171 printk(KERN_WARNING "inflateInit failed\n");
172 up(&inflate_sem);
173 return 1;
174 }
175
176 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
177 ;
178 if (ret != Z_STREAM_END) {
179 printk(KERN_NOTICE "inflate returned %d\n", ret);
180 }
181 zlib_inflateEnd(&inf_strm);
182 up(&inflate_sem);
183 return 0;
184}
185
186static struct jffs2_compressor jffs2_zlib_comp = {
187 .priority = JFFS2_ZLIB_PRIORITY,
188 .name = "zlib",
189 .compr = JFFS2_COMPR_ZLIB,
190 .compress = &jffs2_zlib_compress,
191 .decompress = &jffs2_zlib_decompress,
192#ifdef JFFS2_ZLIB_DISABLED
193 .disabled = 1,
194#else
195 .disabled = 0,
196#endif
197};
198
199int __init jffs2_zlib_init(void)
200{
201 int ret;
202
203 ret = alloc_workspaces();
204 if (ret)
205 return ret;
206
207 ret = jffs2_register_compressor(&jffs2_zlib_comp);
208 if (ret)
209 free_workspaces();
210
211 return ret;
212}
213
214void jffs2_zlib_exit(void)
215{
216 jffs2_unregister_compressor(&jffs2_zlib_comp);
217 free_workspaces();
218}