BUG_ON() Conversion in mm/mmap.c
[linux-2.6] / fs / jffs2 / compr_zlib.c
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.32 2005/11/07 11:14:38 gleixner 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/sched.h>
21 #include <linux/slab.h>
22 #include <linux/zlib.h>
23 #include <linux/zutil.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
36 static DEFINE_MUTEX(deflate_mutex);
37 static DEFINE_MUTEX(inflate_mutex);
38 static z_stream inf_strm, def_strm;
39
40 #ifdef __KERNEL__ /* Linux-only */
41 #include <linux/vmalloc.h>
42 #include <linux/init.h>
43 #include <linux/mutex.h>
44
45 static 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
63 static 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
73 static int jffs2_zlib_compress(unsigned char *data_in,
74                                unsigned char *cpage_out,
75                                uint32_t *sourcelen, uint32_t *dstlen,
76                                void *model)
77 {
78         int ret;
79
80         if (*dstlen <= STREAM_END_SPACE)
81                 return -1;
82
83         mutex_lock(&deflate_mutex);
84
85         if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
86                 printk(KERN_WARNING "deflateInit failed\n");
87                 mutex_unlock(&deflate_mutex);
88                 return -1;
89         }
90
91         def_strm.next_in = data_in;
92         def_strm.total_in = 0;
93
94         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);
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));
105                 if (ret != Z_OK) {
106                         D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
107                         zlib_deflateEnd(&def_strm);
108                         mutex_unlock(&deflate_mutex);
109                         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:
137         mutex_unlock(&deflate_mutex);
138         return ret;
139 }
140
141 static int jffs2_zlib_decompress(unsigned char *data_in,
142                                  unsigned char *cpage_out,
143                                  uint32_t srclen, uint32_t destlen,
144                                  void *model)
145 {
146         int ret;
147         int wbits = MAX_WBITS;
148
149         mutex_lock(&inflate_mutex);
150
151         inf_strm.next_in = data_in;
152         inf_strm.avail_in = srclen;
153         inf_strm.total_in = 0;
154
155         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");
177                 mutex_unlock(&inflate_mutex);
178                 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);
187         mutex_unlock(&inflate_mutex);
188         return 0;
189 }
190
191 static 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
204 int __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
219 void jffs2_zlib_exit(void)
220 {
221     jffs2_unregister_compressor(&jffs2_zlib_comp);
222     free_workspaces();
223 }