Merge branch 'x86/ptrace-syscall-exit' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.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
LT
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/zlib.h>
19#include <linux/zutil.h>
1da177e4
LT
20#include "nodelist.h"
21#include "compr.h"
22
182ec4ee
TG
23 /* Plan: call deflate() with avail_in == *sourcelen,
24 avail_out = *dstlen - 12 and flush == Z_FINISH.
1da177e4
LT
25 If it doesn't manage to finish, call it again with
26 avail_in == 0 and avail_out set to the remaining 12
182ec4ee 27 bytes for it to clean up.
1da177e4
LT
28 Q: Is 12 bytes sufficient?
29 */
30#define STREAM_END_SPACE 12
31
353ab6e9
IM
32static DEFINE_MUTEX(deflate_mutex);
33static DEFINE_MUTEX(inflate_mutex);
1da177e4
LT
34static z_stream inf_strm, def_strm;
35
36#ifdef __KERNEL__ /* Linux-only */
37#include <linux/vmalloc.h>
38#include <linux/init.h>
353ab6e9 39#include <linux/mutex.h>
1da177e4
LT
40
41static int __init alloc_workspaces(void)
42{
43 def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
44 if (!def_strm.workspace) {
45 printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
46 return -ENOMEM;
47 }
48 D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
49 inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
50 if (!inf_strm.workspace) {
51 printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
52 vfree(def_strm.workspace);
53 return -ENOMEM;
54 }
55 D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
56 return 0;
57}
58
59static void free_workspaces(void)
60{
61 vfree(def_strm.workspace);
62 vfree(inf_strm.workspace);
63}
64#else
65#define alloc_workspaces() (0)
66#define free_workspaces() do { } while(0)
67#endif /* __KERNEL__ */
68
75c96f85
AB
69static int jffs2_zlib_compress(unsigned char *data_in,
70 unsigned char *cpage_out,
71 uint32_t *sourcelen, uint32_t *dstlen,
72 void *model)
1da177e4
LT
73{
74 int ret;
75
76 if (*dstlen <= STREAM_END_SPACE)
77 return -1;
78
353ab6e9 79 mutex_lock(&deflate_mutex);
1da177e4
LT
80
81 if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
82 printk(KERN_WARNING "deflateInit failed\n");
353ab6e9 83 mutex_unlock(&deflate_mutex);
1da177e4
LT
84 return -1;
85 }
86
87 def_strm.next_in = data_in;
88 def_strm.total_in = 0;
182ec4ee 89
1da177e4
LT
90 def_strm.next_out = cpage_out;
91 def_strm.total_out = 0;
92
93 while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
94 def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
95 def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
96 D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
97 def_strm.avail_in, def_strm.avail_out));
98 ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
182ec4ee 99 D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
1da177e4
LT
100 def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
101 if (ret != Z_OK) {
102 D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
103 zlib_deflateEnd(&def_strm);
353ab6e9 104 mutex_unlock(&deflate_mutex);
1da177e4
LT
105 return -1;
106 }
107 }
108 def_strm.avail_out += STREAM_END_SPACE;
109 def_strm.avail_in = 0;
110 ret = zlib_deflate(&def_strm, Z_FINISH);
111 zlib_deflateEnd(&def_strm);
112
113 if (ret != Z_STREAM_END) {
114 D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
115 ret = -1;
116 goto out;
117 }
118
119 if (def_strm.total_out >= def_strm.total_in) {
120 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
121 def_strm.total_in, def_strm.total_out));
122 ret = -1;
123 goto out;
124 }
125
126 D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
127 def_strm.total_in, def_strm.total_out));
128
129 *dstlen = def_strm.total_out;
130 *sourcelen = def_strm.total_in;
131 ret = 0;
132 out:
353ab6e9 133 mutex_unlock(&deflate_mutex);
1da177e4
LT
134 return ret;
135}
136
75c96f85
AB
137static int jffs2_zlib_decompress(unsigned char *data_in,
138 unsigned char *cpage_out,
139 uint32_t srclen, uint32_t destlen,
140 void *model)
1da177e4
LT
141{
142 int ret;
143 int wbits = MAX_WBITS;
144
353ab6e9 145 mutex_lock(&inflate_mutex);
1da177e4
LT
146
147 inf_strm.next_in = data_in;
148 inf_strm.avail_in = srclen;
149 inf_strm.total_in = 0;
182ec4ee 150
1da177e4
LT
151 inf_strm.next_out = cpage_out;
152 inf_strm.avail_out = destlen;
153 inf_strm.total_out = 0;
154
155 /* If it's deflate, and it's got no preset dictionary, then
156 we can tell zlib to skip the adler32 check. */
157 if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
158 ((data_in[0] & 0x0f) == Z_DEFLATED) &&
159 !(((data_in[0]<<8) + data_in[1]) % 31)) {
160
161 D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
162 wbits = -((data_in[0] >> 4) + 8);
163 inf_strm.next_in += 2;
164 inf_strm.avail_in -= 2;
165 } else {
166 /* Let this remain D1 for now -- it should never happen */
167 D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
168 }
169
170
171 if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
172 printk(KERN_WARNING "inflateInit failed\n");
353ab6e9 173 mutex_unlock(&inflate_mutex);
1da177e4
LT
174 return 1;
175 }
176
177 while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
178 ;
179 if (ret != Z_STREAM_END) {
180 printk(KERN_NOTICE "inflate returned %d\n", ret);
181 }
182 zlib_inflateEnd(&inf_strm);
353ab6e9 183 mutex_unlock(&inflate_mutex);
ef53cb02 184 return 0;
1da177e4
LT
185}
186
187static struct jffs2_compressor jffs2_zlib_comp = {
188 .priority = JFFS2_ZLIB_PRIORITY,
189 .name = "zlib",
190 .compr = JFFS2_COMPR_ZLIB,
191 .compress = &jffs2_zlib_compress,
192 .decompress = &jffs2_zlib_decompress,
193#ifdef JFFS2_ZLIB_DISABLED
194 .disabled = 1,
195#else
196 .disabled = 0,
197#endif
198};
199
200int __init jffs2_zlib_init(void)
201{
202 int ret;
203
204 ret = alloc_workspaces();
205 if (ret)
ef53cb02 206 return ret;
1da177e4
LT
207
208 ret = jffs2_register_compressor(&jffs2_zlib_comp);
209 if (ret)
ef53cb02 210 free_workspaces();
1da177e4
LT
211
212 return ret;
213}
214
215void jffs2_zlib_exit(void)
216{
217 jffs2_unregister_compressor(&jffs2_zlib_comp);
218 free_workspaces();
219}