2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
7 * University of Szeged, Hungary
9 * Created by Arjan van de Ven <arjan@infradead.org>
11 * For licensing information, see the file 'LICENCE' in this directory.
17 static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
19 /* Available compressors are on this list */
20 static LIST_HEAD(jffs2_compressor_list);
22 /* Actual compression mode */
23 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
25 /* Statistics for blocks stored without compression */
26 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
30 * Return 1 to use this compression
32 static int jffs2_is_best_compression(struct jffs2_compressor *this,
33 struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
35 switch (jffs2_compression_mode) {
36 case JFFS2_COMPR_MODE_SIZE:
40 case JFFS2_COMPR_MODE_FAVOURLZO:
41 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
43 if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
45 if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
47 if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
52 /* Shouldn't happen */
57 * @data_in: Pointer to uncompressed data
58 * @cpage_out: Pointer to returned pointer to buffer for compressed data
59 * @datalen: On entry, holds the amount of data available for compression.
60 * On exit, expected to hold the amount of data actually compressed.
61 * @cdatalen: On entry, holds the amount of space available for compressed
62 * data. On exit, expected to hold the actual size of the compressed
65 * Returns: Lower byte to be stored with data indicating compression type used.
66 * Zero is used to show that the data could not be compressed - the
67 * compressed version was actually larger than the original.
68 * Upper byte will be used later. (soon)
70 * If the cdata buffer isn't large enough to hold all the uncompressed data,
71 * jffs2_compress should compress as much as will fit, and should set
72 * *datalen accordingly to show the amount of data which were compressed.
74 uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
75 unsigned char *data_in, unsigned char **cpage_out,
76 uint32_t *datalen, uint32_t *cdatalen)
78 int ret = JFFS2_COMPR_NONE;
80 struct jffs2_compressor *this, *best=NULL;
81 unsigned char *output_buf = NULL, *tmp_buf;
82 uint32_t orig_slen, orig_dlen;
83 uint32_t best_slen=0, best_dlen=0;
85 if (c->mount_opts.override_compr)
86 mode = c->mount_opts.compr;
88 mode = jffs2_compression_mode;
91 case JFFS2_COMPR_MODE_NONE:
93 case JFFS2_COMPR_MODE_PRIORITY:
94 output_buf = kmalloc(*cdatalen,GFP_KERNEL);
96 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. Compression failed.\n");
100 orig_dlen = *cdatalen;
101 spin_lock(&jffs2_compressor_list_lock);
102 list_for_each_entry(this, &jffs2_compressor_list, list) {
103 /* Skip decompress-only backwards-compatibility and disabled modules */
104 if ((!this->compress)||(this->disabled))
108 spin_unlock(&jffs2_compressor_list_lock);
109 *datalen = orig_slen;
110 *cdatalen = orig_dlen;
111 compr_ret = this->compress(data_in, output_buf, datalen, cdatalen);
112 spin_lock(&jffs2_compressor_list_lock);
116 this->stat_compr_blocks++;
117 this->stat_compr_orig_size += *datalen;
118 this->stat_compr_new_size += *cdatalen;
122 spin_unlock(&jffs2_compressor_list_lock);
123 if (ret == JFFS2_COMPR_NONE)
126 case JFFS2_COMPR_MODE_SIZE:
127 case JFFS2_COMPR_MODE_FAVOURLZO:
128 orig_slen = *datalen;
129 orig_dlen = *cdatalen;
130 spin_lock(&jffs2_compressor_list_lock);
131 list_for_each_entry(this, &jffs2_compressor_list, list) {
132 /* Skip decompress-only backwards-compatibility and disabled modules */
133 if ((!this->compress)||(this->disabled))
135 /* Allocating memory for output buffer if necessary */
136 if ((this->compr_buf_size < orig_slen) && (this->compr_buf)) {
137 spin_unlock(&jffs2_compressor_list_lock);
138 kfree(this->compr_buf);
139 spin_lock(&jffs2_compressor_list_lock);
140 this->compr_buf_size=0;
141 this->compr_buf=NULL;
143 if (!this->compr_buf) {
144 spin_unlock(&jffs2_compressor_list_lock);
145 tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
146 spin_lock(&jffs2_compressor_list_lock);
148 printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n", orig_slen);
152 this->compr_buf = tmp_buf;
153 this->compr_buf_size = orig_slen;
157 spin_unlock(&jffs2_compressor_list_lock);
158 *datalen = orig_slen;
159 *cdatalen = orig_dlen;
160 compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen);
161 spin_lock(&jffs2_compressor_list_lock);
164 if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
165 && (*cdatalen < *datalen)) {
166 best_dlen = *cdatalen;
167 best_slen = *datalen;
173 *cdatalen = best_dlen;
174 *datalen = best_slen;
175 output_buf = best->compr_buf;
176 best->compr_buf = NULL;
177 best->compr_buf_size = 0;
178 best->stat_compr_blocks++;
179 best->stat_compr_orig_size += best_slen;
180 best->stat_compr_new_size += best_dlen;
183 spin_unlock(&jffs2_compressor_list_lock);
186 printk(KERN_ERR "JFFS2: unknown compression mode.\n");
189 if (ret == JFFS2_COMPR_NONE) {
190 *cpage_out = data_in;
191 *datalen = *cdatalen;
192 none_stat_compr_blocks++;
193 none_stat_compr_size += *datalen;
196 *cpage_out = output_buf;
201 int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
202 uint16_t comprtype, unsigned char *cdata_in,
203 unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
205 struct jffs2_compressor *this;
208 /* Older code had a bug where it would write non-zero 'usercompr'
209 fields. Deal with it. */
210 if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
213 switch (comprtype & 0xff) {
214 case JFFS2_COMPR_NONE:
215 /* This should be special-cased elsewhere, but we might as well deal with it */
216 memcpy(data_out, cdata_in, datalen);
217 none_stat_decompr_blocks++;
219 case JFFS2_COMPR_ZERO:
220 memset(data_out, 0, datalen);
223 spin_lock(&jffs2_compressor_list_lock);
224 list_for_each_entry(this, &jffs2_compressor_list, list) {
225 if (comprtype == this->compr) {
227 spin_unlock(&jffs2_compressor_list_lock);
228 ret = this->decompress(cdata_in, data_out, cdatalen, datalen);
229 spin_lock(&jffs2_compressor_list_lock);
231 printk(KERN_WARNING "Decompressor \"%s\" returned %d\n", this->name, ret);
234 this->stat_decompr_blocks++;
237 spin_unlock(&jffs2_compressor_list_lock);
241 printk(KERN_WARNING "JFFS2 compression type 0x%02x not available.\n", comprtype);
242 spin_unlock(&jffs2_compressor_list_lock);
248 int jffs2_register_compressor(struct jffs2_compressor *comp)
250 struct jffs2_compressor *this;
253 printk(KERN_WARNING "NULL compressor name at registering JFFS2 compressor. Failed.\n");
256 comp->compr_buf_size=0;
257 comp->compr_buf=NULL;
259 comp->stat_compr_orig_size=0;
260 comp->stat_compr_new_size=0;
261 comp->stat_compr_blocks=0;
262 comp->stat_decompr_blocks=0;
263 D1(printk(KERN_DEBUG "Registering JFFS2 compressor \"%s\"\n", comp->name));
265 spin_lock(&jffs2_compressor_list_lock);
267 list_for_each_entry(this, &jffs2_compressor_list, list) {
268 if (this->priority < comp->priority) {
269 list_add(&comp->list, this->list.prev);
273 list_add_tail(&comp->list, &jffs2_compressor_list);
275 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
276 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
279 spin_unlock(&jffs2_compressor_list_lock);
284 int jffs2_unregister_compressor(struct jffs2_compressor *comp)
286 D2(struct jffs2_compressor *this;)
288 D1(printk(KERN_DEBUG "Unregistering JFFS2 compressor \"%s\"\n", comp->name));
290 spin_lock(&jffs2_compressor_list_lock);
292 if (comp->usecount) {
293 spin_unlock(&jffs2_compressor_list_lock);
294 printk(KERN_WARNING "JFFS2: Compressor modul is in use. Unregister failed.\n");
297 list_del(&comp->list);
299 D2(list_for_each_entry(this, &jffs2_compressor_list, list) {
300 printk(KERN_DEBUG "Compressor \"%s\", prio %d\n", this->name, this->priority);
302 spin_unlock(&jffs2_compressor_list_lock);
306 void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
308 if (orig != comprbuf)
312 int __init jffs2_compressors_init(void)
314 /* Registering compressors */
315 #ifdef CONFIG_JFFS2_ZLIB
318 #ifdef CONFIG_JFFS2_RTIME
321 #ifdef CONFIG_JFFS2_RUBIN
322 jffs2_rubinmips_init();
323 jffs2_dynrubin_init();
325 #ifdef CONFIG_JFFS2_LZO
328 /* Setting default compression mode */
329 #ifdef CONFIG_JFFS2_CMODE_NONE
330 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
331 D1(printk(KERN_INFO "JFFS2: default compression mode: none\n");)
333 #ifdef CONFIG_JFFS2_CMODE_SIZE
334 jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
335 D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
337 #ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
338 jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
339 D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
341 D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
348 int jffs2_compressors_exit(void)
350 /* Unregistering compressors */
351 #ifdef CONFIG_JFFS2_LZO
354 #ifdef CONFIG_JFFS2_RUBIN
355 jffs2_dynrubin_exit();
356 jffs2_rubinmips_exit();
358 #ifdef CONFIG_JFFS2_RTIME
361 #ifdef CONFIG_JFFS2_ZLIB