]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/jffs2/compr_zlib.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / fs / jffs2 / compr_zlib.c
CommitLineData
1da177e4
LT
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
c00c310e 4 * Copyright © 2001-2007 Red Hat, Inc.
1da177e4
LT
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
1da177e4
LT
10 */
11
12#if !defined(__KERNEL__) && !defined(__ECOS)
13#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
14#endif
15
1da177e4 16#include <linux/kernel.h>
1da177e4
LT
17#include <linux/zlib.h>
18#include <linux/zutil.h>
1da177e4
LT
19#include "nodelist.h"
20#include "compr.h"
21
182ec4ee
TG
22 /* Plan: call deflate() with avail_in == *sourcelen,
23 avail_out = *dstlen - 12 and flush == Z_FINISH.
1da177e4
LT
24 If it doesn't manage to finish, call it again with
25 avail_in == 0 and avail_out set to the remaining 12
182ec4ee 26 bytes for it to clean up.
1da177e4
LT
27 Q: Is 12 bytes sufficient?
28 */
29#define STREAM_END_SPACE 12
30
353ab6e9
IM
31static DEFINE_MUTEX(deflate_mutex);
32static DEFINE_MUTEX(inflate_mutex);
1da177e4
LT
33static z_stream inf_strm, def_strm;
34
35#ifdef __KERNEL__ /* Linux-only */
36#include <linux/vmalloc.h>
37#include <linux/init.h>
353ab6e9 38#include <linux/mutex.h>
1da177e4
LT
39
40static int __init alloc_workspaces(void)
41{
42 def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
43 if (!def_strm.workspace) {
44 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
45 return -ENOMEM;
46 }
47 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
48 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
49 if (!inf_strm.workspace) {
50 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
51 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
75c96f85
AB
68static int jffs2_zlib_compress(unsigned char *data_in,
69 unsigned char *cpage_out,
70 uint32_t *sourcelen, uint32_t *dstlen,
71 void *model)
1da177e4
LT
72{
73 int ret;
74
75 if (*dstlen <= STREAM_END_SPACE)
76 return -1;
77
353ab6e9 78 mutex_lock(&deflate_mutex);
1da177e4
LT
79
80 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
81 printk(KERN_WARNING "deflateInit failed\n");
353ab6e9 82 mutex_unlock(&deflate_mutex);
1da177e4
LT
83 return -1;
84 }
85
86 def_strm.next_in = data_in;
87 def_strm.total_in = 0;
182ec4ee 88
1da177e4
LT
89 def_strm.next_out = cpage_out;
90 def_strm.total_out = 0;
91
92 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
93 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
94 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
95 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
96 def_strm.avail_in, def_strm.avail_out));
97 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
182ec4ee 98 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
1da177e4
LT
99 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
100 if (ret != Z_OK) {
101 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
102 zlib_deflateEnd(&def_strm);
353ab6e9 103 mutex_unlock(&deflate_mutex);
1da177e4
LT
104 return -1;
105 }
106 }
107 def_strm.avail_out += STREAM_END_SPACE;
108 def_strm.avail_in = 0;
109 ret = zlib_deflate(&def_strm, Z_FINISH);
110 zlib_deflateEnd(&def_strm);
111
112 if (ret != Z_STREAM_END) {
113 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
114 ret = -1;
115 goto out;
116 }
117
118 if (def_strm.total_out >= def_strm.total_in) {
119 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
120 def_strm.total_in, def_strm.total_out));
121 ret = -1;
122 goto out;
123 }
124
125 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
126 def_strm.total_in, def_strm.total_out));
127
128 *dstlen = def_strm.total_out;
129 *sourcelen = def_strm.total_in;
130 ret = 0;
131 out:
353ab6e9 132 mutex_unlock(&deflate_mutex);
1da177e4
LT
133 return ret;
134}
135
75c96f85
AB
136static int jffs2_zlib_decompress(unsigned char *data_in,
137 unsigned char *cpage_out,
138 uint32_t srclen, uint32_t destlen,
139 void *model)
1da177e4
LT
140{
141 int ret;
142 int wbits = MAX_WBITS;
143
353ab6e9 144 mutex_lock(&inflate_mutex);
1da177e4
LT
145
146 inf_strm.next_in = data_in;
147 inf_strm.avail_in = srclen;
148 inf_strm.total_in = 0;
182ec4ee 149
1da177e4
LT
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");
353ab6e9 172 mutex_unlock(&inflate_mutex);
1da177e4
LT
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);
353ab6e9 182 mutex_unlock(&inflate_mutex);
ef53cb02 183 return 0;
1da177e4
LT
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)
ef53cb02 205 return ret;
1da177e4
LT
206
207 ret = jffs2_register_compressor(&jffs2_zlib_comp);
208 if (ret)
ef53cb02 209 free_workspaces();
1da177e4
LT
210
211 return ret;
212}
213
214void jffs2_zlib_exit(void)
215{
216 jffs2_unregister_compressor(&jffs2_zlib_comp);
217 free_workspaces();
218}