Blame view

fs/jffs2/compr_zlib.c 5.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2001-2007 Red Hat, Inc.
6088c0587   David Woodhouse   jffs2: Update cop...
5
   * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
   *
   * Created by David Woodhouse <dwmw2@infradead.org>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
   */
  
  #if !defined(__KERNEL__) && !defined(__ECOS)
  #error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
  #endif
5a528957e   Joe Perches   jffs2: Use pr_fmt...
16
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
  #include <linux/zlib.h>
  #include <linux/zutil.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include "nodelist.h"
  #include "compr.h"
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
22
23
  	/* Plan: call deflate() with avail_in == *sourcelen,
  		avail_out = *dstlen - 12 and flush == Z_FINISH.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
  		If it doesn't manage to finish,	call it again with
  		avail_in == 0 and avail_out set to the remaining 12
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
26
  		bytes for it to clean up.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
  	   Q: Is 12 bytes sufficient?
  	*/
  #define STREAM_END_SPACE 12
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
30
31
  static DEFINE_MUTEX(deflate_mutex);
  static DEFINE_MUTEX(inflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
  static z_stream inf_strm, def_strm;
  
  #ifdef __KERNEL__ /* Linux-only */
  #include <linux/vmalloc.h>
  #include <linux/init.h>
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
37
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
  
  static int __init alloc_workspaces(void)
  {
565d76cb7   Jim Keniston   zlib: slim down z...
41
42
  	def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
  							MAX_MEM_LEVEL));
045ead345   Joe Perches   jffs2: Remove unn...
43
  	if (!def_strm.workspace)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  		return -ENOMEM;
045ead345   Joe Perches   jffs2: Remove unn...
45

9c261b33a   Joe Perches   jffs2: Convert mo...
46
47
48
  	jffs2_dbg(1, "Allocated %d bytes for deflate workspace
  ",
  		  zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
  	inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
  	if (!inf_strm.workspace) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  		vfree(def_strm.workspace);
  		return -ENOMEM;
  	}
9c261b33a   Joe Perches   jffs2: Convert mo...
54
55
56
  	jffs2_dbg(1, "Allocated %d bytes for inflate workspace
  ",
  		  zlib_inflate_workspacesize());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
65
66
67
68
  	return 0;
  }
  
  static void free_workspaces(void)
  {
  	vfree(def_strm.workspace);
  	vfree(inf_strm.workspace);
  }
  #else
  #define alloc_workspaces() (0)
  #define free_workspaces() do { } while(0)
  #endif /* __KERNEL__ */
