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