xref: /openbmc/linux/fs/jffs2/compr_zlib.c (revision 045ead34)
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6  *
7  * Created by David Woodhouse <dwmw2@infradead.org>
8  *
9  * For licensing information, see the file 'LICENCE' in this directory.
10  *
11  */
12 
13 #if !defined(__KERNEL__) && !defined(__ECOS)
14 #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
15 #endif
16 
17 #include <linux/kernel.h>
18 #include <linux/zlib.h>
19 #include <linux/zutil.h>
20 #include "nodelist.h"
21 #include "compr.h"
22 
23 	/* Plan: call deflate() with avail_in == *sourcelen,
24 		avail_out = *dstlen - 12 and flush == Z_FINISH.
25 		If it doesn't manage to finish,	call it again with
26 		avail_in == 0 and avail_out set to the remaining 12
27 		bytes for it to clean up.
28 	   Q: Is 12 bytes sufficient?
29 	*/
30 #define STREAM_END_SPACE 12
31 
32 static DEFINE_MUTEX(deflate_mutex);
33 static DEFINE_MUTEX(inflate_mutex);
34 static z_stream inf_strm, def_strm;
35 
36 #ifdef __KERNEL__ /* Linux-only */
37 #include <linux/vmalloc.h>
38 #include <linux/init.h>
39 #include <linux/mutex.h>
40 
41 static int __init alloc_workspaces(void)
42 {
43 	def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
44 							MAX_MEM_LEVEL));
45 	if (!def_strm.workspace)
46 		return -ENOMEM;
47 
48 	D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL)));
49 	inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
50 	if (!inf_strm.workspace) {
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 
58 static 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 
68 static int jffs2_zlib_compress(unsigned char *data_in,
69 			       unsigned char *cpage_out,
70 			       uint32_t *sourcelen, uint32_t *dstlen)
71 {
72 	int ret;
73 
74 	if (*dstlen <= STREAM_END_SPACE)
75 		return -1;
76 
77 	mutex_lock(&deflate_mutex);
78 
79 	if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
80 		printk(KERN_WARNING "deflateInit failed\n");
81 		mutex_unlock(&deflate_mutex);
82 		return -1;
83 	}
84 
85 	def_strm.next_in = data_in;
86 	def_strm.total_in = 0;
87 
88 	def_strm.next_out = cpage_out;
89 	def_strm.total_out = 0;
90 
91 	while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
92 		def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
93 		def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
94 		D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
95 			  def_strm.avail_in, def_strm.avail_out));
96 		ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
97 		D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
98 			  def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
99 		if (ret != Z_OK) {
100 			D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
101 			zlib_deflateEnd(&def_strm);
102 			mutex_unlock(&deflate_mutex);
103 			return -1;
104 		}
105 	}
106 	def_strm.avail_out += STREAM_END_SPACE;
107 	def_strm.avail_in = 0;
108 	ret = zlib_deflate(&def_strm, Z_FINISH);
109 	zlib_deflateEnd(&def_strm);
110 
111 	if (ret != Z_STREAM_END) {
112 		D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
113 		ret = -1;
114 		goto out;
115 	}
116 
117 	if (def_strm.total_out >= def_strm.total_in) {
118 		D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
119 			  def_strm.total_in, def_strm.total_out));
120 		ret = -1;
121 		goto out;
122 	}
123 
124 	D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
125 		  def_strm.total_in, def_strm.total_out));
126 
127 	*dstlen = def_strm.total_out;
128 	*sourcelen = def_strm.total_in;
129 	ret = 0;
130  out:
131 	mutex_unlock(&deflate_mutex);
132 	return ret;
133 }
134 
135 static int jffs2_zlib_decompress(unsigned char *data_in,
136 				 unsigned char *cpage_out,
137 				 uint32_t srclen, uint32_t destlen)
138 {
139 	int ret;
140 	int wbits = MAX_WBITS;
141 
142 	mutex_lock(&inflate_mutex);
143 
144 	inf_strm.next_in = data_in;
145 	inf_strm.avail_in = srclen;
146 	inf_strm.total_in = 0;
147 
148 	inf_strm.next_out = cpage_out;
149 	inf_strm.avail_out = destlen;
150 	inf_strm.total_out = 0;
151 
152 	/* If it's deflate, and it's got no preset dictionary, then
153 	   we can tell zlib to skip the adler32 check. */
154 	if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
155 	    ((data_in[0] & 0x0f) == Z_DEFLATED) &&
156 	    !(((data_in[0]<<8) + data_in[1]) % 31)) {
157 
158 		D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
159 		wbits = -((data_in[0] >> 4) + 8);
160 		inf_strm.next_in += 2;
161 		inf_strm.avail_in -= 2;
162 	} else {
163 		/* Let this remain D1 for now -- it should never happen */
164 		D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
165 	}
166 
167 
168 	if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
169 		printk(KERN_WARNING "inflateInit failed\n");
170 		mutex_unlock(&inflate_mutex);
171 		return 1;
172 	}
173 
174 	while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
175 		;
176 	if (ret != Z_STREAM_END) {
177 		printk(KERN_NOTICE "inflate returned %d\n", ret);
178 	}
179 	zlib_inflateEnd(&inf_strm);
180 	mutex_unlock(&inflate_mutex);
181 	return 0;
182 }
183 
184 static struct jffs2_compressor jffs2_zlib_comp = {
185     .priority = JFFS2_ZLIB_PRIORITY,
186     .name = "zlib",
187     .compr = JFFS2_COMPR_ZLIB,
188     .compress = &jffs2_zlib_compress,
189     .decompress = &jffs2_zlib_decompress,
190 #ifdef JFFS2_ZLIB_DISABLED
191     .disabled = 1,
192 #else
193     .disabled = 0,
194 #endif
195 };
196 
197 int __init jffs2_zlib_init(void)
198 {
199     int ret;
200 
201     ret = alloc_workspaces();
202     if (ret)
203 	    return ret;
204 
205     ret = jffs2_register_compressor(&jffs2_zlib_comp);
206     if (ret)
207 	    free_workspaces();
208 
209     return ret;
210 }
211 
212 void jffs2_zlib_exit(void)
213 {
214     jffs2_unregister_compressor(&jffs2_zlib_comp);
215     free_workspaces();
216 }
217