75c96f858   Adrian Bunk   [PATCH] make some...
69
70
  static int jffs2_zlib_compress(unsigned char *data_in,
  			       unsigned char *cpage_out,
088bd455c   Mike Frysinger   jffs2: drop unuse...
71
  			       uint32_t *sourcelen, uint32_t *dstlen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
74
75
76
  {
  	int ret;
  
  	if (*dstlen <= STREAM_END_SPACE)
  		return -1;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
77
  	mutex_lock(&deflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  
  	if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
da320f055   Joe Perches   jffs2: Convert pr...
80
81
  		pr_warn("deflateInit failed
  ");
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
82
  		mutex_unlock(&deflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
86
87
  		return -1;
  	}
  
  	def_strm.next_in = data_in;
  	def_strm.total_in = 0;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
88

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
  	def_strm.next_out = cpage_out;
  	def_strm.total_out = 0;
  
  	while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
  		def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
d97b07c54   Yinghai Lu   initramfs: suppor...
94
95
96
97
  		def_strm.avail_in = min_t(unsigned long,
  			(*sourcelen-def_strm.total_in), def_strm.avail_out);
  		jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld
  ",
9c261b33a   Joe Perches   jffs2: Convert mo...
98
  			  def_strm.avail_in, def_strm.avail_out);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  		ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
d97b07c54   Yinghai Lu   initramfs: suppor...
100
101
  		jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld
  ",
9c261b33a   Joe Perches   jffs2: Convert mo...
102
103
  			  def_strm.avail_in, def_strm.avail_out,
  			  def_strm.total_in, def_strm.total_out);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  		if (ret != Z_OK) {
9c261b33a   Joe Perches   jffs2: Convert mo...
105
106
  			jffs2_dbg(1, "deflate in loop returned %d
  ", ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  			zlib_deflateEnd(&def_strm);
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
108
  			mutex_unlock(&deflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
116
117
  			return -1;
  		}
  	}
  	def_strm.avail_out += STREAM_END_SPACE;
  	def_strm.avail_in = 0;
  	ret = zlib_deflate(&def_strm, Z_FINISH);
  	zlib_deflateEnd(&def_strm);
  
  	if (ret != Z_STREAM_END) {
9c261b33a   Joe Perches   jffs2: Convert mo...
118
119
  		jffs2_dbg(1, "final deflate returned %d
  ", ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
  		ret = -1;
  		goto out;
  	}
  
  	if (def_strm.total_out >= def_strm.total_in) {
9c261b33a   Joe Perches   jffs2: Convert mo...
125
126
127
  		jffs2_dbg(1, "zlib compressed %ld bytes into %ld; failing
  ",
  			  def_strm.total_in, def_strm.total_out);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
  		ret = -1;
  		goto out;
  	}
9c261b33a   Joe Perches   jffs2: Convert mo...
131
132
133
  	jffs2_dbg(1, "zlib compressed %ld bytes into %ld
  ",
  		  def_strm.total_in, def_strm.total_out);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
138
  
  	*dstlen = def_strm.total_out;
  	*sourcelen = def_strm.total_in;
  	ret = 0;
   out:
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
139
  	mutex_unlock(&deflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
  	return ret;
  }
75c96f858   Adrian Bunk   [PATCH] make some...
142
143
  static int jffs2_zlib_decompress(unsigned char *data_in,
  				 unsigned char *cpage_out,
088bd455c   Mike Frysinger   jffs2: drop unuse...
144
  				 uint32_t srclen, uint32_t destlen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
147
  {
  	int ret;
  	int wbits = MAX_WBITS;
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
148
  	mutex_lock(&inflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
151
152
  
  	inf_strm.next_in = data_in;
  	inf_strm.avail_in = srclen;
  	inf_strm.total_in = 0;
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
153

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
157
158
159
160
161
162
  	inf_strm.next_out = cpage_out;
  	inf_strm.avail_out = destlen;
  	inf_strm.total_out = 0;
  
  	/* If it's deflate, and it's got no preset dictionary, then
  	   we can tell zlib to skip the adler32 check. */
  	if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
  	    ((data_in[0] & 0x0f) == Z_DEFLATED) &&
  	    !(((data_in[0]<<8) + data_in[1]) % 31)) {
9c261b33a   Joe Perches   jffs2: Convert mo...
163
164
  		jffs2_dbg(2, "inflate skipping adler32
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
169
  		wbits = -((data_in[0] >> 4) + 8);
  		inf_strm.next_in += 2;
  		inf_strm.avail_in -= 2;
  	} else {
  		/* Let this remain D1 for now -- it should never happen */
9c261b33a   Joe Perches   jffs2: Convert mo...
170
171
  		jffs2_dbg(1, "inflate not skipping adler32
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
174
175
  	}
  
  
  	if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
da320f055   Joe Perches   jffs2: Convert pr...
176
177
  		pr_warn("inflateInit failed
  ");
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
178
  		mutex_unlock(&inflate_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
  		return 1;
  	}
  
  	while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
  		;
  	if (ret != Z_STREAM_END) {
da320f055   Joe Perches   jffs2: Convert pr...
185
186
  		pr_notice("inflate returned %d
  ", ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
  	}
  	zlib_inflateEnd(&inf_strm);
353ab6e97   Ingo Molnar   [PATCH] sem2mutex...
189
  	mutex_unlock(&inflate_mutex);
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
190
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  }
  
  static struct jffs2_compressor jffs2_zlib_comp = {
      .priority = JFFS2_ZLIB_PRIORITY,
      .name = "zlib",
      .compr = JFFS2_COMPR_ZLIB,
      .compress = &jffs2_zlib_compress,
      .decompress = &jffs2_zlib_decompress,
  #ifdef JFFS2_ZLIB_DISABLED
      .disabled = 1,
  #else
      .disabled = 0,
  #endif
  };
  
  int __init jffs2_zlib_init(void)
  {
      int ret;
  
      ret = alloc_workspaces();
      if (ret)
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
212
  	    return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
  
      ret = jffs2_register_compressor(&jffs2_zlib_comp);
      if (ret)
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
216
  	    free_workspaces();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
219
220
221
222
223
224
225
  
      return ret;
  }
  
  void jffs2_zlib_exit(void)
  {
      jffs2_unregister_compressor(&jffs2_zlib_comp);
      free_workspaces();
  }