blob: f25e70c1b51c5854d987558ec5c84c0c94410b73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Created by Arjan van de Ven <arjanv@redhat.com>
6 *
David Woodhousec00c3102007-04-25 14:16:47 +01007 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
David Woodhouseef53cb02007-07-10 10:01:22 +01008 * University of Szeged, Hungary
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * For licensing information, see the file 'LICENCE' in this directory.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include "compr.h"
15
16static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
17
18/* Available compressors are on this list */
19static LIST_HEAD(jffs2_compressor_list);
20
21/* Actual compression mode */
22static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
23
24/* Statistics for blocks stored without compression */
25static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
26
Richard Purdie3b23c1f2007-07-10 10:28:42 +010027
28/*
29 * Return 1 to use this compression
30 */
31static int jffs2_is_best_compression(struct jffs2_compressor *this,
32 struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
33{
34 switch (jffs2_compression_mode) {
35 case JFFS2_COMPR_MODE_SIZE:
36 if (bestsize > size)
37 return 1;
38 return 0;
39 case JFFS2_COMPR_MODE_FAVOURLZO:
40 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
41 return 1;
42 if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
43 return 1;
44 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
45 return 1;
46 if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
47 return 1;
48
49 return 0;
50 }
51 /* Shouldn't happen */
52 return 0;
53}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* jffs2_compress:
Geert Uytterhoevenfaa5c2a2008-10-17 16:19:45 +020056 * @data_in: Pointer to uncompressed data
57 * @cpage_out: Pointer to returned pointer to buffer for compressed data
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * @datalen: On entry, holds the amount of data available for compression.
59 * On exit, expected to hold the amount of data actually compressed.
60 * @cdatalen: On entry, holds the amount of space available for compressed
61 * data. On exit, expected to hold the actual size of the compressed
62 * data.
63 *
64 * Returns: Lower byte to be stored with data indicating compression type used.
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000065 * Zero is used to show that the data could not be compressed - the
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * compressed version was actually larger than the original.
67 * Upper byte will be used later. (soon)
68 *
69 * If the cdata buffer isn't large enough to hold all the uncompressed data,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000070 * jffs2_compress should compress as much as will fit, and should set
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * *datalen accordingly to show the amount of data which were compressed.
72 */
73uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
David Woodhouseef53cb02007-07-10 10:01:22 +010074 unsigned char *data_in, unsigned char **cpage_out,
75 uint32_t *datalen, uint32_t *cdatalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int ret = JFFS2_COMPR_NONE;
David Woodhouseef53cb02007-07-10 10:01:22 +010078 int compr_ret;
79 struct jffs2_compressor *this, *best=NULL;
80 unsigned char *output_buf = NULL, *tmp_buf;
81 uint32_t orig_slen, orig_dlen;
82 uint32_t best_slen=0, best_dlen=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David Woodhouseef53cb02007-07-10 10:01:22 +010084 switch (jffs2_compression_mode) {
85 case JFFS2_COMPR_MODE_NONE:
86 break;
87 case JFFS2_COMPR_MODE_PRIORITY:
88 output_buf = kmalloc(*cdatalen,GFP_KERNEL);
89 if (!output_buf) {
90 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
91 goto out;
92 }
93 orig_slen = *datalen;
94 orig_dlen = *cdatalen;
95 spin_lock(&jffs2_compressor_list_lock);
96 list_for_each_entry(this, &jffs2_compressor_list, list) {
97 /* Skip decompress-only backwards-compatibility and disabled modules */
98 if ((!this->compress)||(this->disabled))
99 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
David Woodhouseef53cb02007-07-10 10:01:22 +0100101 this->usecount++;
102 spin_unlock(&jffs2_compressor_list_lock);
103 *datalen = orig_slen;
104 *cdatalen = orig_dlen;
105 compr_ret = this->compress(data_in, output_buf, datalen, cdatalen, NULL);
106 spin_lock(&jffs2_compressor_list_lock);
107 this->usecount--;
108 if (!compr_ret) {
109 ret = this->compr;
110 this->stat_compr_blocks++;
111 this->stat_compr_orig_size += *datalen;
112 this->stat_compr_new_size += *cdatalen;
113 break;
114 }
115 }
116 spin_unlock(&jffs2_compressor_list_lock);
117 if (ret == JFFS2_COMPR_NONE)
118 kfree(output_buf);
119 break;
120 case JFFS2_COMPR_MODE_SIZE:
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100121 case JFFS2_COMPR_MODE_FAVOURLZO:
David Woodhouseef53cb02007-07-10 10:01:22 +0100122 orig_slen = *datalen;
123 orig_dlen = *cdatalen;
124 spin_lock(&jffs2_compressor_list_lock);
125 list_for_each_entry(this, &jffs2_compressor_list, list) {
126 /* Skip decompress-only backwards-compatibility and disabled modules */
127 if ((!this->compress)||(this->disabled))
128 continue;
129 /* Allocating memory for output buffer if necessary */
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100130 if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
David Woodhouseef53cb02007-07-10 10:01:22 +0100131 spin_unlock(&jffs2_compressor_list_lock);
132 kfree(this->compr_buf);
133 spin_lock(&jffs2_compressor_list_lock);
134 this->compr_buf_size=0;
135 this->compr_buf=NULL;
136 }
137 if (!this->compr_buf) {
138 spin_unlock(&jffs2_compressor_list_lock);
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100139 tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
David Woodhouseef53cb02007-07-10 10:01:22 +0100140 spin_lock(&jffs2_compressor_list_lock);
141 if (!tmp_buf) {
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100142 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n", orig_slen);
David Woodhouseef53cb02007-07-10 10:01:22 +0100143 continue;
144 }
145 else {
146 this->compr_buf = tmp_buf;
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100147 this->compr_buf_size = orig_slen;
David Woodhouseef53cb02007-07-10 10:01:22 +0100148 }
149 }
150 this->usecount++;
151 spin_unlock(&jffs2_compressor_list_lock);
152 *datalen = orig_slen;
153 *cdatalen = orig_dlen;
154 compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen, NULL);
155 spin_lock(&jffs2_compressor_list_lock);
156 this->usecount--;
157 if (!compr_ret) {
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100158 if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
159 && (*cdatalen < *datalen)) {
David Woodhouseef53cb02007-07-10 10:01:22 +0100160 best_dlen = *cdatalen;
161 best_slen = *datalen;
162 best = this;
163 }
164 }
165 }
166 if (best_dlen) {
167 *cdatalen = best_dlen;
168 *datalen = best_slen;
169 output_buf = best->compr_buf;
170 best->compr_buf = NULL;
171 best->compr_buf_size = 0;
172 best->stat_compr_blocks++;
173 best->stat_compr_orig_size += best_slen;
174 best->stat_compr_new_size += best_dlen;
175 ret = best->compr;
176 }
177 spin_unlock(&jffs2_compressor_list_lock);
178 break;
179 default:
180 printk(KERN_ERR "JFFS2: unknow compression mode.\n");
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 out:
David Woodhouseef53cb02007-07-10 10:01:22 +0100183 if (ret == JFFS2_COMPR_NONE) {
184 *cpage_out = data_in;
185 *datalen = *cdatalen;
186 none_stat_compr_blocks++;
187 none_stat_compr_size += *datalen;
188 }
189 else {
190 *cpage_out = output_buf;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
193}
194
195int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000196 uint16_t comprtype, unsigned char *cdata_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
198{
David Woodhouseef53cb02007-07-10 10:01:22 +0100199 struct jffs2_compressor *this;
200 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Older code had a bug where it would write non-zero 'usercompr'
203 fields. Deal with it. */
204 if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
205 comprtype &= 0xff;
206
207 switch (comprtype & 0xff) {
208 case JFFS2_COMPR_NONE:
209 /* This should be special-cased elsewhere, but we might as well deal with it */
210 memcpy(data_out, cdata_in, datalen);
David Woodhouseef53cb02007-07-10 10:01:22 +0100211 none_stat_decompr_blocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 case JFFS2_COMPR_ZERO:
214 memset(data_out, 0, datalen);
215 break;
216 default:
David Woodhouseef53cb02007-07-10 10:01:22 +0100217 spin_lock(&jffs2_compressor_list_lock);
218 list_for_each_entry(this, &jffs2_compressor_list, list) {
219 if (comprtype == this->compr) {
220 this->usecount++;
221 spin_unlock(&jffs2_compressor_list_lock);
222 ret = this->decompress(cdata_in, data_out, cdatalen, datalen, NULL);
223 spin_lock(&jffs2_compressor_list_lock);
224 if (ret) {
225 printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
226 }
227 else {
228 this->stat_decompr_blocks++;
229 }
230 this->usecount--;
231 spin_unlock(&jffs2_compressor_list_lock);
232 return ret;
233 }
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
David Woodhouseef53cb02007-07-10 10:01:22 +0100236 spin_unlock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EIO;
238 }
239 return 0;
240}
241
242int jffs2_register_compressor(struct jffs2_compressor *comp)
243{
David Woodhouseef53cb02007-07-10 10:01:22 +0100244 struct jffs2_compressor *this;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David Woodhouseef53cb02007-07-10 10:01:22 +0100246 if (!comp->name) {
247 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
248 return -1;
249 }
250 comp->compr_buf_size=0;
251 comp->compr_buf=NULL;
252 comp->usecount=0;
253 comp->stat_compr_orig_size=0;
254 comp->stat_compr_new_size=0;
255 comp->stat_compr_blocks=0;
256 comp->stat_decompr_blocks=0;
257 D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Woodhouseef53cb02007-07-10 10:01:22 +0100259 spin_lock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
David Woodhouseef53cb02007-07-10 10:01:22 +0100261 list_for_each_entry(this, &jffs2_compressor_list, list) {
262 if (this->priority < comp->priority) {
263 list_add(&comp->list, this->list.prev);
264 goto out;
265 }
266 }
267 list_add_tail(&comp->list, &jffs2_compressor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268out:
David Woodhouseef53cb02007-07-10 10:01:22 +0100269 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
270 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
271 })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David Woodhouseef53cb02007-07-10 10:01:22 +0100273 spin_unlock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Woodhouseef53cb02007-07-10 10:01:22 +0100275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278int jffs2_unregister_compressor(struct jffs2_compressor *comp)
279{
David Woodhouseef53cb02007-07-10 10:01:22 +0100280 D2(struct jffs2_compressor *this;)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David Woodhouseef53cb02007-07-10 10:01:22 +0100282 D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
David Woodhouseef53cb02007-07-10 10:01:22 +0100284 spin_lock(&jffs2_compressor_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
David Woodhouseef53cb02007-07-10 10:01:22 +0100286 if (comp->usecount) {
287 spin_unlock(&jffs2_compressor_list_lock);
288 printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
289 return -1;
290 }
291 list_del(&comp->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David Woodhouseef53cb02007-07-10 10:01:22 +0100293 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
294 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
295 })
296 spin_unlock(&jffs2_compressor_list_lock);
297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
301{
David Woodhouseef53cb02007-07-10 10:01:22 +0100302 if (orig != comprbuf)
303 kfree(comprbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
David Brownell7d2beb12006-05-16 16:08:10 +0100306int __init jffs2_compressors_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308/* Registering compressors */
309#ifdef CONFIG_JFFS2_ZLIB
David Woodhouseef53cb02007-07-10 10:01:22 +0100310 jffs2_zlib_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#endif
312#ifdef CONFIG_JFFS2_RTIME
David Woodhouseef53cb02007-07-10 10:01:22 +0100313 jffs2_rtime_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif
315#ifdef CONFIG_JFFS2_RUBIN
David Woodhouseef53cb02007-07-10 10:01:22 +0100316 jffs2_rubinmips_init();
317 jffs2_dynrubin_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#endif
Richard Purdiec799aca2007-07-10 10:28:36 +0100319#ifdef CONFIG_JFFS2_LZO
320 jffs2_lzo_init();
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/* Setting default compression mode */
323#ifdef CONFIG_JFFS2_CMODE_NONE
David Woodhouseef53cb02007-07-10 10:01:22 +0100324 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
325 D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#else
327#ifdef CONFIG_JFFS2_CMODE_SIZE
David Woodhouseef53cb02007-07-10 10:01:22 +0100328 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
329 D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#else
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100331#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
332 jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
333 D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
334#else
David Woodhouseef53cb02007-07-10 10:01:22 +0100335 D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#endif
337#endif
Richard Purdie3b23c1f2007-07-10 10:28:42 +0100338#endif
David Woodhouseef53cb02007-07-10 10:01:22 +0100339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
David Woodhouse3bcc86f2006-06-03 00:25:50 +0100342int jffs2_compressors_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344/* Unregistering compressors */
Richard Purdiec799aca2007-07-10 10:28:36 +0100345#ifdef CONFIG_JFFS2_LZO
346 jffs2_lzo_exit();
347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#ifdef CONFIG_JFFS2_RUBIN
David Woodhouseef53cb02007-07-10 10:01:22 +0100349 jffs2_dynrubin_exit();
350 jffs2_rubinmips_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
352#ifdef CONFIG_JFFS2_RTIME
David Woodhouseef53cb02007-07-10 10:01:22 +0100353 jffs2_rtime_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
355#ifdef CONFIG_JFFS2_ZLIB
David Woodhouseef53cb02007-07-10 10:01:22 +0100356 jffs2_zlib_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#endif
David Woodhouseef53cb02007-07-10 10:01:22 +0100358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}